blob: 4dc4a153e064fc387b21ffda7ad3668f30aacd65 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian6950e422009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33#include "clz.h"
34#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
38
39#define DEBUG_RESIZE 0
40
41
42namespace android {
43
44// ---------------------------------------------------------------------------
45
46const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
47const char* const Layer::typeID = "Layer";
48
49// ---------------------------------------------------------------------------
50
Mathias Agopian9779b222009-09-07 16:32:45 -070051Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
52 const sp<Client>& c, int32_t i)
Mathias Agopian248b5bd2009-09-10 19:41:18 -070053 : LayerBaseClient(flinger, display, c, i),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 mSecure(false),
Mathias Agopian351a7072009-10-05 18:20:39 -070055 mNoEGLImageForSwBuffers(false),
Mathias Agopiancc934762009-09-23 19:16:27 -070056 mNeedsBlending(true),
57 mNeedsDithering(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058{
59 // no OpenGL operation is possible here, since we might not be
60 // in the OpenGL thread.
Mathias Agopian9779b222009-09-07 16:32:45 -070061 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062}
63
64Layer::~Layer()
65{
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070066 destroy();
67 // the actual buffers will be destroyed here
Mathias Agopian248b5bd2009-09-10 19:41:18 -070068}
Mathias Agopian9779b222009-09-07 16:32:45 -070069
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070070void Layer::destroy()
71{
Mathias Agopian9779b222009-09-07 16:32:45 -070072 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -070073 if (mTextures[i].name != -1U) {
Mathias Agopian81b0aa62009-04-22 15:49:28 -070074 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070075 mTextures[i].name = -1U;
Mathias Agopian1473f462009-04-10 14:24:30 -070076 }
77 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
78 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
79 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070080 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian1473f462009-04-10 14:24:30 -070081 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -070082 Mutex::Autolock _l(mLock);
Mathias Agopian9779b222009-09-07 16:32:45 -070083 mBuffers[i].clear();
Mathias Agopian248b5bd2009-09-10 19:41:18 -070084 mWidth = mHeight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -070086 mSurface.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087}
88
Mathias Agopian1473f462009-04-10 14:24:30 -070089sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090{
91 return mSurface;
92}
93
Mathias Agopian6cf0db22009-04-17 19:36:26 -070094status_t Layer::ditch()
95{
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070096 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -080097 mFreezeLock.clear();
Mathias Agopian2e4b68d2009-09-23 16:44:00 -070098 destroy();
Mathias Agopian6cf0db22009-04-17 19:36:26 -070099 return NO_ERROR;
100}
101
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700102status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 PixelFormat format, uint32_t flags)
104{
Mathias Agopiancc934762009-09-23 19:16:27 -0700105 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 PixelFormatInfo info;
107 status_t err = getPixelFormatInfo(format, &info);
108 if (err) return err;
109
Mathias Agopiancc934762009-09-23 19:16:27 -0700110 // the display's pixel format
111 const DisplayHardware& hw(graphicPlane(0).displayHardware());
112 PixelFormatInfo displayInfo;
113 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700114 const uint32_t hwFlags = hw.getFlags();
115
Mathias Agopian9779b222009-09-07 16:32:45 -0700116 mFormat = format;
117 mWidth = w;
118 mHeight = h;
Mathias Agopian6950e422009-10-05 17:07:12 -0700119 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopian351a7072009-10-05 18:20:39 -0700121 mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS);
122
Mathias Agopiancc934762009-09-23 19:16:27 -0700123 // we use the red index
124 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
125 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
126 mNeedsDithering = layerRedsize > displayRedSize;
127
Mathias Agopian9779b222009-09-07 16:32:45 -0700128 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700129 mBuffers[i] = new GraphicBuffer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700131 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 return NO_ERROR;
133}
134
135void Layer::reloadTexture(const Region& dirty)
136{
Mathias Agopian9779b222009-09-07 16:32:45 -0700137 Mutex::Autolock _l(mLock);
Mathias Agopian4961c952009-10-06 19:00:57 -0700138 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian083a557c2009-12-10 15:52:29 -0800139 if (buffer == NULL) {
140 // this situation can happen if we ran out of memory for instance.
141 // not much we can do. continue to use whatever texture was bound
142 // to this context.
143 return;
144 }
145
146 const int index = mFrontBufferIndex;
Mathias Agopian382e17d2009-10-21 16:27:21 -0700147
148 // create the new texture name if needed
149 if (UNLIKELY(mTextures[index].name == -1U)) {
150 mTextures[index].name = createTexture();
151 mTextures[index].width = 0;
152 mTextures[index].height = 0;
153 }
154
Mathias Agopian9042b452009-10-26 20:12:37 -0700155#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian382e17d2009-10-21 16:27:21 -0700156 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
157 if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) {
158 if (mTextures[index].dirty) {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800159 if (initializeEglImage(buffer, &mTextures[index]) != NO_ERROR) {
160 // not sure what we can do here...
161 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
162 goto slowpath;
163 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700164 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700165 } else {
Mathias Agopian382e17d2009-10-21 16:27:21 -0700166 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
167 mHybridBuffer->height != buffer->height)) {
168 mHybridBuffer.clear();
169 mHybridBuffer = new GraphicBuffer(
170 buffer->width, buffer->height, buffer->format,
171 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
172 GraphicBuffer::USAGE_HW_TEXTURE);
Mathias Agopian1d211f82010-03-08 11:14:20 -0800173 if (initializeEglImage(
174 mHybridBuffer, &mTextures[0]) != NO_ERROR) {
175 // not sure what we can do here...
176 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
177 mHybridBuffer.clear();
178 goto slowpath;
179 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700180 }
181
Mathias Agopian382e17d2009-10-21 16:27:21 -0700182 GGLSurface t;
183 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
184 LOGE_IF(res, "error %d (%s) locking buffer %p",
185 res, strerror(res), buffer.get());
186 if (res == NO_ERROR) {
187 Texture* const texture(&mTextures[0]);
Mathias Agopian1473f462009-04-10 14:24:30 -0700188
Mathias Agopian382e17d2009-10-21 16:27:21 -0700189 glBindTexture(GL_TEXTURE_2D, texture->name);
190
191 sp<GraphicBuffer> buf(mHybridBuffer);
192 void* vaddr;
193 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
194 if (res == NO_ERROR) {
195 int bpp = 0;
196 switch (t.format) {
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800197 case HAL_PIXEL_FORMAT_RGB_565:
198 case HAL_PIXEL_FORMAT_RGBA_4444:
Mathias Agopian382e17d2009-10-21 16:27:21 -0700199 bpp = 2;
200 break;
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800201 case HAL_PIXEL_FORMAT_RGBA_8888:
202 case HAL_PIXEL_FORMAT_RGBX_8888:
Mathias Agopian382e17d2009-10-21 16:27:21 -0700203 bpp = 4;
204 break;
Mathias Agopian382e17d2009-10-21 16:27:21 -0700205 default:
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800206 if (isSupportedYuvFormat(t.format)) {
207 // just show the Y plane of YUV buffers
208 bpp = 1;
209 break;
210 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700211 // oops, we don't handle this format!
212 LOGE("layer %p, texture=%d, using format %d, which is not "
213 "supported by the GL", this, texture->name, t.format);
214 }
215 if (bpp) {
216 const Rect bounds(dirty.getBounds());
217 size_t src_stride = t.stride;
218 size_t dst_stride = buf->stride;
219 if (src_stride == dst_stride &&
220 bounds.width() == t.width &&
221 bounds.height() == t.height)
222 {
223 memcpy(vaddr, t.data, t.height * t.stride * bpp);
224 } else {
225 GLubyte const * src = t.data +
226 (bounds.left + bounds.top * src_stride) * bpp;
227 GLubyte * dst = (GLubyte *)vaddr +
228 (bounds.left + bounds.top * dst_stride) * bpp;
229 const size_t length = bounds.width() * bpp;
230 size_t h = bounds.height();
231 src_stride *= bpp;
232 dst_stride *= bpp;
233 while (h--) {
234 memcpy(dst, src, length);
235 dst += dst_stride;
236 src += src_stride;
237 }
238 }
239 }
240 buf->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700241 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700242 buffer->unlock();
243 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700244 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700245 } else
246#endif
247 {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800248slowpath:
Mathias Agopian382e17d2009-10-21 16:27:21 -0700249 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700250 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian382e17d2009-10-21 16:27:21 -0700251 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700252 GGLSurface t;
Mathias Agopian6950e422009-10-05 17:07:12 -0700253 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700254 LOGE_IF(res, "error %d (%s) locking buffer %p",
255 res, strerror(res), buffer.get());
256 if (res == NO_ERROR) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700257 loadTexture(&mTextures[0], dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700258 buffer->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261}
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263void Layer::onDraw(const Region& clip) const
264{
Mathias Agopian6950e422009-10-05 17:07:12 -0700265 int index = mFrontBufferIndex;
266 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
267 index = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700268 GLuint textureName = mTextures[index].name;
Mathias Agopian1473f462009-04-10 14:24:30 -0700269 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 // the texture has not been created yet, this Layer has
271 // in fact never been drawn into. this happens frequently with
272 // SurfaceView.
273 clearWithOpenGL(clip);
274 return;
275 }
Mathias Agopian999543b2009-06-23 18:08:22 -0700276 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277}
278
Mathias Agopian6950e422009-10-05 17:07:12 -0700279sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280{
Mathias Agopian6950e422009-10-05 17:07:12 -0700281 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700282
283 // this ensures our client doesn't go away while we're accessing
284 // the shared area.
285 sp<Client> ourClient(client.promote());
286 if (ourClient == 0) {
287 // oops, the client is already gone
288 return buffer;
289 }
290
Mathias Agopian1473f462009-04-10 14:24:30 -0700291 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700292 * This is called from the client's Surface::dequeue(). This can happen
293 * at any time, especially while we're in the middle of using the
294 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700295 *
Mathias Agopian9779b222009-09-07 16:32:45 -0700296 * Make sure the buffer we're resizing is not the front buffer and has been
297 * dequeued. Once this condition is asserted, we are guaranteed that this
298 * buffer cannot become the front buffer under our feet, since we're called
299 * from Surface::dequeue()
Mathias Agopian1473f462009-04-10 14:24:30 -0700300 */
Mathias Agopian9779b222009-09-07 16:32:45 -0700301 status_t err = lcblk->assertReallocate(index);
302 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700303 if (err != NO_ERROR) {
304 // the surface may have died
305 return buffer;
306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700308 uint32_t w, h;
309 { // scope for the lock
310 Mutex::Autolock _l(mLock);
311 w = mWidth;
312 h = mHeight;
313 buffer = mBuffers[index];
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700314
315 // destroy() could have been called before we get here, we log it
316 // because it's uncommon, and the code below should handle it
317 LOGW_IF(buffer==0,
318 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
319 index, w, h);
320
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700321 mBuffers[index].clear();
322 }
323
Mathias Agopian6950e422009-10-05 17:07:12 -0700324 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700325 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700326 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700327 } else {
Mathias Agopian9779b222009-09-07 16:32:45 -0700328 // here we have to reallocate a new buffer because we could have a
329 // client in our process with a reference to it (eg: status bar),
330 // and we can't release the handle under its feet.
331 buffer.clear();
Mathias Agopian6950e422009-10-05 17:07:12 -0700332 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopian9779b222009-09-07 16:32:45 -0700333 err = buffer->initCheck();
334 }
335
336 if (err || buffer->handle == 0) {
337 LOGE_IF(err || buffer->handle == 0,
338 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
339 this, index, w, h, strerror(-err));
340 } else {
341 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700342 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
343 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700344 }
345
346 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700347 Mutex::Autolock _l(mLock);
348 if (mWidth && mHeight) {
349 // and we have new buffer
350 mBuffers[index] = buffer;
351 // texture is now dirty...
352 mTextures[index].dirty = true;
353 } else {
354 // oops we got killed while we were allocating the buffer
355 buffer.clear();
356 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700357 }
358 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359}
360
Mathias Agopian6950e422009-10-05 17:07:12 -0700361uint32_t Layer::getEffectiveUsage(uint32_t usage) const
362{
363 /*
364 * buffers used for software rendering, but h/w composition
365 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
366 *
367 * buffers used for h/w rendering and h/w composition
368 * are allocated with HW_RENDER | HW_TEXTURE
369 *
370 * buffers used with h/w rendering and either NPOT or no egl_image_ext
371 * are allocated with SW_READ_RARELY | HW_RENDER
372 *
373 */
374
375 if (mSecure) {
376 // secure buffer, don't store it into the GPU
377 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
378 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
379 } else {
380 // it's allowed to modify the usage flags here, but generally
381 // the requested flags should be honored.
Mathias Agopian351a7072009-10-05 18:20:39 -0700382 if (mNoEGLImageForSwBuffers) {
383 if (usage & GraphicBuffer::USAGE_HW_MASK) {
384 // request EGLImage for h/w buffers only
385 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
386 }
387 } else {
388 // request EGLImage for all buffers
389 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
390 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700391 }
392 return usage;
393}
394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395uint32_t Layer::doTransaction(uint32_t flags)
396{
397 const Layer::State& front(drawingState());
398 const Layer::State& temp(currentState());
399
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700400 if ((front.requested_w != temp.requested_w) ||
401 (front.requested_h != temp.requested_h)) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700402 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian9779b222009-09-07 16:32:45 -0700404 "resize (layer=%p), requested (%dx%d), "
405 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700406 this,
407 int(temp.requested_w), int(temp.requested_h),
408 int(front.requested_w), int(front.requested_h),
Mathias Agopian9779b222009-09-07 16:32:45 -0700409 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
410 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
Mathias Agopian9779b222009-09-07 16:32:45 -0700412 // we're being resized and there is a freeze display request,
413 // acquire a freeze lock, so that the screen stays put
414 // until we've redrawn at the new size; this is to avoid
415 // glitches upon orientation changes.
416 if (mFlinger->hasFreezeRequest()) {
417 // if the surface is hidden, don't try to acquire the
418 // freeze lock, since hidden surfaces may never redraw
419 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
420 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
422 }
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700423
Mathias Agopianbd23e302009-09-30 14:07:22 -0700424 // this will make sure LayerBase::doTransaction doesn't update
425 // the drawing state's size
426 Layer::State& editDraw(mDrawingState);
427 editDraw.requested_w = temp.requested_w;
428 editDraw.requested_h = temp.requested_h;
429
Mathias Agopian70cab912009-09-30 12:48:47 -0700430 // record the new size, form this point on, when the client request a
431 // buffer, it'll get the new size.
432 setDrawingSize(temp.requested_w, temp.requested_h);
433
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700434 // all buffers need reallocation
435 lcblk->reallocate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 if (temp.sequence != front.sequence) {
439 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
440 // this surface is now hidden, so it shouldn't hold a freeze lock
441 // (it may never redraw, which is fine if it is hidden)
442 mFreezeLock.clear();
443 }
444 }
445
446 return LayerBase::doTransaction(flags);
447}
448
Mathias Agopian9779b222009-09-07 16:32:45 -0700449void Layer::setDrawingSize(uint32_t w, uint32_t h) {
450 Mutex::Autolock _l(mLock);
451 mWidth = w;
452 mHeight = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453}
454
455// ----------------------------------------------------------------------------
456// pageflip handling...
457// ----------------------------------------------------------------------------
458
459void Layer::lockPageFlip(bool& recomputeVisibleRegions)
460{
Mathias Agopian9779b222009-09-07 16:32:45 -0700461 ssize_t buf = lcblk->retireAndLock();
462 if (buf < NO_ERROR) {
463 //LOGW("nothing to retire (%s)", strerror(-buf));
464 // NOTE: here the buffer is locked because we will used
465 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 return;
467 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700468
469 // ouch, this really should never happen
470 if (uint32_t(buf)>=NUM_BUFFERS) {
471 LOGE("retireAndLock() buffer index (%d) out of range", buf);
472 mPostedDirtyRegion.clear();
473 return;
474 }
475
Mathias Agopian9779b222009-09-07 16:32:45 -0700476 // we retired a buffer, which becomes the new front buffer
477 mFrontBufferIndex = buf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
Mathias Agopian9779b222009-09-07 16:32:45 -0700479 // get the dirty region
Mathias Agopian6950e422009-10-05 17:07:12 -0700480 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700481 if (newFrontBuffer != NULL) {
482 // compute the posted region
483 const Region dirty(lcblk->getDirtyRegion(buf));
484 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700485
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700486 // update the layer size and release freeze-lock
487 const Layer::State& front(drawingState());
488 if (newFrontBuffer->getWidth() == front.requested_w &&
489 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700490 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700491 if ((front.w != front.requested_w) ||
492 (front.h != front.requested_h))
493 {
494 // Here we pretend the transaction happened by updating the
495 // current and drawing states. Drawing state is only accessed
496 // in this thread, no need to have it locked
497 Layer::State& editDraw(mDrawingState);
498 editDraw.w = editDraw.requested_w;
499 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700500
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700501 // We also need to update the current state so that we don't
502 // end-up doing too much work during the next transaction.
503 // NOTE: We actually don't need hold the transaction lock here
504 // because State::w and State::h are only accessed from
505 // this thread
506 Layer::State& editTemp(currentState());
507 editTemp.w = editDraw.w;
508 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700509
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700510 // recompute visible region
511 recomputeVisibleRegions = true;
512 }
513
514 // we now have the correct size, unfreeze the screen
515 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700516 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700517 } else {
518 // this should not happen unless we ran out of memory while
519 // allocating the buffer. we're hoping that things will get back
520 // to normal the next time the app tries to draw into this buffer.
521 // meanwhile, pretend the screen didn't update.
522 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700523 }
524
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700525 if (lcblk->getQueuedCount()) {
526 // signal an event if we have more buffers waiting
527 mFlinger->signalEvent();
528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
Mathias Agopian6950e422009-10-05 17:07:12 -0700530 if (!mPostedDirtyRegion.isEmpty()) {
531 reloadTexture( mPostedDirtyRegion );
532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533}
534
535void Layer::unlockPageFlip(
536 const Transform& planeTransform, Region& outDirtyRegion)
537{
538 Region dirtyRegion(mPostedDirtyRegion);
539 if (!dirtyRegion.isEmpty()) {
540 mPostedDirtyRegion.clear();
541 // The dirty region is given in the layer's coordinate space
542 // transform the dirty region by the surface's transformation
543 // and the global transformation.
544 const Layer::State& s(drawingState());
545 const Transform tr(planeTransform * s.transform);
546 dirtyRegion = tr.transform(dirtyRegion);
547
548 // At this point, the dirty region is in screen space.
549 // Make sure it's constrained by the visible region (which
550 // is in screen space as well).
551 dirtyRegion.andSelf(visibleRegionScreen);
552 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800554 if (visibleRegionScreen.isEmpty()) {
555 // an invisible layer should not hold a freeze-lock
556 // (because it may never be updated and thereore never release it)
557 mFreezeLock.clear();
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559}
560
561void Layer::finishPageFlip()
562{
Mathias Agopian9779b222009-09-07 16:32:45 -0700563 status_t err = lcblk->unlock( mFrontBufferIndex );
564 LOGE_IF(err!=NO_ERROR,
565 "layer %p, buffer=%d wasn't locked!",
566 this, mFrontBufferIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567}
568
Mathias Agopian1473f462009-04-10 14:24:30 -0700569// ---------------------------------------------------------------------------
570
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700571Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
572 SurfaceID id, const sp<Layer>& owner)
573 : Surface(flinger, id, owner->getIdentity(), owner)
574{
575}
576
577Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -0700578{
579}
580
Mathias Agopian6950e422009-10-05 17:07:12 -0700581sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700582{
Mathias Agopian6950e422009-10-05 17:07:12 -0700583 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700584 sp<Layer> owner(getOwner());
585 if (owner != 0) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700586 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
587 "getBuffer() index (%d) out of range", index);
588 if (uint32_t(index) < NUM_BUFFERS) {
589 buffer = owner->requestBuffer(index, usage);
590 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700591 }
592 return buffer;
593}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594
595// ---------------------------------------------------------------------------
596
597
598}; // namespace android