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