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
cc / tiles / image_controller_unittest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/tiles/image_controller.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/synchronization/condition_variable.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/simple_thread.h"
#include "base/threading/thread_checker_impl.h"
#include "base/threading/thread_restrictions.h"
#include "cc/base/features.h"
#include "cc/paint/paint_image_builder.h"
#include "cc/test/cc_test_suite.h"
#include "cc/test/skia_common.h"
#include "cc/test/stub_decode_cache.h"
#include "cc/test/test_paint_worklet_input.h"
#include "cc/test/test_tile_task_runner.h"
#include "cc/tiles/image_decode_cache.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
namespace {
// Image decode cache with introspection!
class TestableCache : public StubDecodeCache {
public:
~TestableCache() override { EXPECT_EQ(number_of_refs_, 0); }
TaskResult GetTaskForImageAndRef(uint32_t client_id,
const DrawImage& image,
const TracingInfo& tracing_info) override {
// Return false for large images to mimic "won't fit in memory"
// behavior.
if (image.paint_image() &&
image.paint_image().width() * image.paint_image().height() >=
1000 * 1000) {
return TaskResult(/*need_unref=*/false, /*is_at_raster_decode=*/true,
/*can_do_hardware_accelerated_decode=*/false);
}
++number_of_refs_;
if (task_to_use_)
return TaskResult(task_to_use_,
/*can_do_hardware_accelerated_decode=*/false);
return TaskResult(/*need_unref=*/true, /*is_at_raster_decode=*/false,
/*can_do_hardware_accelerated_decode=*/false);
}
TaskResult GetOutOfRasterDecodeTaskForImageAndRef(
uint32_t client_id,
const DrawImage& image) override {
return GetTaskForImageAndRef(client_id, image, TracingInfo());
}
void UnrefImage(const DrawImage& image) override {
ASSERT_GT(number_of_refs_, 0);
--number_of_refs_;
}
size_t GetMaximumMemoryLimitBytes() const override {
return 256 * 1024 * 1024;
}
int number_of_refs() const { return number_of_refs_; }
void SetTaskToUse(scoped_refptr<TileTask> task) { task_to_use_ = task; }
private:
int number_of_refs_ = 0;
scoped_refptr<TileTask> task_to_use_;
};
// A simple class that can receive decode callbacks.
class DecodeClient {
public:
DecodeClient() = default;
void Callback(base::OnceClosure quit_closure,
ImageController::ImageDecodeRequestId id,
ImageController::ImageDecodeResult result) {
id_ = id;
result_ = result;
std::move(quit_closure).Run();
}
ImageController::ImageDecodeRequestId id() { return id_; }
ImageController::ImageDecodeResult result() { return result_; }
private:
ImageController::ImageDecodeRequestId id_ = 0;
ImageController::ImageDecodeResult result_ =
ImageController::ImageDecodeResult::FAILURE;
};
// A dummy task that does nothing.
class SimpleTask : public TileTask {
public:
explicit SimpleTask(bool is_raster_task = true)
: TileTask(TileTask::SupportsConcurrentExecution::kYes,
TileTask::SupportsBackgroundThreadPriority::kYes),
is_raster_task_(is_raster_task) {
EXPECT_TRUE(thread_checker_.CalledOnValidThread());
}
SimpleTask(const SimpleTask&) = delete;
SimpleTask& operator=(const SimpleTask&) = delete;
void RunOnWorkerThread() override {
EXPECT_FALSE(HasCompleted());
has_run_ = true;
}
void OnTaskCompleted() override {
EXPECT_TRUE(thread_checker_.CalledOnValidThread());
}
bool IsRasterTask() const override { return is_raster_task_; }
bool has_run() { return has_run_; }
private:
~SimpleTask() override = default;
base::ThreadChecker thread_checker_;
bool has_run_ = false;
bool is_raster_task_;
};
// A task that blocks until instructed otherwise.
class BlockingTask : public TileTask {
public:
BlockingTask()
: TileTask(TileTask::SupportsConcurrentExecution::kYes,
TileTask::SupportsBackgroundThreadPriority::kYes),
run_cv_(&lock_) {
EXPECT_TRUE(thread_checker_.CalledOnValidThread());
}
BlockingTask(const BlockingTask&) = delete;
BlockingTask& operator=(const BlockingTask&) = delete;
void RunOnWorkerThread() override {
EXPECT_FALSE(HasCompleted());
EXPECT_FALSE(thread_checker_.CalledOnValidThread());
base::AutoLock hold(lock_);
base::ScopedAllowBaseSyncPrimitivesForTesting allow_base_sync_primitives;
while (!can_run_)
run_cv_.Wait();
has_run_ = true;
}
void OnTaskCompleted() override {
EXPECT_TRUE(thread_checker_.CalledOnValidThread());
}
void AllowToRun() {
base::AutoLock hold(lock_);
can_run_ = true;
run_cv_.Signal();
}
bool has_run() { return has_run_; }
private:
~BlockingTask() override = default;
// Use ThreadCheckerImpl, so that release builds also get correct behavior.
base::ThreadCheckerImpl thread_checker_;
bool has_run_ = false;
base::Lock lock_;
base::ConditionVariable run_cv_;
bool can_run_ = false;
};
// For tests that exercise image controller's thread, this is the timeout value
// to allow the worker thread to do its work.
int kDefaultTimeoutSeconds = 10;
DrawImage CreateDiscardableDrawImage(gfx::Size size) {
return DrawImage(CreateDiscardablePaintImage(size), false,
SkIRect::MakeWH(size.width(), size.height()),
PaintFlags::FilterQuality::kNone, SkM44(),
PaintImage::kDefaultFrameIndex, TargetColorParams());
}
DrawImage CreateBitmapDrawImage(gfx::Size size) {
return DrawImage(CreateBitmapImage(size), false,
SkIRect::MakeWH(size.width(), size.height()),
PaintFlags::FilterQuality::kNone, SkM44(),
PaintImage::kDefaultFrameIndex);
}
class ImageControllerTest : public testing::Test {
public:
ImageControllerTest()
: task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {
image_ = CreateDiscardableDrawImage(gfx::Size(1, 1));
}
~ImageControllerTest() override = default;
void SetUp() override {
controller_ = std::make_unique<ImageController>(
task_runner_,
base::ThreadPool::CreateSequencedTaskRunner(base::TaskTraits()),
base::DoNothing());
controller_->SetImageDecodeCache(&cache_);
}
void TearDown() override {
controller_.reset();
CCTestSuite::RunUntilIdle();
weak_ptr_factory_.InvalidateWeakPtrs();
}
base::SequencedTaskRunner* task_runner() { return task_runner_.get(); }
ImageController* controller() { return controller_.get(); }
TestableCache* cache() { return &cache_; }
const DrawImage& image() const { return image_; }
// Timeout callback, which errors and exits the runloop.
void Timeout(base::RunLoop* run_loop) {
ADD_FAILURE() << "Timeout.";
run_loop->Quit();
}
// Convenience method to run the run loop with a timeout.
void RunOrTimeout(base::RunLoop* run_loop) {
task_runner_->PostDelayedTask(FROM_HERE,
base::BindOnce(&ImageControllerTest::Timeout,
weak_ptr_factory_.GetWeakPtr(),
base::Unretained(run_loop)),
base::Seconds(kDefaultTimeoutSeconds));
run_loop->Run();
}
void ResetController() { controller_.reset(); }
SkMatrix CreateMatrix(const SkSize& scale, bool is_decomposable) {
SkMatrix matrix;
matrix.setScale(scale.width(), scale.height());
if (!is_decomposable) {
// Perspective is not decomposable, add it.
matrix[SkMatrix::kMPersp0] = 0.1f;
}
return matrix;
}
PaintImage CreatePaintImage(int width, int height) {
scoped_refptr<TestPaintWorkletInput> input =
base::MakeRefCounted<TestPaintWorkletInput>(gfx::SizeF(width, height));
return CreatePaintWorkletPaintImage(input);
}
private:
scoped_refptr<base::SequencedTaskRunner> task_runner_;
TestableCache cache_;
std::unique_ptr<ImageController> controller_;
DrawImage image_;
base::WeakPtrFactory<ImageControllerTest> weak_ptr_factory_{this};
};
TEST_F(ImageControllerTest, NullControllerUnrefsImages) {
std::vector<DrawImage> images(10);
ImageDecodeCache::TracingInfo tracing_info;
ASSERT_EQ(10u, images.size());
auto tasks =
controller()->SetPredecodeImages(std::move(images), tracing_info);
EXPECT_EQ(0u, tasks.size());
EXPECT_EQ(10, cache()->number_of_refs());
controller()->SetImageDecodeCache(nullptr);
EXPECT_EQ(0, cache()->number_of_refs());
}
TEST_F(ImageControllerTest, QueueImageDecode) {
base::RunLoop run_loop;
DecodeClient decode_client;
EXPECT_EQ(image().paint_image().width(), 1);
ImageController::ImageDecodeRequestId expected_id =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client),
run_loop.QuitClosure()));
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id, decode_client.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client.result());
}
TEST_F(ImageControllerTest, QueueImageDecodeNonLazy) {
base::RunLoop run_loop;
DecodeClient decode_client;
DrawImage image = CreateBitmapDrawImage(gfx::Size(1, 1));
ImageController::ImageDecodeRequestId expected_id =
controller()->QueueImageDecode(
image, base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client),
run_loop.QuitClosure()));
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id, decode_client.id());
EXPECT_EQ(ImageController::ImageDecodeResult::DECODE_NOT_REQUIRED,
decode_client.result());
}
TEST_F(ImageControllerTest, QueueImageDecodeTooLarge) {
base::RunLoop run_loop;
DecodeClient decode_client;
DrawImage image = CreateDiscardableDrawImage(gfx::Size(2000, 2000));
ImageController::ImageDecodeRequestId expected_id =
controller()->QueueImageDecode(
image, base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client),
run_loop.QuitClosure()));
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id, decode_client.id());
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client.result());
}
TEST_F(ImageControllerTest, QueueImageDecodeMultipleImages) {
base::RunLoop run_loop;
DecodeClient decode_client1;
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1), base::DoNothing()));
DecodeClient decode_client2;
ImageController::ImageDecodeRequestId expected_id2 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client2), base::DoNothing()));
DecodeClient decode_client3;
ImageController::ImageDecodeRequestId expected_id3 =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client3),
run_loop.QuitClosure()));
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id1, decode_client1.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client1.result());
EXPECT_EQ(expected_id2, decode_client2.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client2.result());
EXPECT_EQ(expected_id3, decode_client3.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client3.result());
}
TEST_F(ImageControllerTest, QueueImageDecodeWithTask) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
base::RunLoop run_loop;
DecodeClient decode_client;
ImageController::ImageDecodeRequestId expected_id =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client),
run_loop.QuitClosure()));
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id, decode_client.id());
EXPECT_TRUE(task->has_run());
EXPECT_TRUE(task->HasCompleted());
}
TEST_F(ImageControllerTest, QueueImageDecodeMultipleImagesSameTask) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
base::RunLoop run_loop;
DecodeClient decode_client1;
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1), base::DoNothing()));
DecodeClient decode_client2;
ImageController::ImageDecodeRequestId expected_id2 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client2), base::DoNothing()));
DecodeClient decode_client3;
ImageController::ImageDecodeRequestId expected_id3 =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client3),
run_loop.QuitClosure()));
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id1, decode_client1.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client1.result());
EXPECT_EQ(expected_id2, decode_client2.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client2.result());
EXPECT_EQ(expected_id3, decode_client3.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client3.result());
EXPECT_TRUE(task->has_run());
EXPECT_TRUE(task->HasCompleted());
}
TEST_F(ImageControllerTest, QueueImageDecodeChangeControllerWithTaskQueued) {
scoped_refptr<BlockingTask> task_one(new BlockingTask);
cache()->SetTaskToUse(task_one);
DecodeClient decode_client1;
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1), base::DoNothing()));
scoped_refptr<BlockingTask> task_two(new BlockingTask);
cache()->SetTaskToUse(task_two);
base::RunLoop run_loop;
DecodeClient decode_client2;
ImageController::ImageDecodeRequestId expected_id2 =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client2),
run_loop.QuitClosure()));
task_one->AllowToRun();
task_two->AllowToRun();
controller()->SetImageDecodeCache(nullptr);
ResetController();
RunOrTimeout(&run_loop);
EXPECT_TRUE(task_one->state().IsCanceled() || task_one->HasCompleted());
EXPECT_TRUE(task_two->state().IsCanceled() || task_two->HasCompleted());
EXPECT_EQ(expected_id1, decode_client1.id());
EXPECT_EQ(expected_id2, decode_client2.id());
}
TEST_F(ImageControllerTest, QueueImageDecodeImageAlreadyLocked) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
base::RunLoop run_loop1;
DecodeClient decode_client1;
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1),
run_loop1.QuitClosure()));
RunOrTimeout(&run_loop1);
EXPECT_EQ(expected_id1, decode_client1.id());
EXPECT_TRUE(task->has_run());
cache()->SetTaskToUse(nullptr);
base::RunLoop run_loop2;
DecodeClient decode_client2;
ImageController::ImageDecodeRequestId expected_id2 =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client2),
run_loop2.QuitClosure()));
RunOrTimeout(&run_loop2);
EXPECT_EQ(expected_id2, decode_client2.id());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client2.result());
}
TEST_F(ImageControllerTest, QueueImageDecodeLockedImageControllerChange) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
base::RunLoop run_loop1;
DecodeClient decode_client1;
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1),
run_loop1.QuitClosure()));
RunOrTimeout(&run_loop1);
EXPECT_EQ(expected_id1, decode_client1.id());
EXPECT_TRUE(task->has_run());
EXPECT_EQ(1, cache()->number_of_refs());
controller()->SetImageDecodeCache(nullptr);
EXPECT_EQ(0, cache()->number_of_refs());
}
TEST_F(ImageControllerTest, DecodeRequestedBeforeCacheIsSet) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
controller()->SetImageDecodeCache(nullptr);
base::RunLoop run_loop;
DecodeClient decode_client;
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback, base::Unretained(&decode_client),
run_loop.QuitClosure()));
controller()->SetImageDecodeCache(cache());
RunOrTimeout(&run_loop);
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client.result());
}
TEST_F(ImageControllerTest, DispatchesDecodeCallbacksAfterCacheReset) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
base::RunLoop run_loop1;
DecodeClient decode_client1;
base::RunLoop run_loop2;
DecodeClient decode_client2;
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback, base::Unretained(&decode_client1),
run_loop1.QuitClosure()));
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback, base::Unretained(&decode_client2),
run_loop2.QuitClosure()));
// Now reset the image cache before decode completed callbacks are posted to
// the compositor thread. Ensure that the completion callbacks for the decode
// is still run.
controller()->SetImageDecodeCache(nullptr);
ResetController();
RunOrTimeout(&run_loop1);
RunOrTimeout(&run_loop2);
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client1.result());
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client2.result());
}
TEST_F(ImageControllerTest, DispatchesDecodeCallbacksAfterCacheChanged) {
scoped_refptr<SimpleTask> task(new SimpleTask);
cache()->SetTaskToUse(task);
base::RunLoop run_loop1;
DecodeClient decode_client1;
base::RunLoop run_loop2;
DecodeClient decode_client2;
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback, base::Unretained(&decode_client1),
run_loop1.QuitClosure()));
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback, base::Unretained(&decode_client2),
run_loop2.QuitClosure()));
// Now reset the image cache before decode completed callbacks are posted to
// the compositor thread. This should orphan the requests.
controller()->SetImageDecodeCache(nullptr);
EXPECT_EQ(0, cache()->number_of_refs());
TestableCache other_cache;
other_cache.SetTaskToUse(task);
controller()->SetImageDecodeCache(&other_cache);
RunOrTimeout(&run_loop1);
RunOrTimeout(&run_loop2);
EXPECT_EQ(2, other_cache.number_of_refs());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client1.result());
EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS,
decode_client2.result());
// Reset the controller since the order of destruction is wrong in this test
// (|other_cache| should outlive the controller. This is normally done via
// SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.)
ResetController();
}
TEST_F(ImageControllerTest, QueueImageDecodeLazyCancelImmediately) {
DecodeClient decode_client1;
DecodeClient decode_client2;
// Create two images so that there is always one that is queued up and
// not run yet. This prevents raciness in this test.
DrawImage image1 = CreateDiscardableDrawImage(gfx::Size(1, 1));
DrawImage image2 = CreateDiscardableDrawImage(gfx::Size(1, 1));
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1), base::DoNothing()));
ImageController::ImageDecodeRequestId expected_id2 =
controller()->QueueImageDecode(
image(),
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client2), base::DoNothing()));
// This needs a ref because it is lazy.
EXPECT_EQ(2, cache()->number_of_refs());
// Instead of running, immediately cancel everything.
controller()->SetImageDecodeCache(nullptr);
// This should not crash, and nothing should have run.
EXPECT_NE(expected_id1, decode_client1.id());
EXPECT_NE(expected_id2, decode_client2.id());
EXPECT_EQ(0u, decode_client1.id());
EXPECT_EQ(0u, decode_client2.id());
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client1.result());
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client2.result());
// Refs should still be cleaned up.
EXPECT_EQ(0, cache()->number_of_refs());
// Explicitly reset the controller so that orphaned task callbacks run
// while the decode clients still exist.
ResetController();
}
TEST_F(ImageControllerTest, QueueImageDecodeNonLazyCancelImmediately) {
DecodeClient decode_client1;
DecodeClient decode_client2;
// Create two images so that there is always one that is queued up and
// not run yet. This prevents raciness in this test.
DrawImage image1 = CreateBitmapDrawImage(gfx::Size(1, 1));
DrawImage image2 = CreateBitmapDrawImage(gfx::Size(1, 1));
ImageController::ImageDecodeRequestId expected_id1 =
controller()->QueueImageDecode(
image1,
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client1), base::DoNothing()));
ImageController::ImageDecodeRequestId expected_id2 =
controller()->QueueImageDecode(
image2,
base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client2), base::DoNothing()));
// No ref needed here, because it is non-lazy.
EXPECT_EQ(0, cache()->number_of_refs());
// Instead of running, immediately cancel everything.
controller()->SetImageDecodeCache(nullptr);
// This should not crash, and nothing should have run.
EXPECT_NE(expected_id1, decode_client1.id());
EXPECT_NE(expected_id2, decode_client2.id());
EXPECT_EQ(0u, decode_client1.id());
EXPECT_EQ(0u, decode_client2.id());
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client1.result());
EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE,
decode_client2.result());
EXPECT_EQ(0, cache()->number_of_refs());
// Explicitly reset the controller so that orphaned task callbacks run
// while the decode clients still exist.
ResetController();
}
TEST_F(ImageControllerTest, ExternalDependency) {
if (!base::FeatureList::IsEnabled(features::kPreventDuplicateImageDecodes)) {
return;
}
// Set up a stand-alone image decode task in a dependency sandwich with two
// external (i.e. raster) tasks.
scoped_refptr<SimpleTask> dependency(new SimpleTask);
scoped_refptr<SimpleTask> task(new SimpleTask(false /*is_raster_task*/));
scoped_refptr<SimpleTask> dependent(new SimpleTask);
dependency->SetExternalDependent(task);
task->SetExternalDependent(dependent);
EXPECT_FALSE(task->dependencies().empty());
EXPECT_FALSE(dependent->dependencies().empty());
base::RunLoop run_loop;
DecodeClient decode_client;
cache()->SetTaskToUse(task);
ImageController::ImageDecodeRequestId expected_id =
controller()->QueueImageDecode(
image(), base::BindOnce(&DecodeClient::Callback,
base::Unretained(&decode_client),
run_loop.QuitClosure()));
EXPECT_FALSE(controller()->HasReadyToRunTaskForTesting());
EXPECT_FALSE(task->has_run());
EXPECT_FALSE(task->HasCompleted());
TestTileTaskRunner::ProcessTask(dependency.get());
controller()->ExternalDependencyCompletedForTask(task);
RunOrTimeout(&run_loop);
EXPECT_EQ(expected_id, decode_client.id());
EXPECT_TRUE(task->has_run());
EXPECT_TRUE(task->HasCompleted());
EXPECT_TRUE(dependent->dependencies().empty());
TestTileTaskRunner::ProcessTask(dependent.get());
}
} // namespace
} // namespace cc