blob: 95a5e029e2fa9caca8c73a38a472c246a22d8230 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080022#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Mathias Agopiana67932f2011-04-20 14:20:59 -070024#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070026#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <utils/Errors.h>
29#include <utils/Log.h>
30#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080031#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Mathias Agopian3330b202009-10-05 17:07:12 -070033#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080035
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070039#include "DisplayDevice.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070040#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include "SurfaceFlinger.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070043#include "SurfaceTextureLayer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Mathias Agopian1b031492012-06-20 17:51:20 -070045#include "DisplayHardware/HWComposer.h"
46
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#define DEBUG_RESIZE 0
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049namespace android {
50
51// ---------------------------------------------------------------------------
52
Mathias Agopian3ee454a2012-08-27 16:28:24 -070053Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client)
54 : LayerBaseClient(flinger, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070055 mTextureName(-1U),
56 mQueuedFrames(0),
57 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070058 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopiana67932f2011-04-20 14:20:59 -070059 mCurrentOpacity(true),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080060 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080061 mFrameLatencyNeeded(false),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080062 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070063 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiana67932f2011-04-20 14:20:59 -070064 mOpaqueLayer(true),
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
Mathias Agopian42977342012-08-05 00:40:46 -070072void Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -070073 HWComposer::HWCLayerInterface* layer) {
Mathias Agopianc3973602012-08-31 17:51:25 -070074 LayerBaseClient::onLayerDisplayed(hw, layer);
Jesse Hallef194142012-06-14 14:45:17 -070075 if (layer) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080076 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFenceFd());
Jesse Hallef194142012-06-14 14:45:17 -070077 }
Mathias Agopiand606de62010-05-10 20:06:11 -070078}
79
Mathias Agopiana67932f2011-04-20 14:20:59 -070080void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070081{
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070083
Andy McFaddenbf974ab2012-12-04 16:51:15 -080084 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Daniel Lamb2675792012-02-23 14:35:13 -080085 sp<BufferQueue> bq = new SurfaceTextureLayer();
Andy McFaddenbf974ab2012-12-04 16:51:15 -080086 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(mTextureName, true,
Mathias Agopiana0db3082012-04-23 13:59:36 -070087 GL_TEXTURE_EXTERNAL_OES, false, bq);
Daniel Lamb2675792012-02-23 14:35:13 -080088
Andy McFaddenbf974ab2012-12-04 16:51:15 -080089 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
90 mSurfaceFlingerConsumer->setFrameAvailableListener(this);
91 mSurfaceFlingerConsumer->setSynchronousMode(true);
Daniel Lamb2675792012-02-23 14:35:13 -080092
Mathias Agopian7f42a9c2012-04-23 20:00:16 -070093#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
94#warning "disabling triple buffering"
Andy McFaddenbf974ab2012-12-04 16:51:15 -080095 mSurfaceFlingerConsumer->setDefaultMaxBufferCount(2);
Mathias Agopian7f42a9c2012-04-23 20:00:16 -070096#else
Andy McFaddenbf974ab2012-12-04 16:51:15 -080097 mSurfaceFlingerConsumer->setDefaultMaxBufferCount(3);
Mathias Agopian303d5382012-02-05 01:49:16 -080098#endif
Andy McFadden69052052012-09-14 16:10:11 -070099
Mathias Agopian84300952012-11-21 16:02:13 -0800100 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
101 updateTransformHint(hw);
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 Agopian921e6ac2012-07-23 23:11:29 -0700106 mFlinger->deleteTextureAsync(mTextureName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700107}
108
Igor Murashkina4a31492012-10-29 13:36:11 -0700109void Layer::onFrameAvailable() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700110 android_atomic_inc(&mQueuedFrames);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800111 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700112}
113
Mathias Agopiand606de62010-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{
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800118 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700119}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700120
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700121void Layer::setName(const String8& name) {
122 LayerBase::setName(name);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800123 mSurfaceFlingerConsumer->setName(name);
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700124}
125
Mathias Agopiana67932f2011-04-20 14:20:59 -0700126sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127{
Andy McFadden882e3a32013-01-08 16:06:15 -0800128 /*
129 * This class provides an implementation of BnSurface (the "native" or
130 * "remote" side of the Binder IPC interface ISurface), and mixes in
131 * LayerCleaner to ensure that mFlinger->onLayerDestroyed() is called for
132 * this layer when the BSurface is destroyed.
133 *
134 * The idea is to provide a handle to the Layer through ISurface that
135 * is cleaned up automatically when the last reference to the ISurface
136 * goes away. (The references will be held on the "proxy" side, while
137 * the Layer exists on the "native" side.)
138 *
139 * The Layer has a reference to an instance of SurfaceFlinger's variant
140 * of GLConsumer, which holds a reference to the BufferQueue. The
141 * getSurfaceTexture() call returns a Binder interface reference for
142 * the producer interface of the buffer queue associated with the Layer.
143 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700144 class BSurface : public BnSurface, public LayerCleaner {
145 wp<const Layer> mOwner;
Andy McFadden2adaf042012-12-18 09:49:45 -0800146 virtual sp<IGraphicBufferProducer> getSurfaceTexture() const {
147 sp<IGraphicBufferProducer> res;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700148 sp<const Layer> that( mOwner.promote() );
149 if (that != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800150 res = that->mSurfaceFlingerConsumer->getBufferQueue();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700151 }
152 return res;
153 }
154 public:
155 BSurface(const sp<SurfaceFlinger>& flinger,
156 const sp<Layer>& layer)
157 : LayerCleaner(flinger, layer), mOwner(layer) { }
158 };
159 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800160 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Jamie Gennis582270d2011-08-17 18:19:00 -0700163wp<IBinder> Layer::getSurfaceTextureBinder() const
164{
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800165 return mSurfaceFlingerConsumer->getBufferQueue()->asBinder();
Jamie Gennis582270d2011-08-17 18:19:00 -0700166}
167
Mathias Agopianf9d93272009-06-19 17:00:27 -0700168status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 PixelFormat format, uint32_t flags)
170{
Mathias Agopian401c2572009-09-23 19:16:27 -0700171 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 PixelFormatInfo info;
173 status_t err = getPixelFormatInfo(format, &info);
Mathias Agopianff615cc2012-02-24 14:58:36 -0800174 if (err) {
175 ALOGE("unsupported pixelformat %d", format);
176 return err;
177 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178
Mathias Agopianca99fb82010-04-14 16:43:44 -0700179 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700180 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700181
182 // never allow a surface larger than what our underlying GL implementation
183 // can handle.
184 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800185 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700186 return BAD_VALUE;
187 }
188
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700189 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700190
Mathias Agopian3165cc22012-08-08 19:42:09 -0700191 mSecure = (flags & ISurfaceComposerClient::eSecure) ? true : false;
192 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
193 mOpaqueLayer = (flags & ISurfaceComposerClient::eOpaque);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700194 mCurrentOpacity = getOpacityForFormat(format);
195
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800196 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
197 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
198 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 return NO_ERROR;
201}
202
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700203Rect Layer::computeBufferCrop() const {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800204 // Start with the SurfaceFlingerConsumer's buffer crop...
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700205 Rect crop;
206 if (!mCurrentCrop.isEmpty()) {
207 crop = mCurrentCrop;
208 } else if (mActiveBuffer != NULL){
209 crop = Rect(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
210 } else {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700211 crop.makeInvalid();
212 return crop;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700213 }
214
215 // ... then reduce that in the same proportions as the window crop reduces
216 // the window size.
217 const State& s(drawingState());
Mathias Agopian93ffb862012-05-16 17:07:49 -0700218 if (!s.active.crop.isEmpty()) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700219 // Transform the window crop to match the buffer coordinate system,
220 // which means using the inverse of the current transform set on the
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800221 // SurfaceFlingerConsumer.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700222 uint32_t invTransform = mCurrentTransform;
Mathias Agopian93ffb862012-05-16 17:07:49 -0700223 int winWidth = s.active.w;
224 int winHeight = s.active.h;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700225 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
226 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
227 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian93ffb862012-05-16 17:07:49 -0700228 winWidth = s.active.h;
229 winHeight = s.active.w;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700230 }
Mathias Agopian93ffb862012-05-16 17:07:49 -0700231 Rect winCrop = s.active.crop.transform(invTransform,
232 s.active.w, s.active.h);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700233
234 float xScale = float(crop.width()) / float(winWidth);
235 float yScale = float(crop.height()) / float(winHeight);
Mathias Agopian93ffb862012-05-16 17:07:49 -0700236 crop.left += int(ceilf(float(winCrop.left) * xScale));
237 crop.top += int(ceilf(float(winCrop.top) * yScale));
238 crop.right -= int(ceilf(float(winWidth - winCrop.right) * xScale));
239 crop.bottom -= int(ceilf(float(winHeight - winCrop.bottom) * yScale));
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700240 }
241
242 return crop;
243}
244
Mathias Agopian4fec8732012-06-29 14:12:52 -0700245void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700246 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700247 HWComposer::HWCLayerInterface& layer)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700248{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700249 LayerBaseClient::setGeometry(hw, layer);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700250
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700251 // enable this layer
252 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700254 if (isSecure() && !hw->isSecure()) {
255 layer.setSkip(true);
256 }
257
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800258 const State& s(drawingState());
259 layer.setPlaneAlpha(s.alpha);
260
Mathias Agopian29a367b2011-07-12 14:51:45 -0700261 /*
262 * Transformations are applied in this order:
263 * 1) buffer orientation/flip/mirror
264 * 2) state transformation (window manager)
265 * 3) layer orientation (screen orientation)
266 * (NOTE: the matrices are multiplied in reverse order)
267 */
268
269 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopian42977342012-08-05 00:40:46 -0700270 const Transform tr(hw->getTransform() * s.transform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700271
272 // this gives us only the "orientation" component of the transform
273 const uint32_t finalTransform = tr.getOrientation();
274
Mathias Agopiana350ff92010-08-10 17:14:02 -0700275 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700276 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700277 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700278 } else {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700279 layer.setTransform(finalTransform);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700280 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700281 layer.setCrop(computeBufferCrop());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700282}
283
Mathias Agopian42977342012-08-05 00:40:46 -0700284void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700285 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian71e83e12012-09-04 20:25:39 -0700286 LayerBaseClient::setPerFrameData(hw, layer);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700287 // NOTE: buffer can be NULL if the client never drew into this
288 // layer yet, or if we ran out of memory
Mathias Agopian71e83e12012-09-04 20:25:39 -0700289 layer.setBuffer(mActiveBuffer);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700290}
Jesse Halldc5b4852012-06-29 15:21:18 -0700291
Mathias Agopian42977342012-08-05 00:40:46 -0700292void Layer::setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700293 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700294 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700295
296 // TODO: there is a possible optimization here: we only need to set the
297 // acquire fence the first time a new buffer is acquired on EACH display.
298
299 if (layer.getCompositionType() == HWC_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800300 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jesse Halldc5b4852012-06-29 15:21:18 -0700301 if (fence.get()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700302 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700303 if (fenceFd == -1) {
304 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
305 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700306 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700307 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700308 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700309}
310
Mathias Agopian42977342012-08-05 00:40:46 -0700311void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800313 ATRACE_CALL();
314
Mathias Agopiana67932f2011-04-20 14:20:59 -0700315 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700317 // in fact never been drawn into. This happens frequently with
318 // SurfaceView because the WindowManager can't know when the client
319 // has drawn the first time.
320
321 // If there is nothing under us, we paint the screen in black, otherwise
322 // we just skip this update.
323
324 // figure out if there is something below us
325 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700326 const SurfaceFlinger::LayerVector& drawingLayers(
327 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700328 const size_t count = drawingLayers.size();
329 for (size_t i=0 ; i<count ; ++i) {
330 const sp<LayerBase>& layer(drawingLayers[i]);
331 if (layer.get() == static_cast<LayerBase const*>(this))
332 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700333 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700334 }
335 // if not everything below us is covered, we plug the holes!
336 Region holes(clip.subtract(under));
337 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700338 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700339 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 return;
341 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700342
Andy McFadden97eba892012-12-11 15:21:45 -0800343 // Bind the current buffer to the GL texture, and wait for it to be
344 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800345 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
346 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800347 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700348 // Go ahead and draw the buffer anyway; no matter what we do the screen
349 // is probably going to have something visibly wrong.
350 }
351
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700352 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
353
354 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700355 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700356 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700357
358 // Query the texture matrix given our current filtering mode.
359 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800360 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
361 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700362
363 // Set things up for texturing.
Mathias Agopianc492e672011-10-18 14:49:27 -0700364 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
365 GLenum filter = GL_NEAREST;
Jamie Genniscbb1a952012-05-08 17:05:52 -0700366 if (useFiltering) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700367 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700368 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700369 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
370 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700371 glMatrixMode(GL_TEXTURE);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700372 glLoadMatrixf(textureMatrix);
Jamie Gennis9575f602011-10-07 14:51:16 -0700373 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700374 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700375 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700376 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700377 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700378 glMatrixMode(GL_TEXTURE);
379 glLoadIdentity();
380 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700381 glDisable(GL_TEXTURE_EXTERNAL_OES);
382 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700383 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700384
Mathias Agopian1b031492012-06-20 17:51:20 -0700385 drawWithOpenGL(hw, clip);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700386
Mathias Agopianc492e672011-10-18 14:49:27 -0700387 glDisable(GL_TEXTURE_EXTERNAL_OES);
388 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389}
390
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800391// As documented in libhardware header, formats in the range
392// 0x100 - 0x1FF are specific to the HAL implementation, and
393// are known to have no alpha channel
394// TODO: move definition for device-specific range into
395// hardware.h, instead of using hard-coded values here.
396#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
397
Mathias Agopiana67932f2011-04-20 14:20:59 -0700398bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800399{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700400 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
401 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800402 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700403 PixelFormatInfo info;
404 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
405 // in case of error (unknown format), we assume no blending
406 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800407}
408
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800409
Mathias Agopiana67932f2011-04-20 14:20:59 -0700410bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700411{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700412 // if we don't have a buffer yet, we're translucent regardless of the
413 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700414 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700415 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700416 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700417
418 // if the layer has the opaque flag, then we're always opaque,
419 // otherwise we use the current buffer's format.
420 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700421}
422
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800423bool Layer::isProtected() const
424{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700425 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800426 return (activeBuffer != 0) &&
427 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
428}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430uint32_t Layer::doTransaction(uint32_t flags)
431{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800432 ATRACE_CALL();
433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 const Layer::State& front(drawingState());
435 const Layer::State& temp(currentState());
436
Mathias Agopian4824d402012-06-04 18:16:30 -0700437 const bool sizeChanged = (temp.requested.w != front.requested.w) ||
438 (temp.requested.h != front.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700439
440 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700441 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000442 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -0700443 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -0700444 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
445 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n"
446 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
447 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
Andy McFadden69052052012-09-14 16:10:11 -0700448 this, (const char*) getName(), mCurrentTransform, mCurrentScalingMode,
Mathias Agopian419e1962012-05-23 14:34:07 -0700449 temp.active.w, temp.active.h,
450 temp.active.crop.left,
451 temp.active.crop.top,
452 temp.active.crop.right,
453 temp.active.crop.bottom,
454 temp.active.crop.getWidth(),
455 temp.active.crop.getHeight(),
456 temp.requested.w, temp.requested.h,
Mathias Agopianb30c4152012-05-16 18:21:32 -0700457 temp.requested.crop.left,
458 temp.requested.crop.top,
459 temp.requested.crop.right,
460 temp.requested.crop.bottom,
461 temp.requested.crop.getWidth(),
462 temp.requested.crop.getHeight(),
Mathias Agopian419e1962012-05-23 14:34:07 -0700463 front.active.w, front.active.h,
464 front.active.crop.left,
465 front.active.crop.top,
466 front.active.crop.right,
467 front.active.crop.bottom,
468 front.active.crop.getWidth(),
469 front.active.crop.getHeight(),
470 front.requested.w, front.requested.h,
Mathias Agopianb30c4152012-05-16 18:21:32 -0700471 front.requested.crop.left,
472 front.requested.crop.top,
473 front.requested.crop.right,
474 front.requested.crop.bottom,
475 front.requested.crop.getWidth(),
Mathias Agopian419e1962012-05-23 14:34:07 -0700476 front.requested.crop.getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700478 // record the new size, form this point on, when the client request
479 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800480 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopianb30c4152012-05-16 18:21:32 -0700481 temp.requested.w, temp.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700483
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700484 if (!isFixedSize()) {
485
486 const bool resizePending = (temp.requested.w != temp.active.w) ||
487 (temp.requested.h != temp.active.h);
488
489 if (resizePending) {
490 // don't let LayerBase::doTransaction update the drawing state
491 // if we have a pending resize, unless we are in fixed-size mode.
492 // the drawing state will be updated only once we receive a buffer
493 // with the correct size.
494 //
495 // in particular, we want to make sure the clip (which is part
496 // of the geometry state) is latched together with the size but is
497 // latched immediately when no resizing is involved.
498
499 flags |= eDontUpdateGeometryState;
500 }
501 }
502
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 return LayerBase::doTransaction(flags);
504}
505
Mathias Agopiana138f892010-05-21 17:24:35 -0700506bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700507 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700508}
509
510bool Layer::isCropped() const {
511 return !mCurrentCrop.isEmpty();
512}
513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514// ----------------------------------------------------------------------------
515// pageflip handling...
516// ----------------------------------------------------------------------------
517
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800518bool Layer::onPreComposition() {
519 mRefreshPending = false;
520 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800521}
522
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700523void Layer::onPostComposition() {
524 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800525 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800526 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
527
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800528 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800529 if (frameReadyFence != NULL) {
530 mFrameTracker.setFrameReadyFence(frameReadyFence);
531 } else {
532 // There was no fence for this frame, so assume that it was ready
533 // to be presented at the desired present time.
534 mFrameTracker.setFrameReadyTime(desiredPresentTime);
535 }
536
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700537 const HWComposer& hwc = mFlinger->getHwComposer();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800538 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jamie Gennis46b6bce2012-12-07 15:42:15 -0800539 // XXX: Temporarily don't use the present fence from HWC to work
540 // around a driver bug.
541 presentFence.clear();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800542 if (presentFence != NULL) {
543 mFrameTracker.setActualPresentFence(presentFence);
544 } else {
545 // The HWC doesn't support present fences, so use the refresh
546 // timestamp instead.
547 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
548 mFrameTracker.setActualPresentTime(presentTime);
549 }
550
551 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700552 mFrameLatencyNeeded = false;
553 }
554}
555
Mathias Agopianda27af92012-09-13 18:17:13 -0700556bool Layer::isVisible() const {
557 return LayerBaseClient::isVisible() && (mActiveBuffer != NULL);
558}
559
Mathias Agopian4fec8732012-06-29 14:12:52 -0700560Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800562 ATRACE_CALL();
563
Mathias Agopian4fec8732012-06-29 14:12:52 -0700564 Region outDirtyRegion;
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700565 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800566
567 // if we've already called updateTexImage() without going through
568 // a composition step, we have to skip this layer at this point
569 // because we cannot call updateTeximage() without a corresponding
570 // compositionComplete() call.
571 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800572 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700573 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800574 }
575
Jamie Gennis351a5132011-09-14 18:23:37 -0700576 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700577 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700578 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700579
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700580 // signal another event if we have more frames pending
581 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800582 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700583 }
584
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800585 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700586 Layer::State& front;
587 Layer::State& current;
588 bool& recomputeVisibleRegions;
589 Reject(Layer::State& front, Layer::State& current,
590 bool& recomputeVisibleRegions)
591 : front(front), current(current),
592 recomputeVisibleRegions(recomputeVisibleRegions) {
593 }
594
595 virtual bool reject(const sp<GraphicBuffer>& buf,
596 const BufferQueue::BufferItem& item) {
597 if (buf == NULL) {
598 return false;
599 }
600
601 uint32_t bufWidth = buf->getWidth();
602 uint32_t bufHeight = buf->getHeight();
603
604 // check that we received a buffer of the right size
605 // (Take the buffer's orientation into account)
606 if (item.mTransform & Transform::ROT_90) {
607 swap(bufWidth, bufHeight);
608 }
609
610
611 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
612 if (front.active != front.requested) {
613
614 if (isFixedSize ||
615 (bufWidth == front.requested.w &&
616 bufHeight == front.requested.h))
617 {
618 // Here we pretend the transaction happened by updating the
619 // current and drawing states. Drawing state is only accessed
620 // in this thread, no need to have it locked
621 front.active = front.requested;
622
623 // We also need to update the current state so that
624 // we don't end-up overwriting the drawing state with
625 // this stale current state during the next transaction
626 //
627 // NOTE: We don't need to hold the transaction lock here
628 // because State::active is only accessed from this thread.
629 current.active = front.active;
630
631 // recompute visible region
632 recomputeVisibleRegions = true;
633 }
634
635 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -0700636 "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700637 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
638 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
Andy McFadden69052052012-09-14 16:10:11 -0700639 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700640 front.active.w, front.active.h,
641 front.active.crop.left,
642 front.active.crop.top,
643 front.active.crop.right,
644 front.active.crop.bottom,
645 front.active.crop.getWidth(),
646 front.active.crop.getHeight(),
647 front.requested.w, front.requested.h,
648 front.requested.crop.left,
649 front.requested.crop.top,
650 front.requested.crop.right,
651 front.requested.crop.bottom,
652 front.requested.crop.getWidth(),
653 front.requested.crop.getHeight());
654 }
655
656 if (!isFixedSize) {
657 if (front.active.w != bufWidth ||
658 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -0700659 // reject this buffer
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700660 return true;
661 }
662 }
663 return false;
664 }
665 };
666
667
668 Reject r(mDrawingState, currentState(), recomputeVisibleRegions);
669
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800670 if (mSurfaceFlingerConsumer->updateTexImage(&r) != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700671 // something happened!
672 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700673 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700674 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700675
Jamie Gennis351a5132011-09-14 18:23:37 -0700676 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800677 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -0700678 if (mActiveBuffer == NULL) {
679 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -0700680 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -0700681 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800682
Mathias Agopian4824d402012-06-04 18:16:30 -0700683 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700684 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -0700685 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700686 // the first time we receive a buffer, we need to trigger a
687 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -0700688 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700689 }
690
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800691 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
692 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
693 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -0700694 if ((crop != mCurrentCrop) ||
695 (transform != mCurrentTransform) ||
696 (scalingMode != mCurrentScalingMode))
697 {
698 mCurrentCrop = crop;
699 mCurrentTransform = transform;
700 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -0700701 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -0700702 }
703
704 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -0700705 uint32_t bufWidth = mActiveBuffer->getWidth();
706 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -0700707 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
708 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -0700709 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -0700710 }
711 }
712
713 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
714 if (oldOpacity != isOpaque()) {
715 recomputeVisibleRegions = true;
716 }
717
Mathias Agopian702634a2012-05-23 17:50:31 -0700718 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
719 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700720
721 // FIXME: postedRegion should be dirty & bounds
722 const Layer::State& front(drawingState());
723 Region dirtyRegion(Rect(front.active.w, front.active.h));
724
725 // transform the dirty region to window-manager space
726 outDirtyRegion = (front.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700727 }
Mathias Agopian4fec8732012-06-29 14:12:52 -0700728 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729}
730
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700731void Layer::dump(String8& result, char* buffer, size_t SIZE) const
732{
733 LayerBaseClient::dump(result, buffer, SIZE);
734
Mathias Agopiana67932f2011-04-20 14:20:59 -0700735 sp<const GraphicBuffer> buf0(mActiveBuffer);
736 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700737 if (buf0 != 0) {
738 w0 = buf0->getWidth();
739 h0 = buf0->getHeight();
740 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700741 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700742 }
743 snprintf(buffer, SIZE,
744 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700745 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Andy McFadden69052052012-09-14 16:10:11 -0700746 " queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700747 mFormat, w0, h0, s0,f0,
Andy McFadden69052052012-09-14 16:10:11 -0700748 mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700749
750 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700751
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800752 if (mSurfaceFlingerConsumer != 0) {
753 mSurfaceFlingerConsumer->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700754 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700755}
756
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800757void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
758{
759 LayerBaseClient::dumpStats(result, buffer, SIZE);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700760 const nsecs_t period =
761 mFlinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800762 result.appendFormat("%lld\n", period);
Jamie Gennis82dbc742012-11-08 19:23:28 -0800763 mFrameTracker.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800764}
765
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800766void Layer::clearStats()
767{
768 LayerBaseClient::clearStats();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800769 mFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800770}
771
Mathias Agopiana67932f2011-04-20 14:20:59 -0700772uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700773{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700774 // TODO: should we do something special if mSecure is set?
775 if (mProtectedByApp) {
776 // need a hardware-protected path to external video sink
777 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700778 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700779 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700780 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700781}
782
Mathias Agopian84300952012-11-21 16:02:13 -0800783void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -0700784 uint32_t orientation = 0;
785 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -0800786 // The transform hint is used to improve performance, but we can
787 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -0700788 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -0700789 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -0700790 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700791 if (orientation & Transform::ROT_INVALID) {
792 orientation = 0;
793 }
794 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800795 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -0700796}
797
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798// ---------------------------------------------------------------------------
799
800
801}; // namespace android