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" |
| 20 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 21 | //#define LOG_NDEBUG 0 |
| 22 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 23 | #undef LOG_TAG |
| 24 | #define LOG_TAG "RegionSamplingThread" |
| 25 | |
| 26 | #include "RegionSamplingThread.h" |
| 27 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 28 | #include <compositionengine/Display.h> |
| 29 | #include <compositionengine/impl/OutputCompositionState.h> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
| 31 | #include <gui/IRegionSamplingListener.h> |
| 32 | #include <ui/DisplayStatInfo.h> |
| 33 | #include <utils/Trace.h> |
| 34 | |
| 35 | #include <string> |
| 36 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 37 | #include "DisplayDevice.h" |
| 38 | #include "Layer.h" |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 39 | #include "Promise.h" |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 40 | #include "Scheduler/DispSync.h" |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 41 | #include "SurfaceFlinger.h" |
| 42 | |
| 43 | namespace android { |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 44 | using namespace std::chrono_literals; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 45 | |
| 46 | template <typename T> |
| 47 | struct SpHash { |
| 48 | size_t operator()(const sp<T>& p) const { return std::hash<T*>()(p.get()); } |
| 49 | }; |
| 50 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 51 | constexpr auto lumaSamplingStepTag = "LumaSamplingStep"; |
| 52 | enum class samplingStep { |
| 53 | noWorkNeeded, |
| 54 | idleTimerWaiting, |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 55 | waitForQuietFrame, |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 56 | waitForZeroPhase, |
| 57 | waitForSamplePhase, |
| 58 | sample |
| 59 | }; |
| 60 | |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 61 | constexpr auto timeForRegionSampling = 5000000ns; |
| 62 | constexpr auto maxRegionSamplingSkips = 10; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 63 | constexpr auto defaultRegionSamplingOffset = -3ms; |
| 64 | constexpr auto defaultRegionSamplingPeriod = 100ms; |
| 65 | constexpr auto defaultRegionSamplingTimerTimeout = 100ms; |
| 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 | |
| 75 | property_get("debug.sf.region_sampling_offset_ns", value, |
| 76 | toNsString(defaultRegionSamplingOffset).c_str()); |
| 77 | int const samplingOffsetNsRaw = atoi(value); |
| 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"); |
| 89 | mSamplingOffset = defaultRegionSamplingOffset; |
| 90 | mSamplingPeriod = defaultRegionSamplingPeriod; |
| 91 | mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout; |
| 92 | } else { |
| 93 | mSamplingOffset = std::chrono::nanoseconds(samplingOffsetNsRaw); |
| 94 | mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw); |
| 95 | mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | struct SamplingOffsetCallback : DispSync::Callback { |
| 100 | SamplingOffsetCallback(RegionSamplingThread& samplingThread, Scheduler& scheduler, |
| 101 | std::chrono::nanoseconds targetSamplingOffset) |
| 102 | : mRegionSamplingThread(samplingThread), |
| 103 | mScheduler(scheduler), |
| 104 | mTargetSamplingOffset(targetSamplingOffset) {} |
| 105 | |
| 106 | ~SamplingOffsetCallback() { stopVsyncListener(); } |
| 107 | |
| 108 | SamplingOffsetCallback(const SamplingOffsetCallback&) = delete; |
| 109 | SamplingOffsetCallback& operator=(const SamplingOffsetCallback&) = delete; |
| 110 | |
| 111 | void startVsyncListener() { |
| 112 | std::lock_guard lock(mMutex); |
| 113 | if (mVsyncListening) return; |
| 114 | |
| 115 | mPhaseIntervalSetting = Phase::ZERO; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 116 | mScheduler.getPrimaryDispSync().addEventListener("SamplingThreadDispSyncListener", 0, this, |
| 117 | mLastCallbackTime); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 118 | mVsyncListening = true; |
| 119 | } |
| 120 | |
| 121 | void stopVsyncListener() { |
| 122 | std::lock_guard lock(mMutex); |
| 123 | stopVsyncListenerLocked(); |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | void stopVsyncListenerLocked() /*REQUIRES(mMutex)*/ { |
| 128 | if (!mVsyncListening) return; |
| 129 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 130 | mScheduler.getPrimaryDispSync().removeEventListener(this, &mLastCallbackTime); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 131 | mVsyncListening = false; |
| 132 | } |
| 133 | |
Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 134 | void onDispSyncEvent(nsecs_t /*when*/, nsecs_t /*expectedVSyncTimestamp*/) final { |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 135 | std::unique_lock<decltype(mMutex)> lock(mMutex); |
| 136 | |
| 137 | if (mPhaseIntervalSetting == Phase::ZERO) { |
| 138 | ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase)); |
| 139 | mPhaseIntervalSetting = Phase::SAMPLING; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 140 | mScheduler.getPrimaryDispSync().changePhaseOffset(this, mTargetSamplingOffset.count()); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | if (mPhaseIntervalSetting == Phase::SAMPLING) { |
| 145 | mPhaseIntervalSetting = Phase::ZERO; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 146 | mScheduler.getPrimaryDispSync().changePhaseOffset(this, 0); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 147 | stopVsyncListenerLocked(); |
| 148 | lock.unlock(); |
| 149 | mRegionSamplingThread.notifySamplingOffset(); |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | RegionSamplingThread& mRegionSamplingThread; |
| 155 | Scheduler& mScheduler; |
| 156 | const std::chrono::nanoseconds mTargetSamplingOffset; |
| 157 | mutable std::mutex mMutex; |
Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 158 | nsecs_t mLastCallbackTime = 0; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 159 | enum class Phase { |
| 160 | ZERO, |
| 161 | SAMPLING |
| 162 | } mPhaseIntervalSetting /*GUARDED_BY(mMutex) macro doesnt work with unique_lock?*/ |
| 163 | = Phase::ZERO; |
| 164 | bool mVsyncListening /*GUARDED_BY(mMutex)*/ = false; |
| 165 | }; |
| 166 | |
| 167 | RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler, |
| 168 | const TimingTunables& tunables) |
| 169 | : mFlinger(flinger), |
| 170 | mScheduler(scheduler), |
| 171 | mTunables(tunables), |
| 172 | mIdleTimer(std::chrono::duration_cast<std::chrono::milliseconds>( |
| 173 | mTunables.mSamplingTimerTimeout), |
| 174 | [] {}, [this] { checkForStaleLuma(); }), |
| 175 | mPhaseCallback(std::make_unique<SamplingOffsetCallback>(*this, mScheduler, |
| 176 | tunables.mSamplingOffset)), |
| 177 | lastSampleTime(0ns) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 178 | mThread = std::thread([this]() { threadMain(); }); |
| 179 | pthread_setname_np(mThread.native_handle(), "RegionSamplingThread"); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 180 | mIdleTimer.start(); |
| 181 | } |
| 182 | |
| 183 | RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler) |
| 184 | : RegionSamplingThread(flinger, scheduler, |
| 185 | TimingTunables{defaultRegionSamplingOffset, |
| 186 | defaultRegionSamplingPeriod, |
| 187 | defaultRegionSamplingTimerTimeout}) {} |
| 188 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 189 | RegionSamplingThread::~RegionSamplingThread() { |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 190 | mIdleTimer.stop(); |
| 191 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 192 | { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 193 | std::lock_guard lock(mThreadControlMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 194 | mRunning = false; |
| 195 | mCondition.notify_one(); |
| 196 | } |
| 197 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 198 | if (mThread.joinable()) { |
| 199 | mThread.join(); |
| 200 | } |
| 201 | } |
| 202 | |
Alec Mouri | 9a02eda | 2020-04-21 17:39:34 -0700 | [diff] [blame] | 203 | void RegionSamplingThread::addListener(const Rect& samplingArea, const wp<Layer>& stopLayer, |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 204 | const sp<IRegionSamplingListener>& listener) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 205 | sp<IBinder> asBinder = IInterface::asBinder(listener); |
| 206 | asBinder->linkToDeath(this); |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 207 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 208 | mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayer, listener}); |
| 209 | } |
| 210 | |
| 211 | void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 212 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 213 | mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener))); |
| 214 | } |
| 215 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 216 | void RegionSamplingThread::checkForStaleLuma() { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 217 | std::lock_guard lock(mThreadControlMutex); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 218 | |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 219 | if (mDiscardedFrames > 0) { |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 220 | ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForZeroPhase)); |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 221 | mDiscardedFrames = 0; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 222 | mPhaseCallback->startVsyncListener(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void RegionSamplingThread::notifyNewContent() { |
| 227 | doSample(); |
| 228 | } |
| 229 | |
| 230 | void RegionSamplingThread::notifySamplingOffset() { |
| 231 | doSample(); |
| 232 | } |
| 233 | |
| 234 | void RegionSamplingThread::doSample() { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 235 | std::lock_guard lock(mThreadControlMutex); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 236 | auto now = std::chrono::nanoseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 237 | if (lastSampleTime + mTunables.mSamplingPeriod > now) { |
| 238 | ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting)); |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 239 | if (mDiscardedFrames == 0) mDiscardedFrames++; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 240 | return; |
| 241 | } |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 242 | if (mDiscardedFrames < maxRegionSamplingSkips) { |
| 243 | // If there is relatively little time left for surfaceflinger |
| 244 | // until the next vsync deadline, defer this sampling work |
| 245 | // to a later frame, when hopefully there will be more time. |
| 246 | DisplayStatInfo stats; |
| 247 | mScheduler.getDisplayStatInfo(&stats); |
| 248 | if (std::chrono::nanoseconds(stats.vsyncTime) - now < timeForRegionSampling) { |
| 249 | ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame)); |
| 250 | mDiscardedFrames++; |
| 251 | return; |
| 252 | } |
| 253 | } |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 254 | |
| 255 | ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample)); |
| 256 | |
John Dias | 84be783 | 2019-06-18 17:05:26 -0700 | [diff] [blame] | 257 | mDiscardedFrames = 0; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 258 | lastSampleTime = now; |
| 259 | |
| 260 | mIdleTimer.reset(); |
| 261 | mPhaseCallback->stopVsyncListener(); |
| 262 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 263 | mSampleRequested = true; |
| 264 | mCondition.notify_one(); |
| 265 | } |
| 266 | |
| 267 | void RegionSamplingThread::binderDied(const wp<IBinder>& who) { |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 268 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 269 | mDescriptors.erase(who); |
| 270 | } |
| 271 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 272 | float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride, |
| 273 | uint32_t orientation, const Rect& sample_area) { |
| 274 | if (!sample_area.isValid() || (sample_area.getWidth() > width) || |
| 275 | (sample_area.getHeight() > height)) { |
| 276 | ALOGE("invalid sampling region requested"); |
| 277 | return 0.0f; |
| 278 | } |
| 279 | |
| 280 | // (b/133849373) ROT_90 screencap images produced upside down |
| 281 | auto area = sample_area; |
| 282 | if (orientation & ui::Transform::ROT_90) { |
| 283 | area.top = height - area.top; |
| 284 | area.bottom = height - area.bottom; |
| 285 | std::swap(area.top, area.bottom); |
Kevin DuBois | 69162d0 | 2019-06-04 20:22:43 -0700 | [diff] [blame] | 286 | |
| 287 | area.left = width - area.left; |
| 288 | area.right = width - area.right; |
| 289 | std::swap(area.left, area.right); |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 292 | const uint32_t pixelCount = (area.bottom - area.top) * (area.right - area.left); |
| 293 | uint32_t accumulatedLuma = 0; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 294 | |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 295 | // Calculates luma with approximation of Rec. 709 primaries |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 296 | for (int32_t row = area.top; row < area.bottom; ++row) { |
| 297 | const uint32_t* rowBase = data + row * stride; |
| 298 | for (int32_t column = area.left; column < area.right; ++column) { |
| 299 | uint32_t pixel = rowBase[column]; |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 300 | const uint32_t r = pixel & 0xFF; |
| 301 | const uint32_t g = (pixel >> 8) & 0xFF; |
| 302 | const uint32_t b = (pixel >> 16) & 0xFF; |
| 303 | const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5; |
| 304 | accumulatedLuma += luma; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Collin Fijalkovich | a95e170 | 2019-10-28 14:46:13 -0700 | [diff] [blame] | 308 | return accumulatedLuma / (255.0f * pixelCount); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 309 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 310 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 311 | std::vector<float> RegionSamplingThread::sampleBuffer( |
| 312 | const sp<GraphicBuffer>& buffer, const Point& leftTop, |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 313 | const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 314 | void* data_raw = nullptr; |
| 315 | buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw); |
| 316 | std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw), |
| 317 | [&buffer](auto) { buffer->unlock(); }); |
| 318 | if (!data) return {}; |
| 319 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 320 | const int32_t width = buffer->getWidth(); |
| 321 | const int32_t height = buffer->getHeight(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 322 | const int32_t stride = buffer->getStride(); |
| 323 | std::vector<float> lumas(descriptors.size()); |
| 324 | std::transform(descriptors.begin(), descriptors.end(), lumas.begin(), |
| 325 | [&](auto const& descriptor) { |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 326 | return sampleArea(data.get(), width, height, stride, orientation, |
| 327 | descriptor.area - leftTop); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 328 | }); |
| 329 | return lumas; |
| 330 | } |
| 331 | |
| 332 | void RegionSamplingThread::captureSample() { |
| 333 | ATRACE_CALL(); |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 334 | std::lock_guard lock(mSamplingMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 335 | |
| 336 | if (mDescriptors.empty()) { |
| 337 | return; |
| 338 | } |
| 339 | |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 340 | wp<const DisplayDevice> displayWeak; |
| 341 | |
| 342 | ui::LayerStack layerStack; |
| 343 | ui::Transform::RotationFlags orientation; |
| 344 | ui::Size displaySize; |
| 345 | |
| 346 | { |
| 347 | // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread |
| 348 | const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice(); |
| 349 | displayWeak = display; |
| 350 | layerStack = display->getLayerStack(); |
| 351 | orientation = ui::Transform::toRotationFlags(display->getOrientation()); |
| 352 | displaySize = display->getSize(); |
| 353 | } |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 354 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 355 | std::vector<RegionSamplingThread::Descriptor> descriptors; |
| 356 | Region sampleRegion; |
| 357 | for (const auto& [listener, descriptor] : mDescriptors) { |
| 358 | sampleRegion.orSelf(descriptor.area); |
| 359 | descriptors.emplace_back(descriptor); |
| 360 | } |
| 361 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 362 | auto dx = 0; |
| 363 | auto dy = 0; |
| 364 | switch (orientation) { |
| 365 | case ui::Transform::ROT_90: |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 366 | dx = displaySize.getWidth(); |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 367 | break; |
| 368 | case ui::Transform::ROT_180: |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 369 | dx = displaySize.getWidth(); |
| 370 | dy = displaySize.getHeight(); |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 371 | break; |
| 372 | case ui::Transform::ROT_270: |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 373 | dy = displaySize.getHeight(); |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 374 | break; |
| 375 | default: |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | ui::Transform t(orientation); |
| 380 | auto screencapRegion = t.transform(sampleRegion); |
| 381 | screencapRegion = screencapRegion.translate(dx, dy); |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 382 | |
| 383 | const Rect sampledBounds = sampleRegion.bounds(); |
| 384 | |
| 385 | SurfaceFlinger::RenderAreaFuture renderAreaFuture = promise::defer([=] { |
| 386 | return DisplayRenderArea::create(displayWeak, sampledBounds, sampledBounds.getSize(), |
| 387 | ui::Dataspace::V0_SRGB, orientation); |
| 388 | }); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 389 | |
| 390 | std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners; |
| 391 | |
| 392 | auto traverseLayers = [&](const LayerVector::Visitor& visitor) { |
| 393 | bool stopLayerFound = false; |
| 394 | auto filterVisitor = [&](Layer* layer) { |
| 395 | // We don't want to capture any layers beyond the stop layer |
| 396 | if (stopLayerFound) return; |
| 397 | |
| 398 | // Likewise if we just found a stop layer, set the flag and abort |
| 399 | for (const auto& [area, stopLayer, listener] : descriptors) { |
| 400 | if (layer == stopLayer.promote().get()) { |
| 401 | stopLayerFound = true; |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Compute the layer's position on the screen |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 407 | const Rect bounds = Rect(layer->getBounds()); |
| 408 | const ui::Transform transform = layer->getTransform(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 409 | constexpr bool roundOutwards = true; |
| 410 | Rect transformed = transform.transform(bounds, roundOutwards); |
| 411 | |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 412 | // 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] | 413 | Rect ignore; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 414 | if (!transformed.intersect(sampledBounds, &ignore)) return; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 415 | |
| 416 | // If the layer doesn't intersect a sampling area, skip capturing it |
| 417 | bool intersectsAnyArea = false; |
| 418 | for (const auto& [area, stopLayer, listener] : descriptors) { |
| 419 | if (transformed.intersect(area, &ignore)) { |
| 420 | intersectsAnyArea = true; |
| 421 | listeners.insert(listener); |
| 422 | } |
| 423 | } |
| 424 | if (!intersectsAnyArea) return; |
| 425 | |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 426 | ALOGV("Traversing [%s] [%d, %d, %d, %d]", layer->getDebugName(), bounds.left, |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 427 | bounds.top, bounds.right, bounds.bottom); |
| 428 | visitor(layer); |
| 429 | }; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 430 | mFlinger.traverseLayersInLayerStack(layerStack, filterVisitor); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 431 | }; |
| 432 | |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 433 | sp<GraphicBuffer> buffer = nullptr; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 434 | if (mCachedBuffer && mCachedBuffer->getWidth() == sampledBounds.getWidth() && |
| 435 | mCachedBuffer->getHeight() == sampledBounds.getHeight()) { |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 436 | buffer = mCachedBuffer; |
| 437 | } else { |
| 438 | const uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 439 | buffer = new GraphicBuffer(sampledBounds.getWidth(), sampledBounds.getHeight(), |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 440 | PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread"); |
| 441 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 442 | |
Robert Carr | 108b2c7 | 2019-04-02 16:32:58 -0700 | [diff] [blame] | 443 | bool ignored; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 444 | mFlinger.captureScreenCommon(std::move(renderAreaFuture), traverseLayers, buffer, |
| 445 | false /* identityTransform */, true /* regionSampling */, ignored); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 446 | |
| 447 | std::vector<Descriptor> activeDescriptors; |
| 448 | for (const auto& descriptor : descriptors) { |
| 449 | if (listeners.count(descriptor.listener) != 0) { |
| 450 | activeDescriptors.emplace_back(descriptor); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | ALOGV("Sampling %zu descriptors", activeDescriptors.size()); |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 455 | std::vector<float> lumas = |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame^] | 456 | sampleBuffer(buffer, sampledBounds.leftTop(), activeDescriptors, orientation); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 457 | if (lumas.size() != activeDescriptors.size()) { |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 458 | ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(), |
| 459 | activeDescriptors.size()); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | for (size_t d = 0; d < activeDescriptors.size(); ++d) { |
| 464 | activeDescriptors[d].listener->onSampleCollected(lumas[d]); |
| 465 | } |
Kevin DuBois | 4efd1f5 | 2019-04-29 10:09:43 -0700 | [diff] [blame] | 466 | |
| 467 | // Extend the lifetime of mCachedBuffer from the previous frame to here to ensure that: |
| 468 | // 1) The region sampling thread is the last owner of the buffer, and the freeing of the buffer |
| 469 | // happens in this thread, as opposed to the main thread. |
| 470 | // 2) The listener(s) receive their notifications prior to freeing the buffer. |
| 471 | mCachedBuffer = buffer; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 472 | ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded)); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 475 | // NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations. |
| 476 | void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS { |
| 477 | std::unique_lock<std::mutex> lock(mThreadControlMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 478 | while (mRunning) { |
| 479 | if (mSampleRequested) { |
| 480 | mSampleRequested = false; |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 481 | lock.unlock(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 482 | captureSample(); |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 483 | lock.lock(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 484 | } |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 485 | mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) { |
| 486 | return mSampleRequested || !mRunning; |
| 487 | }); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 492 | |
| 493 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 494 | #pragma clang diagnostic pop // ignored "-Wconversion" |