opengl_context.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************************/
  2. /* opengl_context.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "opengl_context.h"
  30. #include <pthread.h>
  31. #include "ppapi/gles2/gl2ext_ppapi.h"
  32. #include "os_nacl.h"
  33. #include "ppapi/cpp/instance.h"
  34. #include "ppapi/cpp/module.h"
  35. #include "ppapi/cpp/completion_callback.h"
  36. #include "ppapi/utility/completion_callback_factory.h"
  37. namespace {
  38. // This is called by the brower when the 3D context has been flushed to the
  39. // browser window.
  40. void FlushCallback(void* data, int32_t result) {
  41. static_cast<OpenGLContext*>(data)->set_flush_pending(false);
  42. static_cast<OpenGLContext*>(data)->FlushContext();
  43. }
  44. } // namespace
  45. OpenGLContext::OpenGLContext(pp::Instance* p_instance)
  46. : pp::Graphics3DClient(p_instance),
  47. flush_pending_(false) {
  48. instance = p_instance;
  49. pp::Module* module = pp::Module::Get();
  50. assert(module);
  51. gles2_interface_ = static_cast<const struct PPB_OpenGLES2_Dev*>(
  52. module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
  53. assert(gles2_interface_);
  54. }
  55. OpenGLContext::~OpenGLContext() {
  56. glSetCurrentContextPPAPI(0);
  57. }
  58. bool OpenGLContext::MakeContextCurrent(pp::Instance* instance) {
  59. if (instance == NULL) {
  60. glSetCurrentContextPPAPI(0);
  61. return false;
  62. }
  63. // Lazily create the Pepper context.
  64. if (context_.is_null()) {
  65. int32_t attribs[] = {
  66. PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
  67. PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
  68. PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
  69. PP_GRAPHICS3DATTRIB_SAMPLES, 0,
  70. PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
  71. PP_GRAPHICS3DATTRIB_WIDTH, width,
  72. PP_GRAPHICS3DATTRIB_HEIGHT, height,
  73. PP_GRAPHICS3DATTRIB_NONE
  74. };
  75. context_ = pp::Graphics3D(instance, pp::Graphics3D(), attribs);
  76. if (context_.is_null()) {
  77. glSetCurrentContextPPAPI(0);
  78. return false;
  79. }
  80. instance->BindGraphics(context_);
  81. }
  82. glSetCurrentContextPPAPI(context_.pp_resource());
  83. return true;
  84. }
  85. void OpenGLContext::ResizeContext(const pp::Size& size) {
  86. width = size.width();
  87. height = size.height();
  88. if (!context_.is_null()) {
  89. context_.ResizeBuffers(size.width(), size.height());
  90. }
  91. }
  92. void OpenGLContext::InvalidateContext(pp::Instance* instance) {
  93. glSetCurrentContextPPAPI(0);
  94. }
  95. void OpenGLContext::FlushContext() {
  96. if (flush_pending()) {
  97. // A flush is pending so do nothing; just drop this flush on the floor.
  98. return;
  99. }
  100. set_flush_pending(true);
  101. OSNacl* os = (OSNacl*)OS::get_singleton();
  102. MakeContextCurrent(instance);
  103. os->iterate();
  104. context_.SwapBuffers(pp::CompletionCallback(&FlushCallback, this));
  105. }