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