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.

265 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. #define JUCE_STATIC_LINK_GL_ES_VERSION_2_0 1
  36. #if !JUCE_ANDROID || JUCE_ANDROID_GL_ES_VERSION_3_0
  37. #define JUCE_STATIC_LINK_GL_ES_VERSION_3_0 1
  38. #endif
  39. #if JUCE_OPENGL_ES
  40. #include "opengl/juce_gles2.cpp"
  41. #else
  42. #include "opengl/juce_gl.cpp"
  43. #endif
  44. //==============================================================================
  45. #if JUCE_IOS
  46. #import <QuartzCore/QuartzCore.h>
  47. //==============================================================================
  48. #elif JUCE_WINDOWS
  49. #include <windowsx.h>
  50. #if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
  51. #pragma comment(lib, "OpenGL32.Lib")
  52. #endif
  53. //==============================================================================
  54. #elif JUCE_LINUX || JUCE_BSD
  55. /* Got an include error here?
  56. If you want to install OpenGL support, the packages to get are "mesa-common-dev"
  57. and "freeglut3-dev".
  58. */
  59. #include <GL/glx.h>
  60. //==============================================================================
  61. #elif JUCE_MAC
  62. #include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
  63. #include <OpenGL/OpenGL.h>
  64. //==============================================================================
  65. #elif JUCE_ANDROID
  66. #include <android/native_window.h>
  67. #include <android/native_window_jni.h>
  68. #include <EGL/egl.h>
  69. #endif
  70. //==============================================================================
  71. namespace juce
  72. {
  73. using namespace ::juce::gl;
  74. void OpenGLExtensionFunctions::initialise()
  75. {
  76. gl::loadFunctions();
  77. }
  78. #define X(name) decltype (::juce::gl::name)& OpenGLExtensionFunctions::name = ::juce::gl::name;
  79. JUCE_GL_BASE_FUNCTIONS
  80. JUCE_GL_EXTENSION_FUNCTIONS
  81. JUCE_GL_VERTEXBUFFER_FUNCTIONS
  82. #undef X
  83. #if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
  84. static const char* getGLErrorMessage (const GLenum e) noexcept
  85. {
  86. switch (e)
  87. {
  88. case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
  89. case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
  90. case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
  91. case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
  92. #ifdef GL_STACK_OVERFLOW
  93. case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
  94. #endif
  95. #ifdef GL_STACK_UNDERFLOW
  96. case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
  97. #endif
  98. #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
  99. case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
  100. #endif
  101. default: break;
  102. }
  103. return "Unknown error";
  104. }
  105. #if JUCE_MAC || JUCE_IOS
  106. #ifndef JUCE_IOS_MAC_VIEW
  107. #if JUCE_IOS
  108. #define JUCE_IOS_MAC_VIEW UIView
  109. #define JUCE_IOS_MAC_WINDOW UIWindow
  110. #else
  111. #define JUCE_IOS_MAC_VIEW NSView
  112. #define JUCE_IOS_MAC_WINDOW NSWindow
  113. #endif
  114. #endif
  115. #endif
  116. static bool checkPeerIsValid (OpenGLContext* context)
  117. {
  118. jassert (context != nullptr);
  119. if (context != nullptr)
  120. {
  121. if (auto* comp = context->getTargetComponent())
  122. {
  123. if (auto* peer = comp->getPeer())
  124. {
  125. #if JUCE_MAC || JUCE_IOS
  126. if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
  127. {
  128. if (auto nsWindow = [nsView window])
  129. {
  130. #if JUCE_MAC
  131. return ([nsWindow isVisible]
  132. && (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
  133. #else
  134. ignoreUnused (nsWindow);
  135. return true;
  136. #endif
  137. }
  138. }
  139. #else
  140. ignoreUnused (peer);
  141. return true;
  142. #endif
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. static void checkGLError (const char* file, const int line)
  149. {
  150. for (;;)
  151. {
  152. const GLenum e = glGetError();
  153. if (e == GL_NO_ERROR)
  154. break;
  155. // if the peer is not valid then ignore errors
  156. if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
  157. continue;
  158. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  159. jassertfalse;
  160. }
  161. }
  162. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  163. #else
  164. #define JUCE_CHECK_OPENGL_ERROR ;
  165. #endif
  166. static void clearGLError() noexcept
  167. {
  168. while (glGetError() != GL_NO_ERROR) {}
  169. }
  170. struct OpenGLTargetSaver
  171. {
  172. OpenGLTargetSaver (const OpenGLContext& c) noexcept
  173. : context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
  174. {
  175. glGetIntegerv (GL_VIEWPORT, oldViewport);
  176. }
  177. ~OpenGLTargetSaver() noexcept
  178. {
  179. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
  180. glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  181. }
  182. private:
  183. const OpenGLContext& context;
  184. GLuint oldFramebuffer;
  185. GLint oldViewport[4];
  186. OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
  187. };
  188. } // namespace juce
  189. //==============================================================================
  190. #include "opengl/juce_OpenGLFrameBuffer.cpp"
  191. #include "opengl/juce_OpenGLGraphicsContext.cpp"
  192. #include "opengl/juce_OpenGLHelpers.cpp"
  193. #include "opengl/juce_OpenGLImage.cpp"
  194. #include "opengl/juce_OpenGLPixelFormat.cpp"
  195. #include "opengl/juce_OpenGLShaderProgram.cpp"
  196. #include "opengl/juce_OpenGLTexture.cpp"
  197. //==============================================================================
  198. #if JUCE_MAC || JUCE_IOS
  199. #if JUCE_MAC
  200. #include "native/juce_OpenGL_osx.h"
  201. #else
  202. #include "native/juce_OpenGL_ios.h"
  203. #endif
  204. #elif JUCE_WINDOWS
  205. #include "opengl/juce_wgl.h"
  206. #include "native/juce_OpenGL_win32.h"
  207. #elif JUCE_LINUX || JUCE_BSD
  208. #include "native/juce_OpenGL_linux_X11.h"
  209. #elif JUCE_ANDROID
  210. #include "native/juce_OpenGL_android.h"
  211. #endif
  212. #include "opengl/juce_OpenGLContext.cpp"
  213. #include "utils/juce_OpenGLAppComponent.cpp"