Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 20 | #pragma clang diagnostic ignored "-Wextra" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 21 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 22 | //#define LOG_NDEBUG 0 |
| 23 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 24 | #undef LOG_TAG |
| 25 | #define LOG_TAG "RegionSamplingThread" |
| 26 | |
| 27 | #include "RegionSamplingThread.h" |
| 28 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 29 | #include <common/trace.h> |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 30 | #include <compositionengine/Display.h> |
| 31 | #include <compositionengine/impl/OutputCompositionState.h> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 33 | #include <ftl/future.h> |
Huihong Luo | a339d0a | 2022-02-05 09:42:42 -0800 | [diff] [blame] | 34 | #include <gui/SpHash.h> |
chaviw | e7b9f27 | 2020-08-18 16:08:59 -0700 | [diff] [blame] | 35 | #include <gui/SyncScreenCaptureListener.h> |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 36 | #include <renderengine/impl/ExternalTexture.h> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 37 | #include <ui/DisplayStatInfo.h> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 38 | |
| 39 | #include <string> |
| 40 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 41 | #include "DisplayDevice.h" |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 42 | #include "DisplayRenderArea.h" |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 43 | #include "FrontEnd/LayerCreationArgs.h" |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 44 | #include "Layer.h" |
Melody Hsu | 41ade20 | 2024-05-03 01:25:50 +0000 | [diff] [blame] | 45 | #include "RenderAreaBuilder.h" |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 46 | #include "Scheduler/VsyncController.h" |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 47 | #include "SurfaceFlinger.h" |
| 48 | |
| 49 | namespace android { |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 50 | using namespace std::chrono_literals; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 51 | |
Huihong Luo | a339d0a | 2022-02-05 09:42:42 -0800 | [diff] [blame] | 52 | using gui::SpHash; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 53 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 54 | constexpr auto lumaSamplingStepTag = "LumaSamplingStep"; |
| 55 | enum class samplingStep { |
| 56 | noWorkNeeded, |
| 57 | idleTimerWaiting, |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 58 | waitForQuietFrame, |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 59 | waitForSamplePhase, |
| 60 | sample |
| 61 | }; |
| 62 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 63 | constexpr auto defaultRegionSamplingWorkDuration = 3ms; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 64 | constexpr auto defaultRegionSamplingPeriod = 100ms; |
| 65 | constexpr auto defaultRegionSamplingTimerTimeout = 100ms; |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 66 | constexpr auto maxRegionSamplingDelay = 100ms; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 67 | // TODO: (b/127403193) duration to string conversion could probably be constexpr |
| 68 | template <typename Rep, typename Per> |
| 69 | inline std::string toNsString(std::chrono::duration<Rep, Per> t) { |
| 70 | return std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(t).count()); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 73 | RegionSamplingThread::EnvironmentTimingTunables::EnvironmentTimingTunables() { |
| 74 | char value[PROPERTY_VALUE_MAX] = {}; |
| 75 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 76 | property_get("debug.sf.region_sampling_duration_ns", value, |
| 77 | toNsString(defaultRegionSamplingWorkDuration).c_str()); |
| 78 | int const samplingDurationNsRaw = atoi(value); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 79 | |
| 80 | property_get("debug.sf.region_sampling_period_ns", value, |
| 81 | toNsString(defaultRegionSamplingPeriod).c_str()); |
| 82 | int const samplingPeriodNsRaw = atoi(value); |
| 83 | |
| 84 | property_get("debug.sf.region_sampling_timer_timeout_ns", value, |
| 85 | toNsString(defaultRegionSamplingTimerTimeout).c_str()); |
| 86 | int const samplingTimerTimeoutNsRaw = atoi(value); |
| 87 | |
| 88 | if ((samplingPeriodNsRaw < 0) || (samplingTimerTimeoutNsRaw < 0)) { |
| 89 | ALOGW("User-specified sampling tuning options nonsensical. Using defaults"); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 90 | mSamplingDuration = defaultRegionSamplingWorkDuration; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 91 | mSamplingPeriod = defaultRegionSamplingPeriod; |
| 92 | mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout; |
| 93 | } else { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 94 | mSamplingDuration = std::chrono::nanoseconds(samplingDurationNsRaw); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 95 | mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw); |
| 96 | mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw); |
| 97 | } |
| 98 | } |
| 99 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 100 | RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables) |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 101 | : mFlinger(flinger), |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 102 | mTunables(tunables), |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 103 | mIdleTimer( |
Ady Abraham | dde984b | 2021-03-18 12:47:36 -0700 | [diff] [blame] | 104 | "RegSampIdle", |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 105 | std::chrono::duration_cast<std::chrono::milliseconds>( |
| 106 | mTunables.mSamplingTimerTimeout), |
| 107 | [] {}, [this] { checkForStaleLuma(); }), |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 108 | mLastSampleTime(0ns) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 109 | mThread = std::thread([this]() { threadMain(); }); |
Ady Abraham | dde984b | 2021-03-18 12:47:36 -0700 | [diff] [blame] | 110 | pthread_setname_np(mThread.native_handle(), "RegionSampling"); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 111 | mIdleTimer.start(); |
| 112 | } |
| 113 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 114 | RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger) |
| 115 | : RegionSamplingThread(flinger, |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 116 | TimingTunables{defaultRegionSamplingWorkDuration, |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 117 | defaultRegionSamplingPeriod, |
| 118 | defaultRegionSamplingTimerTimeout}) {} |
| 119 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 120 | RegionSamplingThread::~RegionSamplingThread() { |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 121 | mIdleTimer.stop(); |
| 122 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 123 | { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 124 | std::lock_guard lock(mThreadControlMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 125 | mRunning = false; |
| 126 | mCondition.notify_one(); |
| 127 | } |
| 128 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 129 | if (mThread.joinable()) { |
| 130 | mThread.join(); |
| 131 | } |
| 132 | } |
| 133 | |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 134 | void RegionSamplingThread::addListener(const Rect& samplingArea, uint32_t stopLayerId, |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 135 | const sp<IRegionSamplingListener>& listener) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 136 | sp<IBinder> asBinder = IInterface::asBinder(listener); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 137 | asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this)); |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 138 | std::lock_guard lock(mSamplingMutex); |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 139 | mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayerId, listener}); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 143 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 144 | mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener))); |
| 145 | } |
| 146 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 147 | void RegionSamplingThread::checkForStaleLuma() { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 148 | std::lock_guard lock(mThreadControlMutex); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 149 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 150 | if (mSampleRequestTime.has_value()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 151 | SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase)); |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 152 | mSampleRequestTime.reset(); |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 153 | mFlinger.scheduleSample(); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 157 | void RegionSamplingThread::onCompositionComplete( |
| 158 | std::optional<std::chrono::steady_clock::time_point> samplingDeadline) { |
| 159 | doSample(samplingDeadline); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 162 | void RegionSamplingThread::doSample( |
| 163 | std::optional<std::chrono::steady_clock::time_point> samplingDeadline) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 164 | std::lock_guard lock(mThreadControlMutex); |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 165 | const auto now = std::chrono::steady_clock::now(); |
| 166 | if (mLastSampleTime + mTunables.mSamplingPeriod > now) { |
| 167 | // content changed, but we sampled not too long ago, so we need to sample some time in the |
| 168 | // future. |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 169 | SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting)); |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 170 | mSampleRequestTime = now; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 173 | if (!mSampleRequestTime.has_value() || now - *mSampleRequestTime < maxRegionSamplingDelay) { |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 174 | // If there is relatively little time left for surfaceflinger |
| 175 | // until the next vsync deadline, defer this sampling work |
| 176 | // to a later frame, when hopefully there will be more time. |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 177 | if (samplingDeadline.has_value() && now + mTunables.mSamplingDuration > *samplingDeadline) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 178 | SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame)); |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 179 | mSampleRequestTime = mSampleRequestTime.value_or(now); |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | } |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 183 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 184 | SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample)); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 185 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 186 | mSampleRequestTime.reset(); |
| 187 | mLastSampleTime = now; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 188 | |
| 189 | mIdleTimer.reset(); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 190 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 191 | mSampleRequested = true; |
| 192 | mCondition.notify_one(); |
| 193 | } |
| 194 | |
| 195 | void RegionSamplingThread::binderDied(const wp<IBinder>& who) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 196 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 197 | mDescriptors.erase(who); |
| 198 | } |
| 199 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 200 | float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride, |
| 201 | uint32_t orientation, const Rect& sample_area) { |
| 202 | if (!sample_area.isValid() || (sample_area.getWidth() > width) || |
| 203 | (sample_area.getHeight() > height)) { |
| 204 | ALOGE("invalid sampling region requested"); |
| 205 | return 0.0f; |
| 206 | } |
| 207 | |
Alec Mouri | 439b609 | 2022-09-14 22:24:17 +0000 | [diff] [blame] | 208 | const uint32_t pixelCount = |
| 209 | (sample_area.bottom - sample_area.top) * (sample_area.right - sample_area.left); |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 210 | uint32_t accumulatedLuma = 0; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 211 | |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 212 | // Calculates luma with approximation of Rec. 709 primaries |
Alec Mouri | 439b609 | 2022-09-14 22:24:17 +0000 | [diff] [blame] | 213 | for (int32_t row = sample_area.top; row < sample_area.bottom; ++row) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 214 | const uint32_t* rowBase = data + row * stride; |
Alec Mouri | 439b609 | 2022-09-14 22:24:17 +0000 | [diff] [blame] | 215 | for (int32_t column = sample_area.left; column < sample_area.right; ++column) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 216 | uint32_t pixel = rowBase[column]; |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 217 | const uint32_t r = pixel & 0xFF; |
| 218 | const uint32_t g = (pixel >> 8) & 0xFF; |
| 219 | const uint32_t b = (pixel >> 16) & 0xFF; |
| 220 | const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5; |
| 221 | accumulatedLuma += luma; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 225 | return accumulatedLuma / (255.0f * pixelCount); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 226 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 227 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 228 | std::vector<float> RegionSamplingThread::sampleBuffer( |
| 229 | const sp<GraphicBuffer>& buffer, const Point& leftTop, |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 230 | const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 231 | void* data_raw = nullptr; |
| 232 | buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw); |
| 233 | std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw), |
| 234 | [&buffer](auto) { buffer->unlock(); }); |
| 235 | if (!data) return {}; |
| 236 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 237 | const int32_t width = buffer->getWidth(); |
| 238 | const int32_t height = buffer->getHeight(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 239 | const int32_t stride = buffer->getStride(); |
| 240 | std::vector<float> lumas(descriptors.size()); |
| 241 | std::transform(descriptors.begin(), descriptors.end(), lumas.begin(), |
| 242 | [&](auto const& descriptor) { |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 243 | return sampleArea(data.get(), width, height, stride, orientation, |
| 244 | descriptor.area - leftTop); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 245 | }); |
| 246 | return lumas; |
| 247 | } |
| 248 | |
| 249 | void RegionSamplingThread::captureSample() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 250 | SFTRACE_CALL(); |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 251 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 252 | |
| 253 | if (mDescriptors.empty()) { |
| 254 | return; |
| 255 | } |
| 256 | |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 257 | wp<const DisplayDevice> displayWeak; |
| 258 | |
| 259 | ui::LayerStack layerStack; |
| 260 | ui::Transform::RotationFlags orientation; |
| 261 | ui::Size displaySize; |
| 262 | |
| 263 | { |
| 264 | // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread |
| 265 | const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice(); |
| 266 | displayWeak = display; |
| 267 | layerStack = display->getLayerStack(); |
| 268 | orientation = ui::Transform::toRotationFlags(display->getOrientation()); |
| 269 | displaySize = display->getSize(); |
| 270 | } |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 271 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 272 | std::vector<RegionSamplingThread::Descriptor> descriptors; |
| 273 | Region sampleRegion; |
| 274 | for (const auto& [listener, descriptor] : mDescriptors) { |
| 275 | sampleRegion.orSelf(descriptor.area); |
| 276 | descriptors.emplace_back(descriptor); |
| 277 | } |
| 278 | |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 279 | const Rect sampledBounds = sampleRegion.bounds(); |
| 280 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 281 | std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners; |
| 282 | |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 283 | auto layerFilterFn = [&](const char* layerName, uint32_t layerId, const Rect& bounds, |
| 284 | const ui::Transform transform, bool& outStopTraversal) -> bool { |
| 285 | // Likewise if we just found a stop layer, set the flag and abort |
| 286 | for (const auto& [area, stopLayerId, listener] : descriptors) { |
| 287 | if (stopLayerId != UNASSIGNED_LAYER_ID && layerId == stopLayerId) { |
| 288 | outStopTraversal = true; |
| 289 | return false; |
| 290 | } |
| 291 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 292 | |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 293 | // Compute the layer's position on the screen |
| 294 | constexpr bool roundOutwards = true; |
| 295 | Rect transformed = transform.transform(bounds, roundOutwards); |
| 296 | |
| 297 | // If this layer doesn't intersect with the larger sampledBounds, skip capturing it |
| 298 | Rect ignore; |
| 299 | if (!transformed.intersect(sampledBounds, &ignore)) return false; |
| 300 | |
| 301 | // If the layer doesn't intersect a sampling area, skip capturing it |
| 302 | bool intersectsAnyArea = false; |
| 303 | for (const auto& [area, stopLayer, listener] : descriptors) { |
| 304 | if (transformed.intersect(area, &ignore)) { |
| 305 | intersectsAnyArea = true; |
| 306 | listeners.insert(listener); |
| 307 | } |
| 308 | } |
| 309 | if (!intersectsAnyArea) return false; |
| 310 | |
| 311 | ALOGV("Traversing [%s] [%d, %d, %d, %d]", layerName, bounds.left, bounds.top, bounds.right, |
| 312 | bounds.bottom); |
| 313 | |
| 314 | return true; |
| 315 | }; |
| 316 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 317 | auto filterFn = [&](const frontend::LayerSnapshot& snapshot, bool& outStopTraversal) -> bool { |
| 318 | const Rect bounds = frontend::RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds), |
| 319 | snapshot.transparentRegionHint); |
| 320 | const ui::Transform transform = snapshot.geomLayerTransform; |
| 321 | return layerFilterFn(snapshot.name.c_str(), snapshot.path.id, bounds, transform, |
| 322 | outStopTraversal); |
| 323 | }; |
| 324 | auto getLayerSnapshotsFn = |
| 325 | mFlinger.getLayerSnapshotsForScreenshots(layerStack, CaptureArgs::UNSET_UID, filterFn); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 326 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 327 | std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr; |
| 328 | if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() && |
| 329 | mCachedBuffer->getBuffer()->getHeight() == sampledBounds.getHeight()) { |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 330 | buffer = mCachedBuffer; |
| 331 | } else { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 332 | const uint32_t usage = |
| 333 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 334 | sp<GraphicBuffer> graphicBuffer = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 335 | sp<GraphicBuffer>::make(sampledBounds.getWidth(), sampledBounds.getHeight(), |
| 336 | PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread"); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 337 | const status_t bufferStatus = graphicBuffer->initCheck(); |
Alec Mouri | 7013b6f | 2021-02-12 11:16:54 -0800 | [diff] [blame] | 338 | LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "captureSample: Buffer failed to allocate: %d", |
| 339 | bufferStatus); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 340 | buffer = std::make_shared< |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 341 | renderengine::impl::ExternalTexture>(graphicBuffer, mFlinger.getRenderEngine(), |
| 342 | renderengine::impl::ExternalTexture::Usage:: |
| 343 | WRITEABLE); |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 344 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 345 | |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 346 | constexpr bool kRegionSampling = true; |
| 347 | constexpr bool kGrayscale = false; |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 348 | constexpr bool kIsProtected = false; |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 349 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 350 | SurfaceFlinger::RenderAreaBuilderVariant |
| 351 | renderAreaBuilder(std::in_place_type<DisplayRenderAreaBuilder>, sampledBounds, |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 352 | sampledBounds.getSize(), ui::Dataspace::V0_SRGB, displayWeak, |
| 353 | RenderArea::Options::CAPTURE_SECURE_LAYERS); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 354 | |
| 355 | FenceResult fenceResult; |
| 356 | if (FlagManager::getInstance().single_hop_screenshot() && |
| 357 | FlagManager::getInstance().ce_fence_promise()) { |
| 358 | std::vector<sp<LayerFE>> layerFEs; |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 359 | auto displayState = mFlinger.getSnapshotsFromMainThread(renderAreaBuilder, |
| 360 | getLayerSnapshotsFn, layerFEs); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 361 | fenceResult = |
| 362 | mFlinger.captureScreenshot(renderAreaBuilder, buffer, kRegionSampling, kGrayscale, |
| 363 | kIsProtected, nullptr, displayState, layerFEs) |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 364 | .get(); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 365 | } else { |
| 366 | fenceResult = |
| 367 | mFlinger.captureScreenshotLegacy(renderAreaBuilder, getLayerSnapshotsFn, buffer, |
| 368 | kRegionSampling, kGrayscale, kIsProtected, nullptr) |
| 369 | .get(); |
| 370 | } |
| 371 | if (fenceResult.ok()) { |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 372 | fenceResult.value()->waitForever(LOG_TAG); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 373 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 374 | |
| 375 | std::vector<Descriptor> activeDescriptors; |
| 376 | for (const auto& descriptor : descriptors) { |
| 377 | if (listeners.count(descriptor.listener) != 0) { |
| 378 | activeDescriptors.emplace_back(descriptor); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | ALOGV("Sampling %zu descriptors", activeDescriptors.size()); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 383 | std::vector<float> lumas = sampleBuffer(buffer->getBuffer(), sampledBounds.leftTop(), |
| 384 | activeDescriptors, orientation); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 385 | if (lumas.size() != activeDescriptors.size()) { |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 386 | ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(), |
| 387 | activeDescriptors.size()); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
| 391 | for (size_t d = 0; d < activeDescriptors.size(); ++d) { |
| 392 | activeDescriptors[d].listener->onSampleCollected(lumas[d]); |
| 393 | } |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 394 | |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 395 | mCachedBuffer = buffer; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 396 | SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded)); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 399 | // NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations. |
| 400 | void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS { |
| 401 | std::unique_lock<std::mutex> lock(mThreadControlMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 402 | while (mRunning) { |
| 403 | if (mSampleRequested) { |
| 404 | mSampleRequested = false; |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 405 | lock.unlock(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 406 | captureSample(); |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 407 | lock.lock(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 408 | } |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 409 | mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) { |
| 410 | return mSampleRequested || !mRunning; |
| 411 | }); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 416 | |
| 417 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 418 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |