VVISF & VVGL
GLBuffer.hpp
1 #ifndef VVGL_GLBuffer_hpp
2 #define VVGL_GLBuffer_hpp
3 
4 #include "VVGL_Defines.hpp"
5 
6 #include <vector>
7 #include <chrono>
8 #include "GLContext.hpp"
9 
10 
11 
12 
13 namespace VVGL {
14 
15 
16 using namespace std;
17 
18 
19 
20 
22 
31 class VVGL_EXPORT GLBuffer {
32 
33  public:
35  using BackingReleaseCallback = function<void(GLBuffer&, void*)>;
36 
37 
39  enum Type {
56  };
57 
58 
59  // enums describing the various GL object (usually texture) properties- split up b/c availability depends on platform
60 #if defined(VVGL_SDK_MAC)
61  #include "GLBuffer_Enums_Mac.h"
62 #elif defined(VVGL_SDK_RPI)
63  #include "GLBuffer_Enums_RPI.h"
64 #elif defined(VVGL_SDK_IOS)
65  #include "GLBuffer_Enums_IOS.h"
66 #elif defined(VVGL_SDK_GLFW)
67  #include "GLBuffer_Enums_GLFW.h"
68 #elif defined(VVGL_SDK_QT)
69  #include "GLBuffer_Enums_Qt.h"
70 #endif
71 
72 
76  enum Backing {
79  Backing_External
80  };
81 
82 
84 
87  enum BackingID {
108  };
109 
110 
112 
115  struct VVGL_EXPORT Descriptor {
117  Type type = Type_Tex;
119  Target target = Target_2D;
121 #if defined(VVGL_SDK_MAC)
122  InternalFormat internalFormat = IF_RGBA8;
123 #else
124  InternalFormat internalFormat = IF_RGBA;
125 #endif
126  PixelFormat pixelFormat = PF_RGBA;
129  PixelType pixelType = PT_UByte;
131  Backing cpuBackingType = Backing_None;
133  Backing gpuBackingType = Backing_None;
135  bool texRangeFlag = false;
137  bool texClientStorageFlag = false;
138  uint32_t msAmount = 0;
140  uint32_t localSurfaceID = 0;
141 
143  uint32_t bytesPerRowForWidth(const uint32_t & w) const;
145  uint32_t backingLengthForSize(const Size & s) const;
146  };
147 
148 
149  public:
152 
154  uint32_t name = 0;
156  bool preferDeletion = false;
158  Size size = { 0, 0 };
160  Rect srcRect = { 0, 0, 0, 0 };
162  bool flipped = false;
164  Size backingSize = { 0, 0 };
166  Timestamp contentTimestamp = { static_cast<uint64_t>(0), static_cast<uint32_t>(0) };
168  bool pboMapped = false;
169 
170 
172  BackingReleaseCallback backingReleaseCallback = nullptr;
174  void *backingContext = nullptr;
176  BackingID backingID = BackingID_None;
178  void *cpuBackingPtr = nullptr;
179 
180  private:
181 #if defined(VVGL_SDK_MAC)
182  //id userInfo = nullptr; // RETAINED, nil by default. not used by this class- stick whatever you want here and it will be retained for the lifetime of this buffer. retained if you copy the buffer!
184  IOSurfaceRef _localSurfaceRef = nullptr;
186  IOSurfaceRef _remoteSurfaceRef = nullptr;
187 #endif
188 
189  public:
190  // Every GLBuffer maintains a strong ref to the GLBufferPoolRef that created it, when the buffer is deallocated its underlying GL resources are returned to the pool where they are either released or recycled.
191  GLBufferPoolRef parentBufferPool = nullptr;
192  // If this GLBuffer was created by copying another buffer using GLBufferCopy(), the original source buffer is retained here. If you copy a copy, the original GLBuffer is retained here.
193  GLBufferRef copySourceBuffer = nullptr;
194  int idleCount = 0;
195 
196 
197  // public methods
198  public:
199  GLBuffer(GLBuffer &&) = default;
200  GLBuffer() = default;
201  GLBuffer(GLBufferPoolRef inParentPool);
202  GLBuffer(const GLBuffer &);
203  virtual ~GLBuffer();
204  friend ostream & operator<<(ostream & os, const GLBuffer & n) { os << n.getDescriptionString(); return os; }
205 
206  // copy assignment operators are disabled to prevent accidents
207  GLBuffer& operator=(const GLBuffer&) = delete;
208  //GLBuffer& operator=(GLBuffer&) = delete;
209  //GLBuffer (GLBuffer&&) = default;
210 
212  uint32_t calculateBackingBytesPerRow() { return desc.bytesPerRowForWidth(static_cast<uint32_t>(round(backingSize.width))); }
214  uint32_t calculateBackingLength() { return desc.backingLengthForSize(backingSize); }
215 
216  // use this to create a shallow copy (memberwise copy)
217  GLBuffer * allocShallowCopy();
218 
219 #if defined(VVGL_SDK_MAC)
220  // getter/setters
221  //id getUserInfo() const;
222  //void setUserInfo(id n);
223  IOSurfaceRef localSurfaceRef() const;
224  void setLocalSurfaceRef(const IOSurfaceRef & n);
225  IOSurfaceRef remoteSurfaceRef() const;
226  void setRemoteSurfaceRef(const IOSurfaceRef & n);
227 #endif
228 
229  // member methods
230  bool isComparableForRecycling(const GLBuffer::Descriptor & n) const;
231  uint32_t backingLengthForSize(Size s) const;
233  Rect glReadySrcRect() const;
234  /*
235  Rect croppedSrcRect(Rect & cropRect, bool & takeFlipIntoAccount) const;
236  */
238  bool isFullFrame() const;
240  bool isNPOT2DTex() const;
242  bool isPOT2DTex() const;
243 #if defined(VVGL_SDK_MAC)
244  bool safeToPublishToSyphon() const;
246 #endif
247 
248 #if !defined(VVGL_TARGETENV_GLES) && !defined(VVGL_TARGETENV_GLES3)
249 
254  void mapPBO(const uint32_t & inAccess, const bool & inUseCurrentContext=false);
259  void unmapPBO(const bool & inUseCurrentContext=false);
260 #endif // !defined(VVGL_TARGETENV_GLES) && !defined(VVGL_TARGETENV_GLES3)
261  bool isContentMatch(GLBuffer & n) const;
263  //void draw(const Rect & dst) const;
265  string getDescriptionString() const;
266 };
267 
268 
269 
270 
272 
276 VVGL_EXPORT GLBufferRef GLBufferCopy(const GLBufferRef & n);
277 
278 
279 
280 
281 }
282 
283 
284 
285 
286 #endif /* VVGL_GLBuffer_hpp */
Vertex Attribute Object
Definition: GLBuffer.hpp:55
The GLBuffer was created from an external, CPU-based source.
Definition: GLBuffer.hpp:103
Type
Describes the several different kinds of GLBuffers.
Definition: GLBuffer.hpp:39
Definition: GLBuffer.hpp:13
The CPU backing was a QImage, which will be deleted when the buffer&#39;s resources are no longer needed...
Definition: GLBuffer.hpp:105
The CPU backing is a GWorld, and must be freed appropriately.
Definition: GLBuffer.hpp:91
Texture.
Definition: GLBuffer.hpp:47
STL namespace.
uint32_t backingLengthForSize(const Size &s) const
Returns the amount of memory in bytes required to accommodate a buffer of the passed size with the re...
Basic struct for 2D size.
Definition: VVGL_Geom.hpp:46
There is no resource.
Definition: GLBuffer.hpp:77
The GPU backing was a CVOpenGLTextureRef allocated by CoreVideo- this GLBuffer maintains a strong ref...
Definition: GLBuffer.hpp:97
shared_ptr< GLBuffer > GLBufferRef
Very common- GLBufferRef is a shared pointer around a GLBuffer.
Definition: VVGL_Base.hpp:44
Descriptor desc
Describes basic properties of the underlying resources.
Definition: GLBuffer.hpp:151
BackingID
The "BackingID" is an arbitrary enum that isn&#39;t used functionally by this lib.
Definition: GLBuffer.hpp:87
Representation of a GL resource of some sort- most commonly an OpenGL texture, but can also be other ...
Definition: GLBuffer.hpp:31
Pixel Buffer Object.
Definition: GLBuffer.hpp:49
CPU-only buffer.
Definition: GLBuffer.hpp:41
The Descriptor struct describes the hardware attributes of the GLBuffer.
Definition: GLBuffer.hpp:115
uint32_t calculateBackingLength()
Calculates the total number of bytes that are required to contain this GLBuffer&#39;s image data in syste...
Definition: GLBuffer.hpp:214
uint32_t bytesPerRowForWidth(const uint32_t &w) const
Returns the amount of memory in bytes that a single row of the provided width requires with the recei...
The CPU backing was a QVideoFrame, which will be deleted when the buffer&#39;s resources are no longer ne...
Definition: GLBuffer.hpp:107
The GLBuffer was created from an IOSurface (Mac SDK only) that was generated by another process...
Definition: GLBuffer.hpp:101
function< void(GLBuffer &, void *)> BackingReleaseCallback
This defines a callback that is used to release the backing of a GLBuffer, where appropriate. The GLBuffer being released is the first var, and the GLBuffer&#39;s backingContext is the second var. The backingContext is probably a pointer to an object from another SDK that has been retained, and needs to be released/freed using the appropriate means.
Definition: GLBuffer.hpp:35
Basic struct for a rectangle using VVGL::Point and VVGL::Size.
Definition: VVGL_Geom.hpp:65
The CPU backing was allocated by this library, and will be freed when the buffer&#39;s resources are no l...
Definition: GLBuffer.hpp:93
FBO.
Definition: GLBuffer.hpp:45
The resource was created by this framework (and should be deleted by this framework) ...
Definition: GLBuffer.hpp:78
uint32_t calculateBackingBytesPerRow()
Calculates the number of bytes that are required to display one row&#39;s worth of image data from this G...
Definition: GLBuffer.hpp:212
No backing ID or unknown backing ID.
Definition: GLBuffer.hpp:89
InternalFormat
Definition: GLBuffer.hpp:12
The CPU backing was a CVPixelBufferRef allocated by CoreVideo- this GLBuffer maintains a strong refer...
Definition: GLBuffer.hpp:95
The CPU backing was an NSBitmapImageRep, which will be freed when the buffer&#39;s resources are no longe...
Definition: GLBuffer.hpp:99
Element Buffer Object.
Definition: GLBuffer.hpp:53
string getDescriptionString() const
Returns a std::string that describes some basic properties of the GLBuffer instance.
Backing
Indicates the relationship this buffer has with its backing.
Definition: GLBuffer.hpp:76
Vertex Buffer Object
Definition: GLBuffer.hpp:51
Renderbuffer.
Definition: GLBuffer.hpp:43