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