1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17 #ifndef VSYNC_VSYNC_DISTRIBUTOR_H 18 #define VSYNC_VSYNC_DISTRIBUTOR_H 19 20 #include <refbase.h> 21 22 #include <mutex> 23 #include <vector> 24 #include <thread> 25 #include <condition_variable> 26 27 #include "local_socketpair.h" 28 #include "vsync_controller.h" 29 #include "vsync_connection_stub.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 class VSyncDistributor; 34 struct ConnectionInfo { 35 std::string name_; 36 uint64_t postVSyncCount_; ConnectionInfoConnectionInfo37 ConnectionInfo(std::string name): postVSyncCount_(0) 38 { 39 this->name_ = name; 40 } 41 }; 42 43 class VSyncConnection : public VSyncConnectionStub { 44 public: 45 46 VSyncConnection(const sptr<VSyncDistributor>& distributor, std::string name); 47 ~VSyncConnection(); 48 49 virtual VsyncError RequestNextVSync() override; 50 virtual VsyncError GetReceiveFd(int32_t &fd) override; 51 virtual VsyncError SetVSyncRate(int32_t rate) override; 52 53 int32_t PostEvent(int64_t now); 54 55 int32_t rate_; 56 int32_t highPriorityRate_ = -1; 57 bool highPriorityState_ = false; 58 ConnectionInfo info_; 59 private: 60 // Circular reference, need check 61 wptr<VSyncDistributor> distributor_; 62 sptr<LocalSocketPair> socketPair_; 63 }; 64 65 class VSyncDistributor : public RefBase, public VSyncController::Callback { 66 public: 67 68 VSyncDistributor(sptr<VSyncController> controller, std::string name); 69 ~VSyncDistributor(); 70 // nocopyable 71 VSyncDistributor(const VSyncDistributor &) = delete; 72 VSyncDistributor &operator=(const VSyncDistributor &) = delete; 73 74 VsyncError AddConnection(const sptr<VSyncConnection>& connection); 75 VsyncError RemoveConnection(const sptr<VSyncConnection> &connection); 76 VsyncError RequestNextVSync(const sptr<VSyncConnection>& connection); 77 VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection); 78 VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr<VSyncConnection>& connection); 79 VsyncError GetVSyncConnectionInfos(std::vector<ConnectionInfo>& infos); 80 VsyncError GetQosVSyncRateInfos(std::vector<std::pair<uint32_t, int32_t>>& vsyncRateInfos); 81 VsyncError SetQosVSyncRate(uint32_t pid, int32_t rate); 82 83 private: 84 85 // check, add more info 86 struct VSyncEvent { 87 int64_t timestamp; 88 int64_t vsyncCount; 89 }; 90 void ThreadMain(); 91 void EnableVSync(); 92 void DisableVSync(); 93 void OnVSyncEvent(int64_t now); 94 void CollectConnections(bool &waitForVSync, int64_t timestamp, 95 std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount); 96 VsyncError QosGetPidByName(const std::string& name, uint32_t& pid); 97 98 std::thread threadLoop_; 99 sptr<VSyncController> controller_; 100 std::mutex mutex_; 101 std::condition_variable con_; 102 std::vector<sptr<VSyncConnection> > connections_; 103 VSyncEvent event_; 104 bool vsyncEnabled_; 105 std::string name_; 106 bool vsyncThreadRunning_; 107 }; 108 } // namespace Rosen 109 } // namespace OHOS 110 111 #endif 112