blob: b12c749f277089a15eecae1ee7e9985ca20f4930 [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 Agopian076b1cc2009-04-10 14:24:30 -070030#include <ui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include "clz.h"
33#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "DisplayHardware/DisplayHardware.h"
36
37
38#define DEBUG_RESIZE 0
39
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
45const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
46const char* const Layer::typeID = "Layer";
47
48// ---------------------------------------------------------------------------
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 Agopiana4b740e2009-10-05 18:20:39 -070054 mNoEGLImageForSwBuffers(false),
Mathias Agopian401c2572009-09-23 19:16:27 -070055 mNeedsBlending(true),
56 mNeedsDithering(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057{
58 // no OpenGL operation is possible here, since we might not be
59 // in the OpenGL thread.
Mathias Agopiancbb288b2009-09-07 16:32:45 -070060 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061}
62
63Layer::~Layer()
64{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070065 destroy();
66 // the actual buffers will be destroyed here
Mathias Agopian48d819a2009-09-10 19:41:18 -070067}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070068
Mathias Agopian0aa758d2009-04-22 15:23:34 -070069void Layer::destroy()
70{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070071 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070073 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070074 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 }
76 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
77 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
78 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070079 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070080 }
Mathias Agopian48d819a2009-09-10 19:41:18 -070081 Mutex::Autolock _l(mLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -070082 mBuffers[i].clear();
Mathias Agopian48d819a2009-09-10 19:41:18 -070083 mWidth = mHeight = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070085 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
90 return mSurface;
91}
92
Mathias Agopian9a112062009-04-17 19:36:26 -070093status_t Layer::ditch()
94{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070095 // the layer is not on screen anymore. free as much resources as possible
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());
110 PixelFormatInfo displayInfo;
111 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700112 const uint32_t hwFlags = hw.getFlags();
113
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700114 mFormat = format;
115 mWidth = w;
116 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700117 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700119 mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS);
120
Mathias Agopian401c2572009-09-23 19:16:27 -0700121 // we use the red index
122 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
123 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
124 mNeedsDithering = layerRedsize > displayRedSize;
125
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700126 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700127 mBuffers[i] = new GraphicBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700129 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 return NO_ERROR;
131}
132
133void Layer::reloadTexture(const Region& dirty)
134{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700135 Mutex::Autolock _l(mLock);
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700136 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800137 if (buffer == NULL) {
138 // this situation can happen if we ran out of memory for instance.
139 // not much we can do. continue to use whatever texture was bound
140 // to this context.
141 return;
142 }
143
144 const int index = mFrontBufferIndex;
Mathias Agopian57720c32009-10-21 16:27:21 -0700145
146 // create the new texture name if needed
147 if (UNLIKELY(mTextures[index].name == -1U)) {
148 mTextures[index].name = createTexture();
149 mTextures[index].width = 0;
150 mTextures[index].height = 0;
151 }
152
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700153#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700154 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
155 if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) {
156 if (mTextures[index].dirty) {
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700157 initializeEglImage(buffer, &mTextures[index]);
Mathias Agopian57720c32009-10-21 16:27:21 -0700158 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 } else {
Mathias Agopian57720c32009-10-21 16:27:21 -0700160 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
161 mHybridBuffer->height != buffer->height)) {
162 mHybridBuffer.clear();
163 mHybridBuffer = new GraphicBuffer(
164 buffer->width, buffer->height, buffer->format,
165 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
166 GraphicBuffer::USAGE_HW_TEXTURE);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700167 initializeEglImage(
Mathias Agopian57720c32009-10-21 16:27:21 -0700168 mHybridBuffer, &mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169 }
170
Mathias Agopian57720c32009-10-21 16:27:21 -0700171 GGLSurface t;
172 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
173 LOGE_IF(res, "error %d (%s) locking buffer %p",
174 res, strerror(res), buffer.get());
175 if (res == NO_ERROR) {
176 Texture* const texture(&mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177
Mathias Agopian57720c32009-10-21 16:27:21 -0700178 glBindTexture(GL_TEXTURE_2D, texture->name);
179
180 sp<GraphicBuffer> buf(mHybridBuffer);
181 void* vaddr;
182 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
183 if (res == NO_ERROR) {
184 int bpp = 0;
185 switch (t.format) {
186 case GGL_PIXEL_FORMAT_RGB_565:
187 case GGL_PIXEL_FORMAT_RGBA_4444:
188 bpp = 2;
189 break;
190 case GGL_PIXEL_FORMAT_RGBA_8888:
191 case GGL_PIXEL_FORMAT_RGBX_8888:
192 bpp = 4;
193 break;
194 case GGL_PIXEL_FORMAT_YCbCr_422_SP:
195 case GGL_PIXEL_FORMAT_YCbCr_420_SP:
196 // just show the Y plane of YUV buffers
197 bpp = 1;
198 break;
199 default:
200 // oops, we don't handle this format!
201 LOGE("layer %p, texture=%d, using format %d, which is not "
202 "supported by the GL", this, texture->name, t.format);
203 }
204 if (bpp) {
205 const Rect bounds(dirty.getBounds());
206 size_t src_stride = t.stride;
207 size_t dst_stride = buf->stride;
208 if (src_stride == dst_stride &&
209 bounds.width() == t.width &&
210 bounds.height() == t.height)
211 {
212 memcpy(vaddr, t.data, t.height * t.stride * bpp);
213 } else {
214 GLubyte const * src = t.data +
215 (bounds.left + bounds.top * src_stride) * bpp;
216 GLubyte * dst = (GLubyte *)vaddr +
217 (bounds.left + bounds.top * dst_stride) * bpp;
218 const size_t length = bounds.width() * bpp;
219 size_t h = bounds.height();
220 src_stride *= bpp;
221 dst_stride *= bpp;
222 while (h--) {
223 memcpy(dst, src, length);
224 dst += dst_stride;
225 src += src_stride;
226 }
227 }
228 }
229 buf->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700231 buffer->unlock();
232 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700233 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700234 } else
235#endif
236 {
Mathias Agopian57720c32009-10-21 16:27:21 -0700237 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700238 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian57720c32009-10-21 16:27:21 -0700239 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700241 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700242 LOGE_IF(res, "error %d (%s) locking buffer %p",
243 res, strerror(res), buffer.get());
244 if (res == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700246 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700247 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249}
250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251void Layer::onDraw(const Region& clip) const
252{
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 int index = mFrontBufferIndex;
254 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
255 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700256 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 // the texture has not been created yet, this Layer has
259 // in fact never been drawn into. this happens frequently with
260 // SurfaceView.
261 clearWithOpenGL(clip);
262 return;
263 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700264 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
Mathias Agopian3330b202009-10-05 17:07:12 -0700267sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268{
Mathias Agopian3330b202009-10-05 17:07:12 -0700269 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700270
271 // this ensures our client doesn't go away while we're accessing
272 // the shared area.
273 sp<Client> ourClient(client.promote());
274 if (ourClient == 0) {
275 // oops, the client is already gone
276 return buffer;
277 }
278
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280 * This is called from the client's Surface::dequeue(). This can happen
281 * at any time, especially while we're in the middle of using the
282 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700283 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700284 * Make sure the buffer we're resizing is not the front buffer and has been
285 * dequeued. Once this condition is asserted, we are guaranteed that this
286 * buffer cannot become the front buffer under our feet, since we're called
287 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700288 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700289 status_t err = lcblk->assertReallocate(index);
290 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700291 if (err != NO_ERROR) {
292 // the surface may have died
293 return buffer;
294 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295
Mathias Agopian48d819a2009-09-10 19:41:18 -0700296 uint32_t w, h;
297 { // scope for the lock
298 Mutex::Autolock _l(mLock);
299 w = mWidth;
300 h = mHeight;
301 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700302
303 // destroy() could have been called before we get here, we log it
304 // because it's uncommon, and the code below should handle it
305 LOGW_IF(buffer==0,
306 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
307 index, w, h);
308
Mathias Agopian48d819a2009-09-10 19:41:18 -0700309 mBuffers[index].clear();
310 }
311
Mathias Agopian3330b202009-10-05 17:07:12 -0700312 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700313 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700314 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700316 // here we have to reallocate a new buffer because we could have a
317 // client in our process with a reference to it (eg: status bar),
318 // and we can't release the handle under its feet.
319 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700320 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700321 err = buffer->initCheck();
322 }
323
324 if (err || buffer->handle == 0) {
325 LOGE_IF(err || buffer->handle == 0,
326 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
327 this, index, w, h, strerror(-err));
328 } else {
329 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700330 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
331 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700332 }
333
334 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700335 Mutex::Autolock _l(mLock);
336 if (mWidth && mHeight) {
337 // and we have new buffer
338 mBuffers[index] = buffer;
339 // texture is now dirty...
340 mTextures[index].dirty = true;
341 } else {
342 // oops we got killed while we were allocating the buffer
343 buffer.clear();
344 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700345 }
346 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347}
348
Mathias Agopian3330b202009-10-05 17:07:12 -0700349uint32_t Layer::getEffectiveUsage(uint32_t usage) const
350{
351 /*
352 * buffers used for software rendering, but h/w composition
353 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
354 *
355 * buffers used for h/w rendering and h/w composition
356 * are allocated with HW_RENDER | HW_TEXTURE
357 *
358 * buffers used with h/w rendering and either NPOT or no egl_image_ext
359 * are allocated with SW_READ_RARELY | HW_RENDER
360 *
361 */
362
363 if (mSecure) {
364 // secure buffer, don't store it into the GPU
365 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
366 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
367 } else {
368 // it's allowed to modify the usage flags here, but generally
369 // the requested flags should be honored.
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700370 if (mNoEGLImageForSwBuffers) {
371 if (usage & GraphicBuffer::USAGE_HW_MASK) {
372 // request EGLImage for h/w buffers only
373 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
374 }
375 } else {
376 // request EGLImage for all buffers
377 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
378 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700379 }
380 return usage;
381}
382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383uint32_t Layer::doTransaction(uint32_t flags)
384{
385 const Layer::State& front(drawingState());
386 const Layer::State& temp(currentState());
387
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700388 if ((front.requested_w != temp.requested_w) ||
389 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700390 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700392 "resize (layer=%p), requested (%dx%d), "
393 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700394 this,
395 int(temp.requested_w), int(temp.requested_h),
396 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700397 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
398 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700400 // we're being resized and there is a freeze display request,
401 // acquire a freeze lock, so that the screen stays put
402 // until we've redrawn at the new size; this is to avoid
403 // glitches upon orientation changes.
404 if (mFlinger->hasFreezeRequest()) {
405 // if the surface is hidden, don't try to acquire the
406 // freeze lock, since hidden surfaces may never redraw
407 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
408 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 }
410 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700411
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700412 // this will make sure LayerBase::doTransaction doesn't update
413 // the drawing state's size
414 Layer::State& editDraw(mDrawingState);
415 editDraw.requested_w = temp.requested_w;
416 editDraw.requested_h = temp.requested_h;
417
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700418 // record the new size, form this point on, when the client request a
419 // buffer, it'll get the new size.
420 setDrawingSize(temp.requested_w, temp.requested_h);
421
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700422 // all buffers need reallocation
423 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 if (temp.sequence != front.sequence) {
427 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
428 // this surface is now hidden, so it shouldn't hold a freeze lock
429 // (it may never redraw, which is fine if it is hidden)
430 mFreezeLock.clear();
431 }
432 }
433
434 return LayerBase::doTransaction(flags);
435}
436
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700437void Layer::setDrawingSize(uint32_t w, uint32_t h) {
438 Mutex::Autolock _l(mLock);
439 mWidth = w;
440 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441}
442
443// ----------------------------------------------------------------------------
444// pageflip handling...
445// ----------------------------------------------------------------------------
446
447void Layer::lockPageFlip(bool& recomputeVisibleRegions)
448{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700449 ssize_t buf = lcblk->retireAndLock();
450 if (buf < NO_ERROR) {
451 //LOGW("nothing to retire (%s)", strerror(-buf));
452 // NOTE: here the buffer is locked because we will used
453 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 return;
455 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700456
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700457 // we retired a buffer, which becomes the new front buffer
458 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700460 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700461 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700462 const Region dirty(lcblk->getDirtyRegion(buf));
463 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700464
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700465 const Layer::State& front(drawingState());
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700466 if (newFrontBuffer->getWidth() == front.requested_w &&
467 newFrontBuffer->getHeight() == front.requested_h)
468 {
469 if ((front.w != front.requested_w) ||
470 (front.h != front.requested_h))
471 {
472 // Here we pretend the transaction happened by updating the
473 // current and drawing states. Drawing state is only accessed
474 // in this thread, no need to have it locked
475 Layer::State& editDraw(mDrawingState);
476 editDraw.w = editDraw.requested_w;
477 editDraw.h = editDraw.requested_h;
478
479 // We also need to update the current state so that we don't
480 // end-up doing too much work during the next transaction.
481 // NOTE: We actually don't need hold the transaction lock here
482 // because State::w and State::h are only accessed from
483 // this thread
484 Layer::State& editTemp(currentState());
485 editTemp.w = editDraw.w;
486 editTemp.h = editDraw.h;
487
488 // recompute visible region
489 recomputeVisibleRegions = true;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700490 }
Mathias Agopian8f2d5052009-10-07 17:58:29 -0700491
492 // we now have the correct size, unfreeze the screen
493 mFreezeLock.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700494 }
495
Mathias Agopiane7005012009-10-07 16:44:10 -0700496 if (lcblk->getQueuedCount()) {
497 // signal an event if we have more buffers waiting
498 mFlinger->signalEvent();
499 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500
Mathias Agopian3330b202009-10-05 17:07:12 -0700501 if (!mPostedDirtyRegion.isEmpty()) {
502 reloadTexture( mPostedDirtyRegion );
503 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504}
505
506void Layer::unlockPageFlip(
507 const Transform& planeTransform, Region& outDirtyRegion)
508{
509 Region dirtyRegion(mPostedDirtyRegion);
510 if (!dirtyRegion.isEmpty()) {
511 mPostedDirtyRegion.clear();
512 // The dirty region is given in the layer's coordinate space
513 // transform the dirty region by the surface's transformation
514 // and the global transformation.
515 const Layer::State& s(drawingState());
516 const Transform tr(planeTransform * s.transform);
517 dirtyRegion = tr.transform(dirtyRegion);
518
519 // At this point, the dirty region is in screen space.
520 // Make sure it's constrained by the visible region (which
521 // is in screen space as well).
522 dirtyRegion.andSelf(visibleRegionScreen);
523 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800525 if (visibleRegionScreen.isEmpty()) {
526 // an invisible layer should not hold a freeze-lock
527 // (because it may never be updated and thereore never release it)
528 mFreezeLock.clear();
529 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530}
531
532void Layer::finishPageFlip()
533{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700534 status_t err = lcblk->unlock( mFrontBufferIndex );
535 LOGE_IF(err!=NO_ERROR,
536 "layer %p, buffer=%d wasn't locked!",
537 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538}
539
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540// ---------------------------------------------------------------------------
541
Mathias Agopian9a112062009-04-17 19:36:26 -0700542Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
543 SurfaceID id, const sp<Layer>& owner)
544 : Surface(flinger, id, owner->getIdentity(), owner)
545{
546}
547
548Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700549{
550}
551
Mathias Agopian3330b202009-10-05 17:07:12 -0700552sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553{
Mathias Agopian3330b202009-10-05 17:07:12 -0700554 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700555 sp<Layer> owner(getOwner());
556 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700557 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
558 "getBuffer() index (%d) out of range", index);
559 if (uint32_t(index) < NUM_BUFFERS) {
560 buffer = owner->requestBuffer(index, usage);
561 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700562 }
563 return buffer;
564}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565
566// ---------------------------------------------------------------------------
567
568
569}; // namespace android