1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/shader_manager.h"
#include "base/memory/scoped_ptr.h"
#include "gpu/command_buffer/common/gl_mock.h"
#include "gpu/command_buffer/service/mocks.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Return;
using ::testing::ReturnRef;
namespace gpu {
namespace gles2 {
class ShaderManagerTest : public testing::Test {
public:
ShaderManagerTest() {
}
~ShaderManagerTest() {
manager_.Destroy(false);
}
protected:
virtual void SetUp() {
gl_.reset(new ::testing::StrictMock< ::gfx::MockGLInterface>());
::gfx::GLInterface::SetGLInterface(gl_.get());
}
virtual void TearDown() {
::gfx::GLInterface::SetGLInterface(NULL);
gl_.reset();
}
// Use StrictMock to make 100% sure we know how GL will be called.
scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
ShaderManager manager_;
};
TEST_F(ShaderManagerTest, Basic) {
const GLuint kClient1Id = 1;
const GLuint kService1Id = 11;
const GLenum kShader1Type = GL_VERTEX_SHADER;
const GLuint kClient2Id = 2;
// Check we can create shader.
ShaderManager::ShaderInfo* info0 = manager_.CreateShaderInfo(
kClient1Id, kService1Id, kShader1Type);
// Check shader got created.
ASSERT_TRUE(info0 != NULL);
ShaderManager::ShaderInfo* info1 = manager_.GetShaderInfo(kClient1Id);
ASSERT_EQ(info0, info1);
// Check we get nothing for a non-existent shader.
EXPECT_TRUE(manager_.GetShaderInfo(kClient2Id) == NULL);
// Check we can't get the shader after we remove it.
manager_.MarkAsDeleted(info1);
EXPECT_TRUE(manager_.GetShaderInfo(kClient1Id) == NULL);
}
TEST_F(ShaderManagerTest, Destroy) {
const GLuint kClient1Id = 1;
const GLuint kService1Id = 11;
const GLenum kShader1Type = GL_VERTEX_SHADER;
// Check we can create shader.
ShaderManager::ShaderInfo* info1 = manager_.CreateShaderInfo(
kClient1Id, kService1Id, kShader1Type);
// Check shader got created.
ASSERT_TRUE(info1 != NULL);
EXPECT_CALL(*gl_, DeleteShader(kService1Id))
.Times(1)
.RetiresOnSaturation();
manager_.Destroy(true);
// Check that resources got freed.
info1 = manager_.GetShaderInfo(kClient1Id);
ASSERT_TRUE(info1 == NULL);
}
TEST_F(ShaderManagerTest, DeleteBug) {
const GLuint kClient1Id = 1;
const GLuint kClient2Id = 2;
const GLuint kService1Id = 11;
const GLuint kService2Id = 12;
const GLenum kShaderType = GL_VERTEX_SHADER;
// Check we can create shader.
ShaderManager::ShaderInfo::Ref info1(
manager_.CreateShaderInfo(kClient1Id, kService1Id, kShaderType));
ShaderManager::ShaderInfo::Ref info2(
manager_.CreateShaderInfo(kClient2Id, kService2Id, kShaderType));
ASSERT_TRUE(info1);
ASSERT_TRUE(info2);
manager_.UseShader(info1);
manager_.MarkAsDeleted(info1);
manager_.MarkAsDeleted(info2);
EXPECT_TRUE(manager_.IsOwned(info1));
EXPECT_FALSE(manager_.IsOwned(info2));
}
TEST_F(ShaderManagerTest, ShaderInfo) {
const GLuint kClient1Id = 1;
const GLuint kService1Id = 11;
const GLenum kShader1Type = GL_VERTEX_SHADER;
const char* kClient1Source = "hello world";
// Check we can create shader.
ShaderManager::ShaderInfo* info1 = manager_.CreateShaderInfo(
kClient1Id, kService1Id, kShader1Type);
// Check shader got created.
ASSERT_TRUE(info1 != NULL);
EXPECT_EQ(kService1Id, info1->service_id());
// Check if the shader has correct type.
EXPECT_EQ(kShader1Type, info1->shader_type());
EXPECT_FALSE(info1->IsValid());
EXPECT_FALSE(info1->InUse());
EXPECT_TRUE(info1->source() == NULL);
EXPECT_TRUE(info1->log_info() == NULL);
const char* kLog = "foo";
info1->SetStatus(true, kLog, NULL);
EXPECT_TRUE(info1->IsValid());
EXPECT_STREQ(kLog, info1->log_info()->c_str());
// Check we can set its source.
info1->UpdateSource(kClient1Source);
EXPECT_STREQ(kClient1Source, info1->source()->c_str());
EXPECT_EQ(NULL, info1->translated_source());
// Check we can set its translated source.
info1->UpdateTranslatedSource(kClient1Source);
EXPECT_STREQ(kClient1Source,
info1->translated_source()->c_str());
}
TEST_F(ShaderManagerTest, GetInfo) {
const GLuint kClient1Id = 1;
const GLuint kService1Id = 11;
const GLenum kShader1Type = GL_VERTEX_SHADER;
const GLenum kAttrib1Type = GL_FLOAT_VEC2;
const GLsizei kAttrib1Size = 2;
const char* kAttrib1Name = "attr1";
const GLenum kAttrib2Type = GL_FLOAT_VEC3;
const GLsizei kAttrib2Size = 4;
const char* kAttrib2Name = "attr2";
const GLenum kUniform1Type = GL_FLOAT_MAT2;
const GLsizei kUniform1Size = 3;
const char* kUniform1Name = "uni1";
const GLenum kUniform2Type = GL_FLOAT_MAT3;
const GLsizei kUniform2Size = 5;
const char* kUniform2Name = "uni2";
MockShaderTranslator shader_translator;
ShaderTranslator::VariableMap attrib_map;
attrib_map[kAttrib1Name] = ShaderTranslatorInterface::VariableInfo(
kAttrib1Type, kAttrib1Size, kAttrib1Name);
attrib_map[kAttrib2Name] = ShaderTranslatorInterface::VariableInfo(
kAttrib2Type, kAttrib2Size, kAttrib2Name);
ShaderTranslator::VariableMap uniform_map;
uniform_map[kUniform1Name] = ShaderTranslatorInterface::VariableInfo(
kUniform1Type, kUniform1Size, kUniform1Name);
uniform_map[kUniform2Name] = ShaderTranslatorInterface::VariableInfo(
kUniform2Type, kUniform2Size, kUniform2Name);
EXPECT_CALL(shader_translator, attrib_map())
.WillRepeatedly(ReturnRef(attrib_map));
EXPECT_CALL(shader_translator, uniform_map())
.WillRepeatedly(ReturnRef(uniform_map));
// Check we can create shader.
ShaderManager::ShaderInfo* info1 = manager_.CreateShaderInfo(
kClient1Id, kService1Id, kShader1Type);
// Check shader got created.
ASSERT_TRUE(info1 != NULL);
// Set Status
info1->SetStatus(true, "", &shader_translator);
// Check attrib and uniform infos got copied.
for (ShaderTranslator::VariableMap::const_iterator it = attrib_map.begin();
it != attrib_map.end(); ++it) {
const ShaderManager::ShaderInfo::VariableInfo* variable_info =
info1->GetAttribInfo(it->first);
ASSERT_TRUE(variable_info != NULL);
EXPECT_EQ(it->second.type, variable_info->type);
EXPECT_EQ(it->second.size, variable_info->size);
EXPECT_EQ(it->second.name, variable_info->name);
}
for (ShaderTranslator::VariableMap::const_iterator it = uniform_map.begin();
it != uniform_map.end(); ++it) {
const ShaderManager::ShaderInfo::VariableInfo* variable_info =
info1->GetUniformInfo(it->first);
ASSERT_TRUE(variable_info != NULL);
EXPECT_EQ(it->second.type, variable_info->type);
EXPECT_EQ(it->second.size, variable_info->size);
EXPECT_EQ(it->second.name, variable_info->name);
}
// Check attrib and uniform get cleared.
info1->SetStatus(true, NULL, NULL);
EXPECT_TRUE(info1->log_info() == NULL);
for (ShaderTranslator::VariableMap::const_iterator it = attrib_map.begin();
it != attrib_map.end(); ++it) {
const ShaderManager::ShaderInfo::VariableInfo* variable_info =
info1->GetAttribInfo(it->first);
EXPECT_TRUE(variable_info == NULL);
}
for (ShaderTranslator::VariableMap::const_iterator it = uniform_map.begin();
it != uniform_map.end(); ++it) {
const ShaderManager::ShaderInfo::VariableInfo* variable_info =
info1->GetUniformInfo(it->first);
ASSERT_TRUE(variable_info == NULL);
}
}
TEST_F(ShaderManagerTest, ShaderInfoUseCount) {
const GLuint kClient1Id = 1;
const GLuint kService1Id = 11;
const GLenum kShader1Type = GL_VERTEX_SHADER;
// Check we can create shader.
ShaderManager::ShaderInfo* info1 = manager_.CreateShaderInfo(
kClient1Id, kService1Id, kShader1Type);
// Check shader got created.
ASSERT_TRUE(info1 != NULL);
EXPECT_FALSE(info1->InUse());
EXPECT_FALSE(info1->IsDeleted());
manager_.UseShader(info1);
EXPECT_TRUE(info1->InUse());
manager_.UseShader(info1);
EXPECT_TRUE(info1->InUse());
manager_.MarkAsDeleted(info1);
EXPECT_TRUE(info1->IsDeleted());
ShaderManager::ShaderInfo* info2 = manager_.GetShaderInfo(kClient1Id);
EXPECT_EQ(info1, info2);
manager_.UnuseShader(info1);
EXPECT_TRUE(info1->InUse());
manager_.UnuseShader(info1); // this should delete the info.
info2 = manager_.GetShaderInfo(kClient1Id);
EXPECT_TRUE(info2 == NULL);
info1 = manager_.CreateShaderInfo(kClient1Id, kService1Id, kShader1Type);
ASSERT_TRUE(info1 != NULL);
EXPECT_FALSE(info1->InUse());
manager_.UseShader(info1);
EXPECT_TRUE(info1->InUse());
manager_.UseShader(info1);
EXPECT_TRUE(info1->InUse());
manager_.UnuseShader(info1);
EXPECT_TRUE(info1->InUse());
manager_.UnuseShader(info1);
EXPECT_FALSE(info1->InUse());
info2 = manager_.GetShaderInfo(kClient1Id);
EXPECT_EQ(info1, info2);
manager_.MarkAsDeleted(info1); // this should delete the shader.
info2 = manager_.GetShaderInfo(kClient1Id);
EXPECT_TRUE(info2 == NULL);
}
} // namespace gles2
} // namespace gpu
|