blob: 1a66970210d706550a4e698bf0ecf16e76f952b2 [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
50const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
51const char* const Layer::typeID = "Layer";
52
53// ---------------------------------------------------------------------------
54
Mathias Agopiancbb288b2009-09-07 16:32:45 -070055Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
56 const sp<Client>& c, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -070057 : LayerBaseClient(flinger, display, c, i),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 mSecure(false),
Mathias Agopiana4b740e2009-10-05 18:20:39 -070059 mNoEGLImageForSwBuffers(false),
Mathias Agopian401c2572009-09-23 19:16:27 -070060 mNeedsBlending(true),
61 mNeedsDithering(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062{
63 // no OpenGL operation is possible here, since we might not be
64 // in the OpenGL thread.
Mathias Agopiancbb288b2009-09-07 16:32:45 -070065 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066}
67
68Layer::~Layer()
69{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070070 destroy();
71 // the actual buffers will be destroyed here
Mathias Agopian48d819a2009-09-10 19:41:18 -070072}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070073
Mathias Agopian0aa758d2009-04-22 15:23:34 -070074void Layer::destroy()
75{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070076 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070078 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070079 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070080 }
81 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
82 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
83 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070084 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 }
Mathias Agopian48d819a2009-09-10 19:41:18 -070086 Mutex::Autolock _l(mLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -070087 mBuffers[i].clear();
Mathias Agopian48d819a2009-09-10 19:41:18 -070088 mWidth = mHeight = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070090 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
Mathias Agopian076b1cc2009-04-10 14:24:30 -070093sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094{
95 return mSurface;
96}
97
Mathias Agopian9a112062009-04-17 19:36:26 -070098status_t Layer::ditch()
99{
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700100 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800101 mFreezeLock.clear();
Mathias Agopian8c0a3d72009-09-23 16:44:00 -0700102 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -0700103 return NO_ERROR;
104}
105
Mathias Agopianf9d93272009-06-19 17:00:27 -0700106status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 PixelFormat format, uint32_t flags)
108{
Mathias Agopian401c2572009-09-23 19:16:27 -0700109 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 PixelFormatInfo info;
111 status_t err = getPixelFormatInfo(format, &info);
112 if (err) return err;
113
Mathias Agopian401c2572009-09-23 19:16:27 -0700114 // the display's pixel format
115 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700116 uint32_t const maxSurfaceDims = min(
117 hw.getMaxTextureSize(), hw.getMaxViewportDims());
118
119 // never allow a surface larger than what our underlying GL implementation
120 // can handle.
121 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
122 return BAD_VALUE;
123 }
124
Mathias Agopian401c2572009-09-23 19:16:27 -0700125 PixelFormatInfo displayInfo;
126 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700127 const uint32_t hwFlags = hw.getFlags();
128
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700129 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700130 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700131 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700132 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700134 mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS);
Mathias Agopianca99fb82010-04-14 16:43:44 -0700135
Mathias Agopian401c2572009-09-23 19:16:27 -0700136 // we use the red index
137 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
138 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
139 mNeedsDithering = layerRedsize > displayRedSize;
140
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700141 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700142 mBuffers[i] = new GraphicBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700144 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 return NO_ERROR;
146}
147
148void Layer::reloadTexture(const Region& dirty)
149{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700150 Mutex::Autolock _l(mLock);
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700151 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800152 if (buffer == NULL) {
153 // this situation can happen if we ran out of memory for instance.
154 // not much we can do. continue to use whatever texture was bound
155 // to this context.
156 return;
157 }
158
159 const int index = mFrontBufferIndex;
Mathias Agopian57720c32009-10-21 16:27:21 -0700160
161 // create the new texture name if needed
162 if (UNLIKELY(mTextures[index].name == -1U)) {
163 mTextures[index].name = createTexture();
164 mTextures[index].width = 0;
165 mTextures[index].height = 0;
166 }
167
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700168#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700169 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
170 if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) {
171 if (mTextures[index].dirty) {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800172 if (initializeEglImage(buffer, &mTextures[index]) != NO_ERROR) {
173 // not sure what we can do here...
174 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
175 goto slowpath;
176 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700177 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178 } else {
Mathias Agopian57720c32009-10-21 16:27:21 -0700179 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
180 mHybridBuffer->height != buffer->height)) {
181 mHybridBuffer.clear();
182 mHybridBuffer = new GraphicBuffer(
183 buffer->width, buffer->height, buffer->format,
184 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
185 GraphicBuffer::USAGE_HW_TEXTURE);
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800186 if (initializeEglImage(
187 mHybridBuffer, &mTextures[0]) != NO_ERROR) {
188 // not sure what we can do here...
189 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
190 mHybridBuffer.clear();
191 goto slowpath;
192 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 }
194
Mathias Agopian57720c32009-10-21 16:27:21 -0700195 GGLSurface t;
196 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
197 LOGE_IF(res, "error %d (%s) locking buffer %p",
198 res, strerror(res), buffer.get());
199 if (res == NO_ERROR) {
200 Texture* const texture(&mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201
Mathias Agopian57720c32009-10-21 16:27:21 -0700202 glBindTexture(GL_TEXTURE_2D, texture->name);
203
204 sp<GraphicBuffer> buf(mHybridBuffer);
205 void* vaddr;
206 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
207 if (res == NO_ERROR) {
208 int bpp = 0;
209 switch (t.format) {
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800210 case HAL_PIXEL_FORMAT_RGB_565:
211 case HAL_PIXEL_FORMAT_RGBA_4444:
Mathias Agopian57720c32009-10-21 16:27:21 -0700212 bpp = 2;
213 break;
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800214 case HAL_PIXEL_FORMAT_RGBA_8888:
215 case HAL_PIXEL_FORMAT_RGBX_8888:
Mathias Agopian57720c32009-10-21 16:27:21 -0700216 bpp = 4;
217 break;
Mathias Agopian57720c32009-10-21 16:27:21 -0700218 default:
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800219 if (isSupportedYuvFormat(t.format)) {
220 // just show the Y plane of YUV buffers
221 bpp = 1;
222 break;
223 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700224 // oops, we don't handle this format!
225 LOGE("layer %p, texture=%d, using format %d, which is not "
226 "supported by the GL", this, texture->name, t.format);
227 }
228 if (bpp) {
229 const Rect bounds(dirty.getBounds());
230 size_t src_stride = t.stride;
231 size_t dst_stride = buf->stride;
232 if (src_stride == dst_stride &&
233 bounds.width() == t.width &&
234 bounds.height() == t.height)
235 {
236 memcpy(vaddr, t.data, t.height * t.stride * bpp);
237 } else {
238 GLubyte const * src = t.data +
239 (bounds.left + bounds.top * src_stride) * bpp;
240 GLubyte * dst = (GLubyte *)vaddr +
241 (bounds.left + bounds.top * dst_stride) * bpp;
242 const size_t length = bounds.width() * bpp;
243 size_t h = bounds.height();
244 src_stride *= bpp;
245 dst_stride *= bpp;
246 while (h--) {
247 memcpy(dst, src, length);
248 dst += dst_stride;
249 src += src_stride;
250 }
251 }
252 }
253 buf->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700255 buffer->unlock();
256 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700258 } else
259#endif
260 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800261slowpath:
Mathias Agopian57720c32009-10-21 16:27:21 -0700262 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700263 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian57720c32009-10-21 16:27:21 -0700264 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700266 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700267 LOGE_IF(res, "error %d (%s) locking buffer %p",
268 res, strerror(res), buffer.get());
269 if (res == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700270 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700271 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274}
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276void Layer::onDraw(const Region& clip) const
277{
Mathias Agopian3330b202009-10-05 17:07:12 -0700278 int index = mFrontBufferIndex;
279 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
280 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700282 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 // the texture has not been created yet, this Layer has
284 // in fact never been drawn into. this happens frequently with
285 // SurfaceView.
286 clearWithOpenGL(clip);
287 return;
288 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700289 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290}
291
Mathias Agopian3330b202009-10-05 17:07:12 -0700292sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293{
Mathias Agopian3330b202009-10-05 17:07:12 -0700294 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700295
296 // this ensures our client doesn't go away while we're accessing
297 // the shared area.
298 sp<Client> ourClient(client.promote());
299 if (ourClient == 0) {
300 // oops, the client is already gone
301 return buffer;
302 }
303
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700304 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700305 * This is called from the client's Surface::dequeue(). This can happen
306 * at any time, especially while we're in the middle of using the
307 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700309 * Make sure the buffer we're resizing is not the front buffer and has been
310 * dequeued. Once this condition is asserted, we are guaranteed that this
311 * buffer cannot become the front buffer under our feet, since we're called
312 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700314 status_t err = lcblk->assertReallocate(index);
315 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700316 if (err != NO_ERROR) {
317 // the surface may have died
318 return buffer;
319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320
Mathias Agopian48d819a2009-09-10 19:41:18 -0700321 uint32_t w, h;
322 { // scope for the lock
323 Mutex::Autolock _l(mLock);
324 w = mWidth;
325 h = mHeight;
326 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700327
328 // destroy() could have been called before we get here, we log it
329 // because it's uncommon, and the code below should handle it
330 LOGW_IF(buffer==0,
331 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
332 index, w, h);
333
Mathias Agopian48d819a2009-09-10 19:41:18 -0700334 mBuffers[index].clear();
335 }
336
Mathias Agopian3330b202009-10-05 17:07:12 -0700337 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700338 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700339 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700341 // here we have to reallocate a new buffer because we could have a
342 // client in our process with a reference to it (eg: status bar),
343 // and we can't release the handle under its feet.
344 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700345 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700346 err = buffer->initCheck();
347 }
348
349 if (err || buffer->handle == 0) {
350 LOGE_IF(err || buffer->handle == 0,
351 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
352 this, index, w, h, strerror(-err));
353 } else {
354 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700355 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
356 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700357 }
358
359 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700360 Mutex::Autolock _l(mLock);
361 if (mWidth && mHeight) {
362 // and we have new buffer
363 mBuffers[index] = buffer;
364 // texture is now dirty...
365 mTextures[index].dirty = true;
366 } else {
367 // oops we got killed while we were allocating the buffer
368 buffer.clear();
369 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700370 }
371 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372}
373
Mathias Agopian3330b202009-10-05 17:07:12 -0700374uint32_t Layer::getEffectiveUsage(uint32_t usage) const
375{
376 /*
377 * buffers used for software rendering, but h/w composition
378 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
379 *
380 * buffers used for h/w rendering and h/w composition
381 * are allocated with HW_RENDER | HW_TEXTURE
382 *
383 * buffers used with h/w rendering and either NPOT or no egl_image_ext
384 * are allocated with SW_READ_RARELY | HW_RENDER
385 *
386 */
387
388 if (mSecure) {
389 // secure buffer, don't store it into the GPU
390 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
391 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
392 } else {
393 // it's allowed to modify the usage flags here, but generally
394 // the requested flags should be honored.
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700395 if (mNoEGLImageForSwBuffers) {
396 if (usage & GraphicBuffer::USAGE_HW_MASK) {
397 // request EGLImage for h/w buffers only
398 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
399 }
400 } else {
401 // request EGLImage for all buffers
402 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
403 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700404 }
405 return usage;
406}
407
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408uint32_t Layer::doTransaction(uint32_t flags)
409{
410 const Layer::State& front(drawingState());
411 const Layer::State& temp(currentState());
412
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700413 if ((front.requested_w != temp.requested_w) ||
414 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700415 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700417 "resize (layer=%p), requested (%dx%d), "
418 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700419 this,
420 int(temp.requested_w), int(temp.requested_h),
421 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700422 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
423 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700425 // we're being resized and there is a freeze display request,
426 // acquire a freeze lock, so that the screen stays put
427 // until we've redrawn at the new size; this is to avoid
428 // glitches upon orientation changes.
429 if (mFlinger->hasFreezeRequest()) {
430 // if the surface is hidden, don't try to acquire the
431 // freeze lock, since hidden surfaces may never redraw
432 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
433 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 }
435 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700436
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700437 // this will make sure LayerBase::doTransaction doesn't update
438 // the drawing state's size
439 Layer::State& editDraw(mDrawingState);
440 editDraw.requested_w = temp.requested_w;
441 editDraw.requested_h = temp.requested_h;
442
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700443 // record the new size, form this point on, when the client request a
444 // buffer, it'll get the new size.
445 setDrawingSize(temp.requested_w, temp.requested_h);
446
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700447 // all buffers need reallocation
448 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 if (temp.sequence != front.sequence) {
452 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
453 // this surface is now hidden, so it shouldn't hold a freeze lock
454 // (it may never redraw, which is fine if it is hidden)
455 mFreezeLock.clear();
456 }
457 }
458
459 return LayerBase::doTransaction(flags);
460}
461
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700462void Layer::setDrawingSize(uint32_t w, uint32_t h) {
463 Mutex::Autolock _l(mLock);
464 mWidth = w;
465 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466}
467
468// ----------------------------------------------------------------------------
469// pageflip handling...
470// ----------------------------------------------------------------------------
471
472void Layer::lockPageFlip(bool& recomputeVisibleRegions)
473{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700474 ssize_t buf = lcblk->retireAndLock();
475 if (buf < NO_ERROR) {
476 //LOGW("nothing to retire (%s)", strerror(-buf));
477 // NOTE: here the buffer is locked because we will used
478 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 return;
480 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700481
482 // ouch, this really should never happen
483 if (uint32_t(buf)>=NUM_BUFFERS) {
484 LOGE("retireAndLock() buffer index (%d) out of range", buf);
485 mPostedDirtyRegion.clear();
486 return;
487 }
488
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700489 // we retired a buffer, which becomes the new front buffer
490 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700492 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700493 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700494 if (newFrontBuffer != NULL) {
495 // compute the posted region
496 const Region dirty(lcblk->getDirtyRegion(buf));
497 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700498
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700499 // update the layer size and release freeze-lock
500 const Layer::State& front(drawingState());
501 if (newFrontBuffer->getWidth() == front.requested_w &&
502 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700503 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700504 if ((front.w != front.requested_w) ||
505 (front.h != front.requested_h))
506 {
507 // Here we pretend the transaction happened by updating the
508 // current and drawing states. Drawing state is only accessed
509 // in this thread, no need to have it locked
510 Layer::State& editDraw(mDrawingState);
511 editDraw.w = editDraw.requested_w;
512 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700513
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700514 // We also need to update the current state so that we don't
515 // end-up doing too much work during the next transaction.
516 // NOTE: We actually don't need hold the transaction lock here
517 // because State::w and State::h are only accessed from
518 // this thread
519 Layer::State& editTemp(currentState());
520 editTemp.w = editDraw.w;
521 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700522
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700523 // recompute visible region
524 recomputeVisibleRegions = true;
525 }
526
527 // we now have the correct size, unfreeze the screen
528 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700529 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700530 } else {
531 // this should not happen unless we ran out of memory while
532 // allocating the buffer. we're hoping that things will get back
533 // to normal the next time the app tries to draw into this buffer.
534 // meanwhile, pretend the screen didn't update.
535 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700536 }
537
Mathias Agopiane7005012009-10-07 16:44:10 -0700538 if (lcblk->getQueuedCount()) {
539 // signal an event if we have more buffers waiting
540 mFlinger->signalEvent();
541 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
Mathias Agopian245e4d72010-04-21 15:24:11 -0700543 /* a buffer was posted, so we need to call reloadTexture(), which
544 * will update our internal data structures (eg: EGLImageKHR or
545 * texture names). we need to do this even if mPostedDirtyRegion is
546 * empty -- it's orthogonal to the fact that a new buffer was posted,
547 * for instance, a degenerate case could be that the user did an empty
548 * update but repainted the buffer with appropriate content (after a
549 * resize for instance).
550 */
551 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552}
553
554void Layer::unlockPageFlip(
555 const Transform& planeTransform, Region& outDirtyRegion)
556{
557 Region dirtyRegion(mPostedDirtyRegion);
558 if (!dirtyRegion.isEmpty()) {
559 mPostedDirtyRegion.clear();
560 // The dirty region is given in the layer's coordinate space
561 // transform the dirty region by the surface's transformation
562 // and the global transformation.
563 const Layer::State& s(drawingState());
564 const Transform tr(planeTransform * s.transform);
565 dirtyRegion = tr.transform(dirtyRegion);
566
567 // At this point, the dirty region is in screen space.
568 // Make sure it's constrained by the visible region (which
569 // is in screen space as well).
570 dirtyRegion.andSelf(visibleRegionScreen);
571 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800573 if (visibleRegionScreen.isEmpty()) {
574 // an invisible layer should not hold a freeze-lock
575 // (because it may never be updated and thereore never release it)
576 mFreezeLock.clear();
577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578}
579
580void Layer::finishPageFlip()
581{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700582 status_t err = lcblk->unlock( mFrontBufferIndex );
583 LOGE_IF(err!=NO_ERROR,
584 "layer %p, buffer=%d wasn't locked!",
585 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586}
587
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588// ---------------------------------------------------------------------------
589
Mathias Agopian9a112062009-04-17 19:36:26 -0700590Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
591 SurfaceID id, const sp<Layer>& owner)
592 : Surface(flinger, id, owner->getIdentity(), owner)
593{
594}
595
596Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597{
598}
599
Mathias Agopian3330b202009-10-05 17:07:12 -0700600sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700601{
Mathias Agopian3330b202009-10-05 17:07:12 -0700602 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 sp<Layer> owner(getOwner());
604 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700605 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
606 "getBuffer() index (%d) out of range", index);
607 if (uint32_t(index) < NUM_BUFFERS) {
608 buffer = owner->requestBuffer(index, usage);
609 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700610 }
611 return buffer;
612}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613
614// ---------------------------------------------------------------------------
615
616
617}; // namespace android