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