Skip to content

Commit d5dda66

Browse files
committed
Send numeric identifiers instead of strings
Saves bandwidth by using a number instead of sending the full string to identify the GL command. Change-Id: I5e8010465729d7a9c320d0375582d3a92d76019d Reviewed-by: Mårten Nordheim <[email protected]> Reviewed-by: Edward Welbourne <[email protected]>
1 parent 9e75866 commit d5dda66

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/plugins/platforms/webgl/qwebglcontext.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <QtWebSockets/qwebsocket.h>
4949

5050
#include <cstring>
51+
#include <limits>
5152

5253
QT_BEGIN_NAMESPACE
5354

@@ -286,7 +287,6 @@ static T queryValue(int id, const T &defaultValue = T())
286287
return variant.value<T>();
287288
}
288289

289-
struct GLFunction;
290290
template<typename T>
291291
struct ParameterTypeTraits {
292292
static int typeId() { return qMetaTypeId<T>(); }
@@ -320,6 +320,7 @@ struct GLFunction
320320
};
321321

322322
static QHash<QString, const GLFunction *> byName;
323+
static QStringList remoteFunctionNames;
323324
using ParameterList = QVector<Parameter>;
324325

325326
GLFunction(const QString &remoteName,
@@ -331,18 +332,23 @@ struct GLFunction
331332
{
332333
Q_ASSERT(!byName.contains(localName));
333334
byName.insert(localName, this);
335+
id = remoteFunctionNames.size();
336+
Q_ASSERT(remoteFunctionNames.size() <= std::numeric_limits<quint8>::max());
337+
remoteFunctionNames.append(remoteName);
338+
Q_ASSERT(byName.size() == remoteFunctionNames.size());
334339
}
335340

336341
GLFunction(const QString &name) : GLFunction(name, name, nullptr)
337342
{}
338-
343+
quint8 id;
339344
const QString remoteName;
340345
const QString localName;
341346
const QFunctionPointer functionPointer;
342347
const ParameterList parameters;
343348
};
344349

345350
QHash<QString, const GLFunction *> GLFunction::byName;
351+
QStringList GLFunction::remoteFunctionNames;
346352

347353
template<const GLFunction *Function>
348354
static QWebGLFunctionCall *createEventImpl(bool wait)
@@ -355,7 +361,7 @@ static QWebGLFunctionCall *createEventImpl(bool wait)
355361
if (!clientData || !clientData->socket
356362
|| clientData->socket->state() != QAbstractSocket::ConnectedState)
357363
return nullptr;
358-
return new QWebGLFunctionCall(Function->remoteName, handle->currentSurface(), wait);
364+
return new QWebGLFunctionCall(Function->localName, handle->currentSurface(), wait);
359365
}
360366

361367
static void postEventImpl(QWebGLFunctionCall *event)
@@ -667,7 +673,7 @@ QWEBGL_FUNCTION(disableVertexAttribArray, void, glDisableVertexAttribArray,
667673
QWEBGL_FUNCTION(drawArrays, void, glDrawArrays,
668674
(GLenum) mode, (GLint) first, (GLsizei) count)
669675
{
670-
auto event = currentContext()->createEvent(QStringLiteral("drawArrays"));
676+
auto event = currentContext()->createEvent(QStringLiteral("glDrawArrays"));
671677
if (!event)
672678
return;
673679
event->addParameters(mode, first, count);
@@ -681,7 +687,7 @@ QWEBGL_FUNCTION(drawArrays, void, glDrawArrays,
681687
QWEBGL_FUNCTION(drawElements, void, glDrawElements,
682688
(GLenum) mode, (GLsizei) count, (GLenum) type, (const void *) indices)
683689
{
684-
auto event = currentContext()->createEvent(QStringLiteral("drawElements"));
690+
auto event = currentContext()->createEvent(QStringLiteral("glDrawElements"));
685691
if (!event)
686692
return;
687693
event->addParameters(mode, count, type);
@@ -1177,7 +1183,7 @@ QWEBGL_FUNCTION(shaderSource, void, glShaderSource,
11771183
(GLuint) shader, (GLsizei) count,
11781184
(const GLchar *const *) string, (const GLint *) length)
11791185
{
1180-
auto event = currentContext()->createEvent(QStringLiteral("shaderSource"));
1186+
auto event = currentContext()->createEvent(QStringLiteral("glShaderSource"));
11811187
if (!event)
11821188
return;
11831189
event->addParameters(shader, count);
@@ -1578,4 +1584,16 @@ QVariant QWebGLContext::queryValue(int id)
15781584
return variant;
15791585
}
15801586

1587+
QStringList QWebGLContext::supportedFunctions()
1588+
{
1589+
return GLFunction::remoteFunctionNames;
1590+
}
1591+
1592+
quint8 QWebGLContext::functionIndex(const QString &functionName)
1593+
{
1594+
const auto it = GLFunction::byName.find(functionName);
1595+
Q_ASSERT(it != GLFunction::byName.end());
1596+
return (*it)->id;
1597+
}
1598+
15811599
QT_END_NAMESPACE

src/plugins/platforms/webgl/qwebglcontext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class QWebGLContext : public QPlatformOpenGLContext
6060
static QWebGLFunctionCall *createEvent(const QString &functionName, bool wait = false);
6161
static QVariant queryValue(int id);
6262

63+
static QStringList supportedFunctions();
64+
static quint8 functionIndex(const QString &functionName);
65+
6366
private:
6467
Q_DISABLE_COPY(QWebGLContext)
6568
Q_DECLARE_PRIVATE(QWebGLContext)

src/plugins/platforms/webgl/qwebglwebsocketserver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void QWebGLWebSocketServer::sendMessage(QWebSocket *socket,
175175
QByteArray data;
176176
{
177177
QDataStream stream(&data, QIODevice::WriteOnly);
178-
stream << functionName;
178+
stream << QWebGLContext::functionIndex(functionName);
179179
if (values.contains("id")) {
180180
auto ok = false;
181181
stream << quint32(values["id"].toUInt(&ok));
@@ -295,6 +295,8 @@ void QWebGLWebSocketServer::onNewConnection()
295295
#endif
296296
},
297297
{ QStringLiteral("loadingScreen"), qgetenv("QT_WEBGL_LOADINGSCREEN") },
298+
{ QStringLiteral("supportedFunctions"),
299+
QVariant::fromValue(QWebGLContext::supportedFunctions()) },
298300
{ "sysinfo",
299301
QVariantMap {
300302
{ QStringLiteral("buildAbi"), QSysInfo::buildAbi() },

src/plugins/platforms/webgl/webqt.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ window.onload = function () {
6565
}
6666
};
6767
}
68+
var supportedFunctions;
6869

6970
var sendObject = function (obj) { socket.send(JSON.stringify(obj)); }
7071

@@ -967,10 +968,8 @@ window.onload = function () {
967968

968969
var offset = 0;
969970
var obj = { "parameters" : [] };
970-
obj["functionNameSize"] = view.getUint32(offset);
971-
offset += 4;
972-
obj["function"] = textDecoder.decode(new Uint8Array(buffer, offset, obj.functionNameSize));
973-
offset += obj.functionNameSize;
971+
obj["function"] = supportedFunctions[view.getUint8(offset)];
972+
offset += 1;
974973
if (obj.function in commandsNeedingResponse) {
975974
obj["id"] = view.getUint32(offset);
976975
offset += 4;
@@ -1153,6 +1152,7 @@ window.onload = function () {
11531152
} else if (obj.type === "change_title") {
11541153
document.title = obj.text;
11551154
} else if (obj.type === "connect") {
1155+
supportedFunctions = obj["supportedFunctions"];
11561156
var sysinfo = obj["sysinfo"];
11571157
if (obj["debug"])
11581158
DEBUG = 1;

0 commit comments

Comments
 (0)