6#include <private/qqmlglobal_p.h>
7#include <private/qsgmaterialshader_p.h>
8#include <private/qsgrenderer_p.h>
9#include <private/qquickitem_p.h>
10#include <private/qquickwindow_p.h>
11#include <QtCore/private/qnativeinterface_p.h>
14#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) && defined(__GLIBC__
)
15#define CAN_BACKTRACE_EXECINFO
18#if defined(Q_OS_APPLE)
19#define CAN_BACKTRACE_EXECINFO
22#if defined(QT_NO_DEBUG)
23#undef CAN_BACKTRACE_EXECINFO
26#if defined(CAN_BACKTRACE_EXECINFO)
32Q_GLOBAL_STATIC(QSet<QSGTexture *>, qsg_valid_texture_set)
33Q_GLOBAL_STATIC(QMutex, qsg_valid_texture_mutex)
38bool operator==(
const QSGSamplerDescription &a,
const QSGSamplerDescription &b)
noexcept
40 return a.filtering == b.filtering
41 && a.mipmapFiltering == b.mipmapFiltering
42 && a.horizontalWrap == b.horizontalWrap
43 && a.verticalWrap == b.verticalWrap
44 && a.anisotropylevel == b.anisotropylevel;
47bool operator!=(
const QSGSamplerDescription &a,
const QSGSamplerDescription &b)
noexcept
52size_t qHash(
const QSGSamplerDescription &s, size_t seed)
noexcept
54 const int f = s.filtering;
55 const int m = s.mipmapFiltering;
56 const int w = s.horizontalWrap;
57 const int a = s.anisotropylevel;
58 return (((f & 7) << 24) | ((m & 7) << 16) | ((w & 7) << 8) | (a & 7)) ^ seed;
61QSGSamplerDescription QSGSamplerDescription::fromTexture(QSGTexture *t)
63 QSGSamplerDescription s;
64 s.filtering = t->filtering();
65 s.mipmapFiltering = t->mipmapFiltering();
66 s.horizontalWrap = t->horizontalWrapMode();
67 s.verticalWrap = t->verticalWrapMode();
68 s.anisotropylevel = t->anisotropyLevel();
72QSGTexturePrivate::QSGTexturePrivate(QSGTexture *t)
74 , filteringChanged(
false)
75 , anisotropyChanged(
false)
76 , horizontalWrap(QSGTexture::ClampToEdge)
77 , verticalWrap(QSGTexture::ClampToEdge)
78 , mipmapMode(QSGTexture::None)
79 , filterMode(QSGTexture::Nearest)
80 , anisotropyLevel(QSGTexture::AnisotropyNone)
82 , m_openglTextureAccessor(t)
85 , m_d3d11TextureAccessor(t)
86 , m_d3d12TextureAccessor(t)
89 , m_metalTextureAccessor(t)
92 , m_vulkanTextureAccessor(t)
102static int qt_debug_texture_count = 0;
104#if (defined(Q_OS_LINUX) || defined (Q_OS_APPLE)) && !defined(Q_OS_ANDROID)
105DEFINE_BOOL_CONFIG_OPTION(qmlDebugLeakBacktrace, QML_DEBUG_LEAK_BACKTRACE)
107#define BACKTRACE_SIZE 20
108class SGTextureTraceItem
111 void *backTrace[BACKTRACE_SIZE];
112 size_t backTraceSize;
115static QHash<QSGTexture*, SGTextureTraceItem*> qt_debug_allocated_textures;
120 qCDebug(lcQsgLeak,
"Number of leaked textures: %i", qt_debug_texture_count);
121 qt_debug_texture_count = -1;
123#if defined(CAN_BACKTRACE_EXECINFO)
124 if (qmlDebugLeakBacktrace()) {
125 while (!qt_debug_allocated_textures.isEmpty()) {
126 QHash<QSGTexture*, SGTextureTraceItem*>::Iterator it = qt_debug_allocated_textures.begin();
127 QSGTexture* texture = it.key();
128 SGTextureTraceItem* item = it.value();
130 qt_debug_allocated_textures.erase(it);
132 qDebug() <<
"------";
133 qDebug() <<
"Leaked" << texture <<
"backtrace:";
135 char** symbols = backtrace_symbols(item->backTrace, item->backTraceSize);
138 for (
int i=0; i<(
int) item->backTraceSize; i++)
139 qDebug(
"Backtrace <%02d>: %s", i, symbols[i]);
143 qDebug() <<
"------";
153#if defined(CAN_BACKTRACE_EXECINFO)
154 if (qmlDebugLeakBacktrace()) {
155 SGTextureTraceItem* item =
new SGTextureTraceItem;
156 item->backTraceSize = backtrace(item->backTrace, BACKTRACE_SIZE);
157 qt_debug_allocated_textures.insert(texture, item);
163 ++qt_debug_texture_count;
165 static bool atexit_registered =
false;
166 if (!atexit_registered) {
167 atexit(qt_debug_print_texture_count);
168 atexit_registered =
true;
174#if defined(CAN_BACKTRACE_EXECINFO)
175 if (qmlDebugLeakBacktrace()) {
176 SGTextureTraceItem* item = qt_debug_allocated_textures.value(texture, 0);
178 qt_debug_allocated_textures.remove(texture);
186 --qt_debug_texture_count;
188 if (qt_debug_texture_count < 0)
189 qDebug(
"Texture destroyed after qt_debug_print_texture_count() was called.");
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
266
267
268
269
270
271
272
273
274
275
276
277
278
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
300
301
302QSGTexture::QSGTexture()
303 : QObject(*(
new QSGTexturePrivate(
this)))
306 if (lcQsgLeak().isDebugEnabled())
307 qt_debug_add_texture(
this);
309 QMutexLocker locker(qsg_valid_texture_mutex());
310 qsg_valid_texture_set()->insert(
this);
315
316
317QSGTexture::QSGTexture(QSGTexturePrivate &dd)
321 if (lcQsgLeak().isDebugEnabled())
322 qt_debug_add_texture(
this);
324 QMutexLocker locker(qsg_valid_texture_mutex());
325 qsg_valid_texture_set()->insert(
this);
330
331
332QSGTexture::~QSGTexture()
335 if (lcQsgLeak().isDebugEnabled())
336 qt_debug_remove_texture(
this);
338 QMutexLocker locker(qsg_valid_texture_mutex());
339 qsg_valid_texture_set()->remove(
this);
344
345
346
347
348
349
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
377QSGTexture *QSGTexture::removedFromAtlas(QRhiResourceUpdateBatch *resourceUpdates)
const
379 Q_UNUSED(resourceUpdates);
380 Q_ASSERT_X(!isAtlasTexture(),
"QSGTexture::removedFromAtlas()",
"Called on a non-atlas texture");
385
386
387
388
389bool QSGTexture::isAtlasTexture()
const
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
420
421
422
423
426
427
428
429
430
431
432QRectF QSGTexture::normalizedTextureSubRect()
const
434 return QRectF(0, 0, 1, 1);
438
439
440
441
444
445
446
447
451
452
453
454
455
456
457void QSGTexture::setMipmapFiltering(Filtering filter)
460 if (d->mipmapMode != (uint) filter) {
461 d->mipmapMode = filter;
462 d->filteringChanged =
true;
467
468
469QSGTexture::Filtering QSGTexture::mipmapFiltering()
const
471 return (QSGTexture::Filtering) d_func()->mipmapMode;
476
477
478void QSGTexture::setFiltering(QSGTexture::Filtering filter)
481 if (d->filterMode != (uint) filter) {
482 d->filterMode = filter;
483 d->filteringChanged =
true;
488
489
490QSGTexture::Filtering QSGTexture::filtering()
const
492 return (QSGTexture::Filtering) d_func()->filterMode;
496
497
498
499
500
501
502
503
504
505void QSGTexture::setAnisotropyLevel(AnisotropyLevel level)
508 if (d->anisotropyLevel != (uint) level) {
509 d->anisotropyLevel = level;
510 d->anisotropyChanged =
true;
515
516
517
518
519QSGTexture::AnisotropyLevel QSGTexture::anisotropyLevel()
const
521 return (QSGTexture::AnisotropyLevel) d_func()->anisotropyLevel;
527
528
530void QSGTexture::setHorizontalWrapMode(WrapMode hwrap)
533 if ((uint) hwrap != d->horizontalWrap) {
534 d->horizontalWrap = hwrap;
535 d->wrapChanged =
true;
540
541
542QSGTexture::WrapMode QSGTexture::horizontalWrapMode()
const
544 return (QSGTexture::WrapMode) d_func()->horizontalWrap;
550
551
552void QSGTexture::setVerticalWrapMode(WrapMode vwrap)
555 if ((uint) vwrap != d->verticalWrap) {
556 d->verticalWrap = vwrap;
557 d->wrapChanged =
true;
562
563
564QSGTexture::WrapMode QSGTexture::verticalWrapMode()
const
566 return (QSGTexture::WrapMode) d_func()->verticalWrap;
570
571
572
573
574
575
576
577
578
579
580
581
582
583QRhiTexture *QSGTexture::rhiTexture()
const
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603void QSGTexture::commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
606 Q_UNUSED(resourceUpdates);
609bool QSGTexturePrivate::hasDirtySamplerOptions()
const
611 return wrapChanged || filteringChanged || anisotropyChanged;
614void QSGTexturePrivate::resetDirtySamplerOptions()
616 wrapChanged = filteringChanged = anisotropyChanged =
false;
620
621
622
623
624
625
626
627
628
629
633
634
635
636
637
638
639
640
641
642
643
644
647
648
649QSGDynamicTexture::QSGDynamicTexture(QSGTexturePrivate &dd)
655
656
657QSGDynamicTexture::~QSGDynamicTexture()
661
662
663
664
665
666
667
668
669
670
671
672
673
674
677#if QT_CONFIG(opengl) || defined(Q_QDOC)
678namespace QNativeInterface {
680
681
682
683
684
685
686
687
690
691
692
694QT_DEFINE_NATIVE_INTERFACE(QSGOpenGLTexture);
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
722QSGTexture *QSGOpenGLTexture::fromNative(GLuint textureId,
723 QQuickWindow *window,
725 QQuickWindow::CreateTextureOptions options)
727 return QQuickWindowPrivate::get(window)->createTextureFromNativeTexture(quint64(textureId), 0, size, options);
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
757QSGTexture *QSGOpenGLTexture::fromNativeExternalOES(GLuint textureId,
758 QQuickWindow *window,
760 QQuickWindow::CreateTextureOptions options)
762 return QQuickWindowPrivate::get(window)->createTextureFromNativeTexture(quint64(textureId),
766 QQuickWindowPrivate::NativeTextureIsExternalOES);
770GLuint QSGTexturePlatformOpenGL::nativeTexture()
const
772 if (
auto *tex = m_texture->rhiTexture())
773 return GLuint(tex->nativeTexture().object);
778#if defined(Q_OS_WIN) || defined(Q_QDOC)
779namespace QNativeInterface {
781
782
783
784
785
786
787
788
791
792
793
795QT_DEFINE_NATIVE_INTERFACE(QSGD3D11Texture);
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822QSGTexture *QSGD3D11Texture::fromNative(
void *texture,
823 QQuickWindow *window,
825 QQuickWindow::CreateTextureOptions options)
827 return QQuickWindowPrivate::get(window)->createTextureFromNativeTexture(quint64(texture), 0, size, options);
831void *QSGTexturePlatformD3D11::nativeTexture()
const
833 if (
auto *tex = m_texture->rhiTexture())
834 return reinterpret_cast<
void *>(quintptr(tex->nativeTexture().object));
838namespace QNativeInterface {
840
841
842
843
844
845
846
847
850
851
852
854QT_DEFINE_NATIVE_INTERFACE(QSGD3D12Texture);
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
885QSGTexture *QSGD3D12Texture::fromNative(
void *texture,
887 QQuickWindow *window,
889 QQuickWindow::CreateTextureOptions options)
891 return QQuickWindowPrivate::get(window)->createTextureFromNativeTexture(quint64(texture), resourceState, size, options);
895int QSGTexturePlatformD3D12::nativeResourceState()
const
897 if (
auto *tex = m_texture->rhiTexture())
898 return tex->nativeTexture().layout;
902void *QSGTexturePlatformD3D12::nativeTexture()
const
904 if (
auto *tex = m_texture->rhiTexture())
905 return reinterpret_cast<
void *>(quintptr(tex->nativeTexture().object));
911#if defined(__OBJC__) || defined(Q_QDOC)
912namespace QNativeInterface {
914
915
916
917
918
919
920
921
924
925
926
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
959#if QT_CONFIG(vulkan) || defined(Q_QDOC)
960namespace QNativeInterface {
962
963
964
965
966
967
968
969
972
973
974
977
978
979
981QT_DEFINE_NATIVE_INTERFACE(QSGVulkanTexture);
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
1010QSGTexture *QSGVulkanTexture::fromNative(VkImage image,
1011 VkImageLayout layout,
1012 QQuickWindow *window,
1014 QQuickWindow::CreateTextureOptions options)
1016 return QQuickWindowPrivate::get(window)->createTextureFromNativeTexture(quint64(image), layout, size, options);
1020VkImage QSGTexturePlatformVulkan::nativeImage()
const
1022 if (
auto *tex = m_texture->rhiTexture())
1023 return VkImage(tex->nativeTexture().object);
1024 return VK_NULL_HANDLE;
1027VkImageLayout QSGTexturePlatformVulkan::nativeImageLayout()
const
1029 if (
auto *tex = m_texture->rhiTexture())
1030 return VkImageLayout(tex->nativeTexture().layout);
1031 return VK_IMAGE_LAYOUT_UNDEFINED;
1035void *QSGTexture::resolveInterface(
const char *name,
int revision)
const
1037 using namespace QNativeInterface;
1041 Q_D(
const QSGTexture);
1042 auto *dd =
const_cast<QSGTexturePrivate *>(d);
1045#if QT_CONFIG(vulkan)
1046 QT_NATIVE_INTERFACE_RETURN_IF(QSGVulkanTexture, &dd->m_vulkanTextureAccessor);
1049 QT_NATIVE_INTERFACE_RETURN_IF(QSGMetalTexture, &dd->m_metalTextureAccessor);
1051#if defined(Q_OS_WIN)
1052 QT_NATIVE_INTERFACE_RETURN_IF(QSGD3D11Texture, &dd->m_d3d11TextureAccessor);
1053 QT_NATIVE_INTERFACE_RETURN_IF(QSGD3D12Texture, &dd->m_d3d12TextureAccessor);
1055#if QT_CONFIG(opengl)
1056 QT_NATIVE_INTERFACE_RETURN_IF(QSGOpenGLTexture, &dd->m_openglTextureAccessor);
1064#include "moc_qsgtexture.cpp"
static void qt_debug_print_texture_count()
static void qt_debug_remove_texture(QSGTexture *texture)
static void qt_debug_add_texture(QSGTexture *texture)