forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skia_gpu_object_unittests.cc
131 lines (112 loc) · 4.45 KB
/
skia_gpu_object_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
// 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.
#include "flutter/flow/skia_gpu_object.h"
#include <future>
#include "flutter/fml/message_loop.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/task_runner.h"
#include "flutter/testing/thread_test.h"
#include "gtest/gtest.h"
#include "third_party/skia/include/core/SkRefCnt.h"
namespace flutter {
namespace testing {
class TestSkObject : public SkRefCnt {
public:
TestSkObject(std::shared_ptr<fml::AutoResetWaitableEvent> latch,
fml::TaskQueueId* dtor_task_queue_id)
: latch_(latch), dtor_task_queue_id_(dtor_task_queue_id) {}
~TestSkObject() {
if (dtor_task_queue_id_) {
*dtor_task_queue_id_ = fml::MessageLoop::GetCurrentTaskQueueId();
}
latch_->Signal();
}
private:
std::shared_ptr<fml::AutoResetWaitableEvent> latch_;
fml::TaskQueueId* dtor_task_queue_id_;
};
class SkiaGpuObjectTest : public ThreadTest {
public:
SkiaGpuObjectTest()
: unref_task_runner_(CreateNewThread()),
unref_queue_(fml::MakeRefCounted<SkiaUnrefQueue>(
unref_task_runner(),
fml::TimeDelta::FromSeconds(0))),
delayed_unref_queue_(fml::MakeRefCounted<SkiaUnrefQueue>(
unref_task_runner(),
fml::TimeDelta::FromSeconds(3))) {
// The unref queues must be created in the same thread of the
// unref_task_runner so the queue can access the same-thread-only WeakPtr of
// the GrContext constructed during the creation.
std::promise<bool> queuesCreated;
unref_task_runner_->PostTask([this, &queuesCreated]() {
unref_queue_ = fml::MakeRefCounted<SkiaUnrefQueue>(
unref_task_runner(), fml::TimeDelta::FromSeconds(0));
delayed_unref_queue_ = fml::MakeRefCounted<SkiaUnrefQueue>(
unref_task_runner(), fml::TimeDelta::FromSeconds(3));
queuesCreated.set_value(true);
});
queuesCreated.get_future().wait();
::testing::FLAGS_gtest_death_test_style = "threadsafe";
}
fml::RefPtr<fml::TaskRunner> unref_task_runner() {
return unref_task_runner_;
}
fml::RefPtr<SkiaUnrefQueue> unref_queue() { return unref_queue_; }
fml::RefPtr<SkiaUnrefQueue> delayed_unref_queue() {
return delayed_unref_queue_;
}
private:
fml::RefPtr<fml::TaskRunner> unref_task_runner_;
fml::RefPtr<SkiaUnrefQueue> unref_queue_;
fml::RefPtr<SkiaUnrefQueue> delayed_unref_queue_;
};
TEST_F(SkiaGpuObjectTest, QueueSimple) {
std::shared_ptr<fml::AutoResetWaitableEvent> latch =
std::make_shared<fml::AutoResetWaitableEvent>();
fml::TaskQueueId dtor_task_queue_id(0);
SkRefCnt* ref_object = new TestSkObject(latch, &dtor_task_queue_id);
unref_queue()->Unref(ref_object);
latch->Wait();
ASSERT_EQ(dtor_task_queue_id, unref_task_runner()->GetTaskQueueId());
}
TEST_F(SkiaGpuObjectTest, ObjectDestructor) {
std::shared_ptr<fml::AutoResetWaitableEvent> latch =
std::make_shared<fml::AutoResetWaitableEvent>();
fml::TaskQueueId dtor_task_queue_id(0);
auto object = sk_make_sp<TestSkObject>(latch, &dtor_task_queue_id);
{
SkiaGPUObject<TestSkObject> sk_object(std::move(object), unref_queue());
// Verify that the default SkiaGPUObject dtor queues and unref.
}
latch->Wait();
ASSERT_EQ(dtor_task_queue_id, unref_task_runner()->GetTaskQueueId());
}
TEST_F(SkiaGpuObjectTest, ObjectReset) {
std::shared_ptr<fml::AutoResetWaitableEvent> latch =
std::make_shared<fml::AutoResetWaitableEvent>();
fml::TaskQueueId dtor_task_queue_id(0);
SkiaGPUObject<TestSkObject> sk_object(
sk_make_sp<TestSkObject>(latch, &dtor_task_queue_id), unref_queue());
// Verify that explicitly resetting the GPU object queues and unref.
sk_object.reset();
ASSERT_EQ(sk_object.get(), nullptr);
latch->Wait();
ASSERT_EQ(dtor_task_queue_id, unref_task_runner()->GetTaskQueueId());
}
TEST_F(SkiaGpuObjectTest, ObjectResetTwice) {
std::shared_ptr<fml::AutoResetWaitableEvent> latch =
std::make_shared<fml::AutoResetWaitableEvent>();
fml::TaskQueueId dtor_task_queue_id(0);
SkiaGPUObject<TestSkObject> sk_object(
sk_make_sp<TestSkObject>(latch, &dtor_task_queue_id), unref_queue());
sk_object.reset();
ASSERT_EQ(sk_object.get(), nullptr);
sk_object.reset();
ASSERT_EQ(sk_object.get(), nullptr);
latch->Wait();
ASSERT_EQ(dtor_task_queue_id, unref_task_runner()->GetTaskQueueId());
}
} // namespace testing
} // namespace flutter