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.

222 lines
6.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 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. #include "OpenGL-include.hpp"
  21. START_NAMESPACE_DGL
  22. // -----------------------------------------------------------------------
  23. /**
  24. OpenGL Graphics context.
  25. */
  26. struct OpenGLGraphicsContext : GraphicsContext
  27. {
  28. #ifdef DGL_USE_OPENGL3
  29. #endif
  30. };
  31. // -----------------------------------------------------------------------
  32. static inline
  33. ImageFormat asDISTRHOImageFormat(const GLenum format)
  34. {
  35. switch (format)
  36. {
  37. #ifdef DGL_USE_OPENGL3
  38. case GL_RED:
  39. #else
  40. case GL_LUMINANCE:
  41. #endif
  42. return kImageFormatGrayscale;
  43. case GL_BGR:
  44. return kImageFormatBGR;
  45. case GL_BGRA:
  46. return kImageFormatBGRA;
  47. case GL_RGB:
  48. return kImageFormatRGB;
  49. case GL_RGBA:
  50. return kImageFormatRGBA;
  51. }
  52. return kImageFormatNull;
  53. }
  54. static inline
  55. GLenum asOpenGLImageFormat(const ImageFormat format)
  56. {
  57. switch (format)
  58. {
  59. case kImageFormatNull:
  60. break;
  61. case kImageFormatGrayscale:
  62. #ifdef DGL_USE_OPENGL3
  63. return GL_RED;
  64. #else
  65. return GL_LUMINANCE;
  66. #endif
  67. case kImageFormatBGR:
  68. return GL_BGR;
  69. case kImageFormatBGRA:
  70. return GL_BGRA;
  71. case kImageFormatRGB:
  72. return GL_RGB;
  73. case kImageFormatRGBA:
  74. return GL_RGBA;
  75. }
  76. return 0x0;
  77. }
  78. // -----------------------------------------------------------------------
  79. /**
  80. OpenGL Image class.
  81. This is an Image class that handles raw image data in pixels.
  82. You can init the image data on the contructor or later on by calling loadFromMemory().
  83. To generate raw data useful for this class see the utils/png2rgba.py script.
  84. Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
  85. instead of the default 'GL_BGRA'.
  86. Images are drawn on screen via 2D textures.
  87. */
  88. class OpenGLImage : public ImageBase
  89. {
  90. public:
  91. /**
  92. Constructor for a null Image.
  93. */
  94. OpenGLImage();
  95. /**
  96. Constructor using raw image data.
  97. @note @a rawData must remain valid for the lifetime of this Image.
  98. */
  99. OpenGLImage(const char* rawData, uint width, uint height, ImageFormat format = kImageFormatBGRA);
  100. /**
  101. Constructor using raw image data.
  102. @note @a rawData must remain valid for the lifetime of this Image.
  103. */
  104. OpenGLImage(const char* rawData, const Size<uint>& size, ImageFormat format = kImageFormatBGRA);
  105. /**
  106. Constructor using another image data.
  107. */
  108. OpenGLImage(const OpenGLImage& image);
  109. /**
  110. Destructor.
  111. */
  112. ~OpenGLImage() override;
  113. /**
  114. Load image data from memory.
  115. @note @a rawData must remain valid for the lifetime of this Image.
  116. */
  117. void loadFromMemory(const char* rawData,
  118. const Size<uint>& size,
  119. ImageFormat format = kImageFormatBGRA) noexcept override;
  120. /**
  121. Draw this image at position @a pos using the graphics context @a context.
  122. */
  123. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  124. /**
  125. TODO document this.
  126. */
  127. OpenGLImage& operator=(const OpenGLImage& image) noexcept;
  128. // FIXME this should not be needed
  129. inline void loadFromMemory(const char* rdata, uint w, uint h, ImageFormat fmt = kImageFormatBGRA)
  130. { loadFromMemory(rdata, Size<uint>(w, h), fmt); }
  131. inline void draw(const GraphicsContext& context)
  132. { drawAt(context, Point<int>(0, 0)); }
  133. inline void drawAt(const GraphicsContext& context, int x, int y)
  134. { drawAt(context, Point<int>(x, y)); }
  135. /**
  136. Constructor using raw image data, specifying an OpenGL image format.
  137. @note @a rawData must remain valid for the lifetime of this Image.
  138. DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
  139. */
  140. DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, uint, uint, ImageFormat)")
  141. explicit OpenGLImage(const char* rawData, uint width, uint height, GLenum glFormat);
  142. /**
  143. Constructor using raw image data, specifying an OpenGL image format.
  144. @note @a rawData must remain valid for the lifetime of this Image.
  145. DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
  146. */
  147. DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, const Size<uint>&, ImageFormat)")
  148. explicit OpenGLImage(const char* rawData, const Size<uint>& size, GLenum glFormat);
  149. /**
  150. Draw this image at (0, 0) point using the current OpenGL context.
  151. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  152. */
  153. DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
  154. void draw();
  155. /**
  156. Draw this image at (x, y) point using the current OpenGL context.
  157. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  158. */
  159. DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, int, int)")
  160. void drawAt(int x, int y);
  161. /**
  162. Draw this image at position @a pos using the current OpenGL context.
  163. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  164. */
  165. DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, const Point<int>&)")
  166. void drawAt(const Point<int>& pos);
  167. /**
  168. Get the image type.
  169. DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE.
  170. */
  171. DISTRHO_DEPRECATED
  172. GLenum getType() const noexcept { return GL_UNSIGNED_BYTE; }
  173. private:
  174. GLuint textureId;
  175. bool setupCalled;
  176. };
  177. // -----------------------------------------------------------------------
  178. typedef ImageBaseAboutWindow<OpenGLImage> OpenGLImageAboutWindow;
  179. typedef ImageBaseButton<OpenGLImage> OpenGLImageButton;
  180. typedef ImageBaseKnob<OpenGLImage> OpenGLImageKnob;
  181. typedef ImageBaseSlider<OpenGLImage> OpenGLImageSlider;
  182. typedef ImageBaseSwitch<OpenGLImage> OpenGLImageSwitch;
  183. // -----------------------------------------------------------------------
  184. END_NAMESPACE_DGL
  185. #endif // DGL_OPENGL_HPP_INCLUDED