blob: 5fdec3f64e3a3cda19ff6c14c6a7926e589a0b96 [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
Mathias Agopian9a112062009-04-17 19:36:26 -070090status_t Layer::ditch()
91{
92 mSurface.clear();
93 return NO_ERROR;
94}
95
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096status_t Layer::setBuffers( Client* client,
97 uint32_t w, uint32_t h,
98 PixelFormat format, uint32_t flags)
99{
100 PixelFormatInfo info;
101 status_t err = getPixelFormatInfo(format, &info);
102 if (err) return err;
103
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104 uint32_t bufferFlags = 0;
105 if (flags & ISurfaceComposer::eGPU)
106 bufferFlags |= Buffer::GPU;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108 if (flags & ISurfaceComposer::eSecure)
109 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111
112 if (bufferFlags & Buffer::GPU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 // FIXME: this is msm7201A specific, as its GPU only supports
114 // BGRA_8888.
115 if (format == PIXEL_FORMAT_RGBA_8888) {
116 format = PIXEL_FORMAT_BGRA_8888;
117 }
118 }
119
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700123 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
124 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700126 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700128 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 return NO_ERROR;
130}
131
132void Layer::reloadTexture(const Region& dirty)
133{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 const sp<const Buffer>& buffer(frontBuffer().getBuffer());
135 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
136 int index = mFrontBufferIndex;
137 if (LIKELY(!mTextures[index].dirty)) {
138 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
139 } else {
140 // we need to recreate the texture
141 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
142
143 // create the new texture name if needed
144 if (UNLIKELY(mTextures[index].name == -1U)) {
145 mTextures[index].name = createTexture();
146 } else {
147 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
148 }
149
150 // free the previous image
151 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
152 eglDestroyImageKHR(dpy, mTextures[index].image);
153 mTextures[index].image = EGL_NO_IMAGE_KHR;
154 }
155
156 // construct an EGL_NATIVE_BUFFER_ANDROID
157 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
158
159 // create the new EGLImageKHR
160 const EGLint attrs[] = {
161 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
162 EGL_NONE, EGL_NONE
163 };
164 mTextures[index].image = eglCreateImageKHR(
165 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
166 (EGLClientBuffer)clientBuf, attrs);
167
168 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
169 "eglCreateImageKHR() failed. err=0x%4x",
170 eglGetError());
171
172 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
173 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
174 (GLeglImageOES)mTextures[index].image);
175 GLint error = glGetError();
176 if (UNLIKELY(error != GL_NO_ERROR)) {
177 // this failed, for instance, because we don't support
178 // NPOT.
179 // FIXME: do something!
180 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
181 } else {
182 // Everything went okay!
183 mTextures[index].dirty = false;
184 }
185 }
186 }
187 } else {
188 GGLSurface t;
189 if (LIKELY(buffer->getBitmapSurface(&t) == NO_ERROR)) {
190 if (UNLIKELY(mTextures[0].name == -1U)) {
191 mTextures[0].name = createTexture();
192 }
193 loadTexture(dirty, mTextures[0].name, t,
194 mTextures[0].width, mTextures[0].height);
195 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
199
200void Layer::onDraw(const Region& clip) const
201{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
203 mFrontBufferIndex : 0;
204 GLuint textureName = mTextures[index].name;
205
206 if (UNLIKELY(textureName == -1LU)) {
207 LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 // the texture has not been created yet, this Layer has
209 // in fact never been drawn into. this happens frequently with
210 // SurfaceView.
211 clearWithOpenGL(clip);
212 return;
213 }
214
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 GGLSurface t;
216 sp<const Buffer> buffer(frontBuffer().getBuffer());
217 buffer->getBitmapSurface(&t);
218 drawWithOpenGL(clip, textureName, t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219}
220
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223 /*
224 * This is called from the client's Surface::lock(), after it locked
225 * the surface successfully. We're therefore guaranteed that the
226 * back-buffer is not in use by ourselves.
227 * Of course, we need to validate all this, which is not trivial.
228 *
229 * FIXME: A resize could happen at any time here. What to do about this?
230 * - resize() form post()
231 * - resize() from doTransaction()
232 *
233 * We'll probably need an internal lock for this.
234 *
235 *
236 * TODO: We need to make sure that post() doesn't swap
237 * the buffers under us.
238 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 // it's okay to read swapState for the purpose of figuring out the
241 // backbuffer index, which cannot change (since the app has locked it).
242 const uint32_t state = lcblk->swapState;
243 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
244
245 // get rid of the EGL image, since we shouldn't need it anymore
246 // (note that we're in a different thread than where it is being used)
247 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
248 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
249 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
250 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252
253 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
254 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
255
256 LOGD_IF(DEBUG_RESIZE,
257 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
258 this, backBufferIndex,
259 layerBitmap.getWidth(),
260 layerBitmap.getHeight(),
261 layerBitmap.getBuffer()->getWidth(),
262 layerBitmap.getBuffer()->getHeight());
263
264 if (UNLIKELY(buffer == 0)) {
265 // XXX: what to do, what to do?
266 } else {
267 // texture is now dirty...
268 mTextures[backBufferIndex].dirty = true;
269 // ... so it the visible region (because we consider the surface's
270 // buffer size for visibility calculations)
271 forceVisibilityTransaction();
272 mFlinger->setTransactionFlags(eTraversalNeeded);
273 }
274 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275}
276
277uint32_t Layer::doTransaction(uint32_t flags)
278{
279 const Layer::State& front(drawingState());
280 const Layer::State& temp(currentState());
281
282 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
283 // that the size changed back to its previous value before the buffer
284 // was resized (in the eLocked case below), in which case, we still
285 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
288 /*
289 * Various states we could be in...
290
291 resize = state & eResizeRequested;
292 if (backbufferChanged) {
293 if (resize == 0) {
294 // ERROR, the resized buffer doesn't have its resize flag set
295 } else if (resize == mask) {
296 // ERROR one of the buffer has already been resized
297 } else if (resize == mask ^ eResizeRequested) {
298 // ERROR, the resized buffer doesn't have its resize flag set
299 } else if (resize == eResizeRequested) {
300 // OK, Normal case, proceed with resize
301 }
302 } else {
303 if (resize == 0) {
304 // OK, nothing special, do nothing
305 } else if (resize == mask) {
306 // restarted transaction, do nothing
307 } else if (resize == mask ^ eResizeRequested) {
308 // restarted transaction, do nothing
309 } else if (resize == eResizeRequested) {
310 // OK, size reset to previous value, proceed with resize
311 }
312 }
313 */
314
315 // Index of the back buffer
316 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
317 const uint32_t state = lcblk->swapState;
318 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
319 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
320 uint32_t resizeFlags = state & eResizeRequested;
321
322 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
323 LOGE( "backbuffer size changed, but both resize flags are not set! "
324 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
325 "index=%d, (%dx%d), (%dx%d)",
326 this, state,
327 int(temp.w), int(temp.h),
328 int(drawingState().w), int(drawingState().h),
329 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700330 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
331 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 // if we get there we're pretty screwed. the only reasonable
333 // thing to do is to pretend we should do the resize since
334 // backbufferChanged is set (this also will give a chance to
335 // client to get unblocked)
336 resizeFlags = eResizeRequested;
337 }
338
339 if (resizeFlags == eResizeRequested) {
340 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
341 // here, would be wrong and misleading because by this point
342 // mFrontBufferIndex has not been updated yet.
343
344 LOGD_IF(DEBUG_RESIZE,
345 "resize (layer=%p), state=%08x, "
346 "requested (%dx%d), "
347 "drawing (%d,%d), "
348 "index=%d, (%dx%d), (%dx%d)",
349 this, state,
350 int(temp.w), int(temp.h),
351 int(drawingState().w), int(drawingState().h),
352 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700353 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
354 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
356 if (state & eLocked) {
357 // if the buffer is locked, we can't resize anything because
358 // - the backbuffer is currently in use by the user
359 // - the front buffer is being shown
360 // We just act as if the transaction didn't happen and we
361 // reschedule it later...
362 flags |= eRestartTransaction;
363 } else {
364 // This buffer needs to be resized
365 status_t err =
366 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
367 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700368 const uint32_t mask = clientBackBufferIndex ?
369 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370 android_atomic_and(~mask, &(lcblk->swapState));
371 // since a buffer became available, we can let the client go...
372 mFlinger->scheduleBroadcast(client);
373 mResizeTransactionDone = true;
374
375 // we're being resized and there is a freeze display request,
376 // acquire a freeze lock, so that the screen stays put
377 // until we've redrawn at the new size; this is to avoid
378 // glitches upon orientation changes.
379 if (mFlinger->hasFreezeRequest()) {
380 // if the surface is hidden, don't try to acquire the
381 // freeze lock, since hidden surfaces may never redraw
382 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
383 mFreezeLock = mFlinger->getFreezeLock();
384 }
385 }
386 }
387 }
388 }
389
390 if (temp.sequence != front.sequence) {
391 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
392 // this surface is now hidden, so it shouldn't hold a freeze lock
393 // (it may never redraw, which is fine if it is hidden)
394 mFreezeLock.clear();
395 }
396 }
397
398 return LayerBase::doTransaction(flags);
399}
400
401status_t Layer::resize(
402 int32_t clientBackBufferIndex,
403 uint32_t width, uint32_t height,
404 const char* what)
405{
406 /*
407 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700408 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 */
410
411 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
412
413 // if the new (transaction) size is != from the the backbuffer
414 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700415 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
416 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417
418 LOGD_IF(!backbufferChanged,
419 "(%s) eResizeRequested (layer=%p), but size not changed: "
420 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
421 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
422 what, this,
423 int(width), int(height),
424 int(drawingState().w), int(drawingState().h),
425 int(currentState().w), int(currentState().h),
426 long(lcblk->swapState),
427 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
429 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430
431 // this can happen when changing the size back and forth quickly
432 status_t err = NO_ERROR;
433 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700434
435 LOGD_IF(DEBUG_RESIZE,
436 "resize (layer=%p), requested (%dx%d), "
437 "index=%d, (%dx%d), (%dx%d)",
438 this, int(width), int(height), int(clientBackBufferIndex),
439 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
440 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
441
442 err = mBuffers[clientBackBufferIndex].setSize(width, height);
443 if (UNLIKELY(err != NO_ERROR)) {
444 // This really should never happen
445 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
446 clientBackBufferIndex, width, height, err, strerror(err));
447 // couldn't reallocate the surface
448 android_atomic_write(eInvalidSurface, &lcblk->swapState);
449 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 }
451 return err;
452}
453
454void Layer::setSizeChanged(uint32_t w, uint32_t h)
455{
456 LOGD_IF(DEBUG_RESIZE,
457 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
458 w, h, mCurrentState.w, mCurrentState.h);
459 android_atomic_or(eResizeRequested, &(lcblk->swapState));
460}
461
462// ----------------------------------------------------------------------------
463// pageflip handling...
464// ----------------------------------------------------------------------------
465
466void Layer::lockPageFlip(bool& recomputeVisibleRegions)
467{
468 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
469 // preemptively block the client, because he might set
470 // eFlipRequested at any time and want to use this buffer
471 // for the next frame. This will be unset below if it
472 // turns out we didn't need it.
473
474 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
475 if (!(state & mask))
476 return;
477
478 if (UNLIKELY(state & eInvalidSurface)) {
479 // if eInvalidSurface is set, this means the surface
480 // became invalid during a transaction (NO_MEMORY for instance)
481 mFlinger->scheduleBroadcast(client);
482 return;
483 }
484
485 if (UNLIKELY(state & eFlipRequested)) {
486 uint32_t oldState;
487 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
488 if (oldState & eNextFlipPending) {
489 // Process another round (we know at least a buffer
490 // is ready for that client).
491 mFlinger->signalEvent();
492 }
493 }
494}
495
496Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
497{
498 // atomically swap buffers and (re)set eFlipRequested
499 int32_t oldValue, newValue;
500 layer_cblk_t * const lcblk = this->lcblk;
501 do {
502 oldValue = lcblk->swapState;
503 // get the current value
504
505 LOG_ASSERT(oldValue&eFlipRequested,
506 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
507 long(oldValue));
508
509 newValue = (oldValue ^ eIndex);
510 // swap buffers
511
512 newValue &= ~(eFlipRequested | eNextFlipPending);
513 // clear eFlipRequested and eNextFlipPending
514
515 if (oldValue & eNextFlipPending)
516 newValue |= eFlipRequested;
517 // if eNextFlipPending is set (second buffer already has something
518 // in it) we need to reset eFlipRequested because the client
519 // might never do it
520
521 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
522 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700523
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 const int32_t index = (newValue & eIndex) ^ 1;
525 mFrontBufferIndex = index;
526
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700527 /* NOTE: it's safe to set this flag here because this is only touched
528 * from LayerBitmap::allocate(), which by construction cannot happen
529 * while we're in post().
530 */
531 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 // ... post the new front-buffer
534 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700535 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 // oldValue, newValue, mFrontBufferIndex);
539 //dirty.dump("dirty");
540
541 if (UNLIKELY(oldValue & eResizeRequested)) {
542
543 LOGD_IF(DEBUG_RESIZE,
544 "post (layer=%p), state=%08x, "
545 "index=%d, (%dx%d), (%dx%d)",
546 this, newValue,
547 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
549 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550
551 // here, we just posted the surface and we have resolved
552 // the front/back buffer indices. The client is blocked, so
553 // it cannot start using the new backbuffer.
554
555 // If the backbuffer was resized in THIS round, we actually cannot
556 // resize the frontbuffer because it has *just* been drawn (and we
557 // would have nothing to draw). In this case we just skip the resize
558 // it'll happen after the next page flip or during the next
559 // transaction.
560
561 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
562 if (mResizeTransactionDone && (newValue & mask)) {
563 // Resize the layer's second buffer only if the transaction
564 // happened. It may not have happened yet if eResizeRequested
565 // was set immediately after the "transactionRequested" test,
566 // in which case the drawing state's size would be wrong.
567 mFreezeLock.clear();
568 const Layer::State& s(drawingState());
569 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
570 do {
571 oldValue = lcblk->swapState;
572 if ((oldValue & eResizeRequested) == eResizeRequested) {
573 // ugh, another resize was requested since we processed
574 // the first buffer, don't free the client, and let
575 // the next transaction handle everything.
576 break;
577 }
578 newValue = oldValue & ~mask;
579 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
580 }
581 mResizeTransactionDone = false;
582 recomputeVisibleRegions = true;
583 this->contentDirty = true;
584 }
585 }
586
587 reloadTexture(dirty);
588
589 return dirty;
590}
591
592Point Layer::getPhysicalSize() const
593{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700594 sp<const Buffer> front(frontBuffer().getBuffer());
595 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596}
597
598void Layer::unlockPageFlip(
599 const Transform& planeTransform, Region& outDirtyRegion)
600{
601 Region dirtyRegion(mPostedDirtyRegion);
602 if (!dirtyRegion.isEmpty()) {
603 mPostedDirtyRegion.clear();
604 // The dirty region is given in the layer's coordinate space
605 // transform the dirty region by the surface's transformation
606 // and the global transformation.
607 const Layer::State& s(drawingState());
608 const Transform tr(planeTransform * s.transform);
609 dirtyRegion = tr.transform(dirtyRegion);
610
611 // At this point, the dirty region is in screen space.
612 // Make sure it's constrained by the visible region (which
613 // is in screen space as well).
614 dirtyRegion.andSelf(visibleRegionScreen);
615 outDirtyRegion.orSelf(dirtyRegion);
616
617 // client could be blocked, so signal them so they get a
618 // chance to reevaluate their condition.
619 mFlinger->scheduleBroadcast(client);
620 }
621}
622
623void Layer::finishPageFlip()
624{
625 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
626 LOGE_IF(!(lcblk->swapState & eBusy),
627 "layer %p wasn't locked!", this);
628 android_atomic_and(~eBusy, &(lcblk->swapState));
629 }
630 mFlinger->scheduleBroadcast(client);
631}
632
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700633// ---------------------------------------------------------------------------
634
Mathias Agopian9a112062009-04-17 19:36:26 -0700635Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
636 SurfaceID id, const sp<Layer>& owner)
637 : Surface(flinger, id, owner->getIdentity(), owner)
638{
639}
640
641Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700642{
643}
644
645sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
646{
647 sp<SurfaceBuffer> buffer = 0;
648 sp<Layer> owner(getOwner());
649 if (owner != 0) {
650 buffer = owner->peekBuffer();
651 }
652 return buffer;
653}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654
655// ---------------------------------------------------------------------------
656
657
658}; // namespace android