blob: f3b6c4dd23b345e3af1270bace8028b9affb93c0 [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
Mathias Agopianca99fb82010-04-14 16:43:44 -070047template <typename T> inline T min(T a, T b) {
48 return a<b ? a : b;
49}
50
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051// ---------------------------------------------------------------------------
52
Mathias Agopian96f08192010-06-02 23:28:45 -070053Layer::Layer(SurfaceFlinger* flinger,
54 DisplayID display, const sp<Client>& client)
55 : LayerBaseClient(flinger, display, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070056 mTextureName(-1U),
57 mQueuedFrames(0),
58 mCurrentTransform(0),
59 mCurrentOpacity(true),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080060 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiana67932f2011-04-20 14:20:59 -070062 mOpaqueLayer(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070063 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070064 mSecure(false),
Glenn Kasten16f04532011-01-19 15:27:27 -080065 mProtectedByApp(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070066 mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067{
Mathias Agopiana67932f2011-04-20 14:20:59 -070068 mCurrentCrop.makeInvalid();
69 glGenTextures(1, &mTextureName);
Mathias Agopiand606de62010-05-10 20:06:11 -070070}
71
Mathias Agopianddc31c32011-06-12 18:05:53 -070072void Layer::destroy(RefBase const* base) {
73 mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
Mathias Agopianca4d3602011-05-19 15:38:14 -070074}
75
Mathias Agopiana67932f2011-04-20 14:20:59 -070076void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070077{
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070079 setDestroyer(this);
80
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
82 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
83 private:
84 wp<Layer> mLayer;
85 virtual void onFrameAvailable() {
86 sp<Layer> that(mLayer.promote());
87 if (that != 0) {
88 that->onFrameQueued();
89 }
90 }
91 };
92 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
93 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
94 mSurfaceTexture->setSynchronousMode(true);
95 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070096}
97
Mathias Agopiana67932f2011-04-20 14:20:59 -070098Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -070099{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700100 glDeleteTextures(1, &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{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700112}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700113
Mathias Agopiana67932f2011-04-20 14:20:59 -0700114sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700116 class BSurface : public BnSurface, public LayerCleaner {
117 wp<const Layer> mOwner;
118 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
119 sp<ISurfaceTexture> res;
120 sp<const Layer> that( mOwner.promote() );
121 if (that != NULL) {
122 res = that->mSurfaceTexture;
123 }
124 return res;
125 }
126 public:
127 BSurface(const sp<SurfaceFlinger>& flinger,
128 const sp<Layer>& layer)
129 : LayerCleaner(flinger, layer), mOwner(layer) { }
130 };
131 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800132 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133}
134
Mathias Agopianf9d93272009-06-19 17:00:27 -0700135status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 PixelFormat format, uint32_t flags)
137{
Mathias Agopian401c2572009-09-23 19:16:27 -0700138 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 PixelFormatInfo info;
140 status_t err = getPixelFormatInfo(format, &info);
141 if (err) return err;
142
Mathias Agopian401c2572009-09-23 19:16:27 -0700143 // the display's pixel format
144 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700145 uint32_t const maxSurfaceDims = min(
146 hw.getMaxTextureSize(), hw.getMaxViewportDims());
147
148 // never allow a surface larger than what our underlying GL implementation
149 // can handle.
150 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
151 return BAD_VALUE;
152 }
153
Mathias Agopian401c2572009-09-23 19:16:27 -0700154 PixelFormatInfo displayInfo;
155 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700156 const uint32_t hwFlags = hw.getFlags();
157
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700158 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700159
Mathias Agopian3330b202009-10-05 17:07:12 -0700160 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800161 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700162 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
163 mCurrentOpacity = getOpacityForFormat(format);
164
165 mSurfaceTexture->setDefaultBufferSize(w, h);
166 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700167
Mathias Agopian401c2572009-09-23 19:16:27 -0700168 // we use the red index
169 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
170 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
171 mNeedsDithering = layerRedsize > displayRedSize;
172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 return NO_ERROR;
174}
175
Mathias Agopiana350ff92010-08-10 17:14:02 -0700176void Layer::setGeometry(hwc_layer_t* hwcl)
177{
178 hwcl->compositionType = HWC_FRAMEBUFFER;
179 hwcl->hints = 0;
180 hwcl->flags = 0;
181 hwcl->transform = 0;
182 hwcl->blending = HWC_BLENDING_NONE;
183
184 // we can't do alpha-fade with the hwc HAL
185 const State& s(drawingState());
186 if (s.alpha < 0xFF) {
187 hwcl->flags = HWC_SKIP_LAYER;
188 return;
189 }
190
Mathias Agopian29a367b2011-07-12 14:51:45 -0700191 /*
192 * Transformations are applied in this order:
193 * 1) buffer orientation/flip/mirror
194 * 2) state transformation (window manager)
195 * 3) layer orientation (screen orientation)
196 * (NOTE: the matrices are multiplied in reverse order)
197 */
198
199 const Transform bufferOrientation(mCurrentTransform);
200 const Transform& stateTransform(s.transform);
201 const Transform layerOrientation(mOrientation);
202
203 const Transform tr(layerOrientation * stateTransform * bufferOrientation);
204
205 // this gives us only the "orientation" component of the transform
206 const uint32_t finalTransform = tr.getOrientation();
207
Mathias Agopiana350ff92010-08-10 17:14:02 -0700208 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700209 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700210 hwcl->flags = HWC_SKIP_LAYER;
211 return;
212 }
213
Mathias Agopian29a367b2011-07-12 14:51:45 -0700214 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700215
Mathias Agopiana67932f2011-04-20 14:20:59 -0700216 if (!isOpaque()) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700217 hwcl->blending = mPremultipliedAlpha ?
218 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
219 }
220
Mathias Agopian29a367b2011-07-12 14:51:45 -0700221 // scaling is already applied in mTransformedBounds
Mathias Agopiana350ff92010-08-10 17:14:02 -0700222 hwcl->displayFrame.left = mTransformedBounds.left;
223 hwcl->displayFrame.top = mTransformedBounds.top;
224 hwcl->displayFrame.right = mTransformedBounds.right;
225 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
226
227 hwcl->visibleRegionScreen.rects =
228 reinterpret_cast<hwc_rect_t const *>(
229 visibleRegionScreen.getArray(
230 &hwcl->visibleRegionScreen.numRects));
231}
232
233void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700234 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700235 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800236 // this can happen if the client never drew into this layer yet,
237 // or if we ran out of memory. In that case, don't let
238 // HWC handle it.
239 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700240 hwcl->handle = NULL;
241 return;
242 }
Louis Huemiller04048142010-12-01 12:29:36 -0800243 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800244
Mathias Agopiana67932f2011-04-20 14:20:59 -0700245 if (isCropped()) {
246 hwcl->sourceCrop.left = mCurrentCrop.left;
247 hwcl->sourceCrop.top = mCurrentCrop.top;
248 hwcl->sourceCrop.right = mCurrentCrop.right;
249 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
Mathias Agopianf3450692010-12-08 17:40:28 -0800250 } else {
251 hwcl->sourceCrop.left = 0;
252 hwcl->sourceCrop.top = 0;
253 hwcl->sourceCrop.right = buffer->width;
254 hwcl->sourceCrop.bottom = buffer->height;
255 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700256}
257
Mathias Agopiana67932f2011-04-20 14:20:59 -0700258static inline uint16_t pack565(int r, int g, int b) {
259 return (r<<11)|(g<<5)|b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261void Layer::onDraw(const Region& clip) const
262{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700263 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700265 // in fact never been drawn into. This happens frequently with
266 // SurfaceView because the WindowManager can't know when the client
267 // has drawn the first time.
268
269 // If there is nothing under us, we paint the screen in black, otherwise
270 // we just skip this update.
271
272 // figure out if there is something below us
273 Region under;
274 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
275 const size_t count = drawingLayers.size();
276 for (size_t i=0 ; i<count ; ++i) {
277 const sp<LayerBase>& layer(drawingLayers[i]);
278 if (layer.get() == static_cast<LayerBase const*>(this))
279 break;
280 under.orSelf(layer->visibleRegionScreen);
281 }
282 // if not everything below us is covered, we plug the holes!
283 Region holes(clip.subtract(under));
284 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700285 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700286 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 return;
288 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700289
290 GLenum target = mSurfaceTexture->getCurrentTextureTarget();
291 glBindTexture(target, mTextureName);
292 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
293 // TODO: we could be more subtle with isFixedSize()
294 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
295 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
296 } else {
297 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
298 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
299 }
300 glEnable(target);
301 glMatrixMode(GL_TEXTURE);
302 glLoadMatrixf(mTextureMatrix);
303 glMatrixMode(GL_MODELVIEW);
304
305 drawWithOpenGL(clip);
306
307 glDisable(target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308}
309
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800310// As documented in libhardware header, formats in the range
311// 0x100 - 0x1FF are specific to the HAL implementation, and
312// are known to have no alpha channel
313// TODO: move definition for device-specific range into
314// hardware.h, instead of using hard-coded values here.
315#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
316
Mathias Agopiana67932f2011-04-20 14:20:59 -0700317bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800318{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700319 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
320 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800321 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700322 PixelFormatInfo info;
323 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
324 // in case of error (unknown format), we assume no blending
325 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800326}
327
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800328
Mathias Agopiana67932f2011-04-20 14:20:59 -0700329bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700330{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700331 // if we don't have a buffer yet, we're translucent regardless of the
332 // layer's opaque flag.
333 if (mActiveBuffer == 0)
334 return false;
335
336 // if the layer has the opaque flag, then we're always opaque,
337 // otherwise we use the current buffer's format.
338 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700339}
340
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800341bool Layer::isProtected() const
342{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700343 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800344 return (activeBuffer != 0) &&
345 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
346}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348uint32_t Layer::doTransaction(uint32_t flags)
349{
350 const Layer::State& front(drawingState());
351 const Layer::State& temp(currentState());
352
Mathias Agopiana138f892010-05-21 17:24:35 -0700353 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
354 (front.requested_h != temp.requested_h);
355
356 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700357 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana67932f2011-04-20 14:20:59 -0700359 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
360 "fixedSize=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700361 this,
362 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700363 int(front.requested_w), int(front.requested_h),
364 isFixedSize());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365
Mathias Agopiana138f892010-05-21 17:24:35 -0700366 if (!isFixedSize()) {
367 // we're being resized and there is a freeze display request,
368 // acquire a freeze lock, so that the screen stays put
369 // until we've redrawn at the new size; this is to avoid
370 // glitches upon orientation changes.
371 if (mFlinger->hasFreezeRequest()) {
372 // if the surface is hidden, don't try to acquire the
373 // freeze lock, since hidden surfaces may never redraw
374 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
375 mFreezeLock = mFlinger->getFreezeLock();
376 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700378
379 // this will make sure LayerBase::doTransaction doesn't update
380 // the drawing state's size
381 Layer::State& editDraw(mDrawingState);
382 editDraw.requested_w = temp.requested_w;
383 editDraw.requested_h = temp.requested_h;
384
385 // record the new size, form this point on, when the client request
386 // a buffer, it'll get the new size.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700387 mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 }
389 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700390
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 if (temp.sequence != front.sequence) {
392 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
393 // this surface is now hidden, so it shouldn't hold a freeze lock
394 // (it may never redraw, which is fine if it is hidden)
395 mFreezeLock.clear();
396 }
397 }
398
399 return LayerBase::doTransaction(flags);
400}
401
Mathias Agopiana138f892010-05-21 17:24:35 -0700402bool Layer::isFixedSize() const {
403 Mutex::Autolock _l(mLock);
404 return mFixedSize;
405}
406
Mathias Agopiana67932f2011-04-20 14:20:59 -0700407void Layer::setFixedSize(bool fixedSize)
408{
409 Mutex::Autolock _l(mLock);
410 mFixedSize = fixedSize;
411}
412
413bool Layer::isCropped() const {
414 return !mCurrentCrop.isEmpty();
415}
416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417// ----------------------------------------------------------------------------
418// pageflip handling...
419// ----------------------------------------------------------------------------
420
421void Layer::lockPageFlip(bool& recomputeVisibleRegions)
422{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700423 if (mQueuedFrames > 0) {
424 // signal another event if we have more frames pending
425 if (android_atomic_dec(&mQueuedFrames) > 1) {
426 mFlinger->signalEvent();
427 }
428
Mathias Agopiana67932f2011-04-20 14:20:59 -0700429 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
430 // something happened!
431 recomputeVisibleRegions = true;
432 return;
433 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700434
Mathias Agopiana67932f2011-04-20 14:20:59 -0700435 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
436 mSurfaceTexture->getTransformMatrix(mTextureMatrix);
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700437
Mathias Agopiana67932f2011-04-20 14:20:59 -0700438 const Rect crop(mSurfaceTexture->getCurrentCrop());
439 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
440 if ((crop != mCurrentCrop) || (transform != mCurrentTransform)) {
441 mCurrentCrop = crop;
442 mCurrentTransform = transform;
443 mFlinger->invalidateHwcGeometry();
444 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800445
Mathias Agopiana67932f2011-04-20 14:20:59 -0700446 const bool opacity(getOpacityForFormat(mActiveBuffer->format));
447 if (opacity != mCurrentOpacity) {
448 mCurrentOpacity = opacity;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800449 recomputeVisibleRegions = true;
450 }
451
Mathias Agopiana67932f2011-04-20 14:20:59 -0700452 const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
453 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
454 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700455
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700456 // update the layer size and release freeze-lock
457 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700458
459 // FIXME: mPostedDirtyRegion = dirty & bounds
460 mPostedDirtyRegion.set(front.w, front.h);
461
462 sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
Jamie Gennis3629d7f2011-05-16 16:55:03 -0700463 if ((newFrontBuffer->getWidth() == front.requested_w &&
464 newFrontBuffer->getHeight() == front.requested_h) ||
465 isFixedSize())
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700466 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700467 if ((front.w != front.requested_w) ||
468 (front.h != front.requested_h))
469 {
470 // Here we pretend the transaction happened by updating the
471 // current and drawing states. Drawing state is only accessed
472 // in this thread, no need to have it locked
473 Layer::State& editDraw(mDrawingState);
474 editDraw.w = editDraw.requested_w;
475 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700476
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700477 // We also need to update the current state so that we don't
478 // end-up doing too much work during the next transaction.
479 // NOTE: We actually don't need hold the transaction lock here
480 // because State::w and State::h are only accessed from
481 // this thread
482 Layer::State& editTemp(currentState());
483 editTemp.w = editDraw.w;
484 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700485
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700486 // recompute visible region
487 recomputeVisibleRegions = true;
488 }
489
490 // we now have the correct size, unfreeze the screen
491 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700492 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700493 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494}
495
496void Layer::unlockPageFlip(
497 const Transform& planeTransform, Region& outDirtyRegion)
498{
499 Region dirtyRegion(mPostedDirtyRegion);
500 if (!dirtyRegion.isEmpty()) {
501 mPostedDirtyRegion.clear();
502 // The dirty region is given in the layer's coordinate space
503 // transform the dirty region by the surface's transformation
504 // and the global transformation.
505 const Layer::State& s(drawingState());
506 const Transform tr(planeTransform * s.transform);
507 dirtyRegion = tr.transform(dirtyRegion);
508
509 // At this point, the dirty region is in screen space.
510 // Make sure it's constrained by the visible region (which
511 // is in screen space as well).
512 dirtyRegion.andSelf(visibleRegionScreen);
513 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800515 if (visibleRegionScreen.isEmpty()) {
516 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700517 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800518 mFreezeLock.clear();
519 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520}
521
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700522void Layer::dump(String8& result, char* buffer, size_t SIZE) const
523{
524 LayerBaseClient::dump(result, buffer, SIZE);
525
Mathias Agopiana67932f2011-04-20 14:20:59 -0700526 sp<const GraphicBuffer> buf0(mActiveBuffer);
527 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700528 if (buf0 != 0) {
529 w0 = buf0->getWidth();
530 h0 = buf0->getHeight();
531 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700532 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700533 }
534 snprintf(buffer, SIZE,
535 " "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700536 "format=%2d, activeBuffer=[%3ux%3u:%3u,%3u],"
537 " freezeLock=%p, queued-frames=%d\n",
538 mFormat, w0, h0, s0,f0,
539 getFreezeLock().get(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700540
541 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700542
Mathias Agopiana67932f2011-04-20 14:20:59 -0700543 if (mSurfaceTexture != 0) {
544 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700545 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700546}
547
Mathias Agopiana67932f2011-04-20 14:20:59 -0700548uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700549{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700550 // TODO: should we do something special if mSecure is set?
551 if (mProtectedByApp) {
552 // need a hardware-protected path to external video sink
553 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700554 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700555 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700556}
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558// ---------------------------------------------------------------------------
559
560
561}; // namespace android