blob: 35e29a6315a8e47913eca3f11a968eb943d34df9 [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
42#define DEBUG_RESIZE 0
43
44
45namespace android {
46
Mathias Agopianca99fb82010-04-14 16:43:44 -070047template <typename T> inline T min(T a, T b) {
48 return a<b ? a : b;
49}
50
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051// ---------------------------------------------------------------------------
52
Mathias Agopian96f08192010-06-02 23:28:45 -070053Layer::Layer(SurfaceFlinger* flinger,
54 DisplayID display, const sp<Client>& client)
55 : LayerBaseClient(flinger, display, client),
Mathias Agopiana67932f2011-04-20 14:20:59 -070056 mTextureName(-1U),
57 mQueuedFrames(0),
58 mCurrentTransform(0),
59 mCurrentOpacity(true),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080060 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiana67932f2011-04-20 14:20:59 -070062 mOpaqueLayer(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070063 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070064 mSecure(false),
Glenn Kasten16f04532011-01-19 15:27:27 -080065 mProtectedByApp(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070066 mFixedSize(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);
Mathias Agopiand606de62010-05-10 20:06:11 -070070}
71
Mathias Agopianddc31c32011-06-12 18:05:53 -070072void Layer::destroy(RefBase const* base) {
73 mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
Mathias Agopianca4d3602011-05-19 15:38:14 -070074}
75
Mathias Agopiana67932f2011-04-20 14:20:59 -070076void Layer::onFirstRef()
Mathias Agopian96f08192010-06-02 23:28:45 -070077{
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 LayerBaseClient::onFirstRef();
Mathias Agopianddc31c32011-06-12 18:05:53 -070079 setDestroyer(this);
80
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
82 FrameQueuedListener(Layer* layer) : mLayer(layer) { }
83 private:
84 wp<Layer> mLayer;
85 virtual void onFrameAvailable() {
86 sp<Layer> that(mLayer.promote());
87 if (that != 0) {
88 that->onFrameQueued();
89 }
90 }
91 };
92 mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
93 mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
94 mSurfaceTexture->setSynchronousMode(true);
95 mSurfaceTexture->setBufferCountServer(2);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070096}
97
Mathias Agopiana67932f2011-04-20 14:20:59 -070098Layer::~Layer()
Mathias Agopianb7e930d2010-06-01 15:12:58 -070099{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700100 glDeleteTextures(1, &mTextureName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700101}
102
Mathias Agopiana67932f2011-04-20 14:20:59 -0700103void Layer::onFrameQueued() {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700104 android_atomic_inc(&mQueuedFrames);
105 mFlinger->signalEvent();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700106}
107
Mathias Agopiand606de62010-05-10 20:06:11 -0700108// called with SurfaceFlinger::mStateLock as soon as the layer is entered
109// in the purgatory list
110void Layer::onRemoved()
111{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700112}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700113
Mathias Agopiana67932f2011-04-20 14:20:59 -0700114sp<ISurface> Layer::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700116 class BSurface : public BnSurface, public LayerCleaner {
117 wp<const Layer> mOwner;
118 virtual sp<ISurfaceTexture> getSurfaceTexture() const {
119 sp<ISurfaceTexture> res;
120 sp<const Layer> that( mOwner.promote() );
121 if (that != NULL) {
122 res = that->mSurfaceTexture;
123 }
124 return res;
125 }
126 public:
127 BSurface(const sp<SurfaceFlinger>& flinger,
128 const sp<Layer>& layer)
129 : LayerCleaner(flinger, layer), mOwner(layer) { }
130 };
131 sp<ISurface> sur(new BSurface(mFlinger, this));
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800132 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133}
134
Mathias Agopianf9d93272009-06-19 17:00:27 -0700135status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 PixelFormat format, uint32_t flags)
137{
Mathias Agopian401c2572009-09-23 19:16:27 -0700138 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 PixelFormatInfo info;
140 status_t err = getPixelFormatInfo(format, &info);
141 if (err) return err;
142
Mathias Agopian401c2572009-09-23 19:16:27 -0700143 // the display's pixel format
144 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700145 uint32_t const maxSurfaceDims = min(
146 hw.getMaxTextureSize(), hw.getMaxViewportDims());
147
148 // never allow a surface larger than what our underlying GL implementation
149 // can handle.
150 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
151 return BAD_VALUE;
152 }
153
Mathias Agopian401c2572009-09-23 19:16:27 -0700154 PixelFormatInfo displayInfo;
155 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700156 const uint32_t hwFlags = hw.getFlags();
157
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700158 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700159
Mathias Agopian3330b202009-10-05 17:07:12 -0700160 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800161 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700162 mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
163 mCurrentOpacity = getOpacityForFormat(format);
164
165 mSurfaceTexture->setDefaultBufferSize(w, h);
166 mSurfaceTexture->setDefaultBufferFormat(format);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700167
Mathias Agopian401c2572009-09-23 19:16:27 -0700168 // we use the red index
169 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
170 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
171 mNeedsDithering = layerRedsize > displayRedSize;
172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 return NO_ERROR;
174}
175
Mathias Agopiana350ff92010-08-10 17:14:02 -0700176void Layer::setGeometry(hwc_layer_t* hwcl)
177{
178 hwcl->compositionType = HWC_FRAMEBUFFER;
179 hwcl->hints = 0;
180 hwcl->flags = 0;
181 hwcl->transform = 0;
182 hwcl->blending = HWC_BLENDING_NONE;
183
184 // we can't do alpha-fade with the hwc HAL
185 const State& s(drawingState());
186 if (s.alpha < 0xFF) {
187 hwcl->flags = HWC_SKIP_LAYER;
188 return;
189 }
190
191 // we can only handle simple transformation
192 if (mOrientation & Transform::ROT_INVALID) {
193 hwcl->flags = HWC_SKIP_LAYER;
194 return;
195 }
196
Mathias Agopiana67932f2011-04-20 14:20:59 -0700197 // FIXME: shouldn't we take the state's transform into account here?
198
199 Transform tr(Transform(mOrientation) * Transform(mCurrentTransform));
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800200 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700201
Mathias Agopiana67932f2011-04-20 14:20:59 -0700202 if (!isOpaque()) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700203 hwcl->blending = mPremultipliedAlpha ?
204 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
205 }
206
207 hwcl->displayFrame.left = mTransformedBounds.left;
208 hwcl->displayFrame.top = mTransformedBounds.top;
209 hwcl->displayFrame.right = mTransformedBounds.right;
210 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
211
212 hwcl->visibleRegionScreen.rects =
213 reinterpret_cast<hwc_rect_t const *>(
214 visibleRegionScreen.getArray(
215 &hwcl->visibleRegionScreen.numRects));
216}
217
218void Layer::setPerFrameData(hwc_layer_t* hwcl) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700219 const sp<GraphicBuffer>& buffer(mActiveBuffer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700220 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800221 // this can happen if the client never drew into this layer yet,
222 // or if we ran out of memory. In that case, don't let
223 // HWC handle it.
224 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700225 hwcl->handle = NULL;
226 return;
227 }
Louis Huemiller04048142010-12-01 12:29:36 -0800228 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800229
Mathias Agopiana67932f2011-04-20 14:20:59 -0700230 if (isCropped()) {
231 hwcl->sourceCrop.left = mCurrentCrop.left;
232 hwcl->sourceCrop.top = mCurrentCrop.top;
233 hwcl->sourceCrop.right = mCurrentCrop.right;
234 hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
Mathias Agopianf3450692010-12-08 17:40:28 -0800235 } else {
236 hwcl->sourceCrop.left = 0;
237 hwcl->sourceCrop.top = 0;
238 hwcl->sourceCrop.right = buffer->width;
239 hwcl->sourceCrop.bottom = buffer->height;
240 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700241}
242
Mathias Agopiana67932f2011-04-20 14:20:59 -0700243static inline uint16_t pack565(int r, int g, int b) {
244 return (r<<11)|(g<<5)|b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246void Layer::onDraw(const Region& clip) const
247{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700248 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700250 // in fact never been drawn into. This happens frequently with
251 // SurfaceView because the WindowManager can't know when the client
252 // has drawn the first time.
253
254 // If there is nothing under us, we paint the screen in black, otherwise
255 // we just skip this update.
256
257 // figure out if there is something below us
258 Region under;
259 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
260 const size_t count = drawingLayers.size();
261 for (size_t i=0 ; i<count ; ++i) {
262 const sp<LayerBase>& layer(drawingLayers[i]);
263 if (layer.get() == static_cast<LayerBase const*>(this))
264 break;
265 under.orSelf(layer->visibleRegionScreen);
266 }
267 // if not everything below us is covered, we plug the holes!
268 Region holes(clip.subtract(under));
269 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700270 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700271 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 return;
273 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700274
275 GLenum target = mSurfaceTexture->getCurrentTextureTarget();
276 glBindTexture(target, mTextureName);
277 if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
278 // TODO: we could be more subtle with isFixedSize()
279 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
280 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
281 } else {
282 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
283 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
284 }
285 glEnable(target);
286 glMatrixMode(GL_TEXTURE);
287 glLoadMatrixf(mTextureMatrix);
288 glMatrixMode(GL_MODELVIEW);
289
290 drawWithOpenGL(clip);
291
292 glDisable(target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293}
294
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800295// As documented in libhardware header, formats in the range
296// 0x100 - 0x1FF are specific to the HAL implementation, and
297// are known to have no alpha channel
298// TODO: move definition for device-specific range into
299// hardware.h, instead of using hard-coded values here.
300#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
301
Mathias Agopiana67932f2011-04-20 14:20:59 -0700302bool Layer::getOpacityForFormat(uint32_t format)
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800303{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700304 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
305 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800306 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700307 PixelFormatInfo info;
308 status_t err = getPixelFormatInfo(PixelFormat(format), &info);
309 // in case of error (unknown format), we assume no blending
310 return (err || info.h_alpha <= info.l_alpha);
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800311}
312
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800313
Mathias Agopiana67932f2011-04-20 14:20:59 -0700314bool Layer::isOpaque() const
Mathias Agopiana7f66922010-05-26 22:08:52 -0700315{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316 // if we don't have a buffer yet, we're translucent regardless of the
317 // layer's opaque flag.
318 if (mActiveBuffer == 0)
319 return false;
320
321 // if the layer has the opaque flag, then we're always opaque,
322 // otherwise we use the current buffer's format.
323 return mOpaqueLayer || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700324}
325
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800326bool Layer::isProtected() const
327{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700328 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800329 return (activeBuffer != 0) &&
330 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
331}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700332
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333uint32_t Layer::doTransaction(uint32_t flags)
334{
335 const Layer::State& front(drawingState());
336 const Layer::State& temp(currentState());
337
Mathias Agopiana138f892010-05-21 17:24:35 -0700338 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
339 (front.requested_h != temp.requested_h);
340
341 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700342 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344 "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
345 "fixedSize=%d",
Mathias Agopiana138f892010-05-21 17:24:35 -0700346 this,
347 int(temp.requested_w), int(temp.requested_h),
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 int(front.requested_w), int(front.requested_h),
349 isFixedSize());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
Mathias Agopiana138f892010-05-21 17:24:35 -0700351 if (!isFixedSize()) {
352 // we're being resized and there is a freeze display request,
353 // acquire a freeze lock, so that the screen stays put
354 // until we've redrawn at the new size; this is to avoid
355 // glitches upon orientation changes.
356 if (mFlinger->hasFreezeRequest()) {
357 // if the surface is hidden, don't try to acquire the
358 // freeze lock, since hidden surfaces may never redraw
359 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
360 mFreezeLock = mFlinger->getFreezeLock();
361 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700363
364 // this will make sure LayerBase::doTransaction doesn't update
365 // the drawing state's size
366 Layer::State& editDraw(mDrawingState);
367 editDraw.requested_w = temp.requested_w;
368 editDraw.requested_h = temp.requested_h;
369
370 // record the new size, form this point on, when the client request
371 // a buffer, it'll get the new size.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700372 mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 }
374 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 if (temp.sequence != front.sequence) {
377 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
378 // this surface is now hidden, so it shouldn't hold a freeze lock
379 // (it may never redraw, which is fine if it is hidden)
380 mFreezeLock.clear();
381 }
382 }
383
384 return LayerBase::doTransaction(flags);
385}
386
Mathias Agopiana138f892010-05-21 17:24:35 -0700387bool Layer::isFixedSize() const {
388 Mutex::Autolock _l(mLock);
389 return mFixedSize;
390}
391
Mathias Agopiana67932f2011-04-20 14:20:59 -0700392void Layer::setFixedSize(bool fixedSize)
393{
394 Mutex::Autolock _l(mLock);
395 mFixedSize = fixedSize;
396}
397
398bool Layer::isCropped() const {
399 return !mCurrentCrop.isEmpty();
400}
401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402// ----------------------------------------------------------------------------
403// pageflip handling...
404// ----------------------------------------------------------------------------
405
406void Layer::lockPageFlip(bool& recomputeVisibleRegions)
407{
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700408 if (mQueuedFrames > 0) {
409 // signal another event if we have more frames pending
410 if (android_atomic_dec(&mQueuedFrames) > 1) {
411 mFlinger->signalEvent();
412 }
413
Mathias Agopiana67932f2011-04-20 14:20:59 -0700414 if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
415 // something happened!
416 recomputeVisibleRegions = true;
417 return;
418 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700419
Mathias Agopiana67932f2011-04-20 14:20:59 -0700420 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
421 mSurfaceTexture->getTransformMatrix(mTextureMatrix);
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700422
Mathias Agopiana67932f2011-04-20 14:20:59 -0700423 const Rect crop(mSurfaceTexture->getCurrentCrop());
424 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
425 if ((crop != mCurrentCrop) || (transform != mCurrentTransform)) {
426 mCurrentCrop = crop;
427 mCurrentTransform = transform;
428 mFlinger->invalidateHwcGeometry();
429 }
Mathias Agopianda9584d2010-12-13 18:51:59 -0800430
Mathias Agopiana67932f2011-04-20 14:20:59 -0700431 const bool opacity(getOpacityForFormat(mActiveBuffer->format));
432 if (opacity != mCurrentOpacity) {
433 mCurrentOpacity = opacity;
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800434 recomputeVisibleRegions = true;
435 }
436
Mathias Agopiana67932f2011-04-20 14:20:59 -0700437 const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
438 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
439 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700440
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700441 // update the layer size and release freeze-lock
442 const Layer::State& front(drawingState());
Mathias Agopiana67932f2011-04-20 14:20:59 -0700443
444 // FIXME: mPostedDirtyRegion = dirty & bounds
445 mPostedDirtyRegion.set(front.w, front.h);
446
447 sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
Jamie Gennis3629d7f2011-05-16 16:55:03 -0700448 if ((newFrontBuffer->getWidth() == front.requested_w &&
449 newFrontBuffer->getHeight() == front.requested_h) ||
450 isFixedSize())
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700451 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700452 if ((front.w != front.requested_w) ||
453 (front.h != front.requested_h))
454 {
455 // Here we pretend the transaction happened by updating the
456 // current and drawing states. Drawing state is only accessed
457 // in this thread, no need to have it locked
458 Layer::State& editDraw(mDrawingState);
459 editDraw.w = editDraw.requested_w;
460 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700461
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700462 // We also need to update the current state so that we don't
463 // end-up doing too much work during the next transaction.
464 // NOTE: We actually don't need hold the transaction lock here
465 // because State::w and State::h are only accessed from
466 // this thread
467 Layer::State& editTemp(currentState());
468 editTemp.w = editDraw.w;
469 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700470
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700471 // recompute visible region
472 recomputeVisibleRegions = true;
473 }
474
475 // we now have the correct size, unfreeze the screen
476 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700477 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479}
480
481void Layer::unlockPageFlip(
482 const Transform& planeTransform, Region& outDirtyRegion)
483{
484 Region dirtyRegion(mPostedDirtyRegion);
485 if (!dirtyRegion.isEmpty()) {
486 mPostedDirtyRegion.clear();
487 // The dirty region is given in the layer's coordinate space
488 // transform the dirty region by the surface's transformation
489 // and the global transformation.
490 const Layer::State& s(drawingState());
491 const Transform tr(planeTransform * s.transform);
492 dirtyRegion = tr.transform(dirtyRegion);
493
494 // At this point, the dirty region is in screen space.
495 // Make sure it's constrained by the visible region (which
496 // is in screen space as well).
497 dirtyRegion.andSelf(visibleRegionScreen);
498 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800500 if (visibleRegionScreen.isEmpty()) {
501 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700502 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800503 mFreezeLock.clear();
504 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505}
506
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700507void Layer::dump(String8& result, char* buffer, size_t SIZE) const
508{
509 LayerBaseClient::dump(result, buffer, SIZE);
510
Mathias Agopiana67932f2011-04-20 14:20:59 -0700511 sp<const GraphicBuffer> buf0(mActiveBuffer);
512 uint32_t w0=0, h0=0, s0=0, f0=0;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700513 if (buf0 != 0) {
514 w0 = buf0->getWidth();
515 h0 = buf0->getHeight();
516 s0 = buf0->getStride();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700517 f0 = buf0->format;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700518 }
519 snprintf(buffer, SIZE,
520 " "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700521 "format=%2d, activeBuffer=[%3ux%3u:%3u,%3u],"
522 " freezeLock=%p, queued-frames=%d\n",
523 mFormat, w0, h0, s0,f0,
524 getFreezeLock().get(), mQueuedFrames);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700525
526 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700527
Mathias Agopiana67932f2011-04-20 14:20:59 -0700528 if (mSurfaceTexture != 0) {
529 mSurfaceTexture->dump(result, " ", buffer, SIZE);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700530 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700531}
532
Mathias Agopiana67932f2011-04-20 14:20:59 -0700533uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700534{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700535 // TODO: should we do something special if mSecure is set?
536 if (mProtectedByApp) {
537 // need a hardware-protected path to external video sink
538 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700539 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700540 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700541}
542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543// ---------------------------------------------------------------------------
544
545
546}; // namespace android