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