blob: 9c04d597205c5d9ff50ce80b5ca532a2d761c85a [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"
Mathias Agopian82d7ab62012-01-19 18:34:40 -080041#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#define DEBUG_RESIZE 0
44
45
46namespace android {
47
48// ---------------------------------------------------------------------------
49
Mathias Agopian96f08192010-06-02 23:28:45 -070050Layer::Layer(SurfaceFlinger* flinger,
51 DisplayID display, const sp<Client>& client)
52 : LayerBaseClient(flinger, display, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070053 mTextureName(-1U),
54 mQueuedFrames(0),
55 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070056 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopiana67932f2011-04-20 14:20:59 -070057 mCurrentOpacity(true),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080058 mFrameLatencyNeeded(false),
59 mFrameLatencyOffset(0),
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),
Mathias Agopian933389f2011-07-18 16:15:08 -070065 mProtectedByApp(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066{
Mathias Agopiana67932f2011-04-20 14:20:59 -070067 mCurrentCrop.makeInvalid();
68 glGenTextures(1, &mTextureName);
Jamie Gennise8696a42012-01-15 18:54:57 -080069}
70
71void Layer::onLayerDisplayed() {
72 if (mFrameLatencyNeeded) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -080073 const DisplayHardware& hw(graphicPlane(0).displayHardware());
74 mFrameStats[mFrameLatencyOffset].timestamp = mSurfaceTexture->getTimestamp();
75 mFrameStats[mFrameLatencyOffset].set = systemTime();
76 mFrameStats[mFrameLatencyOffset].vsync = hw.getRefreshTimestamp();
Jamie Gennise8696a42012-01-15 18:54:57 -080077 mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
78 mFrameLatencyNeeded = false;
79 }
Mathias Agopiand606de62010-05-10 20:06:11 -070080}
81
Mathias Agopiana67932f2011-04-20 14:20:59 -070082void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070083{
Mathias Agopiana67932f2011-04-20 14:20:59 -070084 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070085
Mathias Agopiana67932f2011-04-20 14:20:59 -070086 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
87 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
88 private:
89 wp<Layer> mLayer;
90 virtual void onFrameAvailable() {
91 sp<Layer> that(mLayer.promote());
92 if (that != 0) {
93 that->onFrameQueued();
94 }
95 }
96 };
97 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
98 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
99 mSurfaceTexture->setSynchronousMode(true);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800100#ifdef USE_TRIPLE_BUFFERING
101#warning "using triple buffering"
102 mSurfaceTexture->setBufferCountServer(3);
103#else
Mathias Agopiana67932f2011-04-20 14:20:59 -0700104 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800105#endif
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700106}
107
Mathias Agopiana67932f2011-04-20 14:20:59 -0700108Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700109{
Mathias Agopian118d0242011-10-13 16:02:48 -0700110 mFlinger->postMessageAsync(
111 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700112}
113
Mathias Agopiana67932f2011-04-20 14:20:59 -0700114void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700115 android_atomic_inc(&mQueuedFrames);
116 mFlinger->signalEvent();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700117}
118
Mathias Agopiand606de62010-05-10 20:06:11 -0700119// called with SurfaceFlinger::mStateLock as soon as the layer is entered
120// in the purgatory list
121void Layer::onRemoved()
122{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700123 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700124}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700125
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700126void Layer::setName(const String8& name) {
127 LayerBase::setName(name);
128 mSurfaceTexture->setName(name);
129}
130
Mathias Agopiana67932f2011-04-20 14:20:59 -0700131sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700133 class BSurface : public BnSurface, public LayerCleaner {
134 wp<const Layer> mOwner;
135 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
136 sp<ISurfaceTexture> res;
137 sp<const Layer> that( mOwner.promote() );
138 if (that != NULL) {
139 res = that->mSurfaceTexture;
140 }
141 return res;
142 }
143 public:
144 BSurface(const sp<SurfaceFlinger>& flinger,
145 const sp<Layer>& layer)
146 : LayerCleaner(flinger, layer), mOwner(layer) { }
147 };
148 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800149 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Jamie Gennis582270d2011-08-17 18:19:00 -0700152wp<IBinder> Layer::getSurfaceTextureBinder() const
153{
154 return mSurfaceTexture->asBinder();
155}
156
Mathias Agopianf9d93272009-06-19 17:00:27 -0700157status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 PixelFormat format, uint32_t flags)
159{
Mathias Agopian401c2572009-09-23 19:16:27 -0700160 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 PixelFormatInfo info;
162 status_t err = getPixelFormatInfo(format, &info);
163 if (err) return err;
164
Mathias Agopian401c2572009-09-23 19:16:27 -0700165 // the display's pixel format
166 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700167 uint32_t const maxSurfaceDims = min(
168 hw.getMaxTextureSize(), hw.getMaxViewportDims());
169
170 // never allow a surface larger than what our underlying GL implementation
171 // can handle.
172 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
173 return BAD_VALUE;
174 }
175
Mathias Agopian401c2572009-09-23 19:16:27 -0700176 PixelFormatInfo displayInfo;
177 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700178 const uint32_t hwFlags = hw.getFlags();
179
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700180 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700181
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800183 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700184 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
185 mCurrentOpacity = getOpacityForFormat(format);
186
187 mSurfaceTexture->setDefaultBufferSize(w, h);
188 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700189
Mathias Agopian401c2572009-09-23 19:16:27 -0700190 // we use the red index
191 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
192 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
193 mNeedsDithering = layerRedsize > displayRedSize;
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 return NO_ERROR;
196}
197
Mathias Agopiana350ff92010-08-10 17:14:02 -0700198void Layer::setGeometry(hwc_layer_t* hwcl)
199{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700200 LayerBaseClient::setGeometry(hwcl);
201
202 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700203
204 // we can't do alpha-fade with the hwc HAL
205 const State& s(drawingState());
206 if (s.alpha < 0xFF) {
207 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700208 }
209
Mathias Agopian29a367b2011-07-12 14:51:45 -0700210 /*
211 * Transformations are applied in this order:
212 * 1) buffer orientation/flip/mirror
213 * 2) state transformation (window manager)
214 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700215 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700216 * (NOTE: the matrices are multiplied in reverse order)
217 */
218
219 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700220 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700221
222 // this gives us only the "orientation" component of the transform
223 const uint32_t finalTransform = tr.getOrientation();
224
Mathias Agopiana350ff92010-08-10 17:14:02 -0700225 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700226 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700227 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700228 } else {
229 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700230 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700231
232 if (isCropped()) {
233 hwcl->sourceCrop.left = mCurrentCrop.left;
234 hwcl->sourceCrop.top = mCurrentCrop.top;
235 hwcl->sourceCrop.right = mCurrentCrop.right;
236 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
237 } else {
238 const sp<GraphicBuffer>& buffer(mActiveBuffer);
239 hwcl->sourceCrop.left = 0;
240 hwcl->sourceCrop.top = 0;
241 if (buffer != NULL) {
242 hwcl->sourceCrop.right = buffer->width;
243 hwcl->sourceCrop.bottom = buffer->height;
244 } else {
245 hwcl->sourceCrop.right = mTransformedBounds.width();
246 hwcl->sourceCrop.bottom = mTransformedBounds.height();
247 }
248 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700249}
250
251void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700252 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800254 // this can happen if the client never drew into this layer yet,
255 // or if we ran out of memory. In that case, don't let
256 // HWC handle it.
257 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700259 } else {
260 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700261 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700262}
263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264void Layer::onDraw(const Region& clip) const
265{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700266 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700268 // in fact never been drawn into. This happens frequently with
269 // SurfaceView because the WindowManager can't know when the client
270 // has drawn the first time.
271
272 // If there is nothing under us, we paint the screen in black, otherwise
273 // we just skip this update.
274
275 // figure out if there is something below us
276 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700277 const SurfaceFlinger::LayerVector& drawingLayers(
278 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700279 const size_t count = drawingLayers.size();
280 for (size_t i=0 ; i<count ; ++i) {
281 const sp<LayerBase>& layer(drawingLayers[i]);
282 if (layer.get() == static_cast<LayerBase const*>(this))
283 break;
284 under.orSelf(layer->visibleRegionScreen);
285 }
286 // if not everything below us is covered, we plug the holes!
287 Region holes(clip.subtract(under));
288 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700289 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700290 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 return;
292 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700293
Jamie Gennis9575f602011-10-07 14:51:16 -0700294 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700295 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
296 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700297 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
298 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700299 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700300 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700301 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
302 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700303 glMatrixMode(GL_TEXTURE);
304 glLoadMatrixf(mTextureMatrix);
305 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700306 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700307 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700308 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700309 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700310 glMatrixMode(GL_TEXTURE);
311 glLoadIdentity();
312 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700313 glDisable(GL_TEXTURE_EXTERNAL_OES);
314 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700315 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316
317 drawWithOpenGL(clip);
318
Mathias Agopianc492e672011-10-18 14:49:27 -0700319 glDisable(GL_TEXTURE_EXTERNAL_OES);
320 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800323// As documented in libhardware header, formats in the range
324// 0x100 - 0x1FF are specific to the HAL implementation, and
325// are known to have no alpha channel
326// TODO: move definition for device-specific range into
327// hardware.h, instead of using hard-coded values here.
328#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
329
Mathias Agopiana67932f2011-04-20 14:20:59 -0700330bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800331{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700332 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
333 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800334 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700335 PixelFormatInfo info;
336 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
337 // in case of error (unknown format), we assume no blending
338 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800339}
340
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800341
Mathias Agopiana67932f2011-04-20 14:20:59 -0700342bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700343{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344 // if we don't have a buffer yet, we're translucent regardless of the
345 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700346 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700347 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700348 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700349
350 // if the layer has the opaque flag, then we're always opaque,
351 // otherwise we use the current buffer's format.
352 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700353}
354
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800355bool Layer::isProtected() const
356{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700357 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800358 return (activeBuffer != 0) &&
359 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
360}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362uint32_t Layer::doTransaction(uint32_t flags)
363{
364 const Layer::State& front(drawingState());
365 const Layer::State& temp(currentState());
366
Mathias Agopiana138f892010-05-21 17:24:35 -0700367 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
368 (front.requested_h != temp.requested_h);
369
370 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700371 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000372 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700373 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700374 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700375 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700376 this,
377 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700378 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700379 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380
Mathias Agopiana138f892010-05-21 17:24:35 -0700381 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700382 // this will make sure LayerBase::doTransaction doesn't update
383 // the drawing state's size
384 Layer::State& editDraw(mDrawingState);
385 editDraw.requested_w = temp.requested_w;
386 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700388
389 // record the new size, form this point on, when the client request
390 // a buffer, it'll get the new size.
391 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
392 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 return LayerBase::doTransaction(flags);
396}
397
Mathias Agopiana138f892010-05-21 17:24:35 -0700398bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700399 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700400}
401
402bool Layer::isCropped() const {
403 return !mCurrentCrop.isEmpty();
404}
405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406// ----------------------------------------------------------------------------
407// pageflip handling...
408// ----------------------------------------------------------------------------
409
410void Layer::lockPageFlip(bool& recomputeVisibleRegions)
411{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700412 if (mQueuedFrames > 0) {
Jamie Gennis351a5132011-09-14 18:23:37 -0700413 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700414 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700415 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700416
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700417 // signal another event if we have more frames pending
418 if (android_atomic_dec(&mQueuedFrames) > 1) {
419 mFlinger->signalEvent();
420 }
421
Mathias Agopiana67932f2011-04-20 14:20:59 -0700422 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
423 // something happened!
424 recomputeVisibleRegions = true;
425 return;
426 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700427
Jamie Gennis351a5132011-09-14 18:23:37 -0700428 // update the active buffer
429 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800430 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700431
Mathias Agopiana67932f2011-04-20 14:20:59 -0700432 const Rect crop(mSurfaceTexture->getCurrentCrop());
433 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700434 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
435 if ((crop != mCurrentCrop) ||
436 (transform != mCurrentTransform) ||
437 (scalingMode != mCurrentScalingMode))
438 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700439 mCurrentCrop = crop;
440 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700441 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700442 mFlinger->invalidateHwcGeometry();
443 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800444
Mathias Agopianc7f33812011-08-30 15:02:41 -0700445 GLfloat textureMatrix[16];
446 mSurfaceTexture->getTransformMatrix(textureMatrix);
447 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
448 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
449 mFlinger->invalidateHwcGeometry();
450 }
451
Jamie Gennis351a5132011-09-14 18:23:37 -0700452 uint32_t bufWidth = mActiveBuffer->getWidth();
453 uint32_t bufHeight = mActiveBuffer->getHeight();
454 if (oldActiveBuffer != NULL) {
455 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
456 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700457 mFlinger->invalidateHwcGeometry();
458 }
459 }
460
Jamie Gennis351a5132011-09-14 18:23:37 -0700461 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700462 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800463 recomputeVisibleRegions = true;
464 }
465
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700466 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
467 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700468
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700469 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700470 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700471
472 // FIXME: mPostedDirtyRegion = dirty & bounds
473 mPostedDirtyRegion.set(front.w, front.h);
474
Mathias Agopian97c602c2011-07-19 15:24:46 -0700475 if ((front.w != front.requested_w) ||
476 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700477 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700478 // check that we received a buffer of the right size
479 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700480 if (mCurrentTransform & Transform::ROT_90) {
481 swap(bufWidth, bufHeight);
482 }
483
484 if (isFixedSize() ||
485 (bufWidth == front.requested_w &&
486 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700487 {
488 // Here we pretend the transaction happened by updating the
489 // current and drawing states. Drawing state is only accessed
490 // in this thread, no need to have it locked
491 Layer::State& editDraw(mDrawingState);
492 editDraw.w = editDraw.requested_w;
493 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700494
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700495 // We also need to update the current state so that we don't
496 // end-up doing too much work during the next transaction.
497 // NOTE: We actually don't need hold the transaction lock here
498 // because State::w and State::h are only accessed from
499 // this thread
500 Layer::State& editTemp(currentState());
501 editTemp.w = editDraw.w;
502 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700503
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700504 // recompute visible region
505 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700506 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700507
Steve Block9d453682011-12-20 16:23:08 +0000508 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700509 "lockPageFlip : "
510 " (layer=%p), buffer (%ux%u, tr=%02x), "
511 "requested (%dx%d)",
512 this,
513 bufWidth, bufHeight, mCurrentTransform,
514 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700515 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700516 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517}
518
519void Layer::unlockPageFlip(
520 const Transform& planeTransform, Region& outDirtyRegion)
521{
522 Region dirtyRegion(mPostedDirtyRegion);
523 if (!dirtyRegion.isEmpty()) {
524 mPostedDirtyRegion.clear();
525 // The dirty region is given in the layer's coordinate space
526 // transform the dirty region by the surface's transformation
527 // and the global transformation.
528 const Layer::State& s(drawingState());
529 const Transform tr(planeTransform * s.transform);
530 dirtyRegion = tr.transform(dirtyRegion);
531
532 // At this point, the dirty region is in screen space.
533 // Make sure it's constrained by the visible region (which
534 // is in screen space as well).
535 dirtyRegion.andSelf(visibleRegionScreen);
536 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538}
539
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700540void Layer::dump(String8& result, char* buffer, size_t SIZE) const
541{
542 LayerBaseClient::dump(result, buffer, SIZE);
543
Mathias Agopiana67932f2011-04-20 14:20:59 -0700544 sp<const GraphicBuffer> buf0(mActiveBuffer);
545 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700546 if (buf0 != 0) {
547 w0 = buf0->getWidth();
548 h0 = buf0->getHeight();
549 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700550 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700551 }
552 snprintf(buffer, SIZE,
553 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700554 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700555 " transform-hint=0x%02x, queued-frames=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700556 mFormat, w0, h0, s0,f0,
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700557 getTransformHint(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700558
559 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700560
Mathias Agopiana67932f2011-04-20 14:20:59 -0700561 if (mSurfaceTexture != 0) {
562 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700563 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700564}
565
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800566void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
567{
568 LayerBaseClient::dumpStats(result, buffer, SIZE);
569 const size_t o = mFrameLatencyOffset;
570 const DisplayHardware& hw(graphicPlane(0).displayHardware());
571 const nsecs_t period = hw.getRefreshPeriod();
572 result.appendFormat("%lld\n", period);
573 for (size_t i=0 ; i<128 ; i++) {
574 const size_t index = (o+i) % 128;
575 const nsecs_t time_app = mFrameStats[index].timestamp;
576 const nsecs_t time_set = mFrameStats[index].set;
577 const nsecs_t time_vsync = mFrameStats[index].vsync;
578 result.appendFormat("%lld\t%lld\t%lld\n",
579 time_app,
580 time_vsync,
581 time_set);
582 }
583 result.append("\n");
584}
585
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800586void Layer::clearStats()
587{
588 LayerBaseClient::clearStats();
589 memset(mFrameStats, 0, sizeof(mFrameStats));
590}
591
Mathias Agopiana67932f2011-04-20 14:20:59 -0700592uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700593{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700594 // TODO: should we do something special if mSecure is set?
595 if (mProtectedByApp) {
596 // need a hardware-protected path to external video sink
597 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700598 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700599 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700600 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700601}
602
Mathias Agopiana4583642011-08-23 18:03:18 -0700603uint32_t Layer::getTransformHint() const {
604 uint32_t orientation = 0;
605 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700606 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700607 if (orientation & Transform::ROT_INVALID) {
608 orientation = 0;
609 }
610 }
611 return orientation;
612}
613
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614// ---------------------------------------------------------------------------
615
616
617}; // namespace android