当前位置:   article > 正文

Chromium源码由浅入深(五)_chromium源码解读

chromium源码解读

接前一篇文章:Chromium源码由浅入深(四)

上回书一步步跟踪,最终跟踪到components/viz/service/gl/gpu_service_impl.cc中相邻的两个函数(方法),分别是GpuServiceImpl::InitializeWithHost()和GpuServiceImpl::UpdateGPUInfoGL()。再次贴出代码如下:

  1. void GpuServiceImpl::UpdateGPUInfoGL() {
  2. DCHECK(main_runner_->BelongsToCurrentThread());
  3. gpu::CollectGraphicsInfoGL(&gpu_info_, GetContextState()->display());
  4. gpu_host_->DidUpdateGPUInfo(gpu_info_);
  5. }
  6. void GpuServiceImpl::InitializeWithHost(
  7. mojo::PendingRemote<mojom::GpuHost> pending_gpu_host,
  8. gpu::GpuProcessActivityFlags activity_flags,
  9. scoped_refptr<gl::GLSurface> default_offscreen_surface,
  10. gpu::SyncPointManager* sync_point_manager,
  11. gpu::SharedImageManager* shared_image_manager,
  12. gpu::Scheduler* scheduler,
  13. base::WaitableEvent* shutdown_event) {
  14. DCHECK(main_runner_->BelongsToCurrentThread());
  15. mojo::Remote<mojom::GpuHost> gpu_host(std::move(pending_gpu_host));
  16. gpu_host->DidInitialize(gpu_info_, gpu_feature_info_,
  17. gpu_info_for_hardware_gpu_,
  18. gpu_feature_info_for_hardware_gpu_, gpu_extra_info_);
  19. gpu_host_ = mojo::SharedRemote<mojom::GpuHost>(gpu_host.Unbind(), io_runner_);
  20. if (!in_host_process()) {
  21. // The global callback is reset from the dtor. So Unretained() here is safe.
  22. // Note that the callback can be called from any thread. Consequently, the
  23. // callback cannot use a WeakPtr.
  24. GetLogMessageManager()->InstallPostInitializeLogHandler(base::BindRepeating(
  25. &GpuServiceImpl::RecordLogMessage, base::Unretained(this)));
  26. }
  27. if (!sync_point_manager) {
  28. owned_sync_point_manager_ = std::make_unique<gpu::SyncPointManager>();
  29. sync_point_manager = owned_sync_point_manager_.get();
  30. }
  31. if (!shared_image_manager) {
  32. // When using real buffers for testing overlay configurations, we need
  33. // access to SharedImageManager on the viz thread to obtain the buffer
  34. // corresponding to a mailbox.
  35. const bool display_context_on_another_thread =
  36. features::IsDrDcEnabled() && !gpu_driver_bug_workarounds_.disable_drdc;
  37. bool thread_safe_manager = display_context_on_another_thread;
  38. // Raw draw needs to access shared image backing on the compositor thread.
  39. thread_safe_manager |= features::IsUsingRawDraw();
  40. #if BUILDFLAG(IS_OZONE)
  41. thread_safe_manager |= features::ShouldUseRealBuffersForPageFlipTest();
  42. #endif
  43. owned_shared_image_manager_ = std::make_unique<gpu::SharedImageManager>(
  44. thread_safe_manager, display_context_on_another_thread);
  45. shared_image_manager = owned_shared_image_manager_.get();
  46. #if BUILDFLAG(IS_OZONE)
  47. } else {
  48. // With this feature enabled, we don't expect to receive an external
  49. // SharedImageManager.
  50. DCHECK(!features::ShouldUseRealBuffersForPageFlipTest());
  51. #endif
  52. }
  53. shutdown_event_ = shutdown_event;
  54. if (!shutdown_event_) {
  55. owned_shutdown_event_ = std::make_unique<base::WaitableEvent>(
  56. base::WaitableEvent::ResetPolicy::MANUAL,
  57. base::WaitableEvent::InitialState::NOT_SIGNALED);
  58. shutdown_event_ = owned_shutdown_event_.get();
  59. }
  60. if (scheduler) {
  61. scheduler_ = scheduler;
  62. } else {
  63. owned_scheduler_ =
  64. std::make_unique<gpu::Scheduler>(sync_point_manager, gpu_preferences_);
  65. scheduler_ = owned_scheduler_.get();
  66. }
  67. // Defer creation of the render thread. This is to prevent it from handling
  68. // IPC messages before the sandbox has been enabled and all other necessary
  69. // initialization has succeeded.
  70. gpu_channel_manager_ = std::make_unique<gpu::GpuChannelManager>(
  71. gpu_preferences_, this, watchdog_thread_.get(), main_runner_, io_runner_,
  72. scheduler_, sync_point_manager, shared_image_manager,
  73. gpu_memory_buffer_factory_.get(), gpu_feature_info_,
  74. std::move(activity_flags), std::move(default_offscreen_surface),
  75. image_decode_accelerator_worker_.get(), vulkan_context_provider(),
  76. metal_context_provider_.get(), dawn_context_provider());
  77. media_gpu_channel_manager_ = std::make_unique<media::MediaGpuChannelManager>(
  78. gpu_channel_manager_.get());
  79. // Create and Initialize compositor gpu thread.
  80. compositor_gpu_thread_ = CompositorGpuThread::Create(
  81. gpu_channel_manager_.get(),
  82. #if BUILDFLAG(ENABLE_VULKAN)
  83. vulkan_implementation_,
  84. vulkan_context_provider_ ? vulkan_context_provider_->GetDeviceQueue()
  85. : nullptr,
  86. #else
  87. nullptr, nullptr,
  88. #endif
  89. gpu_channel_manager_->default_offscreen_surface()
  90. ? gpu_channel_manager_->default_offscreen_surface()->GetGLDisplay()
  91. : nullptr,
  92. !!watchdog_thread_);
  93. }

实际上这中间还有小插曲,这两个函数还不是最终调用的GpuProcessHost::DidInitialize()和GpuProcessHost::DidUpdateGPUInfo(),而是中间还有个delegate。在components/viz/host/gpu_host_impl.cc中,代码分别如下:

  1. void GpuHostImpl::DidInitialize(
  2. const gpu::GPUInfo& gpu_info,
  3. const gpu::GpuFeatureInfo& gpu_feature_info,
  4. const absl::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
  5. const absl::optional<gpu::GpuFeatureInfo>&
  6. gpu_feature_info_for_hardware_gpu,
  7. const gfx::GpuExtraInfo& gpu_extra_info) {
  8. UMA_HISTOGRAM_BOOLEAN("GPU.GPUProcessInitialized", true);
  9. delegate_->DidInitialize(gpu_info, gpu_feature_info,
  10. gpu_info_for_hardware_gpu,
  11. gpu_feature_info_for_hardware_gpu, gpu_extra_info);
  12. if (!params_.disable_gpu_shader_disk_cache) {
  13. SetChannelDiskCacheHandle(gpu::kDisplayCompositorClientId,
  14. gpu::kDisplayCompositorGpuDiskCacheHandle);
  15. SetChannelDiskCacheHandle(gpu::kGrShaderCacheClientId,
  16. gpu::kGrShaderGpuDiskCacheHandle);
  17. }
  18. }
  1. void GpuHostImpl::DidUpdateGPUInfo(const gpu::GPUInfo& gpu_info) {
  2. delegate_->DidUpdateGPUInfo(gpu_info);
  3. }

不过这些都是C++的小伎俩而已,不是本书的重点,一带而过就好。还是回到正题。

由上边GpuServiceImpl::InitializeWithHost()的代码可以看到,它的参数中并没有gpu_info_,但是却在调用的时候直接传入了gpu_info_,如下所示:

  1. void GpuServiceImpl::InitializeWithHost(
  2. mojo::PendingRemote<mojom::GpuHost> pending_gpu_host,
  3. gpu::GpuProcessActivityFlags activity_flags,
  4. scoped_refptr<gl::GLSurface> default_offscreen_surface,
  5. gpu::SyncPointManager* sync_point_manager,
  6. gpu::SharedImageManager* shared_image_manager,
  7. gpu::Scheduler* scheduler,
  8. base::WaitableEvent* shutdown_event) {
  9. DCHECK(main_runner_->BelongsToCurrentThread());
  10. mojo::Remote<mojom::GpuHost> gpu_host(std::move(pending_gpu_host));
  11. gpu_host->DidInitialize(gpu_info_, gpu_feature_info_,
  12. gpu_info_for_hardware_gpu_,
  13. gpu_feature_info_for_hardware_gpu_, gpu_extra_info_);
  14. ……
  15. }

由于gpu_info_是GpuServiceImpl类的成员变量(components/viz/service/gl/gpu_service_impl.h中定义):

  1. // Information about the GPU, such as device and vendor ID.
  2. gpu::GPUInfo gpu_info_;

因此能够推测,很有可能是在GpuServiceImpl的构造函数中传入的。为了印证这一推测,在Chromium源码路径下搜索创建GpuServiceImpl类的对象的相关代码。结果发现结果太多了(一共2、300处,过滤之后也有小100处),因此决定不走这条路,而走另外一条路,即在Chromium源码路径下搜索与以下函数相关的内容。

  1. void GpuServiceImpl::UpdateGPUInfoGL() {
  2. DCHECK(main_runner_->BelongsToCurrentThread());
  3. gpu::CollectGraphicsInfoGL(&gpu_info_, GetContextState()->display());
  4. gpu_host_->DidUpdateGPUInfo(gpu_info_);
  5. }

果然少多了,只有几处而已。这样就很便于分析问题了。下一回将深入解析这些相关代码。

至于本回,在结束之前,还要说明一下:虽然上边由于搜索创建GpuServiceImpl对象的相关代码的结果太多而放弃,但是推测倒是被印证了。因为GpuServiceImpl类的构造函数本身就已说明了问题。在components/viz/service/gl/gpu_service_impl.cc中,代码如下:

  1. GpuServiceImpl::GpuServiceImpl(
  2. const gpu::GPUInfo& gpu_info,
  3. std::unique_ptr<gpu::GpuWatchdogThread> watchdog_thread,
  4. scoped_refptr<base::SingleThreadTaskRunner> io_runner,
  5. const gpu::GpuFeatureInfo& gpu_feature_info,
  6. const gpu::GpuPreferences& gpu_preferences,
  7. const absl::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
  8. const absl::optional<gpu::GpuFeatureInfo>&
  9. gpu_feature_info_for_hardware_gpu,
  10. const gfx::GpuExtraInfo& gpu_extra_info,
  11. gpu::VulkanImplementation* vulkan_implementation,
  12. base::OnceCallback<void(ExitCode)> exit_callback)
  13. : main_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
  14. io_runner_(std::move(io_runner)),
  15. watchdog_thread_(std::move(watchdog_thread)),
  16. gpu_preferences_(gpu_preferences),
  17. gpu_info_(gpu_info),
  18. gpu_feature_info_(gpu_feature_info),
  19. gpu_driver_bug_workarounds_(
  20. gpu_feature_info.enabled_gpu_driver_bug_workarounds),
  21. gpu_info_for_hardware_gpu_(gpu_info_for_hardware_gpu),
  22. gpu_feature_info_for_hardware_gpu_(gpu_feature_info_for_hardware_gpu),
  23. gpu_extra_info_(gpu_extra_info),
  24. #if BUILDFLAG(ENABLE_VULKAN)
  25. vulkan_implementation_(vulkan_implementation),
  26. #endif
  27. exit_callback_(std::move(exit_callback)) {
  28. DCHECK(!io_runner_->BelongsToCurrentThread());
  29. DCHECK(exit_callback_);
  30. #if BUILDFLAG(IS_CHROMEOS_ASH) && BUILDFLAG(USE_CHROMEOS_MEDIA_ACCELERATION)
  31. protected_buffer_manager_ = new arc::ProtectedBufferManager();
  32. #endif // BUILDFLAG(IS_CHROMEOS_ASH) &&
  33. // BUILDFLAG(USE_CHROMEOS_MEDIA_ACCELERATION)
  34. GrContextOptions context_options =
  35. GetDefaultGrContextOptions(gpu_preferences_.gr_context_type);
  36. if (gpu_preferences_.force_max_texture_size) {
  37. context_options.fMaxTextureSizeOverride =
  38. gpu_preferences_.force_max_texture_size;
  39. }
  40. #if BUILDFLAG(ENABLE_VULKAN)
  41. if (vulkan_implementation_) {
  42. bool is_native_vulkan =
  43. gpu_preferences_.use_vulkan == gpu::VulkanImplementationName::kNative ||
  44. gpu_preferences_.use_vulkan ==
  45. gpu::VulkanImplementationName::kForcedNative;
  46. // With swiftshader the vendor_id is 0xffff. For some tests gpu_info is not
  47. // initialized, so the vendor_id is 0.
  48. bool is_native_gl =
  49. gpu_info_.gpu.vendor_id != 0xffff && gpu_info_.gpu.vendor_id != 0;
  50. const bool is_thread_safe =
  51. features::IsDrDcEnabled() && !gpu_driver_bug_workarounds_.disable_drdc;
  52. // If GL is using a real GPU, the gpu_info will be passed in and vulkan will
  53. // use the same GPU.
  54. vulkan_context_provider_ = VulkanInProcessContextProvider::Create(
  55. vulkan_implementation_, gpu_preferences_.vulkan_heap_memory_limit,
  56. gpu_preferences_.vulkan_sync_cpu_memory_limit, is_thread_safe,
  57. (is_native_vulkan && is_native_gl) ? &gpu_info : nullptr);
  58. if (!vulkan_context_provider_) {
  59. DLOG(ERROR) << "Failed to create Vulkan context provider.";
  60. }
  61. }
  62. #endif
  63. #if BUILDFLAG(SKIA_USE_DAWN)
  64. if (gpu_preferences_.gr_context_type == gpu::GrContextType::kDawn) {
  65. dawn_context_provider_ = DawnContextProvider::Create();
  66. if (!dawn_context_provider_) {
  67. DLOG(ERROR) << "Failed to create Dawn context provider.";
  68. }
  69. }
  70. #endif
  71. #if BUILDFLAG(USE_VAAPI_IMAGE_CODECS)
  72. image_decode_accelerator_worker_ =
  73. media::VaapiImageDecodeAcceleratorWorker::Create();
  74. #endif // BUILDFLAG(USE_VAAPI_IMAGE_CODECS)
  75. #if BUILDFLAG(IS_APPLE)
  76. if (gpu_feature_info_.status_values[gpu::GPU_FEATURE_TYPE_METAL] ==
  77. gpu::kGpuFeatureStatusEnabled) {
  78. metal_context_provider_ = MetalContextProvider::Create(context_options);
  79. }
  80. #endif
  81. #if BUILDFLAG(IS_WIN)
  82. if (media::SupportMediaFoundationClearPlayback()) {
  83. // Initialize the OverlayStateService using the GPUServiceImpl task
  84. // sequence.
  85. auto* overlay_state_service = OverlayStateService::GetInstance();
  86. overlay_state_service->Initialize(
  87. base::SequencedTaskRunner::GetCurrentDefault());
  88. }
  89. // Add GpuServiceImpl to DirectCompositionOverlayCapsMonitor observer list for
  90. // overlay and DXGI info update.
  91. gl::DirectCompositionOverlayCapsMonitor::GetInstance()->AddObserver(this);
  92. #endif
  93. gpu_memory_buffer_factory_ = gpu::GpuMemoryBufferFactory::CreateNativeType(
  94. vulkan_context_provider(), io_runner_);
  95. weak_ptr_ = weak_ptr_factory_.GetWeakPtr();
  96. }

看到以下代码片段了吧:

  1. GpuServiceImpl::GpuServiceImpl(
  2. const gpu::GPUInfo& gpu_info,
  3. std::unique_ptr<gpu::GpuWatchdogThread> watchdog_thread,
  4. scoped_refptr<base::SingleThreadTaskRunner> io_runner,
  5. const gpu::GpuFeatureInfo& gpu_feature_info,
  6. const gpu::GpuPreferences& gpu_preferences,
  7. const absl::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
  8. const absl::optional<gpu::GpuFeatureInfo>&
  9. gpu_feature_info_for_hardware_gpu,
  10. const gfx::GpuExtraInfo& gpu_extra_info,
  11. gpu::VulkanImplementation* vulkan_implementation,
  12. base::OnceCallback<void(ExitCode)> exit_callback)
  13. : main_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
  14. io_runner_(std::move(io_runner)),
  15. watchdog_thread_(std::move(watchdog_thread)),
  16. gpu_preferences_(gpu_preferences),
  17. gpu_info_(gpu_info),
  18. ……
  19. }

这就说明在构造GpuServiceImpl类对象的时候,gpu_info参数就是由调用者传入的,进而赋给了gpu_info_。这个分支等将来对于Chromium代码更加熟悉了,再回过头来研究。

欲知后事如何,且看下回分解。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/882219
推荐阅读
相关标签
  

闽ICP备14008679号