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