blob: ab06513be3ad22070b0374afe19f2f0a037ed52f [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
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
29namespace android {
30
31class GraphicBuffer;
32class IRegionSamplingListener;
33class Layer;
34class SurfaceFlinger;
35
36class RegionSamplingThread : public IBinder::DeathRecipient {
37public:
38 explicit RegionSamplingThread(SurfaceFlinger& flinger);
39 ~RegionSamplingThread();
40
Kevin DuBois7cbcc372019-02-25 14:53:28 -080041 // 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 Stozaec460082018-12-17 15:35:09 -080043 void addListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
44 const sp<IRegionSamplingListener>& listener);
Kevin DuBois7cbcc372019-02-25 14:53:28 -080045 // Remove the listener to stop receiving median luma notifications.
Dan Stozaec460082018-12-17 15:35:09 -080046 void removeListener(const sp<IRegionSamplingListener>& listener);
Kevin DuBois7cbcc372019-02-25 14:53:28 -080047 // Instruct the thread to perform a median luma sampling on the layers.
Dan Stozaec460082018-12-17 15:35:09 -080048 void sampleNow();
49
Kevin DuBois7cbcc372019-02-25 14:53:28 -080050private:
Dan Stozaec460082018-12-17 15:35:09 -080051 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 DuBois7cbcc372019-02-25 14:53:28 -080062 std::vector<float> sampleBuffer(
63 const sp<GraphicBuffer>& buffer, const Point& leftTop,
64 const std::vector<RegionSamplingThread::Descriptor>& descriptors);
Dan Stozaec460082018-12-17 15:35:09 -080065
Dan Stozaec460082018-12-17 15:35:09 -080066 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