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"
36 class GLES2DecoderTestBase : public testing::Test {
38 GLES2DecoderTestBase();
39 virtual ~GLES2DecoderTestBase();
41 // Template to call glGenXXX functions.
43 void GenHelper(GLuint client_id) {
44 int8 buffer[sizeof(T) + sizeof(client_id)];
45 T& cmd = *reinterpret_cast<T*>(&buffer);
46 cmd.Init(1, &client_id);
47 EXPECT_EQ(error::kNoError,
48 ExecuteImmediateCmd(cmd, sizeof(client_id)));
51 // This template exists solely so we can specialize it for
53 template <typename T, int id>
54 void SpecializedSetup(bool valid) {
59 return reinterpret_cast<T*>(immediate_buffer_);
62 template <typename T, typename Command>
63 T GetImmediateDataAs(Command* cmd) {
64 return reinterpret_cast<T>(ImmediateDataAddress(cmd));
67 void ClearSharedMemory() {
68 engine_->ClearSharedMemory();
71 virtual void SetUp() OVERRIDE;
72 virtual void TearDown() OVERRIDE;
75 error::Error ExecuteCmd(const T& cmd) {
76 COMPILE_ASSERT(T::kArgFlags == cmd::kFixed, Cmd_kArgFlags_not_kFixed);
77 return decoder_->DoCommand(cmd.kCmdId,
78 ComputeNumEntries(sizeof(cmd)) - 1,
83 error::Error ExecuteImmediateCmd(const T& cmd, size_t data_size) {
84 COMPILE_ASSERT(T::kArgFlags == cmd::kAtLeastN, Cmd_kArgFlags_not_kAtLeastN);
85 return decoder_->DoCommand(cmd.kCmdId,
86 ComputeNumEntries(sizeof(cmd) + data_size) - 1,
91 T GetSharedMemoryAs() {
92 return reinterpret_cast<T>(shared_memory_address_);
96 T GetSharedMemoryAsWithOffset(uint32 offset) {
97 void* ptr = reinterpret_cast<int8*>(shared_memory_address_) + offset;
98 return reinterpret_cast<T>(ptr);
101 IdAllocatorInterface* GetIdAllocator(GLuint namespace_id) {
102 return group_->GetIdAllocator(namespace_id);
105 Buffer* GetBuffer(GLuint service_id) {
106 return group_->buffer_manager()->GetBuffer(service_id);
109 Framebuffer* GetFramebuffer(GLuint service_id) {
110 return group_->framebuffer_manager()->GetFramebuffer(service_id);
113 Renderbuffer* GetRenderbuffer(
115 return group_->renderbuffer_manager()->GetRenderbuffer(service_id);
118 TextureRef* GetTexture(GLuint client_id) {
119 return group_->texture_manager()->GetTexture(client_id);
122 Shader* GetShader(GLuint client_id) {
123 return group_->shader_manager()->GetShader(client_id);
126 Program* GetProgram(GLuint client_id) {
127 return group_->program_manager()->GetProgram(client_id);
130 QueryManager::Query* GetQueryInfo(GLuint client_id) {
131 return decoder_->GetQueryManager()->GetQuery(client_id);
134 // This name doesn't match the underlying function, but doing it this way
135 // prevents the need to special-case the unit test generation
136 VertexAttribManager* GetVertexArrayInfo(GLuint client_id) {
137 return decoder_->GetVertexArrayManager()->GetVertexAttribManager(client_id);
140 ProgramManager* program_manager() {
141 return group_->program_manager();
144 void DoCreateProgram(GLuint client_id, GLuint service_id);
145 void DoCreateShader(GLenum shader_type, GLuint client_id, GLuint service_id);
147 void SetBucketAsCString(uint32 bucket_id, const char* str);
149 void set_memory_tracker(MemoryTracker* memory_tracker) {
150 memory_tracker_ = memory_tracker;
154 const char* extensions,
155 const char* gl_version,
161 bool request_stencil,
162 bool bind_generates_resource);
164 void InitDecoderWithCommandLine(
165 const char* extensions,
166 const char* gl_version,
172 bool request_stencil,
173 bool bind_generates_resource,
174 const base::CommandLine* command_line);
178 const ContextGroup& group() const {
179 return *group_.get();
182 ::testing::StrictMock< ::gfx::MockGLInterface>* GetGLMock() const {
186 GLES2Decoder* GetDecoder() const {
187 return decoder_.get();
190 typedef TestHelper::AttribInfo AttribInfo;
191 typedef TestHelper::UniformInfo UniformInfo;
194 AttribInfo* attribs, size_t num_attribs,
195 UniformInfo* uniforms, size_t num_uniforms,
196 GLuint client_id, GLuint service_id,
197 GLuint vertex_shader_client_id, GLuint vertex_shader_service_id,
198 GLuint fragment_shader_client_id, GLuint fragment_shader_service_id);
200 void SetupInitCapabilitiesExpectations();
201 void SetupInitStateExpectations();
202 void ExpectEnableDisable(GLenum cap, bool enable);
204 // Setups up a shader for testing glUniform.
205 void SetupShaderForUniform(GLenum uniform_type);
206 void SetupDefaultProgram();
207 void SetupCubemapProgram();
208 void SetupSamplerExternalProgram();
211 // Note that the error is returned as GLint instead of GLenum.
212 // This is because there is a mismatch in the types of GLenum and
213 // the error values GL_NO_ERROR, GL_INVALID_ENUM, etc. GLenum is
214 // typedef'd as unsigned int while the error values are defined as
215 // integers. This is problematic for template functions such as
216 // EXPECT_EQ that expect both types to be the same.
219 void DoBindBuffer(GLenum target, GLuint client_id, GLuint service_id);
220 void DoBindFramebuffer(GLenum target, GLuint client_id, GLuint service_id);
221 void DoBindRenderbuffer(GLenum target, GLuint client_id, GLuint service_id);
222 void DoBindTexture(GLenum target, GLuint client_id, GLuint service_id);
223 void DoBindVertexArrayOES(GLuint client_id, GLuint service_id);
225 bool DoIsBuffer(GLuint client_id);
226 bool DoIsFramebuffer(GLuint client_id);
227 bool DoIsProgram(GLuint client_id);
228 bool DoIsRenderbuffer(GLuint client_id);
229 bool DoIsShader(GLuint client_id);
230 bool DoIsTexture(GLuint client_id);
232 void DoDeleteBuffer(GLuint client_id, GLuint service_id);
233 void DoDeleteFramebuffer(
234 GLuint client_id, GLuint service_id,
235 bool reset_draw, GLenum draw_target, GLuint draw_id,
236 bool reset_read, GLenum read_target, GLuint read_id);
237 void DoDeleteProgram(GLuint client_id, GLuint service_id);
238 void DoDeleteRenderbuffer(GLuint client_id, GLuint service_id);
239 void DoDeleteShader(GLuint client_id, GLuint service_id);
240 void DoDeleteTexture(GLuint client_id, GLuint service_id);
242 void DoCompressedTexImage2D(
243 GLenum target, GLint level, GLenum format,
244 GLsizei width, GLsizei height, GLint border,
245 GLsizei size, uint32 bucket_id);
247 GLenum target, GLint level, GLenum internal_format,
248 GLsizei width, GLsizei height, GLint border,
249 GLenum format, GLenum type,
250 uint32 shared_memory_id, uint32 shared_memory_offset);
251 void DoTexImage2DConvertInternalFormat(
252 GLenum target, GLint level, GLenum requested_internal_format,
253 GLsizei width, GLsizei height, GLint border,
254 GLenum format, GLenum type,
255 uint32 shared_memory_id, uint32 shared_memory_offset,
256 GLenum expected_internal_format);
257 void DoRenderbufferStorage(
258 GLenum target, GLenum internal_format, GLenum actual_format,
259 GLsizei width, GLsizei height, GLenum error);
260 void DoFramebufferRenderbuffer(
263 GLenum renderbuffer_target,
264 GLuint renderbuffer_client_id,
265 GLuint renderbuffer_service_id,
267 void DoFramebufferTexture2D(
268 GLenum target, GLenum attachment, GLenum tex_target,
269 GLuint texture_client_id, GLuint texture_service_id,
270 GLint level, GLenum error);
271 void DoVertexAttribPointer(
272 GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset);
273 void DoVertexAttribDivisorANGLE(GLuint index, GLuint divisor);
275 void DoEnableVertexAttribArray(GLint index);
277 void DoBufferData(GLenum target, GLsizei size);
279 void DoBufferSubData(
280 GLenum target, GLint offset, GLsizei size, const void* data);
282 void SetupVertexBuffer();
283 void SetupAllNeededVertexBuffers();
285 void SetupIndexBuffer();
287 void DeleteVertexBuffer();
289 void DeleteIndexBuffer();
291 void SetupClearTextureExpectations(
293 GLuint old_service_id,
297 GLenum internal_format,
303 void SetupExpectationsForRestoreClearState(
304 GLclampf restore_red,
305 GLclampf restore_green,
306 GLclampf restore_blue,
307 GLclampf restore_alpha,
308 GLuint restore_stencil,
309 GLclampf restore_depth,
310 bool restore_scissor_test);
312 void SetupExpectationsForFramebufferClearing(
315 GLclampf restore_red,
316 GLclampf restore_green,
317 GLclampf restore_blue,
318 GLclampf restore_alpha,
319 GLuint restore_stencil,
320 GLclampf restore_depth,
321 bool restore_scissor_test);
323 void SetupExpectationsForFramebufferClearingMulti(
324 GLuint read_framebuffer_service_id,
325 GLuint draw_framebuffer_service_id,
328 GLclampf restore_red,
329 GLclampf restore_green,
330 GLclampf restore_blue,
331 GLclampf restore_alpha,
332 GLuint restore_stencil,
333 GLclampf restore_depth,
334 bool restore_scissor_test);
336 void SetupExpectationsForApplyingDirtyState(
337 bool framebuffer_is_rgb,
338 bool framebuffer_has_depth,
339 bool framebuffer_has_stencil,
340 GLuint color_bits, // NOTE! bits are 0x1000, 0x0100, 0x0010, and 0x0001
343 GLuint front_stencil_mask,
344 GLuint back_stencil_mask,
345 bool stencil_enabled,
346 bool cull_face_enabled,
347 bool scissor_test_enabled,
350 void SetupExpectationsForApplyingDefaultDirtyState();
352 void AddExpectationsForSimulatedAttrib0WithError(
353 GLsizei num_vertices, GLuint buffer_id, GLenum error);
355 void AddExpectationsForSimulatedAttrib0(
356 GLsizei num_vertices, GLuint buffer_id);
358 void AddExpectationsForGenVertexArraysOES();
359 void AddExpectationsForDeleteVertexArraysOES();
360 void AddExpectationsForBindVertexArrayOES();
361 void AddExpectationsForRestoreAttribState(GLuint attrib);
363 GLvoid* BufferOffset(unsigned i) {
364 return static_cast<int8 *>(NULL)+(i);
367 template <typename Command, typename Result>
368 bool IsObjectHelper(GLuint client_id) {
369 Result* result = static_cast<Result*>(shared_memory_address_);
371 cmd.Init(client_id, kSharedMemoryId, kSharedMemoryOffset);
372 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
373 bool isObject = static_cast<bool>(*result);
374 EXPECT_EQ(GL_NO_ERROR, GetGLError());
379 static const int kBackBufferWidth = 128;
380 static const int kBackBufferHeight = 64;
382 static const GLint kMaxTextureSize = 2048;
383 static const GLint kMaxCubeMapTextureSize = 256;
384 static const GLint kNumVertexAttribs = 16;
385 static const GLint kNumTextureUnits = 8;
386 static const GLint kMaxTextureImageUnits = 8;
387 static const GLint kMaxVertexTextureImageUnits = 2;
388 static const GLint kMaxFragmentUniformVectors = 16;
389 static const GLint kMaxVaryingVectors = 8;
390 static const GLint kMaxVertexUniformVectors = 128;
391 static const GLint kMaxViewportWidth = 8192;
392 static const GLint kMaxViewportHeight = 8192;
394 static const GLint kViewportX = 0;
395 static const GLint kViewportY = 0;
396 static const GLint kViewportWidth = kBackBufferWidth;
397 static const GLint kViewportHeight = kBackBufferHeight;
399 static const GLuint kServiceAttrib0BufferId = 801;
400 static const GLuint kServiceFixedAttribBufferId = 802;
402 static const GLuint kServiceBufferId = 301;
403 static const GLuint kServiceFramebufferId = 302;
404 static const GLuint kServiceRenderbufferId = 303;
405 static const GLuint kServiceTextureId = 304;
406 static const GLuint kServiceProgramId = 305;
407 static const GLuint kServiceShaderId = 306;
408 static const GLuint kServiceElementBufferId = 308;
409 static const GLuint kServiceQueryId = 309;
410 static const GLuint kServiceVertexArrayId = 310;
412 static const int32 kSharedMemoryId = 401;
413 static const size_t kSharedBufferSize = 2048;
414 static const uint32 kSharedMemoryOffset = 132;
415 static const int32 kInvalidSharedMemoryId = 402;
416 static const uint32 kInvalidSharedMemoryOffset = kSharedBufferSize + 1;
417 static const uint32 kInitialResult = 0xBDBDBDBDu;
418 static const uint8 kInitialMemoryValue = 0xBDu;
420 static const uint32 kNewClientId = 501;
421 static const uint32 kNewServiceId = 502;
422 static const uint32 kInvalidClientId = 601;
424 static const GLuint kServiceVertexShaderId = 321;
425 static const GLuint kServiceFragmentShaderId = 322;
427 static const GLuint kServiceCopyTextureChromiumShaderId = 701;
428 static const GLuint kServiceCopyTextureChromiumProgramId = 721;
430 static const GLuint kServiceCopyTextureChromiumTextureBufferId = 751;
431 static const GLuint kServiceCopyTextureChromiumVertexBufferId = 752;
432 static const GLuint kServiceCopyTextureChromiumFBOId = 753;
433 static const GLuint kServiceCopyTextureChromiumPositionAttrib = 761;
434 static const GLuint kServiceCopyTextureChromiumTexAttrib = 762;
435 static const GLuint kServiceCopyTextureChromiumSamplerLocation = 763;
437 static const GLsizei kNumVertices = 100;
438 static const GLsizei kNumIndices = 10;
439 static const int kValidIndexRangeStart = 1;
440 static const int kValidIndexRangeCount = 7;
441 static const int kInvalidIndexRangeStart = 0;
442 static const int kInvalidIndexRangeCount = 7;
443 static const int kOutOfRangeIndexRangeEnd = 10;
444 static const GLuint kMaxValidIndex = 7;
446 static const GLint kMaxAttribLength = 10;
447 static const char* kAttrib1Name;
448 static const char* kAttrib2Name;
449 static const char* kAttrib3Name;
450 static const GLint kAttrib1Size = 1;
451 static const GLint kAttrib2Size = 1;
452 static const GLint kAttrib3Size = 1;
453 static const GLint kAttrib1Location = 0;
454 static const GLint kAttrib2Location = 1;
455 static const GLint kAttrib3Location = 2;
456 static const GLenum kAttrib1Type = GL_FLOAT_VEC4;
457 static const GLenum kAttrib2Type = GL_FLOAT_VEC2;
458 static const GLenum kAttrib3Type = GL_FLOAT_VEC3;
459 static const GLint kInvalidAttribLocation = 30;
460 static const GLint kBadAttribIndex = kNumVertexAttribs;
462 static const GLint kMaxUniformLength = 12;
463 static const char* kUniform1Name;
464 static const char* kUniform2Name;
465 static const char* kUniform3Name;
466 static const GLint kUniform1Size = 1;
467 static const GLint kUniform2Size = 3;
468 static const GLint kUniform3Size = 2;
469 static const GLint kUniform1RealLocation = 3;
470 static const GLint kUniform2RealLocation = 10;
471 static const GLint kUniform2ElementRealLocation = 12;
472 static const GLint kUniform3RealLocation = 20;
473 static const GLint kUniform1FakeLocation = 0; // These are
474 static const GLint kUniform2FakeLocation = 1; // hardcoded
475 static const GLint kUniform2ElementFakeLocation = 0x10001; // to match
476 static const GLint kUniform3FakeLocation = 2; // ProgramManager.
477 static const GLint kUniform1DesiredLocation = -1;
478 static const GLint kUniform2DesiredLocation = -1;
479 static const GLint kUniform3DesiredLocation = -1;
480 static const GLenum kUniform1Type = GL_SAMPLER_2D;
481 static const GLenum kUniform2Type = GL_INT_VEC2;
482 static const GLenum kUniform3Type = GL_FLOAT_VEC3;
483 static const GLenum kUniformSamplerExternalType = GL_SAMPLER_EXTERNAL_OES;
484 static const GLenum kUniformCubemapType = GL_SAMPLER_CUBE;
485 static const GLint kInvalidUniformLocation = 30;
486 static const GLint kBadUniformIndex = 1000;
488 // Use StrictMock to make 100% sure we know how GL will be called.
489 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
490 scoped_refptr<gfx::GLSurfaceStub> surface_;
491 scoped_refptr<gfx::GLContextStubWithExtensions> context_;
492 scoped_ptr<GLES2Decoder> mock_decoder_;
493 scoped_ptr<GLES2Decoder> decoder_;
494 MemoryTracker* memory_tracker_;
496 GLuint client_buffer_id_;
497 GLuint client_framebuffer_id_;
498 GLuint client_program_id_;
499 GLuint client_renderbuffer_id_;
500 GLuint client_shader_id_;
501 GLuint client_texture_id_;
502 GLuint client_element_buffer_id_;
503 GLuint client_vertex_shader_id_;
504 GLuint client_fragment_shader_id_;
505 GLuint client_query_id_;
506 GLuint client_vertexarray_id_;
508 uint32 shared_memory_id_;
509 uint32 shared_memory_offset_;
510 void* shared_memory_address_;
511 void* shared_memory_base_;
513 int8 immediate_buffer_[256];
516 class MockCommandBufferEngine : public CommandBufferEngine {
518 MockCommandBufferEngine();
520 virtual ~MockCommandBufferEngine();
522 virtual scoped_refptr<gpu::Buffer> GetSharedMemoryBuffer(int32 shm_id)
525 void ClearSharedMemory() {
526 memset(valid_buffer_->memory(), kInitialMemoryValue, kSharedBufferSize);
529 virtual void set_token(int32 token) OVERRIDE;
531 virtual bool SetGetBuffer(int32 /* transfer_buffer_id */) OVERRIDE;
533 // Overridden from CommandBufferEngine.
534 virtual bool SetGetOffset(int32 offset) OVERRIDE;
536 // Overridden from CommandBufferEngine.
537 virtual int32 GetGetOffset() OVERRIDE;
540 scoped_refptr<gpu::Buffer> valid_buffer_;
541 scoped_refptr<gpu::Buffer> invalid_buffer_;
544 void AddExpectationsForVertexAttribManager();
546 scoped_ptr< ::testing::StrictMock<MockCommandBufferEngine> > engine_;
547 scoped_refptr<ContextGroup> group_;
550 class GLES2DecoderWithShaderTestBase : public GLES2DecoderTestBase {
552 GLES2DecoderWithShaderTestBase()
553 : GLES2DecoderTestBase() {
557 virtual void SetUp() OVERRIDE;
558 virtual void TearDown() OVERRIDE;
565 #endif // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_