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.

193 lines
6.7KB

  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. class OpenGLFrameBufferImage : public ImagePixelData
  19. {
  20. public:
  21. OpenGLFrameBufferImage (int width, int height)
  22. : ImagePixelData (Image::ARGB, width, height),
  23. pixelStride (4),
  24. lineStride (width * pixelStride)
  25. {
  26. frameBuffer.initialise (width, height);
  27. frameBuffer.clear (Colours::transparentBlack);
  28. }
  29. LowLevelGraphicsContext* createLowLevelContext()
  30. {
  31. return new OpenGLGraphicsContext (frameBuffer);
  32. }
  33. ImageType* createType() const { return new OpenGLImageType(); }
  34. ImagePixelData* clone()
  35. {
  36. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (width, height);
  37. im->incReferenceCount();
  38. {
  39. Image newImage (im);
  40. Graphics g (newImage);
  41. g.drawImageAt (Image (this), 0, 0, false);
  42. }
  43. im->resetReferenceCount();
  44. return im;
  45. }
  46. void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode)
  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. }
  59. OpenGLFrameBuffer frameBuffer;
  60. private:
  61. int pixelStride, lineStride;
  62. struct Dummy
  63. {
  64. Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
  65. static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
  66. static void write (const PixelARGB*) noexcept {}
  67. };
  68. struct Reader
  69. {
  70. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  71. {
  72. frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
  73. Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
  74. verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
  75. }
  76. static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
  77. {
  78. HeapBlock<PixelARGB> tempRow (w);
  79. const int rowSize = sizeof (PixelARGB) * w;
  80. for (int y = 0; y < h / 2; ++y)
  81. {
  82. PixelARGB* const row1 = data + y * w;
  83. PixelARGB* const row2 = data + (h - 1 - y) * w;
  84. memcpy (tempRow, row1, rowSize);
  85. memcpy (row1, row2, rowSize);
  86. memcpy (row2, tempRow, rowSize);
  87. }
  88. }
  89. };
  90. struct Writer
  91. {
  92. Writer (OpenGLFrameBuffer& frameBuffer_, int x, int y, int w, int h) noexcept
  93. : frameBuffer (frameBuffer_), area (x, y, w, h)
  94. {}
  95. void write (const PixelARGB* const data) const noexcept
  96. {
  97. HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
  98. const int rowSize = sizeof (PixelARGB) * area.getWidth();
  99. for (int y = 0; y < area.getHeight(); ++y)
  100. memcpy (invertedCopy + area.getWidth() * y,
  101. data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
  102. frameBuffer.writePixels (invertedCopy, area);
  103. }
  104. OpenGLFrameBuffer& frameBuffer;
  105. const Rectangle<int> area;
  106. JUCE_DECLARE_NON_COPYABLE (Writer);
  107. };
  108. template <class ReaderType, class WriterType>
  109. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  110. {
  111. DataReleaser (OpenGLFrameBuffer& frameBuffer, int x, int y, int w, int h)
  112. : data (w * h),
  113. writer (frameBuffer, x, y, w, h)
  114. {}
  115. ~DataReleaser()
  116. {
  117. writer.write (data);
  118. }
  119. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  120. {
  121. DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
  122. bitmapData.dataReleaser = r;
  123. bitmapData.data = (uint8*) (r->data + (x + y * bitmapData.width));
  124. ReaderType::read (frameBuffer, bitmapData, x, y);
  125. }
  126. HeapBlock<PixelARGB> data;
  127. WriterType writer;
  128. };
  129. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBufferImage);
  130. };
  131. //==============================================================================
  132. OpenGLImageType::OpenGLImageType() {}
  133. OpenGLImageType::~OpenGLImageType() {}
  134. int OpenGLImageType::getTypeID() const
  135. {
  136. return 3;
  137. }
  138. ImagePixelData* OpenGLImageType::create (Image::PixelFormat, int width, int height, bool shouldClearImage) const
  139. {
  140. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (width, height);
  141. if (shouldClearImage)
  142. im->frameBuffer.clear (Colours::transparentBlack);
  143. return im;
  144. }
  145. OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
  146. {
  147. OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData());
  148. return glImage != nullptr ? &(glImage->frameBuffer) : nullptr;
  149. }