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.

131 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Creates an openGL frame buffer.
  21. */
  22. class JUCE_API OpenGLFrameBuffer
  23. {
  24. public:
  25. /** Creates an uninitialised buffer.
  26. To actually allocate the buffer, use initialise().
  27. */
  28. OpenGLFrameBuffer();
  29. /** Destructor. */
  30. ~OpenGLFrameBuffer();
  31. //==============================================================================
  32. /** Tries to allocates a buffer of the given size.
  33. Note that a valid openGL context must be selected when you call this method,
  34. or it will fail.
  35. */
  36. bool initialise (OpenGLContext& context, int width, int height);
  37. /** Tries to allocates a buffer containing a copy of a given image.
  38. Note that a valid openGL context must be selected when you call this method,
  39. or it will fail.
  40. */
  41. bool initialise (OpenGLContext& context, const Image& content);
  42. /** Tries to allocate a copy of another framebuffer.
  43. */
  44. bool initialise (OpenGLFrameBuffer& other);
  45. /** Releases the buffer, if one has been allocated.
  46. Any saved state that was created with saveAndRelease() will also be freed by this call.
  47. */
  48. void release();
  49. /** If the framebuffer is active, this will save a stashed copy of its contents in main memory,
  50. and will release the GL buffer.
  51. After saving, the original state can be restored again by calling reloadSavedCopy().
  52. */
  53. void saveAndRelease();
  54. /** Restores the framebuffer content that was previously saved using saveAndRelease().
  55. After saving to main memory, the original state can be restored by calling restoreToGPUMemory().
  56. */
  57. bool reloadSavedCopy (OpenGLContext& context);
  58. //==============================================================================
  59. /** Returns true if a valid buffer has been allocated. */
  60. bool isValid() const noexcept { return pimpl != nullptr; }
  61. /** Returns the width of the buffer. */
  62. int getWidth() const noexcept;
  63. /** Returns the height of the buffer. */
  64. int getHeight() const noexcept;
  65. /** Returns the texture ID number for using this buffer as a texture. */
  66. GLuint getTextureID() const noexcept;
  67. //==============================================================================
  68. /** Selects this buffer as the current OpenGL rendering target. */
  69. bool makeCurrentRenderingTarget();
  70. /** Deselects this buffer as the current OpenGL rendering target. */
  71. void releaseAsRenderingTarget();
  72. /** Returns the ID of this framebuffer, or 0 if it isn't initialised. */
  73. GLuint getFrameBufferID() const noexcept;
  74. /** Returns the current frame buffer ID for the current context. */
  75. static GLuint getCurrentFrameBufferTarget() noexcept;
  76. /** Clears the framebuffer with the specified colour. */
  77. void clear (Colour colour);
  78. /** Selects the framebuffer as the current target, and clears it to transparent. */
  79. void makeCurrentAndClear();
  80. /** Reads an area of pixels from the framebuffer into a 32-bit ARGB pixel array.
  81. The lineStride is measured as a number of pixels, not bytes - pass a stride
  82. of 0 to indicate a packed array.
  83. */
  84. bool readPixels (PixelARGB* targetData, const Rectangle<int>& sourceArea);
  85. /** Writes an area of pixels into the framebuffer from a specified pixel array.
  86. The lineStride is measured as a number of pixels, not bytes - pass a stride
  87. of 0 to indicate a packed array.
  88. */
  89. bool writePixels (const PixelARGB* srcData, const Rectangle<int>& targetArea);
  90. private:
  91. class Pimpl;
  92. friend struct ContainerDeletePolicy<Pimpl>;
  93. ScopedPointer<Pimpl> pimpl;
  94. class SavedState;
  95. friend struct ContainerDeletePolicy<SavedState>;
  96. ScopedPointer<SavedState> savedState;
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
  98. };