blob: 32585dd9ac52b60cc684917957b049089771fea3 [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>
Dominik Laskowski98041832019-08-01 18:35:59 -070033#include <gui/IRegionSamplingListener.h>
chaviwe7b9f272020-08-18 16:08:59 -070034#include <gui/SyncScreenCaptureListener.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070035#include <ui/DisplayStatInfo.h>
36#include <utils/Trace.h>
37
38#include <string>
39
Dan Stozaec460082018-12-17 15:35:09 -080040#include "DisplayDevice.h"
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020041#include "DisplayRenderArea.h"
Dan Stozaec460082018-12-17 15:35:09 -080042#include "Layer.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070043#include "Scheduler/VsyncController.h"
Dan Stozaec460082018-12-17 15:35:09 -080044#include "SurfaceFlinger.h"
45
46namespace android {
Kevin DuBois413287f2019-02-25 08:46:47 -080047using namespace std::chrono_literals;
Dan Stozaec460082018-12-17 15:35:09 -080048
49template <typename T>
50struct SpHash {
51 size_t operator()(const sp<T>& p) const { return std::hash<T*>()(p.get()); }
52};
53
Kevin DuBois413287f2019-02-25 08:46:47 -080054constexpr auto lumaSamplingStepTag = "LumaSamplingStep";
55enum class samplingStep {
56 noWorkNeeded,
57 idleTimerWaiting,
John Dias84be7832019-06-18 17:05:26 -070058 waitForQuietFrame,
Kevin DuBois413287f2019-02-25 08:46:47 -080059 waitForSamplePhase,
60 sample
61};
62
Ady Abraham9c53ee72020-07-22 21:16:18 -070063constexpr auto defaultRegionSamplingWorkDuration = 3ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080064constexpr auto defaultRegionSamplingPeriod = 100ms;
65constexpr auto defaultRegionSamplingTimerTimeout = 100ms;
Ady Abraham562c2712021-05-07 15:10:42 -070066constexpr auto maxRegionSamplingDelay = 100ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080067// TODO: (b/127403193) duration to string conversion could probably be constexpr
68template <typename Rep, typename Per>
69inline std::string toNsString(std::chrono::duration<Rep, Per> t) {
70 return std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(t).count());
Dan Stozaec460082018-12-17 15:35:09 -080071}
72
Kevin DuBois413287f2019-02-25 08:46:47 -080073RegionSamplingThread::EnvironmentTimingTunables::EnvironmentTimingTunables() {
74 char value[PROPERTY_VALUE_MAX] = {};
75
Ady Abraham9c53ee72020-07-22 21:16:18 -070076 property_get("debug.sf.region_sampling_duration_ns", value,
77 toNsString(defaultRegionSamplingWorkDuration).c_str());
78 int const samplingDurationNsRaw = atoi(value);
Kevin DuBois413287f2019-02-25 08:46:47 -080079
80 property_get("debug.sf.region_sampling_period_ns", value,
81 toNsString(defaultRegionSamplingPeriod).c_str());
82 int const samplingPeriodNsRaw = atoi(value);
83
84 property_get("debug.sf.region_sampling_timer_timeout_ns", value,
85 toNsString(defaultRegionSamplingTimerTimeout).c_str());
86 int const samplingTimerTimeoutNsRaw = atoi(value);
87
88 if ((samplingPeriodNsRaw < 0) || (samplingTimerTimeoutNsRaw < 0)) {
89 ALOGW("User-specified sampling tuning options nonsensical. Using defaults");
Ady Abraham9c53ee72020-07-22 21:16:18 -070090 mSamplingDuration = defaultRegionSamplingWorkDuration;
Kevin DuBois413287f2019-02-25 08:46:47 -080091 mSamplingPeriod = defaultRegionSamplingPeriod;
92 mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout;
93 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070094 mSamplingDuration = std::chrono::nanoseconds(samplingDurationNsRaw);
Kevin DuBois413287f2019-02-25 08:46:47 -080095 mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw);
96 mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw);
97 }
98}
99
Ady Abraham562c2712021-05-07 15:10:42 -0700100RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables)
Kevin DuBois413287f2019-02-25 08:46:47 -0800101 : mFlinger(flinger),
Kevin DuBois413287f2019-02-25 08:46:47 -0800102 mTunables(tunables),
Ady Abraham9c53ee72020-07-22 21:16:18 -0700103 mIdleTimer(
Ady Abrahamdde984b2021-03-18 12:47:36 -0700104 "RegSampIdle",
Ady Abraham9c53ee72020-07-22 21:16:18 -0700105 std::chrono::duration_cast<std::chrono::milliseconds>(
106 mTunables.mSamplingTimerTimeout),
107 [] {}, [this] { checkForStaleLuma(); }),
Ady Abraham562c2712021-05-07 15:10:42 -0700108 mLastSampleTime(0ns) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700109 mThread = std::thread([this]() { threadMain(); });
Ady Abrahamdde984b2021-03-18 12:47:36 -0700110 pthread_setname_np(mThread.native_handle(), "RegionSampling");
Kevin DuBois413287f2019-02-25 08:46:47 -0800111 mIdleTimer.start();
112}
113
Ady Abraham562c2712021-05-07 15:10:42 -0700114RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger)
115 : RegionSamplingThread(flinger,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700116 TimingTunables{defaultRegionSamplingWorkDuration,
Kevin DuBois413287f2019-02-25 08:46:47 -0800117 defaultRegionSamplingPeriod,
118 defaultRegionSamplingTimerTimeout}) {}
119
Dan Stozaec460082018-12-17 15:35:09 -0800120RegionSamplingThread::~RegionSamplingThread() {
Kevin DuBois413287f2019-02-25 08:46:47 -0800121 mIdleTimer.stop();
122
Dan Stozaec460082018-12-17 15:35:09 -0800123 {
Kevin DuBois26afc782019-05-06 16:46:45 -0700124 std::lock_guard lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800125 mRunning = false;
126 mCondition.notify_one();
127 }
128
Dan Stozaec460082018-12-17 15:35:09 -0800129 if (mThread.joinable()) {
130 mThread.join();
131 }
132}
133
Alec Mouri9a02eda2020-04-21 17:39:34 -0700134void RegionSamplingThread::addListener(const Rect& samplingArea, const wp<Layer>& stopLayer,
Dan Stozaec460082018-12-17 15:35:09 -0800135 const sp<IRegionSamplingListener>& listener) {
Dan Stozaec460082018-12-17 15:35:09 -0800136 sp<IBinder> asBinder = IInterface::asBinder(listener);
137 asBinder->linkToDeath(this);
Kevin DuBois26afc782019-05-06 16:46:45 -0700138 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800139 mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayer, listener});
140}
141
142void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700143 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800144 mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener)));
145}
146
Kevin DuBois413287f2019-02-25 08:46:47 -0800147void RegionSamplingThread::checkForStaleLuma() {
Kevin DuBois26afc782019-05-06 16:46:45 -0700148 std::lock_guard lock(mThreadControlMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800149
Ady Abraham562c2712021-05-07 15:10:42 -0700150 if (mSampleRequestTime.has_value()) {
151 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase));
152 mSampleRequestTime.reset();
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700153 mFlinger.scheduleSample();
Kevin DuBois413287f2019-02-25 08:46:47 -0800154 }
155}
156
Ady Abraham562c2712021-05-07 15:10:42 -0700157void RegionSamplingThread::onCompositionComplete(
158 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
159 doSample(samplingDeadline);
Kevin DuBois413287f2019-02-25 08:46:47 -0800160}
161
Ady Abraham562c2712021-05-07 15:10:42 -0700162void RegionSamplingThread::doSample(
163 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700164 std::lock_guard lock(mThreadControlMutex);
Ady Abraham562c2712021-05-07 15:10:42 -0700165 const auto now = std::chrono::steady_clock::now();
166 if (mLastSampleTime + mTunables.mSamplingPeriod > now) {
167 // content changed, but we sampled not too long ago, so we need to sample some time in the
168 // future.
Kevin DuBois413287f2019-02-25 08:46:47 -0800169 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting));
Ady Abraham562c2712021-05-07 15:10:42 -0700170 mSampleRequestTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800171 return;
172 }
Ady Abraham562c2712021-05-07 15:10:42 -0700173 if (!mSampleRequestTime.has_value() || now - *mSampleRequestTime < maxRegionSamplingDelay) {
John Dias84be7832019-06-18 17:05:26 -0700174 // If there is relatively little time left for surfaceflinger
175 // until the next vsync deadline, defer this sampling work
176 // to a later frame, when hopefully there will be more time.
Ady Abraham562c2712021-05-07 15:10:42 -0700177 if (samplingDeadline.has_value() && now + mTunables.mSamplingDuration > *samplingDeadline) {
John Dias84be7832019-06-18 17:05:26 -0700178 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame));
Ady Abraham562c2712021-05-07 15:10:42 -0700179 mSampleRequestTime = mSampleRequestTime.value_or(now);
John Dias84be7832019-06-18 17:05:26 -0700180 return;
181 }
182 }
Kevin DuBois413287f2019-02-25 08:46:47 -0800183
184 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample));
185
Ady Abraham562c2712021-05-07 15:10:42 -0700186 mSampleRequestTime.reset();
187 mLastSampleTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800188
189 mIdleTimer.reset();
Kevin DuBois413287f2019-02-25 08:46:47 -0800190
Dan Stozaec460082018-12-17 15:35:09 -0800191 mSampleRequested = true;
192 mCondition.notify_one();
193}
194
195void RegionSamplingThread::binderDied(const wp<IBinder>& who) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700196 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800197 mDescriptors.erase(who);
198}
199
Kevin DuBoisb325c932019-05-21 08:34:09 -0700200float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
201 uint32_t orientation, const Rect& sample_area) {
202 if (!sample_area.isValid() || (sample_area.getWidth() > width) ||
203 (sample_area.getHeight() > height)) {
204 ALOGE("invalid sampling region requested");
205 return 0.0f;
206 }
207
208 // (b/133849373) ROT_90 screencap images produced upside down
209 auto area = sample_area;
210 if (orientation & ui::Transform::ROT_90) {
211 area.top = height - area.top;
212 area.bottom = height - area.bottom;
213 std::swap(area.top, area.bottom);
Kevin DuBois69162d02019-06-04 20:22:43 -0700214
215 area.left = width - area.left;
216 area.right = width - area.right;
217 std::swap(area.left, area.right);
Kevin DuBoisb325c932019-05-21 08:34:09 -0700218 }
219
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700220 const uint32_t pixelCount = (area.bottom - area.top) * (area.right - area.left);
221 uint32_t accumulatedLuma = 0;
Dan Stozaec460082018-12-17 15:35:09 -0800222
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700223 // Calculates luma with approximation of Rec. 709 primaries
Dan Stozaec460082018-12-17 15:35:09 -0800224 for (int32_t row = area.top; row < area.bottom; ++row) {
225 const uint32_t* rowBase = data + row * stride;
226 for (int32_t column = area.left; column < area.right; ++column) {
227 uint32_t pixel = rowBase[column];
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700228 const uint32_t r = pixel & 0xFF;
229 const uint32_t g = (pixel >> 8) & 0xFF;
230 const uint32_t b = (pixel >> 16) & 0xFF;
231 const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5;
232 accumulatedLuma += luma;
Dan Stozaec460082018-12-17 15:35:09 -0800233 }
234 }
235
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700236 return accumulatedLuma / (255.0f * pixelCount);
Dan Stozaec460082018-12-17 15:35:09 -0800237}
Dan Stozaec460082018-12-17 15:35:09 -0800238
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800239std::vector<float> RegionSamplingThread::sampleBuffer(
240 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700241 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) {
Dan Stozaec460082018-12-17 15:35:09 -0800242 void* data_raw = nullptr;
243 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw);
244 std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw),
245 [&buffer](auto) { buffer->unlock(); });
246 if (!data) return {};
247
Kevin DuBoisb325c932019-05-21 08:34:09 -0700248 const int32_t width = buffer->getWidth();
249 const int32_t height = buffer->getHeight();
Dan Stozaec460082018-12-17 15:35:09 -0800250 const int32_t stride = buffer->getStride();
251 std::vector<float> lumas(descriptors.size());
252 std::transform(descriptors.begin(), descriptors.end(), lumas.begin(),
253 [&](auto const& descriptor) {
Kevin DuBoisb325c932019-05-21 08:34:09 -0700254 return sampleArea(data.get(), width, height, stride, orientation,
255 descriptor.area - leftTop);
Dan Stozaec460082018-12-17 15:35:09 -0800256 });
257 return lumas;
258}
259
260void RegionSamplingThread::captureSample() {
261 ATRACE_CALL();
Kevin DuBois26afc782019-05-06 16:46:45 -0700262 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800263
264 if (mDescriptors.empty()) {
265 return;
266 }
267
Marin Shalamanov1c434292020-06-12 01:47:29 +0200268 wp<const DisplayDevice> displayWeak;
269
270 ui::LayerStack layerStack;
271 ui::Transform::RotationFlags orientation;
272 ui::Size displaySize;
273
274 {
275 // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread
276 const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice();
277 displayWeak = display;
278 layerStack = display->getLayerStack();
279 orientation = ui::Transform::toRotationFlags(display->getOrientation());
280 displaySize = display->getSize();
281 }
Kevin DuBoisb325c932019-05-21 08:34:09 -0700282
Dan Stozaec460082018-12-17 15:35:09 -0800283 std::vector<RegionSamplingThread::Descriptor> descriptors;
284 Region sampleRegion;
285 for (const auto& [listener, descriptor] : mDescriptors) {
286 sampleRegion.orSelf(descriptor.area);
287 descriptors.emplace_back(descriptor);
288 }
289
Marin Shalamanov1c434292020-06-12 01:47:29 +0200290 const Rect sampledBounds = sampleRegion.bounds();
Huihong Luoa8f66852021-07-02 00:22:38 -0700291 constexpr bool kUseIdentityTransform = false;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200292
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800293 SurfaceFlinger::RenderAreaFuture renderAreaFuture = ftl::defer([=] {
Huihong Luoa8f66852021-07-02 00:22:38 -0700294 return DisplayRenderArea::create(displayWeak, sampledBounds, sampledBounds.getSize(),
295 ui::Dataspace::V0_SRGB, kUseIdentityTransform);
Marin Shalamanov1c434292020-06-12 01:47:29 +0200296 });
Dan Stozaec460082018-12-17 15:35:09 -0800297
298 std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners;
299
300 auto traverseLayers = [&](const LayerVector::Visitor& visitor) {
301 bool stopLayerFound = false;
302 auto filterVisitor = [&](Layer* layer) {
303 // We don't want to capture any layers beyond the stop layer
304 if (stopLayerFound) return;
305
306 // Likewise if we just found a stop layer, set the flag and abort
307 for (const auto& [area, stopLayer, listener] : descriptors) {
308 if (layer == stopLayer.promote().get()) {
309 stopLayerFound = true;
310 return;
311 }
312 }
313
314 // Compute the layer's position on the screen
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800315 const Rect bounds = Rect(layer->getBounds());
316 const ui::Transform transform = layer->getTransform();
Dan Stozaec460082018-12-17 15:35:09 -0800317 constexpr bool roundOutwards = true;
318 Rect transformed = transform.transform(bounds, roundOutwards);
319
Marin Shalamanov1c434292020-06-12 01:47:29 +0200320 // If this layer doesn't intersect with the larger sampledBounds, skip capturing it
Dan Stozaec460082018-12-17 15:35:09 -0800321 Rect ignore;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200322 if (!transformed.intersect(sampledBounds, &ignore)) return;
Dan Stozaec460082018-12-17 15:35:09 -0800323
324 // If the layer doesn't intersect a sampling area, skip capturing it
325 bool intersectsAnyArea = false;
326 for (const auto& [area, stopLayer, listener] : descriptors) {
327 if (transformed.intersect(area, &ignore)) {
328 intersectsAnyArea = true;
329 listeners.insert(listener);
330 }
331 }
332 if (!intersectsAnyArea) return;
333
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700334 ALOGV("Traversing [%s] [%d, %d, %d, %d]", layer->getDebugName(), bounds.left,
Dan Stozaec460082018-12-17 15:35:09 -0800335 bounds.top, bounds.right, bounds.bottom);
336 visitor(layer);
337 };
chaviw4b9d5e12020-08-04 18:30:35 -0700338 mFlinger.traverseLayersInLayerStack(layerStack, CaptureArgs::UNSET_UID, filterVisitor);
Dan Stozaec460082018-12-17 15:35:09 -0800339 };
340
Alec Mouria90a5702021-04-16 16:36:21 +0000341 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
342 if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() &&
343 mCachedBuffer->getBuffer()->getHeight() == sampledBounds.getHeight()) {
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700344 buffer = mCachedBuffer;
345 } else {
John Reck67b1e2b2020-08-26 13:17:24 -0700346 const uint32_t usage =
347 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000348 sp<GraphicBuffer> graphicBuffer =
349 new GraphicBuffer(sampledBounds.getWidth(), sampledBounds.getHeight(),
350 PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread");
351 const status_t bufferStatus = graphicBuffer->initCheck();
Alec Mouri7013b6f2021-02-12 11:16:54 -0800352 LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "captureSample: Buffer failed to allocate: %d",
353 bufferStatus);
Alec Mouria90a5702021-04-16 16:36:21 +0000354 buffer = std::make_shared<
355 renderengine::ExternalTexture>(graphicBuffer, mFlinger.getRenderEngine(),
356 renderengine::ExternalTexture::Usage::WRITEABLE);
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700357 }
Dan Stozaec460082018-12-17 15:35:09 -0800358
Sally Qi59a9f502021-10-12 18:53:23 +0000359 auto captureScreenResultFuture =
360 mFlinger.captureScreenCommon(std::move(renderAreaFuture), traverseLayers, buffer,
361 true /* regionSampling */, false /* grayscale */, nullptr);
362 auto& captureScreenResult = captureScreenResultFuture.get();
363 if (captureScreenResult.drawFence.ok()) {
364 sync_wait(captureScreenResult.drawFence.get(), -1);
365 }
Dan Stozaec460082018-12-17 15:35:09 -0800366
367 std::vector<Descriptor> activeDescriptors;
368 for (const auto& descriptor : descriptors) {
369 if (listeners.count(descriptor.listener) != 0) {
370 activeDescriptors.emplace_back(descriptor);
371 }
372 }
373
374 ALOGV("Sampling %zu descriptors", activeDescriptors.size());
Alec Mouria90a5702021-04-16 16:36:21 +0000375 std::vector<float> lumas = sampleBuffer(buffer->getBuffer(), sampledBounds.leftTop(),
376 activeDescriptors, orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800377 if (lumas.size() != activeDescriptors.size()) {
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800378 ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(),
379 activeDescriptors.size());
Dan Stozaec460082018-12-17 15:35:09 -0800380 return;
381 }
382
383 for (size_t d = 0; d < activeDescriptors.size(); ++d) {
384 activeDescriptors[d].listener->onSampleCollected(lumas[d]);
385 }
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700386
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700387 mCachedBuffer = buffer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800388 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded));
Dan Stozaec460082018-12-17 15:35:09 -0800389}
390
Kevin DuBois26afc782019-05-06 16:46:45 -0700391// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
392void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
393 std::unique_lock<std::mutex> lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800394 while (mRunning) {
395 if (mSampleRequested) {
396 mSampleRequested = false;
Kevin DuBois26afc782019-05-06 16:46:45 -0700397 lock.unlock();
Dan Stozaec460082018-12-17 15:35:09 -0800398 captureSample();
Kevin DuBois26afc782019-05-06 16:46:45 -0700399 lock.lock();
Dan Stozaec460082018-12-17 15:35:09 -0800400 }
Kevin DuBois26afc782019-05-06 16:46:45 -0700401 mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) {
402 return mSampleRequested || !mRunning;
403 });
Dan Stozaec460082018-12-17 15:35:09 -0800404 }
405}
406
407} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800408
409// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100410#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"