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.

195 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. static int getAllowedTextureSize (int x)
  21. {
  22. #if JUCE_OPENGL_ALLOW_NON_POWER_OF_TWO_TEXTURES
  23. return x;
  24. #else
  25. return nextPowerOfTwo (x);
  26. #endif
  27. }
  28. OpenGLTexture::OpenGLTexture()
  29. : textureID (0), width (0), height (0), ownerContext (nullptr)
  30. {
  31. }
  32. OpenGLTexture::~OpenGLTexture()
  33. {
  34. release();
  35. }
  36. bool OpenGLTexture::isValidSize (int width, int height)
  37. {
  38. return isPowerOfTwo (width) && isPowerOfTwo (height);
  39. }
  40. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  41. {
  42. ownerContext = OpenGLContext::getCurrentContext();
  43. // Texture objects can only be created when the current thread has an active OpenGL
  44. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  45. jassert (ownerContext != nullptr);
  46. if (textureID == 0)
  47. {
  48. JUCE_CHECK_OPENGL_ERROR
  49. glGenTextures (1, &textureID);
  50. glBindTexture (GL_TEXTURE_2D, textureID);
  51. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  52. auto glMagFilter = (ownerContext->texMagFilter == OpenGLContext::linear ? GL_LINEAR : GL_NEAREST);
  53. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilter);
  54. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  55. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  56. JUCE_CHECK_OPENGL_ERROR
  57. }
  58. else
  59. {
  60. glBindTexture (GL_TEXTURE_2D, textureID);
  61. JUCE_CHECK_OPENGL_ERROR;
  62. }
  63. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  64. JUCE_CHECK_OPENGL_ERROR
  65. width = getAllowedTextureSize (w);
  66. height = getAllowedTextureSize (h);
  67. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  68. if (width != w || height != h)
  69. {
  70. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  71. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  72. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  73. type, GL_UNSIGNED_BYTE, pixels);
  74. }
  75. else
  76. {
  77. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  78. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  79. }
  80. JUCE_CHECK_OPENGL_ERROR
  81. }
  82. template <class PixelType>
  83. struct Flipper
  84. {
  85. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  86. const int w, const int h)
  87. {
  88. dataCopy.malloc (w * h);
  89. for (int y = 0; y < h; ++y)
  90. {
  91. auto* src = (const PixelType*) srcData;
  92. auto* dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  93. for (int x = 0; x < w; ++x)
  94. dst[x].set (src[x]);
  95. srcData += lineStride;
  96. }
  97. }
  98. };
  99. void OpenGLTexture::loadImage (const Image& image)
  100. {
  101. const int imageW = image.getWidth();
  102. const int imageH = image.getHeight();
  103. HeapBlock<PixelARGB> dataCopy;
  104. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  105. switch (srcData.pixelFormat)
  106. {
  107. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  108. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  109. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  110. case Image::UnknownFormat:
  111. default: break;
  112. }
  113. create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
  114. }
  115. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  116. {
  117. create (w, h, pixels, JUCE_RGBA_FORMAT, false);
  118. }
  119. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  120. {
  121. create (w, h, pixels, GL_ALPHA, false);
  122. }
  123. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  124. {
  125. HeapBlock<PixelARGB> flippedCopy;
  126. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
  127. create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
  128. }
  129. void OpenGLTexture::release()
  130. {
  131. if (textureID != 0)
  132. {
  133. // If the texture is deleted while the owner context is not active, it's
  134. // impossible to delete it, so this will be a leak until the context itself
  135. // is deleted.
  136. jassert (ownerContext == OpenGLContext::getCurrentContext());
  137. if (ownerContext == OpenGLContext::getCurrentContext())
  138. {
  139. glDeleteTextures (1, &textureID);
  140. textureID = 0;
  141. width = 0;
  142. height = 0;
  143. }
  144. }
  145. }
  146. void OpenGLTexture::bind() const
  147. {
  148. glBindTexture (GL_TEXTURE_2D, textureID);
  149. }
  150. void OpenGLTexture::unbind() const
  151. {
  152. glBindTexture (GL_TEXTURE_2D, 0);
  153. }
  154. } // namespace juce