FCT
载入中...
搜索中...
未找到
Android_Window.cpp
浏览该文件的文档.
1#include "../FCTAPI.h"
2#include "Android_Window.h"
3
4
5namespace FCT{
6
7#ifdef FCT_USE_VULKAN
8
10 vk::Image image;
11 };
12#endif
13#ifdef FCT_USE_VULKAN
14
15#endif
17 m_ctx = srcCtx;
18 m_ctx->setFlushWindow(this);
19 if (false) {
20
21 }
22#ifdef FCT_USE_VULKAN
23 else if (dynamic_cast<VK_Context*>(srcCtx)) {
24 auto ctx = dynamic_cast<VK_Context *>(srcCtx);
25 bool needReWaited;
26 do {
27 needReWaited = false;
28 while (!hasSurface()) {
29 std::this_thread::sleep_for(std::chrono::milliseconds(1));
30 }
32 if (!hasSurface()) {
33 needReWaited = true;
34 m_nativeSurfaceMutex.unlock();
35 continue;
36 }
40 //m_vkSwapchain = new VK_Swapchain(ctx);
43 present = std::bind(&Android_Window::presentVulkan, this);
46
47 m_nativeSurfaceMutex.unlock();
48
49 } while (needReWaited);
50 }
51 /*
52 else if (dynamic_cast<VK_Context*>(srcCtx)) {
53 auto ctx = dynamic_cast<VK_Context*>(srcCtx);
54 bool needReWaited;
55 do {
56 needReWaited = false;
57 while (!hasSurface()) {
58 std::this_thread::sleep_for(std::chrono::milliseconds(1));
59 }
60 m_nativeSurfaceMutex.lock();
61 if (!hasSurface()) {
62 needReWaited = true;
63 m_nativeSurfaceMutex.unlock();
64 continue;
65 }
66 auto w = ANativeWindow_getWidth(m_nativeWindow);
67 auto h = ANativeWindow_getHeight(m_nativeWindow);
68 m_buffer = ctx->createImage();
69 m_buffer->create(w,h);
70 m_buffer->initTarget();
71 m_nativeSurfaceMutex.unlock();
72 } while (needReWaited);
73 m_contextReource = new Vulkan_WndResource();
74 recreateSwapChain = std::bind(&Android_Window::createVulkanSwapChain, this);
75 present = std::bind(&Android_Window::presentVulkan, this);
76 }
77 */
78#endif
79 else {
80 ferr << "没有受支持的context" << std::endl;
81 }
82 }
83
85 return m_running;
86 }
87
89 auto ctx = m_ctx;
90 bool needReWaited;
91 do {
92 needReWaited = false;
93 while (!hasSurface()) {
94 std::this_thread::sleep_for(std::chrono::milliseconds(1));
95 }
97 if (!hasSurface()) {
98 needReWaited = true;
99 m_nativeSurfaceMutex.unlock();
100 continue;
101 }
106 }
107 present();
108 m_nativeSurfaceMutex.unlock();
109 } while (needReWaited);
110 }
111
113 return 0;
114 }
115
117 return 0;
118 }
119
120 void Android_Window::setCursorPos(int x, int y) {
121
122 }
123
125 g_androidRuntime->getUICommon()->postUiTask([this](void* ){
126 jni::GlobalObject<FCTMainActivityJavaClass>* activity = (g_androidRuntime->getActivity());
127 m_windowObject = new jni::GlobalObject<FCTWindowJavaClass>(
128 activity->Call<"createWindow">(jlong(this),m_x,m_y,m_width,m_height));
129 aout << "创建窗口" << std::endl;
130 });
131 m_created = true;
132 m_running = true;
133 }
134
136 m_running = false;
137 }
138
140 return m_created;
141 }
142/*
143 void Android_Window::createSurface(jobject surface) {
144 if (m_nativeWindow) {
145 ANativeWindow_release(m_nativeWindow);
146 }
147 m_nativeWindow = ANativeWindow_fromSurface(getEnv(), surface);
148 m_needRecreateSwapChain = true;
149 m_hasSurface = true;
150 }
151*/
153 m_hasSurface = false;
155 if (m_nativeWindow) {
156 ANativeWindow_release(m_nativeWindow);
157 m_nativeWindow = nullptr;
158 }
159 m_nativeSurfaceMutex.unlock();
160 }
161
163 m_lt = lt;
164 m_rb = rb;
165 }
166
167 void Android_Window::create(float w, float h, const char *title) {
168 viewport(Vec2(0, 0), Vec2(w, h));
169 m_title = title;;
170 }
171
173
174 return 0;
175 }
176
178 return 0;
179 }
180
182 m_created = false;
183 m_running = false;
184 m_hasSurface = false;
185 m_nativeWindow = nullptr;
186 m_vm = vm;
188 }
189/*
190 void Android_Window::createVulkanSurface() {
191 if (m_vulkanSurface) {
192 auto vkInstance = g_AndroidRuntime.getVKContextCommon()->getInstance();
193 vkInstance.destroySurfaceKHR(m_vulkanSurface);
194 m_vulkanSurface = nullptr;
195 }
196
197 vk::AndroidSurfaceCreateInfoKHR createInfo{};
198 createInfo.setWindow(m_nativeWindow);
199 auto vkInstance = g_AndroidRuntime.getVKContextCommon()->getInstance();
200 m_vulkanSurface = vkInstance.createAndroidSurfaceKHR(createInfo);
201 }
202
203 void Android_Window::createVulkanSwapChain() {
204 vk::SwapchainCreateInfoKHR createInfo{};
205 createVulkanSurface();
206 createInfo.setSurface(m_vulkanSurface)
207 .setMinImageCount(2)
208 .setImageFormat(vk::Format::eR8G8B8A8Unorm);
209 }
210*/
212 return m_hasSurface;
213 }
214
216 m_x = 0;
217 m_y = 0;
218 m_width = 800;
219 m_height = 600;
220 m_vkSwapchain = nullptr;
222 }
223
224 void Android_Window::sizeParam(int w, int h) {
225 m_width = w;
226 m_height = h;
227 }
228
229 void Android_Window::resize(int w, int h) {
230
231 }
232
234 m_running = false;
235 }
236
237 void Android_Window::onSurfaceCreate(jobject surface) {
238 m_nativeWindow = ANativeWindow_fromSurface(g_androidRuntime->getEnv(),surface);
239 ANativeWindow_acquire(m_nativeWindow);
240 fout << "onSurfaceCreate" << std::endl;
241
242 auto w = ANativeWindow_getWidth(m_nativeWindow);
243 auto h = ANativeWindow_getHeight(m_nativeWindow);
244
245 fout << "NativeWindow width: " << w << std::endl;
246 fout << "NativeWindow height: " << h << std::endl;
247
249 m_hasSurface = true;
250 }
251
252 void Android_Window::onSurfaceChanged(int width, int height) {
253
254 fout << "onSurfaceChanged" << std::endl;
255 }
256
259 ANativeWindow_release(m_nativeWindow);
260 fout << "onSurfaceDestroy" << std::endl;
261 m_hasSurface = false;
262 m_nativeSurfaceMutex.unlock();
263 }
264#ifdef FCT_USE_VULKAN
265
267 if (m_vulkanSurface) {
269 }
270 vk::AndroidSurfaceCreateInfoKHR createInfo;
271 createInfo.setWindow(m_nativeWindow);
272
273 auto vkInstance = (dynamic_cast<VK_Context*>(m_ctx))->getVkInstance();
274 m_vulkanSurface = vkInstance.createAndroidSurfaceKHR(createInfo);
275 }
276
282
284 if (m_vulkanSurface) {
285 auto vkInstance = (dynamic_cast<VK_Context*>(m_ctx))->getVkInstance();
286 vkInstance.destroySurfaceKHR(m_vulkanSurface);
287 m_vulkanSurface = nullptr;
288 }
289 }
290
292 if (m_vkSwapchain) {
293 m_vkSwapchain->present();
294 }
295 }
296
297
298#endif
299}
300extern "C"
301JNIEXPORT void JNICALL
303 jni::LocalObject<FCTWindowJavaClass> window(thiz);
304 FCT::Android_Window* pThis = (FCT::Android_Window*)window.Call<"getNativeWindowPtr">();
305 FCT::fout << "窗口关闭" << std::endl;
306 pThis->onDestory();
307}
308
309extern "C"
310JNIEXPORT void JNICALL
312 jlong native_window_ptr) {
313 FCT::Android_Window* wnd = (FCT::Android_Window*)native_window_ptr;
314 wnd->onSurfaceDestroy();
315 // TODO: implement onSUrfaceDestroyed()
316}
317extern "C"
318JNIEXPORT void JNICALL
320 jobject surface,
321 jlong native_window_ptr) {
322 // TODO: implement onSurfaceCreated()
323 FCT::Android_Window* wnd = (FCT::Android_Window*)native_window_ptr;
324 wnd->onSurfaceCreate(surface);
325}
326extern "C"
327JNIEXPORT void JNICALL
329 jlong native_window_ptr,
330 jint width, jint height) {
331 // TODO: implement onSurfaceChanged()
332 FCT::Android_Window* wnd = (FCT::Android_Window*)native_window_ptr;
333 wnd->onSurfaceChanged(width, height);
334}
std::ostream aout
JNIEXPORT void JNICALL Java_com_example_androidtutorial_FCTWindow_onDismiss(JNIEnv *env, jobject thiz)
JNIEXPORT void JNICALL Java_com_example_androidtutorial_FCTSurfaceView_onSurfaceCreated(JNIEnv *env, jobject thiz, jobject surface, jlong native_window_ptr)
JNIEXPORT void JNICALL Java_com_example_androidtutorial_FCTSurfaceView_onSUrfaceDestroyed(JNIEnv *env, jobject thiz, jlong native_window_ptr)
JNIEXPORT void JNICALL Java_com_example_androidtutorial_FCTSurfaceView_onSurfaceChanged(JNIEnv *env, jobject thiz, jlong native_window_ptr, jint width, jint height)
std::function< void()> createSurface
std::function< void()> present
std::function< void()> destorySurface
void viewport(Vec2 lt, Vec2 rb)
vk::SurfaceKHR m_vulkanSurface
void bind(FCT::Context *ctx) override
jni::GlobalObject< FCTWindowJavaClass > * m_windowObject
void onSurfaceCreate(jobject surface)
void setCursorPos(int x, int y) override
std::function< void()> recreateSwapChain
void sizeParam(int w, int h)
void onSurfaceChanged(int width, int height)
ANativeWindow * m_nativeWindow
bool isRunning() const override
void title(const std::string &title)
std::ostream & ferr
std::ostream & fout
Android_Runtime * g_androidRuntime