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