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 #ifndef GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_
8 #include "gpu/command_buffer/common/gles2_cmd_format.h"
9 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
10 #include "gpu/command_buffer/service/buffer_manager.h"
11 #include "gpu/command_buffer/service/cmd_buffer_engine.h"
12 #include "gpu/command_buffer/service/context_group.h"
13 #include "gpu/command_buffer/service/framebuffer_manager.h"
14 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
15 #include "gpu/command_buffer/service/program_manager.h"
16 #include "gpu/command_buffer/service/query_manager.h"
17 #include "gpu/command_buffer/service/renderbuffer_manager.h"
18 #include "gpu/command_buffer/service/shader_manager.h"
19 #include "gpu/command_buffer/service/test_helper.h"
20 #include "gpu/command_buffer/service/texture_manager.h"
21 #include "gpu/command_buffer/service/vertex_array_manager.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gl/gl_context_stub_with_extensions.h"
24 #include "ui/gl/gl_surface_stub.h"
25 #include "ui/gl/gl_mock.h"
34 class GLES2DecoderTestBase : public testing::Test {
36 GLES2DecoderTestBase();
37 virtual ~GLES2DecoderTestBase();
39 // Template to call glGenXXX functions.
41 void GenHelper(GLuint client_id) {
42 int8 buffer[sizeof(T) + sizeof(client_id)];
43 T& cmd = *reinterpret_cast<T*>(&buffer);
44 cmd.Init(1, &client_id);
45 EXPECT_EQ(error::kNoError,
46 ExecuteImmediateCmd(cmd, sizeof(client_id)));
49 // This template exists solely so we can specialize it for
51 template <typename T, int id>
52 void SpecializedSetup(bool valid) {
57 return reinterpret_cast<T*>(immediate_buffer_);
60 template <typename T, typename Command>
61 T GetImmediateDataAs(Command* cmd) {
62 return reinterpret_cast<T>(ImmediateDataAddress(cmd));
65 void ClearSharedMemory() {
66 engine_->ClearSharedMemory();
69 virtual void SetUp() OVERRIDE;
70 virtual void TearDown() OVERRIDE;
73 error::Error ExecuteCmd(const T& cmd) {
74 COMPILE_ASSERT(T::kArgFlags == cmd::kFixed, Cmd_kArgFlags_not_kFixed);
75 return decoder_->DoCommand(cmd.kCmdId,
76 ComputeNumEntries(sizeof(cmd)) - 1,
81 error::Error ExecuteImmediateCmd(const T& cmd, size_t data_size) {
82 COMPILE_ASSERT(T::kArgFlags == cmd::kAtLeastN, Cmd_kArgFlags_not_kAtLeastN);
83 return decoder_->DoCommand(cmd.kCmdId,
84 ComputeNumEntries(sizeof(cmd) + data_size) - 1,
89 T GetSharedMemoryAs() {
90 return reinterpret_cast<T>(shared_memory_address_);
94 T GetSharedMemoryAsWithOffset(uint32 offset) {
95 void* ptr = reinterpret_cast<int8*>(shared_memory_address_) + offset;
96 return reinterpret_cast<T>(ptr);
99 IdAllocatorInterface* GetIdAllocator(GLuint namespace_id) {
100 return group_->GetIdAllocator(namespace_id);
103 Buffer* GetBuffer(GLuint service_id) {
104 return group_->buffer_manager()->GetBuffer(service_id);
107 Framebuffer* GetFramebuffer(GLuint service_id) {
108 return group_->framebuffer_manager()->GetFramebuffer(service_id);
111 Renderbuffer* GetRenderbuffer(
113 return group_->renderbuffer_manager()->GetRenderbuffer(service_id);
116 TextureRef* GetTexture(GLuint client_id) {
117 return group_->texture_manager()->GetTexture(client_id);
120 Shader* GetShader(GLuint client_id) {
121 return group_->shader_manager()->GetShader(client_id);
124 Program* GetProgram(GLuint client_id) {
125 return group_->program_manager()->GetProgram(client_id);
128 QueryManager::Query* GetQueryInfo(GLuint client_id) {
129 return decoder_->GetQueryManager()->GetQuery(client_id);
132 // This name doesn't match the underlying function, but doing it this way
133 // prevents the need to special-case the unit test generation
134 VertexAttribManager* GetVertexArrayInfo(GLuint client_id) {
135 return decoder_->GetVertexArrayManager()->GetVertexAttribManager(client_id);
138 ProgramManager* program_manager() {
139 return group_->program_manager();
142 void DoCreateProgram(GLuint client_id, GLuint service_id);
143 void DoCreateShader(GLenum shader_type, GLuint client_id, GLuint service_id);
145 void SetBucketAsCString(uint32 bucket_id, const char* str);
147 void set_memory_tracker(MemoryTracker* memory_tracker) {
148 memory_tracker_ = memory_tracker;
152 const char* extensions,
153 const char* gl_version,
159 bool request_stencil,
160 bool bind_generates_resource);
162 void InitDecoderWithCommandLine(
163 const char* extensions,
164 const char* gl_version,
170 bool request_stencil,
171 bool bind_generates_resource,
172 const CommandLine* command_line);
174 const ContextGroup& group() const {
175 return *group_.get();
178 ::testing::StrictMock< ::gfx::MockGLInterface>* GetGLMock() const {
182 GLES2Decoder* GetDecoder() const {
183 return decoder_.get();
186 typedef TestHelper::AttribInfo AttribInfo;
187 typedef TestHelper::UniformInfo UniformInfo;
190 AttribInfo* attribs, size_t num_attribs,
191 UniformInfo* uniforms, size_t num_uniforms,
192 GLuint client_id, GLuint service_id,
193 GLuint vertex_shader_client_id, GLuint vertex_shader_service_id,
194 GLuint fragment_shader_client_id, GLuint fragment_shader_service_id);
196 void SetupExpectationsForClearingUniforms(
197 UniformInfo* uniforms, size_t num_uniforms) {
198 TestHelper::SetupExpectationsForClearingUniforms(
199 gl_.get(), uniforms, num_uniforms);
202 void SetupInitCapabilitiesExpectations();
203 void SetupInitStateExpectations();
204 void ExpectEnableDisable(GLenum cap, bool enable);
206 // Setups up a shader for testing glUniform.
207 void SetupShaderForUniform(GLenum uniform_type);
208 void SetupDefaultProgram();
209 void SetupCubemapProgram();
210 void SetupSamplerExternalProgram();
213 // Note that the error is returned as GLint instead of GLenum.
214 // This is because there is a mismatch in the types of GLenum and
215 // the error values GL_NO_ERROR, GL_INVALID_ENUM, etc. GLenum is
216 // typedef'd as unsigned int while the error values are defined as
217 // integers. This is problematic for template functions such as
218 // EXPECT_EQ that expect both types to be the same.
221 void DoBindBuffer(GLenum target, GLuint client_id, GLuint service_id);
222 void DoBindFramebuffer(GLenum target, GLuint client_id, GLuint service_id);
223 void DoBindRenderbuffer(GLenum target, GLuint client_id, GLuint service_id);
224 void DoBindTexture(GLenum target, GLuint client_id, GLuint service_id);
225 void DoBindVertexArrayOES(GLuint client_id, GLuint service_id);
227 bool DoIsBuffer(GLuint client_id);
228 bool DoIsFramebuffer(GLuint client_id);
229 bool DoIsProgram(GLuint client_id);
230 bool DoIsRenderbuffer(GLuint client_id);
231 bool DoIsShader(GLuint client_id);
232 bool DoIsTexture(GLuint client_id);
234 void DoDeleteBuffer(GLuint client_id, GLuint service_id);
235 void DoDeleteFramebuffer(
236 GLuint client_id, GLuint service_id,
237 bool reset_draw, GLenum draw_target, GLuint draw_id,
238 bool reset_read, GLenum read_target, GLuint read_id);
239 void DoDeleteProgram(GLuint client_id, GLuint service_id);
240 void DoDeleteRenderbuffer(GLuint client_id, GLuint service_id);
241 void DoDeleteShader(GLuint client_id, GLuint service_id);
242 void DoDeleteTexture(GLuint client_id, GLuint service_id);
244 void DoCompressedTexImage2D(
245 GLenum target, GLint level, GLenum format,
246 GLsizei width, GLsizei height, GLint border,
247 GLsizei size, uint32 bucket_id);
249 GLenum target, GLint level, GLenum internal_format,
250 GLsizei width, GLsizei height, GLint border,
251 GLenum format, GLenum type,
252 uint32 shared_memory_id, uint32 shared_memory_offset);
253 void DoRenderbufferStorage(
254 GLenum target, GLenum internal_format, GLenum actual_format,
255 GLsizei width, GLsizei height, GLenum error);
256 void DoFramebufferRenderbuffer(
259 GLenum renderbuffer_target,
260 GLuint renderbuffer_client_id,
261 GLuint renderbuffer_service_id,
263 void DoFramebufferTexture2D(
264 GLenum target, GLenum attachment, GLenum tex_target,
265 GLuint texture_client_id, GLuint texture_service_id,
266 GLint level, GLenum error);
267 void DoVertexAttribPointer(
268 GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset);
269 void DoVertexAttribDivisorANGLE(GLuint index, GLuint divisor);
271 void DoEnableVertexAttribArray(GLint index);
273 void DoBufferData(GLenum target, GLsizei size);
275 void DoBufferSubData(
276 GLenum target, GLint offset, GLsizei size, const void* data);
278 void SetupVertexBuffer();
279 void SetupAllNeededVertexBuffers();
281 void SetupIndexBuffer();
283 void DeleteVertexBuffer();
285 void DeleteIndexBuffer();
287 void SetupClearTextureExpections(
289 GLuint old_service_id,
298 void SetupExpectationsForRestoreClearState(
299 GLclampf restore_red,
300 GLclampf restore_green,
301 GLclampf restore_blue,
302 GLclampf restore_alpha,
303 GLuint restore_stencil,
304 GLclampf restore_depth,
305 bool restore_scissor_test);
307 void SetupExpectationsForFramebufferClearing(
310 GLclampf restore_red,
311 GLclampf restore_green,
312 GLclampf restore_blue,
313 GLclampf restore_alpha,
314 GLuint restore_stencil,
315 GLclampf restore_depth,
316 bool restore_scissor_test);
318 void SetupExpectationsForFramebufferClearingMulti(
319 GLuint read_framebuffer_service_id,
320 GLuint draw_framebuffer_service_id,
323 GLclampf restore_red,
324 GLclampf restore_green,
325 GLclampf restore_blue,
326 GLclampf restore_alpha,
327 GLuint restore_stencil,
328 GLclampf restore_depth,
329 bool restore_scissor_test);
331 void SetupExpectationsForApplyingDirtyState(
332 bool framebuffer_is_rgb,
333 bool framebuffer_has_depth,
334 bool framebuffer_has_stencil,
335 GLuint color_bits, // NOTE! bits are 0x1000, 0x0100, 0x0010, and 0x0001
338 GLuint front_stencil_mask,
339 GLuint back_stencil_mask,
340 bool stencil_enabled,
341 bool cull_face_enabled,
342 bool scissor_test_enabled,
345 void SetupExpectationsForApplyingDefaultDirtyState();
347 void AddExpectationsForSimulatedAttrib0WithError(
348 GLsizei num_vertices, GLuint buffer_id, GLenum error);
350 void AddExpectationsForSimulatedAttrib0(
351 GLsizei num_vertices, GLuint buffer_id);
353 void AddExpectationsForGenVertexArraysOES();
354 void AddExpectationsForDeleteVertexArraysOES();
355 void AddExpectationsForBindVertexArrayOES();
356 void AddExpectationsForRestoreAttribState(GLuint attrib);
358 GLvoid* BufferOffset(unsigned i) {
359 return static_cast<int8 *>(NULL)+(i);
362 template <typename Command, typename Result>
363 bool IsObjectHelper(GLuint client_id) {
364 Result* result = static_cast<Result*>(shared_memory_address_);
366 cmd.Init(client_id, kSharedMemoryId, kSharedMemoryOffset);
367 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
368 bool isObject = static_cast<bool>(*result);
369 EXPECT_EQ(GL_NO_ERROR, GetGLError());
374 static const int kBackBufferWidth = 128;
375 static const int kBackBufferHeight = 64;
377 static const GLint kMaxTextureSize = 2048;
378 static const GLint kMaxCubeMapTextureSize = 256;
379 static const GLint kNumVertexAttribs = 16;
380 static const GLint kNumTextureUnits = 8;
381 static const GLint kMaxTextureImageUnits = 8;
382 static const GLint kMaxVertexTextureImageUnits = 2;
383 static const GLint kMaxFragmentUniformVectors = 16;
384 static const GLint kMaxVaryingVectors = 8;
385 static const GLint kMaxVertexUniformVectors = 128;
386 static const GLint kMaxViewportWidth = 8192;
387 static const GLint kMaxViewportHeight = 8192;
389 static const GLint kViewportX = 0;
390 static const GLint kViewportY = 0;
391 static const GLint kViewportWidth = kBackBufferWidth;
392 static const GLint kViewportHeight = kBackBufferHeight;
394 static const GLuint kServiceAttrib0BufferId = 801;
395 static const GLuint kServiceFixedAttribBufferId = 802;
397 static const GLuint kServiceBufferId = 301;
398 static const GLuint kServiceFramebufferId = 302;
399 static const GLuint kServiceRenderbufferId = 303;
400 static const GLuint kServiceTextureId = 304;
401 static const GLuint kServiceProgramId = 305;
402 static const GLuint kServiceShaderId = 306;
403 static const GLuint kServiceElementBufferId = 308;
404 static const GLuint kServiceQueryId = 309;
405 static const GLuint kServiceVertexArrayId = 310;
407 static const int32 kSharedMemoryId = 401;
408 static const size_t kSharedBufferSize = 2048;
409 static const uint32 kSharedMemoryOffset = 132;
410 static const int32 kInvalidSharedMemoryId = 402;
411 static const uint32 kInvalidSharedMemoryOffset = kSharedBufferSize + 1;
412 static const uint32 kInitialResult = 0xBDBDBDBDu;
413 static const uint8 kInitialMemoryValue = 0xBDu;
415 static const uint32 kNewClientId = 501;
416 static const uint32 kNewServiceId = 502;
417 static const uint32 kInvalidClientId = 601;
419 static const GLuint kServiceVertexShaderId = 321;
420 static const GLuint kServiceFragmentShaderId = 322;
422 static const GLuint kServiceCopyTextureChromiumShaderId = 701;
423 static const GLuint kServiceCopyTextureChromiumProgramId = 721;
425 static const GLuint kServiceCopyTextureChromiumTextureBufferId = 751;
426 static const GLuint kServiceCopyTextureChromiumVertexBufferId = 752;
427 static const GLuint kServiceCopyTextureChromiumFBOId = 753;
428 static const GLuint kServiceCopyTextureChromiumPositionAttrib = 761;
429 static const GLuint kServiceCopyTextureChromiumTexAttrib = 762;
430 static const GLuint kServiceCopyTextureChromiumSamplerLocation = 763;
432 static const GLsizei kNumVertices = 100;
433 static const GLsizei kNumIndices = 10;
434 static const int kValidIndexRangeStart = 1;
435 static const int kValidIndexRangeCount = 7;
436 static const int kInvalidIndexRangeStart = 0;
437 static const int kInvalidIndexRangeCount = 7;
438 static const int kOutOfRangeIndexRangeEnd = 10;
439 static const GLuint kMaxValidIndex = 7;
441 static const GLint kMaxAttribLength = 10;
442 static const char* kAttrib1Name;
443 static const char* kAttrib2Name;
444 static const char* kAttrib3Name;
445 static const GLint kAttrib1Size = 1;
446 static const GLint kAttrib2Size = 1;
447 static const GLint kAttrib3Size = 1;
448 static const GLint kAttrib1Location = 0;
449 static const GLint kAttrib2Location = 1;
450 static const GLint kAttrib3Location = 2;
451 static const GLenum kAttrib1Type = GL_FLOAT_VEC4;
452 static const GLenum kAttrib2Type = GL_FLOAT_VEC2;
453 static const GLenum kAttrib3Type = GL_FLOAT_VEC3;
454 static const GLint kInvalidAttribLocation = 30;
455 static const GLint kBadAttribIndex = kNumVertexAttribs;
457 static const GLint kMaxUniformLength = 12;
458 static const char* kUniform1Name;
459 static const char* kUniform2Name;
460 static const char* kUniform3Name;
461 static const GLint kUniform1Size = 1;
462 static const GLint kUniform2Size = 3;
463 static const GLint kUniform3Size = 2;
464 static const GLint kUniform1RealLocation = 3;
465 static const GLint kUniform2RealLocation = 10;
466 static const GLint kUniform2ElementRealLocation = 12;
467 static const GLint kUniform3RealLocation = 20;
468 static const GLint kUniform1FakeLocation = 0; // These are
469 static const GLint kUniform2FakeLocation = 1; // hardcoded
470 static const GLint kUniform2ElementFakeLocation = 0x10001; // to match
471 static const GLint kUniform3FakeLocation = 2; // ProgramManager.
472 static const GLint kUniform1DesiredLocation = -1;
473 static const GLint kUniform2DesiredLocation = -1;
474 static const GLint kUniform3DesiredLocation = -1;
475 static const GLenum kUniform1Type = GL_SAMPLER_2D;
476 static const GLenum kUniform2Type = GL_INT_VEC2;
477 static const GLenum kUniform3Type = GL_FLOAT_VEC3;
478 static const GLenum kUniformSamplerExternalType = GL_SAMPLER_EXTERNAL_OES;
479 static const GLenum kUniformCubemapType = GL_SAMPLER_CUBE;
480 static const GLint kInvalidUniformLocation = 30;
481 static const GLint kBadUniformIndex = 1000;
483 // Use StrictMock to make 100% sure we know how GL will be called.
484 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
485 scoped_refptr<gfx::GLSurfaceStub> surface_;
486 scoped_refptr<gfx::GLContextStubWithExtensions> context_;
487 scoped_ptr<GLES2Decoder> mock_decoder_;
488 scoped_ptr<GLES2Decoder> decoder_;
489 MemoryTracker* memory_tracker_;
491 GLuint client_buffer_id_;
492 GLuint client_framebuffer_id_;
493 GLuint client_program_id_;
494 GLuint client_renderbuffer_id_;
495 GLuint client_shader_id_;
496 GLuint client_texture_id_;
497 GLuint client_element_buffer_id_;
498 GLuint client_vertex_shader_id_;
499 GLuint client_fragment_shader_id_;
500 GLuint client_query_id_;
501 GLuint client_vertexarray_id_;
503 uint32 shared_memory_id_;
504 uint32 shared_memory_offset_;
505 void* shared_memory_address_;
506 void* shared_memory_base_;
508 int8 immediate_buffer_[256];
511 class MockCommandBufferEngine : public CommandBufferEngine {
513 MockCommandBufferEngine();
515 virtual ~MockCommandBufferEngine();
517 virtual gpu::Buffer GetSharedMemoryBuffer(int32 shm_id) OVERRIDE;
519 void ClearSharedMemory() {
520 memset(data_.get(), kInitialMemoryValue, kSharedBufferSize);
523 virtual void set_token(int32 token) OVERRIDE;
525 virtual bool SetGetBuffer(int32 /* transfer_buffer_id */) OVERRIDE;
527 // Overridden from CommandBufferEngine.
528 virtual bool SetGetOffset(int32 offset) OVERRIDE;
530 // Overridden from CommandBufferEngine.
531 virtual int32 GetGetOffset() OVERRIDE;
534 scoped_ptr<int8[]> data_;
535 gpu::Buffer valid_buffer_;
536 gpu::Buffer invalid_buffer_;
539 void AddExpectationsForVertexAttribManager();
541 scoped_ptr< ::testing::StrictMock<MockCommandBufferEngine> > engine_;
542 scoped_refptr<ContextGroup> group_;
545 class GLES2DecoderWithShaderTestBase : public GLES2DecoderTestBase {
547 GLES2DecoderWithShaderTestBase()
548 : GLES2DecoderTestBase() {
552 virtual void SetUp() OVERRIDE;
553 virtual void TearDown() OVERRIDE;
560 #endif // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_