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 <stddef.h> 9 #include <stdint.h> 10 11 #include <array> 12 #include <memory> 13 14 #include "base/test/task_environment.h" 15 #include "gpu/command_buffer/client/client_test_helper.h" 16 #include "gpu/command_buffer/common/gles2_cmd_format.h" 17 #include "gpu/command_buffer/common/gles2_cmd_utils.h" 18 #include "gpu/command_buffer/service/buffer_manager.h" 19 #include "gpu/command_buffer/service/context_group.h" 20 #include "gpu/command_buffer/service/decoder_client.h" 21 #include "gpu/command_buffer/service/framebuffer_manager.h" 22 #include "gpu/command_buffer/service/gl_context_mock.h" 23 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" 24 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h" 25 #include "gpu/command_buffer/service/gles2_cmd_decoder_passthrough.h" 26 #include "gpu/command_buffer/service/gles2_query_manager.h" 27 #include "gpu/command_buffer/service/gpu_tracer.h" 28 #include "gpu/command_buffer/service/image_manager.h" 29 #include "gpu/command_buffer/service/mailbox_manager_impl.h" 30 #include "gpu/command_buffer/service/passthrough_discardable_manager.h" 31 #include "gpu/command_buffer/service/program_manager.h" 32 #include "gpu/command_buffer/service/renderbuffer_manager.h" 33 #include "gpu/command_buffer/service/sampler_manager.h" 34 #include "gpu/command_buffer/service/service_discardable_manager.h" 35 #include "gpu/command_buffer/service/shader_manager.h" 36 #include "gpu/command_buffer/service/shared_image_manager.h" 37 #include "gpu/command_buffer/service/test_helper.h" 38 #include "gpu/command_buffer/service/texture_manager.h" 39 #include "gpu/command_buffer/service/transform_feedback_manager.h" 40 #include "gpu/command_buffer/service/vertex_array_manager.h" 41 #include "gpu/config/gpu_driver_bug_workarounds.h" 42 #include "gpu/config/gpu_preferences.h" 43 #include "testing/gtest/include/gtest/gtest.h" 44 #include "ui/gl/gl_mock.h" 45 #include "ui/gl/gl_surface_stub.h" 46 #include "ui/gl/gl_version_info.h" 47 48 namespace gpu { 49 class MemoryTracker; 50 51 namespace gles2 { 52 class MockCopyTextureResourceManager; 53 class MockCopyTexImageResourceManager; 54 55 class GLES2DecoderTestBase : public ::testing::TestWithParam<bool>, 56 public DecoderClient { 57 public: 58 GLES2DecoderTestBase(); 59 ~GLES2DecoderTestBase() override; 60 61 void OnConsoleMessage(int32_t id, const std::string& message) override; 62 void CacheShader(const std::string& key, const std::string& shader) override; 63 void OnFenceSyncRelease(uint64_t release) override; 64 void OnDescheduleUntilFinished() override; 65 void OnRescheduleAfterFinished() override; 66 void OnSwapBuffers(uint64_t swap_id, uint32_t flags) override; ScheduleGrContextCleanup()67 void ScheduleGrContextCleanup() override {} HandleReturnData(base::span<const uint8_t> data)68 void HandleReturnData(base::span<const uint8_t> data) override {} 69 70 // Template to call glGenXXX functions. 71 template <typename T> GenHelper(GLuint client_id)72 void GenHelper(GLuint client_id) { 73 int8_t buffer[sizeof(T) + sizeof(client_id)]; 74 T& cmd = *reinterpret_cast<T*>(&buffer); 75 cmd.Init(1, &client_id); 76 EXPECT_EQ(error::kNoError, 77 ExecuteImmediateCmd(cmd, sizeof(client_id))); 78 } 79 80 // This template exists solely so we can specialize it for 81 // certain commands. 82 template <typename T, int id> SpecializedSetup(bool valid)83 void SpecializedSetup(bool valid) { 84 } 85 86 template <typename T> GetImmediateAs()87 T* GetImmediateAs() { 88 return reinterpret_cast<T*>(immediate_buffer_); 89 } 90 ClearSharedMemory()91 void ClearSharedMemory() { 92 memset(shared_memory_base_, kInitialMemoryValue, kSharedBufferSize); 93 } 94 95 void SetUp() override; 96 void TearDown() override; 97 98 template <typename T> ExecuteCmd(const T & cmd)99 error::Error ExecuteCmd(const T& cmd) { 100 static_assert(T::kArgFlags == cmd::kFixed, 101 "T::kArgFlags should equal cmd::kFixed"); 102 int entries_processed = 0; 103 return decoder_->DoCommands(1, (const void*)&cmd, 104 ComputeNumEntries(sizeof(cmd)), 105 &entries_processed); 106 } 107 108 template <typename T> ExecuteImmediateCmd(const T & cmd,size_t data_size)109 error::Error ExecuteImmediateCmd(const T& cmd, size_t data_size) { 110 static_assert(T::kArgFlags == cmd::kAtLeastN, 111 "T::kArgFlags should equal cmd::kAtLeastN"); 112 int entries_processed = 0; 113 return decoder_->DoCommands(1, (const void*)&cmd, 114 ComputeNumEntries(sizeof(cmd) + data_size), 115 &entries_processed); 116 } 117 118 template <typename T> GetSharedMemoryAs()119 T GetSharedMemoryAs() { 120 return reinterpret_cast<T>(shared_memory_address_); 121 } 122 123 template <typename T> GetSharedMemoryAsWithOffset(uint32_t offset)124 T GetSharedMemoryAsWithOffset(uint32_t offset) { 125 void* ptr = reinterpret_cast<int8_t*>(shared_memory_address_) + offset; 126 return reinterpret_cast<T>(ptr); 127 } 128 GetBuffer(GLuint client_id)129 Buffer* GetBuffer(GLuint client_id) { 130 return group_->buffer_manager()->GetBuffer(client_id); 131 } 132 GetFramebuffer(GLuint client_id)133 Framebuffer* GetFramebuffer(GLuint client_id) { 134 return decoder_->GetFramebufferManager()->GetFramebuffer(client_id); 135 } 136 GetRenderbuffer(GLuint client_id)137 Renderbuffer* GetRenderbuffer(GLuint client_id) { 138 return group_->renderbuffer_manager()->GetRenderbuffer(client_id); 139 } 140 GetTexture(GLuint client_id)141 TextureRef* GetTexture(GLuint client_id) { 142 return group_->texture_manager()->GetTexture(client_id); 143 } 144 GetShader(GLuint client_id)145 Shader* GetShader(GLuint client_id) { 146 return group_->shader_manager()->GetShader(client_id); 147 } 148 GetProgram(GLuint client_id)149 Program* GetProgram(GLuint client_id) { 150 return group_->program_manager()->GetProgram(client_id); 151 } 152 GetQueryInfo(GLuint client_id)153 QueryManager::Query* GetQueryInfo(GLuint client_id) { 154 return decoder_->GetQueryManager()->GetQuery(client_id); 155 } 156 GetSampler(GLuint client_id)157 Sampler* GetSampler(GLuint client_id) { 158 return group_->sampler_manager()->GetSampler(client_id); 159 } 160 GetTransformFeedback(GLuint client_id)161 TransformFeedback* GetTransformFeedback(GLuint client_id) { 162 return decoder_->GetTransformFeedbackManager()->GetTransformFeedback( 163 client_id); 164 } 165 GetSyncServiceId(GLuint client_id,GLsync * service_id)166 bool GetSyncServiceId(GLuint client_id, GLsync* service_id) const { 167 return group_->GetSyncServiceId(client_id, service_id); 168 } 169 170 // This name doesn't match the underlying function, but doing it this way 171 // prevents the need to special-case the unit test generation GetVertexArrayInfo(GLuint client_id)172 VertexAttribManager* GetVertexArrayInfo(GLuint client_id) { 173 return decoder_->GetVertexArrayManager()->GetVertexAttribManager(client_id); 174 } 175 program_manager()176 ProgramManager* program_manager() { 177 return group_->program_manager(); 178 } 179 feature_info()180 FeatureInfo* feature_info() { 181 return group_->feature_info(); 182 } 183 framebuffer_completeness_cache()184 FramebufferCompletenessCache* framebuffer_completeness_cache() const { 185 return group_->framebuffer_completeness_cache(); 186 } 187 GetFramebufferManager()188 FramebufferManager* GetFramebufferManager() { 189 return decoder_->GetFramebufferManager(); 190 } 191 GetImageManagerForTest()192 ImageManager* GetImageManagerForTest() { 193 return decoder_->GetImageManagerForTest(); 194 } 195 196 void DoCreateProgram(GLuint client_id, GLuint service_id); 197 void DoCreateShader(GLenum shader_type, GLuint client_id, GLuint service_id); 198 void DoFenceSync(GLuint client_id, GLuint service_id); 199 void DoCreateSampler(GLuint client_id, GLuint service_id); 200 void DoCreateTransformFeedback(GLuint client_id, GLuint service_id); 201 202 void SetBucketData(uint32_t bucket_id, const void* data, uint32_t data_size); 203 void SetBucketAsCString(uint32_t bucket_id, const char* str); 204 // If we want a valid bucket, just set |count_in_header| as |count|, 205 // and set |str_end| as 0. 206 void SetBucketAsCStrings(uint32_t bucket_id, 207 GLsizei count, 208 const char** str, 209 GLsizei count_in_header, 210 char str_end); 211 set_memory_tracker(std::unique_ptr<MemoryTracker> memory_tracker)212 void set_memory_tracker(std::unique_ptr<MemoryTracker> memory_tracker) { 213 memory_tracker_ = std::move(memory_tracker); 214 } 215 216 struct InitState { 217 InitState(); 218 InitState(const InitState& other); 219 InitState& operator=(const InitState& other); 220 221 std::string extensions = "GL_EXT_framebuffer_object"; 222 std::string gl_version = "2.1"; 223 bool has_alpha = false; 224 bool has_depth = false; 225 bool has_stencil = false; 226 bool request_alpha = false; 227 bool request_depth = false; 228 bool request_stencil = false; 229 bool bind_generates_resource = false; 230 bool lose_context_when_out_of_memory = false; 231 bool lose_context_on_init = false; 232 bool use_native_vao = true; 233 ContextType context_type = CONTEXT_TYPE_OPENGLES2; 234 }; 235 236 void InitDecoder(const InitState& init); 237 void InitDecoderWithWorkarounds(const InitState& init, 238 const GpuDriverBugWorkarounds& workarounds); 239 ContextResult MaybeInitDecoderWithWorkarounds( 240 const InitState& init, 241 const GpuDriverBugWorkarounds& workarounds); 242 243 void ResetDecoder(); 244 group()245 const ContextGroup& group() const { 246 return *group_.get(); 247 } 248 LoseContexts(error::ContextLostReason reason)249 void LoseContexts(error::ContextLostReason reason) const { 250 group_->LoseContexts(reason); 251 } 252 GetContextLostReason()253 error::ContextLostReason GetContextLostReason() const { 254 return command_buffer_service_->GetState().context_lost_reason; 255 } 256 GetGLMock()257 ::testing::StrictMock<::gl::MockGLInterface>* GetGLMock() const { 258 return gl_.get(); 259 } 260 GetDecoder()261 GLES2Decoder* GetDecoder() const { 262 return decoder_.get(); 263 } 264 GetAndClearBackbufferClearBitsForTest()265 uint32_t GetAndClearBackbufferClearBitsForTest() const { 266 return decoder_->GetAndClearBackbufferClearBitsForTest(); 267 } 268 GetSharedImageManager()269 SharedImageManager* GetSharedImageManager() { return &shared_image_manager_; } 270 271 typedef TestHelper::AttribInfo AttribInfo; 272 typedef TestHelper::UniformInfo UniformInfo; 273 274 void SetupShader( 275 AttribInfo* attribs, size_t num_attribs, 276 UniformInfo* uniforms, size_t num_uniforms, 277 GLuint client_id, GLuint service_id, 278 GLuint vertex_shader_client_id, GLuint vertex_shader_service_id, 279 GLuint fragment_shader_client_id, GLuint fragment_shader_service_id); 280 281 // Setups up a shader for testing glUniform. 282 void SetupShaderForUniform(GLenum uniform_type); 283 void SetupDefaultProgram(); 284 void SetupCubemapProgram(); 285 void SetupSamplerExternalProgram(); 286 void SetupTexture(); 287 288 // Sets up a sampler on texture unit 0 for certain ES3-specific tests. 289 void SetupSampler(); 290 291 // Note that the error is returned as GLint instead of GLenum. 292 // This is because there is a mismatch in the types of GLenum and 293 // the error values GL_NO_ERROR, GL_INVALID_ENUM, etc. GLenum is 294 // typedef'd as unsigned int while the error values are defined as 295 // integers. This is problematic for template functions such as 296 // EXPECT_EQ that expect both types to be the same. 297 GLint GetGLError(); 298 299 void DoBindBuffer(GLenum target, GLuint client_id, GLuint service_id); 300 void DoBindFramebuffer(GLenum target, GLuint client_id, GLuint service_id); 301 void DoBindRenderbuffer(GLenum target, GLuint client_id, GLuint service_id); 302 void DoRenderbufferStorageMultisampleCHROMIUM(GLenum target, 303 GLsizei samples, 304 GLenum internal_format, 305 GLenum gl_format, 306 GLsizei width, 307 GLsizei height, 308 bool expect_bind); 309 void RestoreRenderbufferBindings(); 310 void EnsureRenderbufferBound(bool expect_bind); 311 void DoBindTexture(GLenum target, GLuint client_id, GLuint service_id); 312 void DoBindVertexArrayOES(GLuint client_id, GLuint service_id); 313 void DoBindSampler(GLuint unit, GLuint client_id, GLuint service_id); 314 void DoBindTransformFeedback( 315 GLenum target, GLuint client_id, GLuint service_id); 316 317 bool DoIsBuffer(GLuint client_id); 318 bool DoIsFramebuffer(GLuint client_id); 319 bool DoIsProgram(GLuint client_id); 320 bool DoIsRenderbuffer(GLuint client_id); 321 bool DoIsShader(GLuint client_id); 322 bool DoIsTexture(GLuint client_id); 323 324 void DoDeleteBuffer(GLuint client_id, GLuint service_id); 325 void DoDeleteFramebuffer( 326 GLuint client_id, GLuint service_id, 327 bool reset_draw, GLenum draw_target, GLuint draw_id, 328 bool reset_read, GLenum read_target, GLuint read_id); 329 void DoDeleteProgram(GLuint client_id, GLuint service_id); 330 void DoDeleteRenderbuffer(GLuint client_id, GLuint service_id); 331 void DoDeleteShader(GLuint client_id, GLuint service_id); 332 void DoDeleteTexture(GLuint client_id, GLuint service_id); 333 void DoDeleteSampler(GLuint client_id, GLuint service_id); 334 void DoDeleteTransformFeedback(GLuint client_id, GLuint service_id); 335 336 void DoCompressedTexImage2D(GLenum target, 337 GLint level, 338 GLenum format, 339 GLsizei width, 340 GLsizei height, 341 GLint border, 342 GLsizei size, 343 uint32_t bucket_id); 344 void DoBindTexImage2DCHROMIUM(GLenum target, GLint image_id); 345 void DoTexImage2D(GLenum target, 346 GLint level, 347 GLenum internal_format, 348 GLsizei width, 349 GLsizei height, 350 GLint border, 351 GLenum format, 352 GLenum type, 353 uint32_t shared_memory_id, 354 uint32_t shared_memory_offset); 355 void DoTexImage2DConvertInternalFormat(GLenum target, 356 GLint level, 357 GLenum requested_internal_format, 358 GLsizei width, 359 GLsizei height, 360 GLint border, 361 GLenum format, 362 GLenum type, 363 uint32_t shared_memory_id, 364 uint32_t shared_memory_offset, 365 GLenum expected_internal_format); 366 void DoTexImage3D(GLenum target, 367 GLint level, 368 GLenum internal_format, 369 GLsizei width, 370 GLsizei height, 371 GLsizei depth, 372 GLint border, 373 GLenum format, 374 GLenum type, 375 uint32_t shared_memory_id, 376 uint32_t shared_memory_offset); 377 void DoCopyTexImage2D(GLenum target, 378 GLint level, 379 GLenum internal_format, 380 GLint x, 381 GLint y, 382 GLsizei width, 383 GLsizei height, 384 GLint border); 385 void DoRenderbufferStorage( 386 GLenum target, GLenum internal_format, GLenum actual_format, 387 GLsizei width, GLsizei height, GLenum error); 388 void DoFramebufferRenderbuffer( 389 GLenum target, 390 GLenum attachment, 391 GLenum renderbuffer_target, 392 GLuint renderbuffer_client_id, 393 GLuint renderbuffer_service_id, 394 GLenum error); 395 void DoFramebufferTexture2D( 396 GLenum target, GLenum attachment, GLenum tex_target, 397 GLuint texture_client_id, GLuint texture_service_id, 398 GLint level, GLenum error); 399 GLenum DoCheckFramebufferStatus(GLenum target); 400 void DoVertexAttribPointer( 401 GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset); 402 void DoVertexAttribDivisorANGLE(GLuint index, GLuint divisor); 403 404 void DoEnableDisable(GLenum cap, bool enable); 405 406 void SetDriverVertexAttribEnabled(GLint index, bool enable); 407 void DoEnableVertexAttribArray(GLint index); 408 409 void DoBufferData(GLenum target, GLsizei size); 410 411 void DoBufferSubData( 412 GLenum target, GLint offset, GLsizei size, const void* data); 413 414 void DoScissor(GLint x, GLint y, GLsizei width, GLsizei height); 415 416 void DoPixelStorei(GLenum pname, GLint param); 417 418 void SetupVertexBuffer(); 419 void SetupAllNeededVertexBuffers(); 420 421 void SetupIndexBuffer(); 422 423 void DeleteVertexBuffer(); 424 425 void DeleteIndexBuffer(); 426 427 void SetupClearTextureExpectations(GLuint service_id, 428 GLuint old_service_id, 429 GLenum bind_target, 430 GLenum target, 431 GLint level, 432 GLenum format, 433 GLenum type, 434 GLint xoffset, 435 GLint yoffset, 436 GLsizei width, 437 GLsizei height, 438 GLuint bound_pixel_unpack_buffer); 439 440 void SetupClearTexture3DExpectations(GLsizeiptr buffer_size, 441 GLenum target, 442 GLuint tex_service_id, 443 GLint level, 444 GLenum format, 445 GLenum type, 446 size_t tex_sub_image_3d_num_calls, 447 GLint* xoffset, 448 GLint* yoffset, 449 GLint* zoffset, 450 GLsizei* width, 451 GLsizei* height, 452 GLsizei* depth, 453 GLuint bound_pixel_unpack_buffer); 454 455 void SetupExpectationsForRestoreClearState(GLclampf restore_red, 456 GLclampf restore_green, 457 GLclampf restore_blue, 458 GLclampf restore_alpha, 459 GLuint restore_stencil, 460 GLclampf restore_depth, 461 bool restore_scissor_test, 462 GLint restore_scissor_x, 463 GLint restore_scissor_y, 464 GLsizei restore_scissor_width, 465 GLsizei restore_scissor_height); 466 467 void SetupExpectationsForFramebufferClearing(GLenum target, 468 GLuint clear_bits, 469 GLclampf restore_red, 470 GLclampf restore_green, 471 GLclampf restore_blue, 472 GLclampf restore_alpha, 473 GLuint restore_stencil, 474 GLclampf restore_depth, 475 bool restore_scissor_test, 476 GLint restore_scissor_x, 477 GLint restore_scissor_y, 478 GLsizei restore_scissor_width, 479 GLsizei restore_scissor_height); 480 481 void SetupExpectationsForFramebufferClearingMulti( 482 GLuint read_framebuffer_service_id, 483 GLuint draw_framebuffer_service_id, 484 GLenum target, 485 GLuint clear_bits, 486 GLclampf restore_red, 487 GLclampf restore_green, 488 GLclampf restore_blue, 489 GLclampf restore_alpha, 490 GLuint restore_stencil, 491 GLclampf restore_depth, 492 bool restore_scissor_test, 493 GLint restore_scissor_x, 494 GLint restore_scissor_y, 495 GLsizei restore_scissor_width, 496 GLsizei restore_scissor_height); 497 498 void SetupExpectationsForDepthMask(bool mask); 499 void SetupExpectationsForEnableDisable(GLenum cap, bool enable); 500 void SetupExpectationsForColorMask(bool red, 501 bool green, 502 bool blue, 503 bool alpha); 504 void SetupExpectationsForStencilMask(GLuint front_mask, GLuint back_mask); 505 506 void SetupExpectationsForApplyingDirtyState( 507 bool framebuffer_is_rgb, 508 bool framebuffer_has_depth, 509 bool framebuffer_has_stencil, 510 GLuint color_bits, // NOTE! bits are 0x1000, 0x0100, 0x0010, and 0x0001 511 bool depth_mask, 512 bool depth_enabled, 513 GLuint front_stencil_mask, 514 GLuint back_stencil_mask, 515 bool stencil_enabled); 516 517 void SetupExpectationsForApplyingDefaultDirtyState(); 518 519 void AddExpectationsForSimulatedAttrib0WithError( 520 GLsizei num_vertices, GLuint buffer_id, GLenum error); 521 522 void AddExpectationsForSimulatedAttrib0( 523 GLsizei num_vertices, GLuint buffer_id); 524 525 void AddExpectationsForGenVertexArraysOES(); 526 void AddExpectationsForDeleteVertexArraysOES(); 527 void AddExpectationsForDeleteBoundVertexArraysOES(); 528 void AddExpectationsForBindVertexArrayOES(); 529 void AddExpectationsForRestoreAttribState(GLuint attrib); 530 531 void DoInitializeDiscardableTextureCHROMIUM(GLuint texture_id); 532 void DoUnlockDiscardableTextureCHROMIUM(GLuint texture_id); 533 void DoLockDiscardableTextureCHROMIUM(GLuint texture_id); 534 bool IsDiscardableTextureUnlocked(GLuint texture_id); 535 BufferOffset(unsigned i)536 GLvoid* BufferOffset(unsigned i) { return reinterpret_cast<GLvoid*>(i); } 537 538 template <typename Command, typename Result> IsObjectHelper(GLuint client_id)539 bool IsObjectHelper(GLuint client_id) { 540 Result* result = static_cast<Result*>(shared_memory_address_); 541 Command cmd; 542 cmd.Init(client_id, shared_memory_id_, kSharedMemoryOffset); 543 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd)); 544 bool isObject = static_cast<bool>(*result); 545 EXPECT_EQ(GL_NO_ERROR, GetGLError()); 546 return isObject; 547 } 548 549 protected: 550 static const int kBackBufferWidth = 128; 551 static const int kBackBufferHeight = 64; 552 553 static const GLint kMaxTextureSize = 2048; 554 static const GLint kMaxCubeMapTextureSize = 256; 555 static const GLint kNumVertexAttribs = 16; 556 static const GLint kNumTextureUnits = 8; 557 static const GLint kMaxTextureImageUnits = 8; 558 static const GLint kMaxVertexTextureImageUnits = 2; 559 static const GLint kMaxFragmentUniformVectors = 16; 560 static const GLint kMaxVaryingVectors = 8; 561 static const GLint kMaxVertexUniformVectors = 128; 562 static const GLint kMaxViewportWidth = 8192; 563 static const GLint kMaxViewportHeight = 8192; 564 565 static const GLuint kServiceAttrib0BufferId = 801; 566 static const GLuint kServiceFixedAttribBufferId = 802; 567 568 static const GLuint kServiceBufferId = 301; 569 static const GLuint kServiceFramebufferId = 302; 570 static const GLuint kServiceRenderbufferId = 303; 571 static const GLuint kServiceTextureId = 304; 572 static const GLuint kServiceProgramId = 305; 573 static const GLuint kServiceSamplerId = 306; 574 static const GLuint kServiceShaderId = 307; 575 static const GLuint kServiceElementBufferId = 308; 576 static const GLuint kServiceQueryId = 309; 577 static const GLuint kServiceVertexArrayId = 310; 578 static const GLuint kServiceTransformFeedbackId = 311; 579 static const GLuint kServiceDefaultTransformFeedbackId = 312; 580 static const GLuint kServiceSyncId = 313; 581 582 static const size_t kSharedBufferSize = 2048; 583 static const uint32_t kSharedMemoryOffset = 132; 584 static const int32_t kInvalidSharedMemoryId = 585 FakeCommandBufferServiceBase::kTransferBufferBaseId - 1; 586 static const uint32_t kInvalidSharedMemoryOffset = kSharedBufferSize + 1; 587 static const uint32_t kInitialResult = 0xBDBDBDBDu; 588 static const uint8_t kInitialMemoryValue = 0xBDu; 589 590 static const uint32_t kNewClientId = 501; 591 static const uint32_t kNewServiceId = 502; 592 static const uint32_t kInvalidClientId = 601; 593 594 static const GLuint kServiceVertexShaderId = 321; 595 static const GLuint kServiceFragmentShaderId = 322; 596 597 static const GLuint kServiceCopyTextureChromiumShaderId = 701; 598 static const GLuint kServiceCopyTextureChromiumProgramId = 721; 599 600 static const GLuint kServiceCopyTextureChromiumTextureBufferId = 751; 601 static const GLuint kServiceCopyTextureChromiumVertexBufferId = 752; 602 static const GLuint kServiceCopyTextureChromiumFBOId = 753; 603 static const GLuint kServiceCopyTextureChromiumPositionAttrib = 761; 604 static const GLuint kServiceCopyTextureChromiumTexAttrib = 762; 605 static const GLuint kServiceCopyTextureChromiumSamplerLocation = 763; 606 607 static const GLsizei kNumVertices = 100; 608 static const GLsizei kNumIndices = 10; 609 static const int kValidIndexRangeStart = 1; 610 static const int kValidIndexRangeCount = 7; 611 static const int kInvalidIndexRangeStart = 0; 612 static const int kInvalidIndexRangeCount = 7; 613 static const int kOutOfRangeIndexRangeEnd = 10; 614 static const GLuint kMaxValidIndex = 7; 615 616 static const GLint kMaxAttribLength = 10; 617 static const char* kAttrib1Name; 618 static const char* kAttrib2Name; 619 static const char* kAttrib3Name; 620 static const GLint kAttrib1Size = 1; 621 static const GLint kAttrib2Size = 1; 622 static const GLint kAttrib3Size = 1; 623 static const GLint kAttrib1Location = 0; 624 static const GLint kAttrib2Location = 1; 625 static const GLint kAttrib3Location = 2; 626 static const GLenum kAttrib1Type = GL_FLOAT_VEC4; 627 static const GLenum kAttrib2Type = GL_FLOAT_VEC2; 628 static const GLenum kAttrib3Type = GL_FLOAT_VEC3; 629 static const GLint kInvalidAttribLocation = 30; 630 static const GLint kBadAttribIndex = kNumVertexAttribs; 631 632 static const GLint kMaxUniformLength = 12; 633 static const char* kUniform1Name; 634 static const char* kUniform2Name; 635 static const char* kUniform3Name; 636 static const char* kUniform4Name; 637 static const char* kUniform5Name; 638 static const char* kUniform6Name; 639 static const char* kUniform7Name; 640 static const char* kUniform8Name; 641 static const GLint kUniform1Size = 1; 642 static const GLint kUniform2Size = 3; 643 static const GLint kUniform3Size = 2; 644 static const GLint kUniform4Size = 1; 645 static const GLint kUniform5Size = 1; 646 static const GLint kUniform6Size = 1; 647 static const GLint kUniform7Size = 1; 648 static const GLint kUniform8Size = 2; 649 static const GLint kUniform1RealLocation = 3; 650 static const GLint kUniform2RealLocation = 10; 651 static const GLint kUniform2ElementRealLocation = 12; 652 static const GLint kUniform3RealLocation = 20; 653 static const GLint kUniform4RealLocation = 22; 654 static const GLint kUniform5RealLocation = 30; 655 static const GLint kUniform6RealLocation = 32; 656 static const GLint kUniform7RealLocation = 44; 657 static const GLint kUniform8RealLocation = 56; 658 static const GLint kUniform1FakeLocation = 0; // These are 659 static const GLint kUniform2FakeLocation = 1; // hardcoded 660 static const GLint kUniform2ElementFakeLocation = 0x10001; // to match 661 static const GLint kUniform3FakeLocation = 2; // ProgramManager. 662 static const GLint kUniform4FakeLocation = 3; // 663 static const GLint kUniform5FakeLocation = 4; // 664 static const GLint kUniform6FakeLocation = 5; // 665 static const GLint kUniform7FakeLocation = 6; // 666 static const GLint kUniform8FakeLocation = 7; // 667 static const GLint kUniform1DesiredLocation = -1; 668 static const GLint kUniform2DesiredLocation = -1; 669 static const GLint kUniform3DesiredLocation = -1; 670 static const GLint kUniform4DesiredLocation = -1; 671 static const GLint kUniform5DesiredLocation = -1; 672 static const GLint kUniform6DesiredLocation = -1; 673 static const GLint kUniform7DesiredLocation = -1; 674 static const GLint kUniform8DesiredLocation = -1; 675 static const GLenum kUniform1Type = GL_SAMPLER_2D; 676 static const GLenum kUniform2Type = GL_INT_VEC2; 677 static const GLenum kUniform3Type = GL_FLOAT_VEC3; 678 static const GLenum kUniform4Type = GL_UNSIGNED_INT; 679 static const GLenum kUniform5Type = GL_UNSIGNED_INT_VEC2; 680 static const GLenum kUniform6Type = GL_UNSIGNED_INT_VEC3; 681 static const GLenum kUniform7Type = GL_UNSIGNED_INT_VEC4; 682 static const GLenum kUniform8Type = GL_INT; 683 static const GLenum kUniformSamplerExternalType = GL_SAMPLER_EXTERNAL_OES; 684 static const GLenum kUniformCubemapType = GL_SAMPLER_CUBE; 685 static const GLint kInvalidUniformLocation = 30; 686 static const GLint kBadUniformIndex = 1000; 687 688 static const GLint kOutputVariable1Size = 0; 689 static const GLenum kOutputVariable1Type = GL_FLOAT_VEC4; 690 static const GLuint kOutputVariable1ColorName = 7; 691 static const GLuint kOutputVariable1Index = 0; 692 static const char* kOutputVariable1Name; 693 static const char* kOutputVariable1NameESSL3; 694 695 // Use StrictMock to make 100% sure we know how GL will be called. 696 std::unique_ptr<::testing::StrictMock<::gl::MockGLInterface>> gl_; 697 scoped_refptr<gl::GLSurfaceStub> surface_; 698 scoped_refptr<GLContextMock> context_; 699 std::unique_ptr<FakeCommandBufferServiceBase> command_buffer_service_; 700 TraceOutputter outputter_; 701 std::unique_ptr<MockGLES2Decoder> mock_decoder_; 702 std::unique_ptr<GLES2Decoder> decoder_; 703 std::unique_ptr<MemoryTracker> memory_tracker_; 704 705 bool surface_supports_draw_rectangle_ = false; 706 707 GLuint client_buffer_id_; 708 GLuint client_framebuffer_id_; 709 GLuint client_program_id_; 710 GLuint client_renderbuffer_id_; 711 GLuint client_sampler_id_; 712 GLuint client_shader_id_; 713 GLuint client_texture_id_; 714 GLuint client_element_buffer_id_; 715 GLuint client_vertex_shader_id_; 716 GLuint client_fragment_shader_id_; 717 GLuint client_query_id_; 718 GLuint client_vertexarray_id_; 719 GLuint client_transformfeedback_id_; 720 GLuint client_sync_id_; 721 722 int32_t shared_memory_id_; 723 uint32_t shared_memory_offset_; 724 void* shared_memory_address_; 725 void* shared_memory_base_; 726 727 GLuint service_renderbuffer_id_; 728 bool service_renderbuffer_valid_; 729 730 uint32_t immediate_buffer_[64]; 731 732 const bool ignore_cached_state_for_test_; 733 bool cached_color_mask_red_; 734 bool cached_color_mask_green_; 735 bool cached_color_mask_blue_; 736 bool cached_color_mask_alpha_; 737 bool cached_depth_mask_; 738 GLuint cached_stencil_front_mask_; 739 GLuint cached_stencil_back_mask_; 740 741 struct EnableFlags { 742 EnableFlags(); 743 bool cached_blend; 744 bool cached_cull_face; 745 bool cached_depth_test; 746 bool cached_dither; 747 bool cached_polygon_offset_fill; 748 bool cached_sample_alpha_to_coverage; 749 bool cached_sample_coverage; 750 bool cached_scissor_test; 751 bool cached_stencil_test; 752 }; 753 754 EnableFlags enable_flags_; 755 756 int shader_language_version_; 757 758 std::array<bool, kNumVertexAttribs> attribs_enabled_ = {}; 759 760 private: 761 // MockGLStates is used to track GL states and emulate driver 762 // behaviors on top of MockGLInterface. 763 class MockGLStates { 764 public: MockGLStates()765 MockGLStates() 766 : bound_array_buffer_object_(0), 767 bound_vertex_array_object_(0) { 768 } 769 770 ~MockGLStates() = default; 771 OnBindArrayBuffer(GLuint id)772 void OnBindArrayBuffer(GLuint id) { 773 bound_array_buffer_object_ = id; 774 } 775 OnBindVertexArrayOES(GLuint id)776 void OnBindVertexArrayOES(GLuint id) { 777 bound_vertex_array_object_ = id; 778 } 779 OnVertexAttribNullPointer()780 void OnVertexAttribNullPointer() { 781 // When a vertex array object is bound, some drivers (AMD Linux, 782 // Qualcomm, etc.) have a bug where it incorrectly generates an 783 // GL_INVALID_OPERATION on glVertexAttribPointer() if pointer 784 // is nullptr, no buffer is bound on GL_ARRAY_BUFFER. 785 // Make sure we don't trigger this bug. 786 if (bound_vertex_array_object_ != 0) 787 EXPECT_TRUE(bound_array_buffer_object_ != 0); 788 } 789 790 private: 791 GLuint bound_array_buffer_object_; 792 GLuint bound_vertex_array_object_; 793 }; // class MockGLStates 794 795 void AddExpectationsForVertexAttribManager(); 796 void SetupMockGLBehaviors(); 797 798 GpuPreferences gpu_preferences_; 799 MailboxManagerImpl mailbox_manager_; 800 ShaderTranslatorCache shader_translator_cache_; 801 FramebufferCompletenessCache framebuffer_completeness_cache_; 802 ImageManager image_manager_; 803 ServiceDiscardableManager discardable_manager_; 804 SharedImageManager shared_image_manager_; 805 scoped_refptr<ContextGroup> group_; 806 MockGLStates gl_states_; 807 base::test::SingleThreadTaskEnvironment task_environment_; 808 809 MockCopyTextureResourceManager* copy_texture_manager_; // not owned 810 MockCopyTexImageResourceManager* copy_tex_image_blitter_; // not owned 811 }; 812 813 class GLES2DecoderWithShaderTestBase : public GLES2DecoderTestBase { 814 public: GLES2DecoderWithShaderTestBase()815 GLES2DecoderWithShaderTestBase() 816 : GLES2DecoderTestBase() { 817 } 818 819 protected: 820 void SetUp() override; 821 void TearDown() override; 822 }; 823 824 // SpecializedSetup specializations that are needed in multiple unittest files. 825 template <> 826 void GLES2DecoderTestBase::SpecializedSetup<cmds::LinkProgram, 0>(bool valid); 827 828 MATCHER_P2(PointsToArray, array, size, "") { 829 for (size_t i = 0; i < static_cast<size_t>(size); ++i) { 830 if (arg[i] != array[i]) 831 return false; 832 } 833 return true; 834 } 835 836 class GLES2DecoderPassthroughTestBase : public testing::Test, 837 public DecoderClient { 838 public: 839 GLES2DecoderPassthroughTestBase(ContextType context_type); 840 ~GLES2DecoderPassthroughTestBase() override; 841 842 void OnConsoleMessage(int32_t id, const std::string& message) override; 843 void CacheShader(const std::string& key, const std::string& shader) override; 844 void OnFenceSyncRelease(uint64_t release) override; 845 void OnDescheduleUntilFinished() override; 846 void OnRescheduleAfterFinished() override; 847 void OnSwapBuffers(uint64_t swap_id, uint32_t flags) override; ScheduleGrContextCleanup()848 void ScheduleGrContextCleanup() override {} HandleReturnData(base::span<const uint8_t> data)849 void HandleReturnData(base::span<const uint8_t> data) override {} 850 851 void SetUp() override; 852 void TearDown() override; 853 854 template <typename T> GenHelper(GLuint client_id)855 void GenHelper(GLuint client_id) { 856 int8_t buffer[sizeof(T) + sizeof(client_id)]; 857 T& cmd = *reinterpret_cast<T*>(&buffer); 858 cmd.Init(1, &client_id); 859 EXPECT_EQ(error::kNoError, ExecuteImmediateCmd(cmd, sizeof(client_id))); 860 } 861 862 template <typename Command> IsObjectHelper(GLuint client_id)863 bool IsObjectHelper(GLuint client_id) { 864 typename Command::Result* result = 865 static_cast<typename Command::Result*>(shared_memory_address_); 866 Command cmd; 867 cmd.Init(client_id, shared_memory_id_, kSharedMemoryOffset); 868 EXPECT_EQ(error::kNoError, ExecuteCmd(cmd)); 869 bool isObject = static_cast<bool>(*result); 870 EXPECT_EQ(GL_NO_ERROR, GetGLError()); 871 return isObject; 872 } 873 874 template <typename T> ExecuteCmd(const T & cmd)875 error::Error ExecuteCmd(const T& cmd) { 876 static_assert(T::kArgFlags == cmd::kFixed, 877 "T::kArgFlags should equal cmd::kFixed"); 878 int entries_processed = 0; 879 return decoder_->DoCommands(1, (const void*)&cmd, 880 ComputeNumEntries(sizeof(cmd)), 881 &entries_processed); 882 } 883 884 template <typename T> ExecuteImmediateCmd(const T & cmd,size_t data_size)885 error::Error ExecuteImmediateCmd(const T& cmd, size_t data_size) { 886 static_assert(T::kArgFlags == cmd::kAtLeastN, 887 "T::kArgFlags should equal cmd::kAtLeastN"); 888 int entries_processed = 0; 889 return decoder_->DoCommands(1, (const void*)&cmd, 890 ComputeNumEntries(sizeof(cmd) + data_size), 891 &entries_processed); 892 } 893 894 void SetBucketData(uint32_t bucket_id, const void* data, size_t data_size); 895 896 template <typename T> GetSharedMemoryAs()897 T GetSharedMemoryAs() { 898 return reinterpret_cast<T>(shared_memory_address_); 899 } 900 901 template <typename T> GetSharedMemoryAsWithSize(size_t * out_shmem_size)902 T GetSharedMemoryAsWithSize(size_t* out_shmem_size) { 903 *out_shmem_size = shared_memory_size_; 904 return reinterpret_cast<T>(shared_memory_address_); 905 } 906 907 template <typename T> GetSharedMemoryAsWithOffset(uint32_t offset)908 T GetSharedMemoryAsWithOffset(uint32_t offset) { 909 void* ptr = reinterpret_cast<int8_t*>(shared_memory_address_) + offset; 910 return reinterpret_cast<T>(ptr); 911 } 912 913 template <typename T> GetSharedMemoryAsWithOffsetAndSize(uint32_t offset,size_t * out_shmem_size)914 T GetSharedMemoryAsWithOffsetAndSize(uint32_t offset, 915 size_t* out_shmem_size) { 916 EXPECT_LT(offset, shared_memory_size_); 917 *out_shmem_size = shared_memory_size_ - offset; 918 void* ptr = reinterpret_cast<int8_t*>(shared_memory_address_) + offset; 919 return reinterpret_cast<T>(ptr); 920 } 921 922 template <typename T> GetImmediateAs()923 T* GetImmediateAs() { 924 return reinterpret_cast<T*>(immediate_buffer_); 925 } 926 GetDecoder()927 GLES2DecoderPassthroughImpl* GetDecoder() const { return decoder_.get(); } GetPassthroughResources()928 PassthroughResources* GetPassthroughResources() const { 929 return group_->passthrough_resources(); 930 } GetSharedImageRepresentationFactory()931 SharedImageRepresentationFactory* GetSharedImageRepresentationFactory() 932 const { 933 return group_->shared_image_representation_factory(); 934 } GetSharedImageManager()935 SharedImageManager* GetSharedImageManager() { return &shared_image_manager_; } 936 const base::circular_deque<GLES2DecoderPassthroughImpl::PendingReadPixels>& GetPendingReadPixels()937 GetPendingReadPixels() const { 938 return decoder_->pending_read_pixels_; 939 } 940 941 GLint GetGLError(); 942 943 protected: 944 void DoRequestExtension(const char* extension); 945 946 void DoBindBuffer(GLenum target, GLuint client_id); 947 void DoDeleteBuffer(GLuint client_id); 948 void DoBufferData(GLenum target, 949 GLsizei size, 950 const void* data, 951 GLenum usage); 952 void DoBufferSubData(GLenum target, 953 GLint offset, 954 GLsizeiptr size, 955 const void* data); 956 957 void DoGenTexture(GLuint client_id); 958 bool DoIsTexture(GLuint client_id); 959 void DoBindTexture(GLenum target, GLuint client_id); 960 void DoDeleteTexture(GLuint client_id); 961 void DoTexImage2D(GLenum target, 962 GLint level, 963 GLenum internal_format, 964 GLsizei width, 965 GLsizei height, 966 GLint border, 967 GLenum format, 968 GLenum type, 969 uint32_t shared_memory_id, 970 uint32_t shared_memory_offset); 971 972 void DoBindFramebuffer(GLenum target, GLuint client_id); 973 void DoFramebufferTexture2D(GLenum target, 974 GLenum attachment, 975 GLenum textarget, 976 GLuint texture_client_id, 977 GLint level); 978 void DoFramebufferRenderbuffer(GLenum target, 979 GLenum attachment, 980 GLenum renderbuffertarget, 981 GLuint renderbuffer); 982 983 void DoBindRenderbuffer(GLenum target, GLuint client_id); 984 985 void DoGetIntegerv(GLenum pname, GLint* result, size_t num_results); 986 987 void DoInitializeDiscardableTextureCHROMIUM(GLuint client_id); 988 void DoUnlockDiscardableTextureCHROMIUM(GLuint client_id); 989 void DoLockDiscardableTextureCHROMIUM(GLuint client_id); 990 passthrough_discardable_texture_manager()991 PassthroughDiscardableManager* passthrough_discardable_texture_manager() { 992 return &passthrough_discardable_manager_; 993 } group()994 ContextGroup* group() { return group_.get(); } feature_info()995 FeatureInfo* feature_info() { return group_->feature_info(); } 996 997 static const size_t kSharedBufferSize = 2048; 998 static const uint32_t kSharedMemoryOffset = 132; 999 static const uint32_t kInvalidSharedMemoryOffset = kSharedBufferSize + 1; 1000 static const int32_t kInvalidSharedMemoryId = 1001 FakeCommandBufferServiceBase::kTransferBufferBaseId - 1; 1002 1003 static const uint32_t kNewClientId = 501; 1004 static const GLuint kClientBufferId = 100; 1005 static const GLuint kClientTextureId = 101; 1006 static const GLuint kClientFramebufferId = 102; 1007 static const GLuint kClientRenderbufferId = 103; 1008 1009 int32_t shared_memory_id_; 1010 uint32_t shared_memory_offset_; 1011 void* shared_memory_address_; 1012 void* shared_memory_base_; 1013 size_t shared_memory_size_; 1014 1015 uint32_t immediate_buffer_[64]; 1016 1017 private: 1018 ContextCreationAttribs context_creation_attribs_; 1019 GpuPreferences gpu_preferences_; 1020 MailboxManagerImpl mailbox_manager_; 1021 ShaderTranslatorCache shader_translator_cache_; 1022 FramebufferCompletenessCache framebuffer_completeness_cache_; 1023 ImageManager image_manager_; 1024 ServiceDiscardableManager discardable_manager_; 1025 PassthroughDiscardableManager passthrough_discardable_manager_; 1026 SharedImageManager shared_image_manager_; 1027 1028 scoped_refptr<gl::GLSurface> surface_; 1029 scoped_refptr<gl::GLContext> context_; 1030 std::unique_ptr<FakeCommandBufferServiceBase> command_buffer_service_; 1031 TraceOutputter outputter_; 1032 std::unique_ptr<GLES2DecoderPassthroughImpl> decoder_; 1033 scoped_refptr<ContextGroup> group_; 1034 }; 1035 1036 } // namespace gles2 1037 } // namespace gpu 1038 1039 #endif // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_UNITTEST_BASE_H_ 1040