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.

186 lines
5.5KB

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