OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/resource_pool.h" | 5 #include "cc/resources/resource_pool.h" |
6 | 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "base/thread_task_runner_handle.h" |
7 #include "cc/resources/resource_util.h" | 9 #include "cc/resources/resource_util.h" |
8 #include "cc/resources/scoped_resource.h" | 10 #include "cc/resources/scoped_resource.h" |
9 #include "cc/test/fake_output_surface.h" | 11 #include "cc/test/fake_output_surface.h" |
10 #include "cc/test/fake_output_surface_client.h" | 12 #include "cc/test/fake_output_surface_client.h" |
11 #include "cc/test/fake_resource_provider.h" | 13 #include "cc/test/fake_resource_provider.h" |
12 #include "cc/test/test_shared_bitmap_manager.h" | 14 #include "cc/test/test_shared_bitmap_manager.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 namespace cc { | 17 namespace cc { |
16 namespace { | 18 namespace { |
17 | 19 |
18 class ResourcePoolTest : public testing::Test { | 20 class ResourcePoolTest : public testing::Test { |
19 public: | 21 public: |
20 void SetUp() override { | 22 void SetUp() override { |
21 output_surface_ = FakeOutputSurface::Create3d(); | 23 output_surface_ = FakeOutputSurface::Create3d(); |
22 ASSERT_TRUE(output_surface_->BindToClient(&output_surface_client_)); | 24 ASSERT_TRUE(output_surface_->BindToClient(&output_surface_client_)); |
23 shared_bitmap_manager_.reset(new TestSharedBitmapManager()); | 25 shared_bitmap_manager_.reset(new TestSharedBitmapManager()); |
24 resource_provider_ = FakeResourceProvider::Create( | 26 resource_provider_ = FakeResourceProvider::Create( |
25 output_surface_.get(), shared_bitmap_manager_.get()); | 27 output_surface_.get(), shared_bitmap_manager_.get()); |
26 resource_pool_ = | 28 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
27 ResourcePool::Create(resource_provider_.get(), GL_TEXTURE_2D); | 29 resource_pool_ = ResourcePool::Create(resource_provider_.get(), |
| 30 task_runner_.get(), GL_TEXTURE_2D); |
28 } | 31 } |
29 | 32 |
30 protected: | 33 protected: |
31 FakeOutputSurfaceClient output_surface_client_; | 34 FakeOutputSurfaceClient output_surface_client_; |
32 scoped_ptr<FakeOutputSurface> output_surface_; | 35 scoped_ptr<FakeOutputSurface> output_surface_; |
33 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_; | 36 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_; |
34 scoped_ptr<ResourceProvider> resource_provider_; | 37 scoped_ptr<ResourceProvider> resource_provider_; |
| 38 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
35 scoped_ptr<ResourcePool> resource_pool_; | 39 scoped_ptr<ResourcePool> resource_pool_; |
36 }; | 40 }; |
37 | 41 |
38 TEST_F(ResourcePoolTest, AcquireRelease) { | 42 TEST_F(ResourcePoolTest, AcquireRelease) { |
39 gfx::Size size(100, 100); | 43 gfx::Size size(100, 100); |
40 ResourceFormat format = RGBA_8888; | 44 ResourceFormat format = RGBA_8888; |
41 Resource* resource = resource_pool_->AcquireResource(size, format); | 45 Resource* resource = resource_pool_->AcquireResource(size, format); |
42 EXPECT_EQ(size, resource->size()); | 46 EXPECT_EQ(size, resource->size()); |
43 EXPECT_EQ(format, resource->format()); | 47 EXPECT_EQ(format, resource->format()); |
44 EXPECT_TRUE(resource_provider_->CanLockForWrite(resource->id())); | 48 EXPECT_TRUE(resource_provider_->CanLockForWrite(resource->id())); |
45 | 49 |
46 resource_pool_->ReleaseResource(resource, 0u); | 50 resource_pool_->ReleaseResource(resource, 0u); |
47 } | 51 } |
48 | 52 |
49 TEST_F(ResourcePoolTest, AccountingSingleResource) { | 53 TEST_F(ResourcePoolTest, AccountingSingleResource) { |
50 // Limits high enough to not be hit by this test. | 54 // Limits high enough to not be hit by this test. |
51 size_t bytes_limit = 10 * 1024 * 1024; | 55 size_t bytes_limit = 10 * 1024 * 1024; |
52 size_t count_limit = 100; | 56 size_t count_limit = 100; |
53 resource_pool_->SetResourceUsageLimits(bytes_limit, bytes_limit, count_limit); | 57 resource_pool_->SetResourceUsageLimits(bytes_limit, count_limit); |
54 | 58 |
55 gfx::Size size(100, 100); | 59 gfx::Size size(100, 100); |
56 ResourceFormat format = RGBA_8888; | 60 ResourceFormat format = RGBA_8888; |
57 size_t resource_bytes = | 61 size_t resource_bytes = |
58 ResourceUtil::UncheckedSizeInBytes<size_t>(size, format); | 62 ResourceUtil::UncheckedSizeInBytes<size_t>(size, format); |
59 Resource* resource = resource_pool_->AcquireResource(size, format); | 63 Resource* resource = resource_pool_->AcquireResource(size, format); |
60 | 64 |
61 EXPECT_EQ(resource_bytes, resource_pool_->total_memory_usage_bytes()); | 65 EXPECT_EQ(resource_bytes, resource_pool_->GetTotalMemoryUsageForTesting()); |
62 EXPECT_EQ(resource_bytes, resource_pool_->acquired_memory_usage_bytes()); | 66 EXPECT_EQ(resource_bytes, resource_pool_->memory_usage_bytes()); |
63 EXPECT_EQ(1u, resource_pool_->total_resource_count()); | 67 EXPECT_EQ(1u, resource_pool_->GetTotalResourceCountForTesting()); |
64 EXPECT_EQ(1u, resource_pool_->acquired_resource_count()); | 68 EXPECT_EQ(1u, resource_pool_->resource_count()); |
65 EXPECT_EQ(0u, resource_pool_->busy_resource_count()); | 69 EXPECT_EQ(0u, resource_pool_->GetBusyResourceCountForTesting()); |
66 | 70 |
67 resource_pool_->ReleaseResource(resource, 0u); | 71 resource_pool_->ReleaseResource(resource, 0u); |
68 EXPECT_EQ(resource_bytes, resource_pool_->total_memory_usage_bytes()); | 72 EXPECT_EQ(resource_bytes, resource_pool_->GetTotalMemoryUsageForTesting()); |
69 EXPECT_EQ(1u, resource_pool_->total_resource_count()); | 73 EXPECT_EQ(1u, resource_pool_->GetTotalResourceCountForTesting()); |
70 EXPECT_EQ(1u, resource_pool_->busy_resource_count()); | 74 EXPECT_EQ(1u, resource_pool_->GetBusyResourceCountForTesting()); |
71 | 75 |
72 resource_pool_->CheckBusyResources(); | 76 resource_pool_->CheckBusyResources(); |
73 EXPECT_EQ(resource_bytes, resource_pool_->total_memory_usage_bytes()); | 77 EXPECT_EQ(resource_bytes, resource_pool_->GetTotalMemoryUsageForTesting()); |
74 EXPECT_EQ(0u, resource_pool_->acquired_memory_usage_bytes()); | 78 EXPECT_EQ(0u, resource_pool_->memory_usage_bytes()); |
75 EXPECT_EQ(1u, resource_pool_->total_resource_count()); | 79 EXPECT_EQ(1u, resource_pool_->GetTotalResourceCountForTesting()); |
76 EXPECT_EQ(0u, resource_pool_->acquired_resource_count()); | 80 EXPECT_EQ(0u, resource_pool_->resource_count()); |
77 EXPECT_EQ(0u, resource_pool_->busy_resource_count()); | 81 EXPECT_EQ(0u, resource_pool_->GetBusyResourceCountForTesting()); |
78 | 82 |
79 resource_pool_->SetResourceUsageLimits(0u, 0u, 0u); | 83 resource_pool_->SetResourceUsageLimits(0u, 0u); |
80 resource_pool_->ReduceResourceUsage(); | 84 resource_pool_->ReduceResourceUsage(); |
81 EXPECT_EQ(0u, resource_pool_->total_memory_usage_bytes()); | 85 EXPECT_EQ(0u, resource_pool_->GetTotalMemoryUsageForTesting()); |
82 EXPECT_EQ(0u, resource_pool_->acquired_memory_usage_bytes()); | 86 EXPECT_EQ(0u, resource_pool_->memory_usage_bytes()); |
83 EXPECT_EQ(0u, resource_pool_->total_resource_count()); | 87 EXPECT_EQ(0u, resource_pool_->GetTotalResourceCountForTesting()); |
84 EXPECT_EQ(0u, resource_pool_->acquired_resource_count()); | 88 EXPECT_EQ(0u, resource_pool_->resource_count()); |
85 EXPECT_EQ(0u, resource_pool_->busy_resource_count()); | 89 EXPECT_EQ(0u, resource_pool_->GetBusyResourceCountForTesting()); |
86 } | 90 } |
87 | 91 |
88 TEST_F(ResourcePoolTest, SimpleResourceReuse) { | 92 TEST_F(ResourcePoolTest, SimpleResourceReuse) { |
89 // Limits high enough to not be hit by this test. | 93 // Limits high enough to not be hit by this test. |
90 size_t bytes_limit = 10 * 1024 * 1024; | 94 size_t bytes_limit = 10 * 1024 * 1024; |
91 size_t count_limit = 100; | 95 size_t count_limit = 100; |
92 resource_pool_->SetResourceUsageLimits(bytes_limit, bytes_limit, count_limit); | 96 resource_pool_->SetResourceUsageLimits(bytes_limit, count_limit); |
93 | 97 |
94 gfx::Size size(100, 100); | 98 gfx::Size size(100, 100); |
95 ResourceFormat format = RGBA_8888; | 99 ResourceFormat format = RGBA_8888; |
96 | 100 |
97 Resource* resource = resource_pool_->AcquireResource(size, format); | 101 Resource* resource = resource_pool_->AcquireResource(size, format); |
98 resource_pool_->ReleaseResource(resource, 0u); | 102 resource_pool_->ReleaseResource(resource, 0u); |
99 resource_pool_->CheckBusyResources(); | 103 resource_pool_->CheckBusyResources(); |
100 EXPECT_EQ(1u, resource_provider_->num_resources()); | 104 EXPECT_EQ(1u, resource_provider_->num_resources()); |
101 | 105 |
102 // Same size/format should re-use resource. | 106 // Same size/format should re-use resource. |
103 resource = resource_pool_->AcquireResource(size, format); | 107 resource = resource_pool_->AcquireResource(size, format); |
104 EXPECT_EQ(1u, resource_provider_->num_resources()); | 108 EXPECT_EQ(1u, resource_provider_->num_resources()); |
105 resource_pool_->ReleaseResource(resource, 0u); | 109 resource_pool_->ReleaseResource(resource, 0u); |
106 resource_pool_->CheckBusyResources(); | 110 resource_pool_->CheckBusyResources(); |
107 EXPECT_EQ(1u, resource_provider_->num_resources()); | 111 EXPECT_EQ(1u, resource_provider_->num_resources()); |
108 | 112 |
109 // Different size/format should alloate new resource. | 113 // Different size/format should alloate new resource. |
110 resource = resource_pool_->AcquireResource(gfx::Size(50, 50), LUMINANCE_8); | 114 resource = resource_pool_->AcquireResource(gfx::Size(50, 50), LUMINANCE_8); |
111 EXPECT_EQ(2u, resource_provider_->num_resources()); | 115 EXPECT_EQ(2u, resource_provider_->num_resources()); |
112 resource_pool_->ReleaseResource(resource, 0u); | 116 resource_pool_->ReleaseResource(resource, 0u); |
113 resource_pool_->CheckBusyResources(); | 117 resource_pool_->CheckBusyResources(); |
114 EXPECT_EQ(2u, resource_provider_->num_resources()); | 118 EXPECT_EQ(2u, resource_provider_->num_resources()); |
115 } | 119 } |
116 | 120 |
117 TEST_F(ResourcePoolTest, LostResource) { | 121 TEST_F(ResourcePoolTest, LostResource) { |
118 // Limits high enough to not be hit by this test. | 122 // Limits high enough to not be hit by this test. |
119 size_t bytes_limit = 10 * 1024 * 1024; | 123 size_t bytes_limit = 10 * 1024 * 1024; |
120 size_t count_limit = 100; | 124 size_t count_limit = 100; |
121 resource_pool_->SetResourceUsageLimits(bytes_limit, bytes_limit, count_limit); | 125 resource_pool_->SetResourceUsageLimits(bytes_limit, count_limit); |
122 | 126 |
123 gfx::Size size(100, 100); | 127 gfx::Size size(100, 100); |
124 ResourceFormat format = RGBA_8888; | 128 ResourceFormat format = RGBA_8888; |
125 | 129 |
126 Resource* resource = resource_pool_->AcquireResource(size, format); | 130 Resource* resource = resource_pool_->AcquireResource(size, format); |
127 EXPECT_EQ(1u, resource_provider_->num_resources()); | 131 EXPECT_EQ(1u, resource_provider_->num_resources()); |
128 | 132 |
129 resource_provider_->LoseResourceForTesting(resource->id()); | 133 resource_provider_->LoseResourceForTesting(resource->id()); |
130 resource_pool_->ReleaseResource(resource, 0u); | 134 resource_pool_->ReleaseResource(resource, 0u); |
131 resource_pool_->CheckBusyResources(); | 135 resource_pool_->CheckBusyResources(); |
132 EXPECT_EQ(0u, resource_provider_->num_resources()); | 136 EXPECT_EQ(0u, resource_provider_->num_resources()); |
133 } | 137 } |
134 | 138 |
| 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); |
| 144 |
| 145 // Set a quick resource expiration delay so that this test doesn't take long |
| 146 // to run. |
| 147 resource_pool_->SetResourceExpirationDelayForTesting( |
| 148 base::TimeDelta::FromMilliseconds(10)); |
| 149 |
| 150 gfx::Size size(100, 100); |
| 151 ResourceFormat format = RGBA_8888; |
| 152 |
| 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()); |
| 157 |
| 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()); |
| 163 |
| 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)); |
| 169 run_loop.Run(); |
| 170 |
| 171 EXPECT_EQ(0u, resource_provider_->num_resources()); |
| 172 EXPECT_EQ(0u, resource_pool_->GetTotalMemoryUsageForTesting()); |
| 173 EXPECT_EQ(0u, resource_pool_->memory_usage_bytes()); |
| 174 } |
| 175 |
| 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); |
| 181 |
| 182 // Set a quick resource expiration delay so that this test doesn't take long |
| 183 // to run. |
| 184 resource_pool_->SetResourceExpirationDelayForTesting( |
| 185 base::TimeDelta::FromMilliseconds(100)); |
| 186 |
| 187 gfx::Size size(100, 100); |
| 188 ResourceFormat format = RGBA_8888; |
| 189 |
| 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()); |
| 195 |
| 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()); |
| 201 |
| 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()); |
| 209 |
| 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)); |
| 215 run_loop.Run(); |
| 216 |
| 217 EXPECT_EQ(0u, resource_provider_->num_resources()); |
| 218 EXPECT_EQ(0u, resource_pool_->GetTotalMemoryUsageForTesting()); |
| 219 } |
| 220 |
135 } // namespace | 221 } // namespace |
136 } // namespace cc | 222 } // namespace cc |
OLD | NEW |