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

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