1 /* 2 * Copyright (c) 2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_WINDOW_PREVIEWER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_WINDOW_PREVIEWER_H 18 19 #include <chrono> 20 #include <condition_variable> 21 #include <functional> 22 #include <mutex> 23 #include <queue> 24 #include <refbase.h> 25 #include <thread> 26 #include <ui/rs_surface_node.h> 27 #include <vector> 28 29 #include "adapter/preview/entrance/rs_ace_view.h" 30 #include "base/thread/sem_queue.h" 31 #include "base/utils/time_util.h" 32 #include "core/common/window.h" 33 34 namespace OHOS { 35 namespace Rosen { 36 37 using OnCallback = std::function<void(int64_t)>; 38 struct VsyncCallback { 39 OnCallback onCallback; 40 }; 41 42 using SendRenderDataCallback = bool (*)(const void*, const size_t, const int32_t, const int32_t); 43 class Window : public RefBase { 44 public: Window(SendRenderDataCallback onRender)45 Window(SendRenderDataCallback onRender) 46 { 47 struct Rosen::RSSurfaceNodeConfig rsSurfaceNodeConfig = { 48 .SurfaceNodeName = "preview_surface", 49 .onRender = onRender, 50 }; 51 surfaceNode_ = Rosen::RSSurfaceNode::Create(rsSurfaceNodeConfig); 52 } 53 ~Window()54 ~Window() 55 { 56 vsyncRequests_.Push(false); 57 vsyncThread_->join(); 58 } RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)59 virtual void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback) 60 { 61 { 62 std::unique_lock lock(mutex_); 63 pendingVsyncCallbacks_.emplace_back(std::move(vsyncCallback)); 64 } 65 if (vsyncThread_ == nullptr) { 66 auto func = [this] { VsyncThreadMain(); }; 67 vsyncThread_ = std::make_unique<std::thread>(func); 68 } 69 vsyncRequests_.Push(true); 70 } 71 GetSurfaceNode()72 std::shared_ptr<RSSurfaceNode> GetSurfaceNode() const 73 { 74 return surfaceNode_; 75 } 76 VsyncThreadMain()77 void VsyncThreadMain() 78 { 79 while (true) { 80 bool next = false; 81 vsyncRequests_.PopFront(next); 82 if (next == false) { 83 break; 84 } 85 86 std::this_thread::sleep_for(std::chrono::milliseconds(1)); 87 { 88 std::unique_lock lock(mutex_); 89 vsyncCallbacks_.swap(pendingVsyncCallbacks_); 90 } 91 int64_t now = Ace::GetSysTimestamp(); 92 for (auto& callback : vsyncCallbacks_) { 93 callback->onCallback(now); 94 } 95 vsyncCallbacks_.clear(); 96 } 97 } 98 std::shared_ptr<RSSurfaceNode> surfaceNode_; 99 std::vector<std::shared_ptr<VsyncCallback>> vsyncCallbacks_; 100 std::vector<std::shared_ptr<VsyncCallback>> pendingVsyncCallbacks_; 101 std::unique_ptr<std::thread> vsyncThread_; 102 Ace::SemQueue<bool> vsyncRequests_; 103 std::mutex mutex_; 104 }; 105 106 } // namespace Rosen 107 } // namespace OHOS 108 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_WINDOW_PREVIEWER_H 109