blob: 55b354d6c7f04fce367e4e1a2264bb9d5f022188 [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 Agopiana67932f2011-04-20 14:20:59 -070068void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070069{
Mathias Agopiana67932f2011-04-20 14:20:59 -070070 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070071
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
73 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
74 private:
75 wp<Layer> mLayer;
76 virtual void onFrameAvailable() {
77 sp<Layer> that(mLayer.promote());
78 if (that != 0) {
79 that->onFrameQueued();
80 }
81 }
82 };
83 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
84 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
85 mSurfaceTexture->setSynchronousMode(true);
86 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070087}
88
Mathias Agopiana67932f2011-04-20 14:20:59 -070089Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -070090{
Mathias Agopian47d08122011-08-11 18:18:50 -070091 class MessageDestroyGLState : public MessageBase {
92 GLuint texture;
93 public:
94 MessageDestroyGLState(GLuint texture) : texture(texture) { }
95 virtual bool handler() {
96 glDeleteTextures(1, &texture);
97 return true;
98 }
99 };
100 mFlinger->postMessageAsync( new MessageDestroyGLState(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700101}
102
Mathias Agopiana67932f2011-04-20 14:20:59 -0700103void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700104 android_atomic_inc(&mQueuedFrames);
105 mFlinger->signalEvent();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700106}
107
Mathias Agopiand606de62010-05-10 20:06:11 -0700108// called with SurfaceFlinger::mStateLock as soon as the layer is entered
109// in the purgatory list
110void Layer::onRemoved()
111{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700112 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700113}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700114
Mathias Agopiana67932f2011-04-20 14:20:59 -0700115sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700117 class BSurface : public BnSurface, public LayerCleaner {
118 wp<const Layer> mOwner;
119 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
120 sp<ISurfaceTexture> res;
121 sp<const Layer> that( mOwner.promote() );
122 if (that != NULL) {
123 res = that->mSurfaceTexture;
124 }
125 return res;
126 }
127 public:
128 BSurface(const sp<SurfaceFlinger>& flinger,
129 const sp<Layer>& layer)
130 : LayerCleaner(flinger, layer), mOwner(layer) { }
131 };
132 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800133 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopianf9d93272009-06-19 17:00:27 -0700136status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 PixelFormat format, uint32_t flags)
138{
Mathias Agopian401c2572009-09-23 19:16:27 -0700139 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 PixelFormatInfo info;
141 status_t err = getPixelFormatInfo(format, &info);
142 if (err) return err;
143
Mathias Agopian401c2572009-09-23 19:16:27 -0700144 // the display's pixel format
145 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700146 uint32_t const maxSurfaceDims = min(
147 hw.getMaxTextureSize(), hw.getMaxViewportDims());
148
149 // never allow a surface larger than what our underlying GL implementation
150 // can handle.
151 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
152 return BAD_VALUE;
153 }
154
Mathias Agopian401c2572009-09-23 19:16:27 -0700155 PixelFormatInfo displayInfo;
156 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700157 const uint32_t hwFlags = hw.getFlags();
158
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700159 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700160
Mathias Agopian3330b202009-10-05 17:07:12 -0700161 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800162 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700163 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
164 mCurrentOpacity = getOpacityForFormat(format);
165
166 mSurfaceTexture->setDefaultBufferSize(w, h);
167 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700168
Mathias Agopian401c2572009-09-23 19:16:27 -0700169 // we use the red index
170 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
171 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
172 mNeedsDithering = layerRedsize > displayRedSize;
173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 return NO_ERROR;
175}
176
Mathias Agopiana350ff92010-08-10 17:14:02 -0700177void Layer::setGeometry(hwc_layer_t* hwcl)
178{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700179 LayerBaseClient::setGeometry(hwcl);
180
181 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700182
183 // we can't do alpha-fade with the hwc HAL
184 const State& s(drawingState());
185 if (s.alpha < 0xFF) {
186 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700187 }
188
Mathias Agopian29a367b2011-07-12 14:51:45 -0700189 /*
190 * Transformations are applied in this order:
191 * 1) buffer orientation/flip/mirror
192 * 2) state transformation (window manager)
193 * 3) layer orientation (screen orientation)
194 * (NOTE: the matrices are multiplied in reverse order)
195 */
196
197 const Transform bufferOrientation(mCurrentTransform);
198 const Transform& stateTransform(s.transform);
199 const Transform layerOrientation(mOrientation);
200
201 const Transform tr(layerOrientation * stateTransform * bufferOrientation);
202
203 // this gives us only the "orientation" component of the transform
204 const uint32_t finalTransform = tr.getOrientation();
205
Mathias Agopiana350ff92010-08-10 17:14:02 -0700206 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700207 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700208 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700209 } else {
210 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700211 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212}
213
214void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700215 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700216 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800217 // this can happen if the client never drew into this layer yet,
218 // or if we ran out of memory. In that case, don't let
219 // HWC handle it.
220 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700221 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700222 } else {
223 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700224 }
Mathias Agopianf3450692010-12-08 17:40:28 -0800225
Mathias Agopiana67932f2011-04-20 14:20:59 -0700226 if (isCropped()) {
227 hwcl->sourceCrop.left = mCurrentCrop.left;
228 hwcl->sourceCrop.top = mCurrentCrop.top;
229 hwcl->sourceCrop.right = mCurrentCrop.right;
230 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
Mathias Agopianf3450692010-12-08 17:40:28 -0800231 } else {
232 hwcl->sourceCrop.left = 0;
233 hwcl->sourceCrop.top = 0;
Mathias Agopiane8067a72011-08-02 18:29:51 -0700234 if (buffer != NULL) {
235 hwcl->sourceCrop.right = buffer->width;
236 hwcl->sourceCrop.bottom = buffer->height;
237 } else {
238 hwcl->sourceCrop.right = mTransformedBounds.width();
239 hwcl->sourceCrop.bottom = mTransformedBounds.height();
240 }
Mathias Agopianf3450692010-12-08 17:40:28 -0800241 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700242}
243
Mathias Agopiana67932f2011-04-20 14:20:59 -0700244static inline uint16_t pack565(int r, int g, int b) {
245 return (r<<11)|(g<<5)|b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247void Layer::onDraw(const Region& clip) const
248{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700249 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700251 // in fact never been drawn into. This happens frequently with
252 // SurfaceView because the WindowManager can't know when the client
253 // has drawn the first time.
254
255 // If there is nothing under us, we paint the screen in black, otherwise
256 // we just skip this update.
257
258 // figure out if there is something below us
259 Region under;
260 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
261 const size_t count = drawingLayers.size();
262 for (size_t i=0 ; i<count ; ++i) {
263 const sp<LayerBase>& layer(drawingLayers[i]);
264 if (layer.get() == static_cast<LayerBase const*>(this))
265 break;
266 under.orSelf(layer->visibleRegionScreen);
267 }
268 // if not everything below us is covered, we plug the holes!
269 Region holes(clip.subtract(under));
270 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700271 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700272 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 return;
274 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700275
276 GLenum target = mSurfaceTexture->getCurrentTextureTarget();
277 glBindTexture(target, mTextureName);
278 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
279 // TODO: we could be more subtle with isFixedSize()
280 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
281 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
282 } else {
283 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 }
286 glEnable(target);
287 glMatrixMode(GL_TEXTURE);
288 glLoadMatrixf(mTextureMatrix);
289 glMatrixMode(GL_MODELVIEW);
290
291 drawWithOpenGL(clip);
292
293 glDisable(target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294}
295
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800296// As documented in libhardware header, formats in the range
297// 0x100 - 0x1FF are specific to the HAL implementation, and
298// are known to have no alpha channel
299// TODO: move definition for device-specific range into
300// hardware.h, instead of using hard-coded values here.
301#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
302
Mathias Agopiana67932f2011-04-20 14:20:59 -0700303bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800304{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700305 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
306 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800307 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700308 PixelFormatInfo info;
309 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
310 // in case of error (unknown format), we assume no blending
311 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800312}
313
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800314
Mathias Agopiana67932f2011-04-20 14:20:59 -0700315bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700316{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700317 // if we don't have a buffer yet, we're translucent regardless of the
318 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700319 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700320 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700321 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700322
323 // if the layer has the opaque flag, then we're always opaque,
324 // otherwise we use the current buffer's format.
325 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700326}
327
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800328bool Layer::isProtected() const
329{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700330 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800331 return (activeBuffer != 0) &&
332 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
333}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335uint32_t Layer::doTransaction(uint32_t flags)
336{
337 const Layer::State& front(drawingState());
338 const Layer::State& temp(currentState());
339
Mathias Agopiana138f892010-05-21 17:24:35 -0700340 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
341 (front.requested_h != temp.requested_h);
342
343 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700344 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700346 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700347 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700348 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700349 this,
350 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700351 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700352 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353
Mathias Agopiana138f892010-05-21 17:24:35 -0700354 if (!isFixedSize()) {
355 // we're being resized and there is a freeze display request,
356 // acquire a freeze lock, so that the screen stays put
357 // until we've redrawn at the new size; this is to avoid
358 // glitches upon orientation changes.
359 if (mFlinger->hasFreezeRequest()) {
360 // if the surface is hidden, don't try to acquire the
361 // freeze lock, since hidden surfaces may never redraw
362 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
363 mFreezeLock = mFlinger->getFreezeLock();
364 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700366
367 // this will make sure LayerBase::doTransaction doesn't update
368 // the drawing state's size
369 Layer::State& editDraw(mDrawingState);
370 editDraw.requested_w = temp.requested_w;
371 editDraw.requested_h = temp.requested_h;
372
373 // record the new size, form this point on, when the client request
374 // a buffer, it'll get the new size.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700375 mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 }
377 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 if (temp.sequence != front.sequence) {
380 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
381 // this surface is now hidden, so it shouldn't hold a freeze lock
382 // (it may never redraw, which is fine if it is hidden)
383 mFreezeLock.clear();
384 }
385 }
386
387 return LayerBase::doTransaction(flags);
388}
389
Mathias Agopiana138f892010-05-21 17:24:35 -0700390bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700391 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700392}
393
394bool Layer::isCropped() const {
395 return !mCurrentCrop.isEmpty();
396}
397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398// ----------------------------------------------------------------------------
399// pageflip handling...
400// ----------------------------------------------------------------------------
401
402void Layer::lockPageFlip(bool& recomputeVisibleRegions)
403{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700404 if (mQueuedFrames > 0) {
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700405 const bool oldOpacity = isOpaque();
406
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700407 // signal another event if we have more frames pending
408 if (android_atomic_dec(&mQueuedFrames) > 1) {
409 mFlinger->signalEvent();
410 }
411
Mathias Agopiana67932f2011-04-20 14:20:59 -0700412 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
413 // something happened!
414 recomputeVisibleRegions = true;
415 return;
416 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700417
Mathias Agopiana67932f2011-04-20 14:20:59 -0700418 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
419 mSurfaceTexture->getTransformMatrix(mTextureMatrix);
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700420
Mathias Agopiana67932f2011-04-20 14:20:59 -0700421 const Rect crop(mSurfaceTexture->getCurrentCrop());
422 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700423 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
424 if ((crop != mCurrentCrop) ||
425 (transform != mCurrentTransform) ||
426 (scalingMode != mCurrentScalingMode))
427 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700428 mCurrentCrop = crop;
429 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700430 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700431 mFlinger->invalidateHwcGeometry();
432 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800433
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700434 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
435 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800436 recomputeVisibleRegions = true;
437 }
438
Mathias Agopiana67932f2011-04-20 14:20:59 -0700439 const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
440 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
441 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700442
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700443 // update the layer size and release freeze-lock
444 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700445
446 // FIXME: mPostedDirtyRegion = dirty & bounds
447 mPostedDirtyRegion.set(front.w, front.h);
448
Mathias Agopian97c602c2011-07-19 15:24:46 -0700449
450 if ((front.w != front.requested_w) ||
451 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700452 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700453 // check that we received a buffer of the right size
454 // (Take the buffer's orientation into account)
455 sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
456 uint32_t bufWidth = newFrontBuffer->getWidth();
457 uint32_t bufHeight = newFrontBuffer->getHeight();
458 if (mCurrentTransform & Transform::ROT_90) {
459 swap(bufWidth, bufHeight);
460 }
461
462 if (isFixedSize() ||
463 (bufWidth == front.requested_w &&
464 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700465 {
466 // Here we pretend the transaction happened by updating the
467 // current and drawing states. Drawing state is only accessed
468 // in this thread, no need to have it locked
469 Layer::State& editDraw(mDrawingState);
470 editDraw.w = editDraw.requested_w;
471 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700472
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700473 // We also need to update the current state so that we don't
474 // end-up doing too much work during the next transaction.
475 // NOTE: We actually don't need hold the transaction lock here
476 // because State::w and State::h are only accessed from
477 // this thread
478 Layer::State& editTemp(currentState());
479 editTemp.w = editDraw.w;
480 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700481
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700482 // recompute visible region
483 recomputeVisibleRegions = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700484
Mathias Agopian97c602c2011-07-19 15:24:46 -0700485 // we now have the correct size, unfreeze the screen
486 mFreezeLock.clear();
487 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700488
489 LOGD_IF(DEBUG_RESIZE,
490 "lockPageFlip : "
491 " (layer=%p), buffer (%ux%u, tr=%02x), "
492 "requested (%dx%d)",
493 this,
494 bufWidth, bufHeight, mCurrentTransform,
495 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700496 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700497 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498}
499
500void Layer::unlockPageFlip(
501 const Transform& planeTransform, Region& outDirtyRegion)
502{
503 Region dirtyRegion(mPostedDirtyRegion);
504 if (!dirtyRegion.isEmpty()) {
505 mPostedDirtyRegion.clear();
506 // The dirty region is given in the layer's coordinate space
507 // transform the dirty region by the surface's transformation
508 // and the global transformation.
509 const Layer::State& s(drawingState());
510 const Transform tr(planeTransform * s.transform);
511 dirtyRegion = tr.transform(dirtyRegion);
512
513 // At this point, the dirty region is in screen space.
514 // Make sure it's constrained by the visible region (which
515 // is in screen space as well).
516 dirtyRegion.andSelf(visibleRegionScreen);
517 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800519 if (visibleRegionScreen.isEmpty()) {
520 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700521 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800522 mFreezeLock.clear();
523 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524}
525
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700526void Layer::dump(String8& result, char* buffer, size_t SIZE) const
527{
528 LayerBaseClient::dump(result, buffer, SIZE);
529
Mathias Agopiana67932f2011-04-20 14:20:59 -0700530 sp<const GraphicBuffer> buf0(mActiveBuffer);
531 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700532 if (buf0 != 0) {
533 w0 = buf0->getWidth();
534 h0 = buf0->getHeight();
535 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700536 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700537 }
538 snprintf(buffer, SIZE,
539 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700540 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopiana67932f2011-04-20 14:20:59 -0700541 " freezeLock=%p, queued-frames=%d\n",
542 mFormat, w0, h0, s0,f0,
543 getFreezeLock().get(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700544
545 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700546
Mathias Agopiana67932f2011-04-20 14:20:59 -0700547 if (mSurfaceTexture != 0) {
548 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700549 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700550}
551
Mathias Agopiana67932f2011-04-20 14:20:59 -0700552uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700553{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700554 // TODO: should we do something special if mSecure is set?
555 if (mProtectedByApp) {
556 // need a hardware-protected path to external video sink
557 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700558 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700559 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700560}
561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562// ---------------------------------------------------------------------------
563
564
565}; // namespace android