forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skia_gpu_object_unittests.cc
165 lines (143 loc) · 5.86 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
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
// 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 <utility>
#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_(std::move(latch)), dtor_task_queue_id_(dtor_task_queue_id) {}
virtual ~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 TestResourceContext : public TestSkObject {
public:
TestResourceContext(std::shared_ptr<fml::AutoResetWaitableEvent> latch,
fml::TaskQueueId* dtor_task_queue_id)
: TestSkObject(std::move(latch), dtor_task_queue_id) {}
~TestResourceContext() = default;
void performDeferredCleanup(std::chrono::milliseconds msNotUsed) {}
void deleteBackendTexture(const GrBackendTexture& texture) {}
};
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> queues_created;
unref_task_runner_->PostTask([this, &queues_created]() {
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));
queues_created.set_value(true);
});
queues_created.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.skia_object(), 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.skia_object(), nullptr);
sk_object.reset();
ASSERT_EQ(sk_object.skia_object(), nullptr);
latch->Wait();
ASSERT_EQ(dtor_task_queue_id, unref_task_runner()->GetTaskQueueId());
}
TEST_F(SkiaGpuObjectTest, UnrefResourceContextInTaskRunnerThread) {
std::shared_ptr<fml::AutoResetWaitableEvent> latch =
std::make_shared<fml::AutoResetWaitableEvent>();
fml::RefPtr<UnrefQueue<TestResourceContext>> unref_queue;
fml::TaskQueueId dtor_task_queue_id(0);
unref_task_runner()->PostTask([&]() {
auto resource_context =
sk_make_sp<TestResourceContext>(latch, &dtor_task_queue_id);
unref_queue = fml::MakeRefCounted<UnrefQueue<TestResourceContext>>(
unref_task_runner(), fml::TimeDelta::FromSeconds(0),
std::move(resource_context));
latch->Signal();
});
latch->Wait();
// Delete the unref queue, it will schedule a task to unref the resource
// context in the task runner's thread.
unref_queue = nullptr;
latch->Wait();
// Verify that the resource context was destroyed in the task runner's thread.
ASSERT_EQ(dtor_task_queue_id, unref_task_runner()->GetTaskQueueId());
}
} // namespace testing
} // namespace flutter