blob: d4e57bfc7b34a6601b2f0d4d631e52a59a388cce [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
Kevin DuBois413287f2019-02-25 08:46:47 -080019#include <chrono>
Dan Stozaec460082018-12-17 15:35:09 -080020#include <condition_variable>
21#include <mutex>
22#include <thread>
23#include <unordered_map>
24
25#include <android-base/thread_annotations.h>
26#include <binder/IBinder.h>
27#include <ui/Rect.h>
28#include <utils/StrongPointer.h>
Kevin DuBois413287f2019-02-25 08:46:47 -080029#include "Scheduler/IdleTimer.h"
Dan Stozaec460082018-12-17 15:35:09 -080030
31namespace android {
32
33class GraphicBuffer;
34class IRegionSamplingListener;
35class Layer;
Kevin DuBois413287f2019-02-25 08:46:47 -080036class Scheduler;
Dan Stozaec460082018-12-17 15:35:09 -080037class SurfaceFlinger;
Kevin DuBois413287f2019-02-25 08:46:47 -080038struct SamplingOffsetCallback;
Dan Stozaec460082018-12-17 15:35:09 -080039
40class RegionSamplingThread : public IBinder::DeathRecipient {
41public:
Kevin DuBois413287f2019-02-25 08:46:47 -080042 struct TimingTunables {
43 // debug.sf.sampling_offset_ns
44 // When asynchronously collecting sample, the offset, from zero phase in the vsync timeline
45 // at which the sampling should start.
46 std::chrono::nanoseconds mSamplingOffset;
47 // debug.sf.sampling_period_ns
48 // This is the maximum amount of time the luma recieving client
49 // should have to wait for a new luma value after a frame is updated. The inverse of this is
50 // roughly the sampling rate. Sampling system rounds up sub-vsync sampling period to vsync
51 // period.
52 std::chrono::nanoseconds mSamplingPeriod;
53 // debug.sf.sampling_timer_timeout_ns
54 // This is the interval at which the luma sampling system will check that the luma clients
55 // have up to date information. It defaults to the mSamplingPeriod.
56 std::chrono::nanoseconds mSamplingTimerTimeout;
57 };
58 struct EnvironmentTimingTunables : TimingTunables {
59 EnvironmentTimingTunables();
60 };
61 explicit RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler,
62 const TimingTunables& tunables);
63 explicit RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler);
64
Dan Stozaec460082018-12-17 15:35:09 -080065 ~RegionSamplingThread();
66
Kevin DuBois7cbcc372019-02-25 14:53:28 -080067 // Add a listener to receive luma notifications. The luma reported via listener will
68 // report the median luma for the layers under the stopLayerHandle, in the samplingArea region.
Dan Stozaec460082018-12-17 15:35:09 -080069 void addListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
70 const sp<IRegionSamplingListener>& listener);
Kevin DuBois7cbcc372019-02-25 14:53:28 -080071 // Remove the listener to stop receiving median luma notifications.
Dan Stozaec460082018-12-17 15:35:09 -080072 void removeListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois413287f2019-02-25 08:46:47 -080073
74 // Notifies sampling engine that new content is available. This will trigger a sampling
75 // pass at some point in the future.
76 void notifyNewContent();
77
78 // Notifies the sampling engine that it has a good timing window in which to sample.
79 void notifySamplingOffset();
Dan Stozaec460082018-12-17 15:35:09 -080080
Kevin DuBois7cbcc372019-02-25 14:53:28 -080081private:
Dan Stozaec460082018-12-17 15:35:09 -080082 struct Descriptor {
83 Rect area = Rect::EMPTY_RECT;
84 wp<Layer> stopLayer;
85 sp<IRegionSamplingListener> listener;
86 };
87
88 struct WpHash {
89 size_t operator()(const wp<IBinder>& p) const {
90 return std::hash<IBinder*>()(p.unsafe_get());
91 }
92 };
Kevin DuBois7cbcc372019-02-25 14:53:28 -080093 std::vector<float> sampleBuffer(
94 const sp<GraphicBuffer>& buffer, const Point& leftTop,
95 const std::vector<RegionSamplingThread::Descriptor>& descriptors);
Dan Stozaec460082018-12-17 15:35:09 -080096
Kevin DuBois413287f2019-02-25 08:46:47 -080097 void doSample();
Dan Stozaec460082018-12-17 15:35:09 -080098 void binderDied(const wp<IBinder>& who) override;
Kevin DuBois413287f2019-02-25 08:46:47 -080099 void checkForStaleLuma();
Dan Stozaec460082018-12-17 15:35:09 -0800100
101 void captureSample() REQUIRES(mMutex);
102 void threadMain();
103
104 SurfaceFlinger& mFlinger;
Kevin DuBois413287f2019-02-25 08:46:47 -0800105 Scheduler& mScheduler;
106 const TimingTunables mTunables;
107 scheduler::IdleTimer mIdleTimer;
108
109 std::unique_ptr<SamplingOffsetCallback> const mPhaseCallback;
Dan Stozaec460082018-12-17 15:35:09 -0800110
111 std::mutex mThreadMutex;
112 std::thread mThread GUARDED_BY(mThreadMutex);
113
114 std::mutex mMutex;
115 std::condition_variable_any mCondition;
116 bool mRunning GUARDED_BY(mMutex) = true;
117 bool mSampleRequested GUARDED_BY(mMutex) = false;
118
119 std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mMutex);
Kevin DuBois413287f2019-02-25 08:46:47 -0800120 std::chrono::nanoseconds lastSampleTime GUARDED_BY(mMutex);
121 bool mDiscardedFrames GUARDED_BY(mMutex) = false;
Dan Stozaec460082018-12-17 15:35:09 -0800122};
123
124} // namespace android