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.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. static int getAllowedTextureSize (int x)
  22. {
  23. #if JUCE_OPENGL_ALLOW_NON_POWER_OF_TWO_TEXTURES
  24. return x;
  25. #else
  26. return nextPowerOfTwo (x);
  27. #endif
  28. }
  29. OpenGLTexture::OpenGLTexture()
  30. : textureID (0), width (0), height (0), ownerContext (nullptr)
  31. {
  32. }
  33. OpenGLTexture::~OpenGLTexture()
  34. {
  35. release();
  36. }
  37. bool OpenGLTexture::isValidSize (int width, int height)
  38. {
  39. return isPowerOfTwo (width) && isPowerOfTwo (height);
  40. }
  41. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  42. {
  43. ownerContext = OpenGLContext::getCurrentContext();
  44. // Texture objects can only be created when the current thread has an active OpenGL
  45. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  46. jassert (ownerContext != nullptr);
  47. if (textureID == 0)
  48. {
  49. JUCE_CHECK_OPENGL_ERROR
  50. glGenTextures (1, &textureID);
  51. glBindTexture (GL_TEXTURE_2D, textureID);
  52. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  53. auto glMagFilter = (ownerContext->texMagFilter == OpenGLContext::linear ? GL_LINEAR : GL_NEAREST);
  54. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilter);
  55. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  56. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  57. JUCE_CHECK_OPENGL_ERROR
  58. }
  59. else
  60. {
  61. glBindTexture (GL_TEXTURE_2D, textureID);
  62. JUCE_CHECK_OPENGL_ERROR;
  63. }
  64. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  65. JUCE_CHECK_OPENGL_ERROR
  66. width = getAllowedTextureSize (w);
  67. height = getAllowedTextureSize (h);
  68. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  69. if (width != w || height != h)
  70. {
  71. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  72. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  73. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  74. type, GL_UNSIGNED_BYTE, pixels);
  75. }
  76. else
  77. {
  78. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  79. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  80. }
  81. JUCE_CHECK_OPENGL_ERROR
  82. }
  83. template <class PixelType>
  84. struct Flipper
  85. {
  86. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  87. const int w, const int h)
  88. {
  89. dataCopy.malloc (w * h);
  90. for (int y = 0; y < h; ++y)
  91. {
  92. auto* src = (const PixelType*) srcData;
  93. auto* dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  94. for (int x = 0; x < w; ++x)
  95. dst[x].set (src[x]);
  96. srcData += lineStride;
  97. }
  98. }
  99. };
  100. void OpenGLTexture::loadImage (const Image& image)
  101. {
  102. const int imageW = image.getWidth();
  103. const int imageH = image.getHeight();
  104. HeapBlock<PixelARGB> dataCopy;
  105. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  106. switch (srcData.pixelFormat)
  107. {
  108. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  109. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  110. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  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