• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 #ifndef ANDROID_SF_HWC2_ON_FB_ADAPTER_H
18 #define ANDROID_SF_HWC2_ON_FB_ADAPTER_H
19 
20 #include <condition_variable>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 #include <unordered_set>
25 
26 #define HWC2_INCLUDE_STRINGIFICATION
27 #define HWC2_USE_CPP11
28 #include <hardware/hwcomposer2.h>
29 #undef HWC2_INCLUDE_STRINGIFICATION
30 #undef HWC2_USE_CPP11
31 
32 struct framebuffer_device_t;
33 
34 namespace android {
35 
36 class HWC2OnFbAdapter : public hwc2_device_t {
37 public:
38     HWC2OnFbAdapter(framebuffer_device_t* fbDevice);
39 
40     static HWC2OnFbAdapter& cast(hw_device_t* device);
41     static HWC2OnFbAdapter& cast(hwc2_device_t* device);
42 
43     static hwc2_display_t getDisplayId();
44     static hwc2_config_t getConfigId();
45 
46     void close();
47 
48     struct Info {
49         std::string name;
50         uint32_t width;
51         uint32_t height;
52         int format;
53         int vsync_period_ns;
54         int xdpi_scaled;
55         int ydpi_scaled;
56     };
57     const Info& getInfo() const;
58 
59     void updateDebugString();
60     const std::string& getDebugString() const;
61 
62     enum class State {
63         MODIFIED,
64         VALIDATED_WITH_CHANGES,
65         VALIDATED,
66     };
67     void setState(State state);
68     State getState() const;
69 
70     hwc2_layer_t addLayer();
71     bool removeLayer(hwc2_layer_t layer);
72     bool hasLayer(hwc2_layer_t layer) const;
73     bool markLayerDirty(hwc2_layer_t layer, bool dirty);
74     const std::unordered_set<hwc2_layer_t>& getDirtyLayers() const;
75     void clearDirtyLayers();
76 
77     void setBuffer(buffer_handle_t buffer);
78     bool postBuffer();
79 
80     void setVsyncCallback(HWC2_PFN_VSYNC callback, hwc2_callback_data_t data);
81     void enableVsync(bool enable);
82     void getCapabilities(uint32_t* outCount, int32_t* outCapabilities);
83 
84 private:
85     framebuffer_device_t* mFbDevice{nullptr};
86     Info mFbInfo{};
87 
88     std::string mDebugString;
89 
90     State mState{State::MODIFIED};
91 
92     uint64_t mNextLayerId{0};
93     std::unordered_set<hwc2_layer_t> mLayers;
94     std::unordered_set<hwc2_layer_t> mDirtyLayers;
95 
96     buffer_handle_t mBuffer{nullptr};
97 
98     std::unordered_set<HWC2::Capability> mCapabilities;
99 
100     class VsyncThread {
101     public:
102         static int64_t now();
103         static bool sleepUntil(int64_t t);
104 
105         void start(int64_t first, int64_t period);
106         void stop();
107         void setCallback(HWC2_PFN_VSYNC callback, hwc2_callback_data_t data);
108         void enableCallback(bool enable);
109 
110     private:
111         void vsyncLoop();
112         bool waitUntilNextVsync();
113 
114         std::thread mThread;
115         int64_t mNextVsync{0};
116         int64_t mPeriod{0};
117 
118         std::mutex mMutex;
119         std::condition_variable mCondition;
120         bool mStarted{false};
121         HWC2_PFN_VSYNC mCallback{nullptr};
122         hwc2_callback_data_t mCallbackData{nullptr};
123         bool mCallbackEnabled{false};
124     };
125     VsyncThread mVsyncThread;
126 };
127 
128 } // namespace android
129 
130 #endif // ANDROID_SF_HWC2_ON_FB_ADAPTER_H
131