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