/usr/share/qt5/doc/qtquick/qtquick-rendercontrol-window-multithreaded-cpp.html is in qtdeclarative5-doc-html 5.5.1-2ubuntu6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>window_multithreaded.cpp Example File | Qt Quick 5.5</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.5</li>
<li><a href="qtquick-index.html">Qt Quick</a></li>
<li><a href="qtquick-rendercontrol-example.html">QQuickRenderControl Example</a></li>
<li>window_multithreaded.cpp Example File</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">window_multithreaded.cpp Example File</h1>
<span class="subtitle">rendercontrol/window_multithreaded.cpp</span>
<!-- $$$rendercontrol/window_multithreaded.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp"><span class="comment">/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/</span>
<span class="preprocessor">#include "window_multithreaded.h"</span>
<span class="preprocessor">#include "cuberenderer.h"</span>
<span class="preprocessor">#include <QOpenGLContext></span>
<span class="preprocessor">#include <QOpenGLFunctions></span>
<span class="preprocessor">#include <QOpenGLFramebufferObject></span>
<span class="preprocessor">#include <QOpenGLShaderProgram></span>
<span class="preprocessor">#include <QOpenGLVertexArrayObject></span>
<span class="preprocessor">#include <QOpenGLBuffer></span>
<span class="preprocessor">#include <QOpenGLVertexArrayObject></span>
<span class="preprocessor">#include <QOffscreenSurface></span>
<span class="preprocessor">#include <QQmlEngine></span>
<span class="preprocessor">#include <QQmlComponent></span>
<span class="preprocessor">#include <QQuickItem></span>
<span class="preprocessor">#include <QQuickWindow></span>
<span class="preprocessor">#include <QQuickRenderControl></span>
<span class="preprocessor">#include <QCoreApplication></span>
<span class="comment">/*
This implementation runs the Qt Quick scenegraph's sync and render phases on a
separate, dedicated thread. Rendering the cube using our custom OpenGL engine
happens on that thread as well. This is similar to the built-in threaded
render loop, but does not support all the features. There is no support for
getting Animators running on the render thread for example.
We choose to use QObject's event mechanism to communicate with the QObject
living on the render thread. An alternative would be to subclass QThread and
reimplement run() with a custom event handling approach, like
QSGThreadedRenderLoop does. That would potentially lead to better results but
is also more complex.
*/</span>
<span class="keyword">static</span> <span class="keyword">const</span> <span class="type">QEvent</span><span class="operator">::</span>Type INIT <span class="operator">=</span> <span class="type">QEvent</span><span class="operator">::</span>Type(<span class="type">QEvent</span><span class="operator">::</span>User <span class="operator">+</span> <span class="number">1</span>);
<span class="keyword">static</span> <span class="keyword">const</span> <span class="type">QEvent</span><span class="operator">::</span>Type RENDER <span class="operator">=</span> <span class="type">QEvent</span><span class="operator">::</span>Type(<span class="type">QEvent</span><span class="operator">::</span>User <span class="operator">+</span> <span class="number">2</span>);
<span class="keyword">static</span> <span class="keyword">const</span> <span class="type">QEvent</span><span class="operator">::</span>Type RESIZE <span class="operator">=</span> <span class="type">QEvent</span><span class="operator">::</span>Type(<span class="type">QEvent</span><span class="operator">::</span>User <span class="operator">+</span> <span class="number">3</span>);
<span class="keyword">static</span> <span class="keyword">const</span> <span class="type">QEvent</span><span class="operator">::</span>Type STOP <span class="operator">=</span> <span class="type">QEvent</span><span class="operator">::</span>Type(<span class="type">QEvent</span><span class="operator">::</span>User <span class="operator">+</span> <span class="number">4</span>);
<span class="keyword">static</span> <span class="keyword">const</span> <span class="type">QEvent</span><span class="operator">::</span>Type UPDATE <span class="operator">=</span> <span class="type">QEvent</span><span class="operator">::</span>Type(<span class="type">QEvent</span><span class="operator">::</span>User <span class="operator">+</span> <span class="number">5</span>);
QuickRenderer<span class="operator">::</span>QuickRenderer()
: m_context(<span class="number">0</span>)<span class="operator">,</span>
m_surface(<span class="number">0</span>)<span class="operator">,</span>
m_fbo(<span class="number">0</span>)<span class="operator">,</span>
m_window(<span class="number">0</span>)<span class="operator">,</span>
m_quickWindow(<span class="number">0</span>)<span class="operator">,</span>
m_renderControl(<span class="number">0</span>)<span class="operator">,</span>
m_cubeRenderer(<span class="number">0</span>)<span class="operator">,</span>
m_quit(<span class="keyword">false</span>)
{
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>requestInit()
{
<span class="type">QCoreApplication</span><span class="operator">::</span>postEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type">QEvent</span>(INIT));
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>requestRender()
{
<span class="type">QCoreApplication</span><span class="operator">::</span>postEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type">QEvent</span>(RENDER));
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>requestResize()
{
<span class="type">QCoreApplication</span><span class="operator">::</span>postEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type">QEvent</span>(RESIZE));
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>requestStop()
{
<span class="type">QCoreApplication</span><span class="operator">::</span>postEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type">QEvent</span>(STOP));
}
bool QuickRenderer<span class="operator">::</span>event(<span class="type">QEvent</span> <span class="operator">*</span>e)
{
<span class="type">QMutexLocker</span> lock(<span class="operator">&</span>m_mutex);
<span class="keyword">switch</span> (<span class="type">int</span>(e<span class="operator">-</span><span class="operator">></span>type())) {
<span class="keyword">case</span> INIT:
init();
<span class="keyword">return</span> <span class="keyword">true</span>;
<span class="keyword">case</span> RENDER:
render(<span class="operator">&</span>lock);
<span class="keyword">return</span> <span class="keyword">true</span>;
<span class="keyword">case</span> RESIZE:
<span class="keyword">if</span> (m_cubeRenderer)
m_cubeRenderer<span class="operator">-</span><span class="operator">></span>resize(m_window<span class="operator">-</span><span class="operator">></span>width()<span class="operator">,</span> m_window<span class="operator">-</span><span class="operator">></span>height());
<span class="keyword">return</span> <span class="keyword">true</span>;
<span class="keyword">case</span> STOP:
cleanup();
<span class="keyword">return</span> <span class="keyword">true</span>;
<span class="keyword">default</span>:
<span class="keyword">return</span> <span class="type">QObject</span><span class="operator">::</span>event(e);
}
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>init()
{
m_context<span class="operator">-</span><span class="operator">></span>makeCurrent(m_surface);
<span class="comment">// Pass our offscreen surface to the cube renderer just so that it will</span>
<span class="comment">// have something is can make current during cleanup. QOffscreenSurface,</span>
<span class="comment">// just like QWindow, must always be created on the gui thread (as it might</span>
<span class="comment">// be backed by an actual QWindow).</span>
m_cubeRenderer <span class="operator">=</span> <span class="keyword">new</span> CubeRenderer(m_surface);
m_cubeRenderer<span class="operator">-</span><span class="operator">></span>resize(m_window<span class="operator">-</span><span class="operator">></span>width()<span class="operator">,</span> m_window<span class="operator">-</span><span class="operator">></span>height());
m_renderControl<span class="operator">-</span><span class="operator">></span>initialize(m_context);
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>cleanup()
{
m_context<span class="operator">-</span><span class="operator">></span>makeCurrent(m_surface);
m_renderControl<span class="operator">-</span><span class="operator">></span>invalidate();
<span class="keyword">delete</span> m_fbo;
m_fbo <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">delete</span> m_cubeRenderer;
m_cubeRenderer <span class="operator">=</span> <span class="number">0</span>;
m_context<span class="operator">-</span><span class="operator">></span>doneCurrent();
m_context<span class="operator">-</span><span class="operator">></span>moveToThread(<span class="type">QCoreApplication</span><span class="operator">::</span>instance()<span class="operator">-</span><span class="operator">></span>thread());
m_cond<span class="operator">.</span>wakeOne();
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>ensureFbo()
{
<span class="keyword">if</span> (m_fbo <span class="operator">&</span><span class="operator">&</span> m_fbo<span class="operator">-</span><span class="operator">></span>size() <span class="operator">!</span><span class="operator">=</span> m_window<span class="operator">-</span><span class="operator">></span>size() <span class="operator">*</span> m_window<span class="operator">-</span><span class="operator">></span>devicePixelRatio()) {
<span class="keyword">delete</span> m_fbo;
m_fbo <span class="operator">=</span> <span class="number">0</span>;
}
<span class="keyword">if</span> (<span class="operator">!</span>m_fbo) {
m_fbo <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QOpenGLFramebufferObject</span>(m_window<span class="operator">-</span><span class="operator">></span>size() <span class="operator">*</span> m_window<span class="operator">-</span><span class="operator">></span>devicePixelRatio()<span class="operator">,</span>
<span class="type">QOpenGLFramebufferObject</span><span class="operator">::</span>CombinedDepthStencil);
m_quickWindow<span class="operator">-</span><span class="operator">></span>setRenderTarget(m_fbo);
}
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>render(<span class="type">QMutexLocker</span> <span class="operator">*</span>lock)
{
Q_ASSERT(<span class="type">QThread</span><span class="operator">::</span>currentThread() <span class="operator">!</span><span class="operator">=</span> m_window<span class="operator">-</span><span class="operator">></span>thread());
<span class="keyword">if</span> (<span class="operator">!</span>m_context<span class="operator">-</span><span class="operator">></span>makeCurrent(m_surface)) {
qWarning(<span class="string">"Failed to make context current on render thread"</span>);
<span class="keyword">return</span>;
}
ensureFbo();
<span class="comment">// Synchronization and rendering happens here on the render thread.</span>
m_renderControl<span class="operator">-</span><span class="operator">></span>sync();
<span class="comment">// The gui thread can now continue.</span>
m_cond<span class="operator">.</span>wakeOne();
lock<span class="operator">-</span><span class="operator">></span>unlock();
<span class="comment">// Meanwhile on this thread continue with the actual rendering (into the FBO first).</span>
m_renderControl<span class="operator">-</span><span class="operator">></span>render();
m_context<span class="operator">-</span><span class="operator">></span>functions()<span class="operator">-</span><span class="operator">></span>glFlush();
<span class="comment">// The cube renderer uses its own context, no need to bother with the state here.</span>
<span class="comment">// Get something onto the screen using our custom OpenGL engine.</span>
<span class="type">QMutexLocker</span> quitLock(<span class="operator">&</span>m_quitMutex);
<span class="keyword">if</span> (<span class="operator">!</span>m_quit)
m_cubeRenderer<span class="operator">-</span><span class="operator">></span>render(m_window<span class="operator">,</span> m_context<span class="operator">,</span> m_fbo<span class="operator">-</span><span class="operator">></span>texture());
}
<span class="type">void</span> QuickRenderer<span class="operator">::</span>aboutToQuit()
{
<span class="type">QMutexLocker</span> lock(<span class="operator">&</span>m_quitMutex);
m_quit <span class="operator">=</span> <span class="keyword">true</span>;
}
<span class="keyword">class</span> RenderControl : <span class="keyword">public</span> <span class="type"><a href="qquickrendercontrol.html">QQuickRenderControl</a></span>
{
<span class="keyword">public</span>:
RenderControl(<span class="type">QWindow</span> <span class="operator">*</span>w) : m_window(w) { }
<span class="type">QWindow</span> <span class="operator">*</span>renderWindow(<span class="type">QPoint</span> <span class="operator">*</span>offset) Q_DECL_OVERRIDE;
<span class="keyword">private</span>:
<span class="type">QWindow</span> <span class="operator">*</span>m_window;
};
WindowMultiThreaded<span class="operator">::</span>WindowMultiThreaded()
: m_qmlComponent(Q_NULLPTR)<span class="operator">,</span>
m_rootItem(<span class="number">0</span>)<span class="operator">,</span>
m_quickInitialized(<span class="keyword">false</span>)<span class="operator">,</span>
m_psrRequested(<span class="keyword">false</span>)
{
setSurfaceType(<span class="type">QSurface</span><span class="operator">::</span>OpenGLSurface);
<span class="type">QSurfaceFormat</span> format;
<span class="comment">// Qt Quick may need a depth and stencil buffer. Always make sure these are available.</span>
format<span class="operator">.</span>setDepthBufferSize(<span class="number">16</span>);
format<span class="operator">.</span>setStencilBufferSize(<span class="number">8</span>);
setFormat(format);
m_context <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QOpenGLContext</span>;
m_context<span class="operator">-</span><span class="operator">></span>setFormat(format);
m_context<span class="operator">-</span><span class="operator">></span>create();
m_offscreenSurface <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QOffscreenSurface</span>;
<span class="comment">// Pass m_context->format(), not format. Format does not specify and color buffer</span>
<span class="comment">// sizes, while the context, that has just been created, reports a format that has</span>
<span class="comment">// these values filled in. Pass this to the offscreen surface to make sure it will be</span>
<span class="comment">// compatible with the context's configuration.</span>
m_offscreenSurface<span class="operator">-</span><span class="operator">></span>setFormat(m_context<span class="operator">-</span><span class="operator">></span>format());
m_offscreenSurface<span class="operator">-</span><span class="operator">></span>create();
m_renderControl <span class="operator">=</span> <span class="keyword">new</span> RenderControl(<span class="keyword">this</span>);
<span class="comment">// Create a QQuickWindow that is associated with out render control. Note that this</span>
<span class="comment">// window never gets created or shown, meaning that it will never get an underlying</span>
<span class="comment">// native (platform) window.</span>
m_quickWindow <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qquickwindow.html">QQuickWindow</a></span>(m_renderControl);
<span class="comment">// Create a QML engine.</span>
m_qmlEngine <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QQmlEngine</span>;
<span class="keyword">if</span> (<span class="operator">!</span>m_qmlEngine<span class="operator">-</span><span class="operator">></span>incubationController())
m_qmlEngine<span class="operator">-</span><span class="operator">></span>setIncubationController(m_quickWindow<span class="operator">-</span><span class="operator">></span>incubationController());
m_quickRenderer <span class="operator">=</span> <span class="keyword">new</span> QuickRenderer;
m_quickRenderer<span class="operator">-</span><span class="operator">></span>setContext(m_context);
<span class="comment">// These live on the gui thread. Just give access to them on the render thread.</span>
m_quickRenderer<span class="operator">-</span><span class="operator">></span>setSurface(m_offscreenSurface);
m_quickRenderer<span class="operator">-</span><span class="operator">></span>setWindow(<span class="keyword">this</span>);
m_quickRenderer<span class="operator">-</span><span class="operator">></span>setQuickWindow(m_quickWindow);
m_quickRenderer<span class="operator">-</span><span class="operator">></span>setRenderControl(m_renderControl);
m_quickRendererThread <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QThread</span>;
<span class="comment">// Notify the render control that some scenegraph internals have to live on</span>
<span class="comment">// m_quickRenderThread.</span>
m_renderControl<span class="operator">-</span><span class="operator">></span>prepareThread(m_quickRendererThread);
<span class="comment">// The QOpenGLContext and the QObject representing the rendering logic on</span>
<span class="comment">// the render thread must live on that thread.</span>
m_context<span class="operator">-</span><span class="operator">></span>moveToThread(m_quickRendererThread);
m_quickRenderer<span class="operator">-</span><span class="operator">></span>moveToThread(m_quickRendererThread);
m_quickRendererThread<span class="operator">-</span><span class="operator">></span>start();
<span class="comment">// Now hook up the signals. For simplicy we don't differentiate</span>
<span class="comment">// between renderRequested (only render is needed, no sync) and</span>
<span class="comment">// sceneChanged (polish and sync is needed too).</span>
connect(m_renderControl<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qquickrendercontrol.html">QQuickRenderControl</a></span><span class="operator">::</span>renderRequested<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WindowMultiThreaded<span class="operator">::</span>requestUpdate);
connect(m_renderControl<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qquickrendercontrol.html">QQuickRenderControl</a></span><span class="operator">::</span>sceneChanged<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WindowMultiThreaded<span class="operator">::</span>requestUpdate);
}
WindowMultiThreaded<span class="operator">::</span><span class="operator">~</span>WindowMultiThreaded()
{
<span class="comment">// Release resources and move the context ownership back to this thread.</span>
m_quickRenderer<span class="operator">-</span><span class="operator">></span>mutex()<span class="operator">-</span><span class="operator">></span>lock();
m_quickRenderer<span class="operator">-</span><span class="operator">></span>requestStop();
m_quickRenderer<span class="operator">-</span><span class="operator">></span><span class="type">cond</span>()<span class="operator">-</span><span class="operator">></span>wait(m_quickRenderer<span class="operator">-</span><span class="operator">></span>mutex());
m_quickRenderer<span class="operator">-</span><span class="operator">></span>mutex()<span class="operator">-</span><span class="operator">></span>unlock();
m_quickRendererThread<span class="operator">-</span><span class="operator">></span>quit();
m_quickRendererThread<span class="operator">-</span><span class="operator">></span>wait();
<span class="keyword">delete</span> m_renderControl;
<span class="keyword">delete</span> m_qmlComponent;
<span class="keyword">delete</span> m_quickWindow;
<span class="keyword">delete</span> m_qmlEngine;
<span class="keyword">delete</span> m_offscreenSurface;
<span class="keyword">delete</span> m_context;
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>requestUpdate()
{
<span class="keyword">if</span> (m_quickInitialized <span class="operator">&</span><span class="operator">&</span> <span class="operator">!</span>m_psrRequested) {
m_psrRequested <span class="operator">=</span> <span class="keyword">true</span>;
<span class="type">QCoreApplication</span><span class="operator">::</span>postEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type">QEvent</span>(UPDATE));
}
}
bool WindowMultiThreaded<span class="operator">::</span>event(<span class="type">QEvent</span> <span class="operator">*</span>e)
{
<span class="keyword">if</span> (e<span class="operator">-</span><span class="operator">></span>type() <span class="operator">=</span><span class="operator">=</span> UPDATE) {
polishSyncAndRender();
m_psrRequested <span class="operator">=</span> <span class="keyword">false</span>;
<span class="keyword">return</span> <span class="keyword">true</span>;
} <span class="keyword">else</span> <span class="keyword">if</span> (e<span class="operator">-</span><span class="operator">></span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type">QEvent</span><span class="operator">::</span>Close) {
<span class="comment">// Avoid rendering on the render thread when the window is about to</span>
<span class="comment">// close. Once a QWindow is closed, the underlying platform window will</span>
<span class="comment">// go away, even though the QWindow instance itself is still</span>
<span class="comment">// valid. Operations like swapBuffers() are futile and only result in</span>
<span class="comment">// warnings afterwards. Prevent this.</span>
m_quickRenderer<span class="operator">-</span><span class="operator">></span>aboutToQuit();
}
<span class="keyword">return</span> <span class="type">QWindow</span><span class="operator">::</span>event(e);
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>polishSyncAndRender()
{
Q_ASSERT(<span class="type">QThread</span><span class="operator">::</span>currentThread() <span class="operator">=</span><span class="operator">=</span> thread());
<span class="comment">// Polishing happens on the gui thread.</span>
m_renderControl<span class="operator">-</span><span class="operator">></span>polishItems();
<span class="comment">// Sync happens on the render thread with the gui thread (this one) blocked.</span>
<span class="type">QMutexLocker</span> lock(m_quickRenderer<span class="operator">-</span><span class="operator">></span>mutex());
m_quickRenderer<span class="operator">-</span><span class="operator">></span>requestRender();
<span class="comment">// Wait until sync is complete.</span>
m_quickRenderer<span class="operator">-</span><span class="operator">></span><span class="type">cond</span>()<span class="operator">-</span><span class="operator">></span>wait(m_quickRenderer<span class="operator">-</span><span class="operator">></span>mutex());
<span class="comment">// Rendering happens on the render thread without blocking the gui (main)</span>
<span class="comment">// thread. This is good because the blocking swap (waiting for vsync)</span>
<span class="comment">// happens on the render thread, not blocking other work.</span>
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>run()
{
disconnect(m_qmlComponent<span class="operator">,</span> SIGNAL(statusChanged(<span class="type">QQmlComponent</span><span class="operator">::</span>Status))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(run()));
<span class="keyword">if</span> (m_qmlComponent<span class="operator">-</span><span class="operator">></span>isError()) {
<span class="type">QList</span><span class="operator"><</span><span class="type">QQmlError</span><span class="operator">></span> errorList <span class="operator">=</span> m_qmlComponent<span class="operator">-</span><span class="operator">></span>errors();
foreach (<span class="keyword">const</span> <span class="type">QQmlError</span> <span class="operator">&</span>error<span class="operator">,</span> errorList)
qWarning() <span class="operator"><</span><span class="operator"><</span> error<span class="operator">.</span>url() <span class="operator"><</span><span class="operator"><</span> error<span class="operator">.</span>line() <span class="operator"><</span><span class="operator"><</span> error;
<span class="keyword">return</span>;
}
<span class="type">QObject</span> <span class="operator">*</span>rootObject <span class="operator">=</span> m_qmlComponent<span class="operator">-</span><span class="operator">></span>create();
<span class="keyword">if</span> (m_qmlComponent<span class="operator">-</span><span class="operator">></span>isError()) {
<span class="type">QList</span><span class="operator"><</span><span class="type">QQmlError</span><span class="operator">></span> errorList <span class="operator">=</span> m_qmlComponent<span class="operator">-</span><span class="operator">></span>errors();
foreach (<span class="keyword">const</span> <span class="type">QQmlError</span> <span class="operator">&</span>error<span class="operator">,</span> errorList)
qWarning() <span class="operator"><</span><span class="operator"><</span> error<span class="operator">.</span>url() <span class="operator"><</span><span class="operator"><</span> error<span class="operator">.</span>line() <span class="operator"><</span><span class="operator"><</span> error;
<span class="keyword">return</span>;
}
m_rootItem <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qquickitem.html">QQuickItem</a></span> <span class="operator">*</span><span class="operator">></span>(rootObject);
<span class="keyword">if</span> (<span class="operator">!</span>m_rootItem) {
qWarning(<span class="string">"run: Not a QQuickItem"</span>);
<span class="keyword">delete</span> rootObject;
<span class="keyword">return</span>;
}
<span class="comment">// The root item is ready. Associate it with the window.</span>
m_rootItem<span class="operator">-</span><span class="operator">></span>setParentItem(m_quickWindow<span class="operator">-</span><span class="operator">></span>contentItem());
<span class="comment">// Update item and rendering related geometries.</span>
updateSizes();
m_quickInitialized <span class="operator">=</span> <span class="keyword">true</span>;
<span class="comment">// Initialize the render thread and perform the first polish/sync/render.</span>
m_quickRenderer<span class="operator">-</span><span class="operator">></span>requestInit();
polishSyncAndRender();
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>updateSizes()
{
<span class="comment">// Behave like SizeRootObjectToView.</span>
m_rootItem<span class="operator">-</span><span class="operator">></span>setWidth(width());
m_rootItem<span class="operator">-</span><span class="operator">></span>setHeight(height());
m_quickWindow<span class="operator">-</span><span class="operator">></span>setGeometry(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> width()<span class="operator">,</span> height());
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>startQuick(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>filename)
{
m_qmlComponent <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QQmlComponent</span>(m_qmlEngine<span class="operator">,</span> <span class="type">QUrl</span>(filename));
<span class="keyword">if</span> (m_qmlComponent<span class="operator">-</span><span class="operator">></span>isLoading())
connect(m_qmlComponent<span class="operator">,</span> <span class="operator">&</span><span class="type">QQmlComponent</span><span class="operator">::</span>statusChanged<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WindowMultiThreaded<span class="operator">::</span>run);
<span class="keyword">else</span>
run();
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>exposeEvent(<span class="type">QExposeEvent</span> <span class="operator">*</span>)
{
<span class="keyword">if</span> (isExposed()) {
<span class="keyword">if</span> (<span class="operator">!</span>m_quickInitialized)
startQuick(<span class="type">QStringLiteral</span>(<span class="string">"qrc:/rendercontrol/demo.qml"</span>));
}
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>resizeEvent(<span class="type">QResizeEvent</span> <span class="operator">*</span>)
{
<span class="comment">// If this is a resize after the scene is up and running, recreate the fbo and the</span>
<span class="comment">// Quick item and scene.</span>
<span class="keyword">if</span> (m_rootItem) {
updateSizes();
m_quickRenderer<span class="operator">-</span><span class="operator">></span>requestResize();
polishSyncAndRender();
}
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>mousePressEvent(<span class="type">QMouseEvent</span> <span class="operator">*</span>e)
{
<span class="comment">// Use the constructor taking localPos and screenPos. That puts localPos into the</span>
<span class="comment">// event's localPos and windowPos, and screenPos into the event's screenPos. This way</span>
<span class="comment">// the windowPos in e is ignored and is replaced by localPos. This is necessary</span>
<span class="comment">// because QQuickWindow thinks of itself as a top-level window always.</span>
<span class="type">QMouseEvent</span> mappedEvent(e<span class="operator">-</span><span class="operator">></span>type()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>localPos()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>screenPos()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>button()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>buttons()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>modifiers());
<span class="type">QCoreApplication</span><span class="operator">::</span>sendEvent(m_quickWindow<span class="operator">,</span> <span class="operator">&</span>mappedEvent);
}
<span class="type">void</span> WindowMultiThreaded<span class="operator">::</span>mouseReleaseEvent(<span class="type">QMouseEvent</span> <span class="operator">*</span>e)
{
<span class="type">QMouseEvent</span> mappedEvent(e<span class="operator">-</span><span class="operator">></span>type()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>localPos()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>screenPos()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>button()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>buttons()<span class="operator">,</span> e<span class="operator">-</span><span class="operator">></span>modifiers());
<span class="type">QCoreApplication</span><span class="operator">::</span>sendEvent(m_quickWindow<span class="operator">,</span> <span class="operator">&</span>mappedEvent);
}</pre>
</div>
<!-- @@@rendercontrol/window_multithreaded.cpp -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|