The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Melody Hsu | e4ef87f | 2024-03-26 23:54:45 +0000 | [diff] [blame] | 18 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 19 | #pragma clang diagnostic push |
| 20 | #pragma clang diagnostic ignored "-Wconversion" |
| 21 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 22 | //#define LOG_NDEBUG 0 |
| 23 | #undef LOG_TAG |
| 24 | #define LOG_TAG "Layer" |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 25 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 26 | |
[1;3C | 2b9fc25 | 2021-02-04 16:16:50 -0800 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 30 | #include <common/trace.h> |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 31 | #include <compositionengine/CompositionEngine.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 32 | #include <compositionengine/Display.h> |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 33 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 34 | #include <compositionengine/OutputLayer.h> |
| 35 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 36 | #include <cutils/compiler.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | #include <cutils/native_handle.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 39 | #include <ftl/enum.h> |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 40 | #include <ftl/fake_guard.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 41 | #include <gui/BufferItem.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 42 | #include <gui/Surface.h> |
Alec Mouri | e60041e | 2019-06-14 18:59:51 -0700 | [diff] [blame] | 43 | #include <math.h> |
chaviw | 250bcbb | 2020-08-05 11:17:54 -0700 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 45 | #include <renderengine/RenderEngine.h> |
Alec Mouri | e60041e | 2019-06-14 18:59:51 -0700 | [diff] [blame] | 46 | #include <stdint.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <sys/types.h> |
Alec Mouri | dda07d9 | 2022-04-25 22:39:25 +0000 | [diff] [blame] | 49 | #include <system/graphics-base-v1.0.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 50 | #include <ui/DebugUtils.h> |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 51 | #include <ui/FloatRect.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 52 | #include <ui/GraphicBuffer.h> |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 53 | #include <ui/HdrRenderTypeUtils.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 54 | #include <ui/PixelFormat.h> |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 55 | #include <ui/Rect.h> |
| 56 | #include <ui/Transform.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | #include <utils/Errors.h> |
| 58 | #include <utils/Log.h> |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 59 | #include <utils/NativeHandle.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | #include <utils/StopWatch.h> |
| 61 | |
Alec Mouri | e60041e | 2019-06-14 18:59:51 -0700 | [diff] [blame] | 62 | #include <algorithm> |
Vishnu Nair | 71fcf91 | 2022-10-18 09:14:20 -0700 | [diff] [blame] | 63 | #include <optional> |
Alec Mouri | e60041e | 2019-06-14 18:59:51 -0700 | [diff] [blame] | 64 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 65 | #include "DisplayDevice.h" |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 66 | #include "DisplayHardware/HWComposer.h" |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 67 | #include "FrameTimeline.h" |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 68 | #include "FrameTracer/FrameTracer.h" |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 69 | #include "FrontEnd/LayerCreationArgs.h" |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 70 | #include "FrontEnd/LayerHandle.h" |
Melody Hsu | e4ef87f | 2024-03-26 23:54:45 +0000 | [diff] [blame] | 71 | #include "Layer.h" |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 72 | #include "LayerProtoHelper.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | #include "SurfaceFlinger.h" |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 74 | #include "TimeStats/TimeStats.h" |
Melody Hsu | e4ef87f | 2024-03-26 23:54:45 +0000 | [diff] [blame] | 75 | #include "TransactionCallbackInvoker.h" |
Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame] | 76 | #include "TunnelModeEnabledReporter.h" |
Alec Mouri | 9892aac | 2023-12-11 21:16:59 +0000 | [diff] [blame] | 77 | #include "Utils/FenceUtils.h" |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 78 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 79 | #define DEBUG_RESIZE 0 |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 80 | #define EARLY_RELEASE_ENABLED false |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | namespace android { |
Alec Mouri | 9892aac | 2023-12-11 21:16:59 +0000 | [diff] [blame] | 83 | using namespace std::chrono_literals; |
Marin Shalamanov | 1876e2e | 2020-12-04 13:23:59 +0100 | [diff] [blame] | 84 | namespace { |
| 85 | constexpr int kDumpTableRowLength = 159; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 86 | |
Prabir Pradhan | da0f62c | 2022-07-22 19:53:04 +0000 | [diff] [blame] | 87 | const ui::Transform kIdentityTransform; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 88 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 89 | TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) { |
| 90 | using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility; |
| 91 | using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness; |
| 92 | const auto frameRateCompatibility = [frameRate] { |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 93 | switch (frameRate.vote.type) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 94 | case Layer::FrameRateCompatibility::Default: |
| 95 | return FrameRateCompatibility::Default; |
| 96 | case Layer::FrameRateCompatibility::ExactOrMultiple: |
| 97 | return FrameRateCompatibility::ExactOrMultiple; |
| 98 | default: |
| 99 | return FrameRateCompatibility::Undefined; |
| 100 | } |
| 101 | }(); |
| 102 | |
| 103 | const auto seamlessness = [frameRate] { |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 104 | switch (frameRate.vote.seamlessness) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 105 | case scheduler::Seamlessness::OnlySeamless: |
| 106 | return Seamlessness::ShouldBeSeamless; |
| 107 | case scheduler::Seamlessness::SeamedAndSeamless: |
| 108 | return Seamlessness::NotRequired; |
| 109 | default: |
| 110 | return Seamlessness::Undefined; |
| 111 | } |
| 112 | }(); |
| 113 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 114 | return TimeStats::SetFrameRateVote{.frameRate = frameRate.vote.rate.getValue(), |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 115 | .frameRateCompatibility = frameRateCompatibility, |
| 116 | .seamlessness = seamlessness}; |
| 117 | } |
| 118 | |
Marin Shalamanov | 1876e2e | 2020-12-04 13:23:59 +0100 | [diff] [blame] | 119 | } // namespace |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 121 | using namespace ftl::flag_operators; |
| 122 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 123 | using base::StringAppendF; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 124 | using frontend::LayerSnapshot; |
| 125 | using frontend::RoundedCornerState; |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 126 | using gui::GameMode; |
| 127 | using gui::LayerMetadata; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 128 | using gui::WindowInfo; |
Vishnu Nair | 494a2e4 | 2023-11-10 17:21:19 -0800 | [diff] [blame] | 129 | using ui::Size; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 130 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 131 | using PresentState = frametimeline::SurfaceFrame::PresentState; |
| 132 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 133 | Layer::Layer(const surfaceflinger::LayerCreationArgs& args) |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 134 | : sequence(args.sequence), |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 135 | mFlinger(sp<SurfaceFlinger>::fromExisting(args.flinger)), |
Arthur Hung | 23e0750 | 2021-10-15 11:58:19 +0000 | [diff] [blame] | 136 | mName(base::StringPrintf("%s#%d", args.name.c_str(), sequence)), |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 137 | mWindowType(static_cast<WindowInfo::Type>( |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 138 | args.metadata.getInt32(gui::METADATA_WINDOW_TYPE, 0))) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 139 | ALOGV("Creating Layer %s", getDebugName()); |
| 140 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 141 | mDrawingState.crop = {0, 0, -1, -1}; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 142 | mDrawingState.sequence = 0; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 143 | mDrawingState.transform.set(0, 0); |
| 144 | mDrawingState.frameNumber = 0; |
Alec Mouri | 21d9432 | 2023-10-17 19:51:39 +0000 | [diff] [blame] | 145 | mDrawingState.previousFrameNumber = 0; |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 146 | mDrawingState.barrierFrameNumber = 0; |
| 147 | mDrawingState.producerId = 0; |
| 148 | mDrawingState.barrierProducerId = 0; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 149 | mDrawingState.bufferTransform = 0; |
| 150 | mDrawingState.transformToDisplayInverse = false; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 151 | mDrawingState.acquireFence = sp<Fence>::make(-1); |
| 152 | mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence); |
Alec Mouri | 74c7ae1 | 2023-03-26 02:57:47 +0000 | [diff] [blame] | 153 | mDrawingState.dataspace = ui::Dataspace::V0_SRGB; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 154 | mDrawingState.metadata = args.metadata; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 155 | mDrawingState.frameTimelineInfo = {}; |
| 156 | mDrawingState.postTime = -1; |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 157 | mFrameTracker.setDisplayRefreshPeriod( |
| 158 | args.flinger->mScheduler->getPacesetterVsyncPeriod().ns()); |
Robert Carr | 2e102c9 | 2018-10-23 12:11:15 -0700 | [diff] [blame] | 159 | |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 160 | mOwnerUid = args.ownerUid; |
| 161 | mOwnerPid = args.ownerPid; |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 162 | mOwnerAppId = mOwnerUid % PER_USER_RANGE; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 163 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 164 | mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow; |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 165 | mLayerFEs.emplace_back(frontend::LayerHierarchy::TraversalPath{static_cast<uint32_t>(sequence)}, |
| 166 | args.flinger->getFactory().createLayerFE(mName, this)); |
Dominik Laskowski | 7584836 | 2019-11-11 17:57:20 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void Layer::onFirstRef() { |
| 170 | mFlinger->onLayerFirstRef(this); |
Dan Stoza | 436ccf3 | 2018-06-21 12:10:12 -0700 | [diff] [blame] | 171 | } |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 172 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 173 | Layer::~Layer() { |
Patrick Williams | 70093fd | 2022-12-12 16:51:52 +0000 | [diff] [blame] | 174 | LOG_ALWAYS_FATAL_IF(std::this_thread::get_id() != mFlinger->mMainThreadId, |
| 175 | "Layer destructor called off the main thread."); |
| 176 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 177 | if (mBufferInfo.mBuffer != nullptr) { |
| 178 | callReleaseBufferCallback(mDrawingState.releaseBufferListener, |
| 179 | mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber, |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 180 | mBufferInfo.mFence); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 181 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 182 | const int32_t layerId = getSequence(); |
| 183 | mFlinger->mTimeStats->onDestroy(layerId); |
| 184 | mFlinger->mFrameTracer->onDestroy(layerId); |
| 185 | |
chaviw | 74d90ad | 2019-04-26 14:45:26 -0700 | [diff] [blame] | 186 | mFlinger->onLayerDestroyed(this); |
Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame] | 187 | |
Alec Mouri | d05a745 | 2024-09-20 13:44:02 +0000 | [diff] [blame] | 188 | const auto currentTime = std::chrono::steady_clock::now(); |
| 189 | if (mBufferInfo.mTimeSinceDataspaceUpdate > std::chrono::steady_clock::time_point::min()) { |
| 190 | mFlinger->mLayerEvents.emplace_back(mOwnerUid, getSequence(), mBufferInfo.mDataspace, |
| 191 | std::chrono::duration_cast<std::chrono::milliseconds>( |
| 192 | currentTime - |
| 193 | mBufferInfo.mTimeSinceDataspaceUpdate)); |
| 194 | } |
| 195 | |
Robert Carr | 3e2a299 | 2021-06-11 13:42:55 -0700 | [diff] [blame] | 196 | if (mDrawingState.sidebandStream != nullptr) { |
| 197 | mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount(); |
| 198 | } |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 199 | if (hasTrustedPresentationListener()) { |
| 200 | mFlinger->mNumTrustedPresentationListeners--; |
Vishnu Nair | 781d725 | 2023-01-30 18:16:01 +0000 | [diff] [blame] | 201 | updateTrustedPresentationState(nullptr, nullptr, -1 /* time_in_ms */, true /* leaveState*/); |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 202 | } |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 205 | // --------------------------------------------------------------------------- |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 206 | // set-up |
| 207 | // --------------------------------------------------------------------------- |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 208 | sp<IBinder> Layer::getHandle() { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 209 | Mutex::Autolock _l(mLock); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 210 | if (mGetHandleCalled) { |
| 211 | ALOGE("Get handle called twice" ); |
| 212 | return nullptr; |
| 213 | } |
| 214 | mGetHandleCalled = true; |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 215 | mHandleAlive = true; |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 216 | return sp<LayerHandle>::make(mFlinger, sp<Layer>::fromExisting(this)); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // --------------------------------------------------------------------------- |
| 220 | // h/w composer set-up |
| 221 | // --------------------------------------------------------------------------- |
| 222 | |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 223 | // No early returns. |
Vishnu Nair | 781d725 | 2023-01-30 18:16:01 +0000 | [diff] [blame] | 224 | void Layer::updateTrustedPresentationState(const DisplayDevice* display, |
| 225 | const frontend::LayerSnapshot* snapshot, |
| 226 | int64_t time_in_ms, bool leaveState) { |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 227 | if (!hasTrustedPresentationListener()) { |
| 228 | return; |
| 229 | } |
| 230 | const bool lastState = mLastComputedTrustedPresentationState; |
| 231 | mLastComputedTrustedPresentationState = false; |
| 232 | |
| 233 | if (!leaveState) { |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 234 | const auto outputLayer = findOutputLayerForDisplay(display, snapshot->path); |
Chavi Weingarten | 545da0e | 2023-02-09 14:55:57 +0000 | [diff] [blame] | 235 | if (outputLayer != nullptr) { |
| 236 | if (outputLayer->getState().coveredRegionExcludingDisplayOverlays) { |
| 237 | Region coveredRegion = |
| 238 | *outputLayer->getState().coveredRegionExcludingDisplayOverlays; |
| 239 | mLastComputedTrustedPresentationState = |
| 240 | computeTrustedPresentationState(snapshot->geomLayerBounds, |
| 241 | snapshot->sourceBounds(), coveredRegion, |
| 242 | snapshot->transformedBounds, |
| 243 | snapshot->alpha, |
| 244 | snapshot->geomLayerTransform, |
| 245 | mTrustedPresentationThresholds); |
| 246 | } else { |
| 247 | ALOGE("CoveredRegionExcludingDisplayOverlays was not set for %s. Don't compute " |
| 248 | "TrustedPresentationState", |
| 249 | getDebugName()); |
| 250 | } |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | const bool newState = mLastComputedTrustedPresentationState; |
| 254 | if (lastState && !newState) { |
| 255 | // We were in the trusted presentation state, but now we left it, |
| 256 | // emit the callback if needed |
| 257 | if (mLastReportedTrustedPresentationState) { |
| 258 | mLastReportedTrustedPresentationState = false; |
| 259 | mTrustedPresentationListener.invoke(false); |
| 260 | } |
| 261 | // Reset the timer |
| 262 | mEnteredTrustedPresentationStateTime = -1; |
| 263 | } else if (!lastState && newState) { |
| 264 | // We were not in the trusted presentation state, but we entered it, begin the timer |
| 265 | // and make sure this gets called at least once more! |
| 266 | mEnteredTrustedPresentationStateTime = time_in_ms; |
| 267 | mFlinger->forceFutureUpdate(mTrustedPresentationThresholds.stabilityRequirementMs * 1.5); |
| 268 | } |
| 269 | |
| 270 | // Has the timer elapsed, but we are still in the state? Emit a callback if needed |
| 271 | if (!mLastReportedTrustedPresentationState && newState && |
| 272 | (time_in_ms - mEnteredTrustedPresentationStateTime > |
| 273 | mTrustedPresentationThresholds.stabilityRequirementMs)) { |
| 274 | mLastReportedTrustedPresentationState = true; |
| 275 | mTrustedPresentationListener.invoke(true); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * See SurfaceComposerClient.h: setTrustedPresentationCallback for discussion |
| 281 | * of how the parameters and thresholds are interpreted. The general spirit is |
| 282 | * to produce an upper bound on the amount of the buffer which was presented. |
| 283 | */ |
| 284 | bool Layer::computeTrustedPresentationState(const FloatRect& bounds, const FloatRect& sourceBounds, |
| 285 | const Region& coveredRegion, |
| 286 | const FloatRect& screenBounds, float alpha, |
| 287 | const ui::Transform& effectiveTransform, |
| 288 | const TrustedPresentationThresholds& thresholds) { |
| 289 | if (alpha < thresholds.minAlpha) { |
| 290 | return false; |
| 291 | } |
| 292 | if (sourceBounds.getWidth() == 0 || sourceBounds.getHeight() == 0) { |
| 293 | return false; |
| 294 | } |
| 295 | if (screenBounds.getWidth() == 0 || screenBounds.getHeight() == 0) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | const float sx = effectiveTransform.dsdx(); |
| 300 | const float sy = effectiveTransform.dsdy(); |
| 301 | float fractionRendered = std::min(sx * sy, 1.0f); |
| 302 | |
| 303 | float boundsOverSourceW = bounds.getWidth() / (float)sourceBounds.getWidth(); |
| 304 | float boundsOverSourceH = bounds.getHeight() / (float)sourceBounds.getHeight(); |
| 305 | fractionRendered *= boundsOverSourceW * boundsOverSourceH; |
| 306 | |
Chavi Weingarten | 545da0e | 2023-02-09 14:55:57 +0000 | [diff] [blame] | 307 | Region tJunctionFreeRegion = Region::createTJunctionFreeRegion(coveredRegion); |
| 308 | // Compute the size of all the rects since they may be disconnected. |
| 309 | float coveredSize = 0; |
| 310 | for (auto rect = tJunctionFreeRegion.begin(); rect < tJunctionFreeRegion.end(); rect++) { |
| 311 | float size = rect->width() * rect->height(); |
| 312 | coveredSize += size; |
| 313 | } |
| 314 | |
| 315 | fractionRendered *= (1 - (coveredSize / (screenBounds.getWidth() * screenBounds.getHeight()))); |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 316 | |
| 317 | if (fractionRendered < thresholds.minFractionRendered) { |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 324 | Rect Layer::getCroppedBufferSize(const State& s) const { |
| 325 | Rect size = getBufferSize(s); |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 326 | Rect crop = Rect(getCrop(s)); |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 327 | if (!crop.isEmpty() && size.isValid()) { |
| 328 | size.intersect(crop, &size); |
| 329 | } else if (!crop.isEmpty()) { |
| 330 | size = crop; |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 331 | } |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 332 | return size; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 335 | const char* Layer::getDebugName() const { |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 336 | return mName.c_str(); |
David Sodman | 4b7c4bc | 2017-11-17 12:13:59 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 339 | // --------------------------------------------------------------------------- |
| 340 | // drawing... |
| 341 | // --------------------------------------------------------------------------- |
| 342 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 343 | aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType( |
| 344 | const DisplayDevice& display) const { |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 345 | const auto outputLayer = findOutputLayerForDisplay(&display); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 346 | return getCompositionType(outputLayer); |
| 347 | } |
| 348 | |
| 349 | aidl::android::hardware::graphics::composer3::Composition Layer::getCompositionType( |
| 350 | const compositionengine::OutputLayer* outputLayer) const { |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 351 | if (outputLayer == nullptr) { |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 352 | return aidl::android::hardware::graphics::composer3::Composition::INVALID; |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 353 | } |
| 354 | if (outputLayer->getState().hwc) { |
| 355 | return (*outputLayer->getState().hwc).hwcCompositionType; |
| 356 | } else { |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 357 | return aidl::android::hardware::graphics::composer3::Composition::CLIENT; |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 358 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 361 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 362 | // transaction |
| 363 | // ---------------------------------------------------------------------------- |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 364 | |
Vishnu Nair | c1d19d7 | 2023-08-10 12:35:11 -0700 | [diff] [blame] | 365 | void Layer::commitTransaction() { |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 366 | // Set the present state for all bufferlessSurfaceFramesTX to Presented. The |
| 367 | // bufferSurfaceFrameTX will be presented in latchBuffer. |
| 368 | for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) { |
| 369 | if (surfaceFrame->getPresentState() != PresentState::Presented) { |
| 370 | // With applyPendingStates, we could end up having presented surfaceframes from previous |
| 371 | // states |
Vishnu Nair | 7fe69ed | 2023-02-13 10:13:26 -0800 | [diff] [blame] | 372 | surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 373 | mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame); |
| 374 | } |
| 375 | } |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 376 | mDrawingState.bufferlessSurfaceFramesTX.clear(); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 379 | void Layer::setTransactionFlags(uint32_t mask) { |
| 380 | mTransactionFlags |= mask; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 383 | bool Layer::setCrop(const FloatRect& crop) { |
Vishnu Nair | ea04b6f | 2022-08-19 21:28:17 +0000 | [diff] [blame] | 384 | if (mDrawingState.crop == crop) return false; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 385 | mDrawingState.sequence++; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 386 | mDrawingState.crop = crop; |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 387 | |
Sally Qi | 81d95e6 | 2022-03-21 19:41:33 -0700 | [diff] [blame] | 388 | setTransactionFlags(eTransactionNeeded); |
| 389 | return true; |
| 390 | } |
| 391 | |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 392 | bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) { |
| 393 | return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE; |
| 394 | }; |
| 395 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 396 | void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 397 | nsecs_t postTime, gui::GameMode gameMode) { |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 398 | mDrawingState.postTime = postTime; |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 399 | |
| 400 | // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if |
| 401 | // there are two transactions with the same token, the first one without a buffer and the |
| 402 | // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX |
| 403 | // in that case. |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 404 | auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId); |
| 405 | if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) { |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 406 | // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 407 | mDrawingState.bufferSurfaceFrameTX = it->second; |
| 408 | mDrawingState.bufferlessSurfaceFramesTX.erase(it); |
| 409 | mDrawingState.bufferSurfaceFrameTX->promoteToBuffer(); |
| 410 | mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 411 | } else { |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 412 | mDrawingState.bufferSurfaceFrameTX = |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 413 | createSurfaceFrameForBuffer(info, postTime, mTransactionName, gameMode); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 414 | } |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 415 | |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 416 | setFrameTimelineVsyncForSkippedFrames(info, postTime, mTransactionName, gameMode); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 420 | nsecs_t postTime, |
| 421 | gui::GameMode gameMode) { |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 422 | mDrawingState.frameTimelineInfo = info; |
| 423 | mDrawingState.postTime = postTime; |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 424 | setTransactionFlags(eTransactionNeeded); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 425 | |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 426 | if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX; |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 427 | bufferSurfaceFrameTX != nullptr) { |
| 428 | if (bufferSurfaceFrameTX->getToken() == info.vsyncId) { |
| 429 | // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is |
| 430 | // being used for BufferSurfaceFrame, don't create a new one. |
| 431 | return; |
| 432 | } |
| 433 | } |
| 434 | // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple |
| 435 | // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are |
| 436 | // targeting different vsyncs). |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 437 | auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId); |
| 438 | if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) { |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 439 | auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime, gameMode); |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 440 | mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame; |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 441 | } else { |
| 442 | if (it->second->getPresentState() == PresentState::Presented) { |
| 443 | // If the SurfaceFrame was already presented, its safe to overwrite it since it must |
| 444 | // have been from previous vsync. |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 445 | it->second = createSurfaceFrameForTransaction(info, postTime, gameMode); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 448 | |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 449 | setFrameTimelineVsyncForSkippedFrames(info, postTime, mTransactionName, gameMode); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void Layer::addSurfaceFrameDroppedForBuffer( |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 453 | std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t dropTime) { |
| 454 | surfaceFrame->setDropTime(dropTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 455 | surfaceFrame->setPresentState(PresentState::Dropped); |
| 456 | mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame); |
| 457 | } |
| 458 | |
| 459 | void Layer::addSurfaceFramePresentedForBuffer( |
| 460 | std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime, |
| 461 | nsecs_t currentLatchTime) { |
| 462 | surfaceFrame->setAcquireFenceTime(acquireFenceTime); |
| 463 | surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime); |
| 464 | mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame); |
Vishnu Nair | a156f48 | 2023-02-22 00:23:38 +0000 | [diff] [blame] | 465 | updateLastLatchTime(currentLatchTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction( |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 469 | const FrameTimelineInfo& info, nsecs_t postTime, gui::GameMode gameMode) { |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 470 | auto surfaceFrame = |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 471 | mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid, |
| 472 | getSequence(), mName, |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 473 | mTransactionName, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 474 | /*isBuffer*/ false, gameMode); |
Rachel Lee | ed511ef | 2021-10-11 15:09:51 -0700 | [diff] [blame] | 475 | surfaceFrame->setActualStartTime(info.startTimeNanos); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 476 | // For Transactions, the post time is considered to be both queue and acquire fence time. |
| 477 | surfaceFrame->setActualQueueTime(postTime); |
| 478 | surfaceFrame->setAcquireFenceTime(postTime); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 479 | const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid()); |
| 480 | if (fps) { |
Alec Mouri | 819f630 | 2021-02-12 15:37:21 -0800 | [diff] [blame] | 481 | surfaceFrame->setRenderRate(*fps); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 482 | } |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 483 | return surfaceFrame; |
| 484 | } |
| 485 | |
| 486 | std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer( |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 487 | const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName, |
| 488 | gui::GameMode gameMode) { |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 489 | auto surfaceFrame = |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 490 | mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid, |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 491 | getSequence(), mName, debugName, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 492 | /*isBuffer*/ true, gameMode); |
Rachel Lee | ed511ef | 2021-10-11 15:09:51 -0700 | [diff] [blame] | 493 | surfaceFrame->setActualStartTime(info.startTimeNanos); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 494 | // For buffers, acquire fence time will set during latch. |
| 495 | surfaceFrame->setActualQueueTime(queueTime); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 496 | const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid()); |
| 497 | if (fps) { |
Alec Mouri | 819f630 | 2021-02-12 15:37:21 -0800 | [diff] [blame] | 498 | surfaceFrame->setRenderRate(*fps); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 499 | } |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 500 | return surfaceFrame; |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 503 | void Layer::setFrameTimelineVsyncForSkippedFrames(const FrameTimelineInfo& info, nsecs_t postTime, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 504 | std::string debugName, gui::GameMode gameMode) { |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 505 | if (info.skippedFrameVsyncId == FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | FrameTimelineInfo skippedFrameTimelineInfo = info; |
| 510 | skippedFrameTimelineInfo.vsyncId = info.skippedFrameVsyncId; |
| 511 | |
| 512 | auto surfaceFrame = |
| 513 | mFlinger->mFrameTimeline->createSurfaceFrameForToken(skippedFrameTimelineInfo, |
| 514 | mOwnerPid, mOwnerUid, |
| 515 | getSequence(), mName, debugName, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 516 | /*isBuffer*/ false, gameMode); |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 517 | surfaceFrame->setActualStartTime(skippedFrameTimelineInfo.skippedFrameStartTimeNanos); |
| 518 | // For Transactions, the post time is considered to be both queue and acquire fence time. |
| 519 | surfaceFrame->setActualQueueTime(postTime); |
| 520 | surfaceFrame->setAcquireFenceTime(postTime); |
| 521 | const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid()); |
| 522 | if (fps) { |
| 523 | surfaceFrame->setRenderRate(*fps); |
| 524 | } |
Ady Abraham | 5a3e356 | 2023-06-07 10:32:08 -0700 | [diff] [blame] | 525 | addSurfaceFrameDroppedForBuffer(surfaceFrame, postTime); |
| 526 | } |
| 527 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 528 | bool Layer::setFrameRateForLayerTree(FrameRate frameRate, const scheduler::LayerProps& layerProps, |
| 529 | nsecs_t now) { |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 530 | if (mDrawingState.frameRateForLayerTree == frameRate) { |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | mDrawingState.frameRateForLayerTree = frameRate; |
| 535 | mFlinger->mScheduler |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 536 | ->recordLayerHistory(sequence, layerProps, now, now, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 537 | scheduler::LayerHistory::LayerUpdateType::SetFrameRate); |
Ady Abraham | a850c18 | 2021-08-04 13:04:37 -0700 | [diff] [blame] | 538 | return true; |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Ady Abraham | 59fd8ff | 2021-04-15 20:13:30 -0700 | [diff] [blame] | 541 | Layer::FrameRate Layer::getFrameRateForLayerTree() const { |
| 542 | return getDrawingState().frameRateForLayerTree; |
| 543 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 545 | // ---------------------------------------------------------------------------- |
| 546 | // debugging |
| 547 | // ---------------------------------------------------------------------------- |
| 548 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 549 | void Layer::miniDumpHeader(std::string& result) { |
Marin Shalamanov | 1876e2e | 2020-12-04 13:23:59 +0100 | [diff] [blame] | 550 | result.append(kDumpTableRowLength, '-'); |
| 551 | result.append("\n"); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 552 | result.append(" Layer name\n"); |
| 553 | result.append(" Z | "); |
Ady Abraham | 8f1ee7f | 2019-04-05 10:32:50 -0700 | [diff] [blame] | 554 | result.append(" Window Type | "); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 555 | result.append(" Comp Type | "); |
Yichi Chen | 6ca3519 | 2018-05-29 12:20:43 +0800 | [diff] [blame] | 556 | result.append(" Transform | "); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 557 | result.append(" Disp Frame (LTRB) | "); |
Ady Abraham | be23e6a | 2020-05-04 14:51:16 -0700 | [diff] [blame] | 558 | result.append(" Source Crop (LTRB) | "); |
Marin Shalamanov | 1876e2e | 2020-12-04 13:23:59 +0100 | [diff] [blame] | 559 | result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n"); |
| 560 | result.append(kDumpTableRowLength, '-'); |
| 561 | result.append("\n"); |
Ady Abraham | be23e6a | 2020-05-04 14:51:16 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 564 | void Layer::miniDump(std::string& result, const frontend::LayerSnapshot& snapshot, |
| 565 | const DisplayDevice& display) const { |
| 566 | const auto outputLayer = findOutputLayerForDisplay(&display, snapshot.path); |
| 567 | if (!outputLayer) { |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | StringAppendF(&result, " %s\n", snapshot.debugName.c_str()); |
| 572 | StringAppendF(&result, " %10zu | ", snapshot.globalZ); |
| 573 | StringAppendF(&result, " %10d | ", |
| 574 | snapshot.layerMetadata.getInt32(gui::METADATA_WINDOW_TYPE, 0)); |
| 575 | StringAppendF(&result, "%10s | ", toString(getCompositionType(outputLayer)).c_str()); |
| 576 | const auto& outputLayerState = outputLayer->getState(); |
| 577 | StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str()); |
| 578 | const Rect& frame = outputLayerState.displayFrame; |
| 579 | StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom); |
| 580 | const FloatRect& crop = outputLayerState.sourceCrop; |
| 581 | StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right, |
| 582 | crop.bottom); |
| 583 | const auto frameRate = snapshot.frameRate; |
Ady Abraham | 77d406d | 2023-11-22 15:00:23 -0800 | [diff] [blame] | 584 | std::string frameRateStr; |
| 585 | if (frameRate.vote.rate.isValid()) { |
| 586 | StringAppendF(&frameRateStr, "%.2f", frameRate.vote.rate.getValue()); |
| 587 | } |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 588 | if (frameRate.vote.rate.isValid() || frameRate.vote.type != FrameRateCompatibility::Default) { |
Ady Abraham | 77d406d | 2023-11-22 15:00:23 -0800 | [diff] [blame] | 589 | StringAppendF(&result, "%6s %15s %17s", frameRateStr.c_str(), |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 590 | ftl::enum_string(frameRate.vote.type).c_str(), |
| 591 | ftl::enum_string(frameRate.vote.seamlessness).c_str()); |
Ady Abraham | 77d406d | 2023-11-22 15:00:23 -0800 | [diff] [blame] | 592 | } else if (frameRate.category != FrameRateCategory::Default) { |
| 593 | StringAppendF(&result, "%6s %15s %17s", frameRateStr.c_str(), |
| 594 | (std::string("Cat::") + ftl::enum_string(frameRate.category)).c_str(), |
| 595 | ftl::enum_string(frameRate.vote.seamlessness).c_str()); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 596 | } else { |
| 597 | result.append(41, ' '); |
| 598 | } |
| 599 | |
| 600 | const auto focused = isLayerFocusedBasedOnPriority(snapshot.frameRateSelectionPriority); |
| 601 | StringAppendF(&result, " [%s]\n", focused ? "*" : " "); |
| 602 | |
| 603 | result.append(kDumpTableRowLength, '-'); |
| 604 | result.append("\n"); |
| 605 | } |
| 606 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 607 | void Layer::dumpFrameStats(std::string& result) const { |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 608 | mFrameTracker.dumpStats(result); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 611 | void Layer::clearFrameStats() { |
| 612 | mFrameTracker.clearStats(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 615 | void Layer::getFrameStats(FrameStats* outStats) const { |
| 616 | mFrameTracker.getStats(outStats); |
| 617 | } |
| 618 | |
Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 619 | void Layer::onDisconnect() { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 620 | const int32_t layerId = getSequence(); |
| 621 | mFlinger->mTimeStats->onDestroy(layerId); |
| 622 | mFlinger->mFrameTracer->onDestroy(layerId); |
Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 625 | void Layer::writeCompositionStateToProto(perfetto::protos::LayerProto* layerProto, |
| 626 | ui::LayerStack layerStack) { |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 627 | ftl::FakeGuard guard(mFlinger->mStateLock); // Called from the main thread. |
Vishnu Nair | 2f65e97 | 2023-04-04 16:36:28 +0000 | [diff] [blame] | 628 | ftl::FakeGuard mainThreadGuard(kMainThreadContext); |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 629 | |
| 630 | // Only populate for the primary display. |
Vishnu Nair | 2f65e97 | 2023-04-04 16:36:28 +0000 | [diff] [blame] | 631 | if (const auto display = mFlinger->getDisplayFromLayerStack(layerStack)) { |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 632 | const auto compositionType = getCompositionType(*display); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 633 | layerProto->set_hwc_composition_type( |
| 634 | static_cast<perfetto::protos::HwcCompositionType>(compositionType)); |
Vishnu Nair | 2f65e97 | 2023-04-04 16:36:28 +0000 | [diff] [blame] | 635 | LayerProtoHelper::writeToProto(getVisibleRegion(display), |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 636 | [&]() { return layerProto->mutable_visible_region(); }); |
| 637 | } |
| 638 | } |
| 639 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 640 | compositionengine::OutputLayer* Layer::findOutputLayerForDisplay( |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 641 | const DisplayDevice* display) const { |
| 642 | if (!display) return nullptr; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 643 | sp<LayerFE> layerFE; |
| 644 | frontend::LayerHierarchy::TraversalPath path{.id = static_cast<uint32_t>(sequence)}; |
| 645 | for (auto& [p, layer] : mLayerFEs) { |
| 646 | if (p == path) { |
| 647 | layerFE = layer; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if (!layerFE) return nullptr; |
| 652 | return display->getCompositionDisplay()->getOutputLayerForLayer(layerFE); |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 655 | compositionengine::OutputLayer* Layer::findOutputLayerForDisplay( |
| 656 | const DisplayDevice* display, const frontend::LayerHierarchy::TraversalPath& path) const { |
| 657 | if (!display) return nullptr; |
| 658 | sp<LayerFE> layerFE; |
| 659 | for (auto& [p, layer] : mLayerFEs) { |
| 660 | if (p == path) { |
| 661 | layerFE = layer; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if (!layerFE) return nullptr; |
| 666 | return display->getCompositionDisplay()->getOutputLayerForLayer(layerFE); |
| 667 | } |
| 668 | |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 669 | Region Layer::getVisibleRegion(const DisplayDevice* display) const { |
| 670 | const auto outputLayer = findOutputLayerForDisplay(display); |
| 671 | return outputLayer ? outputLayer->getState().visibleRegion : Region(); |
Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 674 | void Layer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener, |
| 675 | const sp<GraphicBuffer>& buffer, uint64_t framenumber, |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 676 | const sp<Fence>& releaseFence) { |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 677 | if (!listener && !mBufferReleaseChannel) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 680 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 681 | SFTRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber); |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 682 | |
| 683 | ReleaseCallbackId callbackId{buffer->getId(), framenumber}; |
| 684 | const sp<Fence>& fence = releaseFence ? releaseFence : Fence::NO_FENCE; |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 685 | uint32_t currentMaxAcquiredBufferCount = |
| 686 | mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid); |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 687 | |
| 688 | if (listener) { |
| 689 | listener->onReleaseBuffer(callbackId, fence, currentMaxAcquiredBufferCount); |
| 690 | } |
| 691 | |
Patrick Williams | bb50447 | 2024-10-25 13:20:32 -0500 | [diff] [blame^] | 692 | if (!mBufferReleaseChannel) { |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | status_t status = mBufferReleaseChannel->writeReleaseFence(callbackId, fence, |
| 697 | currentMaxAcquiredBufferCount); |
| 698 | if (status != OK) { |
| 699 | int error = -status; |
| 700 | // callReleaseBufferCallback is called during Layer's destructor. In this case, it's |
| 701 | // expected to receive connection errors. |
| 702 | if (error != EPIPE && error != ECONNRESET) { |
| 703 | ALOGD("[%s] writeReleaseFence failed. error %d (%s)", getDebugName(), error, |
| 704 | strerror(error)); |
| 705 | } |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 706 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 709 | sp<CallbackHandle> Layer::findCallbackHandle() { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 710 | // If we are displayed on multiple displays in a single composition cycle then we would |
| 711 | // need to do careful tracking to enable the use of the mLastClientCompositionFence. |
| 712 | // For example we can only use it if all the displays are client comp, and we need |
| 713 | // to merge all the client comp fences. We could do this, but for now we just |
| 714 | // disable the optimization when a layer is composed on multiple displays. |
| 715 | if (mClearClientCompositionFenceOnLayerDisplayed) { |
| 716 | mLastClientCompositionFence = nullptr; |
| 717 | } else { |
| 718 | mClearClientCompositionFenceOnLayerDisplayed = true; |
| 719 | } |
| 720 | |
| 721 | // The previous release fence notifies the client that SurfaceFlinger is done with the previous |
| 722 | // buffer that was presented on this layer. The first transaction that came in this frame that |
| 723 | // replaced the previous buffer on this layer needs this release fence, because the fence will |
| 724 | // let the client know when that previous buffer is removed from the screen. |
| 725 | // |
| 726 | // Every other transaction on this layer does not need a release fence because no other |
| 727 | // Transactions that were set on this layer this frame are going to have their preceding buffer |
| 728 | // removed from the display this frame. |
| 729 | // |
| 730 | // For example, if we have 3 transactions this frame. The first transaction doesn't contain a |
| 731 | // buffer so it doesn't need a previous release fence because the layer still needs the previous |
| 732 | // buffer. The second transaction contains a buffer so it needs a previous release fence because |
| 733 | // the previous buffer will be released this frame. The third transaction also contains a |
| 734 | // buffer. It replaces the buffer in the second transaction. The buffer in the second |
| 735 | // transaction will now no longer be presented so it is released immediately and the third |
| 736 | // transaction doesn't need a previous release fence. |
| 737 | sp<CallbackHandle> ch; |
| 738 | for (auto& handle : mDrawingState.callbackHandles) { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 739 | if (handle->releasePreviousBuffer && mPreviousReleaseBufferEndpoint == handle->listener) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 740 | ch = handle; |
| 741 | break; |
| 742 | } |
| 743 | } |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 744 | return ch; |
| 745 | } |
| 746 | |
| 747 | void Layer::prepareReleaseCallbacks(ftl::Future<FenceResult> futureFenceResult, |
| 748 | ui::LayerStack layerStack) { |
| 749 | sp<CallbackHandle> ch = findCallbackHandle(); |
| 750 | |
| 751 | if (ch != nullptr) { |
| 752 | ch->previousReleaseCallbackId = mPreviousReleaseCallbackId; |
| 753 | ch->previousReleaseFences.emplace_back(std::move(futureFenceResult)); |
| 754 | ch->name = mName; |
| 755 | } else { |
Melody Hsu | e4ef87f | 2024-03-26 23:54:45 +0000 | [diff] [blame] | 756 | // If we didn't get a release callback yet (e.g. some scenarios when capturing |
| 757 | // screenshots asynchronously) then make sure we don't drop the fence. |
| 758 | // Older fences for the same layer stack can be dropped when a new fence arrives. |
| 759 | // An assumption here is that RenderEngine performs work sequentially, so an |
| 760 | // incoming fence will not fire before an existing fence. |
| 761 | mAdditionalPreviousReleaseFences.emplace_or_replace(layerStack, |
| 762 | std::move(futureFenceResult)); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | if (mBufferInfo.mBuffer) { |
| 766 | mPreviouslyPresentedLayerStacks.push_back(layerStack); |
| 767 | } |
| 768 | |
| 769 | if (mDrawingState.frameNumber > 0) { |
| 770 | mDrawingState.previousFrameNumber = mDrawingState.frameNumber; |
| 771 | } |
| 772 | } |
| 773 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 774 | void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) { |
| 775 | for (const auto& handle : mDrawingState.callbackHandles) { |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 776 | handle->bufferReleaseChannel = mBufferReleaseChannel; |
Melody Hsu | 9af3070 | 2024-04-17 18:23:43 +0000 | [diff] [blame] | 777 | handle->transformHint = mTransformHint; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 778 | handle->dequeueReadyTime = dequeueReadyTime; |
| 779 | handle->currentMaxAcquiredBufferCount = |
| 780 | mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 781 | SFTRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(), |
| 782 | handle->previousReleaseCallbackId.framenumber); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | for (auto& handle : mDrawingState.callbackHandles) { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 786 | if (handle->releasePreviousBuffer && mPreviousReleaseBufferEndpoint == handle->listener) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 787 | handle->previousReleaseCallbackId = mPreviousReleaseCallbackId; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 792 | mFlinger->getTransactionCallbackInvoker().addCallbackHandles(mDrawingState.callbackHandles); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 793 | mDrawingState.callbackHandles = {}; |
| 794 | } |
| 795 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 796 | bool Layer::setTransform(uint32_t transform) { |
| 797 | if (mDrawingState.bufferTransform == transform) return false; |
| 798 | mDrawingState.bufferTransform = transform; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 799 | setTransactionFlags(eTransactionNeeded); |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | bool Layer::setTransformToDisplayInverse(bool transformToDisplayInverse) { |
| 804 | if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false; |
| 805 | mDrawingState.sequence++; |
| 806 | mDrawingState.transformToDisplayInverse = transformToDisplayInverse; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 807 | setTransactionFlags(eTransactionNeeded); |
| 808 | return true; |
| 809 | } |
| 810 | |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 811 | void Layer::releasePreviousBuffer() { |
| 812 | mReleasePreviousBuffer = true; |
| 813 | if (!mBufferInfo.mBuffer || |
| 814 | (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) || |
| 815 | mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) { |
| 816 | // If mDrawingState has a buffer, and we are about to update again |
| 817 | // before swapping to drawing state, then the first buffer will be |
| 818 | // dropped and we should decrement the pending buffer count and |
| 819 | // call any release buffer callbacks if set. |
| 820 | callReleaseBufferCallback(mDrawingState.releaseBufferListener, |
| 821 | mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber, |
| 822 | mDrawingState.acquireFence); |
Dominic Lemire | 95c4ecc | 2024-07-09 12:06:18 -0700 | [diff] [blame] | 823 | const int32_t layerId = getSequence(); |
| 824 | mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber); |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 825 | decrementPendingBufferCount(); |
| 826 | if (mDrawingState.bufferSurfaceFrameTX != nullptr && |
| 827 | mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) { |
| 828 | addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX, systemTime()); |
| 829 | mDrawingState.bufferSurfaceFrameTX.reset(); |
| 830 | } |
| 831 | } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) { |
| 832 | callReleaseBufferCallback(mDrawingState.releaseBufferListener, |
| 833 | mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber, |
| 834 | mLastClientCompositionFence); |
| 835 | mLastClientCompositionFence = nullptr; |
| 836 | } |
| 837 | } |
| 838 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 839 | void Layer::resetDrawingStateBufferInfo() { |
| 840 | mDrawingState.producerId = 0; |
| 841 | mDrawingState.frameNumber = 0; |
Alec Mouri | 21d9432 | 2023-10-17 19:51:39 +0000 | [diff] [blame] | 842 | mDrawingState.previousFrameNumber = 0; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 843 | mDrawingState.releaseBufferListener = nullptr; |
| 844 | mDrawingState.buffer = nullptr; |
| 845 | mDrawingState.acquireFence = sp<Fence>::make(-1); |
| 846 | mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence); |
| 847 | mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime(); |
| 848 | mDrawingState.releaseBufferEndpoint = nullptr; |
| 849 | } |
| 850 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 851 | bool Layer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer, |
| 852 | const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 853 | bool isAutoTimestamp, const FrameTimelineInfo& info, gui::GameMode gameMode) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 854 | SFTRACE_FORMAT("setBuffer %s - hasBuffer=%s", getDebugName(), (buffer ? "true" : "false")); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 855 | |
| 856 | const bool frameNumberChanged = |
| 857 | bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged); |
| 858 | const uint64_t frameNumber = |
| 859 | frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 860 | SFTRACE_FORMAT_INSTANT("setBuffer %s - %" PRIu64, getDebugName(), frameNumber); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 861 | |
| 862 | if (mDrawingState.buffer) { |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 863 | releasePreviousBuffer(); |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 864 | } else if (buffer) { |
Vishnu Nair | c09c023 | 2023-03-02 03:22:35 +0000 | [diff] [blame] | 865 | // if we are latching a buffer for the first time then clear the mLastLatchTime since |
| 866 | // we don't want to incorrectly classify a frame if we miss the desired present time. |
| 867 | updateLastLatchTime(0); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 870 | mDrawingState.desiredPresentTime = desiredPresentTime; |
| 871 | mDrawingState.isAutoTimestamp = isAutoTimestamp; |
| 872 | mDrawingState.latchedVsyncId = info.vsyncId; |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 873 | mDrawingState.useVsyncIdForRefreshRateSelection = info.useForRefreshRateSelection; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 874 | if (!buffer) { |
| 875 | resetDrawingStateBufferInfo(); |
| 876 | setTransactionFlags(eTransactionNeeded); |
| 877 | mDrawingState.bufferSurfaceFrameTX = nullptr; |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 878 | setFrameTimelineVsyncForBufferlessTransaction(info, postTime, gameMode); |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 879 | return true; |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 880 | } else { |
| 881 | // release sideband stream if it exists and a non null buffer is being set |
| 882 | if (mDrawingState.sidebandStream != nullptr) { |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 883 | setSidebandStream(nullptr, info, postTime, gameMode); |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 884 | } |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 887 | if ((mDrawingState.producerId > bufferData.producerId) || |
| 888 | ((mDrawingState.producerId == bufferData.producerId) && |
| 889 | (mDrawingState.frameNumber > frameNumber))) { |
| 890 | ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64 |
| 891 | " -> producedId=%d frameNumber=%" PRIu64, |
| 892 | getDebugName(), mDrawingState.producerId, mDrawingState.frameNumber, |
| 893 | bufferData.producerId, frameNumber); |
| 894 | TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_", /*overwrite=*/false); |
| 895 | } |
| 896 | |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 897 | mDrawingState.producerId = bufferData.producerId; |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 898 | mDrawingState.barrierProducerId = |
| 899 | std::max(mDrawingState.producerId, mDrawingState.barrierProducerId); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 900 | mDrawingState.frameNumber = frameNumber; |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 901 | mDrawingState.barrierFrameNumber = |
| 902 | std::max(mDrawingState.frameNumber, mDrawingState.barrierFrameNumber); |
| 903 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 904 | mDrawingState.releaseBufferListener = bufferData.releaseBufferListener; |
| 905 | mDrawingState.buffer = std::move(buffer); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 906 | mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged) |
| 907 | ? bufferData.acquireFence |
| 908 | : Fence::NO_FENCE; |
| 909 | mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence); |
| 910 | if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 911 | // We latched this buffer unsiganled, so we need to pass the acquire fence |
| 912 | // on the callback instead of just the acquire time, since it's unknown at |
| 913 | // this point. |
| 914 | mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence; |
| 915 | } else { |
| 916 | mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime(); |
| 917 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 918 | setTransactionFlags(eTransactionNeeded); |
| 919 | |
| 920 | const int32_t layerId = getSequence(); |
| 921 | mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(), |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 922 | mOwnerUid, postTime, gameMode); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 923 | |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 924 | setFrameTimelineVsyncForBufferTransaction(info, postTime, gameMode); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 925 | |
Nergi Rahardi | 39f510f | 2024-05-23 15:16:54 +0900 | [diff] [blame] | 926 | if (bufferData.dequeueTime > 0) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 927 | const uint64_t bufferId = mDrawingState.buffer->getId(); |
| 928 | mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str()); |
Nergi Rahardi | 39f510f | 2024-05-23 15:16:54 +0900 | [diff] [blame] | 929 | mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, |
| 930 | bufferData.dequeueTime, |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 931 | FrameTracer::FrameEvent::DEQUEUE); |
| 932 | mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime, |
| 933 | FrameTracer::FrameEvent::QUEUE); |
| 934 | } |
| 935 | |
| 936 | mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 937 | |
| 938 | // If the layer had been updated a TextureView, this would make sure the present time could be |
| 939 | // same to TextureView update when it's a small dirty, and get the correct heuristic rate. |
Jerry Chang | 3667800 | 2023-11-29 16:56:17 +0000 | [diff] [blame] | 940 | if (mFlinger->mScheduler->supportSmallDirtyDetection(mOwnerAppId)) { |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 941 | if (mDrawingState.useVsyncIdForRefreshRateSelection) { |
| 942 | mUsedVsyncIdForRefreshRateSelection = true; |
| 943 | } |
| 944 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 945 | return true; |
| 946 | } |
| 947 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 948 | void Layer::setDesiredPresentTime(nsecs_t desiredPresentTime, bool isAutoTimestamp) { |
| 949 | mDrawingState.desiredPresentTime = desiredPresentTime; |
| 950 | mDrawingState.isAutoTimestamp = isAutoTimestamp; |
| 951 | } |
| 952 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 953 | void Layer::recordLayerHistoryBufferUpdate(const scheduler::LayerProps& layerProps, nsecs_t now) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 954 | SFTRACE_CALL(); |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 955 | const nsecs_t presentTime = [&] { |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 956 | if (!mDrawingState.isAutoTimestamp) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 957 | SFTRACE_FORMAT_INSTANT("desiredPresentTime"); |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 958 | return mDrawingState.desiredPresentTime; |
| 959 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 960 | |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 961 | if (mDrawingState.useVsyncIdForRefreshRateSelection) { |
| 962 | const auto prediction = |
| 963 | mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken( |
| 964 | mDrawingState.latchedVsyncId); |
| 965 | if (prediction.has_value()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 966 | SFTRACE_FORMAT_INSTANT("predictedPresentTime"); |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 967 | mMaxTimeForUseVsyncId = prediction->presentTime + |
| 968 | scheduler::LayerHistory::kMaxPeriodForHistory.count(); |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 969 | return prediction->presentTime; |
| 970 | } |
| 971 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 972 | |
Jerry Chang | 3667800 | 2023-11-29 16:56:17 +0000 | [diff] [blame] | 973 | if (!mFlinger->mScheduler->supportSmallDirtyDetection(mOwnerAppId)) { |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 974 | return static_cast<nsecs_t>(0); |
| 975 | } |
| 976 | |
| 977 | // If the layer is not an application and didn't set an explicit rate or desiredPresentTime, |
| 978 | // return "0" to tell the layer history that it will use the max refresh rate without |
| 979 | // calculating the adaptive rate. |
| 980 | if (mWindowType != WindowInfo::Type::APPLICATION && |
| 981 | mWindowType != WindowInfo::Type::BASE_APPLICATION) { |
| 982 | return static_cast<nsecs_t>(0); |
| 983 | } |
| 984 | |
| 985 | // Return the valid present time only when the layer potentially updated a TextureView so |
| 986 | // LayerHistory could heuristically calculate the rate if the UI is continually updating. |
| 987 | if (mUsedVsyncIdForRefreshRateSelection) { |
| 988 | const auto prediction = |
| 989 | mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken( |
| 990 | mDrawingState.latchedVsyncId); |
| 991 | if (prediction.has_value()) { |
| 992 | if (mMaxTimeForUseVsyncId >= prediction->presentTime) { |
| 993 | return prediction->presentTime; |
| 994 | } |
| 995 | mUsedVsyncIdForRefreshRateSelection = false; |
| 996 | } |
| 997 | } |
| 998 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 999 | return static_cast<nsecs_t>(0); |
| 1000 | }(); |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 1001 | |
Vishnu Nair | 2665ca9 | 2024-07-09 22:08:15 +0000 | [diff] [blame] | 1002 | if (SFTRACE_ENABLED() && presentTime > 0) { |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 1003 | const auto presentIn = TimePoint::fromNs(presentTime) - TimePoint::now(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1004 | SFTRACE_FORMAT_INSTANT("presentIn %s", to_string(presentIn).c_str()); |
Ady Abraham | 5526916 | 2023-05-09 11:26:06 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 1007 | mFlinger->mScheduler->recordLayerHistory(sequence, layerProps, presentTime, now, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 1008 | scheduler::LayerHistory::LayerUpdateType::Buffer); |
| 1009 | } |
| 1010 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 1011 | void Layer::recordLayerHistoryAnimationTx(const scheduler::LayerProps& layerProps, nsecs_t now) { |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 1012 | const nsecs_t presentTime = |
| 1013 | mDrawingState.isAutoTimestamp ? 0 : mDrawingState.desiredPresentTime; |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 1014 | mFlinger->mScheduler->recordLayerHistory(sequence, layerProps, presentTime, now, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 1015 | scheduler::LayerHistory::LayerUpdateType::AnimationTX); |
| 1016 | } |
| 1017 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1018 | bool Layer::setDataspace(ui::Dataspace dataspace) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1019 | if (mDrawingState.dataspace == dataspace) return false; |
| 1020 | mDrawingState.dataspace = dataspace; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1021 | setTransactionFlags(eTransactionNeeded); |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 1025 | bool Layer::setExtendedRangeBrightness(float currentBufferRatio, float desiredRatio) { |
Sally Qi | 963049b | 2023-03-23 14:06:21 -0700 | [diff] [blame] | 1026 | if (mDrawingState.currentHdrSdrRatio == currentBufferRatio && |
| 1027 | mDrawingState.desiredHdrSdrRatio == desiredRatio) |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 1028 | return false; |
Sally Qi | 963049b | 2023-03-23 14:06:21 -0700 | [diff] [blame] | 1029 | mDrawingState.currentHdrSdrRatio = currentBufferRatio; |
| 1030 | mDrawingState.desiredHdrSdrRatio = desiredRatio; |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 1031 | setTransactionFlags(eTransactionNeeded); |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 1035 | bool Layer::setDesiredHdrHeadroom(float desiredRatio) { |
| 1036 | if (mDrawingState.desiredHdrSdrRatio == desiredRatio) return false; |
| 1037 | mDrawingState.desiredHdrSdrRatio = desiredRatio; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1038 | setTransactionFlags(eTransactionNeeded); |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 1042 | bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream, const FrameTimelineInfo& info, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 1043 | nsecs_t postTime, gui::GameMode gameMode) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1044 | if (mDrawingState.sidebandStream == sidebandStream) return false; |
| 1045 | |
| 1046 | if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) { |
| 1047 | mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount(); |
| 1048 | } else if (sidebandStream != nullptr) { |
| 1049 | mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount(); |
| 1050 | } |
| 1051 | |
| 1052 | mDrawingState.sidebandStream = sidebandStream; |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 1053 | if (sidebandStream != nullptr && mDrawingState.buffer != nullptr) { |
| 1054 | releasePreviousBuffer(); |
| 1055 | resetDrawingStateBufferInfo(); |
| 1056 | mDrawingState.bufferSurfaceFrameTX = nullptr; |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 1057 | setFrameTimelineVsyncForBufferlessTransaction(info, postTime, gameMode); |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 1058 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1059 | setTransactionFlags(eTransactionNeeded); |
| 1060 | if (!mSidebandStreamChanged.exchange(true)) { |
| 1061 | // mSidebandStreamChanged was false |
| 1062 | mFlinger->onLayerUpdate(); |
| 1063 | } |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 1067 | bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles, |
| 1068 | bool willPresent) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1069 | // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return |
| 1070 | if (handles.empty()) { |
| 1071 | mReleasePreviousBuffer = false; |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
Pascal Muetschard | 81cef29 | 2023-02-06 12:18:52 +0100 | [diff] [blame] | 1075 | std::deque<sp<CallbackHandle>> remainingHandles; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1076 | for (const auto& handle : handles) { |
| 1077 | // If this transaction set a buffer on this layer, release its previous buffer |
| 1078 | handle->releasePreviousBuffer = mReleasePreviousBuffer; |
| 1079 | |
| 1080 | // If this layer will be presented in this frame |
| 1081 | if (willPresent) { |
| 1082 | // If this transaction set an acquire fence on this layer, set its acquire time |
| 1083 | handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence; |
| 1084 | handle->frameNumber = mDrawingState.frameNumber; |
Alec Mouri | 21d9432 | 2023-10-17 19:51:39 +0000 | [diff] [blame] | 1085 | handle->previousFrameNumber = mDrawingState.previousFrameNumber; |
Melody Hsu | 0077fde | 2024-10-17 21:42:50 +0000 | [diff] [blame] | 1086 | if (mPreviousReleaseBufferEndpoint == handle->listener) { |
Melody Hsu | e4ef87f | 2024-03-26 23:54:45 +0000 | [diff] [blame] | 1087 | // Add fence from previous screenshot now so that it can be dispatched to the |
Alec Mouri | 9892aac | 2023-12-11 21:16:59 +0000 | [diff] [blame] | 1088 | // client. |
Melody Hsu | e4ef87f | 2024-03-26 23:54:45 +0000 | [diff] [blame] | 1089 | for (auto& [_, future] : mAdditionalPreviousReleaseFences) { |
| 1090 | handle->previousReleaseFences.emplace_back(std::move(future)); |
Alec Mouri | 9892aac | 2023-12-11 21:16:59 +0000 | [diff] [blame] | 1091 | } |
| 1092 | mAdditionalPreviousReleaseFences.clear(); |
| 1093 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1094 | // Store so latched time and release fence can be set |
| 1095 | mDrawingState.callbackHandles.push_back(handle); |
| 1096 | |
| 1097 | } else { // If this layer will NOT need to be relatched and presented this frame |
Pascal Muetschard | 81cef29 | 2023-02-06 12:18:52 +0100 | [diff] [blame] | 1098 | // Queue this handle to be notified below. |
| 1099 | remainingHandles.push_back(handle); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Pascal Muetschard | 81cef29 | 2023-02-06 12:18:52 +0100 | [diff] [blame] | 1103 | if (!remainingHandles.empty()) { |
| 1104 | // Notify the transaction completed threads these handles are done. These are only the |
| 1105 | // handles that were not added to the mDrawingState, which will be notified later. |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 1106 | mFlinger->getTransactionCallbackInvoker().addCallbackHandles(remainingHandles); |
Pascal Muetschard | 81cef29 | 2023-02-06 12:18:52 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1109 | mReleasePreviousBuffer = false; |
| 1110 | mCallbackHandleAcquireTimeOrFence = -1; |
| 1111 | |
| 1112 | return willPresent; |
| 1113 | } |
| 1114 | |
| 1115 | Rect Layer::getBufferSize(const State& /*s*/) const { |
| 1116 | // for buffer state layers we use the display frame size as the buffer size. |
| 1117 | |
| 1118 | if (mBufferInfo.mBuffer == nullptr) { |
| 1119 | return Rect::INVALID_RECT; |
| 1120 | } |
| 1121 | |
| 1122 | uint32_t bufWidth = mBufferInfo.mBuffer->getWidth(); |
| 1123 | uint32_t bufHeight = mBufferInfo.mBuffer->getHeight(); |
| 1124 | |
| 1125 | // Undo any transformations on the buffer and return the result. |
| 1126 | if (mBufferInfo.mTransform & ui::Transform::ROT_90) { |
| 1127 | std::swap(bufWidth, bufHeight); |
| 1128 | } |
| 1129 | |
| 1130 | if (getTransformToDisplayInverse()) { |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 1131 | uint32_t invTransform = SurfaceFlinger::getActiveDisplayRotationFlags(); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1132 | if (invTransform & ui::Transform::ROT_90) { |
| 1133 | std::swap(bufWidth, bufHeight); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight)); |
| 1138 | } |
| 1139 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1140 | bool Layer::fenceHasSignaled() const { |
| 1141 | if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) { |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
| 1145 | const bool fenceSignaled = |
| 1146 | getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled; |
| 1147 | if (!fenceSignaled) { |
| 1148 | mFlinger->mTimeStats->incrementLatchSkipped(getSequence(), |
| 1149 | TimeStats::LatchSkipReason::LateAcquire); |
| 1150 | } |
| 1151 | |
| 1152 | return fenceSignaled; |
| 1153 | } |
| 1154 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 1155 | void Layer::onPreComposition(nsecs_t refreshStartTime) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1156 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 1157 | handle->refreshStartTime = refreshStartTime; |
| 1158 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1161 | bool Layer::latchSidebandStream(bool& recomputeVisibleRegions) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1162 | if (mSidebandStreamChanged.exchange(false)) { |
| 1163 | const State& s(getDrawingState()); |
| 1164 | // mSidebandStreamChanged was true |
| 1165 | mSidebandStream = s.sidebandStream; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1166 | if (mSidebandStream != nullptr) { |
| 1167 | setTransactionFlags(eTransactionNeeded); |
| 1168 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 1169 | } |
| 1170 | recomputeVisibleRegions = true; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1171 | return true; |
| 1172 | } |
| 1173 | return false; |
| 1174 | } |
| 1175 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 1176 | void Layer::updateTexImage(nsecs_t latchTime, bool bgColorOnly) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1177 | const State& s(getDrawingState()); |
| 1178 | |
| 1179 | if (!s.buffer) { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 1180 | if (bgColorOnly || mBufferInfo.mBuffer) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1181 | for (auto& handle : mDrawingState.callbackHandles) { |
| 1182 | handle->latchTime = latchTime; |
| 1183 | } |
| 1184 | } |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | for (auto& handle : mDrawingState.callbackHandles) { |
| 1189 | if (handle->frameNumber == mDrawingState.frameNumber) { |
| 1190 | handle->latchTime = latchTime; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | const int32_t layerId = getSequence(); |
| 1195 | const uint64_t bufferId = mDrawingState.buffer->getId(); |
| 1196 | const uint64_t frameNumber = mDrawingState.frameNumber; |
| 1197 | const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence); |
| 1198 | mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence); |
| 1199 | mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime); |
| 1200 | |
| 1201 | mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence, |
| 1202 | FrameTracer::FrameEvent::ACQUIRE_FENCE); |
| 1203 | mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime, |
| 1204 | FrameTracer::FrameEvent::LATCH); |
| 1205 | |
| 1206 | auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX; |
| 1207 | if (bufferSurfaceFrame != nullptr && |
| 1208 | bufferSurfaceFrame->getPresentState() != PresentState::Presented) { |
| 1209 | // Update only if the bufferSurfaceFrame wasn't already presented. A Presented |
| 1210 | // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we |
| 1211 | // are processing the next state. |
| 1212 | addSurfaceFramePresentedForBuffer(bufferSurfaceFrame, |
| 1213 | mDrawingState.acquireFenceTime->getSignalTime(), |
| 1214 | latchTime); |
| 1215 | mDrawingState.bufferSurfaceFrameTX.reset(); |
| 1216 | } |
| 1217 | |
| 1218 | std::deque<sp<CallbackHandle>> remainingHandles; |
| 1219 | mFlinger->getTransactionCallbackInvoker() |
| 1220 | .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles); |
| 1221 | mDrawingState.callbackHandles = remainingHandles; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | void Layer::gatherBufferInfo() { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 1225 | mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber}; |
| 1226 | mPreviousReleaseBufferEndpoint = mBufferInfo.mReleaseBufferEndpoint; |
| 1227 | if (!mDrawingState.buffer) { |
| 1228 | mBufferInfo = {}; |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | if ((!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer))) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1233 | decrementPendingBufferCount(); |
| 1234 | } |
| 1235 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1236 | mBufferInfo.mBuffer = mDrawingState.buffer; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 1237 | mBufferInfo.mReleaseBufferEndpoint = mDrawingState.releaseBufferEndpoint; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1238 | mBufferInfo.mFence = mDrawingState.acquireFence; |
| 1239 | mBufferInfo.mFrameNumber = mDrawingState.frameNumber; |
| 1240 | mBufferInfo.mPixelFormat = |
| 1241 | !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat(); |
| 1242 | mBufferInfo.mFrameLatencyNeeded = true; |
| 1243 | mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime; |
| 1244 | mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1245 | mBufferInfo.mTransform = mDrawingState.bufferTransform; |
| 1246 | auto lastDataspace = mBufferInfo.mDataspace; |
| 1247 | mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace); |
Alec Mouri | 74c7ae1 | 2023-03-26 02:57:47 +0000 | [diff] [blame] | 1248 | if (mBufferInfo.mBuffer != nullptr) { |
| 1249 | auto& mapper = GraphicBufferMapper::get(); |
| 1250 | // TODO: We should measure if it's faster to do a blind write if we're on newer api levels |
| 1251 | // and don't need to possibly remaps buffers. |
| 1252 | ui::Dataspace dataspace = ui::Dataspace::UNKNOWN; |
| 1253 | status_t err = OK; |
| 1254 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1255 | SFTRACE_NAME("getDataspace"); |
Alec Mouri | 74c7ae1 | 2023-03-26 02:57:47 +0000 | [diff] [blame] | 1256 | err = mapper.getDataspace(mBufferInfo.mBuffer->getBuffer()->handle, &dataspace); |
| 1257 | } |
| 1258 | if (err != OK || dataspace != mBufferInfo.mDataspace) { |
| 1259 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1260 | SFTRACE_NAME("setDataspace"); |
Alec Mouri | 74c7ae1 | 2023-03-26 02:57:47 +0000 | [diff] [blame] | 1261 | err = mapper.setDataspace(mBufferInfo.mBuffer->getBuffer()->handle, |
| 1262 | static_cast<ui::Dataspace>(mBufferInfo.mDataspace)); |
| 1263 | } |
| 1264 | |
| 1265 | // Some GPU drivers may cache gralloc metadata which means before we composite we need |
| 1266 | // to upsert RenderEngine's caches. Put in a special workaround to be backwards |
| 1267 | // compatible with old vendors, with a ticking clock. |
| 1268 | static const int32_t kVendorVersion = |
Alec Mouri | 8a06fb7 | 2023-11-16 22:07:13 +0000 | [diff] [blame] | 1269 | base::GetIntProperty("ro.board.api_level", __ANDROID_API_FUTURE__); |
Alec Mouri | 74c7ae1 | 2023-03-26 02:57:47 +0000 | [diff] [blame] | 1270 | if (const auto format = |
| 1271 | static_cast<aidl::android::hardware::graphics::common::PixelFormat>( |
| 1272 | mBufferInfo.mBuffer->getPixelFormat()); |
| 1273 | err == OK && kVendorVersion < __ANDROID_API_U__ && |
| 1274 | (format == |
| 1275 | aidl::android::hardware::graphics::common::PixelFormat:: |
| 1276 | IMPLEMENTATION_DEFINED || |
| 1277 | format == aidl::android::hardware::graphics::common::PixelFormat::YCBCR_420_888 || |
| 1278 | format == aidl::android::hardware::graphics::common::PixelFormat::YV12 || |
| 1279 | format == aidl::android::hardware::graphics::common::PixelFormat::YCBCR_P010)) { |
| 1280 | mBufferInfo.mBuffer->remapBuffer(); |
| 1281 | } |
| 1282 | } |
| 1283 | } |
Alec Mouri | d05a745 | 2024-09-20 13:44:02 +0000 | [diff] [blame] | 1284 | if (lastDataspace != mBufferInfo.mDataspace || |
| 1285 | mBufferInfo.mTimeSinceDataspaceUpdate == std::chrono::steady_clock::time_point::min()) { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 1286 | mFlinger->mHdrLayerInfoChanged = true; |
Alec Mouri | d05a745 | 2024-09-20 13:44:02 +0000 | [diff] [blame] | 1287 | const auto currentTime = std::chrono::steady_clock::now(); |
| 1288 | if (mBufferInfo.mTimeSinceDataspaceUpdate > std::chrono::steady_clock::time_point::min()) { |
| 1289 | mFlinger->mLayerEvents |
| 1290 | .emplace_back(mOwnerUid, getSequence(), lastDataspace, |
| 1291 | std::chrono::duration_cast<std::chrono::milliseconds>( |
| 1292 | currentTime - mBufferInfo.mTimeSinceDataspaceUpdate)); |
| 1293 | } |
| 1294 | mBufferInfo.mTimeSinceDataspaceUpdate = currentTime; |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 1295 | } |
Sally Qi | 963049b | 2023-03-23 14:06:21 -0700 | [diff] [blame] | 1296 | if (mBufferInfo.mDesiredHdrSdrRatio != mDrawingState.desiredHdrSdrRatio) { |
| 1297 | mBufferInfo.mDesiredHdrSdrRatio = mDrawingState.desiredHdrSdrRatio; |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 1298 | mFlinger->mHdrLayerInfoChanged = true; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1299 | } |
| 1300 | mBufferInfo.mCrop = computeBufferCrop(mDrawingState); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1301 | mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | Rect Layer::computeBufferCrop(const State& s) { |
| 1305 | if (s.buffer && !s.bufferCrop.isEmpty()) { |
| 1306 | Rect bufferCrop; |
| 1307 | s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop); |
| 1308 | return bufferCrop; |
| 1309 | } else if (s.buffer) { |
| 1310 | return s.buffer->getBounds(); |
| 1311 | } else { |
| 1312 | return s.bufferCrop; |
| 1313 | } |
| 1314 | } |
| 1315 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1316 | void Layer::decrementPendingBufferCount() { |
Melody Hsu | 1635578 | 2024-10-09 08:56:28 +0000 | [diff] [blame] | 1317 | int32_t pendingBuffers = --mPendingBuffers; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1318 | tracePendingBufferCount(pendingBuffers); |
| 1319 | } |
| 1320 | |
| 1321 | void Layer::tracePendingBufferCount(int32_t pendingBuffers) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1322 | SFTRACE_INT(mBlastTransactionName.c_str(), pendingBuffers); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 1325 | sp<LayerFE> Layer::getCompositionEngineLayerFE( |
| 1326 | const frontend::LayerHierarchy::TraversalPath& path) { |
| 1327 | for (auto& [p, layerFE] : mLayerFEs) { |
| 1328 | if (p == path) { |
| 1329 | return layerFE; |
| 1330 | } |
| 1331 | } |
Lloyd Pique | 70ddc67 | 2024-04-30 18:20:40 -0700 | [diff] [blame] | 1332 | auto layerFE = mFlinger->getFactory().createLayerFE(mName, this); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 1333 | mLayerFEs.emplace_back(path, layerFE); |
| 1334 | return layerFE; |
| 1335 | } |
| 1336 | |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 1337 | void Layer::onCompositionPresented(const DisplayDevice* display, |
| 1338 | const std::shared_ptr<FenceTime>& glDoneFence, |
| 1339 | const std::shared_ptr<FenceTime>& presentFence, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 1340 | const CompositorTiming& compositorTiming, |
| 1341 | gui::GameMode gameMode) { |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1342 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 1343 | // composition. |
| 1344 | if (!mBufferInfo.mFrameLatencyNeeded) return; |
| 1345 | |
| 1346 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 1347 | handle->gpuCompositionDoneFence = glDoneFence; |
| 1348 | handle->compositorTiming = compositorTiming; |
| 1349 | } |
| 1350 | |
| 1351 | // Update mFrameTracker. |
| 1352 | nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime; |
| 1353 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 1354 | |
| 1355 | const int32_t layerId = getSequence(); |
| 1356 | mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime); |
| 1357 | |
| 1358 | const auto outputLayer = findOutputLayerForDisplay(display); |
| 1359 | if (outputLayer && outputLayer->requiresClientComposition()) { |
| 1360 | nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp; |
| 1361 | mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber, |
| 1362 | clientCompositionTimestamp, |
| 1363 | FrameTracer::FrameEvent::FALLBACK_COMPOSITION); |
| 1364 | // Update the SurfaceFrames in the drawing state |
| 1365 | if (mDrawingState.bufferSurfaceFrameTX) { |
| 1366 | mDrawingState.bufferSurfaceFrameTX->setGpuComposition(); |
| 1367 | } |
| 1368 | for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) { |
| 1369 | surfaceFrame->setGpuComposition(); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime; |
| 1374 | if (frameReadyFence->isValid()) { |
| 1375 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
| 1376 | } else { |
| 1377 | // There was no fence for this frame, so assume that it was ready |
| 1378 | // to be presented at the desired present time. |
| 1379 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
| 1380 | } |
| 1381 | |
| 1382 | if (display) { |
Dominik Laskowski | fe749dc | 2024-07-26 10:09:31 -0400 | [diff] [blame] | 1383 | const auto activeMode = display->refreshRateSelector().getActiveMode(); |
| 1384 | const Fps refreshRate = activeMode.fps; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1385 | const std::optional<Fps> renderRate = |
| 1386 | mFlinger->mScheduler->getFrameRateOverride(getOwnerUid()); |
| 1387 | |
Alec Mouri | 7c1d8b5 | 2023-08-29 20:14:46 +0000 | [diff] [blame] | 1388 | const auto vote = frameRateToSetFrameRateVotePayload(getFrameRateForLayerTree()); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (presentFence->isValid()) { |
| 1391 | mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence, |
| 1392 | refreshRate, renderRate, vote, gameMode); |
| 1393 | mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber, |
| 1394 | presentFence, |
| 1395 | FrameTracer::FrameEvent::PRESENT_FENCE); |
| 1396 | mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence)); |
| 1397 | } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId()); |
| 1398 | displayId && mFlinger->getHwComposer().isConnected(*displayId)) { |
Dominik Laskowski | dc2bb80 | 2022-09-28 16:02:59 -0400 | [diff] [blame] | 1399 | // The HWC doesn't support present fences, so use the present timestamp instead. |
| 1400 | const nsecs_t presentTimestamp = |
| 1401 | mFlinger->getHwComposer().getPresentTimestamp(*displayId); |
| 1402 | |
| 1403 | const nsecs_t now = systemTime(CLOCK_MONOTONIC); |
Dominik Laskowski | fe749dc | 2024-07-26 10:09:31 -0400 | [diff] [blame] | 1404 | const nsecs_t vsyncPeriod = |
| 1405 | mFlinger->getHwComposer() |
| 1406 | .getDisplayVsyncPeriod(*displayId) |
| 1407 | .value_opt() |
| 1408 | .value_or(activeMode.modePtr->getVsyncRate().getPeriodNsecs()); |
| 1409 | |
Dominik Laskowski | dc2bb80 | 2022-09-28 16:02:59 -0400 | [diff] [blame] | 1410 | const nsecs_t actualPresentTime = now - ((now - presentTimestamp) % vsyncPeriod); |
| 1411 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1412 | mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime, |
| 1413 | refreshRate, renderRate, vote, gameMode); |
| 1414 | mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), |
| 1415 | mCurrentFrameNumber, actualPresentTime, |
| 1416 | FrameTracer::FrameEvent::PRESENT_FENCE); |
| 1417 | mFrameTracker.setActualPresentTime(actualPresentTime); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | mFrameTracker.advanceFrame(); |
| 1422 | mBufferInfo.mFrameLatencyNeeded = false; |
| 1423 | } |
| 1424 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 1425 | bool Layer::latchBufferImpl(bool& recomputeVisibleRegions, nsecs_t latchTime, bool bgColorOnly) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1426 | SFTRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(), |
| 1427 | getDrawingState().frameNumber); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1428 | |
| 1429 | bool refreshRequired = latchSidebandStream(recomputeVisibleRegions); |
| 1430 | |
| 1431 | if (refreshRequired) { |
| 1432 | return refreshRequired; |
| 1433 | } |
| 1434 | |
| 1435 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 1436 | // try again later |
| 1437 | if (!fenceHasSignaled()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1438 | SFTRACE_NAME("!fenceHasSignaled()"); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1439 | mFlinger->onLayerUpdate(); |
| 1440 | return false; |
| 1441 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 1442 | updateTexImage(latchTime, bgColorOnly); |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1443 | |
| 1444 | // Capture the old state of the layer for comparisons later |
| 1445 | BufferInfo oldBufferInfo = mBufferInfo; |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1446 | mPreviousFrameNumber = mCurrentFrameNumber; |
| 1447 | mCurrentFrameNumber = mDrawingState.frameNumber; |
| 1448 | gatherBufferInfo(); |
| 1449 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 1450 | if (mBufferInfo.mBuffer) { |
| 1451 | // We latched a buffer that will be presented soon. Clear the previously presented layer |
| 1452 | // stack list. |
| 1453 | mPreviouslyPresentedLayerStacks.clear(); |
| 1454 | } |
| 1455 | |
| 1456 | if (mDrawingState.buffer == nullptr) { |
| 1457 | const bool bufferReleased = oldBufferInfo.mBuffer != nullptr; |
| 1458 | recomputeVisibleRegions = bufferReleased; |
| 1459 | return bufferReleased; |
| 1460 | } |
| 1461 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1462 | if (oldBufferInfo.mBuffer == nullptr) { |
| 1463 | // the first time we receive a buffer, we need to trigger a |
| 1464 | // geometry invalidation. |
| 1465 | recomputeVisibleRegions = true; |
| 1466 | } |
| 1467 | |
| 1468 | if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) || |
| 1469 | (mBufferInfo.mTransform != oldBufferInfo.mTransform) || |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1470 | (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) { |
| 1471 | recomputeVisibleRegions = true; |
| 1472 | } |
| 1473 | |
| 1474 | if (oldBufferInfo.mBuffer != nullptr) { |
| 1475 | uint32_t bufWidth = mBufferInfo.mBuffer->getWidth(); |
| 1476 | uint32_t bufHeight = mBufferInfo.mBuffer->getHeight(); |
| 1477 | if (bufWidth != oldBufferInfo.mBuffer->getWidth() || |
| 1478 | bufHeight != oldBufferInfo.mBuffer->getHeight()) { |
| 1479 | recomputeVisibleRegions = true; |
| 1480 | } |
| 1481 | } |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1482 | return true; |
| 1483 | } |
| 1484 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1485 | bool Layer::getTransformToDisplayInverse() const { |
| 1486 | return mBufferInfo.mTransformToDisplayInverse; |
| 1487 | } |
| 1488 | |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1489 | ui::Dataspace Layer::translateDataspace(ui::Dataspace dataspace) { |
| 1490 | ui::Dataspace updatedDataspace = dataspace; |
| 1491 | // translate legacy dataspaces to modern dataspaces |
| 1492 | switch (dataspace) { |
Alec Mouri | 74c7ae1 | 2023-03-26 02:57:47 +0000 | [diff] [blame] | 1493 | // Treat unknown dataspaces as V0_sRGB |
| 1494 | case ui::Dataspace::UNKNOWN: |
Patrick Williams | bb25f80 | 2022-08-30 23:02:34 +0000 | [diff] [blame] | 1495 | case ui::Dataspace::SRGB: |
| 1496 | updatedDataspace = ui::Dataspace::V0_SRGB; |
| 1497 | break; |
| 1498 | case ui::Dataspace::SRGB_LINEAR: |
| 1499 | updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR; |
| 1500 | break; |
| 1501 | case ui::Dataspace::JFIF: |
| 1502 | updatedDataspace = ui::Dataspace::V0_JFIF; |
| 1503 | break; |
| 1504 | case ui::Dataspace::BT601_625: |
| 1505 | updatedDataspace = ui::Dataspace::V0_BT601_625; |
| 1506 | break; |
| 1507 | case ui::Dataspace::BT601_525: |
| 1508 | updatedDataspace = ui::Dataspace::V0_BT601_525; |
| 1509 | break; |
| 1510 | case ui::Dataspace::BT709: |
| 1511 | updatedDataspace = ui::Dataspace::V0_BT709; |
| 1512 | break; |
| 1513 | default: |
| 1514 | break; |
| 1515 | } |
| 1516 | |
| 1517 | return updatedDataspace; |
| 1518 | } |
| 1519 | |
| 1520 | sp<GraphicBuffer> Layer::getBuffer() const { |
| 1521 | return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr; |
| 1522 | } |
| 1523 | |
Chavi Weingarten | 328a831 | 2023-01-26 21:17:52 +0000 | [diff] [blame] | 1524 | bool Layer::setTrustedPresentationInfo(TrustedPresentationThresholds const& thresholds, |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 1525 | TrustedPresentationListener const& listener) { |
| 1526 | bool hadTrustedPresentationListener = hasTrustedPresentationListener(); |
| 1527 | mTrustedPresentationListener = listener; |
| 1528 | mTrustedPresentationThresholds = thresholds; |
| 1529 | bool haveTrustedPresentationListener = hasTrustedPresentationListener(); |
| 1530 | if (!hadTrustedPresentationListener && haveTrustedPresentationListener) { |
| 1531 | mFlinger->mNumTrustedPresentationListeners++; |
| 1532 | } else if (hadTrustedPresentationListener && !haveTrustedPresentationListener) { |
| 1533 | mFlinger->mNumTrustedPresentationListeners--; |
| 1534 | } |
Chavi Weingarten | 328a831 | 2023-01-26 21:17:52 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Reset trusted presentation states to ensure we start the time again. |
| 1537 | mEnteredTrustedPresentationStateTime = -1; |
| 1538 | mLastReportedTrustedPresentationState = false; |
| 1539 | mLastComputedTrustedPresentationState = false; |
| 1540 | |
| 1541 | // If there's a new trusted presentation listener, the code needs to go through the composite |
| 1542 | // path to ensure it recomutes the current state and invokes the TrustedPresentationListener if |
| 1543 | // we're already in the requested state. |
| 1544 | return haveTrustedPresentationListener; |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 1547 | void Layer::setBufferReleaseChannel( |
| 1548 | const std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint>& channel) { |
| 1549 | mBufferReleaseChannel = channel; |
| 1550 | } |
| 1551 | |
Vishnu Nair | a156f48 | 2023-02-22 00:23:38 +0000 | [diff] [blame] | 1552 | void Layer::updateLastLatchTime(nsecs_t latchTime) { |
| 1553 | mLastLatchTime = latchTime; |
| 1554 | } |
| 1555 | |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1556 | void Layer::setIsSmallDirty(frontend::LayerSnapshot* snapshot) { |
Jerry Chang | 3667800 | 2023-11-29 16:56:17 +0000 | [diff] [blame] | 1557 | if (!mFlinger->mScheduler->supportSmallDirtyDetection(mOwnerAppId)) { |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1558 | snapshot->isSmallDirty = false; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | if (mWindowType != WindowInfo::Type::APPLICATION && |
| 1563 | mWindowType != WindowInfo::Type::BASE_APPLICATION) { |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1564 | snapshot->isSmallDirty = false; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | } |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 1567 | |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1568 | Rect bounds = snapshot->surfaceDamage.getBounds(); |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 1569 | if (!bounds.isValid()) { |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1570 | snapshot->isSmallDirty = false; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
| 1573 | |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 1574 | // Transform to screen space. |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1575 | bounds = snapshot->localTransform.transform(bounds); |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 1576 | |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 1577 | // If the damage region is a small dirty, this could give the hint for the layer history that |
| 1578 | // it could suppress the heuristic rate when calculating. |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 1579 | snapshot->isSmallDirty = |
| 1580 | mFlinger->mScheduler->isSmallDirtyArea(mOwnerAppId, |
| 1581 | bounds.getWidth() * bounds.getHeight()); |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 1584 | } // namespace android |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1585 | |
| 1586 | #if defined(__gl_h_) |
| 1587 | #error "don't include gl/gl.h in this file" |
| 1588 | #endif |
| 1589 | |
| 1590 | #if defined(__gl2_h_) |
| 1591 | #error "don't include gl2/gl2.h in this file" |
| 1592 | #endif |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 1593 | |
| 1594 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 1595 | #pragma clang diagnostic pop // ignored "-Wconversion" |