blob: c73d56eb59ce4193ffa5aebf78774dcb925e60b4 [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
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080022#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Mathias Agopiana67932f2011-04-20 14:20:59 -070024#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070026#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <utils/Errors.h>
29#include <utils/Log.h>
30#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080031#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Mathias Agopian3330b202009-10-05 17:07:12 -070033#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080035
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include "clz.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070039#include "DisplayHardware/DisplayHardware.h"
40#include "DisplayHardware/HWComposer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070041#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include "SurfaceFlinger.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070044#include "SurfaceTextureLayer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#define DEBUG_RESIZE 0
47
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048namespace android {
49
50// ---------------------------------------------------------------------------
51
Mathias Agopian96f08192010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070055 mTextureName(-1U),
56 mQueuedFrames(0),
57 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070058 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopiana67932f2011-04-20 14:20:59 -070059 mCurrentOpacity(true),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080060 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080061 mFrameLatencyNeeded(false),
62 mFrameLatencyOffset(0),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080063 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070064 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiana67932f2011-04-20 14:20:59 -070065 mOpaqueLayer(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070066 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070067 mSecure(false),
Mathias Agopian933389f2011-07-18 16:15:08 -070068 mProtectedByApp(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069{
Mathias Agopiana67932f2011-04-20 14:20:59 -070070 mCurrentCrop.makeInvalid();
71 glGenTextures(1, &mTextureName);
Jamie Gennise8696a42012-01-15 18:54:57 -080072}
73
74void Layer::onLayerDisplayed() {
75 if (mFrameLatencyNeeded) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -080076 const DisplayHardware& hw(graphicPlane(0).displayHardware());
77 mFrameStats[mFrameLatencyOffset].timestamp = mSurfaceTexture->getTimestamp();
78 mFrameStats[mFrameLatencyOffset].set = systemTime();
79 mFrameStats[mFrameLatencyOffset].vsync = hw.getRefreshTimestamp();
Jamie Gennise8696a42012-01-15 18:54:57 -080080 mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
81 mFrameLatencyNeeded = false;
82 }
Mathias Agopiand606de62010-05-10 20:06:11 -070083}
84
Mathias Agopiana67932f2011-04-20 14:20:59 -070085void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070086{
Mathias Agopiana67932f2011-04-20 14:20:59 -070087 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070088
Mathias Agopiana67932f2011-04-20 14:20:59 -070089 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
90 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
91 private:
92 wp<Layer> mLayer;
93 virtual void onFrameAvailable() {
94 sp<Layer> that(mLayer.promote());
95 if (that != 0) {
96 that->onFrameQueued();
97 }
98 }
99 };
Daniel Lamb2675792012-02-23 14:35:13 -0800100
101 // Creates a custom BufferQueue for SurfaceTexture to use
102 sp<BufferQueue> bq = new SurfaceTextureLayer();
103 mSurfaceTexture = new SurfaceTexture(mTextureName, true,
Mathias Agopiana0db3082012-04-23 13:59:36 -0700104 GL_TEXTURE_EXTERNAL_OES, false, bq);
Daniel Lamb2675792012-02-23 14:35:13 -0800105
Daniel Lamb2675792012-02-23 14:35:13 -0800106 mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopiana67932f2011-04-20 14:20:59 -0700107 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
108 mSurfaceTexture->setSynchronousMode(true);
Daniel Lamb2675792012-02-23 14:35:13 -0800109
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700110#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
111#warning "disabling triple buffering"
Mathias Agopiana67932f2011-04-20 14:20:59 -0700112 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700113#else
114 mSurfaceTexture->setBufferCountServer(3);
Mathias Agopian303d5382012-02-05 01:49:16 -0800115#endif
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700116}
117
Mathias Agopiana67932f2011-04-20 14:20:59 -0700118Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119{
Mathias Agopian118d0242011-10-13 16:02:48 -0700120 mFlinger->postMessageAsync(
121 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700122}
123
Mathias Agopiana67932f2011-04-20 14:20:59 -0700124void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700125 android_atomic_inc(&mQueuedFrames);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700127}
128
Mathias Agopiand606de62010-05-10 20:06:11 -0700129// called with SurfaceFlinger::mStateLock as soon as the layer is entered
130// in the purgatory list
131void Layer::onRemoved()
132{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700133 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700134}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700135
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700136void Layer::setName(const String8& name) {
137 LayerBase::setName(name);
138 mSurfaceTexture->setName(name);
139}
140
Daniel Lamb2675792012-02-23 14:35:13 -0800141void Layer::validateVisibility(const Transform& globalTransform) {
142 LayerBase::validateVisibility(globalTransform);
143
144 // This optimization allows the SurfaceTexture to bake in
145 // the rotation so hardware overlays can be used
146 mSurfaceTexture->setTransformHint(getTransformHint());
147}
148
Mathias Agopiana67932f2011-04-20 14:20:59 -0700149sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700151 class BSurface : public BnSurface, public LayerCleaner {
152 wp<const Layer> mOwner;
153 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
154 sp<ISurfaceTexture> res;
155 sp<const Layer> that( mOwner.promote() );
156 if (that != NULL) {
Daniel Lamb2675792012-02-23 14:35:13 -0800157 res = that->mSurfaceTexture->getBufferQueue();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700158 }
159 return res;
160 }
161 public:
162 BSurface(const sp<SurfaceFlinger>& flinger,
163 const sp<Layer>& layer)
164 : LayerCleaner(flinger, layer), mOwner(layer) { }
165 };
166 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800167 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168}
169
Jamie Gennis582270d2011-08-17 18:19:00 -0700170wp<IBinder> Layer::getSurfaceTextureBinder() const
171{
Daniel Lamb2675792012-02-23 14:35:13 -0800172 return mSurfaceTexture->getBufferQueue()->asBinder();
Jamie Gennis582270d2011-08-17 18:19:00 -0700173}
174
Mathias Agopianf9d93272009-06-19 17:00:27 -0700175status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 PixelFormat format, uint32_t flags)
177{
Mathias Agopian401c2572009-09-23 19:16:27 -0700178 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 PixelFormatInfo info;
180 status_t err = getPixelFormatInfo(format, &info);
Mathias Agopianff615cc2012-02-24 14:58:36 -0800181 if (err) {
182 ALOGE("unsupported pixelformat %d", format);
183 return err;
184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
Mathias Agopian401c2572009-09-23 19:16:27 -0700186 // the display's pixel format
187 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700188 uint32_t const maxSurfaceDims = min(
189 hw.getMaxTextureSize(), hw.getMaxViewportDims());
190
191 // never allow a surface larger than what our underlying GL implementation
192 // can handle.
193 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800194 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700195 return BAD_VALUE;
196 }
197
Mathias Agopian401c2572009-09-23 19:16:27 -0700198 PixelFormatInfo displayInfo;
199 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700200 const uint32_t hwFlags = hw.getFlags();
201
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700202 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700203
Mathias Agopian3330b202009-10-05 17:07:12 -0700204 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800205 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700206 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
207 mCurrentOpacity = getOpacityForFormat(format);
208
209 mSurfaceTexture->setDefaultBufferSize(w, h);
210 mSurfaceTexture->setDefaultBufferFormat(format);
Daniel Lamb2675792012-02-23 14:35:13 -0800211 mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700212
Mathias Agopian401c2572009-09-23 19:16:27 -0700213 // we use the red index
214 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
215 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
216 mNeedsDithering = layerRedsize > displayRedSize;
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 return NO_ERROR;
219}
220
Mathias Agopiana350ff92010-08-10 17:14:02 -0700221void Layer::setGeometry(hwc_layer_t* hwcl)
222{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700223 LayerBaseClient::setGeometry(hwcl);
224
225 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700226
227 // we can't do alpha-fade with the hwc HAL
228 const State& s(drawingState());
229 if (s.alpha < 0xFF) {
230 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700231 }
232
Mathias Agopian29a367b2011-07-12 14:51:45 -0700233 /*
234 * Transformations are applied in this order:
235 * 1) buffer orientation/flip/mirror
236 * 2) state transformation (window manager)
237 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700238 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700239 * (NOTE: the matrices are multiplied in reverse order)
240 */
241
242 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700243 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700244
245 // this gives us only the "orientation" component of the transform
246 const uint32_t finalTransform = tr.getOrientation();
247
Mathias Agopiana350ff92010-08-10 17:14:02 -0700248 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700249 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700250 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700251 } else {
252 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700254
255 if (isCropped()) {
256 hwcl->sourceCrop.left = mCurrentCrop.left;
257 hwcl->sourceCrop.top = mCurrentCrop.top;
258 hwcl->sourceCrop.right = mCurrentCrop.right;
259 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
260 } else {
261 const sp<GraphicBuffer>& buffer(mActiveBuffer);
262 hwcl->sourceCrop.left = 0;
263 hwcl->sourceCrop.top = 0;
264 if (buffer != NULL) {
265 hwcl->sourceCrop.right = buffer->width;
266 hwcl->sourceCrop.bottom = buffer->height;
267 } else {
268 hwcl->sourceCrop.right = mTransformedBounds.width();
269 hwcl->sourceCrop.bottom = mTransformedBounds.height();
270 }
271 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700272}
273
274void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700275 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700276 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800277 // this can happen if the client never drew into this layer yet,
278 // or if we ran out of memory. In that case, don't let
279 // HWC handle it.
280 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700281 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700282 } else {
283 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700284 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700285}
286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287void Layer::onDraw(const Region& clip) const
288{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800289 ATRACE_CALL();
290
Mathias Agopiana67932f2011-04-20 14:20:59 -0700291 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700293 // in fact never been drawn into. This happens frequently with
294 // SurfaceView because the WindowManager can't know when the client
295 // has drawn the first time.
296
297 // If there is nothing under us, we paint the screen in black, otherwise
298 // we just skip this update.
299
300 // figure out if there is something below us
301 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700302 const SurfaceFlinger::LayerVector& drawingLayers(
303 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700304 const size_t count = drawingLayers.size();
305 for (size_t i=0 ; i<count ; ++i) {
306 const sp<LayerBase>& layer(drawingLayers[i]);
307 if (layer.get() == static_cast<LayerBase const*>(this))
308 break;
309 under.orSelf(layer->visibleRegionScreen);
310 }
311 // if not everything below us is covered, we plug the holes!
312 Region holes(clip.subtract(under));
313 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700314 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700315 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 return;
317 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700318
Jamie Gennis9575f602011-10-07 14:51:16 -0700319 if (!isProtected()) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700320 // TODO: we could be more subtle with isFixedSize()
321 const bool useFiltering = getFiltering() || needsFiltering() || isFixedSize();
322
323 // Query the texture matrix given our current filtering mode.
324 float textureMatrix[16];
325 mSurfaceTexture->setFilteringEnabled(useFiltering);
326 mSurfaceTexture->getTransformMatrix(textureMatrix);
327
328 // Set things up for texturing.
Mathias Agopianc492e672011-10-18 14:49:27 -0700329 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
330 GLenum filter = GL_NEAREST;
Jamie Genniscbb1a952012-05-08 17:05:52 -0700331 if (useFiltering) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700332 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700333 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700334 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
335 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700336 glMatrixMode(GL_TEXTURE);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700337 glLoadMatrixf(textureMatrix);
Jamie Gennis9575f602011-10-07 14:51:16 -0700338 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700339 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700340 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700341 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700342 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700343 glMatrixMode(GL_TEXTURE);
344 glLoadIdentity();
345 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700346 glDisable(GL_TEXTURE_EXTERNAL_OES);
347 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700349
350 drawWithOpenGL(clip);
351
Mathias Agopianc492e672011-10-18 14:49:27 -0700352 glDisable(GL_TEXTURE_EXTERNAL_OES);
353 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354}
355
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800356// As documented in libhardware header, formats in the range
357// 0x100 - 0x1FF are specific to the HAL implementation, and
358// are known to have no alpha channel
359// TODO: move definition for device-specific range into
360// hardware.h, instead of using hard-coded values here.
361#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
362
Mathias Agopiana67932f2011-04-20 14:20:59 -0700363bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800364{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700365 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
366 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800367 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700368 PixelFormatInfo info;
369 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
370 // in case of error (unknown format), we assume no blending
371 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800372}
373
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800374
Mathias Agopiana67932f2011-04-20 14:20:59 -0700375bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700376{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700377 // if we don't have a buffer yet, we're translucent regardless of the
378 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700379 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700380 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700381 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382
383 // if the layer has the opaque flag, then we're always opaque,
384 // otherwise we use the current buffer's format.
385 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700386}
387
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800388bool Layer::isProtected() const
389{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700390 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800391 return (activeBuffer != 0) &&
392 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
393}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395uint32_t Layer::doTransaction(uint32_t flags)
396{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800397 ATRACE_CALL();
398
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 const Layer::State& front(drawingState());
400 const Layer::State& temp(currentState());
401
Mathias Agopiana138f892010-05-21 17:24:35 -0700402 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
403 (front.requested_h != temp.requested_h);
404
405 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700406 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000407 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700408 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700409 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700410 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700411 this,
412 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700413 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700414 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415
Mathias Agopiana138f892010-05-21 17:24:35 -0700416 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700417 // this will make sure LayerBase::doTransaction doesn't update
418 // the drawing state's size
419 Layer::State& editDraw(mDrawingState);
420 editDraw.requested_w = temp.requested_w;
421 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700423
424 // record the new size, form this point on, when the client request
425 // a buffer, it'll get the new size.
426 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
427 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 return LayerBase::doTransaction(flags);
431}
432
Mathias Agopiana138f892010-05-21 17:24:35 -0700433bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700434 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700435}
436
437bool Layer::isCropped() const {
438 return !mCurrentCrop.isEmpty();
439}
440
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441// ----------------------------------------------------------------------------
442// pageflip handling...
443// ----------------------------------------------------------------------------
444
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800445bool Layer::onPreComposition() {
446 mRefreshPending = false;
447 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800448}
449
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450void Layer::lockPageFlip(bool& recomputeVisibleRegions)
451{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800452 ATRACE_CALL();
453
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700454 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800455
456 // if we've already called updateTexImage() without going through
457 // a composition step, we have to skip this layer at this point
458 // because we cannot call updateTeximage() without a corresponding
459 // compositionComplete() call.
460 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800461 if (mRefreshPending) {
462 mPostedDirtyRegion.clear();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800463 return;
464 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800465 mRefreshPending = true;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800466
Jamie Gennis351a5132011-09-14 18:23:37 -0700467 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700468 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700469 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700470
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700471 // signal another event if we have more frames pending
472 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800473 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700474 }
475
Mathias Agopiana67932f2011-04-20 14:20:59 -0700476 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
477 // something happened!
478 recomputeVisibleRegions = true;
479 return;
480 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700481
Jamie Gennis351a5132011-09-14 18:23:37 -0700482 // update the active buffer
483 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800484 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700485
Mathias Agopianec923ee2012-02-27 16:58:04 -0800486 if (oldActiveBuffer == NULL && mActiveBuffer != NULL) {
487 // the first time we receive a buffer, we need to trigger a
488 // geometry invalidation.
489 mFlinger->invalidateHwcGeometry();
490 }
491
Mathias Agopiana67932f2011-04-20 14:20:59 -0700492 const Rect crop(mSurfaceTexture->getCurrentCrop());
493 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700494 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
495 if ((crop != mCurrentCrop) ||
496 (transform != mCurrentTransform) ||
497 (scalingMode != mCurrentScalingMode))
498 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700499 mCurrentCrop = crop;
500 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700501 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700502 mFlinger->invalidateHwcGeometry();
503 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800504
Jamie Gennis351a5132011-09-14 18:23:37 -0700505 uint32_t bufWidth = mActiveBuffer->getWidth();
506 uint32_t bufHeight = mActiveBuffer->getHeight();
507 if (oldActiveBuffer != NULL) {
508 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
509 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700510 mFlinger->invalidateHwcGeometry();
511 }
512 }
513
Jamie Gennis351a5132011-09-14 18:23:37 -0700514 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700515 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800516 recomputeVisibleRegions = true;
517 }
518
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700519 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
520 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700522 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700523 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700524
525 // FIXME: mPostedDirtyRegion = dirty & bounds
526 mPostedDirtyRegion.set(front.w, front.h);
527
Mathias Agopian97c602c2011-07-19 15:24:46 -0700528 if ((front.w != front.requested_w) ||
529 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700530 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700531 // check that we received a buffer of the right size
532 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700533 if (mCurrentTransform & Transform::ROT_90) {
534 swap(bufWidth, bufHeight);
535 }
536
537 if (isFixedSize() ||
538 (bufWidth == front.requested_w &&
539 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700540 {
541 // Here we pretend the transaction happened by updating the
542 // current and drawing states. Drawing state is only accessed
543 // in this thread, no need to have it locked
544 Layer::State& editDraw(mDrawingState);
545 editDraw.w = editDraw.requested_w;
546 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700547
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700548 // We also need to update the current state so that we don't
549 // end-up doing too much work during the next transaction.
550 // NOTE: We actually don't need hold the transaction lock here
551 // because State::w and State::h are only accessed from
552 // this thread
553 Layer::State& editTemp(currentState());
554 editTemp.w = editDraw.w;
555 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700556
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700557 // recompute visible region
558 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700559 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700560
Steve Block9d453682011-12-20 16:23:08 +0000561 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700562 "lockPageFlip : "
563 " (layer=%p), buffer (%ux%u, tr=%02x), "
564 "requested (%dx%d)",
565 this,
566 bufWidth, bufHeight, mCurrentTransform,
567 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700568 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700569 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570}
571
572void Layer::unlockPageFlip(
573 const Transform& planeTransform, Region& outDirtyRegion)
574{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800575 ATRACE_CALL();
576
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800577 Region postedRegion(mPostedDirtyRegion);
578 if (!postedRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 mPostedDirtyRegion.clear();
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800580 if (!visibleRegionScreen.isEmpty()) {
581 // The dirty region is given in the layer's coordinate space
582 // transform the dirty region by the surface's transformation
583 // and the global transformation.
584 const Layer::State& s(drawingState());
585 const Transform tr(planeTransform * s.transform);
586 postedRegion = tr.transform(postedRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800588 // At this point, the dirty region is in screen space.
589 // Make sure it's constrained by the visible region (which
590 // is in screen space as well).
591 postedRegion.andSelf(visibleRegionScreen);
592 outDirtyRegion.orSelf(postedRegion);
593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 }
595}
596
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700597void Layer::dump(String8& result, char* buffer, size_t SIZE) const
598{
599 LayerBaseClient::dump(result, buffer, SIZE);
600
Mathias Agopiana67932f2011-04-20 14:20:59 -0700601 sp<const GraphicBuffer> buf0(mActiveBuffer);
602 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700603 if (buf0 != 0) {
604 w0 = buf0->getWidth();
605 h0 = buf0->getHeight();
606 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700607 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700608 }
609 snprintf(buffer, SIZE,
610 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700611 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800612 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700613 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800614 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700615
616 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700617
Mathias Agopiana67932f2011-04-20 14:20:59 -0700618 if (mSurfaceTexture != 0) {
619 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700620 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700621}
622
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800623void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
624{
625 LayerBaseClient::dumpStats(result, buffer, SIZE);
626 const size_t o = mFrameLatencyOffset;
627 const DisplayHardware& hw(graphicPlane(0).displayHardware());
628 const nsecs_t period = hw.getRefreshPeriod();
629 result.appendFormat("%lld\n", period);
630 for (size_t i=0 ; i<128 ; i++) {
631 const size_t index = (o+i) % 128;
632 const nsecs_t time_app = mFrameStats[index].timestamp;
633 const nsecs_t time_set = mFrameStats[index].set;
634 const nsecs_t time_vsync = mFrameStats[index].vsync;
635 result.appendFormat("%lld\t%lld\t%lld\n",
636 time_app,
637 time_vsync,
638 time_set);
639 }
640 result.append("\n");
641}
642
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800643void Layer::clearStats()
644{
645 LayerBaseClient::clearStats();
646 memset(mFrameStats, 0, sizeof(mFrameStats));
647}
648
Mathias Agopiana67932f2011-04-20 14:20:59 -0700649uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700650{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700651 // TODO: should we do something special if mSecure is set?
652 if (mProtectedByApp) {
653 // need a hardware-protected path to external video sink
654 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700655 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700656 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700657 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700658}
659
Mathias Agopiana4583642011-08-23 18:03:18 -0700660uint32_t Layer::getTransformHint() const {
661 uint32_t orientation = 0;
662 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700663 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700664 if (orientation & Transform::ROT_INVALID) {
665 orientation = 0;
666 }
667 }
668 return orientation;
669}
670
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671// ---------------------------------------------------------------------------
672
673
674}; // namespace android