Audio plugin host https://kx.studio/carla
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.

309 lines
8.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_OPENGL_HPP_INCLUDED
  17. #define DGL_OPENGL_HPP_INCLUDED
  18. #include "ImageBase.hpp"
  19. #include "ImageBaseWidgets.hpp"
  20. // -----------------------------------------------------------------------
  21. // Fix OpenGL includes for Windows, based on glfw code (part 1)
  22. #undef DGL_CALLBACK_DEFINED
  23. #undef DGL_WINGDIAPI_DEFINED
  24. #ifdef DISTRHO_OS_WINDOWS
  25. #ifndef APIENTRY
  26. # define APIENTRY __stdcall
  27. #endif // APIENTRY
  28. /* We need WINGDIAPI defined */
  29. #ifndef WINGDIAPI
  30. # if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__POCC__)
  31. # define WINGDIAPI __declspec(dllimport)
  32. # elif defined(__LCC__)
  33. # define WINGDIAPI __stdcall
  34. # else
  35. # define WINGDIAPI extern
  36. # endif
  37. # define DGL_WINGDIAPI_DEFINED
  38. #endif // WINGDIAPI
  39. /* Some <GL/glu.h> files also need CALLBACK defined */
  40. #ifndef CALLBACK
  41. # if defined(_MSC_VER)
  42. # if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC)) && !defined(MIDL_PASS)
  43. # define CALLBACK __stdcall
  44. # else
  45. # define CALLBACK
  46. # endif
  47. # else
  48. # define CALLBACK __stdcall
  49. # endif
  50. # define DGL_CALLBACK_DEFINED
  51. #endif // CALLBACK
  52. /* Most GL/glu.h variants on Windows need wchar_t */
  53. #include <cstddef>
  54. #endif // DISTRHO_OS_WINDOWS
  55. // -----------------------------------------------------------------------
  56. // OpenGL includes
  57. #ifdef DISTRHO_OS_MAC
  58. # ifdef DGL_USE_OPENGL3
  59. # include <OpenGL/gl3.h>
  60. # include <OpenGL/gl3ext.h>
  61. # else
  62. # include <OpenGL/gl.h>
  63. # endif
  64. #else
  65. # ifndef DISTRHO_OS_WINDOWS
  66. # define GL_GLEXT_PROTOTYPES
  67. # endif
  68. # ifndef __GLEW_H__
  69. # include <GL/gl.h>
  70. # include <GL/glext.h>
  71. # endif
  72. #endif
  73. // -----------------------------------------------------------------------
  74. // Missing OpenGL defines
  75. #if defined(GL_BGR_EXT) && !defined(GL_BGR)
  76. # define GL_BGR GL_BGR_EXT
  77. #endif
  78. #if defined(GL_BGRA_EXT) && !defined(GL_BGRA)
  79. # define GL_BGRA GL_BGRA_EXT
  80. #endif
  81. #ifndef GL_CLAMP_TO_BORDER
  82. # define GL_CLAMP_TO_BORDER 0x812D
  83. #endif
  84. // -----------------------------------------------------------------------
  85. // Fix OpenGL includes for Windows, based on glfw code (part 2)
  86. #ifdef DGL_CALLBACK_DEFINED
  87. # undef CALLBACK
  88. # undef DGL_CALLBACK_DEFINED
  89. #endif
  90. #ifdef DGL_WINGDIAPI_DEFINED
  91. # undef WINGDIAPI
  92. # undef DGL_WINGDIAPI_DEFINED
  93. #endif
  94. START_NAMESPACE_DGL
  95. // -----------------------------------------------------------------------
  96. /**
  97. OpenGL Graphics context.
  98. */
  99. struct OpenGLGraphicsContext : GraphicsContext
  100. {
  101. };
  102. // -----------------------------------------------------------------------
  103. static inline
  104. ImageFormat asDISTRHOImageFormat(const GLenum format)
  105. {
  106. switch (format)
  107. {
  108. #ifdef DGL_USE_OPENGL3
  109. case GL_RED:
  110. #else
  111. case GL_LUMINANCE:
  112. #endif
  113. return kImageFormatGrayscale;
  114. case GL_BGR:
  115. return kImageFormatBGR;
  116. case GL_BGRA:
  117. return kImageFormatBGRA;
  118. case GL_RGB:
  119. return kImageFormatRGB;
  120. case GL_RGBA:
  121. return kImageFormatRGBA;
  122. }
  123. return kImageFormatNull;
  124. }
  125. static inline
  126. GLenum asOpenGLImageFormat(const ImageFormat format)
  127. {
  128. switch (format)
  129. {
  130. case kImageFormatNull:
  131. break;
  132. case kImageFormatGrayscale:
  133. #ifdef DGL_USE_OPENGL3
  134. return GL_RED;
  135. #else
  136. return GL_LUMINANCE;
  137. #endif
  138. case kImageFormatBGR:
  139. return GL_BGR;
  140. case kImageFormatBGRA:
  141. return GL_BGRA;
  142. case kImageFormatRGB:
  143. return GL_RGB;
  144. case kImageFormatRGBA:
  145. return GL_RGBA;
  146. }
  147. return 0x0;
  148. }
  149. // -----------------------------------------------------------------------
  150. /**
  151. OpenGL Image class.
  152. This is an Image class that handles raw image data in pixels.
  153. You can init the image data on the contructor or later on by calling loadFromMemory().
  154. To generate raw data useful for this class see the utils/png2rgba.py script.
  155. Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
  156. instead of the default 'GL_BGRA'.
  157. Images are drawn on screen via 2D textures.
  158. */
  159. class OpenGLImage : public ImageBase
  160. {
  161. public:
  162. /**
  163. Constructor for a null Image.
  164. */
  165. OpenGLImage();
  166. /**
  167. Constructor using raw image data.
  168. @note @a rawData must remain valid for the lifetime of this Image.
  169. */
  170. OpenGLImage(const char* rawData, uint width, uint height, ImageFormat format = kImageFormatBGRA);
  171. /**
  172. Constructor using raw image data.
  173. @note @a rawData must remain valid for the lifetime of this Image.
  174. */
  175. OpenGLImage(const char* rawData, const Size<uint>& size, ImageFormat format = kImageFormatBGRA);
  176. /**
  177. Constructor using another image data.
  178. */
  179. OpenGLImage(const OpenGLImage& image);
  180. /**
  181. Destructor.
  182. */
  183. ~OpenGLImage() override;
  184. /**
  185. Load image data from memory.
  186. @note @a rawData must remain valid for the lifetime of this Image.
  187. */
  188. void loadFromMemory(const char* rawData,
  189. const Size<uint>& size,
  190. ImageFormat format = kImageFormatBGRA) noexcept override;
  191. /**
  192. Draw this image at position @a pos using the graphics context @a context.
  193. */
  194. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  195. /**
  196. TODO document this.
  197. */
  198. OpenGLImage& operator=(const OpenGLImage& image) noexcept;
  199. // FIXME this should not be needed
  200. inline void loadFromMemory(const char* rdata, uint w, uint h, ImageFormat fmt = kImageFormatBGRA)
  201. { loadFromMemory(rdata, Size<uint>(w, h), fmt); };
  202. inline void draw(const GraphicsContext& context)
  203. { drawAt(context, Point<int>(0, 0)); };
  204. inline void drawAt(const GraphicsContext& context, int x, int y)
  205. { drawAt(context, Point<int>(x, y)); };
  206. /**
  207. Constructor using raw image data, specifying an OpenGL image format.
  208. @note @a rawData must remain valid for the lifetime of this Image.
  209. DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
  210. */
  211. DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, uint, uint, ImageFormat)")
  212. explicit OpenGLImage(const char* rawData, uint width, uint height, GLenum glFormat);
  213. /**
  214. Constructor using raw image data, specifying an OpenGL image format.
  215. @note @a rawData must remain valid for the lifetime of this Image.
  216. DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
  217. */
  218. DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, const Size<uint>&, ImageFormat)")
  219. explicit OpenGLImage(const char* rawData, const Size<uint>& size, GLenum glFormat);
  220. /**
  221. Draw this image at (0, 0) point using the current OpenGL context.
  222. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  223. */
  224. DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
  225. void draw();
  226. /**
  227. Draw this image at (x, y) point using the current OpenGL context.
  228. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  229. */
  230. DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, int, int)")
  231. void drawAt(int x, int y);
  232. /**
  233. Draw this image at position @a pos using the current OpenGL context.
  234. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  235. */
  236. DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, const Point<int>&)")
  237. void drawAt(const Point<int>& pos);
  238. /**
  239. Get the image type.
  240. DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE.
  241. */
  242. DISTRHO_DEPRECATED
  243. GLenum getType() const noexcept { return GL_UNSIGNED_BYTE; }
  244. private:
  245. GLuint textureId;
  246. bool setupCalled;
  247. };
  248. // -----------------------------------------------------------------------
  249. typedef ImageBaseAboutWindow<OpenGLImage> OpenGLImageAboutWindow;
  250. typedef ImageBaseButton<OpenGLImage> OpenGLImageButton;
  251. typedef ImageBaseKnob<OpenGLImage> OpenGLImageKnob;
  252. typedef ImageBaseSlider<OpenGLImage> OpenGLImageSlider;
  253. typedef ImageBaseSwitch<OpenGLImage> OpenGLImageSwitch;
  254. // -----------------------------------------------------------------------
  255. END_NAMESPACE_DGL
  256. #endif