Repository navigation
Expand file tree
/
Copy pathtest_platform_win.cpp
More file actions
1682 lines (1448 loc) · 82.3 KB
/
Copy pathtest_platform_win.cpp
File metadata and controls
1682 lines (1448 loc) · 82.3 KB
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
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: Tests for Windows platform integration. These are the only tests whose subject is the
// operating system itself -- extended path syntax, DXGI device loss, adapter classification and the
// texture budget taken from it, the adapter the decode device is made on, texture recycling, the
// crash-guard recovery session, the system font stack, the registry settings store, time zone
// conversion, the shell drag data object and the snap-layouts caption button.
// Keeping them here is what lets every other test file stay free of system headers.
#include "pch.h"
#include "libdesktop/platform/win32.h"
#include "libdesktop/render/win32.h"
#include "libdesktop/render/software.h"
#include "av_format.h"
#include "test_fixtures.h"
#include <mmreg.h>
extern "C"
{
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_d3d11va.h"
}
// The recent-folder list must not wait on a share that has dropped, so a network location is
// recognised from its spelling and volume type without being reached.
static void should_recognise_network_locations()
{
assert_equal(true, platform::is_network_location(core::folder_path(R"(\\server\share\photos)")),
"a UNC share is a network location");
assert_equal(false, platform::is_network_location(test_files_folder()), "the local test folder is not");
}
static void should_preserve_writability_probe_collisions()
{
const auto folder = test_temps.next_folder("write-probe");
const auto name = std::format("df-write-probe-{:08x}.tmp", static_cast<uint32_t>(GetCurrentProcessId()));
const auto collision = folder.combine_file(name);
write_test_file(collision, "existing file");
assert_equal(true, platform::is_writable(folder), "a probe collision does not make a writable folder read-only");
assert_equal("existing file", read_test_file(collision), "the probe must not truncate or delete an existing file");
assert_equal(1_z, platform::iterate_file_items(folder, true).files.size(), "the probe cleans up only its own file");
}
static void should_convert_extended_file_system_paths()
{
const auto long_unc = std::string("\\\\server\\share\\") + std::string(MAX_PATH, 'x');
const auto converted = platform::to_file_system_path(core::folder_path(long_unc));
assert_equal(std::wstring(L"\\\\?\\UNC\\server\\share\\") + std::wstring(MAX_PATH, L'x'), converted,
"long UNC path");
const auto extended = std::wstring(L"\\\\?\\C:\\") + std::wstring(MAX_PATH, L'x');
assert_equal(extended, platform::to_file_system_path(core::folder_path(core::utf16_to_utf8(extended))),
"extended path unchanged");
}
static void should_convert_dates_with_their_own_dst_offset()
{
TIME_ZONE_INFORMATION zone{};
zone.Bias = 300;
zone.DaylightBias = -60;
zone.DaylightDate = {0, 3, 0, 2, 2, 0, 0, 0};
zone.StandardDate = {0, 11, 0, 1, 2, 0, 0, 0};
constexpr uint64_t fraction = 1234567;
for (const auto month : {1, 7})
{
const auto utc = core::date_t(2024, month, 15, 12, 0, 0).to_int64() + fraction;
const auto local = core::date_t(2024, month, 15, month == 1 ? 7 : 8, 0, 0).to_int64() + fraction;
assert_equal(local, platform::utc_to_local(utc, &zone), "conversion uses the timestamp's season");
assert_equal(utc, platform::local_to_utc(local, &zone), "reverse conversion preserves every tick");
}
const auto after_jump = core::date_t(2024, 3, 10, 3, 30, 0).to_int64();
assert_equal(core::date_t(2024, 3, 10, 7, 30, 0).to_int64(), platform::local_to_utc(after_jump, &zone),
"an unambiguous time after the spring transition uses daylight bias");
assert_equal(uint64_t{0}, platform::utc_to_local(0, &zone), "an absent timestamp remains absent");
}
namespace
{
constexpr uint64_t test_ticks_per_ms = 10'000;
constexpr uint64_t test_ticks_per_minute = 60'000 * test_ticks_per_ms;
struct tabulation_case
{
std::string_view name;
TIME_ZONE_INFORMATION zone;
// 2024's transitions as UTC instants, and as the local readings the rules name.
std::vector<uint64_t> instants;
std::vector<uint64_t> readings;
};
TIME_ZONE_INFORMATION make_zone(const LONG bias, const LONG daylight_bias, const SYSTEMTIME daylight,
const SYSTEMTIME standard)
{
TIME_ZONE_INFORMATION zone{};
zone.Bias = bias;
zone.DaylightBias = daylight_bias;
zone.DaylightDate = daylight;
zone.StandardDate = standard;
return zone;
}
uint64_t stamp(const int month, const int day, const int hour, const int minute, const int second = 0,
const int ms = 0)
{
return core::date_t(2024, month, day, hour, minute, second).to_int64() +
static_cast<uint64_t>(ms) * test_ticks_per_ms;
}
std::vector<uint64_t> samples_around(const std::vector<uint64_t>& changes, const bool include_gap_edges)
{
std::vector<uint64_t> result;
const auto year_start = stamp(1, 1, 0, 0);
for (auto hour = 0; hour < 366 * 24; hour += 6)
result.push_back(year_start + static_cast<uint64_t>(hour) * 60 * test_ticks_per_minute + 1234567);
for (const auto change : changes)
{
const auto at = [change](const int64_t minutes, const int64_t ms)
{
return static_cast<uint64_t>(static_cast<int64_t>(change) +
minutes * static_cast<int64_t>(test_ticks_per_minute) +
ms * static_cast<int64_t>(test_ticks_per_ms));
};
for (auto minute = -180; minute <= 180; ++minute) result.push_back(at(minute, 0));
const auto edges = include_gap_edges ? std::vector<int>{-60, -30, 0, 30, 60} : std::vector<int>{0};
for (const auto edge : edges)
{
for (auto ms = -3; ms <= 3; ++ms) result.push_back(at(edge, ms) + 4321);
}
}
return result;
}
}
// Dates are converted per item in search, grouping and summaries, and the system conversion costs
// microseconds, so each year's offsets are tabulated from the system once. The tables must agree
// with the system to the tick, including at the millisecond a transition takes effect, for zones
// whose rules span the new year, move by half an hour, or change just before midnight. Shifting a
// tabulated boundary by one millisecond fails this test.
static void should_tabulate_time_zone_offsets_like_the_system()
{
const std::vector<tabulation_case> cases = {
{"northern",
make_zone(300, -60, {0, 3, 0, 2, 2, 0, 0, 0}, {0, 11, 0, 1, 2, 0, 0, 0}),
{stamp(3, 10, 7, 0), stamp(11, 3, 6, 0)},
{stamp(3, 10, 2, 0), stamp(11, 3, 2, 0)}},
{"southern",
make_zone(-600, -60, {0, 10, 0, 1, 2, 0, 0, 0}, {0, 4, 0, 1, 3, 0, 0, 0}),
{stamp(4, 6, 16, 0), stamp(10, 5, 16, 0)},
{stamp(4, 7, 3, 0), stamp(10, 6, 2, 0)}},
{"half-hour daylight",
make_zone(-630, -30, {0, 10, 0, 1, 2, 0, 0, 0}, {0, 4, 0, 1, 2, 0, 0, 0}),
{stamp(4, 6, 15, 0), stamp(10, 5, 15, 30)},
{stamp(4, 7, 2, 0), stamp(10, 6, 2, 0)}},
{"before midnight",
make_zone(-120, -60, {0, 3, 4, 5, 23, 59, 59, 999}, {0, 10, 5, 5, 1, 0, 0, 0}),
{stamp(3, 28, 21, 59, 59, 999), stamp(10, 24, 22, 0)},
{stamp(3, 28, 23, 59, 59, 999), stamp(10, 25, 1, 0)}},
{"no daylight", make_zone(-330, 0, {}, {}), {}, {}},
};
for (const auto& c : cases)
{
for (const auto instant : c.instants)
{
const auto earlier = instant - test_ticks_per_ms;
assert_equal(true,
platform::utc_to_local(earlier, &c.zone) - earlier !=
platform::utc_to_local(instant, &c.zone) - instant,
std::format("{}: the system changes offset at the stated instant", c.name));
}
for (const auto to_local : {true, false})
{
const auto stamps = samples_around(to_local ? c.instants : c.readings, !to_local);
const auto tabulated = platform::tabulated_time_zone_conversions(stamps, c.zone, to_local);
auto mismatches = 0;
std::string first;
for (auto i = 0u; i < stamps.size(); ++i)
{
const auto expected = to_local ? platform::utc_to_local(stamps[i], &c.zone)
: platform::local_to_utc(stamps[i], &c.zone);
if (tabulated[i] != expected && mismatches++ == 0)
first = std::format(" first at {}: {} != {}", stamps[i], tabulated[i], expected);
}
assert_equal(0, mismatches,
std::format("{} {} tabulation matches the system{}", c.name,
to_local ? "UTC-to-local" : "local-to-UTC", first));
}
}
}
// The tables serve whatever zone this machine is in, through the same rule selection as the
// per-call conversion: the year's dynamic rules, or standard time when adjustment is turned off.
static void should_convert_the_current_zone_like_the_system()
{
DYNAMIC_TIME_ZONE_INFORMATION current{};
assert_equal(true, GetDynamicTimeZoneInformation(¤t) != TIME_ZONE_ID_INVALID, "current zone is known");
for (const auto year : {2006, 2007, 2019, 2024})
{
TIME_ZONE_INFORMATION rules{};
assert_equal(true, GetTimeZoneInformationForYear(static_cast<USHORT>(year), ¤t, &rules) != FALSE,
"the year's rules are known");
if (current.DynamicDaylightTimeDisabled)
{
rules.StandardDate.wMonth = 0;
rules.DaylightDate.wMonth = 0;
}
auto mismatches = 0;
const auto start = core::date_t(year, 1, 1, 0, 0, 0).to_int64() + 7654321;
for (auto half_day = 0; half_day < 730; ++half_day)
{
const auto ts = start + static_cast<uint64_t>(half_day) * 720 * test_ticks_per_minute;
if (platform::utc_to_local(ts) != platform::utc_to_local(ts, &rules)) ++mismatches;
if (platform::local_to_utc(ts) != platform::local_to_utc(ts, &rules)) ++mismatches;
}
assert_equal(0, mismatches, std::format("{} converts as the system does", year));
}
}
// Converting each date through the system took about five microseconds; a large collection paid
// that for every date in every search and grouping. Removing the year cache fails this test.
static void should_tabulate_local_time_once_per_year()
{
const auto start = core::date_t(2023, 1, 1, 0, 0, 0).to_int64();
constexpr uint64_t step = 31'536'000'000ull;
const auto before = platform::time_zone_system_queries();
uint64_t total = 0;
for (auto i = 0u; i < 10000; ++i)
{
const auto ts = start + i * step;
total += platform::utc_to_local(ts) / 10000000;
total += platform::local_to_utc(ts) / 10000000;
}
const auto queries = platform::time_zone_system_queries() - before;
assert_equal(true, queries <= 1000,
std::format("{} system conversions for 20000 dates (checksum {})", queries, total));
}
static void should_initialize_unicode_ini_settings()
{
const auto folder = test_temps.next_folder("unicode-ini");
const auto ini = folder.combine_file("diffractor.ini");
const auto settings = platform::create_ini_file_settings(folder);
const std::string value = "C:\\\xe6\x9d\xb1\xe4\xba\xac\\\xf0\x9f\x8e\xac.mp4";
assert_equal(true, settings->write("paths", "movie", value), "Unicode setting is written");
const auto bytes = platform::blob_from_file(ini);
assert_equal(true, bytes.size() >= 2 && bytes[0] == 0xff && bytes[1] == 0xfe,
"new INI files explicitly select Unicode independent of the ANSI code page");
const auto reopened = platform::create_ini_file_settings(folder);
std::string read;
assert_equal(true, reopened->read("paths", "movie", read), "setting remains readable after reopening");
assert_equal(value, read, "the complete Unicode path survives");
assert_equal(false, reopened->root_created(), "existing configuration is not replaced");
}
// The measured length excludes the terminator a string_view does not carry, so converting with -1
// both over-read the view and asked for one wchar more than the measurement allowed. The call then
// failed and left the buffer zeroed, which made every ordinary path convert to a run of NULs.
static void should_convert_utf8_to_ansi()
{
assert_equal("photo.jpg", platform::utf8_to_a("photo.jpg"), "an ordinary name converts");
assert_equal(9_z, platform::utf8_to_a("photo.jpg").size(), "and is not padded with terminators");
assert_equal(true, platform::utf8_to_a("").empty(), "an empty name converts to an empty name");
// The source has no terminator of its own: the conversion must respect the view's length rather
// than read past it.
const std::string backing("C:\\photos\\holiday.jpgTRAILING");
assert_equal("C:\\photos\\holiday.jpg", platform::utf8_to_a(std::string_view(backing).substr(0, 21)),
"a view stops where it says it stops");
// The console commands run with a UTF-8 C locale, where each of these characters takes three
// bytes. An output sized at two bytes per UTF-16 unit truncated a name that is more CJK than
// ASCII, and a truncated conversion answers empty - so the file could not be opened by name.
const std::string cjk = "D:\\\xe5\x8b\x95\xe7\x94\xbb\\\xe6\x9d\xb1\xe4\xba\xac\xe6\x97\x85\xe8\xa1\x8c"
"\xe3\x81\xae\xe6\x80\x9d\xe3\x81\x84\xe5\x87\xba.mp4";
assert_equal(cjk, platform::utf8_to_a(cjk), "a name of three-byte characters converts whole");
}
// Undocking or unplugging a second display leaves a saved rect that intersects no monitor at all.
static void should_restore_a_window_onto_a_display()
{
constexpr recti work(0, 0, 1920, 1040);
const auto from_a_missing_display = platform::fit_window_to_work_area({2200, 300, 3000, 900}, work);
assert_equal(800, from_a_missing_display.width(), "width is preserved");
assert_equal(600, from_a_missing_display.height(), "height is preserved");
assert_equal(true, work.intersects(from_a_missing_display), "and the window lands on the display");
assert_equal(1120, from_a_missing_display.left, "pushed just inside the right edge");
assert_equal(300, from_a_missing_display.top, "the axis that already fitted is left alone");
const auto from_the_left = platform::fit_window_to_work_area({-3000, -400, -2600, -100}, work);
assert_equal(0, from_the_left.left, "a negative origin comes back to the work area");
assert_equal(0, from_the_left.top, "on both axes");
const auto too_large = platform::fit_window_to_work_area({4000, 0, 8000, 3000}, work);
assert_equal(1920, too_large.width(), "a window larger than the display is clamped to it");
assert_equal(1040, too_large.height(), "on both axes");
assert_equal(0, too_large.left, "and positioned at the origin rather than hanging off");
// Which saved rects are corrected at all. Reaching a display is not on its own enough to be usable.
assert_equal(true, platform::window_needs_refit({2200, 300, 3000, 900}, work, false),
"a rect saved on a display that is gone");
assert_equal(false, platform::window_needs_refit({100, 100, 900, 700}, work, true),
"an ordinary rect is restored exactly as it was saved");
assert_equal(false, platform::window_needs_refit({1500, 100, 2400, 700}, work, true),
"and so is one deliberately straddling two displays");
assert_equal(true, platform::window_needs_refit({0, 0, 3840, 2160}, work, true),
"a window saved on a larger display is clamped to this one");
assert_equal(true, platform::window_needs_refit({1900, 100, 2700, 700}, work, true),
"a sliver at the edge leaves nothing to grab");
assert_equal(true, platform::window_needs_refit({100, 1030, 900, 1630}, work, true),
"and neither has one pushed off the bottom");
}
static void should_classify_dxgi_device_loss()
{
assert_equal(true, is_device_loss_error(DXGI_ERROR_DEVICE_REMOVED), "device removed");
assert_equal(true, is_device_loss_error(DXGI_ERROR_DEVICE_RESET), "device reset");
assert_equal(true, is_device_loss_error(DXGI_ERROR_DEVICE_HUNG), "device hung");
assert_equal(true, is_device_loss_error(DXGI_ERROR_DRIVER_INTERNAL_ERROR), "driver internal error");
assert_equal(false, is_device_loss_error(DXGI_STATUS_OCCLUDED), "occlusion is not device loss");
assert_equal(false, is_device_loss_error(E_FAIL), "generic failure is not device loss");
assert_equal(false, is_device_loss_error(S_OK), "success is not device loss");
}
static void should_use_screen_coordinates_for_saved_window_placement()
{
const recti placement(100, 100, 900, 700);
const auto top_taskbar = platform::placement_to_screen(placement, {0, 0, 1920, 1080}, {0, 40, 1920, 1080});
assert_equal(140, top_taskbar.top, "workspace origin is below a top taskbar");
assert_equal(800, top_taskbar.width(), "conversion does not resize the window");
const auto left_taskbar = platform::placement_to_screen(placement, {0, 0, 1920, 1080}, {48, 0, 1920, 1080});
assert_equal(148, left_taskbar.left, "workspace origin is beside a left taskbar");
const auto secondary = platform::placement_to_screen({-1800, 100, -1000, 700}, {-1920, 0, 0, 1080},
{-1920, 40, 0, 1080});
assert_equal(-1800, secondary.left, "secondary-monitor screen origin is retained");
assert_equal(140, secondary.top, "secondary-monitor work offset is applied once");
// Full screen goes the other way: monitor bounds in screen coordinates become a placement.
const auto full_screen = platform::screen_to_placement({0, 0, 1920, 1080}, {0, 0, 1920, 1080}, {48, 0, 1920, 1080});
assert_equal(-48, full_screen.left, "monitor bounds start left of a left taskbar's workspace origin");
assert_equal(1920, full_screen.width(), "conversion does not resize a full-screen window");
const auto round_trip = platform::screen_to_placement(secondary, {-1920, 0, 0, 1080}, {-1920, 40, 0, 1080});
assert_equal(100, round_trip.top, "screen to placement inverts placement to screen");
assert_equal(-1800, round_trip.left, "on both axes");
}
static void should_not_treat_pointer_wheel_ids_as_modifiers()
{
for (const auto message : {0x024eu, 0x024fu})
{
const auto none = platform::wheel_key_state(message, MAKEWPARAM(12, WHEEL_DELTA), {});
assert_equal(false, none.control || none.shift, "pointer ID bits are not modifier flags");
const auto held = platform::wheel_key_state(message, MAKEWPARAM(1, WHEEL_DELTA), {true, true, false});
assert_equal(true, held.control && held.shift, "pointer wheels use keyboard state");
}
const auto mouse = platform::wheel_key_state(WM_MOUSEWHEEL, MAKEWPARAM(MK_CONTROL, WHEEL_DELTA), {});
assert_equal(true, mouse.control, "mouse wheel flags retain their existing meaning");
assert_equal(false, mouse.shift, "unpressed mouse modifiers stay clear");
}
// A machine with no working GPU driver is handed WARP posing as a hardware adapter. Drawn through, it
// redrew the whole window on the CPU every frame, so it is recognised and the CPU backend used instead.
static void should_recognise_the_basic_render_driver()
{
assert_equal(true, is_software_adapter(0x1414, 0x8c, 0), "the Microsoft Basic Render Driver");
assert_equal(true, is_software_adapter(0x10de, 0x2684, DXGI_ADAPTER_FLAG_SOFTWARE),
"any adapter that says it is software");
assert_equal(false, is_software_adapter(0x8086, 0x3ea0, 0), "an Intel integrated GPU");
assert_equal(false, is_software_adapter(0x10de, 0x2684, 0), "an NVIDIA card");
assert_equal(false, is_software_adapter(0x1414, 0x5353, 0), "the vendor alone does not make an adapter WARP");
}
// NV12 and P010 are drawn by sampling views of their planes. A device that could only hold the format
// accepted the texture, refused its views, and the picture never appeared.
static void should_need_sampling_for_planar_video()
{
assert_equal(false, can_sample_texture_format(D3D11_FORMAT_SUPPORT_TEXTURE2D), "holding the format is not enough");
assert_equal(false, can_sample_texture_format(D3D11_FORMAT_SUPPORT_SHADER_SAMPLE),
"nor is sampling a format that cannot be a 2D texture");
assert_equal(false, can_sample_texture_format(0), "nothing supported");
assert_equal(true,
can_sample_texture_format(D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_SHADER_SAMPLE |
D3D11_FORMAT_SUPPORT_DECODER_OUTPUT),
"a device that samples it");
}
// Intel parts report the 128MB carved out at boot as dedicated memory, and allocate textures from shared
// memory. Read as the card's own, it held every Intel laptop at the 64MB floor, and a photo over 16
// megapixels was shown downscaled even at 100%.
static void should_count_shared_memory_for_an_integrated_gpu()
{
constexpr uint64_t mb = 1024ull * 1024ull;
constexpr uint64_t gb = 1024ull * mb;
constexpr auto laptop = static_cast<int64_t>(16 * gb);
const auto budget = [](const uint64_t gpu_bytes, const int64_t total_phys)
{
return static_cast<uint64_t>(calc_texture_budget(gpu_bytes, total_phys));
};
assert_equal(128 * mb + 8 * gb, gpu_texture_memory(128 * mb, 8 * gb, true),
"an integrated part uses shared memory");
assert_equal(8 * gb, gpu_texture_memory(8 * gb, 16 * gb, false), "a discrete card uses its own");
assert_equal(8 * gb, gpu_texture_memory(0, 8 * gb, false), "an adapter with none of its own still has somewhere");
assert_equal(128 * mb, budget(gpu_texture_memory(128 * mb, 8 * gb, true), laptop),
"an integrated laptop GPU is given the full ceiling");
assert_equal(64 * mb, budget(128 * mb, laptop), "which the carve-out alone held at the floor");
assert_equal(64 * mb, budget(512 * mb, laptop), "a small discrete card is still held back");
assert_equal(128 * mb, budget(8 * gb, laptop), "a large card is held to the ceiling");
assert_equal(96 * mb, budget(gpu_texture_memory(128 * mb, 768 * mb, true), static_cast<int64_t>(1536 * mb)),
"physical memory still bounds a small machine");
assert_equal(128 * mb, budget(0, 0), "with nothing to ask, the shipped ceiling");
}
static bool same_adapter(const LUID a, const LUID b)
{
return a.LowPart == b.LowPart && a.HighPart == b.HighPart;
}
struct listed_adapter
{
LUID luid = {};
bool makes_video_devices = false;
};
// In the order Windows lists them, so the first is the default FFmpeg would take when left to choose.
static std::vector<listed_adapter> list_adapters()
{
std::vector<listed_adapter> result;
ComPtr<IDXGIFactory1> factory;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return result;
ComPtr<IDXGIAdapter1> adapter;
for (UINT i = 0; SUCCEEDED(factory->EnumAdapters1(i, &adapter)); ++i)
{
DXGI_ADAPTER_DESC1 desc = {};
if (FAILED(adapter->GetDesc1(&desc))) continue;
ComPtr<ID3D11Device> device;
const auto makes_video = SUCCEEDED(D3D11CreateDevice(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT, nullptr, 0,
D3D11_SDK_VERSION, &device, nullptr, nullptr));
result.push_back({desc.AdapterLuid, makes_video});
}
return result;
}
static LUID adapter_of(const AVBufferRef* hw_device)
{
const auto* const ctx = reinterpret_cast<const AVHWDeviceContext*>(hw_device->data);
const auto* const hwctx = static_cast<const AVD3D11VADeviceContext*>(ctx->hwctx);
ComPtr<IDXGIDevice> dxgi_device;
ComPtr<IDXGIAdapter> adapter;
DXGI_ADAPTER_DESC desc = {};
if (hwctx->device && SUCCEEDED(hwctx->device->QueryInterface(IID_PPV_ARGS(&dxgi_device))) &&
SUCCEEDED(dxgi_device->GetAdapter(&adapter)) && SUCCEEDED(adapter->GetDesc(&desc)))
{
return desc.AdapterLuid;
}
return {};
}
// Left to itself FFmpeg made the decode device on whichever adapter was the default when the first
// video opened. Where that was not the renderer's, the shared-texture bridge could not cross to it and
// every frame went through system memory. The adapters asked about here are the last ones listed,
// which are never that default wherever there are two: on every machine since Windows 8 the
// Microsoft Basic Render Driver is listed after the GPUs.
static void should_create_the_decode_device_on_the_render_adapter()
{
assert_equal(true, av_platform_create_hw_device(AV_HWDEVICE_TYPE_DXVA2) == nullptr,
"a device the renderer cannot present from is not made");
const auto adapters = list_adapters();
if (adapters.empty()) return;
const auto last = adapters.back().luid;
DXGI_ADAPTER_DESC1 found = {};
const auto adapter = find_adapter_by_luid(last);
assert_equal(true, adapter && SUCCEEDED(adapter->GetDesc1(&found)) && same_adapter(last, found.AdapterLuid),
"an adapter is found by what it is, not where it is listed");
assert_equal(true, find_adapter_by_luid({}) == nullptr, "and none for the renderer that is not there");
const auto video = std::ranges::find_if(adapters.rbegin(), adapters.rend(),
[](const listed_adapter& a) { return a.makes_video_devices; });
if (video == adapters.rend()) return;
const auto previous = render_adapter();
const auto previous_device = render_device();
const core::scope_exit restore([previous, previous_device]
{
publish_render_adapter(previous);
publish_render_device(previous_device);
});
publish_render_adapter(video->luid);
// Without a render device to decode on, the decoder is given a device of its own on that adapter.
publish_render_device(nullptr);
auto* device = av_platform_create_hw_device(AV_HWDEVICE_TYPE_D3D11VA);
const core::scope_exit release([&device] { av_buffer_unref(&device); });
assert_equal(true, device != nullptr, "a decode device is made");
if (!device) return;
assert_equal(true, same_adapter(video->luid, adapter_of(device)), "on the adapter the renderer draws with");
assert_equal(true, av_platform_hw_device_usable(device), "and FFmpeg can decode on it");
}
// Decoding on the render device copies each picture once, on one device, into the texture the renderer
// draws, where a device of its own costs two copies a frame through a texture shared between the devices.
// So a render device that carries the video interfaces is the decode device.
static void should_decode_on_the_render_device_when_it_carries_video()
{
const auto adapters = list_adapters();
assert_equal(true, !adapters.empty(), "an adapter is listed");
const auto video = std::ranges::find_if(adapters, [](const listed_adapter& a) { return a.makes_video_devices; });
if (video == adapters.end()) return;
const auto adapter = find_adapter_by_luid(video->luid);
ComPtr<ID3D11Device> render;
assert_equal(true,
adapter &&
SUCCEEDED(D3D11CreateDevice(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,
D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_VIDEO_SUPPORT,
nullptr, 0, D3D11_SDK_VERSION, &render, nullptr, nullptr)),
"a render device with the video interfaces");
if (!render) return;
const auto previous = render_adapter();
const auto previous_device = render_device();
const core::scope_exit restore([previous, previous_device]
{
publish_render_adapter(previous);
publish_render_device(previous_device);
});
publish_render_adapter(video->luid);
publish_render_device(render);
auto* device = av_platform_create_hw_device(AV_HWDEVICE_TYPE_D3D11VA);
const core::scope_exit release([&device] { av_buffer_unref(&device); });
assert_equal(true, device != nullptr, "a decode device is made");
if (!device) return;
const auto* const ctx = reinterpret_cast<const AVHWDeviceContext*>(device->data);
const auto* const hwctx = static_cast<const AVD3D11VADeviceContext*>(ctx->hwctx);
assert_equal(true, hwctx->device == render.Get(), "and it is the render device itself");
assert_equal(true, av_platform_hw_device_usable(device), "which FFmpeg can decode on");
}
// The renderer copies a hardware picture out of the decoder's surface under the lock the decoder takes
// around it, so the lock a picture lends must be that device's own: its functions, with its context.
static void should_lend_the_renderer_the_decode_device_lock()
{
auto* device = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA);
assert_equal(true, device != nullptr, "a D3D11VA device context");
if (!device) return;
const core::scope_exit release_device([&device] { av_buffer_unref(&device); });
auto held = 0;
auto* const hwctx = static_cast<AVD3D11VADeviceContext*>(reinterpret_cast<AVHWDeviceContext*>(device->data)->hwctx);
hwctx->lock = [](void* context)
{
++*static_cast<int*>(context);
};
hwctx->unlock = [](void* context)
{
--*static_cast<int*>(context);
};
hwctx->lock_ctx = &held;
auto* frames = av_hwframe_ctx_alloc(device);
assert_equal(true, frames != nullptr, "a frames context on it");
if (!frames) return;
const core::scope_exit release_frames([&frames] { av_buffer_unref(&frames); });
render::video_picture picture;
av_platform_decode_lock(reinterpret_cast<const AVHWFramesContext*>(frames->data), picture);
assert_equal(true, picture.lock != nullptr && picture.unlock != nullptr, "the picture carries a lock");
if (picture.lock && picture.unlock)
{
picture.lock(picture.lock_context);
assert_equal(1, held, "which is the device's own");
picture.unlock(picture.lock_context);
assert_equal(0, held, "and is released with the device's own");
}
render::video_picture none;
av_platform_decode_lock(nullptr, none);
assert_equal(true, none.lock == nullptr && none.unlock == nullptr, "a picture with no device carries none");
}
// Defined in platform_win_sound.cpp: what the device's format() answers for its mix format.
platform::audio_output_format wasapi_mix_format(const WAVEFORMATEX* format);
// An endpoint's mix format is a WAVEFORMATEX, extended or not, which the device describes as a sample
// format and the channels with the speakers they feed. An extension too short to hold its speaker mask
// names neither its samples nor its speakers.
static void should_describe_a_wasapi_mix_format()
{
constexpr GUID ieee_float = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
constexpr GUID pcm = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
WAVEFORMATEXTENSIBLE mix = {};
mix.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
mix.Format.nChannels = 6;
mix.Format.nSamplesPerSec = 48000;
mix.Format.wBitsPerSample = 32;
mix.Format.nBlockAlign = 24;
mix.Format.nAvgBytesPerSec = 48000 * 24;
mix.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
mix.Samples.wValidBitsPerSample = 32;
mix.dwChannelMask = 0x3F; // 5.1: front left, right and centre, LFE, back left and right
mix.SubFormat = ieee_float;
const auto described = wasapi_mix_format(&mix.Format);
assert_equal(48000u, described.sample_rate, "the endpoint's rate");
assert_equal(true, described.sample_format == platform::audio_sample_format::float32, "its float samples");
assert_equal(6u, described.channels, "its channels");
assert_equal(true, described.channel_mask == 0x3F, "and the speakers they feed");
mix.SubFormat = pcm;
assert_equal(true, wasapi_mix_format(&mix.Format).sample_format == platform::audio_sample_format::int32,
"32-bit integer samples");
mix.Format.cbSize = 0;
const auto short_extension = wasapi_mix_format(&mix.Format);
assert_equal(true, short_extension.sample_format == platform::audio_sample_format::none,
"an extension too short for its mask names no samples");
assert_equal(true, short_extension.channel_mask == 0, "and no speakers");
WAVEFORMATEX stereo = {};
stereo.wFormatTag = WAVE_FORMAT_PCM;
stereo.nChannels = 2;
stereo.nSamplesPerSec = 44100;
stereo.wBitsPerSample = 16;
stereo.nBlockAlign = 4;
stereo.nAvgBytesPerSec = 44100 * 4;
const auto plain = wasapi_mix_format(&stereo);
assert_equal(true, plain.sample_format == platform::audio_sample_format::int16, "a plain format's 16-bit samples");
assert_equal(2u, plain.channels, "on its channels");
assert_equal(true, plain.channel_mask == 0, "names no speakers");
stereo.wBitsPerSample = 24;
stereo.nBlockAlign = 6;
assert_equal(true, wasapi_mix_format(&stereo).sample_format == platform::audio_sample_format::none,
"packed 24-bit samples are not spoken");
assert_equal(0u, wasapi_mix_format(nullptr).channels, "and no format describes no channels");
}
// WARP is in every Windows since 8 and needs no GPU, so the texture tests run the same everywhere.
static ComPtr<ID3D11Device> create_warp_device(ComPtr<ID3D11DeviceContext>& context)
{
ComPtr<ID3D11Device> device;
if (FAILED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT, nullptr, 0,
D3D11_SDK_VERSION, &device, nullptr, &context)))
{
return {};
}
return device;
}
static ComPtr<ID3D11Texture2D> create_plain_texture(ID3D11Device* device, const sizei dims)
{
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = dims.cx;
desc.Height = dims.cy;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
ComPtr<ID3D11Texture2D> texture;
device->CreateTexture2D(&desc, nullptr, &texture);
return texture;
}
// Thumbnails come and go by the hundred as a listing scrolls, and each was a CreateTexture2D on the UI
// thread. A released texture is handed to the next one of its exact size and format - but never while
// a scene a redraw can replay still draws it, or that redraw would show the new picture in its place.
static void should_recycle_a_texture_only_when_nothing_draws_it()
{
ComPtr<ID3D11DeviceContext> context;
const auto device = create_warp_device(context);
assert_equal(true, device != nullptr, "a WARP device");
if (!device) return;
constexpr sizei thumb = {64, 48};
constexpr auto argb = core::texture_format::ARGB;
const auto thumb_bytes = d3d11_texture_pool::texture_bytes(thumb, argb);
d3d11_texture_pool pool(thumb_bytes * 8);
const auto released = create_plain_texture(device.Get(), thumb);
pool.give({thumb, argb, released, {}});
assert_equal(true, pool.take({64, 50}, argb).texture == nullptr, "a different size is not handed out");
assert_equal(true, pool.take(thumb, core::texture_format::RGB).texture == nullptr, "nor a different format");
assert_equal(true, pool.take(thumb, argb).texture == released, "the same size and format is");
assert_equal(0_z, pool.size(), "and leaves the pool");
auto drawn = std::make_shared<const texture_views>();
pool.give({thumb, argb, released, drawn});
assert_equal(true, pool.take(thumb, argb).texture == nullptr, "not while a scene still draws it");
drawn.reset();
assert_equal(true, pool.take(thumb, argb).texture == released, "but once nothing does");
std::vector<ComPtr<ID3D11Texture2D>> made;
for (auto i = 0; i < 9; ++i)
{
made.emplace_back(create_plain_texture(device.Get(), thumb));
pool.give({thumb, argb, made.back(), {}});
}
assert_equal(8_z, pool.size(), "the budget holds eight");
assert_equal(true, pool.bytes() <= thumb_bytes * 8, "and never more than it");
assert_equal(true, pool.take(thumb, argb).texture == made[1], "the oldest went to make room");
const auto photograph = create_plain_texture(device.Get(), {128, 128});
const auto before = pool.size();
pool.give({{128, 128}, argb, photograph, {}});
assert_equal(before, pool.size(), "a texture over an eighth of the budget is not kept");
pool.clear();
assert_equal(0_z, pool.size(), "a cleared pool holds nothing");
assert_equal(0_z, pool.bytes(), "and costs nothing");
}
static bool texture_holds(ID3D11Device* device, ID3D11DeviceContext* context, ID3D11Texture2D* texture,
const uint32_t bgra)
{
D3D11_TEXTURE2D_DESC desc = {};
texture->GetDesc(&desc);
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
ComPtr<ID3D11Texture2D> staging;
if (FAILED(device->CreateTexture2D(&desc, nullptr, &staging))) return false;
context->CopyResource(staging.Get(), texture);
D3D11_MAPPED_SUBRESOURCE mapped = {};
if (FAILED(context->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &mapped))) return false;
auto holds = true;
for (UINT y = 0; y < desc.Height && holds; ++y)
{
const auto* const row = reinterpret_cast<const uint32_t*>(static_cast<const uint8_t*>(mapped.pData) +
static_cast<size_t>(mapped.RowPitch) * y);
holds = std::all_of(row, row + desc.Width, [bgra](const uint32_t px) { return px == bgra; });
}
context->Unmap(staging.Get(), 0);
return holds;
}
// A recycled texture still holds the last picture until it is overwritten, so the reuse is only right if
// the new picture lands in it in full. Read back from the texture the second picture was given, which is
// the one the first picture released.
static void should_overwrite_a_recycled_texture_with_the_new_picture()
{
ComPtr<ID3D11DeviceContext> context;
const auto device = create_warp_device(context);
assert_equal(true, device != nullptr, "a WARP device");
if (!device) return;
const auto f = std::make_shared<factories>();
f->d3d_device = device;
f->d3d_context = context;
f->texture_pool = std::make_unique<d3d11_texture_pool>(1024 * 1024);
constexpr sizei dims = {64, 48};
constexpr auto stride = static_cast<size_t>(dims.cx) * 4;
constexpr auto bytes = stride * dims.cy;
constexpr uint32_t red = 0xFFFF0000u;
constexpr uint32_t green = 0xFF00FF00u;
const std::vector<uint32_t> first_picture(static_cast<size_t>(dims.cx) * dims.cy, red);
const std::vector<uint32_t> second_picture(static_cast<size_t>(dims.cx) * dims.cy, green);
const auto created_before = render::gpu_perf.textures_created.load();
const auto reused_before = render::gpu_perf.textures_reused.load();
{
const auto first = d3d11_create_texture(f);
assert_equal(true,
first->update(dims, core::texture_format::ARGB, core::orientation::top_left,
reinterpret_cast<const uint8_t*>(first_picture.data()), stride,
bytes) == render::texture_update_result::tex_created,
"the first picture makes a texture");
}
assert_equal(1_z, f->texture_pool->size(), "which is kept when it is released");
auto kept = f->texture_pool->take(dims, core::texture_format::ARGB);
const auto first_texture = kept.texture;
f->texture_pool->give(std::move(kept));
{
const auto second = d3d11_create_texture(f);
assert_equal(true,
second->update(dims, core::texture_format::ARGB, core::orientation::top_left,
reinterpret_cast<const uint8_t*>(second_picture.data()), stride,
bytes) == render::texture_update_result::tex_created,
"a texture taken from the pool is new to its caller");
assert_equal(0_z, f->texture_pool->size(), "the second picture took it");
}
assert_equal(1ull, render::gpu_perf.textures_created.load() - created_before, "one texture was made");
assert_equal(1ull, render::gpu_perf.textures_reused.load() - reused_before, "and one reused");
const auto returned = f->texture_pool->take(dims, core::texture_format::ARGB);
assert_equal(true, returned.texture == first_texture, "the second picture was drawn into the first texture");
assert_equal(true, returned.texture && texture_holds(device.Get(), context.Get(), returned.texture.Get(), green),
"and holds the new picture, not the old one");
}
// A decoded frame as the renderers see it, described by the test rather than by a decoder.
class fake_video_frame final : public render::video_frame
{
public:
core::const_surface_ptr prepared;
render::video_picture described;
mutable int cpu_requests = 0;
core::const_surface_ptr prepared_surface() const override { return prepared; }
render::video_picture picture() const override { return described; }
void request_cpu_frames() const override { ++cpu_requests; }
};
// Converts any frame into a 32 x 16 picture, and counts the frames it was asked for.
class fake_frame_scaler final : public render::surface_scaler
{
public:
std::shared_ptr<int> frames;
bool scale_surface(const core::const_surface_ptr&, core::surface_ptr&, sizei, bool) override { return false; }
bool convert_yuv_surface(const core::surface&, const core::surface_ptr&) override { return false; }
bool scale_surface(const render::video_frame&, const core::surface_ptr& surface_out) override
{
++*frames;
return surface_out->alloc(32, 16, core::texture_format::RGB) != nullptr;
}
};
static std::function<std::unique_ptr<render::surface_scaler>()> make_fake_frame_scaler(
const std::shared_ptr<int>& frames)
{
return [frames]() -> std::unique_ptr<render::surface_scaler>
{
auto s = std::make_unique<fake_frame_scaler>();
s->frames = frames;
return s;
};
}
static std::shared_ptr<fake_video_frame> make_prepared_video_frame(const sizei dimensions)
{
auto frame = std::make_shared<fake_video_frame>();
const auto picture = std::make_shared<core::surface>();
picture->alloc(dimensions, core::texture_format::RGB);
frame->prepared = picture;
return frame;
}
// A texture presents a video frame through the frame's interface alone: the picture the decode thread
// prepared when there is one, and otherwise one the application's scaler converts. A hardware picture
// is copied only under its decode device's lock, so one that brings no lock is refused, and with no
// scaler there is nothing to fall back to.
static void should_present_a_video_frame_through_its_interface()
{
ComPtr<ID3D11DeviceContext> context;
const auto device = create_warp_device(context);
assert_equal(true, device != nullptr, "a WARP device");
if (!device) return;
const auto f = std::make_shared<factories>();
f->d3d_device = device;
f->d3d_context = context;
f->texture_pool = std::make_unique<d3d11_texture_pool>(1024 * 1024);
const auto frames_converted = std::make_shared<int>(0);
const auto restore_scaler = render::make_surface_scaler;
const core::scope_exit restore([restore_scaler] { render::make_surface_scaler = restore_scaler; });
render::make_surface_scaler = make_fake_frame_scaler(frames_converted);
const auto texture = d3d11_create_texture(f);
assert_equal(true,
texture->update(make_prepared_video_frame({64, 48})) == render::texture_update_result::tex_created,
"a prepared picture is uploaded");
assert_equal(true, texture->dimensions() == sizei{64, 48}, "as it was prepared");
assert_equal(0, *frames_converted, "with nothing converted");
const auto unprepared = std::make_shared<fake_video_frame>();
assert_equal(true, texture->update(unprepared) != render::texture_update_result::failed,
"a picture in system memory is converted by the scaler");
assert_equal(true, texture->dimensions() == sizei{32, 16}, "and uploaded as converted");
assert_equal(1, *frames_converted, "once");
assert_equal(0, unprepared->cpu_requests, "which asks the decoder for nothing");
const auto unlocked = std::make_shared<fake_video_frame>();
auto texture_name = 0;
unlocked->described.texture = &texture_name;
assert_equal(true, d3d11_create_texture(f)->update(unlocked) == render::texture_update_result::failed,
"a hardware picture without its device's lock is refused");
assert_equal(1, *frames_converted, "rather than converted");
render::make_surface_scaler = nullptr;
assert_equal(true, d3d11_create_texture(f)->update(unprepared) == render::texture_update_result::failed,
"and with no scaler a picture in system memory cannot be presented");
}
// The software renderer never samples the decoder's surfaces, so a hardware picture is converted by
// the scaler like any other, and the decode thread is asked to deliver the rest of the clip in system
// memory instead.
static void should_present_a_video_frame_in_software_through_its_interface()
{
const auto frames_converted = std::make_shared<int>(0);
const auto restore_scaler = render::make_surface_scaler;
const core::scope_exit restore([restore_scaler] { render::make_surface_scaler = restore_scaler; });
render::make_surface_scaler = make_fake_frame_scaler(frames_converted);
const auto renderer = create_software_draw_context(nullptr, nullptr, false, render::default_font_size);
const auto texture = renderer->create_texture();
assert_equal(true,
texture->update(make_prepared_video_frame({64, 48})) == render::texture_update_result::tex_created,
"a prepared picture is copied");
assert_equal(true, texture->dimensions() == sizei{64, 48}, "as it was prepared");
assert_equal(0, *frames_converted, "with nothing converted");
const auto unprepared = std::make_shared<fake_video_frame>();
assert_equal(true, texture->update(unprepared) == render::texture_update_result::tex_created,
"a picture in system memory is converted by the scaler");
assert_equal(true, texture->dimensions() == sizei{32, 16}, "and held as converted");
assert_equal(1, *frames_converted, "once");
assert_equal(0, unprepared->cpu_requests, "which asks the decoder for nothing");
const auto hardware = std::make_shared<fake_video_frame>();
auto texture_name = 0;
hardware->described.texture = &texture_name;
assert_equal(true, texture->update(hardware) == render::texture_update_result::tex_updated,
"a hardware picture is converted too");
assert_equal(2, *frames_converted, "by the scaler");
assert_equal(1, hardware->cpu_requests, "and the decoder is asked for pictures in system memory");
render::make_surface_scaler = nullptr;
assert_equal(true, renderer->create_texture()->update(unprepared) == render::texture_update_result::failed,
"with no scaler a picture in system memory cannot be presented");
}
static void should_suppress_gpu_for_recovery_session()
{
platform::suppress_crash_guard(platform::crash_guard::gpu_render, true);
// Suppression is process-global; a failing assertion below must not leave it on.
const core::scope_exit restore_guard([]
{ platform::suppress_crash_guard(platform::crash_guard::gpu_render, false); });
assert_equal(true, platform::crash_guard_suppressed(platform::crash_guard::gpu_render),
"GPU suppressed during recovery");
assert_equal(false, platform::crash_guard_suppressed(platform::crash_guard::hw_video_decode),
"decode suppression remains independent");
platform::suppress_crash_guard(platform::crash_guard::gpu_render, false);
assert_equal(false, platform::crash_guard_suppressed(platform::crash_guard::gpu_render),
"GPU suppression can be cleared");
}
// Issue #219 - Font glyph fallback for missing (Hangul) glyphs.
// The custom UI renders text with the system font "Calibri" (factories::font_face),