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"
9 #include "base/memory/scoped_ptr.h"
10 #include "gpu/command_buffer/service/error_state_mock.h"
11 #include "gpu/command_buffer/service/feature_info.h"
12 #include "gpu/command_buffer/service/framebuffer_manager.h"
13 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h"
14 #include "gpu/command_buffer/service/gpu_service_test.h"
15 #include "gpu/command_buffer/service/mailbox_manager.h"
16 #include "gpu/command_buffer/service/memory_tracking.h"
17 #include "gpu/command_buffer/service/mocks.h"
18 #include "gpu/command_buffer/service/test_helper.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gl/gl_image_stub.h"
21 #include "ui/gl/gl_mock.h"
23 using ::testing::AtLeast
;
24 using ::testing::Pointee
;
25 using ::testing::Return
;
26 using ::testing::SetArgumentPointee
;
27 using ::testing::StrictMock
;
33 class TextureTestHelper
{
35 static bool IsNPOT(const Texture
* texture
) {
36 return texture
->npot();
38 static bool IsTextureComplete(const Texture
* texture
) {
39 return texture
->texture_complete();
41 static bool IsCubeComplete(const Texture
* texture
) {
42 return texture
->cube_complete();
46 class TextureManagerTest
: public GpuServiceTest
{
48 static const GLint kMaxTextureSize
= 16;
49 static const GLint kMaxCubeMapTextureSize
= 8;
50 static const GLint kMaxRectangleTextureSize
= 16;
51 static const GLint kMaxExternalTextureSize
= 16;
52 static const GLint kMax2dLevels
= 5;
53 static const GLint kMaxCubeMapLevels
= 4;
54 static const GLint kMaxExternalLevels
= 1;
55 static const bool kUseDefaultTextures
= false;
57 TextureManagerTest() : feature_info_(new FeatureInfo()) {}
59 ~TextureManagerTest() override
{}
62 void SetUp() override
{
63 GpuServiceTest::SetUp();
64 manager_
.reset(new TextureManager(NULL
,
67 kMaxCubeMapTextureSize
,
68 kMaxRectangleTextureSize
,
69 kUseDefaultTextures
));
70 TestHelper::SetupTextureManagerInitExpectations(
71 gl_
.get(), "", kUseDefaultTextures
);
72 manager_
->Initialize();
73 error_state_
.reset(new ::testing::StrictMock
<gles2::MockErrorState
>());
76 void TearDown() override
{
77 manager_
->Destroy(false);
79 GpuServiceTest::TearDown();
83 TextureRef
* texture_ref
, GLenum pname
, GLint value
, GLenum error
) {
84 TestHelper::SetTexParameteriWithExpectations(
85 gl_
.get(), error_state_
.get(), manager_
.get(),
86 texture_ref
, pname
, value
, error
);
89 scoped_refptr
<FeatureInfo
> feature_info_
;
90 scoped_ptr
<TextureManager
> manager_
;
91 scoped_ptr
<MockErrorState
> error_state_
;
94 // GCC requires these declarations, but MSVC requires they not be present
96 const GLint
TextureManagerTest::kMaxTextureSize
;
97 const GLint
TextureManagerTest::kMaxCubeMapTextureSize
;
98 const GLint
TextureManagerTest::kMaxRectangleTextureSize
;
99 const GLint
TextureManagerTest::kMaxExternalTextureSize
;
100 const GLint
TextureManagerTest::kMax2dLevels
;
101 const GLint
TextureManagerTest::kMaxCubeMapLevels
;
102 const GLint
TextureManagerTest::kMaxExternalLevels
;
105 TEST_F(TextureManagerTest
, Basic
) {
106 const GLuint kClient1Id
= 1;
107 const GLuint kService1Id
= 11;
108 const GLuint kClient2Id
= 2;
109 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
110 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
111 EXPECT_FALSE(manager_
->HaveUnclearedMips());
112 // Check we can create texture.
113 manager_
->CreateTexture(kClient1Id
, kService1Id
);
114 // Check texture got created.
115 scoped_refptr
<TextureRef
> texture
= manager_
->GetTexture(kClient1Id
);
116 ASSERT_TRUE(texture
.get() != NULL
);
117 EXPECT_EQ(kService1Id
, texture
->service_id());
118 EXPECT_EQ(kClient1Id
, texture
->client_id());
119 EXPECT_EQ(texture
->texture(), manager_
->GetTextureForServiceId(
120 texture
->service_id()));
121 // Check we get nothing for a non-existent texture.
122 EXPECT_TRUE(manager_
->GetTexture(kClient2Id
) == NULL
);
123 // Check trying to a remove non-existent textures does not crash.
124 manager_
->RemoveTexture(kClient2Id
);
125 // Check that it gets deleted when the last reference is released.
126 EXPECT_CALL(*gl_
, DeleteTextures(1, ::testing::Pointee(kService1Id
)))
128 .RetiresOnSaturation();
129 // Check we can't get the texture after we remove it.
130 manager_
->RemoveTexture(kClient1Id
);
131 EXPECT_TRUE(manager_
->GetTexture(kClient1Id
) == NULL
);
132 EXPECT_EQ(0u, texture
->client_id());
135 TEST_F(TextureManagerTest
, SetParameter
) {
136 const GLuint kClient1Id
= 1;
137 const GLuint kService1Id
= 11;
138 // Check we can create texture.
139 manager_
->CreateTexture(kClient1Id
, kService1Id
);
140 // Check texture got created.
141 TextureRef
* texture_ref
= manager_
->GetTexture(kClient1Id
);
142 ASSERT_TRUE(texture_ref
!= NULL
);
143 Texture
* texture
= texture_ref
->texture();
144 manager_
->SetTarget(texture_ref
, GL_TEXTURE_2D
);
145 SetParameter(texture_ref
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
, GL_NO_ERROR
);
146 EXPECT_EQ(static_cast<GLenum
>(GL_NEAREST
), texture
->min_filter());
147 SetParameter(texture_ref
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
, GL_NO_ERROR
);
148 EXPECT_EQ(static_cast<GLenum
>(GL_NEAREST
), texture
->mag_filter());
149 SetParameter(texture_ref
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
, GL_NO_ERROR
);
150 EXPECT_EQ(static_cast<GLenum
>(GL_CLAMP_TO_EDGE
), texture
->wrap_s());
151 SetParameter(texture_ref
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
, GL_NO_ERROR
);
152 EXPECT_EQ(static_cast<GLenum
>(GL_CLAMP_TO_EDGE
), texture
->wrap_t());
153 SetParameter(texture_ref
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, 1, GL_NO_ERROR
);
154 SetParameter(texture_ref
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, 2, GL_NO_ERROR
);
156 texture_ref
, GL_TEXTURE_MIN_FILTER
, GL_CLAMP_TO_EDGE
, GL_INVALID_ENUM
);
157 EXPECT_EQ(static_cast<GLenum
>(GL_NEAREST
), texture
->min_filter());
159 texture_ref
, GL_TEXTURE_MAG_FILTER
, GL_CLAMP_TO_EDGE
, GL_INVALID_ENUM
);
160 EXPECT_EQ(static_cast<GLenum
>(GL_NEAREST
), texture
->min_filter());
161 SetParameter(texture_ref
, GL_TEXTURE_WRAP_S
, GL_NEAREST
, GL_INVALID_ENUM
);
162 EXPECT_EQ(static_cast<GLenum
>(GL_CLAMP_TO_EDGE
), texture
->wrap_s());
163 SetParameter(texture_ref
, GL_TEXTURE_WRAP_T
, GL_NEAREST
, GL_INVALID_ENUM
);
164 EXPECT_EQ(static_cast<GLenum
>(GL_CLAMP_TO_EDGE
), texture
->wrap_t());
165 SetParameter(texture_ref
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, 0, GL_INVALID_VALUE
);
168 TEST_F(TextureManagerTest
, UseDefaultTexturesTrue
) {
169 bool use_default_textures
= true;
170 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
172 TestHelper::SetupTextureManagerInitExpectations(
173 gl_
.get(), "GL_ANGLE_texture_usage", use_default_textures
);
174 TextureManager
manager(NULL
,
177 kMaxCubeMapTextureSize
,
178 kMaxRectangleTextureSize
,
179 use_default_textures
);
180 manager
.Initialize();
182 EXPECT_TRUE(manager
.GetDefaultTextureInfo(GL_TEXTURE_2D
) != NULL
);
183 EXPECT_TRUE(manager
.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP
) != NULL
);
185 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
187 manager
.Destroy(false);
190 TEST_F(TextureManagerTest
, UseDefaultTexturesFalse
) {
191 bool use_default_textures
= false;
192 TestHelper::SetupTextureManagerInitExpectations(
193 gl_
.get(), "GL_ANGLE_texture_usage", use_default_textures
);
194 TextureManager
manager(NULL
,
197 kMaxCubeMapTextureSize
,
198 kMaxRectangleTextureSize
,
199 use_default_textures
);
200 manager
.Initialize();
202 EXPECT_TRUE(manager
.GetDefaultTextureInfo(GL_TEXTURE_2D
) == NULL
);
203 EXPECT_TRUE(manager
.GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP
) == NULL
);
205 // TODO(vmiura): Test GL_TEXTURE_EXTERNAL_OES & GL_TEXTURE_RECTANGLE_ARB.
207 manager
.Destroy(false);
210 TEST_F(TextureManagerTest
, TextureUsageExt
) {
211 TestHelper::SetupTextureManagerInitExpectations(
212 gl_
.get(), "GL_ANGLE_texture_usage", kUseDefaultTextures
);
213 TextureManager
manager(NULL
,
216 kMaxCubeMapTextureSize
,
217 kMaxRectangleTextureSize
,
218 kUseDefaultTextures
);
219 manager
.Initialize();
220 const GLuint kClient1Id
= 1;
221 const GLuint kService1Id
= 11;
222 // Check we can create texture.
223 manager
.CreateTexture(kClient1Id
, kService1Id
);
224 // Check texture got created.
225 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
226 ASSERT_TRUE(texture_ref
!= NULL
);
227 TestHelper::SetTexParameteriWithExpectations(
228 gl_
.get(), error_state_
.get(), &manager
, texture_ref
,
229 GL_TEXTURE_USAGE_ANGLE
, GL_FRAMEBUFFER_ATTACHMENT_ANGLE
, GL_NO_ERROR
);
230 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_ATTACHMENT_ANGLE
),
231 texture_ref
->texture()->usage());
232 manager
.Destroy(false);
235 TEST_F(TextureManagerTest
, Destroy
) {
236 const GLuint kClient1Id
= 1;
237 const GLuint kService1Id
= 11;
238 TestHelper::SetupTextureManagerInitExpectations(
239 gl_
.get(), "", kUseDefaultTextures
);
240 TextureManager
manager(NULL
,
243 kMaxCubeMapTextureSize
,
244 kMaxRectangleTextureSize
,
245 kUseDefaultTextures
);
246 manager
.Initialize();
247 // Check we can create texture.
248 manager
.CreateTexture(kClient1Id
, kService1Id
);
249 // Check texture got created.
250 TextureRef
* texture
= manager
.GetTexture(kClient1Id
);
251 ASSERT_TRUE(texture
!= NULL
);
252 EXPECT_CALL(*gl_
, DeleteTextures(1, ::testing::Pointee(kService1Id
)))
254 .RetiresOnSaturation();
255 TestHelper::SetupTextureManagerDestructionExpectations(
256 gl_
.get(), "", kUseDefaultTextures
);
257 manager
.Destroy(true);
258 // Check that resources got freed.
259 texture
= manager
.GetTexture(kClient1Id
);
260 ASSERT_TRUE(texture
== NULL
);
263 TEST_F(TextureManagerTest
, MaxValues
) {
264 // Check we get the right values for the max sizes.
265 EXPECT_EQ(kMax2dLevels
, manager_
->MaxLevelsForTarget(GL_TEXTURE_2D
));
266 EXPECT_EQ(kMaxCubeMapLevels
,
267 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP
));
268 EXPECT_EQ(kMaxCubeMapLevels
,
269 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X
));
270 EXPECT_EQ(kMaxCubeMapLevels
,
271 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X
));
272 EXPECT_EQ(kMaxCubeMapLevels
,
273 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y
));
274 EXPECT_EQ(kMaxCubeMapLevels
,
275 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
));
276 EXPECT_EQ(kMaxCubeMapLevels
,
277 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z
));
278 EXPECT_EQ(kMaxCubeMapLevels
,
279 manager_
->MaxLevelsForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
));
280 EXPECT_EQ(kMaxExternalLevels
,
281 manager_
->MaxLevelsForTarget(GL_TEXTURE_EXTERNAL_OES
));
282 EXPECT_EQ(kMaxTextureSize
, manager_
->MaxSizeForTarget(GL_TEXTURE_2D
));
283 EXPECT_EQ(kMaxCubeMapTextureSize
,
284 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP
));
285 EXPECT_EQ(kMaxCubeMapTextureSize
,
286 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X
));
287 EXPECT_EQ(kMaxCubeMapTextureSize
,
288 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_X
));
289 EXPECT_EQ(kMaxCubeMapTextureSize
,
290 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Y
));
291 EXPECT_EQ(kMaxCubeMapTextureSize
,
292 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
));
293 EXPECT_EQ(kMaxCubeMapTextureSize
,
294 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_Z
));
295 EXPECT_EQ(kMaxCubeMapTextureSize
,
296 manager_
->MaxSizeForTarget(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
));
297 EXPECT_EQ(kMaxRectangleTextureSize
,
298 manager_
->MaxSizeForTarget(GL_TEXTURE_RECTANGLE_ARB
));
299 EXPECT_EQ(kMaxExternalTextureSize
,
300 manager_
->MaxSizeForTarget(GL_TEXTURE_EXTERNAL_OES
));
303 TEST_F(TextureManagerTest
, ValidForTarget
) {
305 EXPECT_TRUE(manager_
->ValidForTarget(
306 GL_TEXTURE_2D
, 0, kMaxTextureSize
, kMaxTextureSize
, 1));
307 EXPECT_TRUE(manager_
->ValidForTarget(
308 GL_TEXTURE_2D
, kMax2dLevels
- 1, 1, 1, 1));
309 EXPECT_FALSE(manager_
->ValidForTarget(
310 GL_TEXTURE_2D
, kMax2dLevels
- 1, 1, 2, 1));
311 EXPECT_FALSE(manager_
->ValidForTarget(
312 GL_TEXTURE_2D
, kMax2dLevels
- 1, 2, 1, 1));
313 // check level out of range.
314 EXPECT_FALSE(manager_
->ValidForTarget(
315 GL_TEXTURE_2D
, kMax2dLevels
, kMaxTextureSize
, 1, 1));
317 EXPECT_FALSE(manager_
->ValidForTarget(
318 GL_TEXTURE_2D
, kMax2dLevels
, kMaxTextureSize
, 1, 2));
319 // Check NPOT width on level 0
320 EXPECT_TRUE(manager_
->ValidForTarget(GL_TEXTURE_2D
, 0, 5, 2, 1));
321 // Check NPOT height on level 0
322 EXPECT_TRUE(manager_
->ValidForTarget(GL_TEXTURE_2D
, 0, 2, 5, 1));
323 // Check NPOT width on level 1
324 EXPECT_FALSE(manager_
->ValidForTarget(GL_TEXTURE_2D
, 1, 5, 2, 1));
325 // Check NPOT height on level 1
326 EXPECT_FALSE(manager_
->ValidForTarget(GL_TEXTURE_2D
, 1, 2, 5, 1));
329 EXPECT_TRUE(manager_
->ValidForTarget(
330 GL_TEXTURE_CUBE_MAP
, 0,
331 kMaxCubeMapTextureSize
, kMaxCubeMapTextureSize
, 1));
332 EXPECT_TRUE(manager_
->ValidForTarget(
333 GL_TEXTURE_CUBE_MAP
, kMaxCubeMapLevels
- 1, 1, 1, 1));
334 EXPECT_FALSE(manager_
->ValidForTarget(
335 GL_TEXTURE_CUBE_MAP
, kMaxCubeMapLevels
- 1, 2, 2, 1));
336 // check level out of range.
337 EXPECT_FALSE(manager_
->ValidForTarget(
338 GL_TEXTURE_CUBE_MAP
, kMaxCubeMapLevels
,
339 kMaxCubeMapTextureSize
, 1, 1));
341 EXPECT_FALSE(manager_
->ValidForTarget(
342 GL_TEXTURE_CUBE_MAP
, kMaxCubeMapLevels
,
343 kMaxCubeMapTextureSize
, 1, 1));
345 EXPECT_FALSE(manager_
->ValidForTarget(
346 GL_TEXTURE_CUBE_MAP
, kMaxCubeMapLevels
,
347 kMaxCubeMapTextureSize
, 1, 2));
349 for (GLint level
= 0; level
< kMax2dLevels
; ++level
) {
350 EXPECT_TRUE(manager_
->ValidForTarget(
351 GL_TEXTURE_2D
, level
, kMaxTextureSize
>> level
, 1, 1));
352 EXPECT_TRUE(manager_
->ValidForTarget(
353 GL_TEXTURE_2D
, level
, 1, kMaxTextureSize
>> level
, 1));
354 EXPECT_FALSE(manager_
->ValidForTarget(
355 GL_TEXTURE_2D
, level
, (kMaxTextureSize
>> level
) + 1, 1, 1));
356 EXPECT_FALSE(manager_
->ValidForTarget(
357 GL_TEXTURE_2D
, level
, 1, (kMaxTextureSize
>> level
) + 1, 1));
360 for (GLint level
= 0; level
< kMaxCubeMapLevels
; ++level
) {
361 EXPECT_TRUE(manager_
->ValidForTarget(
362 GL_TEXTURE_CUBE_MAP
, level
,
363 kMaxCubeMapTextureSize
>> level
,
364 kMaxCubeMapTextureSize
>> level
,
366 EXPECT_FALSE(manager_
->ValidForTarget(
367 GL_TEXTURE_CUBE_MAP
, level
,
368 (kMaxCubeMapTextureSize
>> level
) * 2,
369 (kMaxCubeMapTextureSize
>> level
) * 2,
374 TEST_F(TextureManagerTest
, ValidForTargetNPOT
) {
375 TestHelper::SetupFeatureInfoInitExpectations(
376 gl_
.get(), "GL_OES_texture_npot");
377 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
378 feature_info
->Initialize();
379 TextureManager
manager(NULL
,
382 kMaxCubeMapTextureSize
,
383 kMaxRectangleTextureSize
,
384 kUseDefaultTextures
);
385 // Check NPOT width on level 0
386 EXPECT_TRUE(manager
.ValidForTarget(GL_TEXTURE_2D
, 0, 5, 2, 1));
387 // Check NPOT height on level 0
388 EXPECT_TRUE(manager
.ValidForTarget(GL_TEXTURE_2D
, 0, 2, 5, 1));
389 // Check NPOT width on level 1
390 EXPECT_TRUE(manager
.ValidForTarget(GL_TEXTURE_2D
, 1, 5, 2, 1));
391 // Check NPOT height on level 1
392 EXPECT_TRUE(manager
.ValidForTarget(GL_TEXTURE_2D
, 1, 2, 5, 1));
393 manager
.Destroy(false);
396 class TextureTestBase
: public GpuServiceTest
{
398 static const GLint kMaxTextureSize
= 16;
399 static const GLint kMaxCubeMapTextureSize
= 8;
400 static const GLint kMaxRectangleTextureSize
= 16;
401 static const GLint kMax2dLevels
= 5;
402 static const GLint kMaxCubeMapLevels
= 4;
403 static const GLuint kClient1Id
= 1;
404 static const GLuint kService1Id
= 11;
405 static const bool kUseDefaultTextures
= false;
408 : feature_info_(new FeatureInfo()) {
410 ~TextureTestBase() override
{ texture_ref_
= NULL
; }
413 void SetUpBase(MemoryTracker
* memory_tracker
, std::string extensions
) {
414 GpuServiceTest::SetUp();
415 if (!extensions
.empty()) {
416 TestHelper::SetupFeatureInfoInitExpectations(gl_
.get(),
418 feature_info_
->Initialize();
421 manager_
.reset(new TextureManager(memory_tracker
,
424 kMaxCubeMapTextureSize
,
425 kMaxRectangleTextureSize
,
426 kUseDefaultTextures
));
427 decoder_
.reset(new ::testing::StrictMock
<gles2::MockGLES2Decoder
>());
428 error_state_
.reset(new ::testing::StrictMock
<gles2::MockErrorState
>());
429 manager_
->CreateTexture(kClient1Id
, kService1Id
);
430 texture_ref_
= manager_
->GetTexture(kClient1Id
);
431 ASSERT_TRUE(texture_ref_
.get() != NULL
);
434 void TearDown() override
{
435 if (texture_ref_
.get()) {
436 // If it's not in the manager then setting texture_ref_ to NULL will
437 // delete the texture.
438 if (!texture_ref_
->client_id()) {
439 // Check that it gets deleted when the last reference is released.
441 DeleteTextures(1, ::testing::Pointee(texture_ref_
->service_id())))
443 .RetiresOnSaturation();
447 manager_
->Destroy(false);
449 GpuServiceTest::TearDown();
453 TextureRef
* texture_ref
, GLenum pname
, GLint value
, GLenum error
) {
454 TestHelper::SetTexParameteriWithExpectations(
455 gl_
.get(), error_state_
.get(), manager_
.get(),
456 texture_ref
, pname
, value
, error
);
459 scoped_ptr
<MockGLES2Decoder
> decoder_
;
460 scoped_ptr
<MockErrorState
> error_state_
;
461 scoped_refptr
<FeatureInfo
> feature_info_
;
462 scoped_ptr
<TextureManager
> manager_
;
463 scoped_refptr
<TextureRef
> texture_ref_
;
466 class TextureTest
: public TextureTestBase
{
468 void SetUp() override
{ SetUpBase(NULL
, std::string()); }
471 class TextureMemoryTrackerTest
: public TextureTestBase
{
473 void SetUp() override
{
474 mock_memory_tracker_
= new StrictMock
<MockMemoryTracker
>();
475 SetUpBase(mock_memory_tracker_
.get(), std::string());
478 scoped_refptr
<MockMemoryTracker
> mock_memory_tracker_
;
481 #define EXPECT_MEMORY_ALLOCATION_CHANGE(old_size, new_size, pool) \
482 EXPECT_CALL(*mock_memory_tracker_.get(), \
483 TrackMemoryAllocatedChange(old_size, new_size, pool)) \
484 .Times(1).RetiresOnSaturation()
486 TEST_F(TextureTest
, Basic
) {
487 Texture
* texture
= texture_ref_
->texture();
488 EXPECT_EQ(0u, texture
->target());
489 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
490 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
491 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
492 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
493 EXPECT_EQ(0, texture
->num_uncleared_mips());
494 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
495 EXPECT_TRUE(texture
->SafeToRenderFrom());
496 EXPECT_FALSE(texture
->IsImmutable());
497 EXPECT_EQ(static_cast<GLenum
>(GL_NEAREST_MIPMAP_LINEAR
),
498 texture
->min_filter());
499 EXPECT_EQ(static_cast<GLenum
>(GL_LINEAR
), texture
->mag_filter());
500 EXPECT_EQ(static_cast<GLenum
>(GL_REPEAT
), texture
->wrap_s());
501 EXPECT_EQ(static_cast<GLenum
>(GL_REPEAT
), texture
->wrap_t());
502 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
503 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
504 EXPECT_EQ(0u, texture
->estimated_size());
507 TEST_F(TextureTest
, SetTargetTexture2D
) {
508 Texture
* texture
= texture_ref_
->texture();
509 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
510 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
511 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
512 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
513 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
514 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
515 EXPECT_TRUE(texture
->SafeToRenderFrom());
516 EXPECT_FALSE(texture
->IsImmutable());
519 TEST_F(TextureTest
, SetTargetTextureExternalOES
) {
520 Texture
* texture
= texture_ref_
->texture();
521 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_EXTERNAL_OES
);
522 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
523 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
524 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
525 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture
));
526 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
527 EXPECT_TRUE(texture
->SafeToRenderFrom());
528 EXPECT_TRUE(texture
->IsImmutable());
531 TEST_F(TextureTest
, ZeroSizeCanNotRender
) {
532 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
533 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
534 manager_
->SetLevelInfo(texture_ref_
.get(),
545 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
546 manager_
->SetLevelInfo(texture_ref_
.get(),
557 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
560 TEST_F(TextureTest
, EstimatedSize
) {
561 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
562 manager_
->SetLevelInfo(texture_ref_
.get(),
573 EXPECT_EQ(8u * 4u * 4u, texture_ref_
->texture()->estimated_size());
574 manager_
->SetLevelInfo(texture_ref_
.get(),
585 EXPECT_EQ(8u * 4u * 4u * 2u, texture_ref_
->texture()->estimated_size());
588 TEST_F(TextureMemoryTrackerTest
, EstimatedSize
) {
589 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
590 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged
);
591 manager_
->SetLevelInfo(texture_ref_
.get(),
602 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged
);
603 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 256, MemoryTracker::kUnmanaged
);
604 manager_
->SetLevelInfo(texture_ref_
.get(),
615 // Add expectation for texture deletion.
616 EXPECT_MEMORY_ALLOCATION_CHANGE(256, 0, MemoryTracker::kUnmanaged
);
617 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged
);
620 TEST_F(TextureMemoryTrackerTest
, SetParameterPool
) {
621 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
622 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kUnmanaged
);
623 manager_
->SetLevelInfo(texture_ref_
.get(),
634 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kUnmanaged
);
635 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 128, MemoryTracker::kManaged
);
636 SetParameter(texture_ref_
.get(),
637 GL_TEXTURE_POOL_CHROMIUM
,
638 GL_TEXTURE_POOL_MANAGED_CHROMIUM
,
640 // Add expectation for texture deletion.
641 EXPECT_MEMORY_ALLOCATION_CHANGE(128, 0, MemoryTracker::kManaged
);
642 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged
);
643 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kManaged
);
646 TEST_F(TextureTest
, POT2D
) {
647 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
648 Texture
* texture
= texture_ref_
->texture();
649 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
650 // Check Setting level 0 to POT
651 manager_
->SetLevelInfo(texture_ref_
.get(),
662 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
663 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
664 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
665 EXPECT_EQ(0, texture
->num_uncleared_mips());
666 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
667 // Set filters to something that will work with a single mip.
669 texture_ref_
.get(), GL_TEXTURE_MIN_FILTER
, GL_LINEAR
, GL_NO_ERROR
);
670 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
671 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
673 SetParameter(texture_ref_
.get(),
674 GL_TEXTURE_MIN_FILTER
,
675 GL_LINEAR_MIPMAP_LINEAR
,
677 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
679 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
681 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
682 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
683 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
684 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
686 manager_
->SetLevelInfo(texture_ref_
.get(),
697 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
698 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
699 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
700 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
701 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
702 // Set a level past the number of mips that would get generated.
703 manager_
->SetLevelInfo(texture_ref_
.get(),
714 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
716 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
717 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
718 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
719 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
722 TEST_F(TextureMemoryTrackerTest
, MarkMipmapsGenerated
) {
723 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
724 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 64, MemoryTracker::kUnmanaged
);
725 manager_
->SetLevelInfo(texture_ref_
.get(),
736 EXPECT_MEMORY_ALLOCATION_CHANGE(64, 0, MemoryTracker::kUnmanaged
);
737 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 84, MemoryTracker::kUnmanaged
);
738 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
739 EXPECT_MEMORY_ALLOCATION_CHANGE(84, 0, MemoryTracker::kUnmanaged
);
740 EXPECT_MEMORY_ALLOCATION_CHANGE(0, 0, MemoryTracker::kUnmanaged
);
743 TEST_F(TextureTest
, UnusedMips
) {
744 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
745 Texture
* texture
= texture_ref_
->texture();
746 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
747 // Set level zero to large size.
748 manager_
->SetLevelInfo(texture_ref_
.get(),
759 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
760 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
761 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
762 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
763 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
764 // Set level zero to large smaller (levels unused mips)
765 manager_
->SetLevelInfo(texture_ref_
.get(),
776 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
777 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
778 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
779 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
780 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
781 // Set an unused level to some size
782 manager_
->SetLevelInfo(texture_ref_
.get(),
793 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
794 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
795 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
796 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
799 TEST_F(TextureTest
, NPOT2D
) {
800 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
801 Texture
* texture
= texture_ref_
->texture();
802 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
803 // Check Setting level 0 to NPOT
804 manager_
->SetLevelInfo(texture_ref_
.get(),
815 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture
));
816 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
817 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
818 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
819 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
821 texture_ref_
.get(), GL_TEXTURE_MIN_FILTER
, GL_LINEAR
, GL_NO_ERROR
);
822 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
823 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
825 texture_ref_
.get(), GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
, GL_NO_ERROR
);
826 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
827 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
829 texture_ref_
.get(), GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
, GL_NO_ERROR
);
830 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
831 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
833 manager_
->SetLevelInfo(texture_ref_
.get(),
844 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
845 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
846 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
847 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
850 TEST_F(TextureTest
, NPOT2DNPOTOK
) {
851 TestHelper::SetupFeatureInfoInitExpectations(
852 gl_
.get(), "GL_OES_texture_npot");
853 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
854 feature_info
->Initialize();
855 TextureManager
manager(NULL
,
858 kMaxCubeMapTextureSize
,
859 kMaxRectangleTextureSize
,
860 kUseDefaultTextures
);
861 manager
.CreateTexture(kClient1Id
, kService1Id
);
862 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
863 ASSERT_TRUE(texture_ref
!= NULL
);
864 Texture
* texture
= texture_ref
->texture();
866 manager
.SetTarget(texture_ref
, GL_TEXTURE_2D
);
867 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
868 // Check Setting level 0 to NPOT
869 manager
.SetLevelInfo(texture_ref
,
870 GL_TEXTURE_2D
, 0, GL_RGBA
, 4, 5, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, true);
871 EXPECT_TRUE(TextureTestHelper::IsNPOT(texture
));
872 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
873 EXPECT_TRUE(manager
.CanGenerateMipmaps(texture_ref
));
874 EXPECT_FALSE(manager
.CanRender(texture_ref
));
875 EXPECT_TRUE(manager
.HaveUnrenderableTextures());
876 EXPECT_TRUE(manager
.MarkMipmapsGenerated(texture_ref
));
877 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
878 EXPECT_TRUE(manager
.CanRender(texture_ref
));
879 EXPECT_FALSE(manager
.HaveUnrenderableTextures());
880 manager
.Destroy(false);
883 TEST_F(TextureTest
, POTCubeMap
) {
884 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_CUBE_MAP
);
885 Texture
* texture
= texture_ref_
->texture();
886 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_CUBE_MAP
), texture
->target());
887 // Check Setting level 0 each face to POT
888 manager_
->SetLevelInfo(texture_ref_
.get(),
889 GL_TEXTURE_CUBE_MAP_POSITIVE_X
,
899 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
900 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
901 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
902 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
903 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
904 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
905 manager_
->SetLevelInfo(texture_ref_
.get(),
906 GL_TEXTURE_CUBE_MAP_NEGATIVE_X
,
916 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
917 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
918 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
919 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
920 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
921 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
922 manager_
->SetLevelInfo(texture_ref_
.get(),
923 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
,
933 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
934 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
935 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
936 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
937 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
938 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
939 manager_
->SetLevelInfo(texture_ref_
.get(),
940 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
,
950 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
951 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
952 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
953 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
954 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
955 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
956 manager_
->SetLevelInfo(texture_ref_
.get(),
957 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
,
967 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
968 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
969 EXPECT_FALSE(TextureTestHelper::IsCubeComplete(texture
));
970 EXPECT_FALSE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
971 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
972 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
973 manager_
->SetLevelInfo(texture_ref_
.get(),
974 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
984 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
985 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
986 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture
));
987 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
988 EXPECT_FALSE(manager_
->CanRender(texture_ref_
.get()));
989 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
992 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
993 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
994 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture
));
995 EXPECT_TRUE(manager_
->CanRender(texture_ref_
.get()));
996 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
999 manager_
->SetLevelInfo(texture_ref_
.get(),
1000 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
1010 EXPECT_FALSE(TextureTestHelper::IsNPOT(texture
));
1011 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
1012 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture
));
1013 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
1014 // Set a level past the number of mips that would get generated.
1015 manager_
->SetLevelInfo(texture_ref_
.get(),
1016 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
1026 EXPECT_TRUE(manager_
->CanGenerateMipmaps(texture_ref_
.get()));
1028 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
1029 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
1030 EXPECT_TRUE(TextureTestHelper::IsCubeComplete(texture
));
1033 TEST_F(TextureTest
, GetLevelSize
) {
1034 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1035 manager_
->SetLevelInfo(texture_ref_
.get(),
1047 GLsizei height
= -1;
1048 Texture
* texture
= texture_ref_
->texture();
1049 EXPECT_FALSE(texture
->GetLevelSize(GL_TEXTURE_2D
, -1, &width
, &height
));
1050 EXPECT_FALSE(texture
->GetLevelSize(GL_TEXTURE_2D
, 1000, &width
, &height
));
1051 EXPECT_FALSE(texture
->GetLevelSize(GL_TEXTURE_2D
, 0, &width
, &height
));
1052 EXPECT_TRUE(texture
->GetLevelSize(GL_TEXTURE_2D
, 1, &width
, &height
));
1053 EXPECT_EQ(4, width
);
1054 EXPECT_EQ(5, height
);
1055 manager_
->RemoveTexture(kClient1Id
);
1056 EXPECT_TRUE(texture
->GetLevelSize(GL_TEXTURE_2D
, 1, &width
, &height
));
1057 EXPECT_EQ(4, width
);
1058 EXPECT_EQ(5, height
);
1061 TEST_F(TextureTest
, GetLevelType
) {
1062 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1063 manager_
->SetLevelInfo(texture_ref_
.get(),
1076 Texture
* texture
= texture_ref_
->texture();
1077 EXPECT_FALSE(texture
->GetLevelType(GL_TEXTURE_2D
, -1, &type
, &format
));
1078 EXPECT_FALSE(texture
->GetLevelType(GL_TEXTURE_2D
, 1000, &type
, &format
));
1079 EXPECT_FALSE(texture
->GetLevelType(GL_TEXTURE_2D
, 0, &type
, &format
));
1080 EXPECT_TRUE(texture
->GetLevelType(GL_TEXTURE_2D
, 1, &type
, &format
));
1081 EXPECT_EQ(static_cast<GLenum
>(GL_UNSIGNED_BYTE
), type
);
1082 EXPECT_EQ(static_cast<GLenum
>(GL_RGBA
), format
);
1083 manager_
->RemoveTexture(kClient1Id
);
1084 EXPECT_TRUE(texture
->GetLevelType(GL_TEXTURE_2D
, 1, &type
, &format
));
1085 EXPECT_EQ(static_cast<GLenum
>(GL_UNSIGNED_BYTE
), type
);
1086 EXPECT_EQ(static_cast<GLenum
>(GL_RGBA
), format
);
1089 TEST_F(TextureTest
, ValidForTexture
) {
1090 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1091 manager_
->SetLevelInfo(texture_ref_
.get(),
1103 Texture
* texture
= texture_ref_
->texture();
1104 EXPECT_FALSE(texture
->ValidForTexture(
1105 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
1106 1, 0, 0, 4, 5, GL_UNSIGNED_BYTE
));
1108 EXPECT_FALSE(texture
->ValidForTexture(
1109 GL_TEXTURE_2D
, 0, 0, 0, 4, 5, GL_UNSIGNED_BYTE
));
1110 // Check bad xoffset.
1111 EXPECT_FALSE(texture
->ValidForTexture(
1112 GL_TEXTURE_2D
, 1, -1, 0, 4, 5, GL_UNSIGNED_BYTE
));
1113 // Check bad xoffset + width > width.
1114 EXPECT_FALSE(texture
->ValidForTexture(
1115 GL_TEXTURE_2D
, 1, 1, 0, 4, 5, GL_UNSIGNED_BYTE
));
1116 // Check bad yoffset.
1117 EXPECT_FALSE(texture
->ValidForTexture(
1118 GL_TEXTURE_2D
, 1, 0, -1, 4, 5, GL_UNSIGNED_BYTE
));
1119 // Check bad yoffset + height > height.
1120 EXPECT_FALSE(texture
->ValidForTexture(
1121 GL_TEXTURE_2D
, 1, 0, 1, 4, 5, GL_UNSIGNED_BYTE
));
1123 EXPECT_FALSE(texture
->ValidForTexture(
1124 GL_TEXTURE_2D
, 1, 0, 0, 5, 5, GL_UNSIGNED_BYTE
));
1125 // Check bad height.
1126 EXPECT_FALSE(texture
->ValidForTexture(
1127 GL_TEXTURE_2D
, 1, 0, 0, 4, 6, GL_UNSIGNED_BYTE
));
1129 EXPECT_FALSE(texture
->ValidForTexture(
1130 GL_TEXTURE_2D
, 1, 0, 0, 4, 5, GL_UNSIGNED_SHORT_4_4_4_4
));
1131 // Check valid full size
1132 EXPECT_TRUE(texture
->ValidForTexture(
1133 GL_TEXTURE_2D
, 1, 0, 0, 4, 5, GL_UNSIGNED_BYTE
));
1134 // Check valid particial size.
1135 EXPECT_TRUE(texture
->ValidForTexture(
1136 GL_TEXTURE_2D
, 1, 1, 1, 2, 3, GL_UNSIGNED_BYTE
));
1137 manager_
->RemoveTexture(kClient1Id
);
1138 EXPECT_TRUE(texture
->ValidForTexture(
1139 GL_TEXTURE_2D
, 1, 0, 0, 4, 5, GL_UNSIGNED_BYTE
));
1142 TEST_F(TextureTest
, FloatNotLinear
) {
1143 TestHelper::SetupFeatureInfoInitExpectations(
1144 gl_
.get(), "GL_OES_texture_float");
1145 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
1146 feature_info
->Initialize();
1147 TextureManager
manager(NULL
,
1150 kMaxCubeMapTextureSize
,
1151 kMaxRectangleTextureSize
,
1152 kUseDefaultTextures
);
1153 manager
.CreateTexture(kClient1Id
, kService1Id
);
1154 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
1155 ASSERT_TRUE(texture_ref
!= NULL
);
1156 manager
.SetTarget(texture_ref
, GL_TEXTURE_2D
);
1157 Texture
* texture
= texture_ref
->texture();
1158 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
1159 manager
.SetLevelInfo(texture_ref
,
1160 GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 1, 0, GL_RGBA
, GL_FLOAT
, true);
1161 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
1162 TestHelper::SetTexParameteriWithExpectations(
1163 gl_
.get(), error_state_
.get(), &manager
,
1164 texture_ref
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
, GL_NO_ERROR
);
1165 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
1166 TestHelper::SetTexParameteriWithExpectations(
1167 gl_
.get(), error_state_
.get(), &manager
, texture_ref
,
1168 GL_TEXTURE_MIN_FILTER
, GL_NEAREST_MIPMAP_NEAREST
, GL_NO_ERROR
);
1169 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
1170 manager
.Destroy(false);
1173 TEST_F(TextureTest
, FloatLinear
) {
1174 TestHelper::SetupFeatureInfoInitExpectations(
1175 gl_
.get(), "GL_OES_texture_float GL_OES_texture_float_linear");
1176 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
1177 feature_info
->Initialize();
1178 TextureManager
manager(NULL
,
1181 kMaxCubeMapTextureSize
,
1182 kMaxRectangleTextureSize
,
1183 kUseDefaultTextures
);
1184 manager
.CreateTexture(kClient1Id
, kService1Id
);
1185 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
1186 ASSERT_TRUE(texture_ref
!= NULL
);
1187 manager
.SetTarget(texture_ref
, GL_TEXTURE_2D
);
1188 Texture
* texture
= texture_ref
->texture();
1189 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
1190 manager
.SetLevelInfo(texture_ref
,
1191 GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 1, 0, GL_RGBA
, GL_FLOAT
, true);
1192 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
1193 manager
.Destroy(false);
1196 TEST_F(TextureTest
, HalfFloatNotLinear
) {
1197 TestHelper::SetupFeatureInfoInitExpectations(
1198 gl_
.get(), "GL_OES_texture_half_float");
1199 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
1200 feature_info
->Initialize();
1201 TextureManager
manager(NULL
,
1204 kMaxCubeMapTextureSize
,
1205 kMaxRectangleTextureSize
,
1206 kUseDefaultTextures
);
1207 manager
.CreateTexture(kClient1Id
, kService1Id
);
1208 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
1209 ASSERT_TRUE(texture_ref
!= NULL
);
1210 manager
.SetTarget(texture_ref
, GL_TEXTURE_2D
);
1211 Texture
* texture
= texture_ref
->texture();
1212 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
1213 manager
.SetLevelInfo(texture_ref
,
1214 GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 1, 0, GL_RGBA
, GL_HALF_FLOAT_OES
, true);
1215 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
1216 TestHelper::SetTexParameteriWithExpectations(
1217 gl_
.get(), error_state_
.get(), &manager
,
1218 texture_ref
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
, GL_NO_ERROR
);
1219 EXPECT_FALSE(TextureTestHelper::IsTextureComplete(texture
));
1220 TestHelper::SetTexParameteriWithExpectations(
1221 gl_
.get(), error_state_
.get(), &manager
, texture_ref
,
1222 GL_TEXTURE_MIN_FILTER
, GL_NEAREST_MIPMAP_NEAREST
, GL_NO_ERROR
);
1223 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
1224 manager
.Destroy(false);
1227 TEST_F(TextureTest
, HalfFloatLinear
) {
1228 TestHelper::SetupFeatureInfoInitExpectations(
1229 gl_
.get(), "GL_OES_texture_half_float GL_OES_texture_half_float_linear");
1230 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
1231 feature_info
->Initialize();
1232 TextureManager
manager(NULL
,
1235 kMaxCubeMapTextureSize
,
1236 kMaxRectangleTextureSize
,
1237 kUseDefaultTextures
);
1238 manager
.CreateTexture(kClient1Id
, kService1Id
);
1239 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
1240 ASSERT_TRUE(texture_ref
!= NULL
);
1241 manager
.SetTarget(texture_ref
, GL_TEXTURE_2D
);
1242 Texture
* texture
= texture_ref
->texture();
1243 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
1244 manager
.SetLevelInfo(texture_ref
,
1245 GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 1, 0, GL_RGBA
, GL_HALF_FLOAT_OES
, true);
1246 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
1247 manager
.Destroy(false);
1250 TEST_F(TextureTest
, EGLImageExternal
) {
1251 TestHelper::SetupFeatureInfoInitExpectations(
1252 gl_
.get(), "GL_OES_EGL_image_external");
1253 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
1254 feature_info
->Initialize();
1255 TextureManager
manager(NULL
,
1258 kMaxCubeMapTextureSize
,
1259 kMaxRectangleTextureSize
,
1260 kUseDefaultTextures
);
1261 manager
.CreateTexture(kClient1Id
, kService1Id
);
1262 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
1263 ASSERT_TRUE(texture_ref
!= NULL
);
1264 manager
.SetTarget(texture_ref
, GL_TEXTURE_EXTERNAL_OES
);
1265 Texture
* texture
= texture_ref
->texture();
1266 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_EXTERNAL_OES
), texture
->target());
1267 EXPECT_FALSE(manager
.CanGenerateMipmaps(texture_ref
));
1268 manager
.Destroy(false);
1271 TEST_F(TextureTest
, DepthTexture
) {
1272 TestHelper::SetupFeatureInfoInitExpectations(
1273 gl_
.get(), "GL_ANGLE_depth_texture");
1274 scoped_refptr
<FeatureInfo
> feature_info(new FeatureInfo());
1275 feature_info
->Initialize();
1276 TextureManager
manager(NULL
,
1279 kMaxCubeMapTextureSize
,
1280 kMaxRectangleTextureSize
,
1281 kUseDefaultTextures
);
1282 manager
.CreateTexture(kClient1Id
, kService1Id
);
1283 TextureRef
* texture_ref
= manager
.GetTexture(kClient1Id
);
1284 ASSERT_TRUE(texture_ref
!= NULL
);
1285 manager
.SetTarget(texture_ref
, GL_TEXTURE_2D
);
1286 manager
.SetLevelInfo(
1287 texture_ref
, GL_TEXTURE_2D
, 0, GL_DEPTH_COMPONENT
, 4, 4, 1, 0,
1288 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, false);
1289 EXPECT_FALSE(manager
.CanGenerateMipmaps(texture_ref
));
1290 manager
.Destroy(false);
1293 TEST_F(TextureTest
, SafeUnsafe
) {
1294 static const GLuint kClient2Id
= 2;
1295 static const GLuint kService2Id
= 12;
1296 static const GLuint kClient3Id
= 3;
1297 static const GLuint kService3Id
= 13;
1298 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1299 Texture
* texture
= texture_ref_
->texture();
1300 EXPECT_EQ(0, texture
->num_uncleared_mips());
1301 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1302 manager_
->SetLevelInfo(texture_ref_
.get(),
1313 EXPECT_FALSE(texture
->SafeToRenderFrom());
1314 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1315 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1316 EXPECT_EQ(1, texture
->num_uncleared_mips());
1317 manager_
->SetLevelCleared(texture_ref_
.get(), GL_TEXTURE_2D
, 0, true);
1318 EXPECT_TRUE(texture
->SafeToRenderFrom());
1319 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1320 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1321 EXPECT_EQ(0, texture
->num_uncleared_mips());
1322 manager_
->SetLevelInfo(texture_ref_
.get(),
1333 EXPECT_FALSE(texture
->SafeToRenderFrom());
1334 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1335 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1336 EXPECT_EQ(1, texture
->num_uncleared_mips());
1337 manager_
->SetLevelCleared(texture_ref_
.get(), GL_TEXTURE_2D
, 1, true);
1338 EXPECT_TRUE(texture
->SafeToRenderFrom());
1339 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1340 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1341 EXPECT_EQ(0, texture
->num_uncleared_mips());
1342 manager_
->SetLevelInfo(texture_ref_
.get(),
1353 manager_
->SetLevelInfo(texture_ref_
.get(),
1364 EXPECT_FALSE(texture
->SafeToRenderFrom());
1365 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1366 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1367 EXPECT_EQ(2, texture
->num_uncleared_mips());
1368 manager_
->SetLevelCleared(texture_ref_
.get(), GL_TEXTURE_2D
, 0, true);
1369 EXPECT_FALSE(texture
->SafeToRenderFrom());
1370 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1371 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1372 EXPECT_EQ(1, texture
->num_uncleared_mips());
1373 manager_
->SetLevelCleared(texture_ref_
.get(), GL_TEXTURE_2D
, 1, true);
1374 EXPECT_TRUE(texture
->SafeToRenderFrom());
1375 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1376 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1377 EXPECT_EQ(0, texture
->num_uncleared_mips());
1378 manager_
->SetLevelInfo(texture_ref_
.get(),
1389 EXPECT_FALSE(texture
->SafeToRenderFrom());
1390 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1391 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1392 EXPECT_EQ(1, texture
->num_uncleared_mips());
1393 manager_
->MarkMipmapsGenerated(texture_ref_
.get());
1394 EXPECT_TRUE(texture
->SafeToRenderFrom());
1395 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1396 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1397 EXPECT_EQ(0, texture
->num_uncleared_mips());
1399 manager_
->CreateTexture(kClient2Id
, kService2Id
);
1400 scoped_refptr
<TextureRef
> texture_ref2(
1401 manager_
->GetTexture(kClient2Id
));
1402 ASSERT_TRUE(texture_ref2
.get() != NULL
);
1403 manager_
->SetTarget(texture_ref2
.get(), GL_TEXTURE_2D
);
1404 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1405 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1406 Texture
* texture2
= texture_ref2
->texture();
1407 EXPECT_EQ(0, texture2
->num_uncleared_mips());
1408 manager_
->SetLevelInfo(texture_ref2
.get(),
1419 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1420 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1421 EXPECT_EQ(0, texture2
->num_uncleared_mips());
1422 manager_
->SetLevelInfo(texture_ref2
.get(),
1433 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1434 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1435 EXPECT_EQ(1, texture2
->num_uncleared_mips());
1437 manager_
->CreateTexture(kClient3Id
, kService3Id
);
1438 scoped_refptr
<TextureRef
> texture_ref3(
1439 manager_
->GetTexture(kClient3Id
));
1440 ASSERT_TRUE(texture_ref3
.get() != NULL
);
1441 manager_
->SetTarget(texture_ref3
.get(), GL_TEXTURE_2D
);
1442 manager_
->SetLevelInfo(texture_ref3
.get(),
1453 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1454 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1455 Texture
* texture3
= texture_ref3
->texture();
1456 EXPECT_EQ(1, texture3
->num_uncleared_mips());
1457 manager_
->SetLevelCleared(texture_ref2
.get(), GL_TEXTURE_2D
, 0, true);
1458 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1459 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1460 EXPECT_EQ(0, texture2
->num_uncleared_mips());
1461 manager_
->SetLevelCleared(texture_ref3
.get(), GL_TEXTURE_2D
, 0, true);
1462 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1463 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1464 EXPECT_EQ(0, texture3
->num_uncleared_mips());
1466 manager_
->SetLevelInfo(texture_ref2
.get(),
1477 manager_
->SetLevelInfo(texture_ref3
.get(),
1488 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1489 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1490 EXPECT_EQ(1, texture2
->num_uncleared_mips());
1491 EXPECT_EQ(1, texture3
->num_uncleared_mips());
1492 manager_
->RemoveTexture(kClient3Id
);
1493 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1494 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1495 manager_
->RemoveTexture(kClient2Id
);
1496 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1497 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1498 EXPECT_CALL(*gl_
, DeleteTextures(1, ::testing::Pointee(kService2Id
)))
1500 .RetiresOnSaturation();
1501 texture_ref2
= NULL
;
1502 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1503 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1504 EXPECT_CALL(*gl_
, DeleteTextures(1, ::testing::Pointee(kService3Id
)))
1506 .RetiresOnSaturation();
1507 texture_ref3
= NULL
;
1508 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1509 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1512 TEST_F(TextureTest
, ClearTexture
) {
1513 EXPECT_CALL(*decoder_
, ClearLevel(_
, _
, _
, _
, _
, _
, _
, _
, _
))
1514 .WillRepeatedly(Return(true));
1515 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1516 manager_
->SetLevelInfo(texture_ref_
.get(),
1527 manager_
->SetLevelInfo(texture_ref_
.get(),
1538 Texture
* texture
= texture_ref_
->texture();
1539 EXPECT_FALSE(texture
->SafeToRenderFrom());
1540 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1541 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1542 EXPECT_EQ(2, texture
->num_uncleared_mips());
1543 manager_
->ClearRenderableLevels(decoder_
.get(), texture_ref_
.get());
1544 EXPECT_TRUE(texture
->SafeToRenderFrom());
1545 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1546 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1547 EXPECT_EQ(0, texture
->num_uncleared_mips());
1548 manager_
->SetLevelInfo(texture_ref_
.get(),
1559 manager_
->SetLevelInfo(texture_ref_
.get(),
1570 EXPECT_FALSE(texture
->SafeToRenderFrom());
1571 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1572 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1573 EXPECT_EQ(2, texture
->num_uncleared_mips());
1574 manager_
->ClearTextureLevel(
1575 decoder_
.get(), texture_ref_
.get(), GL_TEXTURE_2D
, 0);
1576 EXPECT_FALSE(texture
->SafeToRenderFrom());
1577 EXPECT_TRUE(manager_
->HaveUnsafeTextures());
1578 EXPECT_TRUE(manager_
->HaveUnclearedMips());
1579 EXPECT_EQ(1, texture
->num_uncleared_mips());
1580 manager_
->ClearTextureLevel(
1581 decoder_
.get(), texture_ref_
.get(), GL_TEXTURE_2D
, 1);
1582 EXPECT_TRUE(texture
->SafeToRenderFrom());
1583 EXPECT_FALSE(manager_
->HaveUnsafeTextures());
1584 EXPECT_FALSE(manager_
->HaveUnclearedMips());
1585 EXPECT_EQ(0, texture
->num_uncleared_mips());
1588 TEST_F(TextureTest
, UseDeletedTexture
) {
1589 static const GLuint kClient2Id
= 2;
1590 static const GLuint kService2Id
= 12;
1591 // Make the default texture renderable
1592 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1593 manager_
->SetLevelInfo(texture_ref_
.get(),
1604 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
1605 // Make a new texture
1606 manager_
->CreateTexture(kClient2Id
, kService2Id
);
1607 scoped_refptr
<TextureRef
> texture_ref(
1608 manager_
->GetTexture(kClient2Id
));
1609 manager_
->SetTarget(texture_ref
.get(), GL_TEXTURE_2D
);
1610 EXPECT_FALSE(manager_
->CanRender(texture_ref
.get()));
1611 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
1613 manager_
->RemoveTexture(kClient2Id
);
1614 EXPECT_FALSE(manager_
->CanRender(texture_ref
.get()));
1615 EXPECT_TRUE(manager_
->HaveUnrenderableTextures());
1616 // Check that we can still manipulate it and it effects the manager.
1617 manager_
->SetLevelInfo(texture_ref
.get(),
1628 EXPECT_TRUE(manager_
->CanRender(texture_ref
.get()));
1629 EXPECT_FALSE(manager_
->HaveUnrenderableTextures());
1630 EXPECT_CALL(*gl_
, DeleteTextures(1, ::testing::Pointee(kService2Id
)))
1632 .RetiresOnSaturation();
1636 TEST_F(TextureTest
, GetLevelImage
) {
1637 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1638 manager_
->SetLevelInfo(texture_ref_
.get(),
1649 Texture
* texture
= texture_ref_
->texture();
1650 EXPECT_TRUE(texture
->GetLevelImage(GL_TEXTURE_2D
, 1) == NULL
);
1652 scoped_refptr
<gfx::GLImage
> image(new gfx::GLImageStub
);
1653 manager_
->SetLevelImage(texture_ref_
.get(), GL_TEXTURE_2D
, 1, image
.get());
1654 EXPECT_FALSE(texture
->GetLevelImage(GL_TEXTURE_2D
, 1) == NULL
);
1656 manager_
->SetLevelImage(texture_ref_
.get(), GL_TEXTURE_2D
, 1, NULL
);
1657 EXPECT_TRUE(texture
->GetLevelImage(GL_TEXTURE_2D
, 1) == NULL
);
1658 manager_
->SetLevelImage(texture_ref_
.get(), GL_TEXTURE_2D
, 1, image
.get());
1659 // Image should be reset when SetLevelInfo is called.
1660 manager_
->SetLevelInfo(texture_ref_
.get(),
1671 EXPECT_TRUE(texture
->GetLevelImage(GL_TEXTURE_2D
, 1) == NULL
);
1676 bool InSet(std::set
<std::string
>* string_set
, const std::string
& str
) {
1677 std::pair
<std::set
<std::string
>::iterator
, bool> result
=
1678 string_set
->insert(str
);
1679 return !result
.second
;
1682 } // anonymous namespace
1684 TEST_F(TextureTest
, AddToSignature
) {
1685 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
1686 manager_
->SetLevelInfo(texture_ref_
.get(),
1697 std::string signature1
;
1698 std::string signature2
;
1699 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature1
);
1701 std::set
<std::string
> string_set
;
1702 EXPECT_FALSE(InSet(&string_set
, signature1
));
1704 // check changing 1 thing makes a different signature.
1705 manager_
->SetLevelInfo(texture_ref_
.get(),
1716 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1717 EXPECT_FALSE(InSet(&string_set
, signature2
));
1719 // check putting it back makes the same signature.
1720 manager_
->SetLevelInfo(texture_ref_
.get(),
1732 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1733 EXPECT_EQ(signature1
, signature2
);
1735 // Check setting cleared status does not change signature.
1736 manager_
->SetLevelInfo(texture_ref_
.get(),
1748 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1749 EXPECT_EQ(signature1
, signature2
);
1751 // Check changing other settings changes signature.
1752 manager_
->SetLevelInfo(texture_ref_
.get(),
1764 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1765 EXPECT_FALSE(InSet(&string_set
, signature2
));
1767 manager_
->SetLevelInfo(texture_ref_
.get(),
1779 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1780 EXPECT_FALSE(InSet(&string_set
, signature2
));
1782 manager_
->SetLevelInfo(texture_ref_
.get(),
1794 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1795 EXPECT_FALSE(InSet(&string_set
, signature2
));
1797 manager_
->SetLevelInfo(texture_ref_
.get(),
1809 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1810 EXPECT_FALSE(InSet(&string_set
, signature2
));
1812 manager_
->SetLevelInfo(texture_ref_
.get(),
1824 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1825 EXPECT_FALSE(InSet(&string_set
, signature2
));
1828 manager_
->SetLevelInfo(texture_ref_
.get(),
1840 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1841 EXPECT_EQ(signature1
, signature2
);
1843 // check changing parameters changes signature.
1845 texture_ref_
.get(), GL_TEXTURE_MIN_FILTER
, GL_NEAREST
, GL_NO_ERROR
);
1847 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1848 EXPECT_FALSE(InSet(&string_set
, signature2
));
1850 SetParameter(texture_ref_
.get(),
1851 GL_TEXTURE_MIN_FILTER
,
1852 GL_NEAREST_MIPMAP_LINEAR
,
1855 texture_ref_
.get(), GL_TEXTURE_MAG_FILTER
, GL_NEAREST
, GL_NO_ERROR
);
1857 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1858 EXPECT_FALSE(InSet(&string_set
, signature2
));
1861 texture_ref_
.get(), GL_TEXTURE_MAG_FILTER
, GL_LINEAR
, GL_NO_ERROR
);
1863 texture_ref_
.get(), GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
, GL_NO_ERROR
);
1865 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1866 EXPECT_FALSE(InSet(&string_set
, signature2
));
1868 SetParameter(texture_ref_
.get(), GL_TEXTURE_WRAP_S
, GL_REPEAT
, GL_NO_ERROR
);
1870 texture_ref_
.get(), GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
, GL_NO_ERROR
);
1872 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1873 EXPECT_FALSE(InSet(&string_set
, signature2
));
1875 // Check putting it back genenerates the same signature
1876 SetParameter(texture_ref_
.get(), GL_TEXTURE_WRAP_T
, GL_REPEAT
, GL_NO_ERROR
);
1878 manager_
->AddToSignature(texture_ref_
.get(), GL_TEXTURE_2D
, 1, &signature2
);
1879 EXPECT_EQ(signature1
, signature2
);
1881 // Check the set was acutally getting different signatures.
1882 EXPECT_EQ(11u, string_set
.size());
1885 class ProduceConsumeTextureTest
: public TextureTest
,
1886 public ::testing::WithParamInterface
<GLenum
> {
1888 void SetUp() override
{
1889 TextureTest::SetUpBase(NULL
, "GL_OES_EGL_image_external");
1890 manager_
->CreateTexture(kClient2Id
, kService2Id
);
1891 texture2_
= manager_
->GetTexture(kClient2Id
);
1893 EXPECT_CALL(*decoder_
.get(), GetErrorState())
1894 .WillRepeatedly(Return(error_state_
.get()));
1897 void TearDown() override
{
1898 if (texture2_
.get()) {
1899 // If it's not in the manager then setting texture2_ to NULL will
1900 // delete the texture.
1901 if (!texture2_
->client_id()) {
1902 // Check that it gets deleted when the last reference is released.
1905 DeleteTextures(1, ::testing::Pointee(texture2_
->service_id())))
1906 .Times(1).RetiresOnSaturation();
1910 TextureTest::TearDown();
1915 LevelInfo(GLenum target
,
1942 bool operator==(const LevelInfo
& other
) const {
1943 return target
== other
.target
&& format
== other
.format
&&
1944 width
== other
.width
&& height
== other
.height
&&
1945 depth
== other
.depth
&& border
== other
.border
&&
1946 type
== other
.type
&& cleared
== other
.cleared
;
1959 void SetLevelInfo(TextureRef
* texture_ref
,
1961 const LevelInfo
& info
) {
1962 manager_
->SetLevelInfo(texture_ref
,
1975 static LevelInfo
GetLevelInfo(const TextureRef
* texture_ref
,
1978 const Texture
* texture
= texture_ref
->texture();
1980 info
.target
= target
;
1981 EXPECT_TRUE(texture
->GetLevelSize(target
, level
, &info
.width
,
1983 EXPECT_TRUE(texture
->GetLevelType(target
, level
, &info
.type
,
1985 info
.cleared
= texture
->IsLevelCleared(target
, level
);
1989 Texture
* Produce(TextureRef
* texture_ref
) {
1990 Texture
* texture
= manager_
->Produce(texture_ref
);
1991 EXPECT_TRUE(texture
!= NULL
);
1995 void Consume(GLuint client_id
, Texture
* texture
) {
1996 EXPECT_TRUE(manager_
->Consume(client_id
, texture
));
1999 scoped_refptr
<TextureRef
> texture2_
;
2002 static const GLuint kClient2Id
;
2003 static const GLuint kService2Id
;
2006 const GLuint
ProduceConsumeTextureTest::kClient2Id
= 2;
2007 const GLuint
ProduceConsumeTextureTest::kService2Id
= 12;
2009 TEST_F(ProduceConsumeTextureTest
, ProduceConsume2D
) {
2010 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_2D
);
2011 Texture
* texture
= texture_ref_
->texture();
2012 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), texture
->target());
2014 GL_TEXTURE_2D
, GL_RGBA
, 4, 4, 1, 0, GL_UNSIGNED_BYTE
, true);
2015 SetLevelInfo(texture_ref_
.get(), 0, level0
);
2016 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture_ref_
.get()));
2017 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
2018 LevelInfo level1
= GetLevelInfo(texture_ref_
.get(), GL_TEXTURE_2D
, 1);
2019 LevelInfo level2
= GetLevelInfo(texture_ref_
.get(), GL_TEXTURE_2D
, 2);
2020 Texture
* produced_texture
= Produce(texture_ref_
.get());
2021 EXPECT_EQ(produced_texture
, texture
);
2023 // Make this texture bigger with more levels, and make sure they get
2024 // clobbered correctly during Consume().
2025 manager_
->SetTarget(texture2_
.get(), GL_TEXTURE_2D
);
2029 LevelInfo(GL_TEXTURE_2D
, GL_RGBA
, 16, 16, 1, 0, GL_UNSIGNED_BYTE
, false));
2030 EXPECT_TRUE(manager_
->MarkMipmapsGenerated(texture2_
.get()));
2031 texture
= texture2_
->texture();
2032 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
2033 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U, texture
->estimated_size());
2035 GLuint client_id
= texture2_
->client_id();
2036 manager_
->RemoveTexture(client_id
);
2037 Consume(client_id
, produced_texture
);
2038 scoped_refptr
<TextureRef
> restored_texture
= manager_
->GetTexture(client_id
);
2039 EXPECT_EQ(produced_texture
, restored_texture
->texture());
2040 EXPECT_EQ(level0
, GetLevelInfo(restored_texture
.get(), GL_TEXTURE_2D
, 0));
2041 EXPECT_EQ(level1
, GetLevelInfo(restored_texture
.get(), GL_TEXTURE_2D
, 1));
2042 EXPECT_EQ(level2
, GetLevelInfo(restored_texture
.get(), GL_TEXTURE_2D
, 2));
2043 texture
= restored_texture
->texture();
2044 EXPECT_EQ(64U + 16U + 4U, texture
->estimated_size());
2046 EXPECT_FALSE(texture
->GetLevelSize(GL_TEXTURE_2D
, 3, &w
, &h
));
2048 // However the old texture ref still exists if it was referenced somewhere.
2049 EXPECT_EQ(1024U + 256U + 64U + 16U + 4U,
2050 texture2_
->texture()->estimated_size());
2053 TEST_F(ProduceConsumeTextureTest
, ProduceConsumeClearRectangle
) {
2054 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_RECTANGLE_ARB
);
2055 Texture
* texture
= texture_ref_
->texture();
2056 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_RECTANGLE_ARB
), texture
->target());
2058 GL_TEXTURE_RECTANGLE_ARB
, GL_RGBA
, 1, 1, 1, 0, GL_UNSIGNED_BYTE
, false);
2059 SetLevelInfo(texture_ref_
.get(), 0, level0
);
2060 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
2061 Texture
* produced_texture
= Produce(texture_ref_
.get());
2062 EXPECT_EQ(produced_texture
, texture
);
2063 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_RECTANGLE_ARB
),
2064 produced_texture
->target());
2066 GLuint client_id
= texture2_
->client_id();
2067 manager_
->RemoveTexture(client_id
);
2068 Consume(client_id
, produced_texture
);
2069 scoped_refptr
<TextureRef
> restored_texture
= manager_
->GetTexture(client_id
);
2070 EXPECT_EQ(produced_texture
, restored_texture
->texture());
2072 // See if we can clear the previously uncleared level now.
2074 GetLevelInfo(restored_texture
.get(), GL_TEXTURE_RECTANGLE_ARB
, 0));
2075 EXPECT_CALL(*decoder_
, ClearLevel(_
, _
, _
, _
, _
, _
, _
, _
, _
))
2076 .WillRepeatedly(Return(true));
2077 EXPECT_TRUE(manager_
->ClearTextureLevel(
2078 decoder_
.get(), restored_texture
.get(), GL_TEXTURE_RECTANGLE_ARB
, 0));
2081 TEST_F(ProduceConsumeTextureTest
, ProduceConsumeExternal
) {
2082 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_EXTERNAL_OES
);
2083 Texture
* texture
= texture_ref_
->texture();
2084 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_EXTERNAL_OES
), texture
->target());
2086 GL_TEXTURE_EXTERNAL_OES
, GL_RGBA
, 1, 1, 1, 0, GL_UNSIGNED_BYTE
, false);
2087 SetLevelInfo(texture_ref_
.get(), 0, level0
);
2088 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
2089 Texture
* produced_texture
= Produce(texture_ref_
.get());
2090 EXPECT_EQ(produced_texture
, texture
);
2092 GLuint client_id
= texture2_
->client_id();
2093 manager_
->RemoveTexture(client_id
);
2094 Consume(client_id
, produced_texture
);
2095 scoped_refptr
<TextureRef
> restored_texture
= manager_
->GetTexture(client_id
);
2096 EXPECT_EQ(produced_texture
, restored_texture
->texture());
2098 GetLevelInfo(restored_texture
.get(), GL_TEXTURE_EXTERNAL_OES
, 0));
2101 TEST_P(ProduceConsumeTextureTest
, ProduceConsumeTextureWithImage
) {
2102 GLenum target
= GetParam();
2103 manager_
->SetTarget(texture_ref_
.get(), target
);
2104 Texture
* texture
= texture_ref_
->texture();
2105 EXPECT_EQ(static_cast<GLenum
>(target
), texture
->target());
2106 scoped_refptr
<gfx::GLImage
> image(new gfx::GLImageStub
);
2107 manager_
->SetLevelInfo(texture_ref_
.get(),
2118 manager_
->SetLevelImage(texture_ref_
.get(), target
, 0, image
.get());
2119 GLuint service_id
= texture
->service_id();
2120 Texture
* produced_texture
= Produce(texture_ref_
.get());
2122 GLuint client_id
= texture2_
->client_id();
2123 manager_
->RemoveTexture(client_id
);
2124 Consume(client_id
, produced_texture
);
2125 scoped_refptr
<TextureRef
> restored_texture
= manager_
->GetTexture(client_id
);
2126 EXPECT_EQ(produced_texture
, restored_texture
->texture());
2127 EXPECT_EQ(service_id
, restored_texture
->service_id());
2128 EXPECT_EQ(image
.get(), restored_texture
->texture()->GetLevelImage(target
, 0));
2131 static const GLenum kTextureTargets
[] = {GL_TEXTURE_2D
, GL_TEXTURE_EXTERNAL_OES
,
2132 GL_TEXTURE_RECTANGLE_ARB
, };
2134 INSTANTIATE_TEST_CASE_P(Target
,
2135 ProduceConsumeTextureTest
,
2136 ::testing::ValuesIn(kTextureTargets
));
2138 TEST_F(ProduceConsumeTextureTest
, ProduceConsumeCube
) {
2139 manager_
->SetTarget(texture_ref_
.get(), GL_TEXTURE_CUBE_MAP
);
2140 Texture
* texture
= texture_ref_
->texture();
2141 EXPECT_EQ(static_cast<GLenum
>(GL_TEXTURE_CUBE_MAP
), texture
->target());
2142 LevelInfo
face0(GL_TEXTURE_CUBE_MAP_POSITIVE_X
,
2150 LevelInfo
face5(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
2158 SetLevelInfo(texture_ref_
.get(), 0, face0
);
2159 SetLevelInfo(texture_ref_
.get(), 0, face5
);
2160 EXPECT_TRUE(TextureTestHelper::IsTextureComplete(texture
));
2161 Texture
* produced_texture
= Produce(texture_ref_
.get());
2162 EXPECT_EQ(produced_texture
, texture
);
2164 GLuint client_id
= texture2_
->client_id();
2165 manager_
->RemoveTexture(client_id
);
2166 Consume(client_id
, produced_texture
);
2167 scoped_refptr
<TextureRef
> restored_texture
= manager_
->GetTexture(client_id
);
2168 EXPECT_EQ(produced_texture
, restored_texture
->texture());
2171 GetLevelInfo(restored_texture
.get(), GL_TEXTURE_CUBE_MAP_POSITIVE_X
, 0));
2174 GetLevelInfo(restored_texture
.get(), GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
, 0));
2177 class CountingMemoryTracker
: public MemoryTracker
{
2179 CountingMemoryTracker() {
2180 current_size_
[0] = 0;
2181 current_size_
[1] = 0;
2184 void TrackMemoryAllocatedChange(size_t old_size
,
2186 Pool pool
) override
{
2187 DCHECK_LT(static_cast<size_t>(pool
), arraysize(current_size_
));
2188 current_size_
[pool
] += new_size
- old_size
;
2191 bool EnsureGPUMemoryAvailable(size_t size_needed
) override
{ return true; }
2193 size_t GetSize(Pool pool
) {
2194 DCHECK_LT(static_cast<size_t>(pool
), arraysize(current_size_
));
2195 return current_size_
[pool
];
2199 ~CountingMemoryTracker() override
{}
2201 size_t current_size_
[2];
2202 DISALLOW_COPY_AND_ASSIGN(CountingMemoryTracker
);
2205 class SharedTextureTest
: public GpuServiceTest
{
2207 static const bool kUseDefaultTextures
= false;
2209 SharedTextureTest() : feature_info_(new FeatureInfo()) {}
2211 ~SharedTextureTest() override
{}
2213 void SetUp() override
{
2214 GpuServiceTest::SetUp();
2215 memory_tracker1_
= new CountingMemoryTracker
;
2216 texture_manager1_
.reset(
2217 new TextureManager(memory_tracker1_
.get(),
2218 feature_info_
.get(),
2219 TextureManagerTest::kMaxTextureSize
,
2220 TextureManagerTest::kMaxCubeMapTextureSize
,
2221 TextureManagerTest::kMaxRectangleTextureSize
,
2222 kUseDefaultTextures
));
2223 memory_tracker2_
= new CountingMemoryTracker
;
2224 texture_manager2_
.reset(
2225 new TextureManager(memory_tracker2_
.get(),
2226 feature_info_
.get(),
2227 TextureManagerTest::kMaxTextureSize
,
2228 TextureManagerTest::kMaxCubeMapTextureSize
,
2229 TextureManagerTest::kMaxRectangleTextureSize
,
2230 kUseDefaultTextures
));
2231 TestHelper::SetupTextureManagerInitExpectations(
2232 gl_
.get(), "", kUseDefaultTextures
);
2233 texture_manager1_
->Initialize();
2234 TestHelper::SetupTextureManagerInitExpectations(
2235 gl_
.get(), "", kUseDefaultTextures
);
2236 texture_manager2_
->Initialize();
2239 void TearDown() override
{
2240 texture_manager2_
->Destroy(false);
2241 texture_manager2_
.reset();
2242 texture_manager1_
->Destroy(false);
2243 texture_manager1_
.reset();
2244 GpuServiceTest::TearDown();
2248 scoped_refptr
<FeatureInfo
> feature_info_
;
2249 scoped_refptr
<CountingMemoryTracker
> memory_tracker1_
;
2250 scoped_ptr
<TextureManager
> texture_manager1_
;
2251 scoped_refptr
<CountingMemoryTracker
> memory_tracker2_
;
2252 scoped_ptr
<TextureManager
> texture_manager2_
;
2255 TEST_F(SharedTextureTest
, DeleteTextures
) {
2256 scoped_refptr
<TextureRef
> ref1
= texture_manager1_
->CreateTexture(10, 10);
2257 scoped_refptr
<TextureRef
> ref2
=
2258 texture_manager2_
->Consume(20, ref1
->texture());
2259 EXPECT_CALL(*gl_
, DeleteTextures(1, _
))
2262 texture_manager1_
->RemoveTexture(10);
2263 testing::Mock::VerifyAndClearExpectations(gl_
.get());
2265 EXPECT_CALL(*gl_
, DeleteTextures(1, _
))
2267 .RetiresOnSaturation();
2269 texture_manager2_
->RemoveTexture(20);
2270 testing::Mock::VerifyAndClearExpectations(gl_
.get());
2273 TEST_F(SharedTextureTest
, TextureSafetyAccounting
) {
2274 EXPECT_FALSE(texture_manager1_
->HaveUnrenderableTextures());
2275 EXPECT_FALSE(texture_manager1_
->HaveUnsafeTextures());
2276 EXPECT_FALSE(texture_manager1_
->HaveUnclearedMips());
2277 EXPECT_FALSE(texture_manager2_
->HaveUnrenderableTextures());
2278 EXPECT_FALSE(texture_manager2_
->HaveUnsafeTextures());
2279 EXPECT_FALSE(texture_manager2_
->HaveUnclearedMips());
2281 // Newly created texture is renderable.
2282 scoped_refptr
<TextureRef
> ref1
= texture_manager1_
->CreateTexture(10, 10);
2283 EXPECT_FALSE(texture_manager1_
->HaveUnrenderableTextures());
2284 EXPECT_FALSE(texture_manager1_
->HaveUnsafeTextures());
2285 EXPECT_FALSE(texture_manager1_
->HaveUnclearedMips());
2287 // Associate new texture ref to other texture manager, should account for it
2289 scoped_refptr
<TextureRef
> ref2
=
2290 texture_manager2_
->Consume(20, ref1
->texture());
2291 EXPECT_FALSE(texture_manager2_
->HaveUnrenderableTextures());
2292 EXPECT_FALSE(texture_manager2_
->HaveUnsafeTextures());
2293 EXPECT_FALSE(texture_manager2_
->HaveUnclearedMips());
2295 // Make texture renderable but uncleared on one texture manager, should affect
2297 texture_manager1_
->SetTarget(ref1
.get(), GL_TEXTURE_2D
);
2298 EXPECT_TRUE(texture_manager1_
->HaveUnrenderableTextures());
2299 EXPECT_FALSE(texture_manager1_
->HaveUnsafeTextures());
2300 EXPECT_FALSE(texture_manager1_
->HaveUnclearedMips());
2301 EXPECT_TRUE(texture_manager2_
->HaveUnrenderableTextures());
2302 EXPECT_FALSE(texture_manager2_
->HaveUnsafeTextures());
2303 EXPECT_FALSE(texture_manager2_
->HaveUnclearedMips());
2305 texture_manager1_
->SetLevelInfo(ref1
.get(),
2316 EXPECT_FALSE(texture_manager1_
->HaveUnrenderableTextures());
2317 EXPECT_TRUE(texture_manager1_
->HaveUnsafeTextures());
2318 EXPECT_TRUE(texture_manager1_
->HaveUnclearedMips());
2319 EXPECT_FALSE(texture_manager2_
->HaveUnrenderableTextures());
2320 EXPECT_TRUE(texture_manager2_
->HaveUnsafeTextures());
2321 EXPECT_TRUE(texture_manager2_
->HaveUnclearedMips());
2323 // Make texture cleared on one texture manager, should affect other one.
2324 texture_manager1_
->SetLevelCleared(ref1
.get(), GL_TEXTURE_2D
, 0, true);
2325 EXPECT_FALSE(texture_manager1_
->HaveUnsafeTextures());
2326 EXPECT_FALSE(texture_manager1_
->HaveUnclearedMips());
2327 EXPECT_FALSE(texture_manager2_
->HaveUnsafeTextures());
2328 EXPECT_FALSE(texture_manager2_
->HaveUnclearedMips());
2330 EXPECT_CALL(*gl_
, DeleteTextures(1, _
))
2332 .RetiresOnSaturation();
2333 texture_manager1_
->RemoveTexture(10);
2334 texture_manager2_
->RemoveTexture(20);
2337 TEST_F(SharedTextureTest
, FBOCompletenessCheck
) {
2338 const GLenum kCompleteValue
= GL_FRAMEBUFFER_COMPLETE
;
2339 FramebufferManager
framebuffer_manager1(1, 1);
2340 texture_manager1_
->set_framebuffer_manager(&framebuffer_manager1
);
2341 FramebufferManager
framebuffer_manager2(1, 1);
2342 texture_manager2_
->set_framebuffer_manager(&framebuffer_manager2
);
2344 scoped_refptr
<TextureRef
> ref1
= texture_manager1_
->CreateTexture(10, 10);
2345 framebuffer_manager1
.CreateFramebuffer(10, 10);
2346 scoped_refptr
<Framebuffer
> framebuffer1
=
2347 framebuffer_manager1
.GetFramebuffer(10);
2348 framebuffer1
->AttachTexture(
2349 GL_COLOR_ATTACHMENT0
, ref1
.get(), GL_TEXTURE_2D
, 0, 0);
2350 EXPECT_FALSE(framebuffer_manager1
.IsComplete(framebuffer1
.get()));
2351 EXPECT_NE(kCompleteValue
, framebuffer1
->IsPossiblyComplete());
2353 // Make FBO complete in manager 1.
2354 texture_manager1_
->SetTarget(ref1
.get(), GL_TEXTURE_2D
);
2355 texture_manager1_
->SetLevelInfo(ref1
.get(),
2366 EXPECT_EQ(kCompleteValue
, framebuffer1
->IsPossiblyComplete());
2367 framebuffer_manager1
.MarkAsComplete(framebuffer1
.get());
2368 EXPECT_TRUE(framebuffer_manager1
.IsComplete(framebuffer1
.get()));
2370 // Share texture with manager 2.
2371 scoped_refptr
<TextureRef
> ref2
=
2372 texture_manager2_
->Consume(20, ref1
->texture());
2373 framebuffer_manager2
.CreateFramebuffer(20, 20);
2374 scoped_refptr
<Framebuffer
> framebuffer2
=
2375 framebuffer_manager2
.GetFramebuffer(20);
2376 framebuffer2
->AttachTexture(
2377 GL_COLOR_ATTACHMENT0
, ref2
.get(), GL_TEXTURE_2D
, 0, 0);
2378 EXPECT_FALSE(framebuffer_manager2
.IsComplete(framebuffer2
.get()));
2379 EXPECT_EQ(kCompleteValue
, framebuffer2
->IsPossiblyComplete());
2380 framebuffer_manager2
.MarkAsComplete(framebuffer2
.get());
2381 EXPECT_TRUE(framebuffer_manager2
.IsComplete(framebuffer2
.get()));
2383 // Change level for texture, both FBOs should be marked incomplete
2384 texture_manager1_
->SetLevelInfo(ref1
.get(),
2395 EXPECT_FALSE(framebuffer_manager1
.IsComplete(framebuffer1
.get()));
2396 EXPECT_EQ(kCompleteValue
, framebuffer1
->IsPossiblyComplete());
2397 framebuffer_manager1
.MarkAsComplete(framebuffer1
.get());
2398 EXPECT_TRUE(framebuffer_manager1
.IsComplete(framebuffer1
.get()));
2399 EXPECT_FALSE(framebuffer_manager2
.IsComplete(framebuffer2
.get()));
2400 EXPECT_EQ(kCompleteValue
, framebuffer2
->IsPossiblyComplete());
2401 framebuffer_manager2
.MarkAsComplete(framebuffer2
.get());
2402 EXPECT_TRUE(framebuffer_manager2
.IsComplete(framebuffer2
.get()));
2404 EXPECT_CALL(*gl_
, DeleteFramebuffersEXT(1, _
))
2406 .RetiresOnSaturation();
2407 framebuffer_manager1
.RemoveFramebuffer(10);
2408 framebuffer_manager2
.RemoveFramebuffer(20);
2409 EXPECT_CALL(*gl_
, DeleteTextures(1, _
))
2411 .RetiresOnSaturation();
2412 texture_manager1_
->RemoveTexture(10);
2413 texture_manager2_
->RemoveTexture(20);
2416 TEST_F(SharedTextureTest
, Memory
) {
2417 size_t initial_memory1
= memory_tracker1_
->GetSize(MemoryTracker::kUnmanaged
);
2418 size_t initial_memory2
= memory_tracker2_
->GetSize(MemoryTracker::kUnmanaged
);
2420 // Newly created texture is unrenderable.
2421 scoped_refptr
<TextureRef
> ref1
= texture_manager1_
->CreateTexture(10, 10);
2422 texture_manager1_
->SetTarget(ref1
.get(), GL_TEXTURE_2D
);
2423 texture_manager1_
->SetLevelInfo(ref1
.get(),
2435 EXPECT_LT(0u, ref1
->texture()->estimated_size());
2436 EXPECT_EQ(initial_memory1
+ ref1
->texture()->estimated_size(),
2437 memory_tracker1_
->GetSize(MemoryTracker::kUnmanaged
));
2439 // Associate new texture ref to other texture manager, it doesn't account for
2440 // the texture memory, the first memory tracker still has it.
2441 scoped_refptr
<TextureRef
> ref2
=
2442 texture_manager2_
->Consume(20, ref1
->texture());
2443 EXPECT_EQ(initial_memory1
+ ref1
->texture()->estimated_size(),
2444 memory_tracker1_
->GetSize(MemoryTracker::kUnmanaged
));
2445 EXPECT_EQ(initial_memory2
,
2446 memory_tracker2_
->GetSize(MemoryTracker::kUnmanaged
));
2448 // Delete the texture, memory should go to the remaining tracker.
2449 texture_manager1_
->RemoveTexture(10);
2451 EXPECT_EQ(initial_memory1
,
2452 memory_tracker1_
->GetSize(MemoryTracker::kUnmanaged
));
2453 EXPECT_EQ(initial_memory2
+ ref2
->texture()->estimated_size(),
2454 memory_tracker2_
->GetSize(MemoryTracker::kUnmanaged
));
2456 EXPECT_CALL(*gl_
, DeleteTextures(1, _
))
2458 .RetiresOnSaturation();
2460 texture_manager2_
->RemoveTexture(20);
2461 EXPECT_EQ(initial_memory2
,
2462 memory_tracker2_
->GetSize(MemoryTracker::kUnmanaged
));
2465 TEST_F(SharedTextureTest
, Images
) {
2466 scoped_refptr
<TextureRef
> ref1
= texture_manager1_
->CreateTexture(10, 10);
2467 scoped_refptr
<TextureRef
> ref2
=
2468 texture_manager2_
->Consume(20, ref1
->texture());
2470 texture_manager1_
->SetTarget(ref1
.get(), GL_TEXTURE_2D
);
2471 texture_manager1_
->SetLevelInfo(ref1
.get(),
2482 EXPECT_FALSE(ref1
->texture()->HasImages());
2483 EXPECT_FALSE(ref2
->texture()->HasImages());
2484 EXPECT_FALSE(texture_manager1_
->HaveImages());
2485 EXPECT_FALSE(texture_manager2_
->HaveImages());
2486 scoped_refptr
<gfx::GLImage
> image1(new gfx::GLImageStub
);
2487 texture_manager1_
->SetLevelImage(ref1
.get(), GL_TEXTURE_2D
, 1, image1
.get());
2488 EXPECT_TRUE(ref1
->texture()->HasImages());
2489 EXPECT_TRUE(ref2
->texture()->HasImages());
2490 EXPECT_TRUE(texture_manager1_
->HaveImages());
2491 EXPECT_TRUE(texture_manager2_
->HaveImages());
2492 scoped_refptr
<gfx::GLImage
> image2(new gfx::GLImageStub
);
2493 texture_manager1_
->SetLevelImage(ref1
.get(), GL_TEXTURE_2D
, 1, image2
.get());
2494 EXPECT_TRUE(ref1
->texture()->HasImages());
2495 EXPECT_TRUE(ref2
->texture()->HasImages());
2496 EXPECT_TRUE(texture_manager1_
->HaveImages());
2497 EXPECT_TRUE(texture_manager2_
->HaveImages());
2498 texture_manager1_
->SetLevelInfo(ref1
.get(),
2509 EXPECT_FALSE(ref1
->texture()->HasImages());
2510 EXPECT_FALSE(ref2
->texture()->HasImages());
2511 EXPECT_FALSE(texture_manager1_
->HaveImages());
2512 EXPECT_FALSE(texture_manager1_
->HaveImages());
2514 EXPECT_CALL(*gl_
, DeleteTextures(1, _
))
2516 .RetiresOnSaturation();
2517 texture_manager1_
->RemoveTexture(10);
2518 texture_manager2_
->RemoveTexture(20);
2521 } // namespace gles2