The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #ifdef JUCE_OPENGL_H_INCLUDED
  14. /* When you add this cpp file to your project, you mustn't include it in a file where you've
  15. already included any other headers - just put it inside a file on its own, possibly with your config
  16. flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
  17. header files that the compiler may be using.
  18. */
  19. #error "Incorrect use of JUCE cpp file"
  20. #endif
  21. #define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
  22. #define JUCE_CORE_INCLUDE_JNI_HELPERS 1
  23. #define JUCE_CORE_INCLUDE_NATIVE_HEADERS 1
  24. #define JUCE_GRAPHICS_INCLUDE_COREGRAPHICS_HELPERS 1
  25. #define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
  26. #include "juce_opengl.h"
  27. //==============================================================================
  28. #if JUCE_IOS
  29. #import <QuartzCore/QuartzCore.h>
  30. //==============================================================================
  31. #elif JUCE_WINDOWS
  32. #include <windowsx.h>
  33. #if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
  34. #pragma comment(lib, "OpenGL32.Lib")
  35. #endif
  36. //==============================================================================
  37. #elif JUCE_LINUX
  38. /* Got an include error here?
  39. If you want to install OpenGL support, the packages to get are "mesa-common-dev"
  40. and "freeglut3-dev".
  41. */
  42. #include <GL/glx.h>
  43. //==============================================================================
  44. #elif JUCE_MAC
  45. #include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
  46. #include <OpenGL/OpenGL.h>
  47. //==============================================================================
  48. #elif JUCE_ANDROID
  49. #ifndef GL_GLEXT_PROTOTYPES
  50. #define GL_GLEXT_PROTOTYPES 1
  51. #endif
  52. #if JUCE_ANDROID_GL_ES_VERSION_3_0
  53. #include <GLES3/gl3.h>
  54. // workaround for a bug in SDK 18 and 19
  55. // see: https://stackoverflow.com/questions/31003863/gles-3-0-including-gl2ext-h
  56. #define __gl2_h_
  57. #include <GLES2/gl2ext.h>
  58. #else
  59. #include <GLES2/gl2.h>
  60. #endif
  61. #endif
  62. //==============================================================================
  63. namespace juce
  64. {
  65. void OpenGLExtensionFunctions::initialise()
  66. {
  67. #if JUCE_WINDOWS || JUCE_LINUX
  68. #define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
  69. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  70. JUCE_GL_BASE_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  71. #undef JUCE_INIT_GL_FUNCTION
  72. #define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
  73. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name); \
  74. if (name == nullptr) \
  75. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (JUCE_STRINGIFY (name ## EXT));
  76. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  77. #if JUCE_OPENGL3
  78. JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  79. #endif
  80. #undef JUCE_INIT_GL_FUNCTION
  81. #endif
  82. }
  83. #if JUCE_OPENGL_ES
  84. #define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) \
  85. returnType OpenGLExtensionFunctions::name params noexcept { return ::name callparams; }
  86. JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  87. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  88. #if JUCE_OPENGL3
  89. JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  90. #endif
  91. #undef JUCE_DECLARE_GL_FUNCTION
  92. #endif
  93. #undef JUCE_GL_EXTENSION_FUNCTIONS
  94. #if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
  95. static const char* getGLErrorMessage (const GLenum e) noexcept
  96. {
  97. switch (e)
  98. {
  99. case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
  100. case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
  101. case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
  102. case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
  103. #ifdef GL_STACK_OVERFLOW
  104. case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
  105. #endif
  106. #ifdef GL_STACK_UNDERFLOW
  107. case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
  108. #endif
  109. #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
  110. case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
  111. #endif
  112. default: break;
  113. }
  114. return "Unknown error";
  115. }
  116. #if JUCE_MAC || JUCE_IOS
  117. #ifndef JUCE_IOS_MAC_VIEW
  118. #if JUCE_IOS
  119. #define JUCE_IOS_MAC_VIEW UIView
  120. #define JUCE_IOS_MAC_WINDOW UIWindow
  121. #else
  122. #define JUCE_IOS_MAC_VIEW NSView
  123. #define JUCE_IOS_MAC_WINDOW NSWindow
  124. #endif
  125. #endif
  126. #endif
  127. static bool checkPeerIsValid (OpenGLContext* context)
  128. {
  129. jassert (context != nullptr);
  130. if (context != nullptr)
  131. {
  132. if (auto* comp = context->getTargetComponent())
  133. {
  134. if (auto* peer = comp->getPeer())
  135. {
  136. #if JUCE_MAC || JUCE_IOS
  137. if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
  138. {
  139. if (auto nsWindow = [nsView window])
  140. {
  141. #if JUCE_MAC
  142. return ([nsWindow isVisible]
  143. && (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
  144. #else
  145. ignoreUnused (nsWindow);
  146. return true;
  147. #endif
  148. }
  149. }
  150. #else
  151. ignoreUnused (peer);
  152. return true;
  153. #endif
  154. }
  155. }
  156. }
  157. return false;
  158. }
  159. static void checkGLError (const char* file, const int line)
  160. {
  161. for (;;)
  162. {
  163. const GLenum e = glGetError();
  164. if (e == GL_NO_ERROR)
  165. break;
  166. // if the peer is not valid then ignore errors
  167. if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
  168. continue;
  169. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  170. jassertfalse;
  171. }
  172. }
  173. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  174. #else
  175. #define JUCE_CHECK_OPENGL_ERROR ;
  176. #endif
  177. static void clearGLError() noexcept
  178. {
  179. while (glGetError() != GL_NO_ERROR) {}
  180. }
  181. struct OpenGLTargetSaver
  182. {
  183. OpenGLTargetSaver (const OpenGLContext& c) noexcept
  184. : context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
  185. {
  186. glGetIntegerv (GL_VIEWPORT, oldViewport);
  187. }
  188. ~OpenGLTargetSaver() noexcept
  189. {
  190. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
  191. glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  192. }
  193. private:
  194. const OpenGLContext& context;
  195. GLuint oldFramebuffer;
  196. GLint oldViewport[4];
  197. OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
  198. };
  199. } // namespace juce
  200. //==============================================================================
  201. #include "opengl/juce_OpenGLFrameBuffer.cpp"
  202. #include "opengl/juce_OpenGLGraphicsContext.cpp"
  203. #include "opengl/juce_OpenGLHelpers.cpp"
  204. #include "opengl/juce_OpenGLImage.cpp"
  205. #include "opengl/juce_OpenGLPixelFormat.cpp"
  206. #include "opengl/juce_OpenGLShaderProgram.cpp"
  207. #include "opengl/juce_OpenGLTexture.cpp"
  208. //==============================================================================
  209. #if JUCE_MAC || JUCE_IOS
  210. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  211. #if JUCE_MAC
  212. #include "native/juce_OpenGL_osx.h"
  213. #else
  214. #include "native/juce_OpenGL_ios.h"
  215. #endif
  216. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  217. #elif JUCE_WINDOWS
  218. #include "native/juce_OpenGL_win32.h"
  219. #elif JUCE_LINUX
  220. #include "native/juce_OpenGL_linux_X11.h"
  221. #elif JUCE_ANDROID
  222. #include "native/juce_OpenGL_android.h"
  223. #endif
  224. #include "opengl/juce_OpenGLContext.cpp"
  225. #include "utils/juce_OpenGLAppComponent.cpp"