SDL_winrtvideo.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINRT
  20. /* WinRT SDL video driver implementation
  21. Initial work on this was done by David Ludwig (dludwig@pobox.com), and
  22. was based off of SDL's "dummy" video driver.
  23. */
  24. /* Windows includes */
  25. #include <agile.h>
  26. #include <wrl/client.h>
  27. using namespace Windows::UI::Core;
  28. /* SDL includes */
  29. extern "C" {
  30. #include "SDL_video.h"
  31. #include "SDL_mouse.h"
  32. #include "../SDL_sysvideo.h"
  33. #include "../SDL_pixels_c.h"
  34. #include "../../events/SDL_events_c.h"
  35. #include "../../render/SDL_sysrender.h"
  36. #include "SDL_syswm.h"
  37. #include "SDL_winrtopengles.h"
  38. }
  39. #include "../../core/winrt/SDL_winrtapp_direct3d.h"
  40. #include "../../core/winrt/SDL_winrtapp_xaml.h"
  41. #include "SDL_winrtvideo_cpp.h"
  42. #include "SDL_winrtevents_c.h"
  43. #include "SDL_winrtmouse_c.h"
  44. #include "SDL_main.h"
  45. #include "SDL_system.h"
  46. //#include "SDL_log.h"
  47. /* Initialization/Query functions */
  48. static int WINRT_VideoInit(_THIS);
  49. static int WINRT_InitModes(_THIS);
  50. static int WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
  51. static void WINRT_VideoQuit(_THIS);
  52. /* Window functions */
  53. static int WINRT_CreateWindow(_THIS, SDL_Window * window);
  54. static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
  55. static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
  56. /* SDL-internal globals: */
  57. SDL_Window * WINRT_GlobalSDLWindow = NULL;
  58. /* WinRT driver bootstrap functions */
  59. static int
  60. WINRT_Available(void)
  61. {
  62. return (1);
  63. }
  64. static void
  65. WINRT_DeleteDevice(SDL_VideoDevice * device)
  66. {
  67. if (device->driverdata) {
  68. SDL_VideoData * video_data = (SDL_VideoData *)device->driverdata;
  69. if (video_data->winrtEglWindow) {
  70. video_data->winrtEglWindow->Release();
  71. }
  72. SDL_free(video_data);
  73. }
  74. SDL_free(device);
  75. }
  76. static SDL_VideoDevice *
  77. WINRT_CreateDevice(int devindex)
  78. {
  79. SDL_VideoDevice *device;
  80. SDL_VideoData *data;
  81. /* Initialize all variables that we clean on shutdown */
  82. device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
  83. if (!device) {
  84. SDL_OutOfMemory();
  85. if (device) {
  86. SDL_free(device);
  87. }
  88. return (0);
  89. }
  90. data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
  91. if (!data) {
  92. SDL_OutOfMemory();
  93. return (0);
  94. }
  95. SDL_zerop(data);
  96. device->driverdata = data;
  97. /* Set the function pointers */
  98. device->VideoInit = WINRT_VideoInit;
  99. device->VideoQuit = WINRT_VideoQuit;
  100. device->CreateWindow = WINRT_CreateWindow;
  101. device->DestroyWindow = WINRT_DestroyWindow;
  102. device->SetDisplayMode = WINRT_SetDisplayMode;
  103. device->PumpEvents = WINRT_PumpEvents;
  104. device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
  105. #ifdef SDL_VIDEO_OPENGL_EGL
  106. device->GL_LoadLibrary = WINRT_GLES_LoadLibrary;
  107. device->GL_GetProcAddress = WINRT_GLES_GetProcAddress;
  108. device->GL_UnloadLibrary = WINRT_GLES_UnloadLibrary;
  109. device->GL_CreateContext = WINRT_GLES_CreateContext;
  110. device->GL_MakeCurrent = WINRT_GLES_MakeCurrent;
  111. device->GL_SetSwapInterval = WINRT_GLES_SetSwapInterval;
  112. device->GL_GetSwapInterval = WINRT_GLES_GetSwapInterval;
  113. device->GL_SwapWindow = WINRT_GLES_SwapWindow;
  114. device->GL_DeleteContext = WINRT_GLES_DeleteContext;
  115. #endif
  116. device->free = WINRT_DeleteDevice;
  117. return device;
  118. }
  119. #define WINRTVID_DRIVER_NAME "winrt"
  120. VideoBootStrap WINRT_bootstrap = {
  121. WINRTVID_DRIVER_NAME, "SDL WinRT video driver",
  122. WINRT_Available, WINRT_CreateDevice
  123. };
  124. int
  125. WINRT_VideoInit(_THIS)
  126. {
  127. if (WINRT_InitModes(_this) < 0) {
  128. return -1;
  129. }
  130. WINRT_InitMouse(_this);
  131. WINRT_InitTouch(_this);
  132. return 0;
  133. }
  134. int
  135. WINRT_CalcDisplayModeUsingNativeWindow(SDL_DisplayMode * mode)
  136. {
  137. SDL_DisplayModeData * driverdata;
  138. using namespace Windows::Graphics::Display;
  139. // Go no further if a native window cannot be accessed. This can happen,
  140. // for example, if this function is called from certain threads, such as
  141. // the SDL/XAML thread.
  142. if (!CoreWindow::GetForCurrentThread()) {
  143. return SDL_SetError("SDL/WinRT display modes cannot be calculated outside of the main thread, such as in SDL's XAML thread");
  144. }
  145. //SDL_Log("%s, size={%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, DPI = %f\n",
  146. // __FUNCTION__,
  147. // CoreWindow::GetForCurrentThread()->Bounds.Width, CoreWindow::GetForCurrentThread()->Bounds.Height,
  148. // WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  149. // WINRT_DISPLAY_PROPERTY(NativeOrientation),
  150. // WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  151. // WINRT_DISPLAY_PROPERTY(LogicalDpi));
  152. // Calculate the display size given the window size, taking into account
  153. // the current display's DPI:
  154. const float currentDPI = WINRT_DISPLAY_PROPERTY(LogicalDpi);
  155. const float dipsPerInch = 96.0f;
  156. const int w = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Width * currentDPI) / dipsPerInch);
  157. const int h = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Height * currentDPI) / dipsPerInch);
  158. if (w == 0 || w == h) {
  159. return SDL_SetError("Unable to calculate the WinRT window/display's size");
  160. }
  161. // Create a driverdata field:
  162. driverdata = (SDL_DisplayModeData *) SDL_malloc(sizeof(*driverdata));
  163. if (!driverdata) {
  164. return SDL_OutOfMemory();
  165. }
  166. SDL_zerop(driverdata);
  167. // Fill in most fields:
  168. SDL_zerop(mode);
  169. mode->format = SDL_PIXELFORMAT_RGB888;
  170. mode->refresh_rate = 0; // TODO, WinRT: see if refresh rate data is available, or relevant (for WinRT apps)
  171. mode->w = w;
  172. mode->h = h;
  173. mode->driverdata = driverdata;
  174. driverdata->currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
  175. #if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (NTDDI_VERSION == NTDDI_WIN8)
  176. // On Windows Phone 8.0, the native window's size is always in portrait,
  177. // regardless of the device's orientation. This is in contrast to
  178. // Windows 8.x/RT and Windows Phone 8.1, which will resize the native window as the device's
  179. // orientation changes. In order to compensate for this behavior,
  180. // on Windows Phone, the mode's width and height will be swapped when
  181. // the device is in a landscape (non-portrait) mode.
  182. switch (driverdata->currentOrientation) {
  183. case DisplayOrientations::Landscape:
  184. case DisplayOrientations::LandscapeFlipped:
  185. {
  186. const int tmp = mode->h;
  187. mode->h = mode->w;
  188. mode->w = tmp;
  189. break;
  190. }
  191. default:
  192. break;
  193. }
  194. #endif
  195. return 0;
  196. }
  197. int
  198. WINRT_DuplicateDisplayMode(SDL_DisplayMode * dest, const SDL_DisplayMode * src)
  199. {
  200. SDL_DisplayModeData * driverdata;
  201. driverdata = (SDL_DisplayModeData *) SDL_malloc(sizeof(*driverdata));
  202. if (!driverdata) {
  203. return SDL_OutOfMemory();
  204. }
  205. SDL_memcpy(driverdata, src->driverdata, sizeof(SDL_DisplayModeData));
  206. SDL_memcpy(dest, src, sizeof(SDL_DisplayMode));
  207. dest->driverdata = driverdata;
  208. return 0;
  209. }
  210. int
  211. WINRT_InitModes(_THIS)
  212. {
  213. // Retrieve the display mode:
  214. SDL_DisplayMode mode, desktop_mode;
  215. if (WINRT_CalcDisplayModeUsingNativeWindow(&mode) != 0) {
  216. return -1; // If WINRT_CalcDisplayModeUsingNativeWindow fails, it'll already have set the SDL error
  217. }
  218. if (WINRT_DuplicateDisplayMode(&desktop_mode, &mode) != 0) {
  219. return -1;
  220. }
  221. if (SDL_AddBasicVideoDisplay(&desktop_mode) < 0) {
  222. return -1;
  223. }
  224. SDL_AddDisplayMode(&_this->displays[0], &mode);
  225. return 0;
  226. }
  227. static int
  228. WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
  229. {
  230. return 0;
  231. }
  232. void
  233. WINRT_VideoQuit(_THIS)
  234. {
  235. WINRT_QuitMouse(_this);
  236. }
  237. int
  238. WINRT_CreateWindow(_THIS, SDL_Window * window)
  239. {
  240. // Make sure that only one window gets created, at least until multimonitor
  241. // support is added.
  242. if (WINRT_GlobalSDLWindow != NULL) {
  243. SDL_SetError("WinRT only supports one window");
  244. return -1;
  245. }
  246. SDL_WindowData *data = new SDL_WindowData;
  247. if (!data) {
  248. SDL_OutOfMemory();
  249. return -1;
  250. }
  251. window->driverdata = data;
  252. data->sdlWindow = window;
  253. /* To note, when XAML support is enabled, access to the CoreWindow will not
  254. be possible, at least not via the SDL/XAML thread. Attempts to access it
  255. from there will throw exceptions. As such, the SDL_WindowData's
  256. 'coreWindow' field will only be set (to a non-null value) if XAML isn't
  257. enabled.
  258. */
  259. if (!WINRT_XAMLWasEnabled) {
  260. data->coreWindow = CoreWindow::GetForCurrentThread();
  261. }
  262. #if SDL_VIDEO_OPENGL_EGL
  263. /* Setup the EGL surface, but only if OpenGL ES 2 was requested. */
  264. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  265. /* OpenGL ES 2 wasn't requested. Don't set up an EGL surface. */
  266. data->egl_surface = EGL_NO_SURFACE;
  267. } else {
  268. /* OpenGL ES 2 was reuqested. Set up an EGL surface. */
  269. SDL_VideoData * video_data = (SDL_VideoData *)_this->driverdata;
  270. /* Call SDL_EGL_ChooseConfig and eglCreateWindowSurface directly,
  271. * rather than via SDL_EGL_CreateSurface, as older versions of
  272. * ANGLE/WinRT may require that a C++ object, ComPtr<IUnknown>,
  273. * be passed into eglCreateWindowSurface.
  274. */
  275. if (SDL_EGL_ChooseConfig(_this) != 0) {
  276. char buf[512];
  277. SDL_snprintf(buf, sizeof(buf), "SDL_EGL_ChooseConfig failed: %s", SDL_GetError());
  278. return SDL_SetError(buf);
  279. }
  280. if (video_data->winrtEglWindow) { /* ... is the 'old' version of ANGLE/WinRT being used? */
  281. /* Attempt to create a window surface using older versions of
  282. * ANGLE/WinRT:
  283. */
  284. Microsoft::WRL::ComPtr<IUnknown> cpp_winrtEglWindow = video_data->winrtEglWindow;
  285. data->egl_surface = ((eglCreateWindowSurface_Old_Function)_this->egl_data->eglCreateWindowSurface)(
  286. _this->egl_data->egl_display,
  287. _this->egl_data->egl_config,
  288. cpp_winrtEglWindow, NULL);
  289. if (data->egl_surface == NULL) {
  290. return SDL_SetError("eglCreateWindowSurface failed");
  291. }
  292. } else if (data->coreWindow.Get() != nullptr) {
  293. /* Attempt to create a window surface using newer versions of
  294. * ANGLE/WinRT:
  295. */
  296. IInspectable * coreWindowAsIInspectable = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
  297. data->egl_surface = _this->egl_data->eglCreateWindowSurface(
  298. _this->egl_data->egl_display,
  299. _this->egl_data->egl_config,
  300. coreWindowAsIInspectable,
  301. NULL);
  302. if (data->egl_surface == NULL) {
  303. return SDL_SetError("eglCreateWindowSurface failed");
  304. }
  305. } else {
  306. return SDL_SetError("No supported means to create an EGL window surface are available");
  307. }
  308. }
  309. #endif
  310. /* Make sure the window is considered to be positioned at {0,0},
  311. and is considered fullscreen, shown, and the like.
  312. */
  313. window->x = 0;
  314. window->y = 0;
  315. window->flags =
  316. SDL_WINDOW_FULLSCREEN |
  317. SDL_WINDOW_SHOWN |
  318. SDL_WINDOW_BORDERLESS |
  319. SDL_WINDOW_MAXIMIZED |
  320. SDL_WINDOW_INPUT_GRABBED;
  321. #if SDL_VIDEO_OPENGL_EGL
  322. if (data->egl_surface) {
  323. window->flags |= SDL_WINDOW_OPENGL;
  324. }
  325. #endif
  326. /* WinRT does not, as of this writing, appear to support app-adjustable
  327. window sizes. Set the window size to whatever the native WinRT
  328. CoreWindow is set at.
  329. TODO, WinRT: if and when non-fullscreen XAML control support is added to SDL, consider making those resizable via SDL_Window's interfaces.
  330. */
  331. window->w = _this->displays[0].current_mode.w;
  332. window->h = _this->displays[0].current_mode.h;
  333. /* For now, treat WinRT apps as if they always have focus.
  334. TODO, WinRT: try tracking keyboard and mouse focus state with respect to snapped apps
  335. */
  336. SDL_SetMouseFocus(window);
  337. SDL_SetKeyboardFocus(window);
  338. /* Make sure the WinRT app's IFramworkView can post events on
  339. behalf of SDL:
  340. */
  341. WINRT_GlobalSDLWindow = window;
  342. /* All done! */
  343. return 0;
  344. }
  345. void
  346. WINRT_DestroyWindow(_THIS, SDL_Window * window)
  347. {
  348. SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
  349. if (WINRT_GlobalSDLWindow == window) {
  350. WINRT_GlobalSDLWindow = NULL;
  351. }
  352. if (data) {
  353. // Delete the internal window data:
  354. delete data;
  355. data = NULL;
  356. }
  357. }
  358. SDL_bool
  359. WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  360. {
  361. SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
  362. if (info->version.major <= SDL_MAJOR_VERSION) {
  363. info->subsystem = SDL_SYSWM_WINRT;
  364. info->info.winrt.window = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
  365. return SDL_TRUE;
  366. } else {
  367. SDL_SetError("Application not compiled with SDL %d.%d\n",
  368. SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  369. return SDL_FALSE;
  370. }
  371. return SDL_FALSE;
  372. }
  373. #endif /* SDL_VIDEO_DRIVER_WINRT */
  374. /* vi: set ts=4 sw=4 expandtab: */