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.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. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  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 ((size_t) (w * h));
  89. for (int y = 0; y < h; ++y)
  90. {
  91. const PixelType* src = (const PixelType*) srcData;
  92. PixelARGB* const 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. default: break;
  111. }
  112. create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
  113. }
  114. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  115. {
  116. create (w, h, pixels, JUCE_RGBA_FORMAT, false);
  117. }
  118. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  119. {
  120. create (w, h, pixels, GL_ALPHA, false);
  121. }
  122. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  123. {
  124. HeapBlock<PixelARGB> flippedCopy;
  125. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
  126. create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
  127. }
  128. void OpenGLTexture::release()
  129. {
  130. if (textureID != 0)
  131. {
  132. // If the texture is deleted while the owner context is not active, it's
  133. // impossible to delete it, so this will be a leak until the context itself
  134. // is deleted.
  135. jassert (ownerContext == OpenGLContext::getCurrentContext());
  136. if (ownerContext == OpenGLContext::getCurrentContext())
  137. {
  138. glDeleteTextures (1, &textureID);
  139. textureID = 0;
  140. width = 0;
  141. height = 0;
  142. }
  143. }
  144. }
  145. void OpenGLTexture::bind() const
  146. {
  147. glBindTexture (GL_TEXTURE_2D, textureID);
  148. }
  149. void OpenGLTexture::unbind() const
  150. {
  151. glBindTexture (GL_TEXTURE_2D, 0);
  152. }
  153. } // namespace juce