/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef VSYNC_VSYNC_DISTRIBUTOR_H #define VSYNC_VSYNC_DISTRIBUTOR_H #include #include #include #include #include #include "local_socketpair.h" #include "vsync_controller.h" #include "vsync_connection_stub.h" namespace OHOS { namespace Rosen { class VSyncDistributor; struct ConnectionInfo { std::string name_; uint64_t postVSyncCount_; ConnectionInfo(std::string name): postVSyncCount_(0) { this->name_ = name; } }; class VSyncConnection : public VSyncConnectionStub { public: VSyncConnection(const sptr& distributor, std::string name, const sptr& token = nullptr); ~VSyncConnection(); virtual VsyncError RequestNextVSync() override; virtual VsyncError GetReceiveFd(int32_t &fd) override; virtual VsyncError SetVSyncRate(int32_t rate) override; virtual VsyncError GetVSyncPeriod(int64_t &period) override; VsyncError OnVSyncRemoteDied(); int32_t PostEvent(int64_t now, int64_t period, int64_t vsyncCount); int32_t rate_; int32_t highPriorityRate_ = -1; bool highPriorityState_ = false; ConnectionInfo info_; private: class VSyncConnectionDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit VSyncConnectionDeathRecipient(wptr conn); virtual ~VSyncConnectionDeathRecipient() = default; void OnRemoteDied(const wptr& token) override; private: wptr conn_; }; sptr vsyncConnDeathRecipient_ = nullptr; sptr token_ = nullptr; // Circular reference, need check wptr distributor_; sptr socketPair_; bool isRemoteDead_; std::mutex mutex_; }; class VSyncDistributor : public RefBase, public VSyncController::Callback { public: VSyncDistributor(sptr controller, std::string name); ~VSyncDistributor(); // nocopyable VSyncDistributor(const VSyncDistributor &) = delete; VSyncDistributor &operator=(const VSyncDistributor &) = delete; VsyncError AddConnection(const sptr& connection); VsyncError RemoveConnection(const sptr &connection); VsyncError RequestNextVSync(const sptr& connection); VsyncError SetVSyncRate(int32_t rate, const sptr& connection); VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr& connection); VsyncError GetVSyncConnectionInfos(std::vector& infos); VsyncError GetQosVSyncRateInfos(std::vector>& vsyncRateInfos); VsyncError SetQosVSyncRate(uint32_t pid, int32_t rate); VsyncError GetVSyncPeriod(int64_t &period); private: // check, add more info struct VSyncEvent { int64_t timestamp; int64_t vsyncCount; int64_t period; }; void ThreadMain(); void EnableVSync(); void DisableVSync(); void OnVSyncEvent(int64_t now, int64_t period); void CollectConnections(bool &waitForVSync, int64_t timestamp, std::vector> &conns, int64_t vsyncCount); VsyncError QosGetPidByName(const std::string& name, uint32_t& pid); std::thread threadLoop_; sptr controller_; std::mutex mutex_; std::condition_variable con_; std::vector > connections_; VSyncEvent event_; bool vsyncEnabled_; std::string name_; bool vsyncThreadRunning_; }; } // namespace Rosen } // namespace OHOS #endif