blob: 6bc80eea9cd1e550604f9f0a0205dc1900b89f70 [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
Jamie Gennis161534a2012-05-07 13:51:53 -0700144 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
145 !mCurrentCrop.isEmpty()) {
146 // We need to shrink the window size to match the buffer crop
147 // rectangle.
148 const Layer::State& s(drawingState());
149 const Transform tr(globalTransform * s.transform);
150 float windowWidth = s.w;
151 float windowHeight = s.h;
152 float bufferWidth = mActiveBuffer->getWidth();
153 float bufferHeight = mActiveBuffer->getHeight();
154 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
155 float tmp = bufferWidth;
156 bufferWidth = bufferHeight;
157 bufferHeight = tmp;
158 }
159 float xScale = float(windowWidth) / float(bufferWidth);
160 float yScale = float(windowHeight) / float(bufferHeight);
161
162 // Compute the crop in post-transform coordinates.
163 Rect crop(mCurrentCrop.transform(mCurrentTransform,
164 mActiveBuffer->getWidth(), mActiveBuffer->getHeight()));
165
166 float left = ceil(xScale * float(crop.left));
167 float right = floor(xScale * float(crop.right));
168 float top = ceil(yScale * float(crop.top));
169 float bottom = floor(yScale * float(crop.bottom));
170
171 tr.transform(mVertices[0], left, top);
172 tr.transform(mVertices[1], left, bottom);
173 tr.transform(mVertices[2], right, bottom);
174 tr.transform(mVertices[3], right, top);
175
176 const DisplayHardware& hw(graphicPlane(0).displayHardware());
177 const uint32_t hw_h = hw.getHeight();
178 for (size_t i=0 ; i<4 ; i++)
179 mVertices[i][1] = hw_h - mVertices[i][1];
180
181 mTransformedBounds = tr.transform(
182 Rect(int(left), int(top), int(right), int(bottom)));
183 }
184
Daniel Lamb2675792012-02-23 14:35:13 -0800185 // This optimization allows the SurfaceTexture to bake in
186 // the rotation so hardware overlays can be used
187 mSurfaceTexture->setTransformHint(getTransformHint());
188}
189
Mathias Agopiana67932f2011-04-20 14:20:59 -0700190sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700192 class BSurface : public BnSurface, public LayerCleaner {
193 wp<const Layer> mOwner;
194 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
195 sp<ISurfaceTexture> res;
196 sp<const Layer> that( mOwner.promote() );
197 if (that != NULL) {
Daniel Lamb2675792012-02-23 14:35:13 -0800198 res = that->mSurfaceTexture->getBufferQueue();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700199 }
200 return res;
201 }
202 public:
203 BSurface(const sp<SurfaceFlinger>& flinger,
204 const sp<Layer>& layer)
205 : LayerCleaner(flinger, layer), mOwner(layer) { }
206 };
207 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800208 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209}
210
Jamie Gennis582270d2011-08-17 18:19:00 -0700211wp<IBinder> Layer::getSurfaceTextureBinder() const
212{
Daniel Lamb2675792012-02-23 14:35:13 -0800213 return mSurfaceTexture->getBufferQueue()->asBinder();
Jamie Gennis582270d2011-08-17 18:19:00 -0700214}
215
Mathias Agopianf9d93272009-06-19 17:00:27 -0700216status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 PixelFormat format, uint32_t flags)
218{
Mathias Agopian401c2572009-09-23 19:16:27 -0700219 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 PixelFormatInfo info;
221 status_t err = getPixelFormatInfo(format, &info);
Mathias Agopianff615cc2012-02-24 14:58:36 -0800222 if (err) {
223 ALOGE("unsupported pixelformat %d", format);
224 return err;
225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226
Mathias Agopian401c2572009-09-23 19:16:27 -0700227 // the display's pixel format
228 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700229 uint32_t const maxSurfaceDims = min(
230 hw.getMaxTextureSize(), hw.getMaxViewportDims());
231
232 // never allow a surface larger than what our underlying GL implementation
233 // can handle.
234 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800235 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700236 return BAD_VALUE;
237 }
238
Mathias Agopian401c2572009-09-23 19:16:27 -0700239 PixelFormatInfo displayInfo;
240 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700241 const uint32_t hwFlags = hw.getFlags();
242
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700243 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700244
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800246 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700247 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
248 mCurrentOpacity = getOpacityForFormat(format);
249
250 mSurfaceTexture->setDefaultBufferSize(w, h);
251 mSurfaceTexture->setDefaultBufferFormat(format);
Daniel Lamb2675792012-02-23 14:35:13 -0800252 mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700253
Mathias Agopian401c2572009-09-23 19:16:27 -0700254 // we use the red index
255 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
256 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
257 mNeedsDithering = layerRedsize > displayRedSize;
258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 return NO_ERROR;
260}
261
Mathias Agopiana350ff92010-08-10 17:14:02 -0700262void Layer::setGeometry(hwc_layer_t* hwcl)
263{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700264 LayerBaseClient::setGeometry(hwcl);
265
266 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700267
268 // we can't do alpha-fade with the hwc HAL
269 const State& s(drawingState());
270 if (s.alpha < 0xFF) {
271 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700272 }
273
Mathias Agopian29a367b2011-07-12 14:51:45 -0700274 /*
275 * Transformations are applied in this order:
276 * 1) buffer orientation/flip/mirror
277 * 2) state transformation (window manager)
278 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700279 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700280 * (NOTE: the matrices are multiplied in reverse order)
281 */
282
283 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700284 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700285
286 // this gives us only the "orientation" component of the transform
287 const uint32_t finalTransform = tr.getOrientation();
288
Mathias Agopiana350ff92010-08-10 17:14:02 -0700289 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700290 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700291 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700292 } else {
293 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700294 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700295
296 if (isCropped()) {
297 hwcl->sourceCrop.left = mCurrentCrop.left;
298 hwcl->sourceCrop.top = mCurrentCrop.top;
299 hwcl->sourceCrop.right = mCurrentCrop.right;
300 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
301 } else {
302 const sp<GraphicBuffer>& buffer(mActiveBuffer);
303 hwcl->sourceCrop.left = 0;
304 hwcl->sourceCrop.top = 0;
305 if (buffer != NULL) {
306 hwcl->sourceCrop.right = buffer->width;
307 hwcl->sourceCrop.bottom = buffer->height;
308 } else {
309 hwcl->sourceCrop.right = mTransformedBounds.width();
310 hwcl->sourceCrop.bottom = mTransformedBounds.height();
311 }
312 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700313}
314
315void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700317 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800318 // this can happen if the client never drew into this layer yet,
319 // or if we ran out of memory. In that case, don't let
320 // HWC handle it.
321 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700322 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700323 } else {
324 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700325 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700326}
327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328void Layer::onDraw(const Region& clip) const
329{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800330 ATRACE_CALL();
331
Mathias Agopiana67932f2011-04-20 14:20:59 -0700332 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700334 // in fact never been drawn into. This happens frequently with
335 // SurfaceView because the WindowManager can't know when the client
336 // has drawn the first time.
337
338 // If there is nothing under us, we paint the screen in black, otherwise
339 // we just skip this update.
340
341 // figure out if there is something below us
342 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700343 const SurfaceFlinger::LayerVector& drawingLayers(
344 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700345 const size_t count = drawingLayers.size();
346 for (size_t i=0 ; i<count ; ++i) {
347 const sp<LayerBase>& layer(drawingLayers[i]);
348 if (layer.get() == static_cast<LayerBase const*>(this))
349 break;
350 under.orSelf(layer->visibleRegionScreen);
351 }
352 // if not everything below us is covered, we plug the holes!
353 Region holes(clip.subtract(under));
354 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700355 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700356 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 return;
358 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700359
Jamie Gennis9575f602011-10-07 14:51:16 -0700360 if (!isProtected()) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700361 // TODO: we could be more subtle with isFixedSize()
362 const bool useFiltering = getFiltering() || needsFiltering() || isFixedSize();
363
364 // Query the texture matrix given our current filtering mode.
365 float textureMatrix[16];
366 mSurfaceTexture->setFilteringEnabled(useFiltering);
367 mSurfaceTexture->getTransformMatrix(textureMatrix);
368
369 // Set things up for texturing.
Mathias Agopianc492e672011-10-18 14:49:27 -0700370 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
371 GLenum filter = GL_NEAREST;
Jamie Genniscbb1a952012-05-08 17:05:52 -0700372 if (useFiltering) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700373 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700374 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700375 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
376 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700377 glMatrixMode(GL_TEXTURE);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700378 glLoadMatrixf(textureMatrix);
Jamie Gennis9575f602011-10-07 14:51:16 -0700379 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700380 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700381 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700383 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700384 glMatrixMode(GL_TEXTURE);
385 glLoadIdentity();
386 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700387 glDisable(GL_TEXTURE_EXTERNAL_OES);
388 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700389 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700390
391 drawWithOpenGL(clip);
392
Mathias Agopianc492e672011-10-18 14:49:27 -0700393 glDisable(GL_TEXTURE_EXTERNAL_OES);
394 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395}
396
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800397// As documented in libhardware header, formats in the range
398// 0x100 - 0x1FF are specific to the HAL implementation, and
399// are known to have no alpha channel
400// TODO: move definition for device-specific range into
401// hardware.h, instead of using hard-coded values here.
402#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
403
Mathias Agopiana67932f2011-04-20 14:20:59 -0700404bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800405{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700406 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
407 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800408 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700409 PixelFormatInfo info;
410 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
411 // in case of error (unknown format), we assume no blending
412 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800413}
414
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800415
Mathias Agopiana67932f2011-04-20 14:20:59 -0700416bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700417{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700418 // if we don't have a buffer yet, we're translucent regardless of the
419 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700420 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700421 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700422 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700423
424 // if the layer has the opaque flag, then we're always opaque,
425 // otherwise we use the current buffer's format.
426 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700427}
428
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800429bool Layer::isProtected() const
430{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700431 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800432 return (activeBuffer != 0) &&
433 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
434}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436uint32_t Layer::doTransaction(uint32_t flags)
437{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800438 ATRACE_CALL();
439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 const Layer::State& front(drawingState());
441 const Layer::State& temp(currentState());
442
Mathias Agopiana138f892010-05-21 17:24:35 -0700443 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
444 (front.requested_h != temp.requested_h);
445
446 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700447 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000448 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700449 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700450 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700451 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700452 this,
453 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700454 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700455 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456
Mathias Agopiana138f892010-05-21 17:24:35 -0700457 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700458 // this will make sure LayerBase::doTransaction doesn't update
459 // the drawing state's size
460 Layer::State& editDraw(mDrawingState);
461 editDraw.requested_w = temp.requested_w;
462 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700464
465 // record the new size, form this point on, when the client request
466 // a buffer, it'll get the new size.
467 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
468 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 return LayerBase::doTransaction(flags);
472}
473
Mathias Agopiana138f892010-05-21 17:24:35 -0700474bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700475 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700476}
477
478bool Layer::isCropped() const {
479 return !mCurrentCrop.isEmpty();
480}
481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482// ----------------------------------------------------------------------------
483// pageflip handling...
484// ----------------------------------------------------------------------------
485
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800486bool Layer::onPreComposition() {
487 mRefreshPending = false;
488 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800489}
490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491void Layer::lockPageFlip(bool& recomputeVisibleRegions)
492{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800493 ATRACE_CALL();
494
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700495 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800496
497 // if we've already called updateTexImage() without going through
498 // a composition step, we have to skip this layer at this point
499 // because we cannot call updateTeximage() without a corresponding
500 // compositionComplete() call.
501 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800502 if (mRefreshPending) {
503 mPostedDirtyRegion.clear();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800504 return;
505 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800506 mRefreshPending = true;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800507
Jamie Gennis351a5132011-09-14 18:23:37 -0700508 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700509 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700510 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700511
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700512 // signal another event if we have more frames pending
513 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800514 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700515 }
516
Mathias Agopiana67932f2011-04-20 14:20:59 -0700517 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
518 // something happened!
519 recomputeVisibleRegions = true;
520 return;
521 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700522
Jamie Gennis351a5132011-09-14 18:23:37 -0700523 // update the active buffer
524 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800525 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700526
Mathias Agopianec923ee2012-02-27 16:58:04 -0800527 if (oldActiveBuffer == NULL && mActiveBuffer != NULL) {
528 // the first time we receive a buffer, we need to trigger a
529 // geometry invalidation.
530 mFlinger->invalidateHwcGeometry();
531 }
532
Jamie Gennis161534a2012-05-07 13:51:53 -0700533 Rect crop(mSurfaceTexture->getCurrentCrop());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700534 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700535 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
536 if ((crop != mCurrentCrop) ||
537 (transform != mCurrentTransform) ||
538 (scalingMode != mCurrentScalingMode))
539 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700540 mCurrentCrop = crop;
541 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700542 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700543 mFlinger->invalidateHwcGeometry();
Jamie Gennis51dcd582012-05-10 14:37:14 -0700544 recomputeVisibleRegions = true;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700545 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800546
Jamie Gennis351a5132011-09-14 18:23:37 -0700547 uint32_t bufWidth = mActiveBuffer->getWidth();
548 uint32_t bufHeight = mActiveBuffer->getHeight();
549 if (oldActiveBuffer != NULL) {
550 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
551 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700552 mFlinger->invalidateHwcGeometry();
Jamie Gennis51dcd582012-05-10 14:37:14 -0700553 recomputeVisibleRegions = true;
Mathias Agopianc7f33812011-08-30 15:02:41 -0700554 }
555 }
556
Jamie Gennis351a5132011-09-14 18:23:37 -0700557 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700558 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800559 recomputeVisibleRegions = true;
560 }
561
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700562 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
563 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700565 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700566 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700567
568 // FIXME: mPostedDirtyRegion = dirty & bounds
569 mPostedDirtyRegion.set(front.w, front.h);
570
Mathias Agopian97c602c2011-07-19 15:24:46 -0700571 if ((front.w != front.requested_w) ||
572 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700573 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700574 // check that we received a buffer of the right size
575 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700576 if (mCurrentTransform & Transform::ROT_90) {
577 swap(bufWidth, bufHeight);
578 }
579
580 if (isFixedSize() ||
581 (bufWidth == front.requested_w &&
582 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700583 {
584 // Here we pretend the transaction happened by updating the
585 // current and drawing states. Drawing state is only accessed
586 // in this thread, no need to have it locked
587 Layer::State& editDraw(mDrawingState);
588 editDraw.w = editDraw.requested_w;
589 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700590
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700591 // We also need to update the current state so that we don't
592 // end-up doing too much work during the next transaction.
593 // NOTE: We actually don't need hold the transaction lock here
594 // because State::w and State::h are only accessed from
595 // this thread
596 Layer::State& editTemp(currentState());
597 editTemp.w = editDraw.w;
598 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700599
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700600 // recompute visible region
601 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700602 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700603
Steve Block9d453682011-12-20 16:23:08 +0000604 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700605 "lockPageFlip : "
606 " (layer=%p), buffer (%ux%u, tr=%02x), "
607 "requested (%dx%d)",
608 this,
609 bufWidth, bufHeight, mCurrentTransform,
610 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700611 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700612 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613}
614
615void Layer::unlockPageFlip(
616 const Transform& planeTransform, Region& outDirtyRegion)
617{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800618 ATRACE_CALL();
619
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800620 Region postedRegion(mPostedDirtyRegion);
621 if (!postedRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 mPostedDirtyRegion.clear();
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800623 if (!visibleRegionScreen.isEmpty()) {
624 // The dirty region is given in the layer's coordinate space
625 // transform the dirty region by the surface's transformation
626 // and the global transformation.
627 const Layer::State& s(drawingState());
628 const Transform tr(planeTransform * s.transform);
629 postedRegion = tr.transform(postedRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800631 // At this point, the dirty region is in screen space.
632 // Make sure it's constrained by the visible region (which
633 // is in screen space as well).
634 postedRegion.andSelf(visibleRegionScreen);
635 outDirtyRegion.orSelf(postedRegion);
636 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638}
639
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700640void Layer::dump(String8& result, char* buffer, size_t SIZE) const
641{
642 LayerBaseClient::dump(result, buffer, SIZE);
643
Mathias Agopiana67932f2011-04-20 14:20:59 -0700644 sp<const GraphicBuffer> buf0(mActiveBuffer);
645 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700646 if (buf0 != 0) {
647 w0 = buf0->getWidth();
648 h0 = buf0->getHeight();
649 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700650 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700651 }
652 snprintf(buffer, SIZE,
653 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700654 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800655 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700656 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800657 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700658
659 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700660
Mathias Agopiana67932f2011-04-20 14:20:59 -0700661 if (mSurfaceTexture != 0) {
662 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700663 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700664}
665
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800666void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
667{
668 LayerBaseClient::dumpStats(result, buffer, SIZE);
669 const size_t o = mFrameLatencyOffset;
670 const DisplayHardware& hw(graphicPlane(0).displayHardware());
671 const nsecs_t period = hw.getRefreshPeriod();
672 result.appendFormat("%lld\n", period);
673 for (size_t i=0 ; i<128 ; i++) {
674 const size_t index = (o+i) % 128;
675 const nsecs_t time_app = mFrameStats[index].timestamp;
676 const nsecs_t time_set = mFrameStats[index].set;
677 const nsecs_t time_vsync = mFrameStats[index].vsync;
678 result.appendFormat("%lld\t%lld\t%lld\n",
679 time_app,
680 time_vsync,
681 time_set);
682 }
683 result.append("\n");
684}
685
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800686void Layer::clearStats()
687{
688 LayerBaseClient::clearStats();
689 memset(mFrameStats, 0, sizeof(mFrameStats));
690}
691
Mathias Agopiana67932f2011-04-20 14:20:59 -0700692uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700693{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700694 // TODO: should we do something special if mSecure is set?
695 if (mProtectedByApp) {
696 // need a hardware-protected path to external video sink
697 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700698 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700699 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700700 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700701}
702
Mathias Agopiana4583642011-08-23 18:03:18 -0700703uint32_t Layer::getTransformHint() const {
704 uint32_t orientation = 0;
705 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700706 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700707 if (orientation & Transform::ROT_INVALID) {
708 orientation = 0;
709 }
710 }
711 return orientation;
712}
713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714// ---------------------------------------------------------------------------
715
716
717}; // namespace android