YSTest: AndroidHost.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

AndroidHost.h
浏览该文件的文档.
1 /*
2 © 2014-2016, 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
29 #ifndef INC_Android_Helper_AndroidHost_h_
30 #define INC_Android_Helper_AndroidHost_h_ 1
31
32 #include "YModules.h"
33 #include YFM_Helper_Environment // for function, unique_ptr, std::atomic,
34 // Devices::AndroidScreen, ystdex::aligned_store_cast;
35 #if YCL_Android
36 # include <thread>
37 # include <android/native_activity.h>
38 # include <android/configuration.h>
39 # include YFM_YCLib_JNI
40 # include YFM_YCLib_Host
41 #else
42 //# error "Currently only Android is supported."
43 #endif
44
45 namespace YSLib
46 {
47
48 #if YCL_Android
49 namespace Android
50 {
51
53 namespace JNI = platform_ex::JNI;
55 using platform_ex::UniqueHandle;
56
61 struct YF_API ConfigurationDeleter
62 {
64 using pointer = ::AConfiguration*;
65
67 PDefHOp(void, (), pointer p) const ynothrow
68 ImplRet(::AConfiguration_delete(p))
69 };
70
71
77 class YF_API NativeHost
78 {
79 protected:
86 std::thread thrdMain{};
88 vector<byte> vecSavedState{};
90 function<void(void*&, size_t&)> fSaveState{};
91
92 public:
98 int MainReturnValue = EXIT_SUCCESS;
99
100 private:
102 lref<::ANativeActivity> activity;
103 unique_ptr<::AConfiguration, ConfigurationDeleter> p_config;
105 lref<::ALooper> looper;
106 unique_ptr<InputQueue> p_input_queue{};
108
109 mutex msg_mutex{};
111 pair<UniqueHandle, UniqueHandle> msg_pipe;
113 function<void()> msg_task{};
115
119 mutable mutex state_mutex{};
124 std::atomic<bool> paused{{}};
126 unique_ptr<Devices::AndroidScreen> p_screen;
127
128 public:
129 NativeHost(::ANativeActivity&, void*, size_t);
130 ~NativeHost();
131
133 DefPred(const ynothrow, Paused, paused)
135 DefGetter(const ynothrow, ::ANativeActivity&, ActivityRef, activity.get())
137 Devices::Screen&
138 GetHostScreenRef() const ynothrow;
139 DefGetter(const ynothrow, Devices::AndroidScreen&, ScreenRef,
140 Deref(p_screen))
141
142
146 template<typename _type>
147 void
148 BindSavedState(const _type& state)
149 {
150 // TODO: Wait for 'std::is_trivially_copyable' to be implemented.
151 // static_assert(std::is_trivially_copyable<ystdex::remove_reference_t<
152 // _type>>(), "Invalid state type found.");
153 lock_guard<mutex> lck(state_mutex);
154
155 fSaveState = [&](void*& p_saved_state, size_t& saved_size){
156 p_saved_state = std::malloc(sizeof(_type));
157 if(p_saved_state)
158 // TODO: Use inherited class of exception.
159 throw std::bad_alloc();
160 new(p_saved_state) _type(state);
161 saved_size = sizeof(_type);
162 };
163 }
164
165 void
166 ClearSavedStateHandler();
167
168 void
169 LoadConfiguration();
170
171 void
172 ReleaseSavedState() ynothrow;
173
175 void
176 ResizeScreen(const Drawing::Size&);
177
178 template<typename _type>
179 void
180 Restore(_type& state) const
181 {
182 // TODO: Wait for 'std::is_trivially_copyable' to be implemented.
183 // static_assert(std::is_trivially_copyable<ystdex::remove_reference_t<
184 // _type>>(), "Invalid state type found.");
185
186 RestoreSavedState(ystdex::aligned_store_cast<byte*>(&state));
187 }
188
194 void
195 RestoreSavedState(byte* p_byte) const;
196
198 void
199 RunOnUIThread(function<void()>);
200
201 void*
202 SaveInstanceState(size_t* p_len);
203 };
204
206 YF_API ::ANativeWindow&
207 FetchDefaultWindow() ynothrow;
208
209 YF_API NativeHost&
210 FetchNativeHostInstance() ynothrow;
211
212 } // namespace Android;
213 #endif
214
215 } // namespace YSLib;
216 #endif
217
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:232
ystdex::single_thread::mutex
Definition: pseudo_mutex.h:747
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:536
ystdex::lref
左值引用包装。
Definition: ref.hpp:61
YF_API
#define YF_API
Definition: Platform.h:56
ystdex::threading::lock_guard
Definition: pseudo_mutex.h:223
platform::containers::vector
std::vector< _type, _tAlloc > vector
Definition: Container.h:102
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YSLib::LoadConfiguration
YF_API ValueNode LoadConfiguration(bool={})
载入默认配置。
Definition: Initialization.cpp:730
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
ystdex::lref::get
_type & get() const
Definition: ref.hpp:96
platform::Deref
YB_PURE yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:547
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
platform::p
return p p
Definition: YCLib/FileSystem.h:209
YSLib::Devices::Screen
屏幕。
Definition: YDevice.h:133