blob: 3e6b8725248d64a40bf3d47d7ba235f2bb255ee6 [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);
Mathias Agopian303d5382012-02-05 01:49:16 -0800101#ifdef USE_TRIPLE_BUFFERING
102#warning "using triple buffering"
103 mSurfaceTexture->setBufferCountServer(3);
104#else
Mathias Agopiana67932f2011-04-20 14:20:59 -0700105 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800106#endif
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700107}
108
Mathias Agopiana67932f2011-04-20 14:20:59 -0700109Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700110{
Mathias Agopian118d0242011-10-13 16:02:48 -0700111 mFlinger->postMessageAsync(
112 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
Mathias Agopian96f08192010-06-02 23:28:45 -0700113}
114
Mathias Agopiana67932f2011-04-20 14:20:59 -0700115void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700116 android_atomic_inc(&mQueuedFrames);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800117 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700118}
119
Mathias Agopiand606de62010-05-10 20:06:11 -0700120// called with SurfaceFlinger::mStateLock as soon as the layer is entered
121// in the purgatory list
122void Layer::onRemoved()
123{
Jamie Gennisdbe64862011-07-30 14:33:49 -0700124 mSurfaceTexture->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700125}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700126
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700127void Layer::setName(const String8& name) {
128 LayerBase::setName(name);
129 mSurfaceTexture->setName(name);
130}
131
Mathias Agopiana67932f2011-04-20 14:20:59 -0700132sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700134 class BSurface : public BnSurface, public LayerCleaner {
135 wp<const Layer> mOwner;
136 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
137 sp<ISurfaceTexture> res;
138 sp<const Layer> that( mOwner.promote() );
139 if (that != NULL) {
140 res = that->mSurfaceTexture;
141 }
142 return res;
143 }
144 public:
145 BSurface(const sp<SurfaceFlinger>& flinger,
146 const sp<Layer>& layer)
147 : LayerCleaner(flinger, layer), mOwner(layer) { }
148 };
149 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800150 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151}
152
Jamie Gennis582270d2011-08-17 18:19:00 -0700153wp<IBinder> Layer::getSurfaceTextureBinder() const
154{
155 return mSurfaceTexture->asBinder();
156}
157
Mathias Agopianf9d93272009-06-19 17:00:27 -0700158status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 PixelFormat format, uint32_t flags)
160{
Mathias Agopian401c2572009-09-23 19:16:27 -0700161 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 PixelFormatInfo info;
163 status_t err = getPixelFormatInfo(format, &info);
164 if (err) return err;
165
Mathias Agopian401c2572009-09-23 19:16:27 -0700166 // the display's pixel format
167 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700168 uint32_t const maxSurfaceDims = min(
169 hw.getMaxTextureSize(), hw.getMaxViewportDims());
170
171 // never allow a surface larger than what our underlying GL implementation
172 // can handle.
173 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
174 return BAD_VALUE;
175 }
176
Mathias Agopian401c2572009-09-23 19:16:27 -0700177 PixelFormatInfo displayInfo;
178 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700179 const uint32_t hwFlags = hw.getFlags();
180
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700181 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700182
Mathias Agopian3330b202009-10-05 17:07:12 -0700183 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800184 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700185 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
186 mCurrentOpacity = getOpacityForFormat(format);
187
188 mSurfaceTexture->setDefaultBufferSize(w, h);
189 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700190
Mathias Agopian401c2572009-09-23 19:16:27 -0700191 // we use the red index
192 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
193 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
194 mNeedsDithering = layerRedsize > displayRedSize;
195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 return NO_ERROR;
197}
198
Mathias Agopiana350ff92010-08-10 17:14:02 -0700199void Layer::setGeometry(hwc_layer_t* hwcl)
200{
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700201 LayerBaseClient::setGeometry(hwcl);
202
203 hwcl->flags &= ~HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700204
205 // we can't do alpha-fade with the hwc HAL
206 const State& s(drawingState());
207 if (s.alpha < 0xFF) {
208 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700209 }
210
Mathias Agopian29a367b2011-07-12 14:51:45 -0700211 /*
212 * Transformations are applied in this order:
213 * 1) buffer orientation/flip/mirror
214 * 2) state transformation (window manager)
215 * 3) layer orientation (screen orientation)
Mathias Agopiand992db32011-08-18 18:31:00 -0700216 * mTransform is already the composition of (2) and (3)
Mathias Agopian29a367b2011-07-12 14:51:45 -0700217 * (NOTE: the matrices are multiplied in reverse order)
218 */
219
220 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopiand992db32011-08-18 18:31:00 -0700221 const Transform tr(mTransform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700222
223 // this gives us only the "orientation" component of the transform
224 const uint32_t finalTransform = tr.getOrientation();
225
Mathias Agopiana350ff92010-08-10 17:14:02 -0700226 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700227 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700228 hwcl->flags = HWC_SKIP_LAYER;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700229 } else {
230 hwcl->transform = finalTransform;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700231 }
Mathias Agopianc7f33812011-08-30 15:02:41 -0700232
233 if (isCropped()) {
234 hwcl->sourceCrop.left = mCurrentCrop.left;
235 hwcl->sourceCrop.top = mCurrentCrop.top;
236 hwcl->sourceCrop.right = mCurrentCrop.right;
237 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
238 } else {
239 const sp<GraphicBuffer>& buffer(mActiveBuffer);
240 hwcl->sourceCrop.left = 0;
241 hwcl->sourceCrop.top = 0;
242 if (buffer != NULL) {
243 hwcl->sourceCrop.right = buffer->width;
244 hwcl->sourceCrop.bottom = buffer->height;
245 } else {
246 hwcl->sourceCrop.right = mTransformedBounds.width();
247 hwcl->sourceCrop.bottom = mTransformedBounds.height();
248 }
249 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700250}
251
252void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700253 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700254 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800255 // this can happen if the client never drew into this layer yet,
256 // or if we ran out of memory. In that case, don't let
257 // HWC handle it.
258 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700259 hwcl->handle = NULL;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700260 } else {
261 hwcl->handle = buffer->handle;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700262 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700263}
264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265void Layer::onDraw(const Region& clip) const
266{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700267 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700269 // in fact never been drawn into. This happens frequently with
270 // SurfaceView because the WindowManager can't know when the client
271 // has drawn the first time.
272
273 // If there is nothing under us, we paint the screen in black, otherwise
274 // we just skip this update.
275
276 // figure out if there is something below us
277 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700278 const SurfaceFlinger::LayerVector& drawingLayers(
279 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700280 const size_t count = drawingLayers.size();
281 for (size_t i=0 ; i<count ; ++i) {
282 const sp<LayerBase>& layer(drawingLayers[i]);
283 if (layer.get() == static_cast<LayerBase const*>(this))
284 break;
285 under.orSelf(layer->visibleRegionScreen);
286 }
287 // if not everything below us is covered, we plug the holes!
288 Region holes(clip.subtract(under));
289 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700290 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700291 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 return;
293 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700294
Jamie Gennis9575f602011-10-07 14:51:16 -0700295 if (!isProtected()) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700296 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
297 GLenum filter = GL_NEAREST;
Jamie Gennis9575f602011-10-07 14:51:16 -0700298 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
299 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianc492e672011-10-18 14:49:27 -0700300 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700301 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700302 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
303 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700304 glMatrixMode(GL_TEXTURE);
305 glLoadMatrixf(mTextureMatrix);
306 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700307 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700308 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700309 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700310 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700311 glMatrixMode(GL_TEXTURE);
312 glLoadIdentity();
313 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700314 glDisable(GL_TEXTURE_EXTERNAL_OES);
315 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700317
318 drawWithOpenGL(clip);
319
Mathias Agopianc492e672011-10-18 14:49:27 -0700320 glDisable(GL_TEXTURE_EXTERNAL_OES);
321 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322}
323
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800324// As documented in libhardware header, formats in the range
325// 0x100 - 0x1FF are specific to the HAL implementation, and
326// are known to have no alpha channel
327// TODO: move definition for device-specific range into
328// hardware.h, instead of using hard-coded values here.
329#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
330
Mathias Agopiana67932f2011-04-20 14:20:59 -0700331bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800332{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700333 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
334 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800335 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700336 PixelFormatInfo info;
337 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
338 // in case of error (unknown format), we assume no blending
339 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800340}
341
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800342
Mathias Agopiana67932f2011-04-20 14:20:59 -0700343bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700344{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700345 // if we don't have a buffer yet, we're translucent regardless of the
346 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700347 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700349 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700350
351 // if the layer has the opaque flag, then we're always opaque,
352 // otherwise we use the current buffer's format.
353 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700354}
355
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800356bool Layer::isProtected() const
357{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700358 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800359 return (activeBuffer != 0) &&
360 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
361}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700362
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363uint32_t Layer::doTransaction(uint32_t flags)
364{
365 const Layer::State& front(drawingState());
366 const Layer::State& temp(currentState());
367
Mathias Agopiana138f892010-05-21 17:24:35 -0700368 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
369 (front.requested_h != temp.requested_h);
370
371 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700372 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000373 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700374 "doTransaction: "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700375 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700376 "scalingMode=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700377 this,
378 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700379 int(front.requested_w), int(front.requested_h),
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700380 mCurrentScalingMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381
Mathias Agopiana138f892010-05-21 17:24:35 -0700382 if (!isFixedSize()) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700383 // this will make sure LayerBase::doTransaction doesn't update
384 // the drawing state's size
385 Layer::State& editDraw(mDrawingState);
386 editDraw.requested_w = temp.requested_w;
387 editDraw.requested_h = temp.requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 }
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700389
390 // record the new size, form this point on, when the client request
391 // a buffer, it'll get the new size.
392 mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
393 temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 return LayerBase::doTransaction(flags);
397}
398
Mathias Agopiana138f892010-05-21 17:24:35 -0700399bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700400 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700401}
402
403bool Layer::isCropped() const {
404 return !mCurrentCrop.isEmpty();
405}
406
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407// ----------------------------------------------------------------------------
408// pageflip handling...
409// ----------------------------------------------------------------------------
410
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800411bool Layer::onPreComposition()
412{
413 // if there was more than one pending update, request a refresh
414 if (mRefreshPending >= 2) {
415 mRefreshPending = 0;
416 return true;
417 }
418 mRefreshPending = 0;
419 return false;
420}
421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422void Layer::lockPageFlip(bool& recomputeVisibleRegions)
423{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700424 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800425
426 // if we've already called updateTexImage() without going through
427 // a composition step, we have to skip this layer at this point
428 // because we cannot call updateTeximage() without a corresponding
429 // compositionComplete() call.
430 // we'll trigger an update in onPreComposition().
431 if (mRefreshPending++) {
432 return;
433 }
434
Jamie Gennis351a5132011-09-14 18:23:37 -0700435 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700436 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700437 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700438
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700439 // signal another event if we have more frames pending
440 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800441 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700442 }
443
Mathias Agopiana67932f2011-04-20 14:20:59 -0700444 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
445 // something happened!
446 recomputeVisibleRegions = true;
447 return;
448 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700449
Jamie Gennis351a5132011-09-14 18:23:37 -0700450 // update the active buffer
451 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
Jamie Gennise8696a42012-01-15 18:54:57 -0800452 mFrameLatencyNeeded = true;
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700453
Mathias Agopiana67932f2011-04-20 14:20:59 -0700454 const Rect crop(mSurfaceTexture->getCurrentCrop());
455 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
Mathias Agopian933389f2011-07-18 16:15:08 -0700456 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
457 if ((crop != mCurrentCrop) ||
458 (transform != mCurrentTransform) ||
459 (scalingMode != mCurrentScalingMode))
460 {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700461 mCurrentCrop = crop;
462 mCurrentTransform = transform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700463 mCurrentScalingMode = scalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700464 mFlinger->invalidateHwcGeometry();
465 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800466
Mathias Agopianc7f33812011-08-30 15:02:41 -0700467 GLfloat textureMatrix[16];
468 mSurfaceTexture->getTransformMatrix(textureMatrix);
469 if (memcmp(textureMatrix, mTextureMatrix, sizeof(textureMatrix))) {
470 memcpy(mTextureMatrix, textureMatrix, sizeof(textureMatrix));
471 mFlinger->invalidateHwcGeometry();
472 }
473
Jamie Gennis351a5132011-09-14 18:23:37 -0700474 uint32_t bufWidth = mActiveBuffer->getWidth();
475 uint32_t bufHeight = mActiveBuffer->getHeight();
476 if (oldActiveBuffer != NULL) {
477 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
478 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopianc7f33812011-08-30 15:02:41 -0700479 mFlinger->invalidateHwcGeometry();
480 }
481 }
482
Jamie Gennis351a5132011-09-14 18:23:37 -0700483 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700484 if (oldOpacity != isOpaque()) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800485 recomputeVisibleRegions = true;
486 }
487
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700488 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
489 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700490
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700491 // update the layer size if needed
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700492 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700493
494 // FIXME: mPostedDirtyRegion = dirty & bounds
495 mPostedDirtyRegion.set(front.w, front.h);
496
Mathias Agopian97c602c2011-07-19 15:24:46 -0700497 if ((front.w != front.requested_w) ||
498 (front.h != front.requested_h))
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700499 {
Mathias Agopian97c602c2011-07-19 15:24:46 -0700500 // check that we received a buffer of the right size
501 // (Take the buffer's orientation into account)
Mathias Agopian97c602c2011-07-19 15:24:46 -0700502 if (mCurrentTransform & Transform::ROT_90) {
503 swap(bufWidth, bufHeight);
504 }
505
506 if (isFixedSize() ||
507 (bufWidth == front.requested_w &&
508 bufHeight == front.requested_h))
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700509 {
510 // Here we pretend the transaction happened by updating the
511 // current and drawing states. Drawing state is only accessed
512 // in this thread, no need to have it locked
513 Layer::State& editDraw(mDrawingState);
514 editDraw.w = editDraw.requested_w;
515 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700516
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700517 // We also need to update the current state so that we don't
518 // end-up doing too much work during the next transaction.
519 // NOTE: We actually don't need hold the transaction lock here
520 // because State::w and State::h are only accessed from
521 // this thread
522 Layer::State& editTemp(currentState());
523 editTemp.w = editDraw.w;
524 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700525
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700526 // recompute visible region
527 recomputeVisibleRegions = true;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700528 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700529
Steve Block9d453682011-12-20 16:23:08 +0000530 ALOGD_IF(DEBUG_RESIZE,
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700531 "lockPageFlip : "
532 " (layer=%p), buffer (%ux%u, tr=%02x), "
533 "requested (%dx%d)",
534 this,
535 bufWidth, bufHeight, mCurrentTransform,
536 front.requested_w, front.requested_h);
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700537 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700538 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539}
540
541void Layer::unlockPageFlip(
542 const Transform& planeTransform, Region& outDirtyRegion)
543{
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800544 if (mRefreshPending >= 2) {
545 return;
546 }
547
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 Region dirtyRegion(mPostedDirtyRegion);
549 if (!dirtyRegion.isEmpty()) {
550 mPostedDirtyRegion.clear();
551 // The dirty region is given in the layer's coordinate space
552 // transform the dirty region by the surface's transformation
553 // and the global transformation.
554 const Layer::State& s(drawingState());
555 const Transform tr(planeTransform * s.transform);
556 dirtyRegion = tr.transform(dirtyRegion);
557
558 // At this point, the dirty region is in screen space.
559 // Make sure it's constrained by the visible region (which
560 // is in screen space as well).
561 dirtyRegion.andSelf(visibleRegionScreen);
562 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 }
564}
565
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700566void Layer::dump(String8& result, char* buffer, size_t SIZE) const
567{
568 LayerBaseClient::dump(result, buffer, SIZE);
569
Mathias Agopiana67932f2011-04-20 14:20:59 -0700570 sp<const GraphicBuffer> buf0(mActiveBuffer);
571 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700572 if (buf0 != 0) {
573 w0 = buf0->getWidth();
574 h0 = buf0->getHeight();
575 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700576 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700577 }
578 snprintf(buffer, SIZE,
579 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700580 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800581 " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700582 mFormat, w0, h0, s0,f0,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800583 getTransformHint(), mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700584
585 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700586
Mathias Agopiana67932f2011-04-20 14:20:59 -0700587 if (mSurfaceTexture != 0) {
588 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700589 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700590}
591
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800592void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
593{
594 LayerBaseClient::dumpStats(result, buffer, SIZE);
595 const size_t o = mFrameLatencyOffset;
596 const DisplayHardware& hw(graphicPlane(0).displayHardware());
597 const nsecs_t period = hw.getRefreshPeriod();
598 result.appendFormat("%lld\n", period);
599 for (size_t i=0 ; i<128 ; i++) {
600 const size_t index = (o+i) % 128;
601 const nsecs_t time_app = mFrameStats[index].timestamp;
602 const nsecs_t time_set = mFrameStats[index].set;
603 const nsecs_t time_vsync = mFrameStats[index].vsync;
604 result.appendFormat("%lld\t%lld\t%lld\n",
605 time_app,
606 time_vsync,
607 time_set);
608 }
609 result.append("\n");
610}
611
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800612void Layer::clearStats()
613{
614 LayerBaseClient::clearStats();
615 memset(mFrameStats, 0, sizeof(mFrameStats));
616}
617
Mathias Agopiana67932f2011-04-20 14:20:59 -0700618uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700619{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700620 // TODO: should we do something special if mSecure is set?
621 if (mProtectedByApp) {
622 // need a hardware-protected path to external video sink
623 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700624 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700625 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700626 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700627}
628
Mathias Agopiana4583642011-08-23 18:03:18 -0700629uint32_t Layer::getTransformHint() const {
630 uint32_t orientation = 0;
631 if (!mFlinger->mDebugDisableTransformHint) {
Jamie Gennis8d91b422011-09-23 15:54:34 -0700632 orientation = getPlaneOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700633 if (orientation & Transform::ROT_INVALID) {
634 orientation = 0;
635 }
636 }
637 return orientation;
638}
639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640// ---------------------------------------------------------------------------
641
642
643}; // namespace android