entry_nacl.cpp 3.0 KB

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