YSTest: Android.cpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
Android.cpp
浏览该文件的文档.
1 /*
2 © 2014 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 #include "YCLib/YModules.h"
30 #include YFM_Android_YCLib_Android
31 #include <android/native_activity.h>
32 #include "YSLib/Service/YModules.h"
33 #include YFM_YSLib_Service_YGDI
34 #include <android/configuration.h>
35
36 using namespace YSLib;
37 using namespace Drawing;
38
39 namespace platform_ex
40 {
41
42 namespace Android
43 {
44
45 namespace
46 {
47
49 template<typename _tDst, typename _type>
50 inline _tDst
51 CheckScalar(_type val, const std::string& name)
52 {
53 if(YB_UNLIKELY(val > std::numeric_limits<_tDst>::max()))
54 throw Exception(name + " value out of range.");
55 return _tDst(val);
56 }
57
59 template<typename _tDst, typename _type>
60 inline _tDst
61 CheckPositiveScalar(_type val, const std::string& name)
62 {
63 if(val < 0)
64 // XXX: Use more specified exception type.
65 throw Exception("Failed getting " + name + " value.");
66 return CheckScalar<_tDst>(val, name);
67 }
68
70 SDst
71 CheckStride(SDst buf_stride, SDst w)
72 {
73 if(buf_stride < w)
74 // XXX: Use more specified exception type.
75 throw Exception("Stride is small than width");
76 return buf_stride;
77 }
78
79 } // unnamed namespace;
80
81
82 SDst
83 WindowReference::GetWidth() const
84 {
85 return
86 CheckPositiveScalar<SDst>(::ANativeWindow_getWidth(hWindow), "width");
87 }
88 SDst
89 WindowReference::GetHeight() const
90 {
91 return
92 CheckPositiveScalar<SDst>(::ANativeWindow_getHeight(hWindow), "height");
93 }
94
95
96 HostWindow::HostWindow(NativeWindowHandle h_wnd)
97 : WindowReference(h_wnd)
98 {
99 ::ANativeWindow_acquire(h_wnd);
100 }
101 HostWindow::~HostWindow()
102 {
103 ::ANativeWindow_release(GetNativeHandle());
104 }
105
106
107 class ScreenBufferData : public CompactPixmap
108 {
109 public:
110 ScreenBufferData(const Size&, SDst);
111
112 DefDeMoveCtor(ScreenBufferData)
113 };
114
115 ScreenBufferData::ScreenBufferData(const Size& s, SDst buf_stride)
116 : CompactPixmap({}, CheckStride(buf_stride, s.Width), s.Height)
117 {}
118
119
120 ScreenBuffer::ScreenBuffer(const Size& s)
121 : ScreenBuffer(s, s.Width)
122 {}
123 ScreenBuffer::ScreenBuffer(const Size& s, SDst buf_stride)
124 : p_impl(new ScreenBufferData(s, buf_stride)), width(s.Width)
125 {}
126 ScreenBuffer::ScreenBuffer(ScreenBuffer&& sbuf) ynothrow
127 : p_impl(new ScreenBufferData(std::move(*sbuf.p_impl))), width(sbuf.width)
128 {
129 sbuf.width = 0;
130 }
131 ScreenBuffer::~ScreenBuffer()
132 {}
133
134 BitmapPtr
135 ScreenBuffer::GetBufferPtr() const ynothrow
136 {
137 YAssertNonnull(p_impl);
138 return p_impl->GetBufferPtr();
139 }
140 const YSLib::Drawing::Graphics&
141 ScreenBuffer::GetContext() const ynothrow
142 {
143 YAssertNonnull(p_impl);
144 return p_impl->GetContext();
145 }
146 Size
147 ScreenBuffer::GetSize() const ynothrow
148 {
149 YAssertNonnull(p_impl);
150 return {width, p_impl->GetHeight()};
151 }
152 YSLib::SDst
153 ScreenBuffer::GetStride() const ynothrow
154 {
155 YAssertNonnull(p_impl);
156 return p_impl->GetWidth();
157 }
158
159 void
160 ScreenBuffer::Resize(const Size& s)
161 {
162 // TODO: Expand stride for given width using a proper strategy.
163 YAssertNonnull(p_impl);
164 p_impl->SetSize(s);
165 width = s.Width;
166 }
167
168 void
169 ScreenBuffer::UpdateFrom(BitmapPtr p_buf) ynothrow
170 {
171 // TODO: Expand stride for given width using a proper strategy.
172 YAssertNonnull(p_buf),
173 YAssertNonnull(p_impl);
174 std::copy_n(p_buf, GetAreaOf(GetSize()), p_impl->GetBufferPtr());
175 }
176
177 void
178 ScreenBuffer::swap(ScreenBuffer& sbuf) ynothrow
179 {
180 YAssertNonnull(p_impl);
181 p_impl->swap(*sbuf.p_impl),
182 std::swap(width, sbuf.width);
183 }
184
185
186 ScreenRegionBuffer::ScreenRegionBuffer(const Size& s)
187 : ScreenRegionBuffer(s, s.Width)
188 {}
189 ScreenRegionBuffer::ScreenRegionBuffer(const Size& s, SDst buf_stride)
190 : ScreenBuffer(s, buf_stride),
191 mtx()
192 {}
193
194 void
195 ScreenRegionBuffer::UpdateFrom(BitmapPtr buf) ynothrow
196 {
197 std::lock_guard<std::mutex> lck(mtx);
198
199 ScreenBuffer::UpdateFrom(buf);
200 }
201
202 void
203 ScreenRegionBuffer::UpdateTo(NativeWindowHandle h_wnd, const Point& pt) ynothrow
204 {
205 YAssertNonnull(h_wnd);
206
207 const Size& s(GetSize());
208 ::ANativeWindow_Buffer abuf;
209 ::ARect arect{pt.X, pt.Y, pt.X + s.Width, pt.Y + s.Height};
210 std::lock_guard<std::mutex> lck(mtx);
211
212 ::ANativeWindow_lock(h_wnd, &abuf, &arect);
213 CopyTo(static_cast<BitmapPtr>(abuf.bits), GetContext(),
214 WindowReference(h_wnd).GetSize(), {}, {}, s);
215 ::ANativeWindow_unlockAndPost(h_wnd);
216 }
217
218
219 ::ALooper&
220 FetchNativeLooper(bool allow_non_callbacks) ythrow(Exception)
221 {
222 const auto p(::ALooper_prepare(allow_non_callbacks
223 ? ALOOPER_PREPARE_ALLOW_NON_CALLBACKS : 0));
224
225 if(YB_LIKELY(p))
226 return *p;
227 throw Exception("Failed get native looper pointer.");
228 }
229
230 void
231 TraceConfiguration(::AConfiguration& cfg, platform::Logger::Level lv)
232 {
233 char lang[2], country[2];
234
235 ::AConfiguration_getLanguage(&cfg, lang);
236 ::AConfiguration_getCountry(&cfg, country);
237 YTraceDe(lv, "Configuration: MCC = %d, MNC = %d, language code = %c%c,"
238 " country = %c%c, orientation = %d, touch screen = %d, density = %d, "
239 " keyboard = %d, navigation = %d, keysHidden = %d, navHidden = %d,"
240 " SDK version = %d, screen size = %d, screen long = %d,"
241 " UI mode type = %d, UI mode night = %d.",
242 int(::AConfiguration_getMcc(&cfg)), int(::AConfiguration_getMnc(&cfg)),
243 lang[0], lang[1], country[0], country[1],
244 int(::AConfiguration_getOrientation(&cfg)),
245 int(::AConfiguration_getTouchscreen(&cfg)),
246 int(::AConfiguration_getDensity(&cfg)),
247 int(::AConfiguration_getKeyboard(&cfg)),
248 int(::AConfiguration_getNavigation(&cfg)),
249 int(::AConfiguration_getKeysHidden(&cfg)),
250 int(::AConfiguration_getNavHidden(&cfg)),
251 int(::AConfiguration_getSdkVersion(&cfg)),
252 int(::AConfiguration_getScreenSize(&cfg)),
253 int(::AConfiguration_getScreenLong(&cfg)),
254 int(::AConfiguration_getUiModeType(&cfg)),
255 int(::AConfiguration_getUiModeNight(&cfg)));
256 }
257
258 } // namespace Android;
259
260 } // namespace YSLib;
261
262 extern "C" void
263 ANativeActivity_onCreate(::ANativeActivity* p_activity, void*, ::size_t)
264 {
265 #ifndef NDEBUG
266 platform::FetchCommonLogger().FilterLevel = platform::Descriptions::Debug;
267 #endif
268 YTraceDe(Debug, "Creating: %p\n", static_cast<void*>(p_activity));
269 YTraceDe(Notice, "YSLib test succeeded.");
270 }
271
platform::Descriptions::Debug
Definition: ycommon.h:78
platform::Logger::FilterLevel
Level FilterLevel
Definition: Debug.h:141
ANativeActivity_onCreate
void ANativeActivity_onCreate(::ANativeActivity *p_activity, void *,::size_t)
Definition: Android.cpp:263
YSLib::Drawing::CompactPixmap
标准矩形像素图缓冲区。
Definition: ygdi.h:201
platform_ex::Android::WindowReference
本机窗口引用。
Definition: Android.h:65
YModules.h
platform_ex::Android::ScreenBuffer::GetContext
const YSLib::Drawing::Graphics & GetContext() const ynothrow
Definition: Android.cpp:141
platform_ex::Android::ScreenBuffer::ScreenBuffer
ScreenBuffer(const YSLib::Drawing::Size &)
构造：使用指定的缓冲区大小和等于缓冲区宽的像素跨距。
Definition: Android.cpp:120
platform_ex::Android::ScreenBuffer::GetStride
YSLib::SDst GetStride() const ynothrow
Definition: Android.cpp:153
platform_ex::Android::FetchNativeLooper
YF_API::ALooper & FetchNativeLooper(bool) ythrow(Exception)
取当前线程的本机消息循环。
Definition: Android.cpp:220
YSLib::Drawing::GetAreaOf
yconstfn auto GetAreaOf(const Size &s) ynothrow-> decltype(s.Width *s.Height)
取面积。
Definition: ygdibase.h:404
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
platform_ex::Android::ScreenBuffer::~ScreenBuffer
~ScreenBuffer()
Definition: Android.cpp:131
platform_ex::Android::ScreenBuffer::width
YSLib::SDst width
宽：以像素数计量的缓冲区的实际宽度。
Definition: Android.h:149
ystdex::swap
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
YB_UNLIKELY
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
platform_ex::Android::ScreenBuffer::p_impl
std::unique_ptr< ScreenBufferData > p_impl
Definition: Android.h:144
platform_ex::Android::TraceConfiguration
YF_API void TraceConfiguration(::AConfiguration &, platform::Logger::Level=platform::Descriptions::Notice)
Definition: Android.cpp:231
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
YSLib::Exception
std::exception Exception
YSLib 异常基类。
Definition: yexcept.h:44
platform_ex::Android::UpdateFrom
DefGetter(ynothrow, ScreenBuffer &, ScreenBufferRef,*this) using ScreenBuffer voi UpdateFrom)(YSLib::Drawing::BitmapPtr) ynothrow
Definition: Android.h:234
platform_ex::Android::ScreenBuffer
虚拟屏幕缓存。
Definition: Android.h:137
platform::Descriptions::Notice
Definition: ycommon.h:76
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:110
platform_ex::Android::ScreenBuffer::UpdateFrom
void UpdateFrom(YSLib::Drawing::BitmapPtr) ynothrow
从缓冲区更新。
Definition: Android.cpp:169
platform_ex::Android::ScreenBuffer::GetSize
YSLib::Drawing::Size GetSize() const ynothrow
Definition: Android.cpp:147
YSLib::string
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
ythrow
#define ythrow(...)
YSLib 动态异常规范：根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
YSLib::Drawing::GBinaryGroup< SPos >
platform::FetchCommonLogger
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
Definition: Debug.cpp:212
platform_ex::Android::ScreenBuffer::GetBufferPtr
YSLib::Drawing::BitmapPtr GetBufferPtr() const ynothrow
Definition: Android.cpp:135
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:81
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: ycommon.h:69
YSLib::Drawing::Graphics
二维图形接口上下文。
Definition: ygdibase.h:721
platform_ex::NativeWindowHandle
::ANativeWindow * NativeWindowHandle
Definition: Android.h:50
platform_ex::Android::ScreenBufferData
Definition: Android.cpp:107
platform_ex::Android::UpdateTo
void UpdateTo(NativeWindowHandle, const YSLib::Drawing::Point &={}) ynothrow
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:269
platform_ex::Exception
宿主异常。
Definition: E/include/YCLib/MinGW32.h:55
platform::BitmapPtr
PixelType * BitmapPtr
Definition: Video.h:295
YSLib::Drawing::CopyTo
YF_API bool CopyTo(BitmapPtr, const CompactPixmapEx &, const Size &, const Point &, const Point &, const Size &, Rotation=RDeg0)
位图缓冲区向指针指定的缓冲区复制。
Definition: ygdi.cpp:234
YSLib::Drawing::CompactPixmap::SDst
SDst
Definition: ygdi.h:212
YSLib::Drawing::Width
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
platform_ex::Android::ScreenRegionBuffer
ScreenRegionBuffer(const YSLib::Drawing::Size &, YSLib::SDst)
构造：使用指定的缓冲区大小和像素跨距。 像素跨距小于缓冲区大小。
platform_ex::Android::ScreenBufferData::ScreenBufferData
ScreenBufferData(const Size &, SDst)
Definition: Android.cpp:115
YB_LIKELY
#define YB_LIKELY(expr)
Definition: ydef.h:297
platform_ex::Android::ScreenBuffer::Resize
void Resize(const YSLib::Drawing::Size &)
重新设置大小。
Definition: Android.cpp:160
YModules.h
platform_ex::Android::ScreenBuffer::swap
void swap(ScreenBuffer &) ynothrow
交换。
Definition: Android.cpp:178
YSLib::Drawing::Size
屏幕区域大小。
Definition: ygdibase.h:249
YSLib::Drawing::Size::Width
SDst Width
Definition: ygdibase.h:258