blob: aa16d23dd8292e74d5fb4827a3e6157cf77dbc44 [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
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700115void Layer::setName(const String8& name) {
116 LayerBase::setName(name);
117 mSurfaceTexture->setName(name);
118}
119
Mathias Agopiana67932f2011-04-20 14:20:59 -0700120sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700122 class BSurface : public BnSurface, public LayerCleaner {
123 wp<const Layer> mOwner;
124 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
125 sp<ISurfaceTexture> res;
126 sp<const Layer> that( mOwner.promote() );
127 if (that != NULL) {
128 res = that->mSurfaceTexture;
129 }
130 return res;
131 }
132 public:
133 BSurface(const sp<SurfaceFlinger>& flinger,
134 const sp<Layer>& layer)
135 : LayerCleaner(flinger, layer), mOwner(layer) { }
136 };
137 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800138 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Jamie Gennis582270d2011-08-17 18:19:00 -0700141wp<IBinder> Layer::getSurfaceTextureBinder() const
142{
143 return mSurfaceTexture->asBinder();
144}
145
Mathias Agopianf9d93272009-06-19 17:00:27 -0700146status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 PixelFormat format, uint32_t flags)
148{
Mathias Agopian401c2572009-09-23 19:16:27 -0700149 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 PixelFormatInfo info;
151 status_t err = getPixelFormatInfo(format, &info);
152 if (err) return err;
153
Mathias Agopian401c2572009-09-23 19:16:27 -0700154 // the display's pixel format
155 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700156 uint32_t const maxSurfaceDims = min(
157 hw.getMaxTextureSize(), hw.getMaxViewportDims());
158
159 // never allow a surface larger than what our underlying GL implementation
160 // can handle.
161 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
162 return BAD_VALUE;
163 }
164
Mathias Agopian401c2572009-09-23 19:16:27 -0700165 PixelFormatInfo displayInfo;
166 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700167 const uint32_t hwFlags = hw.getFlags();
168
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700169 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700170
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800172 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700173 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
174 mCurrentOpacity = getOpacityForFormat(format);
175
176 mSurfaceTexture->setDefaultBufferSize(w, h);
177 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700178
Mathias Agopian401c2572009-09-23 19:16:27 -0700179 // we use the red index
180 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
181 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
182 mNeedsDithering = layerRedsize > displayRedSize;
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 return NO_ERROR;
185}
186
Mathias Agopiana350ff92010-08-10 17:14:02 -0700187void Layer::setGeometry(hwc_layer_t* hwcl)
188{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700189 LayerBaseClient::setGeometry(hwcl);
190
191 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700192
193 // we can't do alpha-fade with the hwc HAL
194 const State& s(drawingState());
195 if (s.alpha < 0xFF) {
196 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700197 }
198
Mathias Agopian29a367b2011-07-12 14:51:45 -0700199 /*
200 * Transformations are applied in this order:
201 * 1) buffer orientation/flip/mirror
202 * 2) state transformation (window manager)
203 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700204 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700205 * (NOTE: the matrices are multiplied in reverse order)
206 */
207
208 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700209 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700210
211 // this gives us only the "orientation" component of the transform
212 const uint32_t finalTransform = tr.getOrientation();
213
Mathias Agopiana350ff92010-08-10 17:14:02 -0700214 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700215 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700216 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700217 } else {
218 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700219 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700220
221 if (isCropped()) {
222 hwcl->sourceCrop.left = mCurrentCrop.left;
223 hwcl->sourceCrop.top = mCurrentCrop.top;
224 hwcl->sourceCrop.right = mCurrentCrop.right;
225 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
226 } else {
227 const sp<GraphicBuffer>& buffer(mActiveBuffer);
228 hwcl->sourceCrop.left = 0;
229 hwcl->sourceCrop.top = 0;
230 if (buffer != NULL) {
231 hwcl->sourceCrop.right = buffer->width;
232 hwcl->sourceCrop.bottom = buffer->height;
233 } else {
234 hwcl->sourceCrop.right = mTransformedBounds.width();
235 hwcl->sourceCrop.bottom = mTransformedBounds.height();
236 }
237 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700238}
239
240void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700241 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700242 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800243 // this can happen if the client never drew into this layer yet,
244 // or if we ran out of memory. In that case, don't let
245 // HWC handle it.
246 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700247 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700248 } else {
249 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700250 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700251}
252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253void Layer::onDraw(const Region& clip) const
254{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700255 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700257 // in fact never been drawn into. This happens frequently with
258 // SurfaceView because the WindowManager can't know when the client
259 // has drawn the first time.
260
261 // If there is nothing under us, we paint the screen in black, otherwise
262 // we just skip this update.
263
264 // figure out if there is something below us
265 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700266 const SurfaceFlinger::LayerVector& drawingLayers(
267 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700268 const size_t count = drawingLayers.size();
269 for (size_t i=0 ; i<count ; ++i) {
270 const sp<LayerBase>& layer(drawingLayers[i]);
271 if (layer.get() == static_cast<LayerBase const*>(this))
272 break;
273 under.orSelf(layer->visibleRegionScreen);
274 }
275 // if not everything below us is covered, we plug the holes!
276 Region holes(clip.subtract(under));
277 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700278 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700279 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 return;
281 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700282
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700283 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700284 glBindTexture(target, mTextureName);
285 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
286 // TODO: we could be more subtle with isFixedSize()
287 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
288 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
289 } else {
290 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
291 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 }
293 glEnable(target);
294 glMatrixMode(GL_TEXTURE);
295 glLoadMatrixf(mTextureMatrix);
296 glMatrixMode(GL_MODELVIEW);
297
298 drawWithOpenGL(clip);
299
300 glDisable(target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301}
302
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800303// As documented in libhardware header, formats in the range
304// 0x100 - 0x1FF are specific to the HAL implementation, and
305// are known to have no alpha channel
306// TODO: move definition for device-specific range into
307// hardware.h, instead of using hard-coded values here.
308#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
309
Mathias Agopiana67932f2011-04-20 14:20:59 -0700310bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800311{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700312 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
313 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800314 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700315 PixelFormatInfo info;
316 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
317 // in case of error (unknown format), we assume no blending
318 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800319}
320
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800321
Mathias Agopiana67932f2011-04-20 14:20:59 -0700322bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700323{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700324 // if we don't have a buffer yet, we're translucent regardless of the
325 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700326 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700327 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700328 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700329
330 // if the layer has the opaque flag, then we're always opaque,
331 // otherwise we use the current buffer's format.
332 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700333}
334
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800335bool Layer::isProtected() const
336{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700337 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800338 return (activeBuffer != 0) &&
339 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
340}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342uint32_t Layer::doTransaction(uint32_t flags)
343{
344 const Layer::State& front(drawingState());
345 const Layer::State& temp(currentState());
346
Mathias Agopiana138f892010-05-21 17:24:35 -0700347 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
348 (front.requested_h != temp.requested_h);
349
350 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700351 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700353 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700354 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700355 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700356 this,
357 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700358 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700359 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360
Mathias Agopiana138f892010-05-21 17:24:35 -0700361 if (!isFixedSize()) {
362 // we're being resized and there is a freeze display request,
363 // acquire a freeze lock, so that the screen stays put
364 // until we've redrawn at the new size; this is to avoid
365 // glitches upon orientation changes.
366 if (mFlinger->hasFreezeRequest()) {
367 // if the surface is hidden, don't try to acquire the
368 // freeze lock, since hidden surfaces may never redraw
369 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
370 mFreezeLock = mFlinger->getFreezeLock();
371 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700373
374 // this will make sure LayerBase::doTransaction doesn't update
375 // the drawing state's size
376 Layer::State& editDraw(mDrawingState);
377 editDraw.requested_w = temp.requested_w;
378 editDraw.requested_h = temp.requested_h;
379
380 // record the new size, form this point on, when the client request
381 // a buffer, it'll get the new size.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382 mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 }
384 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 if (temp.sequence != front.sequence) {
387 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
388 // this surface is now hidden, so it shouldn't hold a freeze lock
389 // (it may never redraw, which is fine if it is hidden)
390 mFreezeLock.clear();
391 }
392 }
393
394 return LayerBase::doTransaction(flags);
395}
396
Mathias Agopiana138f892010-05-21 17:24:35 -0700397bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700398 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700399}
400
401bool Layer::isCropped() const {
402 return !mCurrentCrop.isEmpty();
403}
404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405// ----------------------------------------------------------------------------
406// pageflip handling...
407// ----------------------------------------------------------------------------
408
409void Layer::lockPageFlip(bool& recomputeVisibleRegions)
410{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700411 if (mQueuedFrames > 0) {
Jamie Gennis945d2912011-09-14 18:23:37 -0700412 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700413 const bool oldOpacity = isOpaque();
Jamie Gennis945d2912011-09-14 18:23:37 -0700414 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700415
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700416 // signal another event if we have more frames pending
417 if (android_atomic_dec(&mQueuedFrames) > 1) {
418 mFlinger->signalEvent();
419 }
420
Mathias Agopiana67932f2011-04-20 14:20:59 -0700421 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
422 // something happened!
423 recomputeVisibleRegions = true;
424 return;
425 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700426
Jamie Gennis945d2912011-09-14 18:23:37 -0700427 // update the active buffer
428 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700429
Mathias Agopiana67932f2011-04-20 14:20:59 -0700430 const Rect crop(mSurfaceTexture->getCurrentCrop());
431 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700432 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
433 if ((crop != mCurrentCrop) ||
434 (transform != mCurrentTransform) ||
435 (scalingMode != mCurrentScalingMode))
436 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700437 mCurrentCrop = crop;
438 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700439 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700440 mFlinger->invalidateHwcGeometry();
441 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800442
Mathias Agopianc7f33812011-08-30 15:02:41 -0700443 GLfloat textureMatrix[16];
444 mSurfaceTexture->getTransformMatrix(textureMatrix);
445 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
446 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
447 mFlinger->invalidateHwcGeometry();
448 }
449
Jamie Gennis945d2912011-09-14 18:23:37 -0700450 uint32_t bufWidth = mActiveBuffer->getWidth();
451 uint32_t bufHeight = mActiveBuffer->getHeight();
452 if (oldActiveBuffer != NULL) {
453 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
454 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700455 mFlinger->invalidateHwcGeometry();
456 }
457 }
458
Jamie Gennis945d2912011-09-14 18:23:37 -0700459 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700460 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800461 recomputeVisibleRegions = true;
462 }
463
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700464 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
465 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700466
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700467 // update the layer size and release freeze-lock
468 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700469
470 // FIXME: mPostedDirtyRegion = dirty & bounds
471 mPostedDirtyRegion.set(front.w, front.h);
472
Mathias Agopian97c602c2011-07-19 15:24:46 -0700473 if ((front.w != front.requested_w) ||
474 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700475 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700476 // check that we received a buffer of the right size
477 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700478 if (mCurrentTransform & Transform::ROT_90) {
479 swap(bufWidth, bufHeight);
480 }
481
482 if (isFixedSize() ||
483 (bufWidth == front.requested_w &&
484 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700485 {
486 // Here we pretend the transaction happened by updating the
487 // current and drawing states. Drawing state is only accessed
488 // in this thread, no need to have it locked
489 Layer::State& editDraw(mDrawingState);
490 editDraw.w = editDraw.requested_w;
491 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700492
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700493 // We also need to update the current state so that we don't
494 // end-up doing too much work during the next transaction.
495 // NOTE: We actually don't need hold the transaction lock here
496 // because State::w and State::h are only accessed from
497 // this thread
498 Layer::State& editTemp(currentState());
499 editTemp.w = editDraw.w;
500 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700501
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700502 // recompute visible region
503 recomputeVisibleRegions = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700504
Mathias Agopian97c602c2011-07-19 15:24:46 -0700505 // we now have the correct size, unfreeze the screen
506 mFreezeLock.clear();
507 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700508
509 LOGD_IF(DEBUG_RESIZE,
510 "lockPageFlip : "
511 " (layer=%p), buffer (%ux%u, tr=%02x), "
512 "requested (%dx%d)",
513 this,
514 bufWidth, bufHeight, mCurrentTransform,
515 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700516 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700517 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518}
519
520void Layer::unlockPageFlip(
521 const Transform& planeTransform, Region& outDirtyRegion)
522{
523 Region dirtyRegion(mPostedDirtyRegion);
524 if (!dirtyRegion.isEmpty()) {
525 mPostedDirtyRegion.clear();
526 // The dirty region is given in the layer's coordinate space
527 // transform the dirty region by the surface's transformation
528 // and the global transformation.
529 const Layer::State& s(drawingState());
530 const Transform tr(planeTransform * s.transform);
531 dirtyRegion = tr.transform(dirtyRegion);
532
533 // At this point, the dirty region is in screen space.
534 // Make sure it's constrained by the visible region (which
535 // is in screen space as well).
536 dirtyRegion.andSelf(visibleRegionScreen);
537 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800539 if (visibleRegionScreen.isEmpty()) {
540 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700541 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800542 mFreezeLock.clear();
543 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544}
545
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700546void Layer::dump(String8& result, char* buffer, size_t SIZE) const
547{
548 LayerBaseClient::dump(result, buffer, SIZE);
549
Mathias Agopiana67932f2011-04-20 14:20:59 -0700550 sp<const GraphicBuffer> buf0(mActiveBuffer);
551 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700552 if (buf0 != 0) {
553 w0 = buf0->getWidth();
554 h0 = buf0->getHeight();
555 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700556 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700557 }
558 snprintf(buffer, SIZE,
559 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700560 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopiana4583642011-08-23 18:03:18 -0700561 " freezeLock=%p, transform-hint=0x%02x, queued-frames=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700562 mFormat, w0, h0, s0,f0,
Mathias Agopiana4583642011-08-23 18:03:18 -0700563 getFreezeLock().get(), getTransformHint(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700564
565 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700566
Mathias Agopiana67932f2011-04-20 14:20:59 -0700567 if (mSurfaceTexture != 0) {
568 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700569 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700570}
571
Mathias Agopiana67932f2011-04-20 14:20:59 -0700572uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700573{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700574 // TODO: should we do something special if mSecure is set?
575 if (mProtectedByApp) {
576 // need a hardware-protected path to external video sink
577 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700578 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700579 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700580 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700581}
582
Mathias Agopiana4583642011-08-23 18:03:18 -0700583uint32_t Layer::getTransformHint() const {
584 uint32_t orientation = 0;
585 if (!mFlinger->mDebugDisableTransformHint) {
586 orientation = getOrientation();
587 if (orientation & Transform::ROT_INVALID) {
588 orientation = 0;
589 }
590 }
591 return orientation;
592}
593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594// ---------------------------------------------------------------------------
595
596
597}; // namespace android