blob: bf59007465a4ae5b4f211b750ca12e9f99072688 [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
41 void addListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
42 const sp<IRegionSamplingListener>& listener);
43 void removeListener(const sp<IRegionSamplingListener>& listener);
44 void sampleNow();
45
46 struct Descriptor {
47 Rect area = Rect::EMPTY_RECT;
48 wp<Layer> stopLayer;
49 sp<IRegionSamplingListener> listener;
50 };
51
52 struct WpHash {
53 size_t operator()(const wp<IBinder>& p) const {
54 return std::hash<IBinder*>()(p.unsafe_get());
55 }
56 };
57
58private:
59 void binderDied(const wp<IBinder>& who) override;
60
61 void captureSample() REQUIRES(mMutex);
62 void threadMain();
63
64 SurfaceFlinger& mFlinger;
65
66 std::mutex mThreadMutex;
67 std::thread mThread GUARDED_BY(mThreadMutex);
68
69 std::mutex mMutex;
70 std::condition_variable_any mCondition;
71 bool mRunning GUARDED_BY(mMutex) = true;
72 bool mSampleRequested GUARDED_BY(mMutex) = false;
73
74 std::unordered_map<wp<IBinder>, Descriptor, WpHash> mDescriptors GUARDED_BY(mMutex);
75};
76
77} // namespace android