YSTest: HostRenderer.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

HostRenderer.h
浏览该文件的文档.
1 /*
2 © 2013-2016 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
28 #ifndef INC_Helper_HostRenderer_h_
29 #define INC_Helper_HostRenderer_h_ 1
30
31 #include "YModules.h"
32 #include YFM_Helper_HostWindow // for Host::Window, ystdex::unimplemented;
33 #if YF_Multithread == 1
34 # include <atomic>
35 # include <thread>
36 #endif
37 #include YFM_YSLib_UI_YRenderer // for UI::BufferedRenderer;
38 #include YFM_YSLib_UI_YWidget // for UI::GetSizeOf;
39 #include YFM_Helper_ScreenBuffer // for ScreenRegionBuffer;
40
41 namespace YSLib
42 {
43
44 #if YF_Hosted
45 namespace Host
46 {
47
52 class YF_API RenderWindow : public Window
53 {
54 private:
56 lref<HostRenderer> renderer;
57
58 public:
59 RenderWindow(HostRenderer&, NativeWindowHandle);
61 RenderWindow(HostRenderer& rd, Host::WindowReference wnd_ref)
62 : RenderWindow(rd, wnd_ref.GetNativeHandle())
63 {}
64
65 DefGetter(const ynothrow, HostRenderer&, Renderer, renderer)
66
67
72 void
73 Refresh() override;
74 };
75
76
84 class YF_API WindowThread : private OwnershipTag<Window>
85 {
86 public:
88 using Guard = ystdex::any;
90 using GuardGenerator = std::function<Guard(Window&)>;
91
92 private:
98 std::atomic<Window*> p_window{{}};
99 std::thread thrd;
100
101 public:
106 GuardGenerator GenerateGuard{};
107
108 template<typename... _tParams>
109 WindowThread(_tParams&&... args)
110 : WindowThread({}, yforward(args)...)
111 {}
113 template<typename... _tParams>
114 WindowThread(GuardGenerator guard_gen, _tParams&&... args)
115 : thrd(&WindowThread::ThreadFunc<_tParams...>, this, yforward(args)...),
116 GenerateGuard(guard_gen)
117 {}
119 DefDelMoveCtor(WindowThread)
124 ~WindowThread();
125
130 DefGetter(const ynothrow, observer_ptr<Window>, WindowPtr,
131 make_observer(static_cast<Window*>(p_window)))
132
133
139 static Guard
140 DefaultGenerateGuard(Window&);
141
142 private:
144 template<typename _func, typename... _tParams>
145 void
146 ThreadFunc(_func&& f, _tParams&&... args) ynothrow
147 {
148 FilterExceptions([&, this]{
149 // XXX: Blocked. 'yforward' cause G++ 5.2 crash: segmentation
150 // fault.
151 ThreadLoop(ystdex::invoke(std::forward<_func&&>(f),
152 std::forward<_tParams&&>(args)...));
153 });
154 }
155
157 void
158 ThreadLoop(NativeWindowHandle);
160 PDefH(void, ThreadLoop, WindowReference wnd_ref)
161 ImplRet(ThreadLoop(wnd_ref.GetNativeHandle()))
162 void
163 ThreadLoop(unique_ptr<Window>);
164
165 public:
166 static void
167 WindowLoop(Window&);
168
169 private:
171 static void
172 WindowLoop(Window&, GuardGenerator);
173 };
174
175
182 class YF_API HostRenderer : public UI::BufferedRenderer
183 {
184 private:
186 lref<UI::IWidget> widget;
188 ScreenRegionBuffer rbuf;
189 WindowThread thrd;
190
191 public:
196 bool RootMode = {};
197
202 template<typename... _tParams>
203 HostRenderer(UI::IWidget& wgt, _tParams&&... args)
204 : HostRenderer(ystdex::identity<RenderWindow>(), wgt, yforward(args)...)
205 {}
207
208 template<class _tWindow, typename... _tParams>
210 HostRenderer(ystdex::identity<_tWindow>, UI::IWidget& wgt,
211 _tParams&&... args)
212 : HostRenderer(ystdex::empty_base<WindowThread>(), wgt,
213 std::mem_fn(&HostRenderer::MakeRenderWindow<
214 _tWindow, ystdex::decay_t<_tParams>...>), this, yforward(args)...)
215 {}
217 template<class _tWindow, typename... _tParams>
218 HostRenderer(ystdex::identity<_tWindow>, UI::IWidget& wgt,
219 WindowThread::GuardGenerator guard_gen, _tParams&&... args)
220 : HostRenderer(ystdex::empty_base<WindowThread>(), wgt, guard_gen,
221 std::mem_fn(&HostRenderer::MakeRenderWindow<_tWindow,
222 ystdex::decay_t<_tParams>...>), this, yforward(args)...)
223 {}
225 template<typename... _tParams>
226 HostRenderer(int, UI::IWidget& wgt, _tParams&&... args)
227 : HostRenderer(0, ystdex::identity<RenderWindow>(), wgt,
228 yforward(args)...)
229 {}
231 template<class _tWindow, typename... _tParams>
232 HostRenderer(int, ystdex::identity<_tWindow>, UI::IWidget& wgt,
233 _tParams&&... args)
234 : HostRenderer(ystdex::empty_base<WindowThread>(), wgt,
235 std::mem_fn(&HostRenderer::MakeRenderWindowEx<
236 _tWindow, ystdex::decay_t<_tParams>...>), this, yforward(args)...)
237 {}
239 template<class _tWindow, typename... _tParams>
240 HostRenderer(int, ystdex::identity<_tWindow>, UI::IWidget& wgt,
241 WindowThread::GuardGenerator guard_gen, _tParams&&... args)
242 : HostRenderer(ystdex::empty_base<WindowThread>(), wgt, guard_gen,
243 std::mem_fn(&HostRenderer::MakeRenderWindowEx<_tWindow,
244 ystdex::decay_t<_tParams>...>), this, yforward(args)...)
245 {}
247
254 ~HostRenderer();
255
256 private:
258 template<typename... _tParams>
259 HostRenderer(ystdex::empty_base<WindowThread>, UI::IWidget& wgt,
260 _tParams&&... args)
261 : BufferedRenderer(),
262 widget(wgt), rbuf(GetSizeOf(wgt)), thrd(yforward(args)...)
263 {
264 InitWidgetView();
265 }
266
267 public:
268 DefDeMoveCtor(HostRenderer)
269
270
271 DefGetter(ynothrow, ScreenRegionBuffer&, BufferRef, rbuf)
272 DefGetter(const ynothrow, UI::IWidget&, WidgetRef, widget.get())
274 DefGetterMem(const ynothrow, observer_ptr<Window>, WindowPtr, thrd)
275
282 void
283 SetSize(const Drawing::Size&) override;
284
293 bool
294 AdjustSize();
295
296 private:
305 void
306 InitWidgetView();
307
308 public:
314 template<class _tWindow, typename _func, typename... _tParams>
315 unique_ptr<Window>
316 MakeRenderWindow(_func&& f, _tParams&&... args)
317 {
318 return make_unique<_tWindow>(*this, yforward(f)(yforward(args)...));
319 }
320
321 template<class _tWindow, typename _func, typename... _tParams>
322 unique_ptr<Window>
323 MakeRenderWindowEx(_func&& f, _tParams&&... args)
324 {
325 return make_unique<_tWindow>(*this, yforward(f)(), yforward(args)...);
326 }
328
335 void
336 RefreshForWidget();
337
349 YB_NONNULL(1) void
350 Update(Drawing::ConstBitmapPtr, const Drawing::Rect&);
351
353 template<typename _type>
354 void
355 UpdateToSurface(_type& sf)
356 {
357 sf.Update(Deref(rbuf.Lock()));
358 }
359
360 #if YCL_Win32
361 template<typename _type, typename... _tParams>
363 void
364 UpdateBoundsToSurface(_type& sf, _tParams&&... args)
365 {
366 sf.UpdateBounds(Deref(rbuf.Lock()), yforward(args)...);
367 }
368 #endif
369
374 Window&
375 Wait();
376
378 YB_NORETURN PDefH(HostRenderer*, clone, ) const override
379 ImplThrow(ystdex::unimplemented("HostRenderer::clone unimplemented."))
380 // DefClone(const override, HostRenderer)
381 };
382
383 } // namespace Host;
384 #endif
385
386 } // namespace YSLib;
387
388 #endif
389
YB_NORETURN
#define YB_NORETURN
指定无返回值函数。
Definition: ydef.h:478
ystdex::identity
恒等元函数。
Definition: type_traits.hpp:961
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
ystdex::any_ops::clone
Definition: any.h:253
YSLib::PDefH
PDefH(Environment &, FetchEnvironment,) ImplRet(FetchGlobalInstance().GetEnvironmentRef()) inline PDefH(GUIHost &
取单一环境对象引用。
YSLib::UI::Styles::Window
窗口背景。
Definition: YStyle.h:311
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:202
ystdex::invoke
result_of_t< _fCallable &&(_tParams &&...)> invoke(_fCallable &&f, _tParams &&...args)
调用可调用对象。
Definition: functional.hpp:337
DefGetterMem
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:246
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
YSLib::Drawing::ConstBitmapPtr
const Pixel * ConstBitmapPtr
Definition: YGraphics.h:44
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:823
std
Definition: memory.hpp:1100
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:440
YSLib::UI::wgt
IWidget & wgt
Definition: YControl.h:112
NPL::Guard
ystdex::any Guard
作用域守护类型。
Definition: NPLA.h:546
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: type_traits.hpp:347
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:166
Drawing
UI
YSLib::YB_NONNULL
YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
ystdex::empty_base
空基类模板。
Definition: ydef.h:902
YModules.h
YSLib::UI::GetSizeOf
pt pt Y const IWidget &wgt const IWidget &wgt GetSizeOf
无效化：使相对部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.h:161
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: memory.hpp:1042
YF_API
#define YF_API
Definition: Platform.h:56
ImplThrow
#define ImplThrow(...)
Definition: YBaseMacro.h:105
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
DefDelMoveCtor
#define DefDelMoveCtor(_t)
Definition: YBaseMacro.h:168