forked from exeldro/obs-source-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source-profiler.cpp
1380 lines (1235 loc) · 44.3 KB
/
source-profiler.cpp
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
#include "version.h"
#include "source-profiler.hpp"
#include <obs-frontend-api.h>
#include <QAction>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QSpinBox>
#include <QPushButton>
#include <QHeaderView>
#include <QComboBox>
#include <QMenu>
#include <util/config-file.h>
OBS_DECLARE_MODULE()
OBS_MODULE_AUTHOR("Exeldro");
OBS_MODULE_USE_DEFAULT_LOCALE("source-profiler", "en-US")
static OBSPerfViewer *perf_viewer = nullptr;
bool obs_module_load(void)
{
blog(LOG_INFO, "[Source Profiler] loaded version %s", PROJECT_VERSION);
QAction *a = (QAction *)obs_frontend_add_tools_menu_qaction(obs_module_text("PerfViewer"));
QAction::connect(a, &QAction::triggered, []() {
if (perf_viewer) {
perf_viewer->activateWindow();
perf_viewer->raise();
} else {
perf_viewer = new OBSPerfViewer();
}
});
return true;
}
OBSPerfViewer::OBSPerfViewer(QWidget *parent) : QDialog(parent)
{
setWindowTitle(QString::fromUtf8(obs_module_text("PerfViewer")));
setAttribute(Qt::WA_DeleteOnClose);
setWindowFlags(windowFlags() & Qt::WindowMaximizeButtonHint & ~Qt::WindowContextHelpButtonHint);
setSizeGripEnabled(true);
setGeometry(0, 0, 805, 300);
model = new PerfTreeModel(this);
proxy = new PerfViewerProxyModel(this);
proxy->setSourceModel(model);
treeView = new QTreeView();
treeView->setModel(proxy);
treeView->setSortingEnabled(true);
treeView->sortByColumn(-1, Qt::AscendingOrder);
treeView->setAlternatingRowColors(true);
treeView->setAnimated(true);
treeView->setSelectionMode(QAbstractItemView::SingleSelection);
auto tvh = treeView->header();
tvh->setSortIndicatorShown(true);
tvh->setSectionsClickable(true);
tvh->setStretchLastSection(false);
for (int i : model->getDefaultHiddenColumns())
tvh->setSectionHidden(i, true);
tvh->setSortIndicatorClearable(true);
tvh->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tvh, &QHeaderView::customContextMenuRequested, this, [&](const QPoint &pos) {
UNUSED_PARAMETER(pos);
QMenu menu;
auto tvh2 = treeView->header();
for (int i = 0; i < tvh2->count(); i++) {
auto title = model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
auto a = menu.addAction(title);
a->setEnabled(i != 0);
a->setCheckable(true);
a->setChecked(!tvh2->isSectionHidden(i));
connect(a, &QAction::triggered, [this, i] {
auto tvh3 = treeView->header();
tvh3->setSectionHidden(i, !tvh3->isSectionHidden(i));
if (!tvh3->isSectionHidden(i))
treeView->resizeColumnToContents(i);
});
}
menu.exec(QCursor::pos());
});
auto l = new QVBoxLayout();
l->setContentsMargins(0, 0, 0, 4);
auto searchBarLayout = new QHBoxLayout();
auto groupByBox = new QComboBox();
groupByBox->addItem(QString::fromUtf8(obs_module_text("PerfViewer.Scene")));
groupByBox->addItem(QString::fromUtf8(obs_module_text("PerfViewer.SceneNested")));
groupByBox->addItem(QString::fromUtf8(obs_module_text("PerfViewer.Source")));
groupByBox->addItem(QString::fromUtf8(obs_module_text("PerfViewer.Filter")));
groupByBox->addItem(QString::fromUtf8(obs_module_text("PerfViewer.Transition")));
groupByBox->addItem(QString::fromUtf8(obs_module_text("PerfViewer.All")));
searchBarLayout->addWidget(groupByBox);
searchBarLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
auto onlyActiveCheckBox = new QCheckBox(QString::fromUtf8(obs_module_text("PerfViewer.OnlyActive")));
searchBarLayout->addWidget(onlyActiveCheckBox);
searchBarLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
auto searchBox = new QLineEdit();
searchBox->setMinimumSize(256, 0);
searchBox->setPlaceholderText(QString::fromUtf8(obs_module_text("PerfViewer.Search")));
searchBarLayout->addWidget(searchBox);
l->addLayout(searchBarLayout);
l->addWidget(treeView);
auto buttonLayout = new QHBoxLayout();
buttonLayout->setContentsMargins(10, 0, 10, 0);
auto versionLabel = new QLabel(
QString::fromUtf8("<a href=\"https://github.com/exeldro/obs-source-profiler\">Source profiler</a> (" PROJECT_VERSION
") by <a href=\"https://www.exeldro.com\">Exeldro</a>"));
versionLabel->setOpenExternalLinks(true);
buttonLayout->addWidget(versionLabel);
buttonLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding));
auto refreshLabel = new QLabel(QString::fromUtf8(obs_module_text("PerfViewer.RefreshInterval")));
buttonLayout->addWidget(refreshLabel);
auto refreshInterval = new QSpinBox();
refreshInterval->setSuffix(" ms");
refreshInterval->setMinimum(500);
refreshInterval->setMaximum(10000);
refreshInterval->setSingleStep(100);
refreshInterval->setValue(1000);
refreshLabel->setBuddy(refreshInterval);
buttonLayout->addWidget(refreshInterval);
auto resetButton = new QPushButton(QString::fromUtf8(obs_frontend_get_locale_string("Reset")));
buttonLayout->addWidget(resetButton);
auto closeButton = new QPushButton(QString::fromUtf8(obs_frontend_get_locale_string("Close")));
buttonLayout->addWidget(closeButton);
l->addLayout(buttonLayout);
setLayout(l);
connect(closeButton, &QPushButton::clicked, this, &OBSPerfViewer::close);
connect(resetButton, &QAbstractButton::clicked, model, &PerfTreeModel::refreshSources);
connect(model, &PerfTreeModel::modelReset, this, &OBSPerfViewer::sourceListUpdated);
connect(groupByBox, &QComboBox::currentIndexChanged, this, [&](int index) {
if (index < 0 || model->getShowMode() == index)
return;
model->setShowMode((PerfTreeModel::ShowMode)index);
});
connect(onlyActiveCheckBox, &QCheckBox::stateChanged, this, [&, onlyActiveCheckBox]() {
bool checked = onlyActiveCheckBox->isChecked();
if (checked == model->getActiveOnly())
return;
model->setActiveOnly(checked);
});
connect(searchBox, &QLineEdit::textChanged, this, [&](const QString &text) {
proxy->setFilterText(text);
if (!text.isEmpty())
treeView->expandAll();
});
connect(refreshInterval, &QSpinBox::valueChanged, model, &PerfTreeModel::setRefreshInterval);
source_profiler_enable(true);
#ifndef __APPLE__
source_profiler_gpu_enable(true);
#endif
auto obs_config = obs_frontend_get_user_config();
auto show_mode = (int)config_get_int(obs_config, "PerfViewer", "showmode");
config_set_default_bool(obs_config, "PerfViewer", "active", true);
bool active_only = config_get_bool(obs_config, "PerfViewer", "active");
model->setActiveOnly(active_only, false);
model->setShowMode((enum PerfTreeModel::ShowMode)show_mode);
const char *geom = config_get_string(obs_config, "PerfViewer", "geometry");
if (geom != nullptr) {
QByteArray ba = QByteArray::fromBase64(QByteArray(geom));
restoreGeometry(ba);
}
groupByBox->setCurrentIndex(show_mode);
onlyActiveCheckBox->setChecked(active_only);
const char *columns = config_get_string(obs_config, "PerfViewer", "columns");
if (columns != nullptr) {
QByteArray ba = QByteArray::fromBase64(QByteArray(columns));
treeView->header()->restoreState(ba);
}
show();
}
OBSPerfViewer::~OBSPerfViewer()
{
perf_viewer = nullptr;
const auto obs_config = obs_frontend_get_user_config();
if (obs_config) {
config_set_string(obs_config, "PerfViewer", "columns", treeView->header()->saveState().toBase64().constData());
config_set_string(obs_config, "PerfViewer", "geometry", saveGeometry().toBase64().constData());
config_set_int(obs_config, "PerfViewer", "showmode", model->getShowMode());
config_set_bool(obs_config, "PerfViewer", "active", model->getActiveOnly());
config_save(obs_config);
}
#ifndef __APPLE__
source_profiler_gpu_enable(false);
#endif
source_profiler_enable(false);
delete model;
}
PerfTreeColumn::PerfTreeColumn(QString name, QVariant (*getValue)(const PerfTreeItem *item), enum PerfTreeColumnType column_type,
bool default_hidden)
: m_get_value(getValue),
m_name(name),
m_column_type(column_type),
m_default_hidden(default_hidden)
{
}
static double ns_to_ms(uint64_t ns)
{
return (double)ns / 1000000.0;
}
PerfTreeModel::PerfTreeModel(QObject *parent) : QAbstractItemModel(parent)
{
columns = {
PerfTreeColumn(QString::fromUtf8(obs_module_text("PerfViewer.Name")),
[](const PerfTreeItem *item) { return QVariant(item->name); }),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.Type")),
[](const PerfTreeItem *item) { return QVariant(item->sourceType); }, COLUMN_TYPE_DEFAULT, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.Active")),
[](const PerfTreeItem *item) { return QVariant(item->active); }, COLUMN_TYPE_BOOL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.Rendered")),
[](const PerfTreeItem *item) { return QVariant(item->rendered); }, COLUMN_TYPE_BOOL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.Enabled")),
[](const PerfTreeItem *item) { return QVariant(item->enabled); }, COLUMN_TYPE_BOOL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.TickAvg")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->tick_avg));
},
COLUMN_TYPE_DURATION, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.TickMax")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->tick_max));
},
COLUMN_TYPE_DURATION, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.RenderAvg")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->render_avg));
},
COLUMN_TYPE_DURATION, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.RenderMax")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->render_max));
},
COLUMN_TYPE_DURATION, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.RenderTotal")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->render_sum));
},
COLUMN_TYPE_DURATION),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.CpuPercentage")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant((double)(item->m_perf->render_sum + item->m_perf->tick_avg) /
(double)obs_get_frame_interval_ns() * 100.0);
},
COLUMN_TYPE_PERCENTAGE),
#ifndef __APPLE__
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.RenderGpuAvg")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->render_gpu_avg));
},
COLUMN_TYPE_DURATION, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.RenderGpuMax")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->render_gpu_max));
},
COLUMN_TYPE_DURATION, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.RenderGpuTotal")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->render_gpu_sum));
},
COLUMN_TYPE_DURATION),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.GpuPercentage")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant((double)item->m_perf->render_gpu_sum / (double)obs_get_frame_interval_ns() * 100.0);
},
COLUMN_TYPE_PERCENTAGE, true),
#endif
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.AsyncFps")),
[](const PerfTreeItem *item) {
if (!item->m_perf || !item->async)
return QVariant();
return QVariant(item->m_perf->async_input);
},
COLUMN_TYPE_FPS, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.AsyncBest")),
[](const PerfTreeItem *item) {
if (!item->m_perf || !item->async)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->async_input_best));
},
COLUMN_TYPE_INTERVAL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.AsyncWorst")),
[](const PerfTreeItem *item) {
if (!item->m_perf || !item->async)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->async_input_worst));
},
COLUMN_TYPE_INTERVAL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.AsyncRenderedFps")),
[](const PerfTreeItem *item) {
if (!item->m_perf || !item->async)
return QVariant();
return QVariant(item->m_perf->async_rendered);
},
COLUMN_TYPE_FPS, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.AsyncRenderedBest")),
[](const PerfTreeItem *item) {
if (!item->m_perf || !item->async)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->async_rendered_best));
},
COLUMN_TYPE_INTERVAL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.AsyncRenderedWorst")),
[](const PerfTreeItem *item) {
if (!item->m_perf || !item->async)
return QVariant();
return QVariant(ns_to_ms(item->m_perf->async_rendered_worst));
},
COLUMN_TYPE_INTERVAL, true),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.Total")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(
ns_to_ms(item->m_perf->tick_avg + item->m_perf->render_sum + item->m_perf->render_gpu_sum));
},
COLUMN_TYPE_DURATION),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.TotalPercentage")),
[](const PerfTreeItem *item) {
if (!item->m_perf)
return QVariant();
return QVariant(
(double)(item->m_perf->tick_avg + item->m_perf->render_sum + item->m_perf->render_gpu_sum) /
(double)obs_get_frame_interval_ns() * 100.0);
},
COLUMN_TYPE_PERCENTAGE),
PerfTreeColumn(
QString::fromUtf8(obs_module_text("PerfViewer.SubItems")),
[](const PerfTreeItem *item) { return QVariant(item->child_count); }, COLUMN_TYPE_COUNT),
};
auto sh = obs_get_signal_handler();
signal_handler_connect(sh, "source_create", source_add, this);
signal_handler_connect(sh, "source_destroy", source_remove, this);
signal_handler_connect(sh, "source_remove", source_remove, this);
signal_handler_connect(sh, "source_activate", source_activate, this);
signal_handler_connect(sh, "source_deactivate", source_deactivate, this);
obs_frontend_add_event_callback(frontend_event, this);
updater.reset(new QuickThread([this] {
while (true) {
obs_queue_task(
OBS_TASK_UI, [](void *) {}, nullptr, true);
QThread::msleep(refreshInterval);
updateData();
}
}));
updater->start();
}
QList<int> PerfTreeModel::getDefaultHiddenColumns()
{
QList<int> hiddenColumns;
for (int i = 0; i < columns.count(); i++) {
auto column = columns.at(i);
if (column.DefaultHidden())
hiddenColumns.append(i);
}
return hiddenColumns;
}
void OBSPerfViewer::sourceListUpdated()
{
if (loaded)
return;
for (int i = 0; i < model->columnCount(); i++) {
if (!treeView->isColumnHidden(i))
treeView->resizeColumnToContents(i);
}
loaded = true;
}
void PerfTreeModel::EnumFilter(obs_source_t *parent, obs_source_t *child, void *data)
{
if (obs_source_get_type(child) != OBS_SOURCE_TYPE_FILTER)
return;
if (!parent)
parent = obs_filter_get_parent(child);
auto root = static_cast<PerfTreeItem *>(data);
if (root->model()->activeOnly && ((parent && !obs_source_active(parent)) || !obs_source_enabled(child)))
return;
auto item = new PerfTreeItem(child, root, root->model());
root->appendChild(item);
}
void PerfTreeModel::EnumTree(obs_source_t *, obs_source_t *child, void *data)
{
EnumAllSource(data, child);
}
bool PerfTreeModel::EnumSceneItem(obs_scene_t *, obs_sceneitem_t *item, void *data)
{
auto parent = static_cast<PerfTreeItem *>(data);
if (parent->model()->activeOnly && !obs_sceneitem_visible(item))
return true;
obs_source_t *source = obs_sceneitem_get_source(item);
auto treeItem = new PerfTreeItem(item, parent, parent->model());
parent->prependChild(treeItem);
auto show_transition = obs_sceneitem_get_transition(item, true);
if (show_transition) {
EnumAllSource(treeItem, show_transition);
}
auto hide_transition = obs_sceneitem_get_transition(item, false);
if (hide_transition) {
EnumAllSource(treeItem, hide_transition);
}
if (obs_source_is_scene(source)) {
if (parent->model()->showMode != SCENE_NESTED)
return true;
obs_scene_t *scene = obs_scene_from_source(source);
obs_scene_enum_items(scene, EnumSceneItem, treeItem);
} else if (obs_sceneitem_is_group(item)) {
obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
obs_scene_enum_items(scene, EnumSceneItem, treeItem);
}
if (obs_source_filter_count(source) > 0) {
obs_source_enum_filters(source, EnumFilter, treeItem);
}
return true;
}
bool PerfTreeModel::EnumAllSource(void *data, obs_source_t *source)
{
if (obs_source_get_type(source) == OBS_SOURCE_TYPE_FILTER)
return true;
auto root = static_cast<PerfTreeItem *>(data);
if (root->model()->activeOnly && !obs_source_active(source))
return true;
auto item = new PerfTreeItem(source, root, root->model());
root->appendChild(item);
if (obs_scene_t *scene = obs_scene_from_source(source)) {
obs_scene_enum_items(scene, EnumSceneItem, item);
} else {
obs_source_enum_active_sources(source, EnumTree, item);
}
if (obs_source_filter_count(source) > 0) {
obs_source_enum_filters(source, EnumFilter, item);
}
return true;
}
bool PerfTreeModel::ExistsChild(PerfTreeItem *parent, obs_source_t *source)
{
for (auto it = parent->m_childItems.begin(); it != parent->m_childItems.end(); it++) {
if ((*it)->m_source && obs_weak_source_references_source((*it)->m_source, source))
return true;
if (ExistsChild(*it, source))
return true;
}
return false;
}
bool PerfTreeModel::EnumScene(void *data, obs_source_t *source)
{
if (obs_source_is_group(source))
return true;
return EnumAllSource(data, source);
}
bool PerfTreeModel::EnumSceneNested(void *data, obs_source_t *source)
{
if (obs_source_is_group(source))
return true;
auto parent = static_cast<PerfTreeItem *>(data);
if (ExistsChild(parent, source))
return true;
return EnumAllSource(data, source);
}
bool PerfTreeModel::EnumNotPrivateSource(void *data, obs_source_t *source)
{
if (obs_obj_is_private(source))
return true;
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT)
return true;
return EnumAllSource(data, source);
}
bool PerfTreeModel::EnumAll(void *data, obs_source_t *source)
{
if (obs_source_get_type(source) == OBS_SOURCE_TYPE_FILTER) {
EnumFilter(nullptr, source, data);
return true;
}
return EnumAllSource(data, source);
}
bool PerfTreeModel::EnumFilterSource(void *data, obs_source_t *source)
{
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_FILTER)
return true;
EnumFilter(nullptr, source, data);
return true;
}
bool PerfTreeModel::EnumTransition(void *data, obs_source_t *source)
{
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_TRANSITION)
return true;
return EnumAllSource(data, source);
}
void PerfTreeModel::refreshSources()
{
if (refreshing)
return;
refreshing = true;
beginResetModel();
delete rootItem;
rootItem = new PerfTreeItem((obs_source_t *)nullptr, nullptr, this);
if (showMode == ShowMode::ALL) {
obs_enum_all_sources(EnumAll, rootItem);
} else if (showMode == ShowMode::SOURCE) {
obs_enum_all_sources(EnumNotPrivateSource, rootItem);
} else if (showMode == ShowMode::SCENE) {
if (obs_frontend_preview_program_mode_active()) {
obs_source_t *output = obs_get_output_source(0);
if (obs_source_get_type(output) == OBS_SOURCE_TYPE_TRANSITION) {
obs_source_release(output);
output = obs_transition_get_active_source(output);
}
if (obs_source_get_type(output) == OBS_SOURCE_TYPE_SCENE && obs_obj_is_private(output)) {
EnumScene(rootItem, output);
}
obs_source_release(output);
}
obs_enum_scenes(EnumScene, rootItem);
} else if (showMode == ShowMode::SCENE_NESTED) {
if (obs_frontend_preview_program_mode_active()) {
obs_source_t *output = obs_get_output_source(0);
if (obs_source_get_type(output) == OBS_SOURCE_TYPE_TRANSITION) {
obs_source_release(output);
output = obs_transition_get_active_source(output);
}
if (obs_source_get_type(output) == OBS_SOURCE_TYPE_SCENE && obs_obj_is_private(output)) {
EnumSceneNested(rootItem, output);
}
obs_source_release(output);
}
obs_enum_scenes(EnumSceneNested, rootItem);
} else if (showMode == ShowMode::FILTER) {
obs_enum_all_sources(EnumFilterSource, rootItem);
} else if (showMode == ShowMode::TRANSITION) {
obs_enum_all_sources(EnumTransition, rootItem);
}
endResetModel();
refreshing = false;
updateData();
}
void PerfTreeModel::updateData()
{
if (refreshing)
return;
// Set target frame time in ms
frameTime = ns_to_ms(obs_get_frame_interval_ns());
if (rootItem)
rootItem->update();
}
void PerfViewerProxyModel::setFilterText(const QString &filter)
{
QRegularExpression regex(filter, QRegularExpression::CaseInsensitiveOption);
setFilterRegularExpression(regex);
}
bool PerfViewerProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex itemIndex = sourceModel()->index(sourceRow, 0, sourceParent);
auto name = sourceModel()->data(itemIndex, Qt::DisplayRole).toString();
return name.contains(filterRegularExpression());
}
PerfTreeModel::~PerfTreeModel()
{
if (updater)
updater->terminate();
obs_frontend_remove_event_callback(frontend_event, this);
auto sh = obs_get_signal_handler();
signal_handler_disconnect(sh, "source_create", source_add, this);
signal_handler_disconnect(sh, "source_destroy", source_remove, this);
signal_handler_disconnect(sh, "source_remove", source_remove, this);
signal_handler_disconnect(sh, "source_activate", source_activate, this);
signal_handler_disconnect(sh, "source_deactivate", source_deactivate, this);
delete rootItem;
}
QVariant ColorFormPercentage(double percentage)
{
if (obs_frontend_is_theme_dark()) {
// https://coolors.co/palette/13141a-1a3278-6e520d-7d1224
if (percentage >= 100.0)
return QColor(125, 18, 36);
if (percentage >= 50.0)
return QColor(110, 82, 13);
if (percentage >= 25.0)
return QColor(26, 50, 120);
return {}; //QColor(19, 20, 26);
}
// https://coolors.co/palette/5b6273-718cdc-eabc48-e85e75
if (percentage >= 100.0)
return QColor(232, 94, 117);
if (percentage >= 50.0)
return QColor(234, 188, 72);
if (percentage >= 25.0)
return QColor(113, 140, 220);
return {}; //QColor(91, 98, 115);
}
QVariant PerfTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return {};
if (role == Qt::CheckStateRole) {
auto column = columns.at(index.column());
if (column.m_column_type != COLUMN_TYPE_BOOL)
return {};
auto item = static_cast<const PerfTreeItem *>(index.internalPointer());
auto d = column.Value(item);
if (d.userType() == QMetaType::Bool)
return d.toBool() ? Qt::Checked : Qt::Unchecked;
} else if (role == Qt::DisplayRole) {
auto column = columns.at(index.column());
if (column.m_column_type == COLUMN_TYPE_BOOL)
return {};
auto item = static_cast<PerfTreeItem *>(index.internalPointer());
auto d = column.Value(item);
if (d.userType() == QMetaType::Bool)
return {};
if (d.userType() == QMetaType::Double) {
if (d.toDouble() < 0.005)
return {};
return QString::asprintf("%.02f", d.toDouble());
}
return d;
} else if (role == Qt::DecorationRole) {
if (index.column() != 0)
return {};
auto item = static_cast<PerfTreeItem *>(index.internalPointer());
return item->icon;
} else if (role == Qt::BackgroundRole) {
auto column_type = columns.at(index.column()).m_column_type;
if (column_type == COLUMN_TYPE_PERCENTAGE) {
auto item = static_cast<PerfTreeItem *>(index.internalPointer());
auto column = columns.at(index.column());
return ColorFormPercentage(column.Value(item).toDouble());
} else if (column_type == COLUMN_TYPE_DURATION) {
if (frameTime <= 0.0)
return {};
auto item = static_cast<PerfTreeItem *>(index.internalPointer());
auto column = columns.at(index.column());
return ColorFormPercentage(column.Value(item).toDouble() / frameTime * 100.0);
} else if (column_type == COLUMN_TYPE_INTERVAL) {
if (frameTime <= 0.0)
return {};
auto item = static_cast<PerfTreeItem *>(index.internalPointer());
auto column = columns.at(index.column());
auto interval = column.Value(item).toDouble();
if (interval > frameTime)
return ColorFormPercentage((interval - frameTime) / frameTime * 100.0);
}
return {};
} else if (role == Qt::TextAlignmentRole) {
if (columns.at(index.column()).m_column_type != COLUMN_TYPE_DEFAULT)
return Qt::AlignRight;
} else if (role == Qt::UserRole) {
auto item = static_cast<PerfTreeItem *>(index.internalPointer());
auto column = columns.at(index.column());
auto d = column.Value(item);
return d;
} else if (role == Qt::InitialSortOrderRole) {
auto column_type = columns.at(index.column()).m_column_type;
if (column_type == COLUMN_TYPE_PERCENTAGE || column_type == COLUMN_TYPE_DURATION)
return Qt::DescendingOrder;
}
return {};
}
Qt::ItemFlags PerfTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
QVariant PerfTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < columns.size()) {
auto column = columns.at(section);
return column.Name();
}
return QAbstractItemModel::headerData(section, orientation, role);
}
QModelIndex PerfTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return {};
PerfTreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<PerfTreeItem *>(parent.internalPointer());
if (auto childItem = parentItem->child(row))
return createIndex(row, column, childItem);
return {};
}
QModelIndex PerfTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return {};
auto childItem = static_cast<PerfTreeItem *>(index.internalPointer());
auto parentItem = childItem->parentItem();
if (parentItem == rootItem)
return {};
return createIndex(parentItem->row(), 0, parentItem);
}
int PerfTreeModel::rowCount(const QModelIndex &parent) const
{
PerfTreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<PerfTreeItem *>(parent.internalPointer());
if (!parentItem)
return 0;
return parentItem->childCount();
}
int PerfTreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<PerfTreeItem *>(parent.internalPointer())->columnCount();
return (int)columns.count();
}
void PerfTreeModel::add_filter(obs_source_t *source, obs_source_t *filter, const QModelIndex &parent)
{
if (refreshing)
return;
auto count = rowCount(parent);
for (int i = 0; i < count; i++) {
auto index2 = index(i, 0, parent);
auto item = static_cast<PerfTreeItem *>(index2.internalPointer());
if (item->m_source && obs_weak_source_references_source(item->m_source, source)) {
auto pos = rowCount(index2);
beginInsertRows(index2, pos, pos);
item->appendChild(new PerfTreeItem(filter, item, this));
endInsertRows();
} else {
add_filter(source, filter, index2);
}
}
}
void PerfTreeModel::remove_source(obs_source_t *source, const QModelIndex &parent)
{
if (refreshing)
return;
auto count = rowCount(parent);
for (int i = count - 1; i >= 0; i--) {
auto index2 = index(i, 0, parent);
auto item = static_cast<PerfTreeItem *>(index2.internalPointer());
if (item->m_source && obs_weak_source_references_source(item->m_source, source)) {
auto sh = obs_source_get_signal_handler(source);
signal_handler_disconnect(sh, "filter_add", item->filter_add, item);
signal_handler_disconnect(sh, "filter_remove", item->filter_remove, item);
signal_handler_disconnect(sh, "item_add", item->sceneitem_add, item);
signal_handler_disconnect(sh, "item_remove", item->sceneitem_remove, item);
signal_handler_disconnect(sh, "item_visible", item->sceneitem_visible, item);
beginRemoveRows(parent, i, i);
item->m_parentItem->m_childItems.removeOne(item);
endRemoveRows();
item->disconnect();
obs_queue_task(
OBS_TASK_UI, [](void *d) { delete (PerfTreeItem *)d; }, item, false);
} else {
remove_source(source, index2);
}
}
}
void PerfTreeModel::remove_weak_source(obs_weak_source_t *source, const QModelIndex &parent)
{
if (refreshing)
return;
auto count = rowCount(parent);
for (int i = count - 1; i >= 0; i--) {
auto index2 = index(i, 0, parent);
auto item = static_cast<PerfTreeItem *>(index2.internalPointer());
if (item->m_source == source) {
beginRemoveRows(parent, i, i);
item->m_parentItem->m_childItems.removeOne(item);
endRemoveRows();
item->disconnect();
obs_queue_task(
OBS_TASK_UI, [](void *d) { delete (PerfTreeItem *)d; }, item, false);
} else {
remove_weak_source(source, index2);
}
}
}
void PerfTreeModel::remove_siblings(const QModelIndex &parent)
{
auto count = rowCount(parent);
for (int i = count - 1; i >= 0; i--) {
auto index2 = index(i, 0, parent);
auto item = static_cast<PerfTreeItem *>(index2.internalPointer());
auto source = obs_weak_source_get_source(item->m_source);
if (source) {
auto sh = obs_source_get_signal_handler(source);
signal_handler_disconnect(sh, "filter_add", item->filter_add, item);
signal_handler_disconnect(sh, "filter_remove", item->filter_remove, item);
signal_handler_disconnect(sh, "item_add", item->sceneitem_add, item);
signal_handler_disconnect(sh, "item_remove", item->sceneitem_remove, item);
signal_handler_disconnect(sh, "item_visible", item->sceneitem_visible, item);
obs_source_release(source);
}
item->m_parentItem->m_childItems.removeOne(item);
item->disconnect();
obs_queue_task(
OBS_TASK_UI, [](void *d) { delete (PerfTreeItem *)d; }, item, false);
}
}
void PerfTreeModel::add_sceneitem(obs_source_t *scene, obs_sceneitem_t *sceneitem, const QModelIndex &parent)
{
if (refreshing)
return;
auto count = rowCount(parent);
for (int i = 0; i < count; i++) {
auto index2 = index(i, 0, parent);
auto item = static_cast<PerfTreeItem *>(index2.internalPointer());
if (item->m_source && obs_weak_source_references_source(item->m_source, scene)) {
auto pos = rowCount(index2);
beginInsertRows(index2, pos, pos);
auto child = new PerfTreeItem(sceneitem, item, this);
item->appendChild(child);
endInsertRows();
obs_source_enum_filters(obs_sceneitem_get_source(sceneitem), EnumFilter, child);
} else {
add_sceneitem(scene, sceneitem, index2);
}
}
}
void PerfTreeModel::remove_sceneitem(obs_source_t *scene, obs_sceneitem_t *sceneitem, const QModelIndex &parent)
{
if (refreshing)
return;
auto count = rowCount(parent);
for (int i = count - 1; i >= 0; i--) {
auto index2 = index(i, 0, parent);
auto item = static_cast<PerfTreeItem *>(index2.internalPointer());
if (item->m_sceneitem && item->m_sceneitem == sceneitem) {
beginRemoveRows(parent, i, i);
item->m_parentItem->m_childItems.removeOne(item);
endRemoveRows();
obs_queue_task(
OBS_TASK_UI, [](void *d) { delete (PerfTreeItem *)d; }, item, false);
} else {
remove_sceneitem(scene, sceneitem, index2);
}
}
}
void PerfTreeModel::source_add(void *data, calldata_t *cd)
{
obs_source_t *source = (obs_source_t *)calldata_ptr(cd, "source");
auto model = (PerfTreeModel *)data;
if ((model->showMode == ShowMode::SCENE || model->showMode == ShowMode::SCENE_NESTED) && !obs_source_is_scene(source))
return;
if (model->showMode == ShowMode::SCENE_NESTED && ExistsChild(model->rootItem, source))
return;
if (model->showMode == ShowMode::SOURCE && obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT)
return;
if (model->showMode == ShowMode::FILTER && obs_source_get_type(source) != OBS_SOURCE_TYPE_FILTER)
return;
if (model->showMode == ShowMode::TRANSITION && obs_source_get_type(source) != OBS_SOURCE_TYPE_TRANSITION)
return;
if (model->activeOnly && !obs_source_active(source))
return;
QModelIndex parent;
auto pos = model->rowCount(parent);