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.

203 lines
6.9KB

  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. class OpenGLFrameBufferImage : public ImagePixelData
  20. {
  21. public:
  22. OpenGLFrameBufferImage (OpenGLContext& c, int w, int h)
  23. : ImagePixelData (Image::ARGB, w, h),
  24. context (c),
  25. pixelStride (4),
  26. lineStride (width * pixelStride)
  27. {
  28. }
  29. bool initialise()
  30. {
  31. return frameBuffer.initialise (context, width, height);
  32. }
  33. LowLevelGraphicsContext* createLowLevelContext() override
  34. {
  35. sendDataChangeMessage();
  36. return createOpenGLGraphicsContext (context, frameBuffer);
  37. }
  38. ImageType* createType() const override { return new OpenGLImageType(); }
  39. ImagePixelData::Ptr clone() override
  40. {
  41. Image newImage (new OpenGLFrameBufferImage (context, width, height));
  42. Graphics g (newImage);
  43. g.drawImageAt (Image (this), 0, 0, false);
  44. return newImage.getPixelData();
  45. }
  46. void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode) override
  47. {
  48. bitmapData.pixelFormat = pixelFormat;
  49. bitmapData.lineStride = lineStride;
  50. bitmapData.pixelStride = pixelStride;
  51. switch (mode)
  52. {
  53. case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  54. case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
  55. case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  56. default: jassertfalse; break;
  57. }
  58. if (mode != Image::BitmapData::readOnly)
  59. sendDataChangeMessage();
  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 ((size_t) w);
  82. const size_t rowSize = sizeof (PixelARGB) * (size_t) 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& fb, int x, int y, int w, int h) noexcept
  96. : frameBuffer (fb), area (x, y, w, h)
  97. {}
  98. void write (const PixelARGB* const data) const noexcept
  99. {
  100. HeapBlock<PixelARGB> invertedCopy ((size_t) (area.getWidth() * area.getHeight()));
  101. const size_t rowSize = sizeof (PixelARGB) * (size_t) 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 ((size_t) (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 ImagePixelData::Ptr();
  149. im->frameBuffer.clear (Colours::transparentBlack);
  150. return im.release();
  151. }
  152. OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
  153. {
  154. if (OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData()))
  155. return &(glImage->frameBuffer);
  156. return nullptr;
  157. }