blob: 64f72d5c87142566d92d3934711007080a3fe430 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
Mathias Agopiana67932f2011-04-20 14:20:59 -070021#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070023#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/StopWatch.h>
28
Mathias Agopian3330b202009-10-05 17:07:12 -070029#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
32#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include "clz.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include "DisplayHardware/DisplayHardware.h"
36#include "DisplayHardware/HWComposer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070037#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include "SurfaceFlinger.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070040#include "SurfaceTextureLayer.h"
Mathias Agopian82d7ab62012-01-19 18:34:40 -080041#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#define DEBUG_RESIZE 0
44
45
46namespace android {
47
48// ---------------------------------------------------------------------------
49
Mathias Agopian96f08192010-06-02 23:28:45 -070050Layer::Layer(SurfaceFlinger* flinger,
51 DisplayID display, const sp<Client>& client)
52 : LayerBaseClient(flinger, display, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070053 mTextureName(-1U),
54 mQueuedFrames(0),
55 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070056 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopiana67932f2011-04-20 14:20:59 -070057 mCurrentOpacity(true),
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080058 mRefreshPending(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080059 mFrameLatencyNeeded(false),
60 mFrameLatencyOffset(0),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080061 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070062 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiana67932f2011-04-20 14:20:59 -070063 mOpaqueLayer(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070064 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070065 mSecure(false),
Mathias Agopian933389f2011-07-18 16:15:08 -070066 mProtectedByApp(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067{
Mathias Agopiana67932f2011-04-20 14:20:59 -070068 mCurrentCrop.makeInvalid();
69 glGenTextures(1, &mTextureName);
Jamie Gennise8696a42012-01-15 18:54:57 -080070}
71
72void Layer::onLayerDisplayed() {
73 if (mFrameLatencyNeeded) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -080074 const DisplayHardware& hw(graphicPlane(0).displayHardware());
75 mFrameStats[mFrameLatencyOffset].timestamp = mSurfaceTexture->getTimestamp();
76 mFrameStats[mFrameLatencyOffset].set = systemTime();
77 mFrameStats[mFrameLatencyOffset].vsync = hw.getRefreshTimestamp();
Jamie Gennise8696a42012-01-15 18:54:57 -080078 mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
79 mFrameLatencyNeeded = false;
80 }
Mathias Agopiand606de62010-05-10 20:06:11 -070081}
82
Mathias Agopiana67932f2011-04-20 14:20:59 -070083void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070084{
Mathias Agopiana67932f2011-04-20 14:20:59 -070085 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070086
Mathias Agopiana67932f2011-04-20 14:20:59 -070087 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
88 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
89 private:
90 wp<Layer> mLayer;
91 virtual void onFrameAvailable() {
92 sp<Layer> that(mLayer.promote());
93 if (that != 0) {
94 that->onFrameQueued();
95 }
96 }
97 };
98 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
99 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
100 mSurfaceTexture->setSynchronousMode(true);
101 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700102}
103
Mathias Agopiana67932f2011-04-20 14:20:59 -0700104Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700105{
Mathias Agopian118d0242011-10-13 16:02:48 -0700106 mFlinger->postMessageAsync(
107 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700108}
109
Mathias Agopiana67932f2011-04-20 14:20:59 -0700110void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700111 android_atomic_inc(&mQueuedFrames);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800112 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700113}
114
Mathias Agopiand606de62010-05-10 20:06:11 -0700115// called with SurfaceFlinger::mStateLock as soon as the layer is entered
116// in the purgatory list
117void Layer::onRemoved()
118{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700119 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700120}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700121
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700122void Layer::setName(const String8& name) {
123 LayerBase::setName(name);
124 mSurfaceTexture->setName(name);
125}
126
Mathias Agopiana67932f2011-04-20 14:20:59 -0700127sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700129 class BSurface : public BnSurface, public LayerCleaner {
130 wp<const Layer> mOwner;
131 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
132 sp<ISurfaceTexture> res;
133 sp<const Layer> that( mOwner.promote() );
134 if (that != NULL) {
135 res = that->mSurfaceTexture;
136 }
137 return res;
138 }
139 public:
140 BSurface(const sp<SurfaceFlinger>& flinger,
141 const sp<Layer>& layer)
142 : LayerCleaner(flinger, layer), mOwner(layer) { }
143 };
144 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800145 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146}
147
Jamie Gennis582270d2011-08-17 18:19:00 -0700148wp<IBinder> Layer::getSurfaceTextureBinder() const
149{
150 return mSurfaceTexture->asBinder();
151}
152
Mathias Agopianf9d93272009-06-19 17:00:27 -0700153status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 PixelFormat format, uint32_t flags)
155{
Mathias Agopian401c2572009-09-23 19:16:27 -0700156 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 PixelFormatInfo info;
158 status_t err = getPixelFormatInfo(format, &info);
159 if (err) return err;
160
Mathias Agopian401c2572009-09-23 19:16:27 -0700161 // the display's pixel format
162 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700163 uint32_t const maxSurfaceDims = min(
164 hw.getMaxTextureSize(), hw.getMaxViewportDims());
165
166 // never allow a surface larger than what our underlying GL implementation
167 // can handle.
168 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
169 return BAD_VALUE;
170 }
171
Mathias Agopian401c2572009-09-23 19:16:27 -0700172 PixelFormatInfo displayInfo;
173 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700174 const uint32_t hwFlags = hw.getFlags();
175
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700176 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700177
Mathias Agopian3330b202009-10-05 17:07:12 -0700178 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800179 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700180 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
181 mCurrentOpacity = getOpacityForFormat(format);
182
183 mSurfaceTexture->setDefaultBufferSize(w, h);
184 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700185
Mathias Agopian401c2572009-09-23 19:16:27 -0700186 // we use the red index
187 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
188 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
189 mNeedsDithering = layerRedsize > displayRedSize;
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 return NO_ERROR;
192}
193
Mathias Agopiana350ff92010-08-10 17:14:02 -0700194void Layer::setGeometry(hwc_layer_t* hwcl)
195{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700196 LayerBaseClient::setGeometry(hwcl);
197
198 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700199
200 // we can't do alpha-fade with the hwc HAL
201 const State& s(drawingState());
202 if (s.alpha < 0xFF) {
203 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700204 }
205
Mathias Agopian29a367b2011-07-12 14:51:45 -0700206 /*
207 * Transformations are applied in this order:
208 * 1) buffer orientation/flip/mirror
209 * 2) state transformation (window manager)
210 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700211 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700212 * (NOTE: the matrices are multiplied in reverse order)
213 */
214
215 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700216 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700217
218 // this gives us only the "orientation" component of the transform
219 const uint32_t finalTransform = tr.getOrientation();
220
Mathias Agopiana350ff92010-08-10 17:14:02 -0700221 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700222 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700223 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700224 } else {
225 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700226 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700227
228 if (isCropped()) {
229 hwcl->sourceCrop.left = mCurrentCrop.left;
230 hwcl->sourceCrop.top = mCurrentCrop.top;
231 hwcl->sourceCrop.right = mCurrentCrop.right;
232 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
233 } else {
234 const sp<GraphicBuffer>& buffer(mActiveBuffer);
235 hwcl->sourceCrop.left = 0;
236 hwcl->sourceCrop.top = 0;
237 if (buffer != NULL) {
238 hwcl->sourceCrop.right = buffer->width;
239 hwcl->sourceCrop.bottom = buffer->height;
240 } else {
241 hwcl->sourceCrop.right = mTransformedBounds.width();
242 hwcl->sourceCrop.bottom = mTransformedBounds.height();
243 }
244 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700245}
246
247void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700248 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700249 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800250 // this can happen if the client never drew into this layer yet,
251 // or if we ran out of memory. In that case, don't let
252 // HWC handle it.
253 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700254 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700255 } else {
256 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700257 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258}
259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260void Layer::onDraw(const Region& clip) const
261{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700262 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700264 // in fact never been drawn into. This happens frequently with
265 // SurfaceView because the WindowManager can't know when the client
266 // has drawn the first time.
267
268 // If there is nothing under us, we paint the screen in black, otherwise
269 // we just skip this update.
270
271 // figure out if there is something below us
272 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700273 const SurfaceFlinger::LayerVector& drawingLayers(
274 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700275 const size_t count = drawingLayers.size();
276 for (size_t i=0 ; i<count ; ++i) {
277 const sp<LayerBase>& layer(drawingLayers[i]);
278 if (layer.get() == static_cast<LayerBase const*>(this))
279 break;
280 under.orSelf(layer->visibleRegionScreen);
281 }
282 // if not everything below us is covered, we plug the holes!
283 Region holes(clip.subtract(under));
284 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700285 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700286 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 return;
288 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700289
Jamie Gennis9575f602011-10-07 14:51:16 -0700290 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700291 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
292 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700293 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
294 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700295 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700296 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700297 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
298 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700299 glMatrixMode(GL_TEXTURE);
300 glLoadMatrixf(mTextureMatrix);
301 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700302 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700303 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700304 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700305 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700306 glMatrixMode(GL_TEXTURE);
307 glLoadIdentity();
308 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700309 glDisable(GL_TEXTURE_EXTERNAL_OES);
310 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700311 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700312
313 drawWithOpenGL(clip);
314
Mathias Agopianc492e672011-10-18 14:49:27 -0700315 glDisable(GL_TEXTURE_EXTERNAL_OES);
316 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317}
318
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800319// As documented in libhardware header, formats in the range
320// 0x100 - 0x1FF are specific to the HAL implementation, and
321// are known to have no alpha channel
322// TODO: move definition for device-specific range into
323// hardware.h, instead of using hard-coded values here.
324#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
325
Mathias Agopiana67932f2011-04-20 14:20:59 -0700326bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800327{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700328 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
329 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800330 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700331 PixelFormatInfo info;
332 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
333 // in case of error (unknown format), we assume no blending
334 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800335}
336
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800337
Mathias Agopiana67932f2011-04-20 14:20:59 -0700338bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700339{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700340 // if we don't have a buffer yet, we're translucent regardless of the
341 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700342 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700343 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700344 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700345
346 // if the layer has the opaque flag, then we're always opaque,
347 // otherwise we use the current buffer's format.
348 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700349}
350
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800351bool Layer::isProtected() const
352{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700353 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800354 return (activeBuffer != 0) &&
355 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
356}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358uint32_t Layer::doTransaction(uint32_t flags)
359{
360 const Layer::State& front(drawingState());
361 const Layer::State& temp(currentState());
362
Mathias Agopiana138f892010-05-21 17:24:35 -0700363 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
364 (front.requested_h != temp.requested_h);
365
366 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700367 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000368 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700369 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700370 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700371 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700372 this,
373 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700374 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700375 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376
Mathias Agopiana138f892010-05-21 17:24:35 -0700377 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700378 // this will make sure LayerBase::doTransaction doesn't update
379 // the drawing state's size
380 Layer::State& editDraw(mDrawingState);
381 editDraw.requested_w = temp.requested_w;
382 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700384
385 // record the new size, form this point on, when the client request
386 // a buffer, it'll get the new size.
387 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
388 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700390
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 return LayerBase::doTransaction(flags);
392}
393
Mathias Agopiana138f892010-05-21 17:24:35 -0700394bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700395 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700396}
397
398bool Layer::isCropped() const {
399 return !mCurrentCrop.isEmpty();
400}
401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402// ----------------------------------------------------------------------------
403// pageflip handling...
404// ----------------------------------------------------------------------------
405
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800406bool Layer::onPreComposition()
407{
408 // if there was more than one pending update, request a refresh
409 if (mRefreshPending >= 2) {
410 mRefreshPending = 0;
411 return true;
412 }
413 mRefreshPending = 0;
414 return false;
415}
416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417void Layer::lockPageFlip(bool& recomputeVisibleRegions)
418{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700419 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800420
421 // if we've already called updateTexImage() without going through
422 // a composition step, we have to skip this layer at this point
423 // because we cannot call updateTeximage() without a corresponding
424 // compositionComplete() call.
425 // we'll trigger an update in onPreComposition().
426 if (mRefreshPending++) {
427 return;
428 }
429
Jamie Gennis351a5132011-09-14 18:23:37 -0700430 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700431 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700432 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700433
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700434 // signal another event if we have more frames pending
435 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800436 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700437 }
438
Mathias Agopiana67932f2011-04-20 14:20:59 -0700439 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
440 // something happened!
441 recomputeVisibleRegions = true;
442 return;
443 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700444
Jamie Gennis351a5132011-09-14 18:23:37 -0700445 // update the active buffer
446 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800447 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700448
Mathias Agopiana67932f2011-04-20 14:20:59 -0700449 const Rect crop(mSurfaceTexture->getCurrentCrop());
450 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700451 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
452 if ((crop != mCurrentCrop) ||
453 (transform != mCurrentTransform) ||
454 (scalingMode != mCurrentScalingMode))
455 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700456 mCurrentCrop = crop;
457 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700458 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700459 mFlinger->invalidateHwcGeometry();
460 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800461
Mathias Agopianc7f33812011-08-30 15:02:41 -0700462 GLfloat textureMatrix[16];
463 mSurfaceTexture->getTransformMatrix(textureMatrix);
464 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
465 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
466 mFlinger->invalidateHwcGeometry();
467 }
468
Jamie Gennis351a5132011-09-14 18:23:37 -0700469 uint32_t bufWidth = mActiveBuffer->getWidth();
470 uint32_t bufHeight = mActiveBuffer->getHeight();
471 if (oldActiveBuffer != NULL) {
472 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
473 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700474 mFlinger->invalidateHwcGeometry();
475 }
476 }
477
Jamie Gennis351a5132011-09-14 18:23:37 -0700478 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700479 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800480 recomputeVisibleRegions = true;
481 }
482
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700483 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
484 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700485
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700486 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700487 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700488
489 // FIXME: mPostedDirtyRegion = dirty & bounds
490 mPostedDirtyRegion.set(front.w, front.h);
491
Mathias Agopian97c602c2011-07-19 15:24:46 -0700492 if ((front.w != front.requested_w) ||
493 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700494 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700495 // check that we received a buffer of the right size
496 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700497 if (mCurrentTransform & Transform::ROT_90) {
498 swap(bufWidth, bufHeight);
499 }
500
501 if (isFixedSize() ||
502 (bufWidth == front.requested_w &&
503 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700504 {
505 // Here we pretend the transaction happened by updating the
506 // current and drawing states. Drawing state is only accessed
507 // in this thread, no need to have it locked
508 Layer::State& editDraw(mDrawingState);
509 editDraw.w = editDraw.requested_w;
510 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700511
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700512 // We also need to update the current state so that we don't
513 // end-up doing too much work during the next transaction.
514 // NOTE: We actually don't need hold the transaction lock here
515 // because State::w and State::h are only accessed from
516 // this thread
517 Layer::State& editTemp(currentState());
518 editTemp.w = editDraw.w;
519 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700520
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700521 // recompute visible region
522 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700523 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700524
Steve Block9d453682011-12-20 16:23:08 +0000525 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700526 "lockPageFlip : "
527 " (layer=%p), buffer (%ux%u, tr=%02x), "
528 "requested (%dx%d)",
529 this,
530 bufWidth, bufHeight, mCurrentTransform,
531 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700532 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700533 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534}
535
536void Layer::unlockPageFlip(
537 const Transform& planeTransform, Region& outDirtyRegion)
538{
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800539 if (mRefreshPending >= 2) {
540 return;
541 }
542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 Region dirtyRegion(mPostedDirtyRegion);
544 if (!dirtyRegion.isEmpty()) {
545 mPostedDirtyRegion.clear();
546 // The dirty region is given in the layer's coordinate space
547 // transform the dirty region by the surface's transformation
548 // and the global transformation.
549 const Layer::State& s(drawingState());
550 const Transform tr(planeTransform * s.transform);
551 dirtyRegion = tr.transform(dirtyRegion);
552
553 // At this point, the dirty region is in screen space.
554 // Make sure it's constrained by the visible region (which
555 // is in screen space as well).
556 dirtyRegion.andSelf(visibleRegionScreen);
557 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 }
559}
560
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700561void Layer::dump(String8& result, char* buffer, size_t SIZE) const
562{
563 LayerBaseClient::dump(result, buffer, SIZE);
564
Mathias Agopiana67932f2011-04-20 14:20:59 -0700565 sp<const GraphicBuffer> buf0(mActiveBuffer);
566 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700567 if (buf0 != 0) {
568 w0 = buf0->getWidth();
569 h0 = buf0->getHeight();
570 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700571 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700572 }
573 snprintf(buffer, SIZE,
574 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700575 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800576 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700577 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800578 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700579
580 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700581
Mathias Agopiana67932f2011-04-20 14:20:59 -0700582 if (mSurfaceTexture != 0) {
583 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700584 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700585}
586
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800587void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
588{
589 LayerBaseClient::dumpStats(result, buffer, SIZE);
590 const size_t o = mFrameLatencyOffset;
591 const DisplayHardware& hw(graphicPlane(0).displayHardware());
592 const nsecs_t period = hw.getRefreshPeriod();
593 result.appendFormat("%lld\n", period);
594 for (size_t i=0 ; i<128 ; i++) {
595 const size_t index = (o+i) % 128;
596 const nsecs_t time_app = mFrameStats[index].timestamp;
597 const nsecs_t time_set = mFrameStats[index].set;
598 const nsecs_t time_vsync = mFrameStats[index].vsync;
599 result.appendFormat("%lld\t%lld\t%lld\n",
600 time_app,
601 time_vsync,
602 time_set);
603 }
604 result.append("\n");
605}
606
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800607void Layer::clearStats()
608{
609 LayerBaseClient::clearStats();
610 memset(mFrameStats, 0, sizeof(mFrameStats));
611}
612
Mathias Agopiana67932f2011-04-20 14:20:59 -0700613uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700614{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700615 // TODO: should we do something special if mSecure is set?
616 if (mProtectedByApp) {
617 // need a hardware-protected path to external video sink
618 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700619 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700620 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700621 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700622}
623
Mathias Agopiana4583642011-08-23 18:03:18 -0700624uint32_t Layer::getTransformHint() const {
625 uint32_t orientation = 0;
626 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700627 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700628 if (orientation & Transform::ROT_INVALID) {
629 orientation = 0;
630 }
631 }
632 return orientation;
633}
634
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635// ---------------------------------------------------------------------------
636
637
638}; // namespace android