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.

207 lines
7.0KB

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