File indexing completed on 2024-12-22 04:12:47

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KISOPENGLBUFFERCIRCULARSTORAGE_H
0008 #define KISOPENGLBUFFERCIRCULARSTORAGE_H
0009 
0010 #include <QScopedPointer>
0011 #include <QOpenGLBuffer>
0012 
0013 /**
0014  * A simple storage class that owns a fixed amount of
0015  * QOpenGLBuffer objects and returns them sequentially.
0016  * Using multiple distinct buffers lets us avoid blocks
0017  */
0018 class KisOpenGLBufferCircularStorage
0019 {
0020 public:
0021     struct BufferBinder
0022     {
0023         /**
0024          * When \p bufferStorage is non-null, fetches the
0025          * next buffer from the circular storage,  binds it
0026          * and loads the provided data into the buffer. After
0027          * loading the data pointer `*dataPtr` is reset to null
0028          * to signal that glTexImage2D should not be provided
0029          * with any pointer.
0030          *
0031          * When \p bufferStorage is null, the binder does nothing,
0032          * the data pointer `*dataPtr`is kept unchanged.
0033          */
0034         BufferBinder(KisOpenGLBufferCircularStorage *bufferStorage, const void **dataPtr, int dataSize);
0035         ~BufferBinder();
0036 
0037         BufferBinder(const BufferBinder &) = delete;
0038         BufferBinder &operator=(const BufferBinder &) = delete;
0039         BufferBinder(BufferBinder &&) = delete;
0040         BufferBinder &operator=(BufferBinder &&) = delete;
0041 
0042     private:
0043         QOpenGLBuffer *m_buffer = nullptr;
0044     };
0045 
0046     KisOpenGLBufferCircularStorage();
0047     KisOpenGLBufferCircularStorage(QOpenGLBuffer::Type type);
0048     ~KisOpenGLBufferCircularStorage();
0049 
0050     KisOpenGLBufferCircularStorage(const KisOpenGLBufferCircularStorage &) = delete;
0051     KisOpenGLBufferCircularStorage &operator=(const KisOpenGLBufferCircularStorage &) = delete;
0052     KisOpenGLBufferCircularStorage(KisOpenGLBufferCircularStorage &&) = delete;
0053     KisOpenGLBufferCircularStorage &operator=(KisOpenGLBufferCircularStorage &&) = delete;
0054 
0055     void allocate(int numBuffers, int bufferSize);
0056     QOpenGLBuffer* getNextBuffer();
0057     bool isValid() const;
0058     int size() const;
0059 
0060     void reset();
0061 
0062     void allocateMoreBuffers();
0063 
0064 private:
0065     void addBuffersImpl(size_t buffersToAdd, int bufferSize);
0066 
0067     struct Private;
0068     const QScopedPointer<Private> m_d;
0069 };
0070 
0071 #endif // KISOPENGLBUFFERCIRCULARSTORAGE_H