blob: a294281428aa3c1ac646ffc2123951bab40eef91 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
Mathias Agopian7bb843c2011-04-20 14:20:59 -070021#include <cutils/compiler.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
Mathias Agopian7bb843c2011-04-20 14:20:59 -070023#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/StopWatch.h>
28
Mathias Agopian6950e422009-10-05 17:07:12 -070029#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080031
32#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34#include "clz.h"
Mathias Agopian7bb843c2011-04-20 14:20:59 -070035#include "DisplayHardware/DisplayHardware.h"
36#include "DisplayHardware/HWComposer.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070037#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include "SurfaceFlinger.h"
Mathias Agopian7bb843c2011-04-20 14:20:59 -070040#include "SurfaceTextureLayer.h"
Mathias Agopian1d997952012-01-19 18:34:40 -080041#include <math.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43#define DEBUG_RESIZE 0
44
45
46namespace android {
47
48// ---------------------------------------------------------------------------
49
Mathias Agopian593c05c2010-06-02 23:28:45 -070050Layer::Layer(SurfaceFlinger* flinger,
51 DisplayID display, const sp<Client>& client)
52 : LayerBaseClient(flinger, display, client),
Mathias Agopian7bb843c2011-04-20 14:20:59 -070053 mTextureName(-1U),
54 mQueuedFrames(0),
55 mCurrentTransform(0),
Mathias Agopianff86f372011-07-18 16:15:08 -070056 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopian7bb843c2011-04-20 14:20:59 -070057 mCurrentOpacity(true),
Mathias Agopian1d997952012-01-19 18:34:40 -080058 mFrameLatencyNeeded(false),
59 mFrameLatencyOffset(0),
Mathias Agopianccf94df2011-03-11 17:01:07 -080060 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian781953d2010-06-25 18:02:21 -070061 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian7bb843c2011-04-20 14:20:59 -070062 mOpaqueLayer(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070063 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070064 mSecure(false),
Mathias Agopianff86f372011-07-18 16:15:08 -070065 mProtectedByApp(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066{
Mathias Agopian7bb843c2011-04-20 14:20:59 -070067 mCurrentCrop.makeInvalid();
68 glGenTextures(1, &mTextureName);
Jamie Gennisb335fad2012-01-15 18:54:57 -080069}
70
71void Layer::onLayerDisplayed() {
72 if (mFrameLatencyNeeded) {
Mathias Agopian1d997952012-01-19 18:34:40 -080073 const DisplayHardware& hw(graphicPlane(0).displayHardware());
74 mFrameStats[mFrameLatencyOffset].timestamp = mSurfaceTexture->getTimestamp();
75 mFrameStats[mFrameLatencyOffset].set = systemTime();
76 mFrameStats[mFrameLatencyOffset].vsync = hw.getRefreshTimestamp();
Jamie Gennisb335fad2012-01-15 18:54:57 -080077 mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
78 mFrameLatencyNeeded = false;
79 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070080}
81
Mathias Agopian7bb843c2011-04-20 14:20:59 -070082void Layer::onFirstRef()
Mathias Agopian593c05c2010-06-02 23:28:45 -070083{
Mathias Agopian7bb843c2011-04-20 14:20:59 -070084 LayerBaseClient::onFirstRef();
Mathias Agopianeb99f0d2011-06-12 18:05:53 -070085
Mathias Agopian7bb843c2011-04-20 14:20:59 -070086 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
87 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
88 private:
89 wp<Layer> mLayer;
90 virtual void onFrameAvailable() {
91 sp<Layer> that(mLayer.promote());
92 if (that != 0) {
93 that->onFrameQueued();
94 }
95 }
96 };
97 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
98 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
99 mSurfaceTexture->setSynchronousMode(true);
100 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopian7623da42010-06-01 15:12:58 -0700101}
102
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700103Layer::~Layer()
Mathias Agopian7623da42010-06-01 15:12:58 -0700104{
Mathias Agopian0ab84ef2011-10-13 16:02:48 -0700105 mFlinger->postMessageAsync(
106 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian593c05c2010-06-02 23:28:45 -0700107}
108
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700109void Layer::onFrameQueued() {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700110 android_atomic_inc(&mQueuedFrames);
111 mFlinger->signalEvent();
Mathias Agopian5e140102010-06-08 19:54:15 -0700112}
113
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700114// called with SurfaceFlinger::mStateLock as soon as the layer is entered
115// in the purgatory list
116void Layer::onRemoved()
117{
Jamie Gennisa616d882011-07-30 14:33:49 -0700118 mSurfaceTexture->abandon();
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700119}
Mathias Agopian9779b2212009-09-07 16:32:45 -0700120
Jamie Gennis5b315da2011-09-16 17:31:54 -0700121void Layer::setName(const String8& name) {
122 LayerBase::setName(name);
123 mSurfaceTexture->setName(name);
124}
125
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700126sp<ISurface> Layer::createSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700128 class BSurface : public BnSurface, public LayerCleaner {
129 wp<const Layer> mOwner;
130 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
131 sp<ISurfaceTexture> res;
132 sp<const Layer> that( mOwner.promote() );
133 if (that != NULL) {
134 res = that->mSurfaceTexture;
135 }
136 return res;
137 }
138 public:
139 BSurface(const sp<SurfaceFlinger>& flinger,
140 const sp<Layer>& layer)
141 : LayerCleaner(flinger, layer), mOwner(layer) { }
142 };
143 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopian1b0114f2011-02-15 19:01:06 -0800144 return sur;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145}
146
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700147wp<IBinder> Layer::getSurfaceTextureBinder() const
148{
149 return mSurfaceTexture->asBinder();
150}
151
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700152status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 PixelFormat format, uint32_t flags)
154{
Mathias Agopiancc934762009-09-23 19:16:27 -0700155 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 PixelFormatInfo info;
157 status_t err = getPixelFormatInfo(format, &info);
158 if (err) return err;
159
Mathias Agopiancc934762009-09-23 19:16:27 -0700160 // the display's pixel format
161 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-04-14 16:43:44 -0700162 uint32_t const maxSurfaceDims = min(
163 hw.getMaxTextureSize(), hw.getMaxViewportDims());
164
165 // never allow a surface larger than what our underlying GL implementation
166 // can handle.
167 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
168 return BAD_VALUE;
169 }
170
Mathias Agopiancc934762009-09-23 19:16:27 -0700171 PixelFormatInfo displayInfo;
172 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700173 const uint32_t hwFlags = hw.getFlags();
174
Mathias Agopian9779b2212009-09-07 16:32:45 -0700175 mFormat = format;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700176
Mathias Agopian6950e422009-10-05 17:07:12 -0700177 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800178 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700179 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
180 mCurrentOpacity = getOpacityForFormat(format);
181
182 mSurfaceTexture->setDefaultBufferSize(w, h);
183 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopian967dce32010-04-14 16:43:44 -0700184
Mathias Agopiancc934762009-09-23 19:16:27 -0700185 // we use the red index
186 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
187 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
188 mNeedsDithering = layerRedsize > displayRedSize;
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 return NO_ERROR;
191}
192
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700193void Layer::setGeometry(hwc_layer_t* hwcl)
194{
Mathias Agopian0fb1a942011-08-02 15:51:37 -0700195 LayerBaseClient::setGeometry(hwcl);
196
197 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700198
199 // we can't do alpha-fade with the hwc HAL
200 const State& s(drawingState());
201 if (s.alpha < 0xFF) {
202 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700203 }
204
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700205 /*
206 * Transformations are applied in this order:
207 * 1) buffer orientation/flip/mirror
208 * 2) state transformation (window manager)
209 * 3) layer orientation (screen orientation)
Mathias Agopian3f883272011-08-18 18:31:00 -0700210 * mTransform is already the composition of (2) and (3)
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700211 * (NOTE: the matrices are multiplied in reverse order)
212 */
213
214 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopian3f883272011-08-18 18:31:00 -0700215 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700216
217 // this gives us only the "orientation" component of the transform
218 const uint32_t finalTransform = tr.getOrientation();
219
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700220 // we can only handle simple transformation
Mathias Agopian9e37abb2011-07-12 14:51:45 -0700221 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700222 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopian0fb1a942011-08-02 15:51:37 -0700223 } else {
224 hwcl->transform = finalTransform;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700225 }
Mathias Agopiance8e0ba2011-08-30 15:02:41 -0700226
227 if (isCropped()) {
228 hwcl->sourceCrop.left = mCurrentCrop.left;
229 hwcl->sourceCrop.top = mCurrentCrop.top;
230 hwcl->sourceCrop.right = mCurrentCrop.right;
231 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
232 } else {
233 const sp<GraphicBuffer>& buffer(mActiveBuffer);
234 hwcl->sourceCrop.left = 0;
235 hwcl->sourceCrop.top = 0;
236 if (buffer != NULL) {
237 hwcl->sourceCrop.right = buffer->width;
238 hwcl->sourceCrop.bottom = buffer->height;
239 } else {
240 hwcl->sourceCrop.right = mTransformedBounds.width();
241 hwcl->sourceCrop.bottom = mTransformedBounds.height();
242 }
243 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700244}
245
246void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700247 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700248 if (buffer == NULL) {
Mathias Agopian575eaf52010-12-13 18:51:59 -0800249 // this can happen if the client never drew into this layer yet,
250 // or if we ran out of memory. In that case, don't let
251 // HWC handle it.
252 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700253 hwcl->handle = NULL;
Mathias Agopian0fb1a942011-08-02 15:51:37 -0700254 } else {
255 hwcl->handle = buffer->handle;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700256 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700257}
258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259void Layer::onDraw(const Region& clip) const
260{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700261 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700263 // in fact never been drawn into. This happens frequently with
264 // SurfaceView because the WindowManager can't know when the client
265 // has drawn the first time.
266
267 // If there is nothing under us, we paint the screen in black, otherwise
268 // we just skip this update.
269
270 // figure out if there is something below us
271 Region under;
Mathias Agopian56e8d672011-08-23 12:34:29 -0700272 const SurfaceFlinger::LayerVector& drawingLayers(
273 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700274 const size_t count = drawingLayers.size();
275 for (size_t i=0 ; i<count ; ++i) {
276 const sp<LayerBase>& layer(drawingLayers[i]);
277 if (layer.get() == static_cast<LayerBase const*>(this))
278 break;
279 under.orSelf(layer->visibleRegionScreen);
280 }
281 // if not everything below us is covered, we plug the holes!
282 Region holes(clip.subtract(under));
283 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700284 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 return;
287 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700288
Jamie Gennis830d0832011-10-07 14:51:16 -0700289 if (!isProtected()) {
Mathias Agopian9044ef02011-10-18 14:49:27 -0700290 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
291 GLenum filter = GL_NEAREST;
Jamie Gennis830d0832011-10-07 14:51:16 -0700292 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
293 // TODO: we could be more subtle with isFixedSize()
Mathias Agopian9044ef02011-10-18 14:49:27 -0700294 filter = GL_LINEAR;
Jamie Gennis830d0832011-10-07 14:51:16 -0700295 }
Mathias Agopian9044ef02011-10-18 14:49:27 -0700296 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
297 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis830d0832011-10-07 14:51:16 -0700298 glMatrixMode(GL_TEXTURE);
299 glLoadMatrixf(mTextureMatrix);
300 glMatrixMode(GL_MODELVIEW);
Mathias Agopian9044ef02011-10-18 14:49:27 -0700301 glDisable(GL_TEXTURE_2D);
Xavier Ducrohetf9e45512011-10-21 16:18:48 -0700302 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700303 } else {
Mathias Agopian9044ef02011-10-18 14:49:27 -0700304 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis830d0832011-10-07 14:51:16 -0700305 glMatrixMode(GL_TEXTURE);
306 glLoadIdentity();
307 glMatrixMode(GL_MODELVIEW);
Mathias Agopian9044ef02011-10-18 14:49:27 -0700308 glDisable(GL_TEXTURE_EXTERNAL_OES);
309 glEnable(GL_TEXTURE_2D);
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700310 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700311
312 drawWithOpenGL(clip);
313
Mathias Agopian9044ef02011-10-18 14:49:27 -0700314 glDisable(GL_TEXTURE_EXTERNAL_OES);
315 glDisable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316}
317
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800318// As documented in libhardware header, formats in the range
319// 0x100 - 0x1FF are specific to the HAL implementation, and
320// are known to have no alpha channel
321// TODO: move definition for device-specific range into
322// hardware.h, instead of using hard-coded values here.
323#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
324
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700325bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800326{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700327 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
328 return true;
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800329 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700330 PixelFormatInfo info;
331 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
332 // in case of error (unknown format), we assume no blending
333 return (err || info.h_alpha <= info.l_alpha);
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800334}
335
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800336
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700337bool Layer::isOpaque() const
Mathias Agopian92377032010-05-26 22:08:52 -0700338{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700339 // if we don't have a buffer yet, we're translucent regardless of the
340 // layer's opaque flag.
Jamie Gennisc0545702011-07-28 14:54:07 -0700341 if (mActiveBuffer == 0) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700342 return false;
Jamie Gennisc0545702011-07-28 14:54:07 -0700343 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700344
345 // if the layer has the opaque flag, then we're always opaque,
346 // otherwise we use the current buffer's format.
347 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopian92377032010-05-26 22:08:52 -0700348}
349
Jamie Gennisf72606c2011-03-09 17:05:02 -0800350bool Layer::isProtected() const
351{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700352 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennisf72606c2011-03-09 17:05:02 -0800353 return (activeBuffer != 0) &&
354 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
355}
Mathias Agopian59751db2010-05-07 15:58:44 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357uint32_t Layer::doTransaction(uint32_t flags)
358{
359 const Layer::State& front(drawingState());
360 const Layer::State& temp(currentState());
361
Mathias Agopian2be352a2010-05-21 17:24:35 -0700362 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
363 (front.requested_h != temp.requested_h);
364
365 if (sizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700366 // the size changed, we need to ask our client to request a new buffer
Steve Block5baa3a62011-12-20 16:23:08 +0000367 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopianf3503c22011-07-25 19:56:08 -0700368 "doTransaction: "
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700369 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopianf3503c22011-07-25 19:56:08 -0700370 "scalingMode=%d",
Mathias Agopian2be352a2010-05-21 17:24:35 -0700371 this,
372 int(temp.requested_w), int(temp.requested_h),
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700373 int(front.requested_w), int(front.requested_h),
Mathias Agopianf3503c22011-07-25 19:56:08 -0700374 mCurrentScalingMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
Mathias Agopian2be352a2010-05-21 17:24:35 -0700376 if (!isFixedSize()) {
Mathias Agopian2be352a2010-05-21 17:24:35 -0700377 // this will make sure LayerBase::doTransaction doesn't update
378 // the drawing state's size
379 Layer::State& editDraw(mDrawingState);
380 editDraw.requested_w = temp.requested_w;
381 editDraw.requested_h = temp.requested_h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
Jamie Gennis3a232122011-09-26 16:54:44 -0700383
384 // record the new size, form this point on, when the client request
385 // a buffer, it'll get the new size.
386 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
387 temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return LayerBase::doTransaction(flags);
391}
392
Mathias Agopian2be352a2010-05-21 17:24:35 -0700393bool Layer::isFixedSize() const {
Mathias Agopianff86f372011-07-18 16:15:08 -0700394 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700395}
396
397bool Layer::isCropped() const {
398 return !mCurrentCrop.isEmpty();
399}
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401// ----------------------------------------------------------------------------
402// pageflip handling...
403// ----------------------------------------------------------------------------
404
405void Layer::lockPageFlip(bool& recomputeVisibleRegions)
406{
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700407 if (mQueuedFrames > 0) {
Jamie Gennis8d468492011-09-14 18:23:37 -0700408 // Capture the old state of the layer for comparisons later
Jamie Gennisc0545702011-07-28 14:54:07 -0700409 const bool oldOpacity = isOpaque();
Jamie Gennis8d468492011-09-14 18:23:37 -0700410 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisc0545702011-07-28 14:54:07 -0700411
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700412 // signal another event if we have more frames pending
413 if (android_atomic_dec(&mQueuedFrames) > 1) {
414 mFlinger->signalEvent();
415 }
416
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700417 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
418 // something happened!
419 recomputeVisibleRegions = true;
420 return;
421 }
Mathias Agopian593c05c2010-06-02 23:28:45 -0700422
Jamie Gennis8d468492011-09-14 18:23:37 -0700423 // update the active buffer
424 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennisb335fad2012-01-15 18:54:57 -0800425 mFrameLatencyNeeded = true;
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700426
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700427 const Rect crop(mSurfaceTexture->getCurrentCrop());
428 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopianff86f372011-07-18 16:15:08 -0700429 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
430 if ((crop != mCurrentCrop) ||
431 (transform != mCurrentTransform) ||
432 (scalingMode != mCurrentScalingMode))
433 {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700434 mCurrentCrop = crop;
435 mCurrentTransform = transform;
Mathias Agopianff86f372011-07-18 16:15:08 -0700436 mCurrentScalingMode = scalingMode;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700437 mFlinger->invalidateHwcGeometry();
438 }
Mathias Agopian575eaf52010-12-13 18:51:59 -0800439
Mathias Agopiance8e0ba2011-08-30 15:02:41 -0700440 GLfloat textureMatrix[16];
441 mSurfaceTexture->getTransformMatrix(textureMatrix);
442 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
443 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
444 mFlinger->invalidateHwcGeometry();
445 }
446
Jamie Gennis8d468492011-09-14 18:23:37 -0700447 uint32_t bufWidth = mActiveBuffer->getWidth();
448 uint32_t bufHeight = mActiveBuffer->getHeight();
449 if (oldActiveBuffer != NULL) {
450 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
451 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopiance8e0ba2011-08-30 15:02:41 -0700452 mFlinger->invalidateHwcGeometry();
453 }
454 }
455
Jamie Gennis8d468492011-09-14 18:23:37 -0700456 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisc0545702011-07-28 14:54:07 -0700457 if (oldOpacity != isOpaque()) {
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800458 recomputeVisibleRegions = true;
459 }
460
Mathias Agopian56e8d672011-08-23 12:34:29 -0700461 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
462 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian1473f462009-04-10 14:24:30 -0700463
Jamie Gennisde14eca2011-10-14 16:44:08 -0700464 // update the layer size if needed
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700465 const Layer::State& front(drawingState());
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700466
467 // FIXME: mPostedDirtyRegion = dirty & bounds
468 mPostedDirtyRegion.set(front.w, front.h);
469
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700470 if ((front.w != front.requested_w) ||
471 (front.h != front.requested_h))
Mathias Agopianbd23e302009-09-30 14:07:22 -0700472 {
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700473 // check that we received a buffer of the right size
474 // (Take the buffer's orientation into account)
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700475 if (mCurrentTransform & Transform::ROT_90) {
476 swap(bufWidth, bufHeight);
477 }
478
479 if (isFixedSize() ||
480 (bufWidth == front.requested_w &&
481 bufHeight == front.requested_h))
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700482 {
483 // Here we pretend the transaction happened by updating the
484 // current and drawing states. Drawing state is only accessed
485 // in this thread, no need to have it locked
486 Layer::State& editDraw(mDrawingState);
487 editDraw.w = editDraw.requested_w;
488 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700489
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700490 // We also need to update the current state so that we don't
491 // end-up doing too much work during the next transaction.
492 // NOTE: We actually don't need hold the transaction lock here
493 // because State::w and State::h are only accessed from
494 // this thread
495 Layer::State& editTemp(currentState());
496 editTemp.w = editDraw.w;
497 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700498
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700499 // recompute visible region
500 recomputeVisibleRegions = true;
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700501 }
Mathias Agopianf3503c22011-07-25 19:56:08 -0700502
Steve Block5baa3a62011-12-20 16:23:08 +0000503 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopianf3503c22011-07-25 19:56:08 -0700504 "lockPageFlip : "
505 " (layer=%p), buffer (%ux%u, tr=%02x), "
506 "requested (%dx%d)",
507 this,
508 bufWidth, bufHeight, mCurrentTransform,
509 front.requested_w, front.requested_h);
Mathias Agopianbd23e302009-09-30 14:07:22 -0700510 }
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512}
513
514void Layer::unlockPageFlip(
515 const Transform& planeTransform, Region& outDirtyRegion)
516{
517 Region dirtyRegion(mPostedDirtyRegion);
518 if (!dirtyRegion.isEmpty()) {
519 mPostedDirtyRegion.clear();
520 // The dirty region is given in the layer's coordinate space
521 // transform the dirty region by the surface's transformation
522 // and the global transformation.
523 const Layer::State& s(drawingState());
524 const Transform tr(planeTransform * s.transform);
525 dirtyRegion = tr.transform(dirtyRegion);
526
527 // At this point, the dirty region is in screen space.
528 // Make sure it's constrained by the visible region (which
529 // is in screen space as well).
530 dirtyRegion.andSelf(visibleRegionScreen);
531 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533}
534
Mathias Agopian9bce8732010-04-20 17:55:49 -0700535void Layer::dump(String8& result, char* buffer, size_t SIZE) const
536{
537 LayerBaseClient::dump(result, buffer, SIZE);
538
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700539 sp<const GraphicBuffer> buf0(mActiveBuffer);
540 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian9bce8732010-04-20 17:55:49 -0700541 if (buf0 != 0) {
542 w0 = buf0->getWidth();
543 h0 = buf0->getHeight();
544 s0 = buf0->getStride();
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700545 f0 = buf0->format;
Mathias Agopian9bce8732010-04-20 17:55:49 -0700546 }
547 snprintf(buffer, SIZE,
548 " "
Mathias Agopian0c3367f2011-08-08 16:02:13 -0700549 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Jamie Gennisde14eca2011-10-14 16:44:08 -0700550 " transform-hint=0x%02x, queued-frames=%d\n",
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700551 mFormat, w0, h0, s0,f0,
Jamie Gennisde14eca2011-10-14 16:44:08 -0700552 getTransformHint(), mQueuedFrames);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700553
554 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700555
Mathias Agopian1d997952012-01-19 18:34:40 -0800556 LayerBase::dumpStats(result, buffer, SIZE);
Jamie Gennisb335fad2012-01-15 18:54:57 -0800557
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700558 if (mSurfaceTexture != 0) {
559 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian5e140102010-06-08 19:54:15 -0700560 }
Mathias Agopian7623da42010-06-01 15:12:58 -0700561}
562
Mathias Agopian1d997952012-01-19 18:34:40 -0800563void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
564{
565 LayerBaseClient::dumpStats(result, buffer, SIZE);
566 const size_t o = mFrameLatencyOffset;
567 const DisplayHardware& hw(graphicPlane(0).displayHardware());
568 const nsecs_t period = hw.getRefreshPeriod();
569 result.appendFormat("%lld\n", period);
570 for (size_t i=0 ; i<128 ; i++) {
571 const size_t index = (o+i) % 128;
572 const nsecs_t time_app = mFrameStats[index].timestamp;
573 const nsecs_t time_set = mFrameStats[index].set;
574 const nsecs_t time_vsync = mFrameStats[index].vsync;
575 result.appendFormat("%lld\t%lld\t%lld\n",
576 time_app,
577 time_vsync,
578 time_set);
579 }
580 result.append("\n");
581}
582
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700583uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopian7623da42010-06-01 15:12:58 -0700584{
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700585 // TODO: should we do something special if mSecure is set?
586 if (mProtectedByApp) {
587 // need a hardware-protected path to external video sink
588 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis6c925d02010-11-02 11:51:32 -0700589 }
Jamie Gennisd52c14d2011-08-10 11:48:07 -0700590 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700591 return usage;
Mathias Agopian59751db2010-05-07 15:58:44 -0700592}
593
Mathias Agopian2143fe02011-08-23 18:03:18 -0700594uint32_t Layer::getTransformHint() const {
595 uint32_t orientation = 0;
596 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennisfd2429d2011-09-23 15:54:34 -0700597 orientation = getPlaneOrientation();
Mathias Agopian2143fe02011-08-23 18:03:18 -0700598 if (orientation & Transform::ROT_INVALID) {
599 orientation = 0;
600 }
601 }
602 return orientation;
603}
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605// ---------------------------------------------------------------------------
606
607
608}; // namespace android