blob: 7fd5434d052300e84033efef137b49195842fae0 [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 Agopian3330b202009-10-05 17:07:12 -0700137 if (LIKELY((mFlags & DisplayHardware::DIRECT_TEXTURE) &&
138 (buffer->usage & GRALLOC_USAGE_HW_TEXTURE))) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 int index = mFrontBufferIndex;
140 if (LIKELY(!mTextures[index].dirty)) {
141 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
142 } else {
143 // we need to recreate the texture
144 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
145
146 // create the new texture name if needed
147 if (UNLIKELY(mTextures[index].name == -1U)) {
148 mTextures[index].name = createTexture();
149 } else {
150 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
151 }
152
153 // free the previous image
154 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
155 eglDestroyImageKHR(dpy, mTextures[index].image);
156 mTextures[index].image = EGL_NO_IMAGE_KHR;
157 }
158
159 // construct an EGL_NATIVE_BUFFER_ANDROID
160 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
161
162 // create the new EGLImageKHR
163 const EGLint attrs[] = {
164 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
165 EGL_NONE, EGL_NONE
166 };
167 mTextures[index].image = eglCreateImageKHR(
168 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
169 (EGLClientBuffer)clientBuf, attrs);
170
171 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
172 "eglCreateImageKHR() failed. err=0x%4x",
173 eglGetError());
174
175 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
176 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
177 (GLeglImageOES)mTextures[index].image);
178 GLint error = glGetError();
179 if (UNLIKELY(error != GL_NO_ERROR)) {
180 // this failed, for instance, because we don't support
181 // NPOT.
182 // FIXME: do something!
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700183 LOGD("layer=%p, glEGLImageTargetTexture2DOES(%p) "
Mathias Agopian816d7d02009-09-14 18:10:30 -0700184 "failed err=0x%04x",
185 this, mTextures[index].image, error);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700186 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
187 } else {
188 // Everything went okay!
Mathias Agopian3330b202009-10-05 17:07:12 -0700189 mTextures[index].NPOTAdjust = false;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700190 mTextures[index].dirty = false;
191 mTextures[index].width = clientBuf->width;
192 mTextures[index].height = clientBuf->height;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 }
194 }
195 }
196 } else {
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700197 for (size_t i=0 ; i<NUM_BUFFERS ; i++)
Mathias Agopian3330b202009-10-05 17:07:12 -0700198 mTextures[i].image = EGL_NO_IMAGE_KHR;
199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700201 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700202 LOGE_IF(res, "error %d (%s) locking buffer %p",
203 res, strerror(res), buffer.get());
Mathias Agopian3330b202009-10-05 17:07:12 -0700204
Mathias Agopian0926f502009-05-04 14:17:04 -0700205 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 if (UNLIKELY(mTextures[0].name == -1U)) {
207 mTextures[0].name = createTexture();
Mathias Agopian3330b202009-10-05 17:07:12 -0700208 mTextures[0].width = 0;
209 mTextures[0].height = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700211 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700212 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
217
218void Layer::onDraw(const Region& clip) const
219{
Mathias Agopian3330b202009-10-05 17:07:12 -0700220 int index = mFrontBufferIndex;
221 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
222 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700224 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 // the texture has not been created yet, this Layer has
226 // in fact never been drawn into. this happens frequently with
227 // SurfaceView.
228 clearWithOpenGL(clip);
229 return;
230 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700231 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232}
233
Mathias Agopian3330b202009-10-05 17:07:12 -0700234sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235{
Mathias Agopian3330b202009-10-05 17:07:12 -0700236 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700237
238 // this ensures our client doesn't go away while we're accessing
239 // the shared area.
240 sp<Client> ourClient(client.promote());
241 if (ourClient == 0) {
242 // oops, the client is already gone
243 return buffer;
244 }
245
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700247 * This is called from the client's Surface::dequeue(). This can happen
248 * at any time, especially while we're in the middle of using the
249 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700251 * Make sure the buffer we're resizing is not the front buffer and has been
252 * dequeued. Once this condition is asserted, we are guaranteed that this
253 * buffer cannot become the front buffer under our feet, since we're called
254 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700255 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700256 status_t err = lcblk->assertReallocate(index);
257 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700258 if (err != NO_ERROR) {
259 // the surface may have died
260 return buffer;
261 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262
Mathias Agopian48d819a2009-09-10 19:41:18 -0700263 uint32_t w, h;
264 { // scope for the lock
265 Mutex::Autolock _l(mLock);
266 w = mWidth;
267 h = mHeight;
268 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700269
270 // destroy() could have been called before we get here, we log it
271 // because it's uncommon, and the code below should handle it
272 LOGW_IF(buffer==0,
273 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
274 index, w, h);
275
Mathias Agopian48d819a2009-09-10 19:41:18 -0700276 mBuffers[index].clear();
277 }
278
Mathias Agopian3330b202009-10-05 17:07:12 -0700279 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700280 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700281 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700282 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700283 // here we have to reallocate a new buffer because we could have a
284 // client in our process with a reference to it (eg: status bar),
285 // and we can't release the handle under its feet.
286 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700287 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700288 err = buffer->initCheck();
289 }
290
291 if (err || buffer->handle == 0) {
292 LOGE_IF(err || buffer->handle == 0,
293 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
294 this, index, w, h, strerror(-err));
295 } else {
296 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700297 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
298 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700299 }
300
301 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700302 Mutex::Autolock _l(mLock);
303 if (mWidth && mHeight) {
304 // and we have new buffer
305 mBuffers[index] = buffer;
306 // texture is now dirty...
307 mTextures[index].dirty = true;
308 } else {
309 // oops we got killed while we were allocating the buffer
310 buffer.clear();
311 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312 }
313 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314}
315
Mathias Agopian3330b202009-10-05 17:07:12 -0700316uint32_t Layer::getEffectiveUsage(uint32_t usage) const
317{
318 /*
319 * buffers used for software rendering, but h/w composition
320 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
321 *
322 * buffers used for h/w rendering and h/w composition
323 * are allocated with HW_RENDER | HW_TEXTURE
324 *
325 * buffers used with h/w rendering and either NPOT or no egl_image_ext
326 * are allocated with SW_READ_RARELY | HW_RENDER
327 *
328 */
329
330 if (mSecure) {
331 // secure buffer, don't store it into the GPU
332 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
333 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
334 } else {
335 // it's allowed to modify the usage flags here, but generally
336 // the requested flags should be honored.
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700337 if (mNoEGLImageForSwBuffers) {
338 if (usage & GraphicBuffer::USAGE_HW_MASK) {
339 // request EGLImage for h/w buffers only
340 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
341 }
342 } else {
343 // request EGLImage for all buffers
344 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
345 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700346 }
347 return usage;
348}
349
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350uint32_t Layer::doTransaction(uint32_t flags)
351{
352 const Layer::State& front(drawingState());
353 const Layer::State& temp(currentState());
354
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700355 if ((front.requested_w != temp.requested_w) ||
356 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700357 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700359 "resize (layer=%p), requested (%dx%d), "
360 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700361 this,
362 int(temp.requested_w), int(temp.requested_h),
363 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700364 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
365 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700367 // we're being resized and there is a freeze display request,
368 // acquire a freeze lock, so that the screen stays put
369 // until we've redrawn at the new size; this is to avoid
370 // glitches upon orientation changes.
371 if (mFlinger->hasFreezeRequest()) {
372 // if the surface is hidden, don't try to acquire the
373 // freeze lock, since hidden surfaces may never redraw
374 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
375 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 }
377 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700378
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700379 // this will make sure LayerBase::doTransaction doesn't update
380 // the drawing state's size
381 Layer::State& editDraw(mDrawingState);
382 editDraw.requested_w = temp.requested_w;
383 editDraw.requested_h = temp.requested_h;
384
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700385 // record the new size, form this point on, when the client request a
386 // buffer, it'll get the new size.
387 setDrawingSize(temp.requested_w, temp.requested_h);
388
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700389 // all buffers need reallocation
390 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700392
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 if (temp.sequence != front.sequence) {
394 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
395 // this surface is now hidden, so it shouldn't hold a freeze lock
396 // (it may never redraw, which is fine if it is hidden)
397 mFreezeLock.clear();
398 }
399 }
400
401 return LayerBase::doTransaction(flags);
402}
403
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700404void Layer::setDrawingSize(uint32_t w, uint32_t h) {
405 Mutex::Autolock _l(mLock);
406 mWidth = w;
407 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
410// ----------------------------------------------------------------------------
411// pageflip handling...
412// ----------------------------------------------------------------------------
413
414void Layer::lockPageFlip(bool& recomputeVisibleRegions)
415{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700416 ssize_t buf = lcblk->retireAndLock();
417 if (buf < NO_ERROR) {
418 //LOGW("nothing to retire (%s)", strerror(-buf));
419 // NOTE: here the buffer is locked because we will used
420 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 return;
422 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700423
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700424 // we retired a buffer, which becomes the new front buffer
425 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700427 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700428 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700429 const Region dirty(lcblk->getDirtyRegion(buf));
430 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700431
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700432 const Layer::State& front(drawingState());
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700433 if (newFrontBuffer->getWidth() == front.requested_w &&
434 newFrontBuffer->getHeight() == front.requested_h)
435 {
436 if ((front.w != front.requested_w) ||
437 (front.h != front.requested_h))
438 {
439 // Here we pretend the transaction happened by updating the
440 // current and drawing states. Drawing state is only accessed
441 // in this thread, no need to have it locked
442 Layer::State& editDraw(mDrawingState);
443 editDraw.w = editDraw.requested_w;
444 editDraw.h = editDraw.requested_h;
445
446 // We also need to update the current state so that we don't
447 // end-up doing too much work during the next transaction.
448 // NOTE: We actually don't need hold the transaction lock here
449 // because State::w and State::h are only accessed from
450 // this thread
451 Layer::State& editTemp(currentState());
452 editTemp.w = editDraw.w;
453 editTemp.h = editDraw.h;
454
455 // recompute visible region
456 recomputeVisibleRegions = true;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700457 }
Mathias Agopian8f2d5052009-10-07 17:58:29 -0700458
459 // we now have the correct size, unfreeze the screen
460 mFreezeLock.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700461 }
462
Mathias Agopiane7005012009-10-07 16:44:10 -0700463 if (lcblk->getQueuedCount()) {
464 // signal an event if we have more buffers waiting
465 mFlinger->signalEvent();
466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467
Mathias Agopian3330b202009-10-05 17:07:12 -0700468 if (!mPostedDirtyRegion.isEmpty()) {
469 reloadTexture( mPostedDirtyRegion );
470 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
473void Layer::unlockPageFlip(
474 const Transform& planeTransform, Region& outDirtyRegion)
475{
476 Region dirtyRegion(mPostedDirtyRegion);
477 if (!dirtyRegion.isEmpty()) {
478 mPostedDirtyRegion.clear();
479 // The dirty region is given in the layer's coordinate space
480 // transform the dirty region by the surface's transformation
481 // and the global transformation.
482 const Layer::State& s(drawingState());
483 const Transform tr(planeTransform * s.transform);
484 dirtyRegion = tr.transform(dirtyRegion);
485
486 // At this point, the dirty region is in screen space.
487 // Make sure it's constrained by the visible region (which
488 // is in screen space as well).
489 dirtyRegion.andSelf(visibleRegionScreen);
490 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 }
492}
493
494void Layer::finishPageFlip()
495{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700496 status_t err = lcblk->unlock( mFrontBufferIndex );
497 LOGE_IF(err!=NO_ERROR,
498 "layer %p, buffer=%d wasn't locked!",
499 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500}
501
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700502// ---------------------------------------------------------------------------
503
Mathias Agopian9a112062009-04-17 19:36:26 -0700504Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
505 SurfaceID id, const sp<Layer>& owner)
506 : Surface(flinger, id, owner->getIdentity(), owner)
507{
508}
509
510Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511{
512}
513
Mathias Agopian3330b202009-10-05 17:07:12 -0700514sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515{
Mathias Agopian3330b202009-10-05 17:07:12 -0700516 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517 sp<Layer> owner(getOwner());
518 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700519 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
520 "getBuffer() index (%d) out of range", index);
521 if (uint32_t(index) < NUM_BUFFERS) {
522 buffer = owner->requestBuffer(index, usage);
523 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700524 }
525 return buffer;
526}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527
528// ---------------------------------------------------------------------------
529
530
531}; // namespace android