blob: 8a7d46784f55f827ce20a65254eb9745fce89099 [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>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <utils/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#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"
34#include "LayerBitmap.h"
35#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
38
39#define DEBUG_RESIZE 0
40
41
42namespace android {
43
44// ---------------------------------------------------------------------------
45
46const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
47const char* const Layer::typeID = "Layer";
48
49// ---------------------------------------------------------------------------
50
51Layer::Layer(SurfaceFlinger* flinger, DisplayID display, Client* c, int32_t i)
52 : LayerBaseClient(flinger, display, c, i),
53 mSecure(false),
54 mFrontBufferIndex(1),
55 mNeedsBlending(true),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056 mResizeTransactionDone(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057{
58 // no OpenGL operation is possible here, since we might not be
59 // in the OpenGL thread.
60}
61
62Layer::~Layer()
63{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070064 destroy();
65 // the actual buffers will be destroyed here
66}
67
68void Layer::destroy()
69{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 for (int i=0 ; i<NUM_BUFFERS ; i++) {
71 if (mTextures[i].name != -1U) {
72 // FIXME: this was originally to work-around a bug in the
73 // adreno driver. this should be fixed now.
Mathias Agopian550b79f2009-04-22 15:49:28 -070074 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070075 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 }
77 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
78 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
79 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070080 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 }
83}
84
85void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags)
86{
87 LayerBase::initStates(w,h,flags);
88
89 if (flags & ISurfaceComposer::eDestroyBackbuffer)
90 lcblk->flags |= eNoCopyBack;
91}
92
Mathias Agopian076b1cc2009-04-10 14:24:30 -070093sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094{
95 return mSurface;
96}
97
Mathias Agopian9a112062009-04-17 19:36:26 -070098status_t Layer::ditch()
99{
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700100 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian9a112062009-04-17 19:36:26 -0700101 mSurface.clear();
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700102 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -0700103 return NO_ERROR;
104}
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106status_t Layer::setBuffers( Client* client,
107 uint32_t w, uint32_t h,
108 PixelFormat format, uint32_t flags)
109{
110 PixelFormatInfo info;
111 status_t err = getPixelFormatInfo(format, &info);
112 if (err) return err;
113
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114 uint32_t bufferFlags = 0;
115 if (flags & ISurfaceComposer::eGPU)
116 bufferFlags |= Buffer::GPU;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 if (flags & ISurfaceComposer::eSecure)
119 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700121 /* FIXME we need this code for msm7201A
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 if (bufferFlags & Buffer::GPU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 // FIXME: this is msm7201A specific, as its GPU only supports
124 // BGRA_8888.
125 if (format == PIXEL_FORMAT_RGBA_8888) {
126 format = PIXEL_FORMAT_BGRA_8888;
127 }
128 }
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700129 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
135 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700139 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 return NO_ERROR;
141}
142
143void Layer::reloadTexture(const Region& dirty)
144{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145 const sp<const Buffer>& buffer(frontBuffer().getBuffer());
146 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
147 int index = mFrontBufferIndex;
148 if (LIKELY(!mTextures[index].dirty)) {
149 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
150 } else {
151 // we need to recreate the texture
152 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
153
154 // create the new texture name if needed
155 if (UNLIKELY(mTextures[index].name == -1U)) {
156 mTextures[index].name = createTexture();
157 } else {
158 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
159 }
160
161 // free the previous image
162 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
163 eglDestroyImageKHR(dpy, mTextures[index].image);
164 mTextures[index].image = EGL_NO_IMAGE_KHR;
165 }
166
167 // construct an EGL_NATIVE_BUFFER_ANDROID
168 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
169
170 // create the new EGLImageKHR
171 const EGLint attrs[] = {
172 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
173 EGL_NONE, EGL_NONE
174 };
175 mTextures[index].image = eglCreateImageKHR(
176 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
177 (EGLClientBuffer)clientBuf, attrs);
178
179 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
180 "eglCreateImageKHR() failed. err=0x%4x",
181 eglGetError());
182
183 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
184 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
185 (GLeglImageOES)mTextures[index].image);
186 GLint error = glGetError();
187 if (UNLIKELY(error != GL_NO_ERROR)) {
188 // this failed, for instance, because we don't support
189 // NPOT.
190 // FIXME: do something!
191 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
192 } else {
193 // Everything went okay!
194 mTextures[index].dirty = false;
195 }
196 }
197 }
198 } else {
199 GGLSurface t;
200 if (LIKELY(buffer->getBitmapSurface(&t) == NO_ERROR)) {
201 if (UNLIKELY(mTextures[0].name == -1U)) {
202 mTextures[0].name = createTexture();
203 }
204 loadTexture(dirty, mTextures[0].name, t,
205 mTextures[0].width, mTextures[0].height);
206 }
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());
228 buffer->getBitmapSurface(&t);
229 drawWithOpenGL(clip, textureName, t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230}
231
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 /*
235 * This is called from the client's Surface::lock(), after it locked
236 * the surface successfully. We're therefore guaranteed that the
237 * back-buffer is not in use by ourselves.
238 * Of course, we need to validate all this, which is not trivial.
239 *
240 * FIXME: A resize could happen at any time here. What to do about this?
241 * - resize() form post()
242 * - resize() from doTransaction()
243 *
244 * We'll probably need an internal lock for this.
245 *
246 *
247 * TODO: We need to make sure that post() doesn't swap
248 * the buffers under us.
249 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251 // it's okay to read swapState for the purpose of figuring out the
252 // backbuffer index, which cannot change (since the app has locked it).
253 const uint32_t state = lcblk->swapState;
254 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
255
256 // get rid of the EGL image, since we shouldn't need it anymore
257 // (note that we're in a different thread than where it is being used)
258 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
259 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
260 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
261 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700263
264 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
265 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
266
267 LOGD_IF(DEBUG_RESIZE,
268 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
269 this, backBufferIndex,
270 layerBitmap.getWidth(),
271 layerBitmap.getHeight(),
272 layerBitmap.getBuffer()->getWidth(),
273 layerBitmap.getBuffer()->getHeight());
274
275 if (UNLIKELY(buffer == 0)) {
276 // XXX: what to do, what to do?
277 } else {
278 // texture is now dirty...
279 mTextures[backBufferIndex].dirty = true;
280 // ... so it the visible region (because we consider the surface's
281 // buffer size for visibility calculations)
282 forceVisibilityTransaction();
283 mFlinger->setTransactionFlags(eTraversalNeeded);
284 }
285 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
287
288uint32_t Layer::doTransaction(uint32_t flags)
289{
290 const Layer::State& front(drawingState());
291 const Layer::State& temp(currentState());
292
293 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
294 // that the size changed back to its previous value before the buffer
295 // was resized (in the eLocked case below), in which case, we still
296 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
299 /*
300 * Various states we could be in...
301
302 resize = state & eResizeRequested;
303 if (backbufferChanged) {
304 if (resize == 0) {
305 // ERROR, the resized buffer doesn't have its resize flag set
306 } else if (resize == mask) {
307 // ERROR one of the buffer has already been resized
308 } else if (resize == mask ^ eResizeRequested) {
309 // ERROR, the resized buffer doesn't have its resize flag set
310 } else if (resize == eResizeRequested) {
311 // OK, Normal case, proceed with resize
312 }
313 } else {
314 if (resize == 0) {
315 // OK, nothing special, do nothing
316 } else if (resize == mask) {
317 // restarted transaction, do nothing
318 } else if (resize == mask ^ eResizeRequested) {
319 // restarted transaction, do nothing
320 } else if (resize == eResizeRequested) {
321 // OK, size reset to previous value, proceed with resize
322 }
323 }
324 */
325
326 // Index of the back buffer
327 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
328 const uint32_t state = lcblk->swapState;
329 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
330 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
331 uint32_t resizeFlags = state & eResizeRequested;
332
333 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
334 LOGE( "backbuffer size changed, but both resize flags are not set! "
335 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
336 "index=%d, (%dx%d), (%dx%d)",
337 this, state,
338 int(temp.w), int(temp.h),
339 int(drawingState().w), int(drawingState().h),
340 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700341 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
342 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 // if we get there we're pretty screwed. the only reasonable
344 // thing to do is to pretend we should do the resize since
345 // backbufferChanged is set (this also will give a chance to
346 // client to get unblocked)
347 resizeFlags = eResizeRequested;
348 }
349
350 if (resizeFlags == eResizeRequested) {
351 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
352 // here, would be wrong and misleading because by this point
353 // mFrontBufferIndex has not been updated yet.
354
355 LOGD_IF(DEBUG_RESIZE,
356 "resize (layer=%p), state=%08x, "
357 "requested (%dx%d), "
358 "drawing (%d,%d), "
359 "index=%d, (%dx%d), (%dx%d)",
360 this, state,
361 int(temp.w), int(temp.h),
362 int(drawingState().w), int(drawingState().h),
363 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700364 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
365 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366
367 if (state & eLocked) {
368 // if the buffer is locked, we can't resize anything because
369 // - the backbuffer is currently in use by the user
370 // - the front buffer is being shown
371 // We just act as if the transaction didn't happen and we
372 // reschedule it later...
373 flags |= eRestartTransaction;
374 } else {
375 // This buffer needs to be resized
376 status_t err =
377 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
378 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700379 const uint32_t mask = clientBackBufferIndex ?
380 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 android_atomic_and(~mask, &(lcblk->swapState));
382 // since a buffer became available, we can let the client go...
383 mFlinger->scheduleBroadcast(client);
384 mResizeTransactionDone = true;
385
386 // we're being resized and there is a freeze display request,
387 // acquire a freeze lock, so that the screen stays put
388 // until we've redrawn at the new size; this is to avoid
389 // glitches upon orientation changes.
390 if (mFlinger->hasFreezeRequest()) {
391 // if the surface is hidden, don't try to acquire the
392 // freeze lock, since hidden surfaces may never redraw
393 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
394 mFreezeLock = mFlinger->getFreezeLock();
395 }
396 }
397 }
398 }
399 }
400
401 if (temp.sequence != front.sequence) {
402 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
403 // this surface is now hidden, so it shouldn't hold a freeze lock
404 // (it may never redraw, which is fine if it is hidden)
405 mFreezeLock.clear();
406 }
407 }
408
409 return LayerBase::doTransaction(flags);
410}
411
412status_t Layer::resize(
413 int32_t clientBackBufferIndex,
414 uint32_t width, uint32_t height,
415 const char* what)
416{
417 /*
418 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700419 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 */
421
422 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
423
424 // if the new (transaction) size is != from the the backbuffer
425 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700426 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
427 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428
429 LOGD_IF(!backbufferChanged,
430 "(%s) eResizeRequested (layer=%p), but size not changed: "
431 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
432 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
433 what, this,
434 int(width), int(height),
435 int(drawingState().w), int(drawingState().h),
436 int(currentState().w), int(currentState().h),
437 long(lcblk->swapState),
438 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700439 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
440 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441
442 // this can happen when changing the size back and forth quickly
443 status_t err = NO_ERROR;
444 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700445
446 LOGD_IF(DEBUG_RESIZE,
447 "resize (layer=%p), requested (%dx%d), "
448 "index=%d, (%dx%d), (%dx%d)",
449 this, int(width), int(height), int(clientBackBufferIndex),
450 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
451 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
452
453 err = mBuffers[clientBackBufferIndex].setSize(width, height);
454 if (UNLIKELY(err != NO_ERROR)) {
455 // This really should never happen
456 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
457 clientBackBufferIndex, width, height, err, strerror(err));
458 // couldn't reallocate the surface
459 android_atomic_write(eInvalidSurface, &lcblk->swapState);
460 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 }
462 return err;
463}
464
465void Layer::setSizeChanged(uint32_t w, uint32_t h)
466{
467 LOGD_IF(DEBUG_RESIZE,
468 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
469 w, h, mCurrentState.w, mCurrentState.h);
470 android_atomic_or(eResizeRequested, &(lcblk->swapState));
471}
472
473// ----------------------------------------------------------------------------
474// pageflip handling...
475// ----------------------------------------------------------------------------
476
477void Layer::lockPageFlip(bool& recomputeVisibleRegions)
478{
479 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
480 // preemptively block the client, because he might set
481 // eFlipRequested at any time and want to use this buffer
482 // for the next frame. This will be unset below if it
483 // turns out we didn't need it.
484
485 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
486 if (!(state & mask))
487 return;
488
489 if (UNLIKELY(state & eInvalidSurface)) {
490 // if eInvalidSurface is set, this means the surface
491 // became invalid during a transaction (NO_MEMORY for instance)
492 mFlinger->scheduleBroadcast(client);
493 return;
494 }
495
496 if (UNLIKELY(state & eFlipRequested)) {
497 uint32_t oldState;
498 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
499 if (oldState & eNextFlipPending) {
500 // Process another round (we know at least a buffer
501 // is ready for that client).
502 mFlinger->signalEvent();
503 }
504 }
505}
506
507Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
508{
509 // atomically swap buffers and (re)set eFlipRequested
510 int32_t oldValue, newValue;
511 layer_cblk_t * const lcblk = this->lcblk;
512 do {
513 oldValue = lcblk->swapState;
514 // get the current value
515
516 LOG_ASSERT(oldValue&eFlipRequested,
517 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
518 long(oldValue));
519
520 newValue = (oldValue ^ eIndex);
521 // swap buffers
522
523 newValue &= ~(eFlipRequested | eNextFlipPending);
524 // clear eFlipRequested and eNextFlipPending
525
526 if (oldValue & eNextFlipPending)
527 newValue |= eFlipRequested;
528 // if eNextFlipPending is set (second buffer already has something
529 // in it) we need to reset eFlipRequested because the client
530 // might never do it
531
532 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
533 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 const int32_t index = (newValue & eIndex) ^ 1;
536 mFrontBufferIndex = index;
537
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538 /* NOTE: it's safe to set this flag here because this is only touched
539 * from LayerBitmap::allocate(), which by construction cannot happen
540 * while we're in post().
541 */
542 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 // ... post the new front-buffer
545 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 // oldValue, newValue, mFrontBufferIndex);
550 //dirty.dump("dirty");
551
552 if (UNLIKELY(oldValue & eResizeRequested)) {
553
554 LOGD_IF(DEBUG_RESIZE,
555 "post (layer=%p), state=%08x, "
556 "index=%d, (%dx%d), (%dx%d)",
557 this, newValue,
558 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
560 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561
562 // here, we just posted the surface and we have resolved
563 // the front/back buffer indices. The client is blocked, so
564 // it cannot start using the new backbuffer.
565
566 // If the backbuffer was resized in THIS round, we actually cannot
567 // resize the frontbuffer because it has *just* been drawn (and we
568 // would have nothing to draw). In this case we just skip the resize
569 // it'll happen after the next page flip or during the next
570 // transaction.
571
572 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
573 if (mResizeTransactionDone && (newValue & mask)) {
574 // Resize the layer's second buffer only if the transaction
575 // happened. It may not have happened yet if eResizeRequested
576 // was set immediately after the "transactionRequested" test,
577 // in which case the drawing state's size would be wrong.
578 mFreezeLock.clear();
579 const Layer::State& s(drawingState());
580 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
581 do {
582 oldValue = lcblk->swapState;
583 if ((oldValue & eResizeRequested) == eResizeRequested) {
584 // ugh, another resize was requested since we processed
585 // the first buffer, don't free the client, and let
586 // the next transaction handle everything.
587 break;
588 }
589 newValue = oldValue & ~mask;
590 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
591 }
592 mResizeTransactionDone = false;
593 recomputeVisibleRegions = true;
594 this->contentDirty = true;
595 }
596 }
597
598 reloadTexture(dirty);
599
600 return dirty;
601}
602
603Point Layer::getPhysicalSize() const
604{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 sp<const Buffer> front(frontBuffer().getBuffer());
606 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607}
608
609void Layer::unlockPageFlip(
610 const Transform& planeTransform, Region& outDirtyRegion)
611{
612 Region dirtyRegion(mPostedDirtyRegion);
613 if (!dirtyRegion.isEmpty()) {
614 mPostedDirtyRegion.clear();
615 // The dirty region is given in the layer's coordinate space
616 // transform the dirty region by the surface's transformation
617 // and the global transformation.
618 const Layer::State& s(drawingState());
619 const Transform tr(planeTransform * s.transform);
620 dirtyRegion = tr.transform(dirtyRegion);
621
622 // At this point, the dirty region is in screen space.
623 // Make sure it's constrained by the visible region (which
624 // is in screen space as well).
625 dirtyRegion.andSelf(visibleRegionScreen);
626 outDirtyRegion.orSelf(dirtyRegion);
627
628 // client could be blocked, so signal them so they get a
629 // chance to reevaluate their condition.
630 mFlinger->scheduleBroadcast(client);
631 }
632}
633
634void Layer::finishPageFlip()
635{
636 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
637 LOGE_IF(!(lcblk->swapState & eBusy),
638 "layer %p wasn't locked!", this);
639 android_atomic_and(~eBusy, &(lcblk->swapState));
640 }
641 mFlinger->scheduleBroadcast(client);
642}
643
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644// ---------------------------------------------------------------------------
645
Mathias Agopian9a112062009-04-17 19:36:26 -0700646Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
647 SurfaceID id, const sp<Layer>& owner)
648 : Surface(flinger, id, owner->getIdentity(), owner)
649{
650}
651
652Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653{
654}
655
656sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
657{
658 sp<SurfaceBuffer> buffer = 0;
659 sp<Layer> owner(getOwner());
660 if (owner != 0) {
661 buffer = owner->peekBuffer();
662 }
663 return buffer;
664}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
666// ---------------------------------------------------------------------------
667
668
669}; // namespace android