blob: 51efdc26e28dd7eaf66df8eabe8ee112015a89e7 [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);
Jamie Gennise8696a42012-01-15 18:54:57 -080066
67 mFrameLatencyNeeded = false;
68 mFrameLatencyOffset = 0;
69 for (int i = 0; i < 128; i++) {
70 mFrameLatencies[i] = 0;
71 }
72}
73
74void Layer::onLayerDisplayed() {
75 if (mFrameLatencyNeeded) {
76 int64_t now = systemTime(SYSTEM_TIME_MONOTONIC);
77 mFrameLatencies[mFrameLatencyOffset] = now -
78 mSurfaceTexture->getTimestamp();
79 mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
80 mFrameLatencyNeeded = false;
81 }
Mathias Agopiand606de62010-05-10 20:06:11 -070082}
83
Mathias Agopiana67932f2011-04-20 14:20:59 -070084void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070085{
Mathias Agopiana67932f2011-04-20 14:20:59 -070086 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070087
Mathias Agopiana67932f2011-04-20 14:20:59 -070088 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
89 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
90 private:
91 wp<Layer> mLayer;
92 virtual void onFrameAvailable() {
93 sp<Layer> that(mLayer.promote());
94 if (that != 0) {
95 that->onFrameQueued();
96 }
97 }
98 };
99 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
100 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
101 mSurfaceTexture->setSynchronousMode(true);
102 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700103}
104
Mathias Agopiana67932f2011-04-20 14:20:59 -0700105Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700106{
Mathias Agopian118d0242011-10-13 16:02:48 -0700107 mFlinger->postMessageAsync(
108 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700109}
110
Mathias Agopiana67932f2011-04-20 14:20:59 -0700111void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700112 android_atomic_inc(&mQueuedFrames);
113 mFlinger->signalEvent();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700114}
115
Mathias Agopiand606de62010-05-10 20:06:11 -0700116// called with SurfaceFlinger::mStateLock as soon as the layer is entered
117// in the purgatory list
118void Layer::onRemoved()
119{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700120 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700121}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700122
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700123void Layer::setName(const String8& name) {
124 LayerBase::setName(name);
125 mSurfaceTexture->setName(name);
126}
127
Mathias Agopiana67932f2011-04-20 14:20:59 -0700128sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700130 class BSurface : public BnSurface, public LayerCleaner {
131 wp<const Layer> mOwner;
132 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
133 sp<ISurfaceTexture> res;
134 sp<const Layer> that( mOwner.promote() );
135 if (that != NULL) {
136 res = that->mSurfaceTexture;
137 }
138 return res;
139 }
140 public:
141 BSurface(const sp<SurfaceFlinger>& flinger,
142 const sp<Layer>& layer)
143 : LayerCleaner(flinger, layer), mOwner(layer) { }
144 };
145 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800146 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}
148
Jamie Gennis582270d2011-08-17 18:19:00 -0700149wp<IBinder> Layer::getSurfaceTextureBinder() const
150{
151 return mSurfaceTexture->asBinder();
152}
153
Mathias Agopianf9d93272009-06-19 17:00:27 -0700154status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 PixelFormat format, uint32_t flags)
156{
Mathias Agopian401c2572009-09-23 19:16:27 -0700157 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 PixelFormatInfo info;
159 status_t err = getPixelFormatInfo(format, &info);
160 if (err) return err;
161
Mathias Agopian401c2572009-09-23 19:16:27 -0700162 // the display's pixel format
163 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700164 uint32_t const maxSurfaceDims = min(
165 hw.getMaxTextureSize(), hw.getMaxViewportDims());
166
167 // never allow a surface larger than what our underlying GL implementation
168 // can handle.
169 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
170 return BAD_VALUE;
171 }
172
Mathias Agopian401c2572009-09-23 19:16:27 -0700173 PixelFormatInfo displayInfo;
174 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700175 const uint32_t hwFlags = hw.getFlags();
176
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700177 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700178
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800180 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700181 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
182 mCurrentOpacity = getOpacityForFormat(format);
183
184 mSurfaceTexture->setDefaultBufferSize(w, h);
185 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700186
Mathias Agopian401c2572009-09-23 19:16:27 -0700187 // we use the red index
188 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
189 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
190 mNeedsDithering = layerRedsize > displayRedSize;
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 return NO_ERROR;
193}
194
Mathias Agopiana350ff92010-08-10 17:14:02 -0700195void Layer::setGeometry(hwc_layer_t* hwcl)
196{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700197 LayerBaseClient::setGeometry(hwcl);
198
199 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700200
201 // we can't do alpha-fade with the hwc HAL
202 const State& s(drawingState());
203 if (s.alpha < 0xFF) {
204 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700205 }
206
Mathias Agopian29a367b2011-07-12 14:51:45 -0700207 /*
208 * Transformations are applied in this order:
209 * 1) buffer orientation/flip/mirror
210 * 2) state transformation (window manager)
211 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700212 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700213 * (NOTE: the matrices are multiplied in reverse order)
214 */
215
216 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700217 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700218
219 // this gives us only the "orientation" component of the transform
220 const uint32_t finalTransform = tr.getOrientation();
221
Mathias Agopiana350ff92010-08-10 17:14:02 -0700222 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700223 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700224 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700225 } else {
226 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700227 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700228
229 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;
234 } else {
235 const sp<GraphicBuffer>& buffer(mActiveBuffer);
236 hwcl->sourceCrop.left = 0;
237 hwcl->sourceCrop.top = 0;
238 if (buffer != NULL) {
239 hwcl->sourceCrop.right = buffer->width;
240 hwcl->sourceCrop.bottom = buffer->height;
241 } else {
242 hwcl->sourceCrop.right = mTransformedBounds.width();
243 hwcl->sourceCrop.bottom = mTransformedBounds.height();
244 }
245 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700246}
247
248void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700249 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700250 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800251 // this can happen if the client never drew into this layer yet,
252 // or if we ran out of memory. In that case, don't let
253 // HWC handle it.
254 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700255 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700256 } else {
257 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700259}
260
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;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700274 const SurfaceFlinger::LayerVector& drawingLayers(
275 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700276 const size_t count = drawingLayers.size();
277 for (size_t i=0 ; i<count ; ++i) {
278 const sp<LayerBase>& layer(drawingLayers[i]);
279 if (layer.get() == static_cast<LayerBase const*>(this))
280 break;
281 under.orSelf(layer->visibleRegionScreen);
282 }
283 // if not everything below us is covered, we plug the holes!
284 Region holes(clip.subtract(under));
285 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700286 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700287 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 return;
289 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700290
Jamie Gennis9575f602011-10-07 14:51:16 -0700291 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700292 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
293 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700294 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
295 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700296 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700297 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700298 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
299 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700300 glMatrixMode(GL_TEXTURE);
301 glLoadMatrixf(mTextureMatrix);
302 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700303 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700304 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700305 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700306 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700307 glMatrixMode(GL_TEXTURE);
308 glLoadIdentity();
309 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700310 glDisable(GL_TEXTURE_EXTERNAL_OES);
311 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700312 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700313
314 drawWithOpenGL(clip);
315
Mathias Agopianc492e672011-10-18 14:49:27 -0700316 glDisable(GL_TEXTURE_EXTERNAL_OES);
317 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318}
319
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800320// As documented in libhardware header, formats in the range
321// 0x100 - 0x1FF are specific to the HAL implementation, and
322// are known to have no alpha channel
323// TODO: move definition for device-specific range into
324// hardware.h, instead of using hard-coded values here.
325#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
326
Mathias Agopiana67932f2011-04-20 14:20:59 -0700327bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800328{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700329 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
330 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800331 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700332 PixelFormatInfo info;
333 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
334 // in case of error (unknown format), we assume no blending
335 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800336}
337
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800338
Mathias Agopiana67932f2011-04-20 14:20:59 -0700339bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700340{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700341 // if we don't have a buffer yet, we're translucent regardless of the
342 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700343 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700345 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700346
347 // if the layer has the opaque flag, then we're always opaque,
348 // otherwise we use the current buffer's format.
349 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700350}
351
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800352bool Layer::isProtected() const
353{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700354 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800355 return (activeBuffer != 0) &&
356 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
357}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359uint32_t Layer::doTransaction(uint32_t flags)
360{
361 const Layer::State& front(drawingState());
362 const Layer::State& temp(currentState());
363
Mathias Agopiana138f892010-05-21 17:24:35 -0700364 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
365 (front.requested_h != temp.requested_h);
366
367 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700368 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000369 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700370 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700371 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700372 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700373 this,
374 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700375 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700376 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377
Mathias Agopiana138f892010-05-21 17:24:35 -0700378 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700379 // 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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700385
386 // record the new size, form this point on, when the client request
387 // a buffer, it'll get the new size.
388 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
389 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 return LayerBase::doTransaction(flags);
393}
394
Mathias Agopiana138f892010-05-21 17:24:35 -0700395bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700396 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700397}
398
399bool Layer::isCropped() const {
400 return !mCurrentCrop.isEmpty();
401}
402
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403// ----------------------------------------------------------------------------
404// pageflip handling...
405// ----------------------------------------------------------------------------
406
407void Layer::lockPageFlip(bool& recomputeVisibleRegions)
408{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700409 if (mQueuedFrames > 0) {
Jamie Gennis351a5132011-09-14 18:23:37 -0700410 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700411 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700412 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700413
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700414 // signal another event if we have more frames pending
415 if (android_atomic_dec(&mQueuedFrames) > 1) {
416 mFlinger->signalEvent();
417 }
418
Mathias Agopiana67932f2011-04-20 14:20:59 -0700419 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
420 // something happened!
421 recomputeVisibleRegions = true;
422 return;
423 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700424
Jamie Gennis351a5132011-09-14 18:23:37 -0700425 // update the active buffer
426 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800427 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700428
Mathias Agopiana67932f2011-04-20 14:20:59 -0700429 const Rect crop(mSurfaceTexture->getCurrentCrop());
430 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700431 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
432 if ((crop != mCurrentCrop) ||
433 (transform != mCurrentTransform) ||
434 (scalingMode != mCurrentScalingMode))
435 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700436 mCurrentCrop = crop;
437 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700438 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700439 mFlinger->invalidateHwcGeometry();
440 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800441
Mathias Agopianc7f33812011-08-30 15:02:41 -0700442 GLfloat textureMatrix[16];
443 mSurfaceTexture->getTransformMatrix(textureMatrix);
444 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
445 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
446 mFlinger->invalidateHwcGeometry();
447 }
448
Jamie Gennis351a5132011-09-14 18:23:37 -0700449 uint32_t bufWidth = mActiveBuffer->getWidth();
450 uint32_t bufHeight = mActiveBuffer->getHeight();
451 if (oldActiveBuffer != NULL) {
452 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
453 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700454 mFlinger->invalidateHwcGeometry();
455 }
456 }
457
Jamie Gennis351a5132011-09-14 18:23:37 -0700458 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700459 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800460 recomputeVisibleRegions = true;
461 }
462
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700463 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
464 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700465
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700466 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700467 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700468
469 // FIXME: mPostedDirtyRegion = dirty & bounds
470 mPostedDirtyRegion.set(front.w, front.h);
471
Mathias Agopian97c602c2011-07-19 15:24:46 -0700472 if ((front.w != front.requested_w) ||
473 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700474 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700475 // check that we received a buffer of the right size
476 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700477 if (mCurrentTransform & Transform::ROT_90) {
478 swap(bufWidth, bufHeight);
479 }
480
481 if (isFixedSize() ||
482 (bufWidth == front.requested_w &&
483 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700484 {
485 // Here we pretend the transaction happened by updating the
486 // current and drawing states. Drawing state is only accessed
487 // in this thread, no need to have it locked
488 Layer::State& editDraw(mDrawingState);
489 editDraw.w = editDraw.requested_w;
490 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700491
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700492 // We also need to update the current state so that we don't
493 // end-up doing too much work during the next transaction.
494 // NOTE: We actually don't need hold the transaction lock here
495 // because State::w and State::h are only accessed from
496 // this thread
497 Layer::State& editTemp(currentState());
498 editTemp.w = editDraw.w;
499 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700500
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700501 // recompute visible region
502 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700503 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700504
Steve Block9d453682011-12-20 16:23:08 +0000505 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700506 "lockPageFlip : "
507 " (layer=%p), buffer (%ux%u, tr=%02x), "
508 "requested (%dx%d)",
509 this,
510 bufWidth, bufHeight, mCurrentTransform,
511 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700512 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700513 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514}
515
516void Layer::unlockPageFlip(
517 const Transform& planeTransform, Region& outDirtyRegion)
518{
519 Region dirtyRegion(mPostedDirtyRegion);
520 if (!dirtyRegion.isEmpty()) {
521 mPostedDirtyRegion.clear();
522 // The dirty region is given in the layer's coordinate space
523 // transform the dirty region by the surface's transformation
524 // and the global transformation.
525 const Layer::State& s(drawingState());
526 const Transform tr(planeTransform * s.transform);
527 dirtyRegion = tr.transform(dirtyRegion);
528
529 // At this point, the dirty region is in screen space.
530 // Make sure it's constrained by the visible region (which
531 // is in screen space as well).
532 dirtyRegion.andSelf(visibleRegionScreen);
533 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534 }
535}
536
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700537void Layer::dump(String8& result, char* buffer, size_t SIZE) const
538{
539 LayerBaseClient::dump(result, buffer, SIZE);
540
Mathias Agopiana67932f2011-04-20 14:20:59 -0700541 sp<const GraphicBuffer> buf0(mActiveBuffer);
542 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700543 if (buf0 != 0) {
544 w0 = buf0->getWidth();
545 h0 = buf0->getHeight();
546 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700547 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700548 }
549 snprintf(buffer, SIZE,
550 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700551 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700552 " transform-hint=0x%02x, queued-frames=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700553 mFormat, w0, h0, s0,f0,
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700554 getTransformHint(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700555
556 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700557
Jamie Gennise8696a42012-01-15 18:54:57 -0800558 const int64_t* l = mFrameLatencies;
559 int o = mFrameLatencyOffset;
560 for (int i = 0; i < 128; i += 8) {
561 snprintf(buffer, SIZE,
562 " "
563 "% 12lld % 12lld % 12lld % 12lld "
564 "% 12lld % 12lld % 12lld % 12lld\n",
565 l[(o+i+0)%128], l[(o+i+1)%128], l[(o+i+2)%128], l[(o+i+3)%128],
566 l[(o+i+4)%128], l[(o+i+5)%128], l[(o+i+6)%128], l[(o+i+7)%128]);
567 result.append(buffer);
568 }
569
Mathias Agopiana67932f2011-04-20 14:20:59 -0700570 if (mSurfaceTexture != 0) {
571 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700572 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700573}
574
Mathias Agopiana67932f2011-04-20 14:20:59 -0700575uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700576{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700577 // TODO: should we do something special if mSecure is set?
578 if (mProtectedByApp) {
579 // need a hardware-protected path to external video sink
580 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700581 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700582 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700583 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700584}
585
Mathias Agopiana4583642011-08-23 18:03:18 -0700586uint32_t Layer::getTransformHint() const {
587 uint32_t orientation = 0;
588 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700589 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700590 if (orientation & Transform::ROT_INVALID) {
591 orientation = 0;
592 }
593 }
594 return orientation;
595}
596
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597// ---------------------------------------------------------------------------
598
599
600}; // namespace android