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/scoped_resource.h"
8 #include "cc/test/fake_output_surface.h"
9 #include "cc/test/fake_output_surface_client.h"
10 #include "cc/test/fake_resource_provider.h"
11 #include "cc/test/test_shared_bitmap_manager.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 class ResourcePoolTest
: public testing::Test
{
19 void SetUp() override
{
20 output_surface_
= FakeOutputSurface::Create3d();
21 ASSERT_TRUE(output_surface_
->BindToClient(&output_surface_client_
));
22 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
23 resource_provider_
= FakeResourceProvider::Create(
24 output_surface_
.get(), shared_bitmap_manager_
.get());
26 ResourcePool::Create(resource_provider_
.get(), GL_TEXTURE_2D
);
30 FakeOutputSurfaceClient output_surface_client_
;
31 scoped_ptr
<FakeOutputSurface
> output_surface_
;
32 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
33 scoped_ptr
<ResourceProvider
> resource_provider_
;
34 scoped_ptr
<ResourcePool
> resource_pool_
;
37 TEST_F(ResourcePoolTest
, AcquireRelease
) {
38 gfx::Size
size(100, 100);
39 ResourceFormat format
= RGBA_8888
;
40 scoped_ptr
<ScopedResource
> resource
=
41 resource_pool_
->AcquireResource(size
, format
);
42 EXPECT_EQ(size
, resource
->size());
43 EXPECT_EQ(format
, resource
->format());
44 EXPECT_TRUE(resource_provider_
->CanLockForWrite(resource
->id()));
46 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
49 TEST_F(ResourcePoolTest
, AccountingSingleResource
) {
50 // Limits high enough to not be hit by this test.
51 size_t bytes_limit
= 10 * 1024 * 1024;
52 size_t count_limit
= 100;
53 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
55 gfx::Size
size(100, 100);
56 ResourceFormat format
= RGBA_8888
;
57 size_t resource_bytes
= Resource::UncheckedMemorySizeBytes(size
, format
);
58 scoped_ptr
<ScopedResource
> resource
=
59 resource_pool_
->AcquireResource(size
, format
);
61 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
62 EXPECT_EQ(resource_bytes
, resource_pool_
->acquired_memory_usage_bytes());
63 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
64 EXPECT_EQ(1u, resource_pool_
->acquired_resource_count());
65 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
67 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
68 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
69 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
70 EXPECT_EQ(1u, resource_pool_
->busy_resource_count());
72 bool wait_if_needed
= false;
73 resource_pool_
->CheckBusyResources(wait_if_needed
);
74 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
75 EXPECT_EQ(0u, resource_pool_
->acquired_memory_usage_bytes());
76 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
77 EXPECT_EQ(0u, resource_pool_
->acquired_resource_count());
78 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
80 resource_pool_
->SetResourceUsageLimits(0u, 0u, 0u);
81 resource_pool_
->ReduceResourceUsage();
82 EXPECT_EQ(0u, resource_pool_
->total_memory_usage_bytes());
83 EXPECT_EQ(0u, resource_pool_
->acquired_memory_usage_bytes());
84 EXPECT_EQ(0u, resource_pool_
->total_resource_count());
85 EXPECT_EQ(0u, resource_pool_
->acquired_resource_count());
86 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
89 TEST_F(ResourcePoolTest
, SimpleResourceReuse
) {
90 // Limits high enough to not be hit by this test.
91 size_t bytes_limit
= 10 * 1024 * 1024;
92 size_t count_limit
= 100;
93 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
95 gfx::Size
size(100, 100);
96 ResourceFormat format
= RGBA_8888
;
97 bool wait_if_needed
= false;
99 scoped_ptr
<ScopedResource
> resource
=
100 resource_pool_
->AcquireResource(size
, format
);
101 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
102 resource_pool_
->CheckBusyResources(wait_if_needed
);
103 EXPECT_EQ(1u, resource_provider_
->num_resources());
105 // Same size/format should re-use resource.
106 resource
= resource_pool_
->AcquireResource(size
, format
);
107 EXPECT_EQ(1u, resource_provider_
->num_resources());
108 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
109 resource_pool_
->CheckBusyResources(wait_if_needed
);
110 EXPECT_EQ(1u, resource_provider_
->num_resources());
112 // Different size/format should alloate new resource.
113 resource
= resource_pool_
->AcquireResource(gfx::Size(50, 50), LUMINANCE_8
);
114 EXPECT_EQ(2u, resource_provider_
->num_resources());
115 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
116 resource_pool_
->CheckBusyResources(wait_if_needed
);
117 EXPECT_EQ(2u, resource_provider_
->num_resources());
120 TEST_F(ResourcePoolTest
, LostResource
) {
121 // Limits high enough to not be hit by this test.
122 size_t bytes_limit
= 10 * 1024 * 1024;
123 size_t count_limit
= 100;
124 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
126 gfx::Size
size(100, 100);
127 ResourceFormat format
= RGBA_8888
;
128 bool wait_if_needed
= false;
130 scoped_ptr
<ScopedResource
> resource
=
131 resource_pool_
->AcquireResource(size
, format
);
132 EXPECT_EQ(1u, resource_provider_
->num_resources());
134 resource_provider_
->LoseResourceForTesting(resource
->id());
135 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
136 resource_pool_
->CheckBusyResources(wait_if_needed
);
137 EXPECT_EQ(0u, resource_provider_
->num_resources());