blob: 68cd84f661944f57cc1e2b60532d4199004a4301 [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"
20
Dan Stozaec460082018-12-17 15:35:09 -080021//#define LOG_NDEBUG 0
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23#undef LOG_TAG
24#define LOG_TAG "RegionSamplingThread"
25
26#include "RegionSamplingThread.h"
27
Kevin DuBoisb325c932019-05-21 08:34:09 -070028#include <compositionengine/Display.h>
29#include <compositionengine/impl/OutputCompositionState.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070030#include <cutils/properties.h>
31#include <gui/IRegionSamplingListener.h>
32#include <ui/DisplayStatInfo.h>
33#include <utils/Trace.h>
34
35#include <string>
36
Dan Stozaec460082018-12-17 15:35:09 -080037#include "DisplayDevice.h"
38#include "Layer.h"
Dominik Laskowski98041832019-08-01 18:35:59 -070039#include "Scheduler/DispSync.h"
Dan Stozaec460082018-12-17 15:35:09 -080040#include "SurfaceFlinger.h"
41
42namespace android {
Kevin DuBois413287f2019-02-25 08:46:47 -080043using namespace std::chrono_literals;
Dan Stozaec460082018-12-17 15:35:09 -080044
45template <typename T>
46struct SpHash {
47 size_t operator()(const sp<T>& p) const { return std::hash<T*>()(p.get()); }
48};
49
Kevin DuBois413287f2019-02-25 08:46:47 -080050constexpr auto lumaSamplingStepTag = "LumaSamplingStep";
51enum class samplingStep {
52 noWorkNeeded,
53 idleTimerWaiting,
John Dias84be7832019-06-18 17:05:26 -070054 waitForQuietFrame,
Kevin DuBois413287f2019-02-25 08:46:47 -080055 waitForZeroPhase,
56 waitForSamplePhase,
57 sample
58};
59
John Dias84be7832019-06-18 17:05:26 -070060constexpr auto timeForRegionSampling = 5000000ns;
61constexpr auto maxRegionSamplingSkips = 10;
Kevin DuBois413287f2019-02-25 08:46:47 -080062constexpr auto defaultRegionSamplingOffset = -3ms;
63constexpr auto defaultRegionSamplingPeriod = 100ms;
64constexpr auto defaultRegionSamplingTimerTimeout = 100ms;
65// 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
74 property_get("debug.sf.region_sampling_offset_ns", value,
75 toNsString(defaultRegionSamplingOffset).c_str());
76 int const samplingOffsetNsRaw = atoi(value);
77
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");
88 mSamplingOffset = defaultRegionSamplingOffset;
89 mSamplingPeriod = defaultRegionSamplingPeriod;
90 mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout;
91 } else {
92 mSamplingOffset = std::chrono::nanoseconds(samplingOffsetNsRaw);
93 mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw);
94 mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw);
95 }
96}
97
98struct SamplingOffsetCallback : DispSync::Callback {
99 SamplingOffsetCallback(RegionSamplingThread& samplingThread, Scheduler& scheduler,
100 std::chrono::nanoseconds targetSamplingOffset)
101 : mRegionSamplingThread(samplingThread),
102 mScheduler(scheduler),
103 mTargetSamplingOffset(targetSamplingOffset) {}
104
105 ~SamplingOffsetCallback() { stopVsyncListener(); }
106
107 SamplingOffsetCallback(const SamplingOffsetCallback&) = delete;
108 SamplingOffsetCallback& operator=(const SamplingOffsetCallback&) = delete;
109
110 void startVsyncListener() {
111 std::lock_guard lock(mMutex);
112 if (mVsyncListening) return;
113
114 mPhaseIntervalSetting = Phase::ZERO;
Dominik Laskowski98041832019-08-01 18:35:59 -0700115 mScheduler.getPrimaryDispSync().addEventListener("SamplingThreadDispSyncListener", 0, this,
116 mLastCallbackTime);
Kevin DuBois413287f2019-02-25 08:46:47 -0800117 mVsyncListening = true;
118 }
119
120 void stopVsyncListener() {
121 std::lock_guard lock(mMutex);
122 stopVsyncListenerLocked();
123 }
124
125private:
126 void stopVsyncListenerLocked() /*REQUIRES(mMutex)*/ {
127 if (!mVsyncListening) return;
128
Dominik Laskowski98041832019-08-01 18:35:59 -0700129 mScheduler.getPrimaryDispSync().removeEventListener(this, &mLastCallbackTime);
Kevin DuBois413287f2019-02-25 08:46:47 -0800130 mVsyncListening = false;
131 }
132
133 void onDispSyncEvent(nsecs_t /* when */) final {
134 std::unique_lock<decltype(mMutex)> lock(mMutex);
135
136 if (mPhaseIntervalSetting == Phase::ZERO) {
137 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase));
138 mPhaseIntervalSetting = Phase::SAMPLING;
Dominik Laskowski98041832019-08-01 18:35:59 -0700139 mScheduler.getPrimaryDispSync().changePhaseOffset(this, mTargetSamplingOffset.count());
Kevin DuBois413287f2019-02-25 08:46:47 -0800140 return;
141 }
142
143 if (mPhaseIntervalSetting == Phase::SAMPLING) {
144 mPhaseIntervalSetting = Phase::ZERO;
Dominik Laskowski98041832019-08-01 18:35:59 -0700145 mScheduler.getPrimaryDispSync().changePhaseOffset(this, 0);
Kevin DuBois413287f2019-02-25 08:46:47 -0800146 stopVsyncListenerLocked();
147 lock.unlock();
148 mRegionSamplingThread.notifySamplingOffset();
149 return;
150 }
151 }
152
153 RegionSamplingThread& mRegionSamplingThread;
154 Scheduler& mScheduler;
155 const std::chrono::nanoseconds mTargetSamplingOffset;
156 mutable std::mutex mMutex;
Alec Mouri7355eb22019-03-05 14:19:10 -0800157 nsecs_t mLastCallbackTime = 0;
Kevin DuBois413287f2019-02-25 08:46:47 -0800158 enum class Phase {
159 ZERO,
160 SAMPLING
161 } mPhaseIntervalSetting /*GUARDED_BY(mMutex) macro doesnt work with unique_lock?*/
162 = Phase::ZERO;
163 bool mVsyncListening /*GUARDED_BY(mMutex)*/ = false;
164};
165
166RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler,
167 const TimingTunables& tunables)
168 : mFlinger(flinger),
169 mScheduler(scheduler),
170 mTunables(tunables),
171 mIdleTimer(std::chrono::duration_cast<std::chrono::milliseconds>(
172 mTunables.mSamplingTimerTimeout),
173 [] {}, [this] { checkForStaleLuma(); }),
174 mPhaseCallback(std::make_unique<SamplingOffsetCallback>(*this, mScheduler,
175 tunables.mSamplingOffset)),
176 lastSampleTime(0ns) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700177 mThread = std::thread([this]() { threadMain(); });
178 pthread_setname_np(mThread.native_handle(), "RegionSamplingThread");
Kevin DuBois413287f2019-02-25 08:46:47 -0800179 mIdleTimer.start();
180}
181
182RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler)
183 : RegionSamplingThread(flinger, scheduler,
184 TimingTunables{defaultRegionSamplingOffset,
185 defaultRegionSamplingPeriod,
186 defaultRegionSamplingTimerTimeout}) {}
187
Dan Stozaec460082018-12-17 15:35:09 -0800188RegionSamplingThread::~RegionSamplingThread() {
Kevin DuBois413287f2019-02-25 08:46:47 -0800189 mIdleTimer.stop();
190
Dan Stozaec460082018-12-17 15:35:09 -0800191 {
Kevin DuBois26afc782019-05-06 16:46:45 -0700192 std::lock_guard lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800193 mRunning = false;
194 mCondition.notify_one();
195 }
196
Dan Stozaec460082018-12-17 15:35:09 -0800197 if (mThread.joinable()) {
198 mThread.join();
199 }
200}
201
202void RegionSamplingThread::addListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
203 const sp<IRegionSamplingListener>& listener) {
Steven Moreland858c55f2020-03-11 17:48:31 -0700204 wp<Layer> stopLayer;
205 if (stopLayerHandle != nullptr && stopLayerHandle->localBinder() != nullptr) {
206 stopLayer = static_cast<Layer::Handle*>(stopLayerHandle.get())->owner;
207 }
Dan Stozaec460082018-12-17 15:35:09 -0800208
209 sp<IBinder> asBinder = IInterface::asBinder(listener);
210 asBinder->linkToDeath(this);
Kevin DuBois26afc782019-05-06 16:46:45 -0700211 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800212 mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayer, listener});
213}
214
215void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700216 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800217 mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener)));
218}
219
Kevin DuBois413287f2019-02-25 08:46:47 -0800220void RegionSamplingThread::checkForStaleLuma() {
Kevin DuBois26afc782019-05-06 16:46:45 -0700221 std::lock_guard lock(mThreadControlMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800222
John Dias84be7832019-06-18 17:05:26 -0700223 if (mDiscardedFrames > 0) {
Kevin DuBois413287f2019-02-25 08:46:47 -0800224 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForZeroPhase));
John Dias84be7832019-06-18 17:05:26 -0700225 mDiscardedFrames = 0;
Kevin DuBois413287f2019-02-25 08:46:47 -0800226 mPhaseCallback->startVsyncListener();
227 }
228}
229
230void RegionSamplingThread::notifyNewContent() {
231 doSample();
232}
233
234void RegionSamplingThread::notifySamplingOffset() {
235 doSample();
236}
237
238void RegionSamplingThread::doSample() {
Kevin DuBois26afc782019-05-06 16:46:45 -0700239 std::lock_guard lock(mThreadControlMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800240 auto now = std::chrono::nanoseconds(systemTime(SYSTEM_TIME_MONOTONIC));
241 if (lastSampleTime + mTunables.mSamplingPeriod > now) {
242 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting));
John Dias84be7832019-06-18 17:05:26 -0700243 if (mDiscardedFrames == 0) mDiscardedFrames++;
Kevin DuBois413287f2019-02-25 08:46:47 -0800244 return;
245 }
John Dias84be7832019-06-18 17:05:26 -0700246 if (mDiscardedFrames < maxRegionSamplingSkips) {
247 // If there is relatively little time left for surfaceflinger
248 // until the next vsync deadline, defer this sampling work
249 // to a later frame, when hopefully there will be more time.
250 DisplayStatInfo stats;
251 mScheduler.getDisplayStatInfo(&stats);
252 if (std::chrono::nanoseconds(stats.vsyncTime) - now < timeForRegionSampling) {
253 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame));
254 mDiscardedFrames++;
255 return;
256 }
257 }
Kevin DuBois413287f2019-02-25 08:46:47 -0800258
259 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample));
260
John Dias84be7832019-06-18 17:05:26 -0700261 mDiscardedFrames = 0;
Kevin DuBois413287f2019-02-25 08:46:47 -0800262 lastSampleTime = now;
263
264 mIdleTimer.reset();
265 mPhaseCallback->stopVsyncListener();
266
Dan Stozaec460082018-12-17 15:35:09 -0800267 mSampleRequested = true;
268 mCondition.notify_one();
269}
270
271void RegionSamplingThread::binderDied(const wp<IBinder>& who) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700272 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800273 mDescriptors.erase(who);
274}
275
Kevin DuBoisb325c932019-05-21 08:34:09 -0700276float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
277 uint32_t orientation, const Rect& sample_area) {
278 if (!sample_area.isValid() || (sample_area.getWidth() > width) ||
279 (sample_area.getHeight() > height)) {
280 ALOGE("invalid sampling region requested");
281 return 0.0f;
282 }
283
284 // (b/133849373) ROT_90 screencap images produced upside down
285 auto area = sample_area;
286 if (orientation & ui::Transform::ROT_90) {
287 area.top = height - area.top;
288 area.bottom = height - area.bottom;
289 std::swap(area.top, area.bottom);
Kevin DuBois69162d02019-06-04 20:22:43 -0700290
291 area.left = width - area.left;
292 area.right = width - area.right;
293 std::swap(area.left, area.right);
Kevin DuBoisb325c932019-05-21 08:34:09 -0700294 }
295
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700296 const uint32_t pixelCount = (area.bottom - area.top) * (area.right - area.left);
297 uint32_t accumulatedLuma = 0;
Dan Stozaec460082018-12-17 15:35:09 -0800298
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700299 // Calculates luma with approximation of Rec. 709 primaries
Dan Stozaec460082018-12-17 15:35:09 -0800300 for (int32_t row = area.top; row < area.bottom; ++row) {
301 const uint32_t* rowBase = data + row * stride;
302 for (int32_t column = area.left; column < area.right; ++column) {
303 uint32_t pixel = rowBase[column];
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700304 const uint32_t r = pixel & 0xFF;
305 const uint32_t g = (pixel >> 8) & 0xFF;
306 const uint32_t b = (pixel >> 16) & 0xFF;
307 const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5;
308 accumulatedLuma += luma;
Dan Stozaec460082018-12-17 15:35:09 -0800309 }
310 }
311
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700312 return accumulatedLuma / (255.0f * pixelCount);
Dan Stozaec460082018-12-17 15:35:09 -0800313}
Dan Stozaec460082018-12-17 15:35:09 -0800314
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800315std::vector<float> RegionSamplingThread::sampleBuffer(
316 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700317 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) {
Dan Stozaec460082018-12-17 15:35:09 -0800318 void* data_raw = nullptr;
319 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw);
320 std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw),
321 [&buffer](auto) { buffer->unlock(); });
322 if (!data) return {};
323
Kevin DuBoisb325c932019-05-21 08:34:09 -0700324 const int32_t width = buffer->getWidth();
325 const int32_t height = buffer->getHeight();
Dan Stozaec460082018-12-17 15:35:09 -0800326 const int32_t stride = buffer->getStride();
327 std::vector<float> lumas(descriptors.size());
328 std::transform(descriptors.begin(), descriptors.end(), lumas.begin(),
329 [&](auto const& descriptor) {
Kevin DuBoisb325c932019-05-21 08:34:09 -0700330 return sampleArea(data.get(), width, height, stride, orientation,
331 descriptor.area - leftTop);
Dan Stozaec460082018-12-17 15:35:09 -0800332 });
333 return lumas;
334}
335
336void RegionSamplingThread::captureSample() {
337 ATRACE_CALL();
Kevin DuBois26afc782019-05-06 16:46:45 -0700338 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800339
340 if (mDescriptors.empty()) {
341 return;
342 }
343
Kevin DuBoisb325c932019-05-21 08:34:09 -0700344 const auto device = mFlinger.getDefaultDisplayDevice();
Dominik Laskowski718f9602019-11-09 20:01:35 -0800345 const auto orientation = ui::Transform::toRotationFlags(device->getOrientation());
Kevin DuBoisb325c932019-05-21 08:34:09 -0700346
Dan Stozaec460082018-12-17 15:35:09 -0800347 std::vector<RegionSamplingThread::Descriptor> descriptors;
348 Region sampleRegion;
349 for (const auto& [listener, descriptor] : mDescriptors) {
350 sampleRegion.orSelf(descriptor.area);
351 descriptors.emplace_back(descriptor);
352 }
353
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800354 const Rect sampledArea = sampleRegion.bounds();
Dan Stozaec460082018-12-17 15:35:09 -0800355
Kevin DuBoisb325c932019-05-21 08:34:09 -0700356 auto dx = 0;
357 auto dy = 0;
358 switch (orientation) {
359 case ui::Transform::ROT_90:
360 dx = device->getWidth();
361 break;
362 case ui::Transform::ROT_180:
363 dx = device->getWidth();
364 dy = device->getHeight();
365 break;
366 case ui::Transform::ROT_270:
367 dy = device->getHeight();
368 break;
369 default:
370 break;
371 }
372
373 ui::Transform t(orientation);
374 auto screencapRegion = t.transform(sampleRegion);
375 screencapRegion = screencapRegion.translate(dx, dy);
376 DisplayRenderArea renderArea(device, screencapRegion.bounds(), sampledArea.getWidth(),
377 sampledArea.getHeight(), ui::Dataspace::V0_SRGB, orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800378
379 std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners;
380
381 auto traverseLayers = [&](const LayerVector::Visitor& visitor) {
382 bool stopLayerFound = false;
383 auto filterVisitor = [&](Layer* layer) {
384 // We don't want to capture any layers beyond the stop layer
385 if (stopLayerFound) return;
386
387 // Likewise if we just found a stop layer, set the flag and abort
388 for (const auto& [area, stopLayer, listener] : descriptors) {
389 if (layer == stopLayer.promote().get()) {
390 stopLayerFound = true;
391 return;
392 }
393 }
394
395 // Compute the layer's position on the screen
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800396 const Rect bounds = Rect(layer->getBounds());
397 const ui::Transform transform = layer->getTransform();
Dan Stozaec460082018-12-17 15:35:09 -0800398 constexpr bool roundOutwards = true;
399 Rect transformed = transform.transform(bounds, roundOutwards);
400
401 // If this layer doesn't intersect with the larger sampledArea, skip capturing it
402 Rect ignore;
403 if (!transformed.intersect(sampledArea, &ignore)) return;
404
405 // If the layer doesn't intersect a sampling area, skip capturing it
406 bool intersectsAnyArea = false;
407 for (const auto& [area, stopLayer, listener] : descriptors) {
408 if (transformed.intersect(area, &ignore)) {
409 intersectsAnyArea = true;
410 listeners.insert(listener);
411 }
412 }
413 if (!intersectsAnyArea) return;
414
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700415 ALOGV("Traversing [%s] [%d, %d, %d, %d]", layer->getDebugName(), bounds.left,
Dan Stozaec460082018-12-17 15:35:09 -0800416 bounds.top, bounds.right, bounds.bottom);
417 visitor(layer);
418 };
419 mFlinger.traverseLayersInDisplay(device, filterVisitor);
420 };
421
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700422 sp<GraphicBuffer> buffer = nullptr;
423 if (mCachedBuffer && mCachedBuffer->getWidth() == sampledArea.getWidth() &&
424 mCachedBuffer->getHeight() == sampledArea.getHeight()) {
425 buffer = mCachedBuffer;
426 } else {
427 const uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
428 buffer = new GraphicBuffer(sampledArea.getWidth(), sampledArea.getHeight(),
429 PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread");
430 }
Dan Stozaec460082018-12-17 15:35:09 -0800431
Robert Carr108b2c72019-04-02 16:32:58 -0700432 bool ignored;
433 mFlinger.captureScreenCommon(renderArea, traverseLayers, buffer, false, ignored);
Dan Stozaec460082018-12-17 15:35:09 -0800434
435 std::vector<Descriptor> activeDescriptors;
436 for (const auto& descriptor : descriptors) {
437 if (listeners.count(descriptor.listener) != 0) {
438 activeDescriptors.emplace_back(descriptor);
439 }
440 }
441
442 ALOGV("Sampling %zu descriptors", activeDescriptors.size());
Kevin DuBoisb325c932019-05-21 08:34:09 -0700443 std::vector<float> lumas =
444 sampleBuffer(buffer, sampledArea.leftTop(), activeDescriptors, orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800445 if (lumas.size() != activeDescriptors.size()) {
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800446 ALOGW("collected %zu median luma values for %zu descriptors", lumas.size(),
447 activeDescriptors.size());
Dan Stozaec460082018-12-17 15:35:09 -0800448 return;
449 }
450
451 for (size_t d = 0; d < activeDescriptors.size(); ++d) {
452 activeDescriptors[d].listener->onSampleCollected(lumas[d]);
453 }
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700454
455 // Extend the lifetime of mCachedBuffer from the previous frame to here to ensure that:
456 // 1) The region sampling thread is the last owner of the buffer, and the freeing of the buffer
457 // happens in this thread, as opposed to the main thread.
458 // 2) The listener(s) receive their notifications prior to freeing the buffer.
459 mCachedBuffer = buffer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800460 ATRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::noWorkNeeded));
Dan Stozaec460082018-12-17 15:35:09 -0800461}
462
Kevin DuBois26afc782019-05-06 16:46:45 -0700463// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
464void RegionSamplingThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
465 std::unique_lock<std::mutex> lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800466 while (mRunning) {
467 if (mSampleRequested) {
468 mSampleRequested = false;
Kevin DuBois26afc782019-05-06 16:46:45 -0700469 lock.unlock();
Dan Stozaec460082018-12-17 15:35:09 -0800470 captureSample();
Kevin DuBois26afc782019-05-06 16:46:45 -0700471 lock.lock();
Dan Stozaec460082018-12-17 15:35:09 -0800472 }
Kevin DuBois26afc782019-05-06 16:46:45 -0700473 mCondition.wait(lock, [this]() REQUIRES(mThreadControlMutex) {
474 return mSampleRequested || !mRunning;
475 });
Dan Stozaec460082018-12-17 15:35:09 -0800476 }
477}
478
479} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800480
481// TODO(b/129481165): remove the #pragma below and fix conversion issues
482#pragma clang diagnostic pop // ignored "-Wconversion"