blob: 76259317482fa7d41a9a297aae8cfa4e3a10c172 [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) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070072 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070073 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 }
75 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
76 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
77 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070078 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 }
81}
82
83void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags)
84{
85 LayerBase::initStates(w,h,flags);
86
87 if (flags & ISurfaceComposer::eDestroyBackbuffer)
88 lcblk->flags |= eNoCopyBack;
89}
90
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092{
93 return mSurface;
94}
95
Mathias Agopian9a112062009-04-17 19:36:26 -070096status_t Layer::ditch()
97{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070098 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian9a112062009-04-17 19:36:26 -070099 mSurface.clear();
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700100 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -0700101 return NO_ERROR;
102}
103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104status_t Layer::setBuffers( Client* client,
105 uint32_t w, uint32_t h,
106 PixelFormat format, uint32_t flags)
107{
108 PixelFormatInfo info;
109 status_t err = getPixelFormatInfo(format, &info);
110 if (err) return err;
111
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 uint32_t bufferFlags = 0;
113 if (flags & ISurfaceComposer::eGPU)
114 bufferFlags |= Buffer::GPU;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700116 if (flags & ISurfaceComposer::eSecure)
117 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700119 /* FIXME we need this code for msm7201A
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 if (bufferFlags & Buffer::GPU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 // FIXME: this is msm7201A specific, as its GPU only supports
122 // BGRA_8888.
123 if (format == PIXEL_FORMAT_RGBA_8888) {
124 format = PIXEL_FORMAT_BGRA_8888;
125 }
126 }
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700127 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
133 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700137 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 return NO_ERROR;
139}
140
141void Layer::reloadTexture(const Region& dirty)
142{
Mathias Agopian0926f502009-05-04 14:17:04 -0700143 const sp<Buffer>& buffer(frontBuffer().getBuffer());
144 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145 int index = mFrontBufferIndex;
146 if (LIKELY(!mTextures[index].dirty)) {
147 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
148 } else {
149 // we need to recreate the texture
150 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
151
152 // create the new texture name if needed
153 if (UNLIKELY(mTextures[index].name == -1U)) {
154 mTextures[index].name = createTexture();
155 } else {
156 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
157 }
158
159 // free the previous image
160 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
161 eglDestroyImageKHR(dpy, mTextures[index].image);
162 mTextures[index].image = EGL_NO_IMAGE_KHR;
163 }
164
165 // construct an EGL_NATIVE_BUFFER_ANDROID
166 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
167
168 // create the new EGLImageKHR
169 const EGLint attrs[] = {
170 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
171 EGL_NONE, EGL_NONE
172 };
173 mTextures[index].image = eglCreateImageKHR(
174 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
175 (EGLClientBuffer)clientBuf, attrs);
176
177 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
178 "eglCreateImageKHR() failed. err=0x%4x",
179 eglGetError());
180
181 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
182 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
183 (GLeglImageOES)mTextures[index].image);
184 GLint error = glGetError();
185 if (UNLIKELY(error != GL_NO_ERROR)) {
186 // this failed, for instance, because we don't support
187 // NPOT.
188 // FIXME: do something!
189 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
190 } else {
191 // Everything went okay!
192 mTextures[index].dirty = false;
193 }
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 }
205 loadTexture(dirty, mTextures[0].name, t,
206 mTextures[0].width, mTextures[0].height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700207 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
212
213void Layer::onDraw(const Region& clip) const
214{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
216 mFrontBufferIndex : 0;
217 GLuint textureName = mTextures[index].name;
218
219 if (UNLIKELY(textureName == -1LU)) {
220 LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 // the texture has not been created yet, this Layer has
222 // in fact never been drawn into. this happens frequently with
223 // SurfaceView.
224 clearWithOpenGL(clip);
225 return;
226 }
227
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228 GGLSurface t;
229 sp<const Buffer> buffer(frontBuffer().getBuffer());
Mathias Agopian0926f502009-05-04 14:17:04 -0700230 drawWithOpenGL(clip, textureName, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700233sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235 /*
236 * This is called from the client's Surface::lock(), after it locked
237 * the surface successfully. We're therefore guaranteed that the
238 * back-buffer is not in use by ourselves.
239 * Of course, we need to validate all this, which is not trivial.
240 *
241 * FIXME: A resize could happen at any time here. What to do about this?
242 * - resize() form post()
243 * - resize() from doTransaction()
244 *
245 * We'll probably need an internal lock for this.
246 *
247 *
248 * TODO: We need to make sure that post() doesn't swap
249 * the buffers under us.
250 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 // it's okay to read swapState for the purpose of figuring out the
253 // backbuffer index, which cannot change (since the app has locked it).
254 const uint32_t state = lcblk->swapState;
255 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
256
257 // get rid of the EGL image, since we shouldn't need it anymore
258 // (note that we're in a different thread than where it is being used)
259 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
260 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
261 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
262 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700264
265 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
266 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
267
268 LOGD_IF(DEBUG_RESIZE,
269 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
270 this, backBufferIndex,
271 layerBitmap.getWidth(),
272 layerBitmap.getHeight(),
273 layerBitmap.getBuffer()->getWidth(),
274 layerBitmap.getBuffer()->getHeight());
275
276 if (UNLIKELY(buffer == 0)) {
277 // XXX: what to do, what to do?
278 } else {
279 // texture is now dirty...
280 mTextures[backBufferIndex].dirty = true;
281 // ... so it the visible region (because we consider the surface's
282 // buffer size for visibility calculations)
283 forceVisibilityTransaction();
284 mFlinger->setTransactionFlags(eTraversalNeeded);
285 }
286 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287}
288
289uint32_t Layer::doTransaction(uint32_t flags)
290{
291 const Layer::State& front(drawingState());
292 const Layer::State& temp(currentState());
293
294 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
295 // that the size changed back to its previous value before the buffer
296 // was resized (in the eLocked case below), in which case, we still
297 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
300 /*
301 * Various states we could be in...
302
303 resize = state & eResizeRequested;
304 if (backbufferChanged) {
305 if (resize == 0) {
306 // ERROR, the resized buffer doesn't have its resize flag set
307 } else if (resize == mask) {
308 // ERROR one of the buffer has already been resized
309 } else if (resize == mask ^ eResizeRequested) {
310 // ERROR, the resized buffer doesn't have its resize flag set
311 } else if (resize == eResizeRequested) {
312 // OK, Normal case, proceed with resize
313 }
314 } else {
315 if (resize == 0) {
316 // OK, nothing special, do nothing
317 } else if (resize == mask) {
318 // restarted transaction, do nothing
319 } else if (resize == mask ^ eResizeRequested) {
320 // restarted transaction, do nothing
321 } else if (resize == eResizeRequested) {
322 // OK, size reset to previous value, proceed with resize
323 }
324 }
325 */
326
327 // Index of the back buffer
328 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
329 const uint32_t state = lcblk->swapState;
330 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
331 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
332 uint32_t resizeFlags = state & eResizeRequested;
333
334 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
335 LOGE( "backbuffer size changed, but both resize flags are not set! "
336 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
337 "index=%d, (%dx%d), (%dx%d)",
338 this, state,
339 int(temp.w), int(temp.h),
340 int(drawingState().w), int(drawingState().h),
341 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700342 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
343 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 // if we get there we're pretty screwed. the only reasonable
345 // thing to do is to pretend we should do the resize since
346 // backbufferChanged is set (this also will give a chance to
347 // client to get unblocked)
348 resizeFlags = eResizeRequested;
349 }
350
351 if (resizeFlags == eResizeRequested) {
352 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
353 // here, would be wrong and misleading because by this point
354 // mFrontBufferIndex has not been updated yet.
355
356 LOGD_IF(DEBUG_RESIZE,
357 "resize (layer=%p), state=%08x, "
358 "requested (%dx%d), "
359 "drawing (%d,%d), "
360 "index=%d, (%dx%d), (%dx%d)",
361 this, state,
362 int(temp.w), int(temp.h),
363 int(drawingState().w), int(drawingState().h),
364 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700365 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
366 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367
368 if (state & eLocked) {
369 // if the buffer is locked, we can't resize anything because
370 // - the backbuffer is currently in use by the user
371 // - the front buffer is being shown
372 // We just act as if the transaction didn't happen and we
373 // reschedule it later...
374 flags |= eRestartTransaction;
375 } else {
376 // This buffer needs to be resized
377 status_t err =
378 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
379 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700380 const uint32_t mask = clientBackBufferIndex ?
381 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 android_atomic_and(~mask, &(lcblk->swapState));
383 // since a buffer became available, we can let the client go...
384 mFlinger->scheduleBroadcast(client);
385 mResizeTransactionDone = true;
386
387 // we're being resized and there is a freeze display request,
388 // acquire a freeze lock, so that the screen stays put
389 // until we've redrawn at the new size; this is to avoid
390 // glitches upon orientation changes.
391 if (mFlinger->hasFreezeRequest()) {
392 // if the surface is hidden, don't try to acquire the
393 // freeze lock, since hidden surfaces may never redraw
394 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
395 mFreezeLock = mFlinger->getFreezeLock();
396 }
397 }
398 }
399 }
400 }
401
402 if (temp.sequence != front.sequence) {
403 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
404 // this surface is now hidden, so it shouldn't hold a freeze lock
405 // (it may never redraw, which is fine if it is hidden)
406 mFreezeLock.clear();
407 }
408 }
409
410 return LayerBase::doTransaction(flags);
411}
412
413status_t Layer::resize(
414 int32_t clientBackBufferIndex,
415 uint32_t width, uint32_t height,
416 const char* what)
417{
418 /*
419 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700420 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 */
422
423 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
424
425 // if the new (transaction) size is != from the the backbuffer
426 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700427 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
428 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429
430 LOGD_IF(!backbufferChanged,
431 "(%s) eResizeRequested (layer=%p), but size not changed: "
432 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
433 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
434 what, this,
435 int(width), int(height),
436 int(drawingState().w), int(drawingState().h),
437 int(currentState().w), int(currentState().h),
438 long(lcblk->swapState),
439 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700440 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
441 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442
443 // this can happen when changing the size back and forth quickly
444 status_t err = NO_ERROR;
445 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700446
447 LOGD_IF(DEBUG_RESIZE,
448 "resize (layer=%p), requested (%dx%d), "
449 "index=%d, (%dx%d), (%dx%d)",
450 this, int(width), int(height), int(clientBackBufferIndex),
451 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
452 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
453
454 err = mBuffers[clientBackBufferIndex].setSize(width, height);
455 if (UNLIKELY(err != NO_ERROR)) {
456 // This really should never happen
457 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
458 clientBackBufferIndex, width, height, err, strerror(err));
459 // couldn't reallocate the surface
460 android_atomic_write(eInvalidSurface, &lcblk->swapState);
461 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 }
463 return err;
464}
465
466void Layer::setSizeChanged(uint32_t w, uint32_t h)
467{
468 LOGD_IF(DEBUG_RESIZE,
469 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
470 w, h, mCurrentState.w, mCurrentState.h);
471 android_atomic_or(eResizeRequested, &(lcblk->swapState));
472}
473
474// ----------------------------------------------------------------------------
475// pageflip handling...
476// ----------------------------------------------------------------------------
477
478void Layer::lockPageFlip(bool& recomputeVisibleRegions)
479{
480 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
481 // preemptively block the client, because he might set
482 // eFlipRequested at any time and want to use this buffer
483 // for the next frame. This will be unset below if it
484 // turns out we didn't need it.
485
486 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
487 if (!(state & mask))
488 return;
489
490 if (UNLIKELY(state & eInvalidSurface)) {
491 // if eInvalidSurface is set, this means the surface
492 // became invalid during a transaction (NO_MEMORY for instance)
493 mFlinger->scheduleBroadcast(client);
494 return;
495 }
496
497 if (UNLIKELY(state & eFlipRequested)) {
498 uint32_t oldState;
499 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
500 if (oldState & eNextFlipPending) {
501 // Process another round (we know at least a buffer
502 // is ready for that client).
503 mFlinger->signalEvent();
504 }
505 }
506}
507
508Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
509{
510 // atomically swap buffers and (re)set eFlipRequested
511 int32_t oldValue, newValue;
512 layer_cblk_t * const lcblk = this->lcblk;
513 do {
514 oldValue = lcblk->swapState;
515 // get the current value
516
517 LOG_ASSERT(oldValue&eFlipRequested,
518 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
519 long(oldValue));
520
521 newValue = (oldValue ^ eIndex);
522 // swap buffers
523
524 newValue &= ~(eFlipRequested | eNextFlipPending);
525 // clear eFlipRequested and eNextFlipPending
526
527 if (oldValue & eNextFlipPending)
528 newValue |= eFlipRequested;
529 // if eNextFlipPending is set (second buffer already has something
530 // in it) we need to reset eFlipRequested because the client
531 // might never do it
532
533 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
534 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700535
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 const int32_t index = (newValue & eIndex) ^ 1;
537 mFrontBufferIndex = index;
538
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539 /* NOTE: it's safe to set this flag here because this is only touched
540 * from LayerBitmap::allocate(), which by construction cannot happen
541 * while we're in post().
542 */
543 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 // ... post the new front-buffer
546 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700549 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 // oldValue, newValue, mFrontBufferIndex);
551 //dirty.dump("dirty");
552
553 if (UNLIKELY(oldValue & eResizeRequested)) {
554
555 LOGD_IF(DEBUG_RESIZE,
556 "post (layer=%p), state=%08x, "
557 "index=%d, (%dx%d), (%dx%d)",
558 this, newValue,
559 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
561 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562
563 // here, we just posted the surface and we have resolved
564 // the front/back buffer indices. The client is blocked, so
565 // it cannot start using the new backbuffer.
566
567 // If the backbuffer was resized in THIS round, we actually cannot
568 // resize the frontbuffer because it has *just* been drawn (and we
569 // would have nothing to draw). In this case we just skip the resize
570 // it'll happen after the next page flip or during the next
571 // transaction.
572
573 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
574 if (mResizeTransactionDone && (newValue & mask)) {
575 // Resize the layer's second buffer only if the transaction
576 // happened. It may not have happened yet if eResizeRequested
577 // was set immediately after the "transactionRequested" test,
578 // in which case the drawing state's size would be wrong.
579 mFreezeLock.clear();
580 const Layer::State& s(drawingState());
581 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
582 do {
583 oldValue = lcblk->swapState;
584 if ((oldValue & eResizeRequested) == eResizeRequested) {
585 // ugh, another resize was requested since we processed
586 // the first buffer, don't free the client, and let
587 // the next transaction handle everything.
588 break;
589 }
590 newValue = oldValue & ~mask;
591 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
592 }
593 mResizeTransactionDone = false;
594 recomputeVisibleRegions = true;
595 this->contentDirty = true;
596 }
597 }
598
599 reloadTexture(dirty);
600
601 return dirty;
602}
603
604Point Layer::getPhysicalSize() const
605{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 sp<const Buffer> front(frontBuffer().getBuffer());
607 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608}
609
610void Layer::unlockPageFlip(
611 const Transform& planeTransform, Region& outDirtyRegion)
612{
613 Region dirtyRegion(mPostedDirtyRegion);
614 if (!dirtyRegion.isEmpty()) {
615 mPostedDirtyRegion.clear();
616 // The dirty region is given in the layer's coordinate space
617 // transform the dirty region by the surface's transformation
618 // and the global transformation.
619 const Layer::State& s(drawingState());
620 const Transform tr(planeTransform * s.transform);
621 dirtyRegion = tr.transform(dirtyRegion);
622
623 // At this point, the dirty region is in screen space.
624 // Make sure it's constrained by the visible region (which
625 // is in screen space as well).
626 dirtyRegion.andSelf(visibleRegionScreen);
627 outDirtyRegion.orSelf(dirtyRegion);
628
629 // client could be blocked, so signal them so they get a
630 // chance to reevaluate their condition.
631 mFlinger->scheduleBroadcast(client);
632 }
633}
634
635void Layer::finishPageFlip()
636{
637 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
638 LOGE_IF(!(lcblk->swapState & eBusy),
639 "layer %p wasn't locked!", this);
640 android_atomic_and(~eBusy, &(lcblk->swapState));
641 }
642 mFlinger->scheduleBroadcast(client);
643}
644
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645// ---------------------------------------------------------------------------
646
Mathias Agopian9a112062009-04-17 19:36:26 -0700647Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
648 SurfaceID id, const sp<Layer>& owner)
649 : Surface(flinger, id, owner->getIdentity(), owner)
650{
651}
652
653Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654{
655}
656
657sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
658{
659 sp<SurfaceBuffer> buffer = 0;
660 sp<Layer> owner(getOwner());
661 if (owner != 0) {
662 buffer = owner->peekBuffer();
663 }
664 return buffer;
665}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666
667// ---------------------------------------------------------------------------
668
669
670}; // namespace android