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 | |
| 19 | #include <condition_variable> |
| 20 | #include <mutex> |
| 21 | #include <thread> |
| 22 | #include <unordered_map> |
| 23 | |
| 24 | #include <android-base/thread_annotations.h> |
| 25 | #include <binder/IBinder.h> |
| 26 | #include <ui/Rect.h> |
| 27 | #include <utils/StrongPointer.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class GraphicBuffer; |
| 32 | class IRegionSamplingListener; |
| 33 | class Layer; |
| 34 | class SurfaceFlinger; |
| 35 | |
| 36 | class RegionSamplingThread : public IBinder::DeathRecipient { |
| 37 | public: |
| 38 | explicit RegionSamplingThread(SurfaceFlinger& flinger); |
| 39 | ~RegionSamplingThread(); |
| 40 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 41 | // Add a listener to receive luma notifications. The luma reported via listener will |
| 42 | // 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] | 43 | void addListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle, |
| 44 | const sp<IRegionSamplingListener>& listener); |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 45 | // Remove the listener to stop receiving median luma notifications. |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 46 | void removeListener(const sp<IRegionSamplingListener>& listener); |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 47 | // Instruct the thread to perform a median luma sampling on the layers. |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 48 | void sampleNow(); |
| 49 | |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 50 | private: |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 51 | struct Descriptor { |
| 52 | Rect area = Rect::EMPTY_RECT; |
| 53 | wp<Layer> stopLayer; |
| 54 | sp<IRegionSamplingListener> listener; |
| 55 | }; |
| 56 | |
| 57 | struct WpHash { |
| 58 | size_t operator()(const wp<IBinder>& p) const { |
| 59 | return std::hash<IBinder*>()(p.unsafe_get()); |
| 60 | } |
| 61 | }; |
Kevin DuBois | 7cbcc37 | 2019-02-25 14:53:28 -0800 | [diff] [blame] | 62 | std::vector<float> sampleBuffer( |
| 63 | const sp<GraphicBuffer>& buffer, const Point& leftTop, |
| 64 | const std::vector<RegionSamplingThread::Descriptor>& descriptors); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 65 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 66 | void binderDied(const wp<IBinder>& who) override; |
| 67 | |
| 68 | void captureSample() REQUIRES(mMutex); |
| 69 | void threadMain(); |
| 70 | |
| 71 | SurfaceFlinger& mFlinger; |
| 72 | |
| 73 | std::mutex mThreadMutex; |
| 74 | std::thread mThread GUARDED_BY(mThreadMutex); |
| 75 | |
| 76 | std::mutex mMutex; |
| 77 | std::condition_variable_any mCondition; |
| 78 | bool mRunning GUARDED_BY(mMutex) = true; |
| 79 | bool mSampleRequested GUARDED_BY(mMutex) = false; |
| 80 | |
| 81 | std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mMutex); |
| 82 | }; |
| 83 | |
| 84 | } // namespace android |