blob: 1870d3a29cff2d55b3eccfd27e01e9a1896a9ddf [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 Agopianf5430db2009-12-11 00:56:10 -080096 mFreezeLock.clear();
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070097 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -070098 return NO_ERROR;
99}
100
Mathias Agopianf9d93272009-06-19 17:00:27 -0700101status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 PixelFormat format, uint32_t flags)
103{
Mathias Agopian401c2572009-09-23 19:16:27 -0700104 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 PixelFormatInfo info;
106 status_t err = getPixelFormatInfo(format, &info);
107 if (err) return err;
108
Mathias Agopian401c2572009-09-23 19:16:27 -0700109 // the display's pixel format
110 const DisplayHardware& hw(graphicPlane(0).displayHardware());
111 PixelFormatInfo displayInfo;
112 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700113 const uint32_t hwFlags = hw.getFlags();
114
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700115 mFormat = format;
116 mWidth = w;
117 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700118 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700120 mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS);
121
Mathias Agopian401c2572009-09-23 19:16:27 -0700122 // we use the red index
123 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
124 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
125 mNeedsDithering = layerRedsize > displayRedSize;
126
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700127 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700128 mBuffers[i] = new GraphicBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700130 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 return NO_ERROR;
132}
133
134void Layer::reloadTexture(const Region& dirty)
135{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700136 Mutex::Autolock _l(mLock);
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700137 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800138 if (buffer == NULL) {
139 // this situation can happen if we ran out of memory for instance.
140 // not much we can do. continue to use whatever texture was bound
141 // to this context.
142 return;
143 }
144
145 const int index = mFrontBufferIndex;
Mathias Agopian57720c32009-10-21 16:27:21 -0700146
147 // create the new texture name if needed
148 if (UNLIKELY(mTextures[index].name == -1U)) {
149 mTextures[index].name = createTexture();
150 mTextures[index].width = 0;
151 mTextures[index].height = 0;
152 }
153
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700154#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700155 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
156 if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) {
157 if (mTextures[index].dirty) {
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700158 initializeEglImage(buffer, &mTextures[index]);
Mathias Agopian57720c32009-10-21 16:27:21 -0700159 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160 } else {
Mathias Agopian57720c32009-10-21 16:27:21 -0700161 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
162 mHybridBuffer->height != buffer->height)) {
163 mHybridBuffer.clear();
164 mHybridBuffer = new GraphicBuffer(
165 buffer->width, buffer->height, buffer->format,
166 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
167 GraphicBuffer::USAGE_HW_TEXTURE);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700168 initializeEglImage(
Mathias Agopian57720c32009-10-21 16:27:21 -0700169 mHybridBuffer, &mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170 }
171
Mathias Agopian57720c32009-10-21 16:27:21 -0700172 GGLSurface t;
173 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
174 LOGE_IF(res, "error %d (%s) locking buffer %p",
175 res, strerror(res), buffer.get());
176 if (res == NO_ERROR) {
177 Texture* const texture(&mTextures[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178
Mathias Agopian57720c32009-10-21 16:27:21 -0700179 glBindTexture(GL_TEXTURE_2D, texture->name);
180
181 sp<GraphicBuffer> buf(mHybridBuffer);
182 void* vaddr;
183 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
184 if (res == NO_ERROR) {
185 int bpp = 0;
186 switch (t.format) {
187 case GGL_PIXEL_FORMAT_RGB_565:
188 case GGL_PIXEL_FORMAT_RGBA_4444:
189 bpp = 2;
190 break;
191 case GGL_PIXEL_FORMAT_RGBA_8888:
192 case GGL_PIXEL_FORMAT_RGBX_8888:
193 bpp = 4;
194 break;
195 case GGL_PIXEL_FORMAT_YCbCr_422_SP:
196 case GGL_PIXEL_FORMAT_YCbCr_420_SP:
197 // just show the Y plane of YUV buffers
198 bpp = 1;
199 break;
200 default:
201 // oops, we don't handle this format!
202 LOGE("layer %p, texture=%d, using format %d, which is not "
203 "supported by the GL", this, texture->name, t.format);
204 }
205 if (bpp) {
206 const Rect bounds(dirty.getBounds());
207 size_t src_stride = t.stride;
208 size_t dst_stride = buf->stride;
209 if (src_stride == dst_stride &&
210 bounds.width() == t.width &&
211 bounds.height() == t.height)
212 {
213 memcpy(vaddr, t.data, t.height * t.stride * bpp);
214 } else {
215 GLubyte const * src = t.data +
216 (bounds.left + bounds.top * src_stride) * bpp;
217 GLubyte * dst = (GLubyte *)vaddr +
218 (bounds.left + bounds.top * dst_stride) * bpp;
219 const size_t length = bounds.width() * bpp;
220 size_t h = bounds.height();
221 src_stride *= bpp;
222 dst_stride *= bpp;
223 while (h--) {
224 memcpy(dst, src, length);
225 dst += dst_stride;
226 src += src_stride;
227 }
228 }
229 }
230 buf->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700231 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700232 buffer->unlock();
233 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700235 } else
236#endif
237 {
Mathias Agopian57720c32009-10-21 16:27:21 -0700238 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700239 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian57720c32009-10-21 16:27:21 -0700240 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700241 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700242 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700243 LOGE_IF(res, "error %d (%s) locking buffer %p",
244 res, strerror(res), buffer.get());
245 if (res == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700246 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700247 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252void Layer::onDraw(const Region& clip) const
253{
Mathias Agopian3330b202009-10-05 17:07:12 -0700254 int index = mFrontBufferIndex;
255 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
256 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 // the texture has not been created yet, this Layer has
260 // in fact never been drawn into. this happens frequently with
261 // SurfaceView.
262 clearWithOpenGL(clip);
263 return;
264 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700265 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
Mathias Agopian3330b202009-10-05 17:07:12 -0700268sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269{
Mathias Agopian3330b202009-10-05 17:07:12 -0700270 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700271
272 // this ensures our client doesn't go away while we're accessing
273 // the shared area.
274 sp<Client> ourClient(client.promote());
275 if (ourClient == 0) {
276 // oops, the client is already gone
277 return buffer;
278 }
279
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700281 * This is called from the client's Surface::dequeue(). This can happen
282 * at any time, especially while we're in the middle of using the
283 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700284 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700285 * Make sure the buffer we're resizing is not the front buffer and has been
286 * dequeued. Once this condition is asserted, we are guaranteed that this
287 * buffer cannot become the front buffer under our feet, since we're called
288 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700290 status_t err = lcblk->assertReallocate(index);
291 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700292 if (err != NO_ERROR) {
293 // the surface may have died
294 return buffer;
295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
Mathias Agopian48d819a2009-09-10 19:41:18 -0700297 uint32_t w, h;
298 { // scope for the lock
299 Mutex::Autolock _l(mLock);
300 w = mWidth;
301 h = mHeight;
302 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700303
304 // destroy() could have been called before we get here, we log it
305 // because it's uncommon, and the code below should handle it
306 LOGW_IF(buffer==0,
307 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
308 index, w, h);
309
Mathias Agopian48d819a2009-09-10 19:41:18 -0700310 mBuffers[index].clear();
311 }
312
Mathias Agopian3330b202009-10-05 17:07:12 -0700313 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700314 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700315 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700317 // here we have to reallocate a new buffer because we could have a
318 // client in our process with a reference to it (eg: status bar),
319 // and we can't release the handle under its feet.
320 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700321 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700322 err = buffer->initCheck();
323 }
324
325 if (err || buffer->handle == 0) {
326 LOGE_IF(err || buffer->handle == 0,
327 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
328 this, index, w, h, strerror(-err));
329 } else {
330 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700331 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
332 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700333 }
334
335 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700336 Mutex::Autolock _l(mLock);
337 if (mWidth && mHeight) {
338 // and we have new buffer
339 mBuffers[index] = buffer;
340 // texture is now dirty...
341 mTextures[index].dirty = true;
342 } else {
343 // oops we got killed while we were allocating the buffer
344 buffer.clear();
345 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 }
347 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348}
349
Mathias Agopian3330b202009-10-05 17:07:12 -0700350uint32_t Layer::getEffectiveUsage(uint32_t usage) const
351{
352 /*
353 * buffers used for software rendering, but h/w composition
354 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
355 *
356 * buffers used for h/w rendering and h/w composition
357 * are allocated with HW_RENDER | HW_TEXTURE
358 *
359 * buffers used with h/w rendering and either NPOT or no egl_image_ext
360 * are allocated with SW_READ_RARELY | HW_RENDER
361 *
362 */
363
364 if (mSecure) {
365 // secure buffer, don't store it into the GPU
366 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
367 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
368 } else {
369 // it's allowed to modify the usage flags here, but generally
370 // the requested flags should be honored.
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700371 if (mNoEGLImageForSwBuffers) {
372 if (usage & GraphicBuffer::USAGE_HW_MASK) {
373 // request EGLImage for h/w buffers only
374 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
375 }
376 } else {
377 // request EGLImage for all buffers
378 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
379 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700380 }
381 return usage;
382}
383
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384uint32_t Layer::doTransaction(uint32_t flags)
385{
386 const Layer::State& front(drawingState());
387 const Layer::State& temp(currentState());
388
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700389 if ((front.requested_w != temp.requested_w) ||
390 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700391 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700393 "resize (layer=%p), requested (%dx%d), "
394 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700395 this,
396 int(temp.requested_w), int(temp.requested_h),
397 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
399 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700401 // we're being resized and there is a freeze display request,
402 // acquire a freeze lock, so that the screen stays put
403 // until we've redrawn at the new size; this is to avoid
404 // glitches upon orientation changes.
405 if (mFlinger->hasFreezeRequest()) {
406 // if the surface is hidden, don't try to acquire the
407 // freeze lock, since hidden surfaces may never redraw
408 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
409 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 }
411 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700412
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700413 // this will make sure LayerBase::doTransaction doesn't update
414 // the drawing state's size
415 Layer::State& editDraw(mDrawingState);
416 editDraw.requested_w = temp.requested_w;
417 editDraw.requested_h = temp.requested_h;
418
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700419 // record the new size, form this point on, when the client request a
420 // buffer, it'll get the new size.
421 setDrawingSize(temp.requested_w, temp.requested_h);
422
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700423 // all buffers need reallocation
424 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 if (temp.sequence != front.sequence) {
428 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
429 // this surface is now hidden, so it shouldn't hold a freeze lock
430 // (it may never redraw, which is fine if it is hidden)
431 mFreezeLock.clear();
432 }
433 }
434
435 return LayerBase::doTransaction(flags);
436}
437
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700438void Layer::setDrawingSize(uint32_t w, uint32_t h) {
439 Mutex::Autolock _l(mLock);
440 mWidth = w;
441 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442}
443
444// ----------------------------------------------------------------------------
445// pageflip handling...
446// ----------------------------------------------------------------------------
447
448void Layer::lockPageFlip(bool& recomputeVisibleRegions)
449{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700450 ssize_t buf = lcblk->retireAndLock();
451 if (buf < NO_ERROR) {
452 //LOGW("nothing to retire (%s)", strerror(-buf));
453 // NOTE: here the buffer is locked because we will used
454 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 return;
456 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700457
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700458 // we retired a buffer, which becomes the new front buffer
459 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700461 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700462 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700463 const Region dirty(lcblk->getDirtyRegion(buf));
464 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700465
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700466 const Layer::State& front(drawingState());
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700467 if (newFrontBuffer->getWidth() == front.requested_w &&
468 newFrontBuffer->getHeight() == front.requested_h)
469 {
470 if ((front.w != front.requested_w) ||
471 (front.h != front.requested_h))
472 {
473 // Here we pretend the transaction happened by updating the
474 // current and drawing states. Drawing state is only accessed
475 // in this thread, no need to have it locked
476 Layer::State& editDraw(mDrawingState);
477 editDraw.w = editDraw.requested_w;
478 editDraw.h = editDraw.requested_h;
479
480 // We also need to update the current state so that we don't
481 // end-up doing too much work during the next transaction.
482 // NOTE: We actually don't need hold the transaction lock here
483 // because State::w and State::h are only accessed from
484 // this thread
485 Layer::State& editTemp(currentState());
486 editTemp.w = editDraw.w;
487 editTemp.h = editDraw.h;
488
489 // recompute visible region
490 recomputeVisibleRegions = true;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700491 }
Mathias Agopian8f2d5052009-10-07 17:58:29 -0700492
493 // we now have the correct size, unfreeze the screen
494 mFreezeLock.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700495 }
496
Mathias Agopiane7005012009-10-07 16:44:10 -0700497 if (lcblk->getQueuedCount()) {
498 // signal an event if we have more buffers waiting
499 mFlinger->signalEvent();
500 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501
Mathias Agopian3330b202009-10-05 17:07:12 -0700502 if (!mPostedDirtyRegion.isEmpty()) {
503 reloadTexture( mPostedDirtyRegion );
504 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505}
506
507void Layer::unlockPageFlip(
508 const Transform& planeTransform, Region& outDirtyRegion)
509{
510 Region dirtyRegion(mPostedDirtyRegion);
511 if (!dirtyRegion.isEmpty()) {
512 mPostedDirtyRegion.clear();
513 // The dirty region is given in the layer's coordinate space
514 // transform the dirty region by the surface's transformation
515 // and the global transformation.
516 const Layer::State& s(drawingState());
517 const Transform tr(planeTransform * s.transform);
518 dirtyRegion = tr.transform(dirtyRegion);
519
520 // At this point, the dirty region is in screen space.
521 // Make sure it's constrained by the visible region (which
522 // is in screen space as well).
523 dirtyRegion.andSelf(visibleRegionScreen);
524 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800526 if (visibleRegionScreen.isEmpty()) {
527 // an invisible layer should not hold a freeze-lock
528 // (because it may never be updated and thereore never release it)
529 mFreezeLock.clear();
530 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531}
532
533void Layer::finishPageFlip()
534{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700535 status_t err = lcblk->unlock( mFrontBufferIndex );
536 LOGE_IF(err!=NO_ERROR,
537 "layer %p, buffer=%d wasn't locked!",
538 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539}
540
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700541// ---------------------------------------------------------------------------
542
Mathias Agopian9a112062009-04-17 19:36:26 -0700543Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
544 SurfaceID id, const sp<Layer>& owner)
545 : Surface(flinger, id, owner->getIdentity(), owner)
546{
547}
548
549Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700550{
551}
552
Mathias Agopian3330b202009-10-05 17:07:12 -0700553sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554{
Mathias Agopian3330b202009-10-05 17:07:12 -0700555 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700556 sp<Layer> owner(getOwner());
557 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700558 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
559 "getBuffer() index (%d) out of range", index);
560 if (uint32_t(index) < NUM_BUFFERS) {
561 buffer = owner->requestBuffer(index, usage);
562 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700563 }
564 return buffer;
565}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566
567// ---------------------------------------------------------------------------
568
569
570}; // namespace android