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.

133 lines
4.9KB

  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. #pragma once
  20. //==============================================================================
  21. /**
  22. Creates an openGL frame buffer.
  23. */
  24. class JUCE_API OpenGLFrameBuffer
  25. {
  26. public:
  27. /** Creates an uninitialised buffer.
  28. To actually allocate the buffer, use initialise().
  29. */
  30. OpenGLFrameBuffer();
  31. /** Destructor. */
  32. ~OpenGLFrameBuffer();
  33. //==============================================================================
  34. /** Tries to allocates a buffer of the given size.
  35. Note that a valid openGL context must be selected when you call this method,
  36. or it will fail.
  37. */
  38. bool initialise (OpenGLContext& context, int width, int height);
  39. /** Tries to allocates a buffer containing a copy of a given image.
  40. Note that a valid openGL context must be selected when you call this method,
  41. or it will fail.
  42. */
  43. bool initialise (OpenGLContext& context, const Image& content);
  44. /** Tries to allocate a copy of another framebuffer.
  45. */
  46. bool initialise (OpenGLFrameBuffer& other);
  47. /** Releases the buffer, if one has been allocated.
  48. Any saved state that was created with saveAndRelease() will also be freed by this call.
  49. */
  50. void release();
  51. /** If the framebuffer is active, this will save a stashed copy of its contents in main memory,
  52. and will release the GL buffer.
  53. After saving, the original state can be restored again by calling reloadSavedCopy().
  54. */
  55. void saveAndRelease();
  56. /** Restores the framebuffer content that was previously saved using saveAndRelease().
  57. After saving to main memory, the original state can be restored by calling restoreToGPUMemory().
  58. */
  59. bool reloadSavedCopy (OpenGLContext& context);
  60. //==============================================================================
  61. /** Returns true if a valid buffer has been allocated. */
  62. bool isValid() const noexcept { return pimpl != nullptr; }
  63. /** Returns the width of the buffer. */
  64. int getWidth() const noexcept;
  65. /** Returns the height of the buffer. */
  66. int getHeight() const noexcept;
  67. /** Returns the texture ID number for using this buffer as a texture. */
  68. GLuint getTextureID() const noexcept;
  69. //==============================================================================
  70. /** Selects this buffer as the current OpenGL rendering target. */
  71. bool makeCurrentRenderingTarget();
  72. /** Deselects this buffer as the current OpenGL rendering target. */
  73. void releaseAsRenderingTarget();
  74. /** Returns the ID of this framebuffer, or 0 if it isn't initialised. */
  75. GLuint getFrameBufferID() const noexcept;
  76. /** Returns the current frame buffer ID for the current context. */
  77. static GLuint getCurrentFrameBufferTarget() noexcept;
  78. /** Clears the framebuffer with the specified colour. */
  79. void clear (Colour colour);
  80. /** Selects the framebuffer as the current target, and clears it to transparent. */
  81. void makeCurrentAndClear();
  82. /** Reads an area of pixels from the framebuffer into a 32-bit ARGB pixel array.
  83. The lineStride is measured as a number of pixels, not bytes - pass a stride
  84. of 0 to indicate a packed array.
  85. */
  86. bool readPixels (PixelARGB* targetData, const Rectangle<int>& sourceArea);
  87. /** Writes an area of pixels into the framebuffer from a specified pixel array.
  88. The lineStride is measured as a number of pixels, not bytes - pass a stride
  89. of 0 to indicate a packed array.
  90. */
  91. bool writePixels (const PixelARGB* srcData, const Rectangle<int>& targetArea);
  92. private:
  93. class Pimpl;
  94. friend struct ContainerDeletePolicy<Pimpl>;
  95. ScopedPointer<Pimpl> pimpl;
  96. class SavedState;
  97. friend struct ContainerDeletePolicy<SavedState>;
  98. ScopedPointer<SavedState> savedState;
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
  100. };