Make GL bindings conditional
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager_unittest.cc
blobc7fef77bf3a7ad4b1154fb01490b86f70cad6e6e
1 // Copyright (c) 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.
5 #include "gpu/command_buffer/service/texture_manager.h"
7 #include <utility>
9 #include "base/memory/scoped_ptr.h"
10 #include "gpu/command_buffer/service/error_state_mock.h"
11 #include "gpu/command_buffer/service/feature_info.h"
12 #include "gpu/command_buffer/service/framebuffer_manager.h"
13 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h"
14 #include "gpu/command_buffer/service/gpu_service_test.h"
15 #include "gpu/command_buffer/service/mailbox_manager.h"
16 #include "gpu/command_buffer/service/memory_tracking.h"
17 #include "gpu/command_buffer/service/mocks.h"
18 #include "gpu/command_buffer/service/test_helper.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gl/gl_image_stub.h"
21 #include "ui/gl/gl_mock.h"
23 using ::testing::AtLeast;
24 using ::testing::Pointee;
25 using ::testing::Return;
26 using ::testing::SetArgumentPointee;
27 using ::testing::StrictMock;
28 using ::testing::_;
30 namespace gpu {
31 namespace gles2 {
33 class TextureTestHelper {
34 public:
35 static bool IsNPOT(const Texture* texture) {
36 return texture->npot();
38 static bool IsTextureComplete(const Texture* texture) {
39 return texture->texture_complete();
41 static bool IsCubeComplete(const Texture* texture) {
42 return texture->cube_complete();
46 class TextureManagerTest : public GpuServiceTest {
47 public:
48 static const GLint kMaxTextureSize = 16;
49 static const GLint kMaxCubeMapTextureSize = 8;
50 static const GLint kMaxRectangleTextureSize = 16;
51 static const GLint kMaxExternalTextureSize = 16;
52 static const GLint kMax2dLevels = 5;
53 static const GLint kMaxCubeMapLevels = 4;
54 static const GLint kMaxExternalLevels = 1;
55 static const bool kUseDefaultTextures = false;
57 TextureManagerTest() : feature_info_(new FeatureInfo()) {}
59 ~TextureManagerTest() override {}
61 protected:
62 void SetUp() override {
63 GpuServiceTest::SetUp();
64 manager_.reset(new TextureManager(NULL,
65 feature_info_.get(),
66 kMaxTextureSize,
67 kMaxCubeMapTextureSize,
68 kMaxRectangleTextureSize,
69 kUseDefaultTextures));
70 TestHelper::SetupTextureManagerInitExpectations(
71 gl_.get(), "", kUseDefaultTextures);
72 manager_->Initialize();
73 error_state_.reset(new ::testing::StrictMock<gles2::MockErrorState>());
76 void TearDown() override {
77 manager_->Destroy(false);
78 manager_.reset();
79 GpuServiceTest::TearDown();
82 void SetParameter(
83 TextureRef* texture_ref, GLenum pname, GLint value, GLenum error) {
84 TestHelper::SetTexParameteriWithExpectations(
85 gl_.get(), error_state_.get(), manager_.get(),
86 texture_ref, pname, value, error);
89 scoped_refptr<FeatureInfo> feature_info_;
90 scoped_ptr<TextureManager> manager_;
91 scoped_ptr<MockErrorState> error_state_;
94 // GCC requires these declarations, but MSVC requires they not be present
95 #ifndef COMPILER_MSVC
96 const GLint TextureManagerTest::kMaxTextureSize;
97 const GLint TextureManagerTest::kMaxCubeMapTextureSize;
98 const GLint TextureManagerTest::kMaxRectangleTextureSize;
99 const GLint TextureManagerTest::kMaxExternalTextureSize;
100 const GLint TextureManagerTest::kMax2dLevels;
101 const GLint TextureManagerTest::kMaxCubeMapLevels;
102 const GLint TextureManagerTest::kMaxExternalLevels;
103 #endif
105 TEST_F(TextureManagerTest, Basic) {
106 const GLuint kClient1Id = 1;
107 const GLuint kService1Id = 11;
108 const GLuint kClient2Id = 2;
109 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
110 EXPECT_FALSE(manager_->HaveUnsafeTextures());
111 EXPECT_FALSE(manager_->HaveUnclearedMips());
112 // Check we can create texture.
113 manager_->CreateTexture(kClient1Id, kService1Id);
114 // Check texture got created.
115 scoped_refptr<TextureRef> texture = manager_->GetTexture(kClient1Id);
116 ASSERT_TRUE(texture.get() != NULL);
117 EXPECT_EQ(kService1Id, texture->service_id());
118 EXPECT_EQ(kClient1Id, texture->client_id());
119 EXPECT_EQ(texture->texture(), manager_->GetTextureForServiceId(
120 texture->service_id()));
121 // Check we get nothing for a non-existent texture.
122 EXPECT_TRUE(manager_->GetTexture(kClient2Id) == NULL);
123 // Check trying to a remove non-existent textures does not crash.
124 manager_->RemoveTexture(kClient2Id);
125 // Check that it gets deleted when the last reference is released.
126 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService1Id)))
127 .Times(1)
128 .RetiresOnSaturation();
129 // Check we can't get the texture after we remove it.
130 manager_->RemoveTexture(kClient1Id);
131 EXPECT_TRUE(manager_->GetTexture(kClient1Id) == NULL);
132 EXPECT_EQ(0u, texture->client_id());
135 TEST_F(TextureManagerTest, SetParameter) {
136 const GLuint kClient1Id = 1;
137 const GLuint kService1Id = 11;
138 // Check we can create texture.
139 manager_->CreateTexture(kClient1Id, kService1Id);
140 // Check texture got created.
141 TextureRef* texture_ref = manager_->GetTexture(kClient1Id);
142 ASSERT_TRUE(texture_ref != NULL);
143 Texture* texture = texture_ref->texture();
144 manager_->SetTarget(texture_ref, GL_TEXTURE_2D);
145 SetParameter(texture_ref, GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
146 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
147 SetParameter(texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
148 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->mag_filter());
149 SetParameter(texture_ref, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
150 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_s());
151 SetParameter(texture_ref, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
152 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_t());
153 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1, GL_NO_ERROR);
154 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2, GL_NO_ERROR);
155 SetParameter(
156 texture_ref, GL_TEXTURE_MIN_FILTER, GL_CLAMP_TO_EDGE, GL_INVALID_ENUM);
157 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
158 SetParameter(
159 texture_ref, GL_TEXTURE_MAG_FILTER, GL_CLAMP_TO_EDGE, GL_INVALID_ENUM);
160 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
161 SetParameter(texture_ref, GL_TEXTURE_WRAP_S, GL_NEAREST, GL_INVALID_ENUM);
162 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_s());
163 SetParameter(texture_ref, GL_TEXTURE_WRAP_T, GL_NEAREST, GL_INVALID_ENUM);
164 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_t());
165 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0, GL_INVALID_VALUE);
168 TEST_F(TextureManagerTest, UseDefaultTexturesTrue) {
169 bool use_default_textures = true;
170 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
172 TestHelper::SetupTextureManagerInitExpectations(
173 gl_.get(), "GL_ANGLE_texture_usage", use_default_textures);
174 TextureManager manager(NULL,
175 feature_info_.get(),
176 kMaxTextureSize,
177 kMaxCubeMapTextureSize,
178 kMaxRectangleTextureSize,
179 use_default_textures);
180 manager.Initialize();
182 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D) != NULL);
183 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP) != NULL);
185 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
187 manager.Destroy(false);
190 TEST_F(TextureManagerTest, UseDefaultTexturesFalse) {
191 bool use_default_textures = false;
192 TestHelper::SetupTextureManagerInitExpectations(
193 gl_.get(), "GL_ANGLE_texture_usage", use_default_textures);
194 TextureManager manager(NULL,
195 feature_info_.get(),
196 kMaxTextureSize,
197 kMaxCubeMapTextureSize,
198 kMaxRectangleTextureSize,
199 use_default_textures);
200 manager.Initialize();
202 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D) == NULL);
203 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP) == NULL);
205 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
207 manager.Destroy(false);
210 TEST_F(TextureManagerTest, TextureUsageExt) {
211 TestHelper::SetupTextureManagerInitExpectations(
212 gl_.get(), "GL_ANGLE_texture_usage", kUseDefaultTextures);
213 TextureManager manager(NULL,
214 feature_info_.get(),
215 kMaxTextureSize,
216 kMaxCubeMapTextureSize,
217 kMaxRectangleTextureSize,
218 kUseDefaultTextures);
219 manager.Initialize();
220 const GLuint kClient1Id = 1;
221 const GLuint kService1Id = 11;
222 // Check we can create texture.
223 manager.CreateTexture(kClient1Id, kService1Id);
224 // Check texture got created.
225 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
226 ASSERT_TRUE(texture_ref != NULL);
227 TestHelper::SetTexParameteriWithExpectations(
228 gl_.get(), error_state_.get(), &manager, texture_ref,
229 GL_TEXTURE_USAGE_ANGLE, GL_FRAMEBUFFER_ATTACHMENT_ANGLE, GL_NO_ERROR);
230 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_ATTACHMENT_ANGLE),
231 texture_ref->texture()->usage());
232 manager.Destroy(false);
235 TEST_F(TextureManagerTest, Destroy) {
236 const GLuint kClient1Id = 1;
237 const GLuint kService1Id = 11;
238 TestHelper::SetupTextureManagerInitExpectations(
239 gl_.get(), "", kUseDefaultTextures);
240 TextureManager manager(NULL,
241 feature_info_.get(),
242 kMaxTextureSize,
243 kMaxCubeMapTextureSize,
244 kMaxRectangleTextureSize,
245 kUseDefaultTextures);
246 manager.Initialize();
247 // Check we can create texture.
248 manager.CreateTexture(kClient1Id, kService1Id);
249 // Check texture got created.
250 TextureRef* texture = manager.GetTexture(kClient1Id);
251 ASSERT_TRUE(texture != NULL);
252 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService1Id)))
253 .Times(1)
254 .RetiresOnSaturation();
255 TestHelper::SetupTextureManagerDestructionExpectations(
256 gl_.get(), "", kUseDefaultTextures);
257 manager.Destroy(true);
258 // Check that resources got freed.
259 texture = manager.GetTexture(kClient1Id);
260 ASSERT_TRUE(texture == NULL);
263 TEST_F(TextureManagerTest, MaxValues) {
264 // Check we get the right values for the max sizes.
265 EXPECT_EQ(kMax2dLevels, manager_->MaxLevelsForTarget(GL_TEXTURE_2D));
266 EXPECT_EQ(kMaxCubeMapLevels,
267 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP));
268 EXPECT_EQ(kMaxCubeMapLevels,
269 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X));
270 EXPECT_EQ(kMaxCubeMapLevels,
271 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X));
272 EXPECT_EQ(kMaxCubeMapLevels,
273 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y));
274 EXPECT_EQ(kMaxCubeMapLevels,
275 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y));
276 EXPECT_EQ(kMaxCubeMapLevels,
277 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z));
278 EXPECT_EQ(kMaxCubeMapLevels,
279 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
280 EXPECT_EQ(kMaxExternalLevels,
281 manager_->MaxLevelsForTarget(GL_TEXTURE_EXTERNAL_OES));
282 EXPECT_EQ(kMaxTextureSize, manager_->MaxSizeForTarget(GL_TEXTURE_2D));
283 EXPECT_EQ(kMaxCubeMapTextureSize,
284 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP));
285 EXPECT_EQ(kMaxCubeMapTextureSize,
286 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X));
287 EXPECT_EQ(kMaxCubeMapTextureSize,
288 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X));
289 EXPECT_EQ(kMaxCubeMapTextureSize,
290 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y));
291 EXPECT_EQ(kMaxCubeMapTextureSize,
292 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y));
293 EXPECT_EQ(kMaxCubeMapTextureSize,
294 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z));
295 EXPECT_EQ(kMaxCubeMapTextureSize,
296 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
297 EXPECT_EQ(kMaxRectangleTextureSize,
298 manager_->MaxSizeForTarget(GL_TEXTURE_RECTANGLE_ARB));
299 EXPECT_EQ(kMaxExternalTextureSize,
300 manager_->MaxSizeForTarget(GL_TEXTURE_EXTERNAL_OES));
303 TEST_F(TextureManagerTest, ValidForTarget) {
304 // check 2d
305 EXPECT_TRUE(manager_->ValidForTarget(
306 GL_TEXTURE_2D, 0, kMaxTextureSize, kMaxTextureSize, 1));
307 EXPECT_TRUE(manager_->ValidForTarget(
308 GL_TEXTURE_2D, kMax2dLevels - 1, 1, 1, 1));
309 EXPECT_FALSE(manager_->ValidForTarget(
310 GL_TEXTURE_2D, kMax2dLevels - 1, 1, 2, 1));
311 EXPECT_FALSE(manager_->ValidForTarget(
312 GL_TEXTURE_2D, kMax2dLevels - 1, 2, 1, 1));
313 // check level out of range.
314 EXPECT_FALSE(manager_->ValidForTarget(
315 GL_TEXTURE_2D, kMax2dLevels, kMaxTextureSize, 1, 1));
316 // check has depth.
317 EXPECT_FALSE(manager_->ValidForTarget(
318 GL_TEXTURE_2D, kMax2dLevels, kMaxTextureSize, 1, 2));
319 // Check NPOT width on level 0
320 EXPECT_TRUE(manager_->ValidForTarget(GL_TEXTURE_2D, 0, 5, 2, 1));
321 // Check NPOT height on level 0
322 EXPECT_TRUE(manager_->ValidForTarget(GL_TEXTURE_2D, 0, 2, 5, 1));
323 // Check NPOT width on level 1
324 EXPECT_FALSE(manager_->ValidForTarget(GL_TEXTURE_2D, 1, 5, 2, 1));
325 // Check NPOT height on level 1
326 EXPECT_FALSE(manager_->ValidForTarget(GL_TEXTURE_2D, 1, 2, 5, 1));
328 // check cube
329 EXPECT_TRUE(manager_->ValidForTarget(
330 GL_TEXTURE_CUBE_MAP, 0,
331 kMaxCubeMapTextureSize, kMaxCubeMapTextureSize, 1));
332 EXPECT_TRUE(manager_->ValidForTarget(
333 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels - 1, 1, 1, 1));
334 EXPECT_FALSE(manager_->ValidForTarget(
335 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels - 1, 2, 2, 1));
336 // check level out of range.
337 EXPECT_FALSE(manager_->ValidForTarget(
338 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
339 kMaxCubeMapTextureSize, 1, 1));
340 // check not square.
341 EXPECT_FALSE(manager_->ValidForTarget(
342 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
343 kMaxCubeMapTextureSize, 1, 1));
344 // check has depth.
345 EXPECT_FALSE(manager_->ValidForTarget(
346 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
347 kMaxCubeMapTextureSize, 1, 2));
349 for (GLint level = 0; level < kMax2dLevels; ++level) {
350 EXPECT_TRUE(manager_->ValidForTarget(
351 GL_TEXTURE_2D, level, kMaxTextureSize >> level, 1, 1));
352 EXPECT_TRUE(manager_->ValidForTarget(
353 GL_TEXTURE_2D, level, 1, kMaxTextureSize >> level, 1));
354 EXPECT_FALSE(manager_->ValidForTarget(
355 GL_TEXTURE_2D, level, (kMaxTextureSize >> level) + 1, 1, 1));
356 EXPECT_FALSE(manager_->ValidForTarget(
357 GL_TEXTURE_2D, level, 1, (kMaxTextureSize >> level) + 1, 1));
360 for (GLint level = 0; level < kMaxCubeMapLevels; ++level) {
361 EXPECT_TRUE(manager_->ValidForTarget(
362 GL_TEXTURE_CUBE_MAP, level,
363 kMaxCubeMapTextureSize >> level,
364 kMaxCubeMapTextureSize >> level,
365 1));
366 EXPECT_FALSE(manager_->ValidForTarget(
367 GL_TEXTURE_CUBE_MAP, level,
368 (kMaxCubeMapTextureSize >> level) * 2,
369 (kMaxCubeMapTextureSize >> level) * 2,
370 1));
374 TEST_F(TextureManagerTest, ValidForTargetNPOT) {
375 TestHelper::SetupFeatureInfoInitExpectations(
376 gl_.get(), "GL_OES_texture_npot");
377 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
378 feature_info->Initialize();
379 TextureManager manager(NULL,
380 feature_info.get(),
381 kMaxTextureSize,
382 kMaxCubeMapTextureSize,
383 kMaxRectangleTextureSize,
384 kUseDefaultTextures);
385 // Check NPOT width on level 0
386 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 0, 5, 2, 1));
387 // Check NPOT height on level 0
388 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 0, 2, 5, 1));
389 // Check NPOT width on level 1
390 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 1, 5, 2, 1));
391 // Check NPOT height on level 1
392 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 1, 2, 5, 1));
393 manager.Destroy(false);
396 class TextureTestBase : public GpuServiceTest {
397 public:
398 static const GLint kMaxTextureSize = 16;
399 static const GLint kMaxCubeMapTextureSize = 8;
400 static const GLint kMaxRectangleTextureSize = 16;
401 static const GLint kMax2dLevels = 5;
402 static const GLint kMaxCubeMapLevels = 4;
403 static const GLuint kClient1Id = 1;
404 static const GLuint kService1Id = 11;
405 static const bool kUseDefaultTextures = false;
407 TextureTestBase()
408 : feature_info_(new FeatureInfo()) {
410 ~TextureTestBase() override { texture_ref_ = NULL; }
412 protected:
413 void SetUpBase(MemoryTracker* memory_tracker, std::string extensions) {
414 GpuServiceTest::SetUp();
415 if (!extensions.empty()) {
416 TestHelper::SetupFeatureInfoInitExpectations(gl_.get(),
417 extensions.c_str());
418 feature_info_->Initialize();
421 manager_.reset(new TextureManager(memory_tracker,
422 feature_info_.get(),
423 kMaxTextureSize,
424 kMaxCubeMapTextureSize,
425 kMaxRectangleTextureSize,
426 kUseDefaultTextures));
427 decoder_.reset(new ::testing::StrictMock<gles2::MockGLES2Decoder>());
428 error_state_.reset(new ::testing::StrictMock<gles2::MockErrorState>());
429 manager_->CreateTexture(kClient1Id, kService1Id);
430 texture_ref_ = manager_->GetTexture(kClient1Id);
431 ASSERT_TRUE(texture_ref_.get() != NULL);
434 void TearDown() override {
435 if (texture_ref_.get()) {
436 // If it's not in the manager then setting texture_ref_ to NULL will
437 // delete the texture.
438 if (!texture_ref_->client_id()) {
439 // Check that it gets deleted when the last reference is released.
440 EXPECT_CALL(*gl_,
441 DeleteTextures(1, ::testing::Pointee(texture_ref_->service_id())))
442 .Times(1)
443 .RetiresOnSaturation();
445 texture_ref_ = NULL;
447 manager_->Destroy(false);
448 manager_.reset();
449 GpuServiceTest::TearDown();
452 void SetParameter(
453 TextureRef* texture_ref, GLenum pname, GLint value, GLenum error) {
454 TestHelper::SetTexParameteriWithExpectations(
455 gl_.get(), error_state_.get(), manager_.get(),
456 texture_ref, pname, value, error);
459 scoped_ptr<MockGLES2Decoder> decoder_;
460 scoped_ptr<MockErrorState> error_state_;
461 scoped_refptr<FeatureInfo> feature_info_;
462 scoped_ptr<TextureManager> manager_;
463 scoped_refptr<TextureRef> texture_ref_;
466 class TextureTest : public TextureTestBase {
467 protected:
468 void SetUp() override { SetUpBase(NULL, std::string()); }
471 class TextureMemoryTrackerTest : public TextureTestBase {
472 protected:
473 void SetUp() override {
474 mock_memory_tracker_ = new StrictMock<MockMemoryTracker>();
475 SetUpBase(mock_memory_tracker_.get(), std::string());
478 scoped_refptr<MockMemoryTracker> mock_memory_tracker_;
481 #define EXPECT_MEMORY_ALLOCATION_CHANGE(old_size, new_size, pool) \
482 EXPECT_CALL(*mock_memory_tracker_.get(), \
483 TrackMemoryAllocatedChange(old_size, new_size, pool)) \
484 .Times(1).RetiresOnSaturation()
486 TEST_F(TextureTest, Basic) {
487 Texture* texture = texture_ref_->texture();
488 EXPECT_EQ(0u, texture->target());
489 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
490 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
491 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
492 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
493 EXPECT_EQ(0, texture->num_uncleared_mips());
494 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
495 EXPECT_TRUE(texture->SafeToRenderFrom());
496 EXPECT_FALSE(texture->IsImmutable());
497 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST_MIPMAP_LINEAR),
498 texture->min_filter());
499 EXPECT_EQ(static_cast<GLenum>(GL_LINEAR), texture->mag_filter());
500 EXPECT_EQ(static_cast<GLenum>(GL_REPEAT), texture->wrap_s());
501 EXPECT_EQ(static_cast<GLenum>(GL_REPEAT), texture->wrap_t());
502 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
503 EXPECT_FALSE(manager_->HaveUnsafeTextures());
504 EXPECT_EQ(0u, texture->estimated_size());
507 TEST_F(TextureTest, SetTargetTexture2D) {
508 Texture* texture = texture_ref_->texture();
509 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
510 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
511 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
512 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
513 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
514 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
515 EXPECT_TRUE(texture->SafeToRenderFrom());
516 EXPECT_FALSE(texture->IsImmutable());
519 TEST_F(TextureTest, SetTargetTextureExternalOES) {
520 Texture* texture = texture_ref_->texture();
521 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_EXTERNAL_OES);
522 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
523 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
524 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
525 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
526 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
527 EXPECT_TRUE(texture->SafeToRenderFrom());
528 EXPECT_TRUE(texture->IsImmutable());
531 TEST_F(TextureTest, ZeroSizeCanNotRender) {
532 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
533 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
534 manager_->SetLevelInfo(texture_ref_.get(),
535 GL_TEXTURE_2D,
537 GL_RGBA,
542 GL_RGBA,
543 GL_UNSIGNED_BYTE,
544 true);
545 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
546 manager_->SetLevelInfo(texture_ref_.get(),
547 GL_TEXTURE_2D,
549 GL_RGBA,
554 GL_RGBA,
555 GL_UNSIGNED_BYTE,
556 true);
557 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
560 TEST_F(TextureTest, EstimatedSize) {
561 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
562 manager_->SetLevelInfo(texture_ref_.get(),
563 GL_TEXTURE_2D,
565 GL_RGBA,
570 GL_RGBA,
571 GL_UNSIGNED_BYTE,
572 true);
573 EXPECT_EQ(8u * 4u * 4u, texture_ref_->texture()->estimated_size());
574 manager_->SetLevelInfo(texture_ref_.get(),
575 GL_TEXTURE_2D,
577 GL_RGBA,
582 GL_RGBA,
583 GL_UNSIGNED_BYTE,
584 true);
585 EXPECT_EQ(8u * 4u * 4u * 2u, texture_ref_->texture()->estimated_size());
588 TEST_F(TextureMemoryTrackerTest, EstimatedSize) {
589 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
590 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged);
591 manager_->SetLevelInfo(texture_ref_.get(),
592 GL_TEXTURE_2D,
594 GL_RGBA,
599 GL_RGBA,
600 GL_UNSIGNED_BYTE,
601 true);
602 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged);
603 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 256, MemoryTracker::kUnmanaged);
604 manager_->SetLevelInfo(texture_ref_.get(),
605 GL_TEXTURE_2D,
607 GL_RGBA,
612 GL_RGBA,
613 GL_UNSIGNED_BYTE,
614 true);
615 // Add expectation for texture deletion.
616 EXPECT_MEMORY_ALLOCATION_CHANGE(256, 0, MemoryTracker::kUnmanaged);
617 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
620 TEST_F(TextureMemoryTrackerTest, SetParameterPool) {
621 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
622 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged);
623 manager_->SetLevelInfo(texture_ref_.get(),
624 GL_TEXTURE_2D,
626 GL_RGBA,
631 GL_RGBA,
632 GL_UNSIGNED_BYTE,
633 true);
634 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged);
635 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kManaged);
636 SetParameter(texture_ref_.get(),
637 GL_TEXTURE_POOL_CHROMIUM,
638 GL_TEXTURE_POOL_MANAGED_CHROMIUM,
639 GL_NO_ERROR);
640 // Add expectation for texture deletion.
641 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kManaged);
642 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
643 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kManaged);
646 TEST_F(TextureTest, POT2D) {
647 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
648 Texture* texture = texture_ref_->texture();
649 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
650 // Check Setting level 0 to POT
651 manager_->SetLevelInfo(texture_ref_.get(),
652 GL_TEXTURE_2D,
654 GL_RGBA,
659 GL_RGBA,
660 GL_UNSIGNED_BYTE,
661 true);
662 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
663 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
664 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
665 EXPECT_EQ(0, texture->num_uncleared_mips());
666 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
667 // Set filters to something that will work with a single mip.
668 SetParameter(
669 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NO_ERROR);
670 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
671 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
672 // Set them back.
673 SetParameter(texture_ref_.get(),
674 GL_TEXTURE_MIN_FILTER,
675 GL_LINEAR_MIPMAP_LINEAR,
676 GL_NO_ERROR);
677 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
679 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
680 // Make mips.
681 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
682 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
683 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
684 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
685 // Change a mip.
686 manager_->SetLevelInfo(texture_ref_.get(),
687 GL_TEXTURE_2D,
689 GL_RGBA,
694 GL_RGBA,
695 GL_UNSIGNED_BYTE,
696 true);
697 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
698 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
699 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
700 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
701 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
702 // Set a level past the number of mips that would get generated.
703 manager_->SetLevelInfo(texture_ref_.get(),
704 GL_TEXTURE_2D,
706 GL_RGBA,
711 GL_RGBA,
712 GL_UNSIGNED_BYTE,
713 true);
714 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
715 // Make mips.
716 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
717 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
718 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
719 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
722 TEST_F(TextureMemoryTrackerTest, MarkMipmapsGenerated) {
723 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
724 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 64, MemoryTracker::kUnmanaged);
725 manager_->SetLevelInfo(texture_ref_.get(),
726 GL_TEXTURE_2D,
728 GL_RGBA,
733 GL_RGBA,
734 GL_UNSIGNED_BYTE,
735 true);
736 EXPECT_MEMORY_ALLOCATION_CHANGE(64, 0, MemoryTracker::kUnmanaged);
737 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 84, MemoryTracker::kUnmanaged);
738 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
739 EXPECT_MEMORY_ALLOCATION_CHANGE(84, 0, MemoryTracker::kUnmanaged);
740 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
743 TEST_F(TextureTest, UnusedMips) {
744 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
745 Texture* texture = texture_ref_->texture();
746 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
747 // Set level zero to large size.
748 manager_->SetLevelInfo(texture_ref_.get(),
749 GL_TEXTURE_2D,
751 GL_RGBA,
756 GL_RGBA,
757 GL_UNSIGNED_BYTE,
758 true);
759 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
760 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
761 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
762 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
763 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
764 // Set level zero to large smaller (levels unused mips)
765 manager_->SetLevelInfo(texture_ref_.get(),
766 GL_TEXTURE_2D,
768 GL_RGBA,
773 GL_RGBA,
774 GL_UNSIGNED_BYTE,
775 true);
776 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
777 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
778 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
779 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
780 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
781 // Set an unused level to some size
782 manager_->SetLevelInfo(texture_ref_.get(),
783 GL_TEXTURE_2D,
785 GL_RGBA,
790 GL_RGBA,
791 GL_UNSIGNED_BYTE,
792 true);
793 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
794 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
795 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
796 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
799 TEST_F(TextureTest, NPOT2D) {
800 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
801 Texture* texture = texture_ref_->texture();
802 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
803 // Check Setting level 0 to NPOT
804 manager_->SetLevelInfo(texture_ref_.get(),
805 GL_TEXTURE_2D,
807 GL_RGBA,
812 GL_RGBA,
813 GL_UNSIGNED_BYTE,
814 true);
815 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
816 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
817 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
818 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
819 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
820 SetParameter(
821 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NO_ERROR);
822 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
823 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
824 SetParameter(
825 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
826 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
827 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
828 SetParameter(
829 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
830 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
831 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
832 // Change it to POT.
833 manager_->SetLevelInfo(texture_ref_.get(),
834 GL_TEXTURE_2D,
836 GL_RGBA,
841 GL_RGBA,
842 GL_UNSIGNED_BYTE,
843 true);
844 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
845 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
846 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
847 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
850 TEST_F(TextureTest, NPOT2DNPOTOK) {
851 TestHelper::SetupFeatureInfoInitExpectations(
852 gl_.get(), "GL_OES_texture_npot");
853 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
854 feature_info->Initialize();
855 TextureManager manager(NULL,
856 feature_info.get(),
857 kMaxTextureSize,
858 kMaxCubeMapTextureSize,
859 kMaxRectangleTextureSize,
860 kUseDefaultTextures);
861 manager.CreateTexture(kClient1Id, kService1Id);
862 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
863 ASSERT_TRUE(texture_ref != NULL);
864 Texture* texture = texture_ref->texture();
866 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
867 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
868 // Check Setting level 0 to NPOT
869 manager.SetLevelInfo(texture_ref,
870 GL_TEXTURE_2D, 0, GL_RGBA, 4, 5, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, true);
871 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
872 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
873 EXPECT_TRUE(manager.CanGenerateMipmaps(texture_ref));
874 EXPECT_FALSE(manager.CanRender(texture_ref));
875 EXPECT_TRUE(manager.HaveUnrenderableTextures());
876 EXPECT_TRUE(manager.MarkMipmapsGenerated(texture_ref));
877 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
878 EXPECT_TRUE(manager.CanRender(texture_ref));
879 EXPECT_FALSE(manager.HaveUnrenderableTextures());
880 manager.Destroy(false);
883 TEST_F(TextureTest, POTCubeMap) {
884 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
885 Texture* texture = texture_ref_->texture();
886 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
887 // Check Setting level 0 each face to POT
888 manager_->SetLevelInfo(texture_ref_.get(),
889 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
891 GL_RGBA,
896 GL_RGBA,
897 GL_UNSIGNED_BYTE,
898 true);
899 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
900 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
901 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
902 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
903 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
904 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
905 manager_->SetLevelInfo(texture_ref_.get(),
906 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
908 GL_RGBA,
913 GL_RGBA,
914 GL_UNSIGNED_BYTE,
915 true);
916 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
917 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
918 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
919 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
920 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
921 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
922 manager_->SetLevelInfo(texture_ref_.get(),
923 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
925 GL_RGBA,
930 GL_RGBA,
931 GL_UNSIGNED_BYTE,
932 true);
933 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
934 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
935 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
936 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
937 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
938 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
939 manager_->SetLevelInfo(texture_ref_.get(),
940 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
942 GL_RGBA,
947 GL_RGBA,
948 GL_UNSIGNED_BYTE,
949 true);
950 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
951 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
952 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
953 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
954 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
955 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
956 manager_->SetLevelInfo(texture_ref_.get(),
957 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
959 GL_RGBA,
964 GL_RGBA,
965 GL_UNSIGNED_BYTE,
966 true);
967 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
968 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
969 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
970 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
971 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
972 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
973 manager_->SetLevelInfo(texture_ref_.get(),
974 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
976 GL_RGBA,
981 GL_RGBA,
982 GL_UNSIGNED_BYTE,
983 true);
984 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
985 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
986 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
987 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
988 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
989 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
991 // Make mips.
992 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
993 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
994 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
995 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
996 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
998 // Change a mip.
999 manager_->SetLevelInfo(texture_ref_.get(),
1000 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
1002 GL_RGBA,
1007 GL_RGBA,
1008 GL_UNSIGNED_BYTE,
1009 true);
1010 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
1011 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1012 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
1013 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
1014 // Set a level past the number of mips that would get generated.
1015 manager_->SetLevelInfo(texture_ref_.get(),
1016 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
1018 GL_RGBA,
1023 GL_RGBA,
1024 GL_UNSIGNED_BYTE,
1025 true);
1026 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
1027 // Make mips.
1028 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
1029 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1030 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
1033 TEST_F(TextureTest, GetLevelSize) {
1034 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1035 manager_->SetLevelInfo(texture_ref_.get(),
1036 GL_TEXTURE_2D,
1038 GL_RGBA,
1043 GL_RGBA,
1044 GL_UNSIGNED_BYTE,
1045 true);
1046 GLsizei width = -1;
1047 GLsizei height = -1;
1048 Texture* texture = texture_ref_->texture();
1049 EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, -1, &width, &height));
1050 EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 1000, &width, &height));
1051 EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
1052 EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 1, &width, &height));
1053 EXPECT_EQ(4, width);
1054 EXPECT_EQ(5, height);
1055 manager_->RemoveTexture(kClient1Id);
1056 EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 1, &width, &height));
1057 EXPECT_EQ(4, width);
1058 EXPECT_EQ(5, height);
1061 TEST_F(TextureTest, GetLevelType) {
1062 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1063 manager_->SetLevelInfo(texture_ref_.get(),
1064 GL_TEXTURE_2D,
1066 GL_RGBA,
1071 GL_RGBA,
1072 GL_UNSIGNED_BYTE,
1073 true);
1074 GLenum type = 0;
1075 GLenum format = 0;
1076 Texture* texture = texture_ref_->texture();
1077 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, -1, &type, &format));
1078 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, 1000, &type, &format));
1079 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &format));
1080 EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &format));
1081 EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
1082 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format);
1083 manager_->RemoveTexture(kClient1Id);
1084 EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &format));
1085 EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
1086 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format);
1089 TEST_F(TextureTest, ValidForTexture) {
1090 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1091 manager_->SetLevelInfo(texture_ref_.get(),
1092 GL_TEXTURE_2D,
1094 GL_RGBA,
1099 GL_RGBA,
1100 GL_UNSIGNED_BYTE,
1101 true);
1102 // Check bad face.
1103 Texture* texture = texture_ref_->texture();
1104 EXPECT_FALSE(texture->ValidForTexture(
1105 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
1106 1, 0, 0, 4, 5, GL_UNSIGNED_BYTE));
1107 // Check bad level.
1108 EXPECT_FALSE(texture->ValidForTexture(
1109 GL_TEXTURE_2D, 0, 0, 0, 4, 5, GL_UNSIGNED_BYTE));
1110 // Check bad xoffset.
1111 EXPECT_FALSE(texture->ValidForTexture(
1112 GL_TEXTURE_2D, 1, -1, 0, 4, 5, GL_UNSIGNED_BYTE));
1113 // Check bad xoffset + width > width.
1114 EXPECT_FALSE(texture->ValidForTexture(
1115 GL_TEXTURE_2D, 1, 1, 0, 4, 5, GL_UNSIGNED_BYTE));
1116 // Check bad yoffset.
1117 EXPECT_FALSE(texture->ValidForTexture(
1118 GL_TEXTURE_2D, 1, 0, -1, 4, 5, GL_UNSIGNED_BYTE));
1119 // Check bad yoffset + height > height.
1120 EXPECT_FALSE(texture->ValidForTexture(
1121 GL_TEXTURE_2D, 1, 0, 1, 4, 5, GL_UNSIGNED_BYTE));
1122 // Check bad width.
1123 EXPECT_FALSE(texture->ValidForTexture(
1124 GL_TEXTURE_2D, 1, 0, 0, 5, 5, GL_UNSIGNED_BYTE));
1125 // Check bad height.
1126 EXPECT_FALSE(texture->ValidForTexture(
1127 GL_TEXTURE_2D, 1, 0, 0, 4, 6, GL_UNSIGNED_BYTE));
1128 // Check bad type.
1129 EXPECT_FALSE(texture->ValidForTexture(
1130 GL_TEXTURE_2D, 1, 0, 0, 4, 5, GL_UNSIGNED_SHORT_4_4_4_4));
1131 // Check valid full size
1132 EXPECT_TRUE(texture->ValidForTexture(
1133 GL_TEXTURE_2D, 1, 0, 0, 4, 5, GL_UNSIGNED_BYTE));
1134 // Check valid particial size.
1135 EXPECT_TRUE(texture->ValidForTexture(
1136 GL_TEXTURE_2D, 1, 1, 1, 2, 3, GL_UNSIGNED_BYTE));
1137 manager_->RemoveTexture(kClient1Id);
1138 EXPECT_TRUE(texture->ValidForTexture(
1139 GL_TEXTURE_2D, 1, 0, 0, 4, 5, GL_UNSIGNED_BYTE));
1142 TEST_F(TextureTest, FloatNotLinear) {
1143 TestHelper::SetupFeatureInfoInitExpectations(
1144 gl_.get(), "GL_OES_texture_float");
1145 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1146 feature_info->Initialize();
1147 TextureManager manager(NULL,
1148 feature_info.get(),
1149 kMaxTextureSize,
1150 kMaxCubeMapTextureSize,
1151 kMaxRectangleTextureSize,
1152 kUseDefaultTextures);
1153 manager.CreateTexture(kClient1Id, kService1Id);
1154 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1155 ASSERT_TRUE(texture_ref != NULL);
1156 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1157 Texture* texture = texture_ref->texture();
1158 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1159 manager.SetLevelInfo(texture_ref,
1160 GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0, GL_RGBA, GL_FLOAT, true);
1161 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1162 TestHelper::SetTexParameteriWithExpectations(
1163 gl_.get(), error_state_.get(), &manager,
1164 texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1165 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1166 TestHelper::SetTexParameteriWithExpectations(
1167 gl_.get(), error_state_.get(), &manager, texture_ref,
1168 GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST, GL_NO_ERROR);
1169 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1170 manager.Destroy(false);
1173 TEST_F(TextureTest, FloatLinear) {
1174 TestHelper::SetupFeatureInfoInitExpectations(
1175 gl_.get(), "GL_OES_texture_float GL_OES_texture_float_linear");
1176 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1177 feature_info->Initialize();
1178 TextureManager manager(NULL,
1179 feature_info.get(),
1180 kMaxTextureSize,
1181 kMaxCubeMapTextureSize,
1182 kMaxRectangleTextureSize,
1183 kUseDefaultTextures);
1184 manager.CreateTexture(kClient1Id, kService1Id);
1185 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1186 ASSERT_TRUE(texture_ref != NULL);
1187 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1188 Texture* texture = texture_ref->texture();
1189 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1190 manager.SetLevelInfo(texture_ref,
1191 GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0, GL_RGBA, GL_FLOAT, true);
1192 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1193 manager.Destroy(false);
1196 TEST_F(TextureTest, HalfFloatNotLinear) {
1197 TestHelper::SetupFeatureInfoInitExpectations(
1198 gl_.get(), "GL_OES_texture_half_float");
1199 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1200 feature_info->Initialize();
1201 TextureManager manager(NULL,
1202 feature_info.get(),
1203 kMaxTextureSize,
1204 kMaxCubeMapTextureSize,
1205 kMaxRectangleTextureSize,
1206 kUseDefaultTextures);
1207 manager.CreateTexture(kClient1Id, kService1Id);
1208 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1209 ASSERT_TRUE(texture_ref != NULL);
1210 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1211 Texture* texture = texture_ref->texture();
1212 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1213 manager.SetLevelInfo(texture_ref,
1214 GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0, GL_RGBA, GL_HALF_FLOAT_OES, true);
1215 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1216 TestHelper::SetTexParameteriWithExpectations(
1217 gl_.get(), error_state_.get(), &manager,
1218 texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1219 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1220 TestHelper::SetTexParameteriWithExpectations(
1221 gl_.get(), error_state_.get(), &manager, texture_ref,
1222 GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST, GL_NO_ERROR);
1223 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1224 manager.Destroy(false);
1227 TEST_F(TextureTest, HalfFloatLinear) {
1228 TestHelper::SetupFeatureInfoInitExpectations(
1229 gl_.get(), "GL_OES_texture_half_float GL_OES_texture_half_float_linear");
1230 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1231 feature_info->Initialize();
1232 TextureManager manager(NULL,
1233 feature_info.get(),
1234 kMaxTextureSize,
1235 kMaxCubeMapTextureSize,
1236 kMaxRectangleTextureSize,
1237 kUseDefaultTextures);
1238 manager.CreateTexture(kClient1Id, kService1Id);
1239 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1240 ASSERT_TRUE(texture_ref != NULL);
1241 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1242 Texture* texture = texture_ref->texture();
1243 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1244 manager.SetLevelInfo(texture_ref,
1245 GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0, GL_RGBA, GL_HALF_FLOAT_OES, true);
1246 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1247 manager.Destroy(false);
1250 TEST_F(TextureTest, EGLImageExternal) {
1251 TestHelper::SetupFeatureInfoInitExpectations(
1252 gl_.get(), "GL_OES_EGL_image_external");
1253 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1254 feature_info->Initialize();
1255 TextureManager manager(NULL,
1256 feature_info.get(),
1257 kMaxTextureSize,
1258 kMaxCubeMapTextureSize,
1259 kMaxRectangleTextureSize,
1260 kUseDefaultTextures);
1261 manager.CreateTexture(kClient1Id, kService1Id);
1262 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1263 ASSERT_TRUE(texture_ref != NULL);
1264 manager.SetTarget(texture_ref, GL_TEXTURE_EXTERNAL_OES);
1265 Texture* texture = texture_ref->texture();
1266 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), texture->target());
1267 EXPECT_FALSE(manager.CanGenerateMipmaps(texture_ref));
1268 manager.Destroy(false);
1271 TEST_F(TextureTest, DepthTexture) {
1272 TestHelper::SetupFeatureInfoInitExpectations(
1273 gl_.get(), "GL_ANGLE_depth_texture");
1274 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1275 feature_info->Initialize();
1276 TextureManager manager(NULL,
1277 feature_info.get(),
1278 kMaxTextureSize,
1279 kMaxCubeMapTextureSize,
1280 kMaxRectangleTextureSize,
1281 kUseDefaultTextures);
1282 manager.CreateTexture(kClient1Id, kService1Id);
1283 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1284 ASSERT_TRUE(texture_ref != NULL);
1285 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1286 manager.SetLevelInfo(
1287 texture_ref, GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 4, 4, 1, 0,
1288 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false);
1289 EXPECT_FALSE(manager.CanGenerateMipmaps(texture_ref));
1290 manager.Destroy(false);
1293 TEST_F(TextureTest, SafeUnsafe) {
1294 static const GLuint kClient2Id = 2;
1295 static const GLuint kService2Id = 12;
1296 static const GLuint kClient3Id = 3;
1297 static const GLuint kService3Id = 13;
1298 EXPECT_FALSE(manager_->HaveUnclearedMips());
1299 Texture* texture = texture_ref_->texture();
1300 EXPECT_EQ(0, texture->num_uncleared_mips());
1301 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1302 manager_->SetLevelInfo(texture_ref_.get(),
1303 GL_TEXTURE_2D,
1305 GL_RGBA,
1310 GL_RGBA,
1311 GL_UNSIGNED_BYTE,
1312 false);
1313 EXPECT_FALSE(texture->SafeToRenderFrom());
1314 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1315 EXPECT_TRUE(manager_->HaveUnclearedMips());
1316 EXPECT_EQ(1, texture->num_uncleared_mips());
1317 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 0, true);
1318 EXPECT_TRUE(texture->SafeToRenderFrom());
1319 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1320 EXPECT_FALSE(manager_->HaveUnclearedMips());
1321 EXPECT_EQ(0, texture->num_uncleared_mips());
1322 manager_->SetLevelInfo(texture_ref_.get(),
1323 GL_TEXTURE_2D,
1325 GL_RGBA,
1330 GL_RGBA,
1331 GL_UNSIGNED_BYTE,
1332 false);
1333 EXPECT_FALSE(texture->SafeToRenderFrom());
1334 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1335 EXPECT_TRUE(manager_->HaveUnclearedMips());
1336 EXPECT_EQ(1, texture->num_uncleared_mips());
1337 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 1, true);
1338 EXPECT_TRUE(texture->SafeToRenderFrom());
1339 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1340 EXPECT_FALSE(manager_->HaveUnclearedMips());
1341 EXPECT_EQ(0, texture->num_uncleared_mips());
1342 manager_->SetLevelInfo(texture_ref_.get(),
1343 GL_TEXTURE_2D,
1345 GL_RGBA,
1350 GL_RGBA,
1351 GL_UNSIGNED_BYTE,
1352 false);
1353 manager_->SetLevelInfo(texture_ref_.get(),
1354 GL_TEXTURE_2D,
1356 GL_RGBA,
1361 GL_RGBA,
1362 GL_UNSIGNED_BYTE,
1363 false);
1364 EXPECT_FALSE(texture->SafeToRenderFrom());
1365 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1366 EXPECT_TRUE(manager_->HaveUnclearedMips());
1367 EXPECT_EQ(2, texture->num_uncleared_mips());
1368 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 0, true);
1369 EXPECT_FALSE(texture->SafeToRenderFrom());
1370 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1371 EXPECT_TRUE(manager_->HaveUnclearedMips());
1372 EXPECT_EQ(1, texture->num_uncleared_mips());
1373 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 1, true);
1374 EXPECT_TRUE(texture->SafeToRenderFrom());
1375 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1376 EXPECT_FALSE(manager_->HaveUnclearedMips());
1377 EXPECT_EQ(0, texture->num_uncleared_mips());
1378 manager_->SetLevelInfo(texture_ref_.get(),
1379 GL_TEXTURE_2D,
1381 GL_RGBA,
1386 GL_RGBA,
1387 GL_UNSIGNED_BYTE,
1388 false);
1389 EXPECT_FALSE(texture->SafeToRenderFrom());
1390 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1391 EXPECT_TRUE(manager_->HaveUnclearedMips());
1392 EXPECT_EQ(1, texture->num_uncleared_mips());
1393 manager_->MarkMipmapsGenerated(texture_ref_.get());
1394 EXPECT_TRUE(texture->SafeToRenderFrom());
1395 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1396 EXPECT_FALSE(manager_->HaveUnclearedMips());
1397 EXPECT_EQ(0, texture->num_uncleared_mips());
1399 manager_->CreateTexture(kClient2Id, kService2Id);
1400 scoped_refptr<TextureRef> texture_ref2(
1401 manager_->GetTexture(kClient2Id));
1402 ASSERT_TRUE(texture_ref2.get() != NULL);
1403 manager_->SetTarget(texture_ref2.get(), GL_TEXTURE_2D);
1404 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1405 EXPECT_FALSE(manager_->HaveUnclearedMips());
1406 Texture* texture2 = texture_ref2->texture();
1407 EXPECT_EQ(0, texture2->num_uncleared_mips());
1408 manager_->SetLevelInfo(texture_ref2.get(),
1409 GL_TEXTURE_2D,
1411 GL_RGBA,
1416 GL_RGBA,
1417 GL_UNSIGNED_BYTE,
1418 true);
1419 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1420 EXPECT_FALSE(manager_->HaveUnclearedMips());
1421 EXPECT_EQ(0, texture2->num_uncleared_mips());
1422 manager_->SetLevelInfo(texture_ref2.get(),
1423 GL_TEXTURE_2D,
1425 GL_RGBA,
1430 GL_RGBA,
1431 GL_UNSIGNED_BYTE,
1432 false);
1433 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1434 EXPECT_TRUE(manager_->HaveUnclearedMips());
1435 EXPECT_EQ(1, texture2->num_uncleared_mips());
1437 manager_->CreateTexture(kClient3Id, kService3Id);
1438 scoped_refptr<TextureRef> texture_ref3(
1439 manager_->GetTexture(kClient3Id));
1440 ASSERT_TRUE(texture_ref3.get() != NULL);
1441 manager_->SetTarget(texture_ref3.get(), GL_TEXTURE_2D);
1442 manager_->SetLevelInfo(texture_ref3.get(),
1443 GL_TEXTURE_2D,
1445 GL_RGBA,
1450 GL_RGBA,
1451 GL_UNSIGNED_BYTE,
1452 false);
1453 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1454 EXPECT_TRUE(manager_->HaveUnclearedMips());
1455 Texture* texture3 = texture_ref3->texture();
1456 EXPECT_EQ(1, texture3->num_uncleared_mips());
1457 manager_->SetLevelCleared(texture_ref2.get(), GL_TEXTURE_2D, 0, true);
1458 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1459 EXPECT_TRUE(manager_->HaveUnclearedMips());
1460 EXPECT_EQ(0, texture2->num_uncleared_mips());
1461 manager_->SetLevelCleared(texture_ref3.get(), GL_TEXTURE_2D, 0, true);
1462 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1463 EXPECT_FALSE(manager_->HaveUnclearedMips());
1464 EXPECT_EQ(0, texture3->num_uncleared_mips());
1466 manager_->SetLevelInfo(texture_ref2.get(),
1467 GL_TEXTURE_2D,
1469 GL_RGBA,
1474 GL_RGBA,
1475 GL_UNSIGNED_BYTE,
1476 false);
1477 manager_->SetLevelInfo(texture_ref3.get(),
1478 GL_TEXTURE_2D,
1480 GL_RGBA,
1485 GL_RGBA,
1486 GL_UNSIGNED_BYTE,
1487 false);
1488 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1489 EXPECT_TRUE(manager_->HaveUnclearedMips());
1490 EXPECT_EQ(1, texture2->num_uncleared_mips());
1491 EXPECT_EQ(1, texture3->num_uncleared_mips());
1492 manager_->RemoveTexture(kClient3Id);
1493 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1494 EXPECT_TRUE(manager_->HaveUnclearedMips());
1495 manager_->RemoveTexture(kClient2Id);
1496 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1497 EXPECT_TRUE(manager_->HaveUnclearedMips());
1498 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService2Id)))
1499 .Times(1)
1500 .RetiresOnSaturation();
1501 texture_ref2 = NULL;
1502 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1503 EXPECT_TRUE(manager_->HaveUnclearedMips());
1504 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService3Id)))
1505 .Times(1)
1506 .RetiresOnSaturation();
1507 texture_ref3 = NULL;
1508 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1509 EXPECT_FALSE(manager_->HaveUnclearedMips());
1512 TEST_F(TextureTest, ClearTexture) {
1513 EXPECT_CALL(*decoder_, ClearLevel(_, _, _, _, _, _, _, _, _, _))
1514 .WillRepeatedly(Return(true));
1515 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1516 manager_->SetLevelInfo(texture_ref_.get(),
1517 GL_TEXTURE_2D,
1519 GL_RGBA,
1524 GL_RGBA,
1525 GL_UNSIGNED_BYTE,
1526 false);
1527 manager_->SetLevelInfo(texture_ref_.get(),
1528 GL_TEXTURE_2D,
1530 GL_RGBA,
1535 GL_RGBA,
1536 GL_UNSIGNED_BYTE,
1537 false);
1538 Texture* texture = texture_ref_->texture();
1539 EXPECT_FALSE(texture->SafeToRenderFrom());
1540 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1541 EXPECT_TRUE(manager_->HaveUnclearedMips());
1542 EXPECT_EQ(2, texture->num_uncleared_mips());
1543 manager_->ClearRenderableLevels(decoder_.get(), texture_ref_.get());
1544 EXPECT_TRUE(texture->SafeToRenderFrom());
1545 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1546 EXPECT_FALSE(manager_->HaveUnclearedMips());
1547 EXPECT_EQ(0, texture->num_uncleared_mips());
1548 manager_->SetLevelInfo(texture_ref_.get(),
1549 GL_TEXTURE_2D,
1551 GL_RGBA,
1556 GL_RGBA,
1557 GL_UNSIGNED_BYTE,
1558 false);
1559 manager_->SetLevelInfo(texture_ref_.get(),
1560 GL_TEXTURE_2D,
1562 GL_RGBA,
1567 GL_RGBA,
1568 GL_UNSIGNED_BYTE,
1569 false);
1570 EXPECT_FALSE(texture->SafeToRenderFrom());
1571 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1572 EXPECT_TRUE(manager_->HaveUnclearedMips());
1573 EXPECT_EQ(2, texture->num_uncleared_mips());
1574 manager_->ClearTextureLevel(
1575 decoder_.get(), texture_ref_.get(), GL_TEXTURE_2D, 0);
1576 EXPECT_FALSE(texture->SafeToRenderFrom());
1577 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1578 EXPECT_TRUE(manager_->HaveUnclearedMips());
1579 EXPECT_EQ(1, texture->num_uncleared_mips());
1580 manager_->ClearTextureLevel(
1581 decoder_.get(), texture_ref_.get(), GL_TEXTURE_2D, 1);
1582 EXPECT_TRUE(texture->SafeToRenderFrom());
1583 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1584 EXPECT_FALSE(manager_->HaveUnclearedMips());
1585 EXPECT_EQ(0, texture->num_uncleared_mips());
1588 TEST_F(TextureTest, UseDeletedTexture) {
1589 static const GLuint kClient2Id = 2;
1590 static const GLuint kService2Id = 12;
1591 // Make the default texture renderable
1592 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1593 manager_->SetLevelInfo(texture_ref_.get(),
1594 GL_TEXTURE_2D,
1596 GL_RGBA,
1601 GL_RGBA,
1602 GL_UNSIGNED_BYTE,
1603 false);
1604 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
1605 // Make a new texture
1606 manager_->CreateTexture(kClient2Id, kService2Id);
1607 scoped_refptr<TextureRef> texture_ref(
1608 manager_->GetTexture(kClient2Id));
1609 manager_->SetTarget(texture_ref.get(), GL_TEXTURE_2D);
1610 EXPECT_FALSE(manager_->CanRender(texture_ref.get()));
1611 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
1612 // Remove it.
1613 manager_->RemoveTexture(kClient2Id);
1614 EXPECT_FALSE(manager_->CanRender(texture_ref.get()));
1615 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
1616 // Check that we can still manipulate it and it effects the manager.
1617 manager_->SetLevelInfo(texture_ref.get(),
1618 GL_TEXTURE_2D,
1620 GL_RGBA,
1625 GL_RGBA,
1626 GL_UNSIGNED_BYTE,
1627 false);
1628 EXPECT_TRUE(manager_->CanRender(texture_ref.get()));
1629 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
1630 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService2Id)))
1631 .Times(1)
1632 .RetiresOnSaturation();
1633 texture_ref = NULL;
1636 TEST_F(TextureTest, GetLevelImage) {
1637 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1638 manager_->SetLevelInfo(texture_ref_.get(),
1639 GL_TEXTURE_2D,
1641 GL_RGBA,
1646 GL_RGBA,
1647 GL_UNSIGNED_BYTE,
1648 true);
1649 Texture* texture = texture_ref_->texture();
1650 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1651 // Set image.
1652 scoped_refptr<gfx::GLImage> image(new gfx::GLImageStub);
1653 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, image.get());
1654 EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1655 // Remove it.
1656 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, NULL);
1657 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1658 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, image.get());
1659 // Image should be reset when SetLevelInfo is called.
1660 manager_->SetLevelInfo(texture_ref_.get(),
1661 GL_TEXTURE_2D,
1663 GL_RGBA,
1668 GL_RGBA,
1669 GL_UNSIGNED_BYTE,
1670 true);
1671 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1674 namespace {
1676 bool InSet(std::set<std::string>* string_set, const std::string& str) {
1677 std::pair<std::set<std::string>::iterator, bool> result =
1678 string_set->insert(str);
1679 return !result.second;
1682 } // anonymous namespace
1684 TEST_F(TextureTest, AddToSignature) {
1685 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1686 manager_->SetLevelInfo(texture_ref_.get(),
1687 GL_TEXTURE_2D,
1689 GL_RGBA,
1694 GL_RGBA,
1695 GL_UNSIGNED_BYTE,
1696 true);
1697 std::string signature1;
1698 std::string signature2;
1699 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature1);
1701 std::set<std::string> string_set;
1702 EXPECT_FALSE(InSet(&string_set, signature1));
1704 // check changing 1 thing makes a different signature.
1705 manager_->SetLevelInfo(texture_ref_.get(),
1706 GL_TEXTURE_2D,
1708 GL_RGBA,
1713 GL_RGBA,
1714 GL_UNSIGNED_BYTE,
1715 true);
1716 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1717 EXPECT_FALSE(InSet(&string_set, signature2));
1719 // check putting it back makes the same signature.
1720 manager_->SetLevelInfo(texture_ref_.get(),
1721 GL_TEXTURE_2D,
1723 GL_RGBA,
1728 GL_RGBA,
1729 GL_UNSIGNED_BYTE,
1730 true);
1731 signature2.clear();
1732 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1733 EXPECT_EQ(signature1, signature2);
1735 // Check setting cleared status does not change signature.
1736 manager_->SetLevelInfo(texture_ref_.get(),
1737 GL_TEXTURE_2D,
1739 GL_RGBA,
1744 GL_RGBA,
1745 GL_UNSIGNED_BYTE,
1746 false);
1747 signature2.clear();
1748 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1749 EXPECT_EQ(signature1, signature2);
1751 // Check changing other settings changes signature.
1752 manager_->SetLevelInfo(texture_ref_.get(),
1753 GL_TEXTURE_2D,
1755 GL_RGBA,
1760 GL_RGBA,
1761 GL_UNSIGNED_BYTE,
1762 false);
1763 signature2.clear();
1764 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1765 EXPECT_FALSE(InSet(&string_set, signature2));
1767 manager_->SetLevelInfo(texture_ref_.get(),
1768 GL_TEXTURE_2D,
1770 GL_RGBA,
1775 GL_RGBA,
1776 GL_UNSIGNED_BYTE,
1777 false);
1778 signature2.clear();
1779 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1780 EXPECT_FALSE(InSet(&string_set, signature2));
1782 manager_->SetLevelInfo(texture_ref_.get(),
1783 GL_TEXTURE_2D,
1785 GL_RGBA,
1790 GL_RGBA,
1791 GL_UNSIGNED_BYTE,
1792 false);
1793 signature2.clear();
1794 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1795 EXPECT_FALSE(InSet(&string_set, signature2));
1797 manager_->SetLevelInfo(texture_ref_.get(),
1798 GL_TEXTURE_2D,
1800 GL_RGBA,
1805 GL_RGB,
1806 GL_UNSIGNED_BYTE,
1807 false);
1808 signature2.clear();
1809 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1810 EXPECT_FALSE(InSet(&string_set, signature2));
1812 manager_->SetLevelInfo(texture_ref_.get(),
1813 GL_TEXTURE_2D,
1815 GL_RGBA,
1820 GL_RGBA,
1821 GL_FLOAT,
1822 false);
1823 signature2.clear();
1824 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1825 EXPECT_FALSE(InSet(&string_set, signature2));
1827 // put it back
1828 manager_->SetLevelInfo(texture_ref_.get(),
1829 GL_TEXTURE_2D,
1831 GL_RGBA,
1836 GL_RGBA,
1837 GL_UNSIGNED_BYTE,
1838 false);
1839 signature2.clear();
1840 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1841 EXPECT_EQ(signature1, signature2);
1843 // check changing parameters changes signature.
1844 SetParameter(
1845 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
1846 signature2.clear();
1847 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1848 EXPECT_FALSE(InSet(&string_set, signature2));
1850 SetParameter(texture_ref_.get(),
1851 GL_TEXTURE_MIN_FILTER,
1852 GL_NEAREST_MIPMAP_LINEAR,
1853 GL_NO_ERROR);
1854 SetParameter(
1855 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1856 signature2.clear();
1857 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1858 EXPECT_FALSE(InSet(&string_set, signature2));
1860 SetParameter(
1861 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_NO_ERROR);
1862 SetParameter(
1863 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
1864 signature2.clear();
1865 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1866 EXPECT_FALSE(InSet(&string_set, signature2));
1868 SetParameter(texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_REPEAT, GL_NO_ERROR);
1869 SetParameter(
1870 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
1871 signature2.clear();
1872 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1873 EXPECT_FALSE(InSet(&string_set, signature2));
1875 // Check putting it back genenerates the same signature
1876 SetParameter(texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_REPEAT, GL_NO_ERROR);
1877 signature2.clear();
1878 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1879 EXPECT_EQ(signature1, signature2);
1881 // Check the set was acutally getting different signatures.
1882 EXPECT_EQ(11u, string_set.size());
1885 class ProduceConsumeTextureTest : public TextureTest,
1886 public ::testing::WithParamInterface<GLenum> {
1887 public:
1888 void SetUp() override {
1889 TextureTest::SetUpBase(NULL, "GL_OES_EGL_image_external");
1890 manager_->CreateTexture(kClient2Id, kService2Id);
1891 texture2_ = manager_->GetTexture(kClient2Id);
1893 EXPECT_CALL(*decoder_.get(), GetErrorState())
1894 .WillRepeatedly(Return(error_state_.get()));
1897 void TearDown() override {
1898 if (texture2_.get()) {
1899 // If it's not in the manager then setting texture2_ to NULL will
1900 // delete the texture.
1901 if (!texture2_->client_id()) {
1902 // Check that it gets deleted when the last reference is released.
1903 EXPECT_CALL(
1904 *gl_,
1905 DeleteTextures(1, ::testing::Pointee(texture2_->service_id())))
1906 .Times(1).RetiresOnSaturation();
1908 texture2_ = NULL;
1910 TextureTest::TearDown();
1913 protected:
1914 struct LevelInfo {
1915 LevelInfo(GLenum target,
1916 GLenum format,
1917 GLsizei width,
1918 GLsizei height,
1919 GLsizei depth,
1920 GLint border,
1921 GLenum type,
1922 bool cleared)
1923 : target(target),
1924 format(format),
1925 width(width),
1926 height(height),
1927 depth(depth),
1928 border(border),
1929 type(type),
1930 cleared(cleared) {}
1932 LevelInfo()
1933 : target(0),
1934 format(0),
1935 width(-1),
1936 height(-1),
1937 depth(1),
1938 border(0),
1939 type(0),
1940 cleared(false) {}
1942 bool operator==(const LevelInfo& other) const {
1943 return target == other.target && format == other.format &&
1944 width == other.width && height == other.height &&
1945 depth == other.depth && border == other.border &&
1946 type == other.type && cleared == other.cleared;
1949 GLenum target;
1950 GLenum format;
1951 GLsizei width;
1952 GLsizei height;
1953 GLsizei depth;
1954 GLint border;
1955 GLenum type;
1956 bool cleared;
1959 void SetLevelInfo(TextureRef* texture_ref,
1960 GLint level,
1961 const LevelInfo& info) {
1962 manager_->SetLevelInfo(texture_ref,
1963 info.target,
1964 level,
1965 info.format,
1966 info.width,
1967 info.height,
1968 info.depth,
1969 info.border,
1970 info.format,
1971 info.type,
1972 info.cleared);
1975 static LevelInfo GetLevelInfo(const TextureRef* texture_ref,
1976 GLint target,
1977 GLint level) {
1978 const Texture* texture = texture_ref->texture();
1979 LevelInfo info;
1980 info.target = target;
1981 EXPECT_TRUE(texture->GetLevelSize(target, level, &info.width,
1982 &info.height));
1983 EXPECT_TRUE(texture->GetLevelType(target, level, &info.type,
1984 &info.format));
1985 info.cleared = texture->IsLevelCleared(target, level);
1986 return info;
1989 Texture* Produce(TextureRef* texture_ref) {
1990 Texture* texture = manager_->Produce(texture_ref);
1991 EXPECT_TRUE(texture != NULL);
1992 return texture;
1995 void Consume(GLuint client_id, Texture* texture) {
1996 EXPECT_TRUE(manager_->Consume(client_id, texture));
1999 scoped_refptr<TextureRef> texture2_;
2001 private:
2002 static const GLuint kClient2Id;
2003 static const GLuint kService2Id;
2006 const GLuint ProduceConsumeTextureTest::kClient2Id = 2;
2007 const GLuint ProduceConsumeTextureTest::kService2Id = 12;
2009 TEST_F(ProduceConsumeTextureTest, ProduceConsume2D) {
2010 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
2011 Texture* texture = texture_ref_->texture();
2012 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
2013 LevelInfo level0(
2014 GL_TEXTURE_2D, GL_RGBA, 4, 4, 1, 0, GL_UNSIGNED_BYTE, true);
2015 SetLevelInfo(texture_ref_.get(), 0, level0);
2016 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
2017 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
2018 LevelInfo level1 = GetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1);
2019 LevelInfo level2 = GetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2);
2020 Texture* produced_texture = Produce(texture_ref_.get());
2021 EXPECT_EQ(produced_texture, texture);
2023 // Make this texture bigger with more levels, and make sure they get
2024 // clobbered correctly during Consume().
2025 manager_->SetTarget(texture2_.get(), GL_TEXTURE_2D);
2026 SetLevelInfo(
2027 texture2_.get(),
2029 LevelInfo(GL_TEXTURE_2D, GL_RGBA, 16, 16, 1, 0, GL_UNSIGNED_BYTE, false));
2030 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture2_.get()));
2031 texture = texture2_->texture();
2032 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
2033 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U, texture->estimated_size());
2035 GLuint client_id = texture2_->client_id();
2036 manager_->RemoveTexture(client_id);
2037 Consume(client_id, produced_texture);
2038 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
2039 EXPECT_EQ(produced_texture, restored_texture->texture());
2040 EXPECT_EQ(level0, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 0));
2041 EXPECT_EQ(level1, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 1));
2042 EXPECT_EQ(level2, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 2));
2043 texture = restored_texture->texture();
2044 EXPECT_EQ(64U + 16U + 4U, texture->estimated_size());
2045 GLint w, h;
2046 EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 3, &w, &h));
2048 // However the old texture ref still exists if it was referenced somewhere.
2049 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U,
2050 texture2_->texture()->estimated_size());
2053 TEST_F(ProduceConsumeTextureTest, ProduceConsumeClearRectangle) {
2054 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_RECTANGLE_ARB);
2055 Texture* texture = texture_ref_->texture();
2056 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB), texture->target());
2057 LevelInfo level0(
2058 GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, 1, 1, 1, 0, GL_UNSIGNED_BYTE, false);
2059 SetLevelInfo(texture_ref_.get(), 0, level0);
2060 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
2061 Texture* produced_texture = Produce(texture_ref_.get());
2062 EXPECT_EQ(produced_texture, texture);
2063 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
2064 produced_texture->target());
2066 GLuint client_id = texture2_->client_id();
2067 manager_->RemoveTexture(client_id);
2068 Consume(client_id, produced_texture);
2069 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
2070 EXPECT_EQ(produced_texture, restored_texture->texture());
2072 // See if we can clear the previously uncleared level now.
2073 EXPECT_EQ(level0,
2074 GetLevelInfo(restored_texture.get(), GL_TEXTURE_RECTANGLE_ARB, 0));
2075 EXPECT_CALL(*decoder_, ClearLevel(_, _, _, _, _, _, _, _, _, _))
2076 .WillRepeatedly(Return(true));
2077 EXPECT_TRUE(manager_->ClearTextureLevel(
2078 decoder_.get(), restored_texture.get(), GL_TEXTURE_RECTANGLE_ARB, 0));
2081 TEST_F(ProduceConsumeTextureTest, ProduceConsumeExternal) {
2082 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_EXTERNAL_OES);
2083 Texture* texture = texture_ref_->texture();
2084 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), texture->target());
2085 LevelInfo level0(
2086 GL_TEXTURE_EXTERNAL_OES, GL_RGBA, 1, 1, 1, 0, GL_UNSIGNED_BYTE, false);
2087 SetLevelInfo(texture_ref_.get(), 0, level0);
2088 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
2089 Texture* produced_texture = Produce(texture_ref_.get());
2090 EXPECT_EQ(produced_texture, texture);
2092 GLuint client_id = texture2_->client_id();
2093 manager_->RemoveTexture(client_id);
2094 Consume(client_id, produced_texture);
2095 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
2096 EXPECT_EQ(produced_texture, restored_texture->texture());
2097 EXPECT_EQ(level0,
2098 GetLevelInfo(restored_texture.get(), GL_TEXTURE_EXTERNAL_OES, 0));
2101 TEST_P(ProduceConsumeTextureTest, ProduceConsumeTextureWithImage) {
2102 GLenum target = GetParam();
2103 manager_->SetTarget(texture_ref_.get(), target);
2104 Texture* texture = texture_ref_->texture();
2105 EXPECT_EQ(static_cast<GLenum>(target), texture->target());
2106 scoped_refptr<gfx::GLImage> image(new gfx::GLImageStub);
2107 manager_->SetLevelInfo(texture_ref_.get(),
2108 target,
2110 GL_RGBA,
2115 GL_RGBA,
2116 GL_UNSIGNED_BYTE,
2117 true);
2118 manager_->SetLevelImage(texture_ref_.get(), target, 0, image.get());
2119 GLuint service_id = texture->service_id();
2120 Texture* produced_texture = Produce(texture_ref_.get());
2122 GLuint client_id = texture2_->client_id();
2123 manager_->RemoveTexture(client_id);
2124 Consume(client_id, produced_texture);
2125 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
2126 EXPECT_EQ(produced_texture, restored_texture->texture());
2127 EXPECT_EQ(service_id, restored_texture->service_id());
2128 EXPECT_EQ(image.get(), restored_texture->texture()->GetLevelImage(target, 0));
2131 static const GLenum kTextureTargets[] = {GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL_OES,
2132 GL_TEXTURE_RECTANGLE_ARB, };
2134 INSTANTIATE_TEST_CASE_P(Target,
2135 ProduceConsumeTextureTest,
2136 ::testing::ValuesIn(kTextureTargets));
2138 TEST_F(ProduceConsumeTextureTest, ProduceConsumeCube) {
2139 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
2140 Texture* texture = texture_ref_->texture();
2141 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
2142 LevelInfo face0(GL_TEXTURE_CUBE_MAP_POSITIVE_X,
2143 GL_RGBA,
2148 GL_UNSIGNED_BYTE,
2149 true);
2150 LevelInfo face5(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
2151 GL_RGBA,
2156 GL_UNSIGNED_BYTE,
2157 true);
2158 SetLevelInfo(texture_ref_.get(), 0, face0);
2159 SetLevelInfo(texture_ref_.get(), 0, face5);
2160 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
2161 Texture* produced_texture = Produce(texture_ref_.get());
2162 EXPECT_EQ(produced_texture, texture);
2164 GLuint client_id = texture2_->client_id();
2165 manager_->RemoveTexture(client_id);
2166 Consume(client_id, produced_texture);
2167 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
2168 EXPECT_EQ(produced_texture, restored_texture->texture());
2169 EXPECT_EQ(
2170 face0,
2171 GetLevelInfo(restored_texture.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0));
2172 EXPECT_EQ(
2173 face5,
2174 GetLevelInfo(restored_texture.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0));
2177 class CountingMemoryTracker : public MemoryTracker {
2178 public:
2179 CountingMemoryTracker() {
2180 current_size_[0] = 0;
2181 current_size_[1] = 0;
2184 void TrackMemoryAllocatedChange(size_t old_size,
2185 size_t new_size,
2186 Pool pool) override {
2187 DCHECK_LT(static_cast<size_t>(pool), arraysize(current_size_));
2188 current_size_[pool] += new_size - old_size;
2191 bool EnsureGPUMemoryAvailable(size_t size_needed) override { return true; }
2193 size_t GetSize(Pool pool) {
2194 DCHECK_LT(static_cast<size_t>(pool), arraysize(current_size_));
2195 return current_size_[pool];
2198 private:
2199 ~CountingMemoryTracker() override {}
2201 size_t current_size_[2];
2202 DISALLOW_COPY_AND_ASSIGN(CountingMemoryTracker);
2205 class SharedTextureTest : public GpuServiceTest {
2206 public:
2207 static const bool kUseDefaultTextures = false;
2209 SharedTextureTest() : feature_info_(new FeatureInfo()) {}
2211 ~SharedTextureTest() override {}
2213 void SetUp() override {
2214 GpuServiceTest::SetUp();
2215 memory_tracker1_ = new CountingMemoryTracker;
2216 texture_manager1_.reset(
2217 new TextureManager(memory_tracker1_.get(),
2218 feature_info_.get(),
2219 TextureManagerTest::kMaxTextureSize,
2220 TextureManagerTest::kMaxCubeMapTextureSize,
2221 TextureManagerTest::kMaxRectangleTextureSize,
2222 kUseDefaultTextures));
2223 memory_tracker2_ = new CountingMemoryTracker;
2224 texture_manager2_.reset(
2225 new TextureManager(memory_tracker2_.get(),
2226 feature_info_.get(),
2227 TextureManagerTest::kMaxTextureSize,
2228 TextureManagerTest::kMaxCubeMapTextureSize,
2229 TextureManagerTest::kMaxRectangleTextureSize,
2230 kUseDefaultTextures));
2231 TestHelper::SetupTextureManagerInitExpectations(
2232 gl_.get(), "", kUseDefaultTextures);
2233 texture_manager1_->Initialize();
2234 TestHelper::SetupTextureManagerInitExpectations(
2235 gl_.get(), "", kUseDefaultTextures);
2236 texture_manager2_->Initialize();
2239 void TearDown() override {
2240 texture_manager2_->Destroy(false);
2241 texture_manager2_.reset();
2242 texture_manager1_->Destroy(false);
2243 texture_manager1_.reset();
2244 GpuServiceTest::TearDown();
2247 protected:
2248 scoped_refptr<FeatureInfo> feature_info_;
2249 scoped_refptr<CountingMemoryTracker> memory_tracker1_;
2250 scoped_ptr<TextureManager> texture_manager1_;
2251 scoped_refptr<CountingMemoryTracker> memory_tracker2_;
2252 scoped_ptr<TextureManager> texture_manager2_;
2255 TEST_F(SharedTextureTest, DeleteTextures) {
2256 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2257 scoped_refptr<TextureRef> ref2 =
2258 texture_manager2_->Consume(20, ref1->texture());
2259 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2260 .Times(0);
2261 ref1 = NULL;
2262 texture_manager1_->RemoveTexture(10);
2263 testing::Mock::VerifyAndClearExpectations(gl_.get());
2265 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2266 .Times(1)
2267 .RetiresOnSaturation();
2268 ref2 = NULL;
2269 texture_manager2_->RemoveTexture(20);
2270 testing::Mock::VerifyAndClearExpectations(gl_.get());
2273 TEST_F(SharedTextureTest, TextureSafetyAccounting) {
2274 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
2275 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
2276 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
2277 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
2278 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
2279 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
2281 // Newly created texture is renderable.
2282 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2283 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
2284 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
2285 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
2287 // Associate new texture ref to other texture manager, should account for it
2288 // too.
2289 scoped_refptr<TextureRef> ref2 =
2290 texture_manager2_->Consume(20, ref1->texture());
2291 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
2292 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
2293 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
2295 // Make texture renderable but uncleared on one texture manager, should affect
2296 // other one.
2297 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2298 EXPECT_TRUE(texture_manager1_->HaveUnrenderableTextures());
2299 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
2300 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
2301 EXPECT_TRUE(texture_manager2_->HaveUnrenderableTextures());
2302 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
2303 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
2305 texture_manager1_->SetLevelInfo(ref1.get(),
2306 GL_TEXTURE_2D,
2308 GL_RGBA,
2313 GL_RGBA,
2314 GL_UNSIGNED_BYTE,
2315 false);
2316 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
2317 EXPECT_TRUE(texture_manager1_->HaveUnsafeTextures());
2318 EXPECT_TRUE(texture_manager1_->HaveUnclearedMips());
2319 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
2320 EXPECT_TRUE(texture_manager2_->HaveUnsafeTextures());
2321 EXPECT_TRUE(texture_manager2_->HaveUnclearedMips());
2323 // Make texture cleared on one texture manager, should affect other one.
2324 texture_manager1_->SetLevelCleared(ref1.get(), GL_TEXTURE_2D, 0, true);
2325 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
2326 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
2327 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
2328 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
2330 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2331 .Times(1)
2332 .RetiresOnSaturation();
2333 texture_manager1_->RemoveTexture(10);
2334 texture_manager2_->RemoveTexture(20);
2337 TEST_F(SharedTextureTest, FBOCompletenessCheck) {
2338 const GLenum kCompleteValue = GL_FRAMEBUFFER_COMPLETE;
2339 FramebufferManager framebuffer_manager1(1, 1);
2340 texture_manager1_->set_framebuffer_manager(&framebuffer_manager1);
2341 FramebufferManager framebuffer_manager2(1, 1);
2342 texture_manager2_->set_framebuffer_manager(&framebuffer_manager2);
2344 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2345 framebuffer_manager1.CreateFramebuffer(10, 10);
2346 scoped_refptr<Framebuffer> framebuffer1 =
2347 framebuffer_manager1.GetFramebuffer(10);
2348 framebuffer1->AttachTexture(
2349 GL_COLOR_ATTACHMENT0, ref1.get(), GL_TEXTURE_2D, 0, 0);
2350 EXPECT_FALSE(framebuffer_manager1.IsComplete(framebuffer1.get()));
2351 EXPECT_NE(kCompleteValue, framebuffer1->IsPossiblyComplete());
2353 // Make FBO complete in manager 1.
2354 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2355 texture_manager1_->SetLevelInfo(ref1.get(),
2356 GL_TEXTURE_2D,
2358 GL_RGBA,
2363 GL_RGBA,
2364 GL_UNSIGNED_BYTE,
2365 true);
2366 EXPECT_EQ(kCompleteValue, framebuffer1->IsPossiblyComplete());
2367 framebuffer_manager1.MarkAsComplete(framebuffer1.get());
2368 EXPECT_TRUE(framebuffer_manager1.IsComplete(framebuffer1.get()));
2370 // Share texture with manager 2.
2371 scoped_refptr<TextureRef> ref2 =
2372 texture_manager2_->Consume(20, ref1->texture());
2373 framebuffer_manager2.CreateFramebuffer(20, 20);
2374 scoped_refptr<Framebuffer> framebuffer2 =
2375 framebuffer_manager2.GetFramebuffer(20);
2376 framebuffer2->AttachTexture(
2377 GL_COLOR_ATTACHMENT0, ref2.get(), GL_TEXTURE_2D, 0, 0);
2378 EXPECT_FALSE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2379 EXPECT_EQ(kCompleteValue, framebuffer2->IsPossiblyComplete());
2380 framebuffer_manager2.MarkAsComplete(framebuffer2.get());
2381 EXPECT_TRUE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2383 // Change level for texture, both FBOs should be marked incomplete
2384 texture_manager1_->SetLevelInfo(ref1.get(),
2385 GL_TEXTURE_2D,
2387 GL_RGBA,
2392 GL_RGBA,
2393 GL_UNSIGNED_BYTE,
2394 true);
2395 EXPECT_FALSE(framebuffer_manager1.IsComplete(framebuffer1.get()));
2396 EXPECT_EQ(kCompleteValue, framebuffer1->IsPossiblyComplete());
2397 framebuffer_manager1.MarkAsComplete(framebuffer1.get());
2398 EXPECT_TRUE(framebuffer_manager1.IsComplete(framebuffer1.get()));
2399 EXPECT_FALSE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2400 EXPECT_EQ(kCompleteValue, framebuffer2->IsPossiblyComplete());
2401 framebuffer_manager2.MarkAsComplete(framebuffer2.get());
2402 EXPECT_TRUE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2404 EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, _))
2405 .Times(2)
2406 .RetiresOnSaturation();
2407 framebuffer_manager1.RemoveFramebuffer(10);
2408 framebuffer_manager2.RemoveFramebuffer(20);
2409 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2410 .Times(1)
2411 .RetiresOnSaturation();
2412 texture_manager1_->RemoveTexture(10);
2413 texture_manager2_->RemoveTexture(20);
2416 TEST_F(SharedTextureTest, Memory) {
2417 size_t initial_memory1 = memory_tracker1_->GetSize(MemoryTracker::kUnmanaged);
2418 size_t initial_memory2 = memory_tracker2_->GetSize(MemoryTracker::kUnmanaged);
2420 // Newly created texture is unrenderable.
2421 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2422 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2423 texture_manager1_->SetLevelInfo(ref1.get(),
2424 GL_TEXTURE_2D,
2426 GL_RGBA,
2431 GL_RGBA,
2432 GL_UNSIGNED_BYTE,
2433 false);
2435 EXPECT_LT(0u, ref1->texture()->estimated_size());
2436 EXPECT_EQ(initial_memory1 + ref1->texture()->estimated_size(),
2437 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2439 // Associate new texture ref to other texture manager, it doesn't account for
2440 // the texture memory, the first memory tracker still has it.
2441 scoped_refptr<TextureRef> ref2 =
2442 texture_manager2_->Consume(20, ref1->texture());
2443 EXPECT_EQ(initial_memory1 + ref1->texture()->estimated_size(),
2444 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2445 EXPECT_EQ(initial_memory2,
2446 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2448 // Delete the texture, memory should go to the remaining tracker.
2449 texture_manager1_->RemoveTexture(10);
2450 ref1 = NULL;
2451 EXPECT_EQ(initial_memory1,
2452 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2453 EXPECT_EQ(initial_memory2 + ref2->texture()->estimated_size(),
2454 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2456 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2457 .Times(1)
2458 .RetiresOnSaturation();
2459 ref2 = NULL;
2460 texture_manager2_->RemoveTexture(20);
2461 EXPECT_EQ(initial_memory2,
2462 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2465 TEST_F(SharedTextureTest, Images) {
2466 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2467 scoped_refptr<TextureRef> ref2 =
2468 texture_manager2_->Consume(20, ref1->texture());
2470 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2471 texture_manager1_->SetLevelInfo(ref1.get(),
2472 GL_TEXTURE_2D,
2474 GL_RGBA,
2479 GL_RGBA,
2480 GL_UNSIGNED_BYTE,
2481 true);
2482 EXPECT_FALSE(ref1->texture()->HasImages());
2483 EXPECT_FALSE(ref2->texture()->HasImages());
2484 EXPECT_FALSE(texture_manager1_->HaveImages());
2485 EXPECT_FALSE(texture_manager2_->HaveImages());
2486 scoped_refptr<gfx::GLImage> image1(new gfx::GLImageStub);
2487 texture_manager1_->SetLevelImage(ref1.get(), GL_TEXTURE_2D, 1, image1.get());
2488 EXPECT_TRUE(ref1->texture()->HasImages());
2489 EXPECT_TRUE(ref2->texture()->HasImages());
2490 EXPECT_TRUE(texture_manager1_->HaveImages());
2491 EXPECT_TRUE(texture_manager2_->HaveImages());
2492 scoped_refptr<gfx::GLImage> image2(new gfx::GLImageStub);
2493 texture_manager1_->SetLevelImage(ref1.get(), GL_TEXTURE_2D, 1, image2.get());
2494 EXPECT_TRUE(ref1->texture()->HasImages());
2495 EXPECT_TRUE(ref2->texture()->HasImages());
2496 EXPECT_TRUE(texture_manager1_->HaveImages());
2497 EXPECT_TRUE(texture_manager2_->HaveImages());
2498 texture_manager1_->SetLevelInfo(ref1.get(),
2499 GL_TEXTURE_2D,
2501 GL_RGBA,
2506 GL_RGBA,
2507 GL_UNSIGNED_BYTE,
2508 true);
2509 EXPECT_FALSE(ref1->texture()->HasImages());
2510 EXPECT_FALSE(ref2->texture()->HasImages());
2511 EXPECT_FALSE(texture_manager1_->HaveImages());
2512 EXPECT_FALSE(texture_manager1_->HaveImages());
2514 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2515 .Times(1)
2516 .RetiresOnSaturation();
2517 texture_manager1_->RemoveTexture(10);
2518 texture_manager2_->RemoveTexture(20);
2521 } // namespace gles2
2522 } // namespace gpu