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