blob: da8c3e062c68540562926e2785cf345b4eb5ec12 [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>
chaviwe7b9f272020-08-18 16:08:59 -070033#include <gui/SyncScreenCaptureListener.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070034#include <ui/DisplayStatInfo.h>
35#include <utils/Trace.h>
36
37#include <string>
38
Dan Stozaec460082018-12-17 15:35:09 -080039#include "DisplayDevice.h"
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020040#include "DisplayRenderArea.h"
Dan Stozaec460082018-12-17 15:35:09 -080041#include "Layer.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070042#include "Scheduler/VsyncController.h"
Dan Stozaec460082018-12-17 15:35:09 -080043#include "SurfaceFlinger.h"
44
45namespace android {
Kevin DuBois413287f2019-02-25 08:46:47 -080046using namespace std::chrono_literals;
Dan Stozaec460082018-12-17 15:35:09 -080047
48template <typename T>
49struct SpHash {
50 size_t operator()(const sp<T>& p) const { return std::hash<T*>()(p.get()); }
51};
52
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
Alec Mouri9a02eda2020-04-21 17:39:34 -0700133void RegionSamplingThread::addListener(const Rect& samplingArea, const wp<Layer>& stopLayer,
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);
136 asBinder->linkToDeath(this);
Kevin DuBois26afc782019-05-06 16:46:45 -0700137 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800138 mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayer, listener});
139}
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
207 // (b/133849373) ROT_90 screencap images produced upside down
208 auto area = sample_area;
209 if (orientation & ui::Transform::ROT_90) {
210 area.top = height - area.top;
211 area.bottom = height - area.bottom;
212 std::swap(area.top, area.bottom);
Kevin DuBois69162d02019-06-04 20:22:43 -0700213
214 area.left = width - area.left;
215 area.right = width - area.right;
216 std::swap(area.left, area.right);
Kevin DuBoisb325c932019-05-21 08:34:09 -0700217 }
218
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700219 const uint32_t pixelCount = (area.bottom - area.top) * (area.right - area.left);
220 uint32_t accumulatedLuma = 0;
Dan Stozaec460082018-12-17 15:35:09 -0800221
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700222 // Calculates luma with approximation of Rec. 709 primaries
Dan Stozaec460082018-12-17 15:35:09 -0800223 for (int32_t row = area.top; row < area.bottom; ++row) {
224 const uint32_t* rowBase = data + row * stride;
225 for (int32_t column = area.left; column < area.right; ++column) {
226 uint32_t pixel = rowBase[column];
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700227 const uint32_t r = pixel & 0xFF;
228 const uint32_t g = (pixel >> 8) & 0xFF;
229 const uint32_t b = (pixel >> 16) & 0xFF;
230 const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5;
231 accumulatedLuma += luma;
Dan Stozaec460082018-12-17 15:35:09 -0800232 }
233 }
234
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700235 return accumulatedLuma / (255.0f * pixelCount);
Dan Stozaec460082018-12-17 15:35:09 -0800236}
Dan Stozaec460082018-12-17 15:35:09 -0800237
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800238std::vector<float> RegionSamplingThread::sampleBuffer(
239 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700240 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) {
Dan Stozaec460082018-12-17 15:35:09 -0800241 void* data_raw = nullptr;
242 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw);
243 std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw),
244 [&buffer](auto) { buffer->unlock(); });
245 if (!data) return {};
246
Kevin DuBoisb325c932019-05-21 08:34:09 -0700247 const int32_t width = buffer->getWidth();
248 const int32_t height = buffer->getHeight();
Dan Stozaec460082018-12-17 15:35:09 -0800249 const int32_t stride = buffer->getStride();
250 std::vector<float> lumas(descriptors.size());
251 std::transform(descriptors.begin(), descriptors.end(), lumas.begin(),
252 [&](auto const& descriptor) {
Kevin DuBoisb325c932019-05-21 08:34:09 -0700253 return sampleArea(data.get(), width, height, stride, orientation,
254 descriptor.area - leftTop);
Dan Stozaec460082018-12-17 15:35:09 -0800255 });
256 return lumas;
257}
258
259void RegionSamplingThread::captureSample() {
260 ATRACE_CALL();
Kevin DuBois26afc782019-05-06 16:46:45 -0700261 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800262
263 if (mDescriptors.empty()) {
264 return;
265 }
266
Marin Shalamanov1c434292020-06-12 01:47:29 +0200267 wp<const DisplayDevice> displayWeak;
268
269 ui::LayerStack layerStack;
270 ui::Transform::RotationFlags orientation;
271 ui::Size displaySize;
272
273 {
274 // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread
275 const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice();
276 displayWeak = display;
277 layerStack = display->getLayerStack();
278 orientation = ui::Transform::toRotationFlags(display->getOrientation());
279 displaySize = display->getSize();
280 }
Kevin DuBoisb325c932019-05-21 08:34:09 -0700281
Dan Stozaec460082018-12-17 15:35:09 -0800282 std::vector<RegionSamplingThread::Descriptor> descriptors;
283 Region sampleRegion;
284 for (const auto& [listener, descriptor] : mDescriptors) {
285 sampleRegion.orSelf(descriptor.area);
286 descriptors.emplace_back(descriptor);
287 }
288
Marin Shalamanov1c434292020-06-12 01:47:29 +0200289 const Rect sampledBounds = sampleRegion.bounds();
Huihong Luoa8f66852021-07-02 00:22:38 -0700290 constexpr bool kUseIdentityTransform = false;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200291
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800292 SurfaceFlinger::RenderAreaFuture renderAreaFuture = ftl::defer([=] {
Huihong Luoa8f66852021-07-02 00:22:38 -0700293 return DisplayRenderArea::create(displayWeak, sampledBounds, sampledBounds.getSize(),
294 ui::Dataspace::V0_SRGB, kUseIdentityTransform);
Marin Shalamanov1c434292020-06-12 01:47:29 +0200295 });
Dan Stozaec460082018-12-17 15:35:09 -0800296
297 std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners;
298
299 auto traverseLayers = [&](const LayerVector::Visitor& visitor) {
300 bool stopLayerFound = false;
301 auto filterVisitor = [&](Layer* layer) {
302 // We don't want to capture any layers beyond the stop layer
303 if (stopLayerFound) return;
304
305 // Likewise if we just found a stop layer, set the flag and abort
306 for (const auto& [area, stopLayer, listener] : descriptors) {
307 if (layer == stopLayer.promote().get()) {
308 stopLayerFound = true;
309 return;
310 }
311 }
312
313 // Compute the layer's position on the screen
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800314 const Rect bounds = Rect(layer->getBounds());
315 const ui::Transform transform = layer->getTransform();
Dan Stozaec460082018-12-17 15:35:09 -0800316 constexpr bool roundOutwards = true;
317 Rect transformed = transform.transform(bounds, roundOutwards);
318
Marin Shalamanov1c434292020-06-12 01:47:29 +0200319 // If this layer doesn't intersect with the larger sampledBounds, skip capturing it
Dan Stozaec460082018-12-17 15:35:09 -0800320 Rect ignore;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200321 if (!transformed.intersect(sampledBounds, &ignore)) return;
Dan Stozaec460082018-12-17 15:35:09 -0800322
323 // If the layer doesn't intersect a sampling area, skip capturing it
324 bool intersectsAnyArea = false;
325 for (const auto& [area, stopLayer, listener] : descriptors) {
326 if (transformed.intersect(area, &ignore)) {
327 intersectsAnyArea = true;
328 listeners.insert(listener);
329 }
330 }
331 if (!intersectsAnyArea) return;
332
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700333 ALOGV("Traversing [%s] [%d, %d, %d, %d]", layer->getDebugName(), bounds.left,
Dan Stozaec460082018-12-17 15:35:09 -0800334 bounds.top, bounds.right, bounds.bottom);
335 visitor(layer);
336 };
chaviw4b9d5e12020-08-04 18:30:35 -0700337 mFlinger.traverseLayersInLayerStack(layerStack, CaptureArgs::UNSET_UID, filterVisitor);
Dan Stozaec460082018-12-17 15:35:09 -0800338 };
339
Alec Mouria90a5702021-04-16 16:36:21 +0000340 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
341 if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() &&
342 mCachedBuffer->getBuffer()->getHeight() == sampledBounds.getHeight()) {
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700343 buffer = mCachedBuffer;
344 } else {
John Reck67b1e2b2020-08-26 13:17:24 -0700345 const uint32_t usage =
346 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000347 sp<GraphicBuffer> graphicBuffer =
348 new GraphicBuffer(sampledBounds.getWidth(), sampledBounds.getHeight(),
349 PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread");
350 const status_t bufferStatus = graphicBuffer->initCheck();
Alec Mouri7013b6f2021-02-12 11:16:54 -0800351 LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "captureSample: Buffer failed to allocate: %d",
352 bufferStatus);
Alec Mouria90a5702021-04-16 16:36:21 +0000353 buffer = std::make_shared<
354 renderengine::ExternalTexture>(graphicBuffer, mFlinger.getRenderEngine(),
355 renderengine::ExternalTexture::Usage::WRITEABLE);
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700356 }
Dan Stozaec460082018-12-17 15:35:09 -0800357
Sally Qi59a9f502021-10-12 18:53:23 +0000358 auto captureScreenResultFuture =
359 mFlinger.captureScreenCommon(std::move(renderAreaFuture), traverseLayers, buffer,
360 true /* regionSampling */, false /* grayscale */, nullptr);
361 auto& captureScreenResult = captureScreenResultFuture.get();
362 if (captureScreenResult.drawFence.ok()) {
363 sync_wait(captureScreenResult.drawFence.get(), -1);
364 }
Dan Stozaec460082018-12-17 15:35:09 -0800365
366 std::vector<Descriptor> activeDescriptors;
367 for (const auto& descriptor : descriptors) {
368 if (listeners.count(descriptor.listener) != 0) {
369 activeDescriptors.emplace_back(descriptor);
370 }
371 }
372
373 ALOGV("Sampling %zu descriptors", activeDescriptors.size());
Alec Mouria90a5702021-04-16 16:36:21 +0000374 std::vector<float> lumas = sampleBuffer(buffer->getBuffer(), sampledBounds.leftTop(),
375 activeDescriptors, orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800376 if (lumas.size() != activeDescriptors.size()) {
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800377 ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(),
378 activeDescriptors.size());
Dan Stozaec460082018-12-17 15:35:09 -0800379 return;
380 }
381
382 for (size_t d = 0; d < activeDescriptors.size(); ++d) {
383 activeDescriptors[d].listener->onSampleCollected(lumas[d]);
384 }
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700385
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700386 mCachedBuffer = buffer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800387 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded));
Dan Stozaec460082018-12-17 15:35:09 -0800388}
389
Kevin DuBois26afc782019-05-06 16:46:45 -0700390// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
391void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
392 std::unique_lock<std::mutex> lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800393 while (mRunning) {
394 if (mSampleRequested) {
395 mSampleRequested = false;
Kevin DuBois26afc782019-05-06 16:46:45 -0700396 lock.unlock();
Dan Stozaec460082018-12-17 15:35:09 -0800397 captureSample();
Kevin DuBois26afc782019-05-06 16:46:45 -0700398 lock.lock();
Dan Stozaec460082018-12-17 15:35:09 -0800399 }
Kevin DuBois26afc782019-05-06 16:46:45 -0700400 mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) {
401 return mSampleRequested || !mRunning;
402 });
Dan Stozaec460082018-12-17 15:35:09 -0800403 }
404}
405
406} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800407
408// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100409#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"