blob: 1fdc8ea50bbae2aae408a59f9f16aa8d2ebdb948 [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
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian3330b202009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include "clz.h"
34#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
38
39#define DEBUG_RESIZE 0
40
41
42namespace android {
43
Mathias Agopianca99fb82010-04-14 16:43:44 -070044template <typename T> inline T min(T a, T b) {
45 return a<b ? a : b;
46}
47
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048// ---------------------------------------------------------------------------
49
Mathias Agopiancbb288b2009-09-07 16:32:45 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
51 const sp<Client>& c, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -070052 : LayerBaseClient(flinger, display, c, i),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 mSecure(false),
Mathias Agopian401c2572009-09-23 19:16:27 -070054 mNeedsBlending(true),
55 mNeedsDithering(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056{
57 // no OpenGL operation is possible here, since we might not be
58 // in the OpenGL thread.
Mathias Agopiancbb288b2009-09-07 16:32:45 -070059 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
62Layer::~Layer()
63{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070064 destroy();
65 // the actual buffers will be destroyed here
Mathias Agopian48d819a2009-09-10 19:41:18 -070066}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070067
Mathias Agopian0aa758d2009-04-22 15:23:34 -070068void Layer::destroy()
69{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070070 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070072 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070073 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 }
75 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
76 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
77 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070078 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 }
Mathias Agopian48d819a2009-09-10 19:41:18 -070080 Mutex::Autolock _l(mLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -070081 mBuffers[i].clear();
Mathias Agopian48d819a2009-09-10 19:41:18 -070082 mWidth = mHeight = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070084 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
89 return mSurface;
90}
91
Mathias Agopian9a112062009-04-17 19:36:26 -070092status_t Layer::ditch()
93{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070094 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -080095 mFreezeLock.clear();
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070096 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -070097 return NO_ERROR;
98}
99
Mathias Agopianf9d93272009-06-19 17:00:27 -0700100status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 PixelFormat format, uint32_t flags)
102{
Mathias Agopian401c2572009-09-23 19:16:27 -0700103 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 PixelFormatInfo info;
105 status_t err = getPixelFormatInfo(format, &info);
106 if (err) return err;
107
Mathias Agopian401c2572009-09-23 19:16:27 -0700108 // the display's pixel format
109 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700110 uint32_t const maxSurfaceDims = min(
111 hw.getMaxTextureSize(), hw.getMaxViewportDims());
112
113 // never allow a surface larger than what our underlying GL implementation
114 // can handle.
115 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
116 return BAD_VALUE;
117 }
118
Mathias Agopian401c2572009-09-23 19:16:27 -0700119 PixelFormatInfo displayInfo;
120 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700121 const uint32_t hwFlags = hw.getFlags();
122
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700123 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700124 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700125 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700126 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700128
Mathias Agopian401c2572009-09-23 19:16:27 -0700129 // we use the red index
130 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
131 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
132 mNeedsDithering = layerRedsize > displayRedSize;
133
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700134 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700135 mBuffers[i] = new GraphicBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700137 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 return NO_ERROR;
139}
140
141void Layer::reloadTexture(const Region& dirty)
142{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700143 Mutex::Autolock _l(mLock);
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700144 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800145 if (buffer == NULL) {
146 // this situation can happen if we ran out of memory for instance.
147 // not much we can do. continue to use whatever texture was bound
148 // to this context.
149 return;
150 }
151
152 const int index = mFrontBufferIndex;
Mathias Agopian57720c32009-10-21 16:27:21 -0700153
154 // create the new texture name if needed
155 if (UNLIKELY(mTextures[index].name == -1U)) {
156 mTextures[index].name = createTexture();
157 mTextures[index].width = 0;
158 mTextures[index].height = 0;
159 }
160
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700161#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700162 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopian89141f92010-05-10 20:10:10 -0700163 if (mTextures[index].dirty) {
164 if (initializeEglImage(buffer, &mTextures[index]) != NO_ERROR) {
165 // not sure what we can do here...
166 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
167 goto slowpath;
Mathias Agopian57720c32009-10-21 16:27:21 -0700168 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700170 } else
171#endif
172 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800173slowpath:
Mathias Agopian57720c32009-10-21 16:27:21 -0700174 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700175 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian57720c32009-10-21 16:27:21 -0700176 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700178 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700179 LOGE_IF(res, "error %d (%s) locking buffer %p",
180 res, strerror(res), buffer.get());
181 if (res == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700183 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188void Layer::onDraw(const Region& clip) const
189{
Mathias Agopian3330b202009-10-05 17:07:12 -0700190 int index = mFrontBufferIndex;
191 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
192 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700196 // in fact never been drawn into. This happens frequently with
197 // SurfaceView because the WindowManager can't know when the client
198 // has drawn the first time.
199
200 // If there is nothing under us, we paint the screen in black, otherwise
201 // we just skip this update.
202
203 // figure out if there is something below us
204 Region under;
205 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
206 const size_t count = drawingLayers.size();
207 for (size_t i=0 ; i<count ; ++i) {
208 const sp<LayerBase>& layer(drawingLayers[i]);
209 if (layer.get() == static_cast<LayerBase const*>(this))
210 break;
211 under.orSelf(layer->visibleRegionScreen);
212 }
213 // if not everything below us is covered, we plug the holes!
214 Region holes(clip.subtract(under));
215 if (!holes.isEmpty()) {
216 clearWithOpenGL(holes);
217 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 return;
219 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700220 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
Mathias Agopian3330b202009-10-05 17:07:12 -0700223sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224{
Mathias Agopian3330b202009-10-05 17:07:12 -0700225 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700226
227 // this ensures our client doesn't go away while we're accessing
228 // the shared area.
229 sp<Client> ourClient(client.promote());
230 if (ourClient == 0) {
231 // oops, the client is already gone
232 return buffer;
233 }
234
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700236 * This is called from the client's Surface::dequeue(). This can happen
237 * at any time, especially while we're in the middle of using the
238 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700239 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700240 * Make sure the buffer we're resizing is not the front buffer and has been
241 * dequeued. Once this condition is asserted, we are guaranteed that this
242 * buffer cannot become the front buffer under our feet, since we're called
243 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700245 status_t err = lcblk->assertReallocate(index);
246 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700247 if (err != NO_ERROR) {
248 // the surface may have died
249 return buffer;
250 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
Mathias Agopian48d819a2009-09-10 19:41:18 -0700252 uint32_t w, h;
253 { // scope for the lock
254 Mutex::Autolock _l(mLock);
255 w = mWidth;
256 h = mHeight;
257 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700258
259 // destroy() could have been called before we get here, we log it
260 // because it's uncommon, and the code below should handle it
261 LOGW_IF(buffer==0,
262 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
263 index, w, h);
264
Mathias Agopian48d819a2009-09-10 19:41:18 -0700265 mBuffers[index].clear();
266 }
267
Mathias Agopian3330b202009-10-05 17:07:12 -0700268 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700269 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700270 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700272 // here we have to reallocate a new buffer because we could have a
273 // client in our process with a reference to it (eg: status bar),
274 // and we can't release the handle under its feet.
275 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700276 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700277 err = buffer->initCheck();
278 }
279
280 if (err || buffer->handle == 0) {
281 LOGE_IF(err || buffer->handle == 0,
282 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
283 this, index, w, h, strerror(-err));
284 } else {
285 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700286 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
287 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700288 }
289
290 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700291 Mutex::Autolock _l(mLock);
292 if (mWidth && mHeight) {
293 // and we have new buffer
294 mBuffers[index] = buffer;
295 // texture is now dirty...
296 mTextures[index].dirty = true;
297 } else {
298 // oops we got killed while we were allocating the buffer
299 buffer.clear();
300 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700301 }
302 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303}
304
Mathias Agopian3330b202009-10-05 17:07:12 -0700305uint32_t Layer::getEffectiveUsage(uint32_t usage) const
306{
307 /*
308 * buffers used for software rendering, but h/w composition
309 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
310 *
311 * buffers used for h/w rendering and h/w composition
312 * are allocated with HW_RENDER | HW_TEXTURE
313 *
314 * buffers used with h/w rendering and either NPOT or no egl_image_ext
315 * are allocated with SW_READ_RARELY | HW_RENDER
316 *
317 */
318
319 if (mSecure) {
320 // secure buffer, don't store it into the GPU
321 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
322 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
323 } else {
324 // it's allowed to modify the usage flags here, but generally
325 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700326 // request EGLImage for all buffers
327 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700328 }
329 return usage;
330}
331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332uint32_t Layer::doTransaction(uint32_t flags)
333{
334 const Layer::State& front(drawingState());
335 const Layer::State& temp(currentState());
336
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700337 if ((front.requested_w != temp.requested_w) ||
338 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700339 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700341 "resize (layer=%p), requested (%dx%d), "
342 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700343 this,
344 int(temp.requested_w), int(temp.requested_h),
345 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700346 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
347 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700349 // we're being resized and there is a freeze display request,
350 // acquire a freeze lock, so that the screen stays put
351 // until we've redrawn at the new size; this is to avoid
352 // glitches upon orientation changes.
353 if (mFlinger->hasFreezeRequest()) {
354 // if the surface is hidden, don't try to acquire the
355 // freeze lock, since hidden surfaces may never redraw
356 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
357 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 }
359 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700360
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700361 // this will make sure LayerBase::doTransaction doesn't update
362 // the drawing state's size
363 Layer::State& editDraw(mDrawingState);
364 editDraw.requested_w = temp.requested_w;
365 editDraw.requested_h = temp.requested_h;
366
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700367 // record the new size, form this point on, when the client request a
368 // buffer, it'll get the new size.
369 setDrawingSize(temp.requested_w, temp.requested_h);
370
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700371 // all buffers need reallocation
372 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 if (temp.sequence != front.sequence) {
376 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
377 // this surface is now hidden, so it shouldn't hold a freeze lock
378 // (it may never redraw, which is fine if it is hidden)
379 mFreezeLock.clear();
380 }
381 }
382
383 return LayerBase::doTransaction(flags);
384}
385
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700386void Layer::setDrawingSize(uint32_t w, uint32_t h) {
387 Mutex::Autolock _l(mLock);
388 mWidth = w;
389 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390}
391
392// ----------------------------------------------------------------------------
393// pageflip handling...
394// ----------------------------------------------------------------------------
395
396void Layer::lockPageFlip(bool& recomputeVisibleRegions)
397{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398 ssize_t buf = lcblk->retireAndLock();
399 if (buf < NO_ERROR) {
400 //LOGW("nothing to retire (%s)", strerror(-buf));
401 // NOTE: here the buffer is locked because we will used
402 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 return;
404 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700405
406 // ouch, this really should never happen
407 if (uint32_t(buf)>=NUM_BUFFERS) {
408 LOGE("retireAndLock() buffer index (%d) out of range", buf);
409 mPostedDirtyRegion.clear();
410 return;
411 }
412
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700413 // we retired a buffer, which becomes the new front buffer
414 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700416 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700417 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700418 if (newFrontBuffer != NULL) {
419 // compute the posted region
420 const Region dirty(lcblk->getDirtyRegion(buf));
421 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700422
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700423 // update the layer size and release freeze-lock
424 const Layer::State& front(drawingState());
425 if (newFrontBuffer->getWidth() == front.requested_w &&
426 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700427 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700428 if ((front.w != front.requested_w) ||
429 (front.h != front.requested_h))
430 {
431 // Here we pretend the transaction happened by updating the
432 // current and drawing states. Drawing state is only accessed
433 // in this thread, no need to have it locked
434 Layer::State& editDraw(mDrawingState);
435 editDraw.w = editDraw.requested_w;
436 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700437
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700438 // We also need to update the current state so that we don't
439 // end-up doing too much work during the next transaction.
440 // NOTE: We actually don't need hold the transaction lock here
441 // because State::w and State::h are only accessed from
442 // this thread
443 Layer::State& editTemp(currentState());
444 editTemp.w = editDraw.w;
445 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700446
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700447 // recompute visible region
448 recomputeVisibleRegions = true;
449 }
450
451 // we now have the correct size, unfreeze the screen
452 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700453 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700454 } else {
455 // this should not happen unless we ran out of memory while
456 // allocating the buffer. we're hoping that things will get back
457 // to normal the next time the app tries to draw into this buffer.
458 // meanwhile, pretend the screen didn't update.
459 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700460 }
461
Mathias Agopiane7005012009-10-07 16:44:10 -0700462 if (lcblk->getQueuedCount()) {
463 // signal an event if we have more buffers waiting
464 mFlinger->signalEvent();
465 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466
Mathias Agopian245e4d72010-04-21 15:24:11 -0700467 /* a buffer was posted, so we need to call reloadTexture(), which
468 * will update our internal data structures (eg: EGLImageKHR or
469 * texture names). we need to do this even if mPostedDirtyRegion is
470 * empty -- it's orthogonal to the fact that a new buffer was posted,
471 * for instance, a degenerate case could be that the user did an empty
472 * update but repainted the buffer with appropriate content (after a
473 * resize for instance).
474 */
475 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476}
477
478void Layer::unlockPageFlip(
479 const Transform& planeTransform, Region& outDirtyRegion)
480{
481 Region dirtyRegion(mPostedDirtyRegion);
482 if (!dirtyRegion.isEmpty()) {
483 mPostedDirtyRegion.clear();
484 // The dirty region is given in the layer's coordinate space
485 // transform the dirty region by the surface's transformation
486 // and the global transformation.
487 const Layer::State& s(drawingState());
488 const Transform tr(planeTransform * s.transform);
489 dirtyRegion = tr.transform(dirtyRegion);
490
491 // At this point, the dirty region is in screen space.
492 // Make sure it's constrained by the visible region (which
493 // is in screen space as well).
494 dirtyRegion.andSelf(visibleRegionScreen);
495 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800497 if (visibleRegionScreen.isEmpty()) {
498 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700499 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800500 mFreezeLock.clear();
501 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502}
503
504void Layer::finishPageFlip()
505{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700506 status_t err = lcblk->unlock( mFrontBufferIndex );
507 LOGE_IF(err!=NO_ERROR,
508 "layer %p, buffer=%d wasn't locked!",
509 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510}
511
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700512
513void Layer::dump(String8& result, char* buffer, size_t SIZE) const
514{
515 LayerBaseClient::dump(result, buffer, SIZE);
516
517 SharedBufferStack::Statistics stats = lcblk->getStats();
518 result.append( lcblk->dump(" ") );
519 sp<const GraphicBuffer> buf0(getBuffer(0));
520 sp<const GraphicBuffer> buf1(getBuffer(1));
521 uint32_t w0=0, h0=0, s0=0;
522 uint32_t w1=0, h1=0, s1=0;
523 if (buf0 != 0) {
524 w0 = buf0->getWidth();
525 h0 = buf0->getHeight();
526 s0 = buf0->getStride();
527 }
528 if (buf1 != 0) {
529 w1 = buf1->getWidth();
530 h1 = buf1->getHeight();
531 s1 = buf1->getStride();
532 }
533 snprintf(buffer, SIZE,
534 " "
535 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
536 " freezeLock=%p, dq-q-time=%u us\n",
537 pixelFormat(),
538 w0, h0, s0, w1, h1, s1,
539 getFreezeLock().get(), stats.totalTime);
540
541 result.append(buffer);
542}
543
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700544// ---------------------------------------------------------------------------
545
Mathias Agopian9a112062009-04-17 19:36:26 -0700546Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
547 SurfaceID id, const sp<Layer>& owner)
548 : Surface(flinger, id, owner->getIdentity(), owner)
549{
550}
551
552Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553{
554}
555
Mathias Agopian3330b202009-10-05 17:07:12 -0700556sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700557{
Mathias Agopian3330b202009-10-05 17:07:12 -0700558 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 sp<Layer> owner(getOwner());
560 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700561 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
562 "getBuffer() index (%d) out of range", index);
563 if (uint32_t(index) < NUM_BUFFERS) {
564 buffer = owner->requestBuffer(index, usage);
565 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700566 }
567 return buffer;
568}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569
570// ---------------------------------------------------------------------------
571
572
573}; // namespace android