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.

169 lines
5.9KB

  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. OpenGLFrameBufferImage::OpenGLFrameBufferImage (int width, int height)
  20. : Image::SharedImage (Image::ARGB, width, height),
  21. pixelStride (4),
  22. lineStride (width * pixelStride)
  23. {
  24. frameBuffer.initialise (width, height);
  25. frameBuffer.clear (Colours::transparentBlack);
  26. }
  27. OpenGLFrameBufferImage::~OpenGLFrameBufferImage() {}
  28. LowLevelGraphicsContext* OpenGLFrameBufferImage::createLowLevelContext()
  29. {
  30. return new OpenGLRenderer (frameBuffer);
  31. }
  32. Image::SharedImage* OpenGLFrameBufferImage::clone()
  33. {
  34. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (getWidth(), getHeight());
  35. im->incReferenceCount();
  36. {
  37. Image newImage (im);
  38. Graphics g (newImage);
  39. g.drawImageAt (Image (this), 0, 0, false);
  40. }
  41. im->resetReferenceCount();
  42. return im;
  43. }
  44. Image::ImageType OpenGLFrameBufferImage::getType() const
  45. {
  46. return Image::NativeImage;
  47. }
  48. namespace OpenGLImageHelpers
  49. {
  50. struct Dummy
  51. {
  52. Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
  53. static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
  54. static void write (const PixelARGB*) noexcept {}
  55. };
  56. struct Reader
  57. {
  58. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  59. {
  60. frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
  61. Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
  62. verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
  63. }
  64. static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
  65. {
  66. HeapBlock<PixelARGB> tempRow (w);
  67. const int rowSize = sizeof (PixelARGB) * w;
  68. for (int y = 0; y < h / 2; ++y)
  69. {
  70. PixelARGB* const row1 = data + y * w;
  71. PixelARGB* const row2 = data + (h - 1 - y) * w;
  72. memcpy (tempRow, row1, rowSize);
  73. memcpy (row1, row2, rowSize);
  74. memcpy (row2, tempRow, rowSize);
  75. }
  76. }
  77. };
  78. struct Writer
  79. {
  80. Writer (OpenGLFrameBuffer& frameBuffer_, int x, int y, int w, int h) noexcept
  81. : frameBuffer (frameBuffer_), area (x, y, w, h)
  82. {}
  83. void write (const PixelARGB* const data) const noexcept
  84. {
  85. HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
  86. const int rowSize = sizeof (PixelARGB) * area.getWidth();
  87. for (int y = 0; y < area.getHeight(); ++y)
  88. memcpy (invertedCopy + area.getWidth() * y,
  89. data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
  90. frameBuffer.writePixels (invertedCopy, area);
  91. }
  92. OpenGLFrameBuffer& frameBuffer;
  93. const Rectangle<int> area;
  94. JUCE_DECLARE_NON_COPYABLE (Writer);
  95. };
  96. template <class ReaderType, class WriterType>
  97. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  98. {
  99. DataReleaser (OpenGLFrameBuffer& frameBuffer, int x, int y, int w, int h)
  100. : data (w * h),
  101. writer (frameBuffer, x, y, w, h)
  102. {}
  103. ~DataReleaser()
  104. {
  105. writer.write (data);
  106. }
  107. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  108. {
  109. DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
  110. bitmapData.dataReleaser = r;
  111. bitmapData.data = (uint8*) (r->data + (x + y * bitmapData.width));
  112. ReaderType::read (frameBuffer, bitmapData, x, y);
  113. }
  114. HeapBlock<PixelARGB> data;
  115. WriterType writer;
  116. };
  117. }
  118. void OpenGLFrameBufferImage::initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y,
  119. Image::BitmapData::ReadWriteMode mode)
  120. {
  121. using namespace OpenGLImageHelpers;
  122. bitmapData.pixelFormat = format;
  123. bitmapData.lineStride = lineStride;
  124. bitmapData.pixelStride = pixelStride;
  125. switch (mode)
  126. {
  127. case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  128. case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
  129. case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  130. default: jassertfalse; break;
  131. }
  132. }
  133. END_JUCE_NAMESPACE