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.

201 lines
7.0KB

  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 (OpenGLContext& context_, int width, int height)
  22. : ImagePixelData (Image::ARGB, width, height),
  23. context (context_),
  24. pixelStride (4),
  25. lineStride (width * pixelStride)
  26. {
  27. }
  28. bool initialise()
  29. {
  30. return frameBuffer.initialise (context, width, height);
  31. }
  32. LowLevelGraphicsContext* createLowLevelContext()
  33. {
  34. return createOpenGLGraphicsContext (context, frameBuffer);
  35. }
  36. ImageType* createType() const { return new OpenGLImageType(); }
  37. ImagePixelData* clone()
  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)
  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. }
  62. OpenGLContext& context;
  63. OpenGLFrameBuffer frameBuffer;
  64. private:
  65. int pixelStride, lineStride;
  66. struct Dummy
  67. {
  68. Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
  69. static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
  70. static void write (const PixelARGB*) noexcept {}
  71. };
  72. struct Reader
  73. {
  74. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  75. {
  76. frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
  77. Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
  78. verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
  79. }
  80. static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
  81. {
  82. HeapBlock<PixelARGB> tempRow (w);
  83. const int rowSize = sizeof (PixelARGB) * w;
  84. for (int y = 0; y < h / 2; ++y)
  85. {
  86. PixelARGB* const row1 = data + y * w;
  87. PixelARGB* const row2 = data + (h - 1 - y) * w;
  88. memcpy (tempRow, row1, rowSize);
  89. memcpy (row1, row2, rowSize);
  90. memcpy (row2, tempRow, rowSize);
  91. }
  92. }
  93. };
  94. struct Writer
  95. {
  96. Writer (OpenGLFrameBuffer& frameBuffer_, int x, int y, int w, int h) noexcept
  97. : frameBuffer (frameBuffer_), area (x, y, w, h)
  98. {}
  99. void write (const PixelARGB* const data) const noexcept
  100. {
  101. HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
  102. const int rowSize = sizeof (PixelARGB) * area.getWidth();
  103. for (int y = 0; y < area.getHeight(); ++y)
  104. memcpy (invertedCopy + area.getWidth() * y,
  105. data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
  106. frameBuffer.writePixels (invertedCopy, area);
  107. }
  108. OpenGLFrameBuffer& frameBuffer;
  109. const Rectangle<int> area;
  110. JUCE_DECLARE_NON_COPYABLE (Writer)
  111. };
  112. template <class ReaderType, class WriterType>
  113. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  114. {
  115. DataReleaser (OpenGLFrameBuffer& fb, int x, int y, int w, int h)
  116. : data (w * h),
  117. writer (fb, x, y, w, h)
  118. {}
  119. ~DataReleaser()
  120. {
  121. writer.write (data);
  122. }
  123. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  124. {
  125. DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
  126. bitmapData.dataReleaser = r;
  127. bitmapData.data = (uint8*) (r->data + (x + y * bitmapData.width));
  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 nullptr;
  149. im->frameBuffer.clear (Colours::transparentBlack);
  150. return im.release();
  151. }
  152. OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
  153. {
  154. OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData());
  155. return glImage != nullptr ? &(glImage->frameBuffer) : nullptr;
  156. }