1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: http://www.qt-project.org/legal
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 <QtCore/QUrl>
52#include <QtCore/QDebug>
53#include <QtGui/QGuiApplication>
54#include <QtQml/QQmlApplicationEngine>
55
56#include <QtQml/qqml.h>
57#include <QtQml/QQmlEngine>
58
59#include <QtGui/QPainter>
60#include <QtGui/QImage>
61
62#include <QtCore/QDateTime>
63
64#include "QtWaylandCompositor/private/qwltexturesharingextension_p.h"
65
66class CustomSharingExtension : public QWaylandTextureSharingExtension
67{
68 Q_OBJECT
69public:
70 CustomSharingExtension() {qDebug("Instantiating custom texture sharing extension.");}
71protected:
72 bool customPixelData(const QString &key, QByteArray *data, QSize *size, uint *glInternalFormat) override
73 {
74 qDebug() << "CustomSharingExtension looking for local texture data for" << key;
75 if (key.startsWith("unreasonably large ")) {
76 int w = 10000;
77 int h = 10000;
78 int numBytes = w * h * 4;
79 *data = QByteArray(numBytes, 0);
80 quint32 *pixels = reinterpret_cast<quint32*>(data->data());
81 for (int i = 0; i < w*h; ++i)
82 pixels[i] = 0xff7f1fff;
83 *glInternalFormat = GL_RGBA8;
84 *size = QSize(w,h);
85 return true;
86 }
87
88 QImage img;
89
90 if (key == QLatin1String("test pattern 1")) {
91 img = QImage(128,128,QImage::Format_ARGB32_Premultiplied);
92 img.fill(QColor(0x55,0x0,0x55,0x01));
93 {
94 QPainter p(&img);
95 QPen pen = p.pen();
96 pen.setWidthF(3);
97 pen.setColor(Qt::red);
98 p.setPen(pen);
99 p.drawLine(0,0,128,128);
100 pen.setColor(Qt::green);
101 p.setPen(pen);
102 p.drawLine(128,0,0,128);
103 pen.setColor(Qt::blue);
104 p.setPen(pen);
105 p.drawLine(32,16,96,16);
106 pen.setColor(Qt::black);
107 p.setPen(pen);
108 p.translate(64, 64);
109 p.rotate(45);
110 p.drawText(QRect(-48, -32, 96, 64),
111 QDateTime::currentDateTime().toString(),
112 QTextOption(Qt::AlignHCenter));
113 }
114 }
115
116 if (!img.isNull()) {
117 img = img.convertToFormat(QImage::Format_RGBA8888);
118 *data = QByteArray(reinterpret_cast<const char*>(img.constBits()), img.sizeInBytes());
119 *size = img.size();
120 *glInternalFormat = GL_RGBA8;
121 return true;
122 }
123 return false;
124 }
125};
126
127Q_COMPOSITOR_DECLARE_QUICK_EXTENSION_CLASS(CustomSharingExtension);
128
129int main(int argc, char *argv[])
130{
131 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
132 QGuiApplication app(argc, argv);
133 QQmlApplicationEngine appEngine;
134
135 qmlRegisterType<CustomSharingExtensionQuickExtension>("com.theqtcompany.customsharingextension", 1, 0, "CustomSharingExtension");
136 appEngine.addImageProvider("wlshared", new QWaylandSharedTextureProvider);
137
138 appEngine.load(QUrl("qrc:///qml/main.qml"));
139
140 return app.exec();
141}
142
143#include "main.moc"
144