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.

193 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. OpenGLTexture::OpenGLTexture()
  21. : textureID (0), width (0), height (0), ownerContext (nullptr)
  22. {
  23. }
  24. OpenGLTexture::~OpenGLTexture()
  25. {
  26. release();
  27. }
  28. bool OpenGLTexture::isValidSize (int width, int height)
  29. {
  30. return isPowerOfTwo (width) && isPowerOfTwo (height);
  31. }
  32. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  33. {
  34. ownerContext = OpenGLContext::getCurrentContext();
  35. // Texture objects can only be created when the current thread has an active OpenGL
  36. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  37. jassert (ownerContext != nullptr);
  38. if (textureID == 0)
  39. {
  40. JUCE_CHECK_OPENGL_ERROR
  41. glGenTextures (1, &textureID);
  42. glBindTexture (GL_TEXTURE_2D, textureID);
  43. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  44. auto glMagFilter = (GLint) (ownerContext->texMagFilter == OpenGLContext::linear ? GL_LINEAR : GL_NEAREST);
  45. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilter);
  46. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  47. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  48. JUCE_CHECK_OPENGL_ERROR
  49. }
  50. else
  51. {
  52. glBindTexture (GL_TEXTURE_2D, textureID);
  53. JUCE_CHECK_OPENGL_ERROR;
  54. }
  55. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  56. JUCE_CHECK_OPENGL_ERROR
  57. const auto textureNpotSupported = ownerContext->isTextureNpotSupported();
  58. const auto getAllowedTextureSize = [&] (int n)
  59. {
  60. return textureNpotSupported ? n : nextPowerOfTwo (n);
  61. };
  62. width = getAllowedTextureSize (w);
  63. height = getAllowedTextureSize (h);
  64. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  65. if (width != w || height != h)
  66. {
  67. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  68. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  69. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  70. type, GL_UNSIGNED_BYTE, pixels);
  71. }
  72. else
  73. {
  74. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  75. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  76. }
  77. JUCE_CHECK_OPENGL_ERROR
  78. }
  79. template <class PixelType>
  80. struct Flipper
  81. {
  82. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  83. const int w, const int h)
  84. {
  85. dataCopy.malloc (w * h);
  86. for (int y = 0; y < h; ++y)
  87. {
  88. auto* src = (const PixelType*) srcData;
  89. auto* dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  90. for (int x = 0; x < w; ++x)
  91. dst[x].set (src[x]);
  92. srcData += lineStride;
  93. }
  94. }
  95. };
  96. void OpenGLTexture::loadImage (const Image& image)
  97. {
  98. const int imageW = image.getWidth();
  99. const int imageH = image.getHeight();
  100. HeapBlock<PixelARGB> dataCopy;
  101. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  102. switch (srcData.pixelFormat)
  103. {
  104. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  105. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  106. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  107. case Image::UnknownFormat:
  108. default: break;
  109. }
  110. create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
  111. }
  112. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  113. {
  114. create (w, h, pixels, JUCE_RGBA_FORMAT, false);
  115. }
  116. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  117. {
  118. create (w, h, pixels, GL_ALPHA, false);
  119. }
  120. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  121. {
  122. HeapBlock<PixelARGB> flippedCopy;
  123. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
  124. create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
  125. }
  126. void OpenGLTexture::release()
  127. {
  128. if (textureID != 0)
  129. {
  130. // If the texture is deleted while the owner context is not active, it's
  131. // impossible to delete it, so this will be a leak until the context itself
  132. // is deleted.
  133. jassert (ownerContext == OpenGLContext::getCurrentContext());
  134. if (ownerContext == OpenGLContext::getCurrentContext())
  135. {
  136. glDeleteTextures (1, &textureID);
  137. textureID = 0;
  138. width = 0;
  139. height = 0;
  140. }
  141. }
  142. }
  143. void OpenGLTexture::bind() const
  144. {
  145. glBindTexture (GL_TEXTURE_2D, textureID);
  146. }
  147. void OpenGLTexture::unbind() const
  148. {
  149. glBindTexture (GL_TEXTURE_2D, 0);
  150. }
  151. } // namespace juce