blob: 839500f8a00d8d792e3d25bb83106fe7eaa1d8a4 [file] [log] [blame]
Dan Stozaec460082018-12-17 15:35:09 -08001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
Dan Stozaec460082018-12-17 15:35:09 -080022//#define LOG_NDEBUG 0
23#define ATRACE_TAG ATRACE_TAG_GRAPHICS
24#undef LOG_TAG
25#define LOG_TAG "RegionSamplingThread"
26
27#include "RegionSamplingThread.h"
28
Kevin DuBoisb325c932019-05-21 08:34:09 -070029#include <compositionengine/Display.h>
30#include <compositionengine/impl/OutputCompositionState.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070031#include <cutils/properties.h>
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -080032#include <ftl/future.h>
Huihong Luoa339d0a2022-02-05 09:42:42 -080033#include <gui/SpHash.h>
chaviwe7b9f272020-08-18 16:08:59 -070034#include <gui/SyncScreenCaptureListener.h>
Vishnu Nairdbbe3852022-01-12 20:22:11 -080035#include <renderengine/impl/ExternalTexture.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070036#include <ui/DisplayStatInfo.h>
37#include <utils/Trace.h>
38
39#include <string>
40
Dan Stozaec460082018-12-17 15:35:09 -080041#include "DisplayDevice.h"
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020042#include "DisplayRenderArea.h"
Vishnu Nair9d77a5c2022-10-28 06:31:21 +000043#include "FrontEnd/LayerCreationArgs.h"
Dan Stozaec460082018-12-17 15:35:09 -080044#include "Layer.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070045#include "Scheduler/VsyncController.h"
Dan Stozaec460082018-12-17 15:35:09 -080046#include "SurfaceFlinger.h"
47
48namespace android {
Kevin DuBois413287f2019-02-25 08:46:47 -080049using namespace std::chrono_literals;
Dan Stozaec460082018-12-17 15:35:09 -080050
Huihong Luoa339d0a2022-02-05 09:42:42 -080051using gui::SpHash;
Dan Stozaec460082018-12-17 15:35:09 -080052
Kevin DuBois413287f2019-02-25 08:46:47 -080053constexpr auto lumaSamplingStepTag = "LumaSamplingStep";
54enum class samplingStep {
55 noWorkNeeded,
56 idleTimerWaiting,
John Dias84be7832019-06-18 17:05:26 -070057 waitForQuietFrame,
Kevin DuBois413287f2019-02-25 08:46:47 -080058 waitForSamplePhase,
59 sample
60};
61
Ady Abraham9c53ee72020-07-22 21:16:18 -070062constexpr auto defaultRegionSamplingWorkDuration = 3ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080063constexpr auto defaultRegionSamplingPeriod = 100ms;
64constexpr auto defaultRegionSamplingTimerTimeout = 100ms;
Ady Abraham562c2712021-05-07 15:10:42 -070065constexpr auto maxRegionSamplingDelay = 100ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080066// TODO: (b/127403193) duration to string conversion could probably be constexpr
67template <typename Rep, typename Per>
68inline std::string toNsString(std::chrono::duration<Rep, Per> t) {
69 return std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(t).count());
Dan Stozaec460082018-12-17 15:35:09 -080070}
71
Kevin DuBois413287f2019-02-25 08:46:47 -080072RegionSamplingThread::EnvironmentTimingTunables::EnvironmentTimingTunables() {
73 char value[PROPERTY_VALUE_MAX] = {};
74
Ady Abraham9c53ee72020-07-22 21:16:18 -070075 property_get("debug.sf.region_sampling_duration_ns", value,
76 toNsString(defaultRegionSamplingWorkDuration).c_str());
77 int const samplingDurationNsRaw = atoi(value);
Kevin DuBois413287f2019-02-25 08:46:47 -080078
79 property_get("debug.sf.region_sampling_period_ns", value,
80 toNsString(defaultRegionSamplingPeriod).c_str());
81 int const samplingPeriodNsRaw = atoi(value);
82
83 property_get("debug.sf.region_sampling_timer_timeout_ns", value,
84 toNsString(defaultRegionSamplingTimerTimeout).c_str());
85 int const samplingTimerTimeoutNsRaw = atoi(value);
86
87 if ((samplingPeriodNsRaw < 0) || (samplingTimerTimeoutNsRaw < 0)) {
88 ALOGW("User-specified sampling tuning options nonsensical. Using defaults");
Ady Abraham9c53ee72020-07-22 21:16:18 -070089 mSamplingDuration = defaultRegionSamplingWorkDuration;
Kevin DuBois413287f2019-02-25 08:46:47 -080090 mSamplingPeriod = defaultRegionSamplingPeriod;
91 mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout;
92 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070093 mSamplingDuration = std::chrono::nanoseconds(samplingDurationNsRaw);
Kevin DuBois413287f2019-02-25 08:46:47 -080094 mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw);
95 mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw);
96 }
97}
98
Ady Abraham562c2712021-05-07 15:10:42 -070099RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables)
Kevin DuBois413287f2019-02-25 08:46:47 -0800100 : mFlinger(flinger),
Kevin DuBois413287f2019-02-25 08:46:47 -0800101 mTunables(tunables),
Ady Abraham9c53ee72020-07-22 21:16:18 -0700102 mIdleTimer(
Ady Abrahamdde984b2021-03-18 12:47:36 -0700103 "RegSampIdle",
Ady Abraham9c53ee72020-07-22 21:16:18 -0700104 std::chrono::duration_cast<std::chrono::milliseconds>(
105 mTunables.mSamplingTimerTimeout),
106 [] {}, [this] { checkForStaleLuma(); }),
Ady Abraham562c2712021-05-07 15:10:42 -0700107 mLastSampleTime(0ns) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700108 mThread = std::thread([this]() { threadMain(); });
Ady Abrahamdde984b2021-03-18 12:47:36 -0700109 pthread_setname_np(mThread.native_handle(), "RegionSampling");
Kevin DuBois413287f2019-02-25 08:46:47 -0800110 mIdleTimer.start();
111}
112
Ady Abraham562c2712021-05-07 15:10:42 -0700113RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger)
114 : RegionSamplingThread(flinger,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700115 TimingTunables{defaultRegionSamplingWorkDuration,
Kevin DuBois413287f2019-02-25 08:46:47 -0800116 defaultRegionSamplingPeriod,
117 defaultRegionSamplingTimerTimeout}) {}
118
Dan Stozaec460082018-12-17 15:35:09 -0800119RegionSamplingThread::~RegionSamplingThread() {
Kevin DuBois413287f2019-02-25 08:46:47 -0800120 mIdleTimer.stop();
121
Dan Stozaec460082018-12-17 15:35:09 -0800122 {
Kevin DuBois26afc782019-05-06 16:46:45 -0700123 std::lock_guard lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800124 mRunning = false;
125 mCondition.notify_one();
126 }
127
Dan Stozaec460082018-12-17 15:35:09 -0800128 if (mThread.joinable()) {
129 mThread.join();
130 }
131}
132
Vishnu Nair9d77a5c2022-10-28 06:31:21 +0000133void RegionSamplingThread::addListener(const Rect& samplingArea, uint32_t stopLayerId,
Dan Stozaec460082018-12-17 15:35:09 -0800134 const sp<IRegionSamplingListener>& listener) {
Dan Stozaec460082018-12-17 15:35:09 -0800135 sp<IBinder> asBinder = IInterface::asBinder(listener);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700136 asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
Kevin DuBois26afc782019-05-06 16:46:45 -0700137 std::lock_guard lock(mSamplingMutex);
Vishnu Nair9d77a5c2022-10-28 06:31:21 +0000138 mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayerId, listener});
Dan Stozaec460082018-12-17 15:35:09 -0800139}
140
141void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700142 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800143 mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener)));
144}
145
Kevin DuBois413287f2019-02-25 08:46:47 -0800146void RegionSamplingThread::checkForStaleLuma() {
Kevin DuBois26afc782019-05-06 16:46:45 -0700147 std::lock_guard lock(mThreadControlMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800148
Ady Abraham562c2712021-05-07 15:10:42 -0700149 if (mSampleRequestTime.has_value()) {
150 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase));
151 mSampleRequestTime.reset();
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700152 mFlinger.scheduleSample();
Kevin DuBois413287f2019-02-25 08:46:47 -0800153 }
154}
155
Ady Abraham562c2712021-05-07 15:10:42 -0700156void RegionSamplingThread::onCompositionComplete(
157 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
158 doSample(samplingDeadline);
Kevin DuBois413287f2019-02-25 08:46:47 -0800159}
160
Ady Abraham562c2712021-05-07 15:10:42 -0700161void RegionSamplingThread::doSample(
162 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700163 std::lock_guard lock(mThreadControlMutex);
Ady Abraham562c2712021-05-07 15:10:42 -0700164 const auto now = std::chrono::steady_clock::now();
165 if (mLastSampleTime + mTunables.mSamplingPeriod > now) {
166 // content changed, but we sampled not too long ago, so we need to sample some time in the
167 // future.
Kevin DuBois413287f2019-02-25 08:46:47 -0800168 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting));
Ady Abraham562c2712021-05-07 15:10:42 -0700169 mSampleRequestTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800170 return;
171 }
Ady Abraham562c2712021-05-07 15:10:42 -0700172 if (!mSampleRequestTime.has_value() || now - *mSampleRequestTime < maxRegionSamplingDelay) {
John Dias84be7832019-06-18 17:05:26 -0700173 // If there is relatively little time left for surfaceflinger
174 // until the next vsync deadline, defer this sampling work
175 // to a later frame, when hopefully there will be more time.
Ady Abraham562c2712021-05-07 15:10:42 -0700176 if (samplingDeadline.has_value() && now + mTunables.mSamplingDuration > *samplingDeadline) {
John Dias84be7832019-06-18 17:05:26 -0700177 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame));
Ady Abraham562c2712021-05-07 15:10:42 -0700178 mSampleRequestTime = mSampleRequestTime.value_or(now);
John Dias84be7832019-06-18 17:05:26 -0700179 return;
180 }
181 }
Kevin DuBois413287f2019-02-25 08:46:47 -0800182
183 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample));
184
Ady Abraham562c2712021-05-07 15:10:42 -0700185 mSampleRequestTime.reset();
186 mLastSampleTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800187
188 mIdleTimer.reset();
Kevin DuBois413287f2019-02-25 08:46:47 -0800189
Dan Stozaec460082018-12-17 15:35:09 -0800190 mSampleRequested = true;
191 mCondition.notify_one();
192}
193
194void RegionSamplingThread::binderDied(const wp<IBinder>& who) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700195 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800196 mDescriptors.erase(who);
197}
198
Kevin DuBoisb325c932019-05-21 08:34:09 -0700199float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
200 uint32_t orientation, const Rect& sample_area) {
201 if (!sample_area.isValid() || (sample_area.getWidth() > width) ||
202 (sample_area.getHeight() > height)) {
203 ALOGE("invalid sampling region requested");
204 return 0.0f;
205 }
206
Alec Mouri439b6092022-09-14 22:24:17 +0000207 const uint32_t pixelCount =
208 (sample_area.bottom - sample_area.top) * (sample_area.right - sample_area.left);
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700209 uint32_t accumulatedLuma = 0;
Dan Stozaec460082018-12-17 15:35:09 -0800210
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700211 // Calculates luma with approximation of Rec. 709 primaries
Alec Mouri439b6092022-09-14 22:24:17 +0000212 for (int32_t row = sample_area.top; row < sample_area.bottom; ++row) {
Dan Stozaec460082018-12-17 15:35:09 -0800213 const uint32_t* rowBase = data + row * stride;
Alec Mouri439b6092022-09-14 22:24:17 +0000214 for (int32_t column = sample_area.left; column < sample_area.right; ++column) {
Dan Stozaec460082018-12-17 15:35:09 -0800215 uint32_t pixel = rowBase[column];
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700216 const uint32_t r = pixel & 0xFF;
217 const uint32_t g = (pixel >> 8) & 0xFF;
218 const uint32_t b = (pixel >> 16) & 0xFF;
219 const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5;
220 accumulatedLuma += luma;
Dan Stozaec460082018-12-17 15:35:09 -0800221 }
222 }
223
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700224 return accumulatedLuma / (255.0f * pixelCount);
Dan Stozaec460082018-12-17 15:35:09 -0800225}
Dan Stozaec460082018-12-17 15:35:09 -0800226
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800227std::vector<float> RegionSamplingThread::sampleBuffer(
228 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700229 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) {
Dan Stozaec460082018-12-17 15:35:09 -0800230 void* data_raw = nullptr;
231 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw);
232 std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw),
233 [&buffer](auto) { buffer->unlock(); });
234 if (!data) return {};
235
Kevin DuBoisb325c932019-05-21 08:34:09 -0700236 const int32_t width = buffer->getWidth();
237 const int32_t height = buffer->getHeight();
Dan Stozaec460082018-12-17 15:35:09 -0800238 const int32_t stride = buffer->getStride();
239 std::vector<float> lumas(descriptors.size());
240 std::transform(descriptors.begin(), descriptors.end(), lumas.begin(),
241 [&](auto const& descriptor) {
Kevin DuBoisb325c932019-05-21 08:34:09 -0700242 return sampleArea(data.get(), width, height, stride, orientation,
243 descriptor.area - leftTop);
Dan Stozaec460082018-12-17 15:35:09 -0800244 });
245 return lumas;
246}
247
248void RegionSamplingThread::captureSample() {
249 ATRACE_CALL();
Kevin DuBois26afc782019-05-06 16:46:45 -0700250 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800251
252 if (mDescriptors.empty()) {
253 return;
254 }
255
Marin Shalamanov1c434292020-06-12 01:47:29 +0200256 wp<const DisplayDevice> displayWeak;
257
258 ui::LayerStack layerStack;
259 ui::Transform::RotationFlags orientation;
260 ui::Size displaySize;
261
262 {
263 // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread
264 const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice();
265 displayWeak = display;
266 layerStack = display->getLayerStack();
267 orientation = ui::Transform::toRotationFlags(display->getOrientation());
268 displaySize = display->getSize();
269 }
Kevin DuBoisb325c932019-05-21 08:34:09 -0700270
Dan Stozaec460082018-12-17 15:35:09 -0800271 std::vector<RegionSamplingThread::Descriptor> descriptors;
272 Region sampleRegion;
273 for (const auto& [listener, descriptor] : mDescriptors) {
274 sampleRegion.orSelf(descriptor.area);
275 descriptors.emplace_back(descriptor);
276 }
277
Marin Shalamanov1c434292020-06-12 01:47:29 +0200278 const Rect sampledBounds = sampleRegion.bounds();
Huihong Luoa8f66852021-07-02 00:22:38 -0700279 constexpr bool kUseIdentityTransform = false;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200280
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800281 SurfaceFlinger::RenderAreaFuture renderAreaFuture = ftl::defer([=] {
Huihong Luoa8f66852021-07-02 00:22:38 -0700282 return DisplayRenderArea::create(displayWeak, sampledBounds, sampledBounds.getSize(),
283 ui::Dataspace::V0_SRGB, kUseIdentityTransform);
Marin Shalamanov1c434292020-06-12 01:47:29 +0200284 });
Dan Stozaec460082018-12-17 15:35:09 -0800285
286 std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners;
287
288 auto traverseLayers = [&](const LayerVector::Visitor& visitor) {
289 bool stopLayerFound = false;
290 auto filterVisitor = [&](Layer* layer) {
291 // We don't want to capture any layers beyond the stop layer
292 if (stopLayerFound) return;
293
294 // Likewise if we just found a stop layer, set the flag and abort
Vishnu Nair9d77a5c2022-10-28 06:31:21 +0000295 for (const auto& [area, stopLayerId, listener] : descriptors) {
296 if (stopLayerId != UNASSIGNED_LAYER_ID && layer->getSequence() == stopLayerId) {
Dan Stozaec460082018-12-17 15:35:09 -0800297 stopLayerFound = true;
298 return;
299 }
300 }
301
302 // Compute the layer's position on the screen
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800303 const Rect bounds = Rect(layer->getBounds());
304 const ui::Transform transform = layer->getTransform();
Dan Stozaec460082018-12-17 15:35:09 -0800305 constexpr bool roundOutwards = true;
306 Rect transformed = transform.transform(bounds, roundOutwards);
307
Marin Shalamanov1c434292020-06-12 01:47:29 +0200308 // If this layer doesn't intersect with the larger sampledBounds, skip capturing it
Dan Stozaec460082018-12-17 15:35:09 -0800309 Rect ignore;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200310 if (!transformed.intersect(sampledBounds, &ignore)) return;
Dan Stozaec460082018-12-17 15:35:09 -0800311
312 // If the layer doesn't intersect a sampling area, skip capturing it
313 bool intersectsAnyArea = false;
314 for (const auto& [area, stopLayer, listener] : descriptors) {
315 if (transformed.intersect(area, &ignore)) {
316 intersectsAnyArea = true;
317 listeners.insert(listener);
318 }
319 }
320 if (!intersectsAnyArea) return;
321
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700322 ALOGV("Traversing [%s] [%d, %d, %d, %d]", layer->getDebugName(), bounds.left,
Dan Stozaec460082018-12-17 15:35:09 -0800323 bounds.top, bounds.right, bounds.bottom);
324 visitor(layer);
325 };
chaviw4b9d5e12020-08-04 18:30:35 -0700326 mFlinger.traverseLayersInLayerStack(layerStack, CaptureArgs::UNSET_UID, filterVisitor);
Dan Stozaec460082018-12-17 15:35:09 -0800327 };
328
Alec Mouria90a5702021-04-16 16:36:21 +0000329 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
330 if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() &&
331 mCachedBuffer->getBuffer()->getHeight() == sampledBounds.getHeight()) {
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700332 buffer = mCachedBuffer;
333 } else {
John Reck67b1e2b2020-08-26 13:17:24 -0700334 const uint32_t usage =
335 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000336 sp<GraphicBuffer> graphicBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700337 sp<GraphicBuffer>::make(sampledBounds.getWidth(), sampledBounds.getHeight(),
338 PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread");
Alec Mouria90a5702021-04-16 16:36:21 +0000339 const status_t bufferStatus = graphicBuffer->initCheck();
Alec Mouri7013b6f2021-02-12 11:16:54 -0800340 LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "captureSample: Buffer failed to allocate: %d",
341 bufferStatus);
Alec Mouria90a5702021-04-16 16:36:21 +0000342 buffer = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800343 renderengine::impl::ExternalTexture>(graphicBuffer, mFlinger.getRenderEngine(),
344 renderengine::impl::ExternalTexture::Usage::
345 WRITEABLE);
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700346 }
Vishnu Nair7aa0eb72023-01-24 03:59:27 +0000347 auto getLayerSnapshots = RenderArea::fromTraverseLayersLambda(traverseLayers);
Dan Stozaec460082018-12-17 15:35:09 -0800348
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700349 constexpr bool kRegionSampling = true;
350 constexpr bool kGrayscale = false;
351
352 if (const auto fenceResult =
Vishnu Nair7aa0eb72023-01-24 03:59:27 +0000353 mFlinger.captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, buffer,
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700354 kRegionSampling, kGrayscale, nullptr)
355 .get();
356 fenceResult.ok()) {
357 fenceResult.value()->waitForever(LOG_TAG);
Sally Qi59a9f502021-10-12 18:53:23 +0000358 }
Dan Stozaec460082018-12-17 15:35:09 -0800359
360 std::vector<Descriptor> activeDescriptors;
361 for (const auto& descriptor : descriptors) {
362 if (listeners.count(descriptor.listener) != 0) {
363 activeDescriptors.emplace_back(descriptor);
364 }
365 }
366
367 ALOGV("Sampling %zu descriptors", activeDescriptors.size());
Alec Mouria90a5702021-04-16 16:36:21 +0000368 std::vector<float> lumas = sampleBuffer(buffer->getBuffer(), sampledBounds.leftTop(),
369 activeDescriptors, orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800370 if (lumas.size() != activeDescriptors.size()) {
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800371 ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(),
372 activeDescriptors.size());
Dan Stozaec460082018-12-17 15:35:09 -0800373 return;
374 }
375
376 for (size_t d = 0; d < activeDescriptors.size(); ++d) {
377 activeDescriptors[d].listener->onSampleCollected(lumas[d]);
378 }
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700379
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700380 mCachedBuffer = buffer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800381 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded));
Dan Stozaec460082018-12-17 15:35:09 -0800382}
383
Kevin DuBois26afc782019-05-06 16:46:45 -0700384// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
385void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
386 std::unique_lock<std::mutex> lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800387 while (mRunning) {
388 if (mSampleRequested) {
389 mSampleRequested = false;
Kevin DuBois26afc782019-05-06 16:46:45 -0700390 lock.unlock();
Dan Stozaec460082018-12-17 15:35:09 -0800391 captureSample();
Kevin DuBois26afc782019-05-06 16:46:45 -0700392 lock.lock();
Dan Stozaec460082018-12-17 15:35:09 -0800393 }
Kevin DuBois26afc782019-05-06 16:46:45 -0700394 mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) {
395 return mSampleRequested || !mRunning;
396 });
Dan Stozaec460082018-12-17 15:35:09 -0800397 }
398}
399
400} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800401
402// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100403#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"