blob: f94a9ba7eebd783f38d453e3f91c387361fb5ea1 [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
254 // we can't do alpha-fade with the hwc HAL
255 const State& s(drawingState());
256 if (s.alpha < 0xFF) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700257 layer.setSkip(true);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258 }
259
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700260 if (isSecure() && !hw->isSecure()) {
261 layer.setSkip(true);
262 }
263
Mathias Agopian29a367b2011-07-12 14:51:45 -0700264 /*
265 * Transformations are applied in this order:
266 * 1) buffer orientation/flip/mirror
267 * 2) state transformation (window manager)
268 * 3) layer orientation (screen orientation)
269 * (NOTE: the matrices are multiplied in reverse order)
270 */
271
272 const Transform bufferOrientation(mCurrentTransform);
Mathias Agopian42977342012-08-05 00:40:46 -0700273 const Transform tr(hw->getTransform() * s.transform * bufferOrientation);
Mathias Agopian29a367b2011-07-12 14:51:45 -0700274
275 // this gives us only the "orientation" component of the transform
276 const uint32_t finalTransform = tr.getOrientation();
277
Mathias Agopiana350ff92010-08-10 17:14:02 -0700278 // we can only handle simple transformation
Mathias Agopian29a367b2011-07-12 14:51:45 -0700279 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700280 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700281 } else {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700282 layer.setTransform(finalTransform);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700283 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700284 layer.setCrop(computeBufferCrop());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700285}
286
Mathias Agopian42977342012-08-05 00:40:46 -0700287void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700288 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian71e83e12012-09-04 20:25:39 -0700289 LayerBaseClient::setPerFrameData(hw, layer);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700290 // NOTE: buffer can be NULL if the client never drew into this
291 // layer yet, or if we ran out of memory
Mathias Agopian71e83e12012-09-04 20:25:39 -0700292 layer.setBuffer(mActiveBuffer);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700293}
Jesse Halldc5b4852012-06-29 15:21:18 -0700294
Mathias Agopian42977342012-08-05 00:40:46 -0700295void Layer::setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700296 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700297 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700298
299 // TODO: there is a possible optimization here: we only need to set the
300 // acquire fence the first time a new buffer is acquired on EACH display.
301
302 if (layer.getCompositionType() == HWC_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800303 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jesse Halldc5b4852012-06-29 15:21:18 -0700304 if (fence.get()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700305 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700306 if (fenceFd == -1) {
307 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
308 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700309 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700310 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700311 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700312}
313
Mathias Agopian42977342012-08-05 00:40:46 -0700314void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800316 ATRACE_CALL();
317
Mathias Agopiana67932f2011-04-20 14:20:59 -0700318 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700320 // in fact never been drawn into. This happens frequently with
321 // SurfaceView because the WindowManager can't know when the client
322 // has drawn the first time.
323
324 // If there is nothing under us, we paint the screen in black, otherwise
325 // we just skip this update.
326
327 // figure out if there is something below us
328 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700329 const SurfaceFlinger::LayerVector& drawingLayers(
330 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700331 const size_t count = drawingLayers.size();
332 for (size_t i=0 ; i<count ; ++i) {
333 const sp<LayerBase>& layer(drawingLayers[i]);
334 if (layer.get() == static_cast<LayerBase const*>(this))
335 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700336 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700337 }
338 // if not everything below us is covered, we plug the holes!
339 Region holes(clip.subtract(under));
340 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700341 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700342 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 return;
344 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700345
Andy McFadden97eba892012-12-11 15:21:45 -0800346 // Bind the current buffer to the GL texture, and wait for it to be
347 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800348 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
349 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800350 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700351 // Go ahead and draw the buffer anyway; no matter what we do the screen
352 // is probably going to have something visibly wrong.
353 }
354
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700355 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
356
357 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700358 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700359 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700360
361 // Query the texture matrix given our current filtering mode.
362 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800363 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
364 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700365
366 // Set things up for texturing.
Mathias Agopianc492e672011-10-18 14:49:27 -0700367 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
368 GLenum filter = GL_NEAREST;
Jamie Genniscbb1a952012-05-08 17:05:52 -0700369 if (useFiltering) {
Mathias Agopianc492e672011-10-18 14:49:27 -0700370 filter = GL_LINEAR;
Jamie Gennis9575f602011-10-07 14:51:16 -0700371 }
Mathias Agopianc492e672011-10-18 14:49:27 -0700372 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
373 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
Jamie Gennis9575f602011-10-07 14:51:16 -0700374 glMatrixMode(GL_TEXTURE);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700375 glLoadMatrixf(textureMatrix);
Jamie Gennis9575f602011-10-07 14:51:16 -0700376 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700377 glDisable(GL_TEXTURE_2D);
Xavier Ducrohet4c4163b2011-10-21 16:18:48 -0700378 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700379 } else {
Mathias Agopianc492e672011-10-18 14:49:27 -0700380 glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
Jamie Gennis9575f602011-10-07 14:51:16 -0700381 glMatrixMode(GL_TEXTURE);
382 glLoadIdentity();
383 glMatrixMode(GL_MODELVIEW);
Mathias Agopianc492e672011-10-18 14:49:27 -0700384 glDisable(GL_TEXTURE_EXTERNAL_OES);
385 glEnable(GL_TEXTURE_2D);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700386 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700387
Mathias Agopian1b031492012-06-20 17:51:20 -0700388 drawWithOpenGL(hw, clip);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700389
Mathias Agopianc492e672011-10-18 14:49:27 -0700390 glDisable(GL_TEXTURE_EXTERNAL_OES);
391 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392}
393
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800394// As documented in libhardware header, formats in the range
395// 0x100 - 0x1FF are specific to the HAL implementation, and
396// are known to have no alpha channel
397// TODO: move definition for device-specific range into
398// hardware.h, instead of using hard-coded values here.
399#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
400
Mathias Agopiana67932f2011-04-20 14:20:59 -0700401bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800402{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700403 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
404 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800405 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700406 PixelFormatInfo info;
407 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
408 // in case of error (unknown format), we assume no blending
409 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800410}
411
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800412
Mathias Agopiana67932f2011-04-20 14:20:59 -0700413bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700414{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700415 // if we don't have a buffer yet, we're translucent regardless of the
416 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700417 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700418 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700419 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700420
421 // if the layer has the opaque flag, then we're always opaque,
422 // otherwise we use the current buffer's format.
423 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700424}
425
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800426bool Layer::isProtected() const
427{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700428 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800429 return (activeBuffer != 0) &&
430 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
431}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433uint32_t Layer::doTransaction(uint32_t flags)
434{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800435 ATRACE_CALL();
436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 const Layer::State& front(drawingState());
438 const Layer::State& temp(currentState());
439
Mathias Agopian4824d402012-06-04 18:16:30 -0700440 const bool sizeChanged = (temp.requested.w != front.requested.w) ||
441 (temp.requested.h != front.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700442
443 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700444 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000445 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -0700446 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -0700447 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
448 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n"
449 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
450 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
Andy McFadden69052052012-09-14 16:10:11 -0700451 this, (const char*) getName(), mCurrentTransform, mCurrentScalingMode,
Mathias Agopian419e1962012-05-23 14:34:07 -0700452 temp.active.w, temp.active.h,
453 temp.active.crop.left,
454 temp.active.crop.top,
455 temp.active.crop.right,
456 temp.active.crop.bottom,
457 temp.active.crop.getWidth(),
458 temp.active.crop.getHeight(),
459 temp.requested.w, temp.requested.h,
Mathias Agopianb30c4152012-05-16 18:21:32 -0700460 temp.requested.crop.left,
461 temp.requested.crop.top,
462 temp.requested.crop.right,
463 temp.requested.crop.bottom,
464 temp.requested.crop.getWidth(),
465 temp.requested.crop.getHeight(),
Mathias Agopian419e1962012-05-23 14:34:07 -0700466 front.active.w, front.active.h,
467 front.active.crop.left,
468 front.active.crop.top,
469 front.active.crop.right,
470 front.active.crop.bottom,
471 front.active.crop.getWidth(),
472 front.active.crop.getHeight(),
473 front.requested.w, front.requested.h,
Mathias Agopianb30c4152012-05-16 18:21:32 -0700474 front.requested.crop.left,
475 front.requested.crop.top,
476 front.requested.crop.right,
477 front.requested.crop.bottom,
478 front.requested.crop.getWidth(),
Mathias Agopian419e1962012-05-23 14:34:07 -0700479 front.requested.crop.getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700481 // record the new size, form this point on, when the client request
482 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800483 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopianb30c4152012-05-16 18:21:32 -0700484 temp.requested.w, temp.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700486
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700487 if (!isFixedSize()) {
488
489 const bool resizePending = (temp.requested.w != temp.active.w) ||
490 (temp.requested.h != temp.active.h);
491
492 if (resizePending) {
493 // don't let LayerBase::doTransaction update the drawing state
494 // if we have a pending resize, unless we are in fixed-size mode.
495 // the drawing state will be updated only once we receive a buffer
496 // with the correct size.
497 //
498 // in particular, we want to make sure the clip (which is part
499 // of the geometry state) is latched together with the size but is
500 // latched immediately when no resizing is involved.
501
502 flags |= eDontUpdateGeometryState;
503 }
504 }
505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 return LayerBase::doTransaction(flags);
507}
508
Mathias Agopiana138f892010-05-21 17:24:35 -0700509bool Layer::isFixedSize() const {
Mathias Agopian933389f2011-07-18 16:15:08 -0700510 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700511}
512
513bool Layer::isCropped() const {
514 return !mCurrentCrop.isEmpty();
515}
516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517// ----------------------------------------------------------------------------
518// pageflip handling...
519// ----------------------------------------------------------------------------
520
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800521bool Layer::onPreComposition() {
522 mRefreshPending = false;
523 return mQueuedFrames > 0;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800524}
525
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700526void Layer::onPostComposition() {
527 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800528 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800529 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
530
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800531 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800532 if (frameReadyFence != NULL) {
533 mFrameTracker.setFrameReadyFence(frameReadyFence);
534 } else {
535 // There was no fence for this frame, so assume that it was ready
536 // to be presented at the desired present time.
537 mFrameTracker.setFrameReadyTime(desiredPresentTime);
538 }
539
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700540 const HWComposer& hwc = mFlinger->getHwComposer();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800541 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jamie Gennis46b6bce2012-12-07 15:42:15 -0800542 // XXX: Temporarily don't use the present fence from HWC to work
543 // around a driver bug.
544 presentFence.clear();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800545 if (presentFence != NULL) {
546 mFrameTracker.setActualPresentFence(presentFence);
547 } else {
548 // The HWC doesn't support present fences, so use the refresh
549 // timestamp instead.
550 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
551 mFrameTracker.setActualPresentTime(presentTime);
552 }
553
554 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700555 mFrameLatencyNeeded = false;
556 }
557}
558
Mathias Agopianda27af92012-09-13 18:17:13 -0700559bool Layer::isVisible() const {
560 return LayerBaseClient::isVisible() && (mActiveBuffer != NULL);
561}
562
Mathias Agopian4fec8732012-06-29 14:12:52 -0700563Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800565 ATRACE_CALL();
566
Mathias Agopian4fec8732012-06-29 14:12:52 -0700567 Region outDirtyRegion;
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700568 if (mQueuedFrames > 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800569
570 // if we've already called updateTexImage() without going through
571 // a composition step, we have to skip this layer at this point
572 // because we cannot call updateTeximage() without a corresponding
573 // compositionComplete() call.
574 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800575 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700576 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800577 }
578
Jamie Gennis351a5132011-09-14 18:23:37 -0700579 // Capture the old state of the layer for comparisons later
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700580 const bool oldOpacity = isOpaque();
Jamie Gennis351a5132011-09-14 18:23:37 -0700581 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -0700582
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700583 // signal another event if we have more frames pending
584 if (android_atomic_dec(&mQueuedFrames) > 1) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800585 mFlinger->signalLayerUpdate();
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700586 }
587
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800588 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700589 Layer::State& front;
590 Layer::State& current;
591 bool& recomputeVisibleRegions;
592 Reject(Layer::State& front, Layer::State& current,
593 bool& recomputeVisibleRegions)
594 : front(front), current(current),
595 recomputeVisibleRegions(recomputeVisibleRegions) {
596 }
597
598 virtual bool reject(const sp<GraphicBuffer>& buf,
599 const BufferQueue::BufferItem& item) {
600 if (buf == NULL) {
601 return false;
602 }
603
604 uint32_t bufWidth = buf->getWidth();
605 uint32_t bufHeight = buf->getHeight();
606
607 // check that we received a buffer of the right size
608 // (Take the buffer's orientation into account)
609 if (item.mTransform & Transform::ROT_90) {
610 swap(bufWidth, bufHeight);
611 }
612
613
614 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
615 if (front.active != front.requested) {
616
617 if (isFixedSize ||
618 (bufWidth == front.requested.w &&
619 bufHeight == front.requested.h))
620 {
621 // Here we pretend the transaction happened by updating the
622 // current and drawing states. Drawing state is only accessed
623 // in this thread, no need to have it locked
624 front.active = front.requested;
625
626 // We also need to update the current state so that
627 // we don't end-up overwriting the drawing state with
628 // this stale current state during the next transaction
629 //
630 // NOTE: We don't need to hold the transaction lock here
631 // because State::active is only accessed from this thread.
632 current.active = front.active;
633
634 // recompute visible region
635 recomputeVisibleRegions = true;
636 }
637
638 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -0700639 "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700640 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
641 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
Andy McFadden69052052012-09-14 16:10:11 -0700642 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700643 front.active.w, front.active.h,
644 front.active.crop.left,
645 front.active.crop.top,
646 front.active.crop.right,
647 front.active.crop.bottom,
648 front.active.crop.getWidth(),
649 front.active.crop.getHeight(),
650 front.requested.w, front.requested.h,
651 front.requested.crop.left,
652 front.requested.crop.top,
653 front.requested.crop.right,
654 front.requested.crop.bottom,
655 front.requested.crop.getWidth(),
656 front.requested.crop.getHeight());
657 }
658
659 if (!isFixedSize) {
660 if (front.active.w != bufWidth ||
661 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -0700662 // reject this buffer
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700663 return true;
664 }
665 }
666 return false;
667 }
668 };
669
670
671 Reject r(mDrawingState, currentState(), recomputeVisibleRegions);
672
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800673 if (mSurfaceFlingerConsumer->updateTexImage(&r) != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700674 // something happened!
675 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700676 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700677 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700678
Jamie Gennis351a5132011-09-14 18:23:37 -0700679 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800680 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -0700681 if (mActiveBuffer == NULL) {
682 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -0700683 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -0700684 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800685
Mathias Agopian4824d402012-06-04 18:16:30 -0700686 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700687 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -0700688 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700689 // the first time we receive a buffer, we need to trigger a
690 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -0700691 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700692 }
693
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800694 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
695 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
696 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -0700697 if ((crop != mCurrentCrop) ||
698 (transform != mCurrentTransform) ||
699 (scalingMode != mCurrentScalingMode))
700 {
701 mCurrentCrop = crop;
702 mCurrentTransform = transform;
703 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -0700704 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -0700705 }
706
707 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -0700708 uint32_t bufWidth = mActiveBuffer->getWidth();
709 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -0700710 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
711 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -0700712 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -0700713 }
714 }
715
716 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
717 if (oldOpacity != isOpaque()) {
718 recomputeVisibleRegions = true;
719 }
720
Mathias Agopian702634a2012-05-23 17:50:31 -0700721 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
722 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700723
724 // FIXME: postedRegion should be dirty & bounds
725 const Layer::State& front(drawingState());
726 Region dirtyRegion(Rect(front.active.w, front.active.h));
727
728 // transform the dirty region to window-manager space
729 outDirtyRegion = (front.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700730 }
Mathias Agopian4fec8732012-06-29 14:12:52 -0700731 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732}
733
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700734void Layer::dump(String8& result, char* buffer, size_t SIZE) const
735{
736 LayerBaseClient::dump(result, buffer, SIZE);
737
Mathias Agopiana67932f2011-04-20 14:20:59 -0700738 sp<const GraphicBuffer> buf0(mActiveBuffer);
739 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700740 if (buf0 != 0) {
741 w0 = buf0->getWidth();
742 h0 = buf0->getHeight();
743 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700744 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700745 }
746 snprintf(buffer, SIZE,
747 " "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700748 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
Andy McFadden69052052012-09-14 16:10:11 -0700749 " queued-frames=%d, mRefreshPending=%d\n",
Mathias Agopiana67932f2011-04-20 14:20:59 -0700750 mFormat, w0, h0, s0,f0,
Andy McFadden69052052012-09-14 16:10:11 -0700751 mQueuedFrames, mRefreshPending);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700752
753 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700754
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800755 if (mSurfaceFlingerConsumer != 0) {
756 mSurfaceFlingerConsumer->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700757 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700758}
759
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800760void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
761{
762 LayerBaseClient::dumpStats(result, buffer, SIZE);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700763 const nsecs_t period =
764 mFlinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800765 result.appendFormat("%lld\n", period);
Jamie Gennis82dbc742012-11-08 19:23:28 -0800766 mFrameTracker.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800767}
768
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800769void Layer::clearStats()
770{
771 LayerBaseClient::clearStats();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800772 mFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800773}
774
Mathias Agopiana67932f2011-04-20 14:20:59 -0700775uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700777 // TODO: should we do something special if mSecure is set?
778 if (mProtectedByApp) {
779 // need a hardware-protected path to external video sink
780 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700781 }
Jamie Gennis3599bf22011-08-10 11:48:07 -0700782 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700783 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700784}
785
Mathias Agopian84300952012-11-21 16:02:13 -0800786void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -0700787 uint32_t orientation = 0;
788 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -0800789 // The transform hint is used to improve performance, but we can
790 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -0700791 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -0700792 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -0700793 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -0700794 if (orientation & Transform::ROT_INVALID) {
795 orientation = 0;
796 }
797 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800798 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -0700799}
800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801// ---------------------------------------------------------------------------
802
803
804}; // namespace android