Layer.cpp revision a350ff98692b3a50cad5cc93f9f83221242ca86a
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 // get the dirty region 534 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); 535 if (newFrontBuffer != NULL) { 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 } else { 572 // this should not happen unless we ran out of memory while 573 // allocating the buffer. we're hoping that things will get back 574 // to normal the next time the app tries to draw into this buffer. 575 // meanwhile, pretend the screen didn't update. 576 mPostedDirtyRegion.clear(); 577 } 578 579 if (lcblk->getQueuedCount()) { 580 // signal an event if we have more buffers waiting 581 mFlinger->signalEvent(); 582 } 583 584 /* a buffer was posted, so we need to call reloadTexture(), which 585 * will update our internal data structures (eg: EGLImageKHR or 586 * texture names). we need to do this even if mPostedDirtyRegion is 587 * empty -- it's orthogonal to the fact that a new buffer was posted, 588 * for instance, a degenerate case could be that the user did an empty 589 * update but repainted the buffer with appropriate content (after a 590 * resize for instance). 591 */ 592 reloadTexture( mPostedDirtyRegion ); 593} 594 595void Layer::unlockPageFlip( 596 const Transform& planeTransform, Region& outDirtyRegion) 597{ 598 Region dirtyRegion(mPostedDirtyRegion); 599 if (!dirtyRegion.isEmpty()) { 600 mPostedDirtyRegion.clear(); 601 // The dirty region is given in the layer's coordinate space 602 // transform the dirty region by the surface's transformation 603 // and the global transformation. 604 const Layer::State& s(drawingState()); 605 const Transform tr(planeTransform * s.transform); 606 dirtyRegion = tr.transform(dirtyRegion); 607 608 // At this point, the dirty region is in screen space. 609 // Make sure it's constrained by the visible region (which 610 // is in screen space as well). 611 dirtyRegion.andSelf(visibleRegionScreen); 612 outDirtyRegion.orSelf(dirtyRegion); 613 } 614 if (visibleRegionScreen.isEmpty()) { 615 // an invisible layer should not hold a freeze-lock 616 // (because it may never be updated and therefore never release it) 617 mFreezeLock.clear(); 618 } 619} 620 621void Layer::finishPageFlip() 622{ 623 ClientRef::Access sharedClient(mUserClientRef); 624 SharedBufferServer* lcblk(sharedClient.get()); 625 if (lcblk) { 626 int buf = mBufferManager.getActiveBufferIndex(); 627 if (buf >= 0) { 628 status_t err = lcblk->unlock( buf ); 629 LOGE_IF(err!=NO_ERROR, 630 "layer %p, buffer=%d wasn't locked!", 631 this, buf); 632 } 633 } 634} 635 636 637void Layer::dump(String8& result, char* buffer, size_t SIZE) const 638{ 639 LayerBaseClient::dump(result, buffer, SIZE); 640 641 ClientRef::Access sharedClient(mUserClientRef); 642 SharedBufferServer* lcblk(sharedClient.get()); 643 uint32_t totalTime = 0; 644 if (lcblk) { 645 SharedBufferStack::Statistics stats = lcblk->getStats(); 646 totalTime= stats.totalTime; 647 result.append( lcblk->dump(" ") ); 648 } 649 650 sp<const GraphicBuffer> buf0(getBuffer(0)); 651 sp<const GraphicBuffer> buf1(getBuffer(1)); 652 uint32_t w0=0, h0=0, s0=0; 653 uint32_t w1=0, h1=0, s1=0; 654 if (buf0 != 0) { 655 w0 = buf0->getWidth(); 656 h0 = buf0->getHeight(); 657 s0 = buf0->getStride(); 658 } 659 if (buf1 != 0) { 660 w1 = buf1->getWidth(); 661 h1 = buf1->getHeight(); 662 s1 = buf1->getStride(); 663 } 664 snprintf(buffer, SIZE, 665 " " 666 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u]," 667 " freezeLock=%p, dq-q-time=%u us\n", 668 mFormat, w0, h0, s0, w1, h1, s1, 669 getFreezeLock().get(), totalTime); 670 671 result.append(buffer); 672} 673 674// --------------------------------------------------------------------------- 675 676Layer::ClientRef::ClientRef() 677 : mControlBlock(0), mToken(-1) { 678} 679 680Layer::ClientRef::~ClientRef() { 681} 682 683int32_t Layer::ClientRef::getToken() const { 684 Mutex::Autolock _l(mLock); 685 return mToken; 686} 687 688sp<UserClient> Layer::ClientRef::getClient() const { 689 Mutex::Autolock _l(mLock); 690 return mUserClient.promote(); 691} 692 693status_t Layer::ClientRef::setToken(const sp<UserClient>& uc, 694 const sp<SharedBufferServer>& sharedClient, int32_t token) { 695 Mutex::Autolock _l(mLock); 696 697 { // scope for strong mUserClient reference 698 sp<UserClient> userClient(mUserClient.promote()); 699 if (mUserClient != 0 && mControlBlock != 0) { 700 mControlBlock->setStatus(NO_INIT); 701 } 702 } 703 704 mUserClient = uc; 705 mToken = token; 706 mControlBlock = sharedClient; 707 return NO_ERROR; 708} 709 710sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const { 711 return mUserClient.promote(); 712} 713 714// this class gives us access to SharedBufferServer safely 715// it makes sure the UserClient (and its associated shared memory) 716// won't go away while we're accessing it. 717Layer::ClientRef::Access::Access(const ClientRef& ref) 718 : mControlBlock(0) 719{ 720 Mutex::Autolock _l(ref.mLock); 721 mUserClientStrongRef = ref.mUserClient.promote(); 722 if (mUserClientStrongRef != 0) 723 mControlBlock = ref.mControlBlock; 724} 725 726Layer::ClientRef::Access::~Access() 727{ 728} 729 730// --------------------------------------------------------------------------- 731 732Layer::BufferManager::BufferManager(TextureManager& tm) 733 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm), 734 mActiveBuffer(-1), mFailover(false) 735{ 736} 737 738Layer::BufferManager::~BufferManager() 739{ 740} 741 742status_t Layer::BufferManager::resize(size_t size) 743{ 744 Mutex::Autolock _l(mLock); 745 mNumBuffers = size; 746 return NO_ERROR; 747} 748 749// only for debugging 750sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const { 751 return mBufferData[index].buffer; 752} 753 754status_t Layer::BufferManager::setActiveBufferIndex(size_t index) { 755 mActiveBuffer = index; 756 return NO_ERROR; 757} 758 759size_t Layer::BufferManager::getActiveBufferIndex() const { 760 return mActiveBuffer; 761} 762 763Texture Layer::BufferManager::getActiveTexture() const { 764 Texture res; 765 if (mFailover || mActiveBuffer<0) { 766 res = mFailoverTexture; 767 } else { 768 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture; 769 } 770 return res; 771} 772 773sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const { 774 sp<GraphicBuffer> result; 775 const ssize_t activeBuffer = mActiveBuffer; 776 if (activeBuffer >= 0) { 777 BufferData const * const buffers = mBufferData; 778 Mutex::Autolock _l(mLock); 779 result = buffers[activeBuffer].buffer; 780 } 781 return result; 782} 783 784sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index) 785{ 786 BufferData* const buffers = mBufferData; 787 sp<GraphicBuffer> buffer; 788 Mutex::Autolock _l(mLock); 789 buffer = buffers[index].buffer; 790 buffers[index].buffer = 0; 791 return buffer; 792} 793 794status_t Layer::BufferManager::attachBuffer(size_t index, 795 const sp<GraphicBuffer>& buffer) 796{ 797 BufferData* const buffers = mBufferData; 798 Mutex::Autolock _l(mLock); 799 buffers[index].buffer = buffer; 800 buffers[index].texture.dirty = true; 801 return NO_ERROR; 802} 803 804status_t Layer::BufferManager::destroy(EGLDisplay dpy) 805{ 806 BufferData* const buffers = mBufferData; 807 size_t num; 808 { // scope for the lock 809 Mutex::Autolock _l(mLock); 810 num = mNumBuffers; 811 for (size_t i=0 ; i<num ; i++) { 812 buffers[i].buffer = 0; 813 } 814 } 815 for (size_t i=0 ; i<num ; i++) { 816 destroyTexture(&buffers[i].texture, dpy); 817 } 818 destroyTexture(&mFailoverTexture, dpy); 819 return NO_ERROR; 820} 821 822status_t Layer::BufferManager::initEglImage(EGLDisplay dpy, 823 const sp<GraphicBuffer>& buffer) 824{ 825 status_t err = NO_INIT; 826 ssize_t index = mActiveBuffer; 827 if (index >= 0) { 828 if (!mFailover) { 829 Image& texture(mBufferData[index].texture); 830 err = mTextureManager.initEglImage(&texture, dpy, buffer); 831 // if EGLImage fails, we switch to regular texture mode, and we 832 // free all resources associated with using EGLImages. 833 if (err == NO_ERROR) { 834 mFailover = false; 835 destroyTexture(&mFailoverTexture, dpy); 836 } else { 837 mFailover = true; 838 const size_t num = mNumBuffers; 839 for (size_t i=0 ; i<num ; i++) { 840 destroyTexture(&mBufferData[i].texture, dpy); 841 } 842 } 843 } else { 844 // we failed once, don't try again 845 err = BAD_VALUE; 846 } 847 } 848 return err; 849} 850 851status_t Layer::BufferManager::loadTexture( 852 const Region& dirty, const GGLSurface& t) 853{ 854 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t); 855} 856 857status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy) 858{ 859 if (tex->name != -1U) { 860 glDeleteTextures(1, &tex->name); 861 tex->name = -1U; 862 } 863 if (tex->image != EGL_NO_IMAGE_KHR) { 864 eglDestroyImageKHR(dpy, tex->image); 865 tex->image = EGL_NO_IMAGE_KHR; 866 } 867 return NO_ERROR; 868} 869 870// --------------------------------------------------------------------------- 871 872Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, 873 const sp<Layer>& owner) 874 : Surface(flinger, owner->getIdentity(), owner) 875{ 876} 877 878Layer::SurfaceLayer::~SurfaceLayer() 879{ 880} 881 882sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, 883 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) 884{ 885 sp<GraphicBuffer> buffer; 886 sp<Layer> owner(getOwner()); 887 if (owner != 0) { 888 /* 889 * requestBuffer() cannot be called from the main thread 890 * as it could cause a dead-lock, since it may have to wait 891 * on conditions updated my the main thread. 892 */ 893 buffer = owner->requestBuffer(index, w, h, format, usage); 894 } 895 return buffer; 896} 897 898status_t Layer::SurfaceLayer::setBufferCount(int bufferCount) 899{ 900 status_t err = DEAD_OBJECT; 901 sp<Layer> owner(getOwner()); 902 if (owner != 0) { 903 /* 904 * setBufferCount() cannot be called from the main thread 905 * as it could cause a dead-lock, since it may have to wait 906 * on conditions updated my the main thread. 907 */ 908 err = owner->setBufferCount(bufferCount); 909 } 910 return err; 911} 912 913// --------------------------------------------------------------------------- 914 915 916}; // namespace android 917