blob: 218cb39f5be9b6bc5b192fa14f0672b53820c1f4 [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
28#include <ui/PixelFormat.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <ui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include "clz.h"
32#include "Layer.h"
33#include "LayerBitmap.h"
34#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 Agopianf9d93272009-06-19 17:00:27 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display, const sp<Client>& c, int32_t i)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 : LayerBaseClient(flinger, display, c, i),
52 mSecure(false),
53 mFrontBufferIndex(1),
54 mNeedsBlending(true),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055 mResizeTransactionDone(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.
59}
60
61Layer::~Layer()
62{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070063 destroy();
64 // the actual buffers will be destroyed here
65}
66
67void Layer::destroy()
68{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 for (int i=0 ; i<NUM_BUFFERS ; i++) {
70 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070071 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070072 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 }
74 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
75 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
76 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070077 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 }
80}
81
82void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags)
83{
84 LayerBase::initStates(w,h,flags);
85
86 if (flags & ISurfaceComposer::eDestroyBackbuffer)
87 lcblk->flags |= eNoCopyBack;
88}
89
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091{
92 return mSurface;
93}
94
Mathias Agopian9a112062009-04-17 19:36:26 -070095status_t Layer::ditch()
96{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070097 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian9a112062009-04-17 19:36:26 -070098 mSurface.clear();
Mathias Agopian0aa758d2009-04-22 15:23:34 -070099 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -0700100 return NO_ERROR;
101}
102
Mathias Agopianf9d93272009-06-19 17:00:27 -0700103status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 PixelFormat format, uint32_t flags)
105{
106 PixelFormatInfo info;
107 status_t err = getPixelFormatInfo(format, &info);
108 if (err) return err;
109
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 uint32_t bufferFlags = 0;
111 if (flags & ISurfaceComposer::eGPU)
112 bufferFlags |= Buffer::GPU;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114 if (flags & ISurfaceComposer::eSecure)
115 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700117 /* FIXME we need this code for msm7201A
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 if (bufferFlags & Buffer::GPU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 // FIXME: this is msm7201A specific, as its GPU only supports
120 // BGRA_8888.
121 if (format == PIXEL_FORMAT_RGBA_8888) {
122 format = PIXEL_FORMAT_BGRA_8888;
123 }
124 }
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700125 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700127 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
131 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700135 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 return NO_ERROR;
137}
138
139void Layer::reloadTexture(const Region& dirty)
140{
Mathias Agopian0926f502009-05-04 14:17:04 -0700141 const sp<Buffer>& buffer(frontBuffer().getBuffer());
142 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143 int index = mFrontBufferIndex;
144 if (LIKELY(!mTextures[index].dirty)) {
145 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
146 } else {
147 // we need to recreate the texture
148 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
149
150 // create the new texture name if needed
151 if (UNLIKELY(mTextures[index].name == -1U)) {
152 mTextures[index].name = createTexture();
153 } else {
154 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
155 }
156
157 // free the previous image
158 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
159 eglDestroyImageKHR(dpy, mTextures[index].image);
160 mTextures[index].image = EGL_NO_IMAGE_KHR;
161 }
162
163 // construct an EGL_NATIVE_BUFFER_ANDROID
164 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
165
166 // create the new EGLImageKHR
167 const EGLint attrs[] = {
168 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
169 EGL_NONE, EGL_NONE
170 };
171 mTextures[index].image = eglCreateImageKHR(
172 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
173 (EGLClientBuffer)clientBuf, attrs);
174
175 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
176 "eglCreateImageKHR() failed. err=0x%4x",
177 eglGetError());
178
179 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
180 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
181 (GLeglImageOES)mTextures[index].image);
182 GLint error = glGetError();
183 if (UNLIKELY(error != GL_NO_ERROR)) {
184 // this failed, for instance, because we don't support
185 // NPOT.
186 // FIXME: do something!
187 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
188 } else {
189 // Everything went okay!
190 mTextures[index].dirty = false;
191 }
192 }
193 }
194 } else {
195 GGLSurface t;
Mathias Agopian0926f502009-05-04 14:17:04 -0700196 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY);
197 LOGE_IF(res, "error %d (%s) locking buffer %p",
198 res, strerror(res), buffer.get());
199 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 if (UNLIKELY(mTextures[0].name == -1U)) {
201 mTextures[0].name = createTexture();
202 }
203 loadTexture(dirty, mTextures[0].name, t,
204 mTextures[0].width, mTextures[0].height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700205 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
210
211void Layer::onDraw(const Region& clip) const
212{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
214 mFrontBufferIndex : 0;
215 GLuint textureName = mTextures[index].name;
216
217 if (UNLIKELY(textureName == -1LU)) {
218 LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 // the texture has not been created yet, this Layer has
220 // in fact never been drawn into. this happens frequently with
221 // SurfaceView.
222 clearWithOpenGL(clip);
223 return;
224 }
225
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700226 GGLSurface t;
227 sp<const Buffer> buffer(frontBuffer().getBuffer());
Mathias Agopian0926f502009-05-04 14:17:04 -0700228 drawWithOpenGL(clip, textureName, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229}
230
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700231sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700233 /*
234 * This is called from the client's Surface::lock(), after it locked
235 * the surface successfully. We're therefore guaranteed that the
236 * back-buffer is not in use by ourselves.
237 * Of course, we need to validate all this, which is not trivial.
238 *
239 * FIXME: A resize could happen at any time here. What to do about this?
240 * - resize() form post()
241 * - resize() from doTransaction()
242 *
243 * We'll probably need an internal lock for this.
244 *
245 *
246 * TODO: We need to make sure that post() doesn't swap
247 * the buffers under us.
248 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 // it's okay to read swapState for the purpose of figuring out the
251 // backbuffer index, which cannot change (since the app has locked it).
252 const uint32_t state = lcblk->swapState;
253 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
254
255 // get rid of the EGL image, since we shouldn't need it anymore
256 // (note that we're in a different thread than where it is being used)
257 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
258 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
259 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
260 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700262
263 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
264 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
265
266 LOGD_IF(DEBUG_RESIZE,
267 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
268 this, backBufferIndex,
269 layerBitmap.getWidth(),
270 layerBitmap.getHeight(),
271 layerBitmap.getBuffer()->getWidth(),
272 layerBitmap.getBuffer()->getHeight());
273
274 if (UNLIKELY(buffer == 0)) {
275 // XXX: what to do, what to do?
276 } else {
277 // texture is now dirty...
278 mTextures[backBufferIndex].dirty = true;
279 // ... so it the visible region (because we consider the surface's
280 // buffer size for visibility calculations)
281 forceVisibilityTransaction();
282 mFlinger->setTransactionFlags(eTraversalNeeded);
283 }
284 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285}
286
Mathias Agopianf9d93272009-06-19 17:00:27 -0700287void Layer::scheduleBroadcast()
288{
289 sp<Client> ourClient(client.promote());
290 if (ourClient != 0) {
291 mFlinger->scheduleBroadcast(ourClient);
292 }
293}
294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295uint32_t Layer::doTransaction(uint32_t flags)
296{
297 const Layer::State& front(drawingState());
298 const Layer::State& temp(currentState());
299
300 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
301 // that the size changed back to its previous value before the buffer
302 // was resized (in the eLocked case below), in which case, we still
303 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700304 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
306 /*
307 * Various states we could be in...
308
309 resize = state & eResizeRequested;
310 if (backbufferChanged) {
311 if (resize == 0) {
312 // ERROR, the resized buffer doesn't have its resize flag set
313 } else if (resize == mask) {
314 // ERROR one of the buffer has already been resized
315 } else if (resize == mask ^ eResizeRequested) {
316 // ERROR, the resized buffer doesn't have its resize flag set
317 } else if (resize == eResizeRequested) {
318 // OK, Normal case, proceed with resize
319 }
320 } else {
321 if (resize == 0) {
322 // OK, nothing special, do nothing
323 } else if (resize == mask) {
324 // restarted transaction, do nothing
325 } else if (resize == mask ^ eResizeRequested) {
326 // restarted transaction, do nothing
327 } else if (resize == eResizeRequested) {
328 // OK, size reset to previous value, proceed with resize
329 }
330 }
331 */
332
333 // Index of the back buffer
334 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
335 const uint32_t state = lcblk->swapState;
336 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
337 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
338 uint32_t resizeFlags = state & eResizeRequested;
339
340 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
341 LOGE( "backbuffer size changed, but both resize flags are not set! "
342 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
343 "index=%d, (%dx%d), (%dx%d)",
344 this, state,
345 int(temp.w), int(temp.h),
346 int(drawingState().w), int(drawingState().h),
347 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700348 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
349 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 // if we get there we're pretty screwed. the only reasonable
351 // thing to do is to pretend we should do the resize since
352 // backbufferChanged is set (this also will give a chance to
353 // client to get unblocked)
354 resizeFlags = eResizeRequested;
355 }
356
357 if (resizeFlags == eResizeRequested) {
358 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
359 // here, would be wrong and misleading because by this point
360 // mFrontBufferIndex has not been updated yet.
361
362 LOGD_IF(DEBUG_RESIZE,
363 "resize (layer=%p), state=%08x, "
364 "requested (%dx%d), "
365 "drawing (%d,%d), "
366 "index=%d, (%dx%d), (%dx%d)",
367 this, state,
368 int(temp.w), int(temp.h),
369 int(drawingState().w), int(drawingState().h),
370 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700371 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
372 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373
374 if (state & eLocked) {
375 // if the buffer is locked, we can't resize anything because
376 // - the backbuffer is currently in use by the user
377 // - the front buffer is being shown
378 // We just act as if the transaction didn't happen and we
379 // reschedule it later...
380 flags |= eRestartTransaction;
381 } else {
382 // This buffer needs to be resized
383 status_t err =
384 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
385 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700386 const uint32_t mask = clientBackBufferIndex ?
387 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 android_atomic_and(~mask, &(lcblk->swapState));
389 // since a buffer became available, we can let the client go...
Mathias Agopianf9d93272009-06-19 17:00:27 -0700390 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 mResizeTransactionDone = true;
392
393 // we're being resized and there is a freeze display request,
394 // acquire a freeze lock, so that the screen stays put
395 // until we've redrawn at the new size; this is to avoid
396 // glitches upon orientation changes.
397 if (mFlinger->hasFreezeRequest()) {
398 // if the surface is hidden, don't try to acquire the
399 // freeze lock, since hidden surfaces may never redraw
400 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
401 mFreezeLock = mFlinger->getFreezeLock();
402 }
403 }
404 }
405 }
406 }
407
408 if (temp.sequence != front.sequence) {
409 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
410 // this surface is now hidden, so it shouldn't hold a freeze lock
411 // (it may never redraw, which is fine if it is hidden)
412 mFreezeLock.clear();
413 }
414 }
415
416 return LayerBase::doTransaction(flags);
417}
418
419status_t Layer::resize(
420 int32_t clientBackBufferIndex,
421 uint32_t width, uint32_t height,
422 const char* what)
423{
424 /*
425 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700426 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 */
428
429 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
430
431 // if the new (transaction) size is != from the the backbuffer
432 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
434 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435
436 LOGD_IF(!backbufferChanged,
437 "(%s) eResizeRequested (layer=%p), but size not changed: "
438 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
439 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
440 what, this,
441 int(width), int(height),
442 int(drawingState().w), int(drawingState().h),
443 int(currentState().w), int(currentState().h),
444 long(lcblk->swapState),
445 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700446 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
447 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448
449 // this can happen when changing the size back and forth quickly
450 status_t err = NO_ERROR;
451 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700452
453 LOGD_IF(DEBUG_RESIZE,
454 "resize (layer=%p), requested (%dx%d), "
455 "index=%d, (%dx%d), (%dx%d)",
456 this, int(width), int(height), int(clientBackBufferIndex),
457 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
458 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
459
460 err = mBuffers[clientBackBufferIndex].setSize(width, height);
461 if (UNLIKELY(err != NO_ERROR)) {
462 // This really should never happen
463 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
464 clientBackBufferIndex, width, height, err, strerror(err));
465 // couldn't reallocate the surface
466 android_atomic_write(eInvalidSurface, &lcblk->swapState);
467 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468 }
469 return err;
470}
471
472void Layer::setSizeChanged(uint32_t w, uint32_t h)
473{
474 LOGD_IF(DEBUG_RESIZE,
475 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
476 w, h, mCurrentState.w, mCurrentState.h);
477 android_atomic_or(eResizeRequested, &(lcblk->swapState));
478}
479
480// ----------------------------------------------------------------------------
481// pageflip handling...
482// ----------------------------------------------------------------------------
483
484void Layer::lockPageFlip(bool& recomputeVisibleRegions)
485{
486 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
487 // preemptively block the client, because he might set
488 // eFlipRequested at any time and want to use this buffer
489 // for the next frame. This will be unset below if it
490 // turns out we didn't need it.
491
492 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
493 if (!(state & mask))
494 return;
495
496 if (UNLIKELY(state & eInvalidSurface)) {
497 // if eInvalidSurface is set, this means the surface
498 // became invalid during a transaction (NO_MEMORY for instance)
Mathias Agopianf9d93272009-06-19 17:00:27 -0700499 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 return;
501 }
502
503 if (UNLIKELY(state & eFlipRequested)) {
504 uint32_t oldState;
505 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
506 if (oldState & eNextFlipPending) {
507 // Process another round (we know at least a buffer
508 // is ready for that client).
509 mFlinger->signalEvent();
510 }
511 }
512}
513
514Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
515{
516 // atomically swap buffers and (re)set eFlipRequested
517 int32_t oldValue, newValue;
518 layer_cblk_t * const lcblk = this->lcblk;
519 do {
520 oldValue = lcblk->swapState;
521 // get the current value
522
523 LOG_ASSERT(oldValue&eFlipRequested,
524 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
525 long(oldValue));
526
527 newValue = (oldValue ^ eIndex);
528 // swap buffers
529
530 newValue &= ~(eFlipRequested | eNextFlipPending);
531 // clear eFlipRequested and eNextFlipPending
532
533 if (oldValue & eNextFlipPending)
534 newValue |= eFlipRequested;
535 // if eNextFlipPending is set (second buffer already has something
536 // in it) we need to reset eFlipRequested because the client
537 // might never do it
538
539 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
540 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700541
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 const int32_t index = (newValue & eIndex) ^ 1;
543 mFrontBufferIndex = index;
544
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545 /* NOTE: it's safe to set this flag here because this is only touched
546 * from LayerBitmap::allocate(), which by construction cannot happen
547 * while we're in post().
548 */
549 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
550
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 // ... post the new front-buffer
552 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700555 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 // oldValue, newValue, mFrontBufferIndex);
557 //dirty.dump("dirty");
558
559 if (UNLIKELY(oldValue & eResizeRequested)) {
560
561 LOGD_IF(DEBUG_RESIZE,
562 "post (layer=%p), state=%08x, "
563 "index=%d, (%dx%d), (%dx%d)",
564 this, newValue,
565 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700566 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
567 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568
569 // here, we just posted the surface and we have resolved
570 // the front/back buffer indices. The client is blocked, so
571 // it cannot start using the new backbuffer.
572
573 // If the backbuffer was resized in THIS round, we actually cannot
574 // resize the frontbuffer because it has *just* been drawn (and we
575 // would have nothing to draw). In this case we just skip the resize
576 // it'll happen after the next page flip or during the next
577 // transaction.
578
579 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
580 if (mResizeTransactionDone && (newValue & mask)) {
581 // Resize the layer's second buffer only if the transaction
582 // happened. It may not have happened yet if eResizeRequested
583 // was set immediately after the "transactionRequested" test,
584 // in which case the drawing state's size would be wrong.
585 mFreezeLock.clear();
586 const Layer::State& s(drawingState());
587 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
588 do {
589 oldValue = lcblk->swapState;
590 if ((oldValue & eResizeRequested) == eResizeRequested) {
591 // ugh, another resize was requested since we processed
592 // the first buffer, don't free the client, and let
593 // the next transaction handle everything.
594 break;
595 }
596 newValue = oldValue & ~mask;
597 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
598 }
599 mResizeTransactionDone = false;
600 recomputeVisibleRegions = true;
601 this->contentDirty = true;
602 }
603 }
604
605 reloadTexture(dirty);
606
607 return dirty;
608}
609
610Point Layer::getPhysicalSize() const
611{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 sp<const Buffer> front(frontBuffer().getBuffer());
613 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614}
615
616void Layer::unlockPageFlip(
617 const Transform& planeTransform, Region& outDirtyRegion)
618{
619 Region dirtyRegion(mPostedDirtyRegion);
620 if (!dirtyRegion.isEmpty()) {
621 mPostedDirtyRegion.clear();
622 // The dirty region is given in the layer's coordinate space
623 // transform the dirty region by the surface's transformation
624 // and the global transformation.
625 const Layer::State& s(drawingState());
626 const Transform tr(planeTransform * s.transform);
627 dirtyRegion = tr.transform(dirtyRegion);
628
629 // At this point, the dirty region is in screen space.
630 // Make sure it's constrained by the visible region (which
631 // is in screen space as well).
632 dirtyRegion.andSelf(visibleRegionScreen);
633 outDirtyRegion.orSelf(dirtyRegion);
634
635 // client could be blocked, so signal them so they get a
636 // chance to reevaluate their condition.
Mathias Agopianf9d93272009-06-19 17:00:27 -0700637 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 }
639}
640
641void Layer::finishPageFlip()
642{
643 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
644 LOGE_IF(!(lcblk->swapState & eBusy),
645 "layer %p wasn't locked!", this);
646 android_atomic_and(~eBusy, &(lcblk->swapState));
647 }
Mathias Agopianf9d93272009-06-19 17:00:27 -0700648 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649}
650
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700651// ---------------------------------------------------------------------------
652
Mathias Agopian9a112062009-04-17 19:36:26 -0700653Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
654 SurfaceID id, const sp<Layer>& owner)
655 : Surface(flinger, id, owner->getIdentity(), owner)
656{
657}
658
659Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660{
661}
662
663sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
664{
665 sp<SurfaceBuffer> buffer = 0;
666 sp<Layer> owner(getOwner());
667 if (owner != 0) {
668 buffer = owner->peekBuffer();
669 }
670 return buffer;
671}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672
673// ---------------------------------------------------------------------------
674
675
676}; // namespace android