entry_nacl.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_NACL
  7. #include <bgfx/platform.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <pthread.h>
  11. #include <string>
  12. #include <ppapi/c/pp_errors.h>
  13. #include <ppapi/c/pp_module.h>
  14. #include <ppapi/c/ppb.h>
  15. #include <ppapi/c/ppb_core.h>
  16. #include <ppapi/c/ppb_graphics_3d.h>
  17. #include <ppapi/c/ppb_instance.h>
  18. #include <ppapi/c/ppb_message_loop.h>
  19. #include <ppapi/c/ppb_url_loader.h>
  20. #include <ppapi/c/ppb_url_request_info.h>
  21. #include <ppapi/c/ppb_url_response_info.h>
  22. #include <ppapi/c/ppb_var.h>
  23. #include <ppapi/c/ppp.h>
  24. #include <ppapi/c/ppp_instance.h>
  25. #include <ppapi/gles2/gl2ext_ppapi.h>
  26. #include <bx/thread.h>
  27. #include "entry.h"
  28. namespace entry
  29. {
  30. const PPB_Core* g_coreInterface;
  31. const PPB_Instance* g_instInterface;
  32. const PPB_Graphics3D* g_graphicsInterface;
  33. const PPB_MessageLoop* g_messageLoopInterface;
  34. const PPB_URLLoader* g_urlLoaderInterface;
  35. const PPB_URLRequestInfo* g_urlRequestInterface;
  36. const PPB_URLResponseInfo* g_urlResponseInterface;
  37. const PPB_Var* g_varInterface;
  38. PP_Instance g_instance;
  39. const Event* poll()
  40. {
  41. return NULL;
  42. }
  43. const Event* poll(WindowHandle _handle)
  44. {
  45. BX_UNUSED(_handle);
  46. return NULL;
  47. }
  48. void release(const Event* _event)
  49. {
  50. BX_UNUSED(_event);
  51. }
  52. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  53. {
  54. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  55. WindowHandle handle = { UINT16_MAX };
  56. return handle;
  57. }
  58. void destroyWindow(WindowHandle _handle)
  59. {
  60. BX_UNUSED(_handle);
  61. }
  62. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  63. {
  64. BX_UNUSED(_handle, _x, _y);
  65. }
  66. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  67. {
  68. BX_UNUSED(_handle, _width, _height);
  69. }
  70. void setWindowTitle(WindowHandle _handle, const char* _title)
  71. {
  72. BX_UNUSED(_handle, _title);
  73. }
  74. void toggleWindowFrame(WindowHandle _handle)
  75. {
  76. BX_UNUSED(_handle);
  77. }
  78. void toggleFullscreen(WindowHandle _handle)
  79. {
  80. BX_UNUSED(_handle);
  81. }
  82. void setMouseLock(WindowHandle _handle, bool _lock)
  83. {
  84. BX_UNUSED(_handle, _lock);
  85. }
  86. template<typename Type>
  87. bool initializeInterface(PPB_GetInterface _interface, const char* _name, const Type*& _result)
  88. {
  89. _result = reinterpret_cast<const Type*>(_interface(_name) );
  90. // DBG("%p %s", _result, _name);
  91. return NULL != _result;
  92. }
  93. struct MainThreadEntry
  94. {
  95. int m_argc;
  96. char** m_argv;
  97. static int32_t threadFunc(void* _userData);
  98. };
  99. struct NaclContext
  100. {
  101. NaclContext()
  102. {
  103. static const char* argv[1] = { "nacl.nexe" };
  104. m_mte.m_argc = 1;
  105. m_mte.m_argv = const_cast<char**>(argv);
  106. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  107. }
  108. ~NaclContext()
  109. {
  110. m_thread.shutdown();
  111. }
  112. MainThreadEntry m_mte;
  113. bx::Thread m_thread;
  114. };
  115. static NaclContext* s_ctx;
  116. int32_t MainThreadEntry::threadFunc(void* _userData)
  117. {
  118. MainThreadEntry* self = (MainThreadEntry*)_userData;
  119. PP_Resource resource = g_messageLoopInterface->Create(g_instance);
  120. g_messageLoopInterface->AttachToCurrentThread(resource);
  121. int32_t result = main(self->m_argc, self->m_argv);
  122. return result;
  123. }
  124. static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t /*_argc*/, const char* /*_argn*/[], const char* /*_argv*/[])
  125. {
  126. g_instance = _instance; // one instance only!
  127. if (bgfx::naclSetInterfaces(g_instance, g_instInterface, g_graphicsInterface, NULL) )
  128. {
  129. s_ctx = new NaclContext;
  130. return PP_TRUE;
  131. }
  132. return PP_FALSE;
  133. }
  134. static void naclInstanceDidDestroy(PP_Instance _instance)
  135. {
  136. BX_UNUSED(_instance);
  137. delete s_ctx;
  138. }
  139. static void naclInstanceDidChangeView(PP_Instance /*_instance*/, PP_Resource /*_view*/)
  140. {
  141. }
  142. static void naclInstanceDidChangeFocus(PP_Instance /*_instance*/, PP_Bool /*_focus*/)
  143. {
  144. }
  145. static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance /*_instance*/, PP_Resource /*_urlLoader*/)
  146. {
  147. return PP_FALSE;
  148. }
  149. } // namespace entry
  150. using namespace entry;
  151. PP_EXPORT const void* PPP_GetInterface(const char* _name)
  152. {
  153. if (0 == strcmp(_name, PPP_INSTANCE_INTERFACE) )
  154. {
  155. static PPP_Instance instanceInterface =
  156. {
  157. &naclInstanceDidCreate,
  158. &naclInstanceDidDestroy,
  159. &naclInstanceDidChangeView,
  160. &naclInstanceDidChangeFocus,
  161. &naclInstanceHandleDocumentLoad,
  162. };
  163. return &instanceInterface;
  164. }
  165. return NULL;
  166. }
  167. PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface)
  168. {
  169. DBG("PPAPI version: %d", PPAPI_RELEASE);
  170. BX_UNUSED(_module);
  171. bool result = true;
  172. result &= initializeInterface(_interface, PPB_CORE_INTERFACE, g_coreInterface);
  173. result &= initializeInterface(_interface, PPB_GRAPHICS_3D_INTERFACE, g_graphicsInterface);
  174. result &= initializeInterface(_interface, PPB_INSTANCE_INTERFACE, g_instInterface);
  175. result &= initializeInterface(_interface, PPB_MESSAGELOOP_INTERFACE, g_messageLoopInterface);
  176. result &= initializeInterface(_interface, PPB_URLLOADER_INTERFACE, g_urlLoaderInterface);
  177. result &= initializeInterface(_interface, PPB_URLREQUESTINFO_INTERFACE, g_urlRequestInterface);
  178. result &= initializeInterface(_interface, PPB_URLRESPONSEINFO_INTERFACE, g_urlResponseInterface);
  179. result &= initializeInterface(_interface, PPB_VAR_INTERFACE, g_varInterface);
  180. result &= glInitializePPAPI(_interface);
  181. return result ? PP_OK : PP_ERROR_NOINTERFACE;
  182. }
  183. PP_EXPORT void PPP_ShutdownModule()
  184. {
  185. }
  186. #endif // ENTRY_CONFIG_USE_NATIVE && BX_PLATFROM_NACL