1 // Copyright 2012 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. 4 5 #include "cc/resources/scoped_resource.h" 6 7 #include "cc/output/renderer.h" 8 #include "cc/test/fake_output_surface.h" 9 #include "cc/test/fake_output_surface_client.h" 10 #include "cc/test/test_shared_bitmap_manager.h" 11 #include "cc/test/tiled_layer_test_common.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace cc { 15 namespace { 16 17 TEST(ScopedResourceTest, NewScopedResource) { 18 FakeOutputSurfaceClient output_surface_client; 19 scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d()); 20 CHECK(output_surface->BindToClient(&output_surface_client)); 21 22 scoped_ptr<SharedBitmapManager> shared_bitmap_manager( 23 new TestSharedBitmapManager()); 24 scoped_ptr<ResourceProvider> resource_provider(ResourceProvider::Create( 25 output_surface.get(), shared_bitmap_manager.get(), 0, false, 1, false)); 26 scoped_ptr<ScopedResource> texture = 27 ScopedResource::Create(resource_provider.get()); 28 29 // New scoped textures do not hold a texture yet. 30 EXPECT_EQ(0u, texture->id()); 31 32 // New scoped textures do not have a size yet. 33 EXPECT_EQ(gfx::Size(), texture->size()); 34 EXPECT_EQ(0u, texture->bytes()); 35 } 36 37 TEST(ScopedResourceTest, CreateScopedResource) { 38 FakeOutputSurfaceClient output_surface_client; 39 scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d()); 40 CHECK(output_surface->BindToClient(&output_surface_client)); 41 42 scoped_ptr<SharedBitmapManager> shared_bitmap_manager( 43 new TestSharedBitmapManager()); 44 scoped_ptr<ResourceProvider> resource_provider(ResourceProvider::Create( 45 output_surface.get(), shared_bitmap_manager.get(), 0, false, 1, false)); 46 scoped_ptr<ScopedResource> texture = 47 ScopedResource::Create(resource_provider.get()); 48 texture->Allocate(gfx::Size(30, 30), 49 ResourceProvider::TextureUsageAny, 50 RGBA_8888); 51 52 // The texture has an allocated byte-size now. 53 size_t expected_bytes = 30 * 30 * 4; 54 EXPECT_EQ(expected_bytes, texture->bytes()); 55 56 EXPECT_LT(0u, texture->id()); 57 EXPECT_EQ(static_cast<unsigned>(RGBA_8888), texture->format()); 58 EXPECT_EQ(gfx::Size(30, 30), texture->size()); 59 } 60 61 TEST(ScopedResourceTest, ScopedResourceIsDeleted) { 62 FakeOutputSurfaceClient output_surface_client; 63 scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d()); 64 CHECK(output_surface->BindToClient(&output_surface_client)); 65 66 scoped_ptr<SharedBitmapManager> shared_bitmap_manager( 67 new TestSharedBitmapManager()); 68 scoped_ptr<ResourceProvider> resource_provider(ResourceProvider::Create( 69 output_surface.get(), shared_bitmap_manager.get(), 0, false, 1, false)); 70 { 71 scoped_ptr<ScopedResource> texture = 72 ScopedResource::Create(resource_provider.get()); 73 74 EXPECT_EQ(0u, resource_provider->num_resources()); 75 texture->Allocate(gfx::Size(30, 30), 76 ResourceProvider::TextureUsageAny, 77 RGBA_8888); 78 EXPECT_LT(0u, texture->id()); 79 EXPECT_EQ(1u, resource_provider->num_resources()); 80 } 81 82 EXPECT_EQ(0u, resource_provider->num_resources()); 83 { 84 scoped_ptr<ScopedResource> texture = 85 ScopedResource::Create(resource_provider.get()); 86 EXPECT_EQ(0u, resource_provider->num_resources()); 87 texture->Allocate(gfx::Size(30, 30), 88 ResourceProvider::TextureUsageAny, 89 RGBA_8888); 90 EXPECT_LT(0u, texture->id()); 91 EXPECT_EQ(1u, resource_provider->num_resources()); 92 texture->Free(); 93 EXPECT_EQ(0u, resource_provider->num_resources()); 94 } 95 } 96 97 TEST(ScopedResourceTest, LeakScopedResource) { 98 FakeOutputSurfaceClient output_surface_client; 99 scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d()); 100 CHECK(output_surface->BindToClient(&output_surface_client)); 101 102 scoped_ptr<SharedBitmapManager> shared_bitmap_manager( 103 new TestSharedBitmapManager()); 104 scoped_ptr<ResourceProvider> resource_provider(ResourceProvider::Create( 105 output_surface.get(), shared_bitmap_manager.get(), 0, false, 1, false)); 106 { 107 scoped_ptr<ScopedResource> texture = 108 ScopedResource::Create(resource_provider.get()); 109 110 EXPECT_EQ(0u, resource_provider->num_resources()); 111 texture->Allocate(gfx::Size(30, 30), 112 ResourceProvider::TextureUsageAny, 113 RGBA_8888); 114 EXPECT_LT(0u, texture->id()); 115 EXPECT_EQ(1u, resource_provider->num_resources()); 116 117 texture->Leak(); 118 EXPECT_EQ(0u, texture->id()); 119 EXPECT_EQ(1u, resource_provider->num_resources()); 120 121 texture->Free(); 122 EXPECT_EQ(0u, texture->id()); 123 EXPECT_EQ(1u, resource_provider->num_resources()); 124 } 125 126 EXPECT_EQ(1u, resource_provider->num_resources()); 127 } 128 129 } // namespace 130 } // namespace cc 131