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.

145 lines
4.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 (Image::PixelFormat format, int width, int height)
  20. : Image::SharedImage (format, 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 LowLevelGraphicsSoftwareRenderer (Image (this));
  31. }
  32. Image::SharedImage* OpenGLFrameBufferImage::clone()
  33. {
  34. OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (getPixelFormat(), 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 void*) noexcept {}
  55. };
  56. struct Reader
  57. {
  58. static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  59. {
  60. frameBuffer.readPixels (bitmapData.data, Rectangle<int> (x, y, bitmapData.width, bitmapData.height));
  61. }
  62. };
  63. struct Writer
  64. {
  65. Writer (OpenGLFrameBuffer& frameBuffer_, int x, int y, int w, int h) noexcept
  66. : frameBuffer (frameBuffer_), area (x, y, w, h)
  67. {}
  68. void write (const void* const data) const noexcept
  69. {
  70. frameBuffer.writePixels (data, 4, area);
  71. }
  72. OpenGLFrameBuffer& frameBuffer;
  73. const Rectangle<int> area;
  74. JUCE_DECLARE_NON_COPYABLE (Writer);
  75. };
  76. template <class ReaderType, class WriterType>
  77. struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
  78. {
  79. DataReleaser (OpenGLFrameBuffer& frameBuffer, size_t dataSize, int x, int y, int w, int h)
  80. : data (dataSize),
  81. writer (frameBuffer, x, y, w, h)
  82. {}
  83. ~DataReleaser()
  84. {
  85. writer.write (data);
  86. }
  87. static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
  88. {
  89. DataReleaser* r = new DataReleaser (frameBuffer, bitmapData.lineStride * bitmapData.height,
  90. x, y, bitmapData.width, bitmapData.height);
  91. bitmapData.dataReleaser = r;
  92. bitmapData.data = r->data + x * bitmapData.pixelStride + y * bitmapData.lineStride;
  93. ReaderType::read (frameBuffer, bitmapData, x, y);
  94. }
  95. HeapBlock<uint8> data;
  96. WriterType writer;
  97. };
  98. }
  99. void OpenGLFrameBufferImage::initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y,
  100. Image::BitmapData::ReadWriteMode mode)
  101. {
  102. using namespace OpenGLImageHelpers;
  103. bitmapData.pixelFormat = format;
  104. bitmapData.lineStride = lineStride;
  105. bitmapData.pixelStride = pixelStride;
  106. switch (mode)
  107. {
  108. case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  109. case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
  110. case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
  111. default: jassertfalse; break;
  112. }
  113. }
  114. END_JUCE_NAMESPACE