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