blob: d1142cca86da68b4a2e106369cd958b1d21e05a6 [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 Agopian076b1cc2009-04-10 14:24:30 -0700118 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 for (int i=0 ; i<2 ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags);
122 if (err != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700124 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700126 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 return NO_ERROR;
128}
129
130void Layer::reloadTexture(const Region& dirty)
131{
Mathias Agopian0926f502009-05-04 14:17:04 -0700132 const sp<Buffer>& buffer(frontBuffer().getBuffer());
133 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 int index = mFrontBufferIndex;
135 if (LIKELY(!mTextures[index].dirty)) {
136 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
137 } else {
138 // we need to recreate the texture
139 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
140
141 // create the new texture name if needed
142 if (UNLIKELY(mTextures[index].name == -1U)) {
143 mTextures[index].name = createTexture();
144 } else {
145 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
146 }
147
148 // free the previous image
149 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
150 eglDestroyImageKHR(dpy, mTextures[index].image);
151 mTextures[index].image = EGL_NO_IMAGE_KHR;
152 }
153
154 // construct an EGL_NATIVE_BUFFER_ANDROID
155 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
156
157 // create the new EGLImageKHR
158 const EGLint attrs[] = {
159 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
160 EGL_NONE, EGL_NONE
161 };
162 mTextures[index].image = eglCreateImageKHR(
163 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
164 (EGLClientBuffer)clientBuf, attrs);
165
166 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
167 "eglCreateImageKHR() failed. err=0x%4x",
168 eglGetError());
169
170 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
171 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
172 (GLeglImageOES)mTextures[index].image);
173 GLint error = glGetError();
174 if (UNLIKELY(error != GL_NO_ERROR)) {
175 // this failed, for instance, because we don't support
176 // NPOT.
177 // FIXME: do something!
178 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
179 } else {
180 // Everything went okay!
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700181 mTextures[index].dirty = false;
182 mTextures[index].width = clientBuf->width;
183 mTextures[index].height = clientBuf->height;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184 }
185 }
186 }
187 } else {
188 GGLSurface t;
Mathias Agopian0926f502009-05-04 14:17:04 -0700189 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY);
190 LOGE_IF(res, "error %d (%s) locking buffer %p",
191 res, strerror(res), buffer.get());
192 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 if (UNLIKELY(mTextures[0].name == -1U)) {
194 mTextures[0].name = createTexture();
195 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700196 loadTexture(&mTextures[0], mTextures[0].name, dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700197 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700198 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
202
203void Layer::onDraw(const Region& clip) const
204{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
206 mFrontBufferIndex : 0;
207 GLuint textureName = mTextures[index].name;
208
209 if (UNLIKELY(textureName == -1LU)) {
210 LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 // the texture has not been created yet, this Layer has
212 // in fact never been drawn into. this happens frequently with
213 // SurfaceView.
214 clearWithOpenGL(clip);
215 return;
216 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700217 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
219
Fred Quintanab2fd4662009-08-11 20:49:35 -0700220sp<SurfaceBuffer> Layer::peekBuffer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222 /*
223 * This is called from the client's Surface::lock(), after it locked
224 * the surface successfully. We're therefore guaranteed that the
225 * back-buffer is not in use by ourselves.
226 * Of course, we need to validate all this, which is not trivial.
227 *
228 * FIXME: A resize could happen at any time here. What to do about this?
229 * - resize() form post()
230 * - resize() from doTransaction()
231 *
232 * We'll probably need an internal lock for this.
233 *
234 *
235 * TODO: We need to make sure that post() doesn't swap
236 * the buffers under us.
237 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700239 // it's okay to read swapState for the purpose of figuring out the
240 // backbuffer index, which cannot change (since the app has locked it).
241 const uint32_t state = lcblk->swapState;
242 const int32_t backBufferIndex = layer_cblk_t::backBuffer(state);
243
244 // get rid of the EGL image, since we shouldn't need it anymore
245 // (note that we're in a different thread than where it is being used)
246 if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) {
247 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
248 eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image);
249 mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251
252 LayerBitmap& layerBitmap(mBuffers[backBufferIndex]);
Fred Quintanab2fd4662009-08-11 20:49:35 -0700253 sp<SurfaceBuffer> buffer = layerBitmap.allocate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254
255 LOGD_IF(DEBUG_RESIZE,
256 "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)",
257 this, backBufferIndex,
258 layerBitmap.getWidth(),
259 layerBitmap.getHeight(),
260 layerBitmap.getBuffer()->getWidth(),
261 layerBitmap.getBuffer()->getHeight());
262
263 if (UNLIKELY(buffer == 0)) {
264 // XXX: what to do, what to do?
265 } else {
266 // texture is now dirty...
267 mTextures[backBufferIndex].dirty = true;
268 // ... so it the visible region (because we consider the surface's
269 // buffer size for visibility calculations)
270 forceVisibilityTransaction();
271 mFlinger->setTransactionFlags(eTraversalNeeded);
272 }
273 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274}
275
Mathias Agopianf9d93272009-06-19 17:00:27 -0700276void Layer::scheduleBroadcast()
277{
278 sp<Client> ourClient(client.promote());
279 if (ourClient != 0) {
280 mFlinger->scheduleBroadcast(ourClient);
281 }
282}
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284uint32_t Layer::doTransaction(uint32_t flags)
285{
286 const Layer::State& front(drawingState());
287 const Layer::State& temp(currentState());
288
289 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
290 // that the size changed back to its previous value before the buffer
291 // was resized (in the eLocked case below), in which case, we still
292 // need to execute the code below so the clients have a chance to be
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 // release. resize() deals with the fact that the size can be the same.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
295 /*
296 * Various states we could be in...
297
298 resize = state & eResizeRequested;
299 if (backbufferChanged) {
300 if (resize == 0) {
301 // ERROR, the resized buffer doesn't have its resize flag set
302 } else if (resize == mask) {
303 // ERROR one of the buffer has already been resized
304 } else if (resize == mask ^ eResizeRequested) {
305 // ERROR, the resized buffer doesn't have its resize flag set
306 } else if (resize == eResizeRequested) {
307 // OK, Normal case, proceed with resize
308 }
309 } else {
310 if (resize == 0) {
311 // OK, nothing special, do nothing
312 } else if (resize == mask) {
313 // restarted transaction, do nothing
314 } else if (resize == mask ^ eResizeRequested) {
315 // restarted transaction, do nothing
316 } else if (resize == eResizeRequested) {
317 // OK, size reset to previous value, proceed with resize
318 }
319 }
320 */
321
322 // Index of the back buffer
323 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
324 const uint32_t state = lcblk->swapState;
325 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
326 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
327 uint32_t resizeFlags = state & eResizeRequested;
328
329 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
330 LOGE( "backbuffer size changed, but both resize flags are not set! "
331 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
332 "index=%d, (%dx%d), (%dx%d)",
333 this, state,
334 int(temp.w), int(temp.h),
335 int(drawingState().w), int(drawingState().h),
336 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700337 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
338 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 // if we get there we're pretty screwed. the only reasonable
340 // thing to do is to pretend we should do the resize since
341 // backbufferChanged is set (this also will give a chance to
342 // client to get unblocked)
343 resizeFlags = eResizeRequested;
344 }
345
346 if (resizeFlags == eResizeRequested) {
347 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
348 // here, would be wrong and misleading because by this point
349 // mFrontBufferIndex has not been updated yet.
350
351 LOGD_IF(DEBUG_RESIZE,
352 "resize (layer=%p), state=%08x, "
353 "requested (%dx%d), "
354 "drawing (%d,%d), "
355 "index=%d, (%dx%d), (%dx%d)",
356 this, state,
357 int(temp.w), int(temp.h),
358 int(drawingState().w), int(drawingState().h),
359 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700360 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
361 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362
363 if (state & eLocked) {
364 // if the buffer is locked, we can't resize anything because
365 // - the backbuffer is currently in use by the user
366 // - the front buffer is being shown
367 // We just act as if the transaction didn't happen and we
368 // reschedule it later...
369 flags |= eRestartTransaction;
370 } else {
371 // This buffer needs to be resized
372 status_t err =
373 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
374 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700375 const uint32_t mask = clientBackBufferIndex ?
376 eResizeBuffer1 : eResizeBuffer0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 android_atomic_and(~mask, &(lcblk->swapState));
378 // since a buffer became available, we can let the client go...
Mathias Agopianf9d93272009-06-19 17:00:27 -0700379 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 mResizeTransactionDone = true;
381
382 // we're being resized and there is a freeze display request,
383 // acquire a freeze lock, so that the screen stays put
384 // until we've redrawn at the new size; this is to avoid
385 // glitches upon orientation changes.
386 if (mFlinger->hasFreezeRequest()) {
387 // if the surface is hidden, don't try to acquire the
388 // freeze lock, since hidden surfaces may never redraw
389 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
390 mFreezeLock = mFlinger->getFreezeLock();
391 }
392 }
393 }
394 }
395 }
396
397 if (temp.sequence != front.sequence) {
398 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
399 // this surface is now hidden, so it shouldn't hold a freeze lock
400 // (it may never redraw, which is fine if it is hidden)
401 mFreezeLock.clear();
402 }
403 }
404
405 return LayerBase::doTransaction(flags);
406}
407
408status_t Layer::resize(
409 int32_t clientBackBufferIndex,
410 uint32_t width, uint32_t height,
411 const char* what)
412{
413 /*
414 * handle resize (backbuffer and frontbuffer reallocation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700415 * this is called from post() or from doTransaction()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 */
417
418 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
419
420 // if the new (transaction) size is != from the the backbuffer
421 // then we need to reallocate the backbuffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700422 bool backbufferChanged = (clientBackBuffer.getWidth() != width) ||
423 (clientBackBuffer.getHeight() != height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424
425 LOGD_IF(!backbufferChanged,
426 "(%s) eResizeRequested (layer=%p), but size not changed: "
427 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
428 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
429 what, this,
430 int(width), int(height),
431 int(drawingState().w), int(drawingState().h),
432 int(currentState().w), int(currentState().h),
433 long(lcblk->swapState),
434 int(clientBackBufferIndex),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700435 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
436 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437
438 // this can happen when changing the size back and forth quickly
439 status_t err = NO_ERROR;
440 if (backbufferChanged) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700441
442 LOGD_IF(DEBUG_RESIZE,
443 "resize (layer=%p), requested (%dx%d), "
444 "index=%d, (%dx%d), (%dx%d)",
445 this, int(width), int(height), int(clientBackBufferIndex),
446 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
447 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
448
449 err = mBuffers[clientBackBufferIndex].setSize(width, height);
450 if (UNLIKELY(err != NO_ERROR)) {
451 // This really should never happen
452 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
453 clientBackBufferIndex, width, height, err, strerror(err));
454 // couldn't reallocate the surface
455 android_atomic_write(eInvalidSurface, &lcblk->swapState);
456 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 }
458 return err;
459}
460
461void Layer::setSizeChanged(uint32_t w, uint32_t h)
462{
463 LOGD_IF(DEBUG_RESIZE,
464 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
465 w, h, mCurrentState.w, mCurrentState.h);
466 android_atomic_or(eResizeRequested, &(lcblk->swapState));
467}
468
469// ----------------------------------------------------------------------------
470// pageflip handling...
471// ----------------------------------------------------------------------------
472
473void Layer::lockPageFlip(bool& recomputeVisibleRegions)
474{
475 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
476 // preemptively block the client, because he might set
477 // eFlipRequested at any time and want to use this buffer
478 // for the next frame. This will be unset below if it
479 // turns out we didn't need it.
480
481 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
482 if (!(state & mask))
483 return;
484
485 if (UNLIKELY(state & eInvalidSurface)) {
486 // if eInvalidSurface is set, this means the surface
487 // became invalid during a transaction (NO_MEMORY for instance)
Mathias Agopianf9d93272009-06-19 17:00:27 -0700488 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 return;
490 }
491
492 if (UNLIKELY(state & eFlipRequested)) {
493 uint32_t oldState;
494 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
495 if (oldState & eNextFlipPending) {
496 // Process another round (we know at least a buffer
497 // is ready for that client).
498 mFlinger->signalEvent();
499 }
500 }
501}
502
503Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
504{
505 // atomically swap buffers and (re)set eFlipRequested
506 int32_t oldValue, newValue;
507 layer_cblk_t * const lcblk = this->lcblk;
508 do {
509 oldValue = lcblk->swapState;
510 // get the current value
511
512 LOG_ASSERT(oldValue&eFlipRequested,
513 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
514 long(oldValue));
515
516 newValue = (oldValue ^ eIndex);
517 // swap buffers
518
519 newValue &= ~(eFlipRequested | eNextFlipPending);
520 // clear eFlipRequested and eNextFlipPending
521
522 if (oldValue & eNextFlipPending)
523 newValue |= eFlipRequested;
524 // if eNextFlipPending is set (second buffer already has something
525 // in it) we need to reset eFlipRequested because the client
526 // might never do it
527
528 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
529 *previousSate = oldValue;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700530
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 const int32_t index = (newValue & eIndex) ^ 1;
532 mFrontBufferIndex = index;
533
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534 /* NOTE: it's safe to set this flag here because this is only touched
535 * from LayerBitmap::allocate(), which by construction cannot happen
536 * while we're in post().
537 */
538 lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty;
539
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 // ... post the new front-buffer
541 Region dirty(lcblk->region + index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700542 dirty.andSelf(frontBuffer().getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700544 //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 // oldValue, newValue, mFrontBufferIndex);
546 //dirty.dump("dirty");
547
548 if (UNLIKELY(oldValue & eResizeRequested)) {
549
550 LOGD_IF(DEBUG_RESIZE,
551 "post (layer=%p), state=%08x, "
552 "index=%d, (%dx%d), (%dx%d)",
553 this, newValue,
554 int(1-index),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700555 int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()),
556 int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557
558 // here, we just posted the surface and we have resolved
559 // the front/back buffer indices. The client is blocked, so
560 // it cannot start using the new backbuffer.
561
562 // If the backbuffer was resized in THIS round, we actually cannot
563 // resize the frontbuffer because it has *just* been drawn (and we
564 // would have nothing to draw). In this case we just skip the resize
565 // it'll happen after the next page flip or during the next
566 // transaction.
567
568 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
569 if (mResizeTransactionDone && (newValue & mask)) {
570 // Resize the layer's second buffer only if the transaction
571 // happened. It may not have happened yet if eResizeRequested
572 // was set immediately after the "transactionRequested" test,
573 // in which case the drawing state's size would be wrong.
574 mFreezeLock.clear();
575 const Layer::State& s(drawingState());
576 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
577 do {
578 oldValue = lcblk->swapState;
579 if ((oldValue & eResizeRequested) == eResizeRequested) {
580 // ugh, another resize was requested since we processed
581 // the first buffer, don't free the client, and let
582 // the next transaction handle everything.
583 break;
584 }
585 newValue = oldValue & ~mask;
586 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
587 }
588 mResizeTransactionDone = false;
589 recomputeVisibleRegions = true;
590 this->contentDirty = true;
591 }
592 }
593
594 reloadTexture(dirty);
595
596 return dirty;
597}
598
599Point Layer::getPhysicalSize() const
600{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700601 sp<const Buffer> front(frontBuffer().getBuffer());
602 return Point(front->getWidth(), front->getHeight());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603}
604
605void Layer::unlockPageFlip(
606 const Transform& planeTransform, Region& outDirtyRegion)
607{
608 Region dirtyRegion(mPostedDirtyRegion);
609 if (!dirtyRegion.isEmpty()) {
610 mPostedDirtyRegion.clear();
611 // The dirty region is given in the layer's coordinate space
612 // transform the dirty region by the surface's transformation
613 // and the global transformation.
614 const Layer::State& s(drawingState());
615 const Transform tr(planeTransform * s.transform);
616 dirtyRegion = tr.transform(dirtyRegion);
617
618 // At this point, the dirty region is in screen space.
619 // Make sure it's constrained by the visible region (which
620 // is in screen space as well).
621 dirtyRegion.andSelf(visibleRegionScreen);
622 outDirtyRegion.orSelf(dirtyRegion);
623
624 // client could be blocked, so signal them so they get a
625 // chance to reevaluate their condition.
Mathias Agopianf9d93272009-06-19 17:00:27 -0700626 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 }
628}
629
630void Layer::finishPageFlip()
631{
632 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
633 LOGE_IF(!(lcblk->swapState & eBusy),
634 "layer %p wasn't locked!", this);
635 android_atomic_and(~eBusy, &(lcblk->swapState));
636 }
Mathias Agopianf9d93272009-06-19 17:00:27 -0700637 scheduleBroadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638}
639
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700640// ---------------------------------------------------------------------------
641
Mathias Agopian9a112062009-04-17 19:36:26 -0700642Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
643 SurfaceID id, const sp<Layer>& owner)
644 : Surface(flinger, id, owner->getIdentity(), owner)
645{
646}
647
648Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649{
650}
651
Fred Quintanab2fd4662009-08-11 20:49:35 -0700652sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653{
654 sp<SurfaceBuffer> buffer = 0;
655 sp<Layer> owner(getOwner());
656 if (owner != 0) {
Fred Quintanab2fd4662009-08-11 20:49:35 -0700657 buffer = owner->peekBuffer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658 }
659 return buffer;
660}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
662// ---------------------------------------------------------------------------
663
664
665}; // namespace android