RegionSamplingThread.cpp 19 KB

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