Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / gles2_cmd_decoder_unittest_1.cc
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.
4
5 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
6
7 #include "gpu/command_buffer/common/gles2_cmd_format.h"
8 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
9 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest_base.h"
10 #include "gpu/command_buffer/service/cmd_buffer_engine.h"
11 #include "gpu/command_buffer/service/context_group.h"
12 #include "gpu/command_buffer/service/program_manager.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gl/gl_mock.h"
15
16 using ::gfx::MockGLInterface;
17 using ::testing::_;
18 using ::testing::DoAll;
19 using ::testing::InSequence;
20 using ::testing::MatcherCast;
21 using ::testing::Pointee;
22 using ::testing::Return;
23 using ::testing::SetArrayArgument;
24 using ::testing::SetArgumentPointee;
25 using ::testing::StrEq;
26
27 namespace gpu {
28 namespace gles2 {
29
30 namespace {
31 void ShaderCacheCb(const std::string& key, const std::string& shader) {
32 }
33 }  // namespace
34
35 class GLES2DecoderTest1 : public GLES2DecoderTestBase {
36  public:
37   GLES2DecoderTest1() { }
38 };
39
40 template <>
41 void GLES2DecoderTestBase::SpecializedSetup<cmds::GenerateMipmap, 0>(
42     bool valid) {
43   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
44   DoTexImage2D(
45       GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
46       kSharedMemoryId, kSharedMemoryOffset);
47   if (valid) {
48     EXPECT_CALL(*gl_, TexParameteri(
49         GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST))
50         .Times(1)
51         .RetiresOnSaturation();
52     EXPECT_CALL(*gl_, TexParameteri(
53         GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR))
54         .Times(1)
55         .RetiresOnSaturation();
56     EXPECT_CALL(*gl_, GetError())
57         .WillOnce(Return(GL_NO_ERROR))
58         .WillOnce(Return(GL_NO_ERROR))
59         .RetiresOnSaturation();
60   }
61 };
62
63 template <>
64 void GLES2DecoderTestBase::SpecializedSetup<cmds::CheckFramebufferStatus, 0>(
65     bool /* valid */) {
66   // Give it a valid framebuffer.
67   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
68                     kServiceRenderbufferId);
69   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
70                     kServiceFramebufferId);
71   DoRenderbufferStorage(
72       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 1, 1, GL_NO_ERROR);
73   DoFramebufferRenderbuffer(
74       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
75       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
76 };
77
78 template <>
79 void GLES2DecoderTestBase::SpecializedSetup<cmds::Clear, 0>(bool valid) {
80   if (valid) {
81     SetupExpectationsForApplyingDefaultDirtyState();
82   }
83 };
84
85 template <>
86 void GLES2DecoderTestBase::SpecializedSetup<cmds::ColorMask, 0>(
87     bool /* valid */) {
88   // We bind a framebuffer color the colormask test since the framebuffer
89   // will be considered RGB.
90   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
91                     kServiceFramebufferId);
92 };
93
94 template <>
95 void GLES2DecoderTestBase::SpecializedSetup<cmds::CopyTexImage2D, 0>(
96     bool valid) {
97   if (valid) {
98     EXPECT_CALL(*gl_, GetError())
99         .WillOnce(Return(GL_NO_ERROR))
100         .WillOnce(Return(GL_NO_ERROR))
101         .RetiresOnSaturation();
102   }
103 };
104
105 template <>
106 void GLES2DecoderTestBase::SpecializedSetup<cmds::CopyTexSubImage2D, 0>(
107     bool valid) {
108   if (valid) {
109     DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
110     DoTexImage2D(
111         GL_TEXTURE_2D, 2, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
112         kSharedMemoryId, kSharedMemoryOffset);
113   }
114 };
115
116 template <>
117 void GLES2DecoderTestBase::SpecializedSetup<cmds::DetachShader, 0>(bool valid) {
118   if (valid) {
119     EXPECT_CALL(*gl_,
120                 AttachShader(kServiceProgramId, kServiceShaderId))
121         .Times(1)
122         .RetiresOnSaturation();
123     cmds::AttachShader attach_cmd;
124     attach_cmd.Init(client_program_id_, client_shader_id_);
125     EXPECT_EQ(error::kNoError, ExecuteCmd(attach_cmd));
126   }
127 };
128
129 template <>
130 void GLES2DecoderTestBase::SpecializedSetup<cmds::FramebufferRenderbuffer, 0>(
131     bool valid) {
132   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
133                     kServiceFramebufferId);
134   if (valid) {
135     EXPECT_CALL(*gl_, GetError())
136         .WillOnce(Return(GL_NO_ERROR))
137         .WillOnce(Return(GL_NO_ERROR))
138         .RetiresOnSaturation();
139   }
140 };
141
142 template <>
143 void GLES2DecoderTestBase::SpecializedSetup<cmds::FramebufferTexture2D, 0>(
144     bool valid) {
145   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
146                     kServiceFramebufferId);
147   if (valid) {
148     EXPECT_CALL(*gl_, GetError())
149         .WillOnce(Return(GL_NO_ERROR))
150         .WillOnce(Return(GL_NO_ERROR))
151         .RetiresOnSaturation();
152   }
153 };
154
155 template <>
156 void GLES2DecoderTestBase::SpecializedSetup<
157     cmds::GetBufferParameteriv, 0>(bool /* valid */) {
158   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
159 };
160
161 template <>
162 void GLES2DecoderTestBase::SpecializedSetup<
163     cmds::GetFramebufferAttachmentParameteriv, 0>(bool /* valid */) {
164   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
165                     kServiceFramebufferId);
166 };
167
168 template <>
169 void GLES2DecoderTestBase::SpecializedSetup<
170     cmds::GetRenderbufferParameteriv, 0>(
171         bool /* valid */) {
172   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
173                     kServiceRenderbufferId);
174 };
175
176 template <>
177 void GLES2DecoderTestBase::SpecializedSetup<cmds::GetProgramiv, 0>(
178     bool valid) {
179   if (valid) {
180     // GetProgramiv calls ClearGLError then GetError to make sure
181     // it actually got a value so it can report correctly to the client.
182     EXPECT_CALL(*gl_, GetError())
183         .WillOnce(Return(GL_NO_ERROR))
184         .RetiresOnSaturation();
185     EXPECT_CALL(*gl_, GetError())
186         .WillOnce(Return(GL_NO_ERROR))
187         .RetiresOnSaturation();
188   }
189 }
190
191 template <>
192 void GLES2DecoderTestBase::SpecializedSetup<cmds::GetProgramInfoLog, 0>(
193     bool /* valid */) {
194   const GLuint kClientVertexShaderId = 5001;
195   const GLuint kServiceVertexShaderId = 6001;
196   const GLuint kClientFragmentShaderId = 5002;
197   const GLuint kServiceFragmentShaderId = 6002;
198   const char* log = "hello";  // Matches auto-generated unit test.
199   DoCreateShader(
200       GL_VERTEX_SHADER, kClientVertexShaderId, kServiceVertexShaderId);
201   DoCreateShader(
202       GL_FRAGMENT_SHADER, kClientFragmentShaderId, kServiceFragmentShaderId);
203
204   GetShader(kClientVertexShaderId)->SetStatus(true, "", NULL);
205   GetShader(kClientFragmentShaderId)->SetStatus(true, "", NULL);
206
207   InSequence dummy;
208   EXPECT_CALL(*gl_,
209               AttachShader(kServiceProgramId, kServiceVertexShaderId))
210       .Times(1)
211       .RetiresOnSaturation();
212   EXPECT_CALL(*gl_,
213               AttachShader(kServiceProgramId, kServiceFragmentShaderId))
214       .Times(1)
215       .RetiresOnSaturation();
216   EXPECT_CALL(*gl_, LinkProgram(kServiceProgramId))
217       .Times(1)
218       .RetiresOnSaturation();
219   EXPECT_CALL(*gl_, GetProgramiv(kServiceProgramId, GL_LINK_STATUS, _))
220       .WillOnce(SetArgumentPointee<2>(1));
221   EXPECT_CALL(*gl_,
222       GetProgramiv(kServiceProgramId, GL_INFO_LOG_LENGTH, _))
223       .WillOnce(SetArgumentPointee<2>(strlen(log) + 1))
224       .RetiresOnSaturation();
225   EXPECT_CALL(*gl_,
226       GetProgramInfoLog(kServiceProgramId, strlen(log) + 1, _, _))
227       .WillOnce(DoAll(
228           SetArgumentPointee<2>(strlen(log)),
229           SetArrayArgument<3>(log, log + strlen(log) + 1)))
230       .RetiresOnSaturation();
231   EXPECT_CALL(*gl_, GetProgramiv(kServiceProgramId, GL_ACTIVE_ATTRIBUTES, _))
232       .WillOnce(SetArgumentPointee<2>(0));
233   EXPECT_CALL(
234       *gl_,
235       GetProgramiv(kServiceProgramId, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, _))
236       .WillOnce(SetArgumentPointee<2>(0));
237   EXPECT_CALL(*gl_, GetProgramiv(kServiceProgramId, GL_ACTIVE_UNIFORMS, _))
238       .WillOnce(SetArgumentPointee<2>(0));
239   EXPECT_CALL(
240       *gl_,
241       GetProgramiv(kServiceProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, _))
242       .WillOnce(SetArgumentPointee<2>(0));
243
244   Program* program = GetProgram(client_program_id_);
245   ASSERT_TRUE(program != NULL);
246
247   cmds::AttachShader attach_cmd;
248   attach_cmd.Init(client_program_id_, kClientVertexShaderId);
249   EXPECT_EQ(error::kNoError, ExecuteCmd(attach_cmd));
250
251   attach_cmd.Init(client_program_id_, kClientFragmentShaderId);
252   EXPECT_EQ(error::kNoError, ExecuteCmd(attach_cmd));
253
254   program->Link(NULL, NULL, NULL, Program::kCountOnlyStaticallyUsed,
255                 base::Bind(&ShaderCacheCb));
256 };
257
258 template <>
259 void GLES2DecoderTestBase::SpecializedSetup<cmds::GetVertexAttribfv, 0>(
260     bool valid) {
261   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
262   DoVertexAttribPointer(1, 1, GL_FLOAT, 0, 0);
263   if (valid) {
264     EXPECT_CALL(*gl_, GetError())
265         .WillOnce(Return(GL_NO_ERROR))
266         .WillOnce(Return(GL_NO_ERROR))
267         .RetiresOnSaturation();
268   }
269 };
270
271 template <>
272 void GLES2DecoderTestBase::SpecializedSetup<cmds::GetVertexAttribiv, 0>(
273     bool valid) {
274   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
275   DoVertexAttribPointer(1, 1, GL_FLOAT, 0, 0);
276   if (valid) {
277     EXPECT_CALL(*gl_, GetError())
278         .WillOnce(Return(GL_NO_ERROR))
279         .WillOnce(Return(GL_NO_ERROR))
280         .RetiresOnSaturation();
281   }
282 };
283
284 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest_1_autogen.h"
285
286 }  // namespace gles2
287 }  // namespace gpu
288