blob: efcdd87642759552927b418002a2c57ed3669e09 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace 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 Agopian4d143ee2012-02-23 20:05:39 -080057 mRefreshPending(false),
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 Agopian303d5382012-02-05 01:49:16 -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 Agopian303d5382012-02-05 01:49:16 -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);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800116 mFlinger->signalLayerUpdate();
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);
Mathias Agopianff615cc2012-02-24 14:58:36 -0800163 if (err) {
164 ALOGE("unsupported pixelformat %d", format);
165 return err;
166 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
Mathias Agopian401c2572009-09-23 19:16:27 -0700168 // the display's pixel format
169 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700170 uint32_t const maxSurfaceDims = min(
171 hw.getMaxTextureSize(), hw.getMaxViewportDims());
172
173 // never allow a surface larger than what our underlying GL implementation
174 // can handle.
175 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800176 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700177 return BAD_VALUE;
178 }
179
Mathias Agopian401c2572009-09-23 19:16:27 -0700180 PixelFormatInfo displayInfo;
181 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700182 const uint32_t hwFlags = hw.getFlags();
183
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700184 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700185
Mathias Agopian3330b202009-10-05 17:07:12 -0700186 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800187 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700188 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
189 mCurrentOpacity = getOpacityForFormat(format);
190
191 mSurfaceTexture->setDefaultBufferSize(w, h);
192 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700193
Mathias Agopian401c2572009-09-23 19:16:27 -0700194 // we use the red index
195 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
196 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
197 mNeedsDithering = layerRedsize > displayRedSize;
198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 return NO_ERROR;
200}
201
Mathias Agopiana350ff92010-08-10 17:14:02 -0700202void Layer::setGeometry(hwc_layer_t* hwcl)
203{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700204 LayerBaseClient::setGeometry(hwcl);
205
206 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207
208 // we can't do alpha-fade with the hwc HAL
209 const State& s(drawingState());
210 if (s.alpha < 0xFF) {
211 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212 }
213
Mathias Agopian29a367b2011-07-12 14:51:45 -0700214 /*
215 * Transformations are applied in this order:
216 * 1) buffer orientation/flip/mirror
217 * 2) state transformation (window manager)
218 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700219 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700220 * (NOTE: the matrices are multiplied in reverse order)
221 */
222
223 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700224 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700225
226 // this gives us only the "orientation" component of the transform
227 const uint32_t finalTransform = tr.getOrientation();
228
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700230 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700231 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700232 } else {
233 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700234 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700235
236 if (isCropped()) {
237 hwcl->sourceCrop.left = mCurrentCrop.left;
238 hwcl->sourceCrop.top = mCurrentCrop.top;
239 hwcl->sourceCrop.right = mCurrentCrop.right;
240 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
241 } else {
242 const sp<GraphicBuffer>& buffer(mActiveBuffer);
243 hwcl->sourceCrop.left = 0;
244 hwcl->sourceCrop.top = 0;
245 if (buffer != NULL) {
246 hwcl->sourceCrop.right = buffer->width;
247 hwcl->sourceCrop.bottom = buffer->height;
248 } else {
249 hwcl->sourceCrop.right = mTransformedBounds.width();
250 hwcl->sourceCrop.bottom = mTransformedBounds.height();
251 }
252 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253}
254
255void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700256 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700257 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800258 // this can happen if the client never drew into this layer yet,
259 // or if we ran out of memory. In that case, don't let
260 // HWC handle it.
261 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700262 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700263 } else {
264 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700265 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700266}
267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268void Layer::onDraw(const Region& clip) const
269{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700270 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700272 // in fact never been drawn into. This happens frequently with
273 // SurfaceView because the WindowManager can't know when the client
274 // has drawn the first time.
275
276 // If there is nothing under us, we paint the screen in black, otherwise
277 // we just skip this update.
278
279 // figure out if there is something below us
280 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700281 const SurfaceFlinger::LayerVector& drawingLayers(
282 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700283 const size_t count = drawingLayers.size();
284 for (size_t i=0 ; i<count ; ++i) {
285 const sp<LayerBase>& layer(drawingLayers[i]);
286 if (layer.get() == static_cast<LayerBase const*>(this))
287 break;
288 under.orSelf(layer->visibleRegionScreen);
289 }
290 // if not everything below us is covered, we plug the holes!
291 Region holes(clip.subtract(under));
292 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700293 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700294 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 return;
296 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700297
Jamie Gennis9575f602011-10-07 14:51:16 -0700298 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700299 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
300 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700301 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
302 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700303 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700304 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700305 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
306 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700307 glMatrixMode(GL_TEXTURE);
308 glLoadMatrixf(mTextureMatrix);
309 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700310 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700311 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700312 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700313 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700314 glMatrixMode(GL_TEXTURE);
315 glLoadIdentity();
316 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700317 glDisable(GL_TEXTURE_EXTERNAL_OES);
318 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700319 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700320
321 drawWithOpenGL(clip);
322
Mathias Agopianc492e672011-10-18 14:49:27 -0700323 glDisable(GL_TEXTURE_EXTERNAL_OES);
324 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800327// As documented in libhardware header, formats in the range
328// 0x100 - 0x1FF are specific to the HAL implementation, and
329// are known to have no alpha channel
330// TODO: move definition for device-specific range into
331// hardware.h, instead of using hard-coded values here.
332#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
333
Mathias Agopiana67932f2011-04-20 14:20:59 -0700334bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800335{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700336 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
337 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800338 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700339 PixelFormatInfo info;
340 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
341 // in case of error (unknown format), we assume no blending
342 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800343}
344
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800345
Mathias Agopiana67932f2011-04-20 14:20:59 -0700346bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700347{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 // if we don't have a buffer yet, we're translucent regardless of the
349 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700350 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700351 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700352 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700353
354 // if the layer has the opaque flag, then we're always opaque,
355 // otherwise we use the current buffer's format.
356 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700357}
358
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800359bool Layer::isProtected() const
360{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700361 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800362 return (activeBuffer != 0) &&
363 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
364}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366uint32_t Layer::doTransaction(uint32_t flags)
367{
368 const Layer::State& front(drawingState());
369 const Layer::State& temp(currentState());
370
Mathias Agopiana138f892010-05-21 17:24:35 -0700371 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
372 (front.requested_h != temp.requested_h);
373
374 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700375 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000376 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700377 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700378 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700379 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700380 this,
381 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700383 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384
Mathias Agopiana138f892010-05-21 17:24:35 -0700385 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700386 // this will make sure LayerBase::doTransaction doesn't update
387 // the drawing state's size
388 Layer::State& editDraw(mDrawingState);
389 editDraw.requested_w = temp.requested_w;
390 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700392
393 // record the new size, form this point on, when the client request
394 // a buffer, it'll get the new size.
395 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
396 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 return LayerBase::doTransaction(flags);
400}
401
Mathias Agopiana138f892010-05-21 17:24:35 -0700402bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700403 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700404}
405
406bool Layer::isCropped() const {
407 return !mCurrentCrop.isEmpty();
408}
409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410// ----------------------------------------------------------------------------
411// pageflip handling...
412// ----------------------------------------------------------------------------
413
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800414bool Layer::onPreComposition() {
415 mRefreshPending = false;
416 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800417}
418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419void Layer::lockPageFlip(bool& recomputeVisibleRegions)
420{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700421 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800422
423 // if we've already called updateTexImage() without going through
424 // a composition step, we have to skip this layer at this point
425 // because we cannot call updateTeximage() without a corresponding
426 // compositionComplete() call.
427 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800428 if (mRefreshPending) {
429 mPostedDirtyRegion.clear();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800430 return;
431 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800432 mRefreshPending = true;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800433
Jamie Gennis351a5132011-09-14 18:23:37 -0700434 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700435 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700436 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700437
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700438 // signal another event if we have more frames pending
439 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800440 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700441 }
442
Mathias Agopiana67932f2011-04-20 14:20:59 -0700443 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
444 // something happened!
445 recomputeVisibleRegions = true;
446 return;
447 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700448
Jamie Gennis351a5132011-09-14 18:23:37 -0700449 // update the active buffer
450 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800451 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700452
Mathias Agopiana67932f2011-04-20 14:20:59 -0700453 const Rect crop(mSurfaceTexture->getCurrentCrop());
454 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700455 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
456 if ((crop != mCurrentCrop) ||
457 (transform != mCurrentTransform) ||
458 (scalingMode != mCurrentScalingMode))
459 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700460 mCurrentCrop = crop;
461 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700462 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700463 mFlinger->invalidateHwcGeometry();
464 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800465
Mathias Agopianc7f33812011-08-30 15:02:41 -0700466 GLfloat textureMatrix[16];
467 mSurfaceTexture->getTransformMatrix(textureMatrix);
468 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
469 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
470 mFlinger->invalidateHwcGeometry();
471 }
472
Jamie Gennis351a5132011-09-14 18:23:37 -0700473 uint32_t bufWidth = mActiveBuffer->getWidth();
474 uint32_t bufHeight = mActiveBuffer->getHeight();
475 if (oldActiveBuffer != NULL) {
476 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
477 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700478 mFlinger->invalidateHwcGeometry();
479 }
480 }
481
Jamie Gennis351a5132011-09-14 18:23:37 -0700482 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700483 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800484 recomputeVisibleRegions = true;
485 }
486
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700487 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
488 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700489
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700490 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700491 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700492
493 // FIXME: mPostedDirtyRegion = dirty & bounds
494 mPostedDirtyRegion.set(front.w, front.h);
495
Mathias Agopian97c602c2011-07-19 15:24:46 -0700496 if ((front.w != front.requested_w) ||
497 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700498 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700499 // check that we received a buffer of the right size
500 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700501 if (mCurrentTransform & Transform::ROT_90) {
502 swap(bufWidth, bufHeight);
503 }
504
505 if (isFixedSize() ||
506 (bufWidth == front.requested_w &&
507 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700508 {
509 // Here we pretend the transaction happened by updating the
510 // current and drawing states. Drawing state is only accessed
511 // in this thread, no need to have it locked
512 Layer::State& editDraw(mDrawingState);
513 editDraw.w = editDraw.requested_w;
514 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700515
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700516 // We also need to update the current state so that we don't
517 // end-up doing too much work during the next transaction.
518 // NOTE: We actually don't need hold the transaction lock here
519 // because State::w and State::h are only accessed from
520 // this thread
521 Layer::State& editTemp(currentState());
522 editTemp.w = editDraw.w;
523 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700524
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700525 // recompute visible region
526 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700527 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700528
Steve Block9d453682011-12-20 16:23:08 +0000529 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700530 "lockPageFlip : "
531 " (layer=%p), buffer (%ux%u, tr=%02x), "
532 "requested (%dx%d)",
533 this,
534 bufWidth, bufHeight, mCurrentTransform,
535 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700536 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700537 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538}
539
540void Layer::unlockPageFlip(
541 const Transform& planeTransform, Region& outDirtyRegion)
542{
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800543 Region postedRegion(mPostedDirtyRegion);
544 if (!postedRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 mPostedDirtyRegion.clear();
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800546 if (!visibleRegionScreen.isEmpty()) {
547 // The dirty region is given in the layer's coordinate space
548 // transform the dirty region by the surface's transformation
549 // and the global transformation.
550 const Layer::State& s(drawingState());
551 const Transform tr(planeTransform * s.transform);
552 postedRegion = tr.transform(postedRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800554 // At this point, the dirty region is in screen space.
555 // Make sure it's constrained by the visible region (which
556 // is in screen space as well).
557 postedRegion.andSelf(visibleRegionScreen);
558 outDirtyRegion.orSelf(postedRegion);
559 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 }
561}
562
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700563void Layer::dump(String8& result, char* buffer, size_t SIZE) const
564{
565 LayerBaseClient::dump(result, buffer, SIZE);
566
Mathias Agopiana67932f2011-04-20 14:20:59 -0700567 sp<const GraphicBuffer> buf0(mActiveBuffer);
568 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700569 if (buf0 != 0) {
570 w0 = buf0->getWidth();
571 h0 = buf0->getHeight();
572 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700573 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700574 }
575 snprintf(buffer, SIZE,
576 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700577 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800578 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700579 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800580 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700581
582 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700583
Mathias Agopiana67932f2011-04-20 14:20:59 -0700584 if (mSurfaceTexture != 0) {
585 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700586 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700587}
588
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800589void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
590{
591 LayerBaseClient::dumpStats(result, buffer, SIZE);
592 const size_t o = mFrameLatencyOffset;
593 const DisplayHardware& hw(graphicPlane(0).displayHardware());
594 const nsecs_t period = hw.getRefreshPeriod();
595 result.appendFormat("%lld\n", period);
596 for (size_t i=0 ; i<128 ; i++) {
597 const size_t index = (o+i) % 128;
598 const nsecs_t time_app = mFrameStats[index].timestamp;
599 const nsecs_t time_set = mFrameStats[index].set;
600 const nsecs_t time_vsync = mFrameStats[index].vsync;
601 result.appendFormat("%lld\t%lld\t%lld\n",
602 time_app,
603 time_vsync,
604 time_set);
605 }
606 result.append("\n");
607}
608
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800609void Layer::clearStats()
610{
611 LayerBaseClient::clearStats();
612 memset(mFrameStats, 0, sizeof(mFrameStats));
613}
614
Mathias Agopiana67932f2011-04-20 14:20:59 -0700615uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700616{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700617 // TODO: should we do something special if mSecure is set?
618 if (mProtectedByApp) {
619 // need a hardware-protected path to external video sink
620 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700621 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700622 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700623 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700624}
625
Mathias Agopiana4583642011-08-23 18:03:18 -0700626uint32_t Layer::getTransformHint() const {
627 uint32_t orientation = 0;
628 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700629 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700630 if (orientation & Transform::ROT_INVALID) {
631 orientation = 0;
632 }
633 }
634 return orientation;
635}
636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637// ---------------------------------------------------------------------------
638
639
640}; // namespace android