raster_command_buffer_stub.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2017 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/service/raster_command_buffer_stub.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/memory/unsafe_shared_memory_region.h"
  8. #include "base/trace_event/trace_event.h"
  9. #include "build/build_config.h"
  10. #include "gpu/command_buffer/common/constants.h"
  11. #include "gpu/command_buffer/common/gpu_memory_buffer_support.h"
  12. #include "gpu/command_buffer/common/mailbox.h"
  13. #include "gpu/command_buffer/service/gl_context_virtual.h"
  14. #include "gpu/command_buffer/service/logger.h"
  15. #include "gpu/command_buffer/service/mailbox_manager.h"
  16. #include "gpu/command_buffer/service/memory_tracking.h"
  17. #include "gpu/command_buffer/service/raster_decoder.h"
  18. #include "gpu/command_buffer/service/service_utils.h"
  19. #include "gpu/command_buffer/service/sync_point_manager.h"
  20. #include "gpu/command_buffer/service/transfer_buffer_manager.h"
  21. #include "gpu/config/gpu_crash_keys.h"
  22. #include "gpu/ipc/service/gpu_channel.h"
  23. #include "gpu/ipc/service/gpu_channel_manager.h"
  24. #include "gpu/ipc/service/gpu_channel_manager_delegate.h"
  25. #include "gpu/ipc/service/gpu_memory_buffer_factory.h"
  26. #include "gpu/ipc/service/gpu_watchdog_thread.h"
  27. #include "ui/gl/gl_bindings.h"
  28. #include "ui/gl/gl_context.h"
  29. #include "ui/gl/gl_implementation.h"
  30. #include "ui/gl/gl_switches.h"
  31. #include "ui/gl/gl_workarounds.h"
  32. #include "ui/gl/init/gl_factory.h"
  33. #if BUILDFLAG(IS_WIN)
  34. #include "base/win/win_util.h"
  35. #endif
  36. #if BUILDFLAG(IS_ANDROID)
  37. #include "gpu/ipc/service/stream_texture_android.h"
  38. #endif
  39. namespace gpu {
  40. RasterCommandBufferStub::RasterCommandBufferStub(
  41. GpuChannel* channel,
  42. const mojom::CreateCommandBufferParams& init_params,
  43. CommandBufferId command_buffer_id,
  44. SequenceId sequence_id,
  45. int32_t stream_id,
  46. int32_t route_id)
  47. : CommandBufferStub(channel,
  48. init_params,
  49. command_buffer_id,
  50. sequence_id,
  51. stream_id,
  52. route_id) {}
  53. RasterCommandBufferStub::~RasterCommandBufferStub() {}
  54. gpu::ContextResult RasterCommandBufferStub::Initialize(
  55. CommandBufferStub* share_command_buffer_stub,
  56. const mojom::CreateCommandBufferParams& init_params,
  57. base::UnsafeSharedMemoryRegion shared_state_shm) {
  58. TRACE_EVENT0("gpu", "RasterBufferStub::Initialize");
  59. UpdateActiveUrl();
  60. GpuChannelManager* manager = channel_->gpu_channel_manager();
  61. DCHECK(manager);
  62. if (share_command_buffer_stub) {
  63. LOG(ERROR) << "Using a share group is not supported with RasterDecoder";
  64. return ContextResult::kFatalFailure;
  65. }
  66. if (surface_handle_ != kNullSurfaceHandle) {
  67. LOG(ERROR) << "ContextResult::kFatalFailure: "
  68. "RasterInterface clients must render offscreen.";
  69. return ContextResult::kFatalFailure;
  70. }
  71. if (init_params.attribs.gpu_preference != gl::GpuPreference::kLowPower ||
  72. init_params.attribs.context_type != CONTEXT_TYPE_OPENGLES2 ||
  73. init_params.attribs.bind_generates_resource) {
  74. LOG(ERROR) << "ContextResult::kFatalFailure: Incompatible creation attribs "
  75. "used with RasterDecoder";
  76. return ContextResult::kFatalFailure;
  77. }
  78. ContextResult result;
  79. auto shared_context_state = manager->GetSharedContextState(&result);
  80. if (!shared_context_state) {
  81. LOG(ERROR) << "ContextResult::kFatalFailure: "
  82. "Failed to create raster decoder state.";
  83. DCHECK_NE(result, gpu::ContextResult::kSuccess);
  84. return result;
  85. }
  86. DCHECK(shared_context_state->IsGLInitialized());
  87. surface_ = shared_context_state->surface();
  88. share_group_ = shared_context_state->share_group();
  89. use_virtualized_gl_context_ =
  90. shared_context_state->use_virtualized_gl_contexts();
  91. memory_tracker_ = CreateMemoryTracker();
  92. command_buffer_ =
  93. std::make_unique<CommandBufferService>(this, memory_tracker_.get());
  94. ImageFactory* image_factory =
  95. manager->gpu_memory_buffer_factory()
  96. ? manager->gpu_memory_buffer_factory()->AsImageFactory()
  97. : nullptr;
  98. std::unique_ptr<raster::RasterDecoder> decoder(raster::RasterDecoder::Create(
  99. this, command_buffer_.get(), manager->outputter(),
  100. manager->gpu_feature_info(), manager->gpu_preferences(),
  101. memory_tracker_.get(), manager->shared_image_manager(), image_factory,
  102. shared_context_state, channel()->is_gpu_host()));
  103. sync_point_client_state_ =
  104. channel_->sync_point_manager()->CreateSyncPointClientState(
  105. CommandBufferNamespace::GPU_IO, command_buffer_id_, sequence_id_);
  106. // TODO(sunnyps): Should this use ScopedCrashKey instead?
  107. crash_keys::gpu_gl_context_is_virtual.Set(use_virtualized_gl_context_ ? "1"
  108. : "0");
  109. scoped_refptr<gl::GLContext> context = shared_context_state->context();
  110. if (!shared_context_state->MakeCurrent(nullptr, false /* needs_gl */)) {
  111. LOG(ERROR) << "ContextResult::kTransientFailure: "
  112. "Failed to make context current.";
  113. return gpu::ContextResult::kTransientFailure;
  114. }
  115. // Initialize the decoder with either the view or pbuffer GLContext.
  116. result = decoder->Initialize(surface_, context, true /* offscreen */,
  117. gpu::gles2::DisallowedFeatures(),
  118. init_params.attribs);
  119. if (result != gpu::ContextResult::kSuccess) {
  120. DLOG(ERROR) << "Failed to initialize decoder.";
  121. return result;
  122. }
  123. if (manager->gpu_preferences().enable_gpu_service_logging) {
  124. decoder->SetLogCommands(true);
  125. }
  126. set_decoder_context(std::move(decoder));
  127. const size_t kSharedStateSize = sizeof(CommandBufferSharedState);
  128. base::WritableSharedMemoryMapping shared_state_mapping =
  129. shared_state_shm.MapAt(0, kSharedStateSize);
  130. if (!shared_state_mapping.IsValid()) {
  131. LOG(ERROR) << "ContextResult::kFatalFailure: "
  132. "Failed to map shared state buffer.";
  133. return gpu::ContextResult::kFatalFailure;
  134. }
  135. command_buffer_->SetSharedStateBuffer(MakeBackingFromSharedMemory(
  136. std::move(shared_state_shm), std::move(shared_state_mapping)));
  137. if (!active_url_.is_empty())
  138. manager->delegate()->DidCreateOffscreenContext(active_url_.url());
  139. manager->delegate()->DidCreateContextSuccessfully();
  140. initialized_ = true;
  141. return gpu::ContextResult::kSuccess;
  142. }
  143. MemoryTracker* RasterCommandBufferStub::GetContextGroupMemoryTracker() const {
  144. return nullptr;
  145. }
  146. void RasterCommandBufferStub::OnSwapBuffers(uint64_t swap_id, uint32_t flags) {}
  147. void RasterCommandBufferStub::SetActiveURL(GURL url) {
  148. active_url_ = ContextUrl(std::move(url));
  149. UpdateActiveUrl();
  150. }
  151. } // namespace gpu