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.

202 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class OpenGLFrameBufferImage : public ImagePixelData
  18. {
  19. public:
  20. OpenGLFrameBufferImage (OpenGLContext& context_, int width, int height)
  21. : ImagePixelData (Image::ARGB, width, height),
  22. context (context_),
  23. pixelStride (4),
  24. lineStride (width * pixelStride)
  25. {
  26. }
  27. bool initialise()
  28. {
  29. return frameBuffer.initialise (context, width, height);
  30. }
  31. LowLevelGraphicsContext* createLowLevelContext() override
  32. {
  33. return createOpenGLGraphicsContext (context, frameBuffer);
  34. }
  35. ImageType* createType() const override { return new OpenGLImageType(); }
  36. ImagePixelData* clone() override
  37. {
  38. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (context, width, height);
  39. im->incReferenceCount();
  40. {
  41. Image newImage (im);
  42. Graphics g (newImage);
  43. g.drawImageAt (Image (this), 0, 0, false);
  44. }
  45. im->resetReferenceCount();
  46. return im;
  47. }
  48. void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode) override
  49. {
  50. bitmapData.pixelFormat = pixelFormat;
  51. bitmapData.lineStride = lineStride;
  52. bitmapData.pixelStride = pixelStride;
  53. switch (mode)
  54. {
  55. case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  56. case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
  57. case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  58. default: jassertfalse; break;
  59. }
  60. }
  61. OpenGLContext& context;
  62. OpenGLFrameBuffer frameBuffer;
  63. private:
  64. int pixelStride, lineStride;
  65. struct Dummy
  66. {
  67. Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
  68. static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
  69. static void write (const PixelARGB*) noexcept {}
  70. };
  71. struct Reader
  72. {
  73. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  74. {
  75. frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
  76. Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
  77. verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
  78. }
  79. static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
  80. {
  81. HeapBlock<PixelARGB> tempRow (w);
  82. const int rowSize = sizeof (PixelARGB) * w;
  83. for (int y = 0; y < h / 2; ++y)
  84. {
  85. PixelARGB* const row1 = data + y * w;
  86. PixelARGB* const row2 = data + (h - 1 - y) * w;
  87. memcpy (tempRow, row1, rowSize);
  88. memcpy (row1, row2, rowSize);
  89. memcpy (row2, tempRow, rowSize);
  90. }
  91. }
  92. };
  93. struct Writer
  94. {
  95. Writer (OpenGLFrameBuffer& frameBuffer_, int x, int y, int w, int h) noexcept
  96. : frameBuffer (frameBuffer_), area (x, y, w, h)
  97. {}
  98. void write (const PixelARGB* const data) const noexcept
  99. {
  100. HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
  101. const int rowSize = sizeof (PixelARGB) * area.getWidth();
  102. for (int y = 0; y < area.getHeight(); ++y)
  103. memcpy (invertedCopy + area.getWidth() * y,
  104. data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
  105. frameBuffer.writePixels (invertedCopy, area);
  106. }
  107. OpenGLFrameBuffer& frameBuffer;
  108. const Rectangle<int> area;
  109. JUCE_DECLARE_NON_COPYABLE (Writer)
  110. };
  111. template <class ReaderType, class WriterType>
  112. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  113. {
  114. DataReleaser (OpenGLFrameBuffer& fb, int x, int y, int w, int h)
  115. : data (w * h),
  116. writer (fb, x, y, w, h)
  117. {}
  118. ~DataReleaser()
  119. {
  120. writer.write (data);
  121. }
  122. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  123. {
  124. DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
  125. bitmapData.dataReleaser = r;
  126. bitmapData.data = (uint8*) r->data.getData();
  127. bitmapData.lineStride = (bitmapData.width * bitmapData.pixelStride + 3) & ~3;
  128. ReaderType::read (frameBuffer, bitmapData, x, y);
  129. }
  130. HeapBlock<PixelARGB> data;
  131. WriterType writer;
  132. };
  133. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBufferImage)
  134. };
  135. //==============================================================================
  136. OpenGLImageType::OpenGLImageType() {}
  137. OpenGLImageType::~OpenGLImageType() {}
  138. int OpenGLImageType::getTypeID() const
  139. {
  140. return 3;
  141. }
  142. ImagePixelData::Ptr OpenGLImageType::create (Image::PixelFormat, int width, int height, bool /*shouldClearImage*/) const
  143. {
  144. OpenGLContext* currentContext = OpenGLContext::getCurrentContext();
  145. jassert (currentContext != nullptr); // an OpenGL image can only be created when a valid context is active!
  146. ScopedPointer<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (*currentContext, width, height));
  147. if (! im->initialise())
  148. return nullptr;
  149. im->frameBuffer.clear (Colours::transparentBlack);
  150. return im.release();
  151. }
  152. OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
  153. {
  154. OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData());
  155. return glImage != nullptr ? &(glImage->frameBuffer) : nullptr;
  156. }