DISTRHO Plugin Framework
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.

224 lines
7.1KB

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