blob: f11bf1846707c01cfc2bd4644e984774f5b4c69c [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 Agopian57720c32009-10-21 16:27:21 -0700137 int index = mFrontBufferIndex;
138
139 // create the new texture name if needed
140 if (UNLIKELY(mTextures[index].name == -1U)) {
141 mTextures[index].name = createTexture();
142 mTextures[index].width = 0;
143 mTextures[index].height = 0;
144 }
145
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700146#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700147 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
148 if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) {
149 if (mTextures[index].dirty) {
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700150 initializeEglImage(buffer, &mTextures[index]);
Mathias Agopian57720c32009-10-21 16:27:21 -0700151 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700152 } else {
Mathias Agopian57720c32009-10-21 16:27:21 -0700153 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
154 mHybridBuffer->height != buffer->height)) {
155 mHybridBuffer.clear();
156 mHybridBuffer = new GraphicBuffer(
157 buffer->width, buffer->height, buffer->format,
158 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
159 GraphicBuffer::USAGE_HW_TEXTURE);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700160 initializeEglImage(
Mathias Agopian57720c32009-10-21 16:27:21 -0700161 mHybridBuffer, &mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700162 }
163
Mathias Agopian57720c32009-10-21 16:27:21 -0700164 GGLSurface t;
165 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
166 LOGE_IF(res, "error %d (%s) locking buffer %p",
167 res, strerror(res), buffer.get());
168 if (res == NO_ERROR) {
169 Texture* const texture(&mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170
Mathias Agopian57720c32009-10-21 16:27:21 -0700171 glBindTexture(GL_TEXTURE_2D, texture->name);
172
173 sp<GraphicBuffer> buf(mHybridBuffer);
174 void* vaddr;
175 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
176 if (res == NO_ERROR) {
177 int bpp = 0;
178 switch (t.format) {
179 case GGL_PIXEL_FORMAT_RGB_565:
180 case GGL_PIXEL_FORMAT_RGBA_4444:
181 bpp = 2;
182 break;
183 case GGL_PIXEL_FORMAT_RGBA_8888:
184 case GGL_PIXEL_FORMAT_RGBX_8888:
185 bpp = 4;
186 break;
187 case GGL_PIXEL_FORMAT_YCbCr_422_SP:
188 case GGL_PIXEL_FORMAT_YCbCr_420_SP:
189 // just show the Y plane of YUV buffers
190 bpp = 1;
191 break;
192 default:
193 // oops, we don't handle this format!
194 LOGE("layer %p, texture=%d, using format %d, which is not "
195 "supported by the GL", this, texture->name, t.format);
196 }
197 if (bpp) {
198 const Rect bounds(dirty.getBounds());
199 size_t src_stride = t.stride;
200 size_t dst_stride = buf->stride;
201 if (src_stride == dst_stride &&
202 bounds.width() == t.width &&
203 bounds.height() == t.height)
204 {
205 memcpy(vaddr, t.data, t.height * t.stride * bpp);
206 } else {
207 GLubyte const * src = t.data +
208 (bounds.left + bounds.top * src_stride) * bpp;
209 GLubyte * dst = (GLubyte *)vaddr +
210 (bounds.left + bounds.top * dst_stride) * bpp;
211 const size_t length = bounds.width() * bpp;
212 size_t h = bounds.height();
213 src_stride *= bpp;
214 dst_stride *= bpp;
215 while (h--) {
216 memcpy(dst, src, length);
217 dst += dst_stride;
218 src += src_stride;
219 }
220 }
221 }
222 buf->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700224 buffer->unlock();
225 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700226 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700227 } else
228#endif
229 {
Mathias Agopian57720c32009-10-21 16:27:21 -0700230 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700231 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian57720c32009-10-21 16:27:21 -0700232 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700233 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700234 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700235 LOGE_IF(res, "error %d (%s) locking buffer %p",
236 res, strerror(res), buffer.get());
237 if (res == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700238 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700239 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242}
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244void Layer::onDraw(const Region& clip) const
245{
Mathias Agopian3330b202009-10-05 17:07:12 -0700246 int index = mFrontBufferIndex;
247 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
248 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 // the texture has not been created yet, this Layer has
252 // in fact never been drawn into. this happens frequently with
253 // SurfaceView.
254 clearWithOpenGL(clip);
255 return;
256 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700257 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258}
259
Mathias Agopian3330b202009-10-05 17:07:12 -0700260sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261{
Mathias Agopian3330b202009-10-05 17:07:12 -0700262 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700263
264 // this ensures our client doesn't go away while we're accessing
265 // the shared area.
266 sp<Client> ourClient(client.promote());
267 if (ourClient == 0) {
268 // oops, the client is already gone
269 return buffer;
270 }
271
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700273 * This is called from the client's Surface::dequeue(). This can happen
274 * at any time, especially while we're in the middle of using the
275 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700276 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700277 * Make sure the buffer we're resizing is not the front buffer and has been
278 * dequeued. Once this condition is asserted, we are guaranteed that this
279 * buffer cannot become the front buffer under our feet, since we're called
280 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700282 status_t err = lcblk->assertReallocate(index);
283 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700284 if (err != NO_ERROR) {
285 // the surface may have died
286 return buffer;
287 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
Mathias Agopian48d819a2009-09-10 19:41:18 -0700289 uint32_t w, h;
290 { // scope for the lock
291 Mutex::Autolock _l(mLock);
292 w = mWidth;
293 h = mHeight;
294 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700295
296 // destroy() could have been called before we get here, we log it
297 // because it's uncommon, and the code below should handle it
298 LOGW_IF(buffer==0,
299 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
300 index, w, h);
301
Mathias Agopian48d819a2009-09-10 19:41:18 -0700302 mBuffers[index].clear();
303 }
304
Mathias Agopian3330b202009-10-05 17:07:12 -0700305 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700306 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700307 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700309 // here we have to reallocate a new buffer because we could have a
310 // client in our process with a reference to it (eg: status bar),
311 // and we can't release the handle under its feet.
312 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700313 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700314 err = buffer->initCheck();
315 }
316
317 if (err || buffer->handle == 0) {
318 LOGE_IF(err || buffer->handle == 0,
319 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
320 this, index, w, h, strerror(-err));
321 } else {
322 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700323 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
324 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700325 }
326
327 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700328 Mutex::Autolock _l(mLock);
329 if (mWidth && mHeight) {
330 // and we have new buffer
331 mBuffers[index] = buffer;
332 // texture is now dirty...
333 mTextures[index].dirty = true;
334 } else {
335 // oops we got killed while we were allocating the buffer
336 buffer.clear();
337 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700338 }
339 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340}
341
Mathias Agopian3330b202009-10-05 17:07:12 -0700342uint32_t Layer::getEffectiveUsage(uint32_t usage) const
343{
344 /*
345 * buffers used for software rendering, but h/w composition
346 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
347 *
348 * buffers used for h/w rendering and h/w composition
349 * are allocated with HW_RENDER | HW_TEXTURE
350 *
351 * buffers used with h/w rendering and either NPOT or no egl_image_ext
352 * are allocated with SW_READ_RARELY | HW_RENDER
353 *
354 */
355
356 if (mSecure) {
357 // secure buffer, don't store it into the GPU
358 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
359 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
360 } else {
361 // it's allowed to modify the usage flags here, but generally
362 // the requested flags should be honored.
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700363 if (mNoEGLImageForSwBuffers) {
364 if (usage & GraphicBuffer::USAGE_HW_MASK) {
365 // request EGLImage for h/w buffers only
366 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
367 }
368 } else {
369 // request EGLImage for all buffers
370 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
371 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700372 }
373 return usage;
374}
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376uint32_t Layer::doTransaction(uint32_t flags)
377{
378 const Layer::State& front(drawingState());
379 const Layer::State& temp(currentState());
380
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700381 if ((front.requested_w != temp.requested_w) ||
382 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700383 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700385 "resize (layer=%p), requested (%dx%d), "
386 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700387 this,
388 int(temp.requested_w), int(temp.requested_h),
389 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700390 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
391 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700393 // we're being resized and there is a freeze display request,
394 // acquire a freeze lock, so that the screen stays put
395 // until we've redrawn at the new size; this is to avoid
396 // glitches upon orientation changes.
397 if (mFlinger->hasFreezeRequest()) {
398 // if the surface is hidden, don't try to acquire the
399 // freeze lock, since hidden surfaces may never redraw
400 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
401 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 }
403 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700404
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700405 // this will make sure LayerBase::doTransaction doesn't update
406 // the drawing state's size
407 Layer::State& editDraw(mDrawingState);
408 editDraw.requested_w = temp.requested_w;
409 editDraw.requested_h = temp.requested_h;
410
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700411 // record the new size, form this point on, when the client request a
412 // buffer, it'll get the new size.
413 setDrawingSize(temp.requested_w, temp.requested_h);
414
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700415 // all buffers need reallocation
416 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 if (temp.sequence != front.sequence) {
420 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
421 // this surface is now hidden, so it shouldn't hold a freeze lock
422 // (it may never redraw, which is fine if it is hidden)
423 mFreezeLock.clear();
424 }
425 }
426
427 return LayerBase::doTransaction(flags);
428}
429
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700430void Layer::setDrawingSize(uint32_t w, uint32_t h) {
431 Mutex::Autolock _l(mLock);
432 mWidth = w;
433 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
436// ----------------------------------------------------------------------------
437// pageflip handling...
438// ----------------------------------------------------------------------------
439
440void Layer::lockPageFlip(bool& recomputeVisibleRegions)
441{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700442 ssize_t buf = lcblk->retireAndLock();
443 if (buf < NO_ERROR) {
444 //LOGW("nothing to retire (%s)", strerror(-buf));
445 // NOTE: here the buffer is locked because we will used
446 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 return;
448 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700449
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700450 // we retired a buffer, which becomes the new front buffer
451 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700453 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700454 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700455 const Region dirty(lcblk->getDirtyRegion(buf));
456 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700457
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700458 const Layer::State& front(drawingState());
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700459 if (newFrontBuffer->getWidth() == front.requested_w &&
460 newFrontBuffer->getHeight() == front.requested_h)
461 {
462 if ((front.w != front.requested_w) ||
463 (front.h != front.requested_h))
464 {
465 // Here we pretend the transaction happened by updating the
466 // current and drawing states. Drawing state is only accessed
467 // in this thread, no need to have it locked
468 Layer::State& editDraw(mDrawingState);
469 editDraw.w = editDraw.requested_w;
470 editDraw.h = editDraw.requested_h;
471
472 // We also need to update the current state so that we don't
473 // end-up doing too much work during the next transaction.
474 // NOTE: We actually don't need hold the transaction lock here
475 // because State::w and State::h are only accessed from
476 // this thread
477 Layer::State& editTemp(currentState());
478 editTemp.w = editDraw.w;
479 editTemp.h = editDraw.h;
480
481 // recompute visible region
482 recomputeVisibleRegions = true;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700483 }
Mathias Agopian8f2d5052009-10-07 17:58:29 -0700484
485 // we now have the correct size, unfreeze the screen
486 mFreezeLock.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700487 }
488
Mathias Agopiane7005012009-10-07 16:44:10 -0700489 if (lcblk->getQueuedCount()) {
490 // signal an event if we have more buffers waiting
491 mFlinger->signalEvent();
492 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493
Mathias Agopian3330b202009-10-05 17:07:12 -0700494 if (!mPostedDirtyRegion.isEmpty()) {
495 reloadTexture( mPostedDirtyRegion );
496 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497}
498
499void Layer::unlockPageFlip(
500 const Transform& planeTransform, Region& outDirtyRegion)
501{
502 Region dirtyRegion(mPostedDirtyRegion);
503 if (!dirtyRegion.isEmpty()) {
504 mPostedDirtyRegion.clear();
505 // The dirty region is given in the layer's coordinate space
506 // transform the dirty region by the surface's transformation
507 // and the global transformation.
508 const Layer::State& s(drawingState());
509 const Transform tr(planeTransform * s.transform);
510 dirtyRegion = tr.transform(dirtyRegion);
511
512 // At this point, the dirty region is in screen space.
513 // Make sure it's constrained by the visible region (which
514 // is in screen space as well).
515 dirtyRegion.andSelf(visibleRegionScreen);
516 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800518 if (visibleRegionScreen.isEmpty()) {
519 // an invisible layer should not hold a freeze-lock
520 // (because it may never be updated and thereore never release it)
521 mFreezeLock.clear();
522 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523}
524
525void Layer::finishPageFlip()
526{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700527 status_t err = lcblk->unlock( mFrontBufferIndex );
528 LOGE_IF(err!=NO_ERROR,
529 "layer %p, buffer=%d wasn't locked!",
530 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531}
532
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700533// ---------------------------------------------------------------------------
534
Mathias Agopian9a112062009-04-17 19:36:26 -0700535Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
536 SurfaceID id, const sp<Layer>& owner)
537 : Surface(flinger, id, owner->getIdentity(), owner)
538{
539}
540
541Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700542{
543}
544
Mathias Agopian3330b202009-10-05 17:07:12 -0700545sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546{
Mathias Agopian3330b202009-10-05 17:07:12 -0700547 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 sp<Layer> owner(getOwner());
549 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700550 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
551 "getBuffer() index (%d) out of range", index);
552 if (uint32_t(index) < NUM_BUFFERS) {
553 buffer = owner->requestBuffer(index, usage);
554 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700555 }
556 return buffer;
557}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558
559// ---------------------------------------------------------------------------
560
561
562}; // namespace android