blob: 2bb1e1252ebf1a5abb100e047221a08ab9589f4f [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian6950e422009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070030#include <ui/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32#include "clz.h"
33#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-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 Agopian9779b222009-09-07 16:32:45 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
51 const sp<Client>& c, int32_t i)
Mathias Agopian248b5bd2009-09-10 19:41:18 -070052 : LayerBaseClient(flinger, display, c, i),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 mSecure(false),
Mathias Agopian351a7072009-10-05 18:20:39 -070054 mNoEGLImageForSwBuffers(false),
Mathias Agopiancc934762009-09-23 19:16:27 -070055 mNeedsBlending(true),
56 mNeedsDithering(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057{
58 // no OpenGL operation is possible here, since we might not be
59 // in the OpenGL thread.
Mathias Agopian9779b222009-09-07 16:32:45 -070060 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061}
62
63Layer::~Layer()
64{
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070065 destroy();
66 // the actual buffers will be destroyed here
Mathias Agopian248b5bd2009-09-10 19:41:18 -070067}
Mathias Agopian9779b222009-09-07 16:32:45 -070068
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070069void Layer::destroy()
70{
Mathias Agopian9779b222009-09-07 16:32:45 -070071 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -070072 if (mTextures[i].name != -1U) {
Mathias Agopian81b0aa62009-04-22 15:49:28 -070073 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070074 mTextures[i].name = -1U;
Mathias Agopian1473f462009-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 Agopiana3aa6c92009-04-22 15:23:34 -070079 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian1473f462009-04-10 14:24:30 -070080 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -070081 Mutex::Autolock _l(mLock);
Mathias Agopian9779b222009-09-07 16:32:45 -070082 mBuffers[i].clear();
Mathias Agopian248b5bd2009-09-10 19:41:18 -070083 mWidth = mHeight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -070085 mSurface.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}
87
Mathias Agopian1473f462009-04-10 14:24:30 -070088sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089{
90 return mSurface;
91}
92
Mathias Agopian6cf0db22009-04-17 19:36:26 -070093status_t Layer::ditch()
94{
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070095 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian2e4b68d2009-09-23 16:44:00 -070096 destroy();
Mathias Agopian6cf0db22009-04-17 19:36:26 -070097 return NO_ERROR;
98}
99
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700100status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 PixelFormat format, uint32_t flags)
102{
Mathias Agopiancc934762009-09-23 19:16:27 -0700103 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 PixelFormatInfo info;
105 status_t err = getPixelFormatInfo(format, &info);
106 if (err) return err;
107
Mathias Agopiancc934762009-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 Agopian351a7072009-10-05 18:20:39 -0700112 const uint32_t hwFlags = hw.getFlags();
113
Mathias Agopian9779b222009-09-07 16:32:45 -0700114 mFormat = format;
115 mWidth = w;
116 mHeight = h;
Mathias Agopian6950e422009-10-05 17:07:12 -0700117 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopian351a7072009-10-05 18:20:39 -0700119 mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS);
120
Mathias Agopiancc934762009-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 Agopian9779b222009-09-07 16:32:45 -0700126 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700127 mBuffers[i] = new GraphicBuffer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700129 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 return NO_ERROR;
131}
132
Mathias Agopian382e17d2009-10-21 16:27:21 -0700133status_t Layer::initializeEglImageLocked(
134 const sp<GraphicBuffer>& buffer, Texture* texture)
135{
136 status_t err = NO_ERROR;
137
138 // we need to recreate the texture
139 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
140
141 // free the previous image
142 if (texture->image != EGL_NO_IMAGE_KHR) {
143 eglDestroyImageKHR(dpy, texture->image);
144 texture->image = EGL_NO_IMAGE_KHR;
145 }
146
147 // construct an EGL_NATIVE_BUFFER_ANDROID
148 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
149
150 // create the new EGLImageKHR
151 const EGLint attrs[] = {
152 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
153 EGL_NONE, EGL_NONE
154 };
155 texture->image = eglCreateImageKHR(
156 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
157 (EGLClientBuffer)clientBuf, attrs);
158
159 LOGE_IF(texture->image == EGL_NO_IMAGE_KHR,
160 "eglCreateImageKHR() failed. err=0x%4x",
161 eglGetError());
162
163 if (texture->image != EGL_NO_IMAGE_KHR) {
164 glBindTexture(GL_TEXTURE_2D, texture->name);
165 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
166 (GLeglImageOES)texture->image);
167 GLint error = glGetError();
168 if (UNLIKELY(error != GL_NO_ERROR)) {
169 // this failed, for instance, because we don't support NPOT.
170 // FIXME: do something!
171 LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) "
172 "failed err=0x%04x",
173 this, texture->image, error);
174 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
175 err = INVALID_OPERATION;
176 } else {
177 // Everything went okay!
178 texture->NPOTAdjust = false;
179 texture->dirty = false;
180 texture->width = clientBuf->width;
181 texture->height = clientBuf->height;
182 }
183 } else {
184 err = INVALID_OPERATION;
185 }
186 return err;
187}
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189void Layer::reloadTexture(const Region& dirty)
190{
Mathias Agopian9779b222009-09-07 16:32:45 -0700191 Mutex::Autolock _l(mLock);
Mathias Agopian4961c952009-10-06 19:00:57 -0700192 sp<GraphicBuffer> buffer(getFrontBufferLocked());
Mathias Agopian382e17d2009-10-21 16:27:21 -0700193 int index = mFrontBufferIndex;
194
195 // create the new texture name if needed
196 if (UNLIKELY(mTextures[index].name == -1U)) {
197 mTextures[index].name = createTexture();
198 mTextures[index].width = 0;
199 mTextures[index].height = 0;
200 }
201
202 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
203 if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) {
204 if (mTextures[index].dirty) {
205 initializeEglImageLocked(buffer, &mTextures[index]);
206 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700207 } else {
Mathias Agopian382e17d2009-10-21 16:27:21 -0700208 if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width ||
209 mHybridBuffer->height != buffer->height)) {
210 mHybridBuffer.clear();
211 mHybridBuffer = new GraphicBuffer(
212 buffer->width, buffer->height, buffer->format,
213 GraphicBuffer::USAGE_SW_WRITE_OFTEN |
214 GraphicBuffer::USAGE_HW_TEXTURE);
215 initializeEglImageLocked(
216 mHybridBuffer, &mTextures[0]);
Mathias Agopian1473f462009-04-10 14:24:30 -0700217 }
218
Mathias Agopian382e17d2009-10-21 16:27:21 -0700219 GGLSurface t;
220 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
221 LOGE_IF(res, "error %d (%s) locking buffer %p",
222 res, strerror(res), buffer.get());
223 if (res == NO_ERROR) {
224 Texture* const texture(&mTextures[0]);
Mathias Agopian1473f462009-04-10 14:24:30 -0700225
Mathias Agopian382e17d2009-10-21 16:27:21 -0700226 glBindTexture(GL_TEXTURE_2D, texture->name);
227
228 sp<GraphicBuffer> buf(mHybridBuffer);
229 void* vaddr;
230 res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr);
231 if (res == NO_ERROR) {
232 int bpp = 0;
233 switch (t.format) {
234 case GGL_PIXEL_FORMAT_RGB_565:
235 case GGL_PIXEL_FORMAT_RGBA_4444:
236 bpp = 2;
237 break;
238 case GGL_PIXEL_FORMAT_RGBA_8888:
239 case GGL_PIXEL_FORMAT_RGBX_8888:
240 bpp = 4;
241 break;
242 case GGL_PIXEL_FORMAT_YCbCr_422_SP:
243 case GGL_PIXEL_FORMAT_YCbCr_420_SP:
244 // just show the Y plane of YUV buffers
245 bpp = 1;
246 break;
247 default:
248 // oops, we don't handle this format!
249 LOGE("layer %p, texture=%d, using format %d, which is not "
250 "supported by the GL", this, texture->name, t.format);
251 }
252 if (bpp) {
253 const Rect bounds(dirty.getBounds());
254 size_t src_stride = t.stride;
255 size_t dst_stride = buf->stride;
256 if (src_stride == dst_stride &&
257 bounds.width() == t.width &&
258 bounds.height() == t.height)
259 {
260 memcpy(vaddr, t.data, t.height * t.stride * bpp);
261 } else {
262 GLubyte const * src = t.data +
263 (bounds.left + bounds.top * src_stride) * bpp;
264 GLubyte * dst = (GLubyte *)vaddr +
265 (bounds.left + bounds.top * dst_stride) * bpp;
266 const size_t length = bounds.width() * bpp;
267 size_t h = bounds.height();
268 src_stride *= bpp;
269 dst_stride *= bpp;
270 while (h--) {
271 memcpy(dst, src, length);
272 dst += dst_stride;
273 src += src_stride;
274 }
275 }
276 }
277 buf->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700278 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700279 buffer->unlock();
280 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700281 }
282 } else {
Mathias Agopian382e17d2009-10-21 16:27:21 -0700283 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700284 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian382e17d2009-10-21 16:27:21 -0700285 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700286 GGLSurface t;
Mathias Agopian6950e422009-10-05 17:07:12 -0700287 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700288 LOGE_IF(res, "error %d (%s) locking buffer %p",
289 res, strerror(res), buffer.get());
290 if (res == NO_ERROR) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700291 loadTexture(&mTextures[0], dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700292 buffer->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295}
296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297void Layer::onDraw(const Region& clip) const
298{
Mathias Agopian6950e422009-10-05 17:07:12 -0700299 int index = mFrontBufferIndex;
300 if (mTextures[index].image == EGL_NO_IMAGE_KHR)
301 index = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700302 GLuint textureName = mTextures[index].name;
Mathias Agopian1473f462009-04-10 14:24:30 -0700303 if (UNLIKELY(textureName == -1LU)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 // the texture has not been created yet, this Layer has
305 // in fact never been drawn into. this happens frequently with
306 // SurfaceView.
307 clearWithOpenGL(clip);
308 return;
309 }
Mathias Agopian999543b2009-06-23 18:08:22 -0700310 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311}
312
Mathias Agopian6950e422009-10-05 17:07:12 -0700313sp<GraphicBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314{
Mathias Agopian6950e422009-10-05 17:07:12 -0700315 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700316
317 // this ensures our client doesn't go away while we're accessing
318 // the shared area.
319 sp<Client> ourClient(client.promote());
320 if (ourClient == 0) {
321 // oops, the client is already gone
322 return buffer;
323 }
324
Mathias Agopian1473f462009-04-10 14:24:30 -0700325 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700326 * This is called from the client's Surface::dequeue(). This can happen
327 * at any time, especially while we're in the middle of using the
328 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700329 *
Mathias Agopian9779b222009-09-07 16:32:45 -0700330 * Make sure the buffer we're resizing is not the front buffer and has been
331 * dequeued. Once this condition is asserted, we are guaranteed that this
332 * buffer cannot become the front buffer under our feet, since we're called
333 * from Surface::dequeue()
Mathias Agopian1473f462009-04-10 14:24:30 -0700334 */
Mathias Agopian9779b222009-09-07 16:32:45 -0700335 status_t err = lcblk->assertReallocate(index);
336 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700337 if (err != NO_ERROR) {
338 // the surface may have died
339 return buffer;
340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700342 uint32_t w, h;
343 { // scope for the lock
344 Mutex::Autolock _l(mLock);
345 w = mWidth;
346 h = mHeight;
347 buffer = mBuffers[index];
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700348
349 // destroy() could have been called before we get here, we log it
350 // because it's uncommon, and the code below should handle it
351 LOGW_IF(buffer==0,
352 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)",
353 index, w, h);
354
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700355 mBuffers[index].clear();
356 }
357
Mathias Agopian6950e422009-10-05 17:07:12 -0700358 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700359 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700360 err = buffer->reallocate(w, h, mFormat, effectiveUsage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700361 } else {
Mathias Agopian9779b222009-09-07 16:32:45 -0700362 // here we have to reallocate a new buffer because we could have a
363 // client in our process with a reference to it (eg: status bar),
364 // and we can't release the handle under its feet.
365 buffer.clear();
Mathias Agopian6950e422009-10-05 17:07:12 -0700366 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage);
Mathias Agopian9779b222009-09-07 16:32:45 -0700367 err = buffer->initCheck();
368 }
369
370 if (err || buffer->handle == 0) {
371 LOGE_IF(err || buffer->handle == 0,
372 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
373 this, index, w, h, strerror(-err));
374 } else {
375 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700376 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
377 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700378 }
379
380 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700381 Mutex::Autolock _l(mLock);
382 if (mWidth && mHeight) {
383 // and we have new buffer
384 mBuffers[index] = buffer;
385 // texture is now dirty...
386 mTextures[index].dirty = true;
387 } else {
388 // oops we got killed while we were allocating the buffer
389 buffer.clear();
390 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700391 }
392 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393}
394
Mathias Agopian6950e422009-10-05 17:07:12 -0700395uint32_t Layer::getEffectiveUsage(uint32_t usage) const
396{
397 /*
398 * buffers used for software rendering, but h/w composition
399 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
400 *
401 * buffers used for h/w rendering and h/w composition
402 * are allocated with HW_RENDER | HW_TEXTURE
403 *
404 * buffers used with h/w rendering and either NPOT or no egl_image_ext
405 * are allocated with SW_READ_RARELY | HW_RENDER
406 *
407 */
408
409 if (mSecure) {
410 // secure buffer, don't store it into the GPU
411 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
412 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
413 } else {
414 // it's allowed to modify the usage flags here, but generally
415 // the requested flags should be honored.
Mathias Agopian351a7072009-10-05 18:20:39 -0700416 if (mNoEGLImageForSwBuffers) {
417 if (usage & GraphicBuffer::USAGE_HW_MASK) {
418 // request EGLImage for h/w buffers only
419 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
420 }
421 } else {
422 // request EGLImage for all buffers
423 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
424 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700425 }
426 return usage;
427}
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429uint32_t Layer::doTransaction(uint32_t flags)
430{
431 const Layer::State& front(drawingState());
432 const Layer::State& temp(currentState());
433
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700434 if ((front.requested_w != temp.requested_w) ||
435 (front.requested_h != temp.requested_h)) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700436 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian9779b222009-09-07 16:32:45 -0700438 "resize (layer=%p), requested (%dx%d), "
439 "drawing (%d,%d), (%dx%d), (%dx%d)",
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700440 this,
441 int(temp.requested_w), int(temp.requested_h),
442 int(front.requested_w), int(front.requested_h),
Mathias Agopian9779b222009-09-07 16:32:45 -0700443 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
444 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
Mathias Agopian9779b222009-09-07 16:32:45 -0700446 // we're being resized and there is a freeze display request,
447 // acquire a freeze lock, so that the screen stays put
448 // until we've redrawn at the new size; this is to avoid
449 // glitches upon orientation changes.
450 if (mFlinger->hasFreezeRequest()) {
451 // if the surface is hidden, don't try to acquire the
452 // freeze lock, since hidden surfaces may never redraw
453 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
454 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456 }
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700457
Mathias Agopianbd23e302009-09-30 14:07:22 -0700458 // this will make sure LayerBase::doTransaction doesn't update
459 // the drawing state's size
460 Layer::State& editDraw(mDrawingState);
461 editDraw.requested_w = temp.requested_w;
462 editDraw.requested_h = temp.requested_h;
463
Mathias Agopian70cab912009-09-30 12:48:47 -0700464 // record the new size, form this point on, when the client request a
465 // buffer, it'll get the new size.
466 setDrawingSize(temp.requested_w, temp.requested_h);
467
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700468 // all buffers need reallocation
469 lcblk->reallocate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 if (temp.sequence != front.sequence) {
473 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
474 // this surface is now hidden, so it shouldn't hold a freeze lock
475 // (it may never redraw, which is fine if it is hidden)
476 mFreezeLock.clear();
477 }
478 }
479
480 return LayerBase::doTransaction(flags);
481}
482
Mathias Agopian9779b222009-09-07 16:32:45 -0700483void Layer::setDrawingSize(uint32_t w, uint32_t h) {
484 Mutex::Autolock _l(mLock);
485 mWidth = w;
486 mHeight = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487}
488
489// ----------------------------------------------------------------------------
490// pageflip handling...
491// ----------------------------------------------------------------------------
492
493void Layer::lockPageFlip(bool& recomputeVisibleRegions)
494{
Mathias Agopian9779b222009-09-07 16:32:45 -0700495 ssize_t buf = lcblk->retireAndLock();
496 if (buf < NO_ERROR) {
497 //LOGW("nothing to retire (%s)", strerror(-buf));
498 // NOTE: here the buffer is locked because we will used
499 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 return;
501 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700502
Mathias Agopian9779b222009-09-07 16:32:45 -0700503 // we retired a buffer, which becomes the new front buffer
504 mFrontBufferIndex = buf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
Mathias Agopian9779b222009-09-07 16:32:45 -0700506 // get the dirty region
Mathias Agopian6950e422009-10-05 17:07:12 -0700507 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9779b222009-09-07 16:32:45 -0700508 const Region dirty(lcblk->getDirtyRegion(buf));
509 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700510
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700511 const Layer::State& front(drawingState());
Mathias Agopianbd23e302009-09-30 14:07:22 -0700512 if (newFrontBuffer->getWidth() == front.requested_w &&
513 newFrontBuffer->getHeight() == front.requested_h)
514 {
515 if ((front.w != front.requested_w) ||
516 (front.h != front.requested_h))
517 {
518 // Here we pretend the transaction happened by updating the
519 // current and drawing states. Drawing state is only accessed
520 // in this thread, no need to have it locked
521 Layer::State& editDraw(mDrawingState);
522 editDraw.w = editDraw.requested_w;
523 editDraw.h = editDraw.requested_h;
524
525 // We also need to update the current state so that we don't
526 // end-up doing too much work during the next transaction.
527 // NOTE: We actually don't need hold the transaction lock here
528 // because State::w and State::h are only accessed from
529 // this thread
530 Layer::State& editTemp(currentState());
531 editTemp.w = editDraw.w;
532 editTemp.h = editDraw.h;
533
534 // recompute visible region
535 recomputeVisibleRegions = true;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700536 }
Mathias Agopian46b2df12009-10-07 17:58:29 -0700537
538 // we now have the correct size, unfreeze the screen
539 mFreezeLock.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700540 }
541
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700542 if (lcblk->getQueuedCount()) {
543 // signal an event if we have more buffers waiting
544 mFlinger->signalEvent();
545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546
Mathias Agopian6950e422009-10-05 17:07:12 -0700547 if (!mPostedDirtyRegion.isEmpty()) {
548 reloadTexture( mPostedDirtyRegion );
549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550}
551
552void Layer::unlockPageFlip(
553 const Transform& planeTransform, Region& outDirtyRegion)
554{
555 Region dirtyRegion(mPostedDirtyRegion);
556 if (!dirtyRegion.isEmpty()) {
557 mPostedDirtyRegion.clear();
558 // The dirty region is given in the layer's coordinate space
559 // transform the dirty region by the surface's transformation
560 // and the global transformation.
561 const Layer::State& s(drawingState());
562 const Transform tr(planeTransform * s.transform);
563 dirtyRegion = tr.transform(dirtyRegion);
564
565 // At this point, the dirty region is in screen space.
566 // Make sure it's constrained by the visible region (which
567 // is in screen space as well).
568 dirtyRegion.andSelf(visibleRegionScreen);
569 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571}
572
573void Layer::finishPageFlip()
574{
Mathias Agopian9779b222009-09-07 16:32:45 -0700575 status_t err = lcblk->unlock( mFrontBufferIndex );
576 LOGE_IF(err!=NO_ERROR,
577 "layer %p, buffer=%d wasn't locked!",
578 this, mFrontBufferIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579}
580
Mathias Agopian1473f462009-04-10 14:24:30 -0700581// ---------------------------------------------------------------------------
582
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700583Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
584 SurfaceID id, const sp<Layer>& owner)
585 : Surface(flinger, id, owner->getIdentity(), owner)
586{
587}
588
589Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -0700590{
591}
592
Mathias Agopian6950e422009-10-05 17:07:12 -0700593sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700594{
Mathias Agopian6950e422009-10-05 17:07:12 -0700595 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700596 sp<Layer> owner(getOwner());
597 if (owner != 0) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700598 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
599 "getBuffer() index (%d) out of range", index);
600 if (uint32_t(index) < NUM_BUFFERS) {
601 buffer = owner->requestBuffer(index, usage);
602 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700603 }
604 return buffer;
605}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
607// ---------------------------------------------------------------------------
608
609
610}; // namespace android