Layer.cpp revision 7a4d0dfd43558c299e6af6c4910ef76db9db3172
14bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber/* 24bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * Copyright (C) 2007 The Android Open Source Project 34bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * 44bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License"); 54bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * you may not use this file except in compliance with the License. 64bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * You may obtain a copy of the License at 74bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * 84bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * http://www.apache.org/licenses/LICENSE-2.0 94bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * 104bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * Unless required by applicable law or agreed to in writing, software 114bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS, 124bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * See the License for the specific language governing permissions and 144bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber * limitations under the License. 154bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber */ 164bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 174bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <stdlib.h> 184bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <stdint.h> 194bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <sys/types.h> 204bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 214bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <cutils/properties.h> 224bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <cutils/native_handle.h> 234bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 244bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <utils/Errors.h> 254bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <utils/Log.h> 264bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <utils/StopWatch.h> 274bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 284bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <ui/GraphicBuffer.h> 294bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <ui/PixelFormat.h> 304bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 314bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include <surfaceflinger/Surface.h> 324bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 334bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include "clz.h" 344bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include "GLExtensions.h" 354bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include "Layer.h" 364bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include "SurfaceFlinger.h" 374bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include "DisplayHardware/DisplayHardware.h" 384bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#include "DisplayHardware/HWComposer.h" 394bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 404bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 414bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber#define DEBUG_RESIZE 0 424bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 434bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 444bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Hubernamespace android { 454bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 464bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Hubertemplate <typename T> inline T min(T a, T b) { 474bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber return a<b ? a : b; 484bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber} 494bbfff2dbf3968c267c3b2ea9f8912a38372a9daAndreas Huber 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 (mUserClient != 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 // Move the active texture into slot 0 862 BufferData activeBufferData = mBufferData[mActiveBufferIndex]; 863 mBufferData[mActiveBufferIndex] = mBufferData[0]; 864 mBufferData[0] = activeBufferData; 865 mActiveBufferIndex = 0; 866 867 // Free the buffers that are no longer needed. 868 for (size_t i = size; i < mNumBuffers; i++) { 869 mBufferData[i].buffer = 0; 870 871 // Create a message to destroy the textures on SurfaceFlinger's GL 872 // thread. 873 class MessageDestroyTexture : public MessageBase { 874 Image mTexture; 875 EGLDisplay mDpy; 876 public: 877 MessageDestroyTexture(const Image& texture, EGLDisplay dpy) 878 : mTexture(texture), mDpy(dpy) { } 879 virtual bool handler() { 880 status_t err = Layer::BufferManager::destroyTexture( 881 &mTexture, mDpy); 882 LOGE_IF(err<0, "error destroying texture: %d (%s)", 883 mTexture.name, strerror(-err)); 884 return true; // XXX: err == 0; ???? 885 } 886 }; 887 888 MessageDestroyTexture *msg = new MessageDestroyTexture( 889 mBufferData[i].texture, dpy); 890 891 // Don't allow this texture to be cleaned up by 892 // BufferManager::destroy. 893 mBufferData[i].texture.name = -1U; 894 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR; 895 896 // Post the message to the SurfaceFlinger object. 897 flinger->postMessageAsync(msg); 898 } 899 } 900 901 mNumBuffers = size; 902 return NO_ERROR; 903} 904 905// only for debugging 906sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const { 907 return mBufferData[index].buffer; 908} 909 910status_t Layer::BufferManager::setActiveBufferIndex(size_t index) { 911 BufferData const * const buffers = mBufferData; 912 Mutex::Autolock _l(mLock); 913 mActiveBuffer = buffers[index].buffer; 914 mActiveBufferIndex = index; 915 return NO_ERROR; 916} 917 918size_t Layer::BufferManager::getActiveBufferIndex() const { 919 return mActiveBufferIndex; 920} 921 922Texture Layer::BufferManager::getActiveTexture() const { 923 Texture res; 924 if (mFailover || mActiveBufferIndex<0) { 925 res = mFailoverTexture; 926 } else { 927 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture; 928 } 929 return res; 930} 931 932sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const { 933 return mActiveBuffer; 934} 935 936bool Layer::BufferManager::hasActiveBuffer() const { 937 return mActiveBufferIndex >= 0; 938} 939 940sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index) 941{ 942 BufferData* const buffers = mBufferData; 943 sp<GraphicBuffer> buffer; 944 Mutex::Autolock _l(mLock); 945 buffer = buffers[index].buffer; 946 buffers[index].buffer = 0; 947 return buffer; 948} 949 950status_t Layer::BufferManager::attachBuffer(size_t index, 951 const sp<GraphicBuffer>& buffer) 952{ 953 BufferData* const buffers = mBufferData; 954 Mutex::Autolock _l(mLock); 955 buffers[index].buffer = buffer; 956 buffers[index].texture.dirty = true; 957 return NO_ERROR; 958} 959 960status_t Layer::BufferManager::destroy(EGLDisplay dpy) 961{ 962 BufferData* const buffers = mBufferData; 963 size_t num; 964 { // scope for the lock 965 Mutex::Autolock _l(mLock); 966 num = mNumBuffers; 967 for (size_t i=0 ; i<num ; i++) { 968 buffers[i].buffer = 0; 969 } 970 } 971 for (size_t i=0 ; i<num ; i++) { 972 destroyTexture(&buffers[i].texture, dpy); 973 } 974 destroyTexture(&mFailoverTexture, dpy); 975 return NO_ERROR; 976} 977 978status_t Layer::BufferManager::initEglImage(EGLDisplay dpy, 979 const sp<GraphicBuffer>& buffer) 980{ 981 status_t err = NO_INIT; 982 ssize_t index = mActiveBufferIndex; 983 if (index >= 0) { 984 if (!mFailover) { 985 Image& texture(mBufferData[index].texture); 986 err = mTextureManager.initEglImage(&texture, dpy, buffer); 987 // if EGLImage fails, we switch to regular texture mode, and we 988 // free all resources associated with using EGLImages. 989 if (err == NO_ERROR) { 990 mFailover = false; 991 destroyTexture(&mFailoverTexture, dpy); 992 } else { 993 mFailover = true; 994 const size_t num = mNumBuffers; 995 for (size_t i=0 ; i<num ; i++) { 996 destroyTexture(&mBufferData[i].texture, dpy); 997 } 998 } 999 } else { 1000 // we failed once, don't try again 1001 err = BAD_VALUE; 1002 } 1003 } 1004 return err; 1005} 1006 1007status_t Layer::BufferManager::loadTexture( 1008 const Region& dirty, const GGLSurface& t) 1009{ 1010 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t); 1011} 1012 1013status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy) 1014{ 1015 if (tex->name != -1U) { 1016 glDeleteTextures(1, &tex->name); 1017 tex->name = -1U; 1018 } 1019 if (tex->image != EGL_NO_IMAGE_KHR) { 1020 eglDestroyImageKHR(dpy, tex->image); 1021 tex->image = EGL_NO_IMAGE_KHR; 1022 } 1023 return NO_ERROR; 1024} 1025 1026// --------------------------------------------------------------------------- 1027 1028Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, 1029 const sp<Layer>& owner) 1030 : Surface(flinger, owner->getIdentity(), owner) 1031{ 1032} 1033 1034Layer::SurfaceLayer::~SurfaceLayer() 1035{ 1036} 1037 1038sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, 1039 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) 1040{ 1041 sp<GraphicBuffer> buffer; 1042 sp<Layer> owner(getOwner()); 1043 if (owner != 0) { 1044 /* 1045 * requestBuffer() cannot be called from the main thread 1046 * as it could cause a dead-lock, since it may have to wait 1047 * on conditions updated my the main thread. 1048 */ 1049 buffer = owner->requestBuffer(index, w, h, format, usage); 1050 } 1051 return buffer; 1052} 1053 1054status_t Layer::SurfaceLayer::setBufferCount(int bufferCount) 1055{ 1056 status_t err = DEAD_OBJECT; 1057 sp<Layer> owner(getOwner()); 1058 if (owner != 0) { 1059 /* 1060 * setBufferCount() cannot be called from the main thread 1061 * as it could cause a dead-lock, since it may have to wait 1062 * on conditions updated my the main thread. 1063 */ 1064 err = owner->setBufferCount(bufferCount); 1065 } 1066 return err; 1067} 1068 1069// --------------------------------------------------------------------------- 1070 1071 1072}; // namespace android 1073