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.

211 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. static int getAllowedTextureSize (int x)
  18. {
  19. #if JUCE_OPENGL_ALLOW_NON_POWER_OF_TWO_TEXTURES
  20. return x;
  21. #else
  22. return nextPowerOfTwo (x);
  23. #endif
  24. }
  25. OpenGLTexture::OpenGLTexture()
  26. : textureID (0), width (0), height (0), ownerContext (nullptr)
  27. {
  28. }
  29. OpenGLTexture::~OpenGLTexture()
  30. {
  31. release();
  32. }
  33. bool OpenGLTexture::isValidSize (int width, int height)
  34. {
  35. return isPowerOfTwo (width) && isPowerOfTwo (height);
  36. }
  37. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  38. {
  39. ownerContext = OpenGLContext::getCurrentContext();
  40. // Texture objects can only be created when the current thread has an active OpenGL
  41. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  42. jassert (ownerContext != nullptr);
  43. if (textureID == 0)
  44. {
  45. JUCE_CHECK_OPENGL_ERROR
  46. glGenTextures (1, &textureID);
  47. glBindTexture (GL_TEXTURE_2D, textureID);
  48. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  49. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  50. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  51. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  52. JUCE_CHECK_OPENGL_ERROR
  53. }
  54. else
  55. {
  56. glBindTexture (GL_TEXTURE_2D, textureID);
  57. JUCE_CHECK_OPENGL_ERROR;
  58. }
  59. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  60. JUCE_CHECK_OPENGL_ERROR
  61. width = getAllowedTextureSize (w);
  62. height = getAllowedTextureSize (h);
  63. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  64. if (width != w || height != h)
  65. {
  66. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  67. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  68. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  69. type, GL_UNSIGNED_BYTE, pixels);
  70. }
  71. else
  72. {
  73. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  74. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  75. }
  76. JUCE_CHECK_OPENGL_ERROR
  77. }
  78. template <class PixelType>
  79. struct Flipper
  80. {
  81. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  82. const int w, const int h)
  83. {
  84. dataCopy.malloc ((size_t) (w * h));
  85. for (int y = 0; y < h; ++y)
  86. {
  87. const PixelType* src = (const PixelType*) srcData;
  88. PixelARGB* const dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  89. for (int x = 0; x < w; ++x)
  90. {
  91. #if JUCE_ANDROID
  92. PixelType s (src[x]);
  93. dst[x].setARGB (s.getAlpha(), s.getBlue(), s.getGreen(), s.getRed());
  94. #else
  95. dst[x].set (src[x]);
  96. #endif
  97. }
  98. srcData += lineStride;
  99. }
  100. }
  101. };
  102. void OpenGLTexture::loadImage (const Image& image)
  103. {
  104. const int imageW = image.getWidth();
  105. const int imageH = image.getHeight();
  106. HeapBlock<PixelARGB> dataCopy;
  107. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  108. switch (srcData.pixelFormat)
  109. {
  110. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  111. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  112. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  113. default: break;
  114. }
  115. create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
  116. }
  117. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  118. {
  119. create (w, h, pixels, JUCE_RGBA_FORMAT, false);
  120. }
  121. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  122. {
  123. create (w, h, pixels, GL_ALPHA, false);
  124. }
  125. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  126. {
  127. HeapBlock<PixelARGB> flippedCopy;
  128. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
  129. create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
  130. }
  131. void OpenGLTexture::release()
  132. {
  133. if (textureID != 0
  134. && ownerContext == OpenGLContext::getCurrentContext())
  135. {
  136. glDeleteTextures (1, &textureID);
  137. textureID = 0;
  138. width = 0;
  139. height = 0;
  140. }
  141. }
  142. void OpenGLTexture::bind() const
  143. {
  144. glBindTexture (GL_TEXTURE_2D, textureID);
  145. }
  146. void OpenGLTexture::unbind() const
  147. {
  148. glBindTexture (GL_TEXTURE_2D, 0);
  149. }
  150. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  151. void OpenGLTexture::draw2D (float x1, float y1,
  152. float x2, float y2,
  153. float x3, float y3,
  154. float x4, float y4,
  155. Colour colour) const
  156. {
  157. bind();
  158. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  159. unbind();
  160. }
  161. void OpenGLTexture::draw3D (float x1, float y1, float z1,
  162. float x2, float y2, float z2,
  163. float x3, float y3, float z3,
  164. float x4, float y4, float z4,
  165. Colour colour) const
  166. {
  167. bind();
  168. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  169. unbind();
  170. }
  171. #endif