OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/gpu/texture_cache.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "mojo/gpu/gl_context.h" | |
10 #include "mojo/gpu/gl_texture.h" | |
11 #include "mojo/public/cpp/application/application_impl.h" | |
12 #include "mojo/public/cpp/application/application_test_base.h" | |
13 #include "mojo/services/geometry/interfaces/geometry.mojom.h" | |
14 #include "mojo/services/surfaces/interfaces/surface_id.mojom.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace { | |
18 | |
19 static const base::TimeDelta kDefaultMessageDelay = | |
20 base::TimeDelta::FromMilliseconds(20); | |
21 | |
22 class TextureCacheTest : public mojo::test::ApplicationTestBase { | |
23 public: | |
24 TextureCacheTest() : weak_factory_(this) {} | |
25 ~TextureCacheTest() override {} | |
26 | |
27 void SetUp() override { | |
28 mojo::test::ApplicationTestBase::SetUp(); | |
29 gl_context_ = mojo::GLContext::Create(application_impl()->shell()); | |
30 quit_message_loop_callback_ = base::Bind( | |
31 &TextureCacheTest::QuitMessageLoopCallback, weak_factory_.GetWeakPtr()); | |
32 } | |
33 | |
34 void QuitMessageLoopCallback() { base::MessageLoop::current()->Quit(); } | |
35 | |
36 void KickMessageLoop() { | |
37 base::MessageLoop::current()->PostDelayedTask( | |
38 FROM_HERE, quit_message_loop_callback_, kDefaultMessageDelay); | |
39 base::MessageLoop::current()->Run(); | |
40 } | |
41 | |
42 protected: | |
43 base::WeakPtr<mojo::GLContext> gl_context_; | |
44 base::Closure quit_message_loop_callback_; | |
45 base::WeakPtrFactory<TextureCacheTest> weak_factory_; | |
46 | |
47 private: | |
48 DISALLOW_COPY_AND_ASSIGN(TextureCacheTest); | |
49 }; | |
50 | |
51 TEST_F(TextureCacheTest, GetTextureOnce) { | |
52 mojo::TextureCache texture_cache(gl_context_, nullptr); | |
53 mojo::Size size; | |
54 size.width = 100; | |
55 size.height = 100; | |
56 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info( | |
57 texture_cache.GetTexture(size).Pass()); | |
58 EXPECT_NE(texture_info->TakeTexture().get(), nullptr); | |
59 } | |
60 | |
61 TEST_F(TextureCacheTest, GetTextureTwice) { | |
62 mojo::TextureCache texture_cache(gl_context_, nullptr); | |
63 mojo::Size size; | |
64 size.width = 100; | |
65 size.height = 100; | |
66 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info_1( | |
67 texture_cache.GetTexture(size).Pass()); | |
68 scoped_ptr<mojo::GLTexture> texture_1(texture_info_1->TakeTexture().Pass()); | |
69 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info_2( | |
70 texture_cache.GetTexture(size).Pass()); | |
71 scoped_ptr<mojo::GLTexture> texture_2(texture_info_2->TakeTexture().Pass()); | |
72 | |
73 EXPECT_NE(texture_1.get(), nullptr); | |
74 EXPECT_NE(texture_2.get(), nullptr); | |
75 EXPECT_NE(texture_1.get(), texture_2.get()); | |
76 EXPECT_NE(texture_info_1->resource_id(), texture_info_2->resource_id()); | |
77 } | |
78 | |
79 TEST_F(TextureCacheTest, GetTextureAfterReturnSameSize) { | |
80 mojo::ResourceReturnerPtr resource_returner; | |
81 mojo::TextureCache texture_cache(gl_context_, &resource_returner); | |
82 mojo::Size size; | |
83 size.width = 100; | |
84 size.height = 100; | |
85 | |
86 // get a texture | |
87 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info_1( | |
88 texture_cache.GetTexture(size).Pass()); | |
89 scoped_ptr<mojo::GLTexture> texture(texture_info_1->TakeTexture().Pass()); | |
90 mojo::GLTexture* texture_ptr = texture.get(); | |
91 EXPECT_NE(texture_ptr, nullptr); | |
92 | |
93 mojo::Array<mojo::ReturnedResourcePtr> resources; | |
94 mojo::ReturnedResourcePtr returnedResource = mojo::ReturnedResource::New(); | |
95 returnedResource->id = texture_info_1->resource_id(); | |
96 returnedResource->sync_point = 0u; | |
97 returnedResource->count = 1u; | |
98 returnedResource->lost = false; | |
99 resources.push_back(returnedResource.Pass()); | |
100 | |
101 // return the texture via resource id | |
102 texture_cache.NotifyPendingResourceReturn(texture_info_1->resource_id(), | |
103 texture.Pass()); | |
104 resource_returner->ReturnResources(resources.Pass()); | |
105 | |
106 KickMessageLoop(); | |
107 | |
108 // get a texture of the same size - it should be the same one as before | |
109 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info_2( | |
110 texture_cache.GetTexture(size).Pass()); | |
111 scoped_ptr<mojo::GLTexture> texture_2(texture_info_2->TakeTexture().Pass()); | |
112 | |
113 EXPECT_NE(texture_2.get(), nullptr); | |
114 EXPECT_EQ(size.width, texture_2->size().width); | |
115 EXPECT_EQ(size.height, texture_2->size().height); | |
116 EXPECT_EQ(texture_info_1->resource_id(), texture_info_2->resource_id()); | |
117 } | |
118 | |
119 TEST_F(TextureCacheTest, GetTextureAfterReturnDifferentSize) { | |
120 mojo::ResourceReturnerPtr resource_returner; | |
121 mojo::TextureCache texture_cache(gl_context_, &resource_returner); | |
122 mojo::Size size; | |
123 size.width = 100; | |
124 size.height = 100; | |
125 | |
126 // get a texture | |
127 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info_1( | |
128 texture_cache.GetTexture(size).Pass()); | |
129 scoped_ptr<mojo::GLTexture> texture(texture_info_1->TakeTexture().Pass()); | |
130 mojo::GLTexture* texture_ptr = texture.get(); | |
131 EXPECT_NE(texture_ptr, nullptr); | |
132 | |
133 mojo::Array<mojo::ReturnedResourcePtr> resources; | |
134 mojo::ReturnedResourcePtr returnedResource = mojo::ReturnedResource::New(); | |
135 returnedResource->id = texture_info_1->resource_id(); | |
136 returnedResource->sync_point = 0u; | |
137 returnedResource->count = 1u; | |
138 returnedResource->lost = false; | |
139 resources.push_back(returnedResource.Pass()); | |
140 | |
141 // return the texture via resource id | |
142 texture_cache.NotifyPendingResourceReturn(texture_info_1->resource_id(), | |
143 texture.Pass()); | |
144 resource_returner->ReturnResources(resources.Pass()); | |
145 | |
146 KickMessageLoop(); | |
147 | |
148 mojo::Size different_size; | |
149 different_size.width = size.width - 1; | |
150 different_size.height = size.height - 1; | |
151 | |
152 // get a texture of the different size - it should not be the same one as | |
153 // before | |
154 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info_2( | |
155 texture_cache.GetTexture(different_size).Pass()); | |
156 scoped_ptr<mojo::GLTexture> texture_2(texture_info_2->TakeTexture().Pass()); | |
157 | |
158 EXPECT_NE(texture_2.get(), nullptr); | |
159 EXPECT_NE(size.width, texture_2->size().width); | |
160 EXPECT_NE(size.height, texture_2->size().height); | |
161 EXPECT_EQ(different_size.width, texture_2->size().width); | |
162 EXPECT_EQ(different_size.height, texture_2->size().height); | |
163 EXPECT_NE(texture_info_1->resource_id(), texture_info_2->resource_id()); | |
164 } | |
165 | |
166 TEST_F(TextureCacheTest, GetTextureReleasedGlContext) { | |
167 gl_context_.reset(); | |
168 mojo::TextureCache texture_cache(gl_context_, nullptr); | |
169 mojo::Size size; | |
170 size.width = 100; | |
171 size.height = 100; | |
172 | |
173 EXPECT_EQ(texture_cache.GetTexture(size).get(), nullptr); | |
174 } | |
175 | |
176 TEST_F(TextureCacheTest, ReturnResourcesReleasedGlContext) { | |
177 mojo::ResourceReturnerPtr resource_returner; | |
178 mojo::TextureCache texture_cache(gl_context_, &resource_returner); | |
179 mojo::Size size; | |
180 size.width = 100; | |
181 size.height = 100; | |
182 | |
183 // get a texture | |
184 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info( | |
185 texture_cache.GetTexture(size).Pass()); | |
186 scoped_ptr<mojo::GLTexture> texture(texture_info->TakeTexture().Pass()); | |
187 mojo::GLTexture* texture_ptr = texture.get(); | |
188 EXPECT_NE(texture_ptr, nullptr); | |
189 | |
190 gl_context_.reset(); | |
191 | |
192 mojo::Array<mojo::ReturnedResourcePtr> resources; | |
193 mojo::ReturnedResourcePtr returnedResource = mojo::ReturnedResource::New(); | |
194 returnedResource->id = texture_info->resource_id(); | |
195 returnedResource->sync_point = 0u; | |
196 returnedResource->count = 1u; | |
197 returnedResource->lost = false; | |
198 resources.push_back(returnedResource.Pass()); | |
199 | |
200 // return the texture via resource id | |
201 texture_cache.NotifyPendingResourceReturn(texture_info->resource_id(), | |
202 texture.Pass()); | |
203 resource_returner->ReturnResources(resources.Pass()); | |
204 | |
205 KickMessageLoop(); | |
206 } | |
207 | |
208 } // namespace | |
OLD | NEW |