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