| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2019 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Wayland module |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
#include <QGuiApplication> |
| 52 |
#include <QtGui/private/qguiapplication_p.h> |
| 53 |
#include <QOpenGLWindow> |
| 54 |
#include <QOpenGLTexture> |
| 55 |
#include <QOpenGLTextureBlitter> |
| 56 |
#include <QPainter> |
| 57 |
#include <QMouseEvent> |
| 58 |
#include <QPlatformSurfaceEvent> |
| 59 |
|
| 60 |
#include <QtWaylandClient/private/qwaylanddisplay_p.h> |
| 61 |
#include <QtWaylandClient/private/qwaylandintegration_p.h> |
| 62 |
#include <QtWaylandClient/private/qwaylandserverbufferintegration_p.h> |
| 63 |
#include "texturesharingextension.h" |
| 64 |
|
| 65 |
#include <QDebug> |
| 66 |
#include <QtGui/qpa/qplatformnativeinterface.h> |
| 67 |
#include <QTimer> |
| 68 |
#include <QMap> |
| 69 |
|
| 70 |
class TestWindow : public QOpenGLWindow |
| 71 |
{ |
| 72 |
Q_OBJECT |
| 73 |
|
| 74 |
public: |
| 75 |
TestWindow() |
| 76 |
: m_extension(nullptr) |
| 77 |
{ |
| 78 |
m_extension = new TextureSharingExtension; |
| 79 |
connect(m_extension, SIGNAL(bufferReceived(QtWaylandClient::QWaylandServerBuffer*, const QString&)), this, SLOT(receiveBuffer(QtWaylandClient::QWaylandServerBuffer*, const QString&))); |
| 80 |
connect(m_extension, &TextureSharingExtension::activeChanged, this, &TestWindow::handleExtensionActive); |
| 81 |
} |
| 82 |
|
| 83 |
public slots: |
| 84 |
void receiveBuffer(QtWaylandClient::QWaylandServerBuffer *buffer, const QString &key) |
| 85 |
{ |
| 86 |
if (!buffer) { |
| 87 |
qWarning() << "Could not find image with key" << key; |
| 88 |
return; |
| 89 |
} |
| 90 |
m_buffers.insert(key, buffer); |
| 91 |
update(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
void handleExtensionActive() |
| 96 |
{ |
| 97 |
if (m_extension->isActive()) |
| 98 |
getImage("qt_logo"); |
| 99 |
} |
| 100 |
|
| 101 |
protected: |
| 102 |
|
| 103 |
void mousePressEvent(QMouseEvent *ev) override { |
| 104 |
QRect rect(10, height() - 10 - 50, 50, 50); |
| 105 |
bool rectPressed = rect.contains(ev->pos()); |
| 106 |
|
| 107 |
static int c; |
| 108 |
|
| 109 |
if (rectPressed && ev->button() == Qt::LeftButton) |
| 110 |
getImage(QString("unreasonably large image %1").arg(c++)); |
| 111 |
else if (ev->button() == Qt::RightButton) |
| 112 |
getImage("guitar.jpg"); |
| 113 |
else if (ev->button() == Qt::MiddleButton) |
| 114 |
unloadImageAt(ev->pos()); |
| 115 |
} |
| 116 |
|
| 117 |
void initializeGL() override |
| 118 |
{ |
| 119 |
m_blitter = new QOpenGLTextureBlitter; |
| 120 |
m_blitter->create(); |
| 121 |
} |
| 122 |
|
| 123 |
void paintGL() override { |
| 124 |
glClearColor(.5, .45, .42, 1.); |
| 125 |
glClear(GL_COLOR_BUFFER_BIT); |
| 126 |
|
| 127 |
// draw a "button" to click in |
| 128 |
glScissor(10,10,50,50); |
| 129 |
glEnable(GL_SCISSOR_TEST); |
| 130 |
glClearColor(0.4, 0.7, 0.9, 1.0); |
| 131 |
glClear(GL_COLOR_BUFFER_BIT); |
| 132 |
glDisable(GL_SCISSOR_TEST); |
| 133 |
|
| 134 |
int x = 0; |
| 135 |
qDebug() << "*** paintGL ***"; |
| 136 |
showBuffers(); |
| 137 |
for (auto buffer: qAsConst(m_buffers)) { |
| 138 |
m_blitter->bind(); |
| 139 |
QSize s(buffer->size()); |
| 140 |
qDebug() << "painting" << buffer << s; |
| 141 |
if (s.width() > 1024) { |
| 142 |
qDebug() << "showing large buffer at reduced size"; |
| 143 |
s = QSize(128,128); |
| 144 |
} |
| 145 |
QRectF targetRect(QPointF(x,0), s); |
| 146 |
QOpenGLTexture *texture = buffer->toOpenGlTexture(); |
| 147 |
if (!texture) { |
| 148 |
qWarning("Null texture"); |
| 149 |
continue; |
| 150 |
} |
| 151 |
auto surfaceOrigin = QOpenGLTextureBlitter::OriginTopLeft; |
| 152 |
QMatrix4x4 targetTransform = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(), size())); |
| 153 |
m_blitter->blit(texture->textureId(), targetTransform, surfaceOrigin); |
| 154 |
m_blitter->release(); |
| 155 |
x += s.width() + 10; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
private: |
| 160 |
void getImage(const QString &key) { |
| 161 |
if (!m_buffers.contains(key)) |
| 162 |
m_extension->requestImage(key); |
| 163 |
} |
| 164 |
|
| 165 |
void showBuffers() const |
| 166 |
{ |
| 167 |
auto end = m_buffers.cend(); |
| 168 |
for (auto it = m_buffers.cbegin(); it != end; ++it) { |
| 169 |
qDebug() << " " << it.key() << it.value(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void unloadImageAt(const QPoint &pos) { |
| 174 |
int x = 0; |
| 175 |
QtWaylandClient::QWaylandServerBuffer *foundBuffer = nullptr; |
| 176 |
QString name; |
| 177 |
auto end = m_buffers.cend(); |
| 178 |
for (auto it = m_buffers.cbegin(); it != end; ++it) { |
| 179 |
auto *buffer = it.value(); |
| 180 |
QSize s(buffer->size()); |
| 181 |
if (s.width() > 1024) |
| 182 |
s = QSize(128,128); |
| 183 |
QRectF targetRect(QPointF(x,0), s); |
| 184 |
//qDebug() << " " << it.key() << it.value() << targetRect << pos; |
| 185 |
|
| 186 |
if (targetRect.contains(pos)) { |
| 187 |
foundBuffer = buffer; |
| 188 |
name = it.key(); |
| 189 |
//qDebug() << "FOUND!!"; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
x += s.width() + 10; |
| 194 |
} |
| 195 |
if (foundBuffer) { |
| 196 |
qDebug() << "unloading image" << name << "found at" << pos; |
| 197 |
unloadImage(name); |
| 198 |
} else { |
| 199 |
qDebug() << "no image at" << pos; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void unloadImage(const QString &key) { |
| 204 |
auto *buf = m_buffers.take(key); |
| 205 |
if (buf) { |
| 206 |
qDebug() << "unloadImage deleting" << buf; |
| 207 |
delete buf; |
| 208 |
m_extension->abandonImage(key); |
| 209 |
} |
| 210 |
update(); |
| 211 |
} |
| 212 |
|
| 213 |
QOpenGLTextureBlitter *m_blitter = nullptr; |
| 214 |
TextureSharingExtension *m_extension = nullptr; |
| 215 |
QMap<QString, QtWaylandClient::QWaylandServerBuffer*> m_buffers; |
| 216 |
|
| 217 |
}; |
| 218 |
|
| 219 |
int main (int argc, char **argv) |
| 220 |
{ |
| 221 |
QGuiApplication app(argc, argv); |
| 222 |
|
| 223 |
TestWindow window; |
| 224 |
window.show(); |
| 225 |
|
| 226 |
return app.exec(); |
| 227 |
} |
| 228 |
|
| 229 |
#include "main.moc" |