blob: 2487dbd7939597e8480fbf63dc120e2d068fd19e [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"
Dan Stozaec460082018-12-17 15:35:09 -080043#include "Layer.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070044#include "Scheduler/VsyncController.h"
Dan Stozaec460082018-12-17 15:35:09 -080045#include "SurfaceFlinger.h"
46
47namespace android {
Kevin DuBois413287f2019-02-25 08:46:47 -080048using namespace std::chrono_literals;
Dan Stozaec460082018-12-17 15:35:09 -080049
Huihong Luoa339d0a2022-02-05 09:42:42 -080050using gui::SpHash;
Dan Stozaec460082018-12-17 15:35:09 -080051
Kevin DuBois413287f2019-02-25 08:46:47 -080052constexpr auto lumaSamplingStepTag = "LumaSamplingStep";
53enum class samplingStep {
54 noWorkNeeded,
55 idleTimerWaiting,
John Dias84be7832019-06-18 17:05:26 -070056 waitForQuietFrame,
Kevin DuBois413287f2019-02-25 08:46:47 -080057 waitForSamplePhase,
58 sample
59};
60
Ady Abraham9c53ee72020-07-22 21:16:18 -070061constexpr auto defaultRegionSamplingWorkDuration = 3ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080062constexpr auto defaultRegionSamplingPeriod = 100ms;
63constexpr auto defaultRegionSamplingTimerTimeout = 100ms;
Ady Abraham562c2712021-05-07 15:10:42 -070064constexpr auto maxRegionSamplingDelay = 100ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080065// TODO: (b/127403193) duration to string conversion could probably be constexpr
66template <typename Rep, typename Per>
67inline std::string toNsString(std::chrono::duration<Rep, Per> t) {
68 return std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(t).count());
Dan Stozaec460082018-12-17 15:35:09 -080069}
70
Kevin DuBois413287f2019-02-25 08:46:47 -080071RegionSamplingThread::EnvironmentTimingTunables::EnvironmentTimingTunables() {
72 char value[PROPERTY_VALUE_MAX] = {};
73
Ady Abraham9c53ee72020-07-22 21:16:18 -070074 property_get("debug.sf.region_sampling_duration_ns", value,
75 toNsString(defaultRegionSamplingWorkDuration).c_str());
76 int const samplingDurationNsRaw = atoi(value);
Kevin DuBois413287f2019-02-25 08:46:47 -080077
78 property_get("debug.sf.region_sampling_period_ns", value,
79 toNsString(defaultRegionSamplingPeriod).c_str());
80 int const samplingPeriodNsRaw = atoi(value);
81
82 property_get("debug.sf.region_sampling_timer_timeout_ns", value,
83 toNsString(defaultRegionSamplingTimerTimeout).c_str());
84 int const samplingTimerTimeoutNsRaw = atoi(value);
85
86 if ((samplingPeriodNsRaw < 0) || (samplingTimerTimeoutNsRaw < 0)) {
87 ALOGW("User-specified sampling tuning options nonsensical. Using defaults");
Ady Abraham9c53ee72020-07-22 21:16:18 -070088 mSamplingDuration = defaultRegionSamplingWorkDuration;
Kevin DuBois413287f2019-02-25 08:46:47 -080089 mSamplingPeriod = defaultRegionSamplingPeriod;
90 mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout;
91 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070092 mSamplingDuration = std::chrono::nanoseconds(samplingDurationNsRaw);
Kevin DuBois413287f2019-02-25 08:46:47 -080093 mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw);
94 mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw);
95 }
96}
97
Ady Abraham562c2712021-05-07 15:10:42 -070098RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables)
Kevin DuBois413287f2019-02-25 08:46:47 -080099 : mFlinger(flinger),
Kevin DuBois413287f2019-02-25 08:46:47 -0800100 mTunables(tunables),
Ady Abraham9c53ee72020-07-22 21:16:18 -0700101 mIdleTimer(
Ady Abrahamdde984b2021-03-18 12:47:36 -0700102 "RegSampIdle",
Ady Abraham9c53ee72020-07-22 21:16:18 -0700103 std::chrono::duration_cast<std::chrono::milliseconds>(
104 mTunables.mSamplingTimerTimeout),
105 [] {}, [this] { checkForStaleLuma(); }),
Ady Abraham562c2712021-05-07 15:10:42 -0700106 mLastSampleTime(0ns) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700107 mThread = std::thread([this]() { threadMain(); });
Ady Abrahamdde984b2021-03-18 12:47:36 -0700108 pthread_setname_np(mThread.native_handle(), "RegionSampling");
Kevin DuBois413287f2019-02-25 08:46:47 -0800109 mIdleTimer.start();
110}
111
Ady Abraham562c2712021-05-07 15:10:42 -0700112RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger)
113 : RegionSamplingThread(flinger,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700114 TimingTunables{defaultRegionSamplingWorkDuration,
Kevin DuBois413287f2019-02-25 08:46:47 -0800115 defaultRegionSamplingPeriod,
116 defaultRegionSamplingTimerTimeout}) {}
117
Dan Stozaec460082018-12-17 15:35:09 -0800118RegionSamplingThread::~RegionSamplingThread() {
Kevin DuBois413287f2019-02-25 08:46:47 -0800119 mIdleTimer.stop();
120
Dan Stozaec460082018-12-17 15:35:09 -0800121 {
Kevin DuBois26afc782019-05-06 16:46:45 -0700122 std::lock_guard lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800123 mRunning = false;
124 mCondition.notify_one();
125 }
126
Dan Stozaec460082018-12-17 15:35:09 -0800127 if (mThread.joinable()) {
128 mThread.join();
129 }
130}
131
Alec Mouri9a02eda2020-04-21 17:39:34 -0700132void RegionSamplingThread::addListener(const Rect& samplingArea, const wp<Layer>& stopLayer,
Dan Stozaec460082018-12-17 15:35:09 -0800133 const sp<IRegionSamplingListener>& listener) {
Dan Stozaec460082018-12-17 15:35:09 -0800134 sp<IBinder> asBinder = IInterface::asBinder(listener);
135 asBinder->linkToDeath(this);
Kevin DuBois26afc782019-05-06 16:46:45 -0700136 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800137 mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayer, listener});
138}
139
140void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700141 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800142 mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener)));
143}
144
Kevin DuBois413287f2019-02-25 08:46:47 -0800145void RegionSamplingThread::checkForStaleLuma() {
Kevin DuBois26afc782019-05-06 16:46:45 -0700146 std::lock_guard lock(mThreadControlMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800147
Ady Abraham562c2712021-05-07 15:10:42 -0700148 if (mSampleRequestTime.has_value()) {
149 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase));
150 mSampleRequestTime.reset();
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700151 mFlinger.scheduleSample();
Kevin DuBois413287f2019-02-25 08:46:47 -0800152 }
153}
154
Ady Abraham562c2712021-05-07 15:10:42 -0700155void RegionSamplingThread::onCompositionComplete(
156 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
157 doSample(samplingDeadline);
Kevin DuBois413287f2019-02-25 08:46:47 -0800158}
159
Ady Abraham562c2712021-05-07 15:10:42 -0700160void RegionSamplingThread::doSample(
161 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700162 std::lock_guard lock(mThreadControlMutex);
Ady Abraham562c2712021-05-07 15:10:42 -0700163 const auto now = std::chrono::steady_clock::now();
164 if (mLastSampleTime + mTunables.mSamplingPeriod > now) {
165 // content changed, but we sampled not too long ago, so we need to sample some time in the
166 // future.
Kevin DuBois413287f2019-02-25 08:46:47 -0800167 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting));
Ady Abraham562c2712021-05-07 15:10:42 -0700168 mSampleRequestTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800169 return;
170 }
Ady Abraham562c2712021-05-07 15:10:42 -0700171 if (!mSampleRequestTime.has_value() || now - *mSampleRequestTime < maxRegionSamplingDelay) {
John Dias84be7832019-06-18 17:05:26 -0700172 // If there is relatively little time left for surfaceflinger
173 // until the next vsync deadline, defer this sampling work
174 // to a later frame, when hopefully there will be more time.
Ady Abraham562c2712021-05-07 15:10:42 -0700175 if (samplingDeadline.has_value() && now + mTunables.mSamplingDuration > *samplingDeadline) {
John Dias84be7832019-06-18 17:05:26 -0700176 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame));
Ady Abraham562c2712021-05-07 15:10:42 -0700177 mSampleRequestTime = mSampleRequestTime.value_or(now);
John Dias84be7832019-06-18 17:05:26 -0700178 return;
179 }
180 }
Kevin DuBois413287f2019-02-25 08:46:47 -0800181
182 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample));
183
Ady Abraham562c2712021-05-07 15:10:42 -0700184 mSampleRequestTime.reset();
185 mLastSampleTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800186
187 mIdleTimer.reset();
Kevin DuBois413287f2019-02-25 08:46:47 -0800188
Dan Stozaec460082018-12-17 15:35:09 -0800189 mSampleRequested = true;
190 mCondition.notify_one();
191}
192
193void RegionSamplingThread::binderDied(const wp<IBinder>& who) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700194 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800195 mDescriptors.erase(who);
196}
197
Kevin DuBoisb325c932019-05-21 08:34:09 -0700198float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
199 uint32_t orientation, const Rect& sample_area) {
200 if (!sample_area.isValid() || (sample_area.getWidth() > width) ||
201 (sample_area.getHeight() > height)) {
202 ALOGE("invalid sampling region requested");
203 return 0.0f;
204 }
205
206 // (b/133849373) ROT_90 screencap images produced upside down
207 auto area = sample_area;
208 if (orientation & ui::Transform::ROT_90) {
209 area.top = height - area.top;
210 area.bottom = height - area.bottom;
211 std::swap(area.top, area.bottom);
Kevin DuBois69162d02019-06-04 20:22:43 -0700212
213 area.left = width - area.left;
214 area.right = width - area.right;
215 std::swap(area.left, area.right);
Kevin DuBoisb325c932019-05-21 08:34:09 -0700216 }
217
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700218 const uint32_t pixelCount = (area.bottom - area.top) * (area.right - area.left);
219 uint32_t accumulatedLuma = 0;
Dan Stozaec460082018-12-17 15:35:09 -0800220
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700221 // Calculates luma with approximation of Rec. 709 primaries
Dan Stozaec460082018-12-17 15:35:09 -0800222 for (int32_t row = area.top; row < area.bottom; ++row) {
223 const uint32_t* rowBase = data + row * stride;
224 for (int32_t column = area.left; column < area.right; ++column) {
225 uint32_t pixel = rowBase[column];
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700226 const uint32_t r = pixel & 0xFF;
227 const uint32_t g = (pixel >> 8) & 0xFF;
228 const uint32_t b = (pixel >> 16) & 0xFF;
229 const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5;
230 accumulatedLuma += luma;
Dan Stozaec460082018-12-17 15:35:09 -0800231 }
232 }
233
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700234 return accumulatedLuma / (255.0f * pixelCount);
Dan Stozaec460082018-12-17 15:35:09 -0800235}
Dan Stozaec460082018-12-17 15:35:09 -0800236
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800237std::vector<float> RegionSamplingThread::sampleBuffer(
238 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700239 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) {
Dan Stozaec460082018-12-17 15:35:09 -0800240 void* data_raw = nullptr;
241 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw);
242 std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw),
243 [&buffer](auto) { buffer->unlock(); });
244 if (!data) return {};
245
Kevin DuBoisb325c932019-05-21 08:34:09 -0700246 const int32_t width = buffer->getWidth();
247 const int32_t height = buffer->getHeight();
Dan Stozaec460082018-12-17 15:35:09 -0800248 const int32_t stride = buffer->getStride();
249 std::vector<float> lumas(descriptors.size());
250 std::transform(descriptors.begin(), descriptors.end(), lumas.begin(),
251 [&](auto const& descriptor) {
Kevin DuBoisb325c932019-05-21 08:34:09 -0700252 return sampleArea(data.get(), width, height, stride, orientation,
253 descriptor.area - leftTop);
Dan Stozaec460082018-12-17 15:35:09 -0800254 });
255 return lumas;
256}
257
258void RegionSamplingThread::captureSample() {
259 ATRACE_CALL();
Kevin DuBois26afc782019-05-06 16:46:45 -0700260 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800261
262 if (mDescriptors.empty()) {
263 return;
264 }
265
Marin Shalamanov1c434292020-06-12 01:47:29 +0200266 wp<const DisplayDevice> displayWeak;
267
268 ui::LayerStack layerStack;
269 ui::Transform::RotationFlags orientation;
270 ui::Size displaySize;
271
272 {
273 // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread
274 const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice();
275 displayWeak = display;
276 layerStack = display->getLayerStack();
277 orientation = ui::Transform::toRotationFlags(display->getOrientation());
278 displaySize = display->getSize();
279 }
Kevin DuBoisb325c932019-05-21 08:34:09 -0700280
Dan Stozaec460082018-12-17 15:35:09 -0800281 std::vector<RegionSamplingThread::Descriptor> descriptors;
282 Region sampleRegion;
283 for (const auto& [listener, descriptor] : mDescriptors) {
284 sampleRegion.orSelf(descriptor.area);
285 descriptors.emplace_back(descriptor);
286 }
287
Marin Shalamanov1c434292020-06-12 01:47:29 +0200288 const Rect sampledBounds = sampleRegion.bounds();
Huihong Luoa8f66852021-07-02 00:22:38 -0700289 constexpr bool kUseIdentityTransform = false;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200290
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800291 SurfaceFlinger::RenderAreaFuture renderAreaFuture = ftl::defer([=] {
Huihong Luoa8f66852021-07-02 00:22:38 -0700292 return DisplayRenderArea::create(displayWeak, sampledBounds, sampledBounds.getSize(),
293 ui::Dataspace::V0_SRGB, kUseIdentityTransform);
Marin Shalamanov1c434292020-06-12 01:47:29 +0200294 });
Dan Stozaec460082018-12-17 15:35:09 -0800295
296 std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners;
297
298 auto traverseLayers = [&](const LayerVector::Visitor& visitor) {
299 bool stopLayerFound = false;
300 auto filterVisitor = [&](Layer* layer) {
301 // We don't want to capture any layers beyond the stop layer
302 if (stopLayerFound) return;
303
304 // Likewise if we just found a stop layer, set the flag and abort
305 for (const auto& [area, stopLayer, listener] : descriptors) {
306 if (layer == stopLayer.promote().get()) {
307 stopLayerFound = true;
308 return;
309 }
310 }
311
312 // Compute the layer's position on the screen
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800313 const Rect bounds = Rect(layer->getBounds());
314 const ui::Transform transform = layer->getTransform();
Dan Stozaec460082018-12-17 15:35:09 -0800315 constexpr bool roundOutwards = true;
316 Rect transformed = transform.transform(bounds, roundOutwards);
317
Marin Shalamanov1c434292020-06-12 01:47:29 +0200318 // If this layer doesn't intersect with the larger sampledBounds, skip capturing it
Dan Stozaec460082018-12-17 15:35:09 -0800319 Rect ignore;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200320 if (!transformed.intersect(sampledBounds, &ignore)) return;
Dan Stozaec460082018-12-17 15:35:09 -0800321
322 // If the layer doesn't intersect a sampling area, skip capturing it
323 bool intersectsAnyArea = false;
324 for (const auto& [area, stopLayer, listener] : descriptors) {
325 if (transformed.intersect(area, &ignore)) {
326 intersectsAnyArea = true;
327 listeners.insert(listener);
328 }
329 }
330 if (!intersectsAnyArea) return;
331
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700332 ALOGV("Traversing [%s] [%d, %d, %d, %d]", layer->getDebugName(), bounds.left,
Dan Stozaec460082018-12-17 15:35:09 -0800333 bounds.top, bounds.right, bounds.bottom);
334 visitor(layer);
335 };
chaviw4b9d5e12020-08-04 18:30:35 -0700336 mFlinger.traverseLayersInLayerStack(layerStack, CaptureArgs::UNSET_UID, filterVisitor);
Dan Stozaec460082018-12-17 15:35:09 -0800337 };
338
Alec Mouria90a5702021-04-16 16:36:21 +0000339 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
340 if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() &&
341 mCachedBuffer->getBuffer()->getHeight() == sampledBounds.getHeight()) {
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700342 buffer = mCachedBuffer;
343 } else {
John Reck67b1e2b2020-08-26 13:17:24 -0700344 const uint32_t usage =
345 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000346 sp<GraphicBuffer> graphicBuffer =
347 new GraphicBuffer(sampledBounds.getWidth(), sampledBounds.getHeight(),
348 PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread");
349 const status_t bufferStatus = graphicBuffer->initCheck();
Alec Mouri7013b6f2021-02-12 11:16:54 -0800350 LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "captureSample: Buffer failed to allocate: %d",
351 bufferStatus);
Alec Mouria90a5702021-04-16 16:36:21 +0000352 buffer = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800353 renderengine::impl::ExternalTexture>(graphicBuffer, mFlinger.getRenderEngine(),
354 renderengine::impl::ExternalTexture::Usage::
355 WRITEABLE);
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700356 }
Dan Stozaec460082018-12-17 15:35:09 -0800357
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700358 constexpr bool kRegionSampling = true;
359 constexpr bool kGrayscale = false;
360
361 if (const auto fenceResult =
362 mFlinger.captureScreenCommon(std::move(renderAreaFuture), traverseLayers, buffer,
363 kRegionSampling, kGrayscale, nullptr)
364 .get();
365 fenceResult.ok()) {
366 fenceResult.value()->waitForever(LOG_TAG);
Sally Qi59a9f502021-10-12 18:53:23 +0000367 }
Dan Stozaec460082018-12-17 15:35:09 -0800368
369 std::vector<Descriptor> activeDescriptors;
370 for (const auto& descriptor : descriptors) {
371 if (listeners.count(descriptor.listener) != 0) {
372 activeDescriptors.emplace_back(descriptor);
373 }
374 }
375
376 ALOGV("Sampling %zu descriptors", activeDescriptors.size());
Alec Mouria90a5702021-04-16 16:36:21 +0000377 std::vector<float> lumas = sampleBuffer(buffer->getBuffer(), sampledBounds.leftTop(),
378 activeDescriptors, orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800379 if (lumas.size() != activeDescriptors.size()) {
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800380 ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(),
381 activeDescriptors.size());
Dan Stozaec460082018-12-17 15:35:09 -0800382 return;
383 }
384
385 for (size_t d = 0; d < activeDescriptors.size(); ++d) {
386 activeDescriptors[d].listener->onSampleCollected(lumas[d]);
387 }
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700388
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700389 mCachedBuffer = buffer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800390 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded));
Dan Stozaec460082018-12-17 15:35:09 -0800391}
392
Kevin DuBois26afc782019-05-06 16:46:45 -0700393// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
394void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
395 std::unique_lock<std::mutex> lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800396 while (mRunning) {
397 if (mSampleRequested) {
398 mSampleRequested = false;
Kevin DuBois26afc782019-05-06 16:46:45 -0700399 lock.unlock();
Dan Stozaec460082018-12-17 15:35:09 -0800400 captureSample();
Kevin DuBois26afc782019-05-06 16:46:45 -0700401 lock.lock();
Dan Stozaec460082018-12-17 15:35:09 -0800402 }
Kevin DuBois26afc782019-05-06 16:46:45 -0700403 mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) {
404 return mSampleRequested || !mRunning;
405 });
Dan Stozaec460082018-12-17 15:35:09 -0800406 }
407}
408
409} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800410
411// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100412#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"