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 resource_pool_
->CheckBusyResources();
75 EXPECT_EQ(resource_bytes
, resource_pool_
->total_memory_usage_bytes());
76 EXPECT_EQ(0u, resource_pool_
->acquired_memory_usage_bytes());
77 EXPECT_EQ(1u, resource_pool_
->total_resource_count());
78 EXPECT_EQ(0u, resource_pool_
->acquired_resource_count());
79 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
81 resource_pool_
->SetResourceUsageLimits(0u, 0u, 0u);
82 resource_pool_
->ReduceResourceUsage();
83 EXPECT_EQ(0u, resource_pool_
->total_memory_usage_bytes());
84 EXPECT_EQ(0u, resource_pool_
->acquired_memory_usage_bytes());
85 EXPECT_EQ(0u, resource_pool_
->total_resource_count());
86 EXPECT_EQ(0u, resource_pool_
->acquired_resource_count());
87 EXPECT_EQ(0u, resource_pool_
->busy_resource_count());
90 TEST_F(ResourcePoolTest
, SimpleResourceReuse
) {
91 // Limits high enough to not be hit by this test.
92 size_t bytes_limit
= 10 * 1024 * 1024;
93 size_t count_limit
= 100;
94 resource_pool_
->SetResourceUsageLimits(bytes_limit
, bytes_limit
, count_limit
);
96 gfx::Size
size(100, 100);
97 ResourceFormat format
= RGBA_8888
;
99 scoped_ptr
<ScopedResource
> resource
=
100 resource_pool_
->AcquireResource(size
, format
);
101 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
102 resource_pool_
->CheckBusyResources();
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();
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();
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
;
129 scoped_ptr
<ScopedResource
> resource
=
130 resource_pool_
->AcquireResource(size
, format
);
131 EXPECT_EQ(1u, resource_provider_
->num_resources());
133 resource_provider_
->LoseResourceForTesting(resource
->id());
134 resource_pool_
->ReleaseResource(resource
.Pass(), 0u);
135 resource_pool_
->CheckBusyResources();
136 EXPECT_EQ(0u, resource_provider_
->num_resources());