1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/resources/resource_pool.h"
7 #include "base/run_loop.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "cc/resources/resource_util.h"
10 #include "cc/resources/scoped_resource.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/fake_output_surface_client.h"
13 #include "cc/test/fake_resource_provider.h"
14 #include "cc/test/test_shared_bitmap_manager.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 class ResourcePoolTest
: public testing::Test
{
22 void SetUp() override
{
23 output_surface_
= FakeOutputSurface::Create3d();
24 ASSERT_TRUE(output_surface_
->BindToClient(&output_surface_client_
));
25 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
26 resource_provider_
= FakeResourceProvider::Create(
27 output_surface_
.get(), shared_bitmap_manager_
.get());
28 task_runner_
= base::ThreadTaskRunnerHandle::Get();
29 resource_pool_
= ResourcePool::Create(resource_provider_
.get(),
30 task_runner_
.get(), GL_TEXTURE_2D
);
34 FakeOutputSurfaceClient output_surface_client_
;
35 scoped_ptr
<FakeOutputSurface
> output_surface_
;
36 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
37 scoped_ptr
<ResourceProvider
> resource_provider_
;
38 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
39 scoped_ptr
<ResourcePool
> resource_pool_
;
42 TEST_F(ResourcePoolTest
, AcquireRelease
) {
43 gfx::Size
size(100, 100);
44 ResourceFormat format
= RGBA_8888
;
45 Resource
* resource
= resource_pool_
->AcquireResource(size
, format
);
46 EXPECT_EQ(size
, resource
->size());
47 EXPECT_EQ(format
, resource
->format());
48 EXPECT_TRUE(resource_provider_
->CanLockForWrite(resource
->id()));
50 resource_pool_
->ReleaseResource(resource
, 0u);
53 TEST_F(ResourcePoolTest
, AccountingSingleResource
) {
54 // Limits high enough to not be hit by this test.
55 size_t bytes_limit
= 10 * 1024 * 1024;
56 size_t count_limit
= 100;
57 resource_pool_
->SetResourceUsageLimits(bytes_limit
, count_limit
);
59 gfx::Size
size(100, 100);
60 ResourceFormat format
= RGBA_8888
;
61 size_t resource_bytes
=
62 ResourceUtil::UncheckedSizeInBytes
<size_t>(size
, format
);
63 Resource
* resource
= resource_pool_
->AcquireResource(size
, format
);
65 EXPECT_EQ(resource_bytes
, resource_pool_
->GetTotalMemoryUsageForTesting());
66 EXPECT_EQ(resource_bytes
, resource_pool_
->memory_usage_bytes());
67 EXPECT_EQ(1u, resource_pool_
->GetTotalResourceCountForTesting());
68 EXPECT_EQ(1u, resource_pool_
->resource_count());
69 EXPECT_EQ(0u, resource_pool_
->GetBusyResourceCountForTesting());
71 resource_pool_
->ReleaseResource(resource
, 0u);
72 EXPECT_EQ(resource_bytes
, resource_pool_
->GetTotalMemoryUsageForTesting());
73 EXPECT_EQ(1u, resource_pool_
->GetTotalResourceCountForTesting());
74 EXPECT_EQ(1u, resource_pool_
->GetBusyResourceCountForTesting());
76 resource_pool_
->CheckBusyResources();
77 EXPECT_EQ(resource_bytes
, resource_pool_
->GetTotalMemoryUsageForTesting());
78 EXPECT_EQ(0u, resource_pool_
->memory_usage_bytes());
79 EXPECT_EQ(1u, resource_pool_
->GetTotalResourceCountForTesting());
80 EXPECT_EQ(0u, resource_pool_
->resource_count());
81 EXPECT_EQ(0u, resource_pool_
->GetBusyResourceCountForTesting());
83 resource_pool_
->SetResourceUsageLimits(0u, 0u);
84 resource_pool_
->ReduceResourceUsage();
85 EXPECT_EQ(0u, resource_pool_
->GetTotalMemoryUsageForTesting());
86 EXPECT_EQ(0u, resource_pool_
->memory_usage_bytes());
87 EXPECT_EQ(0u, resource_pool_
->GetTotalResourceCountForTesting());
88 EXPECT_EQ(0u, resource_pool_
->resource_count());
89 EXPECT_EQ(0u, resource_pool_
->GetBusyResourceCountForTesting());
92 TEST_F(ResourcePoolTest
, SimpleResourceReuse
) {
93 // Limits high enough to not be hit by this test.
94 size_t bytes_limit
= 10 * 1024 * 1024;
95 size_t count_limit
= 100;
96 resource_pool_
->SetResourceUsageLimits(bytes_limit
, count_limit
);
98 gfx::Size
size(100, 100);
99 ResourceFormat format
= RGBA_8888
;
101 Resource
* resource
= resource_pool_
->AcquireResource(size
, format
);
102 resource_pool_
->ReleaseResource(resource
, 0u);
103 resource_pool_
->CheckBusyResources();
104 EXPECT_EQ(1u, resource_provider_
->num_resources());
106 // Same size/format should re-use resource.
107 resource
= resource_pool_
->AcquireResource(size
, format
);
108 EXPECT_EQ(1u, resource_provider_
->num_resources());
109 resource_pool_
->ReleaseResource(resource
, 0u);
110 resource_pool_
->CheckBusyResources();
111 EXPECT_EQ(1u, resource_provider_
->num_resources());
113 // Different size/format should alloate new resource.
114 resource
= resource_pool_
->AcquireResource(gfx::Size(50, 50), LUMINANCE_8
);
115 EXPECT_EQ(2u, resource_provider_
->num_resources());
116 resource_pool_
->ReleaseResource(resource
, 0u);
117 resource_pool_
->CheckBusyResources();
118 EXPECT_EQ(2u, resource_provider_
->num_resources());
121 TEST_F(ResourcePoolTest
, LostResource
) {
122 // Limits high enough to not be hit by this test.
123 size_t bytes_limit
= 10 * 1024 * 1024;
124 size_t count_limit
= 100;
125 resource_pool_
->SetResourceUsageLimits(bytes_limit
, count_limit
);
127 gfx::Size
size(100, 100);
128 ResourceFormat format
= RGBA_8888
;
130 Resource
* resource
= resource_pool_
->AcquireResource(size
, format
);
131 EXPECT_EQ(1u, resource_provider_
->num_resources());
133 resource_provider_
->LoseResourceForTesting(resource
->id());
134 resource_pool_
->ReleaseResource(resource
, 0u);
135 resource_pool_
->CheckBusyResources();
136 EXPECT_EQ(0u, resource_provider_
->num_resources());
139 TEST_F(ResourcePoolTest
, BusyResourcesEventuallyFreed
) {
140 // Limits high enough to not be hit by this test.
141 size_t bytes_limit
= 10 * 1024 * 1024;
142 size_t count_limit
= 100;
143 resource_pool_
->SetResourceUsageLimits(bytes_limit
, count_limit
);
145 // Set a quick resource expiration delay so that this test doesn't take long
147 resource_pool_
->SetResourceExpirationDelayForTesting(
148 base::TimeDelta::FromMilliseconds(10));
150 gfx::Size
size(100, 100);
151 ResourceFormat format
= RGBA_8888
;
153 Resource
* resource
= resource_pool_
->AcquireResource(size
, format
);
154 EXPECT_EQ(1u, resource_provider_
->num_resources());
155 EXPECT_EQ(40000u, resource_pool_
->GetTotalMemoryUsageForTesting());
156 EXPECT_EQ(1u, resource_pool_
->resource_count());
158 resource_pool_
->ReleaseResource(resource
, 0u);
159 EXPECT_EQ(1u, resource_provider_
->num_resources());
160 EXPECT_EQ(40000u, resource_pool_
->GetTotalMemoryUsageForTesting());
161 EXPECT_EQ(0u, resource_pool_
->memory_usage_bytes());
162 EXPECT_EQ(1u, resource_pool_
->GetBusyResourceCountForTesting());
164 // Wait for our resource pool to evict resources. We expect resources to be
165 // released within 10 ms, give the thread up to 200.
166 base::RunLoop run_loop
;
167 task_runner_
->PostDelayedTask(FROM_HERE
, run_loop
.QuitClosure(),
168 base::TimeDelta::FromMillisecondsD(200));
171 EXPECT_EQ(0u, resource_provider_
->num_resources());
172 EXPECT_EQ(0u, resource_pool_
->GetTotalMemoryUsageForTesting());
173 EXPECT_EQ(0u, resource_pool_
->memory_usage_bytes());
176 TEST_F(ResourcePoolTest
, UnusedResourcesEventuallyFreed
) {
177 // Limits high enough to not be hit by this test.
178 size_t bytes_limit
= 10 * 1024 * 1024;
179 size_t count_limit
= 100;
180 resource_pool_
->SetResourceUsageLimits(bytes_limit
, count_limit
);
182 // Set a quick resource expiration delay so that this test doesn't take long
184 resource_pool_
->SetResourceExpirationDelayForTesting(
185 base::TimeDelta::FromMilliseconds(100));
187 gfx::Size
size(100, 100);
188 ResourceFormat format
= RGBA_8888
;
190 Resource
* resource
= resource_pool_
->AcquireResource(size
, format
);
191 EXPECT_EQ(1u, resource_provider_
->num_resources());
192 EXPECT_EQ(40000u, resource_pool_
->GetTotalMemoryUsageForTesting());
193 EXPECT_EQ(1u, resource_pool_
->GetTotalResourceCountForTesting());
194 EXPECT_EQ(1u, resource_pool_
->resource_count());
196 resource_pool_
->ReleaseResource(resource
, 0u);
197 EXPECT_EQ(1u, resource_provider_
->num_resources());
198 EXPECT_EQ(40000u, resource_pool_
->GetTotalMemoryUsageForTesting());
199 EXPECT_EQ(1u, resource_pool_
->GetTotalResourceCountForTesting());
200 EXPECT_EQ(1u, resource_pool_
->GetBusyResourceCountForTesting());
202 // Transfer the resource from the busy pool to the unused pool.
203 resource_pool_
->CheckBusyResources();
204 EXPECT_EQ(1u, resource_provider_
->num_resources());
205 EXPECT_EQ(40000u, resource_pool_
->GetTotalMemoryUsageForTesting());
206 EXPECT_EQ(1u, resource_pool_
->GetTotalResourceCountForTesting());
207 EXPECT_EQ(0u, resource_pool_
->resource_count());
208 EXPECT_EQ(0u, resource_pool_
->GetBusyResourceCountForTesting());
210 // Wait for our resource pool to evict resources. We expect resources to be
211 // released within 100 ms, give the thread up to 200.
212 base::RunLoop run_loop
;
213 task_runner_
->PostDelayedTask(FROM_HERE
, run_loop
.QuitClosure(),
214 base::TimeDelta::FromMillisecondsD(200));
217 EXPECT_EQ(0u, resource_provider_
->num_resources());
218 EXPECT_EQ(0u, resource_pool_
->GetTotalMemoryUsageForTesting());