| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qbuffer.h" |
| 5 | #include "qbuffer_p.h" |
| 6 | #include <Qt3DCore/private/corelogging_p.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | namespace Qt3DCore { |
| 11 | |
| 12 | const char *QBufferPrivate::UpdateDataPropertyName = "QT3D_updateData"; |
| 13 | |
| 14 | QBufferPrivate::QBufferPrivate() |
| 15 | : QNodePrivate() |
| 16 | , m_usage(QBuffer::StaticDraw) |
| 17 | , m_access(QBuffer::Write) |
| 18 | , m_dirty(false) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | QBufferPrivate *QBufferPrivate::get(QBuffer *q) |
| 23 | { |
| 24 | return q->d_func(); |
| 25 | } |
| 26 | |
| 27 | void QBufferPrivate::update() |
| 28 | { |
| 29 | if (!m_blockNotifications) { |
| 30 | m_dirty = true; |
| 31 | markDirty(changes: QScene::BuffersDirty); |
| 32 | } |
| 33 | QNodePrivate::update(); |
| 34 | } |
| 35 | |
| 36 | void QBufferPrivate::setData(const QByteArray &data) |
| 37 | { |
| 38 | // this is called when date is loaded from backend, should not set dirty flag |
| 39 | Q_Q(QBuffer); |
| 40 | const bool blocked = q->blockNotifications(block: true); |
| 41 | m_data = data; |
| 42 | emit q->dataChanged(bytes: data); |
| 43 | q->blockNotifications(block: blocked); |
| 44 | } |
| 45 | |
| 46 | /*! |
| 47 | * \qmltype Buffer |
| 48 | * \nativetype Qt3DCore::QBuffer |
| 49 | * \inqmlmodule Qt3D.Core |
| 50 | * |
| 51 | * \brief Provides a data store for raw data to later be used as vertices or |
| 52 | * uniforms. |
| 53 | */ |
| 54 | |
| 55 | /*! |
| 56 | * \qmlproperty QBuffer::UsageType Buffer::usage |
| 57 | * |
| 58 | * Holds the buffer usage. |
| 59 | */ |
| 60 | |
| 61 | /*! |
| 62 | * \class Qt3DCore::QBuffer |
| 63 | * \inheaderfile Qt3DCore/QBuffer |
| 64 | * \inmodule Qt3DCore |
| 65 | * |
| 66 | * \inherits Qt3DCore::QNode |
| 67 | * |
| 68 | * \brief Provides a data store for raw data to later be used as vertices or |
| 69 | * uniforms. |
| 70 | * |
| 71 | * Data can be provided directly using QBuffer::setData(). |
| 72 | */ |
| 73 | |
| 74 | /*! |
| 75 | * \fn void Qt3DCore::QBuffer::dataChanged(const QByteArray &bytes) |
| 76 | * |
| 77 | * This signal is emitted with \a bytes when data changes. |
| 78 | */ |
| 79 | |
| 80 | /*! |
| 81 | * \fn void Qt3DCore::QBuffer::dataAvailable() |
| 82 | * |
| 83 | * This signal is emitted when data becomes available. |
| 84 | */ |
| 85 | |
| 86 | /*! |
| 87 | * \enum Qt3DCore::QBuffer::UsageType |
| 88 | * |
| 89 | * The type of the usage. |
| 90 | * |
| 91 | * \value StreamDraw |
| 92 | * GL_STREAM_DRAW |
| 93 | * \value StreamRead |
| 94 | * GL_STREAM_READ |
| 95 | * \value StreamCopy |
| 96 | * GL_STREAM_COPY |
| 97 | * \value StaticDraw |
| 98 | * GL_STATIC_DRAW |
| 99 | * \value StaticRead |
| 100 | * GL_STATIC_READ |
| 101 | * \value StaticCopy |
| 102 | * GL_STATIC_COPY |
| 103 | * \value DynamicDraw |
| 104 | * GL_DYNAMIC_DRAW |
| 105 | * \value DynamicRead |
| 106 | * GL_DYNAMIC_READ |
| 107 | * \value DynamicCopy |
| 108 | * GL_DYNAMIC_COPY |
| 109 | */ |
| 110 | |
| 111 | /*! |
| 112 | * \enum Qt3DCore::QBuffer::AccessType |
| 113 | * |
| 114 | * \value Write |
| 115 | * Write access |
| 116 | * \value Read |
| 117 | * Read access |
| 118 | * \value ReadWrite |
| 119 | * Write|Read |
| 120 | */ |
| 121 | |
| 122 | /*! |
| 123 | * Constructs a new QBuffer with \a parent. |
| 124 | */ |
| 125 | QBuffer::QBuffer(QNode *parent) |
| 126 | : QNode(*new QBufferPrivate(), parent) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | /*! |
| 131 | * \internal |
| 132 | */ |
| 133 | QBuffer::~QBuffer() |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | /*! |
| 138 | * Sets \a bytes as data. |
| 139 | */ |
| 140 | void QBuffer::setData(const QByteArray &bytes) |
| 141 | { |
| 142 | Q_D(QBuffer); |
| 143 | if (bytes != d->m_data) { |
| 144 | d->setData(bytes); |
| 145 | d->update(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | * Updates the data by replacing it with \a bytes at \a offset. |
| 151 | */ |
| 152 | void QBuffer::updateData(int offset, const QByteArray &bytes) |
| 153 | { |
| 154 | Q_D(QBuffer); |
| 155 | Q_ASSERT(offset >= 0 && (offset + bytes.size()) <= d->m_data.size()); |
| 156 | |
| 157 | // Update data |
| 158 | d->m_data.replace(index: offset, len: bytes.size(), s: bytes); |
| 159 | const bool blocked = blockNotifications(block: true); |
| 160 | emit dataChanged(bytes: d->m_data); |
| 161 | blockNotifications(block: blocked); |
| 162 | |
| 163 | QBufferUpdate updateData; |
| 164 | updateData.offset = offset; |
| 165 | updateData.data = bytes; |
| 166 | |
| 167 | QVariantList updateDataList; |
| 168 | const QVariant propertyData = property(name: QBufferPrivate::UpdateDataPropertyName); |
| 169 | if (propertyData.isValid()) |
| 170 | updateDataList = propertyData.toList(); |
| 171 | updateDataList.push_back(t: QVariant::fromValue(value: updateData)); |
| 172 | |
| 173 | setProperty(name: QBufferPrivate::UpdateDataPropertyName, value: updateDataList); |
| 174 | d->update(); |
| 175 | } |
| 176 | |
| 177 | /*! |
| 178 | * \return the data. |
| 179 | */ |
| 180 | QByteArray QBuffer::data() const |
| 181 | { |
| 182 | Q_D(const QBuffer); |
| 183 | return d->m_data; |
| 184 | } |
| 185 | |
| 186 | /*! |
| 187 | * \property Qt3DCore::QBuffer::usage |
| 188 | * |
| 189 | * Holds the buffer usage. |
| 190 | */ |
| 191 | QBuffer::UsageType QBuffer::usage() const |
| 192 | { |
| 193 | Q_D(const QBuffer); |
| 194 | return d->m_usage; |
| 195 | } |
| 196 | |
| 197 | void QBuffer::setUsage(QBuffer::UsageType usage) |
| 198 | { |
| 199 | Q_D(QBuffer); |
| 200 | if (usage != d->m_usage) { |
| 201 | d->m_usage = usage; |
| 202 | emit usageChanged(usage); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void QBuffer::setAccessType(QBuffer::AccessType access) |
| 207 | { |
| 208 | Q_D(QBuffer); |
| 209 | if (d->m_access != access) { |
| 210 | d->m_access = access; |
| 211 | Q_EMIT accessTypeChanged(access); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | * \property Qt3DCore::QBuffer::accessType |
| 217 | * |
| 218 | * Returns the \l {QBuffer::}{AccessType} of the buffer. |
| 219 | * |
| 220 | * \sa QBuffer::AccessType |
| 221 | */ |
| 222 | QBuffer::AccessType QBuffer::accessType() const |
| 223 | { |
| 224 | Q_D(const QBuffer); |
| 225 | return d->m_access; |
| 226 | } |
| 227 | |
| 228 | } // namespace Qt3DCore |
| 229 | |
| 230 | QT_END_NAMESPACE |
| 231 | |
| 232 | #include "moc_qbuffer.cpp" |
| 233 |
