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.

135 lines
4.8KB

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