OSDN Git Service

Merge remote-tracking branch 'goffioul/master'
[android-x86/external-drmfb-composer.git] / GraphicsThread.h
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2019 Stephan Gerhold
3
4 #pragma once
5
6 #include <condition_variable>
7 #include <mutex>
8 #include <thread>
9
10 namespace android {
11 namespace hardware {
12 namespace graphics {
13 namespace composer {
14 namespace V2_1 {
15 namespace drmfb {
16
17 struct GraphicsThread {
18     GraphicsThread(std::string name);
19     virtual ~GraphicsThread();
20
21     void enable();
22     void disable();
23     void stop();
24
25 protected:
26     virtual void run() {};
27     virtual void work(std::unique_lock<std::mutex>& lock);
28
29     template<typename F>
30     void loop(std::unique_lock<std::mutex>& lock, F f) {
31         while (mEnabled) {
32             lock.unlock();
33             f();
34             lock.lock();
35         }
36     }
37
38 private:
39     void main();
40
41     std::mutex mMutex;
42     std::thread mThread;
43     std::string mName;
44
45     bool mStarted;
46     bool mEnabled;
47     std::condition_variable mCondition;
48 };
49
50 }  // namespace drmfb
51 }  // namespace V2_1
52 }  // namespace composer
53 }  // namespace graphics
54 }  // namespace hardware
55 }  // namespace android