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 
108 {
109 public:
110  ScreenBufferData(const Size&, SDst);
111 
113 };
114 
116  : CompactPixmap({}, CheckStride(buf_stride, s.Width), s.Height)
117 {}
118 
119 
121  : ScreenBuffer(s, s.Width)
122 {}
124  : p_impl(new ScreenBufferData(s, buf_stride)), width(s.Width)
125 {}
127  : p_impl(new ScreenBufferData(std::move(*sbuf.p_impl))), width(sbuf.width)
128 {
129  sbuf.width = 0;
130 }
132 {}
133 
134 BitmapPtr
136 {
138  return p_impl->GetBufferPtr();
139 }
142 {
144  return p_impl->GetContext();
145 }
146 Size
148 {
150  return {width, p_impl->GetHeight()};
151 }
154 {
156  return p_impl->GetWidth();
157 }
158 
159 void
161 {
162  // TODO: Expand stride for given width using a proper strategy.
164  p_impl->SetSize(s);
165  width = s.Width;
166 }
167 
168 void
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
179 {
180  YAssertNonnull(p_impl);
181  p_impl->swap(*sbuf.p_impl),
182  std::swap(width, sbuf.width);
183 }
184 
185 
187  : ScreenRegionBuffer(s, s.Width)
188 {}
190  : ScreenBuffer(s, buf_stride),
191  mtx()
192 {}
193 
194 void
196 {
197  std::lock_guard<std::mutex> lck(mtx);
198 
200 }
201 
202 void
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
267 #endif
268  YTraceDe(Debug, "Creating: %p\n", static_cast<void*>(p_activity));
269  YTraceDe(Notice, "YSLib test succeeded.");
270 }
271 
Level FilterLevel
Definition: Debug.h:141
void ANativeActivity_onCreate(::ANativeActivity *p_activity, void *,::size_t)
Definition: Android.cpp:263
标准矩形像素图缓冲区。
Definition: ygdi.h:201
本机窗口引用。
Definition: Android.h:65
const YSLib::Drawing::Graphics & GetContext() const ynothrow
Definition: Android.cpp:141
ScreenBuffer(const YSLib::Drawing::Size &)
构造:使用指定的缓冲区大小和等于缓冲区宽的像素跨距。
Definition: Android.cpp:120
YSLib::SDst GetStride() const ynothrow
Definition: Android.cpp:153
YF_API::ALooper & FetchNativeLooper(bool) ythrow(Exception)
取当前线程的本机消息循环。
Definition: Android.cpp:220
yconstfn auto GetAreaOf(const Size &s) ynothrow-> decltype(s.Width *s.Height)
取面积。
Definition: ygdibase.h:404
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
YSLib::SDst width
宽:以像素数计量的缓冲区的实际宽度。
Definition: Android.h:149
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
std::unique_ptr< ScreenBufferData > p_impl
Definition: Android.h:144
YF_API void TraceConfiguration(::AConfiguration &, platform::Logger::Level=platform::Descriptions::Notice)
Definition: Android.cpp:231
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
std::exception Exception
YSLib 异常基类。
Definition: yexcept.h:44
DefGetter(ynothrow, ScreenBuffer &, ScreenBufferRef,*this) using ScreenBuffer voi UpdateFrom)(YSLib::Drawing::BitmapPtr) ynothrow
Definition: Android.h:234
虚拟屏幕缓存。
Definition: Android.h:137
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
yconstfn const string & name
Definition: Loader.h:110
void UpdateFrom(YSLib::Drawing::BitmapPtr) ynothrow
从缓冲区更新。
Definition: Android.cpp:169
YSLib::Drawing::Size GetSize() const ynothrow
Definition: Android.cpp:147
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
#define ythrow(...)
YSLib 动态异常规范:根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
Definition: Debug.cpp:212
YSLib::Drawing::BitmapPtr GetBufferPtr() const ynothrow
Definition: Android.cpp:135
#define YAssertNonnull(_expr)
Definition: cassert.h:81
RecordLevel
记录等级。
Definition: ycommon.h:69
二维图形接口上下文。
Definition: ygdibase.h:721
::ANativeWindow * NativeWindowHandle
Definition: Android.h:50
void UpdateTo(NativeWindowHandle, const YSLib::Drawing::Point &={}) ynothrow
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:269
PixelType * BitmapPtr
Definition: Video.h:295
YF_API bool CopyTo(BitmapPtr, const CompactPixmapEx &, const Size &, const Point &, const Point &, const Size &, Rotation=RDeg0)
位图缓冲区向指针指定的缓冲区复制。
Definition: ygdi.cpp:234
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
ScreenRegionBuffer(const YSLib::Drawing::Size &, YSLib::SDst)
构造:使用指定的缓冲区大小和像素跨距。 像素跨距小于缓冲区大小。
ScreenBufferData(const Size &, SDst)
Definition: Android.cpp:115
#define YB_LIKELY(expr)
Definition: ydef.h:297
void Resize(const YSLib::Drawing::Size &)
重新设置大小。
Definition: Android.cpp:160
void swap(ScreenBuffer &) ynothrow
交换。
Definition: Android.cpp:178
屏幕区域大小。
Definition: ygdibase.h:249