Skip to content

Commit f73929c

Browse files
committed
Remove the parameterCount parameter
Saves 4 bytes per message. Combined with other optimizations, this can save a lot of bandwidth. It also adds a new serialization type, the array. The parameter count cannot be removed without supporting arrays. Change-Id: I5f9c0901d50fb7d9613461d9860338e18dcbe3cd Reviewed-by: Mårten Nordheim <[email protected]> Reviewed-by: Edward Welbourne <[email protected]>
1 parent 8d3d896 commit f73929c

File tree

5 files changed

+156
-140
lines changed

5 files changed

+156
-140
lines changed

src/plugins/platforms/webgl/qwebglcontext.cpp

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,22 @@ template<class POINTER, class COUNT>
250250
inline QWebGLFunctionCall *addHelper(QWebGLFunctionCall *event,
251251
const QPair<POINTER, COUNT> &elements)
252252
{
253-
if (event) {
254-
for (auto i = 0; i < elements.second; ++i)
255-
event->add(elements.first[i]);
256-
}
253+
QVariantList list;
254+
for (auto i = 0; i < elements.second; ++i)
255+
list.append(QVariant::fromValue(elements.first[i]));
256+
event->addList(list);
257257
return event;
258258
}
259259

260+
template<class SIZE>
261+
inline void addHelper(QWebGLFunctionCall *event, const QPair<const float *, SIZE> &elements)
262+
{
263+
QVariantList list;
264+
for (auto i = 0; i < elements.second; ++i)
265+
list.append(QVariant::fromValue<double>(elements.first[i]));
266+
event->addList(list);
267+
}
268+
260269
template<class T>
261270
inline QWebGLFunctionCall *addHelper(QWebGLFunctionCall *event, const T &value)
262271
{
@@ -631,7 +640,7 @@ QWEBGL_FUNCTION(deleteBuffers, void, glDeleteBuffers,
631640
QWEBGL_FUNCTION(deleteFramebuffers, void, glDeleteFramebuffers,
632641
(GLsizei) n, (const GLuint *) framebuffers)
633642
{
634-
postEvent<&deleteFramebuffers>(n, qMakePair(framebuffers, n));
643+
postEvent<&deleteFramebuffers>(qMakePair(framebuffers, n));
635644
}
636645

637646
QWEBGL_FUNCTION_POSTEVENT(deleteProgram, glDeleteProgram,
@@ -640,7 +649,7 @@ QWEBGL_FUNCTION_POSTEVENT(deleteProgram, glDeleteProgram,
640649
QWEBGL_FUNCTION(deleteRenderbuffers, void, glDeleteRenderbuffers,
641650
(GLsizei) n, (const GLuint *) renderbuffers)
642651
{
643-
postEvent<&deleteRenderbuffers>(n, qMakePair(renderbuffers, n));
652+
postEvent<&deleteRenderbuffers>(qMakePair(renderbuffers, n));
644653
}
645654

646655
QWEBGL_FUNCTION_POSTEVENT(deleteShader, glDeleteShader,
@@ -649,7 +658,7 @@ QWEBGL_FUNCTION_POSTEVENT(deleteShader, glDeleteShader,
649658
QWEBGL_FUNCTION(deleteTextures, void, glDeleteTextures,
650659
(GLsizei) n, (const GLuint *) textures)
651660
{
652-
postEvent<&deleteTextures>(n, qMakePair(textures, n));
661+
postEvent<&deleteTextures>(qMakePair(textures, n));
653662
}
654663

655664
QWEBGL_FUNCTION_POSTEVENT(depthFunc, glDepthFunc,
@@ -963,7 +972,7 @@ QWEBGL_FUNCTION(getFramebufferAttachmentParameteriv, void, glGetFramebufferAttac
963972
QWEBGL_FUNCTION(getProgramInfoLog, void, glGetProgramInfoLog,
964973
(GLuint) program, (GLsizei) bufSize, (GLsizei *) length, (GLchar *) infoLog)
965974
{
966-
auto value = postEventAndQuery<&getProgramInfoLog>(QString(), program, bufSize);
975+
auto value = postEventAndQuery<&getProgramInfoLog>(QString(), program);
967976
*length = value.length();
968977
if (bufSize >= value.length())
969978
std::memcpy(infoLog, value.constData(), value.length());
@@ -984,7 +993,7 @@ QWEBGL_FUNCTION(getRenderbufferParameteriv, void, glGetRenderbufferParameteriv,
984993
QWEBGL_FUNCTION(getShaderInfoLog, void, glGetShaderInfoLog,
985994
(GLuint) shader, (GLsizei) bufSize, (GLsizei *) length, (GLchar *) infoLog)
986995
{
987-
const auto value = postEventAndQuery<&getShaderInfoLog>(QString(), shader, bufSize);
996+
const auto value = postEventAndQuery<&getShaderInfoLog>(QString(), shader);
988997
*length = value.length();
989998
if (bufSize >= value.length())
990999
std::memcpy(infoLog, value.constData(), value.length());
@@ -1010,7 +1019,7 @@ QWEBGL_FUNCTION(getShaderPrecisionFormat, void, glGetShaderPrecisionFormat,
10101019
QWEBGL_FUNCTION(getShaderSource, void, glGetShaderSource,
10111020
(GLuint) shader, (GLsizei) bufSize, (GLsizei *) length, (GLchar *) source)
10121021
{
1013-
const auto value = postEventAndQuery<&getShaderSource>(QString(), shader, bufSize);
1022+
const auto value = postEventAndQuery<&getShaderSource>(QString(), shader);
10141023
*length = value.length();
10151024
if (bufSize >= value.length())
10161025
std::memcpy(source, value.constData(), value.length());
@@ -1184,17 +1193,15 @@ QWEBGL_FUNCTION(shaderSource, void, glShaderSource,
11841193
(GLuint) shader, (GLsizei) count,
11851194
(const GLchar *const *) string, (const GLint *) length)
11861195
{
1187-
auto event = currentContext()->createEvent(QStringLiteral("glShaderSource"));
1188-
if (!event)
1189-
return;
1190-
event->addParameters(shader, count);
1191-
for (int i = 0; i < count; ++i) {
1192-
if (!length)
1193-
event->addString(QString::fromLatin1(string[i]));
1194-
else
1195-
event->addString(QString::fromLatin1(string[i], length[i]));
1196-
}
1197-
QCoreApplication::postEvent(QWebGLIntegrationPrivate::instance()->webSocketServer, event);
1196+
QString fullString;
1197+
std::function<void(int)> concat;
1198+
if (length)
1199+
concat = [&](int i) { fullString.append(QString::fromLatin1(string[i], length[i])); };
1200+
else
1201+
concat = [&](int i) { fullString.append(QString::fromLatin1(string[i])); };
1202+
for (int i = 0; i < count; ++i)
1203+
concat(i);
1204+
postEvent<&shaderSource>(shader, fullString);
11981205
}
11991206

12001207
QWEBGL_FUNCTION_POSTEVENT(stencilFunc, glStencilFunc,
@@ -1273,7 +1280,7 @@ QWEBGL_FUNCTION_POSTEVENT(uniform1f, glUniform1f, (GLint) location, (GLfloat) v0
12731280
QWEBGL_FUNCTION(uniform1fv, void, glUniform1fv,
12741281
(GLint) location, (GLsizei) count, (const GLfloat *) value)
12751282
{
1276-
postEvent<&uniform1fv>(location, count, qMakePair(value, count));
1283+
postEvent<&uniform1fv>(location, qMakePair(value, count));
12771284
}
12781285

12791286
QWEBGL_FUNCTION_POSTEVENT(uniform1i, glUniform1i,
@@ -1282,7 +1289,7 @@ QWEBGL_FUNCTION_POSTEVENT(uniform1i, glUniform1i,
12821289
QWEBGL_FUNCTION(uniform1iv, void, glUniform1iv,
12831290
(GLint) location, (GLsizei) count, (const GLint *) value)
12841291
{
1285-
postEvent<&uniform1iv>(location, count, qMakePair(value, count));
1292+
postEvent<&uniform1iv>(location, qMakePair(value, count));
12861293
}
12871294

12881295
QWEBGL_FUNCTION_POSTEVENT(uniform2f, glUniform2f,
@@ -1291,7 +1298,7 @@ QWEBGL_FUNCTION_POSTEVENT(uniform2f, glUniform2f,
12911298
QWEBGL_FUNCTION(uniform2fv, void, glUniform2fv,
12921299
(GLint) location, (GLsizei) count, (const GLfloat *) value)
12931300
{
1294-
postEvent<&uniform2fv>(location, count, qMakePair(value, count * 2));
1301+
postEvent<&uniform2fv>(location, qMakePair(value, count * 2));
12951302
}
12961303

12971304
QWEBGL_FUNCTION_POSTEVENT(uniform2i, glUniform2i,
@@ -1300,17 +1307,16 @@ QWEBGL_FUNCTION_POSTEVENT(uniform2i, glUniform2i,
13001307
QWEBGL_FUNCTION(uniform2iv, void, glUniform2iv,
13011308
(GLint) location, (GLsizei) count, (const GLint *) value)
13021309
{
1303-
postEvent<&uniform2iv>(location, count, qMakePair(value, count * 2));
1310+
postEvent<&uniform2iv>(location, qMakePair(value, count * 2));
13041311
}
13051312

13061313
QWEBGL_FUNCTION_POSTEVENT(uniform3f, glUniform3f,
13071314
(GLint) location, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2)
13081315

1309-
13101316
QWEBGL_FUNCTION(uniform3fv, void, glUniform3fv,
13111317
(GLint) location, (GLsizei) count, (const GLfloat *) value)
13121318
{
1313-
postEvent<&uniform3fv>(location, count, qMakePair(value, count * 3));
1319+
postEvent<&uniform3fv>(location, qMakePair(value, count * 3));
13141320
}
13151321

13161322
QWEBGL_FUNCTION_POSTEVENT(uniform3i, glUniform3i,
@@ -1319,7 +1325,7 @@ QWEBGL_FUNCTION_POSTEVENT(uniform3i, glUniform3i,
13191325
QWEBGL_FUNCTION(uniform3iv, void, glUniform3iv,
13201326
(GLint) location, (GLsizei) count, (const GLint *) value)
13211327
{
1322-
postEvent<&uniform3iv>(location, count, qMakePair(value, count * 3));
1328+
postEvent<&uniform3iv>(location, qMakePair(value, count * 3));
13231329
}
13241330

13251331
QWEBGL_FUNCTION_POSTEVENT(uniform4f, glUniform4f,
@@ -1329,7 +1335,7 @@ QWEBGL_FUNCTION_POSTEVENT(uniform4f, glUniform4f,
13291335
QWEBGL_FUNCTION(uniform4fv, void, glUniform4fv,
13301336
(GLint) location, (GLsizei) count, (const GLfloat *) value)
13311337
{
1332-
postEvent<&uniform4fv>(location, count, qMakePair(value, count * 4));
1338+
postEvent<&uniform4fv>(location, qMakePair(value, count * 4));
13331339
}
13341340

13351341
QWEBGL_FUNCTION_POSTEVENT(uniform4i, glUniform4i,
@@ -1338,25 +1344,25 @@ QWEBGL_FUNCTION_POSTEVENT(uniform4i, glUniform4i,
13381344
QWEBGL_FUNCTION(uniform4iv, void, glUniform4iv,
13391345
(GLint) location, (GLsizei) count, (const GLint *) value)
13401346
{
1341-
postEvent<&uniform4iv>(location, count, qMakePair(value, count * 4));
1347+
postEvent<&uniform4iv>(location, qMakePair(value, count * 4));
13421348
}
13431349

13441350
QWEBGL_FUNCTION(uniformMatrix2fv, void, glUniformMatrix2fv,
13451351
(GLint) location, (GLsizei) count, (GLboolean) transpose, (const GLfloat *) value)
13461352
{
1347-
postEvent<&uniformMatrix2fv>(location, count, transpose, qMakePair(value, count * 4));
1353+
postEvent<&uniformMatrix2fv>(location, transpose, qMakePair(value, count * 4));
13481354
}
13491355

13501356
QWEBGL_FUNCTION(uniformMatrix3fv, void, glUniformMatrix3fv,
13511357
(GLint) location, (GLsizei) count, (GLboolean) transpose, (const GLfloat *) value)
13521358
{
1353-
postEvent<&uniformMatrix3fv>(location, count, transpose, qMakePair(value, count * 9));
1359+
postEvent<&uniformMatrix3fv>(location, transpose, qMakePair(value, count * 9));
13541360
}
13551361

13561362
QWEBGL_FUNCTION(uniformMatrix4fv, void, glUniformMatrix4fv,
13571363
(GLint) location, (GLsizei) count, (GLboolean) transpose, (const GLfloat *) value)
13581364
{
1359-
postEvent<&uniformMatrix4fv>(location, count, transpose, qMakePair(value, count * 16));
1365+
postEvent<&uniformMatrix4fv>(location, transpose, qMakePair(value, count * 16));
13601366
}
13611367

13621368
QWEBGL_FUNCTION_POSTEVENT(useProgram, glUseProgram,

src/plugins/platforms/webgl/qwebglfunctioncall.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ void QWebGLFunctionCall::addData(const QByteArray &data)
136136
d->parameters.append(data);
137137
}
138138

139+
void QWebGLFunctionCall::addList(const QVariantList &list)
140+
{
141+
Q_D(QWebGLFunctionCall);
142+
d->parameters.append(QVariant::fromValue(list));
143+
}
144+
139145
void QWebGLFunctionCall::addNull()
140146
{
141147
Q_D(QWebGLFunctionCall);

src/plugins/platforms/webgl/qwebglfunctioncall.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class QWebGLFunctionCall : public QEvent
6464
void addUInt(uint value);
6565
void addFloat(float value);
6666
void addData(const QByteArray &data);
67+
void addList(const QVariantList &list);
6768
void addNull();
6869

6970
void add(const QString &value) { addString(value); }
@@ -72,6 +73,7 @@ class QWebGLFunctionCall : public QEvent
7273
void add(uint value) { addUInt(value); }
7374
void add(float value) { addFloat(value); }
7475
void add(const QByteArray &data) { addData(data); }
76+
void add(const QVariantList &list) { addList(list); }
7577
void add(std::nullptr_t) { addNull(); }
7678

7779
template<class...Ts>

src/plugins/platforms/webgl/qwebglwebsocketserver.cpp

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -181,39 +181,48 @@ void QWebGLWebSocketServer::sendMessage(QWebSocket *socket,
181181
stream << quint32(values["id"].toUInt(&ok));
182182
Q_ASSERT(ok);
183183
}
184-
stream << parameterCount;
185-
for (const auto &value : qAsConst(parameters)) {
186-
if (value.isNull()) {
187-
stream << (quint8)'n';
188-
} else switch (value.type()) {
189-
case QVariant::Int:
190-
stream << (quint8)'i' << value.toInt();
191-
break;
192-
case QVariant::UInt:
193-
stream << (quint8)'u' << value.toUInt();
194-
break;
195-
case QVariant::Bool:
196-
stream << (quint8)'b' << (quint8)value.toBool();
197-
break;
198-
case QVariant::Double:
199-
stream << (quint8)'d' << value.toDouble();
200-
break;
201-
case QVariant::String:
202-
stream << (quint8)'s' << value.toString().toUtf8();
203-
break;
204-
case QVariant::ByteArray: {
205-
const auto byteArray = value.toByteArray();
206-
if (byteArray.isNull())
184+
const std::function<void(const QVariantList &)> serialize = [&stream, &serialize](
185+
const QVariantList &parameters) {
186+
for (const auto &value : parameters) {
187+
if (value.isNull()) {
207188
stream << (quint8)'n';
208-
else
209-
stream << (quint8)'x' << byteArray;
210-
break;
189+
} else switch (value.type()) {
190+
case QVariant::Int:
191+
stream << (quint8)'i' << value.toInt();
192+
break;
193+
case QVariant::UInt:
194+
stream << (quint8)'u' << value.toUInt();
195+
break;
196+
case QVariant::Bool:
197+
stream << (quint8)'b' << (quint8)value.toBool();
198+
break;
199+
case QVariant::Double:
200+
stream << (quint8)'d' << value.toDouble();
201+
break;
202+
case QVariant::String:
203+
stream << (quint8)'s' << value.toString().toUtf8();
204+
break;
205+
case QVariant::ByteArray: {
206+
const auto byteArray = value.toByteArray();
207+
if (byteArray.isNull())
208+
stream << (quint8)'n';
209+
else
210+
stream << (quint8)'x' << byteArray;
211+
break;
212+
}
213+
case QVariant::List: {
214+
const auto list = value.toList();
215+
stream << quint8('a') << quint8(list.size());
216+
serialize(list);
217+
break;
218+
}
219+
default:
220+
qCCritical(lc, "Unsupported type: %d", value.type());
221+
break;
222+
}
211223
}
212-
default:
213-
qCCritical(lc, "Unsupported type: %d", value.type());
214-
break;
215-
}
216-
}
224+
};
225+
serialize(parameters);
217226
stream << (quint32)0xbaadf00d;
218227
}
219228
const quint32 totalMessageSize = data.size();

0 commit comments

Comments
 (0)