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.

288 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #ifdef JUCE_OPENGL_H_INCLUDED
  19. /* When you add this cpp file to your project, you mustn't include it in a file where you've
  20. already included any other headers - just put it inside a file on its own, possibly with your config
  21. flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
  22. header files that the compiler may be using.
  23. */
  24. #error "Incorrect use of JUCE cpp file"
  25. #endif
  26. #define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
  27. #define JUCE_CORE_INCLUDE_JNI_HELPERS 1
  28. #define JUCE_CORE_INCLUDE_NATIVE_HEADERS 1
  29. #define JUCE_GRAPHICS_INCLUDE_COREGRAPHICS_HELPERS 1
  30. #define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
  31. #define JUCE_GUI_BASICS_INCLUDE_SCOPED_THREAD_DPI_AWARENESS_SETTER 1
  32. #include "juce_opengl.h"
  33. #define JUCE_STATIC_LINK_GL_VERSION_1_0 1
  34. #define JUCE_STATIC_LINK_GL_VERSION_1_1 1
  35. #if JUCE_MAC
  36. #define JUCE_STATIC_LINK_GL_VERSION_1_2 1
  37. #define JUCE_STATIC_LINK_GL_VERSION_1_3 1
  38. #define JUCE_STATIC_LINK_GL_VERSION_1_4 1
  39. #define JUCE_STATIC_LINK_GL_VERSION_1_5 1
  40. #define JUCE_STATIC_LINK_GL_VERSION_2_0 1
  41. #define JUCE_STATIC_LINK_GL_VERSION_2_1 1
  42. #define JUCE_STATIC_LINK_GL_VERSION_3_0 1
  43. #define JUCE_STATIC_LINK_GL_VERSION_3_1 1
  44. #define JUCE_STATIC_LINK_GL_VERSION_3_2 1
  45. #endif
  46. #define JUCE_STATIC_LINK_GL_ES_VERSION_2_0 1
  47. #if !JUCE_ANDROID || JUCE_ANDROID_GL_ES_VERSION_3_0
  48. #define JUCE_STATIC_LINK_GL_ES_VERSION_3_0 1
  49. #endif
  50. #if JUCE_OPENGL_ES
  51. #include "opengl/juce_gles2.cpp"
  52. #else
  53. #include "opengl/juce_gl.cpp"
  54. #endif
  55. //==============================================================================
  56. #if JUCE_IOS
  57. #import <QuartzCore/QuartzCore.h>
  58. //==============================================================================
  59. #elif JUCE_WINDOWS
  60. #include <windowsx.h>
  61. #if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
  62. #pragma comment(lib, "OpenGL32.Lib")
  63. #endif
  64. //==============================================================================
  65. #elif JUCE_LINUX || JUCE_BSD
  66. /* Got an include error here?
  67. If you want to install OpenGL support, the packages to get are "mesa-common-dev"
  68. and "freeglut3-dev".
  69. */
  70. #include <GL/glx.h>
  71. //==============================================================================
  72. #elif JUCE_MAC
  73. #include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
  74. #include <OpenGL/OpenGL.h>
  75. //==============================================================================
  76. #elif JUCE_ANDROID
  77. #include <android/native_window.h>
  78. #include <android/native_window_jni.h>
  79. #include <EGL/egl.h>
  80. #endif
  81. //==============================================================================
  82. namespace juce
  83. {
  84. using namespace ::juce::gl;
  85. void OpenGLExtensionFunctions::initialise()
  86. {
  87. gl::loadFunctions();
  88. }
  89. #define X(name) decltype (::juce::gl::name)& OpenGLExtensionFunctions::name = ::juce::gl::name;
  90. JUCE_GL_BASE_FUNCTIONS
  91. JUCE_GL_EXTENSION_FUNCTIONS
  92. JUCE_GL_VERTEXBUFFER_FUNCTIONS
  93. #undef X
  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 [[maybe_unused]] = comp->getPeer())
  135. {
  136. #if JUCE_MAC || JUCE_IOS
  137. if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
  138. {
  139. if ([[maybe_unused]] auto nsWindow = [nsView window])
  140. {
  141. #if JUCE_MAC
  142. return ([nsWindow isVisible]
  143. && (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
  144. #else
  145. return true;
  146. #endif
  147. }
  148. }
  149. #else
  150. return true;
  151. #endif
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. static void checkGLError (const char* file, const int line)
  158. {
  159. for (;;)
  160. {
  161. const GLenum e = glGetError();
  162. if (e == GL_NO_ERROR)
  163. break;
  164. // if the peer is not valid then ignore errors
  165. if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
  166. continue;
  167. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  168. jassertfalse;
  169. }
  170. }
  171. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  172. #else
  173. #define JUCE_CHECK_OPENGL_ERROR ;
  174. #endif
  175. static void clearGLError() noexcept
  176. {
  177. #if JUCE_DEBUG
  178. while (glGetError() != GL_NO_ERROR) {}
  179. #endif
  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. #if JUCE_MAC
  211. #include "native/juce_OpenGL_mac.h"
  212. #else
  213. #include "native/juce_OpenGL_ios.h"
  214. #endif
  215. #elif JUCE_WINDOWS
  216. #include "opengl/juce_wgl.h"
  217. #include "native/juce_OpenGL_windows.h"
  218. #define JUCE_IMPL_WGL_EXTENSION_FUNCTION(name) \
  219. decltype (juce::OpenGLContext::NativeContext::name) juce::OpenGLContext::NativeContext::name = nullptr;
  220. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB)
  221. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT)
  222. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT)
  223. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglCreateContextAttribsARB)
  224. #undef JUCE_IMPL_WGL_EXTENSION_FUNCTION
  225. #elif JUCE_LINUX || JUCE_BSD
  226. #include <juce_gui_basics/native/juce_ScopedWindowAssociation_linux.h>
  227. #include "native/juce_OpenGL_linux.h"
  228. #elif JUCE_ANDROID
  229. #include "native/juce_OpenGL_android.h"
  230. #endif
  231. #include "opengl/juce_OpenGLContext.cpp"
  232. #include "utils/juce_OpenGLAppComponent.cpp"