Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager_unittest.cc
blob838fa529a8c7b839fd3d04f453f0fb50880d174c
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/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "gpu/command_buffer/service/error_state_mock.h"
12 #include "gpu/command_buffer/service/feature_info.h"
13 #include "gpu/command_buffer/service/framebuffer_manager.h"
14 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h"
15 #include "gpu/command_buffer/service/gpu_service_test.h"
16 #include "gpu/command_buffer/service/mailbox_manager.h"
17 #include "gpu/command_buffer/service/memory_tracking.h"
18 #include "gpu/command_buffer/service/mocks.h"
19 #include "gpu/command_buffer/service/test_helper.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/gl/gl_image_stub.h"
22 #include "ui/gl/gl_mock.h"
23 #include "ui/gl/gl_switches.h"
25 using ::testing::AtLeast;
26 using ::testing::Pointee;
27 using ::testing::Return;
28 using ::testing::SetArgPointee;
29 using ::testing::StrictMock;
30 using ::testing::_;
32 namespace gpu {
33 namespace gles2 {
35 class TextureTestHelper {
36 public:
37 static bool IsNPOT(const Texture* texture) {
38 return texture->npot();
40 static bool IsTextureComplete(const Texture* texture) {
41 return texture->texture_complete();
43 static bool IsCubeComplete(const Texture* texture) {
44 return texture->cube_complete();
48 class TextureManagerTest : public GpuServiceTest {
49 public:
50 static const GLint kMaxTextureSize = 16;
51 static const GLint kMaxCubeMapTextureSize = 8;
52 static const GLint kMaxRectangleTextureSize = 16;
53 static const GLint kMaxExternalTextureSize = 16;
54 static const GLint kMax3DTextureSize = 256;
55 static const GLint kMax2dLevels = 5;
56 static const GLint kMaxCubeMapLevels = 4;
57 static const GLint kMaxExternalLevels = 1;
58 static const bool kUseDefaultTextures = false;
60 TextureManagerTest() {
61 // Always run with this command line, but the ES3 features are not
62 // enabled without FeatureInfo::EnableES3Validators().
63 base::CommandLine command_line(0, nullptr);
64 command_line.AppendSwitch(switches::kEnableUnsafeES3APIs);
65 feature_info_ = new FeatureInfo(command_line);
68 ~TextureManagerTest() override {}
70 protected:
71 void SetUp() override {
72 GpuServiceTest::SetUp();
73 manager_.reset(new TextureManager(NULL,
74 feature_info_.get(),
75 kMaxTextureSize,
76 kMaxCubeMapTextureSize,
77 kMaxRectangleTextureSize,
78 kMax3DTextureSize,
79 kUseDefaultTextures));
80 TestHelper::SetupTextureManagerInitExpectations(
81 gl_.get(), false, "", kUseDefaultTextures);
82 manager_->Initialize();
83 error_state_.reset(new ::testing::StrictMock<gles2::MockErrorState>());
86 void TearDown() override {
87 manager_->Destroy(false);
88 manager_.reset();
89 GpuServiceTest::TearDown();
92 void SetParameter(
93 TextureRef* texture_ref, GLenum pname, GLint value, GLenum error) {
94 TestHelper::SetTexParameteriWithExpectations(
95 gl_.get(), error_state_.get(), manager_.get(),
96 texture_ref, pname, value, error);
99 void SetupFeatureInfo(const char* gl_extensions,
100 const char* gl_version,
101 bool enable_es3) {
102 TestHelper::SetupFeatureInfoInitExpectationsWithGLVersion(
103 gl_.get(), gl_extensions, "", gl_version);
104 feature_info_->Initialize();
105 if (enable_es3) {
106 EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_COLOR_ATTACHMENTS, _))
107 .WillOnce(SetArgPointee<1>(8))
108 .RetiresOnSaturation();
109 EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_DRAW_BUFFERS, _))
110 .WillOnce(SetArgPointee<1>(8))
111 .RetiresOnSaturation();
112 feature_info_->EnableES3Validators();
116 scoped_refptr<FeatureInfo> feature_info_;
117 scoped_ptr<TextureManager> manager_;
118 scoped_ptr<MockErrorState> error_state_;
121 // GCC requires these declarations, but MSVC requires they not be present
122 #ifndef COMPILER_MSVC
123 const GLint TextureManagerTest::kMaxTextureSize;
124 const GLint TextureManagerTest::kMaxCubeMapTextureSize;
125 const GLint TextureManagerTest::kMaxRectangleTextureSize;
126 const GLint TextureManagerTest::kMaxExternalTextureSize;
127 const GLint TextureManagerTest::kMax2dLevels;
128 const GLint TextureManagerTest::kMaxCubeMapLevels;
129 const GLint TextureManagerTest::kMaxExternalLevels;
130 #endif
132 TEST_F(TextureManagerTest, Basic) {
133 const GLuint kClient1Id = 1;
134 const GLuint kService1Id = 11;
135 const GLuint kClient2Id = 2;
136 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
137 EXPECT_FALSE(manager_->HaveUnsafeTextures());
138 EXPECT_FALSE(manager_->HaveUnclearedMips());
139 // Check we can create texture.
140 manager_->CreateTexture(kClient1Id, kService1Id);
141 // Check texture got created.
142 scoped_refptr<TextureRef> texture = manager_->GetTexture(kClient1Id);
143 ASSERT_TRUE(texture.get() != NULL);
144 EXPECT_EQ(kService1Id, texture->service_id());
145 EXPECT_EQ(kClient1Id, texture->client_id());
146 EXPECT_EQ(texture->texture(), manager_->GetTextureForServiceId(
147 texture->service_id()));
148 // Check we get nothing for a non-existent texture.
149 EXPECT_TRUE(manager_->GetTexture(kClient2Id) == NULL);
150 // Check trying to a remove non-existent textures does not crash.
151 manager_->RemoveTexture(kClient2Id);
152 // Check that it gets deleted when the last reference is released.
153 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService1Id)))
154 .Times(1)
155 .RetiresOnSaturation();
156 // Check we can't get the texture after we remove it.
157 manager_->RemoveTexture(kClient1Id);
158 EXPECT_TRUE(manager_->GetTexture(kClient1Id) == NULL);
159 EXPECT_EQ(0u, texture->client_id());
162 TEST_F(TextureManagerTest, SetParameter) {
163 const GLuint kClient1Id = 1;
164 const GLuint kService1Id = 11;
165 // Check we can create texture.
166 manager_->CreateTexture(kClient1Id, kService1Id);
167 // Check texture got created.
168 TextureRef* texture_ref = manager_->GetTexture(kClient1Id);
169 ASSERT_TRUE(texture_ref != NULL);
170 Texture* texture = texture_ref->texture();
171 manager_->SetTarget(texture_ref, GL_TEXTURE_2D);
172 SetParameter(texture_ref, GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
173 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
174 SetParameter(texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
175 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->mag_filter());
176 SetParameter(texture_ref, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
177 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_s());
178 SetParameter(texture_ref, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
179 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_t());
180 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1, GL_NO_ERROR);
181 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2, GL_NO_ERROR);
182 SetParameter(
183 texture_ref, GL_TEXTURE_MIN_FILTER, GL_CLAMP_TO_EDGE, GL_INVALID_ENUM);
184 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
185 SetParameter(
186 texture_ref, GL_TEXTURE_MAG_FILTER, GL_CLAMP_TO_EDGE, GL_INVALID_ENUM);
187 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
188 SetParameter(texture_ref, GL_TEXTURE_WRAP_S, GL_NEAREST, GL_INVALID_ENUM);
189 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_s());
190 SetParameter(texture_ref, GL_TEXTURE_WRAP_T, GL_NEAREST, GL_INVALID_ENUM);
191 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_t());
192 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0, GL_INVALID_VALUE);
195 TEST_F(TextureManagerTest, UseDefaultTexturesTrue) {
196 bool use_default_textures = true;
197 TestHelper::SetupTextureManagerInitExpectations(gl_.get(),
198 false, "GL_ANGLE_texture_usage", use_default_textures);
199 TextureManager manager(NULL,
200 feature_info_.get(),
201 kMaxTextureSize,
202 kMaxCubeMapTextureSize,
203 kMaxRectangleTextureSize,
204 kMax3DTextureSize,
205 use_default_textures);
206 manager.Initialize();
208 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D) != NULL);
209 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP) != NULL);
211 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
213 manager.Destroy(false);
216 TEST_F(TextureManagerTest, UseDefaultTexturesFalse) {
217 bool use_default_textures = false;
218 TestHelper::SetupTextureManagerInitExpectations(gl_.get(),
219 false, "GL_ANGLE_texture_usage", use_default_textures);
220 TextureManager manager(NULL,
221 feature_info_.get(),
222 kMaxTextureSize,
223 kMaxCubeMapTextureSize,
224 kMaxRectangleTextureSize,
225 kMax3DTextureSize,
226 use_default_textures);
227 manager.Initialize();
229 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D) == NULL);
230 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP) == NULL);
232 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
234 manager.Destroy(false);
237 TEST_F(TextureManagerTest, UseDefaultTexturesTrueES3) {
238 bool use_default_textures = true;
239 SetupFeatureInfo("", "OpenGL ES 3.0", true);
240 TestHelper::SetupTextureManagerInitExpectations(gl_.get(),
241 true, "", use_default_textures);
242 TextureManager manager(NULL,
243 feature_info_.get(),
244 kMaxTextureSize,
245 kMaxCubeMapTextureSize,
246 kMaxRectangleTextureSize,
247 kMax3DTextureSize,
248 use_default_textures);
249 manager.Initialize();
251 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_3D) != NULL);
252 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D_ARRAY) != NULL);
254 manager.Destroy(false);
257 TEST_F(TextureManagerTest, UseDefaultTexturesFalseES3) {
258 bool use_default_textures = false;
259 SetupFeatureInfo("", "OpenGL ES 3.0", true);
260 TestHelper::SetupTextureManagerInitExpectations(gl_.get(),
261 true, "", use_default_textures);
262 TextureManager manager(NULL,
263 feature_info_.get(),
264 kMaxTextureSize,
265 kMaxCubeMapTextureSize,
266 kMaxRectangleTextureSize,
267 kMax3DTextureSize,
268 use_default_textures);
269 manager.Initialize();
271 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_3D) == NULL);
272 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D_ARRAY) == NULL);
274 manager.Destroy(false);
277 TEST_F(TextureManagerTest, TextureUsageExt) {
278 TestHelper::SetupTextureManagerInitExpectations(
279 gl_.get(), false, "GL_ANGLE_texture_usage", kUseDefaultTextures);
280 TextureManager manager(NULL,
281 feature_info_.get(),
282 kMaxTextureSize,
283 kMaxCubeMapTextureSize,
284 kMaxRectangleTextureSize,
285 kMax3DTextureSize,
286 kUseDefaultTextures);
287 manager.Initialize();
288 const GLuint kClient1Id = 1;
289 const GLuint kService1Id = 11;
290 // Check we can create texture.
291 manager.CreateTexture(kClient1Id, kService1Id);
292 // Check texture got created.
293 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
294 ASSERT_TRUE(texture_ref != NULL);
295 TestHelper::SetTexParameteriWithExpectations(
296 gl_.get(), error_state_.get(), &manager, texture_ref,
297 GL_TEXTURE_USAGE_ANGLE, GL_FRAMEBUFFER_ATTACHMENT_ANGLE, GL_NO_ERROR);
298 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_ATTACHMENT_ANGLE),
299 texture_ref->texture()->usage());
300 manager.Destroy(false);
303 TEST_F(TextureManagerTest, Destroy) {
304 const GLuint kClient1Id = 1;
305 const GLuint kService1Id = 11;
306 TestHelper::SetupTextureManagerInitExpectations(
307 gl_.get(), false, "", kUseDefaultTextures);
308 TextureManager manager(NULL,
309 feature_info_.get(),
310 kMaxTextureSize,
311 kMaxCubeMapTextureSize,
312 kMaxRectangleTextureSize,
313 kMax3DTextureSize,
314 kUseDefaultTextures);
315 manager.Initialize();
316 // Check we can create texture.
317 manager.CreateTexture(kClient1Id, kService1Id);
318 // Check texture got created.
319 TextureRef* texture = manager.GetTexture(kClient1Id);
320 ASSERT_TRUE(texture != NULL);
321 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService1Id)))
322 .Times(1)
323 .RetiresOnSaturation();
324 TestHelper::SetupTextureManagerDestructionExpectations(
325 gl_.get(), false, "", kUseDefaultTextures);
326 manager.Destroy(true);
327 // Check that resources got freed.
328 texture = manager.GetTexture(kClient1Id);
329 ASSERT_TRUE(texture == NULL);
332 TEST_F(TextureManagerTest, MaxValues) {
333 // Check we get the right values for the max sizes.
334 EXPECT_EQ(kMax2dLevels, manager_->MaxLevelsForTarget(GL_TEXTURE_2D));
335 EXPECT_EQ(kMaxCubeMapLevels,
336 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP));
337 EXPECT_EQ(kMaxCubeMapLevels,
338 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X));
339 EXPECT_EQ(kMaxCubeMapLevels,
340 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X));
341 EXPECT_EQ(kMaxCubeMapLevels,
342 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y));
343 EXPECT_EQ(kMaxCubeMapLevels,
344 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y));
345 EXPECT_EQ(kMaxCubeMapLevels,
346 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z));
347 EXPECT_EQ(kMaxCubeMapLevels,
348 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
349 EXPECT_EQ(kMaxExternalLevels,
350 manager_->MaxLevelsForTarget(GL_TEXTURE_EXTERNAL_OES));
351 EXPECT_EQ(kMaxTextureSize, manager_->MaxSizeForTarget(GL_TEXTURE_2D));
352 EXPECT_EQ(kMaxCubeMapTextureSize,
353 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP));
354 EXPECT_EQ(kMaxCubeMapTextureSize,
355 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X));
356 EXPECT_EQ(kMaxCubeMapTextureSize,
357 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X));
358 EXPECT_EQ(kMaxCubeMapTextureSize,
359 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y));
360 EXPECT_EQ(kMaxCubeMapTextureSize,
361 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y));
362 EXPECT_EQ(kMaxCubeMapTextureSize,
363 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z));
364 EXPECT_EQ(kMaxCubeMapTextureSize,
365 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
366 EXPECT_EQ(kMaxRectangleTextureSize,
367 manager_->MaxSizeForTarget(GL_TEXTURE_RECTANGLE_ARB));
368 EXPECT_EQ(kMaxExternalTextureSize,
369 manager_->MaxSizeForTarget(GL_TEXTURE_EXTERNAL_OES));
372 TEST_F(TextureManagerTest, ValidForTarget) {
373 // check 2d
374 EXPECT_TRUE(manager_->ValidForTarget(
375 GL_TEXTURE_2D, 0, kMaxTextureSize, kMaxTextureSize, 1));
376 EXPECT_TRUE(manager_->ValidForTarget(
377 GL_TEXTURE_2D, kMax2dLevels - 1, 1, 1, 1));
378 EXPECT_FALSE(manager_->ValidForTarget(
379 GL_TEXTURE_2D, kMax2dLevels - 1, 1, 2, 1));
380 EXPECT_FALSE(manager_->ValidForTarget(
381 GL_TEXTURE_2D, kMax2dLevels - 1, 2, 1, 1));
382 // check level out of range.
383 EXPECT_FALSE(manager_->ValidForTarget(
384 GL_TEXTURE_2D, kMax2dLevels, kMaxTextureSize, 1, 1));
385 // check has depth.
386 EXPECT_FALSE(manager_->ValidForTarget(
387 GL_TEXTURE_2D, kMax2dLevels, kMaxTextureSize, 1, 2));
388 // Check NPOT width on level 0
389 EXPECT_TRUE(manager_->ValidForTarget(GL_TEXTURE_2D, 0, 5, 2, 1));
390 // Check NPOT height on level 0
391 EXPECT_TRUE(manager_->ValidForTarget(GL_TEXTURE_2D, 0, 2, 5, 1));
392 // Check NPOT width on level 1
393 EXPECT_FALSE(manager_->ValidForTarget(GL_TEXTURE_2D, 1, 5, 2, 1));
394 // Check NPOT height on level 1
395 EXPECT_FALSE(manager_->ValidForTarget(GL_TEXTURE_2D, 1, 2, 5, 1));
397 // check cube
398 EXPECT_TRUE(manager_->ValidForTarget(
399 GL_TEXTURE_CUBE_MAP, 0,
400 kMaxCubeMapTextureSize, kMaxCubeMapTextureSize, 1));
401 EXPECT_TRUE(manager_->ValidForTarget(
402 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels - 1, 1, 1, 1));
403 EXPECT_FALSE(manager_->ValidForTarget(
404 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels - 1, 2, 2, 1));
405 // check level out of range.
406 EXPECT_FALSE(manager_->ValidForTarget(
407 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
408 kMaxCubeMapTextureSize, 1, 1));
409 // check not square.
410 EXPECT_FALSE(manager_->ValidForTarget(
411 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
412 kMaxCubeMapTextureSize, 1, 1));
413 // check has depth.
414 EXPECT_FALSE(manager_->ValidForTarget(
415 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
416 kMaxCubeMapTextureSize, 1, 2));
418 for (GLint level = 0; level < kMax2dLevels; ++level) {
419 EXPECT_TRUE(manager_->ValidForTarget(
420 GL_TEXTURE_2D, level, kMaxTextureSize >> level, 1, 1));
421 EXPECT_TRUE(manager_->ValidForTarget(
422 GL_TEXTURE_2D, level, 1, kMaxTextureSize >> level, 1));
423 EXPECT_FALSE(manager_->ValidForTarget(
424 GL_TEXTURE_2D, level, (kMaxTextureSize >> level) + 1, 1, 1));
425 EXPECT_FALSE(manager_->ValidForTarget(
426 GL_TEXTURE_2D, level, 1, (kMaxTextureSize >> level) + 1, 1));
429 for (GLint level = 0; level < kMaxCubeMapLevels; ++level) {
430 EXPECT_TRUE(manager_->ValidForTarget(
431 GL_TEXTURE_CUBE_MAP, level,
432 kMaxCubeMapTextureSize >> level,
433 kMaxCubeMapTextureSize >> level,
434 1));
435 EXPECT_FALSE(manager_->ValidForTarget(
436 GL_TEXTURE_CUBE_MAP, level,
437 (kMaxCubeMapTextureSize >> level) * 2,
438 (kMaxCubeMapTextureSize >> level) * 2,
439 1));
443 TEST_F(TextureManagerTest, ValidForTargetNPOT) {
444 TestHelper::SetupFeatureInfoInitExpectations(
445 gl_.get(), "GL_OES_texture_npot");
446 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
447 feature_info->Initialize();
448 TextureManager manager(NULL,
449 feature_info.get(),
450 kMaxTextureSize,
451 kMaxCubeMapTextureSize,
452 kMaxRectangleTextureSize,
453 kMax3DTextureSize,
454 kUseDefaultTextures);
455 // Check NPOT width on level 0
456 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 0, 5, 2, 1));
457 // Check NPOT height on level 0
458 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 0, 2, 5, 1));
459 // Check NPOT width on level 1
460 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 1, 5, 2, 1));
461 // Check NPOT height on level 1
462 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 1, 2, 5, 1));
463 manager.Destroy(false);
466 class TextureTestBase : public GpuServiceTest {
467 public:
468 static const GLint kMaxTextureSize = 16;
469 static const GLint kMaxCubeMapTextureSize = 8;
470 static const GLint kMaxRectangleTextureSize = 16;
471 static const GLint kMax3DTextureSize = 256;
472 static const GLint kMax2dLevels = 5;
473 static const GLint kMaxCubeMapLevels = 4;
474 static const GLuint kClient1Id = 1;
475 static const GLuint kService1Id = 11;
476 static const bool kUseDefaultTextures = false;
478 TextureTestBase()
479 : feature_info_(new FeatureInfo()) {
481 ~TextureTestBase() override { texture_ref_ = NULL; }
483 protected:
484 void SetUpBase(MemoryTracker* memory_tracker, const std::string& extensions) {
485 GpuServiceTest::SetUp();
486 if (!extensions.empty()) {
487 TestHelper::SetupFeatureInfoInitExpectations(gl_.get(),
488 extensions.c_str());
489 feature_info_->Initialize();
492 manager_.reset(new TextureManager(memory_tracker,
493 feature_info_.get(),
494 kMaxTextureSize,
495 kMaxCubeMapTextureSize,
496 kMaxRectangleTextureSize,
497 kMax3DTextureSize,
498 kUseDefaultTextures));
499 decoder_.reset(new ::testing::StrictMock<gles2::MockGLES2Decoder>());
500 error_state_.reset(new ::testing::StrictMock<gles2::MockErrorState>());
501 manager_->CreateTexture(kClient1Id, kService1Id);
502 texture_ref_ = manager_->GetTexture(kClient1Id);
503 ASSERT_TRUE(texture_ref_.get() != NULL);
506 void TearDown() override {
507 if (texture_ref_.get()) {
508 // If it's not in the manager then setting texture_ref_ to NULL will
509 // delete the texture.
510 if (!texture_ref_->client_id()) {
511 // Check that it gets deleted when the last reference is released.
512 EXPECT_CALL(*gl_,
513 DeleteTextures(1, ::testing::Pointee(texture_ref_->service_id())))
514 .Times(1)
515 .RetiresOnSaturation();
517 texture_ref_ = NULL;
519 manager_->Destroy(false);
520 manager_.reset();
521 GpuServiceTest::TearDown();
524 void SetParameter(
525 TextureRef* texture_ref, GLenum pname, GLint value, GLenum error) {
526 TestHelper::SetTexParameteriWithExpectations(
527 gl_.get(), error_state_.get(), manager_.get(),
528 texture_ref, pname, value, error);
531 scoped_ptr<MockGLES2Decoder> decoder_;
532 scoped_ptr<MockErrorState> error_state_;
533 scoped_refptr<FeatureInfo> feature_info_;
534 scoped_ptr<TextureManager> manager_;
535 scoped_refptr<TextureRef> texture_ref_;
538 class TextureTest : public TextureTestBase {
539 protected:
540 void SetUp() override { SetUpBase(NULL, std::string()); }
543 class TextureMemoryTrackerTest : public TextureTestBase {
544 protected:
545 void SetUp() override {
546 mock_memory_tracker_ = new StrictMock<MockMemoryTracker>();
547 SetUpBase(mock_memory_tracker_.get(), std::string());
550 scoped_refptr<MockMemoryTracker> mock_memory_tracker_;
553 #define EXPECT_MEMORY_ALLOCATION_CHANGE(old_size, new_size, pool) \
554 EXPECT_CALL(*mock_memory_tracker_.get(), \
555 TrackMemoryAllocatedChange(old_size, new_size, pool)) \
556 .Times(1).RetiresOnSaturation()
558 TEST_F(TextureTest, Basic) {
559 Texture* texture = texture_ref_->texture();
560 EXPECT_EQ(0u, texture->target());
561 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
562 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
563 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
564 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
565 EXPECT_EQ(0, texture->num_uncleared_mips());
566 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
567 EXPECT_TRUE(texture->SafeToRenderFrom());
568 EXPECT_FALSE(texture->IsImmutable());
569 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST_MIPMAP_LINEAR),
570 texture->min_filter());
571 EXPECT_EQ(static_cast<GLenum>(GL_LINEAR), texture->mag_filter());
572 EXPECT_EQ(static_cast<GLenum>(GL_REPEAT), texture->wrap_s());
573 EXPECT_EQ(static_cast<GLenum>(GL_REPEAT), texture->wrap_t());
574 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
575 EXPECT_FALSE(manager_->HaveUnsafeTextures());
576 EXPECT_EQ(0u, texture->estimated_size());
579 TEST_F(TextureTest, SetTargetTexture2D) {
580 Texture* texture = texture_ref_->texture();
581 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
582 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
583 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
584 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
585 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
586 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
587 EXPECT_TRUE(texture->SafeToRenderFrom());
588 EXPECT_FALSE(texture->IsImmutable());
591 TEST_F(TextureTest, SetTargetTextureExternalOES) {
592 Texture* texture = texture_ref_->texture();
593 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_EXTERNAL_OES);
594 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
595 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
596 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
597 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
598 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
599 EXPECT_TRUE(texture->SafeToRenderFrom());
600 EXPECT_TRUE(texture->IsImmutable());
603 TEST_F(TextureTest, ZeroSizeCanNotRender) {
604 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
605 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
606 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1,
607 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
608 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
609 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1,
610 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
611 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
614 TEST_F(TextureTest, EstimatedSize) {
615 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
616 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 1,
617 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
618 EXPECT_EQ(8u * 4u * 4u, texture_ref_->texture()->estimated_size());
619 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2, GL_RGBA, 8, 4, 1,
620 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
621 EXPECT_EQ(8u * 4u * 4u * 2u, texture_ref_->texture()->estimated_size());
624 TEST_F(TextureMemoryTrackerTest, EstimatedSize) {
625 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
626 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged);
627 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 1,
628 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
629 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged);
630 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 256, MemoryTracker::kUnmanaged);
631 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2, GL_RGBA, 8, 4, 1,
632 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
633 // Add expectation for texture deletion.
634 EXPECT_MEMORY_ALLOCATION_CHANGE(256, 0, MemoryTracker::kUnmanaged);
635 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
638 TEST_F(TextureMemoryTrackerTest, SetParameterPool) {
639 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
640 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged);
641 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 1,
642 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
643 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged);
644 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kManaged);
645 SetParameter(texture_ref_.get(),
646 GL_TEXTURE_POOL_CHROMIUM,
647 GL_TEXTURE_POOL_MANAGED_CHROMIUM,
648 GL_NO_ERROR);
649 // Add expectation for texture deletion.
650 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kManaged);
651 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
652 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kManaged);
655 TEST_F(TextureTest, POT2D) {
656 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
657 Texture* texture = texture_ref_->texture();
658 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
659 // Check Setting level 0 to POT
660 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
661 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
662 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
663 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
664 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
665 EXPECT_TRUE(texture->SafeToRenderFrom());
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(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 4, 1,
687 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
688 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
689 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
690 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
691 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
692 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
693 // Set a level past the number of mips that would get generated.
694 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 3, GL_RGBA, 4, 4, 1,
695 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
696 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
697 // Make mips.
698 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
699 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
700 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
701 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
704 TEST_F(TextureMemoryTrackerTest, MarkMipmapsGenerated) {
705 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
706 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 64, MemoryTracker::kUnmanaged);
707 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
708 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
709 EXPECT_MEMORY_ALLOCATION_CHANGE(64, 0, MemoryTracker::kUnmanaged);
710 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 84, MemoryTracker::kUnmanaged);
711 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
712 EXPECT_MEMORY_ALLOCATION_CHANGE(84, 0, MemoryTracker::kUnmanaged);
713 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
716 TEST_F(TextureTest, UnusedMips) {
717 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
718 Texture* texture = texture_ref_->texture();
719 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
720 // Set level zero to large size.
721 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
722 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
723 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
724 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
725 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
726 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
727 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
728 // Set level zero to large smaller (levels unused mips)
729 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 1,
730 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
731 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
732 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
733 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
734 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
735 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
736 // Set an unused level to some size
737 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 4, GL_RGBA, 16, 16,
738 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(16, 16));
739 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
740 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
741 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
742 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
745 TEST_F(TextureTest, NPOT2D) {
746 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
747 Texture* texture = texture_ref_->texture();
748 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
749 // Check Setting level 0 to NPOT
750 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 5, 1,
751 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
752 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
753 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
754 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
755 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
756 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
757 SetParameter(
758 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NO_ERROR);
759 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
760 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
761 SetParameter(
762 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
763 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
764 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
765 SetParameter(
766 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
767 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
768 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
769 // Change it to POT.
770 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
771 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
772 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
773 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
774 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
775 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
778 TEST_F(TextureTest, NPOT2DNPOTOK) {
779 TestHelper::SetupFeatureInfoInitExpectations(
780 gl_.get(), "GL_OES_texture_npot");
781 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
782 feature_info->Initialize();
783 TextureManager manager(NULL,
784 feature_info.get(),
785 kMaxTextureSize,
786 kMaxCubeMapTextureSize,
787 kMaxRectangleTextureSize,
788 kMax3DTextureSize,
789 kUseDefaultTextures);
790 manager.CreateTexture(kClient1Id, kService1Id);
791 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
792 ASSERT_TRUE(texture_ref != NULL);
793 Texture* texture = texture_ref->texture();
795 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
796 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
797 // Check Setting level 0 to NPOT
798 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 4, 5, 1, 0,
799 GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
800 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
801 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
802 EXPECT_TRUE(manager.CanGenerateMipmaps(texture_ref));
803 EXPECT_FALSE(manager.CanRender(texture_ref));
804 EXPECT_TRUE(manager.HaveUnrenderableTextures());
805 EXPECT_TRUE(manager.MarkMipmapsGenerated(texture_ref));
806 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
807 EXPECT_TRUE(manager.CanRender(texture_ref));
808 EXPECT_FALSE(manager.HaveUnrenderableTextures());
809 manager.Destroy(false);
812 TEST_F(TextureTest, POTCubeMap) {
813 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
814 Texture* texture = texture_ref_->texture();
815 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
816 // Check Setting level 0 each face to POT
817 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
818 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
819 gfx::Rect(4, 4));
820 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
821 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
822 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
823 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
824 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
825 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
826 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
827 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
828 gfx::Rect(4, 4));
829 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
830 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
831 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
832 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
833 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
834 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
835 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
836 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
837 gfx::Rect(4, 4));
838 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
839 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
840 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
841 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
842 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
843 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
844 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
845 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
846 gfx::Rect(4, 4));
847 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
848 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
849 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
850 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
851 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
852 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
853 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
854 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
855 gfx::Rect(4, 4));
856 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
857 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
858 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
859 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
860 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
861 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
862 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
863 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
864 gfx::Rect(4, 4));
865 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
866 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
867 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
868 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
869 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
870 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
872 // Make mips.
873 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
874 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
875 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
876 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
877 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
879 // Change a mip.
880 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 1,
881 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
882 gfx::Rect(4, 4));
883 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
884 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
885 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
886 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
887 // Set a level past the number of mips that would get generated.
888 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 3,
889 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
890 gfx::Rect(4, 4));
891 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
892 // Make mips.
893 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
894 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
895 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
898 TEST_F(TextureTest, POTCubeMapWithoutMipmap) {
899 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
900 SetParameter(
901 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
902 SetParameter(
903 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
904 SetParameter(
905 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
906 SetParameter(
907 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
909 Texture* texture = texture_ref_->texture();
910 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
911 // Check Setting level 0 each face to POT
912 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
913 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
914 gfx::Rect(4, 4));
915 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
916 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
917 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
918 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
919 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
920 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
921 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
922 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
923 gfx::Rect(4, 4));
924 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
925 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
926 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
927 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
928 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
929 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
930 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
931 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
932 gfx::Rect(4, 4));
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(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
940 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
941 gfx::Rect(4, 4));
942 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
943 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
944 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
945 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
946 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
947 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
948 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
949 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
950 gfx::Rect(4, 4));
951 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
952 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
953 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
954 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
955 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
956 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
957 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
958 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
959 gfx::Rect(4, 4));
960 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
961 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
962 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
963 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
964 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
965 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
968 TEST_F(TextureTest, GetLevelSize) {
969 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_3D);
970 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_3D, 1, GL_RGBA, 4, 5, 6,
971 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
972 GLsizei width = -1;
973 GLsizei height = -1;
974 GLsizei depth = -1;
975 Texture* texture = texture_ref_->texture();
976 EXPECT_FALSE(
977 texture->GetLevelSize(GL_TEXTURE_3D, -1, &width, &height, &depth));
978 EXPECT_FALSE(
979 texture->GetLevelSize(GL_TEXTURE_3D, 1000, &width, &height, &depth));
980 EXPECT_FALSE(
981 texture->GetLevelSize(GL_TEXTURE_3D, 0, &width, &height, &depth));
982 EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_3D, 1, &width, &height, &depth));
983 EXPECT_EQ(4, width);
984 EXPECT_EQ(5, height);
985 EXPECT_EQ(6, depth);
986 manager_->RemoveTexture(kClient1Id);
987 EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_3D, 1, &width, &height, &depth));
988 EXPECT_EQ(4, width);
989 EXPECT_EQ(5, height);
990 EXPECT_EQ(6, depth);
993 TEST_F(TextureTest, GetLevelType) {
994 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
995 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 5, 1,
996 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
997 GLenum type = 0;
998 GLenum format = 0;
999 Texture* texture = texture_ref_->texture();
1000 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, -1, &type, &format));
1001 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, 1000, &type, &format));
1002 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &format));
1003 EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &format));
1004 EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
1005 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format);
1006 manager_->RemoveTexture(kClient1Id);
1007 EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &format));
1008 EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
1009 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format);
1012 TEST_F(TextureTest, ValidForTexture) {
1013 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1014 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 5, 6,
1015 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
1016 // Check bad face.
1017 Texture* texture = texture_ref_->texture();
1018 EXPECT_FALSE(texture->ValidForTexture(
1019 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 1, 0, 0, 0, 4, 5, 6));
1020 // Check bad level.
1021 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 0, 0, 0, 0, 4, 5, 6));
1022 // Check bad xoffset.
1023 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, -1, 0, 0, 4, 5, 6));
1024 // Check bad xoffset + width > width.
1025 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 1, 0, 0, 4, 5, 6));
1026 // Check bad yoffset.
1027 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, -1, 0, 4, 5, 6));
1028 // Check bad yoffset + height > height.
1029 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 1, 0, 4, 5, 6));
1030 // Check bad zoffset.
1031 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, -1, 4, 5, 6));
1032 // Check bad zoffset + depth > depth.
1033 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, 1, 4, 5, 6));
1034 // Check bad width.
1035 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, 0, 5, 5, 6));
1036 // Check bad height.
1037 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, 0, 4, 6, 6));
1038 // Check bad depth.
1039 EXPECT_FALSE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 7));
1040 // Check valid full size
1041 EXPECT_TRUE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 6));
1042 // Check valid particial size.
1043 EXPECT_TRUE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 1, 1, 1, 2, 3, 4));
1044 manager_->RemoveTexture(kClient1Id);
1045 EXPECT_TRUE(texture->ValidForTexture(GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 6));
1048 TEST_F(TextureTest, FloatNotLinear) {
1049 TestHelper::SetupFeatureInfoInitExpectations(
1050 gl_.get(), "GL_OES_texture_float");
1051 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1052 feature_info->Initialize();
1053 TextureManager manager(NULL,
1054 feature_info.get(),
1055 kMaxTextureSize,
1056 kMaxCubeMapTextureSize,
1057 kMaxRectangleTextureSize,
1058 kMax3DTextureSize,
1059 kUseDefaultTextures);
1060 manager.CreateTexture(kClient1Id, kService1Id);
1061 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1062 ASSERT_TRUE(texture_ref != NULL);
1063 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1064 Texture* texture = texture_ref->texture();
1065 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1066 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1067 GL_RGBA, GL_FLOAT, gfx::Rect(1, 1));
1068 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1069 TestHelper::SetTexParameteriWithExpectations(
1070 gl_.get(), error_state_.get(), &manager,
1071 texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1072 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1073 TestHelper::SetTexParameteriWithExpectations(
1074 gl_.get(), error_state_.get(), &manager, texture_ref,
1075 GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST, GL_NO_ERROR);
1076 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1077 manager.Destroy(false);
1080 TEST_F(TextureTest, FloatLinear) {
1081 TestHelper::SetupFeatureInfoInitExpectations(
1082 gl_.get(), "GL_OES_texture_float GL_OES_texture_float_linear");
1083 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1084 feature_info->Initialize();
1085 TextureManager manager(NULL,
1086 feature_info.get(),
1087 kMaxTextureSize,
1088 kMaxCubeMapTextureSize,
1089 kMaxRectangleTextureSize,
1090 kMax3DTextureSize,
1091 kUseDefaultTextures);
1092 manager.CreateTexture(kClient1Id, kService1Id);
1093 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1094 ASSERT_TRUE(texture_ref != NULL);
1095 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1096 Texture* texture = texture_ref->texture();
1097 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1098 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1099 GL_RGBA, GL_FLOAT, gfx::Rect(1, 1));
1100 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1101 manager.Destroy(false);
1104 TEST_F(TextureTest, HalfFloatNotLinear) {
1105 TestHelper::SetupFeatureInfoInitExpectations(
1106 gl_.get(), "GL_OES_texture_half_float");
1107 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1108 feature_info->Initialize();
1109 TextureManager manager(NULL,
1110 feature_info.get(),
1111 kMaxTextureSize,
1112 kMaxCubeMapTextureSize,
1113 kMaxRectangleTextureSize,
1114 kMax3DTextureSize,
1115 kUseDefaultTextures);
1116 manager.CreateTexture(kClient1Id, kService1Id);
1117 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1118 ASSERT_TRUE(texture_ref != NULL);
1119 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1120 Texture* texture = texture_ref->texture();
1121 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1122 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1123 GL_RGBA, GL_HALF_FLOAT_OES, gfx::Rect(1, 1));
1124 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1125 TestHelper::SetTexParameteriWithExpectations(
1126 gl_.get(), error_state_.get(), &manager,
1127 texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1128 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1129 TestHelper::SetTexParameteriWithExpectations(
1130 gl_.get(), error_state_.get(), &manager, texture_ref,
1131 GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST, GL_NO_ERROR);
1132 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1133 manager.Destroy(false);
1136 TEST_F(TextureTest, HalfFloatLinear) {
1137 TestHelper::SetupFeatureInfoInitExpectations(
1138 gl_.get(), "GL_OES_texture_half_float GL_OES_texture_half_float_linear");
1139 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1140 feature_info->Initialize();
1141 TextureManager manager(NULL,
1142 feature_info.get(),
1143 kMaxTextureSize,
1144 kMaxCubeMapTextureSize,
1145 kMaxRectangleTextureSize,
1146 kMax3DTextureSize,
1147 kUseDefaultTextures);
1148 manager.CreateTexture(kClient1Id, kService1Id);
1149 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1150 ASSERT_TRUE(texture_ref != NULL);
1151 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1152 Texture* texture = texture_ref->texture();
1153 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1154 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1155 GL_RGBA, GL_HALF_FLOAT_OES, gfx::Rect(1, 1));
1156 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1157 manager.Destroy(false);
1160 TEST_F(TextureTest, EGLImageExternal) {
1161 TestHelper::SetupFeatureInfoInitExpectations(
1162 gl_.get(), "GL_OES_EGL_image_external");
1163 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1164 feature_info->Initialize();
1165 TextureManager manager(NULL,
1166 feature_info.get(),
1167 kMaxTextureSize,
1168 kMaxCubeMapTextureSize,
1169 kMaxRectangleTextureSize,
1170 kMax3DTextureSize,
1171 kUseDefaultTextures);
1172 manager.CreateTexture(kClient1Id, kService1Id);
1173 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1174 ASSERT_TRUE(texture_ref != NULL);
1175 manager.SetTarget(texture_ref, GL_TEXTURE_EXTERNAL_OES);
1176 Texture* texture = texture_ref->texture();
1177 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), texture->target());
1178 EXPECT_FALSE(manager.CanGenerateMipmaps(texture_ref));
1179 manager.Destroy(false);
1182 TEST_F(TextureTest, DepthTexture) {
1183 TestHelper::SetupFeatureInfoInitExpectations(
1184 gl_.get(), "GL_ANGLE_depth_texture");
1185 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1186 feature_info->Initialize();
1187 TextureManager manager(NULL,
1188 feature_info.get(),
1189 kMaxTextureSize,
1190 kMaxCubeMapTextureSize,
1191 kMaxRectangleTextureSize,
1192 kMax3DTextureSize,
1193 kUseDefaultTextures);
1194 manager.CreateTexture(kClient1Id, kService1Id);
1195 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1196 ASSERT_TRUE(texture_ref != NULL);
1197 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1198 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 4, 4,
1199 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, gfx::Rect());
1200 EXPECT_FALSE(manager.CanGenerateMipmaps(texture_ref));
1201 manager.Destroy(false);
1204 TEST_F(TextureTest, SafeUnsafe) {
1205 static const GLuint kClient2Id = 2;
1206 static const GLuint kService2Id = 12;
1207 static const GLuint kClient3Id = 3;
1208 static const GLuint kService3Id = 13;
1209 EXPECT_FALSE(manager_->HaveUnclearedMips());
1210 Texture* texture = texture_ref_->texture();
1211 EXPECT_EQ(0, texture->num_uncleared_mips());
1212 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1213 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1214 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1215 EXPECT_FALSE(texture->SafeToRenderFrom());
1216 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1217 EXPECT_TRUE(manager_->HaveUnclearedMips());
1218 EXPECT_EQ(1, texture->num_uncleared_mips());
1219 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 0, true);
1220 EXPECT_TRUE(texture->SafeToRenderFrom());
1221 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1222 EXPECT_FALSE(manager_->HaveUnclearedMips());
1223 EXPECT_EQ(0, texture->num_uncleared_mips());
1224 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 8, 8, 1,
1225 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1226 EXPECT_FALSE(texture->SafeToRenderFrom());
1227 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1228 EXPECT_TRUE(manager_->HaveUnclearedMips());
1229 EXPECT_EQ(1, texture->num_uncleared_mips());
1230 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 1, true);
1231 EXPECT_TRUE(texture->SafeToRenderFrom());
1232 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1233 EXPECT_FALSE(manager_->HaveUnclearedMips());
1234 EXPECT_EQ(0, texture->num_uncleared_mips());
1235 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1236 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1237 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 8, 8, 1,
1238 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1239 EXPECT_FALSE(texture->SafeToRenderFrom());
1240 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1241 EXPECT_TRUE(manager_->HaveUnclearedMips());
1242 EXPECT_EQ(2, texture->num_uncleared_mips());
1243 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 0, true);
1244 EXPECT_FALSE(texture->SafeToRenderFrom());
1245 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1246 EXPECT_TRUE(manager_->HaveUnclearedMips());
1247 EXPECT_EQ(1, texture->num_uncleared_mips());
1248 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 1, true);
1249 EXPECT_TRUE(texture->SafeToRenderFrom());
1250 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1251 EXPECT_FALSE(manager_->HaveUnclearedMips());
1252 EXPECT_EQ(0, texture->num_uncleared_mips());
1253 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 8, 8, 1,
1254 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1255 EXPECT_FALSE(texture->SafeToRenderFrom());
1256 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1257 EXPECT_TRUE(manager_->HaveUnclearedMips());
1258 EXPECT_EQ(1, texture->num_uncleared_mips());
1259 manager_->MarkMipmapsGenerated(texture_ref_.get());
1260 EXPECT_TRUE(texture->SafeToRenderFrom());
1261 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1262 EXPECT_FALSE(manager_->HaveUnclearedMips());
1263 EXPECT_EQ(0, texture->num_uncleared_mips());
1265 manager_->CreateTexture(kClient2Id, kService2Id);
1266 scoped_refptr<TextureRef> texture_ref2(
1267 manager_->GetTexture(kClient2Id));
1268 ASSERT_TRUE(texture_ref2.get() != NULL);
1269 manager_->SetTarget(texture_ref2.get(), GL_TEXTURE_2D);
1270 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1271 EXPECT_FALSE(manager_->HaveUnclearedMips());
1272 Texture* texture2 = texture_ref2->texture();
1273 EXPECT_EQ(0, texture2->num_uncleared_mips());
1274 manager_->SetLevelInfo(texture_ref2.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1275 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 8));
1276 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1277 EXPECT_FALSE(manager_->HaveUnclearedMips());
1278 EXPECT_EQ(0, texture2->num_uncleared_mips());
1279 manager_->SetLevelInfo(texture_ref2.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1280 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
1281 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1282 EXPECT_TRUE(manager_->HaveUnclearedMips());
1283 EXPECT_EQ(1, texture2->num_uncleared_mips());
1285 manager_->CreateTexture(kClient3Id, kService3Id);
1286 scoped_refptr<TextureRef> texture_ref3(
1287 manager_->GetTexture(kClient3Id));
1288 ASSERT_TRUE(texture_ref3.get() != NULL);
1289 manager_->SetTarget(texture_ref3.get(), GL_TEXTURE_2D);
1290 manager_->SetLevelInfo(texture_ref3.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1291 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1292 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1293 EXPECT_TRUE(manager_->HaveUnclearedMips());
1294 Texture* texture3 = texture_ref3->texture();
1295 EXPECT_EQ(1, texture3->num_uncleared_mips());
1296 manager_->SetLevelCleared(texture_ref2.get(), GL_TEXTURE_2D, 0, true);
1297 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1298 EXPECT_TRUE(manager_->HaveUnclearedMips());
1299 EXPECT_EQ(0, texture2->num_uncleared_mips());
1300 manager_->SetLevelCleared(texture_ref3.get(), GL_TEXTURE_2D, 0, true);
1301 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1302 EXPECT_FALSE(manager_->HaveUnclearedMips());
1303 EXPECT_EQ(0, texture3->num_uncleared_mips());
1305 manager_->SetLevelInfo(texture_ref2.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1306 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(1, 1, 1, 1));
1307 manager_->SetLevelInfo(texture_ref3.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1308 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4, 4, 4));
1309 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1310 EXPECT_TRUE(manager_->HaveUnclearedMips());
1311 EXPECT_EQ(1, texture2->num_uncleared_mips());
1312 EXPECT_EQ(1, texture3->num_uncleared_mips());
1313 manager_->RemoveTexture(kClient3Id);
1314 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1315 EXPECT_TRUE(manager_->HaveUnclearedMips());
1316 manager_->RemoveTexture(kClient2Id);
1317 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1318 EXPECT_TRUE(manager_->HaveUnclearedMips());
1319 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService2Id)))
1320 .Times(1)
1321 .RetiresOnSaturation();
1322 texture_ref2 = NULL;
1323 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1324 EXPECT_TRUE(manager_->HaveUnclearedMips());
1325 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService3Id)))
1326 .Times(1)
1327 .RetiresOnSaturation();
1328 texture_ref3 = NULL;
1329 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1330 EXPECT_FALSE(manager_->HaveUnclearedMips());
1333 TEST_F(TextureTest, ClearTexture) {
1334 EXPECT_CALL(*decoder_, ClearLevel(_, _, _, _, _, _, _, _, _))
1335 .WillRepeatedly(Return(true));
1336 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1337 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1338 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1339 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 4, 1,
1340 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1341 Texture* texture = texture_ref_->texture();
1342 EXPECT_FALSE(texture->SafeToRenderFrom());
1343 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1344 EXPECT_TRUE(manager_->HaveUnclearedMips());
1345 EXPECT_EQ(2, texture->num_uncleared_mips());
1346 manager_->ClearRenderableLevels(decoder_.get(), texture_ref_.get());
1347 EXPECT_TRUE(texture->SafeToRenderFrom());
1348 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1349 EXPECT_FALSE(manager_->HaveUnclearedMips());
1350 EXPECT_EQ(0, texture->num_uncleared_mips());
1351 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1352 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1353 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 4, 1,
1354 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 3));
1355 EXPECT_FALSE(texture->SafeToRenderFrom());
1356 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1357 EXPECT_TRUE(manager_->HaveUnclearedMips());
1358 EXPECT_EQ(2, texture->num_uncleared_mips());
1359 manager_->ClearTextureLevel(
1360 decoder_.get(), texture_ref_.get(), GL_TEXTURE_2D, 0);
1361 EXPECT_FALSE(texture->SafeToRenderFrom());
1362 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1363 EXPECT_TRUE(manager_->HaveUnclearedMips());
1364 EXPECT_EQ(1, texture->num_uncleared_mips());
1365 manager_->ClearTextureLevel(
1366 decoder_.get(), texture_ref_.get(), GL_TEXTURE_2D, 1);
1367 EXPECT_TRUE(texture->SafeToRenderFrom());
1368 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1369 EXPECT_FALSE(manager_->HaveUnclearedMips());
1370 EXPECT_EQ(0, texture->num_uncleared_mips());
1373 TEST_F(TextureTest, UseDeletedTexture) {
1374 static const GLuint kClient2Id = 2;
1375 static const GLuint kService2Id = 12;
1376 // Make the default texture renderable
1377 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1378 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1,
1379 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1380 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
1381 // Make a new texture
1382 manager_->CreateTexture(kClient2Id, kService2Id);
1383 scoped_refptr<TextureRef> texture_ref(
1384 manager_->GetTexture(kClient2Id));
1385 manager_->SetTarget(texture_ref.get(), GL_TEXTURE_2D);
1386 EXPECT_FALSE(manager_->CanRender(texture_ref.get()));
1387 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
1388 // Remove it.
1389 manager_->RemoveTexture(kClient2Id);
1390 EXPECT_FALSE(manager_->CanRender(texture_ref.get()));
1391 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
1392 // Check that we can still manipulate it and it effects the manager.
1393 manager_->SetLevelInfo(texture_ref.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1,
1394 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1395 EXPECT_TRUE(manager_->CanRender(texture_ref.get()));
1396 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
1397 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService2Id)))
1398 .Times(1)
1399 .RetiresOnSaturation();
1400 texture_ref = NULL;
1403 TEST_F(TextureTest, GetLevelImage) {
1404 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1405 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1406 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1407 Texture* texture = texture_ref_->texture();
1408 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1409 // Set image.
1410 scoped_refptr<gfx::GLImage> image(new gfx::GLImageStub);
1411 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, image.get());
1412 EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1413 // Remove it.
1414 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, NULL);
1415 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1416 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, image.get());
1417 // Image should be reset when SetLevelInfo is called.
1418 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1419 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1420 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1423 namespace {
1425 bool InSet(std::set<std::string>* string_set, const std::string& str) {
1426 std::pair<std::set<std::string>::iterator, bool> result =
1427 string_set->insert(str);
1428 return !result.second;
1431 } // anonymous namespace
1433 TEST_F(TextureTest, AddToSignature) {
1434 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1435 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1436 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1437 std::string signature1;
1438 std::string signature2;
1439 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature1);
1441 std::set<std::string> string_set;
1442 EXPECT_FALSE(InSet(&string_set, signature1));
1444 // check changing 1 thing makes a different signature.
1445 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 2, 1,
1446 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 2));
1447 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1448 EXPECT_FALSE(InSet(&string_set, signature2));
1450 // check putting it back makes the same signature.
1451 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1452 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1453 signature2.clear();
1454 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1455 EXPECT_EQ(signature1, signature2);
1457 // Check setting cleared status does not change signature.
1458 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1459 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1460 signature2.clear();
1461 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1462 EXPECT_EQ(signature1, signature2);
1464 // Check changing other settings changes signature.
1465 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 4, 1,
1466 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1467 signature2.clear();
1468 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1469 EXPECT_FALSE(InSet(&string_set, signature2));
1471 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 2,
1472 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1473 signature2.clear();
1474 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1475 EXPECT_FALSE(InSet(&string_set, signature2));
1477 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1478 1, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1479 signature2.clear();
1480 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1481 EXPECT_FALSE(InSet(&string_set, signature2));
1483 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1484 0, GL_RGB, GL_UNSIGNED_BYTE, gfx::Rect());
1485 signature2.clear();
1486 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1487 EXPECT_FALSE(InSet(&string_set, signature2));
1489 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1490 0, GL_RGBA, GL_FLOAT, gfx::Rect());
1491 signature2.clear();
1492 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1493 EXPECT_FALSE(InSet(&string_set, signature2));
1495 // put it back
1496 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1497 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1498 signature2.clear();
1499 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1500 EXPECT_EQ(signature1, signature2);
1502 // check changing parameters changes signature.
1503 SetParameter(
1504 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
1505 signature2.clear();
1506 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1507 EXPECT_FALSE(InSet(&string_set, signature2));
1509 SetParameter(texture_ref_.get(),
1510 GL_TEXTURE_MIN_FILTER,
1511 GL_NEAREST_MIPMAP_LINEAR,
1512 GL_NO_ERROR);
1513 SetParameter(
1514 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1515 signature2.clear();
1516 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1517 EXPECT_FALSE(InSet(&string_set, signature2));
1519 SetParameter(
1520 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_NO_ERROR);
1521 SetParameter(
1522 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
1523 signature2.clear();
1524 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1525 EXPECT_FALSE(InSet(&string_set, signature2));
1527 SetParameter(texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_REPEAT, GL_NO_ERROR);
1528 SetParameter(
1529 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
1530 signature2.clear();
1531 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1532 EXPECT_FALSE(InSet(&string_set, signature2));
1534 // Check putting it back genenerates the same signature
1535 SetParameter(texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_REPEAT, GL_NO_ERROR);
1536 signature2.clear();
1537 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1538 EXPECT_EQ(signature1, signature2);
1540 // Check the set was acutally getting different signatures.
1541 EXPECT_EQ(11u, string_set.size());
1544 class ProduceConsumeTextureTest : public TextureTest,
1545 public ::testing::WithParamInterface<GLenum> {
1546 public:
1547 void SetUp() override {
1548 TextureTest::SetUpBase(NULL, "GL_OES_EGL_image_external");
1549 manager_->CreateTexture(kClient2Id, kService2Id);
1550 texture2_ = manager_->GetTexture(kClient2Id);
1552 EXPECT_CALL(*decoder_.get(), GetErrorState())
1553 .WillRepeatedly(Return(error_state_.get()));
1556 void TearDown() override {
1557 if (texture2_.get()) {
1558 // If it's not in the manager then setting texture2_ to NULL will
1559 // delete the texture.
1560 if (!texture2_->client_id()) {
1561 // Check that it gets deleted when the last reference is released.
1562 EXPECT_CALL(
1563 *gl_,
1564 DeleteTextures(1, ::testing::Pointee(texture2_->service_id())))
1565 .Times(1).RetiresOnSaturation();
1567 texture2_ = NULL;
1569 TextureTest::TearDown();
1572 protected:
1573 struct LevelInfo {
1574 LevelInfo(GLenum target,
1575 GLenum format,
1576 GLsizei width,
1577 GLsizei height,
1578 GLsizei depth,
1579 GLint border,
1580 GLenum type,
1581 const gfx::Rect& cleared_rect)
1582 : target(target),
1583 format(format),
1584 width(width),
1585 height(height),
1586 depth(depth),
1587 border(border),
1588 type(type),
1589 cleared_rect(cleared_rect) {}
1591 LevelInfo()
1592 : target(0),
1593 format(0),
1594 width(-1),
1595 height(-1),
1596 depth(1),
1597 border(0),
1598 type(0) {}
1600 bool operator==(const LevelInfo& other) const {
1601 return target == other.target && format == other.format &&
1602 width == other.width && height == other.height &&
1603 depth == other.depth && border == other.border &&
1604 type == other.type && cleared_rect == other.cleared_rect;
1607 GLenum target;
1608 GLenum format;
1609 GLsizei width;
1610 GLsizei height;
1611 GLsizei depth;
1612 GLint border;
1613 GLenum type;
1614 gfx::Rect cleared_rect;
1617 void SetLevelInfo(TextureRef* texture_ref,
1618 GLint level,
1619 const LevelInfo& info) {
1620 manager_->SetLevelInfo(texture_ref, info.target, level, info.format,
1621 info.width, info.height, info.depth, info.border,
1622 info.format, info.type, info.cleared_rect);
1625 static LevelInfo GetLevelInfo(const TextureRef* texture_ref,
1626 GLint target,
1627 GLint level) {
1628 const Texture* texture = texture_ref->texture();
1629 LevelInfo info;
1630 info.target = target;
1631 EXPECT_TRUE(texture->GetLevelSize(target, level, &info.width,
1632 &info.height, &info.depth));
1633 EXPECT_TRUE(texture->GetLevelType(target, level, &info.type,
1634 &info.format));
1635 info.cleared_rect = texture->GetLevelClearedRect(target, level);
1636 return info;
1639 Texture* Produce(TextureRef* texture_ref) {
1640 Texture* texture = manager_->Produce(texture_ref);
1641 EXPECT_TRUE(texture != NULL);
1642 return texture;
1645 void Consume(GLuint client_id, Texture* texture) {
1646 EXPECT_TRUE(manager_->Consume(client_id, texture));
1649 scoped_refptr<TextureRef> texture2_;
1651 private:
1652 static const GLuint kClient2Id;
1653 static const GLuint kService2Id;
1656 const GLuint ProduceConsumeTextureTest::kClient2Id = 2;
1657 const GLuint ProduceConsumeTextureTest::kService2Id = 12;
1659 TEST_F(ProduceConsumeTextureTest, ProduceConsume2D) {
1660 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1661 Texture* texture = texture_ref_->texture();
1662 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1663 LevelInfo level0(GL_TEXTURE_2D, GL_RGBA, 4, 4, 1, 0, GL_UNSIGNED_BYTE,
1664 gfx::Rect(4, 4));
1665 SetLevelInfo(texture_ref_.get(), 0, level0);
1666 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
1667 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1668 LevelInfo level1 = GetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1);
1669 LevelInfo level2 = GetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2);
1670 Texture* produced_texture = Produce(texture_ref_.get());
1671 EXPECT_EQ(produced_texture, texture);
1673 // Make this texture bigger with more levels, and make sure they get
1674 // clobbered correctly during Consume().
1675 manager_->SetTarget(texture2_.get(), GL_TEXTURE_2D);
1676 SetLevelInfo(texture2_.get(), 0, LevelInfo(GL_TEXTURE_2D, GL_RGBA, 16, 16, 1,
1677 0, GL_UNSIGNED_BYTE, gfx::Rect()));
1678 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture2_.get()));
1679 texture = texture2_->texture();
1680 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1681 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U, texture->estimated_size());
1683 GLuint client_id = texture2_->client_id();
1684 manager_->RemoveTexture(client_id);
1685 Consume(client_id, produced_texture);
1686 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1687 EXPECT_EQ(produced_texture, restored_texture->texture());
1688 EXPECT_EQ(level0, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 0));
1689 EXPECT_EQ(level1, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 1));
1690 EXPECT_EQ(level2, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 2));
1691 texture = restored_texture->texture();
1692 EXPECT_EQ(64U + 16U + 4U, texture->estimated_size());
1693 GLint w, h;
1694 EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 3, &w, &h, nullptr));
1696 // However the old texture ref still exists if it was referenced somewhere.
1697 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U,
1698 texture2_->texture()->estimated_size());
1701 TEST_F(ProduceConsumeTextureTest, ProduceConsumeClearRectangle) {
1702 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_RECTANGLE_ARB);
1703 Texture* texture = texture_ref_->texture();
1704 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB), texture->target());
1705 LevelInfo level0(GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, 1, 1, 1, 0,
1706 GL_UNSIGNED_BYTE, gfx::Rect());
1707 SetLevelInfo(texture_ref_.get(), 0, level0);
1708 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1709 Texture* produced_texture = Produce(texture_ref_.get());
1710 EXPECT_EQ(produced_texture, texture);
1711 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
1712 produced_texture->target());
1714 GLuint client_id = texture2_->client_id();
1715 manager_->RemoveTexture(client_id);
1716 Consume(client_id, produced_texture);
1717 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1718 EXPECT_EQ(produced_texture, restored_texture->texture());
1720 // See if we can clear the previously uncleared level now.
1721 EXPECT_EQ(level0,
1722 GetLevelInfo(restored_texture.get(), GL_TEXTURE_RECTANGLE_ARB, 0));
1723 EXPECT_CALL(*decoder_, ClearLevel(_, _, _, _, _, _, _, _, _))
1724 .WillRepeatedly(Return(true));
1725 EXPECT_TRUE(manager_->ClearTextureLevel(
1726 decoder_.get(), restored_texture.get(), GL_TEXTURE_RECTANGLE_ARB, 0));
1729 TEST_F(ProduceConsumeTextureTest, ProduceConsumeExternal) {
1730 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_EXTERNAL_OES);
1731 Texture* texture = texture_ref_->texture();
1732 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), texture->target());
1733 LevelInfo level0(GL_TEXTURE_EXTERNAL_OES, GL_RGBA, 1, 1, 1, 0,
1734 GL_UNSIGNED_BYTE, gfx::Rect());
1735 SetLevelInfo(texture_ref_.get(), 0, level0);
1736 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1737 Texture* produced_texture = Produce(texture_ref_.get());
1738 EXPECT_EQ(produced_texture, texture);
1740 GLuint client_id = texture2_->client_id();
1741 manager_->RemoveTexture(client_id);
1742 Consume(client_id, produced_texture);
1743 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1744 EXPECT_EQ(produced_texture, restored_texture->texture());
1745 EXPECT_EQ(level0,
1746 GetLevelInfo(restored_texture.get(), GL_TEXTURE_EXTERNAL_OES, 0));
1749 TEST_P(ProduceConsumeTextureTest, ProduceConsumeTextureWithImage) {
1750 GLenum target = GetParam();
1751 manager_->SetTarget(texture_ref_.get(), target);
1752 Texture* texture = texture_ref_->texture();
1753 EXPECT_EQ(static_cast<GLenum>(target), texture->target());
1754 scoped_refptr<gfx::GLImage> image(new gfx::GLImageStub);
1755 manager_->SetLevelInfo(texture_ref_.get(), target, 0, GL_RGBA, 0, 0, 1, 0,
1756 GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1757 manager_->SetLevelImage(texture_ref_.get(), target, 0, image.get());
1758 GLuint service_id = texture->service_id();
1759 Texture* produced_texture = Produce(texture_ref_.get());
1761 GLuint client_id = texture2_->client_id();
1762 manager_->RemoveTexture(client_id);
1763 Consume(client_id, produced_texture);
1764 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1765 EXPECT_EQ(produced_texture, restored_texture->texture());
1766 EXPECT_EQ(service_id, restored_texture->service_id());
1767 EXPECT_EQ(image.get(), restored_texture->texture()->GetLevelImage(target, 0));
1770 static const GLenum kTextureTargets[] = {GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL_OES,
1771 GL_TEXTURE_RECTANGLE_ARB, };
1773 INSTANTIATE_TEST_CASE_P(Target,
1774 ProduceConsumeTextureTest,
1775 ::testing::ValuesIn(kTextureTargets));
1777 TEST_F(ProduceConsumeTextureTest, ProduceConsumeCube) {
1778 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
1779 Texture* texture = texture_ref_->texture();
1780 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
1781 LevelInfo face0(GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_RGBA, 1, 1, 1, 0,
1782 GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
1783 LevelInfo face5(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, GL_RGBA, 3, 3, 1, 0,
1784 GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
1785 SetLevelInfo(texture_ref_.get(), 0, face0);
1786 SetLevelInfo(texture_ref_.get(), 0, face5);
1787 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1788 Texture* produced_texture = Produce(texture_ref_.get());
1789 EXPECT_EQ(produced_texture, texture);
1791 GLuint client_id = texture2_->client_id();
1792 manager_->RemoveTexture(client_id);
1793 Consume(client_id, produced_texture);
1794 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1795 EXPECT_EQ(produced_texture, restored_texture->texture());
1796 EXPECT_EQ(
1797 face0,
1798 GetLevelInfo(restored_texture.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0));
1799 EXPECT_EQ(
1800 face5,
1801 GetLevelInfo(restored_texture.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0));
1804 class CountingMemoryTracker : public MemoryTracker {
1805 public:
1806 CountingMemoryTracker() {
1807 current_size_[0] = 0;
1808 current_size_[1] = 0;
1811 void TrackMemoryAllocatedChange(size_t old_size,
1812 size_t new_size,
1813 Pool pool) override {
1814 DCHECK_LT(static_cast<size_t>(pool), arraysize(current_size_));
1815 current_size_[pool] += new_size - old_size;
1818 bool EnsureGPUMemoryAvailable(size_t size_needed) override { return true; }
1820 size_t GetSize(Pool pool) {
1821 DCHECK_LT(static_cast<size_t>(pool), arraysize(current_size_));
1822 return current_size_[pool];
1825 uint64_t ClientTracingId() const override { return 0; }
1827 int ClientId() const override { return 0; }
1829 uint64_t ShareGroupTracingGUID() const override { return 0; }
1831 private:
1832 ~CountingMemoryTracker() override {}
1834 size_t current_size_[2];
1835 DISALLOW_COPY_AND_ASSIGN(CountingMemoryTracker);
1838 class SharedTextureTest : public GpuServiceTest {
1839 public:
1840 static const bool kUseDefaultTextures = false;
1842 SharedTextureTest() : feature_info_(new FeatureInfo()) {}
1844 ~SharedTextureTest() override {}
1846 void SetUp() override {
1847 GpuServiceTest::SetUp();
1848 memory_tracker1_ = new CountingMemoryTracker;
1849 texture_manager1_.reset(
1850 new TextureManager(memory_tracker1_.get(),
1851 feature_info_.get(),
1852 TextureManagerTest::kMaxTextureSize,
1853 TextureManagerTest::kMaxCubeMapTextureSize,
1854 TextureManagerTest::kMaxRectangleTextureSize,
1855 TextureManagerTest::kMax3DTextureSize,
1856 kUseDefaultTextures));
1857 memory_tracker2_ = new CountingMemoryTracker;
1858 texture_manager2_.reset(
1859 new TextureManager(memory_tracker2_.get(),
1860 feature_info_.get(),
1861 TextureManagerTest::kMaxTextureSize,
1862 TextureManagerTest::kMaxCubeMapTextureSize,
1863 TextureManagerTest::kMaxRectangleTextureSize,
1864 TextureManagerTest::kMax3DTextureSize,
1865 kUseDefaultTextures));
1866 TestHelper::SetupTextureManagerInitExpectations(
1867 gl_.get(), false, "", kUseDefaultTextures);
1868 texture_manager1_->Initialize();
1869 TestHelper::SetupTextureManagerInitExpectations(
1870 gl_.get(), false, "", kUseDefaultTextures);
1871 texture_manager2_->Initialize();
1874 void TearDown() override {
1875 texture_manager2_->Destroy(false);
1876 texture_manager2_.reset();
1877 texture_manager1_->Destroy(false);
1878 texture_manager1_.reset();
1879 GpuServiceTest::TearDown();
1882 protected:
1883 scoped_refptr<FeatureInfo> feature_info_;
1884 scoped_refptr<CountingMemoryTracker> memory_tracker1_;
1885 scoped_ptr<TextureManager> texture_manager1_;
1886 scoped_refptr<CountingMemoryTracker> memory_tracker2_;
1887 scoped_ptr<TextureManager> texture_manager2_;
1890 TEST_F(SharedTextureTest, DeleteTextures) {
1891 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1892 scoped_refptr<TextureRef> ref2 =
1893 texture_manager2_->Consume(20, ref1->texture());
1894 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1895 .Times(0);
1896 ref1 = NULL;
1897 texture_manager1_->RemoveTexture(10);
1898 testing::Mock::VerifyAndClearExpectations(gl_.get());
1900 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1901 .Times(1)
1902 .RetiresOnSaturation();
1903 ref2 = NULL;
1904 texture_manager2_->RemoveTexture(20);
1905 testing::Mock::VerifyAndClearExpectations(gl_.get());
1908 TEST_F(SharedTextureTest, TextureSafetyAccounting) {
1909 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
1910 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1911 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1912 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
1913 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1914 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1916 // Newly created texture is renderable.
1917 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1918 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
1919 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1920 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1922 // Associate new texture ref to other texture manager, should account for it
1923 // too.
1924 scoped_refptr<TextureRef> ref2 =
1925 texture_manager2_->Consume(20, ref1->texture());
1926 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
1927 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1928 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1930 // Make texture renderable but uncleared on one texture manager, should affect
1931 // other one.
1932 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
1933 EXPECT_TRUE(texture_manager1_->HaveUnrenderableTextures());
1934 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1935 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1936 EXPECT_TRUE(texture_manager2_->HaveUnrenderableTextures());
1937 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1938 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1940 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
1941 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1942 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
1943 EXPECT_TRUE(texture_manager1_->HaveUnsafeTextures());
1944 EXPECT_TRUE(texture_manager1_->HaveUnclearedMips());
1945 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
1946 EXPECT_TRUE(texture_manager2_->HaveUnsafeTextures());
1947 EXPECT_TRUE(texture_manager2_->HaveUnclearedMips());
1949 // Make texture cleared on one texture manager, should affect other one.
1950 texture_manager1_->SetLevelCleared(ref1.get(), GL_TEXTURE_2D, 0, true);
1951 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1952 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1953 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1954 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1956 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1957 .Times(1)
1958 .RetiresOnSaturation();
1959 texture_manager1_->RemoveTexture(10);
1960 texture_manager2_->RemoveTexture(20);
1963 TEST_F(SharedTextureTest, FBOCompletenessCheck) {
1964 const GLenum kCompleteValue = GL_FRAMEBUFFER_COMPLETE;
1965 FramebufferManager framebuffer_manager1(
1966 1, 1, ContextGroup::CONTEXT_TYPE_UNDEFINED, NULL);
1967 texture_manager1_->set_framebuffer_manager(&framebuffer_manager1);
1968 FramebufferManager framebuffer_manager2(
1969 1, 1, ContextGroup::CONTEXT_TYPE_UNDEFINED, NULL);
1970 texture_manager2_->set_framebuffer_manager(&framebuffer_manager2);
1972 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1973 framebuffer_manager1.CreateFramebuffer(10, 10);
1974 scoped_refptr<Framebuffer> framebuffer1 =
1975 framebuffer_manager1.GetFramebuffer(10);
1976 framebuffer1->AttachTexture(
1977 GL_COLOR_ATTACHMENT0, ref1.get(), GL_TEXTURE_2D, 0, 0);
1978 EXPECT_FALSE(framebuffer_manager1.IsComplete(framebuffer1.get()));
1979 EXPECT_NE(kCompleteValue, framebuffer1->IsPossiblyComplete());
1981 // Make FBO complete in manager 1.
1982 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
1983 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
1984 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1985 gfx::Rect(1, 1));
1986 EXPECT_EQ(kCompleteValue, framebuffer1->IsPossiblyComplete());
1987 framebuffer_manager1.MarkAsComplete(framebuffer1.get());
1988 EXPECT_TRUE(framebuffer_manager1.IsComplete(framebuffer1.get()));
1990 // Share texture with manager 2.
1991 scoped_refptr<TextureRef> ref2 =
1992 texture_manager2_->Consume(20, ref1->texture());
1993 framebuffer_manager2.CreateFramebuffer(20, 20);
1994 scoped_refptr<Framebuffer> framebuffer2 =
1995 framebuffer_manager2.GetFramebuffer(20);
1996 framebuffer2->AttachTexture(
1997 GL_COLOR_ATTACHMENT0, ref2.get(), GL_TEXTURE_2D, 0, 0);
1998 EXPECT_FALSE(framebuffer_manager2.IsComplete(framebuffer2.get()));
1999 EXPECT_EQ(kCompleteValue, framebuffer2->IsPossiblyComplete());
2000 framebuffer_manager2.MarkAsComplete(framebuffer2.get());
2001 EXPECT_TRUE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2003 // Change level for texture, both FBOs should be marked incomplete
2004 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
2005 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2006 gfx::Rect(1, 1));
2007 EXPECT_FALSE(framebuffer_manager1.IsComplete(framebuffer1.get()));
2008 EXPECT_EQ(kCompleteValue, framebuffer1->IsPossiblyComplete());
2009 framebuffer_manager1.MarkAsComplete(framebuffer1.get());
2010 EXPECT_TRUE(framebuffer_manager1.IsComplete(framebuffer1.get()));
2011 EXPECT_FALSE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2012 EXPECT_EQ(kCompleteValue, framebuffer2->IsPossiblyComplete());
2013 framebuffer_manager2.MarkAsComplete(framebuffer2.get());
2014 EXPECT_TRUE(framebuffer_manager2.IsComplete(framebuffer2.get()));
2016 EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, _))
2017 .Times(2)
2018 .RetiresOnSaturation();
2019 framebuffer_manager1.RemoveFramebuffer(10);
2020 framebuffer_manager2.RemoveFramebuffer(20);
2021 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2022 .Times(1)
2023 .RetiresOnSaturation();
2024 texture_manager1_->RemoveTexture(10);
2025 texture_manager2_->RemoveTexture(20);
2028 TEST_F(SharedTextureTest, Memory) {
2029 size_t initial_memory1 = memory_tracker1_->GetSize(MemoryTracker::kUnmanaged);
2030 size_t initial_memory2 = memory_tracker2_->GetSize(MemoryTracker::kUnmanaged);
2032 // Newly created texture is unrenderable.
2033 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2034 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2035 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 10, 10,
2036 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
2038 EXPECT_LT(0u, ref1->texture()->estimated_size());
2039 EXPECT_EQ(initial_memory1 + ref1->texture()->estimated_size(),
2040 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2042 // Associate new texture ref to other texture manager, it doesn't account for
2043 // the texture memory, the first memory tracker still has it.
2044 scoped_refptr<TextureRef> ref2 =
2045 texture_manager2_->Consume(20, ref1->texture());
2046 EXPECT_EQ(initial_memory1 + ref1->texture()->estimated_size(),
2047 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2048 EXPECT_EQ(initial_memory2,
2049 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2051 // Delete the texture, memory should go to the remaining tracker.
2052 texture_manager1_->RemoveTexture(10);
2053 ref1 = NULL;
2054 EXPECT_EQ(initial_memory1,
2055 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2056 EXPECT_EQ(initial_memory2 + ref2->texture()->estimated_size(),
2057 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2059 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2060 .Times(1)
2061 .RetiresOnSaturation();
2062 ref2 = NULL;
2063 texture_manager2_->RemoveTexture(20);
2064 EXPECT_EQ(initial_memory2,
2065 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2068 TEST_F(SharedTextureTest, Images) {
2069 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2070 scoped_refptr<TextureRef> ref2 =
2071 texture_manager2_->Consume(20, ref1->texture());
2073 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2074 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2,
2075 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2076 gfx::Rect(2, 2));
2077 EXPECT_FALSE(ref1->texture()->HasImages());
2078 EXPECT_FALSE(ref2->texture()->HasImages());
2079 EXPECT_FALSE(texture_manager1_->HaveImages());
2080 EXPECT_FALSE(texture_manager2_->HaveImages());
2081 scoped_refptr<gfx::GLImage> image1(new gfx::GLImageStub);
2082 texture_manager1_->SetLevelImage(ref1.get(), GL_TEXTURE_2D, 1, image1.get());
2083 EXPECT_TRUE(ref1->texture()->HasImages());
2084 EXPECT_TRUE(ref2->texture()->HasImages());
2085 EXPECT_TRUE(texture_manager1_->HaveImages());
2086 EXPECT_TRUE(texture_manager2_->HaveImages());
2087 scoped_refptr<gfx::GLImage> image2(new gfx::GLImageStub);
2088 texture_manager1_->SetLevelImage(ref1.get(), GL_TEXTURE_2D, 1, image2.get());
2089 EXPECT_TRUE(ref1->texture()->HasImages());
2090 EXPECT_TRUE(ref2->texture()->HasImages());
2091 EXPECT_TRUE(texture_manager1_->HaveImages());
2092 EXPECT_TRUE(texture_manager2_->HaveImages());
2093 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2,
2094 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2095 gfx::Rect(2, 2));
2096 EXPECT_FALSE(ref1->texture()->HasImages());
2097 EXPECT_FALSE(ref2->texture()->HasImages());
2098 EXPECT_FALSE(texture_manager1_->HaveImages());
2099 EXPECT_FALSE(texture_manager1_->HaveImages());
2101 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2102 .Times(1)
2103 .RetiresOnSaturation();
2104 texture_manager1_->RemoveTexture(10);
2105 texture_manager2_->RemoveTexture(20);
2109 class TextureFormatTypeValidationTest : public TextureManagerTest {
2110 public:
2111 TextureFormatTypeValidationTest() {}
2112 ~TextureFormatTypeValidationTest() override {}
2114 protected:
2115 void ExpectValid(GLenum format, GLenum type, GLenum internal_format) {
2116 EXPECT_TRUE(manager_->ValidateTextureParameters(
2117 error_state_.get(), "", format, type, internal_format, 0));
2120 void ExpectInvalid(GLenum format, GLenum type, GLenum internal_format) {
2121 EXPECT_CALL(*error_state_,
2122 SetGLError(_, _, _, _, _))
2123 .Times(1)
2124 .RetiresOnSaturation();
2125 EXPECT_FALSE(manager_->ValidateTextureParameters(
2126 error_state_.get(), "", format, type, internal_format, 0));
2129 void ExpectInvalidEnum(GLenum format, GLenum type, GLenum internal_format) {
2130 EXPECT_CALL(*error_state_,
2131 SetGLErrorInvalidEnum(_, _, _, _, _))
2132 .Times(1)
2133 .RetiresOnSaturation();
2134 EXPECT_FALSE(manager_->ValidateTextureParameters(
2135 error_state_.get(), "", format, type, internal_format, 0));
2139 TEST_F(TextureFormatTypeValidationTest, ES2Basic) {
2140 SetupFeatureInfo("", "OpenGL ES 2.0", false);
2142 ExpectValid(GL_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA);
2143 ExpectValid(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB);
2144 ExpectValid(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, GL_RGBA);
2145 ExpectValid(GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE);
2146 ExpectValid(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_LUMINANCE_ALPHA);
2148 ExpectInvalid(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA);
2150 // float / half_float.
2151 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2152 ExpectInvalidEnum(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2154 // GL_EXT_bgra
2155 ExpectInvalidEnum(GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_BGRA_EXT);
2157 // depth / stencil
2158 ExpectInvalidEnum(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2159 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2161 // SRGB
2162 ExpectInvalidEnum(GL_SRGB_EXT, GL_UNSIGNED_BYTE, GL_SRGB_EXT);
2163 ExpectInvalidEnum(GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, GL_SRGB_ALPHA_EXT);
2165 // ES3
2166 ExpectInvalidEnum(GL_RGB, GL_UNSIGNED_BYTE, GL_RGB8);
2169 TEST_F(TextureFormatTypeValidationTest, ES2WithExtTextureFormatBGRA8888) {
2170 SetupFeatureInfo("GL_EXT_texture_format_BGRA8888", "OpenGL ES 2.0", false);
2172 ExpectValid(GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_BGRA_EXT);
2175 TEST_F(TextureFormatTypeValidationTest, ES2WithAppleTextureFormatBGRA8888) {
2176 SetupFeatureInfo("GL_APPLE_texture_format_BGRA8888", "OpenGL ES 2.0", false);
2178 ExpectValid(GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_BGRA_EXT);
2181 TEST_F(TextureFormatTypeValidationTest, ES2WithArbDepth) {
2182 SetupFeatureInfo("GL_ARB_depth_texture", "OpenGL ES 2.0", false);
2184 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2185 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2186 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2189 TEST_F(TextureFormatTypeValidationTest, ES2WithOesDepth) {
2190 SetupFeatureInfo("GL_OES_depth_texture", "OpenGL ES 2.0", false);
2192 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2193 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2194 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2197 TEST_F(TextureFormatTypeValidationTest, ES2WithAngleDepth) {
2198 SetupFeatureInfo("GL_ANGLE_depth_texture", "OpenGL ES 2.0", false);
2200 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2201 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2202 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2205 TEST_F(TextureFormatTypeValidationTest, ES2WithExtPackedDepthStencil) {
2206 SetupFeatureInfo(
2207 "GL_EXT_packed_depth_stencil GL_ARB_depth_texture",
2208 "OpenGL ES 2.0",
2209 false);
2211 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2212 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2213 ExpectValid(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2216 TEST_F(TextureFormatTypeValidationTest, ES2WithRGWithFloat) {
2217 SetupFeatureInfo(
2218 "GL_EXT_texture_rg GL_OES_texture_float GL_OES_texture_half_float",
2219 "OpenGL ES 2.0",
2220 false);
2222 ExpectValid(GL_RED_EXT, GL_HALF_FLOAT_OES, GL_RED_EXT);
2223 ExpectValid(GL_RG_EXT, GL_HALF_FLOAT_OES, GL_RG_EXT);
2224 ExpectValid(GL_RED_EXT, GL_UNSIGNED_BYTE, GL_RED_EXT);
2225 ExpectValid(GL_RG_EXT, GL_UNSIGNED_BYTE, GL_RG_EXT);
2227 ExpectInvalidEnum(GL_RED_EXT, GL_BYTE, GL_RED_EXT);
2228 ExpectInvalidEnum(GL_RG_EXT, GL_BYTE, GL_RG_EXT);
2229 ExpectInvalidEnum(GL_RED_EXT, GL_SHORT, GL_RED_EXT);
2230 ExpectInvalidEnum(GL_RG_EXT, GL_SHORT, GL_RG_EXT);
2233 TEST_F(TextureFormatTypeValidationTest, ES2WithRGNoFloat) {
2234 SetupFeatureInfo("GL_ARB_texture_rg", "OpenGL ES 2.0", false);
2236 ExpectValid(GL_RED_EXT, GL_UNSIGNED_BYTE, GL_RED_EXT);
2237 ExpectValid(GL_RG_EXT, GL_UNSIGNED_BYTE, GL_RG_EXT);
2239 ExpectInvalidEnum(GL_RED_EXT, GL_HALF_FLOAT_OES, GL_RED_EXT);
2240 ExpectInvalidEnum(GL_RG_EXT, GL_HALF_FLOAT_OES, GL_RG_EXT);
2243 TEST_F(TextureFormatTypeValidationTest, ES2OnTopOfES3) {
2244 SetupFeatureInfo("", "OpenGL ES 3.0", false);
2246 ExpectInvalidEnum(GL_RGB, GL_FLOAT, GL_RGB);
2247 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2248 ExpectInvalidEnum(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2249 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2250 ExpectInvalidEnum(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2252 ExpectInvalidEnum(GL_SRGB_EXT, GL_UNSIGNED_BYTE, GL_SRGB_EXT);
2253 ExpectInvalidEnum(GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, GL_SRGB_ALPHA_EXT);
2256 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureFloat) {
2257 SetupFeatureInfo("GL_OES_texture_float", "OpenGL ES 2.0", false);
2259 ExpectValid(GL_RGB, GL_FLOAT, GL_RGB);
2260 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA);
2261 ExpectValid(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2262 ExpectValid(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2263 ExpectValid(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2265 ExpectInvalidEnum(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2266 ExpectInvalidEnum(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2267 ExpectInvalidEnum(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2268 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2269 ExpectInvalidEnum(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2272 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureFloatLinear) {
2273 SetupFeatureInfo(
2274 "GL_OES_texture_float GL_OES_texture_float_linear",
2275 "OpenGL ES 2.0",
2276 false);
2278 ExpectValid(GL_RGB, GL_FLOAT, GL_RGB);
2279 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA);
2280 ExpectValid(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2281 ExpectValid(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2282 ExpectValid(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2284 ExpectInvalidEnum(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2285 ExpectInvalidEnum(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2286 ExpectInvalidEnum(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2287 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2288 ExpectInvalidEnum(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2291 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureHalfFloat) {
2292 SetupFeatureInfo("GL_OES_texture_half_float", "OpenGL ES 2.0", false);
2294 ExpectValid(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2295 ExpectValid(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2296 ExpectValid(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2297 ExpectValid(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2298 ExpectValid(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2300 ExpectInvalidEnum(GL_RGB, GL_FLOAT, GL_RGB);
2301 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2302 ExpectInvalidEnum(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2303 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2304 ExpectInvalidEnum(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2307 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureHalfFloatLinear) {
2308 SetupFeatureInfo(
2309 "GL_OES_texture_half_float GL_OES_texture_half_float_linear",
2310 "OpenGL ES 2.0",
2311 false);
2313 ExpectValid(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2314 ExpectValid(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2315 ExpectValid(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2316 ExpectValid(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2317 ExpectValid(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2319 ExpectInvalidEnum(GL_RGB, GL_FLOAT, GL_RGB);
2320 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2321 ExpectInvalidEnum(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2322 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2323 ExpectInvalidEnum(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2326 TEST_F(TextureFormatTypeValidationTest, ES3Basic) {
2327 SetupFeatureInfo("", "OpenGL ES 3.0", true);
2329 ExpectValid(GL_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA);
2330 ExpectValid(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB);
2331 ExpectValid(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, GL_RGBA);
2332 ExpectValid(GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE);
2333 ExpectValid(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_LUMINANCE_ALPHA);
2335 ExpectValid(GL_RG, GL_BYTE, GL_RG8_SNORM);
2336 ExpectValid(GL_RG_INTEGER, GL_UNSIGNED_INT, GL_RG32UI);
2337 ExpectValid(GL_RG_INTEGER, GL_SHORT, GL_RG16I);
2338 ExpectValid(GL_RGB, GL_UNSIGNED_BYTE, GL_SRGB8);
2339 ExpectValid(GL_RGBA, GL_HALF_FLOAT, GL_RGBA16F);
2340 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA16F);
2341 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA32F);
2343 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT16);
2344 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT24);
2345 ExpectValid(GL_DEPTH_COMPONENT, GL_FLOAT, GL_DEPTH_COMPONENT32F);
2346 ExpectValid(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH24_STENCIL8);
2347 ExpectValid(GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
2348 GL_DEPTH32F_STENCIL8);
2350 ExpectInvalid(GL_RGB_INTEGER, GL_INT, GL_RGBA8);
2353 } // namespace gles2
2354 } // namespace gpu