blob: 7ca7875366a18f55138b9aac050ea32dc70d6448 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian3330b202009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include "clz.h"
34#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
38
39#define DEBUG_RESIZE 0
40
41
42namespace android {
43
44// ---------------------------------------------------------------------------
45
46const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
47const char* const Layer::typeID = "Layer";
48
49// ---------------------------------------------------------------------------
50
Mathias Agopiancbb288b2009-09-07 16:32:45 -070051Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
52 const sp<Client>& c, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -070053 : LayerBaseClient(flinger, display, c, i),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 mSecure(false),
Mathias Agopiana4b740e2009-10-05 18:20:39 -070055 mNoEGLImageForSwBuffers(false),
Mathias Agopian401c2572009-09-23 19:16:27 -070056 mNeedsBlending(true),
57 mNeedsDithering(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058{
59 // no OpenGL operation is possible here, since we might not be
60 // in the OpenGL thread.
Mathias Agopiancbb288b2009-09-07 16:32:45 -070061 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062}
63
64Layer::~Layer()
65{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070066 destroy();
67 // the actual buffers will be destroyed here
Mathias Agopian48d819a2009-09-10 19:41:18 -070068}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070069
Mathias Agopian0aa758d2009-04-22 15:23:34 -070070void Layer::destroy()
71{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070072 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070074 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070075 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-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 Agopian0aa758d2009-04-22 15:23:34 -070080 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 }
Mathias Agopian48d819a2009-09-10 19:41:18 -070082 Mutex::Autolock _l(mLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -070083 mBuffers[i].clear();
Mathias Agopian48d819a2009-09-10 19:41:18 -070084 mWidth = mHeight = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070086 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087}
88
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090{
91 return mSurface;
92}
93
Mathias Agopian9a112062009-04-17 19:36:26 -070094status_t Layer::ditch()
95{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070096 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -080097 mFreezeLock.clear();
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070098 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -070099 return NO_ERROR;
100}
101
Mathias Agopianf9d93272009-06-19 17:00:27 -0700102status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 PixelFormat format, uint32_t flags)
104{
Mathias Agopian401c2572009-09-23 19:16:27 -0700105 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 PixelFormatInfo info;
107 status_t err = getPixelFormatInfo(format, &info);
108 if (err) return err;
109
Mathias Agopian401c2572009-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 Agopiana4b740e2009-10-05 18:20:39 -0700114 const uint32_t hwFlags = hw.getFlags();
115
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700116 mFormat = format;
117 mWidth = w;
118 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700119 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700121 mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS);
122
Mathias Agopian401c2572009-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 Agopiancbb288b2009-09-07 16:32:45 -0700128 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700129 mBuffers[i] = new GraphicBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700131 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 return NO_ERROR;
133}
134
135void Layer::reloadTexture(const Region& dirty)
136{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700137 Mutex::Autolock _l(mLock);
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700138 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian8f03b472009-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 Agopian57720c32009-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 Agopian54ba51d2009-10-26 20:12:37 -0700155#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-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 Agopianfcfeb4b2010-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 Agopian57720c32009-10-21 16:27:21 -0700164 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700165 } else {
Mathias Agopian57720c32009-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 Agopianfcfeb4b2010-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 Agopian076b1cc2009-04-10 14:24:30 -0700180 }
181
Mathias Agopian57720c32009-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 Agopian076b1cc2009-04-10 14:24:30 -0700188
Mathias Agopian57720c32009-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 Agopian54ed4f62010-02-16 17:33:37 -0800197 case HAL_PIXEL_FORMAT_RGB_565:
198 case HAL_PIXEL_FORMAT_RGBA_4444:
Mathias Agopian57720c32009-10-21 16:27:21 -0700199 bpp = 2;
200 break;
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800201 case HAL_PIXEL_FORMAT_RGBA_8888:
202 case HAL_PIXEL_FORMAT_RGBX_8888:
Mathias Agopian57720c32009-10-21 16:27:21 -0700203 bpp = 4;
204 break;
Mathias Agopian57720c32009-10-21 16:27:21 -0700205 default:
Mathias Agopian54ed4f62010-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 Agopian57720c32009-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 Agopian076b1cc2009-04-10 14:24:30 -0700241 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700242 buffer->unlock();
243 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700245 } else
246#endif
247 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800248slowpath:
Mathias Agopian57720c32009-10-21 16:27:21 -0700249 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700250 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian57720c32009-10-21 16:27:21 -0700251 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-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 Agopian3330b202009-10-05 17:07:12 -0700257 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700258 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261}
262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263void Layer::onDraw(const Region& clip) const
264{
Mathias Agopian3330b202009-10-05 17:07:12 -0700265 int index = mFrontBufferIndex;
266 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
267 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700268 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700269 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-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 Agopian1fed11c2009-06-23 18:08:22 -0700276 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277}
278
Mathias Agopian3330b202009-10-05 17:07:12 -0700279sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280{
Mathias Agopian3330b202009-10-05 17:07:12 -0700281 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-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 Agopian076b1cc2009-04-10 14:24:30 -0700291 /*
Mathias Agopiancbb288b2009-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 Agopian076b1cc2009-04-10 14:24:30 -0700295 *
Mathias Agopiancbb288b2009-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 Agopian076b1cc2009-04-10 14:24:30 -0700300 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700301 status_t err = lcblk->assertReallocate(index);
302 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700303 if (err != NO_ERROR) {
304 // the surface may have died
305 return buffer;
306 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
Mathias Agopian48d819a2009-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 Agopian6d9f6982009-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 Agopian48d819a2009-09-10 19:41:18 -0700321 mBuffers[index].clear();
322 }
323
Mathias Agopian3330b202009-10-05 17:07:12 -0700324 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700325 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700326 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327 } else {
Mathias Agopiancbb288b2009-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 Agopian3330b202009-10-05 17:07:12 -0700332 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-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 Agopian7e4a5872009-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 Agopiancbb288b2009-09-07 16:32:45 -0700344 }
345
346 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-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 Agopian076b1cc2009-04-10 14:24:30 -0700357 }
358 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359}
360
Mathias Agopian3330b202009-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 Agopiana4b740e2009-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 Agopian3330b202009-10-05 17:07:12 -0700391 }
392 return usage;
393}
394
The Android Open Source Projectedbf3b62009-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 Agopian7e4a5872009-09-29 22:39:22 -0700400 if ((front.requested_w != temp.requested_w) ||
401 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700402 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700404 "resize (layer=%p), requested (%dx%d), "
405 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-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 Agopiancbb288b2009-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 Projectedbf3b62009-03-03 19:31:44 -0800411
Mathias Agopiancbb288b2009-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 Projectedbf3b62009-03-03 19:31:44 -0800421 }
422 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700423
Mathias Agopiandf3e0b92009-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 Agopian6656dbc2009-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 Agopiancaa600c2009-09-16 18:27:24 -0700434 // all buffers need reallocation
435 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700437
The Android Open Source Projectedbf3b62009-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 Agopiancbb288b2009-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 Projectedbf3b62009-03-03 19:31:44 -0800453}
454
455// ----------------------------------------------------------------------------
456// pageflip handling...
457// ----------------------------------------------------------------------------
458
459void Layer::lockPageFlip(bool& recomputeVisibleRegions)
460{
Mathias Agopiancbb288b2009-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 Projectedbf3b62009-03-03 19:31:44 -0800466 return;
467 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700468
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700469 // we retired a buffer, which becomes the new front buffer
470 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700472 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700473 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700474 const Region dirty(lcblk->getDirtyRegion(buf));
475 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700476
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700477 const Layer::State& front(drawingState());
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700478 if (newFrontBuffer->getWidth() == front.requested_w &&
479 newFrontBuffer->getHeight() == front.requested_h)
480 {
481 if ((front.w != front.requested_w) ||
482 (front.h != front.requested_h))
483 {
484 // Here we pretend the transaction happened by updating the
485 // current and drawing states. Drawing state is only accessed
486 // in this thread, no need to have it locked
487 Layer::State& editDraw(mDrawingState);
488 editDraw.w = editDraw.requested_w;
489 editDraw.h = editDraw.requested_h;
490
491 // We also need to update the current state so that we don't
492 // end-up doing too much work during the next transaction.
493 // NOTE: We actually don't need hold the transaction lock here
494 // because State::w and State::h are only accessed from
495 // this thread
496 Layer::State& editTemp(currentState());
497 editTemp.w = editDraw.w;
498 editTemp.h = editDraw.h;
499
500 // recompute visible region
501 recomputeVisibleRegions = true;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700502 }
Mathias Agopian8f2d5052009-10-07 17:58:29 -0700503
504 // we now have the correct size, unfreeze the screen
505 mFreezeLock.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700506 }
507
Mathias Agopiane7005012009-10-07 16:44:10 -0700508 if (lcblk->getQueuedCount()) {
509 // signal an event if we have more buffers waiting
510 mFlinger->signalEvent();
511 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512
Mathias Agopian3330b202009-10-05 17:07:12 -0700513 if (!mPostedDirtyRegion.isEmpty()) {
514 reloadTexture( mPostedDirtyRegion );
515 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516}
517
518void Layer::unlockPageFlip(
519 const Transform& planeTransform, Region& outDirtyRegion)
520{
521 Region dirtyRegion(mPostedDirtyRegion);
522 if (!dirtyRegion.isEmpty()) {
523 mPostedDirtyRegion.clear();
524 // The dirty region is given in the layer's coordinate space
525 // transform the dirty region by the surface's transformation
526 // and the global transformation.
527 const Layer::State& s(drawingState());
528 const Transform tr(planeTransform * s.transform);
529 dirtyRegion = tr.transform(dirtyRegion);
530
531 // At this point, the dirty region is in screen space.
532 // Make sure it's constrained by the visible region (which
533 // is in screen space as well).
534 dirtyRegion.andSelf(visibleRegionScreen);
535 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800537 if (visibleRegionScreen.isEmpty()) {
538 // an invisible layer should not hold a freeze-lock
539 // (because it may never be updated and thereore never release it)
540 mFreezeLock.clear();
541 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542}
543
544void Layer::finishPageFlip()
545{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700546 status_t err = lcblk->unlock( mFrontBufferIndex );
547 LOGE_IF(err!=NO_ERROR,
548 "layer %p, buffer=%d wasn't locked!",
549 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550}
551
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552// ---------------------------------------------------------------------------
553
Mathias Agopian9a112062009-04-17 19:36:26 -0700554Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
555 SurfaceID id, const sp<Layer>& owner)
556 : Surface(flinger, id, owner->getIdentity(), owner)
557{
558}
559
560Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700561{
562}
563
Mathias Agopian3330b202009-10-05 17:07:12 -0700564sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565{
Mathias Agopian3330b202009-10-05 17:07:12 -0700566 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567 sp<Layer> owner(getOwner());
568 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700569 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
570 "getBuffer() index (%d) out of range", index);
571 if (uint32_t(index) < NUM_BUFFERS) {
572 buffer = owner->requestBuffer(index, usage);
573 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 }
575 return buffer;
576}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577
578// ---------------------------------------------------------------------------
579
580
581}; // namespace android