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