Layer.cpp revision acbe67888f0bd65d5400400f0115bae6bd6199dc
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//#define LOG_NDEBUG 0 18#undef LOG_TAG 19#define LOG_TAG "Layer" 20#define ATRACE_TAG ATRACE_TAG_GRAPHICS 21 22#include <stdlib.h> 23#include <stdint.h> 24#include <sys/types.h> 25#include <math.h> 26 27#include <cutils/compiler.h> 28#include <cutils/native_handle.h> 29#include <cutils/properties.h> 30 31#include <utils/Errors.h> 32#include <utils/Log.h> 33#include <utils/NativeHandle.h> 34#include <utils/StopWatch.h> 35#include <utils/Trace.h> 36 37#include <ui/GraphicBuffer.h> 38#include <ui/PixelFormat.h> 39 40#include <gui/BufferItem.h> 41#include <gui/Surface.h> 42 43#include "clz.h" 44#include "Colorizer.h" 45#include "DisplayDevice.h" 46#include "Layer.h" 47#include "MonitoredProducer.h" 48#include "SurfaceFlinger.h" 49 50#include "DisplayHardware/HWComposer.h" 51 52#include "RenderEngine/RenderEngine.h" 53 54#define DEBUG_RESIZE 0 55 56namespace android { 57 58// --------------------------------------------------------------------------- 59 60int32_t Layer::sSequence = 1; 61 62Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, 63 const String8& name, uint32_t w, uint32_t h, uint32_t flags) 64 : contentDirty(false), 65 sequence(uint32_t(android_atomic_inc(&sSequence))), 66 mFlinger(flinger), 67 mTextureName(-1U), 68 mPremultipliedAlpha(true), 69 mName("unnamed"), 70 mFormat(PIXEL_FORMAT_NONE), 71 mTransactionFlags(0), 72 mPendingStateMutex(), 73 mPendingStates(), 74 mQueuedFrames(0), 75 mSidebandStreamChanged(false), 76 mCurrentTransform(0), 77 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), 78 mCurrentOpacity(true), 79 mCurrentFrameNumber(0), 80 mRefreshPending(false), 81 mFrameLatencyNeeded(false), 82 mFiltering(false), 83 mNeedsFiltering(false), 84 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2), 85 mProtectedByApp(false), 86 mHasSurface(false), 87 mClientRef(client), 88 mPotentialCursor(false), 89 mQueueItemLock(), 90 mQueueItemCondition(), 91 mQueueItems(), 92 mLastFrameNumberReceived(0), 93 mUpdateTexImageFailed(false), 94 mAutoRefresh(false) 95{ 96#ifdef USE_HWC2 97 ALOGV("Creating Layer %s", name.string()); 98#endif 99 100 mCurrentCrop.makeInvalid(); 101 mFlinger->getRenderEngine().genTextures(1, &mTextureName); 102 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); 103 104 uint32_t layerFlags = 0; 105 if (flags & ISurfaceComposerClient::eHidden) 106 layerFlags |= layer_state_t::eLayerHidden; 107 if (flags & ISurfaceComposerClient::eOpaque) 108 layerFlags |= layer_state_t::eLayerOpaque; 109 if (flags & ISurfaceComposerClient::eSecure) 110 layerFlags |= layer_state_t::eLayerSecure; 111 112 if (flags & ISurfaceComposerClient::eNonPremultiplied) 113 mPremultipliedAlpha = false; 114 115 mName = name; 116 117 mCurrentState.active.w = w; 118 mCurrentState.active.h = h; 119 mCurrentState.active.transform.set(0, 0); 120 mCurrentState.active.crop.makeInvalid(); 121 mCurrentState.active.finalCrop.makeInvalid(); 122 mCurrentState.z = 0; 123#ifdef USE_HWC2 124 mCurrentState.alpha = 1.0f; 125#else 126 mCurrentState.alpha = 0xFF; 127#endif 128 mCurrentState.layerStack = 0; 129 mCurrentState.flags = layerFlags; 130 mCurrentState.sequence = 0; 131 mCurrentState.requested = mCurrentState.active; 132 133 // drawing state & current state are identical 134 mDrawingState = mCurrentState; 135 136#ifdef USE_HWC2 137 const auto& hwc = flinger->getHwComposer(); 138 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY); 139 nsecs_t displayPeriod = activeConfig->getVsyncPeriod(); 140#else 141 nsecs_t displayPeriod = 142 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); 143#endif 144 mFrameTracker.setDisplayRefreshPeriod(displayPeriod); 145} 146 147void Layer::onFirstRef() { 148 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use 149 sp<IGraphicBufferProducer> producer; 150 sp<IGraphicBufferConsumer> consumer; 151 BufferQueue::createBufferQueue(&producer, &consumer); 152 mProducer = new MonitoredProducer(producer, mFlinger); 153 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName); 154 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); 155 mSurfaceFlingerConsumer->setContentsChangedListener(this); 156 mSurfaceFlingerConsumer->setName(mName); 157 158#ifdef TARGET_DISABLE_TRIPLE_BUFFERING 159#warning "disabling triple buffering" 160#else 161 mProducer->setMaxDequeuedBufferCount(2); 162#endif 163 164 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); 165 updateTransformHint(hw); 166} 167 168Layer::~Layer() { 169 sp<Client> c(mClientRef.promote()); 170 if (c != 0) { 171 c->detachLayer(this); 172 } 173 174 for (auto& point : mRemoteSyncPoints) { 175 point->setTransactionApplied(); 176 } 177 mFlinger->deleteTextureAsync(mTextureName); 178 mFrameTracker.logAndResetStats(mName); 179} 180 181// --------------------------------------------------------------------------- 182// callbacks 183// --------------------------------------------------------------------------- 184 185#ifdef USE_HWC2 186void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) { 187 if (mHwcLayers.empty()) { 188 return; 189 } 190 mSurfaceFlingerConsumer->setReleaseFence(releaseFence); 191} 192#else 193void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */, 194 HWComposer::HWCLayerInterface* layer) { 195 if (layer) { 196 layer->onDisplayed(); 197 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence()); 198 } 199} 200#endif 201 202void Layer::onFrameAvailable(const BufferItem& item) { 203 // Add this buffer from our internal queue tracker 204 { // Autolock scope 205 Mutex::Autolock lock(mQueueItemLock); 206 207 // Reset the frame number tracker when we receive the first buffer after 208 // a frame number reset 209 if (item.mFrameNumber == 1) { 210 mLastFrameNumberReceived = 0; 211 } 212 213 // Ensure that callbacks are handled in order 214 while (item.mFrameNumber != mLastFrameNumberReceived + 1) { 215 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, 216 ms2ns(500)); 217 if (result != NO_ERROR) { 218 ALOGE("[%s] Timed out waiting on callback", mName.string()); 219 } 220 } 221 222 mQueueItems.push_back(item); 223 android_atomic_inc(&mQueuedFrames); 224 225 // Wake up any pending callbacks 226 mLastFrameNumberReceived = item.mFrameNumber; 227 mQueueItemCondition.broadcast(); 228 } 229 230 mFlinger->signalLayerUpdate(); 231} 232 233void Layer::onFrameReplaced(const BufferItem& item) { 234 { // Autolock scope 235 Mutex::Autolock lock(mQueueItemLock); 236 237 // Ensure that callbacks are handled in order 238 while (item.mFrameNumber != mLastFrameNumberReceived + 1) { 239 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, 240 ms2ns(500)); 241 if (result != NO_ERROR) { 242 ALOGE("[%s] Timed out waiting on callback", mName.string()); 243 } 244 } 245 246 if (mQueueItems.empty()) { 247 ALOGE("Can't replace a frame on an empty queue"); 248 return; 249 } 250 mQueueItems.editItemAt(0) = item; 251 252 // Wake up any pending callbacks 253 mLastFrameNumberReceived = item.mFrameNumber; 254 mQueueItemCondition.broadcast(); 255 } 256} 257 258void Layer::onSidebandStreamChanged() { 259 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) { 260 // mSidebandStreamChanged was false 261 mFlinger->signalLayerUpdate(); 262 } 263} 264 265// called with SurfaceFlinger::mStateLock from the drawing thread after 266// the layer has been remove from the current state list (and just before 267// it's removed from the drawing state list) 268void Layer::onRemoved() { 269 mSurfaceFlingerConsumer->abandon(); 270} 271 272// --------------------------------------------------------------------------- 273// set-up 274// --------------------------------------------------------------------------- 275 276const String8& Layer::getName() const { 277 return mName; 278} 279 280status_t Layer::setBuffers( uint32_t w, uint32_t h, 281 PixelFormat format, uint32_t flags) 282{ 283 uint32_t const maxSurfaceDims = min( 284 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); 285 286 // never allow a surface larger than what our underlying GL implementation 287 // can handle. 288 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { 289 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); 290 return BAD_VALUE; 291 } 292 293 mFormat = format; 294 295 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false; 296 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; 297 mCurrentOpacity = getOpacityForFormat(format); 298 299 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); 300 mSurfaceFlingerConsumer->setDefaultBufferFormat(format); 301 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); 302 303 return NO_ERROR; 304} 305 306/* 307 * The layer handle is just a BBinder object passed to the client 308 * (remote process) -- we don't keep any reference on our side such that 309 * the dtor is called when the remote side let go of its reference. 310 * 311 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for 312 * this layer when the handle is destroyed. 313 */ 314class Layer::Handle : public BBinder, public LayerCleaner { 315 public: 316 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer) 317 : LayerCleaner(flinger, layer), owner(layer) {} 318 319 wp<Layer> owner; 320}; 321 322sp<IBinder> Layer::getHandle() { 323 Mutex::Autolock _l(mLock); 324 325 LOG_ALWAYS_FATAL_IF(mHasSurface, 326 "Layer::getHandle() has already been called"); 327 328 mHasSurface = true; 329 330 return new Handle(mFlinger, this); 331} 332 333sp<IGraphicBufferProducer> Layer::getProducer() const { 334 return mProducer; 335} 336 337// --------------------------------------------------------------------------- 338// h/w composer set-up 339// --------------------------------------------------------------------------- 340 341Rect Layer::getContentCrop() const { 342 // this is the crop rectangle that applies to the buffer 343 // itself (as opposed to the window) 344 Rect crop; 345 if (!mCurrentCrop.isEmpty()) { 346 // if the buffer crop is defined, we use that 347 crop = mCurrentCrop; 348 } else if (mActiveBuffer != NULL) { 349 // otherwise we use the whole buffer 350 crop = mActiveBuffer->getBounds(); 351 } else { 352 // if we don't have a buffer yet, we use an empty/invalid crop 353 crop.makeInvalid(); 354 } 355 return crop; 356} 357 358static Rect reduce(const Rect& win, const Region& exclude) { 359 if (CC_LIKELY(exclude.isEmpty())) { 360 return win; 361 } 362 if (exclude.isRect()) { 363 return win.reduce(exclude.getBounds()); 364 } 365 return Region(win).subtract(exclude).getBounds(); 366} 367 368Rect Layer::computeBounds() const { 369 const Layer::State& s(getDrawingState()); 370 return computeBounds(s.activeTransparentRegion); 371} 372 373Rect Layer::computeBounds(const Region& activeTransparentRegion) const { 374 const Layer::State& s(getDrawingState()); 375 Rect win(s.active.w, s.active.h); 376 if (!s.active.crop.isEmpty()) { 377 win.intersect(s.active.crop, &win); 378 } 379 // subtract the transparent region and snap to the bounds 380 return reduce(win, activeTransparentRegion); 381} 382 383FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const { 384 // the content crop is the area of the content that gets scaled to the 385 // layer's size. 386 FloatRect crop(getContentCrop()); 387 388 // the active.crop is the area of the window that gets cropped, but not 389 // scaled in any ways. 390 const State& s(getDrawingState()); 391 392 // apply the projection's clipping to the window crop in 393 // layerstack space, and convert-back to layer space. 394 // if there are no window scaling involved, this operation will map to full 395 // pixels in the buffer. 396 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have 397 // a viewport clipping and a window transform. we should use floating point to fix this. 398 399 Rect activeCrop(s.active.w, s.active.h); 400 if (!s.active.crop.isEmpty()) { 401 activeCrop = s.active.crop; 402 } 403 404 activeCrop = s.active.transform.transform(activeCrop); 405 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) { 406 activeCrop.clear(); 407 } 408 if (!s.active.finalCrop.isEmpty()) { 409 if(!activeCrop.intersect(s.active.finalCrop, &activeCrop)) { 410 activeCrop.clear(); 411 } 412 } 413 activeCrop = s.active.transform.inverse().transform(activeCrop); 414 415 // This needs to be here as transform.transform(Rect) computes the 416 // transformed rect and then takes the bounding box of the result before 417 // returning. This means 418 // transform.inverse().transform(transform.transform(Rect)) != Rect 419 // in which case we need to make sure the final rect is clipped to the 420 // display bounds. 421 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) { 422 activeCrop.clear(); 423 } 424 425 // subtract the transparent region and snap to the bounds 426 activeCrop = reduce(activeCrop, s.activeTransparentRegion); 427 428 // Transform the window crop to match the buffer coordinate system, 429 // which means using the inverse of the current transform set on the 430 // SurfaceFlingerConsumer. 431 uint32_t invTransform = mCurrentTransform; 432 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 433 /* 434 * the code below applies the display's inverse transform to the buffer 435 */ 436 uint32_t invTransformOrient = hw->getOrientationTransform(); 437 // calculate the inverse transform 438 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) { 439 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 440 NATIVE_WINDOW_TRANSFORM_FLIP_H; 441 // If the transform has been rotated the axis of flip has been swapped 442 // so we need to swap which flip operations we are performing 443 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; 444 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; 445 if (is_h_flipped != is_v_flipped) { 446 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 447 NATIVE_WINDOW_TRANSFORM_FLIP_H; 448 } 449 } 450 // and apply to the current transform 451 invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation(); 452 } 453 454 int winWidth = s.active.w; 455 int winHeight = s.active.h; 456 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 457 // If the activeCrop has been rotate the ends are rotated but not 458 // the space itself so when transforming ends back we can't rely on 459 // a modification of the axes of rotation. To account for this we 460 // need to reorient the inverse rotation in terms of the current 461 // axes of rotation. 462 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; 463 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; 464 if (is_h_flipped == is_v_flipped) { 465 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 466 NATIVE_WINDOW_TRANSFORM_FLIP_H; 467 } 468 winWidth = s.active.h; 469 winHeight = s.active.w; 470 } 471 const Rect winCrop = activeCrop.transform( 472 invTransform, s.active.w, s.active.h); 473 474 // below, crop is intersected with winCrop expressed in crop's coordinate space 475 float xScale = crop.getWidth() / float(winWidth); 476 float yScale = crop.getHeight() / float(winHeight); 477 478 float insetL = winCrop.left * xScale; 479 float insetT = winCrop.top * yScale; 480 float insetR = (winWidth - winCrop.right ) * xScale; 481 float insetB = (winHeight - winCrop.bottom) * yScale; 482 483 crop.left += insetL; 484 crop.top += insetT; 485 crop.right -= insetR; 486 crop.bottom -= insetB; 487 488 return crop; 489} 490 491#ifdef USE_HWC2 492void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice) 493#else 494void Layer::setGeometry( 495 const sp<const DisplayDevice>& hw, 496 HWComposer::HWCLayerInterface& layer) 497#endif 498{ 499#ifdef USE_HWC2 500 const auto hwcId = displayDevice->getHwcDisplayId(); 501 auto& hwcInfo = mHwcLayers[hwcId]; 502#else 503 layer.setDefaultState(); 504#endif 505 506 // enable this layer 507#ifdef USE_HWC2 508 hwcInfo.forceClientComposition = false; 509 510 if (isSecure() && !displayDevice->isSecure()) { 511 hwcInfo.forceClientComposition = true; 512 } 513 514 auto& hwcLayer = hwcInfo.layer; 515#else 516 layer.setSkip(false); 517 518 if (isSecure() && !hw->isSecure()) { 519 layer.setSkip(true); 520 } 521#endif 522 523 // this gives us only the "orientation" component of the transform 524 const State& s(getDrawingState()); 525#ifdef USE_HWC2 526 if (!isOpaque(s) || s.alpha != 1.0f) { 527 auto blendMode = mPremultipliedAlpha ? 528 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage; 529 auto error = hwcLayer->setBlendMode(blendMode); 530 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:" 531 " %s (%d)", mName.string(), to_string(blendMode).c_str(), 532 to_string(error).c_str(), static_cast<int32_t>(error)); 533 } 534#else 535 if (!isOpaque(s) || s.alpha != 0xFF) { 536 layer.setBlending(mPremultipliedAlpha ? 537 HWC_BLENDING_PREMULT : 538 HWC_BLENDING_COVERAGE); 539 } 540#endif 541 542 // apply the layer's transform, followed by the display's global transform 543 // here we're guaranteed that the layer's transform preserves rects 544 Region activeTransparentRegion(s.activeTransparentRegion); 545 if (!s.active.crop.isEmpty()) { 546 Rect activeCrop(s.active.crop); 547 activeCrop = s.active.transform.transform(activeCrop); 548#ifdef USE_HWC2 549 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) { 550#else 551 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) { 552#endif 553 activeCrop.clear(); 554 } 555 activeCrop = s.active.transform.inverse().transform(activeCrop); 556 // This needs to be here as transform.transform(Rect) computes the 557 // transformed rect and then takes the bounding box of the result before 558 // returning. This means 559 // transform.inverse().transform(transform.transform(Rect)) != Rect 560 // in which case we need to make sure the final rect is clipped to the 561 // display bounds. 562 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) { 563 activeCrop.clear(); 564 } 565 // mark regions outside the crop as transparent 566 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top)); 567 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, 568 s.active.w, s.active.h)); 569 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, 570 activeCrop.left, activeCrop.bottom)); 571 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top, 572 s.active.w, activeCrop.bottom)); 573 } 574 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion))); 575 if (!s.active.finalCrop.isEmpty()) { 576 if(!frame.intersect(s.active.finalCrop, &frame)) { 577 frame.clear(); 578 } 579 } 580#ifdef USE_HWC2 581 if (!frame.intersect(displayDevice->getViewport(), &frame)) { 582 frame.clear(); 583 } 584 const Transform& tr(displayDevice->getTransform()); 585 Rect transformedFrame = tr.transform(frame); 586 auto error = hwcLayer->setDisplayFrame(transformedFrame); 587 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame " 588 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left, 589 transformedFrame.top, transformedFrame.right, 590 transformedFrame.bottom, to_string(error).c_str(), 591 static_cast<int32_t>(error)); 592 593 FloatRect sourceCrop = computeCrop(displayDevice); 594 error = hwcLayer->setSourceCrop(sourceCrop); 595 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop " 596 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(), 597 sourceCrop.left, sourceCrop.top, sourceCrop.right, 598 sourceCrop.bottom, to_string(error).c_str(), 599 static_cast<int32_t>(error)); 600 601 error = hwcLayer->setPlaneAlpha(s.alpha); 602 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: " 603 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(), 604 static_cast<int32_t>(error)); 605 606 error = hwcLayer->setZOrder(s.z); 607 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", 608 mName.string(), s.z, to_string(error).c_str(), 609 static_cast<int32_t>(error)); 610#else 611 if (!frame.intersect(hw->getViewport(), &frame)) { 612 frame.clear(); 613 } 614 const Transform& tr(hw->getTransform()); 615 layer.setFrame(tr.transform(frame)); 616 layer.setCrop(computeCrop(hw)); 617 layer.setPlaneAlpha(s.alpha); 618#endif 619 620 /* 621 * Transformations are applied in this order: 622 * 1) buffer orientation/flip/mirror 623 * 2) state transformation (window manager) 624 * 3) layer orientation (screen orientation) 625 * (NOTE: the matrices are multiplied in reverse order) 626 */ 627 628 const Transform bufferOrientation(mCurrentTransform); 629 Transform transform(tr * s.active.transform * bufferOrientation); 630 631 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 632 /* 633 * the code below applies the display's inverse transform to the buffer 634 */ 635#ifdef USE_HWC2 636 uint32_t invTransform = displayDevice->getOrientationTransform(); 637#else 638 uint32_t invTransform = hw->getOrientationTransform(); 639#endif 640 uint32_t t_orientation = transform.getOrientation(); 641 // calculate the inverse transform 642 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 643 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 644 NATIVE_WINDOW_TRANSFORM_FLIP_H; 645 // If the transform has been rotated the axis of flip has been swapped 646 // so we need to swap which flip operations we are performing 647 bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; 648 bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; 649 if (is_h_flipped != is_v_flipped) { 650 t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 651 NATIVE_WINDOW_TRANSFORM_FLIP_H; 652 } 653 } 654 // and apply to the current transform 655 transform = Transform(t_orientation) * Transform(invTransform); 656 } 657 658 // this gives us only the "orientation" component of the transform 659 const uint32_t orientation = transform.getOrientation(); 660#ifdef USE_HWC2 661 if (orientation & Transform::ROT_INVALID) { 662 // we can only handle simple transformation 663 hwcInfo.forceClientComposition = true; 664 } else { 665 auto transform = static_cast<HWC2::Transform>(orientation); 666 auto error = hwcLayer->setTransform(transform); 667 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: " 668 "%s (%d)", mName.string(), to_string(transform).c_str(), 669 to_string(error).c_str(), static_cast<int32_t>(error)); 670 } 671#else 672 if (orientation & Transform::ROT_INVALID) { 673 // we can only handle simple transformation 674 layer.setSkip(true); 675 } else { 676 layer.setTransform(orientation); 677 } 678#endif 679} 680 681#ifdef USE_HWC2 682void Layer::forceClientComposition(int32_t hwcId) { 683 if (mHwcLayers.count(hwcId) == 0) { 684 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId); 685 return; 686 } 687 688 mHwcLayers[hwcId].forceClientComposition = true; 689} 690#endif 691 692#ifdef USE_HWC2 693void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) { 694 // Apply this display's projection's viewport to the visible region 695 // before giving it to the HWC HAL. 696 const Transform& tr = displayDevice->getTransform(); 697 const auto& viewport = displayDevice->getViewport(); 698 Region visible = tr.transform(visibleRegion.intersect(viewport)); 699 auto hwcId = displayDevice->getHwcDisplayId(); 700 auto& hwcLayer = mHwcLayers[hwcId].layer; 701 auto error = hwcLayer->setVisibleRegion(visible); 702 if (error != HWC2::Error::None) { 703 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(), 704 to_string(error).c_str(), static_cast<int32_t>(error)); 705 visible.dump(LOG_TAG); 706 } 707 708 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); 709 if (error != HWC2::Error::None) { 710 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(), 711 to_string(error).c_str(), static_cast<int32_t>(error)); 712 surfaceDamageRegion.dump(LOG_TAG); 713 } 714 715 auto compositionType = HWC2::Composition::Invalid; 716 if (mSidebandStream.get()) { 717 compositionType = HWC2::Composition::Sideband; 718 auto error = hwcLayer->setSidebandStream(mSidebandStream->handle()); 719 if (error != HWC2::Error::None) { 720 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", 721 mName.string(), mSidebandStream->handle(), 722 to_string(error).c_str(), static_cast<int32_t>(error)); 723 return; 724 } 725 } else { 726 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr) { 727 compositionType = HWC2::Composition::Client; 728 auto error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE); 729 if (error != HWC2::Error::None) { 730 ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(), 731 to_string(error).c_str(), static_cast<int32_t>(error)); 732 return; 733 } 734 } else { 735 if (mPotentialCursor) { 736 compositionType = HWC2::Composition::Cursor; 737 } 738 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence(); 739 auto error = hwcLayer->setBuffer(mActiveBuffer->handle, 740 acquireFence); 741 if (error != HWC2::Error::None) { 742 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), 743 mActiveBuffer->handle, to_string(error).c_str(), 744 static_cast<int32_t>(error)); 745 return; 746 } 747 // If it's not a cursor, default to device composition 748 } 749 } 750 751 if (mHwcLayers[hwcId].forceClientComposition) { 752 ALOGV("[%s] Forcing Client composition", mName.string()); 753 setCompositionType(hwcId, HWC2::Composition::Client); 754 } else if (compositionType != HWC2::Composition::Invalid) { 755 ALOGV("[%s] Requesting %s composition", mName.string(), 756 to_string(compositionType).c_str()); 757 setCompositionType(hwcId, compositionType); 758 } else { 759 ALOGV("[%s] Requesting Device composition", mName.string()); 760 setCompositionType(hwcId, HWC2::Composition::Device); 761 } 762} 763#else 764void Layer::setPerFrameData(const sp<const DisplayDevice>& hw, 765 HWComposer::HWCLayerInterface& layer) { 766 // we have to set the visible region on every frame because 767 // we currently free it during onLayerDisplayed(), which is called 768 // after HWComposer::commit() -- every frame. 769 // Apply this display's projection's viewport to the visible region 770 // before giving it to the HWC HAL. 771 const Transform& tr = hw->getTransform(); 772 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport())); 773 layer.setVisibleRegionScreen(visible); 774 layer.setSurfaceDamage(surfaceDamageRegion); 775 776 if (mSidebandStream.get()) { 777 layer.setSidebandStream(mSidebandStream); 778 } else { 779 // NOTE: buffer can be NULL if the client never drew into this 780 // layer yet, or if we ran out of memory 781 layer.setBuffer(mActiveBuffer); 782 } 783} 784#endif 785 786#ifdef USE_HWC2 787void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) { 788 auto hwcId = displayDevice->getHwcDisplayId(); 789 if (mHwcLayers.count(hwcId) == 0 || 790 getCompositionType(hwcId) != HWC2::Composition::Cursor) { 791 return; 792 } 793 794 // This gives us only the "orientation" component of the transform 795 const State& s(getCurrentState()); 796 797 // Apply the layer's transform, followed by the display's global transform 798 // Here we're guaranteed that the layer's transform preserves rects 799 Rect win(s.active.w, s.active.h); 800 if (!s.active.crop.isEmpty()) { 801 win.intersect(s.active.crop, &win); 802 } 803 // Subtract the transparent region and snap to the bounds 804 Rect bounds = reduce(win, s.activeTransparentRegion); 805 Rect frame(s.active.transform.transform(bounds)); 806 frame.intersect(displayDevice->getViewport(), &frame); 807 if (!s.active.finalCrop.isEmpty()) { 808 frame.intersect(s.active.finalCrop, &frame); 809 } 810 auto& displayTransform(displayDevice->getTransform()); 811 auto position = displayTransform.transform(frame); 812 813 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, 814 position.top); 815 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position " 816 "to (%d, %d): %s (%d)", mName.string(), position.left, 817 position.top, to_string(error).c_str(), 818 static_cast<int32_t>(error)); 819} 820#else 821void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */, 822 HWComposer::HWCLayerInterface& layer) { 823 int fenceFd = -1; 824 825 // TODO: there is a possible optimization here: we only need to set the 826 // acquire fence the first time a new buffer is acquired on EACH display. 827 828 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) { 829 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence(); 830 if (fence->isValid()) { 831 fenceFd = fence->dup(); 832 if (fenceFd == -1) { 833 ALOGW("failed to dup layer fence, skipping sync: %d", errno); 834 } 835 } 836 } 837 layer.setAcquireFenceFd(fenceFd); 838} 839 840Rect Layer::getPosition( 841 const sp<const DisplayDevice>& hw) 842{ 843 // this gives us only the "orientation" component of the transform 844 const State& s(getCurrentState()); 845 846 // apply the layer's transform, followed by the display's global transform 847 // here we're guaranteed that the layer's transform preserves rects 848 Rect win(s.active.w, s.active.h); 849 if (!s.active.crop.isEmpty()) { 850 win.intersect(s.active.crop, &win); 851 } 852 // subtract the transparent region and snap to the bounds 853 Rect bounds = reduce(win, s.activeTransparentRegion); 854 Rect frame(s.active.transform.transform(bounds)); 855 frame.intersect(hw->getViewport(), &frame); 856 if (!s.active.finalCrop.isEmpty()) { 857 frame.intersect(s.active.finalCrop, &frame); 858 } 859 const Transform& tr(hw->getTransform()); 860 return Rect(tr.transform(frame)); 861} 862#endif 863 864// --------------------------------------------------------------------------- 865// drawing... 866// --------------------------------------------------------------------------- 867 868void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const { 869 onDraw(hw, clip, false); 870} 871 872void Layer::draw(const sp<const DisplayDevice>& hw, 873 bool useIdentityTransform) const { 874 onDraw(hw, Region(hw->bounds()), useIdentityTransform); 875} 876 877void Layer::draw(const sp<const DisplayDevice>& hw) const { 878 onDraw(hw, Region(hw->bounds()), false); 879} 880 881void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip, 882 bool useIdentityTransform) const 883{ 884 ATRACE_CALL(); 885 886 if (CC_UNLIKELY(mActiveBuffer == 0)) { 887 // the texture has not been created yet, this Layer has 888 // in fact never been drawn into. This happens frequently with 889 // SurfaceView because the WindowManager can't know when the client 890 // has drawn the first time. 891 892 // If there is nothing under us, we paint the screen in black, otherwise 893 // we just skip this update. 894 895 // figure out if there is something below us 896 Region under; 897 const SurfaceFlinger::LayerVector& drawingLayers( 898 mFlinger->mDrawingState.layersSortedByZ); 899 const size_t count = drawingLayers.size(); 900 for (size_t i=0 ; i<count ; ++i) { 901 const sp<Layer>& layer(drawingLayers[i]); 902 if (layer.get() == static_cast<Layer const*>(this)) 903 break; 904 under.orSelf( hw->getTransform().transform(layer->visibleRegion) ); 905 } 906 // if not everything below us is covered, we plug the holes! 907 Region holes(clip.subtract(under)); 908 if (!holes.isEmpty()) { 909 clearWithOpenGL(hw, holes, 0, 0, 0, 1); 910 } 911 return; 912 } 913 914 // Bind the current buffer to the GL texture, and wait for it to be 915 // ready for us to draw into. 916 status_t err = mSurfaceFlingerConsumer->bindTextureImage(); 917 if (err != NO_ERROR) { 918 ALOGW("onDraw: bindTextureImage failed (err=%d)", err); 919 // Go ahead and draw the buffer anyway; no matter what we do the screen 920 // is probably going to have something visibly wrong. 921 } 922 923 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure()); 924 925 RenderEngine& engine(mFlinger->getRenderEngine()); 926 927 if (!blackOutLayer) { 928 // TODO: we could be more subtle with isFixedSize() 929 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize(); 930 931 // Query the texture matrix given our current filtering mode. 932 float textureMatrix[16]; 933 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering); 934 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix); 935 936 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 937 938 /* 939 * the code below applies the display's inverse transform to the texture transform 940 */ 941 942 // create a 4x4 transform matrix from the display transform flags 943 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1); 944 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1); 945 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1); 946 947 mat4 tr; 948 uint32_t transform = hw->getOrientationTransform(); 949 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) 950 tr = tr * rot90; 951 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) 952 tr = tr * flipH; 953 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) 954 tr = tr * flipV; 955 956 // calculate the inverse 957 tr = inverse(tr); 958 959 // and finally apply it to the original texture matrix 960 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); 961 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); 962 } 963 964 // Set things up for texturing. 965 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); 966 mTexture.setFiltering(useFiltering); 967 mTexture.setMatrix(textureMatrix); 968 969 engine.setupLayerTexturing(mTexture); 970 } else { 971 engine.setupLayerBlackedOut(); 972 } 973 drawWithOpenGL(hw, clip, useIdentityTransform); 974 engine.disableTexturing(); 975} 976 977 978void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw, 979 const Region& /* clip */, float red, float green, float blue, 980 float alpha) const 981{ 982 RenderEngine& engine(mFlinger->getRenderEngine()); 983 computeGeometry(hw, mMesh, false); 984 engine.setupFillWithColor(red, green, blue, alpha); 985 engine.drawMesh(mMesh); 986} 987 988void Layer::clearWithOpenGL( 989 const sp<const DisplayDevice>& hw, const Region& clip) const { 990 clearWithOpenGL(hw, clip, 0,0,0,0); 991} 992 993void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw, 994 const Region& /* clip */, bool useIdentityTransform) const { 995 const State& s(getDrawingState()); 996 997 computeGeometry(hw, mMesh, useIdentityTransform); 998 999 /* 1000 * NOTE: the way we compute the texture coordinates here produces 1001 * different results than when we take the HWC path -- in the later case 1002 * the "source crop" is rounded to texel boundaries. 1003 * This can produce significantly different results when the texture 1004 * is scaled by a large amount. 1005 * 1006 * The GL code below is more logical (imho), and the difference with 1007 * HWC is due to a limitation of the HWC API to integers -- a question 1008 * is suspend is whether we should ignore this problem or revert to 1009 * GL composition when a buffer scaling is applied (maybe with some 1010 * minimal value)? Or, we could make GL behave like HWC -- but this feel 1011 * like more of a hack. 1012 */ 1013 Rect win(computeBounds()); 1014 1015 if (!s.active.finalCrop.isEmpty()) { 1016 win = s.active.transform.transform(win); 1017 if (!win.intersect(s.active.finalCrop, &win)) { 1018 win.clear(); 1019 } 1020 win = s.active.transform.inverse().transform(win); 1021 if (!win.intersect(computeBounds(), &win)) { 1022 win.clear(); 1023 } 1024 } 1025 1026 float left = float(win.left) / float(s.active.w); 1027 float top = float(win.top) / float(s.active.h); 1028 float right = float(win.right) / float(s.active.w); 1029 float bottom = float(win.bottom) / float(s.active.h); 1030 1031 // TODO: we probably want to generate the texture coords with the mesh 1032 // here we assume that we only have 4 vertices 1033 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>()); 1034 texCoords[0] = vec2(left, 1.0f - top); 1035 texCoords[1] = vec2(left, 1.0f - bottom); 1036 texCoords[2] = vec2(right, 1.0f - bottom); 1037 texCoords[3] = vec2(right, 1.0f - top); 1038 1039 RenderEngine& engine(mFlinger->getRenderEngine()); 1040 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha); 1041 engine.drawMesh(mMesh); 1042 engine.disableBlending(); 1043} 1044 1045#ifdef USE_HWC2 1046void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, 1047 bool callIntoHwc) { 1048 if (mHwcLayers.count(hwcId) == 0) { 1049 ALOGE("setCompositionType called without a valid HWC layer"); 1050 return; 1051 } 1052 auto& hwcInfo = mHwcLayers[hwcId]; 1053 auto& hwcLayer = hwcInfo.layer; 1054 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), 1055 to_string(type).c_str(), static_cast<int>(callIntoHwc)); 1056 if (hwcInfo.compositionType != type) { 1057 ALOGV(" actually setting"); 1058 hwcInfo.compositionType = type; 1059 if (callIntoHwc) { 1060 auto error = hwcLayer->setCompositionType(type); 1061 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set " 1062 "composition type %s: %s (%d)", mName.string(), 1063 to_string(type).c_str(), to_string(error).c_str(), 1064 static_cast<int32_t>(error)); 1065 } 1066 } 1067} 1068 1069HWC2::Composition Layer::getCompositionType(int32_t hwcId) const { 1070 if (mHwcLayers.count(hwcId) == 0) { 1071 ALOGE("getCompositionType called without a valid HWC layer"); 1072 return HWC2::Composition::Invalid; 1073 } 1074 return mHwcLayers.at(hwcId).compositionType; 1075} 1076 1077void Layer::setClearClientTarget(int32_t hwcId, bool clear) { 1078 if (mHwcLayers.count(hwcId) == 0) { 1079 ALOGE("setClearClientTarget called without a valid HWC layer"); 1080 return; 1081 } 1082 mHwcLayers[hwcId].clearClientTarget = clear; 1083} 1084 1085bool Layer::getClearClientTarget(int32_t hwcId) const { 1086 if (mHwcLayers.count(hwcId) == 0) { 1087 ALOGE("getClearClientTarget called without a valid HWC layer"); 1088 return false; 1089 } 1090 return mHwcLayers.at(hwcId).clearClientTarget; 1091} 1092#endif 1093 1094uint32_t Layer::getProducerStickyTransform() const { 1095 int producerStickyTransform = 0; 1096 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform); 1097 if (ret != OK) { 1098 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__, 1099 strerror(-ret), ret); 1100 return 0; 1101 } 1102 return static_cast<uint32_t>(producerStickyTransform); 1103} 1104 1105uint64_t Layer::getHeadFrameNumber() const { 1106 Mutex::Autolock lock(mQueueItemLock); 1107 if (!mQueueItems.empty()) { 1108 return mQueueItems[0].mFrameNumber; 1109 } else { 1110 return mCurrentFrameNumber; 1111 } 1112} 1113 1114bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) { 1115 if (point->getFrameNumber() <= mCurrentFrameNumber) { 1116 // Don't bother with a SyncPoint, since we've already latched the 1117 // relevant frame 1118 return false; 1119 } 1120 1121 Mutex::Autolock lock(mLocalSyncPointMutex); 1122 mLocalSyncPoints.push_back(point); 1123 return true; 1124} 1125 1126void Layer::setFiltering(bool filtering) { 1127 mFiltering = filtering; 1128} 1129 1130bool Layer::getFiltering() const { 1131 return mFiltering; 1132} 1133 1134// As documented in libhardware header, formats in the range 1135// 0x100 - 0x1FF are specific to the HAL implementation, and 1136// are known to have no alpha channel 1137// TODO: move definition for device-specific range into 1138// hardware.h, instead of using hard-coded values here. 1139#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) 1140 1141bool Layer::getOpacityForFormat(uint32_t format) { 1142 if (HARDWARE_IS_DEVICE_FORMAT(format)) { 1143 return true; 1144 } 1145 switch (format) { 1146 case HAL_PIXEL_FORMAT_RGBA_8888: 1147 case HAL_PIXEL_FORMAT_BGRA_8888: 1148 return false; 1149 } 1150 // in all other case, we have no blending (also for unknown formats) 1151 return true; 1152} 1153 1154// ---------------------------------------------------------------------------- 1155// local state 1156// ---------------------------------------------------------------------------- 1157 1158static void boundPoint(vec2* point, const Rect& crop) { 1159 if (point->x < crop.left) { 1160 point->x = crop.left; 1161 } 1162 if (point->x > crop.right) { 1163 point->x = crop.right; 1164 } 1165 if (point->y < crop.top) { 1166 point->y = crop.top; 1167 } 1168 if (point->y > crop.bottom) { 1169 point->y = crop.bottom; 1170 } 1171} 1172 1173void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, 1174 bool useIdentityTransform) const 1175{ 1176 const Layer::State& s(getDrawingState()); 1177 const Transform tr(hw->getTransform()); 1178 const uint32_t hw_h = hw->getHeight(); 1179 Rect win(s.active.w, s.active.h); 1180 if (!s.active.crop.isEmpty()) { 1181 win.intersect(s.active.crop, &win); 1182 } 1183 // subtract the transparent region and snap to the bounds 1184 win = reduce(win, s.activeTransparentRegion); 1185 1186 vec2 lt = vec2(win.left, win.top); 1187 vec2 lb = vec2(win.left, win.bottom); 1188 vec2 rb = vec2(win.right, win.bottom); 1189 vec2 rt = vec2(win.right, win.top); 1190 1191 if (!useIdentityTransform) { 1192 lt = s.active.transform.transform(lt); 1193 lb = s.active.transform.transform(lb); 1194 rb = s.active.transform.transform(rb); 1195 rt = s.active.transform.transform(rt); 1196 } 1197 1198 if (!s.active.finalCrop.isEmpty()) { 1199 boundPoint(<, s.active.finalCrop); 1200 boundPoint(&lb, s.active.finalCrop); 1201 boundPoint(&rb, s.active.finalCrop); 1202 boundPoint(&rt, s.active.finalCrop); 1203 } 1204 1205 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); 1206 position[0] = tr.transform(lt); 1207 position[1] = tr.transform(lb); 1208 position[2] = tr.transform(rb); 1209 position[3] = tr.transform(rt); 1210 for (size_t i=0 ; i<4 ; i++) { 1211 position[i].y = hw_h - position[i].y; 1212 } 1213} 1214 1215bool Layer::isOpaque(const Layer::State& s) const 1216{ 1217 // if we don't have a buffer yet, we're translucent regardless of the 1218 // layer's opaque flag. 1219 if (mActiveBuffer == 0) { 1220 return false; 1221 } 1222 1223 // if the layer has the opaque flag, then we're always opaque, 1224 // otherwise we use the current buffer's format. 1225 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; 1226} 1227 1228bool Layer::isSecure() const 1229{ 1230 const Layer::State& s(mDrawingState); 1231 return (s.flags & layer_state_t::eLayerSecure); 1232} 1233 1234bool Layer::isProtected() const 1235{ 1236 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); 1237 return (activeBuffer != 0) && 1238 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); 1239} 1240 1241bool Layer::isFixedSize() const { 1242 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; 1243} 1244 1245bool Layer::isCropped() const { 1246 return !mCurrentCrop.isEmpty(); 1247} 1248 1249bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const { 1250 return mNeedsFiltering || hw->needsFiltering(); 1251} 1252 1253void Layer::setVisibleRegion(const Region& visibleRegion) { 1254 // always called from main thread 1255 this->visibleRegion = visibleRegion; 1256} 1257 1258void Layer::setCoveredRegion(const Region& coveredRegion) { 1259 // always called from main thread 1260 this->coveredRegion = coveredRegion; 1261} 1262 1263void Layer::setVisibleNonTransparentRegion(const Region& 1264 setVisibleNonTransparentRegion) { 1265 // always called from main thread 1266 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion; 1267} 1268 1269// ---------------------------------------------------------------------------- 1270// transaction 1271// ---------------------------------------------------------------------------- 1272 1273void Layer::pushPendingState() { 1274 if (!mCurrentState.modified) { 1275 return; 1276 } 1277 1278 // If this transaction is waiting on the receipt of a frame, generate a sync 1279 // point and send it to the remote layer. 1280 if (mCurrentState.handle != nullptr) { 1281 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get()); 1282 sp<Layer> handleLayer = handle->owner.promote(); 1283 if (handleLayer == nullptr) { 1284 ALOGE("[%s] Unable to promote Layer handle", mName.string()); 1285 // If we can't promote the layer we are intended to wait on, 1286 // then it is expired or otherwise invalid. Allow this transaction 1287 // to be applied as per normal (no synchronization). 1288 mCurrentState.handle = nullptr; 1289 } else { 1290 auto syncPoint = std::make_shared<SyncPoint>( 1291 mCurrentState.frameNumber); 1292 if (handleLayer->addSyncPoint(syncPoint)) { 1293 mRemoteSyncPoints.push_back(std::move(syncPoint)); 1294 } else { 1295 // We already missed the frame we're supposed to synchronize 1296 // on, so go ahead and apply the state update 1297 mCurrentState.handle = nullptr; 1298 } 1299 } 1300 1301 // Wake us up to check if the frame has been received 1302 setTransactionFlags(eTransactionNeeded); 1303 } 1304 mPendingStates.push_back(mCurrentState); 1305} 1306 1307void Layer::popPendingState() { 1308 auto oldFlags = mCurrentState.flags; 1309 mCurrentState = mPendingStates[0]; 1310 mCurrentState.flags = (oldFlags & ~mCurrentState.mask) | 1311 (mCurrentState.flags & mCurrentState.mask); 1312 1313 mPendingStates.removeAt(0); 1314} 1315 1316bool Layer::applyPendingStates() { 1317 bool stateUpdateAvailable = false; 1318 while (!mPendingStates.empty()) { 1319 if (mPendingStates[0].handle != nullptr) { 1320 if (mRemoteSyncPoints.empty()) { 1321 // If we don't have a sync point for this, apply it anyway. It 1322 // will be visually wrong, but it should keep us from getting 1323 // into too much trouble. 1324 ALOGE("[%s] No local sync point found", mName.string()); 1325 popPendingState(); 1326 stateUpdateAvailable = true; 1327 continue; 1328 } 1329 1330 if (mRemoteSyncPoints.front()->getFrameNumber() != 1331 mPendingStates[0].frameNumber) { 1332 ALOGE("[%s] Unexpected sync point frame number found", 1333 mName.string()); 1334 1335 // Signal our end of the sync point and then dispose of it 1336 mRemoteSyncPoints.front()->setTransactionApplied(); 1337 mRemoteSyncPoints.pop_front(); 1338 continue; 1339 } 1340 1341 if (mRemoteSyncPoints.front()->frameIsAvailable()) { 1342 // Apply the state update 1343 popPendingState(); 1344 stateUpdateAvailable = true; 1345 1346 // Signal our end of the sync point and then dispose of it 1347 mRemoteSyncPoints.front()->setTransactionApplied(); 1348 mRemoteSyncPoints.pop_front(); 1349 } else { 1350 break; 1351 } 1352 } else { 1353 popPendingState(); 1354 stateUpdateAvailable = true; 1355 } 1356 } 1357 1358 // If we still have pending updates, wake SurfaceFlinger back up and point 1359 // it at this layer so we can process them 1360 if (!mPendingStates.empty()) { 1361 setTransactionFlags(eTransactionNeeded); 1362 mFlinger->setTransactionFlags(eTraversalNeeded); 1363 } 1364 1365 mCurrentState.modified = false; 1366 return stateUpdateAvailable; 1367} 1368 1369void Layer::notifyAvailableFrames() { 1370 auto headFrameNumber = getHeadFrameNumber(); 1371 Mutex::Autolock lock(mLocalSyncPointMutex); 1372 for (auto& point : mLocalSyncPoints) { 1373 if (headFrameNumber >= point->getFrameNumber()) { 1374 point->setFrameAvailable(); 1375 } 1376 } 1377} 1378 1379uint32_t Layer::doTransaction(uint32_t flags) { 1380 ATRACE_CALL(); 1381 1382 pushPendingState(); 1383 if (!applyPendingStates()) { 1384 return 0; 1385 } 1386 1387 const Layer::State& s(getDrawingState()); 1388 const Layer::State& c(getCurrentState()); 1389 1390 const bool sizeChanged = (c.requested.w != s.requested.w) || 1391 (c.requested.h != s.requested.h); 1392 1393 if (sizeChanged) { 1394 // the size changed, we need to ask our client to request a new buffer 1395 ALOGD_IF(DEBUG_RESIZE, 1396 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n" 1397 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1398 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n" 1399 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1400 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", 1401 this, getName().string(), mCurrentTransform, mCurrentScalingMode, 1402 c.active.w, c.active.h, 1403 c.active.crop.left, 1404 c.active.crop.top, 1405 c.active.crop.right, 1406 c.active.crop.bottom, 1407 c.active.crop.getWidth(), 1408 c.active.crop.getHeight(), 1409 c.requested.w, c.requested.h, 1410 c.requested.crop.left, 1411 c.requested.crop.top, 1412 c.requested.crop.right, 1413 c.requested.crop.bottom, 1414 c.requested.crop.getWidth(), 1415 c.requested.crop.getHeight(), 1416 s.active.w, s.active.h, 1417 s.active.crop.left, 1418 s.active.crop.top, 1419 s.active.crop.right, 1420 s.active.crop.bottom, 1421 s.active.crop.getWidth(), 1422 s.active.crop.getHeight(), 1423 s.requested.w, s.requested.h, 1424 s.requested.crop.left, 1425 s.requested.crop.top, 1426 s.requested.crop.right, 1427 s.requested.crop.bottom, 1428 s.requested.crop.getWidth(), 1429 s.requested.crop.getHeight()); 1430 1431 // record the new size, form this point on, when the client request 1432 // a buffer, it'll get the new size. 1433 mSurfaceFlingerConsumer->setDefaultBufferSize( 1434 c.requested.w, c.requested.h); 1435 } 1436 1437 if (!isFixedSize()) { 1438 1439 const bool resizePending = (c.requested.w != c.active.w) || 1440 (c.requested.h != c.active.h); 1441 1442 if (resizePending && mSidebandStream == NULL) { 1443 // don't let Layer::doTransaction update the drawing state 1444 // if we have a pending resize, unless we are in fixed-size mode. 1445 // the drawing state will be updated only once we receive a buffer 1446 // with the correct size. 1447 // 1448 // in particular, we want to make sure the clip (which is part 1449 // of the geometry state) is latched together with the size but is 1450 // latched immediately when no resizing is involved. 1451 // 1452 // If a sideband stream is attached, however, we want to skip this 1453 // optimization so that transactions aren't missed when a buffer 1454 // never arrives 1455 1456 flags |= eDontUpdateGeometryState; 1457 } 1458 } 1459 1460 // always set active to requested, unless we're asked not to 1461 // this is used by Layer, which special cases resizes. 1462 if (flags & eDontUpdateGeometryState) { 1463 } else { 1464 Layer::State& editCurrentState(getCurrentState()); 1465 editCurrentState.active = c.requested; 1466 } 1467 1468 if (s.active != c.active) { 1469 // invalidate and recompute the visible regions if needed 1470 flags |= Layer::eVisibleRegion; 1471 } 1472 1473 if (c.sequence != s.sequence) { 1474 // invalidate and recompute the visible regions if needed 1475 flags |= eVisibleRegion; 1476 this->contentDirty = true; 1477 1478 // we may use linear filtering, if the matrix scales us 1479 const uint8_t type = c.active.transform.getType(); 1480 mNeedsFiltering = (!c.active.transform.preserveRects() || 1481 (type >= Transform::SCALE)); 1482 } 1483 1484 // Commit the transaction 1485 commitTransaction(); 1486 return flags; 1487} 1488 1489void Layer::commitTransaction() { 1490 mDrawingState = mCurrentState; 1491} 1492 1493uint32_t Layer::getTransactionFlags(uint32_t flags) { 1494 return android_atomic_and(~flags, &mTransactionFlags) & flags; 1495} 1496 1497uint32_t Layer::setTransactionFlags(uint32_t flags) { 1498 return android_atomic_or(flags, &mTransactionFlags); 1499} 1500 1501bool Layer::setPosition(float x, float y) { 1502 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y) 1503 return false; 1504 mCurrentState.sequence++; 1505 mCurrentState.requested.transform.set(x, y); 1506 mCurrentState.modified = true; 1507 setTransactionFlags(eTransactionNeeded); 1508 return true; 1509} 1510bool Layer::setLayer(uint32_t z) { 1511 if (mCurrentState.z == z) 1512 return false; 1513 mCurrentState.sequence++; 1514 mCurrentState.z = z; 1515 mCurrentState.modified = true; 1516 setTransactionFlags(eTransactionNeeded); 1517 return true; 1518} 1519bool Layer::setSize(uint32_t w, uint32_t h) { 1520 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) 1521 return false; 1522 mCurrentState.requested.w = w; 1523 mCurrentState.requested.h = h; 1524 mCurrentState.modified = true; 1525 setTransactionFlags(eTransactionNeeded); 1526 return true; 1527} 1528#ifdef USE_HWC2 1529bool Layer::setAlpha(float alpha) { 1530#else 1531bool Layer::setAlpha(uint8_t alpha) { 1532#endif 1533 if (mCurrentState.alpha == alpha) 1534 return false; 1535 mCurrentState.sequence++; 1536 mCurrentState.alpha = alpha; 1537 mCurrentState.modified = true; 1538 setTransactionFlags(eTransactionNeeded); 1539 return true; 1540} 1541bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) { 1542 mCurrentState.sequence++; 1543 mCurrentState.requested.transform.set( 1544 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy); 1545 mCurrentState.modified = true; 1546 setTransactionFlags(eTransactionNeeded); 1547 return true; 1548} 1549bool Layer::setTransparentRegionHint(const Region& transparent) { 1550 mCurrentState.requestedTransparentRegion = transparent; 1551 mCurrentState.modified = true; 1552 setTransactionFlags(eTransactionNeeded); 1553 return true; 1554} 1555bool Layer::setFlags(uint8_t flags, uint8_t mask) { 1556 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); 1557 if (mCurrentState.flags == newFlags) 1558 return false; 1559 mCurrentState.sequence++; 1560 mCurrentState.flags = newFlags; 1561 mCurrentState.mask = mask; 1562 mCurrentState.modified = true; 1563 setTransactionFlags(eTransactionNeeded); 1564 return true; 1565} 1566bool Layer::setCrop(const Rect& crop) { 1567 if (mCurrentState.requested.crop == crop) 1568 return false; 1569 mCurrentState.sequence++; 1570 mCurrentState.requested.crop = crop; 1571 mCurrentState.modified = true; 1572 setTransactionFlags(eTransactionNeeded); 1573 return true; 1574} 1575bool Layer::setFinalCrop(const Rect& crop) { 1576 if (mCurrentState.requested.finalCrop == crop) 1577 return false; 1578 mCurrentState.sequence++; 1579 mCurrentState.requested.finalCrop = crop; 1580 mCurrentState.modified = true; 1581 setTransactionFlags(eTransactionNeeded); 1582 return true; 1583} 1584 1585bool Layer::setLayerStack(uint32_t layerStack) { 1586 if (mCurrentState.layerStack == layerStack) 1587 return false; 1588 mCurrentState.sequence++; 1589 mCurrentState.layerStack = layerStack; 1590 mCurrentState.modified = true; 1591 setTransactionFlags(eTransactionNeeded); 1592 return true; 1593} 1594 1595void Layer::deferTransactionUntil(const sp<IBinder>& handle, 1596 uint64_t frameNumber) { 1597 mCurrentState.handle = handle; 1598 mCurrentState.frameNumber = frameNumber; 1599 // We don't set eTransactionNeeded, because just receiving a deferral 1600 // request without any other state updates shouldn't actually induce a delay 1601 mCurrentState.modified = true; 1602 pushPendingState(); 1603 mCurrentState.handle = nullptr; 1604 mCurrentState.frameNumber = 0; 1605 mCurrentState.modified = false; 1606} 1607 1608void Layer::useSurfaceDamage() { 1609 if (mFlinger->mForceFullDamage) { 1610 surfaceDamageRegion = Region::INVALID_REGION; 1611 } else { 1612 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage(); 1613 } 1614} 1615 1616void Layer::useEmptyDamage() { 1617 surfaceDamageRegion.clear(); 1618} 1619 1620// ---------------------------------------------------------------------------- 1621// pageflip handling... 1622// ---------------------------------------------------------------------------- 1623 1624bool Layer::shouldPresentNow(const DispSync& dispSync) const { 1625 if (mSidebandStreamChanged || mAutoRefresh) { 1626 return true; 1627 } 1628 1629 Mutex::Autolock lock(mQueueItemLock); 1630 if (mQueueItems.empty()) { 1631 return false; 1632 } 1633 auto timestamp = mQueueItems[0].mTimestamp; 1634 nsecs_t expectedPresent = 1635 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync); 1636 1637 // Ignore timestamps more than a second in the future 1638 bool isPlausible = timestamp < (expectedPresent + s2ns(1)); 1639 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible " 1640 "relative to expectedPresent %" PRId64, mName.string(), timestamp, 1641 expectedPresent); 1642 1643 bool isDue = timestamp < expectedPresent; 1644 return isDue || !isPlausible; 1645} 1646 1647bool Layer::onPreComposition() { 1648 mRefreshPending = false; 1649 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh; 1650} 1651 1652void Layer::onPostComposition() { 1653 if (mFrameLatencyNeeded) { 1654 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp(); 1655 mFrameTracker.setDesiredPresentTime(desiredPresentTime); 1656 1657 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence(); 1658 if (frameReadyFence->isValid()) { 1659 mFrameTracker.setFrameReadyFence(frameReadyFence); 1660 } else { 1661 // There was no fence for this frame, so assume that it was ready 1662 // to be presented at the desired present time. 1663 mFrameTracker.setFrameReadyTime(desiredPresentTime); 1664 } 1665 1666 const HWComposer& hwc = mFlinger->getHwComposer(); 1667#ifdef USE_HWC2 1668 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY); 1669#else 1670 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY); 1671#endif 1672 if (presentFence->isValid()) { 1673 mFrameTracker.setActualPresentFence(presentFence); 1674 } else { 1675 // The HWC doesn't support present fences, so use the refresh 1676 // timestamp instead. 1677 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY); 1678 mFrameTracker.setActualPresentTime(presentTime); 1679 } 1680 1681 mFrameTracker.advanceFrame(); 1682 mFrameLatencyNeeded = false; 1683 } 1684} 1685 1686#ifdef USE_HWC2 1687void Layer::releasePendingBuffer() { 1688 mSurfaceFlingerConsumer->releasePendingBuffer(); 1689} 1690#endif 1691 1692bool Layer::isVisible() const { 1693 const Layer::State& s(mDrawingState); 1694#ifdef USE_HWC2 1695 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f 1696 && (mActiveBuffer != NULL || mSidebandStream != NULL); 1697#else 1698 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha 1699 && (mActiveBuffer != NULL || mSidebandStream != NULL); 1700#endif 1701} 1702 1703Region Layer::latchBuffer(bool& recomputeVisibleRegions) 1704{ 1705 ATRACE_CALL(); 1706 1707 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) { 1708 // mSidebandStreamChanged was true 1709 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream(); 1710 if (mSidebandStream != NULL) { 1711 setTransactionFlags(eTransactionNeeded); 1712 mFlinger->setTransactionFlags(eTraversalNeeded); 1713 } 1714 recomputeVisibleRegions = true; 1715 1716 const State& s(getDrawingState()); 1717 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h))); 1718 } 1719 1720 Region outDirtyRegion; 1721 if (mQueuedFrames > 0 || mAutoRefresh) { 1722 1723 // if we've already called updateTexImage() without going through 1724 // a composition step, we have to skip this layer at this point 1725 // because we cannot call updateTeximage() without a corresponding 1726 // compositionComplete() call. 1727 // we'll trigger an update in onPreComposition(). 1728 if (mRefreshPending) { 1729 return outDirtyRegion; 1730 } 1731 1732 // Capture the old state of the layer for comparisons later 1733 const State& s(getDrawingState()); 1734 const bool oldOpacity = isOpaque(s); 1735 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer; 1736 1737 struct Reject : public SurfaceFlingerConsumer::BufferRejecter { 1738 Layer::State& front; 1739 Layer::State& current; 1740 bool& recomputeVisibleRegions; 1741 bool stickyTransformSet; 1742 Reject(Layer::State& front, Layer::State& current, 1743 bool& recomputeVisibleRegions, bool stickySet) 1744 : front(front), current(current), 1745 recomputeVisibleRegions(recomputeVisibleRegions), 1746 stickyTransformSet(stickySet) { 1747 } 1748 1749 virtual bool reject(const sp<GraphicBuffer>& buf, 1750 const BufferItem& item) { 1751 if (buf == NULL) { 1752 return false; 1753 } 1754 1755 uint32_t bufWidth = buf->getWidth(); 1756 uint32_t bufHeight = buf->getHeight(); 1757 1758 // check that we received a buffer of the right size 1759 // (Take the buffer's orientation into account) 1760 if (item.mTransform & Transform::ROT_90) { 1761 swap(bufWidth, bufHeight); 1762 } 1763 1764 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; 1765 if (front.active != front.requested) { 1766 1767 if (isFixedSize || 1768 (bufWidth == front.requested.w && 1769 bufHeight == front.requested.h)) 1770 { 1771 // Here we pretend the transaction happened by updating the 1772 // current and drawing states. Drawing state is only accessed 1773 // in this thread, no need to have it locked 1774 front.active = front.requested; 1775 1776 // We also need to update the current state so that 1777 // we don't end-up overwriting the drawing state with 1778 // this stale current state during the next transaction 1779 // 1780 // NOTE: We don't need to hold the transaction lock here 1781 // because State::active is only accessed from this thread. 1782 current.active = front.active; 1783 1784 // recompute visible region 1785 recomputeVisibleRegions = true; 1786 } 1787 1788 ALOGD_IF(DEBUG_RESIZE, 1789 "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n" 1790 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1791 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", 1792 bufWidth, bufHeight, item.mTransform, item.mScalingMode, 1793 front.active.w, front.active.h, 1794 front.active.crop.left, 1795 front.active.crop.top, 1796 front.active.crop.right, 1797 front.active.crop.bottom, 1798 front.active.crop.getWidth(), 1799 front.active.crop.getHeight(), 1800 front.requested.w, front.requested.h, 1801 front.requested.crop.left, 1802 front.requested.crop.top, 1803 front.requested.crop.right, 1804 front.requested.crop.bottom, 1805 front.requested.crop.getWidth(), 1806 front.requested.crop.getHeight()); 1807 } 1808 1809 if (!isFixedSize && !stickyTransformSet) { 1810 if (front.active.w != bufWidth || 1811 front.active.h != bufHeight) { 1812 // reject this buffer 1813 ALOGE("rejecting buffer: bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}", 1814 bufWidth, bufHeight, front.active.w, front.active.h); 1815 return true; 1816 } 1817 } 1818 1819 // if the transparent region has changed (this test is 1820 // conservative, but that's fine, worst case we're doing 1821 // a bit of extra work), we latch the new one and we 1822 // trigger a visible-region recompute. 1823 if (!front.activeTransparentRegion.isTriviallyEqual( 1824 front.requestedTransparentRegion)) { 1825 front.activeTransparentRegion = front.requestedTransparentRegion; 1826 1827 // We also need to update the current state so that 1828 // we don't end-up overwriting the drawing state with 1829 // this stale current state during the next transaction 1830 // 1831 // NOTE: We don't need to hold the transaction lock here 1832 // because State::active is only accessed from this thread. 1833 current.activeTransparentRegion = front.activeTransparentRegion; 1834 1835 // recompute visible region 1836 recomputeVisibleRegions = true; 1837 } 1838 1839 return false; 1840 } 1841 }; 1842 1843 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions, 1844 getProducerStickyTransform() != 0); 1845 1846 1847 // Check all of our local sync points to ensure that all transactions 1848 // which need to have been applied prior to the frame which is about to 1849 // be latched have signaled 1850 1851 auto headFrameNumber = getHeadFrameNumber(); 1852 bool matchingFramesFound = false; 1853 bool allTransactionsApplied = true; 1854 { 1855 Mutex::Autolock lock(mLocalSyncPointMutex); 1856 for (auto& point : mLocalSyncPoints) { 1857 if (point->getFrameNumber() > headFrameNumber) { 1858 break; 1859 } 1860 1861 matchingFramesFound = true; 1862 1863 if (!point->frameIsAvailable()) { 1864 // We haven't notified the remote layer that the frame for 1865 // this point is available yet. Notify it now, and then 1866 // abort this attempt to latch. 1867 point->setFrameAvailable(); 1868 allTransactionsApplied = false; 1869 break; 1870 } 1871 1872 allTransactionsApplied &= point->transactionIsApplied(); 1873 } 1874 } 1875 1876 if (matchingFramesFound && !allTransactionsApplied) { 1877 mFlinger->signalLayerUpdate(); 1878 return outDirtyRegion; 1879 } 1880 1881 // This boolean is used to make sure that SurfaceFlinger's shadow copy 1882 // of the buffer queue isn't modified when the buffer queue is returning 1883 // BufferItem's that weren't actually queued. This can happen in single 1884 // buffer mode. 1885 bool queuedBuffer = false; 1886 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r, 1887 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer, 1888 mLastFrameNumberReceived); 1889 if (updateResult == BufferQueue::PRESENT_LATER) { 1890 // Producer doesn't want buffer to be displayed yet. Signal a 1891 // layer update so we check again at the next opportunity. 1892 mFlinger->signalLayerUpdate(); 1893 return outDirtyRegion; 1894 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) { 1895 // If the buffer has been rejected, remove it from the shadow queue 1896 // and return early 1897 if (queuedBuffer) { 1898 Mutex::Autolock lock(mQueueItemLock); 1899 mQueueItems.removeAt(0); 1900 android_atomic_dec(&mQueuedFrames); 1901 } 1902 return outDirtyRegion; 1903 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) { 1904 // This can occur if something goes wrong when trying to create the 1905 // EGLImage for this buffer. If this happens, the buffer has already 1906 // been released, so we need to clean up the queue and bug out 1907 // early. 1908 if (queuedBuffer) { 1909 Mutex::Autolock lock(mQueueItemLock); 1910 mQueueItems.clear(); 1911 android_atomic_and(0, &mQueuedFrames); 1912 } 1913 1914 // Once we have hit this state, the shadow queue may no longer 1915 // correctly reflect the incoming BufferQueue's contents, so even if 1916 // updateTexImage starts working, the only safe course of action is 1917 // to continue to ignore updates. 1918 mUpdateTexImageFailed = true; 1919 1920 return outDirtyRegion; 1921 } 1922 1923 if (queuedBuffer) { 1924 // Autolock scope 1925 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); 1926 1927 Mutex::Autolock lock(mQueueItemLock); 1928 1929 // Remove any stale buffers that have been dropped during 1930 // updateTexImage 1931 while (mQueueItems[0].mFrameNumber != currentFrameNumber) { 1932 mQueueItems.removeAt(0); 1933 android_atomic_dec(&mQueuedFrames); 1934 } 1935 1936 mQueueItems.removeAt(0); 1937 } 1938 1939 1940 // Decrement the queued-frames count. Signal another event if we 1941 // have more frames pending. 1942 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) 1943 || mAutoRefresh) { 1944 mFlinger->signalLayerUpdate(); 1945 } 1946 1947 if (updateResult != NO_ERROR) { 1948 // something happened! 1949 recomputeVisibleRegions = true; 1950 return outDirtyRegion; 1951 } 1952 1953 // update the active buffer 1954 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(); 1955 if (mActiveBuffer == NULL) { 1956 // this can only happen if the very first buffer was rejected. 1957 return outDirtyRegion; 1958 } 1959 1960 mRefreshPending = true; 1961 mFrameLatencyNeeded = true; 1962 if (oldActiveBuffer == NULL) { 1963 // the first time we receive a buffer, we need to trigger a 1964 // geometry invalidation. 1965 recomputeVisibleRegions = true; 1966 } 1967 1968 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop()); 1969 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform()); 1970 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode()); 1971 if ((crop != mCurrentCrop) || 1972 (transform != mCurrentTransform) || 1973 (scalingMode != mCurrentScalingMode)) 1974 { 1975 mCurrentCrop = crop; 1976 mCurrentTransform = transform; 1977 mCurrentScalingMode = scalingMode; 1978 recomputeVisibleRegions = true; 1979 } 1980 1981 if (oldActiveBuffer != NULL) { 1982 uint32_t bufWidth = mActiveBuffer->getWidth(); 1983 uint32_t bufHeight = mActiveBuffer->getHeight(); 1984 if (bufWidth != uint32_t(oldActiveBuffer->width) || 1985 bufHeight != uint32_t(oldActiveBuffer->height)) { 1986 recomputeVisibleRegions = true; 1987 } 1988 } 1989 1990 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); 1991 if (oldOpacity != isOpaque(s)) { 1992 recomputeVisibleRegions = true; 1993 } 1994 1995 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); 1996 1997 // Remove any sync points corresponding to the buffer which was just 1998 // latched 1999 { 2000 Mutex::Autolock lock(mLocalSyncPointMutex); 2001 auto point = mLocalSyncPoints.begin(); 2002 while (point != mLocalSyncPoints.end()) { 2003 if (!(*point)->frameIsAvailable() || 2004 !(*point)->transactionIsApplied()) { 2005 // This sync point must have been added since we started 2006 // latching. Don't drop it yet. 2007 ++point; 2008 continue; 2009 } 2010 2011 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { 2012 point = mLocalSyncPoints.erase(point); 2013 } else { 2014 ++point; 2015 } 2016 } 2017 } 2018 2019 // FIXME: postedRegion should be dirty & bounds 2020 Region dirtyRegion(Rect(s.active.w, s.active.h)); 2021 2022 // transform the dirty region to window-manager space 2023 outDirtyRegion = (s.active.transform.transform(dirtyRegion)); 2024 } 2025 return outDirtyRegion; 2026} 2027 2028uint32_t Layer::getEffectiveUsage(uint32_t usage) const 2029{ 2030 // TODO: should we do something special if mSecure is set? 2031 if (mProtectedByApp) { 2032 // need a hardware-protected path to external video sink 2033 usage |= GraphicBuffer::USAGE_PROTECTED; 2034 } 2035 if (mPotentialCursor) { 2036 usage |= GraphicBuffer::USAGE_CURSOR; 2037 } 2038 usage |= GraphicBuffer::USAGE_HW_COMPOSER; 2039 return usage; 2040} 2041 2042void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const { 2043 uint32_t orientation = 0; 2044 if (!mFlinger->mDebugDisableTransformHint) { 2045 // The transform hint is used to improve performance, but we can 2046 // only have a single transform hint, it cannot 2047 // apply to all displays. 2048 const Transform& planeTransform(hw->getTransform()); 2049 orientation = planeTransform.getOrientation(); 2050 if (orientation & Transform::ROT_INVALID) { 2051 orientation = 0; 2052 } 2053 } 2054 mSurfaceFlingerConsumer->setTransformHint(orientation); 2055} 2056 2057// ---------------------------------------------------------------------------- 2058// debugging 2059// ---------------------------------------------------------------------------- 2060 2061void Layer::dump(String8& result, Colorizer& colorizer) const 2062{ 2063 const Layer::State& s(getDrawingState()); 2064 2065 colorizer.colorize(result, Colorizer::GREEN); 2066 result.appendFormat( 2067 "+ %s %p (%s)\n", 2068 getTypeId(), this, getName().string()); 2069 colorizer.reset(result); 2070 2071 s.activeTransparentRegion.dump(result, "transparentRegion"); 2072 visibleRegion.dump(result, "visibleRegion"); 2073 surfaceDamageRegion.dump(result, "surfaceDamageRegion"); 2074 sp<Client> client(mClientRef.promote()); 2075 2076 result.appendFormat( " " 2077 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), " 2078 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), " 2079 "isOpaque=%1d, invalidate=%1d, " 2080#ifdef USE_HWC2 2081 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" 2082#else 2083 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" 2084#endif 2085 " client=%p\n", 2086 s.layerStack, s.z, s.active.transform.tx(), s.active.transform.ty(), s.active.w, s.active.h, 2087 s.active.crop.left, s.active.crop.top, 2088 s.active.crop.right, s.active.crop.bottom, 2089 s.active.finalCrop.left, s.active.finalCrop.top, 2090 s.active.finalCrop.right, s.active.finalCrop.bottom, 2091 isOpaque(s), contentDirty, 2092 s.alpha, s.flags, 2093 s.active.transform[0][0], s.active.transform[0][1], 2094 s.active.transform[1][0], s.active.transform[1][1], 2095 client.get()); 2096 2097 sp<const GraphicBuffer> buf0(mActiveBuffer); 2098 uint32_t w0=0, h0=0, s0=0, f0=0; 2099 if (buf0 != 0) { 2100 w0 = buf0->getWidth(); 2101 h0 = buf0->getHeight(); 2102 s0 = buf0->getStride(); 2103 f0 = buf0->format; 2104 } 2105 result.appendFormat( 2106 " " 2107 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X]," 2108 " queued-frames=%d, mRefreshPending=%d\n", 2109 mFormat, w0, h0, s0,f0, 2110 mQueuedFrames, mRefreshPending); 2111 2112 if (mSurfaceFlingerConsumer != 0) { 2113 mSurfaceFlingerConsumer->dump(result, " "); 2114 } 2115} 2116 2117void Layer::dumpFrameStats(String8& result) const { 2118 mFrameTracker.dumpStats(result); 2119} 2120 2121void Layer::clearFrameStats() { 2122 mFrameTracker.clearStats(); 2123} 2124 2125void Layer::logFrameStats() { 2126 mFrameTracker.logAndResetStats(mName); 2127} 2128 2129void Layer::getFrameStats(FrameStats* outStats) const { 2130 mFrameTracker.getStats(outStats); 2131} 2132 2133// --------------------------------------------------------------------------- 2134 2135Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger, 2136 const sp<Layer>& layer) 2137 : mFlinger(flinger), mLayer(layer) { 2138} 2139 2140Layer::LayerCleaner::~LayerCleaner() { 2141 // destroy client resources 2142 mFlinger->onLayerDestroyed(mLayer); 2143} 2144 2145// --------------------------------------------------------------------------- 2146}; // namespace android 2147 2148#if defined(__gl_h_) 2149#error "don't include gl/gl.h in this file" 2150#endif 2151 2152#if defined(__gl2_h_) 2153#error "don't include gl2/gl2.h in this file" 2154#endif 2155