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