blob: 19c7ddd4fbabd4e6a1ccb636038bbf9f0ef78afa [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
Mathias Agopian7bb843c2011-04-20 14:20:59 -070021#include <cutils/compiler.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
Mathias Agopian7bb843c2011-04-20 14:20:59 -070023#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/StopWatch.h>
28
Mathias Agopian6950e422009-10-05 17:07:12 -070029#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080031
32#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34#include "clz.h"
Mathias Agopian7bb843c2011-04-20 14:20:59 -070035#include "DisplayHardware/DisplayHardware.h"
36#include "DisplayHardware/HWComposer.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070037#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include "SurfaceFlinger.h"
Mathias Agopian7bb843c2011-04-20 14:20:59 -070040#include "SurfaceTextureLayer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42#define DEBUG_RESIZE 0
43
44
45namespace android {
46
47// ---------------------------------------------------------------------------
48
Mathias Agopian593c05c2010-06-02 23:28:45 -070049Layer::Layer(SurfaceFlinger* flinger,
50 DisplayID display, const sp<Client>& client)
51 : LayerBaseClient(flinger, display, client),
Mathias Agopian7bb843c2011-04-20 14:20:59 -070052 mTextureName(-1U),
53 mQueuedFrames(0),
54 mCurrentTransform(0),
Mathias Agopianff86f372011-07-18 16:15:08 -070055 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopian7bb843c2011-04-20 14:20:59 -070056 mCurrentOpacity(true),
Mathias Agopianccf94df2011-03-11 17:01:07 -080057 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian781953d2010-06-25 18:02:21 -070058 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian7bb843c2011-04-20 14:20:59 -070059 mOpaqueLayer(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070060 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070061 mSecure(false),
Mathias Agopianff86f372011-07-18 16:15:08 -070062 mProtectedByApp(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063{
Mathias Agopian7bb843c2011-04-20 14:20:59 -070064 mCurrentCrop.makeInvalid();
65 glGenTextures(1, &mTextureName);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070066}
67
Mathias Agopian7bb843c2011-04-20 14:20:59 -070068void Layer::onFirstRef()
Mathias Agopian593c05c2010-06-02 23:28:45 -070069{
Mathias Agopian7bb843c2011-04-20 14:20:59 -070070 LayerBaseClient::onFirstRef();
Mathias Agopianeb99f0d2011-06-12 18:05:53 -070071
Mathias Agopian7bb843c2011-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 Agopian7623da42010-06-01 15:12:58 -070087}
88
Mathias Agopian7bb843c2011-04-20 14:20:59 -070089Layer::~Layer()
Mathias Agopian7623da42010-06-01 15:12:58 -070090{
Mathias Agopian91b53982011-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 Agopian593c05c2010-06-02 23:28:45 -0700101}
102
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700103void Layer::onFrameQueued() {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700104 android_atomic_inc(&mQueuedFrames);
105 mFlinger->signalEvent();
Mathias Agopian5e140102010-06-08 19:54:15 -0700106}
107
Mathias Agopian9f2c4fd2010-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 Gennisa616d882011-07-30 14:33:49 -0700112 mSurfaceTexture->abandon();
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700113}
Mathias Agopian9779b222009-09-07 16:32:45 -0700114
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700115sp<ISurface> Layer::createSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116{
Mathias Agopian7bb843c2011-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 Agopian1b0114f2011-02-15 19:01:06 -0800133 return sur;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134}
135
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700136wp<IBinder> Layer::getSurfaceTextureBinder() const
137{
138 return mSurfaceTexture->asBinder();
139}
140
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700141status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 PixelFormat format, uint32_t flags)
143{
Mathias Agopiancc934762009-09-23 19:16:27 -0700144 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 PixelFormatInfo info;
146 status_t err = getPixelFormatInfo(format, &info);
147 if (err) return err;
148
Mathias Agopiancc934762009-09-23 19:16:27 -0700149 // the display's pixel format
150 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-04-14 16:43:44 -0700151 uint32_t const maxSurfaceDims = min(
152 hw.getMaxTextureSize(), hw.getMaxViewportDims());
153
154 // never allow a surface larger than what our underlying GL implementation
155 // can handle.
156 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
157 return BAD_VALUE;
158 }
159
Mathias Agopiancc934762009-09-23 19:16:27 -0700160 PixelFormatInfo displayInfo;
161 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700162 const uint32_t hwFlags = hw.getFlags();
163
Mathias Agopian9779b222009-09-07 16:32:45 -0700164 mFormat = format;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700165
Mathias Agopian6950e422009-10-05 17:07:12 -0700166 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800167 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700168 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
169 mCurrentOpacity = getOpacityForFormat(format);
170
171 mSurfaceTexture->setDefaultBufferSize(w, h);
172 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopian967dce32010-04-14 16:43:44 -0700173
Mathias Agopiancc934762009-09-23 19:16:27 -0700174 // we use the red index
175 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
176 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
177 mNeedsDithering = layerRedsize > displayRedSize;
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 return NO_ERROR;
180}
181
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700182void Layer::setGeometry(hwc_layer_t* hwcl)
183{
Mathias Agopian0fb1a942011-08-02 15:51:37 -0700184 LayerBaseClient::setGeometry(hwcl);
185
186 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700187
188 // we can't do alpha-fade with the hwc HAL
189 const State& s(drawingState());
190 if (s.alpha < 0xFF) {
191 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700192 }
193
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700194 /*
195 * Transformations are applied in this order:
196 * 1) buffer orientation/flip/mirror
197 * 2) state transformation (window manager)
198 * 3) layer orientation (screen orientation)
Mathias Agopian3f883272011-08-18 18:31:00 -0700199 * mTransform is already the composition of (2) and (3)
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700200 * (NOTE: the matrices are multiplied in reverse order)
201 */
202
203 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopian3f883272011-08-18 18:31:00 -0700204 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700205
206 // this gives us only the "orientation" component of the transform
207 const uint32_t finalTransform = tr.getOrientation();
208
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700209 // we can only handle simple transformation
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700210 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700211 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopian0fb1a942011-08-02 15:51:37 -0700212 } else {
213 hwcl->transform = finalTransform;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700214 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700215}
216
217void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700218 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700219 if (buffer == NULL) {
Mathias Agopian575eaf52010-12-13 18:51:59 -0800220 // this can happen if the client never drew into this layer yet,
221 // or if we ran out of memory. In that case, don't let
222 // HWC handle it.
223 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700224 hwcl->handle = NULL;
Mathias Agopian0fb1a942011-08-02 15:51:37 -0700225 } else {
226 hwcl->handle = buffer->handle;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700227 }
Mathias Agopianac843f22010-12-08 17:40:28 -0800228
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700229 if (isCropped()) {
230 hwcl->sourceCrop.left = mCurrentCrop.left;
231 hwcl->sourceCrop.top = mCurrentCrop.top;
232 hwcl->sourceCrop.right = mCurrentCrop.right;
233 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
Mathias Agopianac843f22010-12-08 17:40:28 -0800234 } else {
235 hwcl->sourceCrop.left = 0;
236 hwcl->sourceCrop.top = 0;
Mathias Agopianb17ee7f2011-08-02 18:29:51 -0700237 if (buffer != NULL) {
238 hwcl->sourceCrop.right = buffer->width;
239 hwcl->sourceCrop.bottom = buffer->height;
240 } else {
241 hwcl->sourceCrop.right = mTransformedBounds.width();
242 hwcl->sourceCrop.bottom = mTransformedBounds.height();
243 }
Mathias Agopianac843f22010-12-08 17:40:28 -0800244 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700245}
246
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700247static inline uint16_t pack565(int r, int g, int b) {
248 return (r<<11)|(g<<5)|b;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250void Layer::onDraw(const Region& clip) const
251{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700252 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700254 // in fact never been drawn into. This happens frequently with
255 // SurfaceView because the WindowManager can't know when the client
256 // has drawn the first time.
257
258 // If there is nothing under us, we paint the screen in black, otherwise
259 // we just skip this update.
260
261 // figure out if there is something below us
262 Region under;
263 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
264 const size_t count = drawingLayers.size();
265 for (size_t i=0 ; i<count ; ++i) {
266 const sp<LayerBase>& layer(drawingLayers[i]);
267 if (layer.get() == static_cast<LayerBase const*>(this))
268 break;
269 under.orSelf(layer->visibleRegionScreen);
270 }
271 // if not everything below us is covered, we plug the holes!
272 Region holes(clip.subtract(under));
273 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700274 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 return;
277 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700278
279 GLenum target = mSurfaceTexture->getCurrentTextureTarget();
280 glBindTexture(target, mTextureName);
281 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
282 // TODO: we could be more subtle with isFixedSize()
283 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
284 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
285 } else {
286 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
287 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
288 }
289 glEnable(target);
290 glMatrixMode(GL_TEXTURE);
291 glLoadMatrixf(mTextureMatrix);
292 glMatrixMode(GL_MODELVIEW);
293
294 drawWithOpenGL(clip);
295
296 glDisable(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297}
298
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800299// As documented in libhardware header, formats in the range
300// 0x100 - 0x1FF are specific to the HAL implementation, and
301// are known to have no alpha channel
302// TODO: move definition for device-specific range into
303// hardware.h, instead of using hard-coded values here.
304#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
305
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700306bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800307{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700308 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
309 return true;
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800310 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700311 PixelFormatInfo info;
312 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
313 // in case of error (unknown format), we assume no blending
314 return (err || info.h_alpha <= info.l_alpha);
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800315}
316
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800317
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700318bool Layer::isOpaque() const
Mathias Agopian92377032010-05-26 22:08:52 -0700319{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700320 // if we don't have a buffer yet, we're translucent regardless of the
321 // layer's opaque flag.
Jamie Gennisc0545702011-07-28 14:54:07 -0700322 if (mActiveBuffer == 0) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700323 return false;
Jamie Gennisc0545702011-07-28 14:54:07 -0700324 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700325
326 // if the layer has the opaque flag, then we're always opaque,
327 // otherwise we use the current buffer's format.
328 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopian92377032010-05-26 22:08:52 -0700329}
330
Jamie Gennisf72606c2011-03-09 17:05:02 -0800331bool Layer::isProtected() const
332{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700333 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennisf72606c2011-03-09 17:05:02 -0800334 return (activeBuffer != 0) &&
335 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
336}
Mathias Agopian59751db2010-05-07 15:58:44 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338uint32_t Layer::doTransaction(uint32_t flags)
339{
340 const Layer::State& front(drawingState());
341 const Layer::State& temp(currentState());
342
Mathias Agopian2be352a2010-05-21 17:24:35 -0700343 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
344 (front.requested_h != temp.requested_h);
345
346 if (sizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700347 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 LOGD_IF(DEBUG_RESIZE,
Mathias Agopianf3503c22011-07-25 19:56:08 -0700349 "doTransaction: "
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700350 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopianf3503c22011-07-25 19:56:08 -0700351 "scalingMode=%d",
Mathias Agopian2be352a2010-05-21 17:24:35 -0700352 this,
353 int(temp.requested_w), int(temp.requested_h),
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700354 int(front.requested_w), int(front.requested_h),
Mathias Agopianf3503c22011-07-25 19:56:08 -0700355 mCurrentScalingMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356
Mathias Agopian2be352a2010-05-21 17:24:35 -0700357 if (!isFixedSize()) {
358 // we're being resized and there is a freeze display request,
359 // acquire a freeze lock, so that the screen stays put
360 // until we've redrawn at the new size; this is to avoid
361 // glitches upon orientation changes.
362 if (mFlinger->hasFreezeRequest()) {
363 // if the surface is hidden, don't try to acquire the
364 // freeze lock, since hidden surfaces may never redraw
365 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
366 mFreezeLock = mFlinger->getFreezeLock();
367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700369
370 // this will make sure LayerBase::doTransaction doesn't update
371 // the drawing state's size
372 Layer::State& editDraw(mDrawingState);
373 editDraw.requested_w = temp.requested_w;
374 editDraw.requested_h = temp.requested_h;
375
376 // record the new size, form this point on, when the client request
377 // a buffer, it'll get the new size.
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700378 mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
380 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 if (temp.sequence != front.sequence) {
383 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
384 // this surface is now hidden, so it shouldn't hold a freeze lock
385 // (it may never redraw, which is fine if it is hidden)
386 mFreezeLock.clear();
387 }
388 }
389
390 return LayerBase::doTransaction(flags);
391}
392
Mathias Agopian2be352a2010-05-21 17:24:35 -0700393bool Layer::isFixedSize() const {
Mathias Agopianff86f372011-07-18 16:15:08 -0700394 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700395}
396
397bool Layer::isCropped() const {
398 return !mCurrentCrop.isEmpty();
399}
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401// ----------------------------------------------------------------------------
402// pageflip handling...
403// ----------------------------------------------------------------------------
404
405void Layer::lockPageFlip(bool& recomputeVisibleRegions)
406{
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700407 if (mQueuedFrames > 0) {
Jamie Gennisc0545702011-07-28 14:54:07 -0700408 const bool oldOpacity = isOpaque();
409
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700410 // signal another event if we have more frames pending
411 if (android_atomic_dec(&mQueuedFrames) > 1) {
412 mFlinger->signalEvent();
413 }
414
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700415 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
416 // something happened!
417 recomputeVisibleRegions = true;
418 return;
419 }
Mathias Agopian593c05c2010-06-02 23:28:45 -0700420
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700421 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
422 mSurfaceTexture->getTransformMatrix(mTextureMatrix);
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700423
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700424 const Rect crop(mSurfaceTexture->getCurrentCrop());
425 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopianff86f372011-07-18 16:15:08 -0700426 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
427 if ((crop != mCurrentCrop) ||
428 (transform != mCurrentTransform) ||
429 (scalingMode != mCurrentScalingMode))
430 {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700431 mCurrentCrop = crop;
432 mCurrentTransform = transform;
Mathias Agopianff86f372011-07-18 16:15:08 -0700433 mCurrentScalingMode = scalingMode;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700434 mFlinger->invalidateHwcGeometry();
435 }
Mathias Agopian575eaf52010-12-13 18:51:59 -0800436
Jamie Gennisc0545702011-07-28 14:54:07 -0700437 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
438 if (oldOpacity != isOpaque()) {
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800439 recomputeVisibleRegions = true;
440 }
441
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700442 const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
443 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
444 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian1473f462009-04-10 14:24:30 -0700445
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700446 // update the layer size and release freeze-lock
447 const Layer::State& front(drawingState());
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700448
449 // FIXME: mPostedDirtyRegion = dirty & bounds
450 mPostedDirtyRegion.set(front.w, front.h);
451
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700452
453 if ((front.w != front.requested_w) ||
454 (front.h != front.requested_h))
Mathias Agopianbd23e302009-09-30 14:07:22 -0700455 {
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700456 // check that we received a buffer of the right size
457 // (Take the buffer's orientation into account)
458 sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
459 uint32_t bufWidth = newFrontBuffer->getWidth();
460 uint32_t bufHeight = newFrontBuffer->getHeight();
461 if (mCurrentTransform & Transform::ROT_90) {
462 swap(bufWidth, bufHeight);
463 }
464
465 if (isFixedSize() ||
466 (bufWidth == front.requested_w &&
467 bufHeight == front.requested_h))
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700468 {
469 // Here we pretend the transaction happened by updating the
470 // current and drawing states. Drawing state is only accessed
471 // in this thread, no need to have it locked
472 Layer::State& editDraw(mDrawingState);
473 editDraw.w = editDraw.requested_w;
474 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700475
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700476 // We also need to update the current state so that we don't
477 // end-up doing too much work during the next transaction.
478 // NOTE: We actually don't need hold the transaction lock here
479 // because State::w and State::h are only accessed from
480 // this thread
481 Layer::State& editTemp(currentState());
482 editTemp.w = editDraw.w;
483 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700484
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700485 // recompute visible region
486 recomputeVisibleRegions = true;
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700487
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700488 // we now have the correct size, unfreeze the screen
489 mFreezeLock.clear();
490 }
Mathias Agopianf3503c22011-07-25 19:56:08 -0700491
492 LOGD_IF(DEBUG_RESIZE,
493 "lockPageFlip : "
494 " (layer=%p), buffer (%ux%u, tr=%02x), "
495 "requested (%dx%d)",
496 this,
497 bufWidth, bufHeight, mCurrentTransform,
498 front.requested_w, front.requested_h);
Mathias Agopianbd23e302009-09-30 14:07:22 -0700499 }
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501}
502
503void Layer::unlockPageFlip(
504 const Transform& planeTransform, Region& outDirtyRegion)
505{
506 Region dirtyRegion(mPostedDirtyRegion);
507 if (!dirtyRegion.isEmpty()) {
508 mPostedDirtyRegion.clear();
509 // The dirty region is given in the layer's coordinate space
510 // transform the dirty region by the surface's transformation
511 // and the global transformation.
512 const Layer::State& s(drawingState());
513 const Transform tr(planeTransform * s.transform);
514 dirtyRegion = tr.transform(dirtyRegion);
515
516 // At this point, the dirty region is in screen space.
517 // Make sure it's constrained by the visible region (which
518 // is in screen space as well).
519 dirtyRegion.andSelf(visibleRegionScreen);
520 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800522 if (visibleRegionScreen.isEmpty()) {
523 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700524 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800525 mFreezeLock.clear();
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527}
528
Mathias Agopian9bce8732010-04-20 17:55:49 -0700529void Layer::dump(String8& result, char* buffer, size_t SIZE) const
530{
531 LayerBaseClient::dump(result, buffer, SIZE);
532
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700533 sp<const GraphicBuffer> buf0(mActiveBuffer);
534 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian9bce8732010-04-20 17:55:49 -0700535 if (buf0 != 0) {
536 w0 = buf0->getWidth();
537 h0 = buf0->getHeight();
538 s0 = buf0->getStride();
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700539 f0 = buf0->format;
Mathias Agopian9bce8732010-04-20 17:55:49 -0700540 }
541 snprintf(buffer, SIZE,
542 " "
Mathias Agopian0c3367f2011-08-08 16:02:13 -0700543 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700544 " freezeLock=%p, queued-frames=%d\n",
545 mFormat, w0, h0, s0,f0,
546 getFreezeLock().get(), mQueuedFrames);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700547
548 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700549
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700550 if (mSurfaceTexture != 0) {
551 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian5e140102010-06-08 19:54:15 -0700552 }
Mathias Agopian7623da42010-06-01 15:12:58 -0700553}
554
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700555uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopian7623da42010-06-01 15:12:58 -0700556{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700557 // TODO: should we do something special if mSecure is set?
558 if (mProtectedByApp) {
559 // need a hardware-protected path to external video sink
560 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis6c925d02010-11-02 11:51:32 -0700561 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700562 return usage;
Mathias Agopian59751db2010-05-07 15:58:44 -0700563}
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565// ---------------------------------------------------------------------------
566
567
568}; // namespace android