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