blob: 31e63ef2c6b1627b34102e4967f90ff2a2134e59 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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
17#define LOG_TAG "SurfaceFlinger"
18
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <cutils/properties.h>
24
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/StopWatch.h>
28
29#include <ui/PixelFormat.h>
30#include <ui/EGLDisplaySurface.h>
31
32#include "clz.h"
33#include "Layer.h"
34#include "LayerBitmap.h"
35#include "SurfaceFlinger.h"
36#include "VRamHeap.h"
37#include "DisplayHardware/DisplayHardware.h"
38
39
40#define DEBUG_RESIZE 0
41
42
43namespace android {
44
45// ---------------------------------------------------------------------------
46
47const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
48const char* const Layer::typeID = "Layer";
49
50// ---------------------------------------------------------------------------
51
52Layer::Layer(SurfaceFlinger* flinger, DisplayID display, Client* c, int32_t i)
53 : LayerBaseClient(flinger, display, c, i),
54 mSecure(false),
55 mFrontBufferIndex(1),
56 mNeedsBlending(true),
57 mResizeTransactionDone(false),
58 mTextureName(-1U), mTextureWidth(0), mTextureHeight(0)
59{
60 // no OpenGL operation is possible here, since we might not be
61 // in the OpenGL thread.
62}
63
64Layer::~Layer()
65{
66 client->free(clientIndex());
67 // this should always be called from the OpenGL thread
68 if (mTextureName != -1U) {
69 //glDeleteTextures(1, &mTextureName);
70 deletedTextures.add(mTextureName);
71 }
72}
73
74void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags)
75{
76 LayerBase::initStates(w,h,flags);
77
78 if (flags & ISurfaceComposer::eDestroyBackbuffer)
79 lcblk->flags |= eNoCopyBack;
80}
81
82sp<LayerBaseClient::Surface> Layer::getSurface() const
83{
84 return mSurface;
85}
86
87status_t Layer::setBuffers( Client* client,
88 uint32_t w, uint32_t h,
89 PixelFormat format, uint32_t flags)
90{
91 PixelFormatInfo info;
92 status_t err = getPixelFormatInfo(format, &info);
93 if (err) return err;
94
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080095 // TODO: if eHardware is explicitly requested, we should fail
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096 // on systems where we can't allocate memory that can be used with
97 // DMA engines for instance.
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080098
99 // FIXME: we always ask for hardware for now (this should come from copybit)
100 flags |= ISurfaceComposer::eHardware;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700101
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800102 const uint32_t memory_flags = flags &
103 (ISurfaceComposer::eGPU |
104 ISurfaceComposer::eHardware |
105 ISurfaceComposer::eSecure);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106
107 // pixel-alignment. the final alignment may be bigger because
108 // we always force a 4-byte aligned bpr.
109 uint32_t alignment = 1;
110
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800111 if (flags & ISurfaceComposer::eGPU) {
112 // FIXME: this value should come from the h/w
113 alignment = 8;
114 // FIXME: this is msm7201A specific, as its GPU only supports
115 // BGRA_8888.
116 if (format == PIXEL_FORMAT_RGBA_8888) {
117 format = PIXEL_FORMAT_BGRA_8888;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700118 }
119 }
120
121 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
122 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
123 sp<MemoryDealer> allocators[2];
124 for (int i=0 ; i<2 ; i++) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800125 allocators[i] = client->createAllocator(memory_flags);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126 if (allocators[i] == 0)
127 return NO_MEMORY;
128 mBuffers[i].init(allocators[i]);
129 int err = mBuffers[i].setBits(w, h, alignment, format, LayerBitmap::SECURE_BITS);
130 if (err != NO_ERROR)
131 return err;
132 mBuffers[i].clear(); // clear the bits for security
133 mBuffers[i].getInfo(lcblk->surface + i);
134 }
135
136 mSurface = new Surface(clientIndex(),
137 allocators[0]->getMemoryHeap(),
138 allocators[1]->getMemoryHeap(),
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800139 mIdentity);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140
141 return NO_ERROR;
142}
143
144void Layer::reloadTexture(const Region& dirty)
145{
146 if (UNLIKELY(mTextureName == -1U)) {
147 // create the texture name the first time
148 // can't do that in the ctor, because it runs in another thread.
149 mTextureName = createTexture();
150 }
151 const GGLSurface& t(frontBuffer().surface());
152 loadTexture(dirty, mTextureName, t, mTextureWidth, mTextureHeight);
153}
154
155
156void Layer::onDraw(const Region& clip) const
157{
158 if (UNLIKELY(mTextureName == -1LU)) {
159 //LOGW("Layer %p doesn't have a texture", this);
160 // the texture has not been created yet, this Layer has
161 // in fact never been drawn into. this happens frequently with
162 // SurfaceView.
163 clearWithOpenGL(clip);
164 return;
165 }
166
167 const DisplayHardware& hw(graphicPlane(0).displayHardware());
168 const LayerBitmap& front(frontBuffer());
169 const GGLSurface& t(front.surface());
170
171 status_t err = NO_ERROR;
172 const int can_use_copybit = canUseCopybit();
173 if (can_use_copybit) {
174 // StopWatch watch("copybit");
175 const State& s(drawingState());
176
177 copybit_image_t dst;
178 hw.getDisplaySurface(&dst);
179 const copybit_rect_t& drect
180 = reinterpret_cast<const copybit_rect_t&>(mTransformedBounds);
181
182 copybit_image_t src;
183 front.getBitmapSurface(&src);
184 copybit_rect_t srect = { 0, 0, t.width, t.height };
185
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800186 copybit_device_t* copybit = mFlinger->getBlitEngine();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700187 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, getOrientation());
188 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, s.alpha);
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800189 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190
191 region_iterator it(clip);
192 err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it);
193 }
194
195 if (!can_use_copybit || err) {
196 drawWithOpenGL(clip, mTextureName, t);
197 }
198}
199
200status_t Layer::reallocateBuffer(int32_t index, uint32_t w, uint32_t h)
201{
202 LOGD_IF(DEBUG_RESIZE,
203 "reallocateBuffer (layer=%p), "
204 "requested (%dx%d), "
205 "index=%d, (%dx%d), (%dx%d)",
206 this,
207 int(w), int(h),
208 int(index),
209 int(mBuffers[0].width()), int(mBuffers[0].height()),
210 int(mBuffers[1].width()), int(mBuffers[1].height()));
211
212 status_t err = mBuffers[index].resize(w, h);
213 if (err == NO_ERROR) {
214 mBuffers[index].getInfo(lcblk->surface + index);
215 } else {
216 LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s",
217 index, w, h, err, strerror(err));
218 // XXX: what to do, what to do? We could try to free some
219 // hidden surfaces, instead of killing this one?
220 }
221 return err;
222}
223
224uint32_t Layer::doTransaction(uint32_t flags)
225{
226 const Layer::State& front(drawingState());
227 const Layer::State& temp(currentState());
228
229 // the test front.{w|h} != temp.{w|h} is not enough because it is possible
230 // that the size changed back to its previous value before the buffer
231 // was resized (in the eLocked case below), in which case, we still
232 // need to execute the code below so the clients have a chance to be
233 // release. resze() deals with the fact that the size can be the same.
234
235 /*
236 * Various states we could be in...
237
238 resize = state & eResizeRequested;
239 if (backbufferChanged) {
240 if (resize == 0) {
241 // ERROR, the resized buffer doesn't have its resize flag set
242 } else if (resize == mask) {
243 // ERROR one of the buffer has already been resized
244 } else if (resize == mask ^ eResizeRequested) {
245 // ERROR, the resized buffer doesn't have its resize flag set
246 } else if (resize == eResizeRequested) {
247 // OK, Normal case, proceed with resize
248 }
249 } else {
250 if (resize == 0) {
251 // OK, nothing special, do nothing
252 } else if (resize == mask) {
253 // restarted transaction, do nothing
254 } else if (resize == mask ^ eResizeRequested) {
255 // restarted transaction, do nothing
256 } else if (resize == eResizeRequested) {
257 // OK, size reset to previous value, proceed with resize
258 }
259 }
260 */
261
262 // Index of the back buffer
263 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
264 const uint32_t state = lcblk->swapState;
265 const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state);
266 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
267 uint32_t resizeFlags = state & eResizeRequested;
268
269 if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) {
270 LOGE( "backbuffer size changed, but both resize flags are not set! "
271 "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), "
272 "index=%d, (%dx%d), (%dx%d)",
273 this, state,
274 int(temp.w), int(temp.h),
275 int(drawingState().w), int(drawingState().h),
276 int(clientBackBufferIndex),
277 int(mBuffers[0].width()), int(mBuffers[0].height()),
278 int(mBuffers[1].width()), int(mBuffers[1].height()));
279 // if we get there we're pretty screwed. the only reasonable
280 // thing to do is to pretend we should do the resize since
281 // backbufferChanged is set (this also will give a chance to
282 // client to get unblocked)
283 resizeFlags = eResizeRequested;
284 }
285
286 if (resizeFlags == eResizeRequested) {
287 // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex
288 // here, would be wrong and misleading because by this point
289 // mFrontBufferIndex has not been updated yet.
290
291 LOGD_IF(DEBUG_RESIZE,
292 "resize (layer=%p), state=%08x, "
293 "requested (%dx%d), "
294 "drawing (%d,%d), "
295 "index=%d, (%dx%d), (%dx%d)",
296 this, state,
297 int(temp.w), int(temp.h),
298 int(drawingState().w), int(drawingState().h),
299 int(clientBackBufferIndex),
300 int(mBuffers[0].width()), int(mBuffers[0].height()),
301 int(mBuffers[1].width()), int(mBuffers[1].height()));
302
303 if (state & eLocked) {
304 // if the buffer is locked, we can't resize anything because
305 // - the backbuffer is currently in use by the user
306 // - the front buffer is being shown
307 // We just act as if the transaction didn't happen and we
308 // reschedule it later...
309 flags |= eRestartTransaction;
310 } else {
311 // This buffer needs to be resized
312 status_t err =
313 resize(clientBackBufferIndex, temp.w, temp.h, "transaction");
314 if (err == NO_ERROR) {
315 const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0;
316 android_atomic_and(~mask, &(lcblk->swapState));
The Android Open Source Project27629322009-01-09 17:51:23 -0800317 // since a buffer became available, we can let the client go...
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 mFlinger->scheduleBroadcast(client);
319 mResizeTransactionDone = true;
320
321 // we're being resized and there is a freeze display request,
322 // acquire a freeze lock, so that the screen stays put
323 // until we've redrawn at the new size; this is to avoid
324 // glitches upon orientation changes.
325 if (mFlinger->hasFreezeRequest()) {
326 // if the surface is hidden, don't try to acquire the
327 // freeze lock, since hidden surfaces may never redraw
328 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
329 mFreezeLock = mFlinger->getFreezeLock();
330 }
331 }
332 }
333 }
334 }
335
336 if (temp.sequence != front.sequence) {
337 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
338 // this surface is now hidden, so it shouldn't hold a freeze lock
339 // (it may never redraw, which is fine if it is hidden)
340 mFreezeLock.clear();
341 }
342 }
343
344 return LayerBase::doTransaction(flags);
345}
346
347status_t Layer::resize(
348 int32_t clientBackBufferIndex,
349 uint32_t width, uint32_t height,
350 const char* what)
351{
352 /*
353 * handle resize (backbuffer and frontbuffer reallocation)
354 */
355
356 const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]);
357
358 // if the new (transaction) size is != from the the backbuffer
359 // then we need to reallocate the backbuffer
360 bool backbufferChanged = (clientBackBuffer.width() != width) ||
361 (clientBackBuffer.height() != height);
362
363 LOGD_IF(!backbufferChanged,
364 "(%s) eResizeRequested (layer=%p), but size not changed: "
365 "requested (%dx%d), drawing (%d,%d), current (%d,%d),"
366 "state=%08lx, index=%d, (%dx%d), (%dx%d)",
367 what, this,
368 int(width), int(height),
369 int(drawingState().w), int(drawingState().h),
370 int(currentState().w), int(currentState().h),
371 long(lcblk->swapState),
372 int(clientBackBufferIndex),
373 int(mBuffers[0].width()), int(mBuffers[0].height()),
374 int(mBuffers[1].width()), int(mBuffers[1].height()));
375
376 // this can happen when changing the size back and forth quickly
377 status_t err = NO_ERROR;
378 if (backbufferChanged) {
379 err = reallocateBuffer(clientBackBufferIndex, width, height);
380 }
381 if (UNLIKELY(err != NO_ERROR)) {
382 // couldn't reallocate the surface
383 android_atomic_write(eInvalidSurface, &lcblk->swapState);
384 memset(lcblk->surface+clientBackBufferIndex, 0, sizeof(surface_info_t));
385 }
386 return err;
387}
388
389void Layer::setSizeChanged(uint32_t w, uint32_t h)
390{
391 LOGD_IF(DEBUG_RESIZE,
392 "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)",
393 w, h, mCurrentState.w, mCurrentState.h);
394 android_atomic_or(eResizeRequested, &(lcblk->swapState));
395}
396
397// ----------------------------------------------------------------------------
398// pageflip handling...
399// ----------------------------------------------------------------------------
400
401void Layer::lockPageFlip(bool& recomputeVisibleRegions)
402{
403 uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState));
404 // preemptively block the client, because he might set
405 // eFlipRequested at any time and want to use this buffer
406 // for the next frame. This will be unset below if it
407 // turns out we didn't need it.
408
409 uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested;
410 if (!(state & mask))
411 return;
412
413 if (UNLIKELY(state & eInvalidSurface)) {
414 // if eInvalidSurface is set, this means the surface
415 // became invalid during a transaction (NO_MEMORY for instance)
416 mFlinger->scheduleBroadcast(client);
417 return;
418 }
419
420 if (UNLIKELY(state & eFlipRequested)) {
421 uint32_t oldState;
422 mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions);
423 if (oldState & eNextFlipPending) {
424 // Process another round (we know at least a buffer
425 // is ready for that client).
426 mFlinger->signalEvent();
427 }
428 }
429}
430
431Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions)
432{
433 // atomically swap buffers and (re)set eFlipRequested
434 int32_t oldValue, newValue;
435 layer_cblk_t * const lcblk = this->lcblk;
436 do {
437 oldValue = lcblk->swapState;
438 // get the current value
439
440 LOG_ASSERT(oldValue&eFlipRequested,
441 "eFlipRequested not set, yet we're flipping! (state=0x%08lx)",
442 long(oldValue));
443
444 newValue = (oldValue ^ eIndex);
445 // swap buffers
446
447 newValue &= ~(eFlipRequested | eNextFlipPending);
448 // clear eFlipRequested and eNextFlipPending
449
450 if (oldValue & eNextFlipPending)
451 newValue |= eFlipRequested;
452 // if eNextFlipPending is set (second buffer already has something
453 // in it) we need to reset eFlipRequested because the client
454 // might never do it
455
456 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
457 *previousSate = oldValue;
458
459 const int32_t index = (newValue & eIndex) ^ 1;
460 mFrontBufferIndex = index;
461
462 // ... post the new front-buffer
463 Region dirty(lcblk->region + index);
464 dirty.andSelf(frontBuffer().bounds());
465
466 //LOGI("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n",
467 // oldValue, newValue, mFrontBufferIndex);
468 //dirty.dump("dirty");
469
470 if (UNLIKELY(oldValue & eResizeRequested)) {
471
472 LOGD_IF(DEBUG_RESIZE,
473 "post (layer=%p), state=%08x, "
474 "index=%d, (%dx%d), (%dx%d)",
475 this, newValue,
476 int(1-index),
477 int(mBuffers[0].width()), int(mBuffers[0].height()),
478 int(mBuffers[1].width()), int(mBuffers[1].height()));
479
480 // here, we just posted the surface and we have resolved
481 // the front/back buffer indices. The client is blocked, so
482 // it cannot start using the new backbuffer.
483
484 // If the backbuffer was resized in THIS round, we actually cannot
485 // resize the frontbuffer because it has *just* been drawn (and we
486 // would have nothing to draw). In this case we just skip the resize
487 // it'll happen after the next page flip or during the next
488 // transaction.
489
490 const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0;
491 if (mResizeTransactionDone && (newValue & mask)) {
492 // Resize the layer's second buffer only if the transaction
493 // happened. It may not have happened yet if eResizeRequested
494 // was set immediately after the "transactionRequested" test,
495 // in which case the drawing state's size would be wrong.
496 mFreezeLock.clear();
497 const Layer::State& s(drawingState());
498 if (resize(1-index, s.w, s.h, "post") == NO_ERROR) {
499 do {
500 oldValue = lcblk->swapState;
501 if ((oldValue & eResizeRequested) == eResizeRequested) {
502 // ugh, another resize was requested since we processed
503 // the first buffer, don't free the client, and let
504 // the next transaction handle everything.
505 break;
506 }
507 newValue = oldValue & ~mask;
508 } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState)));
509 }
510 mResizeTransactionDone = false;
511 recomputeVisibleRegions = true;
The Android Open Source Project27629322009-01-09 17:51:23 -0800512 this->contentDirty = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 }
514 }
515
516 reloadTexture(dirty);
517
518 return dirty;
519}
520
521Point Layer::getPhysicalSize() const
522{
523 const LayerBitmap& front(frontBuffer());
524 return Point(front.width(), front.height());
525}
526
527void Layer::unlockPageFlip(
528 const Transform& planeTransform, Region& outDirtyRegion)
529{
530 Region dirtyRegion(mPostedDirtyRegion);
531 if (!dirtyRegion.isEmpty()) {
532 mPostedDirtyRegion.clear();
533 // The dirty region is given in the layer's coordinate space
534 // transform the dirty region by the surface's transformation
535 // and the global transformation.
536 const Layer::State& s(drawingState());
537 const Transform tr(planeTransform * s.transform);
538 dirtyRegion = tr.transform(dirtyRegion);
539
540 // At this point, the dirty region is in screen space.
541 // Make sure it's constrained by the visible region (which
542 // is in screen space as well).
543 dirtyRegion.andSelf(visibleRegionScreen);
544 outDirtyRegion.orSelf(dirtyRegion);
545
546 // client could be blocked, so signal them so they get a
547 // chance to reevaluate their condition.
548 mFlinger->scheduleBroadcast(client);
549 }
550}
551
552void Layer::finishPageFlip()
553{
554 if (LIKELY(!(lcblk->swapState & eInvalidSurface))) {
555 LOGE_IF(!(lcblk->swapState & eBusy),
556 "layer %p wasn't locked!", this);
557 android_atomic_and(~eBusy, &(lcblk->swapState));
558 }
559 mFlinger->scheduleBroadcast(client);
560}
561
562
563// ---------------------------------------------------------------------------
564
565
566}; // namespace android