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.

295 lines
8.7KB

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