forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraster_thread_merger_unittests.cc
661 lines (535 loc) · 21.9 KB
/
raster_thread_merger_unittests.cc
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#define FML_USED_ON_EMBEDDER
#include "flutter/fml/raster_thread_merger.h"
#include <thread>
#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/thread.h"
#include "gtest/gtest.h"
namespace fml {
namespace testing {
/// A mock task queue NOT calling MessageLoop->Run() in thread
struct TaskQueueWrapper {
fml::MessageLoop* loop = nullptr;
/// The waiter for message loop initialized ok
fml::AutoResetWaitableEvent latch;
/// The waiter for thread finished
fml::AutoResetWaitableEvent term;
/// This field must below latch and term member, because
/// cpp standard reference:
/// non-static data members are initialized in the order they were declared in
/// the class definition
std::thread thread;
TaskQueueWrapper()
: thread([this]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop = &fml::MessageLoop::GetCurrent();
latch.Signal();
term.Wait();
}) {
latch.Wait();
}
~TaskQueueWrapper() {
term.Signal();
thread.join();
}
fml::TaskQueueId GetTaskQueueId() const {
return loop->GetTaskRunner()->GetTaskQueueId();
}
};
TEST(RasterThreadMerger, RemainMergedTillLeaseExpires) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
const size_t kNumFramesMerged = 5;
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->MergeWithLease(kNumFramesMerged);
for (size_t i = 0; i < kNumFramesMerged; i++) {
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->DecrementLease();
}
ASSERT_FALSE(raster_thread_merger->IsMerged());
}
TEST(RasterThreadMerger, IsNotOnRasterizingThread) {
fml::MessageLoop* loop1 = nullptr;
fml::AutoResetWaitableEvent latch1;
std::thread thread1([&loop1, &latch1]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop1 = &fml::MessageLoop::GetCurrent();
loop1->GetTaskRunner()->PostTask([&]() { latch1.Signal(); });
loop1->Run();
});
fml::MessageLoop* loop2 = nullptr;
fml::AutoResetWaitableEvent latch2;
std::thread thread2([&loop2, &latch2]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop2 = &fml::MessageLoop::GetCurrent();
loop2->GetTaskRunner()->PostTask([&]() { latch2.Signal(); });
loop2->Run();
});
latch1.Wait();
latch2.Wait();
fml::TaskQueueId qid1 = loop1->GetTaskRunner()->GetTaskQueueId();
fml::TaskQueueId qid2 = loop2->GetTaskRunner()->GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
fml::CountDownLatch pre_merge(2), post_merge(2), post_unmerge(2);
loop1->GetTaskRunner()->PostTask([&]() {
ASSERT_FALSE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
pre_merge.CountDown();
});
loop2->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_FALSE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid2);
pre_merge.CountDown();
});
pre_merge.Wait();
raster_thread_merger->MergeWithLease(1);
loop1->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
post_merge.CountDown();
});
loop2->GetTaskRunner()->PostTask([&]() {
// this will be false since this is going to be run
// on loop1 really.
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
post_merge.CountDown();
});
post_merge.Wait();
raster_thread_merger->DecrementLease();
loop1->GetTaskRunner()->PostTask([&]() {
ASSERT_FALSE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid1);
post_unmerge.CountDown();
});
loop2->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_FALSE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid2);
post_unmerge.CountDown();
});
post_unmerge.Wait();
loop1->GetTaskRunner()->PostTask([&]() { loop1->Terminate(); });
loop2->GetTaskRunner()->PostTask([&]() { loop2->Terminate(); });
thread1.join();
thread2.join();
}
TEST(RasterThreadMerger, LeaseExtension) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
const size_t kNumFramesMerged = 5;
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->MergeWithLease(kNumFramesMerged);
// let there be one more turn till the leases expire.
for (size_t i = 0; i < kNumFramesMerged - 1; i++) {
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->DecrementLease();
}
// extend the lease once.
raster_thread_merger->ExtendLeaseTo(kNumFramesMerged);
// we will NOT last for 1 extra turn, we just set it.
for (size_t i = 0; i < kNumFramesMerged; i++) {
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->DecrementLease();
}
ASSERT_FALSE(raster_thread_merger->IsMerged());
}
TEST(RasterThreadMerger, WaitUntilMerged) {
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger;
fml::AutoResetWaitableEvent create_thread_merger_latch;
fml::MessageLoop* loop_platform = nullptr;
fml::AutoResetWaitableEvent latch_platform;
fml::AutoResetWaitableEvent term_platform;
fml::AutoResetWaitableEvent latch_merged;
std::thread thread_platform([&]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop_platform = &fml::MessageLoop::GetCurrent();
latch_platform.Signal();
create_thread_merger_latch.Wait();
raster_thread_merger->WaitUntilMerged();
latch_merged.Signal();
term_platform.Wait();
});
const size_t kNumFramesMerged = 5;
fml::MessageLoop* loop_raster = nullptr;
fml::AutoResetWaitableEvent term_raster;
std::thread thread_raster([&]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop_raster = &fml::MessageLoop::GetCurrent();
latch_platform.Wait();
fml::TaskQueueId qid_platform =
loop_platform->GetTaskRunner()->GetTaskQueueId();
fml::TaskQueueId qid_raster =
loop_raster->GetTaskRunner()->GetTaskQueueId();
raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid_platform, qid_raster);
ASSERT_FALSE(raster_thread_merger->IsMerged());
create_thread_merger_latch.Signal();
raster_thread_merger->MergeWithLease(kNumFramesMerged);
term_raster.Wait();
});
latch_merged.Wait();
ASSERT_TRUE(raster_thread_merger->IsMerged());
for (size_t i = 0; i < kNumFramesMerged; i++) {
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->DecrementLease();
}
ASSERT_FALSE(raster_thread_merger->IsMerged());
term_platform.Signal();
term_raster.Signal();
thread_platform.join();
thread_raster.join();
}
TEST(RasterThreadMerger, HandleTaskQueuesAreTheSame) {
TaskQueueWrapper queue;
fml::TaskQueueId qid1 = queue.GetTaskQueueId();
fml::TaskQueueId qid2 = qid1;
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
// Statically merged.
ASSERT_TRUE(raster_thread_merger->IsMerged());
// Test decrement lease and unmerge are both no-ops.
// The task queues should be always merged.
const size_t kNumFramesMerged = 5;
raster_thread_merger->MergeWithLease(kNumFramesMerged);
for (size_t i = 0; i < kNumFramesMerged; i++) {
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->DecrementLease();
}
ASSERT_TRUE(raster_thread_merger->IsMerged());
// Wait until merged should also return immediately.
raster_thread_merger->WaitUntilMerged();
ASSERT_TRUE(raster_thread_merger->IsMerged());
}
TEST(RasterThreadMerger, Enable) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
raster_thread_merger->Disable();
raster_thread_merger->MergeWithLease(1);
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->Enable();
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->MergeWithLease(1);
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->DecrementLease();
ASSERT_FALSE(raster_thread_merger->IsMerged());
}
TEST(RasterThreadMerger, Disable) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
raster_thread_merger->Disable();
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->MergeWithLease(1);
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->Enable();
raster_thread_merger->MergeWithLease(1);
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->Disable();
raster_thread_merger->UnMergeNowIfLastOne();
ASSERT_TRUE(raster_thread_merger->IsMerged());
{
auto decrement_result = raster_thread_merger->DecrementLease();
ASSERT_EQ(fml::RasterThreadStatus::kRemainsMerged, decrement_result);
}
ASSERT_TRUE(raster_thread_merger->IsMerged());
raster_thread_merger->Enable();
raster_thread_merger->UnMergeNowIfLastOne();
ASSERT_FALSE(raster_thread_merger->IsMerged());
raster_thread_merger->MergeWithLease(1);
ASSERT_TRUE(raster_thread_merger->IsMerged());
{
auto decrement_result = raster_thread_merger->DecrementLease();
ASSERT_EQ(fml::RasterThreadStatus::kUnmergedNow, decrement_result);
}
ASSERT_FALSE(raster_thread_merger->IsMerged());
}
TEST(RasterThreadMerger, IsEnabled) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
ASSERT_TRUE(raster_thread_merger->IsEnabled());
raster_thread_merger->Disable();
ASSERT_FALSE(raster_thread_merger->IsEnabled());
raster_thread_merger->Enable();
ASSERT_TRUE(raster_thread_merger->IsEnabled());
}
TEST(RasterThreadMerger, TwoMergersWithSameThreadPairShareEnabledState) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto merger1 =
RasterThreadMerger::CreateOrShareThreadMerger(nullptr, qid1, qid2);
const auto merger2 =
RasterThreadMerger::CreateOrShareThreadMerger(merger1, qid1, qid2);
ASSERT_TRUE(merger1->IsEnabled());
ASSERT_TRUE(merger2->IsEnabled());
merger1->Disable();
ASSERT_FALSE(merger1->IsEnabled());
ASSERT_FALSE(merger2->IsEnabled());
merger2->Enable();
ASSERT_TRUE(merger1->IsEnabled());
ASSERT_TRUE(merger2->IsEnabled());
}
TEST(RasterThreadMerger, RunExpiredTasksWhileFirstTaskMergesThreads) {
fml::MessageLoop* loop_platform = nullptr;
fml::AutoResetWaitableEvent latch1;
std::thread thread_platform([&loop_platform, &latch1]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop_platform = &fml::MessageLoop::GetCurrent();
loop_platform->GetTaskRunner()->PostTask([&]() { latch1.Signal(); });
loop_platform->Run();
});
fml::MessageLoop* loop_raster = nullptr;
fml::AutoResetWaitableEvent latch2;
std::thread thread_raster([&loop_raster, &loop_platform, &latch1, &latch2]() {
latch1.Wait();
fml::MessageLoop::EnsureInitializedForCurrentThread();
loop_raster = &fml::MessageLoop::GetCurrent();
fml::TaskQueueId qid_platform =
loop_platform->GetTaskRunner()->GetTaskQueueId();
fml::TaskQueueId qid_raster =
loop_raster->GetTaskRunner()->GetTaskQueueId();
fml::CountDownLatch post_merge(2);
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid_platform, qid_raster);
loop_raster->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_FALSE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid_raster);
raster_thread_merger->MergeWithLease(1);
post_merge.CountDown();
});
loop_raster->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid_platform);
raster_thread_merger->DecrementLease();
post_merge.CountDown();
});
loop_raster->RunExpiredTasksNow();
post_merge.Wait();
latch2.Signal();
});
latch2.Wait();
loop_platform->GetTaskRunner()->PostTask(
[&]() { loop_platform->Terminate(); });
thread_platform.join();
thread_raster.join();
}
TEST(RasterThreadMerger, RunExpiredTasksWhileFirstTaskUnMergesThreads) {
fml::Thread platform_thread("test_platform_thread");
fml::AutoResetWaitableEvent raster_latch;
std::thread thread_raster([&]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
fml::MessageLoop* loop_raster = &fml::MessageLoop::GetCurrent();
fml::TaskQueueId qid_platform =
platform_thread.GetTaskRunner()->GetTaskQueueId();
fml::TaskQueueId qid_raster =
loop_raster->GetTaskRunner()->GetTaskQueueId();
fml::AutoResetWaitableEvent merge_latch;
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid_platform, qid_raster);
loop_raster->GetTaskRunner()->PostTask([&]() {
raster_thread_merger->MergeWithLease(1);
merge_latch.Signal();
});
loop_raster->RunExpiredTasksNow();
merge_latch.Wait();
// threads should be merged at this point.
fml::AutoResetWaitableEvent unmerge_latch;
loop_raster->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_TRUE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid_platform);
raster_thread_merger->DecrementLease();
unmerge_latch.Signal();
});
fml::AutoResetWaitableEvent post_unmerge_latch;
loop_raster->GetTaskRunner()->PostTask([&]() {
ASSERT_TRUE(raster_thread_merger->IsOnRasterizingThread());
ASSERT_FALSE(raster_thread_merger->IsOnPlatformThread());
ASSERT_EQ(fml::MessageLoop::GetCurrentTaskQueueId(), qid_raster);
post_unmerge_latch.Signal();
});
unmerge_latch.Wait();
loop_raster->RunExpiredTasksNow();
post_unmerge_latch.Wait();
raster_latch.Signal();
});
raster_latch.Wait();
thread_raster.join();
}
TEST(RasterThreadMerger, SetMergeUnmergeCallback) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
const auto raster_thread_merger =
fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
int callbacks = 0;
raster_thread_merger->SetMergeUnmergeCallback(
[&callbacks]() { callbacks++; });
ASSERT_EQ(0, callbacks);
raster_thread_merger->MergeWithLease(1);
ASSERT_EQ(1, callbacks);
raster_thread_merger->DecrementLease();
ASSERT_EQ(2, callbacks);
}
TEST(RasterThreadMerger, MultipleMergersCanMergeSameThreadPair) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
// Two mergers will share one same inner merger
const auto raster_thread_merger1 =
fml::RasterThreadMerger::CreateOrShareThreadMerger(nullptr, qid1, qid2);
const auto raster_thread_merger2 =
fml::RasterThreadMerger::CreateOrShareThreadMerger(raster_thread_merger1,
qid1, qid2);
const size_t kNumFramesMerged = 5;
ASSERT_FALSE(raster_thread_merger1->IsMerged());
ASSERT_FALSE(raster_thread_merger2->IsMerged());
// Merge using the first merger
raster_thread_merger1->MergeWithLease(kNumFramesMerged);
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
// let there be one more turn till the leases expire.
for (size_t i = 0; i < kNumFramesMerged - 1; i++) {
// Check merge state using the two merger
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
raster_thread_merger1->DecrementLease();
}
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
// extend the lease once with the first merger
raster_thread_merger1->ExtendLeaseTo(kNumFramesMerged);
// we will NOT last for 1 extra turn, we just set it.
for (size_t i = 0; i < kNumFramesMerged; i++) {
// Check merge state using the two merger
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
raster_thread_merger1->DecrementLease();
}
ASSERT_FALSE(raster_thread_merger1->IsMerged());
ASSERT_FALSE(raster_thread_merger2->IsMerged());
}
TEST(RasterThreadMerger, TheLastCallerOfMultipleMergersCanUnmergeNow) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
// Two mergers will share one same inner merger
const auto raster_thread_merger1 =
fml::RasterThreadMerger::CreateOrShareThreadMerger(nullptr, qid1, qid2);
const auto raster_thread_merger2 =
fml::RasterThreadMerger::CreateOrShareThreadMerger(raster_thread_merger1,
qid1, qid2);
const size_t kNumFramesMerged = 5;
ASSERT_FALSE(raster_thread_merger1->IsMerged());
ASSERT_FALSE(raster_thread_merger2->IsMerged());
// Merge using the mergers
raster_thread_merger1->MergeWithLease(kNumFramesMerged);
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
// Extend the second merger's lease
raster_thread_merger2->ExtendLeaseTo(kNumFramesMerged);
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
// Two callers state becomes one caller left.
raster_thread_merger1->UnMergeNowIfLastOne();
// Check if still merged
ASSERT_TRUE(raster_thread_merger1->IsMerged());
ASSERT_TRUE(raster_thread_merger2->IsMerged());
// One caller state becomes no callers left.
raster_thread_merger2->UnMergeNowIfLastOne();
// Check if unmerged
ASSERT_FALSE(raster_thread_merger1->IsMerged());
ASSERT_FALSE(raster_thread_merger2->IsMerged());
}
/// This case tests multiple standalone engines using independent merger to
/// merge two different raster threads into the same platform thread.
TEST(RasterThreadMerger,
TwoIndependentMergersCanMergeTwoDifferentThreadsIntoSamePlatformThread) {
TaskQueueWrapper queue1;
TaskQueueWrapper queue2;
TaskQueueWrapper queue3;
fml::TaskQueueId qid1 = queue1.GetTaskQueueId();
fml::TaskQueueId qid2 = queue2.GetTaskQueueId();
fml::TaskQueueId qid3 = queue3.GetTaskQueueId();
// Two mergers will NOT share same inner merger
const auto merger_from_2_to_1 =
fml::RasterThreadMerger::CreateOrShareThreadMerger(nullptr, qid1, qid2);
const auto merger_from_3_to_1 =
fml::RasterThreadMerger::CreateOrShareThreadMerger(merger_from_2_to_1,
qid1, qid3);
const size_t kNumFramesMerged = 5;
ASSERT_FALSE(merger_from_2_to_1->IsMerged());
ASSERT_FALSE(merger_from_3_to_1->IsMerged());
// Merge thread2 into thread1
merger_from_2_to_1->MergeWithLease(kNumFramesMerged);
// Merge thread3 into thread1
merger_from_3_to_1->MergeWithLease(kNumFramesMerged);
ASSERT_TRUE(merger_from_2_to_1->IsMerged());
ASSERT_TRUE(merger_from_3_to_1->IsMerged());
for (size_t i = 0; i < kNumFramesMerged; i++) {
ASSERT_TRUE(merger_from_2_to_1->IsMerged());
merger_from_2_to_1->DecrementLease();
}
ASSERT_FALSE(merger_from_2_to_1->IsMerged());
ASSERT_TRUE(merger_from_3_to_1->IsMerged());
for (size_t i = 0; i < kNumFramesMerged; i++) {
ASSERT_TRUE(merger_from_3_to_1->IsMerged());
merger_from_3_to_1->DecrementLease();
}
ASSERT_FALSE(merger_from_2_to_1->IsMerged());
ASSERT_FALSE(merger_from_3_to_1->IsMerged());
merger_from_2_to_1->MergeWithLease(kNumFramesMerged);
ASSERT_TRUE(merger_from_2_to_1->IsMerged());
ASSERT_FALSE(merger_from_3_to_1->IsMerged());
merger_from_3_to_1->MergeWithLease(kNumFramesMerged);
ASSERT_TRUE(merger_from_2_to_1->IsMerged());
ASSERT_TRUE(merger_from_3_to_1->IsMerged());
// Can unmerge independently
merger_from_2_to_1->UnMergeNowIfLastOne();
ASSERT_FALSE(merger_from_2_to_1->IsMerged());
ASSERT_TRUE(merger_from_3_to_1->IsMerged());
// Can unmerge independently
merger_from_3_to_1->UnMergeNowIfLastOne();
ASSERT_FALSE(merger_from_2_to_1->IsMerged());
ASSERT_FALSE(merger_from_3_to_1->IsMerged());
}
} // namespace testing
} // namespace fml