• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "glfw_render_context.h"
17 
18 #include <mutex>
19 
20 #include <GLFW/glfw3.h>
21 
22 #include "hilog/log.h"
23 
24 namespace OHOS::Rosen {
25 namespace {
26 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001403, "GlfwRC" };
27 }
28 
GetGlobal()29 std::shared_ptr<GlfwRenderContext> GlfwRenderContext::GetGlobal()
30 {
31     if (global_ == nullptr) {
32         static std::mutex mutex;
33         std::lock_guard lock(mutex);
34         if (global_ == nullptr) {
35             global_ = std::make_shared<GlfwRenderContext>();
36         }
37     }
38 
39     return global_;
40 }
41 
Init()42 int GlfwRenderContext::Init()
43 {
44     ::OHOS::HiviewDFX::HiLog::Info(LABEL, "Init");
45     external_ = false;
46     return glfwInit();
47 }
48 
InitFrom(void * glfwWindow)49 void GlfwRenderContext::InitFrom(void *glfwWindow)
50 {
51     if (glfwWindow == nullptr) {
52         ::OHOS::HiviewDFX::HiLog::Error(LABEL, "InitFrom glfwWindow is nullptr");
53         return;
54     }
55     ::OHOS::HiviewDFX::HiLog::Info(LABEL, "InitFrom glfwWindow");
56 
57     external_ = true;
58 
59     // to be done: replace this with normal way, a no flutter way.
60     // from third_party/flutter/engine/flutter/shell/platform/glfw/flutter_glfw.cc +39
61     window_ = *reinterpret_cast<GLFWwindow **>(glfwWindow);
62 
63     glfwSetCharCallback(window_, nullptr);
64     glfwSetCursorEnterCallback(window_, nullptr);
65     glfwSetCursorPosCallback(window_, nullptr);
66     glfwSetFramebufferSizeCallback(window_, nullptr);
67     glfwSetKeyCallback(window_, nullptr);
68     glfwSetMouseButtonCallback(window_, nullptr);
69     glfwSetScrollCallback(window_, nullptr);
70     glfwSetWindowRefreshCallback(window_, nullptr);
71     glfwSetWindowUserPointer(window_, this);
72 }
73 
Terminate()74 void GlfwRenderContext::Terminate()
75 {
76     if (external_) {
77         return;
78     }
79 
80     glfwTerminate();
81 }
82 
CreateWindow(int32_t width,int32_t height,bool visible)83 int GlfwRenderContext::CreateWindow(int32_t width, int32_t height, bool visible)
84 {
85     if (external_) {
86         return 0;
87     }
88 
89     if (window_ != nullptr) {
90         return 0;
91     }
92 
93     auto flag = visible ? GLFW_TRUE : GLFW_FALSE;
94     glfwWindowHint(GLFW_DECORATED, flag);
95     glfwWindowHint(GLFW_VISIBLE, flag);
96     window_ = glfwCreateWindow(width, height, "glfw window", nullptr, nullptr);
97     if (window_ == nullptr) {
98         return 1;
99     }
100 
101     glfwSetWindowUserPointer(window_, this);
102     return 0;
103 }
104 
DestroyWindow()105 void GlfwRenderContext::DestroyWindow()
106 {
107     if (external_) {
108         return;
109     }
110 
111     if (window_ != nullptr) {
112         glfwDestroyWindow(window_);
113     }
114 }
115 
WindowShouldClose()116 int GlfwRenderContext::WindowShouldClose()
117 {
118     return glfwWindowShouldClose(window_);
119 }
120 
WaitForEvents()121 void GlfwRenderContext::WaitForEvents()
122 {
123     glfwWaitEvents();
124 }
125 
PollEvents()126 void GlfwRenderContext::PollEvents()
127 {
128     glfwPollEvents();
129 }
130 
GetWindowSize(int32_t & width,int32_t & height)131 void GlfwRenderContext::GetWindowSize(int32_t &width, int32_t &height)
132 {
133     glfwGetWindowSize(window_, &width, &height);
134 }
135 
SetWindowSize(int32_t width,int32_t height)136 void GlfwRenderContext::SetWindowSize(int32_t width, int32_t height)
137 {
138     glfwSetWindowSize(window_, width, height);
139 }
140 
SetWindowTitle(const std::string & title)141 void GlfwRenderContext::SetWindowTitle(const std::string &title)
142 {
143     glfwSetWindowTitle(window_, title.c_str());
144 }
145 
GetClipboardData()146 std::string GlfwRenderContext::GetClipboardData()
147 {
148     return glfwGetClipboardString(window_);
149 }
150 
SetClipboardData(const std::string & data)151 void GlfwRenderContext::SetClipboardData(const std::string &data)
152 {
153     glfwSetClipboardString(window_, data.c_str());
154 }
155 
MakeCurrent()156 void GlfwRenderContext::MakeCurrent()
157 {
158     glfwMakeContextCurrent(window_);
159 }
160 
SwapBuffers()161 void GlfwRenderContext::SwapBuffers()
162 {
163     glfwSwapBuffers(window_);
164 }
165 
OnMouseButton(const OnMouseButtonFunc & onMouseBotton)166 void GlfwRenderContext::OnMouseButton(const OnMouseButtonFunc &onMouseBotton)
167 {
168     onMouseBotton_ = onMouseBotton;
169     glfwSetMouseButtonCallback(window_, GlfwRenderContext::OnMouseButton);
170 }
171 
OnCursorPos(const OnCursorPosFunc & onCursorPos)172 void GlfwRenderContext::OnCursorPos(const OnCursorPosFunc &onCursorPos)
173 {
174     onCursorPos_ = onCursorPos;
175     glfwSetCursorPosCallback(window_, GlfwRenderContext::OnCursorPos);
176 }
177 
OnKey(const OnKeyFunc & onKey)178 void GlfwRenderContext::OnKey(const OnKeyFunc &onKey)
179 {
180     onKey_ = onKey;
181     glfwSetKeyCallback(window_, GlfwRenderContext::OnKey);
182 }
183 
OnChar(const OnCharFunc & onChar)184 void GlfwRenderContext::OnChar(const OnCharFunc &onChar)
185 {
186     onChar_ = onChar;
187     glfwSetCharCallback(window_, GlfwRenderContext::OnChar);
188 }
189 
OnMouseButton(GLFWwindow * window,int button,int action,int mods)190 void GlfwRenderContext::OnMouseButton(GLFWwindow *window, int button, int action, int mods)
191 {
192     const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
193     if (that->onMouseBotton_) {
194         that->onMouseBotton_(button, action == GLFW_PRESS, mods);
195     }
196 }
197 
OnCursorPos(GLFWwindow * window,double x,double y)198 void GlfwRenderContext::OnCursorPos(GLFWwindow *window, double x, double y)
199 {
200     const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
201     if (that->onCursorPos_) {
202         that->onCursorPos_(x, y);
203     }
204 }
205 
OnKey(GLFWwindow * window,int key,int scancode,int action,int mods)206 void GlfwRenderContext::OnKey(GLFWwindow *window, int key, int scancode, int action, int mods)
207 {
208     const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
209     if (that->onKey_) {
210         that->onKey_(key, scancode, action, mods);
211     }
212 }
213 
OnChar(GLFWwindow * window,unsigned int codepoint)214 void GlfwRenderContext::OnChar(GLFWwindow *window, unsigned int codepoint)
215 {
216     const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
217     if (that->onChar_) {
218         that->onChar_(codepoint);
219     }
220 }
221 } // namespace OHOS::Rosen
222