blob: 86632db4903a3369d998e41c9e7fe87b6acdcb76 [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>
20#include <binder/IBinder.h>
21#include <renderengine/ExternalTexture.h>
22#include <ui/GraphicBuffer.h>
23#include <ui/Rect.h>
24#include <utils/StrongPointer.h>
25
Kevin DuBois413287f2019-02-25 08:46:47 -080026#include <chrono>
Dan Stozaec460082018-12-17 15:35:09 -080027#include <condition_variable>
28#include <mutex>
29#include <thread>
30#include <unordered_map>
31
Ana Krulecf2c006d2019-06-21 15:37:07 -070032#include "Scheduler/OneShotTimer.h"
Dan Stozaec460082018-12-17 15:35:09 -080033
34namespace android {
35
Dan Stozaec460082018-12-17 15:35:09 -080036class IRegionSamplingListener;
37class Layer;
Kevin DuBois413287f2019-02-25 08:46:47 -080038class Scheduler;
Dan Stozaec460082018-12-17 15:35:09 -080039class SurfaceFlinger;
Kevin DuBois413287f2019-02-25 08:46:47 -080040struct SamplingOffsetCallback;
Dan Stozaec460082018-12-17 15:35:09 -080041
Kevin DuBoisb325c932019-05-21 08:34:09 -070042float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride,
43 uint32_t orientation, const Rect& area);
Kevin DuBoisbb27bcd2019-04-02 14:34:35 -070044
Dan Stozaec460082018-12-17 15:35:09 -080045class RegionSamplingThread : public IBinder::DeathRecipient {
46public:
Kevin DuBois413287f2019-02-25 08:46:47 -080047 struct TimingTunables {
Ady Abraham9c53ee72020-07-22 21:16:18 -070048 // debug.sf.sampling_duration_ns
49 // When asynchronously collecting sample, the duration, at which the sampling should start
50 // before a vsync
51 std::chrono::nanoseconds mSamplingDuration;
Kevin DuBois413287f2019-02-25 08:46:47 -080052 // debug.sf.sampling_period_ns
53 // This is the maximum amount of time the luma recieving client
54 // should have to wait for a new luma value after a frame is updated. The inverse of this is
55 // roughly the sampling rate. Sampling system rounds up sub-vsync sampling period to vsync
56 // period.
57 std::chrono::nanoseconds mSamplingPeriod;
58 // debug.sf.sampling_timer_timeout_ns
59 // This is the interval at which the luma sampling system will check that the luma clients
60 // have up to date information. It defaults to the mSamplingPeriod.
61 std::chrono::nanoseconds mSamplingTimerTimeout;
62 };
63 struct EnvironmentTimingTunables : TimingTunables {
64 EnvironmentTimingTunables();
65 };
66 explicit RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler,
67 const TimingTunables& tunables);
68 explicit RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler);
69
Dan Stozaec460082018-12-17 15:35:09 -080070 ~RegionSamplingThread();
71
Kevin DuBois7cbcc372019-02-25 14:53:28 -080072 // Add a listener to receive luma notifications. The luma reported via listener will
73 // report the median luma for the layers under the stopLayerHandle, in the samplingArea region.
Alec Mouri9a02eda2020-04-21 17:39:34 -070074 void addListener(const Rect& samplingArea, const wp<Layer>& stopLayer,
Dan Stozaec460082018-12-17 15:35:09 -080075 const sp<IRegionSamplingListener>& listener);
Kevin DuBois7cbcc372019-02-25 14:53:28 -080076 // Remove the listener to stop receiving median luma notifications.
Dan Stozaec460082018-12-17 15:35:09 -080077 void removeListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois413287f2019-02-25 08:46:47 -080078
79 // Notifies sampling engine that new content is available. This will trigger a sampling
80 // pass at some point in the future.
81 void notifyNewContent();
82
83 // Notifies the sampling engine that it has a good timing window in which to sample.
84 void notifySamplingOffset();
Dan Stozaec460082018-12-17 15:35:09 -080085
Kevin DuBois7cbcc372019-02-25 14:53:28 -080086private:
Dan Stozaec460082018-12-17 15:35:09 -080087 struct Descriptor {
88 Rect area = Rect::EMPTY_RECT;
89 wp<Layer> stopLayer;
90 sp<IRegionSamplingListener> listener;
91 };
92
93 struct WpHash {
94 size_t operator()(const wp<IBinder>& p) const {
95 return std::hash<IBinder*>()(p.unsafe_get());
96 }
97 };
Kevin DuBois7cbcc372019-02-25 14:53:28 -080098 std::vector<float> sampleBuffer(
99 const sp<GraphicBuffer>& buffer, const Point& leftTop,
Kevin DuBoisb325c932019-05-21 08:34:09 -0700100 const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation);
Dan Stozaec460082018-12-17 15:35:09 -0800101
Kevin DuBois413287f2019-02-25 08:46:47 -0800102 void doSample();
Dan Stozaec460082018-12-17 15:35:09 -0800103 void binderDied(const wp<IBinder>& who) override;
Kevin DuBois413287f2019-02-25 08:46:47 -0800104 void checkForStaleLuma();
Dan Stozaec460082018-12-17 15:35:09 -0800105
Kevin DuBois26afc782019-05-06 16:46:45 -0700106 void captureSample();
Dan Stozaec460082018-12-17 15:35:09 -0800107 void threadMain();
108
109 SurfaceFlinger& mFlinger;
Kevin DuBois413287f2019-02-25 08:46:47 -0800110 Scheduler& mScheduler;
111 const TimingTunables mTunables;
Ana Krulecf2c006d2019-06-21 15:37:07 -0700112 scheduler::OneShotTimer mIdleTimer;
Kevin DuBois413287f2019-02-25 08:46:47 -0800113
114 std::unique_ptr<SamplingOffsetCallback> const mPhaseCallback;
Dan Stozaec460082018-12-17 15:35:09 -0800115
Kevin DuBois26afc782019-05-06 16:46:45 -0700116 std::thread mThread;
Dan Stozaec460082018-12-17 15:35:09 -0800117
Kevin DuBois26afc782019-05-06 16:46:45 -0700118 std::mutex mThreadControlMutex;
Dan Stozaec460082018-12-17 15:35:09 -0800119 std::condition_variable_any mCondition;
Kevin DuBois26afc782019-05-06 16:46:45 -0700120 bool mRunning GUARDED_BY(mThreadControlMutex) = true;
121 bool mSampleRequested GUARDED_BY(mThreadControlMutex) = false;
John Dias84be7832019-06-18 17:05:26 -0700122 uint32_t mDiscardedFrames GUARDED_BY(mThreadControlMutex) = 0;
Kevin DuBois26afc782019-05-06 16:46:45 -0700123 std::chrono::nanoseconds lastSampleTime GUARDED_BY(mThreadControlMutex);
Dan Stozaec460082018-12-17 15:35:09 -0800124
Kevin DuBois26afc782019-05-06 16:46:45 -0700125 std::mutex mSamplingMutex;
126 std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mSamplingMutex);
Alec Mouria90a5702021-04-16 16:36:21 +0000127 std::shared_ptr<renderengine::ExternalTexture> mCachedBuffer GUARDED_BY(mSamplingMutex) =
128 nullptr;
Dan Stozaec460082018-12-17 15:35:09 -0800129};
130
131} // namespace android