blob: c40fec16fbc95ab8d43fba78c57324394209a36f [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!
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 {
197 GGLSurface t;
Mathias Agopian0926f502009-05-04 14:17:04 -0700198 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY);
199 LOGE_IF(res, "error %d (%s) locking buffer %p",
200 res, strerror(res), buffer.get());
201 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 if (UNLIKELY(mTextures[0].name == -1U)) {
203 mTextures[0].name = createTexture();
204 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700205 loadTexture(&mTextures[0], mTextures[0].name, dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700206 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209}
210
211
212void Layer::onDraw(const Region& clip) const
213{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
215 mFrontBufferIndex : 0;
216 GLuint textureName = mTextures[index].name;
217
218 if (UNLIKELY(textureName == -1LU)) {
219 LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 // the texture has not been created yet, this Layer has
221 // in fact never been drawn into. this happens frequently with
222 // SurfaceView.
223 clearWithOpenGL(clip);
224 return;
225 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700226 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227}
228
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700231 /*
232 * This is called from the client's Surface::lock(), after it locked
233 * the surface successfully. We're therefore guaranteed that the
234 * back-buffer is not in use by ourselves.
235 * Of course, we need to validate all this, which is not trivial.
236 *
237 * FIXME: A resize could happen at any time here. What to do about this?
238 * - resize() form post()
239 * - resize() from doTransaction()
240 *
241 * We'll probably need an internal lock for this.
242 *
243 *
244 * TODO: We need to make sure that post() doesn't swap
245 * the buffers under us.
246 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248 // it's okay to read swapState for the purpose of figuring out the
249 // backbuffer index, which cannot change (since the app has locked it).
250 const uint32_t state = lcblk->swapState;
251 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
252
253 // get rid of the EGL image, since we shouldn't need it anymore
254 // (note that we're in a different thread than where it is being used)
255 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
256 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
257 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
258 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260
261 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
262 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
263
264 LOGD_IF(DEBUG_RESIZE,
265 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
266 this, backBufferIndex,
267 layerBitmap.getWidth(),
268 layerBitmap.getHeight(),
269 layerBitmap.getBuffer()->getWidth(),
270 layerBitmap.getBuffer()->getHeight());
271
272 if (UNLIKELY(buffer == 0)) {
273 // XXX: what to do, what to do?
274 } else {
275 // texture is now dirty...
276 mTextures[backBufferIndex].dirty = true;
277 // ... so it the visible region (because we consider the surface's
278 // buffer size for visibility calculations)
279 forceVisibilityTransaction();
280 mFlinger->setTransactionFlags(eTraversalNeeded);
281 }
282 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283}
284
Mathias Agopianf9d93272009-06-19 17:00:27 -0700285void Layer::scheduleBroadcast()
286{
287 sp<Client> ourClient(client.promote());
288 if (ourClient != 0) {
289 mFlinger->scheduleBroadcast(ourClient);
290 }
291}
292
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293uint32_t Layer::doTransaction(uint32_t flags)
294{
295 const Layer::State& front(drawingState());
296 const Layer::State& temp(currentState());
297
298 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
299 // that the size changed back to its previous value before the buffer
300 // was resized (in the eLocked case below), in which case, we still
301 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700302 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
304 /*
305 * Various states we could be in...
306
307 resize = state & eResizeRequested;
308 if (backbufferChanged) {
309 if (resize == 0) {
310 // ERROR, the resized buffer doesn't have its resize flag set
311 } else if (resize == mask) {
312 // ERROR one of the buffer has already been resized
313 } else if (resize == mask ^ eResizeRequested) {
314 // ERROR, the resized buffer doesn't have its resize flag set
315 } else if (resize == eResizeRequested) {
316 // OK, Normal case, proceed with resize
317 }
318 } else {
319 if (resize == 0) {
320 // OK, nothing special, do nothing
321 } else if (resize == mask) {
322 // restarted transaction, do nothing
323 } else if (resize == mask ^ eResizeRequested) {
324 // restarted transaction, do nothing
325 } else if (resize == eResizeRequested) {
326 // OK, size reset to previous value, proceed with resize
327 }
328 }
329 */
330
331 // Index of the back buffer
332 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
333 const uint32_t state = lcblk->swapState;
334 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
335 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
336 uint32_t resizeFlags = state & eResizeRequested;
337
338 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
339 LOGE( "backbuffer size changed, but both resize flags are not set! "
340 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
341 "index=%d, (%dx%d), (%dx%d)",
342 this, state,
343 int(temp.w), int(temp.h),
344 int(drawingState().w), int(drawingState().h),
345 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
347 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 // if we get there we're pretty screwed. the only reasonable
349 // thing to do is to pretend we should do the resize since
350 // backbufferChanged is set (this also will give a chance to
351 // client to get unblocked)
352 resizeFlags = eResizeRequested;
353 }
354
355 if (resizeFlags == eResizeRequested) {
356 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
357 // here, would be wrong and misleading because by this point
358 // mFrontBufferIndex has not been updated yet.
359
360 LOGD_IF(DEBUG_RESIZE,
361 "resize (layer=%p), state=%08x, "
362 "requested (%dx%d), "
363 "drawing (%d,%d), "
364 "index=%d, (%dx%d), (%dx%d)",
365 this, state,
366 int(temp.w), int(temp.h),
367 int(drawingState().w), int(drawingState().h),
368 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700369 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
370 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371
372 if (state & eLocked) {
373 // if the buffer is locked, we can't resize anything because
374 // - the backbuffer is currently in use by the user
375 // - the front buffer is being shown
376 // We just act as if the transaction didn't happen and we
377 // reschedule it later...
378 flags |= eRestartTransaction;
379 } else {
380 // This buffer needs to be resized
381 status_t err =
382 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
383 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700384 const uint32_t mask = clientBackBufferIndex ?
385 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 android_atomic_and(~mask, &(lcblk->swapState));
387 // since a buffer became available, we can let the client go...
Mathias Agopianf9d93272009-06-19 17:00:27 -0700388 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 mResizeTransactionDone = true;
390
391 // we're being resized and there is a freeze display request,
392 // acquire a freeze lock, so that the screen stays put
393 // until we've redrawn at the new size; this is to avoid
394 // glitches upon orientation changes.
395 if (mFlinger->hasFreezeRequest()) {
396 // if the surface is hidden, don't try to acquire the
397 // freeze lock, since hidden surfaces may never redraw
398 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
399 mFreezeLock = mFlinger->getFreezeLock();
400 }
401 }
402 }
403 }
404 }
405
406 if (temp.sequence != front.sequence) {
407 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
408 // this surface is now hidden, so it shouldn't hold a freeze lock
409 // (it may never redraw, which is fine if it is hidden)
410 mFreezeLock.clear();
411 }
412 }
413
414 return LayerBase::doTransaction(flags);
415}
416
417status_t Layer::resize(
418 int32_t clientBackBufferIndex,
419 uint32_t width, uint32_t height,
420 const char* what)
421{
422 /*
423 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700424 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 */
426
427 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
428
429 // if the new (transaction) size is != from the the backbuffer
430 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700431 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
432 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433
434 LOGD_IF(!backbufferChanged,
435 "(%s) eResizeRequested (layer=%p), but size not changed: "
436 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
437 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
438 what, this,
439 int(width), int(height),
440 int(drawingState().w), int(drawingState().h),
441 int(currentState().w), int(currentState().h),
442 long(lcblk->swapState),
443 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700444 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
445 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446
447 // this can happen when changing the size back and forth quickly
448 status_t err = NO_ERROR;
449 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700450
451 LOGD_IF(DEBUG_RESIZE,
452 "resize (layer=%p), requested (%dx%d), "
453 "index=%d, (%dx%d), (%dx%d)",
454 this, int(width), int(height), int(clientBackBufferIndex),
455 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
456 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
457
458 err = mBuffers[clientBackBufferIndex].setSize(width, height);
459 if (UNLIKELY(err != NO_ERROR)) {
460 // This really should never happen
461 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
462 clientBackBufferIndex, width, height, err, strerror(err));
463 // couldn't reallocate the surface
464 android_atomic_write(eInvalidSurface, &lcblk->swapState);
465 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 }
467 return err;
468}
469
470void Layer::setSizeChanged(uint32_t w, uint32_t h)
471{
472 LOGD_IF(DEBUG_RESIZE,
473 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
474 w, h, mCurrentState.w, mCurrentState.h);
475 android_atomic_or(eResizeRequested, &(lcblk->swapState));
476}
477
478// ----------------------------------------------------------------------------
479// pageflip handling...
480// ----------------------------------------------------------------------------
481
482void Layer::lockPageFlip(bool& recomputeVisibleRegions)
483{
484 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
485 // preemptively block the client, because he might set
486 // eFlipRequested at any time and want to use this buffer
487 // for the next frame. This will be unset below if it
488 // turns out we didn't need it.
489
490 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
491 if (!(state & mask))
492 return;
493
494 if (UNLIKELY(state & eInvalidSurface)) {
495 // if eInvalidSurface is set, this means the surface
496 // became invalid during a transaction (NO_MEMORY for instance)
Mathias Agopianf9d93272009-06-19 17:00:27 -0700497 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 return;
499 }
500
501 if (UNLIKELY(state & eFlipRequested)) {
502 uint32_t oldState;
503 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
504 if (oldState & eNextFlipPending) {
505 // Process another round (we know at least a buffer
506 // is ready for that client).
507 mFlinger->signalEvent();
508 }
509 }
510}
511
512Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
513{
514 // atomically swap buffers and (re)set eFlipRequested
515 int32_t oldValue, newValue;
516 layer_cblk_t * const lcblk = this->lcblk;
517 do {
518 oldValue = lcblk->swapState;
519 // get the current value
520
521 LOG_ASSERT(oldValue&eFlipRequested,
522 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
523 long(oldValue));
524
525 newValue = (oldValue ^ eIndex);
526 // swap buffers
527
528 newValue &= ~(eFlipRequested | eNextFlipPending);
529 // clear eFlipRequested and eNextFlipPending
530
531 if (oldValue & eNextFlipPending)
532 newValue |= eFlipRequested;
533 // if eNextFlipPending is set (second buffer already has something
534 // in it) we need to reset eFlipRequested because the client
535 // might never do it
536
537 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
538 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 const int32_t index = (newValue & eIndex) ^ 1;
541 mFrontBufferIndex = index;
542
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543 /* NOTE: it's safe to set this flag here because this is only touched
544 * from LayerBitmap::allocate(), which by construction cannot happen
545 * while we're in post().
546 */
547 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 // ... post the new front-buffer
550 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700551 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 // oldValue, newValue, mFrontBufferIndex);
555 //dirty.dump("dirty");
556
557 if (UNLIKELY(oldValue & eResizeRequested)) {
558
559 LOGD_IF(DEBUG_RESIZE,
560 "post (layer=%p), state=%08x, "
561 "index=%d, (%dx%d), (%dx%d)",
562 this, newValue,
563 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
565 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566
567 // here, we just posted the surface and we have resolved
568 // the front/back buffer indices. The client is blocked, so
569 // it cannot start using the new backbuffer.
570
571 // If the backbuffer was resized in THIS round, we actually cannot
572 // resize the frontbuffer because it has *just* been drawn (and we
573 // would have nothing to draw). In this case we just skip the resize
574 // it'll happen after the next page flip or during the next
575 // transaction.
576
577 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
578 if (mResizeTransactionDone && (newValue & mask)) {
579 // Resize the layer's second buffer only if the transaction
580 // happened. It may not have happened yet if eResizeRequested
581 // was set immediately after the "transactionRequested" test,
582 // in which case the drawing state's size would be wrong.
583 mFreezeLock.clear();
584 const Layer::State& s(drawingState());
585 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
586 do {
587 oldValue = lcblk->swapState;
588 if ((oldValue & eResizeRequested) == eResizeRequested) {
589 // ugh, another resize was requested since we processed
590 // the first buffer, don't free the client, and let
591 // the next transaction handle everything.
592 break;
593 }
594 newValue = oldValue & ~mask;
595 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
596 }
597 mResizeTransactionDone = false;
598 recomputeVisibleRegions = true;
599 this->contentDirty = true;
600 }
601 }
602
603 reloadTexture(dirty);
604
605 return dirty;
606}
607
608Point Layer::getPhysicalSize() const
609{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700610 sp<const Buffer> front(frontBuffer().getBuffer());
611 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612}
613
614void Layer::unlockPageFlip(
615 const Transform& planeTransform, Region& outDirtyRegion)
616{
617 Region dirtyRegion(mPostedDirtyRegion);
618 if (!dirtyRegion.isEmpty()) {
619 mPostedDirtyRegion.clear();
620 // The dirty region is given in the layer's coordinate space
621 // transform the dirty region by the surface's transformation
622 // and the global transformation.
623 const Layer::State& s(drawingState());
624 const Transform tr(planeTransform * s.transform);
625 dirtyRegion = tr.transform(dirtyRegion);
626
627 // At this point, the dirty region is in screen space.
628 // Make sure it's constrained by the visible region (which
629 // is in screen space as well).
630 dirtyRegion.andSelf(visibleRegionScreen);
631 outDirtyRegion.orSelf(dirtyRegion);
632
633 // client could be blocked, so signal them so they get a
634 // chance to reevaluate their condition.
Mathias Agopianf9d93272009-06-19 17:00:27 -0700635 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 }
637}
638
639void Layer::finishPageFlip()
640{
641 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
642 LOGE_IF(!(lcblk->swapState & eBusy),
643 "layer %p wasn't locked!", this);
644 android_atomic_and(~eBusy, &(lcblk->swapState));
645 }
Mathias Agopianf9d93272009-06-19 17:00:27 -0700646 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647}
648
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649// ---------------------------------------------------------------------------
650
Mathias Agopian9a112062009-04-17 19:36:26 -0700651Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
652 SurfaceID id, const sp<Layer>& owner)
653 : Surface(flinger, id, owner->getIdentity(), owner)
654{
655}
656
657Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658{
659}
660
661sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
662{
663 sp<SurfaceBuffer> buffer = 0;
664 sp<Layer> owner(getOwner());
665 if (owner != 0) {
666 buffer = owner->peekBuffer();
667 }
668 return buffer;
669}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670
671// ---------------------------------------------------------------------------
672
673
674}; // namespace android