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
66#ifndef GL_RGBA8
67#define GL_RGBA8 0x8058
68#endif
69
70class CustomSharingExtension : public QWaylandTextureSharingExtension
71{
72 Q_OBJECT
73public:
74 CustomSharingExtension() {qDebug(msg: "Instantiating custom texture sharing extension.");}
75protected:
76 bool customPixelData(const QString &key, QByteArray *data, QSize *size, uint *glInternalFormat) override
77 {
78 qDebug() << "CustomSharingExtension looking for local texture data for" << key;
79 if (key.startsWith(s: "unreasonably large ")) {
80 int w = 10000;
81 int h = 10000;
82 int numBytes = w * h * 4;
83 *data = QByteArray(numBytes, 0);
84 quint32 *pixels = reinterpret_cast<quint32*>(data->data());
85 for (int i = 0; i < w*h; ++i)
86 pixels[i] = 0xff7f1fff;
87 *glInternalFormat = GL_RGBA8;
88 *size = QSize(w,h);
89 return true;
90 }
91
92 QImage img;
93
94 if (key == QLatin1String("test pattern 1")) {
95 img = QImage(128,128,QImage::Format_ARGB32_Premultiplied);
96 img.fill(color: QColor(0x55,0x0,0x55,0x01));
97 {
98 QPainter p(&img);
99 QPen pen = p.pen();
100 pen.setWidthF(3);
101 pen.setColor(Qt::red);
102 p.setPen(pen);
103 p.drawLine(x1: 0,y1: 0,x2: 128,y2: 128);
104 pen.setColor(Qt::green);
105 p.setPen(pen);
106 p.drawLine(x1: 128,y1: 0,x2: 0,y2: 128);
107 pen.setColor(Qt::blue);
108 p.setPen(pen);
109 p.drawLine(x1: 32,y1: 16,x2: 96,y2: 16);
110 pen.setColor(Qt::black);
111 p.setPen(pen);
112 p.translate(dx: 64, dy: 64);
113 p.rotate(a: 45);
114 p.drawText(r: QRect(-48, -32, 96, 64),
115 text: QDateTime::currentDateTime().toString(),
116 o: QTextOption(Qt::AlignHCenter));
117 }
118 }
119
120 if (!img.isNull()) {
121 img = img.convertToFormat(f: QImage::Format_RGBA8888);
122 *data = QByteArray(reinterpret_cast<const char*>(img.constBits()), img.sizeInBytes());
123 *size = img.size();
124 *glInternalFormat = GL_RGBA8;
125 return true;
126 }
127 return false;
128 }
129};
130
131Q_COMPOSITOR_DECLARE_QUICK_EXTENSION_CLASS(CustomSharingExtension);
132
133int main(int argc, char *argv[])
134{
135 QCoreApplication::setAttribute(attribute: Qt::AA_ShareOpenGLContexts, on: true);
136 QGuiApplication app(argc, argv);
137 QQmlApplicationEngine appEngine;
138
139 qmlRegisterType<CustomSharingExtensionQuickExtension>(uri: "com.theqtcompany.customsharingextension", versionMajor: 1, versionMinor: 0, qmlName: "CustomSharingExtension");
140 appEngine.addImageProvider("wlshared", new QWaylandSharedTextureProvider);
141
142 appEngine.load(url: QUrl("qrc:///qml/main.qml"));
143
144 return app.exec();
145}
146
147#include "main.moc"
148

source code of qtwayland/examples/wayland/texture-sharing/custom-compositor/main.cpp