Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1 | /* |
| 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 DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 19 | #include <chrono> |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 20 | #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 DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 29 | #include "Scheduler/IdleTimer.h" |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class GraphicBuffer; |
| 34 | class IRegionSamplingListener; |
| 35 | class Layer; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 36 | class Scheduler; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 37 | class SurfaceFlinger; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 38 | struct SamplingOffsetCallback; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 39 | |
Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame^] | 40 | float sampleArea(const uint32_t* data, int32_t stride, const Rect& area); |
| 41 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 42 | class RegionSamplingThread : public IBinder::DeathRecipient { |
| 43 | public: |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 44 | struct TimingTunables { |
| 45 | // debug.sf.sampling_offset_ns |
| 46 | // When asynchronously collecting sample, the offset, from zero phase in the vsync timeline |
| 47 | // at which the sampling should start. |
| 48 | std::chrono::nanoseconds mSamplingOffset; |
| 49 | // debug.sf.sampling_period_ns |
| 50 | // This is the maximum amount of time the luma recieving client |
| 51 | // should have to wait for a new luma value after a frame is updated. The inverse of this is |
| 52 | // roughly the sampling rate. Sampling system rounds up sub-vsync sampling period to vsync |
| 53 | // period. |
| 54 | std::chrono::nanoseconds mSamplingPeriod; |
| 55 | // debug.sf.sampling_timer_timeout_ns |
| 56 | // This is the interval at which the luma sampling system will check that the luma clients |
| 57 | // have up to date information. It defaults to the mSamplingPeriod. |
| 58 | std::chrono::nanoseconds mSamplingTimerTimeout; |
| 59 | }; |
| 60 | struct EnvironmentTimingTunables : TimingTunables { |
| 61 | EnvironmentTimingTunables(); |
| 62 | }; |
| 63 | explicit RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler, |
| 64 | const TimingTunables& tunables); |
| 65 | explicit RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& scheduler); |
| 66 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 67 | ~RegionSamplingThread(); |
| 68 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 69 | // Add a listener to receive luma notifications. The luma reported via listener will |
| 70 | // report the median luma for the layers under the stopLayerHandle, in the samplingArea region. |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 71 | void addListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle, |
| 72 | const sp<IRegionSamplingListener>& listener); |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 73 | // Remove the listener to stop receiving median luma notifications. |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 74 | void removeListener(const sp<IRegionSamplingListener>& listener); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 75 | |
| 76 | // Notifies sampling engine that new content is available. This will trigger a sampling |
| 77 | // pass at some point in the future. |
| 78 | void notifyNewContent(); |
| 79 | |
| 80 | // Notifies the sampling engine that it has a good timing window in which to sample. |
| 81 | void notifySamplingOffset(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 82 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 83 | private: |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 84 | struct Descriptor { |
| 85 | Rect area = Rect::EMPTY_RECT; |
| 86 | wp<Layer> stopLayer; |
| 87 | sp<IRegionSamplingListener> listener; |
| 88 | }; |
| 89 | |
| 90 | struct WpHash { |
| 91 | size_t operator()(const wp<IBinder>& p) const { |
| 92 | return std::hash<IBinder*>()(p.unsafe_get()); |
| 93 | } |
| 94 | }; |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 95 | std::vector<float> sampleBuffer( |
| 96 | const sp<GraphicBuffer>& buffer, const Point& leftTop, |
| 97 | const std::vector<RegionSamplingThread::Descriptor>& descriptors); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 98 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 99 | void doSample(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 100 | void binderDied(const wp<IBinder>& who) override; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 101 | void checkForStaleLuma(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 102 | |
| 103 | void captureSample() REQUIRES(mMutex); |
| 104 | void threadMain(); |
| 105 | |
| 106 | SurfaceFlinger& mFlinger; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 107 | Scheduler& mScheduler; |
| 108 | const TimingTunables mTunables; |
| 109 | scheduler::IdleTimer mIdleTimer; |
| 110 | |
| 111 | std::unique_ptr<SamplingOffsetCallback> const mPhaseCallback; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 112 | |
| 113 | std::mutex mThreadMutex; |
| 114 | std::thread mThread GUARDED_BY(mThreadMutex); |
| 115 | |
| 116 | std::mutex mMutex; |
| 117 | std::condition_variable_any mCondition; |
| 118 | bool mRunning GUARDED_BY(mMutex) = true; |
| 119 | bool mSampleRequested GUARDED_BY(mMutex) = false; |
| 120 | |
| 121 | std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mMutex); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 122 | std::chrono::nanoseconds lastSampleTime GUARDED_BY(mMutex); |
| 123 | bool mDiscardedFrames GUARDED_BY(mMutex) = false; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace android |