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.

194 lines
6.0KB

  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. OpenGLTexture::OpenGLTexture()
  18. : textureID (0), width (0), height (0), ownerContext (nullptr)
  19. {
  20. }
  21. OpenGLTexture::~OpenGLTexture()
  22. {
  23. release();
  24. }
  25. bool OpenGLTexture::isValidSize (int width, int height)
  26. {
  27. return isPowerOfTwo (width) && isPowerOfTwo (height);
  28. }
  29. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  30. {
  31. ownerContext = OpenGLContext::getCurrentContext();
  32. // Texture objects can only be created when the current thread has an active OpenGL
  33. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  34. jassert (ownerContext != nullptr);
  35. if (textureID == 0)
  36. {
  37. JUCE_CHECK_OPENGL_ERROR
  38. glGenTextures (1, &textureID);
  39. glBindTexture (GL_TEXTURE_2D, textureID);
  40. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  41. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  42. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  43. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  44. JUCE_CHECK_OPENGL_ERROR
  45. }
  46. else
  47. {
  48. glBindTexture (GL_TEXTURE_2D, textureID);
  49. JUCE_CHECK_OPENGL_ERROR;
  50. }
  51. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  52. JUCE_CHECK_OPENGL_ERROR
  53. width = nextPowerOfTwo (w);
  54. height = nextPowerOfTwo (h);
  55. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  56. if (width != w || height != h)
  57. {
  58. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  59. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  60. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  61. type, GL_UNSIGNED_BYTE, pixels);
  62. }
  63. else
  64. {
  65. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  66. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  67. }
  68. JUCE_CHECK_OPENGL_ERROR
  69. }
  70. template <class PixelType>
  71. struct Flipper
  72. {
  73. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  74. const int w, const int h)
  75. {
  76. dataCopy.malloc ((size_t) (w * h));
  77. for (int y = 0; y < h; ++y)
  78. {
  79. const PixelType* src = (const PixelType*) srcData;
  80. PixelARGB* const dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  81. for (int x = 0; x < w; ++x)
  82. dst[x].set (src[x]);
  83. srcData += lineStride;
  84. }
  85. }
  86. };
  87. void OpenGLTexture::loadImage (const Image& image)
  88. {
  89. const int imageW = image.getWidth();
  90. const int imageH = image.getHeight();
  91. HeapBlock<PixelARGB> dataCopy;
  92. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  93. switch (srcData.pixelFormat)
  94. {
  95. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  96. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  97. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  98. default: break;
  99. }
  100. create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
  101. }
  102. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  103. {
  104. create (w, h, pixels, JUCE_RGBA_FORMAT, false);
  105. }
  106. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  107. {
  108. create (w, h, pixels, GL_ALPHA, false);
  109. }
  110. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  111. {
  112. HeapBlock<PixelARGB> flippedCopy;
  113. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
  114. create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
  115. }
  116. void OpenGLTexture::release()
  117. {
  118. if (textureID != 0
  119. && ownerContext == OpenGLContext::getCurrentContext())
  120. {
  121. glDeleteTextures (1, &textureID);
  122. textureID = 0;
  123. width = 0;
  124. height = 0;
  125. }
  126. }
  127. void OpenGLTexture::bind() const
  128. {
  129. glBindTexture (GL_TEXTURE_2D, textureID);
  130. }
  131. void OpenGLTexture::unbind() const
  132. {
  133. glBindTexture (GL_TEXTURE_2D, 0);
  134. }
  135. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  136. void OpenGLTexture::draw2D (float x1, float y1,
  137. float x2, float y2,
  138. float x3, float y3,
  139. float x4, float y4,
  140. Colour colour) const
  141. {
  142. bind();
  143. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  144. unbind();
  145. }
  146. void OpenGLTexture::draw3D (float x1, float y1, float z1,
  147. float x2, float y2, float z2,
  148. float x3, float y3, float z3,
  149. float x4, float y4, float z4,
  150. Colour colour) const
  151. {
  152. bind();
  153. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  154. unbind();
  155. }
  156. #endif