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.

134 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Creates an openGL frame buffer.
  23. @tags{OpenGL}
  24. */
  25. class JUCE_API OpenGLFrameBuffer
  26. {
  27. public:
  28. /** Creates an uninitialised buffer.
  29. To actually allocate the buffer, use initialise().
  30. */
  31. OpenGLFrameBuffer();
  32. /** Destructor. */
  33. ~OpenGLFrameBuffer();
  34. //==============================================================================
  35. /** Tries to allocates a buffer of the given size.
  36. Note that a valid openGL context must be selected when you call this method,
  37. or it will fail.
  38. */
  39. bool initialise (OpenGLContext& context, int width, int height);
  40. /** Tries to allocates a buffer containing a copy of a given image.
  41. Note that a valid openGL context must be selected when you call this method,
  42. or it will fail.
  43. */
  44. bool initialise (OpenGLContext& context, const Image& content);
  45. /** Tries to allocate a copy of another framebuffer.
  46. */
  47. bool initialise (OpenGLFrameBuffer& other);
  48. /** Releases the buffer, if one has been allocated.
  49. Any saved state that was created with saveAndRelease() will also be freed by this call.
  50. */
  51. void release();
  52. /** If the framebuffer is active, this will save a stashed copy of its contents in main memory,
  53. and will release the GL buffer.
  54. After saving, the original state can be restored again by calling reloadSavedCopy().
  55. */
  56. void saveAndRelease();
  57. /** Restores the framebuffer content that was previously saved using saveAndRelease().
  58. After saving to main memory, the original state can be restored by calling restoreToGPUMemory().
  59. */
  60. bool reloadSavedCopy (OpenGLContext& context);
  61. //==============================================================================
  62. /** Returns true if a valid buffer has been allocated. */
  63. bool isValid() const noexcept { return pimpl != nullptr; }
  64. /** Returns the width of the buffer. */
  65. int getWidth() const noexcept;
  66. /** Returns the height of the buffer. */
  67. int getHeight() const noexcept;
  68. /** Returns the texture ID number for using this buffer as a texture. */
  69. GLuint getTextureID() const noexcept;
  70. //==============================================================================
  71. /** Selects this buffer as the current OpenGL rendering target. */
  72. bool makeCurrentRenderingTarget();
  73. /** Deselects this buffer as the current OpenGL rendering target. */
  74. void releaseAsRenderingTarget();
  75. /** Returns the ID of this framebuffer, or 0 if it isn't initialised. */
  76. GLuint getFrameBufferID() const noexcept;
  77. /** Returns the current frame buffer ID for the current context. */
  78. static GLuint getCurrentFrameBufferTarget() noexcept;
  79. /** Clears the framebuffer with the specified colour. */
  80. void clear (Colour colour);
  81. /** Selects the framebuffer as the current target, and clears it to transparent. */
  82. void makeCurrentAndClear();
  83. /** Reads an area of pixels from the framebuffer into a 32-bit ARGB pixel array.
  84. The lineStride is measured as a number of pixels, not bytes - pass a stride
  85. of 0 to indicate a packed array.
  86. */
  87. bool readPixels (PixelARGB* targetData, const Rectangle<int>& sourceArea);
  88. /** Writes an area of pixels into the framebuffer from a specified pixel array.
  89. The lineStride is measured as a number of pixels, not bytes - pass a stride
  90. of 0 to indicate a packed array.
  91. */
  92. bool writePixels (const PixelARGB* srcData, const Rectangle<int>& targetArea);
  93. private:
  94. class Pimpl;
  95. std::unique_ptr<Pimpl> pimpl;
  96. class SavedState;
  97. std::unique_ptr<SavedState> savedState;
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
  99. };
  100. } // namespace juce