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