// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qplatformgraphicsbuffer.h"
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFunctions>
#include <QtGui/qopengl.h>
#include <QtCore/QDebug>

QT_BEGIN_NAMESPACE
/*!
    \class QPlatformGraphicsBuffer
    \inmodule QtGui
    \since 5.5
    \brief The QPlatformGraphicsBuffer is a windowsystem abstraction for native graphics buffers

    Different platforms have different ways of representing graphics buffers. On
    some platforms, it is possible to create one graphics buffer that you can bind
    to a texture and also get main memory access to the image bits. On the
    other hand, on some platforms all graphics buffer abstraction is completely
    hidden.

    QPlatformGraphicsBuffer is an abstraction of a single Graphics Buffer.

    There is no public constructor nor any public factory function.

    \internal
*/

/*!
    \enum QPlatformGraphicsBuffer::AccessType

    This enum describes the access that is desired or granted for the graphics
    buffer.

    \value None
    \value SWReadAccess
    \value SWWriteAccess
    \value TextureAccess
    \value HWCompositor
*/

/*!
    \enum QPlatformGraphicsBuffer::Origin

    This enum describes the origin of the content of the buffer.

    \value OriginTopLeft
    \value OriginBottomLeft
*/

/*!
    Protected constructor to initialize the private members.

    \a size is the size of the buffer.
    \a format is the format of the buffer.

    \sa size() format()
*/
QPlatformGraphicsBuffer::QPlatformGraphicsBuffer(const QSize &size, const QPixelFormat &format)
    : m_size(size)
    , m_format(format)
{
}


/*!
    Virtual destructor.
*/
QPlatformGraphicsBuffer::~QPlatformGraphicsBuffer()
{
}

/*!
    Binds the content of this graphics buffer into the currently bound texture.

    This function should fail for buffers not capable of locking to TextureAccess.

    \a rect is the subrect which is desired to be bounded to the texture. This
    argument has a no less than semantic, meaning more (if not all) of the buffer
    can be bounded to the texture. An empty QRect is interpreted as entire buffer
    should be bound.

    This function only supports binding buffers to the GL_TEXTURE_2D texture
    target.

    Returns true on success, otherwise false.
*/
bool QPlatformGraphicsBuffer::bindToTexture(const QRect &rect) const
{
    Q_UNUSED(rect);
    return false;
}

/*!
    \fn QPlatformGraphicsBuffer::AccessTypes QPlatformGraphicsBuffer::isLocked() const
    Function to check if the buffer is locked.

    \sa lock()
*/

/*!
    Before the data can be retrieved or before a buffer can be bound to a
    texture it needs to be locked. This is a separate function call since this
    operation might be time consuming, and it would not be satisfactory to do
    it per function call.

    \a access is the access type wanted.

    \a rect is the subrect which is desired to be locked. This
    argument has a no less than semantic, meaning more (if not all) of the buffer
    can be locked. An empty QRect is interpreted as entire buffer should be locked.

    Return true on successfully locking all AccessTypes specified \a access
    otherwise returns false and no locks have been granted.
*/
bool QPlatformGraphicsBuffer::lock(AccessTypes access, const QRect &rect)
{
    bool locked = doLock(access, rect);
    if (locked)
        m_lock_access |= access;

    return locked;
}

/*!
    Unlocks the current buffer lock.

    This function calls doUnlock, and then emits the unlocked signal with the
    AccessTypes from before doUnlock was called.
*/
void QPlatformGraphicsBuffer::unlock()
{
    if (m_lock_access == None)
        return;
    AccessTypes previous = m_lock_access;
    doUnlock();
    m_lock_access = None;
    emit unlocked(previous);
}


/*!
    \fn QPlatformGraphicsBuffer::doLock(AccessTypes access, const QRect &rect = QRect())

    This function should be reimplemented by subclasses. If one of the \a
    access types specified cannot be locked, then all should fail and this
    function should return false.

    \a rect is the subrect which is desired to be locked. This
    argument has a no less than semantic, meaning more (if not all) of the
    buffer can be locked. An empty QRect should be interpreted as the entire buffer
    should be locked.

    It is safe to call isLocked() to verify the current lock state.
*/

/*!
    \fn QPlatformGraphicsBuffer::doUnlock()

    This function should remove all locks set on the buffer.

    It is safe to call isLocked() to verify the current lock state.
*/

/*!
    \fn QPlatformGraphicsBuffer::unlocked(AccessTypes previousAccessTypes)

    Signal that is emitted after unlocked has been called.

    \a previousAccessTypes is the access types locked before unlock was called.
*/

/*!
    Accessor for the bytes of the buffer. This function needs to be called on a
    buffer with SWReadAccess access lock. Behavior is undefined for modifying
    the memory returned when not having a SWWriteAccess.
*/
const uchar *QPlatformGraphicsBuffer::data() const
{ return nullptr; }

/*!
    Accessor for the bytes of the buffer. This function needs to be called on a
    buffer with SWReadAccess access lock. Behavior is undefined for modifying
    the memory returned when not having a SWWriteAccess.
*/
uchar *QPlatformGraphicsBuffer::data()
{
    return nullptr;
}

/*!
    Accessor for the length of the data buffer. This function is a convenience
    function multiplying height of buffer with bytesPerLine().

    \sa data() bytesPerLine() size()
*/
int QPlatformGraphicsBuffer::byteCount() const
{
    return size().height() * bytesPerLine();
}

/*!
    Accessor for bytes per line in the graphics buffer.
*/
int QPlatformGraphicsBuffer::bytesPerLine() const
{
    return 0;
}


/*!
    In origin of the content of the graphics buffer.

    Default implementation is OriginTopLeft, as this is the coordinate
    system default for Qt. However, for most regular OpenGL textures
    this will be OriginBottomLeft.
*/
QPlatformGraphicsBuffer::Origin QPlatformGraphicsBuffer::origin() const
{
    return OriginTopLeft;
}

/*!
    \fn QPlatformGraphicsBuffer::size() const

    Accessor for content size.
*/

/*!
    \fn QPlatformGraphicsBuffer::format() const

    Accessor for the pixel format of the buffer.
*/

QT_END_NAMESPACE

#include "moc_qplatformgraphicsbuffer.cpp"
