entry_nacl.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bx/bx.h>
  6. #if BX_PLATFORM_NACL
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10. #include <ppapi/c/pp_errors.h>
  11. #include <ppapi/c/pp_module.h>
  12. #include <ppapi/c/ppb.h>
  13. #include <ppapi/c/ppb_graphics_3d.h>
  14. #include <ppapi/c/ppb_instance.h>
  15. #include <ppapi/c/ppp.h>
  16. #include <ppapi/c/ppp_instance.h>
  17. #include <ppapi/gles2/gl2ext_ppapi.h>
  18. #include <bgfxplatform.h>
  19. #include "dbg.h"
  20. extern int _main_(int _argc, char** _argv);
  21. static void* _entry_(void*)
  22. {
  23. const char* argv[1] = { "nacl.nexe" };
  24. _main_(1, const_cast<char**>(argv) );
  25. return NULL;
  26. }
  27. struct NaclContext
  28. {
  29. NaclContext()
  30. : m_thread(0)
  31. {
  32. }
  33. const PPB_Instance* m_instInterface;
  34. const PPB_Graphics3D* m_graphicsInterface;
  35. pthread_t m_thread;
  36. PP_Module m_module;
  37. PP_Instance m_instance;
  38. };
  39. static NaclContext s_ctx;
  40. static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t _argc, const char* _argn[], const char* _argv[])
  41. {
  42. s_ctx.m_instance = _instance;
  43. bgfx::naclSetIntefraces(s_ctx.m_instance, s_ctx.m_instInterface, s_ctx.m_graphicsInterface, NULL);
  44. pthread_create(&s_ctx.m_thread, NULL, _entry_, NULL);
  45. return PP_TRUE;
  46. }
  47. static void naclInstanceDidDestroy(PP_Instance _instance)
  48. {
  49. if (s_ctx.m_thread)
  50. {
  51. pthread_join(s_ctx.m_thread, NULL);
  52. }
  53. }
  54. static void naclInstanceDidChangeView(PP_Instance _instance, PP_Resource _view)
  55. {
  56. }
  57. static void naclInstanceDidChangeFocus(PP_Instance _instance, PP_Bool _focus)
  58. {
  59. }
  60. static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance _instance, PP_Resource _urlLoader)
  61. {
  62. return PP_FALSE;
  63. }
  64. PP_EXPORT const void* PPP_GetInterface(const char* _name)
  65. {
  66. if (0 == strcmp(_name, PPP_INSTANCE_INTERFACE) )
  67. {
  68. static PPP_Instance instanceInterface =
  69. {
  70. &naclInstanceDidCreate,
  71. &naclInstanceDidDestroy,
  72. &naclInstanceDidChangeView,
  73. &naclInstanceDidChangeFocus,
  74. &naclInstanceHandleDocumentLoad,
  75. };
  76. return &instanceInterface;
  77. }
  78. return NULL;
  79. }
  80. template<typename Type>
  81. bool initializeInterface(const Type*& _result, PPB_GetInterface _interface, const char* _name)
  82. {
  83. _result = reinterpret_cast<const Type*>(_interface(_name) );
  84. return NULL != _result;
  85. }
  86. PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface)
  87. {
  88. s_ctx.m_module = _module;
  89. bool result = true;
  90. result &= initializeInterface(s_ctx.m_instInterface, _interface, PPB_INSTANCE_INTERFACE);
  91. result &= initializeInterface(s_ctx.m_graphicsInterface, _interface, PPB_GRAPHICS_3D_INTERFACE);
  92. result &= glInitializePPAPI(_interface);
  93. return result ? PP_OK : PP_ERROR_NOINTERFACE;
  94. }
  95. PP_EXPORT void PPP_ShutdownModule()
  96. {
  97. }
  98. #endif // BX_PLATFROM_NACL