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.

198 lines
6.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class OpenGLFrameBufferImage : public ImagePixelData
  21. {
  22. public:
  23. OpenGLFrameBufferImage (int width, int height)
  24. : ImagePixelData (Image::ARGB, width, height),
  25. pixelStride (4),
  26. lineStride (width * pixelStride)
  27. {
  28. frameBuffer.initialise (width, height);
  29. frameBuffer.clear (Colours::transparentBlack);
  30. }
  31. LowLevelGraphicsContext* createLowLevelContext()
  32. {
  33. return new OpenGLRenderer (frameBuffer);
  34. }
  35. ImageType* createType() const { return new OpenGLImageType(); }
  36. ImagePixelData* clone()
  37. {
  38. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (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)
  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. OpenGLFrameBuffer frameBuffer;
  62. private:
  63. int pixelStride, lineStride;
  64. struct Dummy
  65. {
  66. Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
  67. static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
  68. static void write (const PixelARGB*) noexcept {}
  69. };
  70. struct Reader
  71. {
  72. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  73. {
  74. frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
  75. Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
  76. verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
  77. }
  78. static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
  79. {
  80. HeapBlock<PixelARGB> tempRow (w);
  81. const int rowSize = sizeof (PixelARGB) * w;
  82. for (int y = 0; y < h / 2; ++y)
  83. {
  84. PixelARGB* const row1 = data + y * w;
  85. PixelARGB* const row2 = data + (h - 1 - y) * w;
  86. memcpy (tempRow, row1, rowSize);
  87. memcpy (row1, row2, rowSize);
  88. memcpy (row2, tempRow, rowSize);
  89. }
  90. }
  91. };
  92. struct Writer
  93. {
  94. Writer (OpenGLFrameBuffer& frameBuffer_, int x, int y, int w, int h) noexcept
  95. : frameBuffer (frameBuffer_), area (x, y, w, h)
  96. {}
  97. void write (const PixelARGB* const data) const noexcept
  98. {
  99. HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
  100. const int rowSize = sizeof (PixelARGB) * area.getWidth();
  101. for (int y = 0; y < area.getHeight(); ++y)
  102. memcpy (invertedCopy + area.getWidth() * y,
  103. data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
  104. frameBuffer.writePixels (invertedCopy, area);
  105. }
  106. OpenGLFrameBuffer& frameBuffer;
  107. const Rectangle<int> area;
  108. JUCE_DECLARE_NON_COPYABLE (Writer);
  109. };
  110. template <class ReaderType, class WriterType>
  111. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  112. {
  113. DataReleaser (OpenGLFrameBuffer& frameBuffer, int x, int y, int w, int h)
  114. : data (w * h),
  115. writer (frameBuffer, x, y, w, h)
  116. {}
  117. ~DataReleaser()
  118. {
  119. writer.write (data);
  120. }
  121. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  122. {
  123. DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
  124. bitmapData.dataReleaser = r;
  125. bitmapData.data = (uint8*) (r->data + (x + y * bitmapData.width));
  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* OpenGLImageType::create (Image::PixelFormat, int width, int height, bool shouldClearImage) const
  141. {
  142. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (width, height);
  143. if (shouldClearImage)
  144. im->frameBuffer.clear (Colours::transparentBlack);
  145. return im;
  146. }
  147. OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
  148. {
  149. OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData());
  150. return glImage != nullptr ? &(glImage->frameBuffer) : nullptr;
  151. }
  152. END_JUCE_NAMESPACE