blob: 46430714482a4b96f5b0bfbaaa84059e2dcc490d [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,
104 GL_TEXTURE_EXTERNAL_OES, false,bq);
105
106
107
108 mSurfaceTexture->setTransformHint(getTransformHint());
109 mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopiana67932f2011-04-20 14:20:59 -0700110 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
111 mSurfaceTexture->setSynchronousMode(true);
Daniel Lamb2675792012-02-23 14:35:13 -0800112
Mathias Agopian303d5382012-02-05 01:49:16 -0800113#ifdef USE_TRIPLE_BUFFERING
114#warning "using triple buffering"
115 mSurfaceTexture->setBufferCountServer(3);
116#else
Mathias Agopiana67932f2011-04-20 14:20:59 -0700117 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800118#endif
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119}
120
Mathias Agopiana67932f2011-04-20 14:20:59 -0700121Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700122{
Mathias Agopian118d0242011-10-13 16:02:48 -0700123 mFlinger->postMessageAsync(
124 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700125}
126
Mathias Agopiana67932f2011-04-20 14:20:59 -0700127void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700128 android_atomic_inc(&mQueuedFrames);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800129 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700130}
131
Mathias Agopiand606de62010-05-10 20:06:11 -0700132// called with SurfaceFlinger::mStateLock as soon as the layer is entered
133// in the purgatory list
134void Layer::onRemoved()
135{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700136 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700137}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700138
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700139void Layer::setName(const String8& name) {
140 LayerBase::setName(name);
141 mSurfaceTexture->setName(name);
142}
143
Daniel Lamb2675792012-02-23 14:35:13 -0800144void Layer::validateVisibility(const Transform& globalTransform) {
145 LayerBase::validateVisibility(globalTransform);
146
147 // This optimization allows the SurfaceTexture to bake in
148 // the rotation so hardware overlays can be used
149 mSurfaceTexture->setTransformHint(getTransformHint());
150}
151
Mathias Agopiana67932f2011-04-20 14:20:59 -0700152sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700154 class BSurface : public BnSurface, public LayerCleaner {
155 wp<const Layer> mOwner;
156 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
157 sp<ISurfaceTexture> res;
158 sp<const Layer> that( mOwner.promote() );
159 if (that != NULL) {
Daniel Lamb2675792012-02-23 14:35:13 -0800160 res = that->mSurfaceTexture->getBufferQueue();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700161 }
162 return res;
163 }
164 public:
165 BSurface(const sp<SurfaceFlinger>& flinger,
166 const sp<Layer>& layer)
167 : LayerCleaner(flinger, layer), mOwner(layer) { }
168 };
169 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800170 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171}
172
Jamie Gennis582270d2011-08-17 18:19:00 -0700173wp<IBinder> Layer::getSurfaceTextureBinder() const
174{
Daniel Lamb2675792012-02-23 14:35:13 -0800175 return mSurfaceTexture->getBufferQueue()->asBinder();
Jamie Gennis582270d2011-08-17 18:19:00 -0700176}
177
Mathias Agopianf9d93272009-06-19 17:00:27 -0700178status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 PixelFormat format, uint32_t flags)
180{
Mathias Agopian401c2572009-09-23 19:16:27 -0700181 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 PixelFormatInfo info;
183 status_t err = getPixelFormatInfo(format, &info);
Mathias Agopianff615cc2012-02-24 14:58:36 -0800184 if (err) {
185 ALOGE("unsupported pixelformat %d", format);
186 return err;
187 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
Mathias Agopian401c2572009-09-23 19:16:27 -0700189 // the display's pixel format
190 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700191 uint32_t const maxSurfaceDims = min(
192 hw.getMaxTextureSize(), hw.getMaxViewportDims());
193
194 // never allow a surface larger than what our underlying GL implementation
195 // can handle.
196 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800197 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700198 return BAD_VALUE;
199 }
200
Mathias Agopian401c2572009-09-23 19:16:27 -0700201 PixelFormatInfo displayInfo;
202 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700203 const uint32_t hwFlags = hw.getFlags();
204
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700205 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700206
Mathias Agopian3330b202009-10-05 17:07:12 -0700207 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800208 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700209 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
210 mCurrentOpacity = getOpacityForFormat(format);
211
212 mSurfaceTexture->setDefaultBufferSize(w, h);
213 mSurfaceTexture->setDefaultBufferFormat(format);
Daniel Lamb2675792012-02-23 14:35:13 -0800214 mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700215
Mathias Agopian401c2572009-09-23 19:16:27 -0700216 // we use the red index
217 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
218 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
219 mNeedsDithering = layerRedsize > displayRedSize;
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 return NO_ERROR;
222}
223
Mathias Agopiana350ff92010-08-10 17:14:02 -0700224void Layer::setGeometry(hwc_layer_t* hwcl)
225{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700226 LayerBaseClient::setGeometry(hwcl);
227
228 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229
230 // we can't do alpha-fade with the hwc HAL
231 const State& s(drawingState());
232 if (s.alpha < 0xFF) {
233 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700234 }
235
Mathias Agopian29a367b2011-07-12 14:51:45 -0700236 /*
237 * Transformations are applied in this order:
238 * 1) buffer orientation/flip/mirror
239 * 2) state transformation (window manager)
240 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700241 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700242 * (NOTE: the matrices are multiplied in reverse order)
243 */
244
245 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700246 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700247
248 // this gives us only the "orientation" component of the transform
249 const uint32_t finalTransform = tr.getOrientation();
250
Mathias Agopiana350ff92010-08-10 17:14:02 -0700251 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700252 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700254 } else {
255 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700256 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700257
258 if (isCropped()) {
259 hwcl->sourceCrop.left = mCurrentCrop.left;
260 hwcl->sourceCrop.top = mCurrentCrop.top;
261 hwcl->sourceCrop.right = mCurrentCrop.right;
262 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
263 } else {
264 const sp<GraphicBuffer>& buffer(mActiveBuffer);
265 hwcl->sourceCrop.left = 0;
266 hwcl->sourceCrop.top = 0;
267 if (buffer != NULL) {
268 hwcl->sourceCrop.right = buffer->width;
269 hwcl->sourceCrop.bottom = buffer->height;
270 } else {
271 hwcl->sourceCrop.right = mTransformedBounds.width();
272 hwcl->sourceCrop.bottom = mTransformedBounds.height();
273 }
274 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700275}
276
277void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700278 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700279 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800280 // this can happen if the client never drew into this layer yet,
281 // or if we ran out of memory. In that case, don't let
282 // HWC handle it.
283 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700284 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700285 } else {
286 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700287 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700288}
289
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290void Layer::onDraw(const Region& clip) const
291{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800292 ATRACE_CALL();
293
Mathias Agopiana67932f2011-04-20 14:20:59 -0700294 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700296 // in fact never been drawn into. This happens frequently with
297 // SurfaceView because the WindowManager can't know when the client
298 // has drawn the first time.
299
300 // If there is nothing under us, we paint the screen in black, otherwise
301 // we just skip this update.
302
303 // figure out if there is something below us
304 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700305 const SurfaceFlinger::LayerVector& drawingLayers(
306 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700307 const size_t count = drawingLayers.size();
308 for (size_t i=0 ; i<count ; ++i) {
309 const sp<LayerBase>& layer(drawingLayers[i]);
310 if (layer.get() == static_cast<LayerBase const*>(this))
311 break;
312 under.orSelf(layer->visibleRegionScreen);
313 }
314 // if not everything below us is covered, we plug the holes!
315 Region holes(clip.subtract(under));
316 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700317 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 return;
320 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700321
Jamie Gennis9575f602011-10-07 14:51:16 -0700322 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700323 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
324 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700325 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
326 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700327 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700328 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700329 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
330 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700331 glMatrixMode(GL_TEXTURE);
332 glLoadMatrixf(mTextureMatrix);
333 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700334 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700335 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700336 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700337 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700338 glMatrixMode(GL_TEXTURE);
339 glLoadIdentity();
340 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700341 glDisable(GL_TEXTURE_EXTERNAL_OES);
342 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700343 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344
345 drawWithOpenGL(clip);
346
Mathias Agopianc492e672011-10-18 14:49:27 -0700347 glDisable(GL_TEXTURE_EXTERNAL_OES);
348 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800351// As documented in libhardware header, formats in the range
352// 0x100 - 0x1FF are specific to the HAL implementation, and
353// are known to have no alpha channel
354// TODO: move definition for device-specific range into
355// hardware.h, instead of using hard-coded values here.
356#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
357
Mathias Agopiana67932f2011-04-20 14:20:59 -0700358bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800359{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700360 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
361 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800362 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700363 PixelFormatInfo info;
364 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
365 // in case of error (unknown format), we assume no blending
366 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800367}
368
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800369
Mathias Agopiana67932f2011-04-20 14:20:59 -0700370bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700371{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700372 // if we don't have a buffer yet, we're translucent regardless of the
373 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700374 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700375 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700376 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700377
378 // if the layer has the opaque flag, then we're always opaque,
379 // otherwise we use the current buffer's format.
380 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700381}
382
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800383bool Layer::isProtected() const
384{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700385 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800386 return (activeBuffer != 0) &&
387 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
388}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390uint32_t Layer::doTransaction(uint32_t flags)
391{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800392 ATRACE_CALL();
393
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 const Layer::State& front(drawingState());
395 const Layer::State& temp(currentState());
396
Mathias Agopiana138f892010-05-21 17:24:35 -0700397 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
398 (front.requested_h != temp.requested_h);
399
400 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700401 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000402 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700403 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700404 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700405 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700406 this,
407 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700408 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700409 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410
Mathias Agopiana138f892010-05-21 17:24:35 -0700411 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700412 // this will make sure LayerBase::doTransaction doesn't update
413 // the drawing state's size
414 Layer::State& editDraw(mDrawingState);
415 editDraw.requested_w = temp.requested_w;
416 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700418
419 // record the new size, form this point on, when the client request
420 // a buffer, it'll get the new size.
421 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
422 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 return LayerBase::doTransaction(flags);
426}
427
Mathias Agopiana138f892010-05-21 17:24:35 -0700428bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700429 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700430}
431
432bool Layer::isCropped() const {
433 return !mCurrentCrop.isEmpty();
434}
435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436// ----------------------------------------------------------------------------
437// pageflip handling...
438// ----------------------------------------------------------------------------
439
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800440bool Layer::onPreComposition() {
441 mRefreshPending = false;
442 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800443}
444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445void Layer::lockPageFlip(bool& recomputeVisibleRegions)
446{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800447 ATRACE_CALL();
448
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700449 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800450
451 // if we've already called updateTexImage() without going through
452 // a composition step, we have to skip this layer at this point
453 // because we cannot call updateTeximage() without a corresponding
454 // compositionComplete() call.
455 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800456 if (mRefreshPending) {
457 mPostedDirtyRegion.clear();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800458 return;
459 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800460 mRefreshPending = true;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800461
Jamie Gennis351a5132011-09-14 18:23:37 -0700462 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700463 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700464 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700465
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700466 // signal another event if we have more frames pending
467 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800468 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700469 }
470
Mathias Agopiana67932f2011-04-20 14:20:59 -0700471 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
472 // something happened!
473 recomputeVisibleRegions = true;
474 return;
475 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700476
Jamie Gennis351a5132011-09-14 18:23:37 -0700477 // update the active buffer
478 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800479 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700480
Mathias Agopianec923ee2012-02-27 16:58:04 -0800481 if (oldActiveBuffer == NULL && mActiveBuffer != NULL) {
482 // the first time we receive a buffer, we need to trigger a
483 // geometry invalidation.
484 mFlinger->invalidateHwcGeometry();
485 }
486
Mathias Agopiana67932f2011-04-20 14:20:59 -0700487 const Rect crop(mSurfaceTexture->getCurrentCrop());
488 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700489 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
490 if ((crop != mCurrentCrop) ||
491 (transform != mCurrentTransform) ||
492 (scalingMode != mCurrentScalingMode))
493 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700494 mCurrentCrop = crop;
495 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700496 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700497 mFlinger->invalidateHwcGeometry();
498 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800499
Mathias Agopianc7f33812011-08-30 15:02:41 -0700500 GLfloat textureMatrix[16];
501 mSurfaceTexture->getTransformMatrix(textureMatrix);
502 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
503 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
504 mFlinger->invalidateHwcGeometry();
505 }
506
Jamie Gennis351a5132011-09-14 18:23:37 -0700507 uint32_t bufWidth = mActiveBuffer->getWidth();
508 uint32_t bufHeight = mActiveBuffer->getHeight();
509 if (oldActiveBuffer != NULL) {
510 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
511 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700512 mFlinger->invalidateHwcGeometry();
513 }
514 }
515
Jamie Gennis351a5132011-09-14 18:23:37 -0700516 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700517 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800518 recomputeVisibleRegions = true;
519 }
520
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700521 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
522 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700523
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700524 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700525 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700526
527 // FIXME: mPostedDirtyRegion = dirty & bounds
528 mPostedDirtyRegion.set(front.w, front.h);
529
Mathias Agopian97c602c2011-07-19 15:24:46 -0700530 if ((front.w != front.requested_w) ||
531 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700532 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700533 // check that we received a buffer of the right size
534 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700535 if (mCurrentTransform & Transform::ROT_90) {
536 swap(bufWidth, bufHeight);
537 }
538
539 if (isFixedSize() ||
540 (bufWidth == front.requested_w &&
541 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700542 {
543 // Here we pretend the transaction happened by updating the
544 // current and drawing states. Drawing state is only accessed
545 // in this thread, no need to have it locked
546 Layer::State& editDraw(mDrawingState);
547 editDraw.w = editDraw.requested_w;
548 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700549
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700550 // We also need to update the current state so that we don't
551 // end-up doing too much work during the next transaction.
552 // NOTE: We actually don't need hold the transaction lock here
553 // because State::w and State::h are only accessed from
554 // this thread
555 Layer::State& editTemp(currentState());
556 editTemp.w = editDraw.w;
557 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700558
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700559 // recompute visible region
560 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700561 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700562
Steve Block9d453682011-12-20 16:23:08 +0000563 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700564 "lockPageFlip : "
565 " (layer=%p), buffer (%ux%u, tr=%02x), "
566 "requested (%dx%d)",
567 this,
568 bufWidth, bufHeight, mCurrentTransform,
569 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700570 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700571 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572}
573
574void Layer::unlockPageFlip(
575 const Transform& planeTransform, Region& outDirtyRegion)
576{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800577 ATRACE_CALL();
578
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800579 Region postedRegion(mPostedDirtyRegion);
580 if (!postedRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 mPostedDirtyRegion.clear();
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800582 if (!visibleRegionScreen.isEmpty()) {
583 // The dirty region is given in the layer's coordinate space
584 // transform the dirty region by the surface's transformation
585 // and the global transformation.
586 const Layer::State& s(drawingState());
587 const Transform tr(planeTransform * s.transform);
588 postedRegion = tr.transform(postedRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800590 // At this point, the dirty region is in screen space.
591 // Make sure it's constrained by the visible region (which
592 // is in screen space as well).
593 postedRegion.andSelf(visibleRegionScreen);
594 outDirtyRegion.orSelf(postedRegion);
595 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 }
597}
598
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700599void Layer::dump(String8& result, char* buffer, size_t SIZE) const
600{
601 LayerBaseClient::dump(result, buffer, SIZE);
602
Mathias Agopiana67932f2011-04-20 14:20:59 -0700603 sp<const GraphicBuffer> buf0(mActiveBuffer);
604 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700605 if (buf0 != 0) {
606 w0 = buf0->getWidth();
607 h0 = buf0->getHeight();
608 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700609 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700610 }
611 snprintf(buffer, SIZE,
612 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700613 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800614 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700615 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800616 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700617
618 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700619
Mathias Agopiana67932f2011-04-20 14:20:59 -0700620 if (mSurfaceTexture != 0) {
621 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700622 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700623}
624
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800625void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
626{
627 LayerBaseClient::dumpStats(result, buffer, SIZE);
628 const size_t o = mFrameLatencyOffset;
629 const DisplayHardware& hw(graphicPlane(0).displayHardware());
630 const nsecs_t period = hw.getRefreshPeriod();
631 result.appendFormat("%lld\n", period);
632 for (size_t i=0 ; i<128 ; i++) {
633 const size_t index = (o+i) % 128;
634 const nsecs_t time_app = mFrameStats[index].timestamp;
635 const nsecs_t time_set = mFrameStats[index].set;
636 const nsecs_t time_vsync = mFrameStats[index].vsync;
637 result.appendFormat("%lld\t%lld\t%lld\n",
638 time_app,
639 time_vsync,
640 time_set);
641 }
642 result.append("\n");
643}
644
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800645void Layer::clearStats()
646{
647 LayerBaseClient::clearStats();
648 memset(mFrameStats, 0, sizeof(mFrameStats));
649}
650
Mathias Agopiana67932f2011-04-20 14:20:59 -0700651uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700652{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700653 // TODO: should we do something special if mSecure is set?
654 if (mProtectedByApp) {
655 // need a hardware-protected path to external video sink
656 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700657 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700658 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700659 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700660}
661
Mathias Agopiana4583642011-08-23 18:03:18 -0700662uint32_t Layer::getTransformHint() const {
663 uint32_t orientation = 0;
664 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700665 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700666 if (orientation & Transform::ROT_INVALID) {
667 orientation = 0;
668 }
669 }
670 return orientation;
671}
672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673// ---------------------------------------------------------------------------
674
675
676}; // namespace android