blob: df7fe5c49ecf1413323d71d4e105fecd71b0379a [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 };
100 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
101 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
102 mSurfaceTexture->setSynchronousMode(true);
Mathias Agopian303d5382012-02-05 01:49:16 -0800103#ifdef USE_TRIPLE_BUFFERING
104#warning "using triple buffering"
105 mSurfaceTexture->setBufferCountServer(3);
106#else
Mathias Agopiana67932f2011-04-20 14:20:59 -0700107 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800108#endif
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700109}
110
Mathias Agopiana67932f2011-04-20 14:20:59 -0700111Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700112{
Mathias Agopian118d0242011-10-13 16:02:48 -0700113 mFlinger->postMessageAsync(
114 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700115}
116
Mathias Agopiana67932f2011-04-20 14:20:59 -0700117void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700118 android_atomic_inc(&mQueuedFrames);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800119 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700120}
121
Mathias Agopiand606de62010-05-10 20:06:11 -0700122// called with SurfaceFlinger::mStateLock as soon as the layer is entered
123// in the purgatory list
124void Layer::onRemoved()
125{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700126 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700127}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700128
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700129void Layer::setName(const String8& name) {
130 LayerBase::setName(name);
131 mSurfaceTexture->setName(name);
132}
133
Mathias Agopiana67932f2011-04-20 14:20:59 -0700134sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700136 class BSurface : public BnSurface, public LayerCleaner {
137 wp<const Layer> mOwner;
138 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
139 sp<ISurfaceTexture> res;
140 sp<const Layer> that( mOwner.promote() );
141 if (that != NULL) {
142 res = that->mSurfaceTexture;
143 }
144 return res;
145 }
146 public:
147 BSurface(const sp<SurfaceFlinger>& flinger,
148 const sp<Layer>& layer)
149 : LayerCleaner(flinger, layer), mOwner(layer) { }
150 };
151 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800152 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153}
154
Jamie Gennis582270d2011-08-17 18:19:00 -0700155wp<IBinder> Layer::getSurfaceTextureBinder() const
156{
157 return mSurfaceTexture->asBinder();
158}
159
Mathias Agopianf9d93272009-06-19 17:00:27 -0700160status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 PixelFormat format, uint32_t flags)
162{
Mathias Agopian401c2572009-09-23 19:16:27 -0700163 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 PixelFormatInfo info;
165 status_t err = getPixelFormatInfo(format, &info);
Mathias Agopianff615cc2012-02-24 14:58:36 -0800166 if (err) {
167 ALOGE("unsupported pixelformat %d", format);
168 return err;
169 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170
Mathias Agopian401c2572009-09-23 19:16:27 -0700171 // the display's pixel format
172 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700173 uint32_t const maxSurfaceDims = min(
174 hw.getMaxTextureSize(), hw.getMaxViewportDims());
175
176 // never allow a surface larger than what our underlying GL implementation
177 // can handle.
178 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800179 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700180 return BAD_VALUE;
181 }
182
Mathias Agopian401c2572009-09-23 19:16:27 -0700183 PixelFormatInfo displayInfo;
184 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700185 const uint32_t hwFlags = hw.getFlags();
186
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700187 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700188
Mathias Agopian3330b202009-10-05 17:07:12 -0700189 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800190 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700191 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
192 mCurrentOpacity = getOpacityForFormat(format);
193
194 mSurfaceTexture->setDefaultBufferSize(w, h);
195 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700196
Mathias Agopian401c2572009-09-23 19:16:27 -0700197 // we use the red index
198 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
199 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
200 mNeedsDithering = layerRedsize > displayRedSize;
201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 return NO_ERROR;
203}
204
Mathias Agopiana350ff92010-08-10 17:14:02 -0700205void Layer::setGeometry(hwc_layer_t* hwcl)
206{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700207 LayerBaseClient::setGeometry(hwcl);
208
209 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700210
211 // we can't do alpha-fade with the hwc HAL
212 const State& s(drawingState());
213 if (s.alpha < 0xFF) {
214 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700215 }
216
Mathias Agopian29a367b2011-07-12 14:51:45 -0700217 /*
218 * Transformations are applied in this order:
219 * 1) buffer orientation/flip/mirror
220 * 2) state transformation (window manager)
221 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700222 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700223 * (NOTE: the matrices are multiplied in reverse order)
224 */
225
226 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700227 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700228
229 // this gives us only the "orientation" component of the transform
230 const uint32_t finalTransform = tr.getOrientation();
231
Mathias Agopiana350ff92010-08-10 17:14:02 -0700232 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700233 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700234 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700235 } else {
236 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700237 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700238
239 if (isCropped()) {
240 hwcl->sourceCrop.left = mCurrentCrop.left;
241 hwcl->sourceCrop.top = mCurrentCrop.top;
242 hwcl->sourceCrop.right = mCurrentCrop.right;
243 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
244 } else {
245 const sp<GraphicBuffer>& buffer(mActiveBuffer);
246 hwcl->sourceCrop.left = 0;
247 hwcl->sourceCrop.top = 0;
248 if (buffer != NULL) {
249 hwcl->sourceCrop.right = buffer->width;
250 hwcl->sourceCrop.bottom = buffer->height;
251 } else {
252 hwcl->sourceCrop.right = mTransformedBounds.width();
253 hwcl->sourceCrop.bottom = mTransformedBounds.height();
254 }
255 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700256}
257
258void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700259 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700260 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800261 // this can happen if the client never drew into this layer yet,
262 // or if we ran out of memory. In that case, don't let
263 // HWC handle it.
264 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700265 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700266 } else {
267 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700268 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700269}
270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271void Layer::onDraw(const Region& clip) const
272{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800273 ATRACE_CALL();
274
Mathias Agopiana67932f2011-04-20 14:20:59 -0700275 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700277 // in fact never been drawn into. This happens frequently with
278 // SurfaceView because the WindowManager can't know when the client
279 // has drawn the first time.
280
281 // If there is nothing under us, we paint the screen in black, otherwise
282 // we just skip this update.
283
284 // figure out if there is something below us
285 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700286 const SurfaceFlinger::LayerVector& drawingLayers(
287 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700288 const size_t count = drawingLayers.size();
289 for (size_t i=0 ; i<count ; ++i) {
290 const sp<LayerBase>& layer(drawingLayers[i]);
291 if (layer.get() == static_cast<LayerBase const*>(this))
292 break;
293 under.orSelf(layer->visibleRegionScreen);
294 }
295 // if not everything below us is covered, we plug the holes!
296 Region holes(clip.subtract(under));
297 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700298 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700299 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 return;
301 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700302
Jamie Gennis9575f602011-10-07 14:51:16 -0700303 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700304 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
305 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700306 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
307 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700308 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700309 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700310 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
311 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700312 glMatrixMode(GL_TEXTURE);
313 glLoadMatrixf(mTextureMatrix);
314 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700315 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700316 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700317 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700318 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700319 glMatrixMode(GL_TEXTURE);
320 glLoadIdentity();
321 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700322 glDisable(GL_TEXTURE_EXTERNAL_OES);
323 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700324 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700325
326 drawWithOpenGL(clip);
327
Mathias Agopianc492e672011-10-18 14:49:27 -0700328 glDisable(GL_TEXTURE_EXTERNAL_OES);
329 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800332// As documented in libhardware header, formats in the range
333// 0x100 - 0x1FF are specific to the HAL implementation, and
334// are known to have no alpha channel
335// TODO: move definition for device-specific range into
336// hardware.h, instead of using hard-coded values here.
337#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
338
Mathias Agopiana67932f2011-04-20 14:20:59 -0700339bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800340{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700341 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
342 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800343 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344 PixelFormatInfo info;
345 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
346 // in case of error (unknown format), we assume no blending
347 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800348}
349
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800350
Mathias Agopiana67932f2011-04-20 14:20:59 -0700351bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700352{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700353 // if we don't have a buffer yet, we're translucent regardless of the
354 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700355 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700356 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700357 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700358
359 // if the layer has the opaque flag, then we're always opaque,
360 // otherwise we use the current buffer's format.
361 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700362}
363
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800364bool Layer::isProtected() const
365{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700366 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800367 return (activeBuffer != 0) &&
368 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
369}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371uint32_t Layer::doTransaction(uint32_t flags)
372{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800373 ATRACE_CALL();
374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 const Layer::State& front(drawingState());
376 const Layer::State& temp(currentState());
377
Mathias Agopiana138f892010-05-21 17:24:35 -0700378 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
379 (front.requested_h != temp.requested_h);
380
381 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700382 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000383 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700384 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700385 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700386 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700387 this,
388 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700389 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700390 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391
Mathias Agopiana138f892010-05-21 17:24:35 -0700392 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700393 // this will make sure LayerBase::doTransaction doesn't update
394 // the drawing state's size
395 Layer::State& editDraw(mDrawingState);
396 editDraw.requested_w = temp.requested_w;
397 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700399
400 // record the new size, form this point on, when the client request
401 // a buffer, it'll get the new size.
402 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
403 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 return LayerBase::doTransaction(flags);
407}
408
Mathias Agopiana138f892010-05-21 17:24:35 -0700409bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700410 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700411}
412
413bool Layer::isCropped() const {
414 return !mCurrentCrop.isEmpty();
415}
416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417// ----------------------------------------------------------------------------
418// pageflip handling...
419// ----------------------------------------------------------------------------
420
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800421bool Layer::onPreComposition() {
422 mRefreshPending = false;
423 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800424}
425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426void Layer::lockPageFlip(bool& recomputeVisibleRegions)
427{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800428 ATRACE_CALL();
429
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700430 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800431
432 // if we've already called updateTexImage() without going through
433 // a composition step, we have to skip this layer at this point
434 // because we cannot call updateTeximage() without a corresponding
435 // compositionComplete() call.
436 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800437 if (mRefreshPending) {
438 mPostedDirtyRegion.clear();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800439 return;
440 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800441 mRefreshPending = true;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800442
Jamie Gennis351a5132011-09-14 18:23:37 -0700443 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700444 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700445 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700446
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700447 // signal another event if we have more frames pending
448 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800449 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700450 }
451
Mathias Agopiana67932f2011-04-20 14:20:59 -0700452 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
453 // something happened!
454 recomputeVisibleRegions = true;
455 return;
456 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700457
Jamie Gennis351a5132011-09-14 18:23:37 -0700458 // update the active buffer
459 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800460 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700461
Mathias Agopianec923ee2012-02-27 16:58:04 -0800462 if (oldActiveBuffer == NULL && mActiveBuffer != NULL) {
463 // the first time we receive a buffer, we need to trigger a
464 // geometry invalidation.
465 mFlinger->invalidateHwcGeometry();
466 }
467
Mathias Agopiana67932f2011-04-20 14:20:59 -0700468 const Rect crop(mSurfaceTexture->getCurrentCrop());
469 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700470 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
471 if ((crop != mCurrentCrop) ||
472 (transform != mCurrentTransform) ||
473 (scalingMode != mCurrentScalingMode))
474 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700475 mCurrentCrop = crop;
476 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700477 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700478 mFlinger->invalidateHwcGeometry();
479 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800480
Mathias Agopianc7f33812011-08-30 15:02:41 -0700481 GLfloat textureMatrix[16];
482 mSurfaceTexture->getTransformMatrix(textureMatrix);
483 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
484 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
485 mFlinger->invalidateHwcGeometry();
486 }
487
Jamie Gennis351a5132011-09-14 18:23:37 -0700488 uint32_t bufWidth = mActiveBuffer->getWidth();
489 uint32_t bufHeight = mActiveBuffer->getHeight();
490 if (oldActiveBuffer != NULL) {
491 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
492 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700493 mFlinger->invalidateHwcGeometry();
494 }
495 }
496
Jamie Gennis351a5132011-09-14 18:23:37 -0700497 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700498 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800499 recomputeVisibleRegions = true;
500 }
501
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700502 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
503 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700504
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700505 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700506 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700507
508 // FIXME: mPostedDirtyRegion = dirty & bounds
509 mPostedDirtyRegion.set(front.w, front.h);
510
Mathias Agopian97c602c2011-07-19 15:24:46 -0700511 if ((front.w != front.requested_w) ||
512 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700513 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700514 // check that we received a buffer of the right size
515 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700516 if (mCurrentTransform & Transform::ROT_90) {
517 swap(bufWidth, bufHeight);
518 }
519
520 if (isFixedSize() ||
521 (bufWidth == front.requested_w &&
522 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700523 {
524 // Here we pretend the transaction happened by updating the
525 // current and drawing states. Drawing state is only accessed
526 // in this thread, no need to have it locked
527 Layer::State& editDraw(mDrawingState);
528 editDraw.w = editDraw.requested_w;
529 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700530
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700531 // We also need to update the current state so that we don't
532 // end-up doing too much work during the next transaction.
533 // NOTE: We actually don't need hold the transaction lock here
534 // because State::w and State::h are only accessed from
535 // this thread
536 Layer::State& editTemp(currentState());
537 editTemp.w = editDraw.w;
538 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700539
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700540 // recompute visible region
541 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700542 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700543
Steve Block9d453682011-12-20 16:23:08 +0000544 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700545 "lockPageFlip : "
546 " (layer=%p), buffer (%ux%u, tr=%02x), "
547 "requested (%dx%d)",
548 this,
549 bufWidth, bufHeight, mCurrentTransform,
550 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700551 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700552 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553}
554
555void Layer::unlockPageFlip(
556 const Transform& planeTransform, Region& outDirtyRegion)
557{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800558 ATRACE_CALL();
559
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800560 Region postedRegion(mPostedDirtyRegion);
561 if (!postedRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 mPostedDirtyRegion.clear();
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800563 if (!visibleRegionScreen.isEmpty()) {
564 // The dirty region is given in the layer's coordinate space
565 // transform the dirty region by the surface's transformation
566 // and the global transformation.
567 const Layer::State& s(drawingState());
568 const Transform tr(planeTransform * s.transform);
569 postedRegion = tr.transform(postedRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800571 // At this point, the dirty region is in screen space.
572 // Make sure it's constrained by the visible region (which
573 // is in screen space as well).
574 postedRegion.andSelf(visibleRegionScreen);
575 outDirtyRegion.orSelf(postedRegion);
576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
578}
579
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700580void Layer::dump(String8& result, char* buffer, size_t SIZE) const
581{
582 LayerBaseClient::dump(result, buffer, SIZE);
583
Mathias Agopiana67932f2011-04-20 14:20:59 -0700584 sp<const GraphicBuffer> buf0(mActiveBuffer);
585 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700586 if (buf0 != 0) {
587 w0 = buf0->getWidth();
588 h0 = buf0->getHeight();
589 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700590 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700591 }
592 snprintf(buffer, SIZE,
593 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700594 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800595 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700596 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800597 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700598
599 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700600
Mathias Agopiana67932f2011-04-20 14:20:59 -0700601 if (mSurfaceTexture != 0) {
602 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700603 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700604}
605
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800606void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
607{
608 LayerBaseClient::dumpStats(result, buffer, SIZE);
609 const size_t o = mFrameLatencyOffset;
610 const DisplayHardware& hw(graphicPlane(0).displayHardware());
611 const nsecs_t period = hw.getRefreshPeriod();
612 result.appendFormat("%lld\n", period);
613 for (size_t i=0 ; i<128 ; i++) {
614 const size_t index = (o+i) % 128;
615 const nsecs_t time_app = mFrameStats[index].timestamp;
616 const nsecs_t time_set = mFrameStats[index].set;
617 const nsecs_t time_vsync = mFrameStats[index].vsync;
618 result.appendFormat("%lld\t%lld\t%lld\n",
619 time_app,
620 time_vsync,
621 time_set);
622 }
623 result.append("\n");
624}
625
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800626void Layer::clearStats()
627{
628 LayerBaseClient::clearStats();
629 memset(mFrameStats, 0, sizeof(mFrameStats));
630}
631
Mathias Agopiana67932f2011-04-20 14:20:59 -0700632uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700633{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700634 // TODO: should we do something special if mSecure is set?
635 if (mProtectedByApp) {
636 // need a hardware-protected path to external video sink
637 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700638 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700639 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700640 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700641}
642
Mathias Agopiana4583642011-08-23 18:03:18 -0700643uint32_t Layer::getTransformHint() const {
644 uint32_t orientation = 0;
645 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700646 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700647 if (orientation & Transform::ROT_INVALID) {
648 orientation = 0;
649 }
650 }
651 return orientation;
652}
653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654// ---------------------------------------------------------------------------
655
656
657}; // namespace android