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.

281 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. #include "juce_opengl.h"
  26. //==============================================================================
  27. #if JUCE_IOS
  28. #import <QuartzCore/QuartzCore.h>
  29. //==============================================================================
  30. #elif JUCE_WINDOWS
  31. #include <windowsx.h>
  32. #if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
  33. #pragma comment(lib, "OpenGL32.Lib")
  34. #endif
  35. //==============================================================================
  36. #elif JUCE_LINUX
  37. /* Got an include error here?
  38. If you want to install OpenGL support, the packages to get are "mesa-common-dev"
  39. and "freeglut3-dev".
  40. */
  41. #include <GL/glx.h>
  42. //==============================================================================
  43. #elif JUCE_MAC
  44. #include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
  45. #include <OpenGL/OpenGL.h>
  46. //==============================================================================
  47. #elif JUCE_ANDROID
  48. #ifndef GL_GLEXT_PROTOTYPES
  49. #define GL_GLEXT_PROTOTYPES 1
  50. #endif
  51. #if JUCE_ANDROID_GL_ES_VERSION_3_0
  52. #include <GLES3/gl3.h>
  53. // workaround for a bug in SDK 18 and 19
  54. // see: https://stackoverflow.com/questions/31003863/gles-3-0-including-gl2ext-h
  55. #define __gl2_h_
  56. #include <GLES2/gl2ext.h>
  57. #else
  58. #include <GLES2/gl2.h>
  59. #endif
  60. #endif
  61. //==============================================================================
  62. namespace juce
  63. {
  64. void OpenGLExtensionFunctions::initialise()
  65. {
  66. #if JUCE_WINDOWS || JUCE_LINUX
  67. #define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
  68. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  69. JUCE_GL_BASE_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  70. #undef JUCE_INIT_GL_FUNCTION
  71. #define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
  72. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name); \
  73. if (name == nullptr) \
  74. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (JUCE_STRINGIFY (name ## EXT));
  75. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  76. #if JUCE_OPENGL3
  77. JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  78. #endif
  79. #undef JUCE_INIT_GL_FUNCTION
  80. #endif
  81. }
  82. #if JUCE_OPENGL_ES
  83. #define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) \
  84. returnType OpenGLExtensionFunctions::name params noexcept { return ::name callparams; }
  85. JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  86. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  87. #if JUCE_OPENGL3
  88. JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  89. #endif
  90. #undef JUCE_DECLARE_GL_FUNCTION
  91. #endif
  92. #undef JUCE_GL_EXTENSION_FUNCTIONS
  93. #if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
  94. static const char* getGLErrorMessage (const GLenum e) noexcept
  95. {
  96. switch (e)
  97. {
  98. case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
  99. case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
  100. case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
  101. case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
  102. #ifdef GL_STACK_OVERFLOW
  103. case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
  104. #endif
  105. #ifdef GL_STACK_UNDERFLOW
  106. case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
  107. #endif
  108. #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
  109. case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
  110. #endif
  111. default: break;
  112. }
  113. return "Unknown error";
  114. }
  115. #if JUCE_MAC || JUCE_IOS
  116. #ifndef JUCE_IOS_MAC_VIEW
  117. #if JUCE_IOS
  118. #define JUCE_IOS_MAC_VIEW UIView
  119. #define JUCE_IOS_MAC_WINDOW UIWindow
  120. #else
  121. #define JUCE_IOS_MAC_VIEW NSView
  122. #define JUCE_IOS_MAC_WINDOW NSWindow
  123. #endif
  124. #endif
  125. #endif
  126. static bool checkPeerIsValid (OpenGLContext* context)
  127. {
  128. jassert (context != nullptr);
  129. if (context != nullptr)
  130. {
  131. if (auto* comp = context->getTargetComponent())
  132. {
  133. if (auto* peer = comp->getPeer())
  134. {
  135. #if JUCE_MAC || JUCE_IOS
  136. if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
  137. {
  138. if (auto nsWindow = [nsView window])
  139. {
  140. #if JUCE_MAC
  141. return ([nsWindow isVisible]
  142. && (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
  143. #else
  144. ignoreUnused (nsWindow);
  145. return true;
  146. #endif
  147. }
  148. }
  149. #else
  150. ignoreUnused (peer);
  151. return true;
  152. #endif
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. static void checkGLError (const char* file, const int line)
  159. {
  160. for (;;)
  161. {
  162. const GLenum e = glGetError();
  163. if (e == GL_NO_ERROR)
  164. break;
  165. // if the peer is not valid then ignore errors
  166. if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
  167. continue;
  168. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  169. jassertfalse;
  170. }
  171. }
  172. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  173. #else
  174. #define JUCE_CHECK_OPENGL_ERROR ;
  175. #endif
  176. static void clearGLError() noexcept
  177. {
  178. while (glGetError() != GL_NO_ERROR) {}
  179. }
  180. struct OpenGLTargetSaver
  181. {
  182. OpenGLTargetSaver (const OpenGLContext& c) noexcept
  183. : context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
  184. {
  185. glGetIntegerv (GL_VIEWPORT, oldViewport);
  186. }
  187. ~OpenGLTargetSaver() noexcept
  188. {
  189. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
  190. glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  191. }
  192. private:
  193. const OpenGLContext& context;
  194. GLuint oldFramebuffer;
  195. GLint oldViewport[4];
  196. OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
  197. };
  198. } // namespace juce
  199. //==============================================================================
  200. #include "opengl/juce_OpenGLFrameBuffer.cpp"
  201. #include "opengl/juce_OpenGLGraphicsContext.cpp"
  202. #include "opengl/juce_OpenGLHelpers.cpp"
  203. #include "opengl/juce_OpenGLImage.cpp"
  204. #include "opengl/juce_OpenGLPixelFormat.cpp"
  205. #include "opengl/juce_OpenGLShaderProgram.cpp"
  206. #include "opengl/juce_OpenGLTexture.cpp"
  207. //==============================================================================
  208. #if JUCE_MAC || JUCE_IOS
  209. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  210. #if JUCE_MAC
  211. #include "native/juce_OpenGL_osx.h"
  212. #else
  213. #include "native/juce_OpenGL_ios.h"
  214. #endif
  215. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  216. #elif JUCE_WINDOWS
  217. #include "native/juce_OpenGL_win32.h"
  218. #elif JUCE_LINUX
  219. #include "native/juce_OpenGL_linux_X11.h"
  220. #elif JUCE_ANDROID
  221. #include "native/juce_OpenGL_android.h"
  222. #endif
  223. #include "opengl/juce_OpenGLContext.cpp"
  224. #include "utils/juce_OpenGLAppComponent.cpp"