summaryrefslogtreecommitdiffstats
path: root/src/runtime/Qt3DSApplication.cpp
blob: f12fef90c37c9a2ec2eb956cce7e0a272709ec11 (plain)
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
/****************************************************************************
**
** Copyright (C) 2013 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

// We need a Qt header first here because Qt's metatype system insists that Bool
// can not be defined first before Qt headers are included and the includes below
// define Bool by way of Xll/XLib.h via khronos -> egl -> X11
#include <QImage>

#include "RuntimePrefix.h"
#include "Qt3DSApplication.h"
#include "Qt3DSApplicationValues.h"
#include "foundation/Qt3DSAtomic.h"
#include "Qt3DSMemory.h"
#include "Qt3DSRuntimeFactory.h"
#include "foundation/Qt3DSFoundation.h"
#include "foundation/Qt3DSBroadcastingAllocator.h"
#include "foundation/FileTools.h"
#include "Qt3DSIScriptBridge.h"
#include "foundation/Qt3DSOption.h"
#include "foundation/XML.h"
#include "foundation/IOStreams.h"
#include "foundation/Qt3DSContainers.h"
#include "EASTL/hash_map.h"
#include "Qt3DSPresentation.h"
#include "Qt3DSInputEventTypes.h"
#include "Qt3DSSceneManager.h"
#include "Qt3DSIScene.h"
#include "Qt3DSInputEngine.h"
#include "Qt3DSMetadata.h"
#include "Qt3DSUIPParser.h"
#include "foundation/Socket.h"
#include "EventPollingSystem.h"
#include "Qt3DSRenderContextCore.h"
#include "foundation/Qt3DSPerfTimer.h"
#include "foundation/SerializationTypes.h"
#include "EASTL/sort.h"
#include "Qt3DSRenderBufferLoader.h"
#include "foundation/Qt3DSMutex.h"
#include "foundation/Qt3DSSync.h"
#include "Qt3DSTextRenderer.h"
#include "Qt3DSRenderThreadPool.h"
#include "foundation/StringConversionImpl.h"
#include "Qt3DSRenderLoadedTexture.h"
#include "render/Qt3DSRenderContext.h"
#include "Qt3DSActivationManager.h"
#include "Qt3DSRenderer.h"
#include "Qt3DSRenderShaderCache.h"
#include "Qt3DSRenderInputStreamFactory.h"
#include "Qt3DSAudioPlayer.h"
#include "Qt3DSElementSystem.h"
#include "Qt3DSSlideSystem.h"
#include "Qt3DSQmlElementHelper.h"
#include "Qt3DSRenderBufferManager.h"
#include "Qt3DSRenderRenderList.h"
#include "Qt3DSRenderImageBatchLoader.h"
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qpair.h>
#include <QtCore/qdir.h>
#include "q3dsvariantconfig_p.h"

using namespace qt3ds;
using namespace qt3ds::runtime;
using namespace qt3ds::render;
using namespace Q3DStudio;

namespace qt3ds {
namespace foundation {
template <>
struct StringConversion<QT3DSVec2>
{
    void StrTo(const char8_t *buffer, QT3DSVec2 &item)
    {
        char *endPtr = NULL;
        item.x = (float)strtod(buffer, &endPtr);
        if (endPtr)
            item.y = (float)strtod(endPtr, NULL);
    }
};
}
}

bool qt3ds::runtime::isImagePath(const QString &path)
{
    int index = path.lastIndexOf(QLatin1Char('.'));
    if (index < 0)
        return false;
    const QString ext = path.right(path.length() - index - 1);
    return (ext == QLatin1String("jpg") || ext == QLatin1String("jpeg")
            || ext == QLatin1String("png") || ext == QLatin1String("hdr")
#ifndef LEGACY_ASTC_LOADING
            || ext == QLatin1String("astc")
#endif
            || ext == QLatin1String("dds") || ext == QLatin1String("ktx"));
}

struct SFrameTimer
{
    int m_FrameCount;
    QT3DSU64 m_FrameTime;
    SFrameTimer(QT3DSU64 fc = 0)
        : m_FrameCount(fc)
        , m_FrameTime(qt3ds::foundation::Time::getCurrentCounterValue())
    {
    }

    QT3DSF32 GetElapsedSeconds(QT3DSU64 currentTime) const
    {
        QT3DSU64 diff = currentTime - m_FrameTime;
        QT3DSF32 diffNanos
                = static_cast<QT3DSF32>(qt3ds::foundation::Time::sCounterFreq.toTensOfNanos(diff));
        return diffNanos / qt3ds::foundation::Time::sNumTensOfNanoSecondsInASecond;
    }

    QT3DSF32 GetElapsedSeconds() const
    {
        return GetElapsedSeconds(qt3ds::foundation::Time::getCurrentCounterValue());
    }

    QPair<QT3DSF32, int> GetFPS(int updateFC)
    {
        int elapsedFrames = updateFC - m_FrameCount;
        QT3DSU64 currentTime = qt3ds::foundation::Time::getCurrentCounterValue();
        QT3DSF32 elapsedSeconds = GetElapsedSeconds(currentTime);
        QT3DSF32 retval = elapsedFrames / elapsedSeconds;
        m_FrameCount = updateFC;
        m_FrameTime = currentTime;
        return qMakePair(retval, elapsedFrames);
    }
};

struct SRefCountedAssetValue : public SAssetValue
{
    NVFoundationBase &m_Foundation;
    QT3DSI32 mRefCount;
    SRefCountedAssetValue(NVFoundationBase &fnd)
        : SAssetValue()
        , m_Foundation(fnd)
        , mRefCount(0)
    {
    }

    SRefCountedAssetValue(NVFoundationBase &fnd, const SAssetValue &asset)
        : SAssetValue(asset)
        , m_Foundation(fnd)
        , mRefCount(0)
    {
    }

    SRefCountedAssetValue(NVFoundationBase &fnd, const SPresentationAsset &asset)
        : SAssetValue(asset)
        , m_Foundation(fnd)
        , mRefCount(0)
    {
    }

    SRefCountedAssetValue(NVFoundationBase &fnd, const SBehaviorAsset &asset)
        : SAssetValue(asset)
        , m_Foundation(fnd)
        , mRefCount(0)
    {
    }

    SRefCountedAssetValue(NVFoundationBase &fnd, const SRenderPluginAsset &asset)
        : SAssetValue(asset)
        , m_Foundation(fnd)
        , mRefCount(0)
    {
    }

    SRefCountedAssetValue(NVFoundationBase &fnd, const SSCXMLAsset &asset)
        : SAssetValue(asset)
        , m_Foundation(fnd)
        , mRefCount(0)
    {
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE(m_Foundation.getAllocator())
};

typedef nvhash_map<CRegisteredString, NVScopedRefCounted<SRefCountedAssetValue>> TIdAssetMap;
typedef nvhash_map<THashValue, CRegisteredString> THashStrMap;
typedef nvvector<eastl::pair<CRegisteredString, NVScopedRefCounted<SRefCountedAssetValue>>>
TIdAssetList;
typedef eastl::pair<QT3DSU32, TElement *> THandleElementPair;
typedef NVConstDataRef<THandleElementPair> THandleElementDataBuffer;
typedef nvvector<THandleElementDataBuffer> THandleElementDataBufferList;
typedef nvhash_map<QT3DSU32, TElement *> THandleElementMap;

struct SHandleElementPairComparator
{
    bool operator()(const THandleElementPair &lhs, const THandleElementPair &rhs) const
    {
        return lhs.first < rhs.first;
    }
};

static int s_debug = -1;

struct SSlideResourceCounter
{
    QHash<QString, int> counters;
    QSet<QString> createSet;
    QSet<QString> deleteSet;

    QVector<QString> loadedSlides;

    void increment(const QSet<QString> &set)
    {
        for (auto &r : set) {
            if (counters.value(r, 0) == 0)
                createSet.insert(r);
            counters[r]++;
        }
    }
    void decrement(const QSet<QString> &set)
    {
        for (auto &r : set) {
            if (counters.contains(r)) {
                int count = qMax(counters[r] - 1, 0);
                if (count == 0)
                    deleteSet.insert(r);
                counters[r] = count;
            }
        }
    }
    void begin()
    {
        createSet.clear();
        deleteSet.clear();
    }
    void reset()
    {
        loadedSlides.clear();
        counters.clear();
        begin();
    }
    QSet<QString> toImageSet(const QVector<QString> &vec)
    {
        QSet<QString> s;
        for (const auto &x : vec) {
            if (isImagePath(x))
                s.insert(x);
        }
        return s;
    }
    void handleLoadSlide(const QString &slide, SSlideKey key, ISlideSystem &slideSystem)
    {
        if (loadedSlides.contains(slide))
            return;
        loadedSlides.push_back(slide);
        begin();
        increment(toImageSet(slideSystem.GetSourcePaths(key)));
        print();
    }
    void handleUnloadSlide(const QString &slide, SSlideKey key, ISlideSystem &slideSystem)
    {
        if (!loadedSlides.contains(slide))
            return;
        loadedSlides.removeOne(slide);
        begin();
        decrement(toImageSet(slideSystem.GetSourcePaths(key)));
        print();
    }
    void print()
    {
        if (qt3ds::TRACE_INFO().isInfoEnabled()) {
            qCInfo(qt3ds::TRACE_INFO) << "SlideResourceCounter resources:";
            const auto keys = counters.keys();
            for (auto &x : keys)
                qCInfo(qt3ds::TRACE_INFO) << x << ": " << counters[x];
            if (createSet.size()) {
                qCInfo(qt3ds::TRACE_INFO) << "New resources: ";
                for (auto y : qAsConst(createSet))
                    qCInfo(qt3ds::TRACE_INFO) << y;
            }
            if (deleteSet.size()) {
                qCInfo(qt3ds::TRACE_INFO) << "Deleted resources: ";
                for (auto y : qAsConst(deleteSet))
                    qCInfo(qt3ds::TRACE_INFO) << y;
            }
        }
    }
};


struct SApp;

struct AssetHandlers {
    static bool handlePresentation(SApp &app, SAssetValue &asset, bool initRenderThread = false);
    static bool handleBehavior(SApp &app, SAssetValue &asset);
    static bool handleQmlPresentation(IRuntimeFactory &factory, SAssetValue &asset);
};

struct STextureUploadRenderTask : public IRenderTask, public IImageLoadListener
{
    IImageBatchLoader &m_batchLoader;
    IBufferManager &m_bufferManager;
    NVRenderContextType m_type;
    bool m_preferKtx;
    bool m_flipCompressedTextures;
    QSet<QString> m_uploadSet;
    QSet<QString> m_uploadWaitSet;
    QSet<QString> m_deleteSet;
    QMutex m_updateMutex;
    QHash<QT3DSU32, QSet<QString>> m_batches;
    volatile QT3DSI32 mRefCount;

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE_OVERRIDE(m_bufferManager.GetStringTable()
                                                      .GetAllocator())

    STextureUploadRenderTask(IImageBatchLoader &loader, IBufferManager &mgr,
                             NVRenderContextType type, bool preferKtx, bool flipCompressedTextures)
        : m_batchLoader(loader), m_bufferManager(mgr), m_type(type), m_preferKtx(preferKtx),
          m_flipCompressedTextures(flipCompressedTextures),
          mRefCount(0)
    {

    }
    void Run() override
    {
        QMutexLocker loc(&m_updateMutex);
        // Delete first so that maximum required memory is reduced
        if (!m_deleteSet.isEmpty())
            m_bufferManager.unloadSet(m_deleteSet);
        if (!m_uploadSet.isEmpty()) {
            nvvector<CRegisteredString> sourcePaths(m_bufferManager.GetStringTable().GetAllocator(),
                                                    "TempSourcePathList");
            for (auto &s : qAsConst(m_uploadSet))
                sourcePaths.push_back(m_bufferManager.GetStringTable().RegisterStr(s));
            QT3DSU32 id = m_batchLoader.LoadImageBatch(sourcePaths, CRegisteredString(),
                                                       this, m_type, m_preferKtx, false);
            if (id) {
                m_batches[id] = m_uploadSet;
                m_uploadSet.clear();
            }
        }
        if (!m_uploadWaitSet.isEmpty()) {
            nvvector<CRegisteredString> sourcePaths(m_bufferManager.GetStringTable().GetAllocator(),
                                                    "TempSourcePathList");
            for (auto &s : qAsConst(m_uploadWaitSet))
                sourcePaths.push_back(m_bufferManager.GetStringTable().RegisterStr(s));
            QT3DSU32 id = m_batchLoader.LoadImageBatch(sourcePaths, CRegisteredString(),
                                                       this, m_type, m_preferKtx, false);
            if (id) {
                m_batchLoader.BlockUntilLoaded(id);
                m_bufferManager.loadSet(m_uploadWaitSet, m_flipCompressedTextures);
                m_uploadWaitSet.clear();
            }
        }
    }
    void add(const QSet<QString> &set, bool wait)
    {
        QMutexLocker loc(&m_updateMutex);
        if (wait)
            m_uploadWaitSet.unite(set);
        else
            m_uploadSet.unite(set);
        m_deleteSet.subtract(set);
    }
    void remove(const QSet<QString> &set)
    {
        QMutexLocker loc(&m_updateMutex);
        m_uploadSet.subtract(set);
        m_uploadWaitSet.subtract(set);
        m_deleteSet.unite(set);
    }
    bool persistent() const override
    {
        return true;
    }
    void OnImageLoadComplete(CRegisteredString inPath, ImageLoadResult::Enum inResult) override
    {
        Q_UNUSED(inPath);
        Q_UNUSED(inResult);
    }
    void OnImageBatchComplete(QT3DSU64 inBatch) override
    {
        m_bufferManager.loadSet(m_batches[inBatch]);
    }
};

class IAppLoadContext : public NVRefCounted
{
public:
    virtual void EndLoad() = 0;
    virtual bool OnGraphicsInitialized(IRuntimeFactory &inFactory, bool initInRenderThread) = 0;
    virtual bool HasCompletedLoading() = 0;
    static IAppLoadContext &CreateXMLLoadContext(
            SApp &inApp, const char8_t *inScaleMode);
};

inline float Clamp(float val, float inMin = 0.0f, float inMax = 1.0f)
{
    if (val < inMin)
        return inMin;
    if (val > inMax)
        return inMax;
    return val;
}

// A set of common settings that may come from the UIA or from the command line.
// command line settings always override uia settings.
struct SApplicationSettings
{
    Option<bool> m_LayerCacheEnabled;
    Option<bool> m_LayerGpuProfilingEnabled;

    SApplicationSettings() {}

    template <typename TDataType>
    static Option<TDataType> Choose(const Option<TDataType> &inCommandLine,
                                    const Option<TDataType> &inUIAFile)
    {
        if (inCommandLine.hasValue())
            return inCommandLine;
        return inUIAFile;
    }

    SApplicationSettings(const SApplicationSettings &inCommandLine,
                         const SApplicationSettings &inUIAFileSettings)
        : m_LayerCacheEnabled(
              Choose(inCommandLine.m_LayerCacheEnabled, inUIAFileSettings.m_LayerCacheEnabled))
        , m_LayerGpuProfilingEnabled(Choose(inCommandLine.m_LayerGpuProfilingEnabled,
                                            inUIAFileSettings.m_LayerGpuProfilingEnabled))
    {
    }

    static const char8_t *LayerCacheName() { return "layer-caching"; }
    static const char8_t *LayerGpuProfilerName() { return "layer-gpu-profiling"; }
    static const char8_t *ShaderCacheName() { return "shader-cache-persistence"; }

    void ParseBoolEnableDisableItem(const IDOMReader &inReader, const char8_t *itemName,
                                    Option<bool> &itemValue)
    {
        const char8_t *inItem;
        if (const_cast<IDOMReader &>(inReader).UnregisteredAtt(itemName, inItem)) {
            if (AreEqualCaseless(inItem, "disabled"))
                itemValue = false;
            else
                itemValue = true;
        }
    }

    void ParseBoolEnableDisableItem(const eastl::vector<eastl::string> &inCommandLine,
                                    const char8_t *itemName, Option<bool> &itemValue)
    {
        eastl::string temp;
        temp.assign("-");
        temp.append(itemName);
        for (QT3DSU32 idx = 0, end = inCommandLine.size(); idx < end; ++idx) {
            if (inCommandLine[idx].find(temp) == 0) {
                if (inCommandLine[idx].length() == temp.size()) {
                    qCWarning(qt3ds::INVALID_OPERATION)
                            << "Unable to parse parameter %s. Please pass =enable|disable as "
                            << "part of the parameter. " << temp.c_str();
                } else {
                    temp = inCommandLine[idx].substr(temp.size() + 1);
                    eastl::string::size_type start = temp.find_first_of("'\"");
                    if (start != eastl::string::npos)
                        temp.erase(0, start);

                    eastl::string::size_type end = temp.find_first_of("'\"");
                    if (end != eastl::string::npos)
                        temp.erase(end);
                    if (AreEqualCaseless(temp.c_str(), "disabled"))
                        itemValue = false;
                    else
                        itemValue = true;
                    qCInfo(qt3ds::INVALID_OPERATION)
                            << "Item " << itemName
                            << (itemValue ? " enabled" : " disabled");
                }
            }
        }
    }

    template <typename TReaderType>
    void ParseItems(const TReaderType &inReader)
    {
        ParseBoolEnableDisableItem(inReader, LayerCacheName(), m_LayerCacheEnabled);
        ParseBoolEnableDisableItem(inReader, LayerGpuProfilerName(), m_LayerGpuProfilingEnabled);
    }

    void Parse(IDOMReader &inReader) { ParseItems(inReader); }

    void Parse(const eastl::vector<eastl::string> &inCommandLine) { ParseItems(inCommandLine); }

    struct SOptionSerializer
    {
        bool m_HasValue;
        bool m_Value;
        QT3DSU8 m_Padding[2];
        SOptionSerializer(const Option<bool> &inValue = Empty())
            : m_HasValue(inValue.hasValue())
            , m_Value(inValue.hasValue() ? *inValue : false)
        {
            m_Padding[0] = 0;
            m_Padding[1] = 0;
        }

        operator Option<bool>() const
        {
            if (m_HasValue)
                return m_Value;
            return Empty();
        }
    };

    void Save(IOutStream &outStream) const
    {
        outStream.Write(SOptionSerializer(m_LayerCacheEnabled));
        outStream.Write(SOptionSerializer(m_LayerGpuProfilingEnabled));
    }

    void Load(IInStream &inStream)
    {
        SOptionSerializer s;
        inStream.Read(s);
        m_LayerCacheEnabled = s;
        inStream.Read(s);
        m_LayerGpuProfilingEnabled = s;
    }
};

struct SDummyAudioPlayer : public IAudioPlayer
{
    virtual ~SDummyAudioPlayer() {}
    bool PlaySoundFile(const char *inFilePath) override
    {
        (void *)inFilePath;
        qCWarning(qt3ds::TRACE_INFO)
                << "Qt3DSTegraApplication: Unimplemented method IAudioPlayer::PlaySoundFile";
        return false;
    }
} g_DummyAudioPlayer;

struct SAudioPlayerWrapper : public IAudioPlayer
{
private:
    IApplication *m_Application;
    IAudioPlayer *m_RealPlayer;

public:
    SAudioPlayerWrapper()
        : m_Application(0)
        , m_RealPlayer(&g_DummyAudioPlayer)
    {
    }
    virtual ~SAudioPlayerWrapper() {}

    void SetApplication(IApplication &inApplication) { m_Application = &inApplication; }

    void SetPlayer(IAudioPlayer *inPlayer)
    {
        if (inPlayer)
            m_RealPlayer = inPlayer;
        else
            m_RealPlayer = &g_DummyAudioPlayer;
    }

    bool PlaySoundFile(const char *inFilePath) override
    {
        eastl::string theFilePath(nonNull(inFilePath));
        if (m_RealPlayer != &g_DummyAudioPlayer) {
            qt3ds::foundation::CFileTools::CombineBaseAndRelative(
                        m_Application->GetProjectDirectory().c_str(), inFilePath, theFilePath);
        }
        return m_RealPlayer->PlaySoundFile(theFilePath.c_str());
    }
};

struct SApp : public IApplication
{
    NVScopedRefCounted<Q3DStudio::IRuntimeFactoryCore> m_CoreFactory;
    NVScopedRefCounted<Q3DStudio::IRuntimeFactory> m_RuntimeFactory;

    Q3DStudio::CInputEngine *m_InputEnginePtr;
    CAppStr m_ApplicationDir;
    CAppStr m_ProjectDir;
    CAppStr m_PresentationId;
    CAppStr m_DLLDirectory;
    TIdAssetMap m_AssetMap;
    // Keep the assets ordered.  This enables the uia order to mean something.
    TIdAssetList m_OrderedAssets;
    SPickFrame m_PickFrame;
    SPickFrame m_MousePickCache;
    SPickFrame m_MouseOverCache;
    THashStrMap m_HashStrMap;
    CTimer m_Timer;
    Q3DStudio::INT64 m_ManualTime;
    SFrameTimer m_FrameTimer;
    Q3DStudio::INT32 m_FrameCount;
    // the name of the file without extension.
    eastl::string m_Filename;
    Q3DSVariantConfig m_variantConfig;
    NVScopedRefCounted<STextureUploadRenderTask> m_uploadRenderTask;

    qt3ds::foundation::NVScopedReleasable<IRuntimeMetaData> m_MetaData;
    nvvector<eastl::pair<SBehaviorAsset, bool>> m_Behaviors;
    NVScopedRefCounted<SocketSystem> m_SocketSystem;
    NVScopedRefCounted<SocketStream> m_ServerStream;
    NVScopedRefCounted<IActivityZoneManager> m_ActivityZoneManager;
    NVScopedRefCounted<IElementAllocator> m_ElementAllocator;

    // Handles are loaded sorted but only added to the handle map when needed.
    nvvector<char8_t> m_LoadBuffer;
    Mutex m_RunnableMutex;
    nvvector<NVScopedRefCounted<IAppRunnable>> m_ThreadRunnables;
    nvvector<NVScopedRefCounted<IAppRunnable>> m_MainThreadRunnables;
    NVScopedRefCounted<IAppLoadContext> m_AppLoadContext;
    bool m_DisableState;
    bool m_ProfileLogging;
    bool m_LastRenderWasDirty;
    bool m_ProgressiveLeftFrame;
    QT3DSU64 m_LastFrameStartTime;
    QT3DSU64 m_ThisFrameStartTime;
    double m_MillisecondsSinceLastFrame;
    // We get odd oscillations if we do are too quick to report that the frame wasn't dirty
    // after input.
    int m_DirtyCountdown;
    SApplicationSettings m_UIAFileSettings;
    eastl::pair<NVDataRef<Q3DStudio::TElement *>, size_t> m_ElementLoadResult;

    SAudioPlayerWrapper m_AudioPlayer;

    Qt3DSAssetVisitor *m_visitor;

    bool m_createSuccessful;

    DataInputMap m_dataInputDefs;
    DataOutputMap m_dataOutputDefs;

    bool m_initialFrame = true;
    int m_skipFrameCount = 0;
    SSlideResourceCounter m_resourceCounter;
    QSet<QString> m_createSet;

    QT3DSI32 mRefCount;
    SApp(Q3DStudio::IRuntimeFactoryCore &inFactory, const char8_t *inAppDir)
        : m_CoreFactory(inFactory)
        , m_InputEnginePtr(NULL)
        , m_ApplicationDir(inFactory.GetFoundation().getAllocator())
        , m_ProjectDir(inFactory.GetFoundation().getAllocator())
        , m_PresentationId(inFactory.GetFoundation().getAllocator())
        , m_DLLDirectory(inFactory.GetFoundation().getAllocator())
        , m_AssetMap(inFactory.GetFoundation().getAllocator(), "SApp::m_AssetMap")
        , m_OrderedAssets(inFactory.GetFoundation().getAllocator(), "SApp::m_OrderedAssets")
        , m_HashStrMap(inFactory.GetFoundation().getAllocator(), "SApp::m_HashStrMap")
        , m_Timer(inFactory.GetTimeProvider())
        , m_ManualTime(0)
        , m_FrameCount(0)
        , m_Behaviors(inFactory.GetFoundation().getAllocator(), "SApp::m_Behaviors")
        , m_ActivityZoneManager(IActivityZoneManager::CreateActivityZoneManager(
                                    inFactory.GetFoundation(), inFactory.GetStringTable()))
        , m_ElementAllocator(IElementAllocator::CreateElementAllocator(inFactory.GetFoundation(),
                                                                       inFactory.GetStringTable()))
        , m_LoadBuffer(inFactory.GetFoundation().getAllocator(), "SApp::m_LoadBuffer")
        , m_RunnableMutex(inFactory.GetFoundation().getAllocator())
        , m_ThreadRunnables(inFactory.GetFoundation().getAllocator(), "SApp::m_ThreadRunnables")
        , m_MainThreadRunnables(inFactory.GetFoundation().getAllocator(),
                                "SApp::m_MainThreadRunnables")
        , m_DisableState(true)
        , m_ProfileLogging(false)
        , m_LastRenderWasDirty(true)
        , m_ProgressiveLeftFrame(true)
        , m_LastFrameStartTime(0)
        , m_ThisFrameStartTime(0)
        , m_MillisecondsSinceLastFrame(0)
        , m_DirtyCountdown(5)
        , m_visitor(nullptr)
        , m_createSuccessful(false)
        , mRefCount(0)
    {
        m_PresentationId.append("__initial");
        m_AudioPlayer.SetApplication(*this);
        eastl::string tempStr(inAppDir);
        CFileTools::NormalizePath(tempStr);
        m_ApplicationDir.assign(tempStr.c_str());

        Q3DStudio_memset(&m_PickFrame, 0, sizeof(SPickFrame));
        Q3DStudio_memset(&m_MousePickCache, 0, sizeof(SPickFrame));
        Q3DStudio_memset(&m_MouseOverCache, 0, sizeof(SPickFrame));

        m_Timer.Start();

        m_CoreFactory->SetApplicationCore(this);
        m_CoreFactory->GetScriptEngineQml().SetApplicationCore(*this);

        m_CoreFactory->AddSearchPath(tempStr.c_str());
    }

    ~SApp()
    {
        EndLoad();
        {
            Mutex::ScopedLock __locker(m_RunnableMutex);
            m_ThreadRunnables.clear();
        }
        // Ensure we stop the timer.
        HasCompletedLoading();
        m_AppLoadContext = NULL;

        for (QT3DSU32 idx = 0, end = m_OrderedAssets.size(); idx < end; ++idx) {
            SAssetValue &theAsset = *m_OrderedAssets[idx].second;
            if (theAsset.getType() == AssetValueTypes::Presentation) {
                SPresentationAsset &thePresAsset = *theAsset.getDataPtr<SPresentationAsset>();
                if (thePresAsset.m_Presentation) {
                    Q3DStudio_delete(thePresAsset.m_Presentation, CPresentation);
                    thePresAsset.m_Presentation = NULL;
                }
            }
        }
    }

    void setPresentationId(const QString &id) override
    {
        QString oldId = QString::fromLocal8Bit(m_PresentationId.c_str());
        if (oldId == id)
            return;

        CRegisteredString idStr = m_CoreFactory->GetStringTable().RegisterStr(id);
        // Update id key in m_AssetMap
        TIdAssetMap::iterator iter
                = m_AssetMap.find(m_CoreFactory->GetStringTable().RegisterStr(oldId));
        if (iter != m_AssetMap.end()
                && iter->second->getType() == AssetValueTypes::Presentation) {
            m_AssetMap.insert(eastl::make_pair(idStr, iter->second));
            m_AssetMap.erase(iter);
        }
        for (unsigned i = 0; i < m_OrderedAssets.size(); i++) {
            auto &asset = m_OrderedAssets[i];
            if (oldId == asset.first.c_str()) {
                asset.first = idStr;
                break;
            }
        }

        m_PresentationId.assign(qPrintable(id));
    }

    void setAssetVisitor(qt3ds::Qt3DSAssetVisitor *v) override
    {
        m_visitor = v;
    }

    QVector<CPresentation *> getPresentations()
    {
        QVector<CPresentation *> presentations;
        for (QT3DSU32 idx = 0, end = m_OrderedAssets.size(); idx < end; ++idx) {
            SAssetValue &theAsset = *m_OrderedAssets[idx].second;
            if (theAsset.getType() == AssetValueTypes::Presentation) {
                SPresentationAsset &thePresAsset = *theAsset.getDataPtr<SPresentationAsset>();
                if (thePresAsset.m_Presentation)
                    presentations.push_back(thePresAsset.m_Presentation);
            }
        }
        return presentations;
    }

    void addRef() override { atomicIncrement(&mRefCount); }

    void release() override
    {
        atomicDecrement(&mRefCount);
        if (mRefCount <= 0)
            NVDelete(m_CoreFactory->GetFoundation().getAllocator(), this);
    }

    void QueueForMainThread(IAppRunnable &inRunnable) override
    {
        Mutex::ScopedLock __locker(m_RunnableMutex);
        m_ThreadRunnables.push_back(inRunnable);
    }

    virtual void EnableProfileLogging()
    {
        m_ProfileLogging = true;
        if (m_RuntimeFactory)
            m_RuntimeFactory->GetScriptEngineQml().EnableProfiling();
    }

    // Verbose logging is disabled by default.
    virtual void SetVerboseLogging(bool inEnableVerboseLogging)
    {

    }

    ////////////////////////////////////////////////////////////////////////////
    // Update rhythm implementations
    ////////////////////////////////////////////////////////////////////////////

    void SetPickFrame(const SPickFrame &inPickFrame)
    {
        // The model has changed, fire enter and exit mouse events
        if (inPickFrame.m_Model != m_PickFrame.m_Model) {
            // For determining onGroupedMouseOver/Out:
            // arg1 = the original onMouseOut model and arg2 = the original onMouseOver model
            UVariant theMouseOutModel;
            UVariant theMouseOverModel;
            theMouseOutModel.m_VoidPointer = m_PickFrame.m_Model;
            theMouseOverModel.m_VoidPointer = inPickFrame.m_Model;

            // It seems like you would want to 'onMouseOut' before you 'onMouseOver' something new?
            if (m_PickFrame.m_Model) {
                m_PickFrame.m_Model->GetBelongedPresentation()->FireEvent(
                            ON_MOUSEOUT, m_PickFrame.m_Model, &theMouseOutModel, &theMouseOverModel,
                            ATTRIBUTETYPE_POINTER, ATTRIBUTETYPE_POINTER);
            }
            if (inPickFrame.m_Model) {
                inPickFrame.m_Model->GetBelongedPresentation()->FireEvent(
                            ON_MOUSEOVER, inPickFrame.m_Model, &theMouseOutModel, &theMouseOverModel,
                            ATTRIBUTETYPE_POINTER, ATTRIBUTETYPE_POINTER);
                m_MouseOverCache = inPickFrame;
            }
        }

        const TEventCommandHash *theEventArray[] = { &ON_MOUSEDOWN,       &ON_MOUSEUP,
                                                     &ON_MIDDLEMOUSEDOWN, &ON_MIDDLEMOUSEUP,
                                                     &ON_RIGHTMOUSEDOWN,  &ON_RIGHTMOUSEUP };
        const TEventCommandHash *theClickEventArray[] = { &ON_MOUSECLICK, &ON_MIDDLEMOUSECLICK,
                                                          &ON_RIGHTMOUSECLICK };

        // Click events...
        // NOTE:  This is a fancy way to iterate programatically over all the handled mouse inputs
        // handled (declared in AKPickFrame.h for now)
        // we iterate to INPUTBUTTONCOUNT (see comment in AKPickFrame.h) * 2, because we handle
        // mouse down and up
        for (QT3DSI32 theMouseEvtIter = 0; theMouseEvtIter < MOUSEBUTTONCOUNT - 1;
             theMouseEvtIter++) {
            // we effectively iterate to MOUSEBUTTONCOUNT * 2 (see comment in AKPickFrame.h) to
            // handle mouse down and up
            QT3DSI32 theMouseDownFlag = 1 << (theMouseEvtIter * 2);
            QT3DSI32 theMouseUpFlag = 1 << (theMouseEvtIter * 2 + 1);

            // on*MouseDown
            // if this frame, the mouse button is down, and last frame it wasn't (new down click)
            if (inPickFrame.m_Model && inPickFrame.m_InputFrame.m_MouseFlags & theMouseDownFlag
                    && !(m_PickFrame.m_InputFrame.m_MouseFlags & theMouseDownFlag)) {
                // fire the 'on*MouseDown' event - which is at the even indices since the down
                // events for each button are before the up
                inPickFrame.m_Model->GetBelongedPresentation()->FireEvent(
                            *theEventArray[theMouseEvtIter * 2], inPickFrame.m_Model);

                // cache this as the last item we 'onMouseDown' on
                m_MousePickCache = inPickFrame;
            }

            // on*MouseUp
            // if we mouse up on anything, send the event
            if (inPickFrame.m_InputFrame.m_MouseFlags & theMouseUpFlag) {
                // fire the 'on*MouseUp' event - odd indices (1,3,5 etc)
                if (m_MousePickCache.m_Model) {
                    m_MousePickCache.m_Model->GetBelongedPresentation()->FireEvent(
                                *theEventArray[theMouseEvtIter * 2 + 1], m_MousePickCache.m_Model);
                }

                // on*MouseClick
                // if we had a up click on the same item we were mouse down on last frame ... we had
                // a click
                if (inPickFrame.m_Model && inPickFrame.m_Model == m_MousePickCache.m_Model) {
                    inPickFrame.m_Model->GetBelongedPresentation()->FireEvent(
                                *theClickEventArray[theMouseEvtIter], inPickFrame.m_Model);
                }

                // clear the stored 'last mouse down' since we just got a mouse up
                Q3DStudio_memset(&m_MousePickCache, 0, sizeof(SPickFrame));
            }

            // on*MouseDblClick
        }

        if (m_MouseOverCache.m_Model) {

            if (inPickFrame.m_InputFrame.m_MouseFlags & VSCROLLWHEEL) {
                UVariant theScrollValue;
                theScrollValue.m_INT32 = inPickFrame.m_InputFrame.m_ScrollValue;
                m_MouseOverCache.m_Model->GetBelongedPresentation()->FireEvent(
                            ON_VERTICALSCROLLWHEEL, m_MouseOverCache.m_Model, &theScrollValue,
                            NULL, ATTRIBUTETYPE_INT32);
            } else if (inPickFrame.m_InputFrame.m_MouseFlags & HSCROLLWHEEL) {
                UVariant theScrollValue;
                theScrollValue.m_INT32 = inPickFrame.m_InputFrame.m_ScrollValue;
                m_MouseOverCache.m_Model->GetBelongedPresentation()->FireEvent(
                            ON_HORIZONTALSCROLLWHEEL, m_MouseOverCache.m_Model, &theScrollValue,
                            NULL, ATTRIBUTETYPE_INT32);
            }
        }

        // Do this last
        m_PickFrame = inPickFrame;
    }

    void ClearPresentationDirtyLists()
    {
        const QVector<CPresentation *> presentations(getPresentations());
        for (auto pres : presentations)
            pres->ClearDirtyList();
    }

    void forAllPresentations(const QVector<CPresentation *> &presentations, bool checkActive,
                             std::function<void(CPresentation *)> func)
    {
        for (auto pres : presentations) {
            if (!checkActive || pres->GetActive())
                func(pres);
        }
    }

    void UpdatePresentations()
    {
        QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(), "UpdatePresentations: Total")
        // Transfer the input frame to the kernel for pick processing
        // the scene manager now handles the picking on each of its scenes
        SetPickFrame(m_RuntimeFactory->GetSceneManager().AdvancePickFrame(
                         m_InputEnginePtr->GetInputFrame()));
        // clear up mouse flag for horizontal and vertical scroll
        m_InputEnginePtr->GetInputFrame().m_MouseFlags &= !(HSCROLLWHEEL | VSCROLLWHEEL);

        // Update all the presentations.
        // Animations are advanced based on m_Timer by default, but this can be overridden via
        // SetTimeMilliSecs().
        Q3DStudio::INT64 globalTime(GetTimeMilliSecs());

        QVector<CPresentation *> presentations(getPresentations());

        {
            QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(),
                                    "UpdatePresentations: PreUpdate")
            forAllPresentations(presentations, true, [globalTime](CPresentation *p) {
                p->PreUpdate(globalTime);
            });
        }
        {
            QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(),
                                    "UpdatePresentations: BeginUpdate")
            forAllPresentations(presentations, true, [](CPresentation *p) {
                p->BeginUpdate();
            });
        }
        // Allow EndUpdate and PostUpdate for inactive presentations so we can activate it
        {
            QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(),
                                    "UpdatePresentations: EndUpdate")
            forAllPresentations(presentations, false, [](CPresentation *p) {
                p->EndUpdate();
            });
        }
        {
            QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(),
                                    "UpdatePresentations: PostUpdate")
            forAllPresentations(presentations, false, [globalTime](CPresentation *p) {
                p->PostUpdate(globalTime);
            });
        }

        // Run the garbage collection
        m_CoreFactory->GetScriptEngineQml().StepGC();
    }

    void NotifyDataOutputs()
    {
        {
            QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(),
                                    "Application: NotifyDataOutputs")

            // Allow presentations to notify of registered data output changes
            for (QT3DSU32 idx = 0, end = m_OrderedAssets.size(); idx < end; ++idx) {
                if (m_OrderedAssets[idx].second->getType() == AssetValueTypes::Presentation) {
                    SPresentationAsset &asset(
                                *m_OrderedAssets[idx].second->getDataPtr<SPresentationAsset>());
                    CPresentation *presentation = asset.m_Presentation;
                    // allow PostUpdate also for inactive presentations so that we can
                    // activate it
                    if (presentation)
                        presentation->NotifyDataOutputs();
                }
            }

            // Notify @timeline attribute changes and store latest value to notified DataOutputDef
            QMutableMapIterator<QString, DataOutputDef> iter(m_dataOutputDefs);
            while (iter.hasNext()) {
                iter.next();
                DataOutputDef &outDef = iter.value();
                if (outDef.observedAttribute.propertyType == ATTRIBUTETYPE_DATAINPUT_TIMELINE
                        && outDef.timelineComponent) {
                    qreal newValue = outDef.timelineComponent->GetTimePolicy().GetTime();
                    qreal timelineEndTime
                            = outDef.timelineComponent->GetTimePolicy().GetLoopingDuration();

                    // Normalize the value to dataOutput range (if defined)
                    if (outDef.min < outDef.max && timelineEndTime != 0.0) {
                        newValue = (newValue/timelineEndTime) * qreal(outDef.max - outDef.min);
                        newValue += qreal(outDef.min);
                    } else {
                        // Normalize to milliseconds
                        newValue *= 1000.0;
                    }

                    if (!outDef.value.isValid() || newValue != outDef.value.toReal()) {
                        outDef.value.setValue(newValue);;
                        GetPrimaryPresentation()->signalProxy()->SigDataOutputValueUpdated(
                                    outDef.name, outDef.value);
                    }
                }
            }
        } // End QT3DS_PERF_SCOPED_TIMER scope
    }

    bool UpdateScenes() { return m_RuntimeFactory->GetSceneManager().Update(); }

    bool LazyLoadSubPresentations()
    {
        bool loadedSomething = false;
        QVector<CRegisteredString> activeSubpresentations;
        m_RuntimeFactory->GetSceneManager().GetActiveSubPresentations(activeSubpresentations);

        for (auto subPres : qAsConst(activeSubpresentations)) {
            // Already loaded?
            if (GetPresentationById(subPres.c_str()))
                continue;
            bool done = false;
            for (unsigned int i = 0; i < m_OrderedAssets.size() && !done; ++i) {
                if (m_OrderedAssets[i].first == subPres) {
                    // Load asset
                    SAssetValue &theAsset = *m_OrderedAssets[i].second;
                    switch (theAsset.getType()) {
                    case AssetValueTypes::Presentation: {
                        AssetHandlers::handlePresentation(*this, theAsset);
                        loadedSomething = true;
                        done = true;

                        SPresentationAsset &thePresentationAsset
                                = *theAsset.getDataPtr<SPresentationAsset>();
                        CPresentation *thePresentation = thePresentationAsset.m_Presentation;
                        if (thePresentation) {
                            QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                                    "Application: SetActivityZone")
                            thePresentation->SetActivityZone(
                                      &m_ActivityZoneManager->CreateActivityZone(*thePresentation));
                            thePresentation->SetActive(thePresentationAsset.m_Active);
                        }
                    } break;
                    case AssetValueTypes::Behavior:
                        AssetHandlers::handleBehavior(*this, theAsset);
                        loadedSomething = true;
                        done = true;
                        break;
                    case AssetValueTypes::QmlPresentation:
                        AssetHandlers::handleQmlPresentation(GetRuntimeFactory(), theAsset);
                        loadedSomething = true;
                        done = true;
                        break;
                        // SCXML, NoAssetValue do not need processing here
                    default:
                        done = true;
                        break;
                    }
                }
            }
        }
        return loadedSomething;
    }

    void Render()
    {
        QT3DS_PERF_SCOPED_TIMER(m_RuntimeFactory->GetPerfTimer(), "Application: Render")
        CPresentation *pres = GetPrimaryPresentation();
        if (pres) {
            auto &rc = m_RuntimeFactory->GetQt3DSRenderContext();
            if (!rc.IsStereoscopic()) {
                rc.SetStereoView(StereoViews::Mono);
                m_LastRenderWasDirty = m_RuntimeFactory->GetSceneManager()
                        .RenderPresentation(pres, m_initialFrame);
            } else {
                // In stereoscopic mode, render 2 times for left & right eye
                if (!rc.GetStereoProgressiveEnabled() || m_ProgressiveLeftFrame) {
                    rc.SetStereoView(StereoViews::Left);
                    m_LastRenderWasDirty = m_RuntimeFactory->GetSceneManager()
                            .RenderPresentation(pres, m_initialFrame);
                }
                if (!rc.GetStereoProgressiveEnabled() || !m_ProgressiveLeftFrame) {
                    rc.SetStereoView(StereoViews::Right);
                    m_RuntimeFactory->GetSceneManager()
                            .RenderPresentation(pres, m_initialFrame);
                }
                m_ProgressiveLeftFrame = !m_ProgressiveLeftFrame;
            }

            m_initialFrame = false;
        }
    }

    void ResetDirtyCounter() { m_DirtyCountdown = 5; }

    // Returns true when skipping the frame, false when rendering it
    bool checkSkipFrame()
    {
        auto &rc = m_RuntimeFactory->GetQt3DSRenderContext();
        int skipFrames = rc.GetSkipFramesInterval();
        if (skipFrames == 0)
            return false;

        if (m_skipFrameCount <= 0) {
            m_skipFrameCount = skipFrames;
            return false;
        }

        m_skipFrameCount--;
        return true;
    }

    // Update all the presentations and render them.
    bool UpdateAndRender() override
    {
        QT3DS_ASSERT(m_AppLoadContext.mPtr == NULL);
        m_ThisFrameStartTime = qt3ds::foundation::Time::getCurrentCounterValue();
        if (m_LastFrameStartTime) {
            QT3DSU64 durationSinceLastFrame = m_ThisFrameStartTime - m_LastFrameStartTime;
            m_MillisecondsSinceLastFrame =
                    qt3ds::foundation::Time::sCounterFreq.toTensOfNanos(durationSinceLastFrame)
                    * (1.0 / 100000.0);
        } else {
            m_MillisecondsSinceLastFrame = 0;
        }

        ++m_FrameCount;

        // First off, update any application level behaviors.
        IScriptBridge &theScriptEngine = m_CoreFactory->GetScriptEngineQml();
        for (QT3DSU32 idx = 0, end = m_Behaviors.size(); idx < end; ++idx) {
            eastl::pair<SBehaviorAsset, bool> &entry(m_Behaviors[idx]);
            if (!entry.second) {
                entry.second = true;
                theScriptEngine.ExecuteApplicationScriptFunction(entry.first.m_Handle,
                                                                 "onInitialize");
            }
        }

        // TODO: Initialize presentations

        for (QT3DSU32 idx = 0, end = m_Behaviors.size(); idx < end; ++idx) {
            eastl::pair<SBehaviorAsset, bool> &entry(m_Behaviors[idx]);
            theScriptEngine.ExecuteApplicationScriptFunction(entry.first.m_Handle, "onUpdate");
        }

        UpdatePresentations();
        bool dirty = UpdateScenes();

        // If subpresentations changed we need to check if any of them needs to be loaded.
        if (LazyLoadSubPresentations()) {
            // Just redo all
            UpdatePresentations();
            dirty |= UpdateScenes();
        }
        bool renderNextFrame = false;
        if (m_LastRenderWasDirty || dirty || m_initialFrame)
            renderNextFrame = true;

        bool skip = checkSkipFrame();
        // If we skip rendering this frame, mark next frame to be rendered
        renderNextFrame |= skip;
        if (!skip)
            Render();

        m_InputEnginePtr->ClearInputFrame();

        NotifyDataOutputs();

        ClearPresentationDirtyLists();

        if (!m_CoreFactory->GetEventSystem().GetAndClearEventFetchedFlag())
            m_CoreFactory->GetEventSystem().PurgeEvents(); // GetNextEvents of event system has not
                                                           // been called in this round, so clear
                                                           // events to avoid events to be piled up

        m_RuntimeFactory->GetQt3DSRenderContext().SetFrameTime(m_MillisecondsSinceLastFrame);
        if (floor(m_FrameTimer.GetElapsedSeconds()) > 0.0f) {
            QPair<QT3DSF32, int> fps = m_FrameTimer.GetFPS(m_FrameCount);
            m_RuntimeFactory->GetQt3DSRenderContext().SetFPS(fps);
            if (m_ProfileLogging) {
                qCInfo(PERF_INFO, "Render Statistics: %3.2ffps, frame count %d",
                       fps.first, fps.second);
            }
        }

        fflush(stdout);
        m_LastFrameStartTime = m_ThisFrameStartTime;
        if (m_LastRenderWasDirty)
            ResetDirtyCounter();
        else
            m_DirtyCountdown = NVMax(0, m_DirtyCountdown - 1);
        return renderNextFrame;
    }

    // hook this up to -layer-caching.
    // otherwise it might be hard to measure performance
    bool IsApplicationDirty() override
    {
        return (m_DirtyCountdown > 0);
    }

    double GetMillisecondsSinceLastFrame() override { return m_MillisecondsSinceLastFrame; }

    void MarkApplicationDirty() override { ResetDirtyCounter(); }

    Q3DStudio::IAudioPlayer &GetAudioPlayer() override { return m_AudioPlayer; }
    ////////////////////////////////////////////////////////////////////////////////
    // Generalized save/load
    ////////////////////////////////////////////////////////////////////////////////

    void loadComponentSlideResources(TElement *component, CPresentation *presentation, int index,
                                     const QString slideName, bool wait)
    {
        if (m_RuntimeFactory->GetQt3DSRenderContext().GetBufferManager()
                .isReloadableResourcesEnabled()) {
            QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                    "Application: Load Slide Resources")
            auto &slidesystem = presentation->GetSlideSystem();
            SSlideKey key;
            key.m_Component = component;
            key.m_Index = index;
            slidesystem.setUnloadSlide(key, false);
            const QString completeName = presentation->GetName() + QLatin1Char(':')
                    + QString::fromUtf8(key.m_Component->name()) + QLatin1Char(':') + slideName;
            qCInfo(TRACE_INFO) << "Load component slide resources: " << completeName;
            m_resourceCounter.handleLoadSlide(completeName, key, slidesystem);
            if (m_uploadRenderTask)
                m_uploadRenderTask->add(m_resourceCounter.createSet, wait);
            else
                m_createSet.unite(m_resourceCounter.createSet);

            QVector<QString> newAssets;

            getComponentSlideAssets(newAssets, presentation, component, index);

            // Load subpresentations of components under non-master slides of the main scene
            // Also load subpresentation located in master slides of sub-components
            if (presentation->GetRoot() != component || index > 0) {
                QVector<element::SElement *> components;
                component->findComponents(components);
                for (int i = 0; i < components.size(); ++i) {
                    if (components[i] != component
                            && slidesystem.isElementInSlide(*components[i], *component, index)) {
                        getComponentSlideAssets(newAssets, presentation, components[i], 0);
                        getComponentSlideAssets(newAssets, presentation, components[i], 1);
                    }
                }
            }

            if (newAssets.size())
                qCInfo(TRACE_INFO) << "Slide assets: " << newAssets;
            for (QT3DSU32 idx = 0, end = m_OrderedAssets.size(); idx < end; ++idx) {
                QString assetId = QString::fromUtf8(m_OrderedAssets[idx].first.c_str());
                if (newAssets.contains(assetId) && !GetPresentationById(qUtf8Printable(assetId))) {
                    SAssetValue &theAsset = *m_OrderedAssets[idx].second;
                    switch (theAsset.getType()) {
                    case AssetValueTypes::Presentation: {
                        AssetHandlers::handlePresentation(*this, theAsset);
                        SPresentationAsset &thePresentationAsset
                                = *theAsset.getDataPtr<SPresentationAsset>();
                        CPresentation *thePresentation = thePresentationAsset.m_Presentation;
                        if (thePresentation) {
                            QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                                    "Application: SetActivityZone")
                            thePresentation->SetActivityZone(
                                      &m_ActivityZoneManager->CreateActivityZone(*thePresentation));
                            thePresentation->SetActive(thePresentationAsset.m_Active);
                        }
                    } break;
                    case AssetValueTypes::Behavior: {
                        AssetHandlers::handleBehavior(*this, theAsset);
                    } break;
                    case AssetValueTypes::QmlPresentation: {
                        AssetHandlers::handleQmlPresentation(*m_RuntimeFactory, theAsset);
                    } break;
                        // SCXML, NoAssetValue do not need processing here
                    default:
                        break;
                    }
                }
            }
        }
    }

    void getComponentSlideAssets(QVector<QString> &initialAssets, CPresentation *presentation,
                                 TElement *component, int index)
    {
        auto &slideSystem = presentation->GetSlideSystem();
        SSlideKey key;
        key.m_Component = component;
        key.m_Index = index;
        const auto subpress = slideSystem.GetSubPresentations(key);
        for (auto pres : subpress) {
            if (!initialAssets.contains(pres))
                initialAssets.push_back(pres);
        }
    }

    void unloadComponentSlideResources(TElement *component, CPresentation *presentation, int index,
                                       const QString slideName)
    {
        if (m_RuntimeFactory->GetQt3DSRenderContext().GetBufferManager()
                .isReloadableResourcesEnabled()) {
            auto &slidesystem = presentation->GetSlideSystem();
            SSlideKey key;
            key.m_Component = component;
            key.m_Index = index;
            slidesystem.setUnloadSlide(key, true);
            if (!slidesystem.isActiveSlide(key)) {
                const QString completeName = presentation->GetName() + QLatin1Char(':')
                        + QString::fromUtf8(key.m_Component->name()) + QLatin1Char(':') + slideName;
                qCInfo(TRACE_INFO) << "Unload component slide resources: " << completeName;
                m_resourceCounter.handleUnloadSlide(completeName, key, slidesystem);

                if (m_uploadRenderTask)
                    m_uploadRenderTask->remove(m_resourceCounter.deleteSet);
            }
        }
    }

    bool LoadUIP(SPresentationAsset &inAsset,
                 NVConstDataRef<SElementAttributeReference> inExternalReferences,
                 bool initInRenderThread)
    {
        QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(), "Application: LoadUIP")
        GetMetaData();
        eastl::string theFile;
        CFileTools::CombineBaseAndRelative(GetProjectDirectory().c_str(), inAsset.m_Src.c_str(),
                                           theFile);
        // Check if the file event exists
        NVScopedRefCounted<qt3ds::render::IRefCountedInputStream> theStream
                = m_CoreFactory->GetRenderContextCore().GetInputStreamFactory().GetStreamForFile(
                    theFile.c_str());
        if (theStream) {
            theStream = nullptr;
            CPresentation *thePresentation
                    = Q3DStudio_new(CPresentation) CPresentation(inAsset.m_Id.c_str(),
                                                                 GetProjectDirectory().c_str(),
                                                                 this);
            inAsset.m_Presentation = thePresentation;
            thePresentation->SetFilePath(theFile.c_str());
            NVScopedReleasable<IUIPParser> theUIPParser(IUIPParser::Create(
                                                            theFile.c_str(), *m_MetaData,
                                                            m_CoreFactory->GetInputStreamFactory(),
                                                            m_CoreFactory->GetStringTable()));
            Q3DStudio::IScene *newScene = nullptr;
            if (theUIPParser->Load(*thePresentation, inExternalReferences, initInRenderThread)) {
                // Load the scene graph portion of the scene.
                newScene = m_RuntimeFactory->GetSceneManager().LoadScene(
                            thePresentation, theUIPParser.mPtr,
                            m_CoreFactory->GetScriptEngineQml(),
                            m_variantConfig);
            }

            if (newScene == NULL) {
                Q3DStudio_delete(thePresentation, CPresentation);
                qCWarning(qt3ds::INVALID_OPERATION)
                        << "Unable to load presentation " << theFile.c_str();
                inAsset.m_Presentation = NULL;
                return false;
            } else {
                if (inAsset.m_Id.IsValid() && m_PresentationId.empty())
                    m_PresentationId.assign(inAsset.m_Id);

                if (inAsset.m_Id.IsValid())
                    newScene->RegisterOffscreenRenderer(inAsset.m_Id);

                // Load scene master slide resources
                // Also load master slide resources of components located in the master slide
                QVector<TElement *> components;
                thePresentation->GetRoot()->findComponents(components);
                for (auto &component : qAsConst(components)) {
                    if (component->m_OnMaster || component == thePresentation->GetRoot())
                        loadComponentSlideResources(component, thePresentation, 0, "Master", true);
                }

                return true;
            }
        }
        qCWarning(qt3ds::INVALID_OPERATION) << "Unable to load presentation " << theFile.c_str();
        return false;
    }

    bool LoadUIA(IDOMReader &inReader, NVFoundationBase &fnd)
    {
            IDOMReader::Scope __preparseScope(inReader);
        {
            m_UIAFileSettings.Parse(inReader);
        }
        {
            IDOMReader::Scope __assetsScope(inReader);
            if (!inReader.MoveToFirstChild("assets")) {
                qCCritical(INVALID_OPERATION,
                           "UIA input xml doesn't contain <assets> tag; load failed");
                return false;
            }

            eastl::string pathString;

            const char8_t *initialItem = "";
            inReader.UnregisteredAtt("initial", initialItem);
            m_PresentationId.clear();
            if (!isTrivial(initialItem)) {
                if (initialItem[0] == '#')
                    ++initialItem;

                m_PresentationId.assign(initialItem);
            }
            eastl::vector<SElementAttributeReference> theUIPReferences;
            eastl::string tempString;

            for (bool success = inReader.MoveToFirstChild(); success;
                 success = inReader.MoveToNextSibling()) {
                IDOMReader::Scope __assetScope(inReader);
                const char8_t *itemId("");
                inReader.UnregisteredAtt("id", itemId);
                const char8_t *src = "";
                inReader.UnregisteredAtt("src", src);
                pathString.clear();
                if (!isTrivial(src))
                    CFileTools::CombineBaseAndRelative(m_ProjectDir.c_str(), src, pathString);

                const char8_t *assetName = inReader.GetElementName();
                if (AreEqual(assetName, "presentation")) {
                    SPresentationAsset theAsset(RegisterStr(itemId), RegisterStr(src));
                    bool activeFlag;
                    if (inReader.Att("active", activeFlag))
                        theAsset.m_Active = activeFlag;
                    RegisterAsset(theAsset);
                } else if (AreEqual(assetName, "dataInput")) {
                    DataInputDef diDef;
                    const char8_t *name = "";
                    const char8_t *type = "";
                    const char8_t *metadataStr = "";
                    diDef.value = QVariant::Invalid;
                    inReader.UnregisteredAtt("name", name);
                    inReader.UnregisteredAtt("type", type);
                    inReader.Att("min", diDef.min);
                    inReader.Att("max", diDef.max);
                    if (AreEqual(type, "Ranged Number"))
                        diDef.type = DataInOutTypeRangedNumber;
                    else if (AreEqual(type, "String"))
                        diDef.type = DataInOutTypeString;
                    else if (AreEqual(type, "Float"))
                        diDef.type = DataInOutTypeFloat;
                    else if (AreEqual(type, "Vector4"))
                        diDef.type = DataInOutTypeVector4;
                    else if (AreEqual(type, "Vector3"))
                        diDef.type = DataInOutTypeVector3;
                    else if (AreEqual(type, "Vector2"))
                        diDef.type = DataInOutTypeVector2;
                    else if (AreEqual(type, "Boolean"))
                        diDef.type = DataInOutTypeBoolean;
                    else if (AreEqual(type, "Variant"))
                        diDef.type = DataInOutTypeVariant;

                    inReader.UnregisteredAtt("metadata", metadataStr);
                    QString metaData = QString(metadataStr);
                    if (!metaData.isEmpty()) {
                        auto metadataList = metaData.split(QLatin1Char('$'));

                        if (metadataList.size() & 1) {
                            qWarning("Malformed datainput metadata for datainput %s, cannot"
                                     "parse key - value pairs. Stop parsing metadata.",
                                     qUtf8Printable(name));
                        } else {
                            for (int i = 0; i < metadataList.size(); i += 2) {
                                if (metadataList[i].isEmpty()) {
                                    qWarning("Malformed datainput metadata for datainput %s "
                                             "- metadata key empty. Stop parsing metadata.",
                                             qUtf8Printable(name));
                                    break;
                                }
                                diDef.metadata.insert(metadataList[i], metadataList[i+1]);
                            }
                        }
                    }
                    m_dataInputDefs.insert(QString::fromUtf8(name), diDef);
// #TODO Remove below once QT3DS-3510 task has been completed.
                    // By default data inputs should not have data outputs, but this is needed
                    // until editor can support configuring data nodes as in/out/in-out types
                    DataOutputDef outDef;
                    outDef.type = diDef.type;
                    outDef.value = diDef.value;
                    outDef.name = QString::fromUtf8(name);
                    m_dataOutputDefs.insert(QString::fromUtf8(name), outDef);
// #TODO Remove above once QT3DS-3510 UI change has been done
                } else if (AreEqual(assetName, "dataOutput")) {
                    DataOutputDef outDef;
                    const char8_t *name = "";
                    const char8_t *type = "";
                    outDef.value = QVariant::Invalid;
                    inReader.UnregisteredAtt("name", name);
                    inReader.UnregisteredAtt("type", type);
                    inReader.Att("min", outDef.min);
                    inReader.Att("max", outDef.max);
                    if (type) {
                        if (AreEqual(type, "Ranged Number"))
                            outDef.type = DataInOutTypeRangedNumber;
                        else if (AreEqual(type, "String"))
                            outDef.type = DataInOutTypeString;
                        else if (AreEqual(type, "Float"))
                            outDef.type = DataInOutTypeFloat;
                        else if (AreEqual(type, "Vector4"))
                            outDef.type = DataInOutTypeVector4;
                        else if (AreEqual(type, "Vector3"))
                            outDef.type = DataInOutTypeVector3;
                        else if (AreEqual(type, "Vector2"))
                            outDef.type = DataInOutTypeVector2;
                        else if (AreEqual(type, "Boolean"))
                            outDef.type = DataInOutTypeBoolean;
                        else if (AreEqual(type, "Variant"))
                            outDef.type = DataInOutTypeVariant;
                    }

                    outDef.name = QString::fromUtf8(name);
                    m_dataOutputDefs.insert(QString::fromUtf8(name), outDef);
                } else if (AreEqual(assetName, "renderplugin")) {
                    const char8_t *pluginArgs = "";
                    inReader.UnregisteredAtt("args", pluginArgs);
                    RegisterAsset(SRenderPluginAsset(RegisterStr(itemId), RegisterStr(src),
                                                     RegisterStr(pluginArgs)));
                } else if (AreEqual(assetName, "behavior")) {
                    SBehaviorAsset theAsset(RegisterStr(itemId), RegisterStr(src), 0);
                    RegisterAsset(theAsset);
                } else if (AreEqual(assetName, "presentation-qml")) {
                    const char8_t *args = "";
                    inReader.UnregisteredAtt("args", args);
                    RegisterAsset(SQmlPresentationAsset(RegisterStr(itemId), RegisterStr(src),
                                                        RegisterStr(args)));
                } else {
                    qCWarning(WARNING, "Unrecognized <assets> child %s", assetName);
                }
            }
        } // end assets scope
        const char8_t *initialScaleMode = "";
        inReader.UnregisteredAtt("scalemode", initialScaleMode);

        m_AppLoadContext
                = IAppLoadContext::CreateXMLLoadContext(*this,
                                                        initialScaleMode);
        return true;
    }

    DataInputMap &dataInputMap() override
    {
        return m_dataInputDefs;
    }

    DataOutputMap &dataOutputMap() override
    {
        return m_dataOutputDefs;
    }

    QList<QString> dataInputs() const override
    {
        return m_dataInputDefs.keys();
    }

    QList<QString> dataOutputs() const override
    {
        return m_dataOutputDefs.keys();
    }

    float dataInputMax(const QString &name) const override
    {
        return m_dataInputDefs[name].max;
    }

    float dataInputMin(const QString &name) const override
    {
        return m_dataInputDefs[name].min;
    }

    QHash<QString, QString> dataInputMetadata(const QString &name) const override
    {
        return m_dataInputDefs[name].metadata;
    }

    struct SAppXMLErrorHandler : public qt3ds::foundation::CXmlErrorHandler
    {
        NVFoundationBase &m_Foundation;
        const char8_t *m_FilePath;
        SAppXMLErrorHandler(NVFoundationBase &fnd, const char8_t *filePath)
            : m_Foundation(fnd)
            , m_FilePath(filePath)
        {
        }

        void OnXmlError(TXMLCharPtr errorName, int line, int /*column*/) override
        {
            qCWarning(INVALID_OPERATION, m_FilePath, line, "%s", errorName);
        }
    };

    bool BeginLoad(const QString &sourcePath, const QStringList &variantList) override
    {
        m_CoreFactory->GetPerfTimer().StartMeasuring();
        QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(), "Application: Begin Load")
        eastl::string directory;
        eastl::string filename;
        eastl::string extension;
        CFileTools::Split(sourcePath.toUtf8().constData(), directory, filename, extension);
        eastl::string projectDirectory(directory);

        m_ProjectDir.assign(projectDirectory.c_str());
        m_CoreFactory->AddSearchPath(projectDirectory.c_str());

        // add additional search path
        QString projectDir = CFileTools::NormalizePathForQtUsage(projectDirectory.c_str());
        if (!projectDir.startsWith(QStringLiteral(":"))) {
            eastl::string relativeProjectDir;
            CFileTools::CombineBaseAndRelative(m_ApplicationDir.c_str(), projectDirectory.c_str(),
                                               relativeProjectDir);
            m_CoreFactory->AddSearchPath(relativeProjectDir.c_str());
        }

        // For QT3DS-3353 assume project fonts are in a subdirectory relative to project.
        eastl::string projectFontDirectory = projectDirectory + "/fonts";

        NVFoundationBase &fnd(m_CoreFactory->GetFoundation());

        if (m_CoreFactory->GetRenderContextCore().getDistanceFieldRenderer()) {
            m_CoreFactory->GetRenderContextCore().getDistanceFieldRenderer()
                    ->AddProjectFontDirectory(projectFontDirectory.c_str());
        }

        if (m_CoreFactory->GetRenderContextCore().GetTextRendererCore()) {
            m_CoreFactory->GetRenderContextCore().GetTextRendererCore()->AddProjectFontDirectory(
                        projectFontDirectory.c_str());
            m_CoreFactory->GetRenderContextCore().GetTextRendererCore()->BeginPreloadFonts(
                        m_CoreFactory->GetRenderContextCore().GetThreadPool(),
                        m_CoreFactory->GetRenderContextCore().GetPerfTimer());
        }
        m_Filename = filename;
        m_variantConfig.setVariantList(variantList);
        bool retval = false;
        if (extension.comparei("uip") == 0) {
            m_PresentationId.assign("__initial");
            eastl::string relativePath = "./";
            relativePath.append(filename);
            relativePath.append(".");
            relativePath.append("uip");
            RegisterAsset(SPresentationAsset(RegisterStr(m_PresentationId.c_str()),
                                             RegisterStr(relativePath.c_str())));
            m_AppLoadContext = IAppLoadContext::CreateXMLLoadContext(*this, "");

            retval = true;
        } else if (extension.comparei("uia") == 0) {
            CFileSeekableIOStream inputStream(sourcePath, FileReadFlags());
            if (inputStream.IsOpen()) {
                NVScopedRefCounted<IStringTable> strTable(
                            IStringTable::CreateStringTable(fnd.getAllocator()));
                NVScopedRefCounted<IDOMFactory> domFactory(
                            IDOMFactory::CreateDOMFactory(fnd.getAllocator(), strTable));
                SAppXMLErrorHandler errorHandler(fnd, sourcePath.toUtf8().constData());
                eastl::pair<SNamespacePairNode *, SDOMElement *> readResult =
                        CDOMSerializer::Read(*domFactory, inputStream, &errorHandler);
                if (!readResult.second) {
                    qCCritical(INVALID_PARAMETER, "%s doesn't appear to be valid xml",
                               sourcePath.toUtf8().constData());
                } else {
                    NVScopedRefCounted<IDOMReader> domReader = IDOMReader::CreateDOMReader(
                                fnd.getAllocator(), *readResult.second, strTable, domFactory);
                    if (m_visitor)
                        m_visitor->visit(sourcePath.toUtf8().constData());
                    retval = LoadUIA(*domReader, fnd);
                }
            } else {
                qCCritical(INVALID_PARAMETER, "Unable to open input file %s",
                           sourcePath.toUtf8().constData());
            }
        } else {
            QT3DS_ASSERT(false);
        }
        return retval;
    }

    void EndLoad() override
    {
        if (m_AppLoadContext)
            m_AppLoadContext->EndLoad();
    }

    void RunAllRunnables()
    {
        {
            Mutex::ScopedLock __locker(m_RunnableMutex);
            m_MainThreadRunnables = m_ThreadRunnables;
            m_ThreadRunnables.clear();
        }
        for (QT3DSU32 idx = 0, end = m_MainThreadRunnables.size(); idx < end; ++idx)
            m_MainThreadRunnables[idx]->Run();
        m_MainThreadRunnables.clear();
    }

    bool HasCompletedLoading() override
    {
        RunAllRunnables();
        if (m_AppLoadContext)
            return m_AppLoadContext->HasCompletedLoading();

        return true;
    }

    bool createSuccessful() override
    {
        return m_createSuccessful;
    }

    bool presentationComponentSlide(const QString &elementPath,
                                    Q3DStudio::CPresentation *&presentation,
                                    TElement *&component,
                                    QString &slideName,
                                    int &index)
    {
        presentation = GetPrimaryPresentation();
        slideName = elementPath;
        QString componentName;
        if (elementPath.contains(QLatin1Char(':'))) {
            // presentation : component : slide
            QStringList splits = elementPath.split(QLatin1Char(':'));
            if (splits.size() == 3) {
                presentation = GetPresentationById(qPrintable(splits[0]));
                componentName = splits[1];
                slideName = splits[2];
            } else {
                componentName = splits[0];
                slideName = splits[1];
            }
            // else assume main presentation and component:slide
        }
        component = presentation->GetRoot();
        if (!componentName.isNull() && componentName != component->name()) {
            component = CQmlElementHelper::GetElement(*this, presentation,
                                                      qPrintable(componentName), nullptr);
        }
        if (!component) {
            qCWarning(WARNING) << "Could not find slide: " << elementPath;
            return false;
        }
        ISlideSystem &s = presentation->GetSlideSystem();
        index = s.FindSlide(*component, qPrintable(slideName));
        if (index == 0xFF) {
            qCWarning(WARNING) << "Could not find slide: " << elementPath;
            return false;
        }
        return true;
    }

    void preloadSlide(const QString &slide) override
    {
        CPresentation *pres = nullptr;
        TElement *component = nullptr;
        QString slideName;
        int index;
        if (presentationComponentSlide(slide, pres, component, slideName, index))
            loadComponentSlideResources(component, pres, index, slideName, false);
    }

    void unloadSlide(const QString &slide) override
    {
        CPresentation *pres = nullptr;
        TElement *component = nullptr;
        QString slideName;
        int index;
        if (presentationComponentSlide(slide, pres, component, slideName, index))
            unloadComponentSlideResources(component, pres, index, slideName);
    }

    void setDelayedLoading(bool enable)
    {
        m_RuntimeFactory->GetQt3DSRenderContext().GetBufferManager()
                .enableReloadableResources(enable);
    }

    void ComponentSlideEntered(Q3DStudio::CPresentation *presentation,
                               Q3DStudio::TElement *component,
                               const QString &elementPath, int slideIndex,
                               const QString &slideName) override
    {
        loadComponentSlideResources(component, presentation, slideIndex, slideName, true);
    }

    void ComponentSlideExited(Q3DStudio::CPresentation *presentation,
                              Q3DStudio::TElement *component,
                              const QString &elementPath, int slideIndex,
                              const QString &slideName) override
    {
        unloadComponentSlideResources(component, presentation, slideIndex, slideName);
    }

    // will force loading to end if endLoad hasn't been called yet.  Will fire off loading
    // of resources that need to be uploaded to opengl.  Maintains reference to runtime factory
    IApplication &CreateApplication(Q3DStudio::CInputEngine &inInputEngine,
                                    Q3DStudio::IAudioPlayer *inAudioPlayer,
                                    Q3DStudio::IRuntimeFactory &inFactory,
                                    const QByteArray &shaderCache,
                                    bool initInRenderThread,
                                    QString &shaderCacheErrors) override
    {
        {
            QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                    "Application: Initialize Graphics")

            {
                QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(), "Application: EndLoad")
                EndLoad();
            }
            m_InputEnginePtr = &inInputEngine;
            m_RuntimeFactory = inFactory;

#ifdef QT3DS_ENABLE_PERF_LOGGING
            EnableProfileLogging();
#endif
            {
                QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                        "Application: Load Context Graphics Initialized")
                if (m_AppLoadContext)
                    m_createSuccessful = m_AppLoadContext->OnGraphicsInitialized(
                                inFactory, initInRenderThread);
                // Guarantees the end of the multithreaded access to the various components
                m_AppLoadContext = NULL;
                if (!m_createSuccessful)
                    return *this;
            }

            {
                QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                        "Application: End Font Preload")
                if (m_CoreFactory->GetRenderContextCore().GetTextRendererCore())
                    m_CoreFactory->GetRenderContextCore()
                            .GetTextRendererCore()
                            ->EndPreloadFonts();
            }

            RunAllRunnables();
            // Moving set application to the end ensures that the application load context is not
            // accessing
            // the lua state in another thread while we are calling set application.  This
            // apparently may cause
            // the call to set application to fail miserably.
            m_RuntimeFactory->SetApplication(this);
            m_RuntimeFactory->GetStringTable().DisableMultithreadedAccess();

            for (QT3DSU32 idx = 0, end = m_OrderedAssets.size(); idx < end; ++idx) {
                if (m_OrderedAssets[idx].second->getType() == AssetValueTypes::Presentation) {
                    SPresentationAsset &theAsset(
                                *m_OrderedAssets[idx].second->getDataPtr<SPresentationAsset>());
                    CPresentation *thePresentation = theAsset.m_Presentation;
                    if (thePresentation) {
                        QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                                "Application: SetActivityZone")
                        thePresentation->SetActivityZone(
                                    &m_ActivityZoneManager->CreateActivityZone(*thePresentation));
                        thePresentation->SetActive(theAsset.m_Active);
                    }
                }
            }

            inInputEngine.SetApplication(this);
        }
        SApplicationSettings finalSettings(/*m_CommandLineSettings, */m_UIAFileSettings);
        if (finalSettings.m_LayerCacheEnabled.hasValue()) {
            inFactory.GetQt3DSRenderContext().GetRenderer().EnableLayerCaching(
                        *finalSettings.m_LayerCacheEnabled);
        }


        if (!shaderCache.isEmpty()) {
            QString errors;
            inFactory.GetQt3DSRenderContext().GetShaderCache().importShaderCache(shaderCache, errors);
            if (!errors.isEmpty())
                shaderCacheErrors = errors;
        }

        m_AudioPlayer.SetPlayer(inAudioPlayer);

        auto &rc = m_RuntimeFactory->GetQt3DSRenderContext();
        m_uploadRenderTask = QT3DS_NEW(m_CoreFactory->GetFoundation().getAllocator(),
                                       STextureUploadRenderTask(rc.GetImageBatchLoader(),
                                            rc.GetBufferManager(),
                                            rc.GetRenderContext().GetRenderContextType(),
                                            GetPrimaryPresentation()->GetScene()->preferKtx(),
                                            GetPrimaryPresentation()->GetScene()
                                                                ->flipCompressedTextures()));
        m_uploadRenderTask->add(m_createSet, true);
        m_RuntimeFactory->GetQt3DSRenderContext().GetRenderList()
                                                                .AddRenderTask(*m_uploadRenderTask);
        m_createSet.clear();
        return *this;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    //	Getters/Setters
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    CRegisteredString RegisterStr(const char8_t *inStr)
    {
        return m_CoreFactory->GetStringTable().RegisterStr(inStr);
    }

    // The directory that contains the executable and the root resource path
    CRegisteredString GetApplicationDirectory() const override
    {
        return const_cast<SApp &>(*this).m_CoreFactory->GetStringTable().RegisterStr(
                    m_ApplicationDir.c_str());
    }
    // Directory that contained the XIF file.
    CRegisteredString GetProjectDirectory() const override
    {
        QT3DS_ASSERT(m_ProjectDir.size());
        return const_cast<SApp &>(*this).m_CoreFactory->GetStringTable().RegisterStr(
                    m_ProjectDir.c_str());
    }

    CRegisteredString GetDllDir() const override
    {
        if (m_DLLDirectory.size()) {
            return const_cast<SApp &>(*this).m_CoreFactory->GetStringTable().RegisterStr(
                        m_DLLDirectory.c_str());
        }
        return CRegisteredString();
    }

    void SetDllDir(const char *inDllDir) override
    {
        m_DLLDirectory.assign(nonNull(inDllDir));
        m_CoreFactory->SetDllDir(inDllDir);
    }

    Q3DStudio::IRuntimeFactory &GetRuntimeFactory() const override { return *m_RuntimeFactory.mPtr; }
    Q3DStudio::IRuntimeFactoryCore &GetRuntimeFactoryCore() override { return *m_CoreFactory; }

    Q3DStudio::CPresentation *m_primaryPresentation = nullptr;
    Q3DStudio::CPresentation *GetPrimaryPresentation() override
    {
        if (!m_primaryPresentation)
            m_primaryPresentation = GetPresentationById(m_PresentationId.c_str());
        return m_primaryPresentation;
    }

    virtual Q3DStudio::CPresentation *LoadAndGetPresentationById(const QString &inId) override
    {
        return GetPresentationById(inId, true);
    }

    Q3DStudio::CPresentation *GetPresentationById(const QString &inId, bool load = false)
    {
        if (!inId.isEmpty()) {
            TIdAssetMap::iterator iter
                    = m_AssetMap.find(m_CoreFactory->GetStringTable().RegisterStr(inId));
            if (iter != m_AssetMap.end()
                    && iter->second->getType() == AssetValueTypes::Presentation) {
                CPresentation *ret = iter->second->getData<SPresentationAsset>().m_Presentation;
                if (!ret && load) {
                    AssetHandlers::handlePresentation(*this, *iter->second);
                    SPresentationAsset &thePresentationAsset
                            = *iter->second->getDataPtr<SPresentationAsset>();
                    CPresentation *thePresentation = thePresentationAsset.m_Presentation;
                    if (thePresentation) {
                        QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(),
                                                "Application: SetActivityZone")
                        thePresentation->SetActivityZone(
                                  &m_ActivityZoneManager->CreateActivityZone(*thePresentation));
                        thePresentation->SetActive(thePresentationAsset.m_Active);
                    }
                }
                return iter->second->getData<SPresentationAsset>().m_Presentation;
            }
        }
        return NULL;
    }

    // Returns a list of all presentations in the application
    // The primary presentation is returned at index 0
    QList<Q3DStudio::CPresentation *> GetPresentationList() override
    {
        QList<Q3DStudio::CPresentation *> list;
        auto &stringTable = m_CoreFactory->GetStringTable();
        auto presentationId = stringTable.RegisterStr(m_PresentationId.c_str());
        for (TIdAssetMap::iterator iter = m_AssetMap.begin(); iter != m_AssetMap.end(); ++iter) {
            if (iter->second->getType() == AssetValueTypes::Presentation) {
                Q3DStudio::CPresentation *presentation
                        = iter->second->getData<SPresentationAsset>().m_Presentation;
                if (presentation) {
                    if (iter->first == presentationId)
                        list.prepend(presentation);
                    else
                        list.append(presentation);
                }
            }
        }
        return list;
    }

    template <typename TAssetType>
    void RegisterAsset(const TAssetType &inAsset)
    {
        NVScopedRefCounted<SRefCountedAssetValue> theValue(
                    QT3DS_NEW(m_CoreFactory->GetFoundation().getAllocator(),
                              SRefCountedAssetValue(m_CoreFactory->GetFoundation(), inAsset)));
        if (inAsset.m_Id.IsValid())
            m_AssetMap.insert(eastl::make_pair(inAsset.m_Id, theValue));

        m_OrderedAssets.push_back(eastl::make_pair(inAsset.m_Id, theValue));

        if (m_visitor) {
            m_visitor->visit(inAsset.Type(), inAsset.m_Id.c_str(), inAsset.m_Src.c_str(),
                             inAsset.m_Args.c_str());
        }
    }

    THashValue HashString(const char *inStr) override
    {
        if (inStr == NULL)
            inStr = "";
        THashValue retval = CHash::HashString(inStr);
        eastl::pair<THashStrMap::iterator, bool> insertResult
                = m_HashStrMap.insert(eastl::make_pair(retval, CRegisteredString()));
        if (insertResult.second)
            insertResult.first->second = m_CoreFactory->GetStringTable().RegisterStr(inStr);
        return retval;
    }

    const char *ReverseHash(THashValue theValue) override
    {
        THashStrMap::iterator find = m_HashStrMap.find(theValue);
        if (find != m_HashStrMap.end())
            return find->second.c_str();
        return "";
    }

    void SetFrameCount(Q3DStudio::INT32 inFrameCount) override { m_FrameCount = inFrameCount; }

    Q3DStudio::INT32 GetFrameCount() override { return m_FrameCount; }

    void SetTimeMilliSecs(Q3DStudio::INT64 inMilliSecs) override { m_ManualTime = inMilliSecs; }

    Q3DStudio::INT64 GetTimeMilliSecs() override
    {
        return m_ManualTime == 0 ? m_Timer.GetTimeMilliSecs() : m_ManualTime;
    }

    void ResetTime() override
    {
        m_Timer.Reset();
        m_ManualTime = 0;
    }

    Q3DStudio::CInputEngine &GetInputEngine() override
    {
        QT3DS_ASSERT(m_InputEnginePtr);
        return *m_InputEnginePtr;
    }

    Q3DStudio::IRuntimeMetaData &GetMetaData() override
    {
        QT3DS_PERF_SCOPED_TIMER(m_CoreFactory->GetPerfTimer(), "Application: GetMetaData")
        if (!m_MetaData) {
            m_MetaData = &IRuntimeMetaData::Create(m_CoreFactory->GetInputStreamFactory());
            if (!m_MetaData) {
                qCCritical(qt3ds::INVALID_OPERATION)
                        << "IRuntimeMetaData::Create: Failed to create meta data";
            }
        }
        return *m_MetaData;
    }

    IActivityZoneManager &GetActivityZoneManager() override { return *m_ActivityZoneManager; }

    IElementAllocator &GetElementAllocator() override { return *m_ElementAllocator; }

    Q3DStudio::UINT32 GetHandleForElement(Q3DStudio::TElement *inElement) override
    {
        return inElement->GetHandle();
    }

    Q3DStudio::TElement *GetElementByHandle(Q3DStudio::UINT32 inHandle) override
    {
        return GetElementAllocator().FindElementByHandle(inHandle);
    }

    void OutputPerfLoggingData() override {
        m_CoreFactory->GetPerfTimer().OutputTimerData();
        m_CoreFactory->GetPerfTimer().ResetTimerData();
    }
};

struct SXMLLoader : public IAppLoadContext
{
    SApp &m_App;
    eastl::string m_ScaleMode;
    QT3DSI32 mRefCount;

    SXMLLoader(SApp &inApp, const char8_t *sc)
        : m_App(inApp)
        , m_ScaleMode(nonNull(sc))
        , mRefCount(0)
    {
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE(m_App.m_CoreFactory->GetFoundation().getAllocator())

    void EndLoad() override {}

    bool HasCompletedLoading() override { return true; }

    bool OnGraphicsInitialized(IRuntimeFactory &inFactory, bool initInRenderThread) override
    {
        eastl::string tempString;
        const bool delayedLoading = inFactory.GetQt3DSRenderContext().GetBufferManager()
                .isReloadableResourcesEnabled();

        // First load the initial presentation
        CAppStr initial = m_App.m_PresentationId;
        auto initialStr = this->m_App.m_CoreFactory->GetStringTable().RegisterStr(initial.c_str());
        if (initial.empty()) {
            for (QT3DSU32 idx = 0, end = m_App.m_OrderedAssets.size(); idx < end; ++idx) {
                SAssetValue &theAsset = *m_App.m_OrderedAssets[idx].second;
                if (theAsset.getType() == AssetValueTypes::Presentation) {
                    initial.assign(theAsset.getDataPtr<SPresentationAsset>()->m_Id.c_str());
                    break;
                }
            }
        }
        // Do we even have a presentation
        if (initial.empty())
            return false;

        // Load it
        for (QT3DSU32 idx = 0, end = m_App.m_OrderedAssets.size(); idx < end; ++idx) {
            if (m_App.m_OrderedAssets[idx].first == initialStr) {
                SAssetValue &theAsset = *m_App.m_OrderedAssets[idx].second;
                AssetHandlers::handlePresentation(m_App, theAsset, initInRenderThread);
                break;
            }
        }

        QVector<QString> initialAssets;
        CPresentation *mainPresentation = m_App.GetPrimaryPresentation();
        QVector<element::SElement*> components;
        mainPresentation->GetRoot()->findComponents(components);

        // Load subpresentations of components under master slide
        for (int i = 0; i < components.size(); ++i) {
            if (components[i]->m_OnMaster || components[i] == mainPresentation->GetRoot()) {
                m_App.getComponentSlideAssets(initialAssets, mainPresentation, components[i], 0);
                m_App.getComponentSlideAssets(initialAssets, mainPresentation, components[i], 1);
            }
        }

        if (!delayedLoading || (m_App.m_OrderedAssets.size() > 1 && initialAssets.size() > 0)) {
            for (QT3DSU32 idx = 0, end = m_App.m_OrderedAssets.size(); idx < end; ++idx) {
                QString assetId = QString::fromUtf8(m_App.m_OrderedAssets[idx].first.c_str());
                if (!m_App.GetPresentationById(qPrintable(assetId))
                        && (m_App.m_OrderedAssets[idx].first != initialStr
                        && (initialAssets.contains(assetId) || !delayedLoading))) {
                    SAssetValue &theAsset = *m_App.m_OrderedAssets[idx].second;
                    switch (theAsset.getType()) {
                    case AssetValueTypes::Presentation:
                        AssetHandlers::handlePresentation(m_App, theAsset);
                        break;
                    case AssetValueTypes::Behavior:
                        AssetHandlers::handleBehavior(m_App, theAsset);
                        break;
                    case AssetValueTypes::QmlPresentation:
                        AssetHandlers::handleQmlPresentation(inFactory, theAsset);
                        break;
                        // SCXML, NoAssetValue do not need processing here
                    default:
                        break;
                    }
                }
            }
        }
        if (m_ScaleMode.empty() == false) {
            const char8_t *initialScaleMode(m_ScaleMode.c_str());
            // Force loading to finish here, just like used to happen.
            if (AreEqual(initialScaleMode, "center")) {
                inFactory.GetQt3DSRenderContext().SetScaleMode(qt3ds::render::ScaleModes::ExactSize);
            } else if (AreEqual(initialScaleMode, "fit")) {
                inFactory.GetQt3DSRenderContext().SetScaleMode(qt3ds::render::ScaleModes::ScaleToFit);
            } else if (AreEqual(initialScaleMode, "fill")) {
                inFactory.GetQt3DSRenderContext().SetScaleMode(qt3ds::render::ScaleModes::ScaleToFill);
            } else {
                qCCritical(INVALID_PARAMETER, "Unrecognized scale mode attribute value: ",
                           initialScaleMode);
            }
        }
        return true;
    }

    virtual void OnFirstRender() {}
};

IAppLoadContext &IAppLoadContext::CreateXMLLoadContext(
        SApp &inApp, const char8_t *inScaleMode)
{
    return *QT3DS_NEW(inApp.m_CoreFactory->GetFoundation().getAllocator(),
                      SXMLLoader)(inApp, inScaleMode);
}

CAppStr::CAppStr(NVAllocatorCallback &alloc, const char8_t *inStr)
    : TBase(inStr, ForwardingAllocator(alloc, "CAppStr"))
{
}

CAppStr::CAppStr(const CAppStr &inOther)
    : TBase(inOther)
{
}

CAppStr::CAppStr()
    : TBase()
{
}

CAppStr &CAppStr::operator=(const CAppStr &inOther)
{
    TBase::operator=(inOther);
    return *this;
}

IApplication &IApplication::CreateApplicationCore(Q3DStudio::IRuntimeFactoryCore &inFactory,
                                                  const char8_t *inApplicationDirectory)
{
    return *QT3DS_NEW(inFactory.GetFoundation().getAllocator(), SApp)(inFactory,
                                                                      inApplicationDirectory);
}

bool AssetHandlers::handlePresentation(SApp &app, SAssetValue &asset, bool initInRenderThread)
{
    QT3DS_PERF_SCOPED_TIMER(app.m_CoreFactory->GetPerfTimer(), "AssetHandlers: handlePresentation")
    eastl::string thePathStr;

    CFileTools::CombineBaseAndRelative(app.GetProjectDirectory().c_str(),
                                       asset.GetSource(), thePathStr);

    QDir::addSearchPath(QStringLiteral("qt3dstudio"),
                        QFileInfo(QString(thePathStr.c_str()))
                        .absoluteDir().absolutePath());
    SPresentationAsset &thePresentationAsset
            = *asset.getDataPtr<SPresentationAsset>();
    eastl::vector<SElementAttributeReference> theUIPReferences;

    if (!app.LoadUIP(thePresentationAsset,
                       toConstDataRef(theUIPReferences.data(),
                                      (QT3DSU32)theUIPReferences.size()), initInRenderThread)) {
        qCCritical(INVALID_OPERATION, "Unable to load presentation %s",
                   thePathStr.c_str());
        return false;
    }
    return true;
}

bool AssetHandlers::handleBehavior(SApp &app, SAssetValue &asset)
{
    SBehaviorAsset &theBehaviorAsset = *asset.getDataPtr<SBehaviorAsset>();
    Q3DStudio::INT32 scriptId
            = app.m_CoreFactory->GetScriptEngineQml().InitializeApplicationBehavior(
                theBehaviorAsset.m_Src);
    if (scriptId == 0) {
        qCCritical(INVALID_OPERATION, "Unable to load application behavior %s",
                   theBehaviorAsset.m_Src.c_str());
        return false;
    } else {
        theBehaviorAsset.m_Handle = scriptId;
        app.m_Behaviors.push_back(eastl::make_pair(theBehaviorAsset, false));
    }
    return true;
}

bool AssetHandlers::handleQmlPresentation(IRuntimeFactory &factory, SAssetValue &asset)
{
    SQmlPresentationAsset &qmlAsset = *asset.getDataPtr<SQmlPresentationAsset>();
    factory.GetSceneManager().LoadQmlStreamerPlugin(qmlAsset.m_Id);
    return true;
}

// Checks if the event is one that can cause picking
bool IApplication::isPickingEvent(TEventCommandHash event)
{
    return (event == ON_MOUSEDOWN
            || event == ON_MOUSEUP
            || event == ON_MIDDLEMOUSEDOWN
            || event == ON_MIDDLEMOUSEUP
            || event == ON_RIGHTMOUSEDOWN
            || event == ON_RIGHTMOUSEUP
            || event == ON_MOUSECLICK
            || event == ON_MIDDLEMOUSECLICK
            || event == ON_RIGHTMOUSECLICK
            || event == ON_MOUSEOVER
            || event == ON_MOUSEOUT
            || event == ON_GROUPEDMOUSEOVER
            || event == ON_GROUPEDMOUSEOUT);
}

QDebug operator<<(QDebug debug, const DataInOutAttribute &value)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "DataInOutAttribute(";
    debug.nospace() << "elementPath:" << value.elementPath;
    debug.nospace() << ", attributeNames: {";
    for (auto name : value.attributeName)
        debug << QString::fromUtf8(name);

    debug.nospace() << "}, propertyType:" << value.propertyType;
    return debug;
}

QDebug operator<<(QDebug debug, const DataInOutType &value)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "DataInOutType::";
    switch (value) {
    case DataInOutType::DataInOutTypeInvalid:
        debug.nospace() << "DataInOutTypeInvalid";
        break;
    case DataInOutType::DataInOutTypeRangedNumber:
        debug.nospace() << "DataInOutTypeRangedNumber";
        break;
    case DataInOutType::DataInOutTypeString:
        debug.nospace() << "DataInOutTypeString";
        break;
    case DataInOutType::DataInOutTypeFloat:
        debug.nospace() << "DataInOutTypeFloat";
        break;
    case DataInOutType::DataInOutTypeBoolean:
        debug.nospace() << "DataInOutTypeBoolean";
        break;
    case DataInOutType::DataInOutTypeVector4:
        debug.nospace() << "DataInOutTypeVector4";
        break;
    case DataInOutType::DataInOutTypeVector3:
        debug.nospace() << "DataInOutTypeVector3";
        break;
    case DataInOutType::DataInOutTypeVector2:
        debug.nospace() << "DataInOutTypeVector2";
        break;
    case DataInOutType::DataInOutTypeVariant:
        debug.nospace() << "DataInOutTypeVariant";
        break;
    default:
        debug.nospace() << "UNKNOWN";
    }
    return debug;
}

QDebug operator<<(QDebug debug, const DataInputValueRole &value)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "DataInputValueRole::";
    switch (value) {
    case DataInputValueRole::Value:
        debug.nospace() << "Value";
        break;
    case DataInputValueRole::Min:
        debug.nospace() << "Min";
        break;
    case DataInputValueRole::Max:
        debug.nospace() << "Max";
        break;
    default:
        debug.nospace() << "UNKNOWN";
    }
    return debug;
}

// TODO: optionally print out also metadata, but note that it is not
// relevant for any runtime or editor -side code debugging (strictly user-side
// information).
QDebug operator<<(QDebug debug, const DataInputDef &value)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "DataInputDef(";
    debug.nospace() << "type:" << value.type;
    debug.nospace() << ", controlledAttributes: {";
    for (auto attr : value.controlledAttributes)
        debug << attr;

    debug.nospace() << "}, min:" << value.min;
    debug.nospace() << ", min:" << value.min << ", max:" << value.max;
    debug.nospace() << ", value:" << value.value;

    debug.nospace() << "})";
    return debug;
}

QDebug operator<<(QDebug debug, const DataOutputDef &value)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "DataOutputDef(";
    debug.nospace() << "name:" << value.name << ", type:" << value.type;
    debug.nospace() << ", observedHandle:" << value.observedHandle;
    debug.nospace() << ", min:" << value.min << ", max:" << value.max;
    debug.nospace() << ", timelineComponent:" << value.timelineComponent << ")";
    return debug;
}