blob: b62b15cb6d75af32e0bc20f372d14d8310e43c31 [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
17#pragma once
18
Alec Mouria90a5702021-04-16 16:36:21 +000019#include <android-base/thread_annotations.h>
Huihong Luoecc1f902021-11-20 11:55:05 -080020#include <android/gui/IRegionSamplingListener.h>
Alec Mouria90a5702021-04-16 16:36:21 +000021#include <binder/IBinder.h>
22#include <renderengine/ExternalTexture.h>
23#include <ui/GraphicBuffer.h>
24#include <ui/Rect.h>
25#include <utils/StrongPointer.h>
26
Kevin DuBois413287f2019-02-25 08:46:47 -080027#include <chrono>
Dan Stozaec460082018-12-17 15:35:09 -080028#include <condition_variable>
Vishnu Nair9d77a5c2022-10-28 06:31:21 +000029#include <cstdint>
Dan Stozaec460082018-12-17 15:35:09 -080030#include <mutex>
31#include <thread>
32#include <unordered_map>
33
Ana Krulecf2c006d2019-06-21 15:37:07 -070034#include "Scheduler/OneShotTimer.h"
Angel Aguayob398ee22021-10-14 00:39:15 +000035#include "WpHash.h"
Dan Stozaec460082018-12-17 15:35:09 -080036
37namespace android {
38
Dan Stozaec460082018-12-17 15:35:09 -080039class Layer;
Kevin DuBois413287f2019-02-25 08:46:47 -080040class Scheduler;
Dan Stozaec460082018-12-17 15:35:09 -080041class SurfaceFlinger;
Kevin DuBois413287f2019-02-25 08:46:47 -080042struct SamplingOffsetCallback;
Dan Stozaec460082018-12-17 15:35:09 -080043
Huihong Luoecc1f902021-11-20 11:55:05 -080044using gui::IRegionSamplingListener;
45
Kevin DuBoisb325c932019-05-21 08:34:09 -070046float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
47 uint32_t orientation, const Rect& area);
Kevin DuBoisbb27bcd2019-04-02 14:34:35 -070048
Dan Stozaec460082018-12-17 15:35:09 -080049class RegionSamplingThread : public IBinder::DeathRecipient {
50public:
Kevin DuBois413287f2019-02-25 08:46:47 -080051 struct TimingTunables {
Ady Abraham9c53ee72020-07-22 21:16:18 -070052 // debug.sf.sampling_duration_ns
53 // When asynchronously collecting sample, the duration, at which the sampling should start
54 // before a vsync
55 std::chrono::nanoseconds mSamplingDuration;
Kevin DuBois413287f2019-02-25 08:46:47 -080056 // debug.sf.sampling_period_ns
57 // This is the maximum amount of time the luma recieving client
58 // should have to wait for a new luma value after a frame is updated. The inverse of this is
59 // roughly the sampling rate. Sampling system rounds up sub-vsync sampling period to vsync
60 // period.
61 std::chrono::nanoseconds mSamplingPeriod;
62 // debug.sf.sampling_timer_timeout_ns
63 // This is the interval at which the luma sampling system will check that the luma clients
64 // have up to date information. It defaults to the mSamplingPeriod.
65 std::chrono::nanoseconds mSamplingTimerTimeout;
66 };
67 struct EnvironmentTimingTunables : TimingTunables {
68 EnvironmentTimingTunables();
69 };
Ady Abraham562c2712021-05-07 15:10:42 -070070 explicit RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables);
71 explicit RegionSamplingThread(SurfaceFlinger& flinger);
Kevin DuBois413287f2019-02-25 08:46:47 -080072
Dan Stozaec460082018-12-17 15:35:09 -080073 ~RegionSamplingThread();
74
Kevin DuBois7cbcc372019-02-25 14:53:28 -080075 // Add a listener to receive luma notifications. The luma reported via listener will
76 // report the median luma for the layers under the stopLayerHandle, in the samplingArea region.
Vishnu Nair9d77a5c2022-10-28 06:31:21 +000077 void addListener(const Rect& samplingArea, uint32_t stopLayerId,
Dan Stozaec460082018-12-17 15:35:09 -080078 const sp<IRegionSamplingListener>& listener);
Kevin DuBois7cbcc372019-02-25 14:53:28 -080079 // Remove the listener to stop receiving median luma notifications.
Dan Stozaec460082018-12-17 15:35:09 -080080 void removeListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois413287f2019-02-25 08:46:47 -080081
Ady Abraham562c2712021-05-07 15:10:42 -070082 // Notifies sampling engine that composition is done and new content is
83 // available, and the deadline for the sampling work on the main thread to
84 // be completed without eating the budget of another frame.
85 void onCompositionComplete(
86 std::optional<std::chrono::steady_clock::time_point> samplingDeadline);
Dan Stozaec460082018-12-17 15:35:09 -080087
Kevin DuBois7cbcc372019-02-25 14:53:28 -080088private:
Dan Stozaec460082018-12-17 15:35:09 -080089 struct Descriptor {
90 Rect area = Rect::EMPTY_RECT;
Vishnu Nair9d77a5c2022-10-28 06:31:21 +000091 uint32_t stopLayerId;
Dan Stozaec460082018-12-17 15:35:09 -080092 sp<IRegionSamplingListener> listener;
93 };
94
Kevin DuBois7cbcc372019-02-25 14:53:28 -080095 std::vector<float> sampleBuffer(
96 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -070097 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation);
Dan Stozaec460082018-12-17 15:35:09 -080098
Ady Abraham562c2712021-05-07 15:10:42 -070099 void doSample(std::optional<std::chrono::steady_clock::time_point> samplingDeadline);
Dan Stozaec460082018-12-17 15:35:09 -0800100 void binderDied(const wp<IBinder>& who) override;
Kevin DuBois413287f2019-02-25 08:46:47 -0800101 void checkForStaleLuma();
Dan Stozaec460082018-12-17 15:35:09 -0800102
Kevin DuBois26afc782019-05-06 16:46:45 -0700103 void captureSample();
Dan Stozaec460082018-12-17 15:35:09 -0800104 void threadMain();
105
106 SurfaceFlinger& mFlinger;
Kevin DuBois413287f2019-02-25 08:46:47 -0800107 const TimingTunables mTunables;
Ana Krulecf2c006d2019-06-21 15:37:07 -0700108 scheduler::OneShotTimer mIdleTimer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800109
Kevin DuBois26afc782019-05-06 16:46:45 -0700110 std::thread mThread;
Dan Stozaec460082018-12-17 15:35:09 -0800111
Kevin DuBois26afc782019-05-06 16:46:45 -0700112 std::mutex mThreadControlMutex;
Dan Stozaec460082018-12-17 15:35:09 -0800113 std::condition_variable_any mCondition;
Kevin DuBois26afc782019-05-06 16:46:45 -0700114 bool mRunning GUARDED_BY(mThreadControlMutex) = true;
115 bool mSampleRequested GUARDED_BY(mThreadControlMutex) = false;
Ady Abraham562c2712021-05-07 15:10:42 -0700116 std::optional<std::chrono::steady_clock::time_point> mSampleRequestTime
117 GUARDED_BY(mThreadControlMutex);
118 std::chrono::steady_clock::time_point mLastSampleTime GUARDED_BY(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800119
Kevin DuBois26afc782019-05-06 16:46:45 -0700120 std::mutex mSamplingMutex;
121 std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mSamplingMutex);
Alec Mouria90a5702021-04-16 16:36:21 +0000122 std::shared_ptr<renderengine::ExternalTexture> mCachedBuffer GUARDED_BY(mSamplingMutex) =
123 nullptr;
Dan Stozaec460082018-12-17 15:35:09 -0800124};
125
126} // namespace android