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 glfwGetWindowSize(window_, &width_, &height_);
74 ::OHOS::HiviewDFX::HiLog::Info(LABEL, "glfwSetWindowSizeCallback %{public}d %{public}d", width_, height_);
75 glfwSetWindowSizeCallback(window_, GlfwRenderContext::OnSizeChanged);
76 }
77
Terminate()78 void GlfwRenderContext::Terminate()
79 {
80 if (external_) {
81 return;
82 }
83
84 glfwTerminate();
85 }
86
CreateGlfwWindow(int32_t width,int32_t height,bool visible)87 int GlfwRenderContext::CreateGlfwWindow(int32_t width, int32_t height, bool visible)
88 {
89 if (external_) {
90 return 0;
91 }
92
93 if (window_ != nullptr) {
94 return 0;
95 }
96
97 auto flag = visible ? GLFW_TRUE : GLFW_FALSE;
98 glfwWindowHint(GLFW_DECORATED, flag);
99 glfwWindowHint(GLFW_VISIBLE, flag);
100 window_ = glfwCreateWindow(width, height, "glfw window", nullptr, nullptr);
101 if (window_ == nullptr) {
102 return 1;
103 }
104
105 glfwSetWindowUserPointer(window_, this);
106
107 width_ = width;
108 height_ = height;
109 ::OHOS::HiviewDFX::HiLog::Info(LABEL, "glfwSetWindowSizeCallback %{public}d %{public}d", width, height);
110 glfwSetWindowSizeCallback(window_, GlfwRenderContext::OnSizeChanged);
111 return 0;
112 }
113
DestroyWindow()114 void GlfwRenderContext::DestroyWindow()
115 {
116 if (external_) {
117 return;
118 }
119
120 if (window_ != nullptr) {
121 glfwDestroyWindow(window_);
122 }
123 }
124
WindowShouldClose()125 int GlfwRenderContext::WindowShouldClose()
126 {
127 return glfwWindowShouldClose(window_);
128 }
129
WaitForEvents()130 void GlfwRenderContext::WaitForEvents()
131 {
132 glfwWaitEvents();
133 }
134
PollEvents()135 void GlfwRenderContext::PollEvents()
136 {
137 glfwPollEvents();
138 }
139
GetWindowSize(int32_t & width,int32_t & height)140 void GlfwRenderContext::GetWindowSize(int32_t &width, int32_t &height)
141 {
142 glfwGetWindowSize(window_, &width, &height);
143 }
144
SetWindowSize(int32_t width,int32_t height)145 void GlfwRenderContext::SetWindowSize(int32_t width, int32_t height)
146 {
147 width_ = width;
148 height_ = height;
149 glfwSetWindowSize(window_, width, height);
150 }
151
SetWindowTitle(const std::string & title)152 void GlfwRenderContext::SetWindowTitle(const std::string &title)
153 {
154 glfwSetWindowTitle(window_, title.c_str());
155 }
156
GetClipboardData()157 std::string GlfwRenderContext::GetClipboardData()
158 {
159 return glfwGetClipboardString(window_);
160 }
161
SetClipboardData(const std::string & data)162 void GlfwRenderContext::SetClipboardData(const std::string &data)
163 {
164 glfwSetClipboardString(window_, data.c_str());
165 }
166
MakeCurrent()167 void GlfwRenderContext::MakeCurrent()
168 {
169 glfwMakeContextCurrent(window_);
170 }
171
SwapBuffers()172 void GlfwRenderContext::SwapBuffers()
173 {
174 glfwSwapBuffers(window_);
175 }
176
OnMouseButton(const OnMouseButtonFunc & onMouseBotton)177 void GlfwRenderContext::OnMouseButton(const OnMouseButtonFunc &onMouseBotton)
178 {
179 onMouseBotton_ = onMouseBotton;
180 glfwSetMouseButtonCallback(window_, GlfwRenderContext::OnMouseButton);
181 }
182
OnCursorPos(const OnCursorPosFunc & onCursorPos)183 void GlfwRenderContext::OnCursorPos(const OnCursorPosFunc &onCursorPos)
184 {
185 onCursorPos_ = onCursorPos;
186 glfwSetCursorPosCallback(window_, GlfwRenderContext::OnCursorPos);
187 }
188
OnKey(const OnKeyFunc & onKey)189 void GlfwRenderContext::OnKey(const OnKeyFunc &onKey)
190 {
191 onKey_ = onKey;
192 glfwSetKeyCallback(window_, GlfwRenderContext::OnKey);
193 }
194
OnChar(const OnCharFunc & onChar)195 void GlfwRenderContext::OnChar(const OnCharFunc &onChar)
196 {
197 onChar_ = onChar;
198 glfwSetCharCallback(window_, GlfwRenderContext::OnChar);
199 }
200
OnMouseButton(GLFWwindow * window,int button,int action,int mods)201 void GlfwRenderContext::OnMouseButton(GLFWwindow *window, int button, int action, int mods)
202 {
203 const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
204 if (that->onMouseBotton_) {
205 that->onMouseBotton_(button, action == GLFW_PRESS, mods);
206 }
207 }
208
OnCursorPos(GLFWwindow * window,double x,double y)209 void GlfwRenderContext::OnCursorPos(GLFWwindow *window, double x, double y)
210 {
211 const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
212 if (that->onCursorPos_) {
213 that->onCursorPos_(x, y);
214 }
215 }
216
OnKey(GLFWwindow * window,int key,int scancode,int action,int mods)217 void GlfwRenderContext::OnKey(GLFWwindow *window, int key, int scancode, int action, int mods)
218 {
219 const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
220 if (that->onKey_) {
221 that->onKey_(key, scancode, action, mods);
222 }
223 }
224
OnChar(GLFWwindow * window,unsigned int codepoint)225 void GlfwRenderContext::OnChar(GLFWwindow *window, unsigned int codepoint)
226 {
227 const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
228 if (that->onChar_) {
229 that->onChar_(codepoint);
230 }
231 }
232
OnSizeChanged(GLFWwindow * window,int32_t width,int32_t height)233 void GlfwRenderContext::OnSizeChanged(GLFWwindow *window, int32_t width, int32_t height)
234 {
235 ::OHOS::HiviewDFX::HiLog::Info(LABEL, "OnSizeChanged %{public}d %{public}d", width, height);
236 const auto &that = reinterpret_cast<GlfwRenderContext *>(glfwGetWindowUserPointer(window));
237 if (that->width_ != width || that->height_ != height) {
238 glfwSetWindowSize(window, that->width_, that->height_);
239 }
240 ::OHOS::HiviewDFX::HiLog::Info(LABEL, "OnSizeChanged done %{public}d %{public}d", width, height);
241 }
242 } // namespace OHOS::Rosen
243