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.

188 lines
5.6KB

  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. static int getAllowedTextureSize (int x)
  20. {
  21. #if JUCE_OPENGL_ALLOW_NON_POWER_OF_TWO_TEXTURES
  22. return x;
  23. #else
  24. return nextPowerOfTwo (x);
  25. #endif
  26. }
  27. OpenGLTexture::OpenGLTexture()
  28. : textureID (0), width (0), height (0), ownerContext (nullptr)
  29. {
  30. }
  31. OpenGLTexture::~OpenGLTexture()
  32. {
  33. release();
  34. }
  35. bool OpenGLTexture::isValidSize (int width, int height)
  36. {
  37. return isPowerOfTwo (width) && isPowerOfTwo (height);
  38. }
  39. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  40. {
  41. ownerContext = OpenGLContext::getCurrentContext();
  42. // Texture objects can only be created when the current thread has an active OpenGL
  43. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  44. jassert (ownerContext != nullptr);
  45. if (textureID == 0)
  46. {
  47. JUCE_CHECK_OPENGL_ERROR
  48. glGenTextures (1, &textureID);
  49. glBindTexture (GL_TEXTURE_2D, textureID);
  50. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  51. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  52. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  53. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  54. JUCE_CHECK_OPENGL_ERROR
  55. }
  56. else
  57. {
  58. glBindTexture (GL_TEXTURE_2D, textureID);
  59. JUCE_CHECK_OPENGL_ERROR;
  60. }
  61. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  62. JUCE_CHECK_OPENGL_ERROR
  63. width = getAllowedTextureSize (w);
  64. height = getAllowedTextureSize (h);
  65. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  66. if (width != w || height != h)
  67. {
  68. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  69. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  70. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  71. type, GL_UNSIGNED_BYTE, pixels);
  72. }
  73. else
  74. {
  75. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  76. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  77. }
  78. JUCE_CHECK_OPENGL_ERROR
  79. }
  80. template <class PixelType>
  81. struct Flipper
  82. {
  83. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  84. const int w, const int h)
  85. {
  86. dataCopy.malloc ((size_t) (w * h));
  87. for (int y = 0; y < h; ++y)
  88. {
  89. const PixelType* src = (const PixelType*) srcData;
  90. PixelARGB* const dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  91. for (int x = 0; x < w; ++x)
  92. dst[x].set (src[x]);
  93. srcData += lineStride;
  94. }
  95. }
  96. };
  97. void OpenGLTexture::loadImage (const Image& image)
  98. {
  99. const int imageW = image.getWidth();
  100. const int imageH = image.getHeight();
  101. HeapBlock<PixelARGB> dataCopy;
  102. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  103. switch (srcData.pixelFormat)
  104. {
  105. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  106. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  107. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  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. }