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 "cc/resources/resource_util.h"
8 #include "cc/resources/scoped_resource.h"
9 #include "cc/test/fake_output_surface.h"
10 #include "cc/test/fake_output_surface_client.h"
11 #include "cc/test/fake_resource_provider.h"
12 #include "cc/test/test_shared_bitmap_manager.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class ResourcePoolTest
: public testing::Test
{
20 void SetUp() override
{
21 output_surface_
= FakeOutputSurface::Create3d();
22 ASSERT_TRUE(output_surface_
->BindToClient(&output_surface_client_
));
23 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
24 resource_provider_
= FakeResourceProvider::Create(
25 output_surface_
.get(), shared_bitmap_manager_
.get());
27 ResourcePool::Create(resource_provider_
.get(), GL_TEXTURE_2D
);
31 FakeOutputSurfaceClient output_surface_client_
;
32 scoped_ptr
<FakeOutputSurface
> output_surface_
;
33 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
34 scoped_ptr
<ResourceProvider
> resource_provider_
;
35 scoped_ptr
<ResourcePool
> resource_pool_
;
38 TEST_F(ResourcePoolTest
, AcquireRelease
) {
39 gfx::Size
size(100, 100);
40 ResourceFormat format
= RGBA_8888
;
41 scoped_ptr
<ScopedResource
> resource
=
42 resource_pool_
->AcquireResource(size
, format
);
43 EXPECT_EQ(size
, resource
->size());
44 EXPECT_EQ(format
, resource
->format());
45 EXPECT_TRUE(resource_provider_
->CanLockForWrite(resource
->id()));
47 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
50 TEST_F(ResourcePoolTest
, AccountingSingleResource
) {
51 // Limits high enough to not be hit by this test.
52 size_t bytes_limit
= 10 * 1024 * 1024;
53 size_t count_limit
= 100;
54 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
56 gfx::Size
size(100, 100);
57 ResourceFormat format
= RGBA_8888
;
58 size_t resource_bytes
=
59 ResourceUtil::UncheckedSizeInBytes
<size_t>(size
, format
);
60 scoped_ptr
<ScopedResource
> resource
=
61 resource_pool_
->AcquireResource(size
, format
);
63 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
64 EXPECT_EQ(resource_bytes
, resource_pool_
->acquired_memory_usage_bytes());
65 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
66 EXPECT_EQ(1u, resource_pool_
->acquired_resource_count());
67 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
69 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
70 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
71 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
72 EXPECT_EQ(1u, resource_pool_
->busy_resource_count());
74 bool wait_if_needed
= false;
75 resource_pool_
->CheckBusyResources(wait_if_needed
);
76 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
77 EXPECT_EQ(0u, resource_pool_
->acquired_memory_usage_bytes());
78 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
79 EXPECT_EQ(0u, resource_pool_
->acquired_resource_count());
80 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
82 resource_pool_
->SetResourceUsageLimits(0u, 0u, 0u);
83 resource_pool_
->ReduceResourceUsage();
84 EXPECT_EQ(0u, resource_pool_
->total_memory_usage_bytes());
85 EXPECT_EQ(0u, resource_pool_
->acquired_memory_usage_bytes());
86 EXPECT_EQ(0u, resource_pool_
->total_resource_count());
87 EXPECT_EQ(0u, resource_pool_
->acquired_resource_count());
88 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
91 TEST_F(ResourcePoolTest
, SimpleResourceReuse
) {
92 // Limits high enough to not be hit by this test.
93 size_t bytes_limit
= 10 * 1024 * 1024;
94 size_t count_limit
= 100;
95 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
97 gfx::Size
size(100, 100);
98 ResourceFormat format
= RGBA_8888
;
99 bool wait_if_needed
= false;
101 scoped_ptr
<ScopedResource
> resource
=
102 resource_pool_
->AcquireResource(size
, format
);
103 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
104 resource_pool_
->CheckBusyResources(wait_if_needed
);
105 EXPECT_EQ(1u, resource_provider_
->num_resources());
107 // Same size/format should re-use resource.
108 resource
= resource_pool_
->AcquireResource(size
, format
);
109 EXPECT_EQ(1u, resource_provider_
->num_resources());
110 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
111 resource_pool_
->CheckBusyResources(wait_if_needed
);
112 EXPECT_EQ(1u, resource_provider_
->num_resources());
114 // Different size/format should alloate new resource.
115 resource
= resource_pool_
->AcquireResource(gfx::Size(50, 50), LUMINANCE_8
);
116 EXPECT_EQ(2u, resource_provider_
->num_resources());
117 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
118 resource_pool_
->CheckBusyResources(wait_if_needed
);
119 EXPECT_EQ(2u, resource_provider_
->num_resources());
122 TEST_F(ResourcePoolTest
, LostResource
) {
123 // Limits high enough to not be hit by this test.
124 size_t bytes_limit
= 10 * 1024 * 1024;
125 size_t count_limit
= 100;
126 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
128 gfx::Size
size(100, 100);
129 ResourceFormat format
= RGBA_8888
;
130 bool wait_if_needed
= false;
132 scoped_ptr
<ScopedResource
> resource
=
133 resource_pool_
->AcquireResource(size
, format
);
134 EXPECT_EQ(1u, resource_provider_
->num_resources());
136 resource_provider_
->LoseResourceForTesting(resource
->id());
137 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
138 resource_pool_
->CheckBusyResources(wait_if_needed
);
139 EXPECT_EQ(0u, resource_provider_
->num_resources());