opengl_context.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*************************************************************************/
  2. /* opengl_context.h */
  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. #ifndef EXAMPLES_TUMBLER_OPENGL_CONTEXT_H_
  30. #define EXAMPLES_TUMBLER_OPENGL_CONTEXT_H_
  31. ///
  32. /// @file
  33. /// OpenGLContext manages the OpenGL context in the browser that is associated
  34. /// with a @a pp::Instance instance.
  35. ///
  36. #include <pthread.h>
  37. #include <algorithm>
  38. #include <string>
  39. #include "ppapi/c/ppb_opengles2.h"
  40. //#include "ppapi/cpp/dev/context_3d_dev.h"
  41. #include "ppapi/cpp/graphics_3d_client.h"
  42. #include "ppapi/cpp/graphics_3d.h"
  43. //#include "ppapi/cpp/dev/surface_3d_dev.h"
  44. #include "ppapi/cpp/instance.h"
  45. #include "ppapi/cpp/size.h"
  46. // A convenience wrapper for a shared OpenGLContext pointer type. As other
  47. // smart pointer types are needed, add them here.
  48. #include <tr1/memory>
  49. class OpenGLContext;
  50. typedef std::tr1::shared_ptr<OpenGLContext> SharedOpenGLContext;
  51. /// OpenGLContext manages an OpenGL rendering context in the browser.
  52. ///
  53. class OpenGLContext : public pp::Graphics3DClient {
  54. public:
  55. explicit OpenGLContext(pp::Instance* instance);
  56. /// Release all the in-browser resources used by this context, and make this
  57. /// context invalid.
  58. virtual ~OpenGLContext();
  59. /// The Graphics3DClient interfcace.
  60. virtual void Graphics3DContextLost() {
  61. assert(!"Unexpectedly lost graphics context");
  62. }
  63. /// Make @a this the current 3D context in @a instance.
  64. /// @param instance The instance of the NaCl module that will receive the
  65. /// the current 3D context.
  66. /// @return success.
  67. bool MakeContextCurrent(pp::Instance* instance);
  68. /// Flush the contents of this context to the browser's 3D device.
  69. void FlushContext();
  70. /// Make the underlying 3D device invalid, so that any subsequent rendering
  71. /// commands will have no effect. The next call to MakeContextCurrent() will
  72. /// cause the underlying 3D device to get rebound and start receiving
  73. /// receiving rendering commands again. Use InvalidateContext(), for
  74. /// example, when resizing the context's viewing area.
  75. void InvalidateContext(pp::Instance* instance);
  76. void ResizeContext(const pp::Size& size);
  77. /// The OpenGL ES 2.0 interface.
  78. const struct PPB_OpenGLES2_Dev* gles2() const {
  79. return gles2_interface_;
  80. }
  81. /// The PP_Resource needed to make GLES2 calls through the Pepper interface.
  82. const PP_Resource gl_context() const {
  83. return context_.pp_resource();
  84. }
  85. /// Indicate whether a flush is pending. This can only be called from the
  86. /// main thread; it is not thread safe.
  87. bool flush_pending() const {
  88. return flush_pending_;
  89. }
  90. void set_flush_pending(bool flag) {
  91. flush_pending_ = flag;
  92. }
  93. private:
  94. pp::Graphics3D context_;
  95. bool flush_pending_;
  96. int width, height;
  97. pp::Instance* instance;
  98. const struct PPB_OpenGLES2_Dev* gles2_interface_;
  99. };
  100. #endif // EXAMPLES_TUMBLER_OPENGL_CONTEXT_H_