blob: 991615800da45d674265807c58199c33675ad880 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
28#include <ui/PixelFormat.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <ui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include "clz.h"
32#include "Layer.h"
33#include "LayerBitmap.h"
34#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "DisplayHardware/DisplayHardware.h"
36
37
38#define DEBUG_RESIZE 0
39
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
45const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
46const char* const Layer::typeID = "Layer";
47
48// ---------------------------------------------------------------------------
49
Mathias Agopianf9d93272009-06-19 17:00:27 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display, const sp<Client>& c, int32_t i)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 : LayerBaseClient(flinger, display, c, i),
52 mSecure(false),
53 mFrontBufferIndex(1),
54 mNeedsBlending(true),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055 mResizeTransactionDone(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056{
57 // no OpenGL operation is possible here, since we might not be
58 // in the OpenGL thread.
59}
60
61Layer::~Layer()
62{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070063 destroy();
64 // the actual buffers will be destroyed here
65}
66
67void Layer::destroy()
68{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 for (int i=0 ; i<NUM_BUFFERS ; i++) {
70 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070071 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070072 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 }
74 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
75 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
76 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070077 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 }
Mathias Agopian759fdb22009-07-02 17:33:40 -070079 mBuffers[i].free();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 }
Mathias Agopian759fdb22009-07-02 17:33:40 -070081 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082}
83
84void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags)
85{
86 LayerBase::initStates(w,h,flags);
87
88 if (flags & ISurfaceComposer::eDestroyBackbuffer)
89 lcblk->flags |= eNoCopyBack;
90}
91
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093{
94 return mSurface;
95}
96
Mathias Agopian9a112062009-04-17 19:36:26 -070097status_t Layer::ditch()
98{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070099 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700100 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -0700101 return NO_ERROR;
102}
103
Mathias Agopianf9d93272009-06-19 17:00:27 -0700104status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 PixelFormat format, uint32_t flags)
106{
107 PixelFormatInfo info;
108 status_t err = getPixelFormatInfo(format, &info);
109 if (err) return err;
110
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111 uint32_t bufferFlags = 0;
112 if (flags & ISurfaceComposer::eGPU)
113 bufferFlags |= Buffer::GPU;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700115 if (flags & ISurfaceComposer::eSecure)
116 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700118 /* FIXME we need this code for msm7201A
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119 if (bufferFlags & Buffer::GPU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 // FIXME: this is msm7201A specific, as its GPU only supports
121 // BGRA_8888.
122 if (format == PIXEL_FORMAT_RGBA_8888) {
123 format = PIXEL_FORMAT_BGRA_8888;
124 }
125 }
Mathias Agopian7be3e5d2009-04-30 14:43:18 -0700126 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700128 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
132 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700136 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 return NO_ERROR;
138}
139
140void Layer::reloadTexture(const Region& dirty)
141{
Mathias Agopian0926f502009-05-04 14:17:04 -0700142 const sp<Buffer>& buffer(frontBuffer().getBuffer());
143 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700144 int index = mFrontBufferIndex;
145 if (LIKELY(!mTextures[index].dirty)) {
146 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
147 } else {
148 // we need to recreate the texture
149 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
150
151 // create the new texture name if needed
152 if (UNLIKELY(mTextures[index].name == -1U)) {
153 mTextures[index].name = createTexture();
154 } else {
155 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
156 }
157
158 // free the previous image
159 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
160 eglDestroyImageKHR(dpy, mTextures[index].image);
161 mTextures[index].image = EGL_NO_IMAGE_KHR;
162 }
163
164 // construct an EGL_NATIVE_BUFFER_ANDROID
165 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
166
167 // create the new EGLImageKHR
168 const EGLint attrs[] = {
169 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
170 EGL_NONE, EGL_NONE
171 };
172 mTextures[index].image = eglCreateImageKHR(
173 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
174 (EGLClientBuffer)clientBuf, attrs);
175
176 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
177 "eglCreateImageKHR() failed. err=0x%4x",
178 eglGetError());
179
180 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
181 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
182 (GLeglImageOES)mTextures[index].image);
183 GLint error = glGetError();
184 if (UNLIKELY(error != GL_NO_ERROR)) {
185 // this failed, for instance, because we don't support
186 // NPOT.
187 // FIXME: do something!
188 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
189 } else {
190 // Everything went okay!
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700191 mTextures[index].dirty = false;
192 mTextures[index].width = clientBuf->width;
193 mTextures[index].height = clientBuf->height;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 }
195 }
196 }
197 } else {
198 GGLSurface t;
Mathias Agopian0926f502009-05-04 14:17:04 -0700199 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY);
200 LOGE_IF(res, "error %d (%s) locking buffer %p",
201 res, strerror(res), buffer.get());
202 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203 if (UNLIKELY(mTextures[0].name == -1U)) {
204 mTextures[0].name = createTexture();
205 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700206 loadTexture(&mTextures[0], mTextures[0].name, dirty, t);
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 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700227 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228}
229
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232 /*
233 * This is called from the client's Surface::lock(), after it locked
234 * the surface successfully. We're therefore guaranteed that the
235 * back-buffer is not in use by ourselves.
236 * Of course, we need to validate all this, which is not trivial.
237 *
238 * FIXME: A resize could happen at any time here. What to do about this?
239 * - resize() form post()
240 * - resize() from doTransaction()
241 *
242 * We'll probably need an internal lock for this.
243 *
244 *
245 * TODO: We need to make sure that post() doesn't swap
246 * the buffers under us.
247 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 // it's okay to read swapState for the purpose of figuring out the
250 // backbuffer index, which cannot change (since the app has locked it).
251 const uint32_t state = lcblk->swapState;
252 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
253
254 // get rid of the EGL image, since we shouldn't need it anymore
255 // (note that we're in a different thread than where it is being used)
256 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
257 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
258 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
259 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700261
262 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
263 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
264
265 LOGD_IF(DEBUG_RESIZE,
266 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
267 this, backBufferIndex,
268 layerBitmap.getWidth(),
269 layerBitmap.getHeight(),
270 layerBitmap.getBuffer()->getWidth(),
271 layerBitmap.getBuffer()->getHeight());
272
273 if (UNLIKELY(buffer == 0)) {
274 // XXX: what to do, what to do?
275 } else {
276 // texture is now dirty...
277 mTextures[backBufferIndex].dirty = true;
278 // ... so it the visible region (because we consider the surface's
279 // buffer size for visibility calculations)
280 forceVisibilityTransaction();
281 mFlinger->setTransactionFlags(eTraversalNeeded);
282 }
283 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284}
285
Mathias Agopianf9d93272009-06-19 17:00:27 -0700286void Layer::scheduleBroadcast()
287{
288 sp<Client> ourClient(client.promote());
289 if (ourClient != 0) {
290 mFlinger->scheduleBroadcast(ourClient);
291 }
292}
293
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294uint32_t Layer::doTransaction(uint32_t flags)
295{
296 const Layer::State& front(drawingState());
297 const Layer::State& temp(currentState());
298
299 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
300 // that the size changed back to its previous value before the buffer
301 // was resized (in the eLocked case below), in which case, we still
302 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304
305 /*
306 * Various states we could be in...
307
308 resize = state & eResizeRequested;
309 if (backbufferChanged) {
310 if (resize == 0) {
311 // ERROR, the resized buffer doesn't have its resize flag set
312 } else if (resize == mask) {
313 // ERROR one of the buffer has already been resized
314 } else if (resize == mask ^ eResizeRequested) {
315 // ERROR, the resized buffer doesn't have its resize flag set
316 } else if (resize == eResizeRequested) {
317 // OK, Normal case, proceed with resize
318 }
319 } else {
320 if (resize == 0) {
321 // OK, nothing special, do nothing
322 } else if (resize == mask) {
323 // restarted transaction, do nothing
324 } else if (resize == mask ^ eResizeRequested) {
325 // restarted transaction, do nothing
326 } else if (resize == eResizeRequested) {
327 // OK, size reset to previous value, proceed with resize
328 }
329 }
330 */
331
332 // Index of the back buffer
333 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
334 const uint32_t state = lcblk->swapState;
335 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
336 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
337 uint32_t resizeFlags = state & eResizeRequested;
338
339 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
340 LOGE( "backbuffer size changed, but both resize flags are not set! "
341 "(layer=%p), state=%08x, requested (%dx%d), 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 // if we get there we're pretty screwed. the only reasonable
350 // thing to do is to pretend we should do the resize since
351 // backbufferChanged is set (this also will give a chance to
352 // client to get unblocked)
353 resizeFlags = eResizeRequested;
354 }
355
356 if (resizeFlags == eResizeRequested) {
357 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
358 // here, would be wrong and misleading because by this point
359 // mFrontBufferIndex has not been updated yet.
360
361 LOGD_IF(DEBUG_RESIZE,
362 "resize (layer=%p), state=%08x, "
363 "requested (%dx%d), "
364 "drawing (%d,%d), "
365 "index=%d, (%dx%d), (%dx%d)",
366 this, state,
367 int(temp.w), int(temp.h),
368 int(drawingState().w), int(drawingState().h),
369 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700370 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
371 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372
373 if (state & eLocked) {
374 // if the buffer is locked, we can't resize anything because
375 // - the backbuffer is currently in use by the user
376 // - the front buffer is being shown
377 // We just act as if the transaction didn't happen and we
378 // reschedule it later...
379 flags |= eRestartTransaction;
380 } else {
381 // This buffer needs to be resized
382 status_t err =
383 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
384 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700385 const uint32_t mask = clientBackBufferIndex ?
386 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 android_atomic_and(~mask, &(lcblk->swapState));
388 // since a buffer became available, we can let the client go...
Mathias Agopianf9d93272009-06-19 17:00:27 -0700389 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 mResizeTransactionDone = true;
391
392 // we're being resized and there is a freeze display request,
393 // acquire a freeze lock, so that the screen stays put
394 // until we've redrawn at the new size; this is to avoid
395 // glitches upon orientation changes.
396 if (mFlinger->hasFreezeRequest()) {
397 // if the surface is hidden, don't try to acquire the
398 // freeze lock, since hidden surfaces may never redraw
399 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
400 mFreezeLock = mFlinger->getFreezeLock();
401 }
402 }
403 }
404 }
405 }
406
407 if (temp.sequence != front.sequence) {
408 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
409 // this surface is now hidden, so it shouldn't hold a freeze lock
410 // (it may never redraw, which is fine if it is hidden)
411 mFreezeLock.clear();
412 }
413 }
414
415 return LayerBase::doTransaction(flags);
416}
417
418status_t Layer::resize(
419 int32_t clientBackBufferIndex,
420 uint32_t width, uint32_t height,
421 const char* what)
422{
423 /*
424 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700425 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 */
427
428 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
429
430 // if the new (transaction) size is != from the the backbuffer
431 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700432 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
433 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434
435 LOGD_IF(!backbufferChanged,
436 "(%s) eResizeRequested (layer=%p), but size not changed: "
437 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
438 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
439 what, this,
440 int(width), int(height),
441 int(drawingState().w), int(drawingState().h),
442 int(currentState().w), int(currentState().h),
443 long(lcblk->swapState),
444 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700445 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
446 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447
448 // this can happen when changing the size back and forth quickly
449 status_t err = NO_ERROR;
450 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700451
452 LOGD_IF(DEBUG_RESIZE,
453 "resize (layer=%p), requested (%dx%d), "
454 "index=%d, (%dx%d), (%dx%d)",
455 this, int(width), int(height), int(clientBackBufferIndex),
456 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
457 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
458
459 err = mBuffers[clientBackBufferIndex].setSize(width, height);
460 if (UNLIKELY(err != NO_ERROR)) {
461 // This really should never happen
462 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
463 clientBackBufferIndex, width, height, err, strerror(err));
464 // couldn't reallocate the surface
465 android_atomic_write(eInvalidSurface, &lcblk->swapState);
466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 }
468 return err;
469}
470
471void Layer::setSizeChanged(uint32_t w, uint32_t h)
472{
473 LOGD_IF(DEBUG_RESIZE,
474 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
475 w, h, mCurrentState.w, mCurrentState.h);
476 android_atomic_or(eResizeRequested, &(lcblk->swapState));
477}
478
479// ----------------------------------------------------------------------------
480// pageflip handling...
481// ----------------------------------------------------------------------------
482
483void Layer::lockPageFlip(bool& recomputeVisibleRegions)
484{
485 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
486 // preemptively block the client, because he might set
487 // eFlipRequested at any time and want to use this buffer
488 // for the next frame. This will be unset below if it
489 // turns out we didn't need it.
490
491 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
492 if (!(state & mask))
493 return;
494
495 if (UNLIKELY(state & eInvalidSurface)) {
496 // if eInvalidSurface is set, this means the surface
497 // became invalid during a transaction (NO_MEMORY for instance)
Mathias Agopianf9d93272009-06-19 17:00:27 -0700498 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 return;
500 }
501
502 if (UNLIKELY(state & eFlipRequested)) {
503 uint32_t oldState;
504 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
505 if (oldState & eNextFlipPending) {
506 // Process another round (we know at least a buffer
507 // is ready for that client).
508 mFlinger->signalEvent();
509 }
510 }
511}
512
513Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
514{
515 // atomically swap buffers and (re)set eFlipRequested
516 int32_t oldValue, newValue;
517 layer_cblk_t * const lcblk = this->lcblk;
518 do {
519 oldValue = lcblk->swapState;
520 // get the current value
521
522 LOG_ASSERT(oldValue&eFlipRequested,
523 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
524 long(oldValue));
525
526 newValue = (oldValue ^ eIndex);
527 // swap buffers
528
529 newValue &= ~(eFlipRequested | eNextFlipPending);
530 // clear eFlipRequested and eNextFlipPending
531
532 if (oldValue & eNextFlipPending)
533 newValue |= eFlipRequested;
534 // if eNextFlipPending is set (second buffer already has something
535 // in it) we need to reset eFlipRequested because the client
536 // might never do it
537
538 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
539 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 const int32_t index = (newValue & eIndex) ^ 1;
542 mFrontBufferIndex = index;
543
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700544 /* NOTE: it's safe to set this flag here because this is only touched
545 * from LayerBitmap::allocate(), which by construction cannot happen
546 * while we're in post().
547 */
548 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
549
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 // ... post the new front-buffer
551 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 // oldValue, newValue, mFrontBufferIndex);
556 //dirty.dump("dirty");
557
558 if (UNLIKELY(oldValue & eResizeRequested)) {
559
560 LOGD_IF(DEBUG_RESIZE,
561 "post (layer=%p), state=%08x, "
562 "index=%d, (%dx%d), (%dx%d)",
563 this, newValue,
564 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
566 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567
568 // here, we just posted the surface and we have resolved
569 // the front/back buffer indices. The client is blocked, so
570 // it cannot start using the new backbuffer.
571
572 // If the backbuffer was resized in THIS round, we actually cannot
573 // resize the frontbuffer because it has *just* been drawn (and we
574 // would have nothing to draw). In this case we just skip the resize
575 // it'll happen after the next page flip or during the next
576 // transaction.
577
578 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
579 if (mResizeTransactionDone && (newValue & mask)) {
580 // Resize the layer's second buffer only if the transaction
581 // happened. It may not have happened yet if eResizeRequested
582 // was set immediately after the "transactionRequested" test,
583 // in which case the drawing state's size would be wrong.
584 mFreezeLock.clear();
585 const Layer::State& s(drawingState());
586 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
587 do {
588 oldValue = lcblk->swapState;
589 if ((oldValue & eResizeRequested) == eResizeRequested) {
590 // ugh, another resize was requested since we processed
591 // the first buffer, don't free the client, and let
592 // the next transaction handle everything.
593 break;
594 }
595 newValue = oldValue & ~mask;
596 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
597 }
598 mResizeTransactionDone = false;
599 recomputeVisibleRegions = true;
600 this->contentDirty = true;
601 }
602 }
603
604 reloadTexture(dirty);
605
606 return dirty;
607}
608
609Point Layer::getPhysicalSize() const
610{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611 sp<const Buffer> front(frontBuffer().getBuffer());
612 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613}
614
615void Layer::unlockPageFlip(
616 const Transform& planeTransform, Region& outDirtyRegion)
617{
618 Region dirtyRegion(mPostedDirtyRegion);
619 if (!dirtyRegion.isEmpty()) {
620 mPostedDirtyRegion.clear();
621 // The dirty region is given in the layer's coordinate space
622 // transform the dirty region by the surface's transformation
623 // and the global transformation.
624 const Layer::State& s(drawingState());
625 const Transform tr(planeTransform * s.transform);
626 dirtyRegion = tr.transform(dirtyRegion);
627
628 // At this point, the dirty region is in screen space.
629 // Make sure it's constrained by the visible region (which
630 // is in screen space as well).
631 dirtyRegion.andSelf(visibleRegionScreen);
632 outDirtyRegion.orSelf(dirtyRegion);
633
634 // client could be blocked, so signal them so they get a
635 // chance to reevaluate their condition.
Mathias Agopianf9d93272009-06-19 17:00:27 -0700636 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638}
639
640void Layer::finishPageFlip()
641{
642 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
643 LOGE_IF(!(lcblk->swapState & eBusy),
644 "layer %p wasn't locked!", this);
645 android_atomic_and(~eBusy, &(lcblk->swapState));
646 }
Mathias Agopianf9d93272009-06-19 17:00:27 -0700647 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648}
649
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700650// ---------------------------------------------------------------------------
651
Mathias Agopian9a112062009-04-17 19:36:26 -0700652Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
653 SurfaceID id, const sp<Layer>& owner)
654 : Surface(flinger, id, owner->getIdentity(), owner)
655{
656}
657
658Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659{
660}
661
662sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
663{
664 sp<SurfaceBuffer> buffer = 0;
665 sp<Layer> owner(getOwner());
666 if (owner != 0) {
667 buffer = owner->peekBuffer();
668 }
669 return buffer;
670}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671
672// ---------------------------------------------------------------------------
673
674
675}; // namespace android