raster_in_process_context.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2018 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. #include "gpu/ipc/raster_in_process_context.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/command_line.h"
  8. #include "base/logging.h"
  9. #include "base/run_loop.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "gpu/command_buffer/client/gles2_cmd_helper.h"
  12. #include "gpu/command_buffer/client/raster_cmd_helper.h"
  13. #include "gpu/command_buffer/client/raster_implementation_gles.h"
  14. #include "gpu/command_buffer/client/shared_memory_limits.h"
  15. #include "gpu/command_buffer/client/transfer_buffer.h"
  16. #include "gpu/command_buffer/common/command_buffer.h"
  17. #include "gpu/command_buffer/common/constants.h"
  18. #include "gpu/command_buffer/common/context_creation_attribs.h"
  19. #include "gpu/command_buffer/service/service_utils.h"
  20. #include "gpu/config/gpu_feature_info.h"
  21. #include "gpu/config/gpu_switches.h"
  22. #include "gpu/ipc/common/surface_handle.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. namespace gpu {
  25. RasterInProcessContext::RasterInProcessContext() = default;
  26. RasterInProcessContext::~RasterInProcessContext() {
  27. // Trigger any pending lost contexts. First do a full sync between client
  28. // and service threads. Then execute any pending tasks.
  29. if (raster_implementation_) {
  30. raster_implementation_->Finish();
  31. base::RunLoop().RunUntilIdle();
  32. raster_implementation_.reset();
  33. }
  34. transfer_buffer_.reset();
  35. helper_.reset();
  36. command_buffer_.reset();
  37. }
  38. ContextResult RasterInProcessContext::Initialize(
  39. CommandBufferTaskExecutor* task_executor,
  40. const ContextCreationAttribs& attribs,
  41. const SharedMemoryLimits& memory_limits,
  42. ImageFactory* image_factory,
  43. gpu::raster::GrShaderCache* gr_shader_cache,
  44. GpuProcessActivityFlags* activity_flags) {
  45. DCHECK(attribs.enable_raster_interface);
  46. if (!attribs.enable_raster_interface) {
  47. return ContextResult::kFatalFailure;
  48. }
  49. DCHECK(!attribs.enable_gles2_interface);
  50. if (attribs.enable_gles2_interface) {
  51. return ContextResult::kFatalFailure;
  52. }
  53. command_buffer_ =
  54. std::make_unique<InProcessCommandBuffer>(task_executor, GURL());
  55. auto result = command_buffer_->Initialize(attribs, image_factory,
  56. base::ThreadTaskRunnerHandle::Get(),
  57. gr_shader_cache, activity_flags);
  58. if (result != ContextResult::kSuccess) {
  59. DLOG(ERROR) << "Failed to initialize InProcessCommmandBuffer";
  60. return result;
  61. }
  62. // Check for consistency.
  63. DCHECK(!attribs.bind_generates_resource);
  64. constexpr bool bind_generates_resource = false;
  65. // Create the RasterCmdHelper, which writes the command buffer protocol.
  66. auto raster_helper =
  67. std::make_unique<raster::RasterCmdHelper>(command_buffer_.get());
  68. result = raster_helper->Initialize(memory_limits.command_buffer_size);
  69. if (result != ContextResult::kSuccess) {
  70. LOG(ERROR) << "Failed to initialize RasterCmdHelper";
  71. return result;
  72. }
  73. transfer_buffer_ = std::make_unique<TransferBuffer>(raster_helper.get());
  74. raster_implementation_ = std::make_unique<raster::RasterImplementation>(
  75. raster_helper.get(), transfer_buffer_.get(), bind_generates_resource,
  76. attribs.lose_context_when_out_of_memory, command_buffer_.get(),
  77. nullptr /* image_decode_accelerator */);
  78. result = raster_implementation_->Initialize(memory_limits);
  79. raster_implementation_->SetLostContextCallback(base::BindOnce(
  80. []() { EXPECT_TRUE(false) << "Unexpected lost context."; }));
  81. helper_ = std::move(raster_helper);
  82. return result;
  83. }
  84. const Capabilities& RasterInProcessContext::GetCapabilities() const {
  85. return command_buffer_->GetCapabilities();
  86. }
  87. const GpuFeatureInfo& RasterInProcessContext::GetGpuFeatureInfo() const {
  88. return command_buffer_->GetGpuFeatureInfo();
  89. }
  90. raster::RasterImplementation* RasterInProcessContext::GetImplementation() {
  91. return raster_implementation_.get();
  92. }
  93. ContextSupport* RasterInProcessContext::GetContextSupport() {
  94. return raster_implementation_.get();
  95. }
  96. SharedImageInterface* RasterInProcessContext::GetSharedImageInterface() {
  97. return command_buffer_->GetSharedImageInterface();
  98. }
  99. ServiceTransferCache* RasterInProcessContext::GetTransferCacheForTest() const {
  100. return command_buffer_->GetTransferCacheForTest();
  101. }
  102. InProcessCommandBuffer* RasterInProcessContext::GetCommandBufferForTest()
  103. const {
  104. return command_buffer_.get();
  105. }
  106. int RasterInProcessContext::GetRasterDecoderIdForTest() const {
  107. return command_buffer_->GetRasterDecoderIdForTest();
  108. }
  109. // static
  110. bool RasterInProcessContext::SupportedInTest() {
  111. const base::CommandLine* command_line =
  112. base::CommandLine::ForCurrentProcess();
  113. GpuPreferences gpu_preferences = gles2::ParseGpuPreferences(command_line);
  114. return !gpu_preferences.use_passthrough_cmd_decoder ||
  115. !gles2::PassthroughCommandDecoderSupported();
  116. }
  117. } // namespace gpu