Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager_unittest.cc
blob6cadd978df8c1ed05b4b2d95badeb728acdbd8a9
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(), "", 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 scoped_refptr<FeatureInfo> feature_info_;
100 scoped_ptr<TextureManager> manager_;
101 scoped_ptr<MockErrorState> error_state_;
104 // GCC requires these declarations, but MSVC requires they not be present
105 #ifndef COMPILER_MSVC
106 const GLint TextureManagerTest::kMaxTextureSize;
107 const GLint TextureManagerTest::kMaxCubeMapTextureSize;
108 const GLint TextureManagerTest::kMaxRectangleTextureSize;
109 const GLint TextureManagerTest::kMaxExternalTextureSize;
110 const GLint TextureManagerTest::kMax2dLevels;
111 const GLint TextureManagerTest::kMaxCubeMapLevels;
112 const GLint TextureManagerTest::kMaxExternalLevels;
113 #endif
115 TEST_F(TextureManagerTest, Basic) {
116 const GLuint kClient1Id = 1;
117 const GLuint kService1Id = 11;
118 const GLuint kClient2Id = 2;
119 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
120 EXPECT_FALSE(manager_->HaveUnsafeTextures());
121 EXPECT_FALSE(manager_->HaveUnclearedMips());
122 // Check we can create texture.
123 manager_->CreateTexture(kClient1Id, kService1Id);
124 // Check texture got created.
125 scoped_refptr<TextureRef> texture = manager_->GetTexture(kClient1Id);
126 ASSERT_TRUE(texture.get() != NULL);
127 EXPECT_EQ(kService1Id, texture->service_id());
128 EXPECT_EQ(kClient1Id, texture->client_id());
129 EXPECT_EQ(texture->texture(), manager_->GetTextureForServiceId(
130 texture->service_id()));
131 // Check we get nothing for a non-existent texture.
132 EXPECT_TRUE(manager_->GetTexture(kClient2Id) == NULL);
133 // Check trying to a remove non-existent textures does not crash.
134 manager_->RemoveTexture(kClient2Id);
135 // Check that it gets deleted when the last reference is released.
136 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService1Id)))
137 .Times(1)
138 .RetiresOnSaturation();
139 // Check we can't get the texture after we remove it.
140 manager_->RemoveTexture(kClient1Id);
141 EXPECT_TRUE(manager_->GetTexture(kClient1Id) == NULL);
142 EXPECT_EQ(0u, texture->client_id());
145 TEST_F(TextureManagerTest, SetParameter) {
146 const GLuint kClient1Id = 1;
147 const GLuint kService1Id = 11;
148 // Check we can create texture.
149 manager_->CreateTexture(kClient1Id, kService1Id);
150 // Check texture got created.
151 TextureRef* texture_ref = manager_->GetTexture(kClient1Id);
152 ASSERT_TRUE(texture_ref != NULL);
153 Texture* texture = texture_ref->texture();
154 manager_->SetTarget(texture_ref, GL_TEXTURE_2D);
155 SetParameter(texture_ref, GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
156 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
157 SetParameter(texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
158 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->mag_filter());
159 SetParameter(texture_ref, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
160 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_s());
161 SetParameter(texture_ref, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
162 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_t());
163 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1, GL_NO_ERROR);
164 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2, GL_NO_ERROR);
165 SetParameter(
166 texture_ref, GL_TEXTURE_MIN_FILTER, GL_CLAMP_TO_EDGE, GL_INVALID_ENUM);
167 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
168 SetParameter(
169 texture_ref, GL_TEXTURE_MAG_FILTER, GL_CLAMP_TO_EDGE, GL_INVALID_ENUM);
170 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST), texture->min_filter());
171 SetParameter(texture_ref, GL_TEXTURE_WRAP_S, GL_NEAREST, GL_INVALID_ENUM);
172 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_s());
173 SetParameter(texture_ref, GL_TEXTURE_WRAP_T, GL_NEAREST, GL_INVALID_ENUM);
174 EXPECT_EQ(static_cast<GLenum>(GL_CLAMP_TO_EDGE), texture->wrap_t());
175 SetParameter(texture_ref, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0, GL_INVALID_VALUE);
178 TEST_F(TextureManagerTest, UseDefaultTexturesTrue) {
179 bool use_default_textures = true;
180 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
182 TestHelper::SetupTextureManagerInitExpectations(
183 gl_.get(), "GL_ANGLE_texture_usage", use_default_textures);
184 TextureManager manager(NULL,
185 feature_info_.get(),
186 kMaxTextureSize,
187 kMaxCubeMapTextureSize,
188 kMaxRectangleTextureSize,
189 kMax3DTextureSize,
190 use_default_textures);
191 manager.Initialize();
193 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D) != NULL);
194 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP) != NULL);
196 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
198 manager.Destroy(false);
201 TEST_F(TextureManagerTest, UseDefaultTexturesFalse) {
202 bool use_default_textures = false;
203 TestHelper::SetupTextureManagerInitExpectations(
204 gl_.get(), "GL_ANGLE_texture_usage", use_default_textures);
205 TextureManager manager(NULL,
206 feature_info_.get(),
207 kMaxTextureSize,
208 kMaxCubeMapTextureSize,
209 kMaxRectangleTextureSize,
210 kMax3DTextureSize,
211 use_default_textures);
212 manager.Initialize();
214 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_2D) == NULL);
215 EXPECT_TRUE(manager.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP) == NULL);
217 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
219 manager.Destroy(false);
222 TEST_F(TextureManagerTest, TextureUsageExt) {
223 TestHelper::SetupTextureManagerInitExpectations(
224 gl_.get(), "GL_ANGLE_texture_usage", kUseDefaultTextures);
225 TextureManager manager(NULL,
226 feature_info_.get(),
227 kMaxTextureSize,
228 kMaxCubeMapTextureSize,
229 kMaxRectangleTextureSize,
230 kMax3DTextureSize,
231 kUseDefaultTextures);
232 manager.Initialize();
233 const GLuint kClient1Id = 1;
234 const GLuint kService1Id = 11;
235 // Check we can create texture.
236 manager.CreateTexture(kClient1Id, kService1Id);
237 // Check texture got created.
238 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
239 ASSERT_TRUE(texture_ref != NULL);
240 TestHelper::SetTexParameteriWithExpectations(
241 gl_.get(), error_state_.get(), &manager, texture_ref,
242 GL_TEXTURE_USAGE_ANGLE, GL_FRAMEBUFFER_ATTACHMENT_ANGLE, GL_NO_ERROR);
243 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_ATTACHMENT_ANGLE),
244 texture_ref->texture()->usage());
245 manager.Destroy(false);
248 TEST_F(TextureManagerTest, Destroy) {
249 const GLuint kClient1Id = 1;
250 const GLuint kService1Id = 11;
251 TestHelper::SetupTextureManagerInitExpectations(
252 gl_.get(), "", kUseDefaultTextures);
253 TextureManager manager(NULL,
254 feature_info_.get(),
255 kMaxTextureSize,
256 kMaxCubeMapTextureSize,
257 kMaxRectangleTextureSize,
258 kMax3DTextureSize,
259 kUseDefaultTextures);
260 manager.Initialize();
261 // Check we can create texture.
262 manager.CreateTexture(kClient1Id, kService1Id);
263 // Check texture got created.
264 TextureRef* texture = manager.GetTexture(kClient1Id);
265 ASSERT_TRUE(texture != NULL);
266 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService1Id)))
267 .Times(1)
268 .RetiresOnSaturation();
269 TestHelper::SetupTextureManagerDestructionExpectations(
270 gl_.get(), "", kUseDefaultTextures);
271 manager.Destroy(true);
272 // Check that resources got freed.
273 texture = manager.GetTexture(kClient1Id);
274 ASSERT_TRUE(texture == NULL);
277 TEST_F(TextureManagerTest, MaxValues) {
278 // Check we get the right values for the max sizes.
279 EXPECT_EQ(kMax2dLevels, manager_->MaxLevelsForTarget(GL_TEXTURE_2D));
280 EXPECT_EQ(kMaxCubeMapLevels,
281 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP));
282 EXPECT_EQ(kMaxCubeMapLevels,
283 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X));
284 EXPECT_EQ(kMaxCubeMapLevels,
285 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X));
286 EXPECT_EQ(kMaxCubeMapLevels,
287 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y));
288 EXPECT_EQ(kMaxCubeMapLevels,
289 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y));
290 EXPECT_EQ(kMaxCubeMapLevels,
291 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z));
292 EXPECT_EQ(kMaxCubeMapLevels,
293 manager_->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
294 EXPECT_EQ(kMaxExternalLevels,
295 manager_->MaxLevelsForTarget(GL_TEXTURE_EXTERNAL_OES));
296 EXPECT_EQ(kMaxTextureSize, manager_->MaxSizeForTarget(GL_TEXTURE_2D));
297 EXPECT_EQ(kMaxCubeMapTextureSize,
298 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP));
299 EXPECT_EQ(kMaxCubeMapTextureSize,
300 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X));
301 EXPECT_EQ(kMaxCubeMapTextureSize,
302 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X));
303 EXPECT_EQ(kMaxCubeMapTextureSize,
304 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y));
305 EXPECT_EQ(kMaxCubeMapTextureSize,
306 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y));
307 EXPECT_EQ(kMaxCubeMapTextureSize,
308 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z));
309 EXPECT_EQ(kMaxCubeMapTextureSize,
310 manager_->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
311 EXPECT_EQ(kMaxRectangleTextureSize,
312 manager_->MaxSizeForTarget(GL_TEXTURE_RECTANGLE_ARB));
313 EXPECT_EQ(kMaxExternalTextureSize,
314 manager_->MaxSizeForTarget(GL_TEXTURE_EXTERNAL_OES));
317 TEST_F(TextureManagerTest, ValidForTarget) {
318 // check 2d
319 EXPECT_TRUE(manager_->ValidForTarget(
320 GL_TEXTURE_2D, 0, kMaxTextureSize, kMaxTextureSize, 1));
321 EXPECT_TRUE(manager_->ValidForTarget(
322 GL_TEXTURE_2D, kMax2dLevels - 1, 1, 1, 1));
323 EXPECT_FALSE(manager_->ValidForTarget(
324 GL_TEXTURE_2D, kMax2dLevels - 1, 1, 2, 1));
325 EXPECT_FALSE(manager_->ValidForTarget(
326 GL_TEXTURE_2D, kMax2dLevels - 1, 2, 1, 1));
327 // check level out of range.
328 EXPECT_FALSE(manager_->ValidForTarget(
329 GL_TEXTURE_2D, kMax2dLevels, kMaxTextureSize, 1, 1));
330 // check has depth.
331 EXPECT_FALSE(manager_->ValidForTarget(
332 GL_TEXTURE_2D, kMax2dLevels, kMaxTextureSize, 1, 2));
333 // Check NPOT width on level 0
334 EXPECT_TRUE(manager_->ValidForTarget(GL_TEXTURE_2D, 0, 5, 2, 1));
335 // Check NPOT height on level 0
336 EXPECT_TRUE(manager_->ValidForTarget(GL_TEXTURE_2D, 0, 2, 5, 1));
337 // Check NPOT width on level 1
338 EXPECT_FALSE(manager_->ValidForTarget(GL_TEXTURE_2D, 1, 5, 2, 1));
339 // Check NPOT height on level 1
340 EXPECT_FALSE(manager_->ValidForTarget(GL_TEXTURE_2D, 1, 2, 5, 1));
342 // check cube
343 EXPECT_TRUE(manager_->ValidForTarget(
344 GL_TEXTURE_CUBE_MAP, 0,
345 kMaxCubeMapTextureSize, kMaxCubeMapTextureSize, 1));
346 EXPECT_TRUE(manager_->ValidForTarget(
347 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels - 1, 1, 1, 1));
348 EXPECT_FALSE(manager_->ValidForTarget(
349 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels - 1, 2, 2, 1));
350 // check level out of range.
351 EXPECT_FALSE(manager_->ValidForTarget(
352 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
353 kMaxCubeMapTextureSize, 1, 1));
354 // check not square.
355 EXPECT_FALSE(manager_->ValidForTarget(
356 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
357 kMaxCubeMapTextureSize, 1, 1));
358 // check has depth.
359 EXPECT_FALSE(manager_->ValidForTarget(
360 GL_TEXTURE_CUBE_MAP, kMaxCubeMapLevels,
361 kMaxCubeMapTextureSize, 1, 2));
363 for (GLint level = 0; level < kMax2dLevels; ++level) {
364 EXPECT_TRUE(manager_->ValidForTarget(
365 GL_TEXTURE_2D, level, kMaxTextureSize >> level, 1, 1));
366 EXPECT_TRUE(manager_->ValidForTarget(
367 GL_TEXTURE_2D, level, 1, kMaxTextureSize >> level, 1));
368 EXPECT_FALSE(manager_->ValidForTarget(
369 GL_TEXTURE_2D, level, (kMaxTextureSize >> level) + 1, 1, 1));
370 EXPECT_FALSE(manager_->ValidForTarget(
371 GL_TEXTURE_2D, level, 1, (kMaxTextureSize >> level) + 1, 1));
374 for (GLint level = 0; level < kMaxCubeMapLevels; ++level) {
375 EXPECT_TRUE(manager_->ValidForTarget(
376 GL_TEXTURE_CUBE_MAP, level,
377 kMaxCubeMapTextureSize >> level,
378 kMaxCubeMapTextureSize >> level,
379 1));
380 EXPECT_FALSE(manager_->ValidForTarget(
381 GL_TEXTURE_CUBE_MAP, level,
382 (kMaxCubeMapTextureSize >> level) * 2,
383 (kMaxCubeMapTextureSize >> level) * 2,
384 1));
388 TEST_F(TextureManagerTest, ValidForTargetNPOT) {
389 TestHelper::SetupFeatureInfoInitExpectations(
390 gl_.get(), "GL_OES_texture_npot");
391 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
392 feature_info->Initialize();
393 TextureManager manager(NULL,
394 feature_info.get(),
395 kMaxTextureSize,
396 kMaxCubeMapTextureSize,
397 kMaxRectangleTextureSize,
398 kMax3DTextureSize,
399 kUseDefaultTextures);
400 // Check NPOT width on level 0
401 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 0, 5, 2, 1));
402 // Check NPOT height on level 0
403 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 0, 2, 5, 1));
404 // Check NPOT width on level 1
405 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 1, 5, 2, 1));
406 // Check NPOT height on level 1
407 EXPECT_TRUE(manager.ValidForTarget(GL_TEXTURE_2D, 1, 2, 5, 1));
408 manager.Destroy(false);
411 class TextureTestBase : public GpuServiceTest {
412 public:
413 static const GLint kMaxTextureSize = 16;
414 static const GLint kMaxCubeMapTextureSize = 8;
415 static const GLint kMaxRectangleTextureSize = 16;
416 static const GLint kMax3DTextureSize = 256;
417 static const GLint kMax2dLevels = 5;
418 static const GLint kMaxCubeMapLevels = 4;
419 static const GLuint kClient1Id = 1;
420 static const GLuint kService1Id = 11;
421 static const bool kUseDefaultTextures = false;
423 TextureTestBase()
424 : feature_info_(new FeatureInfo()) {
426 ~TextureTestBase() override { texture_ref_ = NULL; }
428 protected:
429 void SetUpBase(MemoryTracker* memory_tracker, std::string extensions) {
430 GpuServiceTest::SetUp();
431 if (!extensions.empty()) {
432 TestHelper::SetupFeatureInfoInitExpectations(gl_.get(),
433 extensions.c_str());
434 feature_info_->Initialize();
437 manager_.reset(new TextureManager(memory_tracker,
438 feature_info_.get(),
439 kMaxTextureSize,
440 kMaxCubeMapTextureSize,
441 kMaxRectangleTextureSize,
442 kMax3DTextureSize,
443 kUseDefaultTextures));
444 decoder_.reset(new ::testing::StrictMock<gles2::MockGLES2Decoder>());
445 error_state_.reset(new ::testing::StrictMock<gles2::MockErrorState>());
446 manager_->CreateTexture(kClient1Id, kService1Id);
447 texture_ref_ = manager_->GetTexture(kClient1Id);
448 ASSERT_TRUE(texture_ref_.get() != NULL);
451 void TearDown() override {
452 if (texture_ref_.get()) {
453 // If it's not in the manager then setting texture_ref_ to NULL will
454 // delete the texture.
455 if (!texture_ref_->client_id()) {
456 // Check that it gets deleted when the last reference is released.
457 EXPECT_CALL(*gl_,
458 DeleteTextures(1, ::testing::Pointee(texture_ref_->service_id())))
459 .Times(1)
460 .RetiresOnSaturation();
462 texture_ref_ = NULL;
464 manager_->Destroy(false);
465 manager_.reset();
466 GpuServiceTest::TearDown();
469 void SetParameter(
470 TextureRef* texture_ref, GLenum pname, GLint value, GLenum error) {
471 TestHelper::SetTexParameteriWithExpectations(
472 gl_.get(), error_state_.get(), manager_.get(),
473 texture_ref, pname, value, error);
476 scoped_ptr<MockGLES2Decoder> decoder_;
477 scoped_ptr<MockErrorState> error_state_;
478 scoped_refptr<FeatureInfo> feature_info_;
479 scoped_ptr<TextureManager> manager_;
480 scoped_refptr<TextureRef> texture_ref_;
483 class TextureTest : public TextureTestBase {
484 protected:
485 void SetUp() override { SetUpBase(NULL, std::string()); }
488 class TextureMemoryTrackerTest : public TextureTestBase {
489 protected:
490 void SetUp() override {
491 mock_memory_tracker_ = new StrictMock<MockMemoryTracker>();
492 SetUpBase(mock_memory_tracker_.get(), std::string());
495 scoped_refptr<MockMemoryTracker> mock_memory_tracker_;
498 #define EXPECT_MEMORY_ALLOCATION_CHANGE(old_size, new_size, pool) \
499 EXPECT_CALL(*mock_memory_tracker_.get(), \
500 TrackMemoryAllocatedChange(old_size, new_size, pool)) \
501 .Times(1).RetiresOnSaturation()
503 TEST_F(TextureTest, Basic) {
504 Texture* texture = texture_ref_->texture();
505 EXPECT_EQ(0u, texture->target());
506 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
507 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
508 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
509 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
510 EXPECT_EQ(0, texture->num_uncleared_mips());
511 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
512 EXPECT_TRUE(texture->SafeToRenderFrom());
513 EXPECT_FALSE(texture->IsImmutable());
514 EXPECT_EQ(static_cast<GLenum>(GL_NEAREST_MIPMAP_LINEAR),
515 texture->min_filter());
516 EXPECT_EQ(static_cast<GLenum>(GL_LINEAR), texture->mag_filter());
517 EXPECT_EQ(static_cast<GLenum>(GL_REPEAT), texture->wrap_s());
518 EXPECT_EQ(static_cast<GLenum>(GL_REPEAT), texture->wrap_t());
519 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
520 EXPECT_FALSE(manager_->HaveUnsafeTextures());
521 EXPECT_EQ(0u, texture->estimated_size());
524 TEST_F(TextureTest, SetTargetTexture2D) {
525 Texture* texture = texture_ref_->texture();
526 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
527 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
528 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
529 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
530 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
531 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
532 EXPECT_TRUE(texture->SafeToRenderFrom());
533 EXPECT_FALSE(texture->IsImmutable());
536 TEST_F(TextureTest, SetTargetTextureExternalOES) {
537 Texture* texture = texture_ref_->texture();
538 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_EXTERNAL_OES);
539 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
540 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
541 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
542 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
543 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
544 EXPECT_TRUE(texture->SafeToRenderFrom());
545 EXPECT_TRUE(texture->IsImmutable());
548 TEST_F(TextureTest, ZeroSizeCanNotRender) {
549 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
550 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
551 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1,
552 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
553 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
554 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1,
555 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
556 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
559 TEST_F(TextureTest, EstimatedSize) {
560 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
561 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 1,
562 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
563 EXPECT_EQ(8u * 4u * 4u, texture_ref_->texture()->estimated_size());
564 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2, GL_RGBA, 8, 4, 1,
565 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
566 EXPECT_EQ(8u * 4u * 4u * 2u, texture_ref_->texture()->estimated_size());
569 TEST_F(TextureMemoryTrackerTest, EstimatedSize) {
570 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
571 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged);
572 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 1,
573 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
574 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged);
575 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 256, MemoryTracker::kUnmanaged);
576 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2, GL_RGBA, 8, 4, 1,
577 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
578 // Add expectation for texture deletion.
579 EXPECT_MEMORY_ALLOCATION_CHANGE(256, 0, MemoryTracker::kUnmanaged);
580 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
583 TEST_F(TextureMemoryTrackerTest, SetParameterPool) {
584 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
585 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged);
586 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 1,
587 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 4));
588 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged);
589 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kManaged);
590 SetParameter(texture_ref_.get(),
591 GL_TEXTURE_POOL_CHROMIUM,
592 GL_TEXTURE_POOL_MANAGED_CHROMIUM,
593 GL_NO_ERROR);
594 // Add expectation for texture deletion.
595 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kManaged);
596 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
597 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kManaged);
600 TEST_F(TextureTest, POT2D) {
601 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
602 Texture* texture = texture_ref_->texture();
603 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
604 // Check Setting level 0 to POT
605 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
606 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
607 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
608 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
609 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
610 EXPECT_TRUE(texture->SafeToRenderFrom());
611 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
612 // Set filters to something that will work with a single mip.
613 SetParameter(
614 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NO_ERROR);
615 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
616 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
617 // Set them back.
618 SetParameter(texture_ref_.get(),
619 GL_TEXTURE_MIN_FILTER,
620 GL_LINEAR_MIPMAP_LINEAR,
621 GL_NO_ERROR);
622 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
624 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
625 // Make mips.
626 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
627 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
628 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
629 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
630 // Change a mip.
631 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 4, 1,
632 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
633 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
634 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
635 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
636 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
637 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
638 // Set a level past the number of mips that would get generated.
639 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 3, GL_RGBA, 4, 4, 1,
640 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
641 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
642 // Make mips.
643 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
644 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
645 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
646 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
649 TEST_F(TextureMemoryTrackerTest, MarkMipmapsGenerated) {
650 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
651 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 64, MemoryTracker::kUnmanaged);
652 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
653 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
654 EXPECT_MEMORY_ALLOCATION_CHANGE(64, 0, MemoryTracker::kUnmanaged);
655 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 84, MemoryTracker::kUnmanaged);
656 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
657 EXPECT_MEMORY_ALLOCATION_CHANGE(84, 0, MemoryTracker::kUnmanaged);
658 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged);
661 TEST_F(TextureTest, UnusedMips) {
662 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
663 Texture* texture = texture_ref_->texture();
664 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
665 // Set level zero to large size.
666 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
667 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
668 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
669 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
670 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
671 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
672 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
673 // Set level zero to large smaller (levels unused mips)
674 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 1,
675 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
676 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
677 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
678 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
679 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
680 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
681 // Set an unused level to some size
682 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 4, GL_RGBA, 16, 16,
683 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(16, 16));
684 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
685 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
686 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
687 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
690 TEST_F(TextureTest, NPOT2D) {
691 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
692 Texture* texture = texture_ref_->texture();
693 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
694 // Check Setting level 0 to NPOT
695 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 5, 1,
696 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
697 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
698 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
699 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
700 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
701 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
702 SetParameter(
703 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NO_ERROR);
704 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
705 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
706 SetParameter(
707 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
708 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
709 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
710 SetParameter(
711 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
712 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
713 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
714 // Change it to POT.
715 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
716 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4));
717 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
718 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
719 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
720 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
723 TEST_F(TextureTest, NPOT2DNPOTOK) {
724 TestHelper::SetupFeatureInfoInitExpectations(
725 gl_.get(), "GL_OES_texture_npot");
726 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
727 feature_info->Initialize();
728 TextureManager manager(NULL,
729 feature_info.get(),
730 kMaxTextureSize,
731 kMaxCubeMapTextureSize,
732 kMaxRectangleTextureSize,
733 kMax3DTextureSize,
734 kUseDefaultTextures);
735 manager.CreateTexture(kClient1Id, kService1Id);
736 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
737 ASSERT_TRUE(texture_ref != NULL);
738 Texture* texture = texture_ref->texture();
740 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
741 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
742 // Check Setting level 0 to NPOT
743 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 4, 5, 1, 0,
744 GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
745 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture));
746 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
747 EXPECT_TRUE(manager.CanGenerateMipmaps(texture_ref));
748 EXPECT_FALSE(manager.CanRender(texture_ref));
749 EXPECT_TRUE(manager.HaveUnrenderableTextures());
750 EXPECT_TRUE(manager.MarkMipmapsGenerated(texture_ref));
751 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
752 EXPECT_TRUE(manager.CanRender(texture_ref));
753 EXPECT_FALSE(manager.HaveUnrenderableTextures());
754 manager.Destroy(false);
757 TEST_F(TextureTest, POTCubeMap) {
758 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
759 Texture* texture = texture_ref_->texture();
760 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
761 // Check Setting level 0 each face to POT
762 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
763 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
764 gfx::Rect(4, 4));
765 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
766 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
767 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
768 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
769 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
770 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
771 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
772 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
773 gfx::Rect(4, 4));
774 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
775 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
776 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
777 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
778 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
779 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
780 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
781 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
782 gfx::Rect(4, 4));
783 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
784 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
785 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
786 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
787 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
788 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
789 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
790 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
791 gfx::Rect(4, 4));
792 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
793 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
794 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
795 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
796 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
797 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
798 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
799 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
800 gfx::Rect(4, 4));
801 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
802 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
803 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
804 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
805 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
806 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
807 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
808 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
809 gfx::Rect(4, 4));
810 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
811 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
812 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
813 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
814 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
815 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
817 // Make mips.
818 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
819 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
820 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
821 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
822 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
824 // Change a mip.
825 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 1,
826 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
827 gfx::Rect(4, 4));
828 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
829 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
830 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
831 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
832 // Set a level past the number of mips that would get generated.
833 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 3,
834 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
835 gfx::Rect(4, 4));
836 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
837 // Make mips.
838 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
839 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
840 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
843 TEST_F(TextureTest, POTCubeMapWithoutMipmap) {
844 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
845 SetParameter(
846 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
847 SetParameter(
848 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
849 SetParameter(
850 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
851 SetParameter(
852 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
854 Texture* texture = texture_ref_->texture();
855 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
856 // Check Setting level 0 each face to POT
857 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
858 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
859 gfx::Rect(4, 4));
860 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
861 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
862 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
863 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
864 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
865 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
866 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
867 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
868 gfx::Rect(4, 4));
869 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
870 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
871 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
872 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
873 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
874 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
875 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
876 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
877 gfx::Rect(4, 4));
878 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
879 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
880 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
881 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
882 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
883 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
884 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
885 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
886 gfx::Rect(4, 4));
887 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
888 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
889 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
890 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
891 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
892 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
893 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
894 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
895 gfx::Rect(4, 4));
896 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
897 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
898 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture));
899 EXPECT_FALSE(manager_->CanGenerateMipmaps(texture_ref_.get()));
900 EXPECT_FALSE(manager_->CanRender(texture_ref_.get()));
901 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
902 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
903 GL_RGBA, 4, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
904 gfx::Rect(4, 4));
905 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture));
906 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
907 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture));
908 EXPECT_TRUE(manager_->CanGenerateMipmaps(texture_ref_.get()));
909 EXPECT_TRUE(manager_->CanRender(texture_ref_.get()));
910 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
913 TEST_F(TextureTest, GetLevelSize) {
914 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_3D);
915 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_3D, 1, GL_RGBA, 4, 5, 6,
916 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
917 GLsizei width = -1;
918 GLsizei height = -1;
919 GLsizei depth = -1;
920 Texture* texture = texture_ref_->texture();
921 EXPECT_FALSE(
922 texture->GetLevelSize(GL_TEXTURE_3D, -1, &width, &height, &depth));
923 EXPECT_FALSE(
924 texture->GetLevelSize(GL_TEXTURE_3D, 1000, &width, &height, &depth));
925 EXPECT_FALSE(
926 texture->GetLevelSize(GL_TEXTURE_3D, 0, &width, &height, &depth));
927 EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_3D, 1, &width, &height, &depth));
928 EXPECT_EQ(4, width);
929 EXPECT_EQ(5, height);
930 EXPECT_EQ(6, depth);
931 manager_->RemoveTexture(kClient1Id);
932 EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_3D, 1, &width, &height, &depth));
933 EXPECT_EQ(4, width);
934 EXPECT_EQ(5, height);
935 EXPECT_EQ(6, depth);
938 TEST_F(TextureTest, GetLevelType) {
939 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
940 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 5, 1,
941 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
942 GLenum type = 0;
943 GLenum format = 0;
944 Texture* texture = texture_ref_->texture();
945 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, -1, &type, &format));
946 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, 1000, &type, &format));
947 EXPECT_FALSE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &format));
948 EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &format));
949 EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
950 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format);
951 manager_->RemoveTexture(kClient1Id);
952 EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &format));
953 EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
954 EXPECT_EQ(static_cast<GLenum>(GL_RGBA), format);
957 TEST_F(TextureTest, ValidForTexture) {
958 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
959 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 5, 6,
960 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 5));
961 // Check bad face.
962 Texture* texture = texture_ref_->texture();
963 EXPECT_FALSE(texture->ValidForTexture(
964 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
965 1, 0, 0, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
966 // Check bad level.
967 EXPECT_FALSE(texture->ValidForTexture(
968 GL_TEXTURE_2D, 0, 0, 0, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
969 // Check bad xoffset.
970 EXPECT_FALSE(texture->ValidForTexture(
971 GL_TEXTURE_2D, 1, -1, 0, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
972 // Check bad xoffset + width > width.
973 EXPECT_FALSE(texture->ValidForTexture(
974 GL_TEXTURE_2D, 1, 1, 0, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
975 // Check bad yoffset.
976 EXPECT_FALSE(texture->ValidForTexture(
977 GL_TEXTURE_2D, 1, 0, -1, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
978 // Check bad yoffset + height > height.
979 EXPECT_FALSE(texture->ValidForTexture(
980 GL_TEXTURE_2D, 1, 0, 1, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
981 // Check bad zoffset.
982 EXPECT_FALSE(texture->ValidForTexture(
983 GL_TEXTURE_2D, 1, 0, 0, -1, 4, 5, 6, GL_UNSIGNED_BYTE));
984 // Check bad zoffset + depth > depth.
985 EXPECT_FALSE(texture->ValidForTexture(
986 GL_TEXTURE_2D, 1, 0, 0, 1, 4, 5, 6, GL_UNSIGNED_BYTE));
987 // Check bad width.
988 EXPECT_FALSE(texture->ValidForTexture(
989 GL_TEXTURE_2D, 1, 0, 0, 0, 5, 5, 6, GL_UNSIGNED_BYTE));
990 // Check bad height.
991 EXPECT_FALSE(texture->ValidForTexture(
992 GL_TEXTURE_2D, 1, 0, 0, 0, 4, 6, 6, GL_UNSIGNED_BYTE));
993 // Check bad depth.
994 EXPECT_FALSE(texture->ValidForTexture(
995 GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 7, GL_UNSIGNED_BYTE));
996 // Check bad type.
997 EXPECT_FALSE(texture->ValidForTexture(
998 GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 6, GL_UNSIGNED_SHORT_4_4_4_4));
999 // Check valid full size
1000 EXPECT_TRUE(texture->ValidForTexture(
1001 GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
1002 // Check valid particial size.
1003 EXPECT_TRUE(texture->ValidForTexture(
1004 GL_TEXTURE_2D, 1, 1, 1, 1, 2, 3, 4, GL_UNSIGNED_BYTE));
1005 manager_->RemoveTexture(kClient1Id);
1006 EXPECT_TRUE(texture->ValidForTexture(
1007 GL_TEXTURE_2D, 1, 0, 0, 0, 4, 5, 6, GL_UNSIGNED_BYTE));
1010 TEST_F(TextureTest, FloatNotLinear) {
1011 TestHelper::SetupFeatureInfoInitExpectations(
1012 gl_.get(), "GL_OES_texture_float");
1013 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1014 feature_info->Initialize();
1015 TextureManager manager(NULL,
1016 feature_info.get(),
1017 kMaxTextureSize,
1018 kMaxCubeMapTextureSize,
1019 kMaxRectangleTextureSize,
1020 kMax3DTextureSize,
1021 kUseDefaultTextures);
1022 manager.CreateTexture(kClient1Id, kService1Id);
1023 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1024 ASSERT_TRUE(texture_ref != NULL);
1025 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1026 Texture* texture = texture_ref->texture();
1027 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1028 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1029 GL_RGBA, GL_FLOAT, gfx::Rect(1, 1));
1030 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1031 TestHelper::SetTexParameteriWithExpectations(
1032 gl_.get(), error_state_.get(), &manager,
1033 texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1034 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1035 TestHelper::SetTexParameteriWithExpectations(
1036 gl_.get(), error_state_.get(), &manager, texture_ref,
1037 GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST, GL_NO_ERROR);
1038 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1039 manager.Destroy(false);
1042 TEST_F(TextureTest, FloatLinear) {
1043 TestHelper::SetupFeatureInfoInitExpectations(
1044 gl_.get(), "GL_OES_texture_float GL_OES_texture_float_linear");
1045 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1046 feature_info->Initialize();
1047 TextureManager manager(NULL,
1048 feature_info.get(),
1049 kMaxTextureSize,
1050 kMaxCubeMapTextureSize,
1051 kMaxRectangleTextureSize,
1052 kMax3DTextureSize,
1053 kUseDefaultTextures);
1054 manager.CreateTexture(kClient1Id, kService1Id);
1055 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1056 ASSERT_TRUE(texture_ref != NULL);
1057 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1058 Texture* texture = texture_ref->texture();
1059 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1060 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1061 GL_RGBA, GL_FLOAT, gfx::Rect(1, 1));
1062 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1063 manager.Destroy(false);
1066 TEST_F(TextureTest, HalfFloatNotLinear) {
1067 TestHelper::SetupFeatureInfoInitExpectations(
1068 gl_.get(), "GL_OES_texture_half_float");
1069 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1070 feature_info->Initialize();
1071 TextureManager manager(NULL,
1072 feature_info.get(),
1073 kMaxTextureSize,
1074 kMaxCubeMapTextureSize,
1075 kMaxRectangleTextureSize,
1076 kMax3DTextureSize,
1077 kUseDefaultTextures);
1078 manager.CreateTexture(kClient1Id, kService1Id);
1079 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1080 ASSERT_TRUE(texture_ref != NULL);
1081 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1082 Texture* texture = texture_ref->texture();
1083 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1084 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1085 GL_RGBA, GL_HALF_FLOAT_OES, gfx::Rect(1, 1));
1086 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1087 TestHelper::SetTexParameteriWithExpectations(
1088 gl_.get(), error_state_.get(), &manager,
1089 texture_ref, GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1090 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture));
1091 TestHelper::SetTexParameteriWithExpectations(
1092 gl_.get(), error_state_.get(), &manager, texture_ref,
1093 GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST, GL_NO_ERROR);
1094 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1095 manager.Destroy(false);
1098 TEST_F(TextureTest, HalfFloatLinear) {
1099 TestHelper::SetupFeatureInfoInitExpectations(
1100 gl_.get(), "GL_OES_texture_half_float GL_OES_texture_half_float_linear");
1101 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1102 feature_info->Initialize();
1103 TextureManager manager(NULL,
1104 feature_info.get(),
1105 kMaxTextureSize,
1106 kMaxCubeMapTextureSize,
1107 kMaxRectangleTextureSize,
1108 kMax3DTextureSize,
1109 kUseDefaultTextures);
1110 manager.CreateTexture(kClient1Id, kService1Id);
1111 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1112 ASSERT_TRUE(texture_ref != NULL);
1113 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1114 Texture* texture = texture_ref->texture();
1115 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1116 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1, 0,
1117 GL_RGBA, GL_HALF_FLOAT_OES, gfx::Rect(1, 1));
1118 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1119 manager.Destroy(false);
1122 TEST_F(TextureTest, EGLImageExternal) {
1123 TestHelper::SetupFeatureInfoInitExpectations(
1124 gl_.get(), "GL_OES_EGL_image_external");
1125 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1126 feature_info->Initialize();
1127 TextureManager manager(NULL,
1128 feature_info.get(),
1129 kMaxTextureSize,
1130 kMaxCubeMapTextureSize,
1131 kMaxRectangleTextureSize,
1132 kMax3DTextureSize,
1133 kUseDefaultTextures);
1134 manager.CreateTexture(kClient1Id, kService1Id);
1135 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1136 ASSERT_TRUE(texture_ref != NULL);
1137 manager.SetTarget(texture_ref, GL_TEXTURE_EXTERNAL_OES);
1138 Texture* texture = texture_ref->texture();
1139 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), texture->target());
1140 EXPECT_FALSE(manager.CanGenerateMipmaps(texture_ref));
1141 manager.Destroy(false);
1144 TEST_F(TextureTest, DepthTexture) {
1145 TestHelper::SetupFeatureInfoInitExpectations(
1146 gl_.get(), "GL_ANGLE_depth_texture");
1147 scoped_refptr<FeatureInfo> feature_info(new FeatureInfo());
1148 feature_info->Initialize();
1149 TextureManager manager(NULL,
1150 feature_info.get(),
1151 kMaxTextureSize,
1152 kMaxCubeMapTextureSize,
1153 kMaxRectangleTextureSize,
1154 kMax3DTextureSize,
1155 kUseDefaultTextures);
1156 manager.CreateTexture(kClient1Id, kService1Id);
1157 TextureRef* texture_ref = manager.GetTexture(kClient1Id);
1158 ASSERT_TRUE(texture_ref != NULL);
1159 manager.SetTarget(texture_ref, GL_TEXTURE_2D);
1160 manager.SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 4, 4,
1161 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, gfx::Rect());
1162 EXPECT_FALSE(manager.CanGenerateMipmaps(texture_ref));
1163 manager.Destroy(false);
1166 TEST_F(TextureTest, SafeUnsafe) {
1167 static const GLuint kClient2Id = 2;
1168 static const GLuint kService2Id = 12;
1169 static const GLuint kClient3Id = 3;
1170 static const GLuint kService3Id = 13;
1171 EXPECT_FALSE(manager_->HaveUnclearedMips());
1172 Texture* texture = texture_ref_->texture();
1173 EXPECT_EQ(0, texture->num_uncleared_mips());
1174 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1175 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1176 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1177 EXPECT_FALSE(texture->SafeToRenderFrom());
1178 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1179 EXPECT_TRUE(manager_->HaveUnclearedMips());
1180 EXPECT_EQ(1, texture->num_uncleared_mips());
1181 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 0, true);
1182 EXPECT_TRUE(texture->SafeToRenderFrom());
1183 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1184 EXPECT_FALSE(manager_->HaveUnclearedMips());
1185 EXPECT_EQ(0, texture->num_uncleared_mips());
1186 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 8, 8, 1,
1187 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1188 EXPECT_FALSE(texture->SafeToRenderFrom());
1189 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1190 EXPECT_TRUE(manager_->HaveUnclearedMips());
1191 EXPECT_EQ(1, texture->num_uncleared_mips());
1192 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 1, true);
1193 EXPECT_TRUE(texture->SafeToRenderFrom());
1194 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1195 EXPECT_FALSE(manager_->HaveUnclearedMips());
1196 EXPECT_EQ(0, texture->num_uncleared_mips());
1197 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1198 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1199 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 8, 8, 1,
1200 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1201 EXPECT_FALSE(texture->SafeToRenderFrom());
1202 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1203 EXPECT_TRUE(manager_->HaveUnclearedMips());
1204 EXPECT_EQ(2, texture->num_uncleared_mips());
1205 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 0, true);
1206 EXPECT_FALSE(texture->SafeToRenderFrom());
1207 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1208 EXPECT_TRUE(manager_->HaveUnclearedMips());
1209 EXPECT_EQ(1, texture->num_uncleared_mips());
1210 manager_->SetLevelCleared(texture_ref_.get(), GL_TEXTURE_2D, 1, true);
1211 EXPECT_TRUE(texture->SafeToRenderFrom());
1212 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1213 EXPECT_FALSE(manager_->HaveUnclearedMips());
1214 EXPECT_EQ(0, texture->num_uncleared_mips());
1215 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 8, 8, 1,
1216 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1217 EXPECT_FALSE(texture->SafeToRenderFrom());
1218 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1219 EXPECT_TRUE(manager_->HaveUnclearedMips());
1220 EXPECT_EQ(1, texture->num_uncleared_mips());
1221 manager_->MarkMipmapsGenerated(texture_ref_.get());
1222 EXPECT_TRUE(texture->SafeToRenderFrom());
1223 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1224 EXPECT_FALSE(manager_->HaveUnclearedMips());
1225 EXPECT_EQ(0, texture->num_uncleared_mips());
1227 manager_->CreateTexture(kClient2Id, kService2Id);
1228 scoped_refptr<TextureRef> texture_ref2(
1229 manager_->GetTexture(kClient2Id));
1230 ASSERT_TRUE(texture_ref2.get() != NULL);
1231 manager_->SetTarget(texture_ref2.get(), GL_TEXTURE_2D);
1232 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1233 EXPECT_FALSE(manager_->HaveUnclearedMips());
1234 Texture* texture2 = texture_ref2->texture();
1235 EXPECT_EQ(0, texture2->num_uncleared_mips());
1236 manager_->SetLevelInfo(texture_ref2.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1237 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(8, 8));
1238 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1239 EXPECT_FALSE(manager_->HaveUnclearedMips());
1240 EXPECT_EQ(0, texture2->num_uncleared_mips());
1241 manager_->SetLevelInfo(texture_ref2.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1242 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
1243 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1244 EXPECT_TRUE(manager_->HaveUnclearedMips());
1245 EXPECT_EQ(1, texture2->num_uncleared_mips());
1247 manager_->CreateTexture(kClient3Id, kService3Id);
1248 scoped_refptr<TextureRef> texture_ref3(
1249 manager_->GetTexture(kClient3Id));
1250 ASSERT_TRUE(texture_ref3.get() != NULL);
1251 manager_->SetTarget(texture_ref3.get(), GL_TEXTURE_2D);
1252 manager_->SetLevelInfo(texture_ref3.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1253 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1254 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1255 EXPECT_TRUE(manager_->HaveUnclearedMips());
1256 Texture* texture3 = texture_ref3->texture();
1257 EXPECT_EQ(1, texture3->num_uncleared_mips());
1258 manager_->SetLevelCleared(texture_ref2.get(), GL_TEXTURE_2D, 0, true);
1259 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1260 EXPECT_TRUE(manager_->HaveUnclearedMips());
1261 EXPECT_EQ(0, texture2->num_uncleared_mips());
1262 manager_->SetLevelCleared(texture_ref3.get(), GL_TEXTURE_2D, 0, true);
1263 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1264 EXPECT_FALSE(manager_->HaveUnclearedMips());
1265 EXPECT_EQ(0, texture3->num_uncleared_mips());
1267 manager_->SetLevelInfo(texture_ref2.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1268 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(1, 1, 1, 1));
1269 manager_->SetLevelInfo(texture_ref3.get(), GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 1,
1270 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 4, 4, 4));
1271 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1272 EXPECT_TRUE(manager_->HaveUnclearedMips());
1273 EXPECT_EQ(1, texture2->num_uncleared_mips());
1274 EXPECT_EQ(1, texture3->num_uncleared_mips());
1275 manager_->RemoveTexture(kClient3Id);
1276 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1277 EXPECT_TRUE(manager_->HaveUnclearedMips());
1278 manager_->RemoveTexture(kClient2Id);
1279 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1280 EXPECT_TRUE(manager_->HaveUnclearedMips());
1281 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService2Id)))
1282 .Times(1)
1283 .RetiresOnSaturation();
1284 texture_ref2 = NULL;
1285 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1286 EXPECT_TRUE(manager_->HaveUnclearedMips());
1287 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService3Id)))
1288 .Times(1)
1289 .RetiresOnSaturation();
1290 texture_ref3 = NULL;
1291 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1292 EXPECT_FALSE(manager_->HaveUnclearedMips());
1295 TEST_F(TextureTest, ClearTexture) {
1296 EXPECT_CALL(*decoder_, ClearLevel(_, _, _, _, _, _, _, _, _))
1297 .WillRepeatedly(Return(true));
1298 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1299 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1300 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1301 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 4, 1,
1302 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1303 Texture* texture = texture_ref_->texture();
1304 EXPECT_FALSE(texture->SafeToRenderFrom());
1305 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1306 EXPECT_TRUE(manager_->HaveUnclearedMips());
1307 EXPECT_EQ(2, texture->num_uncleared_mips());
1308 manager_->ClearRenderableLevels(decoder_.get(), texture_ref_.get());
1309 EXPECT_TRUE(texture->SafeToRenderFrom());
1310 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1311 EXPECT_FALSE(manager_->HaveUnclearedMips());
1312 EXPECT_EQ(0, texture->num_uncleared_mips());
1313 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 1,
1314 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1315 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 4, 1,
1316 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 3));
1317 EXPECT_FALSE(texture->SafeToRenderFrom());
1318 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1319 EXPECT_TRUE(manager_->HaveUnclearedMips());
1320 EXPECT_EQ(2, texture->num_uncleared_mips());
1321 manager_->ClearTextureLevel(
1322 decoder_.get(), texture_ref_.get(), GL_TEXTURE_2D, 0);
1323 EXPECT_FALSE(texture->SafeToRenderFrom());
1324 EXPECT_TRUE(manager_->HaveUnsafeTextures());
1325 EXPECT_TRUE(manager_->HaveUnclearedMips());
1326 EXPECT_EQ(1, texture->num_uncleared_mips());
1327 manager_->ClearTextureLevel(
1328 decoder_.get(), texture_ref_.get(), GL_TEXTURE_2D, 1);
1329 EXPECT_TRUE(texture->SafeToRenderFrom());
1330 EXPECT_FALSE(manager_->HaveUnsafeTextures());
1331 EXPECT_FALSE(manager_->HaveUnclearedMips());
1332 EXPECT_EQ(0, texture->num_uncleared_mips());
1335 TEST_F(TextureTest, UseDeletedTexture) {
1336 static const GLuint kClient2Id = 2;
1337 static const GLuint kService2Id = 12;
1338 // Make the default texture renderable
1339 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1340 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1,
1341 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1342 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
1343 // Make a new texture
1344 manager_->CreateTexture(kClient2Id, kService2Id);
1345 scoped_refptr<TextureRef> texture_ref(
1346 manager_->GetTexture(kClient2Id));
1347 manager_->SetTarget(texture_ref.get(), GL_TEXTURE_2D);
1348 EXPECT_FALSE(manager_->CanRender(texture_ref.get()));
1349 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
1350 // Remove it.
1351 manager_->RemoveTexture(kClient2Id);
1352 EXPECT_FALSE(manager_->CanRender(texture_ref.get()));
1353 EXPECT_TRUE(manager_->HaveUnrenderableTextures());
1354 // Check that we can still manipulate it and it effects the manager.
1355 manager_->SetLevelInfo(texture_ref.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 1,
1356 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1357 EXPECT_TRUE(manager_->CanRender(texture_ref.get()));
1358 EXPECT_FALSE(manager_->HaveUnrenderableTextures());
1359 EXPECT_CALL(*gl_, DeleteTextures(1, ::testing::Pointee(kService2Id)))
1360 .Times(1)
1361 .RetiresOnSaturation();
1362 texture_ref = NULL;
1365 TEST_F(TextureTest, GetLevelImage) {
1366 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1367 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1368 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1369 Texture* texture = texture_ref_->texture();
1370 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1371 // Set image.
1372 scoped_refptr<gfx::GLImage> image(new gfx::GLImageStub);
1373 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, image.get());
1374 EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1375 // Remove it.
1376 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, NULL);
1377 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1378 manager_->SetLevelImage(texture_ref_.get(), GL_TEXTURE_2D, 1, image.get());
1379 // Image should be reset when SetLevelInfo is called.
1380 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1381 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1382 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 1) == NULL);
1385 namespace {
1387 bool InSet(std::set<std::string>* string_set, const std::string& str) {
1388 std::pair<std::set<std::string>::iterator, bool> result =
1389 string_set->insert(str);
1390 return !result.second;
1393 } // anonymous namespace
1395 TEST_F(TextureTest, AddToSignature) {
1396 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1397 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1398 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1399 std::string signature1;
1400 std::string signature2;
1401 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature1);
1403 std::set<std::string> string_set;
1404 EXPECT_FALSE(InSet(&string_set, signature1));
1406 // check changing 1 thing makes a different signature.
1407 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 4, 2, 1,
1408 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(4, 2));
1409 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1410 EXPECT_FALSE(InSet(&string_set, signature2));
1412 // check putting it back makes the same signature.
1413 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1414 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(2, 2));
1415 signature2.clear();
1416 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1417 EXPECT_EQ(signature1, signature2);
1419 // Check setting cleared status does not change signature.
1420 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1421 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1422 signature2.clear();
1423 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1424 EXPECT_EQ(signature1, signature2);
1426 // Check changing other settings changes signature.
1427 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 4, 1,
1428 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1429 signature2.clear();
1430 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1431 EXPECT_FALSE(InSet(&string_set, signature2));
1433 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 2,
1434 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1435 signature2.clear();
1436 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1437 EXPECT_FALSE(InSet(&string_set, signature2));
1439 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1440 1, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1441 signature2.clear();
1442 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1443 EXPECT_FALSE(InSet(&string_set, signature2));
1445 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1446 0, GL_RGB, GL_UNSIGNED_BYTE, gfx::Rect());
1447 signature2.clear();
1448 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1449 EXPECT_FALSE(InSet(&string_set, signature2));
1451 manager_->SetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 1,
1452 0, GL_RGBA, GL_FLOAT, gfx::Rect());
1453 signature2.clear();
1454 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1455 EXPECT_FALSE(InSet(&string_set, signature2));
1457 // put it back
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 parameters changes signature.
1465 SetParameter(
1466 texture_ref_.get(), GL_TEXTURE_MIN_FILTER, GL_NEAREST, GL_NO_ERROR);
1467 signature2.clear();
1468 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1469 EXPECT_FALSE(InSet(&string_set, signature2));
1471 SetParameter(texture_ref_.get(),
1472 GL_TEXTURE_MIN_FILTER,
1473 GL_NEAREST_MIPMAP_LINEAR,
1474 GL_NO_ERROR);
1475 SetParameter(
1476 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_NEAREST, GL_NO_ERROR);
1477 signature2.clear();
1478 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1479 EXPECT_FALSE(InSet(&string_set, signature2));
1481 SetParameter(
1482 texture_ref_.get(), GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_NO_ERROR);
1483 SetParameter(
1484 texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
1485 signature2.clear();
1486 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1487 EXPECT_FALSE(InSet(&string_set, signature2));
1489 SetParameter(texture_ref_.get(), GL_TEXTURE_WRAP_S, GL_REPEAT, GL_NO_ERROR);
1490 SetParameter(
1491 texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE, GL_NO_ERROR);
1492 signature2.clear();
1493 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1494 EXPECT_FALSE(InSet(&string_set, signature2));
1496 // Check putting it back genenerates the same signature
1497 SetParameter(texture_ref_.get(), GL_TEXTURE_WRAP_T, GL_REPEAT, GL_NO_ERROR);
1498 signature2.clear();
1499 manager_->AddToSignature(texture_ref_.get(), GL_TEXTURE_2D, 1, &signature2);
1500 EXPECT_EQ(signature1, signature2);
1502 // Check the set was acutally getting different signatures.
1503 EXPECT_EQ(11u, string_set.size());
1506 class ProduceConsumeTextureTest : public TextureTest,
1507 public ::testing::WithParamInterface<GLenum> {
1508 public:
1509 void SetUp() override {
1510 TextureTest::SetUpBase(NULL, "GL_OES_EGL_image_external");
1511 manager_->CreateTexture(kClient2Id, kService2Id);
1512 texture2_ = manager_->GetTexture(kClient2Id);
1514 EXPECT_CALL(*decoder_.get(), GetErrorState())
1515 .WillRepeatedly(Return(error_state_.get()));
1518 void TearDown() override {
1519 if (texture2_.get()) {
1520 // If it's not in the manager then setting texture2_ to NULL will
1521 // delete the texture.
1522 if (!texture2_->client_id()) {
1523 // Check that it gets deleted when the last reference is released.
1524 EXPECT_CALL(
1525 *gl_,
1526 DeleteTextures(1, ::testing::Pointee(texture2_->service_id())))
1527 .Times(1).RetiresOnSaturation();
1529 texture2_ = NULL;
1531 TextureTest::TearDown();
1534 protected:
1535 struct LevelInfo {
1536 LevelInfo(GLenum target,
1537 GLenum format,
1538 GLsizei width,
1539 GLsizei height,
1540 GLsizei depth,
1541 GLint border,
1542 GLenum type,
1543 const gfx::Rect& cleared_rect)
1544 : target(target),
1545 format(format),
1546 width(width),
1547 height(height),
1548 depth(depth),
1549 border(border),
1550 type(type),
1551 cleared_rect(cleared_rect) {}
1553 LevelInfo()
1554 : target(0),
1555 format(0),
1556 width(-1),
1557 height(-1),
1558 depth(1),
1559 border(0),
1560 type(0) {}
1562 bool operator==(const LevelInfo& other) const {
1563 return target == other.target && format == other.format &&
1564 width == other.width && height == other.height &&
1565 depth == other.depth && border == other.border &&
1566 type == other.type && cleared_rect == other.cleared_rect;
1569 GLenum target;
1570 GLenum format;
1571 GLsizei width;
1572 GLsizei height;
1573 GLsizei depth;
1574 GLint border;
1575 GLenum type;
1576 gfx::Rect cleared_rect;
1579 void SetLevelInfo(TextureRef* texture_ref,
1580 GLint level,
1581 const LevelInfo& info) {
1582 manager_->SetLevelInfo(texture_ref, info.target, level, info.format,
1583 info.width, info.height, info.depth, info.border,
1584 info.format, info.type, info.cleared_rect);
1587 static LevelInfo GetLevelInfo(const TextureRef* texture_ref,
1588 GLint target,
1589 GLint level) {
1590 const Texture* texture = texture_ref->texture();
1591 LevelInfo info;
1592 info.target = target;
1593 EXPECT_TRUE(texture->GetLevelSize(target, level, &info.width,
1594 &info.height, &info.depth));
1595 EXPECT_TRUE(texture->GetLevelType(target, level, &info.type,
1596 &info.format));
1597 info.cleared_rect = texture->GetLevelClearedRect(target, level);
1598 return info;
1601 Texture* Produce(TextureRef* texture_ref) {
1602 Texture* texture = manager_->Produce(texture_ref);
1603 EXPECT_TRUE(texture != NULL);
1604 return texture;
1607 void Consume(GLuint client_id, Texture* texture) {
1608 EXPECT_TRUE(manager_->Consume(client_id, texture));
1611 scoped_refptr<TextureRef> texture2_;
1613 private:
1614 static const GLuint kClient2Id;
1615 static const GLuint kService2Id;
1618 const GLuint ProduceConsumeTextureTest::kClient2Id = 2;
1619 const GLuint ProduceConsumeTextureTest::kService2Id = 12;
1621 TEST_F(ProduceConsumeTextureTest, ProduceConsume2D) {
1622 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_2D);
1623 Texture* texture = texture_ref_->texture();
1624 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_2D), texture->target());
1625 LevelInfo level0(GL_TEXTURE_2D, GL_RGBA, 4, 4, 1, 0, GL_UNSIGNED_BYTE,
1626 gfx::Rect(4, 4));
1627 SetLevelInfo(texture_ref_.get(), 0, level0);
1628 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture_ref_.get()));
1629 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1630 LevelInfo level1 = GetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 1);
1631 LevelInfo level2 = GetLevelInfo(texture_ref_.get(), GL_TEXTURE_2D, 2);
1632 Texture* produced_texture = Produce(texture_ref_.get());
1633 EXPECT_EQ(produced_texture, texture);
1635 // Make this texture bigger with more levels, and make sure they get
1636 // clobbered correctly during Consume().
1637 manager_->SetTarget(texture2_.get(), GL_TEXTURE_2D);
1638 SetLevelInfo(texture2_.get(), 0, LevelInfo(GL_TEXTURE_2D, GL_RGBA, 16, 16, 1,
1639 0, GL_UNSIGNED_BYTE, gfx::Rect()));
1640 EXPECT_TRUE(manager_->MarkMipmapsGenerated(texture2_.get()));
1641 texture = texture2_->texture();
1642 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1643 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U, texture->estimated_size());
1645 GLuint client_id = texture2_->client_id();
1646 manager_->RemoveTexture(client_id);
1647 Consume(client_id, produced_texture);
1648 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1649 EXPECT_EQ(produced_texture, restored_texture->texture());
1650 EXPECT_EQ(level0, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 0));
1651 EXPECT_EQ(level1, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 1));
1652 EXPECT_EQ(level2, GetLevelInfo(restored_texture.get(), GL_TEXTURE_2D, 2));
1653 texture = restored_texture->texture();
1654 EXPECT_EQ(64U + 16U + 4U, texture->estimated_size());
1655 GLint w, h;
1656 EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 3, &w, &h, nullptr));
1658 // However the old texture ref still exists if it was referenced somewhere.
1659 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U,
1660 texture2_->texture()->estimated_size());
1663 TEST_F(ProduceConsumeTextureTest, ProduceConsumeClearRectangle) {
1664 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_RECTANGLE_ARB);
1665 Texture* texture = texture_ref_->texture();
1666 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB), texture->target());
1667 LevelInfo level0(GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, 1, 1, 1, 0,
1668 GL_UNSIGNED_BYTE, gfx::Rect());
1669 SetLevelInfo(texture_ref_.get(), 0, level0);
1670 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1671 Texture* produced_texture = Produce(texture_ref_.get());
1672 EXPECT_EQ(produced_texture, texture);
1673 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
1674 produced_texture->target());
1676 GLuint client_id = texture2_->client_id();
1677 manager_->RemoveTexture(client_id);
1678 Consume(client_id, produced_texture);
1679 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1680 EXPECT_EQ(produced_texture, restored_texture->texture());
1682 // See if we can clear the previously uncleared level now.
1683 EXPECT_EQ(level0,
1684 GetLevelInfo(restored_texture.get(), GL_TEXTURE_RECTANGLE_ARB, 0));
1685 EXPECT_CALL(*decoder_, ClearLevel(_, _, _, _, _, _, _, _, _))
1686 .WillRepeatedly(Return(true));
1687 EXPECT_TRUE(manager_->ClearTextureLevel(
1688 decoder_.get(), restored_texture.get(), GL_TEXTURE_RECTANGLE_ARB, 0));
1691 TEST_F(ProduceConsumeTextureTest, ProduceConsumeExternal) {
1692 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_EXTERNAL_OES);
1693 Texture* texture = texture_ref_->texture();
1694 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), texture->target());
1695 LevelInfo level0(GL_TEXTURE_EXTERNAL_OES, GL_RGBA, 1, 1, 1, 0,
1696 GL_UNSIGNED_BYTE, gfx::Rect());
1697 SetLevelInfo(texture_ref_.get(), 0, level0);
1698 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1699 Texture* produced_texture = Produce(texture_ref_.get());
1700 EXPECT_EQ(produced_texture, texture);
1702 GLuint client_id = texture2_->client_id();
1703 manager_->RemoveTexture(client_id);
1704 Consume(client_id, produced_texture);
1705 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1706 EXPECT_EQ(produced_texture, restored_texture->texture());
1707 EXPECT_EQ(level0,
1708 GetLevelInfo(restored_texture.get(), GL_TEXTURE_EXTERNAL_OES, 0));
1711 TEST_P(ProduceConsumeTextureTest, ProduceConsumeTextureWithImage) {
1712 GLenum target = GetParam();
1713 manager_->SetTarget(texture_ref_.get(), target);
1714 Texture* texture = texture_ref_->texture();
1715 EXPECT_EQ(static_cast<GLenum>(target), texture->target());
1716 scoped_refptr<gfx::GLImage> image(new gfx::GLImageStub);
1717 manager_->SetLevelInfo(texture_ref_.get(), target, 0, GL_RGBA, 0, 0, 1, 0,
1718 GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1719 manager_->SetLevelImage(texture_ref_.get(), target, 0, image.get());
1720 GLuint service_id = texture->service_id();
1721 Texture* produced_texture = Produce(texture_ref_.get());
1723 GLuint client_id = texture2_->client_id();
1724 manager_->RemoveTexture(client_id);
1725 Consume(client_id, produced_texture);
1726 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1727 EXPECT_EQ(produced_texture, restored_texture->texture());
1728 EXPECT_EQ(service_id, restored_texture->service_id());
1729 EXPECT_EQ(image.get(), restored_texture->texture()->GetLevelImage(target, 0));
1732 static const GLenum kTextureTargets[] = {GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL_OES,
1733 GL_TEXTURE_RECTANGLE_ARB, };
1735 INSTANTIATE_TEST_CASE_P(Target,
1736 ProduceConsumeTextureTest,
1737 ::testing::ValuesIn(kTextureTargets));
1739 TEST_F(ProduceConsumeTextureTest, ProduceConsumeCube) {
1740 manager_->SetTarget(texture_ref_.get(), GL_TEXTURE_CUBE_MAP);
1741 Texture* texture = texture_ref_->texture();
1742 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP), texture->target());
1743 LevelInfo face0(GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_RGBA, 1, 1, 1, 0,
1744 GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
1745 LevelInfo face5(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, GL_RGBA, 3, 3, 1, 0,
1746 GL_UNSIGNED_BYTE, gfx::Rect(1, 1));
1747 SetLevelInfo(texture_ref_.get(), 0, face0);
1748 SetLevelInfo(texture_ref_.get(), 0, face5);
1749 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture));
1750 Texture* produced_texture = Produce(texture_ref_.get());
1751 EXPECT_EQ(produced_texture, texture);
1753 GLuint client_id = texture2_->client_id();
1754 manager_->RemoveTexture(client_id);
1755 Consume(client_id, produced_texture);
1756 scoped_refptr<TextureRef> restored_texture = manager_->GetTexture(client_id);
1757 EXPECT_EQ(produced_texture, restored_texture->texture());
1758 EXPECT_EQ(
1759 face0,
1760 GetLevelInfo(restored_texture.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0));
1761 EXPECT_EQ(
1762 face5,
1763 GetLevelInfo(restored_texture.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0));
1766 class CountingMemoryTracker : public MemoryTracker {
1767 public:
1768 CountingMemoryTracker() {
1769 current_size_[0] = 0;
1770 current_size_[1] = 0;
1773 void TrackMemoryAllocatedChange(size_t old_size,
1774 size_t new_size,
1775 Pool pool) override {
1776 DCHECK_LT(static_cast<size_t>(pool), arraysize(current_size_));
1777 current_size_[pool] += new_size - old_size;
1780 bool EnsureGPUMemoryAvailable(size_t size_needed) override { return true; }
1782 size_t GetSize(Pool pool) {
1783 DCHECK_LT(static_cast<size_t>(pool), arraysize(current_size_));
1784 return current_size_[pool];
1787 uint64_t ClientTracingId() const override { return 0; }
1789 int ClientId() const override { return 0; }
1791 private:
1792 ~CountingMemoryTracker() override {}
1794 size_t current_size_[2];
1795 DISALLOW_COPY_AND_ASSIGN(CountingMemoryTracker);
1798 class SharedTextureTest : public GpuServiceTest {
1799 public:
1800 static const bool kUseDefaultTextures = false;
1802 SharedTextureTest() : feature_info_(new FeatureInfo()) {}
1804 ~SharedTextureTest() override {}
1806 void SetUp() override {
1807 GpuServiceTest::SetUp();
1808 memory_tracker1_ = new CountingMemoryTracker;
1809 texture_manager1_.reset(
1810 new TextureManager(memory_tracker1_.get(),
1811 feature_info_.get(),
1812 TextureManagerTest::kMaxTextureSize,
1813 TextureManagerTest::kMaxCubeMapTextureSize,
1814 TextureManagerTest::kMaxRectangleTextureSize,
1815 TextureManagerTest::kMax3DTextureSize,
1816 kUseDefaultTextures));
1817 memory_tracker2_ = new CountingMemoryTracker;
1818 texture_manager2_.reset(
1819 new TextureManager(memory_tracker2_.get(),
1820 feature_info_.get(),
1821 TextureManagerTest::kMaxTextureSize,
1822 TextureManagerTest::kMaxCubeMapTextureSize,
1823 TextureManagerTest::kMaxRectangleTextureSize,
1824 TextureManagerTest::kMax3DTextureSize,
1825 kUseDefaultTextures));
1826 TestHelper::SetupTextureManagerInitExpectations(
1827 gl_.get(), "", kUseDefaultTextures);
1828 texture_manager1_->Initialize();
1829 TestHelper::SetupTextureManagerInitExpectations(
1830 gl_.get(), "", kUseDefaultTextures);
1831 texture_manager2_->Initialize();
1834 void TearDown() override {
1835 texture_manager2_->Destroy(false);
1836 texture_manager2_.reset();
1837 texture_manager1_->Destroy(false);
1838 texture_manager1_.reset();
1839 GpuServiceTest::TearDown();
1842 protected:
1843 scoped_refptr<FeatureInfo> feature_info_;
1844 scoped_refptr<CountingMemoryTracker> memory_tracker1_;
1845 scoped_ptr<TextureManager> texture_manager1_;
1846 scoped_refptr<CountingMemoryTracker> memory_tracker2_;
1847 scoped_ptr<TextureManager> texture_manager2_;
1850 TEST_F(SharedTextureTest, DeleteTextures) {
1851 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1852 scoped_refptr<TextureRef> ref2 =
1853 texture_manager2_->Consume(20, ref1->texture());
1854 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1855 .Times(0);
1856 ref1 = NULL;
1857 texture_manager1_->RemoveTexture(10);
1858 testing::Mock::VerifyAndClearExpectations(gl_.get());
1860 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1861 .Times(1)
1862 .RetiresOnSaturation();
1863 ref2 = NULL;
1864 texture_manager2_->RemoveTexture(20);
1865 testing::Mock::VerifyAndClearExpectations(gl_.get());
1868 TEST_F(SharedTextureTest, TextureSafetyAccounting) {
1869 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
1870 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1871 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1872 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
1873 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1874 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1876 // Newly created texture is renderable.
1877 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1878 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
1879 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1880 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1882 // Associate new texture ref to other texture manager, should account for it
1883 // too.
1884 scoped_refptr<TextureRef> ref2 =
1885 texture_manager2_->Consume(20, ref1->texture());
1886 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
1887 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1888 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1890 // Make texture renderable but uncleared on one texture manager, should affect
1891 // other one.
1892 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
1893 EXPECT_TRUE(texture_manager1_->HaveUnrenderableTextures());
1894 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1895 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1896 EXPECT_TRUE(texture_manager2_->HaveUnrenderableTextures());
1897 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1898 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1900 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
1901 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1902 EXPECT_FALSE(texture_manager1_->HaveUnrenderableTextures());
1903 EXPECT_TRUE(texture_manager1_->HaveUnsafeTextures());
1904 EXPECT_TRUE(texture_manager1_->HaveUnclearedMips());
1905 EXPECT_FALSE(texture_manager2_->HaveUnrenderableTextures());
1906 EXPECT_TRUE(texture_manager2_->HaveUnsafeTextures());
1907 EXPECT_TRUE(texture_manager2_->HaveUnclearedMips());
1909 // Make texture cleared on one texture manager, should affect other one.
1910 texture_manager1_->SetLevelCleared(ref1.get(), GL_TEXTURE_2D, 0, true);
1911 EXPECT_FALSE(texture_manager1_->HaveUnsafeTextures());
1912 EXPECT_FALSE(texture_manager1_->HaveUnclearedMips());
1913 EXPECT_FALSE(texture_manager2_->HaveUnsafeTextures());
1914 EXPECT_FALSE(texture_manager2_->HaveUnclearedMips());
1916 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1917 .Times(1)
1918 .RetiresOnSaturation();
1919 texture_manager1_->RemoveTexture(10);
1920 texture_manager2_->RemoveTexture(20);
1923 TEST_F(SharedTextureTest, FBOCompletenessCheck) {
1924 const GLenum kCompleteValue = GL_FRAMEBUFFER_COMPLETE;
1925 FramebufferManager framebuffer_manager1(
1926 1, 1, ContextGroup::CONTEXT_TYPE_UNDEFINED);
1927 texture_manager1_->set_framebuffer_manager(&framebuffer_manager1);
1928 FramebufferManager framebuffer_manager2(
1929 1, 1, ContextGroup::CONTEXT_TYPE_UNDEFINED);
1930 texture_manager2_->set_framebuffer_manager(&framebuffer_manager2);
1932 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1933 framebuffer_manager1.CreateFramebuffer(10, 10);
1934 scoped_refptr<Framebuffer> framebuffer1 =
1935 framebuffer_manager1.GetFramebuffer(10);
1936 framebuffer1->AttachTexture(
1937 GL_COLOR_ATTACHMENT0, ref1.get(), GL_TEXTURE_2D, 0, 0);
1938 EXPECT_FALSE(framebuffer_manager1.IsComplete(framebuffer1.get()));
1939 EXPECT_NE(kCompleteValue, framebuffer1->IsPossiblyComplete());
1941 // Make FBO complete in manager 1.
1942 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
1943 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
1944 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1945 gfx::Rect(1, 1));
1946 EXPECT_EQ(kCompleteValue, framebuffer1->IsPossiblyComplete());
1947 framebuffer_manager1.MarkAsComplete(framebuffer1.get());
1948 EXPECT_TRUE(framebuffer_manager1.IsComplete(framebuffer1.get()));
1950 // Share texture with manager 2.
1951 scoped_refptr<TextureRef> ref2 =
1952 texture_manager2_->Consume(20, ref1->texture());
1953 framebuffer_manager2.CreateFramebuffer(20, 20);
1954 scoped_refptr<Framebuffer> framebuffer2 =
1955 framebuffer_manager2.GetFramebuffer(20);
1956 framebuffer2->AttachTexture(
1957 GL_COLOR_ATTACHMENT0, ref2.get(), GL_TEXTURE_2D, 0, 0);
1958 EXPECT_FALSE(framebuffer_manager2.IsComplete(framebuffer2.get()));
1959 EXPECT_EQ(kCompleteValue, framebuffer2->IsPossiblyComplete());
1960 framebuffer_manager2.MarkAsComplete(framebuffer2.get());
1961 EXPECT_TRUE(framebuffer_manager2.IsComplete(framebuffer2.get()));
1963 // Change level for texture, both FBOs should be marked incomplete
1964 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
1965 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1966 gfx::Rect(1, 1));
1967 EXPECT_FALSE(framebuffer_manager1.IsComplete(framebuffer1.get()));
1968 EXPECT_EQ(kCompleteValue, framebuffer1->IsPossiblyComplete());
1969 framebuffer_manager1.MarkAsComplete(framebuffer1.get());
1970 EXPECT_TRUE(framebuffer_manager1.IsComplete(framebuffer1.get()));
1971 EXPECT_FALSE(framebuffer_manager2.IsComplete(framebuffer2.get()));
1972 EXPECT_EQ(kCompleteValue, framebuffer2->IsPossiblyComplete());
1973 framebuffer_manager2.MarkAsComplete(framebuffer2.get());
1974 EXPECT_TRUE(framebuffer_manager2.IsComplete(framebuffer2.get()));
1976 EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, _))
1977 .Times(2)
1978 .RetiresOnSaturation();
1979 framebuffer_manager1.RemoveFramebuffer(10);
1980 framebuffer_manager2.RemoveFramebuffer(20);
1981 EXPECT_CALL(*gl_, DeleteTextures(1, _))
1982 .Times(1)
1983 .RetiresOnSaturation();
1984 texture_manager1_->RemoveTexture(10);
1985 texture_manager2_->RemoveTexture(20);
1988 TEST_F(SharedTextureTest, Memory) {
1989 size_t initial_memory1 = memory_tracker1_->GetSize(MemoryTracker::kUnmanaged);
1990 size_t initial_memory2 = memory_tracker2_->GetSize(MemoryTracker::kUnmanaged);
1992 // Newly created texture is unrenderable.
1993 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
1994 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
1995 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 0, GL_RGBA, 10, 10,
1996 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect());
1998 EXPECT_LT(0u, ref1->texture()->estimated_size());
1999 EXPECT_EQ(initial_memory1 + ref1->texture()->estimated_size(),
2000 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2002 // Associate new texture ref to other texture manager, it doesn't account for
2003 // the texture memory, the first memory tracker still has it.
2004 scoped_refptr<TextureRef> ref2 =
2005 texture_manager2_->Consume(20, ref1->texture());
2006 EXPECT_EQ(initial_memory1 + ref1->texture()->estimated_size(),
2007 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2008 EXPECT_EQ(initial_memory2,
2009 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2011 // Delete the texture, memory should go to the remaining tracker.
2012 texture_manager1_->RemoveTexture(10);
2013 ref1 = NULL;
2014 EXPECT_EQ(initial_memory1,
2015 memory_tracker1_->GetSize(MemoryTracker::kUnmanaged));
2016 EXPECT_EQ(initial_memory2 + ref2->texture()->estimated_size(),
2017 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2019 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2020 .Times(1)
2021 .RetiresOnSaturation();
2022 ref2 = NULL;
2023 texture_manager2_->RemoveTexture(20);
2024 EXPECT_EQ(initial_memory2,
2025 memory_tracker2_->GetSize(MemoryTracker::kUnmanaged));
2028 TEST_F(SharedTextureTest, Images) {
2029 scoped_refptr<TextureRef> ref1 = texture_manager1_->CreateTexture(10, 10);
2030 scoped_refptr<TextureRef> ref2 =
2031 texture_manager2_->Consume(20, ref1->texture());
2033 texture_manager1_->SetTarget(ref1.get(), GL_TEXTURE_2D);
2034 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2,
2035 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2036 gfx::Rect(2, 2));
2037 EXPECT_FALSE(ref1->texture()->HasImages());
2038 EXPECT_FALSE(ref2->texture()->HasImages());
2039 EXPECT_FALSE(texture_manager1_->HaveImages());
2040 EXPECT_FALSE(texture_manager2_->HaveImages());
2041 scoped_refptr<gfx::GLImage> image1(new gfx::GLImageStub);
2042 texture_manager1_->SetLevelImage(ref1.get(), GL_TEXTURE_2D, 1, image1.get());
2043 EXPECT_TRUE(ref1->texture()->HasImages());
2044 EXPECT_TRUE(ref2->texture()->HasImages());
2045 EXPECT_TRUE(texture_manager1_->HaveImages());
2046 EXPECT_TRUE(texture_manager2_->HaveImages());
2047 scoped_refptr<gfx::GLImage> image2(new gfx::GLImageStub);
2048 texture_manager1_->SetLevelImage(ref1.get(), GL_TEXTURE_2D, 1, image2.get());
2049 EXPECT_TRUE(ref1->texture()->HasImages());
2050 EXPECT_TRUE(ref2->texture()->HasImages());
2051 EXPECT_TRUE(texture_manager1_->HaveImages());
2052 EXPECT_TRUE(texture_manager2_->HaveImages());
2053 texture_manager1_->SetLevelInfo(ref1.get(), GL_TEXTURE_2D, 1, GL_RGBA, 2, 2,
2054 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2055 gfx::Rect(2, 2));
2056 EXPECT_FALSE(ref1->texture()->HasImages());
2057 EXPECT_FALSE(ref2->texture()->HasImages());
2058 EXPECT_FALSE(texture_manager1_->HaveImages());
2059 EXPECT_FALSE(texture_manager1_->HaveImages());
2061 EXPECT_CALL(*gl_, DeleteTextures(1, _))
2062 .Times(1)
2063 .RetiresOnSaturation();
2064 texture_manager1_->RemoveTexture(10);
2065 texture_manager2_->RemoveTexture(20);
2069 class TextureFormatTypeValidationTest : public TextureManagerTest {
2070 public:
2071 TextureFormatTypeValidationTest() {}
2072 ~TextureFormatTypeValidationTest() override {}
2074 protected:
2075 void SetupFeatureInfo(const char* gl_extensions, const char* gl_version) {
2076 TestHelper::SetupFeatureInfoInitExpectationsWithGLVersion(
2077 gl_.get(), gl_extensions, "", gl_version);
2078 feature_info_->Initialize();
2081 void ExpectValid(GLenum format, GLenum type, GLenum internal_format) {
2082 EXPECT_TRUE(manager_->ValidateTextureParameters(
2083 error_state_.get(), "", format, type, internal_format, 0));
2086 void ExpectInvalid(GLenum format, GLenum type, GLenum internal_format) {
2087 EXPECT_CALL(*error_state_,
2088 SetGLError(_, _, _, _, _))
2089 .Times(1)
2090 .RetiresOnSaturation();
2091 EXPECT_FALSE(manager_->ValidateTextureParameters(
2092 error_state_.get(), "", format, type, internal_format, 0));
2095 void ExpectInvalidEnum(GLenum format, GLenum type, GLenum internal_format) {
2096 EXPECT_CALL(*error_state_,
2097 SetGLErrorInvalidEnum(_, _, _, _, _))
2098 .Times(1)
2099 .RetiresOnSaturation();
2100 EXPECT_FALSE(manager_->ValidateTextureParameters(
2101 error_state_.get(), "", format, type, internal_format, 0));
2105 TEST_F(TextureFormatTypeValidationTest, ES2Basic) {
2106 SetupFeatureInfo("", "OpenGL ES 2.0");
2108 ExpectValid(GL_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA);
2109 ExpectValid(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB);
2110 ExpectValid(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, GL_RGBA);
2111 ExpectValid(GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE);
2112 ExpectValid(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_LUMINANCE_ALPHA);
2114 ExpectInvalid(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA);
2116 // float / half_float.
2117 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2118 ExpectInvalidEnum(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2120 // GL_EXT_bgra
2121 ExpectInvalidEnum(GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_BGRA_EXT);
2123 // depth / stencil
2124 ExpectInvalidEnum(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2125 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2127 // SRGB
2128 ExpectInvalidEnum(GL_SRGB_EXT, GL_UNSIGNED_BYTE, GL_SRGB_EXT);
2129 ExpectInvalidEnum(GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, GL_SRGB_ALPHA_EXT);
2131 // ES3
2132 ExpectInvalidEnum(GL_RGB, GL_UNSIGNED_BYTE, GL_RGB8);
2135 TEST_F(TextureFormatTypeValidationTest, ES2WithExtTextureFormatBGRA8888) {
2136 SetupFeatureInfo("GL_EXT_texture_format_BGRA8888", "OpenGL ES 2.0");
2138 ExpectValid(GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_BGRA_EXT);
2141 TEST_F(TextureFormatTypeValidationTest, ES2WithAppleTextureFormatBGRA8888) {
2142 SetupFeatureInfo("GL_APPLE_texture_format_BGRA8888", "OpenGL ES 2.0");
2144 ExpectValid(GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_BGRA_EXT);
2147 TEST_F(TextureFormatTypeValidationTest, ES2WithArbDepth) {
2148 SetupFeatureInfo("GL_ARB_depth_texture", "OpenGL ES 2.0");
2150 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2151 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2152 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2155 TEST_F(TextureFormatTypeValidationTest, ES2WithOesDepth) {
2156 SetupFeatureInfo("GL_OES_depth_texture", "OpenGL ES 2.0");
2158 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2159 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2160 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2163 TEST_F(TextureFormatTypeValidationTest, ES2WithAngleDepth) {
2164 SetupFeatureInfo("GL_ANGLE_depth_texture", "OpenGL ES 2.0");
2166 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2167 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2168 ExpectInvalidEnum(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2171 TEST_F(TextureFormatTypeValidationTest, ES2WithExtPackedDepthStencil) {
2172 SetupFeatureInfo(
2173 "GL_EXT_packed_depth_stencil GL_ARB_depth_texture", "OpenGL ES 2.0");
2175 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT);
2176 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT);
2177 ExpectValid(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL);
2180 TEST_F(TextureFormatTypeValidationTest, ES2WithRGWithFloat) {
2181 SetupFeatureInfo(
2182 "GL_EXT_texture_rg GL_OES_texture_float GL_OES_texture_half_float",
2183 "OpenGL ES 2.0");
2185 ExpectValid(GL_RED_EXT, GL_HALF_FLOAT_OES, GL_RED_EXT);
2186 ExpectValid(GL_RG_EXT, GL_HALF_FLOAT_OES, GL_RG_EXT);
2187 ExpectValid(GL_RED_EXT, GL_UNSIGNED_BYTE, GL_RED_EXT);
2188 ExpectValid(GL_RG_EXT, GL_UNSIGNED_BYTE, GL_RG_EXT);
2190 ExpectInvalidEnum(GL_RED_EXT, GL_BYTE, GL_RED_EXT);
2191 ExpectInvalidEnum(GL_RG_EXT, GL_BYTE, GL_RG_EXT);
2192 ExpectInvalidEnum(GL_RED_EXT, GL_SHORT, GL_RED_EXT);
2193 ExpectInvalidEnum(GL_RG_EXT, GL_SHORT, GL_RG_EXT);
2196 TEST_F(TextureFormatTypeValidationTest, ES2WithRGNoFloat) {
2197 SetupFeatureInfo("GL_ARB_texture_rg", "OpenGL ES 2.0");
2199 ExpectValid(GL_RED_EXT, GL_UNSIGNED_BYTE, GL_RED_EXT);
2200 ExpectValid(GL_RG_EXT, GL_UNSIGNED_BYTE, GL_RG_EXT);
2202 ExpectInvalidEnum(GL_RED_EXT, GL_HALF_FLOAT_OES, GL_RED_EXT);
2203 ExpectInvalidEnum(GL_RG_EXT, GL_HALF_FLOAT_OES, GL_RG_EXT);
2206 TEST_F(TextureFormatTypeValidationTest, ES2OnTopOfES3) {
2207 SetupFeatureInfo("", "OpenGL ES 3.0");
2209 ExpectInvalidEnum(GL_RGB, GL_FLOAT, GL_RGB);
2210 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2211 ExpectInvalidEnum(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2212 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2213 ExpectInvalidEnum(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2215 ExpectInvalidEnum(GL_SRGB_EXT, GL_UNSIGNED_BYTE, GL_SRGB_EXT);
2216 ExpectInvalidEnum(GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, GL_SRGB_ALPHA_EXT);
2219 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureFloat) {
2220 SetupFeatureInfo("GL_OES_texture_float", "OpenGL ES 2.0");
2222 ExpectValid(GL_RGB, GL_FLOAT, GL_RGB);
2223 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA);
2224 ExpectValid(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2225 ExpectValid(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2226 ExpectValid(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2228 ExpectInvalidEnum(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2229 ExpectInvalidEnum(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2230 ExpectInvalidEnum(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2231 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2232 ExpectInvalidEnum(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2235 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureFloatLinear) {
2236 SetupFeatureInfo(
2237 "GL_OES_texture_float GL_OES_texture_float_linear", "OpenGL ES 2.0");
2239 ExpectValid(GL_RGB, GL_FLOAT, GL_RGB);
2240 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA);
2241 ExpectValid(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2242 ExpectValid(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2243 ExpectValid(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2245 ExpectInvalidEnum(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2246 ExpectInvalidEnum(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2247 ExpectInvalidEnum(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2248 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2249 ExpectInvalidEnum(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2252 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureHalfFloat) {
2253 SetupFeatureInfo("GL_OES_texture_half_float", "OpenGL ES 2.0");
2255 ExpectValid(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2256 ExpectValid(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2257 ExpectValid(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2258 ExpectValid(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2259 ExpectValid(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2261 ExpectInvalidEnum(GL_RGB, GL_FLOAT, GL_RGB);
2262 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2263 ExpectInvalidEnum(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2264 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2265 ExpectInvalidEnum(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2268 TEST_F(TextureFormatTypeValidationTest, ES2WithOesTextureHalfFloatLinear) {
2269 SetupFeatureInfo(
2270 "GL_OES_texture_half_float GL_OES_texture_half_float_linear",
2271 "OpenGL ES 2.0");
2273 ExpectValid(GL_RGB, GL_HALF_FLOAT_OES, GL_RGB);
2274 ExpectValid(GL_RGBA, GL_HALF_FLOAT_OES, GL_RGBA);
2275 ExpectValid(GL_LUMINANCE, GL_HALF_FLOAT_OES, GL_LUMINANCE);
2276 ExpectValid(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_LUMINANCE_ALPHA);
2277 ExpectValid(GL_ALPHA, GL_HALF_FLOAT_OES, GL_ALPHA);
2279 ExpectInvalidEnum(GL_RGB, GL_FLOAT, GL_RGB);
2280 ExpectInvalidEnum(GL_RGBA, GL_FLOAT, GL_RGBA);
2281 ExpectInvalidEnum(GL_LUMINANCE, GL_FLOAT, GL_LUMINANCE);
2282 ExpectInvalidEnum(GL_LUMINANCE_ALPHA, GL_FLOAT, GL_LUMINANCE_ALPHA);
2283 ExpectInvalidEnum(GL_ALPHA, GL_FLOAT, GL_ALPHA);
2286 TEST_F(TextureFormatTypeValidationTest, ES3Basic) {
2287 SetupFeatureInfo("", "OpenGL ES 3.0");
2288 EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_COLOR_ATTACHMENTS, _))
2289 .WillOnce(SetArgPointee<1>(8))
2290 .RetiresOnSaturation();
2291 EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_DRAW_BUFFERS, _))
2292 .WillOnce(SetArgPointee<1>(8))
2293 .RetiresOnSaturation();
2294 feature_info_->EnableES3Validators();
2296 ExpectValid(GL_ALPHA, GL_UNSIGNED_BYTE, GL_ALPHA);
2297 ExpectValid(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB);
2298 ExpectValid(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, GL_RGBA);
2299 ExpectValid(GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE);
2300 ExpectValid(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_LUMINANCE_ALPHA);
2302 ExpectValid(GL_RG, GL_BYTE, GL_RG8_SNORM);
2303 ExpectValid(GL_RG_INTEGER, GL_UNSIGNED_INT, GL_RG32UI);
2304 ExpectValid(GL_RG_INTEGER, GL_SHORT, GL_RG16I);
2305 ExpectValid(GL_RGB, GL_UNSIGNED_BYTE, GL_SRGB8);
2306 ExpectValid(GL_RGBA, GL_HALF_FLOAT, GL_RGBA16F);
2307 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA16F);
2308 ExpectValid(GL_RGBA, GL_FLOAT, GL_RGBA32F);
2310 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_COMPONENT16);
2311 ExpectValid(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_DEPTH_COMPONENT24);
2312 ExpectValid(GL_DEPTH_COMPONENT, GL_FLOAT, GL_DEPTH_COMPONENT32F);
2313 ExpectValid(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH24_STENCIL8);
2314 ExpectValid(GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
2315 GL_DEPTH32F_STENCIL8);
2317 ExpectInvalid(GL_RGB_INTEGER, GL_INT, GL_RGBA8);
2320 } // namespace gles2
2321 } // namespace gpu