blob: 980b78b7a767998cfe83a6d04fdd87ad758e4fe1 [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 Agopian076b1cc2009-04-10 14:24:30 -070064 for (int i=0 ; i<NUM_BUFFERS ; i++) {
65 if (mTextures[i].name != -1U) {
66 // FIXME: this was originally to work-around a bug in the
67 // adreno driver. this should be fixed now.
68 deletedTextures.add(mTextures[i].name);
69 }
70 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
71 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
72 eglDestroyImageKHR(dpy, mTextures[i].image);
73 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 }
75}
76
77void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags)
78{
79 LayerBase::initStates(w,h,flags);
80
81 if (flags & ISurfaceComposer::eDestroyBackbuffer)
82 lcblk->flags |= eNoCopyBack;
83}
84
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086{
87 return mSurface;
88}
89
90status_t Layer::setBuffers( Client* client,
91 uint32_t w, uint32_t h,
92 PixelFormat format, uint32_t flags)
93{
94 PixelFormatInfo info;
95 status_t err = getPixelFormatInfo(format, &info);
96 if (err) return err;
97
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 uint32_t bufferFlags = 0;
99 if (flags & ISurfaceComposer::eGPU)
100 bufferFlags |= Buffer::GPU;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 if (flags & ISurfaceComposer::eSecure)
103 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105
106 if (bufferFlags & Buffer::GPU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 // FIXME: this is msm7201A specific, as its GPU only supports
108 // BGRA_8888.
109 if (format == PIXEL_FORMAT_RGBA_8888) {
110 format = PIXEL_FORMAT_BGRA_8888;
111 }
112 }
113
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700117 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
118 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 mSurface = new SurfaceLayer(clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 return NO_ERROR;
124}
125
126void Layer::reloadTexture(const Region& dirty)
127{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700128 const sp<const Buffer>& buffer(frontBuffer().getBuffer());
129 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
130 int index = mFrontBufferIndex;
131 if (LIKELY(!mTextures[index].dirty)) {
132 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
133 } else {
134 // we need to recreate the texture
135 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
136
137 // create the new texture name if needed
138 if (UNLIKELY(mTextures[index].name == -1U)) {
139 mTextures[index].name = createTexture();
140 } else {
141 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
142 }
143
144 // free the previous image
145 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
146 eglDestroyImageKHR(dpy, mTextures[index].image);
147 mTextures[index].image = EGL_NO_IMAGE_KHR;
148 }
149
150 // construct an EGL_NATIVE_BUFFER_ANDROID
151 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
152
153 // create the new EGLImageKHR
154 const EGLint attrs[] = {
155 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
156 EGL_NONE, EGL_NONE
157 };
158 mTextures[index].image = eglCreateImageKHR(
159 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
160 (EGLClientBuffer)clientBuf, attrs);
161
162 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
163 "eglCreateImageKHR() failed. err=0x%4x",
164 eglGetError());
165
166 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
167 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
168 (GLeglImageOES)mTextures[index].image);
169 GLint error = glGetError();
170 if (UNLIKELY(error != GL_NO_ERROR)) {
171 // this failed, for instance, because we don't support
172 // NPOT.
173 // FIXME: do something!
174 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
175 } else {
176 // Everything went okay!
177 mTextures[index].dirty = false;
178 }
179 }
180 }
181 } else {
182 GGLSurface t;
183 if (LIKELY(buffer->getBitmapSurface(&t) == NO_ERROR)) {
184 if (UNLIKELY(mTextures[0].name == -1U)) {
185 mTextures[0].name = createTexture();
186 }
187 loadTexture(dirty, mTextures[0].name, t,
188 mTextures[0].width, mTextures[0].height);
189 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
193
194void Layer::onDraw(const Region& clip) const
195{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
197 mFrontBufferIndex : 0;
198 GLuint textureName = mTextures[index].name;
199
200 if (UNLIKELY(textureName == -1LU)) {
201 LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 // the texture has not been created yet, this Layer has
203 // in fact never been drawn into. this happens frequently with
204 // SurfaceView.
205 clearWithOpenGL(clip);
206 return;
207 }
208
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209 GGLSurface t;
210 sp<const Buffer> buffer(frontBuffer().getBuffer());
211 buffer->getBitmapSurface(&t);
212 drawWithOpenGL(clip, textureName, t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213}
214
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 /*
218 * This is called from the client's Surface::lock(), after it locked
219 * the surface successfully. We're therefore guaranteed that the
220 * back-buffer is not in use by ourselves.
221 * Of course, we need to validate all this, which is not trivial.
222 *
223 * FIXME: A resize could happen at any time here. What to do about this?
224 * - resize() form post()
225 * - resize() from doTransaction()
226 *
227 * We'll probably need an internal lock for this.
228 *
229 *
230 * TODO: We need to make sure that post() doesn't swap
231 * the buffers under us.
232 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 // it's okay to read swapState for the purpose of figuring out the
235 // backbuffer index, which cannot change (since the app has locked it).
236 const uint32_t state = lcblk->swapState;
237 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
238
239 // get rid of the EGL image, since we shouldn't need it anymore
240 // (note that we're in a different thread than where it is being used)
241 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
242 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
243 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
244 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246
247 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
248 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
249
250 LOGD_IF(DEBUG_RESIZE,
251 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
252 this, backBufferIndex,
253 layerBitmap.getWidth(),
254 layerBitmap.getHeight(),
255 layerBitmap.getBuffer()->getWidth(),
256 layerBitmap.getBuffer()->getHeight());
257
258 if (UNLIKELY(buffer == 0)) {
259 // XXX: what to do, what to do?
260 } else {
261 // texture is now dirty...
262 mTextures[backBufferIndex].dirty = true;
263 // ... so it the visible region (because we consider the surface's
264 // buffer size for visibility calculations)
265 forceVisibilityTransaction();
266 mFlinger->setTransactionFlags(eTraversalNeeded);
267 }
268 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269}
270
271uint32_t Layer::doTransaction(uint32_t flags)
272{
273 const Layer::State& front(drawingState());
274 const Layer::State& temp(currentState());
275
276 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
277 // that the size changed back to its previous value before the buffer
278 // was resized (in the eLocked case below), in which case, we still
279 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
282 /*
283 * Various states we could be in...
284
285 resize = state & eResizeRequested;
286 if (backbufferChanged) {
287 if (resize == 0) {
288 // ERROR, the resized buffer doesn't have its resize flag set
289 } else if (resize == mask) {
290 // ERROR one of the buffer has already been resized
291 } else if (resize == mask ^ eResizeRequested) {
292 // ERROR, the resized buffer doesn't have its resize flag set
293 } else if (resize == eResizeRequested) {
294 // OK, Normal case, proceed with resize
295 }
296 } else {
297 if (resize == 0) {
298 // OK, nothing special, do nothing
299 } else if (resize == mask) {
300 // restarted transaction, do nothing
301 } else if (resize == mask ^ eResizeRequested) {
302 // restarted transaction, do nothing
303 } else if (resize == eResizeRequested) {
304 // OK, size reset to previous value, proceed with resize
305 }
306 }
307 */
308
309 // Index of the back buffer
310 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
311 const uint32_t state = lcblk->swapState;
312 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
313 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
314 uint32_t resizeFlags = state & eResizeRequested;
315
316 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
317 LOGE( "backbuffer size changed, but both resize flags are not set! "
318 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
319 "index=%d, (%dx%d), (%dx%d)",
320 this, state,
321 int(temp.w), int(temp.h),
322 int(drawingState().w), int(drawingState().h),
323 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
325 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 // if we get there we're pretty screwed. the only reasonable
327 // thing to do is to pretend we should do the resize since
328 // backbufferChanged is set (this also will give a chance to
329 // client to get unblocked)
330 resizeFlags = eResizeRequested;
331 }
332
333 if (resizeFlags == eResizeRequested) {
334 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
335 // here, would be wrong and misleading because by this point
336 // mFrontBufferIndex has not been updated yet.
337
338 LOGD_IF(DEBUG_RESIZE,
339 "resize (layer=%p), state=%08x, "
340 "requested (%dx%d), "
341 "drawing (%d,%d), "
342 "index=%d, (%dx%d), (%dx%d)",
343 this, state,
344 int(temp.w), int(temp.h),
345 int(drawingState().w), int(drawingState().h),
346 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
348 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
350 if (state & eLocked) {
351 // if the buffer is locked, we can't resize anything because
352 // - the backbuffer is currently in use by the user
353 // - the front buffer is being shown
354 // We just act as if the transaction didn't happen and we
355 // reschedule it later...
356 flags |= eRestartTransaction;
357 } else {
358 // This buffer needs to be resized
359 status_t err =
360 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
361 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700362 const uint32_t mask = clientBackBufferIndex ?
363 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 android_atomic_and(~mask, &(lcblk->swapState));
365 // since a buffer became available, we can let the client go...
366 mFlinger->scheduleBroadcast(client);
367 mResizeTransactionDone = true;
368
369 // we're being resized and there is a freeze display request,
370 // acquire a freeze lock, so that the screen stays put
371 // until we've redrawn at the new size; this is to avoid
372 // glitches upon orientation changes.
373 if (mFlinger->hasFreezeRequest()) {
374 // if the surface is hidden, don't try to acquire the
375 // freeze lock, since hidden surfaces may never redraw
376 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
377 mFreezeLock = mFlinger->getFreezeLock();
378 }
379 }
380 }
381 }
382 }
383
384 if (temp.sequence != front.sequence) {
385 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
386 // this surface is now hidden, so it shouldn't hold a freeze lock
387 // (it may never redraw, which is fine if it is hidden)
388 mFreezeLock.clear();
389 }
390 }
391
392 return LayerBase::doTransaction(flags);
393}
394
395status_t Layer::resize(
396 int32_t clientBackBufferIndex,
397 uint32_t width, uint32_t height,
398 const char* what)
399{
400 /*
401 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700402 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 */
404
405 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
406
407 // if the new (transaction) size is != from the the backbuffer
408 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700409 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
410 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411
412 LOGD_IF(!backbufferChanged,
413 "(%s) eResizeRequested (layer=%p), but size not changed: "
414 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
415 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
416 what, this,
417 int(width), int(height),
418 int(drawingState().w), int(drawingState().h),
419 int(currentState().w), int(currentState().h),
420 long(lcblk->swapState),
421 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700422 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
423 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424
425 // this can happen when changing the size back and forth quickly
426 status_t err = NO_ERROR;
427 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428
429 LOGD_IF(DEBUG_RESIZE,
430 "resize (layer=%p), requested (%dx%d), "
431 "index=%d, (%dx%d), (%dx%d)",
432 this, int(width), int(height), int(clientBackBufferIndex),
433 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
434 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
435
436 err = mBuffers[clientBackBufferIndex].setSize(width, height);
437 if (UNLIKELY(err != NO_ERROR)) {
438 // This really should never happen
439 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
440 clientBackBufferIndex, width, height, err, strerror(err));
441 // couldn't reallocate the surface
442 android_atomic_write(eInvalidSurface, &lcblk->swapState);
443 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444 }
445 return err;
446}
447
448void Layer::setSizeChanged(uint32_t w, uint32_t h)
449{
450 LOGD_IF(DEBUG_RESIZE,
451 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
452 w, h, mCurrentState.w, mCurrentState.h);
453 android_atomic_or(eResizeRequested, &(lcblk->swapState));
454}
455
456// ----------------------------------------------------------------------------
457// pageflip handling...
458// ----------------------------------------------------------------------------
459
460void Layer::lockPageFlip(bool& recomputeVisibleRegions)
461{
462 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
463 // preemptively block the client, because he might set
464 // eFlipRequested at any time and want to use this buffer
465 // for the next frame. This will be unset below if it
466 // turns out we didn't need it.
467
468 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
469 if (!(state & mask))
470 return;
471
472 if (UNLIKELY(state & eInvalidSurface)) {
473 // if eInvalidSurface is set, this means the surface
474 // became invalid during a transaction (NO_MEMORY for instance)
475 mFlinger->scheduleBroadcast(client);
476 return;
477 }
478
479 if (UNLIKELY(state & eFlipRequested)) {
480 uint32_t oldState;
481 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
482 if (oldState & eNextFlipPending) {
483 // Process another round (we know at least a buffer
484 // is ready for that client).
485 mFlinger->signalEvent();
486 }
487 }
488}
489
490Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
491{
492 // atomically swap buffers and (re)set eFlipRequested
493 int32_t oldValue, newValue;
494 layer_cblk_t * const lcblk = this->lcblk;
495 do {
496 oldValue = lcblk->swapState;
497 // get the current value
498
499 LOG_ASSERT(oldValue&eFlipRequested,
500 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
501 long(oldValue));
502
503 newValue = (oldValue ^ eIndex);
504 // swap buffers
505
506 newValue &= ~(eFlipRequested | eNextFlipPending);
507 // clear eFlipRequested and eNextFlipPending
508
509 if (oldValue & eNextFlipPending)
510 newValue |= eFlipRequested;
511 // if eNextFlipPending is set (second buffer already has something
512 // in it) we need to reset eFlipRequested because the client
513 // might never do it
514
515 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
516 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 const int32_t index = (newValue & eIndex) ^ 1;
519 mFrontBufferIndex = index;
520
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521 /* NOTE: it's safe to set this flag here because this is only touched
522 * from LayerBitmap::allocate(), which by construction cannot happen
523 * while we're in post().
524 */
525 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
526
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 // ... post the new front-buffer
528 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700529 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700531 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 // oldValue, newValue, mFrontBufferIndex);
533 //dirty.dump("dirty");
534
535 if (UNLIKELY(oldValue & eResizeRequested)) {
536
537 LOGD_IF(DEBUG_RESIZE,
538 "post (layer=%p), state=%08x, "
539 "index=%d, (%dx%d), (%dx%d)",
540 this, newValue,
541 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700542 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
543 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544
545 // here, we just posted the surface and we have resolved
546 // the front/back buffer indices. The client is blocked, so
547 // it cannot start using the new backbuffer.
548
549 // If the backbuffer was resized in THIS round, we actually cannot
550 // resize the frontbuffer because it has *just* been drawn (and we
551 // would have nothing to draw). In this case we just skip the resize
552 // it'll happen after the next page flip or during the next
553 // transaction.
554
555 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
556 if (mResizeTransactionDone && (newValue & mask)) {
557 // Resize the layer's second buffer only if the transaction
558 // happened. It may not have happened yet if eResizeRequested
559 // was set immediately after the "transactionRequested" test,
560 // in which case the drawing state's size would be wrong.
561 mFreezeLock.clear();
562 const Layer::State& s(drawingState());
563 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
564 do {
565 oldValue = lcblk->swapState;
566 if ((oldValue & eResizeRequested) == eResizeRequested) {
567 // ugh, another resize was requested since we processed
568 // the first buffer, don't free the client, and let
569 // the next transaction handle everything.
570 break;
571 }
572 newValue = oldValue & ~mask;
573 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
574 }
575 mResizeTransactionDone = false;
576 recomputeVisibleRegions = true;
577 this->contentDirty = true;
578 }
579 }
580
581 reloadTexture(dirty);
582
583 return dirty;
584}
585
586Point Layer::getPhysicalSize() const
587{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588 sp<const Buffer> front(frontBuffer().getBuffer());
589 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590}
591
592void Layer::unlockPageFlip(
593 const Transform& planeTransform, Region& outDirtyRegion)
594{
595 Region dirtyRegion(mPostedDirtyRegion);
596 if (!dirtyRegion.isEmpty()) {
597 mPostedDirtyRegion.clear();
598 // The dirty region is given in the layer's coordinate space
599 // transform the dirty region by the surface's transformation
600 // and the global transformation.
601 const Layer::State& s(drawingState());
602 const Transform tr(planeTransform * s.transform);
603 dirtyRegion = tr.transform(dirtyRegion);
604
605 // At this point, the dirty region is in screen space.
606 // Make sure it's constrained by the visible region (which
607 // is in screen space as well).
608 dirtyRegion.andSelf(visibleRegionScreen);
609 outDirtyRegion.orSelf(dirtyRegion);
610
611 // client could be blocked, so signal them so they get a
612 // chance to reevaluate their condition.
613 mFlinger->scheduleBroadcast(client);
614 }
615}
616
617void Layer::finishPageFlip()
618{
619 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
620 LOGE_IF(!(lcblk->swapState & eBusy),
621 "layer %p wasn't locked!", this);
622 android_atomic_and(~eBusy, &(lcblk->swapState));
623 }
624 mFlinger->scheduleBroadcast(client);
625}
626
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627// ---------------------------------------------------------------------------
628
629Layer::SurfaceLayer::SurfaceLayer(SurfaceID id, const sp<Layer>& owner)
630 : Surface(id, owner->getIdentity(), owner)
631{
632}
633
634sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
635{
636 sp<SurfaceBuffer> buffer = 0;
637 sp<Layer> owner(getOwner());
638 if (owner != 0) {
639 buffer = owner->peekBuffer();
640 }
641 return buffer;
642}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
644// ---------------------------------------------------------------------------
645
646
647}; // namespace android