blob: 32f300fff9530a760a845ef4abaf4a628baa27d2 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
Mathias Agopiana67932f2011-04-20 14:20:59 -070021#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070023#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/StopWatch.h>
28
Mathias Agopian3330b202009-10-05 17:07:12 -070029#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
32#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include "clz.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include "DisplayHardware/DisplayHardware.h"
36#include "DisplayHardware/HWComposer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070037#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include "SurfaceFlinger.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070040#include "SurfaceTextureLayer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
42#define DEBUG_RESIZE 0
43
44
45namespace android {
46
47// ---------------------------------------------------------------------------
48
Mathias Agopian96f08192010-06-02 23:28:45 -070049Layer::Layer(SurfaceFlinger* flinger,
50 DisplayID display, const sp<Client>& client)
51 : LayerBaseClient(flinger, display, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070052 mTextureName(-1U),
53 mQueuedFrames(0),
54 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070055 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopiana67932f2011-04-20 14:20:59 -070056 mCurrentOpacity(true),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080057 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiana67932f2011-04-20 14:20:59 -070059 mOpaqueLayer(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070060 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061 mSecure(false),
Mathias Agopian933389f2011-07-18 16:15:08 -070062 mProtectedByApp(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063{
Mathias Agopiana67932f2011-04-20 14:20:59 -070064 mCurrentCrop.makeInvalid();
65 glGenTextures(1, &mTextureName);
Mathias Agopiand606de62010-05-10 20:06:11 -070066}
67
Mathias Agopianddc31c32011-06-12 18:05:53 -070068void Layer::destroy(RefBase const* base) {
69 mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
Mathias Agopianca4d3602011-05-19 15:38:14 -070070}
71
Mathias Agopiana67932f2011-04-20 14:20:59 -070072void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070073{
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070075 setDestroyer(this);
76
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
78 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
79 private:
80 wp<Layer> mLayer;
81 virtual void onFrameAvailable() {
82 sp<Layer> that(mLayer.promote());
83 if (that != 0) {
84 that->onFrameQueued();
85 }
86 }
87 };
88 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
89 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
90 mSurfaceTexture->setSynchronousMode(true);
91 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070092}
93
Mathias Agopiana67932f2011-04-20 14:20:59 -070094Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -070095{
Mathias Agopiana67932f2011-04-20 14:20:59 -070096 glDeleteTextures(1, &mTextureName);
Mathias Agopian96f08192010-06-02 23:28:45 -070097}
98
Mathias Agopiana67932f2011-04-20 14:20:59 -070099void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700100 android_atomic_inc(&mQueuedFrames);
101 mFlinger->signalEvent();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700102}
103
Mathias Agopiand606de62010-05-10 20:06:11 -0700104// called with SurfaceFlinger::mStateLock as soon as the layer is entered
105// in the purgatory list
106void Layer::onRemoved()
107{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700108 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700109}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700110
Mathias Agopiana67932f2011-04-20 14:20:59 -0700111sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700113 class BSurface : public BnSurface, public LayerCleaner {
114 wp<const Layer> mOwner;
115 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
116 sp<ISurfaceTexture> res;
117 sp<const Layer> that( mOwner.promote() );
118 if (that != NULL) {
119 res = that->mSurfaceTexture;
120 }
121 return res;
122 }
123 public:
124 BSurface(const sp<SurfaceFlinger>& flinger,
125 const sp<Layer>& layer)
126 : LayerCleaner(flinger, layer), mOwner(layer) { }
127 };
128 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800129 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopianf9d93272009-06-19 17:00:27 -0700132status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 PixelFormat format, uint32_t flags)
134{
Mathias Agopian401c2572009-09-23 19:16:27 -0700135 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 PixelFormatInfo info;
137 status_t err = getPixelFormatInfo(format, &info);
138 if (err) return err;
139
Mathias Agopian401c2572009-09-23 19:16:27 -0700140 // the display's pixel format
141 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700142 uint32_t const maxSurfaceDims = min(
143 hw.getMaxTextureSize(), hw.getMaxViewportDims());
144
145 // never allow a surface larger than what our underlying GL implementation
146 // can handle.
147 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
148 return BAD_VALUE;
149 }
150
Mathias Agopian401c2572009-09-23 19:16:27 -0700151 PixelFormatInfo displayInfo;
152 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700153 const uint32_t hwFlags = hw.getFlags();
154
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700155 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700156
Mathias Agopian3330b202009-10-05 17:07:12 -0700157 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800158 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700159 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
160 mCurrentOpacity = getOpacityForFormat(format);
161
162 mSurfaceTexture->setDefaultBufferSize(w, h);
163 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700164
Mathias Agopian401c2572009-09-23 19:16:27 -0700165 // we use the red index
166 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
167 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
168 mNeedsDithering = layerRedsize > displayRedSize;
169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 return NO_ERROR;
171}
172
Mathias Agopiana350ff92010-08-10 17:14:02 -0700173void Layer::setGeometry(hwc_layer_t* hwcl)
174{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700175 LayerBaseClient::setGeometry(hwcl);
176
177 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700178
179 // we can't do alpha-fade with the hwc HAL
180 const State& s(drawingState());
181 if (s.alpha < 0xFF) {
182 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700183 }
184
Mathias Agopian29a367b2011-07-12 14:51:45 -0700185 /*
186 * Transformations are applied in this order:
187 * 1) buffer orientation/flip/mirror
188 * 2) state transformation (window manager)
189 * 3) layer orientation (screen orientation)
190 * (NOTE: the matrices are multiplied in reverse order)
191 */
192
193 const Transform bufferOrientation(mCurrentTransform);
194 const Transform& stateTransform(s.transform);
195 const Transform layerOrientation(mOrientation);
196
197 const Transform tr(layerOrientation * stateTransform * bufferOrientation);
198
199 // this gives us only the "orientation" component of the transform
200 const uint32_t finalTransform = tr.getOrientation();
201
Mathias Agopiana350ff92010-08-10 17:14:02 -0700202 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700203 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700204 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700205 } else {
206 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700208}
209
210void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700211 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800213 // this can happen if the client never drew into this layer yet,
214 // or if we ran out of memory. In that case, don't let
215 // HWC handle it.
216 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700217 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700218 } else {
219 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700220 }
Mathias Agopianf3450692010-12-08 17:40:28 -0800221
Mathias Agopiana67932f2011-04-20 14:20:59 -0700222 if (isCropped()) {
223 hwcl->sourceCrop.left = mCurrentCrop.left;
224 hwcl->sourceCrop.top = mCurrentCrop.top;
225 hwcl->sourceCrop.right = mCurrentCrop.right;
226 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
Mathias Agopianf3450692010-12-08 17:40:28 -0800227 } else {
228 hwcl->sourceCrop.left = 0;
229 hwcl->sourceCrop.top = 0;
230 hwcl->sourceCrop.right = buffer->width;
231 hwcl->sourceCrop.bottom = buffer->height;
232 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700233}
234
Mathias Agopiana67932f2011-04-20 14:20:59 -0700235static inline uint16_t pack565(int r, int g, int b) {
236 return (r<<11)|(g<<5)|b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238void Layer::onDraw(const Region& clip) const
239{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700240 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700242 // in fact never been drawn into. This happens frequently with
243 // SurfaceView because the WindowManager can't know when the client
244 // has drawn the first time.
245
246 // If there is nothing under us, we paint the screen in black, otherwise
247 // we just skip this update.
248
249 // figure out if there is something below us
250 Region under;
251 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
252 const size_t count = drawingLayers.size();
253 for (size_t i=0 ; i<count ; ++i) {
254 const sp<LayerBase>& layer(drawingLayers[i]);
255 if (layer.get() == static_cast<LayerBase const*>(this))
256 break;
257 under.orSelf(layer->visibleRegionScreen);
258 }
259 // if not everything below us is covered, we plug the holes!
260 Region holes(clip.subtract(under));
261 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700262 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700263 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 return;
265 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700266
267 GLenum target = mSurfaceTexture->getCurrentTextureTarget();
268 glBindTexture(target, mTextureName);
269 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
270 // TODO: we could be more subtle with isFixedSize()
271 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
272 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
273 } else {
274 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
275 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
276 }
277 glEnable(target);
278 glMatrixMode(GL_TEXTURE);
279 glLoadMatrixf(mTextureMatrix);
280 glMatrixMode(GL_MODELVIEW);
281
282 drawWithOpenGL(clip);
283
284 glDisable(target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285}
286
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800287// As documented in libhardware header, formats in the range
288// 0x100 - 0x1FF are specific to the HAL implementation, and
289// are known to have no alpha channel
290// TODO: move definition for device-specific range into
291// hardware.h, instead of using hard-coded values here.
292#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
293
Mathias Agopiana67932f2011-04-20 14:20:59 -0700294bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800295{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700296 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
297 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800298 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700299 PixelFormatInfo info;
300 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
301 // in case of error (unknown format), we assume no blending
302 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800303}
304
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800305
Mathias Agopiana67932f2011-04-20 14:20:59 -0700306bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700307{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700308 // if we don't have a buffer yet, we're translucent regardless of the
309 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700310 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700311 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700312 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700313
314 // if the layer has the opaque flag, then we're always opaque,
315 // otherwise we use the current buffer's format.
316 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700317}
318
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800319bool Layer::isProtected() const
320{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700321 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800322 return (activeBuffer != 0) &&
323 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
324}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700325
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326uint32_t Layer::doTransaction(uint32_t flags)
327{
328 const Layer::State& front(drawingState());
329 const Layer::State& temp(currentState());
330
Mathias Agopiana138f892010-05-21 17:24:35 -0700331 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
332 (front.requested_h != temp.requested_h);
333
334 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700335 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700337 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700338 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700339 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700340 this,
341 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700342 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700343 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344
Mathias Agopiana138f892010-05-21 17:24:35 -0700345 if (!isFixedSize()) {
346 // we're being resized and there is a freeze display request,
347 // acquire a freeze lock, so that the screen stays put
348 // until we've redrawn at the new size; this is to avoid
349 // glitches upon orientation changes.
350 if (mFlinger->hasFreezeRequest()) {
351 // if the surface is hidden, don't try to acquire the
352 // freeze lock, since hidden surfaces may never redraw
353 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
354 mFreezeLock = mFlinger->getFreezeLock();
355 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700357
358 // this will make sure LayerBase::doTransaction doesn't update
359 // the drawing state's size
360 Layer::State& editDraw(mDrawingState);
361 editDraw.requested_w = temp.requested_w;
362 editDraw.requested_h = temp.requested_h;
363
364 // record the new size, form this point on, when the client request
365 // a buffer, it'll get the new size.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700366 mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 }
368 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370 if (temp.sequence != front.sequence) {
371 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
372 // this surface is now hidden, so it shouldn't hold a freeze lock
373 // (it may never redraw, which is fine if it is hidden)
374 mFreezeLock.clear();
375 }
376 }
377
378 return LayerBase::doTransaction(flags);
379}
380
Mathias Agopiana138f892010-05-21 17:24:35 -0700381bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700382 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700383}
384
385bool Layer::isCropped() const {
386 return !mCurrentCrop.isEmpty();
387}
388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389// ----------------------------------------------------------------------------
390// pageflip handling...
391// ----------------------------------------------------------------------------
392
393void Layer::lockPageFlip(bool& recomputeVisibleRegions)
394{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700395 if (mQueuedFrames > 0) {
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700396 const bool oldOpacity = isOpaque();
397
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700398 // signal another event if we have more frames pending
399 if (android_atomic_dec(&mQueuedFrames) > 1) {
400 mFlinger->signalEvent();
401 }
402
Mathias Agopiana67932f2011-04-20 14:20:59 -0700403 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
404 // something happened!
405 recomputeVisibleRegions = true;
406 return;
407 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700408
Mathias Agopiana67932f2011-04-20 14:20:59 -0700409 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
410 mSurfaceTexture->getTransformMatrix(mTextureMatrix);
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700411
Mathias Agopiana67932f2011-04-20 14:20:59 -0700412 const Rect crop(mSurfaceTexture->getCurrentCrop());
413 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700414 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
415 if ((crop != mCurrentCrop) ||
416 (transform != mCurrentTransform) ||
417 (scalingMode != mCurrentScalingMode))
418 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700419 mCurrentCrop = crop;
420 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700421 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700422 mFlinger->invalidateHwcGeometry();
423 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800424
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700425 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
426 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800427 recomputeVisibleRegions = true;
428 }
429
Mathias Agopiana67932f2011-04-20 14:20:59 -0700430 const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
431 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
432 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700434 // update the layer size and release freeze-lock
435 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700436
437 // FIXME: mPostedDirtyRegion = dirty & bounds
438 mPostedDirtyRegion.set(front.w, front.h);
439
Mathias Agopian97c602c2011-07-19 15:24:46 -0700440
441 if ((front.w != front.requested_w) ||
442 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700443 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700444 // check that we received a buffer of the right size
445 // (Take the buffer's orientation into account)
446 sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
447 uint32_t bufWidth = newFrontBuffer->getWidth();
448 uint32_t bufHeight = newFrontBuffer->getHeight();
449 if (mCurrentTransform & Transform::ROT_90) {
450 swap(bufWidth, bufHeight);
451 }
452
453 if (isFixedSize() ||
454 (bufWidth == front.requested_w &&
455 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700456 {
457 // Here we pretend the transaction happened by updating the
458 // current and drawing states. Drawing state is only accessed
459 // in this thread, no need to have it locked
460 Layer::State& editDraw(mDrawingState);
461 editDraw.w = editDraw.requested_w;
462 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700463
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700464 // We also need to update the current state so that we don't
465 // end-up doing too much work during the next transaction.
466 // NOTE: We actually don't need hold the transaction lock here
467 // because State::w and State::h are only accessed from
468 // this thread
469 Layer::State& editTemp(currentState());
470 editTemp.w = editDraw.w;
471 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700472
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700473 // recompute visible region
474 recomputeVisibleRegions = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700475
Mathias Agopian97c602c2011-07-19 15:24:46 -0700476 // we now have the correct size, unfreeze the screen
477 mFreezeLock.clear();
478 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700479
480 LOGD_IF(DEBUG_RESIZE,
481 "lockPageFlip : "
482 " (layer=%p), buffer (%ux%u, tr=%02x), "
483 "requested (%dx%d)",
484 this,
485 bufWidth, bufHeight, mCurrentTransform,
486 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700487 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700488 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489}
490
491void Layer::unlockPageFlip(
492 const Transform& planeTransform, Region& outDirtyRegion)
493{
494 Region dirtyRegion(mPostedDirtyRegion);
495 if (!dirtyRegion.isEmpty()) {
496 mPostedDirtyRegion.clear();
497 // The dirty region is given in the layer's coordinate space
498 // transform the dirty region by the surface's transformation
499 // and the global transformation.
500 const Layer::State& s(drawingState());
501 const Transform tr(planeTransform * s.transform);
502 dirtyRegion = tr.transform(dirtyRegion);
503
504 // At this point, the dirty region is in screen space.
505 // Make sure it's constrained by the visible region (which
506 // is in screen space as well).
507 dirtyRegion.andSelf(visibleRegionScreen);
508 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800510 if (visibleRegionScreen.isEmpty()) {
511 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700512 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800513 mFreezeLock.clear();
514 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515}
516
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700517void Layer::dump(String8& result, char* buffer, size_t SIZE) const
518{
519 LayerBaseClient::dump(result, buffer, SIZE);
520
Mathias Agopiana67932f2011-04-20 14:20:59 -0700521 sp<const GraphicBuffer> buf0(mActiveBuffer);
522 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700523 if (buf0 != 0) {
524 w0 = buf0->getWidth();
525 h0 = buf0->getHeight();
526 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700527 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700528 }
529 snprintf(buffer, SIZE,
530 " "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700531 "format=%2d, activeBuffer=[%3ux%3u:%3u,%3u],"
532 " freezeLock=%p, queued-frames=%d\n",
533 mFormat, w0, h0, s0,f0,
534 getFreezeLock().get(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700535
536 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700537
Mathias Agopiana67932f2011-04-20 14:20:59 -0700538 if (mSurfaceTexture != 0) {
539 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700540 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700541}
542
Mathias Agopiana67932f2011-04-20 14:20:59 -0700543uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700544{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700545 // TODO: should we do something special if mSecure is set?
546 if (mProtectedByApp) {
547 // need a hardware-protected path to external video sink
548 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700549 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700550 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700551}
552
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553// ---------------------------------------------------------------------------
554
555
556}; // namespace android