blob: 615492a872b4016bc44956dabaae80c60bf9ceef [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
Vishnu Nairbe0ad902024-06-27 23:38:43 +000029#include <common/trace.h>
Kevin DuBoisb325c932019-05-21 08:34:09 -070030#include <compositionengine/Display.h>
31#include <compositionengine/impl/OutputCompositionState.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070032#include <cutils/properties.h>
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -080033#include <ftl/future.h>
Huihong Luoa339d0a2022-02-05 09:42:42 -080034#include <gui/SpHash.h>
chaviwe7b9f272020-08-18 16:08:59 -070035#include <gui/SyncScreenCaptureListener.h>
Vishnu Nairdbbe3852022-01-12 20:22:11 -080036#include <renderengine/impl/ExternalTexture.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070037#include <ui/DisplayStatInfo.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070038
39#include <string>
40
Dan Stozaec460082018-12-17 15:35:09 -080041#include "DisplayDevice.h"
Vishnu Nair9d77a5c2022-10-28 06:31:21 +000042#include "FrontEnd/LayerCreationArgs.h"
Dan Stozaec460082018-12-17 15:35:09 -080043#include "Layer.h"
44#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
Huihong Luoa339d0a2022-02-05 09:42:42 -080049using gui::SpHash;
Dan Stozaec460082018-12-17 15:35:09 -080050
Kevin DuBois413287f2019-02-25 08:46:47 -080051constexpr auto lumaSamplingStepTag = "LumaSamplingStep";
52enum class samplingStep {
53 noWorkNeeded,
54 idleTimerWaiting,
John Dias84be7832019-06-18 17:05:26 -070055 waitForQuietFrame,
Kevin DuBois413287f2019-02-25 08:46:47 -080056 waitForSamplePhase,
57 sample
58};
59
Ady Abraham9c53ee72020-07-22 21:16:18 -070060constexpr auto defaultRegionSamplingWorkDuration = 3ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080061constexpr auto defaultRegionSamplingPeriod = 100ms;
62constexpr auto defaultRegionSamplingTimerTimeout = 100ms;
Ady Abraham562c2712021-05-07 15:10:42 -070063constexpr auto maxRegionSamplingDelay = 100ms;
Kevin DuBois413287f2019-02-25 08:46:47 -080064// TODO: (b/127403193) duration to string conversion could probably be constexpr
65template <typename Rep, typename Per>
66inline std::string toNsString(std::chrono::duration<Rep, Per> t) {
67 return std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(t).count());
Dan Stozaec460082018-12-17 15:35:09 -080068}
69
Kevin DuBois413287f2019-02-25 08:46:47 -080070RegionSamplingThread::EnvironmentTimingTunables::EnvironmentTimingTunables() {
71 char value[PROPERTY_VALUE_MAX] = {};
72
Ady Abraham9c53ee72020-07-22 21:16:18 -070073 property_get("debug.sf.region_sampling_duration_ns", value,
74 toNsString(defaultRegionSamplingWorkDuration).c_str());
75 int const samplingDurationNsRaw = atoi(value);
Kevin DuBois413287f2019-02-25 08:46:47 -080076
77 property_get("debug.sf.region_sampling_period_ns", value,
78 toNsString(defaultRegionSamplingPeriod).c_str());
79 int const samplingPeriodNsRaw = atoi(value);
80
81 property_get("debug.sf.region_sampling_timer_timeout_ns", value,
82 toNsString(defaultRegionSamplingTimerTimeout).c_str());
83 int const samplingTimerTimeoutNsRaw = atoi(value);
84
85 if ((samplingPeriodNsRaw < 0) || (samplingTimerTimeoutNsRaw < 0)) {
86 ALOGW("User-specified sampling tuning options nonsensical. Using defaults");
Ady Abraham9c53ee72020-07-22 21:16:18 -070087 mSamplingDuration = defaultRegionSamplingWorkDuration;
Kevin DuBois413287f2019-02-25 08:46:47 -080088 mSamplingPeriod = defaultRegionSamplingPeriod;
89 mSamplingTimerTimeout = defaultRegionSamplingTimerTimeout;
90 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070091 mSamplingDuration = std::chrono::nanoseconds(samplingDurationNsRaw);
Kevin DuBois413287f2019-02-25 08:46:47 -080092 mSamplingPeriod = std::chrono::nanoseconds(samplingPeriodNsRaw);
93 mSamplingTimerTimeout = std::chrono::nanoseconds(samplingTimerTimeoutNsRaw);
94 }
95}
96
Ady Abraham562c2712021-05-07 15:10:42 -070097RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables)
Kevin DuBois413287f2019-02-25 08:46:47 -080098 : mFlinger(flinger),
Kevin DuBois413287f2019-02-25 08:46:47 -080099 mTunables(tunables),
Ady Abraham9c53ee72020-07-22 21:16:18 -0700100 mIdleTimer(
Ady Abrahamdde984b2021-03-18 12:47:36 -0700101 "RegSampIdle",
Ady Abraham9c53ee72020-07-22 21:16:18 -0700102 std::chrono::duration_cast<std::chrono::milliseconds>(
103 mTunables.mSamplingTimerTimeout),
104 [] {}, [this] { checkForStaleLuma(); }),
Ady Abraham562c2712021-05-07 15:10:42 -0700105 mLastSampleTime(0ns) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700106 mThread = std::thread([this]() { threadMain(); });
Ady Abrahamdde984b2021-03-18 12:47:36 -0700107 pthread_setname_np(mThread.native_handle(), "RegionSampling");
Kevin DuBois413287f2019-02-25 08:46:47 -0800108 mIdleTimer.start();
109}
110
Ady Abraham562c2712021-05-07 15:10:42 -0700111RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger)
112 : RegionSamplingThread(flinger,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 TimingTunables{defaultRegionSamplingWorkDuration,
Kevin DuBois413287f2019-02-25 08:46:47 -0800114 defaultRegionSamplingPeriod,
115 defaultRegionSamplingTimerTimeout}) {}
116
Dan Stozaec460082018-12-17 15:35:09 -0800117RegionSamplingThread::~RegionSamplingThread() {
Kevin DuBois413287f2019-02-25 08:46:47 -0800118 mIdleTimer.stop();
119
Dan Stozaec460082018-12-17 15:35:09 -0800120 {
Kevin DuBois26afc782019-05-06 16:46:45 -0700121 std::lock_guard lock(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800122 mRunning = false;
123 mCondition.notify_one();
124 }
125
Dan Stozaec460082018-12-17 15:35:09 -0800126 if (mThread.joinable()) {
127 mThread.join();
128 }
129}
130
Vishnu Nair9d77a5c2022-10-28 06:31:21 +0000131void RegionSamplingThread::addListener(const Rect& samplingArea, uint32_t stopLayerId,
Dan Stozaec460082018-12-17 15:35:09 -0800132 const sp<IRegionSamplingListener>& listener) {
Dan Stozaec460082018-12-17 15:35:09 -0800133 sp<IBinder> asBinder = IInterface::asBinder(listener);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700134 asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
Kevin DuBois26afc782019-05-06 16:46:45 -0700135 std::lock_guard lock(mSamplingMutex);
Vishnu Nair9d77a5c2022-10-28 06:31:21 +0000136 mDescriptors.emplace(wp<IBinder>(asBinder), Descriptor{samplingArea, stopLayerId, listener});
Dan Stozaec460082018-12-17 15:35:09 -0800137}
138
139void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& listener) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700140 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800141 mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener)));
142}
143
Kevin DuBois413287f2019-02-25 08:46:47 -0800144void RegionSamplingThread::checkForStaleLuma() {
Kevin DuBois26afc782019-05-06 16:46:45 -0700145 std::lock_guard lock(mThreadControlMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800146
Ady Abraham562c2712021-05-07 15:10:42 -0700147 if (mSampleRequestTime.has_value()) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000148 SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForSamplePhase));
Ady Abraham562c2712021-05-07 15:10:42 -0700149 mSampleRequestTime.reset();
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700150 mFlinger.scheduleSample();
Kevin DuBois413287f2019-02-25 08:46:47 -0800151 }
152}
153
Ady Abraham562c2712021-05-07 15:10:42 -0700154void RegionSamplingThread::onCompositionComplete(
155 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
156 doSample(samplingDeadline);
Kevin DuBois413287f2019-02-25 08:46:47 -0800157}
158
Ady Abraham562c2712021-05-07 15:10:42 -0700159void RegionSamplingThread::doSample(
160 std::optional<std::chrono::steady_clock::time_point> samplingDeadline) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700161 std::lock_guard lock(mThreadControlMutex);
Ady Abraham562c2712021-05-07 15:10:42 -0700162 const auto now = std::chrono::steady_clock::now();
163 if (mLastSampleTime + mTunables.mSamplingPeriod > now) {
164 // content changed, but we sampled not too long ago, so we need to sample some time in the
165 // future.
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000166 SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::idleTimerWaiting));
Ady Abraham562c2712021-05-07 15:10:42 -0700167 mSampleRequestTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800168 return;
169 }
Ady Abraham562c2712021-05-07 15:10:42 -0700170 if (!mSampleRequestTime.has_value() || now - *mSampleRequestTime < maxRegionSamplingDelay) {
John Dias84be7832019-06-18 17:05:26 -0700171 // If there is relatively little time left for surfaceflinger
172 // until the next vsync deadline, defer this sampling work
173 // to a later frame, when hopefully there will be more time.
Ady Abraham562c2712021-05-07 15:10:42 -0700174 if (samplingDeadline.has_value() && now + mTunables.mSamplingDuration > *samplingDeadline) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000175 SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::waitForQuietFrame));
Ady Abraham562c2712021-05-07 15:10:42 -0700176 mSampleRequestTime = mSampleRequestTime.value_or(now);
John Dias84be7832019-06-18 17:05:26 -0700177 return;
178 }
179 }
Kevin DuBois413287f2019-02-25 08:46:47 -0800180
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000181 SFTRACE_INT(lumaSamplingStepTag, static_cast<int>(samplingStep::sample));
Kevin DuBois413287f2019-02-25 08:46:47 -0800182
Ady Abraham562c2712021-05-07 15:10:42 -0700183 mSampleRequestTime.reset();
184 mLastSampleTime = now;
Kevin DuBois413287f2019-02-25 08:46:47 -0800185
186 mIdleTimer.reset();
Kevin DuBois413287f2019-02-25 08:46:47 -0800187
Dan Stozaec460082018-12-17 15:35:09 -0800188 mSampleRequested = true;
189 mCondition.notify_one();
190}
191
192void RegionSamplingThread::binderDied(const wp<IBinder>& who) {
Kevin DuBois26afc782019-05-06 16:46:45 -0700193 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800194 mDescriptors.erase(who);
195}
196
Kevin DuBoisb325c932019-05-21 08:34:09 -0700197float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
198 uint32_t orientation, const Rect& sample_area) {
199 if (!sample_area.isValid() || (sample_area.getWidth() > width) ||
200 (sample_area.getHeight() > height)) {
201 ALOGE("invalid sampling region requested");
202 return 0.0f;
203 }
204
Alec Mouri439b6092022-09-14 22:24:17 +0000205 const uint32_t pixelCount =
206 (sample_area.bottom - sample_area.top) * (sample_area.right - sample_area.left);
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700207 uint32_t accumulatedLuma = 0;
Dan Stozaec460082018-12-17 15:35:09 -0800208
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700209 // Calculates luma with approximation of Rec. 709 primaries
Alec Mouri439b6092022-09-14 22:24:17 +0000210 for (int32_t row = sample_area.top; row < sample_area.bottom; ++row) {
Dan Stozaec460082018-12-17 15:35:09 -0800211 const uint32_t* rowBase = data + row * stride;
Alec Mouri439b6092022-09-14 22:24:17 +0000212 for (int32_t column = sample_area.left; column < sample_area.right; ++column) {
Dan Stozaec460082018-12-17 15:35:09 -0800213 uint32_t pixel = rowBase[column];
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700214 const uint32_t r = pixel & 0xFF;
215 const uint32_t g = (pixel >> 8) & 0xFF;
216 const uint32_t b = (pixel >> 16) & 0xFF;
217 const uint32_t luma = (r * 7 + b * 2 + g * 23) >> 5;
218 accumulatedLuma += luma;
Dan Stozaec460082018-12-17 15:35:09 -0800219 }
220 }
221
Collin Fijalkovicha95e1702019-10-28 14:46:13 -0700222 return accumulatedLuma / (255.0f * pixelCount);
Dan Stozaec460082018-12-17 15:35:09 -0800223}
Dan Stozaec460082018-12-17 15:35:09 -0800224
Kevin DuBois7cbcc372019-02-25 14:53:28 -0800225std::vector<float> RegionSamplingThread::sampleBuffer(
226 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700227 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation) {
Dan Stozaec460082018-12-17 15:35:09 -0800228 void* data_raw = nullptr;
229 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &data_raw);
230 std::shared_ptr<uint32_t> data(reinterpret_cast<uint32_t*>(data_raw),
231 [&buffer](auto) { buffer->unlock(); });
232 if (!data) return {};
233
Kevin DuBoisb325c932019-05-21 08:34:09 -0700234 const int32_t width = buffer->getWidth();
235 const int32_t height = buffer->getHeight();
Dan Stozaec460082018-12-17 15:35:09 -0800236 const int32_t stride = buffer->getStride();
237 std::vector<float> lumas(descriptors.size());
238 std::transform(descriptors.begin(), descriptors.end(), lumas.begin(),
239 [&](auto const& descriptor) {
Kevin DuBoisb325c932019-05-21 08:34:09 -0700240 return sampleArea(data.get(), width, height, stride, orientation,
241 descriptor.area - leftTop);
Dan Stozaec460082018-12-17 15:35:09 -0800242 });
243 return lumas;
244}
245
246void RegionSamplingThread::captureSample() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000247 SFTRACE_CALL();
Kevin DuBois26afc782019-05-06 16:46:45 -0700248 std::lock_guard lock(mSamplingMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800249
250 if (mDescriptors.empty()) {
251 return;
252 }
253
Marin Shalamanov1c434292020-06-12 01:47:29 +0200254 wp<const DisplayDevice> displayWeak;
255
256 ui::LayerStack layerStack;
257 ui::Transform::RotationFlags orientation;
258 ui::Size displaySize;
Melody Hsu5b3d9762025-01-28 21:22:52 +0000259 Rect layerStackSpaceRect;
Marin Shalamanov1c434292020-06-12 01:47:29 +0200260
261 {
262 // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread
263 const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice();
264 displayWeak = display;
265 layerStack = display->getLayerStack();
266 orientation = ui::Transform::toRotationFlags(display->getOrientation());
267 displaySize = display->getSize();
Melody Hsu5b3d9762025-01-28 21:22:52 +0000268 layerStackSpaceRect = display->getLayerStackSpaceRect();
Marin Shalamanov1c434292020-06-12 01:47:29 +0200269 }
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();
279
Dan Stozaec460082018-12-17 15:35:09 -0800280 std::unordered_set<sp<IRegionSamplingListener>, SpHash<IRegionSamplingListener>> listeners;
281
Vishnu Nairc5a5b6e2023-02-28 00:19:07 +0000282 auto layerFilterFn = [&](const char* layerName, uint32_t layerId, const Rect& bounds,
283 const ui::Transform transform, bool& outStopTraversal) -> bool {
284 // Likewise if we just found a stop layer, set the flag and abort
285 for (const auto& [area, stopLayerId, listener] : descriptors) {
286 if (stopLayerId != UNASSIGNED_LAYER_ID && layerId == stopLayerId) {
287 outStopTraversal = true;
288 return false;
289 }
290 }
Dan Stozaec460082018-12-17 15:35:09 -0800291
Vishnu Nairc5a5b6e2023-02-28 00:19:07 +0000292 // Compute the layer's position on the screen
293 constexpr bool roundOutwards = true;
294 Rect transformed = transform.transform(bounds, roundOutwards);
295
296 // If this layer doesn't intersect with the larger sampledBounds, skip capturing it
297 Rect ignore;
298 if (!transformed.intersect(sampledBounds, &ignore)) return false;
299
300 // If the layer doesn't intersect a sampling area, skip capturing it
301 bool intersectsAnyArea = false;
302 for (const auto& [area, stopLayer, listener] : descriptors) {
303 if (transformed.intersect(area, &ignore)) {
304 intersectsAnyArea = true;
305 listeners.insert(listener);
306 }
307 }
308 if (!intersectsAnyArea) return false;
309
310 ALOGV("Traversing [%s] [%d, %d, %d, %d]", layerName, bounds.left, bounds.top, bounds.right,
311 bounds.bottom);
312
313 return true;
314 };
315
Melody Hsueddfe932024-06-04 18:10:41 +0000316 auto filterFn = [&](const frontend::LayerSnapshot& snapshot, bool& outStopTraversal) -> bool {
317 const Rect bounds = frontend::RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
318 snapshot.transparentRegionHint);
319 const ui::Transform transform = snapshot.geomLayerTransform;
320 return layerFilterFn(snapshot.name.c_str(), snapshot.path.id, bounds, transform,
321 outStopTraversal);
322 };
323 auto getLayerSnapshotsFn =
324 mFlinger.getLayerSnapshotsForScreenshots(layerStack, CaptureArgs::UNSET_UID, filterFn);
Dan Stozaec460082018-12-17 15:35:09 -0800325
Alec Mouria90a5702021-04-16 16:36:21 +0000326 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
327 if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() &&
328 mCachedBuffer->getBuffer()->getHeight() == sampledBounds.getHeight()) {
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700329 buffer = mCachedBuffer;
330 } else {
John Reck67b1e2b2020-08-26 13:17:24 -0700331 const uint32_t usage =
332 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000333 sp<GraphicBuffer> graphicBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700334 sp<GraphicBuffer>::make(sampledBounds.getWidth(), sampledBounds.getHeight(),
335 PIXEL_FORMAT_RGBA_8888, 1, usage, "RegionSamplingThread");
Alec Mouria90a5702021-04-16 16:36:21 +0000336 const status_t bufferStatus = graphicBuffer->initCheck();
Alec Mouri7013b6f2021-02-12 11:16:54 -0800337 LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "captureSample: Buffer failed to allocate: %d",
338 bufferStatus);
Alec Mouria90a5702021-04-16 16:36:21 +0000339 buffer = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800340 renderengine::impl::ExternalTexture>(graphicBuffer, mFlinger.getRenderEngine(),
341 renderengine::impl::ExternalTexture::Usage::
342 WRITEABLE);
Kevin DuBois4efd1f52019-04-29 10:09:43 -0700343 }
Dan Stozaec460082018-12-17 15:35:09 -0800344
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700345 constexpr bool kRegionSampling = true;
346 constexpr bool kGrayscale = false;
Chavi Weingarten18fa7c62023-11-28 21:16:03 +0000347 constexpr bool kIsProtected = false;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700348
Melody Hsu5b3d9762025-01-28 21:22:52 +0000349 SurfaceFlinger::ScreenshotArgs screenshotArgs;
350 screenshotArgs.captureTypeVariant = displayWeak;
Gil Dekelde4ce292025-03-03 17:08:43 -0500351 screenshotArgs.displayIdVariant = std::nullopt;
Melody Hsu5b3d9762025-01-28 21:22:52 +0000352 screenshotArgs.sourceCrop = sampledBounds.isEmpty() ? layerStackSpaceRect : sampledBounds;
353 screenshotArgs.reqSize = sampledBounds.getSize();
354 screenshotArgs.dataspace = ui::Dataspace::V0_SRGB;
355 screenshotArgs.isSecure = true;
356 screenshotArgs.seamlessTransition = false;
Melody Hsu8c42cf12024-06-05 00:07:03 +0000357
Melody Hsu16c4c322024-10-29 07:50:14 +0000358 std::vector<std::pair<Layer*, sp<LayerFE>>> layers;
Melody Hsu36cb1ed2025-02-11 07:58:26 +0000359 mFlinger.getSnapshotsFromMainThread(screenshotArgs, getLayerSnapshotsFn, layers);
360 FenceResult fenceResult = mFlinger.captureScreenshot(screenshotArgs, buffer, kRegionSampling,
361 kGrayscale, kIsProtected, nullptr, layers)
362 .get();
Melody Hsu8c42cf12024-06-05 00:07:03 +0000363 if (fenceResult.ok()) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700364 fenceResult.value()->waitForever(LOG_TAG);
Sally Qi59a9f502021-10-12 18:53:23 +0000365 }
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;
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000388 SFTRACE_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"