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 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 19 | #include <android-base/thread_annotations.h> |
Huihong Luo | ecc1f90 | 2021-11-20 11:55:05 -0800 | [diff] [blame] | 20 | #include <android/gui/IRegionSamplingListener.h> |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 21 | #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 DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 27 | #include <chrono> |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 28 | #include <condition_variable> |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 29 | #include <cstdint> |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 30 | #include <mutex> |
| 31 | #include <thread> |
| 32 | #include <unordered_map> |
| 33 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 34 | #include "Scheduler/OneShotTimer.h" |
Angel Aguayo | b398ee2 | 2021-10-14 00:39:15 +0000 | [diff] [blame] | 35 | #include "WpHash.h" |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 39 | class Layer; |
| 40 | class SurfaceFlinger; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 41 | struct SamplingOffsetCallback; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 42 | |
Huihong Luo | ecc1f90 | 2021-11-20 11:55:05 -0800 | [diff] [blame] | 43 | using gui::IRegionSamplingListener; |
| 44 | |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 45 | float sampleArea(const uint32_t* data, int32_t width, int32_t height, int32_t stride, |
| 46 | uint32_t orientation, const Rect& area); |
Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 47 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 48 | class RegionSamplingThread : public IBinder::DeathRecipient { |
| 49 | public: |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 50 | struct TimingTunables { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 51 | // debug.sf.sampling_duration_ns |
| 52 | // When asynchronously collecting sample, the duration, at which the sampling should start |
| 53 | // before a vsync |
| 54 | std::chrono::nanoseconds mSamplingDuration; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 55 | // debug.sf.sampling_period_ns |
| 56 | // This is the maximum amount of time the luma recieving client |
| 57 | // should have to wait for a new luma value after a frame is updated. The inverse of this is |
| 58 | // roughly the sampling rate. Sampling system rounds up sub-vsync sampling period to vsync |
| 59 | // period. |
| 60 | std::chrono::nanoseconds mSamplingPeriod; |
| 61 | // debug.sf.sampling_timer_timeout_ns |
| 62 | // This is the interval at which the luma sampling system will check that the luma clients |
| 63 | // have up to date information. It defaults to the mSamplingPeriod. |
| 64 | std::chrono::nanoseconds mSamplingTimerTimeout; |
| 65 | }; |
| 66 | struct EnvironmentTimingTunables : TimingTunables { |
| 67 | EnvironmentTimingTunables(); |
| 68 | }; |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 69 | explicit RegionSamplingThread(SurfaceFlinger& flinger, const TimingTunables& tunables); |
| 70 | explicit RegionSamplingThread(SurfaceFlinger& flinger); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 71 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 72 | ~RegionSamplingThread(); |
| 73 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 74 | // Add a listener to receive luma notifications. The luma reported via listener will |
| 75 | // report the median luma for the layers under the stopLayerHandle, in the samplingArea region. |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 76 | void addListener(const Rect& samplingArea, uint32_t stopLayerId, |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 77 | const sp<IRegionSamplingListener>& listener); |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 78 | // Remove the listener to stop receiving median luma notifications. |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 79 | void removeListener(const sp<IRegionSamplingListener>& listener); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 80 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 81 | // Notifies sampling engine that composition is done and new content is |
| 82 | // available, and the deadline for the sampling work on the main thread to |
| 83 | // be completed without eating the budget of another frame. |
| 84 | void onCompositionComplete( |
| 85 | std::optional<std::chrono::steady_clock::time_point> samplingDeadline); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 86 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 87 | private: |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 88 | struct Descriptor { |
| 89 | Rect area = Rect::EMPTY_RECT; |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 90 | uint32_t stopLayerId; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 91 | sp<IRegionSamplingListener> listener; |
| 92 | }; |
| 93 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 94 | std::vector<float> sampleBuffer( |
| 95 | const sp<GraphicBuffer>& buffer, const Point& leftTop, |
Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 96 | const std::vector<RegionSamplingThread::Descriptor>& descriptors, uint32_t orientation); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 97 | |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 98 | void doSample(std::optional<std::chrono::steady_clock::time_point> samplingDeadline); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 99 | void binderDied(const wp<IBinder>& who) override; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 100 | void checkForStaleLuma(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 101 | |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 102 | void captureSample(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 103 | void threadMain(); |
| 104 | |
| 105 | SurfaceFlinger& mFlinger; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 106 | const TimingTunables mTunables; |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 107 | scheduler::OneShotTimer mIdleTimer; |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 108 | |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 109 | std::thread mThread; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 110 | |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 111 | std::mutex mThreadControlMutex; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 112 | std::condition_variable_any mCondition; |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 113 | bool mRunning GUARDED_BY(mThreadControlMutex) = true; |
| 114 | bool mSampleRequested GUARDED_BY(mThreadControlMutex) = false; |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 115 | std::optional<std::chrono::steady_clock::time_point> mSampleRequestTime |
| 116 | GUARDED_BY(mThreadControlMutex); |
| 117 | std::chrono::steady_clock::time_point mLastSampleTime GUARDED_BY(mThreadControlMutex); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 118 | |
Kevin DuBois | 26afc78 | 2019-05-06 16:46:45 -0700 | [diff] [blame] | 119 | std::mutex mSamplingMutex; |
| 120 | std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mSamplingMutex); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 121 | std::shared_ptr<renderengine::ExternalTexture> mCachedBuffer GUARDED_BY(mSamplingMutex) = |
| 122 | nullptr; |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace android |