blob: f38efab3d24f1e9fb61f8d8c4f6a87390273e228 [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 Agopian9042b452009-10-26 20:12:37 -0700159 initializeEglImage(buffer, &mTextures[index]);
Mathias Agopian382e17d2009-10-21 16:27:21 -0700160 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700161 } else {
Mathias Agopian382e17d2009-10-21 16:27:21 -0700162 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
163 mHybridBuffer->height != buffer->height)) {
164 mHybridBuffer.clear();
165 mHybridBuffer = new GraphicBuffer(
166 buffer->width, buffer->height, buffer->format,
167 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
168 GraphicBuffer::USAGE_HW_TEXTURE);
Mathias Agopian9042b452009-10-26 20:12:37 -0700169 initializeEglImage(
Mathias Agopian382e17d2009-10-21 16:27:21 -0700170 mHybridBuffer, &mTextures[0]);
Mathias Agopian1473f462009-04-10 14:24:30 -0700171 }
172
Mathias Agopian382e17d2009-10-21 16:27:21 -0700173 GGLSurface t;
174 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
175 LOGE_IF(res, "error %d (%s) locking buffer %p",
176 res, strerror(res), buffer.get());
177 if (res == NO_ERROR) {
178 Texture* const texture(&mTextures[0]);
Mathias Agopian1473f462009-04-10 14:24:30 -0700179
Mathias Agopian382e17d2009-10-21 16:27:21 -0700180 glBindTexture(GL_TEXTURE_2D, texture->name);
181
182 sp<GraphicBuffer> buf(mHybridBuffer);
183 void* vaddr;
184 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
185 if (res == NO_ERROR) {
186 int bpp = 0;
187 switch (t.format) {
188 case GGL_PIXEL_FORMAT_RGB_565:
189 case GGL_PIXEL_FORMAT_RGBA_4444:
190 bpp = 2;
191 break;
192 case GGL_PIXEL_FORMAT_RGBA_8888:
193 case GGL_PIXEL_FORMAT_RGBX_8888:
194 bpp = 4;
195 break;
196 case GGL_PIXEL_FORMAT_YCbCr_422_SP:
197 case GGL_PIXEL_FORMAT_YCbCr_420_SP:
198 // just show the Y plane of YUV buffers
199 bpp = 1;
200 break;
201 default:
202 // oops, we don't handle this format!
203 LOGE("layer %p, texture=%d, using format %d, which is not "
204 "supported by the GL", this, texture->name, t.format);
205 }
206 if (bpp) {
207 const Rect bounds(dirty.getBounds());
208 size_t src_stride = t.stride;
209 size_t dst_stride = buf->stride;
210 if (src_stride == dst_stride &&
211 bounds.width() == t.width &&
212 bounds.height() == t.height)
213 {
214 memcpy(vaddr, t.data, t.height * t.stride * bpp);
215 } else {
216 GLubyte const * src = t.data +
217 (bounds.left + bounds.top * src_stride) * bpp;
218 GLubyte * dst = (GLubyte *)vaddr +
219 (bounds.left + bounds.top * dst_stride) * bpp;
220 const size_t length = bounds.width() * bpp;
221 size_t h = bounds.height();
222 src_stride *= bpp;
223 dst_stride *= bpp;
224 while (h--) {
225 memcpy(dst, src, length);
226 dst += dst_stride;
227 src += src_stride;
228 }
229 }
230 }
231 buf->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700232 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700233 buffer->unlock();
234 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700235 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700236 } else
237#endif
238 {
Mathias Agopian382e17d2009-10-21 16:27:21 -0700239 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700240 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian382e17d2009-10-21 16:27:21 -0700241 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700242 GGLSurface t;
Mathias Agopian6950e422009-10-05 17:07:12 -0700243 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700244 LOGE_IF(res, "error %d (%s) locking buffer %p",
245 res, strerror(res), buffer.get());
246 if (res == NO_ERROR) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700247 loadTexture(&mTextures[0], dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700248 buffer->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251}
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253void Layer::onDraw(const Region& clip) const
254{
Mathias Agopian6950e422009-10-05 17:07:12 -0700255 int index = mFrontBufferIndex;
256 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
257 index = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700258 GLuint textureName = mTextures[index].name;
Mathias Agopian1473f462009-04-10 14:24:30 -0700259 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 // the texture has not been created yet, this Layer has
261 // in fact never been drawn into. this happens frequently with
262 // SurfaceView.
263 clearWithOpenGL(clip);
264 return;
265 }
Mathias Agopian999543b2009-06-23 18:08:22 -0700266 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267}
268
Mathias Agopian6950e422009-10-05 17:07:12 -0700269sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270{
Mathias Agopian6950e422009-10-05 17:07:12 -0700271 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700272
273 // this ensures our client doesn't go away while we're accessing
274 // the shared area.
275 sp<Client> ourClient(client.promote());
276 if (ourClient == 0) {
277 // oops, the client is already gone
278 return buffer;
279 }
280
Mathias Agopian1473f462009-04-10 14:24:30 -0700281 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700282 * This is called from the client's Surface::dequeue(). This can happen
283 * at any time, especially while we're in the middle of using the
284 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700285 *
Mathias Agopian9779b222009-09-07 16:32:45 -0700286 * Make sure the buffer we're resizing is not the front buffer and has been
287 * dequeued. Once this condition is asserted, we are guaranteed that this
288 * buffer cannot become the front buffer under our feet, since we're called
289 * from Surface::dequeue()
Mathias Agopian1473f462009-04-10 14:24:30 -0700290 */
Mathias Agopian9779b222009-09-07 16:32:45 -0700291 status_t err = lcblk->assertReallocate(index);
292 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700293 if (err != NO_ERROR) {
294 // the surface may have died
295 return buffer;
296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700298 uint32_t w, h;
299 { // scope for the lock
300 Mutex::Autolock _l(mLock);
301 w = mWidth;
302 h = mHeight;
303 buffer = mBuffers[index];
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700304
305 // destroy() could have been called before we get here, we log it
306 // because it's uncommon, and the code below should handle it
307 LOGW_IF(buffer==0,
308 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
309 index, w, h);
310
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700311 mBuffers[index].clear();
312 }
313
Mathias Agopian6950e422009-10-05 17:07:12 -0700314 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700315 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700316 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700317 } else {
Mathias Agopian9779b222009-09-07 16:32:45 -0700318 // here we have to reallocate a new buffer because we could have a
319 // client in our process with a reference to it (eg: status bar),
320 // and we can't release the handle under its feet.
321 buffer.clear();
Mathias Agopian6950e422009-10-05 17:07:12 -0700322 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopian9779b222009-09-07 16:32:45 -0700323 err = buffer->initCheck();
324 }
325
326 if (err || buffer->handle == 0) {
327 LOGE_IF(err || buffer->handle == 0,
328 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
329 this, index, w, h, strerror(-err));
330 } else {
331 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700332 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
333 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700334 }
335
336 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700337 Mutex::Autolock _l(mLock);
338 if (mWidth && mHeight) {
339 // and we have new buffer
340 mBuffers[index] = buffer;
341 // texture is now dirty...
342 mTextures[index].dirty = true;
343 } else {
344 // oops we got killed while we were allocating the buffer
345 buffer.clear();
346 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700347 }
348 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349}
350
Mathias Agopian6950e422009-10-05 17:07:12 -0700351uint32_t Layer::getEffectiveUsage(uint32_t usage) const
352{
353 /*
354 * buffers used for software rendering, but h/w composition
355 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
356 *
357 * buffers used for h/w rendering and h/w composition
358 * are allocated with HW_RENDER | HW_TEXTURE
359 *
360 * buffers used with h/w rendering and either NPOT or no egl_image_ext
361 * are allocated with SW_READ_RARELY | HW_RENDER
362 *
363 */
364
365 if (mSecure) {
366 // secure buffer, don't store it into the GPU
367 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
368 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
369 } else {
370 // it's allowed to modify the usage flags here, but generally
371 // the requested flags should be honored.
Mathias Agopian351a7072009-10-05 18:20:39 -0700372 if (mNoEGLImageForSwBuffers) {
373 if (usage & GraphicBuffer::USAGE_HW_MASK) {
374 // request EGLImage for h/w buffers only
375 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
376 }
377 } else {
378 // request EGLImage for all buffers
379 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
380 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700381 }
382 return usage;
383}
384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385uint32_t Layer::doTransaction(uint32_t flags)
386{
387 const Layer::State& front(drawingState());
388 const Layer::State& temp(currentState());
389
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700390 if ((front.requested_w != temp.requested_w) ||
391 (front.requested_h != temp.requested_h)) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700392 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian9779b222009-09-07 16:32:45 -0700394 "resize (layer=%p), requested (%dx%d), "
395 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700396 this,
397 int(temp.requested_w), int(temp.requested_h),
398 int(front.requested_w), int(front.requested_h),
Mathias Agopian9779b222009-09-07 16:32:45 -0700399 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
400 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Mathias Agopian9779b222009-09-07 16:32:45 -0700402 // we're being resized and there is a freeze display request,
403 // acquire a freeze lock, so that the screen stays put
404 // until we've redrawn at the new size; this is to avoid
405 // glitches upon orientation changes.
406 if (mFlinger->hasFreezeRequest()) {
407 // if the surface is hidden, don't try to acquire the
408 // freeze lock, since hidden surfaces may never redraw
409 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
410 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412 }
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700413
Mathias Agopianbd23e302009-09-30 14:07:22 -0700414 // this will make sure LayerBase::doTransaction doesn't update
415 // the drawing state's size
416 Layer::State& editDraw(mDrawingState);
417 editDraw.requested_w = temp.requested_w;
418 editDraw.requested_h = temp.requested_h;
419
Mathias Agopian70cab912009-09-30 12:48:47 -0700420 // record the new size, form this point on, when the client request a
421 // buffer, it'll get the new size.
422 setDrawingSize(temp.requested_w, temp.requested_h);
423
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700424 // all buffers need reallocation
425 lcblk->reallocate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 if (temp.sequence != front.sequence) {
429 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
430 // this surface is now hidden, so it shouldn't hold a freeze lock
431 // (it may never redraw, which is fine if it is hidden)
432 mFreezeLock.clear();
433 }
434 }
435
436 return LayerBase::doTransaction(flags);
437}
438
Mathias Agopian9779b222009-09-07 16:32:45 -0700439void Layer::setDrawingSize(uint32_t w, uint32_t h) {
440 Mutex::Autolock _l(mLock);
441 mWidth = w;
442 mHeight = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443}
444
445// ----------------------------------------------------------------------------
446// pageflip handling...
447// ----------------------------------------------------------------------------
448
449void Layer::lockPageFlip(bool& recomputeVisibleRegions)
450{
Mathias Agopian9779b222009-09-07 16:32:45 -0700451 ssize_t buf = lcblk->retireAndLock();
452 if (buf < NO_ERROR) {
453 //LOGW("nothing to retire (%s)", strerror(-buf));
454 // NOTE: here the buffer is locked because we will used
455 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 return;
457 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700458
Mathias Agopian9779b222009-09-07 16:32:45 -0700459 // we retired a buffer, which becomes the new front buffer
460 mFrontBufferIndex = buf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461
Mathias Agopian9779b222009-09-07 16:32:45 -0700462 // get the dirty region
Mathias Agopian6950e422009-10-05 17:07:12 -0700463 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9779b222009-09-07 16:32:45 -0700464 const Region dirty(lcblk->getDirtyRegion(buf));
465 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700466
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700467 const Layer::State& front(drawingState());
Mathias Agopianbd23e302009-09-30 14:07:22 -0700468 if (newFrontBuffer->getWidth() == front.requested_w &&
469 newFrontBuffer->getHeight() == front.requested_h)
470 {
471 if ((front.w != front.requested_w) ||
472 (front.h != front.requested_h))
473 {
474 // Here we pretend the transaction happened by updating the
475 // current and drawing states. Drawing state is only accessed
476 // in this thread, no need to have it locked
477 Layer::State& editDraw(mDrawingState);
478 editDraw.w = editDraw.requested_w;
479 editDraw.h = editDraw.requested_h;
480
481 // We also need to update the current state so that we don't
482 // end-up doing too much work during the next transaction.
483 // NOTE: We actually don't need hold the transaction lock here
484 // because State::w and State::h are only accessed from
485 // this thread
486 Layer::State& editTemp(currentState());
487 editTemp.w = editDraw.w;
488 editTemp.h = editDraw.h;
489
490 // recompute visible region
491 recomputeVisibleRegions = true;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700492 }
Mathias Agopian46b2df12009-10-07 17:58:29 -0700493
494 // we now have the correct size, unfreeze the screen
495 mFreezeLock.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700496 }
497
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700498 if (lcblk->getQueuedCount()) {
499 // signal an event if we have more buffers waiting
500 mFlinger->signalEvent();
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
Mathias Agopian6950e422009-10-05 17:07:12 -0700503 if (!mPostedDirtyRegion.isEmpty()) {
504 reloadTexture( mPostedDirtyRegion );
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506}
507
508void Layer::unlockPageFlip(
509 const Transform& planeTransform, Region& outDirtyRegion)
510{
511 Region dirtyRegion(mPostedDirtyRegion);
512 if (!dirtyRegion.isEmpty()) {
513 mPostedDirtyRegion.clear();
514 // The dirty region is given in the layer's coordinate space
515 // transform the dirty region by the surface's transformation
516 // and the global transformation.
517 const Layer::State& s(drawingState());
518 const Transform tr(planeTransform * s.transform);
519 dirtyRegion = tr.transform(dirtyRegion);
520
521 // At this point, the dirty region is in screen space.
522 // Make sure it's constrained by the visible region (which
523 // is in screen space as well).
524 dirtyRegion.andSelf(visibleRegionScreen);
525 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800527 if (visibleRegionScreen.isEmpty()) {
528 // an invisible layer should not hold a freeze-lock
529 // (because it may never be updated and thereore never release it)
530 mFreezeLock.clear();
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532}
533
534void Layer::finishPageFlip()
535{
Mathias Agopian9779b222009-09-07 16:32:45 -0700536 status_t err = lcblk->unlock( mFrontBufferIndex );
537 LOGE_IF(err!=NO_ERROR,
538 "layer %p, buffer=%d wasn't locked!",
539 this, mFrontBufferIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540}
541
Mathias Agopian1473f462009-04-10 14:24:30 -0700542// ---------------------------------------------------------------------------
543
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700544Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
545 SurfaceID id, const sp<Layer>& owner)
546 : Surface(flinger, id, owner->getIdentity(), owner)
547{
548}
549
550Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -0700551{
552}
553
Mathias Agopian6950e422009-10-05 17:07:12 -0700554sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700555{
Mathias Agopian6950e422009-10-05 17:07:12 -0700556 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700557 sp<Layer> owner(getOwner());
558 if (owner != 0) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700559 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
560 "getBuffer() index (%d) out of range", index);
561 if (uint32_t(index) < NUM_BUFFERS) {
562 buffer = owner->requestBuffer(index, usage);
563 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700564 }
565 return buffer;
566}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567
568// ---------------------------------------------------------------------------
569
570
571}; // namespace android