gles2_cmd_decoder_unittest_base.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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#ifndef GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_ 6#define GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_ 7 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/stream_texture_manager_mock.h" 20#include "gpu/command_buffer/service/test_helper.h" 21#include "gpu/command_buffer/service/texture_manager.h" 22#include "gpu/command_buffer/service/vertex_array_manager.h" 23#include "testing/gtest/include/gtest/gtest.h" 24#include "ui/gl/gl_context_stub.h" 25#include "ui/gl/gl_surface_stub.h" 26#include "ui/gl/gl_mock.h" 27 28namespace gpu { 29namespace gles2 { 30 31class MemoryTracker; 32 33class GLES2DecoderTestBase : public testing::Test { 34 public: 35 GLES2DecoderTestBase(); 36 virtual ~GLES2DecoderTestBase(); 37 38 // Template to call glGenXXX functions. 39 template <typename T> 40 void GenHelper(GLuint client_id) { 41 int8 buffer[sizeof(T) + sizeof(client_id)]; 42 T& cmd = *reinterpret_cast<T*>(&buffer); 43 cmd.Init(1, &client_id); 44 EXPECT_EQ(error::kNoError, 45 ExecuteImmediateCmd(cmd, sizeof(client_id))); 46 } 47 48 // This template exists solely so we can specialize it for 49 // certain commands. 50 template <typename T, int id> 51 void SpecializedSetup(bool valid) { 52 } 53 54 template <typename T> 55 T* GetImmediateAs() { 56 return reinterpret_cast<T*>(immediate_buffer_); 57 } 58 59 template <typename T, typename Command> 60 T GetImmediateDataAs(Command* cmd) { 61 return reinterpret_cast<T>(ImmediateDataAddress(cmd)); 62 } 63 64 void ClearSharedMemory() { 65 engine_->ClearSharedMemory(); 66 } 67 68 virtual void SetUp() OVERRIDE; 69 virtual void TearDown() OVERRIDE; 70 71 template <typename T> 72 error::Error ExecuteCmd(const T& cmd) { 73 COMPILE_ASSERT(T::kArgFlags == cmd::kFixed, Cmd_kArgFlags_not_kFixed); 74 return decoder_->DoCommand(cmd.kCmdId, 75 ComputeNumEntries(sizeof(cmd)) - 1, 76 &cmd); 77 } 78 79 template <typename T> 80 error::Error ExecuteImmediateCmd(const T& cmd, size_t data_size) { 81 COMPILE_ASSERT(T::kArgFlags == cmd::kAtLeastN, Cmd_kArgFlags_not_kAtLeastN); 82 return decoder_->DoCommand(cmd.kCmdId, 83 ComputeNumEntries(sizeof(cmd) + data_size) - 1, 84 &cmd); 85 } 86 87 template <typename T> 88 T GetSharedMemoryAs() { 89 return reinterpret_cast<T>(shared_memory_address_); 90 } 91 92 template <typename T> 93 T GetSharedMemoryAsWithOffset(uint32 offset) { 94 void* ptr = reinterpret_cast<int8*>(shared_memory_address_) + offset; 95 return reinterpret_cast<T>(ptr); 96 } 97 98 IdAllocatorInterface* GetIdAllocator(GLuint namespace_id) { 99 return group_->GetIdAllocator(namespace_id); 100 } 101 102 Buffer* GetBuffer(GLuint service_id) { 103 return group_->buffer_manager()->GetBuffer(service_id); 104 } 105 106 Framebuffer* GetFramebuffer(GLuint service_id) { 107 return group_->framebuffer_manager()->GetFramebuffer(service_id); 108 } 109 110 Renderbuffer* GetRenderbuffer( 111 GLuint service_id) { 112 return group_->renderbuffer_manager()->GetRenderbuffer(service_id); 113 } 114 115 TextureRef* GetTexture(GLuint client_id) { 116 return group_->texture_manager()->GetTexture(client_id); 117 } 118 119 Shader* GetShader(GLuint client_id) { 120 return group_->shader_manager()->GetShader(client_id); 121 } 122 123 Program* GetProgram(GLuint client_id) { 124 return group_->program_manager()->GetProgram(client_id); 125 } 126 127 QueryManager::Query* GetQueryInfo(GLuint client_id) { 128 return decoder_->GetQueryManager()->GetQuery(client_id); 129 } 130 131 // This name doesn't match the underlying function, but doing it this way 132 // prevents the need to special-case the unit test generation 133 VertexAttribManager* GetVertexArrayInfo(GLuint client_id) { 134 return decoder_->GetVertexArrayManager()->GetVertexAttribManager(client_id); 135 } 136 137 ProgramManager* program_manager() { 138 return group_->program_manager(); 139 } 140 141 ::testing::StrictMock<MockStreamTextureManager>* 142 stream_texture_manager() const { 143 return stream_texture_manager_.get(); 144 } 145 146 void DoCreateProgram(GLuint client_id, GLuint service_id); 147 void DoCreateShader(GLenum shader_type, GLuint client_id, GLuint service_id); 148 149 void SetBucketAsCString(uint32 bucket_id, const char* str); 150 151 void set_memory_tracker(MemoryTracker* memory_tracker) { 152 memory_tracker_ = memory_tracker; 153 } 154 155 void InitDecoder( 156 const char* extensions, 157 bool has_alpha, 158 bool has_depth, 159 bool has_stencil, 160 bool request_alpha, 161 bool request_depth, 162 bool request_stencil, 163 bool bind_generates_resource); 164 165 const ContextGroup& group() const { 166 return *group_.get(); 167 } 168 169 ::testing::StrictMock< ::gfx::MockGLInterface>* GetGLMock() const { 170 return gl_.get(); 171 } 172 173 GLES2Decoder* GetDecoder() const { 174 return decoder_.get(); 175 } 176 177 typedef TestHelper::AttribInfo AttribInfo; 178 typedef TestHelper::UniformInfo UniformInfo; 179 180 void SetupShader( 181 AttribInfo* attribs, size_t num_attribs, 182 UniformInfo* uniforms, size_t num_uniforms, 183 GLuint client_id, GLuint service_id, 184 GLuint vertex_shader_client_id, GLuint vertex_shader_service_id, 185 GLuint fragment_shader_client_id, GLuint fragment_shader_service_id); 186 187 void SetupExpectationsForClearingUniforms( 188 UniformInfo* uniforms, size_t num_uniforms) { 189 TestHelper::SetupExpectationsForClearingUniforms( 190 gl_.get(), uniforms, num_uniforms); 191 } 192 193 void SetupInitCapabilitiesExpectations(); 194 void SetupInitStateExpectations(); 195 void ExpectEnableDisable(GLenum cap, bool enable); 196 197 // Setups up a shader for testing glUniform. 198 void SetupShaderForUniform(GLenum uniform_type); 199 void SetupDefaultProgram(); 200 void SetupCubemapProgram(); 201 void SetupTexture(); 202 203 // Note that the error is returned as GLint instead of GLenum. 204 // This is because there is a mismatch in the types of GLenum and 205 // the error values GL_NO_ERROR, GL_INVALID_ENUM, etc. GLenum is 206 // typedef'd as unsigned int while the error values are defined as 207 // integers. This is problematic for template functions such as 208 // EXPECT_EQ that expect both types to be the same. 209 GLint GetGLError(); 210 211 void DoBindBuffer(GLenum target, GLuint client_id, GLuint service_id); 212 void DoBindFramebuffer(GLenum target, GLuint client_id, GLuint service_id); 213 void DoBindRenderbuffer(GLenum target, GLuint client_id, GLuint service_id); 214 void DoBindTexture(GLenum target, GLuint client_id, GLuint service_id); 215 void DoBindVertexArrayOES(GLuint client_id, GLuint service_id); 216 217 bool DoIsBuffer(GLuint client_id); 218 bool DoIsFramebuffer(GLuint client_id); 219 bool DoIsProgram(GLuint client_id); 220 bool DoIsRenderbuffer(GLuint client_id); 221 bool DoIsShader(GLuint client_id); 222 bool DoIsTexture(GLuint client_id); 223 224 void DoDeleteBuffer(GLuint client_id, GLuint service_id); 225 void DoDeleteFramebuffer( 226 GLuint client_id, GLuint service_id, 227 bool reset_draw, GLenum draw_target, GLuint draw_id, 228 bool reset_read, GLenum read_target, GLuint read_id); 229 void DoDeleteProgram(GLuint client_id, GLuint service_id); 230 void DoDeleteRenderbuffer(GLuint client_id, GLuint service_id); 231 void DoDeleteShader(GLuint client_id, GLuint service_id); 232 void DoDeleteTexture(GLuint client_id, GLuint service_id); 233 234 void DoCompressedTexImage2D( 235 GLenum target, GLint level, GLenum format, 236 GLsizei width, GLsizei height, GLint border, 237 GLsizei size, uint32 bucket_id); 238 void DoTexImage2D( 239 GLenum target, GLint level, GLenum internal_format, 240 GLsizei width, GLsizei height, GLint border, 241 GLenum format, GLenum type, 242 uint32 shared_memory_id, uint32 shared_memory_offset); 243 void DoTexImage2DSameSize( 244 GLenum target, GLint level, GLenum internal_format, 245 GLsizei width, GLsizei height, GLint border, 246 GLenum format, GLenum type, 247 uint32 shared_memory_id, uint32 shared_memory_offset); 248 void DoRenderbufferStorage( 249 GLenum target, GLenum internal_format, GLenum actual_format, 250 GLsizei width, GLsizei height, GLenum error); 251 void DoFramebufferRenderbuffer( 252 GLenum target, 253 GLenum attachment, 254 GLenum renderbuffer_target, 255 GLuint renderbuffer_client_id, 256 GLuint renderbuffer_service_id, 257 GLenum error); 258 void DoFramebufferTexture2D( 259 GLenum target, GLenum attachment, GLenum tex_target, 260 GLuint texture_client_id, GLuint texture_service_id, 261 GLint level, GLenum error); 262 void DoVertexAttribPointer( 263 GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset); 264 void DoVertexAttribDivisorANGLE(GLuint index, GLuint divisor); 265 266 void DoEnableVertexAttribArray(GLint index); 267 268 void DoBufferData(GLenum target, GLsizei size); 269 270 void DoBufferSubData( 271 GLenum target, GLint offset, GLsizei size, const void* data); 272 273 void SetupVertexBuffer(); 274 void SetupAllNeededVertexBuffers(); 275 276 void SetupIndexBuffer(); 277 278 void DeleteVertexBuffer(); 279 280 void DeleteIndexBuffer(); 281 282 void SetupClearTextureExpections( 283 GLuint service_id, 284 GLuint old_service_id, 285 GLenum bind_target, 286 GLenum target, 287 GLint level, 288 GLenum format, 289 GLenum type, 290 GLsizei width, 291 GLsizei height); 292 293 void SetupExpectationsForRestoreClearState( 294 GLclampf restore_red, 295 GLclampf restore_green, 296 GLclampf restore_blue, 297 GLclampf restore_alpha, 298 GLuint restore_stencil, 299 GLclampf restore_depth, 300 bool restore_scissor_test); 301 302 void SetupExpectationsForFramebufferClearing( 303 GLenum target, 304 GLuint clear_bits, 305 GLclampf restore_red, 306 GLclampf restore_green, 307 GLclampf restore_blue, 308 GLclampf restore_alpha, 309 GLuint restore_stencil, 310 GLclampf restore_depth, 311 bool restore_scissor_test); 312 313 void SetupExpectationsForFramebufferClearingMulti( 314 GLuint read_framebuffer_service_id, 315 GLuint draw_framebuffer_service_id, 316 GLenum target, 317 GLuint clear_bits, 318 GLclampf restore_red, 319 GLclampf restore_green, 320 GLclampf restore_blue, 321 GLclampf restore_alpha, 322 GLuint restore_stencil, 323 GLclampf restore_depth, 324 bool restore_scissor_test); 325 326 void SetupExpectationsForApplyingDirtyState( 327 bool framebuffer_is_rgb, 328 bool framebuffer_has_depth, 329 bool framebuffer_has_stencil, 330 GLuint color_bits, // NOTE! bits are 0x1000, 0x0100, 0x0010, and 0x0001 331 bool depth_mask, 332 bool depth_enabled, 333 GLuint front_stencil_mask, 334 GLuint back_stencil_mask, 335 bool stencil_enabled, 336 bool cull_face_enabled, 337 bool scissor_test_enabled, 338 bool blend_enabled); 339 340 void SetupExpectationsForApplyingDefaultDirtyState(); 341 342 void AddExpectationsForSimulatedAttrib0WithError( 343 GLsizei num_vertices, GLuint buffer_id, GLenum error); 344 345 void AddExpectationsForSimulatedAttrib0( 346 GLsizei num_vertices, GLuint buffer_id); 347 348 void AddExpectationsForGenVertexArraysOES(); 349 void AddExpectationsForDeleteVertexArraysOES(); 350 void AddExpectationsForBindVertexArrayOES(); 351 void AddExpectationsForRestoreAttribState(GLuint attrib); 352 353 GLvoid* BufferOffset(unsigned i) { 354 return static_cast<int8 *>(NULL)+(i); 355 } 356 357 template <typename Command, typename Result> 358 bool IsObjectHelper(GLuint client_id) { 359 Result* result = static_cast<Result*>(shared_memory_address_); 360 Command cmd; 361 cmd.Init(client_id, kSharedMemoryId, kSharedMemoryOffset); 362 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd)); 363 bool isObject = static_cast<bool>(*result); 364 EXPECT_EQ(GL_NO_ERROR, GetGLError()); 365 return isObject; 366 } 367 368 protected: 369 static const int kBackBufferWidth = 128; 370 static const int kBackBufferHeight = 64; 371 372 static const GLint kMaxTextureSize = 2048; 373 static const GLint kMaxCubeMapTextureSize = 256; 374 static const GLint kNumVertexAttribs = 16; 375 static const GLint kNumTextureUnits = 8; 376 static const GLint kMaxTextureImageUnits = 8; 377 static const GLint kMaxVertexTextureImageUnits = 2; 378 static const GLint kMaxFragmentUniformVectors = 16; 379 static const GLint kMaxVaryingVectors = 8; 380 static const GLint kMaxVertexUniformVectors = 128; 381 static const GLint kMaxViewportWidth = 8192; 382 static const GLint kMaxViewportHeight = 8192; 383 384 static const GLint kViewportX = 0; 385 static const GLint kViewportY = 0; 386 static const GLint kViewportWidth = kBackBufferWidth; 387 static const GLint kViewportHeight = kBackBufferHeight; 388 389 static const GLuint kServiceAttrib0BufferId = 801; 390 static const GLuint kServiceFixedAttribBufferId = 802; 391 392 static const GLuint kServiceBufferId = 301; 393 static const GLuint kServiceFramebufferId = 302; 394 static const GLuint kServiceRenderbufferId = 303; 395 static const GLuint kServiceTextureId = 304; 396 static const GLuint kServiceProgramId = 305; 397 static const GLuint kServiceShaderId = 306; 398 static const GLuint kServiceElementBufferId = 308; 399 static const GLuint kServiceQueryId = 309; 400 static const GLuint kServiceVertexArrayId = 310; 401 402 static const int32 kSharedMemoryId = 401; 403 static const size_t kSharedBufferSize = 2048; 404 static const uint32 kSharedMemoryOffset = 132; 405 static const int32 kInvalidSharedMemoryId = 402; 406 static const uint32 kInvalidSharedMemoryOffset = kSharedBufferSize + 1; 407 static const uint32 kInitialResult = 0xBDBDBDBDu; 408 static const uint8 kInitialMemoryValue = 0xBDu; 409 410 static const uint32 kNewClientId = 501; 411 static const uint32 kNewServiceId = 502; 412 static const uint32 kInvalidClientId = 601; 413 414 static const GLuint kServiceVertexShaderId = 321; 415 static const GLuint kServiceFragmentShaderId = 322; 416 417 static const GLuint kServiceCopyTextureChromiumShaderId = 701; 418 static const GLuint kServiceCopyTextureChromiumProgramId = 721; 419 420 static const GLuint kServiceCopyTextureChromiumTextureBufferId = 751; 421 static const GLuint kServiceCopyTextureChromiumVertexBufferId = 752; 422 static const GLuint kServiceCopyTextureChromiumFBOId = 753; 423 static const GLuint kServiceCopyTextureChromiumPositionAttrib = 761; 424 static const GLuint kServiceCopyTextureChromiumTexAttrib = 762; 425 static const GLuint kServiceCopyTextureChromiumSamplerLocation = 763; 426 427 static const GLsizei kNumVertices = 100; 428 static const GLsizei kNumIndices = 10; 429 static const int kValidIndexRangeStart = 1; 430 static const int kValidIndexRangeCount = 7; 431 static const int kInvalidIndexRangeStart = 0; 432 static const int kInvalidIndexRangeCount = 7; 433 static const int kOutOfRangeIndexRangeEnd = 10; 434 static const GLuint kMaxValidIndex = 7; 435 436 static const GLint kMaxAttribLength = 10; 437 static const char* kAttrib1Name; 438 static const char* kAttrib2Name; 439 static const char* kAttrib3Name; 440 static const GLint kAttrib1Size = 1; 441 static const GLint kAttrib2Size = 1; 442 static const GLint kAttrib3Size = 1; 443 static const GLint kAttrib1Location = 0; 444 static const GLint kAttrib2Location = 1; 445 static const GLint kAttrib3Location = 2; 446 static const GLenum kAttrib1Type = GL_FLOAT_VEC4; 447 static const GLenum kAttrib2Type = GL_FLOAT_VEC2; 448 static const GLenum kAttrib3Type = GL_FLOAT_VEC3; 449 static const GLint kInvalidAttribLocation = 30; 450 static const GLint kBadAttribIndex = kNumVertexAttribs; 451 452 static const GLint kMaxUniformLength = 12; 453 static const char* kUniform1Name; 454 static const char* kUniform2Name; 455 static const char* kUniform3Name; 456 static const GLint kUniform1Size = 1; 457 static const GLint kUniform2Size = 3; 458 static const GLint kUniform3Size = 2; 459 static const GLint kUniform1RealLocation = 3; 460 static const GLint kUniform2RealLocation = 10; 461 static const GLint kUniform2ElementRealLocation = 12; 462 static const GLint kUniform3RealLocation = 20; 463 static const GLint kUniform1FakeLocation = 0; // These are 464 static const GLint kUniform2FakeLocation = 1; // hardcoded 465 static const GLint kUniform2ElementFakeLocation = 0x10001; // to match 466 static const GLint kUniform3FakeLocation = 2; // ProgramManager. 467 static const GLint kUniform1DesiredLocation = -1; 468 static const GLint kUniform2DesiredLocation = -1; 469 static const GLint kUniform3DesiredLocation = -1; 470 static const GLenum kUniform1Type = GL_SAMPLER_2D; 471 static const GLenum kUniform2Type = GL_INT_VEC2; 472 static const GLenum kUniform3Type = GL_FLOAT_VEC3; 473 static const GLenum kUniformCubemapType = GL_SAMPLER_CUBE; 474 static const GLint kInvalidUniformLocation = 30; 475 static const GLint kBadUniformIndex = 1000; 476 477 // Use StrictMock to make 100% sure we know how GL will be called. 478 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_; 479 scoped_refptr<gfx::GLSurfaceStub> surface_; 480 scoped_refptr<gfx::GLContextStub> context_; 481 scoped_ptr<GLES2Decoder> mock_decoder_; 482 scoped_ptr<GLES2Decoder> decoder_; 483 MemoryTracker* memory_tracker_; 484 485 GLuint client_buffer_id_; 486 GLuint client_framebuffer_id_; 487 GLuint client_program_id_; 488 GLuint client_renderbuffer_id_; 489 GLuint client_shader_id_; 490 GLuint client_texture_id_; 491 GLuint client_element_buffer_id_; 492 GLuint client_vertex_shader_id_; 493 GLuint client_fragment_shader_id_; 494 GLuint client_query_id_; 495 GLuint client_vertexarray_id_; 496 497 uint32 shared_memory_id_; 498 uint32 shared_memory_offset_; 499 void* shared_memory_address_; 500 void* shared_memory_base_; 501 502 int8 immediate_buffer_[256]; 503 504 private: 505 class MockCommandBufferEngine : public CommandBufferEngine { 506 public: 507 MockCommandBufferEngine(); 508 509 virtual ~MockCommandBufferEngine(); 510 511 virtual gpu::Buffer GetSharedMemoryBuffer(int32 shm_id) OVERRIDE; 512 513 void ClearSharedMemory() { 514 memset(data_.get(), kInitialMemoryValue, kSharedBufferSize); 515 } 516 517 virtual void set_token(int32 token) OVERRIDE; 518 519 virtual bool SetGetBuffer(int32 /* transfer_buffer_id */) OVERRIDE; 520 521 // Overridden from CommandBufferEngine. 522 virtual bool SetGetOffset(int32 offset) OVERRIDE; 523 524 // Overridden from CommandBufferEngine. 525 virtual int32 GetGetOffset() OVERRIDE; 526 527 private: 528 scoped_ptr<int8[]> data_; 529 gpu::Buffer valid_buffer_; 530 gpu::Buffer invalid_buffer_; 531 }; 532 533 void AddExpectationsForVertexAttribManager(); 534 535 scoped_ptr< ::testing::StrictMock<MockCommandBufferEngine> > engine_; 536 scoped_ptr< ::testing::StrictMock<MockStreamTextureManager> > 537 stream_texture_manager_; 538 scoped_refptr<ContextGroup> group_; 539}; 540 541class GLES2DecoderWithShaderTestBase : public GLES2DecoderTestBase { 542 public: 543 GLES2DecoderWithShaderTestBase() 544 : GLES2DecoderTestBase() { 545 } 546 547 protected: 548 virtual void SetUp() OVERRIDE; 549 virtual void TearDown() OVERRIDE; 550 551}; 552 553} // namespace gles2 554} // namespace gpu 555 556#endif // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_ 557