blob: c1f7f27a14ff0fb261c73298b4cc54e88b41174e [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 Agopian401c2572009-09-23 19:16:27 -070054 mNeedsBlending(true),
55 mNeedsDithering(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056{
57 // no OpenGL operation is possible here, since we might not be
58 // in the OpenGL thread.
Mathias Agopiancbb288b2009-09-07 16:32:45 -070059 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
62Layer::~Layer()
63{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070064 destroy();
65 // the actual buffers will be destroyed here
Mathias Agopian48d819a2009-09-10 19:41:18 -070066}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070067
Mathias Agopian0aa758d2009-04-22 15:23:34 -070068void Layer::destroy()
69{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070070 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070072 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070073 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 }
75 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
76 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
77 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070078 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 }
Mathias Agopian48d819a2009-09-10 19:41:18 -070080 Mutex::Autolock _l(mLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -070081 mBuffers[i].clear();
Mathias Agopian48d819a2009-09-10 19:41:18 -070082 mWidth = mHeight = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070084 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
89 return mSurface;
90}
91
Mathias Agopian9a112062009-04-17 19:36:26 -070092status_t Layer::ditch()
93{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070094 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian8c0a3d72009-09-23 16:44:00 -070095 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -070096 return NO_ERROR;
97}
98
Mathias Agopianf9d93272009-06-19 17:00:27 -070099status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 PixelFormat format, uint32_t flags)
101{
Mathias Agopian401c2572009-09-23 19:16:27 -0700102 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 PixelFormatInfo info;
104 status_t err = getPixelFormatInfo(format, &info);
105 if (err) return err;
106
Mathias Agopian401c2572009-09-23 19:16:27 -0700107 // the display's pixel format
108 const DisplayHardware& hw(graphicPlane(0).displayHardware());
109 PixelFormatInfo displayInfo;
110 getPixelFormatInfo(hw.getFormat(), &displayInfo);
111
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700112 mFormat = format;
113 mWidth = w;
114 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700115 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopian401c2572009-09-23 19:16:27 -0700117
118 // we use the red index
119 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
120 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
121 mNeedsDithering = layerRedsize > displayRedSize;
122
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700123 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700124 mBuffers[i] = new GraphicBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700126 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 return NO_ERROR;
128}
129
130void Layer::reloadTexture(const Region& dirty)
131{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700132 Mutex::Autolock _l(mLock);
Mathias Agopian3330b202009-10-05 17:07:12 -0700133 sp<GraphicBuffer> buffer(getFrontBuffer());
134 if (LIKELY((mFlags & DisplayHardware::DIRECT_TEXTURE) &&
135 (buffer->usage & GRALLOC_USAGE_HW_TEXTURE))) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700136 int index = mFrontBufferIndex;
137 if (LIKELY(!mTextures[index].dirty)) {
138 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
139 } else {
140 // we need to recreate the texture
141 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
142
143 // create the new texture name if needed
144 if (UNLIKELY(mTextures[index].name == -1U)) {
145 mTextures[index].name = createTexture();
146 } else {
147 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
148 }
149
150 // free the previous image
151 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
152 eglDestroyImageKHR(dpy, mTextures[index].image);
153 mTextures[index].image = EGL_NO_IMAGE_KHR;
154 }
155
156 // construct an EGL_NATIVE_BUFFER_ANDROID
157 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
158
159 // create the new EGLImageKHR
160 const EGLint attrs[] = {
161 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
162 EGL_NONE, EGL_NONE
163 };
164 mTextures[index].image = eglCreateImageKHR(
165 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
166 (EGLClientBuffer)clientBuf, attrs);
167
168 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
169 "eglCreateImageKHR() failed. err=0x%4x",
170 eglGetError());
171
172 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
173 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
174 (GLeglImageOES)mTextures[index].image);
175 GLint error = glGetError();
176 if (UNLIKELY(error != GL_NO_ERROR)) {
177 // this failed, for instance, because we don't support
178 // NPOT.
179 // FIXME: do something!
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700180 LOGD("layer=%p, glEGLImageTargetTexture2DOES(%p) "
Mathias Agopian816d7d02009-09-14 18:10:30 -0700181 "failed err=0x%04x",
182 this, mTextures[index].image, error);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
184 } else {
185 // Everything went okay!
Mathias Agopian3330b202009-10-05 17:07:12 -0700186 mTextures[index].NPOTAdjust = false;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700187 mTextures[index].dirty = false;
188 mTextures[index].width = clientBuf->width;
189 mTextures[index].height = clientBuf->height;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700190 }
191 }
192 }
193 } else {
Mathias Agopian3330b202009-10-05 17:07:12 -0700194 for (int i=0 ; i<NUM_BUFFERS ; i++)
195 mTextures[i].image = EGL_NO_IMAGE_KHR;
196
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700198 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700199 LOGE_IF(res, "error %d (%s) locking buffer %p",
200 res, strerror(res), buffer.get());
Mathias Agopian3330b202009-10-05 17:07:12 -0700201
Mathias Agopian0926f502009-05-04 14:17:04 -0700202 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203 if (UNLIKELY(mTextures[0].name == -1U)) {
204 mTextures[0].name = createTexture();
Mathias Agopian3330b202009-10-05 17:07:12 -0700205 mTextures[0].width = 0;
206 mTextures[0].height = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700208 loadTexture(&mTextures[0], dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700209 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
214
215void Layer::onDraw(const Region& clip) const
216{
Mathias Agopian3330b202009-10-05 17:07:12 -0700217 int index = mFrontBufferIndex;
218 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
219 index = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 // the texture has not been created yet, this Layer has
223 // in fact never been drawn into. this happens frequently with
224 // SurfaceView.
225 clearWithOpenGL(clip);
226 return;
227 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700228 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229}
230
Mathias Agopian3330b202009-10-05 17:07:12 -0700231sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232{
Mathias Agopian3330b202009-10-05 17:07:12 -0700233 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700234
235 // this ensures our client doesn't go away while we're accessing
236 // the shared area.
237 sp<Client> ourClient(client.promote());
238 if (ourClient == 0) {
239 // oops, the client is already gone
240 return buffer;
241 }
242
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700244 * This is called from the client's Surface::dequeue(). This can happen
245 * at any time, especially while we're in the middle of using the
246 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700247 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700248 * Make sure the buffer we're resizing is not the front buffer and has been
249 * dequeued. Once this condition is asserted, we are guaranteed that this
250 * buffer cannot become the front buffer under our feet, since we're called
251 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700253 status_t err = lcblk->assertReallocate(index);
254 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700255 if (err != NO_ERROR) {
256 // the surface may have died
257 return buffer;
258 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259
Mathias Agopian48d819a2009-09-10 19:41:18 -0700260 uint32_t w, h;
261 { // scope for the lock
262 Mutex::Autolock _l(mLock);
263 w = mWidth;
264 h = mHeight;
265 buffer = mBuffers[index];
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700266
267 // destroy() could have been called before we get here, we log it
268 // because it's uncommon, and the code below should handle it
269 LOGW_IF(buffer==0,
270 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
271 index, w, h);
272
Mathias Agopian48d819a2009-09-10 19:41:18 -0700273 mBuffers[index].clear();
274 }
275
Mathias Agopian3330b202009-10-05 17:07:12 -0700276 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700277 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700278 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280 // here we have to reallocate a new buffer because we could have a
281 // client in our process with a reference to it (eg: status bar),
282 // and we can't release the handle under its feet.
283 buffer.clear();
Mathias Agopian3330b202009-10-05 17:07:12 -0700284 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700285 err = buffer->initCheck();
286 }
287
288 if (err || buffer->handle == 0) {
289 LOGE_IF(err || buffer->handle == 0,
290 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
291 this, index, w, h, strerror(-err));
292 } else {
293 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700294 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
295 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700296 }
297
298 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700299 Mutex::Autolock _l(mLock);
300 if (mWidth && mHeight) {
301 // and we have new buffer
302 mBuffers[index] = buffer;
303 // texture is now dirty...
304 mTextures[index].dirty = true;
305 } else {
306 // oops we got killed while we were allocating the buffer
307 buffer.clear();
308 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700309 }
310 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311}
312
Mathias Agopian3330b202009-10-05 17:07:12 -0700313uint32_t Layer::getEffectiveUsage(uint32_t usage) const
314{
315 /*
316 * buffers used for software rendering, but h/w composition
317 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
318 *
319 * buffers used for h/w rendering and h/w composition
320 * are allocated with HW_RENDER | HW_TEXTURE
321 *
322 * buffers used with h/w rendering and either NPOT or no egl_image_ext
323 * are allocated with SW_READ_RARELY | HW_RENDER
324 *
325 */
326
327 if (mSecure) {
328 // secure buffer, don't store it into the GPU
329 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
330 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
331 } else {
332 // it's allowed to modify the usage flags here, but generally
333 // the requested flags should be honored.
334 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
335 }
336 return usage;
337}
338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339uint32_t Layer::doTransaction(uint32_t flags)
340{
341 const Layer::State& front(drawingState());
342 const Layer::State& temp(currentState());
343
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700344 if ((front.requested_w != temp.requested_w) ||
345 (front.requested_h != temp.requested_h)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700346 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700348 "resize (layer=%p), requested (%dx%d), "
349 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700350 this,
351 int(temp.requested_w), int(temp.requested_h),
352 int(front.requested_w), int(front.requested_h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700353 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
354 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700356 // we're being resized and there is a freeze display request,
357 // acquire a freeze lock, so that the screen stays put
358 // until we've redrawn at the new size; this is to avoid
359 // glitches upon orientation changes.
360 if (mFlinger->hasFreezeRequest()) {
361 // if the surface is hidden, don't try to acquire the
362 // freeze lock, since hidden surfaces may never redraw
363 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
364 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365 }
366 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700367
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700368 // this will make sure LayerBase::doTransaction doesn't update
369 // the drawing state's size
370 Layer::State& editDraw(mDrawingState);
371 editDraw.requested_w = temp.requested_w;
372 editDraw.requested_h = temp.requested_h;
373
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700374 // record the new size, form this point on, when the client request a
375 // buffer, it'll get the new size.
376 setDrawingSize(temp.requested_w, temp.requested_h);
377
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700378 // all buffers need reallocation
379 lcblk->reallocate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 if (temp.sequence != front.sequence) {
383 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
384 // this surface is now hidden, so it shouldn't hold a freeze lock
385 // (it may never redraw, which is fine if it is hidden)
386 mFreezeLock.clear();
387 }
388 }
389
390 return LayerBase::doTransaction(flags);
391}
392
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700393void Layer::setDrawingSize(uint32_t w, uint32_t h) {
394 Mutex::Autolock _l(mLock);
395 mWidth = w;
396 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397}
398
399// ----------------------------------------------------------------------------
400// pageflip handling...
401// ----------------------------------------------------------------------------
402
403void Layer::lockPageFlip(bool& recomputeVisibleRegions)
404{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700405 ssize_t buf = lcblk->retireAndLock();
406 if (buf < NO_ERROR) {
407 //LOGW("nothing to retire (%s)", strerror(-buf));
408 // NOTE: here the buffer is locked because we will used
409 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 return;
411 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700412
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700413 // we retired a buffer, which becomes the new front buffer
414 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700416 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700417 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700418 const Region dirty(lcblk->getDirtyRegion(buf));
419 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700420
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700421 const Layer::State& front(drawingState());
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700422 if (newFrontBuffer->getWidth() == front.requested_w &&
423 newFrontBuffer->getHeight() == front.requested_h)
424 {
425 if ((front.w != front.requested_w) ||
426 (front.h != front.requested_h))
427 {
428 // Here we pretend the transaction happened by updating the
429 // current and drawing states. Drawing state is only accessed
430 // in this thread, no need to have it locked
431 Layer::State& editDraw(mDrawingState);
432 editDraw.w = editDraw.requested_w;
433 editDraw.h = editDraw.requested_h;
434
435 // We also need to update the current state so that we don't
436 // end-up doing too much work during the next transaction.
437 // NOTE: We actually don't need hold the transaction lock here
438 // because State::w and State::h are only accessed from
439 // this thread
440 Layer::State& editTemp(currentState());
441 editTemp.w = editDraw.w;
442 editTemp.h = editDraw.h;
443
444 // recompute visible region
445 recomputeVisibleRegions = true;
446
447 // we now have the correct size, unfreeze the screen
448 mFreezeLock.clear();
449 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700450 }
451
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700452 // FIXME: signal an event if we have more buffers waiting
453 // mFlinger->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454
Mathias Agopian3330b202009-10-05 17:07:12 -0700455 if (!mPostedDirtyRegion.isEmpty()) {
456 reloadTexture( mPostedDirtyRegion );
457 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458}
459
460void Layer::unlockPageFlip(
461 const Transform& planeTransform, Region& outDirtyRegion)
462{
463 Region dirtyRegion(mPostedDirtyRegion);
464 if (!dirtyRegion.isEmpty()) {
465 mPostedDirtyRegion.clear();
466 // The dirty region is given in the layer's coordinate space
467 // transform the dirty region by the surface's transformation
468 // and the global transformation.
469 const Layer::State& s(drawingState());
470 const Transform tr(planeTransform * s.transform);
471 dirtyRegion = tr.transform(dirtyRegion);
472
473 // At this point, the dirty region is in screen space.
474 // Make sure it's constrained by the visible region (which
475 // is in screen space as well).
476 dirtyRegion.andSelf(visibleRegionScreen);
477 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 }
479}
480
481void Layer::finishPageFlip()
482{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700483 status_t err = lcblk->unlock( mFrontBufferIndex );
484 LOGE_IF(err!=NO_ERROR,
485 "layer %p, buffer=%d wasn't locked!",
486 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487}
488
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700489// ---------------------------------------------------------------------------
490
Mathias Agopian9a112062009-04-17 19:36:26 -0700491Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
492 SurfaceID id, const sp<Layer>& owner)
493 : Surface(flinger, id, owner->getIdentity(), owner)
494{
495}
496
497Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700498{
499}
500
Mathias Agopian3330b202009-10-05 17:07:12 -0700501sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700502{
Mathias Agopian3330b202009-10-05 17:07:12 -0700503 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700504 sp<Layer> owner(getOwner());
505 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700506 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
507 "getBuffer() index (%d) out of range", index);
508 if (uint32_t(index) < NUM_BUFFERS) {
509 buffer = owner->requestBuffer(index, usage);
510 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511 }
512 return buffer;
513}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514
515// ---------------------------------------------------------------------------
516
517
518}; // namespace android