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