1 // Copyright 2014 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/gles2_cmd_decoder.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "gpu/command_buffer/common/gles2_cmd_format.h"
10 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
11 #include "gpu/command_buffer/common/id_allocator.h"
12 #include "gpu/command_buffer/service/async_pixel_transfer_delegate_mock.h"
13 #include "gpu/command_buffer/service/async_pixel_transfer_manager.h"
14 #include "gpu/command_buffer/service/async_pixel_transfer_manager_mock.h"
15 #include "gpu/command_buffer/service/cmd_buffer_engine.h"
16 #include "gpu/command_buffer/service/context_group.h"
17 #include "gpu/command_buffer/service/context_state.h"
18 #include "gpu/command_buffer/service/gl_surface_mock.h"
19 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest.h"
21 #include "gpu/command_buffer/service/gpu_switches.h"
22 #include "gpu/command_buffer/service/image_manager.h"
23 #include "gpu/command_buffer/service/mailbox_manager.h"
24 #include "gpu/command_buffer/service/mocks.h"
25 #include "gpu/command_buffer/service/program_manager.h"
26 #include "gpu/command_buffer/service/test_helper.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/gl/gl_implementation.h"
29 #include "ui/gl/gl_mock.h"
30 #include "ui/gl/gl_surface_stub.h"
32 #if !defined(GL_DEPTH24_STENCIL8)
33 #define GL_DEPTH24_STENCIL8 0x88F0
36 using ::gfx::MockGLInterface;
38 using ::testing::DoAll;
39 using ::testing::InSequence;
40 using ::testing::Invoke;
41 using ::testing::MatcherCast;
42 using ::testing::Mock;
43 using ::testing::Pointee;
44 using ::testing::Return;
45 using ::testing::SaveArg;
46 using ::testing::SetArrayArgument;
47 using ::testing::SetArgumentPointee;
48 using ::testing::SetArgPointee;
49 using ::testing::StrEq;
50 using ::testing::StrictMock;
57 TEST_P(GLES2DecoderWithShaderTest, GetVertexAttribPointervSucceeds) {
58 const float dummy = 0;
59 const GLuint kOffsetToTestFor = sizeof(dummy) * 4;
60 const GLuint kIndexToTest = 1;
61 GetVertexAttribPointerv::Result* result =
62 static_cast<GetVertexAttribPointerv::Result*>(shared_memory_address_);
64 const GLuint* result_value = result->GetData();
65 // Test that initial value is 0.
66 GetVertexAttribPointerv cmd;
67 cmd.Init(kIndexToTest,
68 GL_VERTEX_ATTRIB_ARRAY_POINTER,
70 shared_memory_offset_);
71 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
72 EXPECT_EQ(sizeof(*result_value), result->size);
73 EXPECT_EQ(0u, *result_value);
74 EXPECT_EQ(GL_NO_ERROR, GetGLError());
76 // Set the value and see that we get it.
78 DoVertexAttribPointer(kIndexToTest, 2, GL_FLOAT, 0, kOffsetToTestFor);
80 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
81 EXPECT_EQ(sizeof(*result_value), result->size);
82 EXPECT_EQ(kOffsetToTestFor, *result_value);
83 EXPECT_EQ(GL_NO_ERROR, GetGLError());
86 TEST_P(GLES2DecoderWithShaderTest, GetVertexAttribPointervBadArgsFails) {
87 const GLuint kIndexToTest = 1;
88 GetVertexAttribPointerv::Result* result =
89 static_cast<GetVertexAttribPointerv::Result*>(shared_memory_address_);
91 const GLuint* result_value = result->GetData();
92 // Test pname invalid fails.
93 GetVertexAttribPointerv cmd;
94 cmd.Init(kIndexToTest,
95 GL_VERTEX_ATTRIB_ARRAY_POINTER + 1,
97 shared_memory_offset_);
98 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
99 EXPECT_EQ(0u, result->size);
100 EXPECT_EQ(kInitialResult, *result_value);
101 EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
103 // Test index out of range fails.
105 cmd.Init(kNumVertexAttribs,
106 GL_VERTEX_ATTRIB_ARRAY_POINTER,
108 shared_memory_offset_);
109 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
110 EXPECT_EQ(0u, result->size);
111 EXPECT_EQ(kInitialResult, *result_value);
112 EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
114 // Test memory id bad fails.
115 cmd.Init(kIndexToTest,
116 GL_VERTEX_ATTRIB_ARRAY_POINTER,
117 kInvalidSharedMemoryId,
118 shared_memory_offset_);
119 EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
121 // Test memory offset bad fails.
122 cmd.Init(kIndexToTest,
123 GL_VERTEX_ATTRIB_ARRAY_POINTER,
125 kInvalidSharedMemoryOffset);
126 EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
129 TEST_P(GLES2DecoderWithShaderTest, BindBufferToDifferentTargetFails) {
130 // Bind the buffer to GL_ARRAY_BUFFER
131 DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
132 // Attempt to rebind to GL_ELEMENT_ARRAY_BUFFER
133 // NOTE: Real GLES2 does not have this restriction but WebGL and we do.
134 // This can be restriction can be removed at runtime.
135 EXPECT_CALL(*gl_, BindBuffer(_, _)).Times(0);
137 cmd.Init(GL_ELEMENT_ARRAY_BUFFER, client_buffer_id_);
138 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
139 EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
142 TEST_P(GLES2DecoderWithShaderTest, VertexAttribPointer) {
144 static const GLenum types[] = {
145 GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT,
146 GL_FLOAT, GL_FIXED, GL_INT, GL_UNSIGNED_INT,
148 static const GLsizei sizes[] = {
149 1, 1, 2, 2, 4, 4, 4, 4,
151 static const GLuint indices[] = {
152 0, 1, kNumVertexAttribs - 1, kNumVertexAttribs,
154 static const GLsizei offset_mult[] = {
157 static const GLsizei offset_offset[] = {
160 static const GLsizei stride_mult[] = {
161 -1, 0, 0, 1, 1, 2, 1000,
163 static const GLsizei stride_offset[] = {
166 for (size_t tt = 0; tt < arraysize(types); ++tt) {
167 GLenum type = types[tt];
168 GLsizei num_bytes = sizes[tt];
169 for (size_t ii = 0; ii < arraysize(indices); ++ii) {
170 GLuint index = indices[ii];
171 for (GLint size = 0; size < 5; ++size) {
172 for (size_t oo = 0; oo < arraysize(offset_mult); ++oo) {
173 GLuint offset = num_bytes * offset_mult[oo] + offset_offset[oo];
174 for (size_t ss = 0; ss < arraysize(stride_mult); ++ss) {
175 GLsizei stride = num_bytes * stride_mult[ss] + stride_offset[ss];
176 for (int normalize = 0; normalize < 2; ++normalize) {
177 bool index_good = index < static_cast<GLuint>(kNumVertexAttribs);
178 bool size_good = (size > 0 && size < 5);
179 bool offset_good = (offset % num_bytes == 0);
181 (stride % num_bytes == 0) && stride >= 0 && stride <= 255;
182 bool type_good = (type != GL_INT && type != GL_UNSIGNED_INT &&
184 bool good = size_good && offset_good && stride_good &&
185 type_good && index_good;
186 bool call = good && (type != GL_FIXED);
189 VertexAttribPointer(index,
194 BufferOffset(offset)));
196 VertexAttribPointer cmd;
197 cmd.Init(index, size, type, normalize, stride, offset);
198 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
200 EXPECT_EQ(GL_NO_ERROR, GetGLError());
201 } else if (size_good && offset_good && stride_good && type_good &&
203 EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
204 } else if (size_good && offset_good && stride_good &&
205 !type_good && index_good) {
206 EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
207 } else if (size_good && offset_good && !stride_good &&
208 type_good && index_good) {
209 if (stride < 0 || stride > 255) {
210 EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
212 EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
214 } else if (size_good && !offset_good && stride_good &&
215 type_good && index_good) {
216 EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
217 } else if (!size_good && offset_good && stride_good &&
218 type_good && index_good) {
219 EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
221 EXPECT_NE(GL_NO_ERROR, GetGLError());
231 class GLES2DecoderVertexArraysOESTest : public GLES2DecoderWithShaderTest {
233 GLES2DecoderVertexArraysOESTest() {}
235 bool vertex_array_deleted_manually_;
237 virtual void SetUp() {
239 init.extensions = "GL_OES_vertex_array_object";
240 init.gl_version = "opengl es 2.0";
241 init.bind_generates_resource = true;
243 SetupDefaultProgram();
245 AddExpectationsForGenVertexArraysOES();
246 GenHelper<GenVertexArraysOESImmediate>(client_vertexarray_id_);
248 vertex_array_deleted_manually_ = false;
251 virtual void TearDown() {
252 // This should only be set if the test handled deletion of the vertex array
253 // itself. Necessary because vertex_array_objects are not sharable, and thus
254 // not managed in the ContextGroup, meaning they will be destroyed during
256 if (!vertex_array_deleted_manually_) {
257 AddExpectationsForDeleteVertexArraysOES();
260 GLES2DecoderWithShaderTest::TearDown();
263 void GenVertexArraysOESValidArgs() {
264 AddExpectationsForGenVertexArraysOES();
265 GetSharedMemoryAs<GLuint*>()[0] = kNewClientId;
266 GenVertexArraysOES cmd;
267 cmd.Init(1, shared_memory_id_, shared_memory_offset_);
268 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
269 EXPECT_EQ(GL_NO_ERROR, GetGLError());
270 EXPECT_TRUE(GetVertexArrayInfo(kNewClientId) != NULL);
271 AddExpectationsForDeleteVertexArraysOES();
274 void GenVertexArraysOESInvalidArgs() {
275 EXPECT_CALL(*gl_, GenVertexArraysOES(_, _)).Times(0);
276 GetSharedMemoryAs<GLuint*>()[0] = client_vertexarray_id_;
277 GenVertexArraysOES cmd;
278 cmd.Init(1, shared_memory_id_, shared_memory_offset_);
279 EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd));
282 void GenVertexArraysOESImmediateValidArgs() {
283 AddExpectationsForGenVertexArraysOES();
284 GenVertexArraysOESImmediate* cmd =
285 GetImmediateAs<GenVertexArraysOESImmediate>();
286 GLuint temp = kNewClientId;
288 EXPECT_EQ(error::kNoError, ExecuteImmediateCmd(*cmd, sizeof(temp)));
289 EXPECT_EQ(GL_NO_ERROR, GetGLError());
290 EXPECT_TRUE(GetVertexArrayInfo(kNewClientId) != NULL);
291 AddExpectationsForDeleteVertexArraysOES();
294 void GenVertexArraysOESImmediateInvalidArgs() {
295 EXPECT_CALL(*gl_, GenVertexArraysOES(_, _)).Times(0);
296 GenVertexArraysOESImmediate* cmd =
297 GetImmediateAs<GenVertexArraysOESImmediate>();
298 cmd->Init(1, &client_vertexarray_id_);
299 EXPECT_EQ(error::kInvalidArguments,
300 ExecuteImmediateCmd(*cmd, sizeof(&client_vertexarray_id_)));
303 void DeleteVertexArraysOESValidArgs() {
304 AddExpectationsForDeleteVertexArraysOES();
305 GetSharedMemoryAs<GLuint*>()[0] = client_vertexarray_id_;
306 DeleteVertexArraysOES cmd;
307 cmd.Init(1, shared_memory_id_, shared_memory_offset_);
308 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
309 EXPECT_EQ(GL_NO_ERROR, GetGLError());
310 EXPECT_TRUE(GetVertexArrayInfo(client_vertexarray_id_) == NULL);
311 vertex_array_deleted_manually_ = true;
314 void DeleteVertexArraysOESInvalidArgs() {
315 GetSharedMemoryAs<GLuint*>()[0] = kInvalidClientId;
316 DeleteVertexArraysOES cmd;
317 cmd.Init(1, shared_memory_id_, shared_memory_offset_);
318 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
321 void DeleteVertexArraysOESImmediateValidArgs() {
322 AddExpectationsForDeleteVertexArraysOES();
323 DeleteVertexArraysOESImmediate& cmd =
324 *GetImmediateAs<DeleteVertexArraysOESImmediate>();
325 cmd.Init(1, &client_vertexarray_id_);
326 EXPECT_EQ(error::kNoError,
327 ExecuteImmediateCmd(cmd, sizeof(client_vertexarray_id_)));
328 EXPECT_EQ(GL_NO_ERROR, GetGLError());
329 EXPECT_TRUE(GetVertexArrayInfo(client_vertexarray_id_) == NULL);
330 vertex_array_deleted_manually_ = true;
333 void DeleteVertexArraysOESImmediateInvalidArgs() {
334 DeleteVertexArraysOESImmediate& cmd =
335 *GetImmediateAs<DeleteVertexArraysOESImmediate>();
336 GLuint temp = kInvalidClientId;
338 EXPECT_EQ(error::kNoError, ExecuteImmediateCmd(cmd, sizeof(temp)));
341 void DeleteBoundVertexArraysOESImmediateValidArgs() {
342 BindVertexArrayOESValidArgs();
344 AddExpectationsForDeleteBoundVertexArraysOES();
345 DeleteVertexArraysOESImmediate& cmd =
346 *GetImmediateAs<DeleteVertexArraysOESImmediate>();
347 cmd.Init(1, &client_vertexarray_id_);
348 EXPECT_EQ(error::kNoError,
349 ExecuteImmediateCmd(cmd, sizeof(client_vertexarray_id_)));
350 EXPECT_EQ(GL_NO_ERROR, GetGLError());
351 EXPECT_TRUE(GetVertexArrayInfo(client_vertexarray_id_) == NULL);
352 vertex_array_deleted_manually_ = true;
355 void IsVertexArrayOESValidArgs() {
356 IsVertexArrayOES cmd;
357 cmd.Init(client_vertexarray_id_, shared_memory_id_, shared_memory_offset_);
358 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
359 EXPECT_EQ(GL_NO_ERROR, GetGLError());
362 void IsVertexArrayOESInvalidArgsBadSharedMemoryId() {
363 IsVertexArrayOES cmd;
365 client_vertexarray_id_, kInvalidSharedMemoryId, shared_memory_offset_);
366 EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
368 client_vertexarray_id_, shared_memory_id_, kInvalidSharedMemoryOffset);
369 EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
372 void BindVertexArrayOESValidArgs() {
373 AddExpectationsForBindVertexArrayOES();
374 BindVertexArrayOES cmd;
375 cmd.Init(client_vertexarray_id_);
376 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
377 EXPECT_EQ(GL_NO_ERROR, GetGLError());
380 void BindVertexArrayOESValidArgsNewId() {
381 BindVertexArrayOES cmd;
382 cmd.Init(kNewClientId);
383 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
384 EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
388 INSTANTIATE_TEST_CASE_P(Service,
389 GLES2DecoderVertexArraysOESTest,
392 class GLES2DecoderEmulatedVertexArraysOESTest
393 : public GLES2DecoderVertexArraysOESTest {
395 GLES2DecoderEmulatedVertexArraysOESTest() {}
397 virtual void SetUp() {
399 init.gl_version = "3.0";
400 init.bind_generates_resource = true;
402 SetupDefaultProgram();
404 AddExpectationsForGenVertexArraysOES();
405 GenHelper<GenVertexArraysOESImmediate>(client_vertexarray_id_);
407 vertex_array_deleted_manually_ = false;
411 INSTANTIATE_TEST_CASE_P(Service,
412 GLES2DecoderEmulatedVertexArraysOESTest,
415 // Test vertex array objects with native support
416 TEST_P(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESValidArgs) {
417 GenVertexArraysOESValidArgs();
419 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest, GenVertexArraysOESValidArgs) {
420 GenVertexArraysOESValidArgs();
423 TEST_P(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESInvalidArgs) {
424 GenVertexArraysOESInvalidArgs();
426 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest, ) {
427 GenVertexArraysOESInvalidArgs();
430 TEST_P(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESImmediateValidArgs) {
431 GenVertexArraysOESImmediateValidArgs();
433 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
434 GenVertexArraysOESImmediateValidArgs) {
435 GenVertexArraysOESImmediateValidArgs();
438 TEST_P(GLES2DecoderVertexArraysOESTest,
439 GenVertexArraysOESImmediateInvalidArgs) {
440 GenVertexArraysOESImmediateInvalidArgs();
442 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
443 GenVertexArraysOESImmediateInvalidArgs) {
444 GenVertexArraysOESImmediateInvalidArgs();
447 TEST_P(GLES2DecoderVertexArraysOESTest, DeleteVertexArraysOESValidArgs) {
448 DeleteVertexArraysOESValidArgs();
450 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
451 DeleteVertexArraysOESValidArgs) {
452 DeleteVertexArraysOESValidArgs();
455 TEST_P(GLES2DecoderVertexArraysOESTest, DeleteVertexArraysOESInvalidArgs) {
456 DeleteVertexArraysOESInvalidArgs();
458 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
459 DeleteVertexArraysOESInvalidArgs) {
460 DeleteVertexArraysOESInvalidArgs();
463 TEST_P(GLES2DecoderVertexArraysOESTest,
464 DeleteVertexArraysOESImmediateValidArgs) {
465 DeleteVertexArraysOESImmediateValidArgs();
467 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
468 DeleteVertexArraysOESImmediateValidArgs) {
469 DeleteVertexArraysOESImmediateValidArgs();
472 TEST_P(GLES2DecoderVertexArraysOESTest,
473 DeleteVertexArraysOESImmediateInvalidArgs) {
474 DeleteVertexArraysOESImmediateInvalidArgs();
476 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
477 DeleteVertexArraysOESImmediateInvalidArgs) {
478 DeleteVertexArraysOESImmediateInvalidArgs();
481 TEST_P(GLES2DecoderVertexArraysOESTest,
482 DeleteBoundVertexArraysOESImmediateValidArgs) {
483 DeleteBoundVertexArraysOESImmediateValidArgs();
485 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
486 DeleteBoundVertexArraysOESImmediateValidArgs) {
487 DeleteBoundVertexArraysOESImmediateValidArgs();
490 TEST_P(GLES2DecoderVertexArraysOESTest, IsVertexArrayOESValidArgs) {
491 IsVertexArrayOESValidArgs();
493 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest, IsVertexArrayOESValidArgs) {
494 IsVertexArrayOESValidArgs();
497 TEST_P(GLES2DecoderVertexArraysOESTest,
498 IsVertexArrayOESInvalidArgsBadSharedMemoryId) {
499 IsVertexArrayOESInvalidArgsBadSharedMemoryId();
501 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
502 IsVertexArrayOESInvalidArgsBadSharedMemoryId) {
503 IsVertexArrayOESInvalidArgsBadSharedMemoryId();
506 TEST_P(GLES2DecoderVertexArraysOESTest, BindVertexArrayOESValidArgs) {
507 BindVertexArrayOESValidArgs();
509 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest, BindVertexArrayOESValidArgs) {
510 BindVertexArrayOESValidArgs();
513 TEST_P(GLES2DecoderVertexArraysOESTest, BindVertexArrayOESValidArgsNewId) {
514 BindVertexArrayOESValidArgsNewId();
516 TEST_P(GLES2DecoderEmulatedVertexArraysOESTest,
517 BindVertexArrayOESValidArgsNewId) {
518 BindVertexArrayOESValidArgsNewId();
521 TEST_P(GLES2DecoderTest, BufferDataGLError) {
522 GLenum target = GL_ARRAY_BUFFER;
524 DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
525 BufferManager* manager = group().buffer_manager();
526 Buffer* buffer = manager->GetBuffer(client_buffer_id_);
527 ASSERT_TRUE(buffer != NULL);
528 EXPECT_EQ(0, buffer->size());
529 EXPECT_CALL(*gl_, GetError())
530 .WillOnce(Return(GL_NO_ERROR))
531 .WillOnce(Return(GL_OUT_OF_MEMORY))
532 .RetiresOnSaturation();
533 EXPECT_CALL(*gl_, BufferData(target, size, _, GL_STREAM_DRAW))
535 .RetiresOnSaturation();
537 cmd.Init(target, size, 0, 0, GL_STREAM_DRAW);
538 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
539 EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
540 EXPECT_EQ(0, buffer->size());
543 // TODO(gman): BufferData
545 // TODO(gman): BufferDataImmediate
547 // TODO(gman): BufferSubData
549 // TODO(gman): BufferSubDataImmediate