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
ash / frame_sink / frame_sink_holder_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/frame_sink/frame_sink_holder.h"
#include <algorithm>
#include <memory>
#include <vector>
#include "ash/frame_sink/frame_sink_holder_test_api.h"
#include "ash/frame_sink/frame_sink_host.h"
#include "ash/frame_sink/test/test_begin_frame_source.h"
#include "ash/frame_sink/test/test_frame_factory.h"
#include "ash/frame_sink/test/test_layer_tree_frame_sink.h"
#include "ash/frame_sink/ui_resource_manager.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/common/gpu/context_provider.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/resources/resource_id.h"
#include "components/viz/common/resources/returned_resource.h"
#include "components/viz/common/resources/transferable_resource.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace ash {
namespace {
class StubBeginFrameSource : public viz::BeginFrameSource {
public:
StubBeginFrameSource() : viz::BeginFrameSource(0u) {}
void DidFinishFrame(viz::BeginFrameObserver* obs) override {}
void AddObserver(viz::BeginFrameObserver* obs) override {}
void RemoveObserver(viz::BeginFrameObserver* obs) override {}
void OnGpuNoLongerBusy() override {}
};
MATCHER_P(IsBeginFrameAckEqual, value, "") {
return arg.frame_id == value.frame_id && arg.trace_id == value.trace_id &&
arg.has_damage == value.has_damage;
}
class FrameSinkHolderTest : public AshTestBase {
public:
FrameSinkHolderTest() = default;
FrameSinkHolderTest(const FrameSinkHolderTest&) = delete;
FrameSinkHolderTest& operator=(const FrameSinkHolderTest&) = delete;
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
aura::Window* root_window =
Shell::Get()->GetRootWindowForDisplayId(GetPrimaryDisplay().id());
auto host_window = std::make_unique<aura::Window>(/*delegate=*/nullptr);
host_window_ = host_window.release();
host_window_->Init(ui::LayerType::LAYER_SOLID_COLOR);
root_window->AddChild(host_window_);
frame_factory_ = std::make_unique<TestFrameFactory>();
auto layer_tree_frame_sink = std::make_unique<TestLayerTreeFrameSink>();
layer_tree_frame_sink_ = layer_tree_frame_sink.get();
frame_sink_holder_ = std::make_unique<FrameSinkHolder>(
std::move(layer_tree_frame_sink),
base::BindRepeating(&TestFrameFactory::CreateCompositorFrame,
base::Unretained(frame_factory_.get())),
/*on_first_frame_requested_callback=*/base::DoNothing(),
/*on_frame_sink_lost_callback=*/base::DoNothing());
holder_weak_ptr_ = frame_sink_holder_->GetWeakPtr();
}
UiResourceManager& GetResourceManager() {
return frame_sink_holder_->resource_manager();
}
// If `frame_sink_holder_` lifetime has been extended in a unittest and the
// holder did not schedule a delete task, it will get destroyed once we
// delete the root_window of `host_window_`.
std::unique_ptr<FrameSinkHolder> frame_sink_holder_;
raw_ptr<aura::Window, DanglingUntriaged> host_window_;
// Will be used to access the frame_sink_holder once we pass the
// ownership of `frame_sink_holder_` to
// `DeleteWhenLastResourceHasBeenReclaimed()` in unittests.
base::WeakPtr<FrameSinkHolder> holder_weak_ptr_;
// Factory to create test compositor frames.
std::unique_ptr<TestFrameFactory> frame_factory_;
// Keeping a reference to be used in tests.
raw_ptr<TestLayerTreeFrameSink, DanglingUntriaged>
layer_tree_frame_sink_; // no owned
};
TEST_F(FrameSinkHolderTest, SubmitFrameSynchronouslyBeforeFirstFrameRequested) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
ASSERT_FALSE(test_api.IsFirstFrameRequested());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
// Confirm that FrameSinkHolder did not submit any frame yet.
EXPECT_TRUE(test_api.LastSubmittedFrameSize().IsEmpty());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 0);
// FrameSinkHolder has pending a frame that will be sent out asynchronously.
EXPECT_TRUE(test_api.IsPendingFrame());
// Asynchronous frame request.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
ASSERT_TRUE(test_api.IsFirstFrameRequested());
EXPECT_FALSE(test_api.IsPendingFrame());
EXPECT_TRUE(test_api.IsPendingFrameAck());
// LayerTreeFrameSink received the frame.
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// Manual BeginFrameAck is used for synchronous frames only.
EXPECT_THAT(
layer_tree_frame_sink_->GetLatestReceivedFrame().metadata.begin_frame_ack,
testing::Not(IsBeginFrameAckEqual(
viz::BeginFrameAck::CreateManualAckWithDamage())));
frame_sink_holder_->DidReceiveCompositorFrameAck();
EXPECT_FALSE(test_api.IsPendingFrameAck());
layer_tree_frame_sink_->ResetLatestFrameState();
// Now that FrameSinkHolder has received the requested for the first frame, it
// can now submit frames synchronously.
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 2);
EXPECT_EQ(test_api.LastSubmittedFrameSize(),
layer_tree_frame_sink_->GetLatestReceivedFrame().size_in_pixels());
// Manual BeginFrameAck is used only for synchronously submitted frames.
EXPECT_THAT(
layer_tree_frame_sink_->GetLatestReceivedFrame().metadata.begin_frame_ack,
IsBeginFrameAckEqual(viz::BeginFrameAck::CreateManualAckWithDamage()));
}
TEST_F(FrameSinkHolderTest, ObserveBeginFrameSourceOnDemand) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
StubBeginFrameSource source;
frame_sink_holder_->SetBeginFrameSource(&source);
// FrameSinkHolder should be observing the source when it is set.
EXPECT_TRUE(test_api.IsObservingBeginFrameSource());
// After consecutively not producing frames for a certain number of
// BeginFrames, FrameSinkHolder should stop observing the source.
for (int i = 0; i < 4; i++) {
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
}
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_TRUE(test_api.IsObservingBeginFrameSource());
for (int i = 0; i < 5; i++) {
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
}
EXPECT_FALSE(test_api.IsObservingBeginFrameSource());
// However, if there is request to submit a new frame, FrameSinkHolder should
// start observing the source again.
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_TRUE(test_api.IsObservingBeginFrameSource());
frame_sink_holder_->SetBeginFrameSource(nullptr);
}
TEST_F(FrameSinkHolderTest, ObserveBeginFrameSourceOnDemand_AutoUpdate) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
frame_sink_holder_->SetAutoUpdateMode(true);
StubBeginFrameSource source;
frame_sink_holder_->SetBeginFrameSource(&source);
// FrameSinkHolder should be observing BeginFrameSource when it is set.
EXPECT_TRUE(test_api.IsObservingBeginFrameSource());
// When auto update mode is on, we should not stop observation of
// BeginFrameSource.
for (int i = 0; i < 10; i++) {
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
}
EXPECT_TRUE(test_api.IsObservingBeginFrameSource());
// However once the auto update mode is turned off, we should stop observing
// the BeginFrameSource as needed.
frame_sink_holder_->SetAutoUpdateMode(false);
for (int i = 0; i < 5; i++) {
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
}
EXPECT_FALSE(test_api.IsObservingBeginFrameSource());
// Renablending the auto update mode should start the observation of
// BeginFrameSource.
frame_sink_holder_->SetAutoUpdateMode(true);
EXPECT_TRUE(test_api.IsObservingBeginFrameSource());
frame_sink_holder_->SetBeginFrameSource(nullptr);
}
TEST_F(FrameSinkHolderTest, SubmitFrameSynchronouslyWhilePendingFrameAck) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_TRUE(test_api.IsPendingFrameAck());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
frame_factory_->SetFrameMetaData(gfx::Size(200, 200), 1.0);
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
// This confirms that FrameSinkHolder did not submit frame synchronously,
// since it has not received frame ack for the last frame.
EXPECT_EQ(layer_tree_frame_sink_->GetLatestReceivedFrame().size_in_pixels(),
gfx::Size(100, 100));
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// FrameSinkHolder fell to asynchronous frame submission.
EXPECT_TRUE(test_api.IsPendingFrame());
}
TEST_F(FrameSinkHolderTest, HandlingAsynchronousFrameRequests_NoAutoUpdate) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
// FrameSinkHolder has no request to submit a frame.
auto skipped_reason = layer_tree_frame_sink_->GetLatestFrameSkippedReason();
ASSERT_TRUE(skipped_reason.has_value());
EXPECT_EQ(skipped_reason, cc::FrameSkippedReason::kNoDamage);
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/false);
EXPECT_TRUE(test_api.IsPendingFrame());
// This time FrameSinkHolder has a request to submit frame asynchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// Asynchronously submitted frames will not have manual BeginFrameAck.
EXPECT_THAT(
layer_tree_frame_sink_->GetLatestReceivedFrame().metadata.begin_frame_ack,
testing::Not(IsBeginFrameAckEqual(
viz::BeginFrameAck::CreateManualAckWithDamage())));
EXPECT_FALSE(test_api.IsPendingFrame());
EXPECT_TRUE(test_api.IsPendingFrameAck());
layer_tree_frame_sink_->ResetLatestFrameState();
// FrameSinkHolder did not submit a frame since it is still waiting for ack.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
skipped_reason = layer_tree_frame_sink_->GetLatestFrameSkippedReason();
ASSERT_TRUE(skipped_reason.has_value());
EXPECT_EQ(skipped_reason, cc::FrameSkippedReason::kWaitingOnMain);
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// Received ack.
frame_sink_holder_->DidReceiveCompositorFrameAck();
EXPECT_FALSE(test_api.IsPendingFrameAck());
layer_tree_frame_sink_->ResetLatestFrameState();
// FrameSinkHolder did not submit anything because it did not have any pending
// request.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
skipped_reason = layer_tree_frame_sink_->GetLatestFrameSkippedReason();
ASSERT_TRUE(skipped_reason.has_value());
EXPECT_EQ(skipped_reason, cc::FrameSkippedReason::kNoDamage);
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// FrameSinkHolder has an async request again.
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/false);
EXPECT_TRUE(test_api.IsPendingFrame());
EXPECT_FALSE(test_api.IsPendingFrameAck());
layer_tree_frame_sink_->ResetLatestFrameState();
// FrameSinkHolder should now submit a new frame.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
// Asynchronously submitted frames will not have manual begin_frame_ack.
EXPECT_THAT(
layer_tree_frame_sink_->GetLatestReceivedFrame().metadata.begin_frame_ack,
testing::Not(IsBeginFrameAckEqual(
viz::BeginFrameAck::CreateManualAckWithDamage())));
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 2);
EXPECT_FALSE(test_api.IsPendingFrame());
EXPECT_TRUE(test_api.IsPendingFrameAck());
}
TEST_F(FrameSinkHolderTest, DontSubmitNewFramesWhenWaitingToDeleteSinkHolder) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
base::RunLoop loop;
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// The lifetime of frame_sink_holder has been extended since there are still
// some exported resources.
EXPECT_FALSE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
ASSERT_TRUE(holder_weak_ptr_);
// During deletion FrameSinkHolder submits a empty frame.
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 2);
layer_tree_frame_sink_->ResetLatestFrameState();
holder_weak_ptr_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
// Confirms that FrameSinkHolder did not submit a new frame on asynchronous
// request.
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 2);
}
TEST_F(FrameSinkHolderTest,
DeleteSinkHolderImmediatelyWhenNoFramesIsSubmitted) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
// Confirms that FrameSinkHolder has not submitted any frames yet.
EXPECT_TRUE(test_api.LastSubmittedFrameSize().IsEmpty());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 0);
// FrameSinkHolder will get deleted straight away since it has not submitted
// any resources to the display compositor.
EXPECT_TRUE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// Since FrameSinkHolder is deleted immediately, we expect the weak_ptr to be
// not valid.
EXPECT_FALSE(holder_weak_ptr_);
}
TEST_F(FrameSinkHolderTest, ExtendLifeTimeOfHolderToRootWindow) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
viz::ResourceId id_2 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
viz::ResourceId id_3 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1, id_2, id_3});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
// Confirms that FrameSinkHolder has not submitted any frames.
EXPECT_FALSE(test_api.LastSubmittedFrameSize().IsEmpty());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// Since FrameSinkHolder has not received the resources back from display
// compositor, it extend its lifetime.
EXPECT_FALSE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// Since FrameSinkHolder lifetime is extend, we expect the weak_ptr to be
// valid.
EXPECT_TRUE(holder_weak_ptr_);
}
TEST_F(FrameSinkHolderTest, KeepSubmittingFrameWhenAutoUpdateIsOn) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Request a frame.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
// Since auto_fresh_mode is off, FrameSinkHolder did not submit any frame as
// there was not request for a frame submission.
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 0);
// Request a frame again. FrameSinkHolder should not submit a frame.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 0);
frame_sink_holder_->SetAutoUpdateMode(/*mode=*/true);
// After auto_fresh_mode on, when compositor requests for a frame,
// FrameSinkHolder should submit a frame.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// FrameSinkHolder should not submit a new frame sas it has no received an
// ack from the compositor,
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
// Receive an ack.
frame_sink_holder_->DidReceiveCompositorFrameAck();
// In auto_fresh mode, FrameSinkHolder will keep on submitting frames
// asynchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 2);
frame_sink_holder_->DidReceiveCompositorFrameAck();
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 3);
}
TEST_F(FrameSinkHolderTest, DeleteHolderAfterReclaimingAllResources) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
base::RunLoop loop;
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
viz::ResourceId id_2 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1, id_2});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_FALSE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// The lifetime of frame_sink_holder has been extended since there are still
// some exported resources.
ASSERT_TRUE(holder_weak_ptr_);
std::vector<viz::ReturnedResource> to_be_returned_resources;
layer_tree_frame_sink_->GetFrameResourcesToReturn(to_be_returned_resources);
// Reclaim the exported resources.
holder_weak_ptr_->ReclaimResources(std::move(to_be_returned_resources));
// Wait for the deletion task to be completed.
loop.RunUntilIdle();
ASSERT_FALSE(holder_weak_ptr_);
}
TEST_F(FrameSinkHolderTest, LayerTreeFrameSinkLost) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_EQ(GetResourceManager().exported_resources_count(), 1u);
frame_sink_holder_->DidLoseLayerTreeFrameSink();
// When FrameSinkHolder loses the LayerTreeFrameSink, it marks all the
// exported resources as lost.
EXPECT_EQ(GetResourceManager().exported_resources_count(), 0u);
}
TEST_F(FrameSinkHolderTest,
LayerTreeFrameSinkLostWhenWaitingToDeleteSinkHolder) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
base::RunLoop loop;
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
EXPECT_FALSE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// The lifetime of frame_sink_holder has been extended since there are still
// some exported resources.
ASSERT_TRUE(holder_weak_ptr_);
holder_weak_ptr_->DidLoseLayerTreeFrameSink();
// Since FrameSinkHolder cannot reclaim back exported resources, it schedules
// to delete itself.
// Wait for deletion task to complete.
loop.RunUntilIdle();
ASSERT_FALSE(holder_weak_ptr_);
}
TEST_F(FrameSinkHolderTest,
DeleteSinkHolderWithExportedResources_DuringShutdown) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
// Confirms we have an exported resource.
EXPECT_EQ(frame_sink_holder_->resource_manager().exported_resources_count(),
1u);
// During shutdown, root_window can be null. We can replicate it by
// removing the host window from the window hierarchy.
aura::Window* root_window =
Shell::Get()->GetRootWindowForDisplayId(GetPrimaryDisplay().id());
root_window->RemoveChild(host_window_);
// Since `host_window_` is removed from window tree hierarchy, wrapping it in
// a unique_ptr to delete this object as it goes out of scope and stop it
// from leaking memory.
auto host_window = base::WrapUnique<aura::Window>(host_window_);
// Since FrameSinkHolder cannot extend its lifetime, it marks the resources
// as lost and deletes itself immediately.
EXPECT_TRUE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// Since FrameSinkHolder is deleted immediately, we expect the weak_ptr to be
// not valid.
EXPECT_FALSE(holder_weak_ptr_);
}
TEST_F(FrameSinkHolderTest,
DeleteSinkHolderImmediatelyWhenNoExportedResources) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
// Confirms that FrameSinkHolder has submitted a frame.
EXPECT_FALSE(test_api.LastSubmittedFrameSize().IsEmpty());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
std::vector<viz::ReturnedResource> to_be_returned_resources;
layer_tree_frame_sink_->GetFrameResourcesToReturn(to_be_returned_resources);
frame_sink_holder_->ReclaimResources(std::move(to_be_returned_resources));
// We can delete the holder straight way since we have no exported resources.
ASSERT_EQ(GetResourceManager().exported_resources_count(), 0u);
EXPECT_TRUE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// Since FrameSinkHolder is deleted immediately, we expect the weak_ptr to be
// not valid.
EXPECT_FALSE(holder_weak_ptr_);
}
TEST_F(FrameSinkHolderTest, DeleteSinkHolderImmediatelyWhenFrameSinkIsLost) {
FrameSinkHolderTestApi test_api(frame_sink_holder_.get());
viz::ResourceId id_1 =
GetResourceManager().OfferResource(std::make_unique<UiResource>());
frame_factory_->SetFrameResources({id_1});
frame_factory_->SetFrameMetaData(gfx::Size(100, 100), 1.0);
// Call OnBeginFrame so that FrameSinkHolder can know that it can submit
// frames synchronously.
frame_sink_holder_->OnBeginFrame(CreateValidBeginFrameArgsForTesting());
frame_sink_holder_->SubmitCompositorFrame(/*synchronous_draw=*/true);
// Confirms that FrameSinkHolder has submitted a frame.
EXPECT_FALSE(test_api.LastSubmittedFrameSize().IsEmpty());
EXPECT_EQ(layer_tree_frame_sink_->num_of_frames_received(), 1);
ASSERT_EQ(GetResourceManager().exported_resources_count(), 1u);
frame_sink_holder_->DidLoseLayerTreeFrameSink();
// Since frame sink is lost, the exported resources cannot be reclaimed.
EXPECT_TRUE(FrameSinkHolder::DeleteWhenLastResourceHasBeenReclaimed(
std::move(frame_sink_holder_), host_window_));
// Since FrameSinkHolder is deleted immediately, we expect the weak_ptr to be
// not valid.
EXPECT_FALSE(holder_weak_ptr_);
}
} // namespace
} // namespace ash