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_CONTROLLER_H 18 #define VSYNC_VSYNC_CONTROLLER_H 19 20 #include <cstdint> 21 #include <refbase.h> 22 #include <mutex> 23 #include "vsync_generator.h" 24 #include "graphic_common.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 class VSyncController : public VSyncGenerator::Callback { 29 public: 30 class Callback { 31 public: 32 // Start of DVSync 33 virtual void OnDVSyncEvent(int64_t now, int64_t period, 34 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate) = 0; 35 // End of DVSync 36 virtual void OnVSyncEvent(int64_t now, int64_t period, 37 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate) = 0; 38 virtual ~Callback() = default; 39 /* std::pair<id, refresh rate> */ 40 virtual void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates) = 0; 41 }; 42 43 VSyncController(const sptr<VSyncGenerator> &geng, int64_t offset); 44 ~VSyncController(); 45 46 // nocopyable 47 VSyncController(const VSyncController &) = delete; 48 VSyncController &operator=(const VSyncController &) = delete; 49 50 virtual VsyncError SetEnable(bool enable, bool& isGeneratorEnable); 51 VsyncError SetCallback(Callback* cb); 52 VsyncError SetPhaseOffset(int64_t offset); 53 bool NeedPreexecuteAndUpdateTs(int64_t& timestamp, int64_t& period); 54 void ChangeAdaptiveStatus(bool isAdaptive); 55 void AdjustAdaptiveOffset(int64_t period, int64_t offset); 56 void ResetOffset(); SetUrgent(bool isUrgent)57 void SetUrgent(bool isUrgent) 58 { 59 isDirectly_ = isUrgent; 60 } SetRS(bool isRS)61 void SetRS(bool isRS) 62 { 63 isRS_ = isRS; 64 } 65 66 private: 67 friend class DVSyncController; 68 void OnVSyncEvent(int64_t now, int64_t period, 69 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate); 70 void OnPhaseOffsetChanged(int64_t phaseOffset); 71 int64_t GetPhaseOffset(); 72 /* std::pair<id, refresh rate> */ 73 void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates); 74 wptr<VSyncGenerator> generator_; 75 std::mutex callbackMutex_; 76 Callback* callback_; 77 78 std::mutex offsetMutex_; 79 int64_t phaseOffset_; 80 int64_t normalPhaseOffset_; 81 bool enabled_; 82 std::atomic<int64_t> lastVsyncTime_ = 0; 83 bool isDirectly_ = false; 84 bool isRS_ = false; 85 }; 86 } // namespace Rosen 87 } // namespace OHOS 88 89 #endif 90