App.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "App.h"
  2. #include <ppltasks.h>
  3. using namespace TemplateApp;
  4. using namespace concurrency;
  5. using namespace Windows::ApplicationModel;
  6. using namespace Windows::ApplicationModel::Core;
  7. using namespace Windows::ApplicationModel::Activation;
  8. using namespace Windows::Foundation::Collections;
  9. using namespace Windows::UI::Core;
  10. using namespace Windows::UI::Input;
  11. using namespace Windows::System;
  12. using namespace Windows::Storage;
  13. using namespace Windows::Foundation;
  14. using namespace Windows::Graphics::Display;
  15. using namespace Platform;
  16. // The main function is only used to initialize our IFrameworkView class.
  17. [Platform::MTAThread]
  18. int main(Platform::Array<Platform::String^>^)
  19. {
  20. auto direct3DApplicationSource = ref new Direct3DApplicationSource();
  21. CoreApplication::Run(direct3DApplicationSource);
  22. return 0;
  23. }
  24. IFrameworkView^ Direct3DApplicationSource::CreateView()
  25. {
  26. return ref new App();
  27. }
  28. App::App() :
  29. m_windowClosed(false),
  30. m_windowVisible(true)
  31. {
  32. }
  33. // The first method called when the IFrameworkView is being created.
  34. void App::Initialize(CoreApplicationView^ applicationView)
  35. {
  36. // Register event handlers for app lifecycle. This example includes Activated, so that we
  37. // can make the CoreWindow active and start rendering on the window.
  38. applicationView->Activated +=
  39. ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
  40. CoreApplication::Suspending +=
  41. ref new Windows::Foundation::EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
  42. CoreApplication::Resuming +=
  43. ref new Windows::Foundation::EventHandler<Platform::Object^>(this, &App::OnResuming);
  44. }
  45. // Called when the CoreWindow object is created (or re-created).
  46. void App::SetWindow(CoreWindow^ window)
  47. {
  48. window->SizeChanged +=
  49. ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &App::OnWindowSizeChanged);
  50. window->VisibilityChanged +=
  51. ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
  52. window->Closed +=
  53. ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
  54. DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
  55. currentDisplayInformation->DpiChanged +=
  56. ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDpiChanged);
  57. currentDisplayInformation->OrientationChanged +=
  58. ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnOrientationChanged);
  59. DisplayInformation::DisplayContentsInvalidated +=
  60. ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDisplayContentsInvalidated);
  61. PolycodeView *view = new PolycodeView();
  62. view->window = reinterpret_cast<IUnknown*>(window);
  63. const EGLint configAttributes[] =
  64. {
  65. EGL_RED_SIZE, 8,
  66. EGL_GREEN_SIZE, 8,
  67. EGL_BLUE_SIZE, 8,
  68. EGL_ALPHA_SIZE, 8,
  69. EGL_DEPTH_SIZE, 8,
  70. EGL_STENCIL_SIZE, 8,
  71. EGL_NONE
  72. };
  73. const EGLint contextAttributes[] =
  74. {
  75. EGL_CONTEXT_CLIENT_VERSION, 2,
  76. EGL_NONE
  77. };
  78. const EGLint surfaceAttributes[] =
  79. {
  80. // EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER is part of the same optimization as EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER (see above).
  81. // If you have compilation issues with it then please update your Visual Studio templates.
  82. EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER, EGL_TRUE,
  83. EGL_NONE
  84. };
  85. const EGLint defaultDisplayAttributes[] =
  86. {
  87. // These are the default display attributes, used to request ANGLE's D3D11 renderer.
  88. // eglInitialize will only succeed with these attributes if the hardware supports D3D11 Feature Level 10_0+.
  89. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  90. // EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER is an optimization that can have large performance benefits on mobile devices.
  91. // Its syntax is subject to change, though. Please update your Visual Studio templates if you experience compilation issues with it.
  92. EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
  93. // EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE is an option that enables ANGLE to automatically call
  94. // the IDXGIDevice3::Trim method on behalf of the application when it gets suspended.
  95. // Calling IDXGIDevice3::Trim when an application is suspended is a Windows Store application certification requirement.
  96. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
  97. EGL_NONE,
  98. };
  99. const EGLint fl9_3DisplayAttributes[] =
  100. {
  101. // These can be used to request ANGLE's D3D11 renderer, with D3D11 Feature Level 9_3.
  102. // These attributes are used if the call to eglInitialize fails with the default display attributes.
  103. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  104. EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
  105. EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
  106. EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
  107. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
  108. EGL_NONE,
  109. };
  110. const EGLint warpDisplayAttributes[] =
  111. {
  112. // These attributes can be used to request D3D11 WARP.
  113. // They are used if eglInitialize fails with both the default display attributes and the 9_3 display attributes.
  114. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  115. EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE,
  116. EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
  117. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
  118. EGL_NONE,
  119. };
  120. EGLConfig config = NULL;
  121. // eglGetPlatformDisplayEXT is an alternative to eglGetDisplay. It allows us to pass in display attributes, used to configure D3D11.
  122. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
  123. if (!eglGetPlatformDisplayEXT)
  124. {
  125. throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT");
  126. }
  127. //
  128. // To initialize the display, we make three sets of calls to eglGetPlatformDisplayEXT and eglInitialize, with varying
  129. // parameters passed to eglGetPlatformDisplayEXT:
  130. // 1) The first calls uses "defaultDisplayAttributes" as a parameter. This corresponds to D3D11 Feature Level 10_0+.
  131. // 2) If eglInitialize fails for step 1 (e.g. because 10_0+ isn't supported by the default GPU), then we try again
  132. // using "fl9_3DisplayAttributes". This corresponds to D3D11 Feature Level 9_3.
  133. // 3) If eglInitialize fails for step 2 (e.g. because 9_3+ isn't supported by the default GPU), then we try again
  134. // using "warpDisplayAttributes". This corresponds to D3D11 Feature Level 11_0 on WARP, a D3D11 software rasterizer.
  135. //
  136. // This tries to initialize EGL to D3D11 Feature Level 10_0+. See above comment for details.
  137. mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes);
  138. if (mEglDisplay == EGL_NO_DISPLAY)
  139. {
  140. throw Exception::CreateException(E_FAIL, L"Failed to get EGL display");
  141. }
  142. if (eglInitialize(mEglDisplay, NULL, NULL) == EGL_FALSE)
  143. {
  144. // This tries to initialize EGL to D3D11 Feature Level 9_3, if 10_0+ is unavailable (e.g. on some mobile devices).
  145. mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes);
  146. if (mEglDisplay == EGL_NO_DISPLAY)
  147. {
  148. throw Exception::CreateException(E_FAIL, L"Failed to get EGL display");
  149. }
  150. if (eglInitialize(mEglDisplay, NULL, NULL) == EGL_FALSE)
  151. {
  152. // This initializes EGL to D3D11 Feature Level 11_0 on WARP, if 9_3+ is unavailable on the default GPU.
  153. mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes);
  154. if (mEglDisplay == EGL_NO_DISPLAY)
  155. {
  156. throw Exception::CreateException(E_FAIL, L"Failed to get EGL display");
  157. }
  158. if (eglInitialize(mEglDisplay, NULL, NULL) == EGL_FALSE)
  159. {
  160. // If all of the calls to eglInitialize returned EGL_FALSE then an error has occurred.
  161. throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL");
  162. }
  163. }
  164. }
  165. EGLint numConfigs = 0;
  166. if ((eglChooseConfig(mEglDisplay, configAttributes, &config, 1, &numConfigs) == EGL_FALSE) || (numConfigs == 0))
  167. {
  168. throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig");
  169. }
  170. // Create a PropertySet and initialize with the EGLNativeWindowType.
  171. PropertySet^ surfaceCreationProperties = ref new PropertySet();
  172. surfaceCreationProperties->Insert(ref new Platform::String(EGLNativeWindowTypeProperty), window);
  173. // You can configure the surface to render at a lower resolution and be scaled up to
  174. // the full window size. This scaling is often free on mobile hardware.
  175. //
  176. // One way to configure the SwapChainPanel is to specify precisely which resolution it should render at.
  177. // Size customRenderSurfaceSize = Size(800, 600);
  178. // surfaceCreationProperties->Insert(ref new String(EGLRenderSurfaceSizeProperty), PropertyValue::CreateSize(customRenderSurfaceSize));
  179. //
  180. // Another way is to tell the SwapChainPanel to render at a certain scale factor compared to its size.
  181. // e.g. if the SwapChainPanel is 1920x1280 then setting a factor of 0.5f will make the app render at 960x640
  182. // float customResolutionScale = 0.5f;
  183. // surfaceCreationProperties->Insert(ref new String(EGLRenderResolutionScaleProperty), PropertyValue::CreateSingle(customResolutionScale));
  184. mEglSurface = eglCreateWindowSurface(mEglDisplay, config, reinterpret_cast<IInspectable*>(surfaceCreationProperties), surfaceAttributes);
  185. if (mEglSurface == EGL_NO_SURFACE)
  186. {
  187. throw Exception::CreateException(E_FAIL, L"Failed to create EGL fullscreen surface");
  188. }
  189. mEglContext = eglCreateContext(mEglDisplay, config, EGL_NO_CONTEXT, contextAttributes);
  190. if (mEglContext == EGL_NO_CONTEXT)
  191. {
  192. throw Exception::CreateException(E_FAIL, L"Failed to create EGL context");
  193. }
  194. /*
  195. surfaceCreationProperties = ref new PropertySet();
  196. surfaceCreationProperties->Insert(ref new Platform::String(EGLNativeWindowTypeProperty), window);
  197. view->eglWindow = reinterpret_cast<IInspectable*>(surfaceCreationProperties);
  198. */
  199. view->mEglContext = mEglContext;
  200. view->mEglDisplay = mEglDisplay;
  201. view->mEglSurface = mEglSurface;
  202. app = new PolycodeTemplateApp(view);
  203. }
  204. // Initializes scene resources, or loads a previously saved app state.
  205. void App::Load(Platform::String^ entryPoint)
  206. {
  207. }
  208. // This method is called after the window becomes active.
  209. void App::Run()
  210. {
  211. while (!m_windowClosed)
  212. {
  213. if (m_windowVisible)
  214. {
  215. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  216. }
  217. else
  218. {
  219. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  220. }
  221. EGLint panelWidth = 0;
  222. EGLint panelHeight = 0;
  223. eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &panelWidth);
  224. eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &panelHeight);
  225. if (panelWidth != lastPanelWidth || panelHeight != lastPanelHeight) {
  226. Services()->getCore()->resizeTo(panelWidth, panelHeight);
  227. }
  228. lastPanelWidth = panelWidth;
  229. lastPanelHeight = panelHeight;
  230. app->Update();
  231. }
  232. }
  233. // Required for IFrameworkView.
  234. // Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
  235. // class is torn down while the app is in the foreground.
  236. void App::Uninitialize()
  237. {
  238. }
  239. // Application lifecycle event handlers.
  240. void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  241. {
  242. // Run() won't start until the CoreWindow is activated.
  243. CoreWindow::GetForCurrentThread()->Activate();
  244. }
  245. void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  246. {
  247. }
  248. void App::OnResuming(Platform::Object^ sender, Platform::Object^ args)
  249. {
  250. }
  251. // Window event handlers.
  252. void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
  253. {
  254. }
  255. void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  256. {
  257. m_windowVisible = args->Visible;
  258. }
  259. void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  260. {
  261. m_windowClosed = true;
  262. }
  263. // DisplayInformation event handlers.
  264. void App::OnDpiChanged(DisplayInformation^ sender, Object^ args)
  265. {
  266. }
  267. void App::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
  268. {
  269. }
  270. void App::OnDisplayContentsInvalidated(DisplayInformation^ sender, Object^ args)
  271. {
  272. }