1 /*
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 #include <stdlib.h>
18 #include <stdint.h>
19 #include <sys/types.h>
20
21 #include <cutils/properties.h>
22 #include <cutils/native_handle.h>
23
24 #include <utils/Errors.h>
25 #include <utils/Log.h>
26 #include <utils/StopWatch.h>
27
28 #include <ui/GraphicBuffer.h>
29 #include <ui/PixelFormat.h>
30
31 #include <surfaceflinger/Surface.h>
32
33 #include "clz.h"
34 #include "GLExtensions.h"
35 #include "Layer.h"
36 #include "SurfaceFlinger.h"
37 #include "DisplayHardware/DisplayHardware.h"
38
39
40 #define DEBUG_RESIZE 0
41
42
43 namespace android {
44
min(T a,T b)45 template <typename T> inline T min(T a, T b) {
46 return a<b ? a : b;
47 }
48
49 // ---------------------------------------------------------------------------
50
Layer(SurfaceFlinger * flinger,DisplayID display,const sp<Client> & client)51 Layer::Layer(SurfaceFlinger* flinger,
52 DisplayID display, const sp<Client>& client)
53 : LayerBaseClient(flinger, display, client),
54 mGLExtensions(GLExtensions::getInstance()),
55 mNeedsBlending(true),
56 mNeedsDithering(false),
57 mSecure(false),
58 mTextureManager(),
59 mBufferManager(mTextureManager),
60 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false),
61 mBypassState(false)
62 {
63 setDestroyer(this);
64 }
65
~Layer()66 Layer::~Layer()
67 {
68 // FIXME: must be called from the main UI thread
69 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
70 mBufferManager.destroy(dpy);
71
72 // we can use getUserClientUnsafe here because we know we're
73 // single-threaded at that point.
74 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
75 if (ourClient != 0) {
76 ourClient->detachLayer(this);
77 }
78 }
79
destroy(RefBase const * base)80 void Layer::destroy(RefBase const* base) {
81 mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
82 }
83
setToken(const sp<UserClient> & userClient,SharedClient * sharedClient,int32_t token)84 status_t Layer::setToken(const sp<UserClient>& userClient,
85 SharedClient* sharedClient, int32_t token)
86 {
87 sp<SharedBufferServer> lcblk = new SharedBufferServer(
88 sharedClient, token, mBufferManager.getDefaultBufferCount(),
89 getIdentity());
90
91 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
92
93 LOGE_IF(err != NO_ERROR,
94 "ClientRef::setToken(%p, %p, %u) failed",
95 userClient.get(), lcblk.get(), token);
96
97 if (err == NO_ERROR) {
98 // we need to free the buffers associated with this surface
99 }
100
101 return err;
102 }
103
getToken() const104 int32_t Layer::getToken() const
105 {
106 return mUserClientRef.getToken();
107 }
108
getClient() const109 sp<UserClient> Layer::getClient() const
110 {
111 return mUserClientRef.getClient();
112 }
113
114 // called with SurfaceFlinger::mStateLock as soon as the layer is entered
115 // in the purgatory list
onRemoved()116 void Layer::onRemoved()
117 {
118 ClientRef::Access sharedClient(mUserClientRef);
119 SharedBufferServer* lcblk(sharedClient.get());
120 if (lcblk) {
121 // wake up the condition
122 lcblk->setStatus(NO_INIT);
123 }
124 }
125
createSurface() const126 sp<LayerBaseClient::Surface> Layer::createSurface() const
127 {
128 return mSurface;
129 }
130
setBuffers(uint32_t w,uint32_t h,PixelFormat format,uint32_t flags)131 status_t Layer::setBuffers( uint32_t w, uint32_t h,
132 PixelFormat format, uint32_t flags)
133 {
134 // this surfaces pixel format
135 PixelFormatInfo info;
136 status_t err = getPixelFormatInfo(format, &info);
137 if (err) return err;
138
139 // the display's pixel format
140 const DisplayHardware& hw(graphicPlane(0).displayHardware());
141 uint32_t const maxSurfaceDims = min(
142 hw.getMaxTextureSize(), hw.getMaxViewportDims());
143
144 // never allow a surface larger than what our underlying GL implementation
145 // can handle.
146 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
147 return BAD_VALUE;
148 }
149
150 PixelFormatInfo displayInfo;
151 getPixelFormatInfo(hw.getFormat(), &displayInfo);
152 const uint32_t hwFlags = hw.getFlags();
153
154 mFormat = format;
155 mWidth = w;
156 mHeight = h;
157
158 mReqFormat = format;
159 mReqWidth = w;
160 mReqHeight = h;
161
162 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
163 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
164
165 // we use the red index
166 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
167 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
168 mNeedsDithering = layerRedsize > displayRedSize;
169
170 mSurface = new SurfaceLayer(mFlinger, this);
171 return NO_ERROR;
172 }
173
reloadTexture(const Region & dirty)174 void Layer::reloadTexture(const Region& dirty)
175 {
176 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
177 if (buffer == NULL) {
178 // this situation can happen if we ran out of memory for instance.
179 // not much we can do. continue to use whatever texture was bound
180 // to this context.
181 return;
182 }
183
184 if (mGLExtensions.haveDirectTexture()) {
185 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
186 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
187 // not sure what we can do here...
188 goto slowpath;
189 }
190 } else {
191 slowpath:
192 GGLSurface t;
193 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
194 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
195 LOGE_IF(res, "error %d (%s) locking buffer %p",
196 res, strerror(res), buffer.get());
197 if (res == NO_ERROR) {
198 mBufferManager.loadTexture(dirty, t);
199 buffer->unlock();
200 }
201 } else {
202 // we can't do anything
203 }
204 }
205 }
206
drawForSreenShot() const207 void Layer::drawForSreenShot() const
208 {
209 const bool currentFiltering = mNeedsFiltering;
210 const_cast<Layer*>(this)->mNeedsFiltering = true;
211 LayerBase::drawForSreenShot();
212 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
213 }
214
onDraw(const Region & clip) const215 void Layer::onDraw(const Region& clip) const
216 {
217 Texture tex(mBufferManager.getActiveTexture());
218 if (tex.name == -1LU) {
219 // the texture has not been created yet, this Layer has
220 // in fact never been drawn into. This happens frequently with
221 // SurfaceView because the WindowManager can't know when the client
222 // has drawn the first time.
223
224 // If there is nothing under us, we paint the screen in black, otherwise
225 // we just skip this update.
226
227 // figure out if there is something below us
228 Region under;
229 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
230 const size_t count = drawingLayers.size();
231 for (size_t i=0 ; i<count ; ++i) {
232 const sp<LayerBase>& layer(drawingLayers[i]);
233 if (layer.get() == static_cast<LayerBase const*>(this))
234 break;
235 under.orSelf(layer->visibleRegionScreen);
236 }
237 // if not everything below us is covered, we plug the holes!
238 Region holes(clip.subtract(under));
239 if (!holes.isEmpty()) {
240 clearWithOpenGL(holes, 0, 0, 0, 1);
241 }
242 return;
243 }
244
245 #ifdef USE_COMPOSITION_BYPASS
246 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
247 if ((buffer != NULL) && (buffer->transform)) {
248 // Here we have a "bypass" buffer, but we need to composite it
249 // most likely because it's not fullscreen anymore.
250 // Since the buffer may have a transformation applied by the client
251 // we need to inverse this transformation here.
252
253 // calculate the inverse of the buffer transform
254 const uint32_t mask = HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
255 const uint32_t bufferTransformInverse = buffer->transform ^ mask;
256
257 // To accomplish the inverse transform, we use "mBufferTransform"
258 // which is not used by Layer.cpp
259 const_cast<Layer*>(this)->mBufferTransform = bufferTransformInverse;
260 drawWithOpenGL(clip, tex);
261 // reset to "no transfrom"
262 const_cast<Layer*>(this)->mBufferTransform = 0;
263 return;
264 }
265 #endif
266
267 drawWithOpenGL(clip, tex);
268 }
269
needsFiltering() const270 bool Layer::needsFiltering() const
271 {
272 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
273 // if our buffer is not the same size than ourselves,
274 // we need filtering.
275 Mutex::Autolock _l(mLock);
276 if (mNeedsScaling)
277 return true;
278 }
279 return LayerBase::needsFiltering();
280 }
281
282
setBufferCount(int bufferCount)283 status_t Layer::setBufferCount(int bufferCount)
284 {
285 ClientRef::Access sharedClient(mUserClientRef);
286 SharedBufferServer* lcblk(sharedClient.get());
287 if (!lcblk) {
288 // oops, the client is already gone
289 return DEAD_OBJECT;
290 }
291
292 // NOTE: lcblk->resize() is protected by an internal lock
293 status_t err = lcblk->resize(bufferCount);
294 if (err == NO_ERROR)
295 mBufferManager.resize(bufferCount);
296
297 return err;
298 }
299
requestBuffer(int index,uint32_t reqWidth,uint32_t reqHeight,uint32_t reqFormat,uint32_t usage)300 sp<GraphicBuffer> Layer::requestBuffer(int index,
301 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
302 uint32_t usage)
303 {
304 sp<GraphicBuffer> buffer;
305
306 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
307 return buffer;
308
309 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
310 return buffer;
311
312 // this ensures our client doesn't go away while we're accessing
313 // the shared area.
314 ClientRef::Access sharedClient(mUserClientRef);
315 SharedBufferServer* lcblk(sharedClient.get());
316 if (!lcblk) {
317 // oops, the client is already gone
318 return buffer;
319 }
320
321 /*
322 * This is called from the client's Surface::dequeue(). This can happen
323 * at any time, especially while we're in the middle of using the
324 * buffer 'index' as our front buffer.
325 */
326
327 uint32_t w, h, f, bypass;
328 { // scope for the lock
329 Mutex::Autolock _l(mLock);
330
331 bypass = mBypassState;
332
333 // zero means default
334 mFixedSize = reqWidth && reqHeight;
335 if (!reqFormat) reqFormat = mFormat;
336 if (!reqWidth) reqWidth = mWidth;
337 if (!reqHeight) reqHeight = mHeight;
338
339 w = reqWidth;
340 h = reqHeight;
341 f = reqFormat;
342
343 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
344 (reqFormat != mReqFormat)) {
345 mReqWidth = reqWidth;
346 mReqHeight = reqHeight;
347 mReqFormat = reqFormat;
348 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
349
350 lcblk->reallocateAllExcept(index);
351 }
352 }
353
354 // here we have to reallocate a new buffer because the buffer could be
355 // used as the front buffer, or by a client in our process
356 // (eg: status bar), and we can't release the handle under its feet.
357 uint32_t effectiveUsage = getEffectiveUsage(usage);
358
359 status_t err = NO_MEMORY;
360
361 #ifdef USE_COMPOSITION_BYPASS
362 if (!mSecure && bypass && (effectiveUsage & GRALLOC_USAGE_HW_RENDER)) {
363 // always allocate a buffer matching the screen size. the size
364 // may be different from (w,h) if the buffer is rotated.
365 const DisplayHardware& hw(graphicPlane(0).displayHardware());
366 int32_t w = hw.getWidth();
367 int32_t h = hw.getHeight();
368 int32_t f = hw.getFormat();
369
370 buffer = new GraphicBuffer(w, h, f, effectiveUsage | GRALLOC_USAGE_HW_FB);
371 err = buffer->initCheck();
372 buffer->transform = uint8_t(getOrientation());
373
374 if (err != NO_ERROR) {
375 // allocation didn't succeed, probably because an older bypass
376 // window hasn't released all its resources yet.
377 ClientRef::Access sharedClient(mUserClientRef);
378 SharedBufferServer* lcblk(sharedClient.get());
379 if (lcblk) {
380 // all buffers need reallocation
381 lcblk->reallocateAll();
382 }
383 }
384 }
385 #endif
386
387 if (err != NO_ERROR) {
388 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
389 err = buffer->initCheck();
390 }
391
392 if (err || buffer->handle == 0) {
393 GraphicBuffer::dumpAllocationsToSystemLog();
394 LOGE_IF(err || buffer->handle == 0,
395 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
396 this, index, w, h, strerror(-err));
397 } else {
398 LOGD_IF(DEBUG_RESIZE,
399 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
400 this, index, w, h, buffer->handle);
401 }
402
403 if (err == NO_ERROR && buffer->handle != 0) {
404 Mutex::Autolock _l(mLock);
405 mBufferManager.attachBuffer(index, buffer);
406 }
407 return buffer;
408 }
409
getEffectiveUsage(uint32_t usage) const410 uint32_t Layer::getEffectiveUsage(uint32_t usage) const
411 {
412 /*
413 * buffers used for software rendering, but h/w composition
414 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
415 *
416 * buffers used for h/w rendering and h/w composition
417 * are allocated with HW_RENDER | HW_TEXTURE
418 *
419 * buffers used with h/w rendering and either NPOT or no egl_image_ext
420 * are allocated with SW_READ_RARELY | HW_RENDER
421 *
422 */
423
424 if (mSecure) {
425 // secure buffer, don't store it into the GPU
426 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
427 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
428 } else {
429 // it's allowed to modify the usage flags here, but generally
430 // the requested flags should be honored.
431 // request EGLImage for all buffers
432 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
433 }
434 return usage;
435 }
436
setBypass(bool enable)437 bool Layer::setBypass(bool enable)
438 {
439 Mutex::Autolock _l(mLock);
440
441 if (mNeedsScaling || mNeedsFiltering) {
442 return false;
443 }
444
445 if (mBypassState != enable) {
446 mBypassState = enable;
447 ClientRef::Access sharedClient(mUserClientRef);
448 SharedBufferServer* lcblk(sharedClient.get());
449 if (lcblk) {
450 // all buffers need reallocation
451 lcblk->reallocateAll();
452 }
453 }
454
455 return true;
456 }
457
updateBuffersOrientation()458 void Layer::updateBuffersOrientation()
459 {
460 sp<GraphicBuffer> buffer(getBypassBuffer());
461 if (buffer != NULL && mOrientation != buffer->transform) {
462 ClientRef::Access sharedClient(mUserClientRef);
463 SharedBufferServer* lcblk(sharedClient.get());
464 if (lcblk) { // all buffers need reallocation
465 lcblk->reallocateAll();
466 }
467 }
468 }
469
doTransaction(uint32_t flags)470 uint32_t Layer::doTransaction(uint32_t flags)
471 {
472 const Layer::State& front(drawingState());
473 const Layer::State& temp(currentState());
474
475 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
476 (front.requested_h != temp.requested_h);
477
478 if (sizeChanged) {
479 // the size changed, we need to ask our client to request a new buffer
480 LOGD_IF(DEBUG_RESIZE,
481 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
482 this,
483 int(temp.requested_w), int(temp.requested_h),
484 int(front.requested_w), int(front.requested_h));
485
486 if (!isFixedSize()) {
487 // we're being resized and there is a freeze display request,
488 // acquire a freeze lock, so that the screen stays put
489 // until we've redrawn at the new size; this is to avoid
490 // glitches upon orientation changes.
491 if (mFlinger->hasFreezeRequest()) {
492 // if the surface is hidden, don't try to acquire the
493 // freeze lock, since hidden surfaces may never redraw
494 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
495 mFreezeLock = mFlinger->getFreezeLock();
496 }
497 }
498
499 // this will make sure LayerBase::doTransaction doesn't update
500 // the drawing state's size
501 Layer::State& editDraw(mDrawingState);
502 editDraw.requested_w = temp.requested_w;
503 editDraw.requested_h = temp.requested_h;
504
505 // record the new size, form this point on, when the client request
506 // a buffer, it'll get the new size.
507 setBufferSize(temp.requested_w, temp.requested_h);
508
509 ClientRef::Access sharedClient(mUserClientRef);
510 SharedBufferServer* lcblk(sharedClient.get());
511 if (lcblk) {
512 // all buffers need reallocation
513 lcblk->reallocateAll();
514 }
515 } else {
516 // record the new size
517 setBufferSize(temp.requested_w, temp.requested_h);
518 }
519 }
520
521 if (temp.sequence != front.sequence) {
522 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
523 // this surface is now hidden, so it shouldn't hold a freeze lock
524 // (it may never redraw, which is fine if it is hidden)
525 mFreezeLock.clear();
526 }
527 }
528
529 return LayerBase::doTransaction(flags);
530 }
531
setBufferSize(uint32_t w,uint32_t h)532 void Layer::setBufferSize(uint32_t w, uint32_t h) {
533 Mutex::Autolock _l(mLock);
534 mWidth = w;
535 mHeight = h;
536 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
537 }
538
isFixedSize() const539 bool Layer::isFixedSize() const {
540 Mutex::Autolock _l(mLock);
541 return mFixedSize;
542 }
543
544 // ----------------------------------------------------------------------------
545 // pageflip handling...
546 // ----------------------------------------------------------------------------
547
lockPageFlip(bool & recomputeVisibleRegions)548 void Layer::lockPageFlip(bool& recomputeVisibleRegions)
549 {
550 ClientRef::Access sharedClient(mUserClientRef);
551 SharedBufferServer* lcblk(sharedClient.get());
552 if (!lcblk) {
553 // client died
554 recomputeVisibleRegions = true;
555 return;
556 }
557
558 ssize_t buf = lcblk->retireAndLock();
559 if (buf == NOT_ENOUGH_DATA) {
560 // NOTE: This is not an error, it simply means there is nothing to
561 // retire. The buffer is locked because we will use it
562 // for composition later in the loop
563 return;
564 }
565
566 if (buf < NO_ERROR) {
567 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
568 mPostedDirtyRegion.clear();
569 return;
570 }
571
572 // we retired a buffer, which becomes the new front buffer
573 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
574 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
575 mPostedDirtyRegion.clear();
576 return;
577 }
578
579 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
580 if (newFrontBuffer != NULL) {
581 // get the dirty region
582 // compute the posted region
583 const Region dirty(lcblk->getDirtyRegion(buf));
584 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
585
586 // update the layer size and release freeze-lock
587 const Layer::State& front(drawingState());
588 if (newFrontBuffer->getWidth() == front.requested_w &&
589 newFrontBuffer->getHeight() == front.requested_h)
590 {
591 if ((front.w != front.requested_w) ||
592 (front.h != front.requested_h))
593 {
594 // Here we pretend the transaction happened by updating the
595 // current and drawing states. Drawing state is only accessed
596 // in this thread, no need to have it locked
597 Layer::State& editDraw(mDrawingState);
598 editDraw.w = editDraw.requested_w;
599 editDraw.h = editDraw.requested_h;
600
601 // We also need to update the current state so that we don't
602 // end-up doing too much work during the next transaction.
603 // NOTE: We actually don't need hold the transaction lock here
604 // because State::w and State::h are only accessed from
605 // this thread
606 Layer::State& editTemp(currentState());
607 editTemp.w = editDraw.w;
608 editTemp.h = editDraw.h;
609
610 // recompute visible region
611 recomputeVisibleRegions = true;
612 }
613
614 // we now have the correct size, unfreeze the screen
615 mFreezeLock.clear();
616 }
617
618 // get the crop region
619 setBufferCrop( lcblk->getCrop(buf) );
620
621 // get the transformation
622 setBufferTransform( lcblk->getTransform(buf) );
623
624 } else {
625 // this should not happen unless we ran out of memory while
626 // allocating the buffer. we're hoping that things will get back
627 // to normal the next time the app tries to draw into this buffer.
628 // meanwhile, pretend the screen didn't update.
629 mPostedDirtyRegion.clear();
630 }
631
632 if (lcblk->getQueuedCount()) {
633 // signal an event if we have more buffers waiting
634 mFlinger->signalEvent();
635 }
636
637 /* a buffer was posted, so we need to call reloadTexture(), which
638 * will update our internal data structures (eg: EGLImageKHR or
639 * texture names). we need to do this even if mPostedDirtyRegion is
640 * empty -- it's orthogonal to the fact that a new buffer was posted,
641 * for instance, a degenerate case could be that the user did an empty
642 * update but repainted the buffer with appropriate content (after a
643 * resize for instance).
644 */
645 reloadTexture( mPostedDirtyRegion );
646 }
647
unlockPageFlip(const Transform & planeTransform,Region & outDirtyRegion)648 void Layer::unlockPageFlip(
649 const Transform& planeTransform, Region& outDirtyRegion)
650 {
651 Region dirtyRegion(mPostedDirtyRegion);
652 if (!dirtyRegion.isEmpty()) {
653 mPostedDirtyRegion.clear();
654 // The dirty region is given in the layer's coordinate space
655 // transform the dirty region by the surface's transformation
656 // and the global transformation.
657 const Layer::State& s(drawingState());
658 const Transform tr(planeTransform * s.transform);
659 dirtyRegion = tr.transform(dirtyRegion);
660
661 // At this point, the dirty region is in screen space.
662 // Make sure it's constrained by the visible region (which
663 // is in screen space as well).
664 dirtyRegion.andSelf(visibleRegionScreen);
665 outDirtyRegion.orSelf(dirtyRegion);
666 }
667 if (visibleRegionScreen.isEmpty()) {
668 // an invisible layer should not hold a freeze-lock
669 // (because it may never be updated and therefore never release it)
670 mFreezeLock.clear();
671 }
672 }
673
dump(String8 & result,char * buffer,size_t SIZE) const674 void Layer::dump(String8& result, char* buffer, size_t SIZE) const
675 {
676 LayerBaseClient::dump(result, buffer, SIZE);
677
678 ClientRef::Access sharedClient(mUserClientRef);
679 SharedBufferServer* lcblk(sharedClient.get());
680 uint32_t totalTime = 0;
681 if (lcblk) {
682 SharedBufferStack::Statistics stats = lcblk->getStats();
683 totalTime= stats.totalTime;
684 result.append( lcblk->dump(" ") );
685 }
686
687 sp<const GraphicBuffer> buf0(getBuffer(0));
688 sp<const GraphicBuffer> buf1(getBuffer(1));
689 uint32_t w0=0, h0=0, s0=0;
690 uint32_t w1=0, h1=0, s1=0;
691 if (buf0 != 0) {
692 w0 = buf0->getWidth();
693 h0 = buf0->getHeight();
694 s0 = buf0->getStride();
695 }
696 if (buf1 != 0) {
697 w1 = buf1->getWidth();
698 h1 = buf1->getHeight();
699 s1 = buf1->getStride();
700 }
701 snprintf(buffer, SIZE,
702 " "
703 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
704 " freezeLock=%p, bypass=%d, dq-q-time=%u us\n",
705 mFormat, w0, h0, s0, w1, h1, s1,
706 getFreezeLock().get(), mBypassState, totalTime);
707
708 result.append(buffer);
709 }
710
711 // ---------------------------------------------------------------------------
712
ClientRef()713 Layer::ClientRef::ClientRef()
714 : mControlBlock(0), mToken(-1) {
715 }
716
~ClientRef()717 Layer::ClientRef::~ClientRef() {
718 }
719
getToken() const720 int32_t Layer::ClientRef::getToken() const {
721 Mutex::Autolock _l(mLock);
722 return mToken;
723 }
724
getClient() const725 sp<UserClient> Layer::ClientRef::getClient() const {
726 Mutex::Autolock _l(mLock);
727 return mUserClient.promote();
728 }
729
setToken(const sp<UserClient> & uc,const sp<SharedBufferServer> & sharedClient,int32_t token)730 status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
731 const sp<SharedBufferServer>& sharedClient, int32_t token) {
732 Mutex::Autolock _l(mLock);
733
734 { // scope for strong mUserClient reference
735 sp<UserClient> userClient(mUserClient.promote());
736 if (mUserClient != 0 && mControlBlock != 0) {
737 mControlBlock->setStatus(NO_INIT);
738 }
739 }
740
741 mUserClient = uc;
742 mToken = token;
743 mControlBlock = sharedClient;
744 return NO_ERROR;
745 }
746
getUserClientUnsafe() const747 sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
748 return mUserClient.promote();
749 }
750
751 // this class gives us access to SharedBufferServer safely
752 // it makes sure the UserClient (and its associated shared memory)
753 // won't go away while we're accessing it.
Access(const ClientRef & ref)754 Layer::ClientRef::Access::Access(const ClientRef& ref)
755 : mControlBlock(0)
756 {
757 Mutex::Autolock _l(ref.mLock);
758 mUserClientStrongRef = ref.mUserClient.promote();
759 if (mUserClientStrongRef != 0)
760 mControlBlock = ref.mControlBlock;
761 }
762
~Access()763 Layer::ClientRef::Access::~Access()
764 {
765 }
766
767 // ---------------------------------------------------------------------------
768
BufferManager(TextureManager & tm)769 Layer::BufferManager::BufferManager(TextureManager& tm)
770 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
771 mActiveBuffer(-1), mFailover(false)
772 {
773 }
774
~BufferManager()775 Layer::BufferManager::~BufferManager()
776 {
777 }
778
resize(size_t size)779 status_t Layer::BufferManager::resize(size_t size)
780 {
781 Mutex::Autolock _l(mLock);
782 mNumBuffers = size;
783 return NO_ERROR;
784 }
785
786 // only for debugging
getBuffer(size_t index) const787 sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
788 return mBufferData[index].buffer;
789 }
790
setActiveBufferIndex(size_t index)791 status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
792 mActiveBuffer = index;
793 return NO_ERROR;
794 }
795
getActiveBufferIndex() const796 size_t Layer::BufferManager::getActiveBufferIndex() const {
797 return mActiveBuffer;
798 }
799
getActiveTexture() const800 Texture Layer::BufferManager::getActiveTexture() const {
801 Texture res;
802 if (mFailover || mActiveBuffer<0) {
803 res = mFailoverTexture;
804 } else {
805 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
806 }
807 return res;
808 }
809
getActiveBuffer() const810 sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
811 sp<GraphicBuffer> result;
812 const ssize_t activeBuffer = mActiveBuffer;
813 if (activeBuffer >= 0) {
814 BufferData const * const buffers = mBufferData;
815 Mutex::Autolock _l(mLock);
816 result = buffers[activeBuffer].buffer;
817 }
818 return result;
819 }
820
detachBuffer(size_t index)821 sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
822 {
823 BufferData* const buffers = mBufferData;
824 sp<GraphicBuffer> buffer;
825 Mutex::Autolock _l(mLock);
826 buffer = buffers[index].buffer;
827 buffers[index].buffer = 0;
828 return buffer;
829 }
830
attachBuffer(size_t index,const sp<GraphicBuffer> & buffer)831 status_t Layer::BufferManager::attachBuffer(size_t index,
832 const sp<GraphicBuffer>& buffer)
833 {
834 BufferData* const buffers = mBufferData;
835 Mutex::Autolock _l(mLock);
836 buffers[index].buffer = buffer;
837 buffers[index].texture.dirty = true;
838 return NO_ERROR;
839 }
840
destroy(EGLDisplay dpy)841 status_t Layer::BufferManager::destroy(EGLDisplay dpy)
842 {
843 BufferData* const buffers = mBufferData;
844 size_t num;
845 { // scope for the lock
846 Mutex::Autolock _l(mLock);
847 num = mNumBuffers;
848 for (size_t i=0 ; i<num ; i++) {
849 buffers[i].buffer = 0;
850 }
851 }
852 for (size_t i=0 ; i<num ; i++) {
853 destroyTexture(&buffers[i].texture, dpy);
854 }
855 destroyTexture(&mFailoverTexture, dpy);
856 return NO_ERROR;
857 }
858
initEglImage(EGLDisplay dpy,const sp<GraphicBuffer> & buffer)859 status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
860 const sp<GraphicBuffer>& buffer)
861 {
862 status_t err = NO_INIT;
863 ssize_t index = mActiveBuffer;
864 if (index >= 0) {
865 if (!mFailover) {
866 Image& texture(mBufferData[index].texture);
867 err = mTextureManager.initEglImage(&texture, dpy, buffer);
868 // if EGLImage fails, we switch to regular texture mode, and we
869 // free all resources associated with using EGLImages.
870 if (err == NO_ERROR) {
871 mFailover = false;
872 destroyTexture(&mFailoverTexture, dpy);
873 } else {
874 mFailover = true;
875 const size_t num = mNumBuffers;
876 for (size_t i=0 ; i<num ; i++) {
877 destroyTexture(&mBufferData[i].texture, dpy);
878 }
879 }
880 } else {
881 // we failed once, don't try again
882 err = BAD_VALUE;
883 }
884 }
885 return err;
886 }
887
loadTexture(const Region & dirty,const GGLSurface & t)888 status_t Layer::BufferManager::loadTexture(
889 const Region& dirty, const GGLSurface& t)
890 {
891 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
892 }
893
destroyTexture(Image * tex,EGLDisplay dpy)894 status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
895 {
896 if (tex->name != -1U) {
897 glDeleteTextures(1, &tex->name);
898 tex->name = -1U;
899 }
900 if (tex->image != EGL_NO_IMAGE_KHR) {
901 eglDestroyImageKHR(dpy, tex->image);
902 tex->image = EGL_NO_IMAGE_KHR;
903 }
904 return NO_ERROR;
905 }
906
907 // ---------------------------------------------------------------------------
908
SurfaceLayer(const sp<SurfaceFlinger> & flinger,const sp<Layer> & owner)909 Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
910 const sp<Layer>& owner)
911 : Surface(flinger, owner->getIdentity(), owner)
912 {
913 }
914
~SurfaceLayer()915 Layer::SurfaceLayer::~SurfaceLayer()
916 {
917 }
918
requestBuffer(int index,uint32_t w,uint32_t h,uint32_t format,uint32_t usage)919 sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
920 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
921 {
922 sp<GraphicBuffer> buffer;
923 sp<Layer> owner(getOwner());
924 if (owner != 0) {
925 /*
926 * requestBuffer() cannot be called from the main thread
927 * as it could cause a dead-lock, since it may have to wait
928 * on conditions updated my the main thread.
929 */
930 buffer = owner->requestBuffer(index, w, h, format, usage);
931 }
932 return buffer;
933 }
934
setBufferCount(int bufferCount)935 status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
936 {
937 status_t err = DEAD_OBJECT;
938 sp<Layer> owner(getOwner());
939 if (owner != 0) {
940 /*
941 * setBufferCount() cannot be called from the main thread
942 * as it could cause a dead-lock, since it may have to wait
943 * on conditions updated my the main thread.
944 */
945 err = owner->setBufferCount(bufferCount);
946 }
947 return err;
948 }
949
950 // ---------------------------------------------------------------------------
951
952
953 }; // namespace android
954