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 SetupInitCapabilitiesExpectations(); 201 void SetupInitStateExpectations(); 202 void ExpectEnableDisable(GLenum cap, bool enable); 203 204 // Setups up a shader for testing glUniform. 205 void SetupShaderForUniform(GLenum uniform_type); 206 void SetupDefaultProgram(); 207 void SetupCubemapProgram(); 208 void SetupSamplerExternalProgram(); 209 void SetupTexture(); 210 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. 217 GLint GetGLError(); 218 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); 224 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); 231 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); 241 242 void DoCompressedTexImage2D( 243 GLenum target, GLint level, GLenum format, 244 GLsizei width, GLsizei height, GLint border, 245 GLsizei size, uint32 bucket_id); 246 void DoTexImage2D( 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 DoRenderbufferStorage( 252 GLenum target, GLenum internal_format, GLenum actual_format, 253 GLsizei width, GLsizei height, GLenum error); 254 void DoFramebufferRenderbuffer( 255 GLenum target, 256 GLenum attachment, 257 GLenum renderbuffer_target, 258 GLuint renderbuffer_client_id, 259 GLuint renderbuffer_service_id, 260 GLenum error); 261 void DoFramebufferTexture2D( 262 GLenum target, GLenum attachment, GLenum tex_target, 263 GLuint texture_client_id, GLuint texture_service_id, 264 GLint level, GLenum error); 265 void DoVertexAttribPointer( 266 GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset); 267 void DoVertexAttribDivisorANGLE(GLuint index, GLuint divisor); 268 269 void DoEnableVertexAttribArray(GLint index); 270 271 void DoBufferData(GLenum target, GLsizei size); 272 273 void DoBufferSubData( 274 GLenum target, GLint offset, GLsizei size, const void* data); 275 276 void SetupVertexBuffer(); 277 void SetupAllNeededVertexBuffers(); 278 279 void SetupIndexBuffer(); 280 281 void DeleteVertexBuffer(); 282 283 void DeleteIndexBuffer(); 284 285 void SetupClearTextureExpections( 286 GLuint service_id, 287 GLuint old_service_id, 288 GLenum bind_target, 289 GLenum target, 290 GLint level, 291 GLenum format, 292 GLenum type, 293 GLsizei width, 294 GLsizei height); 295 296 void SetupExpectationsForRestoreClearState( 297 GLclampf restore_red, 298 GLclampf restore_green, 299 GLclampf restore_blue, 300 GLclampf restore_alpha, 301 GLuint restore_stencil, 302 GLclampf restore_depth, 303 bool restore_scissor_test); 304 305 void SetupExpectationsForFramebufferClearing( 306 GLenum target, 307 GLuint clear_bits, 308 GLclampf restore_red, 309 GLclampf restore_green, 310 GLclampf restore_blue, 311 GLclampf restore_alpha, 312 GLuint restore_stencil, 313 GLclampf restore_depth, 314 bool restore_scissor_test); 315 316 void SetupExpectationsForFramebufferClearingMulti( 317 GLuint read_framebuffer_service_id, 318 GLuint draw_framebuffer_service_id, 319 GLenum target, 320 GLuint clear_bits, 321 GLclampf restore_red, 322 GLclampf restore_green, 323 GLclampf restore_blue, 324 GLclampf restore_alpha, 325 GLuint restore_stencil, 326 GLclampf restore_depth, 327 bool restore_scissor_test); 328 329 void SetupExpectationsForApplyingDirtyState( 330 bool framebuffer_is_rgb, 331 bool framebuffer_has_depth, 332 bool framebuffer_has_stencil, 333 GLuint color_bits, // NOTE! bits are 0x1000, 0x0100, 0x0010, and 0x0001 334 bool depth_mask, 335 bool depth_enabled, 336 GLuint front_stencil_mask, 337 GLuint back_stencil_mask, 338 bool stencil_enabled, 339 bool cull_face_enabled, 340 bool scissor_test_enabled, 341 bool blend_enabled); 342 343 void SetupExpectationsForApplyingDefaultDirtyState(); 344 345 void AddExpectationsForSimulatedAttrib0WithError( 346 GLsizei num_vertices, GLuint buffer_id, GLenum error); 347 348 void AddExpectationsForSimulatedAttrib0( 349 GLsizei num_vertices, GLuint buffer_id); 350 351 void AddExpectationsForGenVertexArraysOES(); 352 void AddExpectationsForDeleteVertexArraysOES(); 353 void AddExpectationsForBindVertexArrayOES(); 354 void AddExpectationsForRestoreAttribState(GLuint attrib); 355 356 GLvoid* BufferOffset(unsigned i) { 357 return static_cast<int8 *>(NULL)+(i); 358 } 359 360 template <typename Command, typename Result> 361 bool IsObjectHelper(GLuint client_id) { 362 Result* result = static_cast<Result*>(shared_memory_address_); 363 Command cmd; 364 cmd.Init(client_id, kSharedMemoryId, kSharedMemoryOffset); 365 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd)); 366 bool isObject = static_cast<bool>(*result); 367 EXPECT_EQ(GL_NO_ERROR, GetGLError()); 368 return isObject; 369 } 370 371 protected: 372 static const int kBackBufferWidth = 128; 373 static const int kBackBufferHeight = 64; 374 375 static const GLint kMaxTextureSize = 2048; 376 static const GLint kMaxCubeMapTextureSize = 256; 377 static const GLint kNumVertexAttribs = 16; 378 static const GLint kNumTextureUnits = 8; 379 static const GLint kMaxTextureImageUnits = 8; 380 static const GLint kMaxVertexTextureImageUnits = 2; 381 static const GLint kMaxFragmentUniformVectors = 16; 382 static const GLint kMaxVaryingVectors = 8; 383 static const GLint kMaxVertexUniformVectors = 128; 384 static const GLint kMaxViewportWidth = 8192; 385 static const GLint kMaxViewportHeight = 8192; 386 387 static const GLint kViewportX = 0; 388 static const GLint kViewportY = 0; 389 static const GLint kViewportWidth = kBackBufferWidth; 390 static const GLint kViewportHeight = kBackBufferHeight; 391 392 static const GLuint kServiceAttrib0BufferId = 801; 393 static const GLuint kServiceFixedAttribBufferId = 802; 394 395 static const GLuint kServiceBufferId = 301; 396 static const GLuint kServiceFramebufferId = 302; 397 static const GLuint kServiceRenderbufferId = 303; 398 static const GLuint kServiceTextureId = 304; 399 static const GLuint kServiceProgramId = 305; 400 static const GLuint kServiceShaderId = 306; 401 static const GLuint kServiceElementBufferId = 308; 402 static const GLuint kServiceQueryId = 309; 403 static const GLuint kServiceVertexArrayId = 310; 404 405 static const int32 kSharedMemoryId = 401; 406 static const size_t kSharedBufferSize = 2048; 407 static const uint32 kSharedMemoryOffset = 132; 408 static const int32 kInvalidSharedMemoryId = 402; 409 static const uint32 kInvalidSharedMemoryOffset = kSharedBufferSize + 1; 410 static const uint32 kInitialResult = 0xBDBDBDBDu; 411 static const uint8 kInitialMemoryValue = 0xBDu; 412 413 static const uint32 kNewClientId = 501; 414 static const uint32 kNewServiceId = 502; 415 static const uint32 kInvalidClientId = 601; 416 417 static const GLuint kServiceVertexShaderId = 321; 418 static const GLuint kServiceFragmentShaderId = 322; 419 420 static const GLuint kServiceCopyTextureChromiumShaderId = 701; 421 static const GLuint kServiceCopyTextureChromiumProgramId = 721; 422 423 static const GLuint kServiceCopyTextureChromiumTextureBufferId = 751; 424 static const GLuint kServiceCopyTextureChromiumVertexBufferId = 752; 425 static const GLuint kServiceCopyTextureChromiumFBOId = 753; 426 static const GLuint kServiceCopyTextureChromiumPositionAttrib = 761; 427 static const GLuint kServiceCopyTextureChromiumTexAttrib = 762; 428 static const GLuint kServiceCopyTextureChromiumSamplerLocation = 763; 429 430 static const GLsizei kNumVertices = 100; 431 static const GLsizei kNumIndices = 10; 432 static const int kValidIndexRangeStart = 1; 433 static const int kValidIndexRangeCount = 7; 434 static const int kInvalidIndexRangeStart = 0; 435 static const int kInvalidIndexRangeCount = 7; 436 static const int kOutOfRangeIndexRangeEnd = 10; 437 static const GLuint kMaxValidIndex = 7; 438 439 static const GLint kMaxAttribLength = 10; 440 static const char* kAttrib1Name; 441 static const char* kAttrib2Name; 442 static const char* kAttrib3Name; 443 static const GLint kAttrib1Size = 1; 444 static const GLint kAttrib2Size = 1; 445 static const GLint kAttrib3Size = 1; 446 static const GLint kAttrib1Location = 0; 447 static const GLint kAttrib2Location = 1; 448 static const GLint kAttrib3Location = 2; 449 static const GLenum kAttrib1Type = GL_FLOAT_VEC4; 450 static const GLenum kAttrib2Type = GL_FLOAT_VEC2; 451 static const GLenum kAttrib3Type = GL_FLOAT_VEC3; 452 static const GLint kInvalidAttribLocation = 30; 453 static const GLint kBadAttribIndex = kNumVertexAttribs; 454 455 static const GLint kMaxUniformLength = 12; 456 static const char* kUniform1Name; 457 static const char* kUniform2Name; 458 static const char* kUniform3Name; 459 static const GLint kUniform1Size = 1; 460 static const GLint kUniform2Size = 3; 461 static const GLint kUniform3Size = 2; 462 static const GLint kUniform1RealLocation = 3; 463 static const GLint kUniform2RealLocation = 10; 464 static const GLint kUniform2ElementRealLocation = 12; 465 static const GLint kUniform3RealLocation = 20; 466 static const GLint kUniform1FakeLocation = 0; // These are 467 static const GLint kUniform2FakeLocation = 1; // hardcoded 468 static const GLint kUniform2ElementFakeLocation = 0x10001; // to match 469 static const GLint kUniform3FakeLocation = 2; // ProgramManager. 470 static const GLint kUniform1DesiredLocation = -1; 471 static const GLint kUniform2DesiredLocation = -1; 472 static const GLint kUniform3DesiredLocation = -1; 473 static const GLenum kUniform1Type = GL_SAMPLER_2D; 474 static const GLenum kUniform2Type = GL_INT_VEC2; 475 static const GLenum kUniform3Type = GL_FLOAT_VEC3; 476 static const GLenum kUniformSamplerExternalType = GL_SAMPLER_EXTERNAL_OES; 477 static const GLenum kUniformCubemapType = GL_SAMPLER_CUBE; 478 static const GLint kInvalidUniformLocation = 30; 479 static const GLint kBadUniformIndex = 1000; 480 481 // Use StrictMock to make 100% sure we know how GL will be called. 482 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_; 483 scoped_refptr<gfx::GLSurfaceStub> surface_; 484 scoped_refptr<gfx::GLContextStub> context_; 485 scoped_ptr<GLES2Decoder> mock_decoder_; 486 scoped_ptr<GLES2Decoder> decoder_; 487 MemoryTracker* memory_tracker_; 488 489 GLuint client_buffer_id_; 490 GLuint client_framebuffer_id_; 491 GLuint client_program_id_; 492 GLuint client_renderbuffer_id_; 493 GLuint client_shader_id_; 494 GLuint client_texture_id_; 495 GLuint client_element_buffer_id_; 496 GLuint client_vertex_shader_id_; 497 GLuint client_fragment_shader_id_; 498 GLuint client_query_id_; 499 GLuint client_vertexarray_id_; 500 501 uint32 shared_memory_id_; 502 uint32 shared_memory_offset_; 503 void* shared_memory_address_; 504 void* shared_memory_base_; 505 506 int8 immediate_buffer_[256]; 507 508 private: 509 class MockCommandBufferEngine : public CommandBufferEngine { 510 public: 511 MockCommandBufferEngine(); 512 513 virtual ~MockCommandBufferEngine(); 514 515 virtual gpu::Buffer GetSharedMemoryBuffer(int32 shm_id) OVERRIDE; 516 517 void ClearSharedMemory() { 518 memset(data_.get(), kInitialMemoryValue, kSharedBufferSize); 519 } 520 521 virtual void set_token(int32 token) OVERRIDE; 522 523 virtual bool SetGetBuffer(int32 /* transfer_buffer_id */) OVERRIDE; 524 525 // Overridden from CommandBufferEngine. 526 virtual bool SetGetOffset(int32 offset) OVERRIDE; 527 528 // Overridden from CommandBufferEngine. 529 virtual int32 GetGetOffset() OVERRIDE; 530 531 private: 532 scoped_ptr<int8[]> data_; 533 gpu::Buffer valid_buffer_; 534 gpu::Buffer invalid_buffer_; 535 }; 536 537 void AddExpectationsForVertexAttribManager(); 538 539 scoped_ptr< ::testing::StrictMock<MockCommandBufferEngine> > engine_; 540 scoped_ptr< ::testing::StrictMock<MockStreamTextureManager> > 541 stream_texture_manager_; 542 scoped_refptr<ContextGroup> group_; 543}; 544 545class GLES2DecoderWithShaderTestBase : public GLES2DecoderTestBase { 546 public: 547 GLES2DecoderWithShaderTestBase() 548 : GLES2DecoderTestBase() { 549 } 550 551 protected: 552 virtual void SetUp() OVERRIDE; 553 virtual void TearDown() OVERRIDE; 554 555}; 556 557} // namespace gles2 558} // namespace gpu 559 560#endif // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_ 561