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.

201 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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& c, int w, int h)
  21. : ImagePixelData (Image::ARGB, w, h),
  22. context (c),
  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. sendDataChangeMessage();
  34. return createOpenGLGraphicsContext (context, frameBuffer);
  35. }
  36. ImageType* createType() const override { return new OpenGLImageType(); }
  37. ImagePixelData::Ptr clone() override
  38. {
  39. Image newImage (new OpenGLFrameBufferImage (context, width, height));
  40. Graphics g (newImage);
  41. g.drawImageAt (Image (this), 0, 0, false);
  42. return newImage.getPixelData();
  43. }
  44. void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode) override
  45. {
  46. bitmapData.pixelFormat = pixelFormat;
  47. bitmapData.lineStride = lineStride;
  48. bitmapData.pixelStride = pixelStride;
  49. switch (mode)
  50. {
  51. case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  52. case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
  53. case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  54. default: jassertfalse; break;
  55. }
  56. if (mode != Image::BitmapData::readOnly)
  57. sendDataChangeMessage();
  58. }
  59. OpenGLContext& context;
  60. OpenGLFrameBuffer frameBuffer;
  61. private:
  62. int pixelStride, lineStride;
  63. struct Dummy
  64. {
  65. Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
  66. static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
  67. static void write (const PixelARGB*) noexcept {}
  68. };
  69. struct Reader
  70. {
  71. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  72. {
  73. frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
  74. Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
  75. verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
  76. }
  77. static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
  78. {
  79. HeapBlock<PixelARGB> tempRow ((size_t) w);
  80. const size_t rowSize = sizeof (PixelARGB) * (size_t) w;
  81. for (int y = 0; y < h / 2; ++y)
  82. {
  83. PixelARGB* const row1 = data + y * w;
  84. PixelARGB* const row2 = data + (h - 1 - y) * w;
  85. memcpy (tempRow, row1, rowSize);
  86. memcpy (row1, row2, rowSize);
  87. memcpy (row2, tempRow, rowSize);
  88. }
  89. }
  90. };
  91. struct Writer
  92. {
  93. Writer (OpenGLFrameBuffer& fb, int x, int y, int w, int h) noexcept
  94. : frameBuffer (fb), area (x, y, w, h)
  95. {}
  96. void write (const PixelARGB* const data) const noexcept
  97. {
  98. HeapBlock<PixelARGB> invertedCopy ((size_t) (area.getWidth() * area.getHeight()));
  99. const size_t rowSize = sizeof (PixelARGB) * (size_t) area.getWidth();
  100. for (int y = 0; y < area.getHeight(); ++y)
  101. memcpy (invertedCopy + area.getWidth() * y,
  102. data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
  103. frameBuffer.writePixels (invertedCopy, area);
  104. }
  105. OpenGLFrameBuffer& frameBuffer;
  106. const Rectangle<int> area;
  107. JUCE_DECLARE_NON_COPYABLE (Writer)
  108. };
  109. template <class ReaderType, class WriterType>
  110. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  111. {
  112. DataReleaser (OpenGLFrameBuffer& fb, int x, int y, int w, int h)
  113. : data ((size_t) (w * h)),
  114. writer (fb, x, y, w, h)
  115. {}
  116. ~DataReleaser()
  117. {
  118. writer.write (data);
  119. }
  120. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  121. {
  122. DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
  123. bitmapData.dataReleaser = r;
  124. bitmapData.data = (uint8*) r->data.getData();
  125. bitmapData.lineStride = (bitmapData.width * bitmapData.pixelStride + 3) & ~3;
  126. ReaderType::read (frameBuffer, bitmapData, x, y);
  127. }
  128. HeapBlock<PixelARGB> data;
  129. WriterType writer;
  130. };
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBufferImage)
  132. };
  133. //==============================================================================
  134. OpenGLImageType::OpenGLImageType() {}
  135. OpenGLImageType::~OpenGLImageType() {}
  136. int OpenGLImageType::getTypeID() const
  137. {
  138. return 3;
  139. }
  140. ImagePixelData::Ptr OpenGLImageType::create (Image::PixelFormat, int width, int height, bool /*shouldClearImage*/) const
  141. {
  142. OpenGLContext* currentContext = OpenGLContext::getCurrentContext();
  143. jassert (currentContext != nullptr); // an OpenGL image can only be created when a valid context is active!
  144. ScopedPointer<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (*currentContext, width, height));
  145. if (! im->initialise())
  146. return ImagePixelData::Ptr();
  147. im->frameBuffer.clear (Colours::transparentBlack);
  148. return im.release();
  149. }
  150. OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
  151. {
  152. if (OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData()))
  153. return &(glImage->frameBuffer);
  154. return nullptr;
  155. }