[go: nahoru, domu]

blob: e4ecb5eeed060988b554c33f7e7ff9e89f311c7e [file] [log] [blame]
fsamuelb25f8542016-04-22 19:14:071// Copyright 2016 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
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:385#include "components/viz/service/gl/gpu_service_impl.h"
fsamuelb25f8542016-04-22 19:14:076
Jinho Bang24ca9cd2018-01-02 22:01:007#include <memory>
Jonathan Backera57031b2018-06-04 15:44:328#include <string>
Claudio DeSouza591a9972018-02-21 17:27:169#include <utility>
Adlai Holler7a7503a2020-08-05 21:22:1710#include <vector>
Jinho Bang24ca9cd2018-01-02 22:01:0011
fsamuel4af18692016-11-22 21:41:3412#include "base/bind.h"
Zhenyao Mo74b0491b2017-08-19 06:53:1713#include "base/command_line.h"
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:3414#include "base/feature_list.h"
Dale Curtis1b6becebb2020-03-30 20:13:3515#include "base/no_destructor.h"
Gabriel Charette44db1422018-08-06 11:19:3316#include "base/task/post_task.h"
Gabriel Charettedd8d5985e2020-02-26 18:38:3517#include "base/task/thread_pool.h"
Robert Liao1214b5e2017-07-29 02:48:5818#include "base/task_runner_util.h"
penghuang591151b2016-06-04 13:45:3219#include "base/threading/thread_task_runner_handle.h"
penghuangcab01d22016-06-12 15:16:3920#include "build/build_config.h"
Yuta Hijikata5a9692ce2020-11-20 07:26:1621#include "build/chromeos_buildflags.h"
Saman Sami1cf52172020-01-13 19:05:2722#include "components/viz/common/features.h"
Christopher Cameron4f7ce362019-04-26 02:21:3723#include "components/viz/common/gpu/metal_context_provider.h"
fsamuel2a2351a92016-12-01 01:08:0424#include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
penghuang591151b2016-06-04 13:45:3225#include "gpu/command_buffer/service/gpu_switches.h"
sunnyps8f9139e2017-05-12 17:53:2526#include "gpu/command_buffer/service/scheduler.h"
Peng Huang8294a5a7f2019-01-23 18:56:2627#include "gpu/command_buffer/service/shared_context_state.h"
Peng Huang0fb98bc2020-04-07 22:08:2828#include "gpu/command_buffer/service/skia_utils.h"
penghuang591151b2016-06-04 13:45:3229#include "gpu/command_buffer/service/sync_point_manager.h"
Robert Liao1214b5e2017-07-29 02:48:5830#include "gpu/config/dx_diag_node.h"
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:3431#include "gpu/config/gpu_finch_features.h"
penghuang591151b2016-06-04 13:45:3232#include "gpu/config/gpu_info_collector.h"
33#include "gpu/config/gpu_switches.h"
34#include "gpu/config/gpu_util.h"
Khushalc2667e32018-07-20 01:07:1835#include "gpu/ipc/common/gpu_client_ids.h"
penghuang591151b2016-06-04 13:45:3236#include "gpu/ipc/common/gpu_memory_buffer_support.h"
jonross1f2903d2020-03-06 13:59:4037#include "gpu/ipc/common/gpu_peak_memory.h"
penghuang591151b2016-06-04 13:45:3238#include "gpu/ipc/common/memory_stats.h"
Sadrul Habib Chowdhury7102df0b2018-05-30 03:04:1739#include "gpu/ipc/in_process_command_buffer.h"
sunnyps41a6a15a2017-03-31 00:38:5440#include "gpu/ipc/service/gpu_channel.h"
fsamuel4af18692016-11-22 21:41:3441#include "gpu/ipc/service/gpu_channel_manager.h"
penghuang591151b2016-06-04 13:45:3242#include "gpu/ipc/service/gpu_memory_buffer_factory.h"
sadrul2fb7e152016-08-30 05:21:4543#include "gpu/ipc/service/gpu_watchdog_thread.h"
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:3444#include "gpu/ipc/service/image_decode_accelerator_worker.h"
Peng Huangcfc3180d2018-07-16 21:47:0845#include "gpu/vulkan/buildflags.h"
penghuang591151b2016-06-04 13:45:3246#include "ipc/ipc_channel_handle.h"
sunnyps41a6a15a2017-03-31 00:38:5447#include "ipc/ipc_sync_channel.h"
penghuang591151b2016-06-04 13:45:3248#include "ipc/ipc_sync_message_filter.h"
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:3449#include "media/gpu/buildflags.h"
Miguel Casasbe4dac82017-12-20 17:52:3950#include "media/gpu/gpu_video_accelerator_util.h"
Miguel Casas-Sanchezf5261d92017-07-26 06:31:3451#include "media/gpu/gpu_video_encode_accelerator_factory.h"
penghuang591151b2016-06-04 13:45:3252#include "media/gpu/ipc/service/gpu_video_decode_accelerator.h"
xhwangb2402d92016-10-15 07:29:3753#include "media/gpu/ipc/service/media_gpu_channel_manager.h"
Miguel Casas-Sanchez7d464702017-07-29 14:19:2154#include "media/mojo/services/mojo_video_encode_accelerator_provider.h"
Gyuyoung Kima1ddaae2019-09-12 07:30:2555#include "mojo/public/cpp/bindings/self_owned_receiver.h"
Sean Gilhulye5342322019-11-08 16:40:5356#include "skia/buildflags.h"
Adlai Holler7a7503a2020-08-05 21:22:1757#include "third_party/skia/include/gpu/GrDirectContext.h"
Peng Huangfb0cf372018-03-22 17:29:2758#include "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h"
59#include "third_party/skia/include/gpu/gl/GrGLInterface.h"
60#include "ui/gl/gl_context.h"
penghuang591151b2016-06-04 13:45:3261#include "ui/gl/gl_implementation.h"
62#include "ui/gl/gl_switches.h"
Kenneth Russell3689f3e2018-08-07 00:45:4063#include "ui/gl/gl_utils.h"
penghuang591151b2016-06-04 13:45:3264#include "ui/gl/gpu_switching_manager.h"
Peng Huangfb0cf372018-03-22 17:29:2765#include "ui/gl/init/create_gr_gl_interface.h"
penghuang591151b2016-06-04 13:45:3266#include "ui/gl/init/gl_factory.h"
67#include "url/gurl.h"
fsamuelb25f8542016-04-22 19:14:0768
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:3469#if BUILDFLAG(USE_VAAPI)
Gil Dekel270ab992019-07-29 22:59:1070#include "media/gpu/vaapi/vaapi_image_decode_accelerator_worker.h"
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:3471#endif // BUILDFLAG(USE_VAAPI)
72
sadrul9cef0df2017-03-03 18:18:0773#if defined(OS_ANDROID)
Andrew Grieve26407882018-08-28 15:15:3374#include "components/viz/service/gl/throw_uncaught_exception.h"
Dale Curtisa3f8ba32020-04-27 23:01:0975#include "media/base/android/media_codec_util.h"
sadrul9cef0df2017-03-03 18:18:0776#endif
77
Yuta Hijikata5a9692ce2020-11-20 07:26:1678#if BUILDFLAG(IS_CHROMEOS_ASH)
Hirokazu Honda12eaf9c2017-12-12 02:40:1679#include "components/arc/video_accelerator/gpu_arc_video_decode_accelerator.h"
Peng Huang417f0cf72017-11-20 20:06:1680#include "components/arc/video_accelerator/gpu_arc_video_encode_accelerator.h"
Hirokazu Honda05268502018-03-31 05:18:3981#include "components/arc/video_accelerator/gpu_arc_video_protected_buffer_allocator.h"
Peng Huang417f0cf72017-11-20 20:06:1682#include "components/arc/video_accelerator/protected_buffer_manager.h"
83#include "components/arc/video_accelerator/protected_buffer_manager_proxy.h"
Wei Lee01d63ef2019-05-09 09:24:5084#include "components/chromeos_camera/gpu_mjpeg_decode_accelerator_factory.h"
85#include "components/chromeos_camera/mojo_jpeg_encode_accelerator_service.h"
86#include "components/chromeos_camera/mojo_mjpeg_decode_accelerator_service.h"
Yuta Hijikata5a9692ce2020-11-20 07:26:1687#endif // BUILDFLAG(IS_CHROMEOS_ASH)
Peng Huang417f0cf72017-11-20 20:06:1688
Christopher Cameron51e1d9e2017-10-14 00:01:4589#if defined(OS_WIN)
Zhenyao Moe0bc16a2019-05-21 19:24:5990#include "ui/gl/direct_composition_surface_win.h"
Christopher Cameron51e1d9e2017-10-14 00:01:4591#endif
92
Avi Drissman8fd92172020-07-28 15:49:4593#if defined(OS_APPLE)
Sidney San Martín9387b402018-06-08 18:13:2694#include "ui/base/cocoa/quartz_util.h"
95#endif
96
Sean Gilhulye5342322019-11-08 16:40:5397#if BUILDFLAG(SKIA_USE_DAWN)
98#include "components/viz/common/gpu/dawn_context_provider.h"
99#endif
100
Sebastien Marchand836d04b32020-04-28 18:10:03101#if BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX)
102#include "base/test/clang_profiling.h"
103#endif
104
Peng Huang81a9a982020-05-13 02:55:20105#if BUILDFLAG(ENABLE_VULKAN)
106#include "components/viz/common/gpu/vulkan_context_provider.h"
107#include "components/viz/common/gpu/vulkan_in_process_context_provider.h"
108#endif
109
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38110namespace viz {
sadrulad8b21be2016-08-26 00:45:09111
sadrul6aafa7d2017-03-22 22:56:35112namespace {
113
Dale Curtisef404832020-03-31 23:05:42114using LogCallback = base::RepeatingCallback<
115 void(int severity, const std::string& header, const std::string& message)>;
Dale Curtis1b6becebb2020-03-30 20:13:35116
117struct LogMessage {
118 LogMessage(int severity,
119 const std::string& header,
120 const std::string& message)
121 : severity(severity),
122 header(std::move(header)),
123 message(std::move(message)) {}
124 const int severity;
125 const std::string header;
126 const std::string message;
127};
128
Dale Curtisef404832020-03-31 23:05:42129// Forward declare log handlers so they can be used within LogMessageManager.
130bool PreInitializeLogHandler(int severity,
131 const char* file,
132 int line,
133 size_t message_start,
134 const std::string& message);
135bool PostInitializeLogHandler(int severity,
136 const char* file,
137 int line,
138 size_t message_start,
139 const std::string& message);
140
141// Class which manages LOG() message forwarding before and after GpuServiceImpl
142// InitializeWithHost(). Prior to initialize, log messages are deferred and kept
143// within the class. During initialize, InstallPostInitializeLogHandler() will
144// be called to flush deferred messages and route new ones directly to GpuHost.
145class LogMessageManager {
146 public:
147 LogMessageManager() = default;
148 ~LogMessageManager() = delete;
149
150 // Queues a deferred LOG() message into |deferred_messages_| unless
151 // |log_callback_| has been set -- in which case RouteMessage() is called.
152 void AddDeferredMessage(int severity,
153 const std::string& header,
154 const std::string& message) {
155 base::AutoLock lock(message_lock_);
156 // During InstallPostInitializeLogHandler() there's a brief window where a
157 // call into this function may be waiting on |message_lock_|, so we need to
158 // check if |log_callback_| was set once we get the lock.
159 if (log_callback_) {
160 RouteMessage(severity, std::move(header), std::move(message));
161 return;
162 }
163
164 // Otherwise just queue the message for InstallPostInitializeLogHandler() to
165 // forward later.
166 deferred_messages_.emplace_back(severity, std::move(header),
167 std::move(message));
168 }
169
170 // Used after InstallPostInitializeLogHandler() to route messages directly to
171 // |log_callback_|; avoids the need for a global lock.
172 void RouteMessage(int severity,
173 const std::string& header,
174 const std::string& message) {
175 log_callback_.Run(severity, std::move(header), std::move(message));
176 }
177
178 // If InstallPostInitializeLogHandler() will never be called, this method is
179 // called prior to process exit to ensure logs are forwarded.
180 void FlushMessages(mojom::GpuHost* gpu_host) {
181 base::AutoLock lock(message_lock_);
182 for (auto& log : deferred_messages_) {
183 gpu_host->RecordLogMessage(log.severity, std::move(log.header),
184 std::move(log.message));
185 }
186 deferred_messages_.clear();
187 }
188
189 // Used prior to InitializeWithHost() during GpuMain startup to ensure logs
190 // aren't lost before initialize.
191 void InstallPreInitializeLogHandler() {
192 DCHECK(!log_callback_);
193 logging::SetLogMessageHandler(PreInitializeLogHandler);
194 }
195
196 // Called by InitializeWithHost() to take over logging from the
197 // PostInitializeLogHandler(). Flushes all deferred messages.
198 void InstallPostInitializeLogHandler(LogCallback log_callback) {
199 base::AutoLock lock(message_lock_);
200 DCHECK(!log_callback_);
201 log_callback_ = std::move(log_callback);
202 for (auto& log : deferred_messages_)
203 RouteMessage(log.severity, std::move(log.header), std::move(log.message));
204 deferred_messages_.clear();
205 logging::SetLogMessageHandler(PostInitializeLogHandler);
206 }
207
208 // Called when it's no longer safe to invoke |log_callback_|.
209 void ShutdownLogging() { logging::SetLogMessageHandler(nullptr); }
210
211 private:
212 base::Lock message_lock_;
213 std::vector<LogMessage> deferred_messages_ GUARDED_BY(message_lock_);
214
215 // Set once under |mesage_lock_|, but may be accessed without lock after that.
216 LogCallback log_callback_;
217};
218
219LogMessageManager* GetLogMessageManager() {
220 static base::NoDestructor<LogMessageManager> message_manager;
221 return message_manager.get();
Dale Curtis1b6becebb2020-03-30 20:13:35222}
223
Dale Curtisef404832020-03-31 23:05:42224bool PreInitializeLogHandler(int severity,
225 const char* file,
226 int line,
227 size_t message_start,
228 const std::string& message) {
229 GetLogMessageManager()->AddDeferredMessage(severity,
230 message.substr(0, message_start),
231 message.substr(message_start));
232 return false;
233}
234
235bool PostInitializeLogHandler(int severity,
236 const char* file,
237 int line,
238 size_t message_start,
239 const std::string& message) {
240 GetLogMessageManager()->RouteMessage(severity,
241 message.substr(0, message_start),
242 message.substr(message_start));
243 return false;
Dale Curtis1b6becebb2020-03-30 20:13:35244}
sadrul6aafa7d2017-03-22 22:56:35245
Wei Lee01d63ef2019-05-09 09:24:50246bool IsAcceleratedJpegDecodeSupported() {
Yuta Hijikata5a9692ce2020-11-20 07:26:16247#if BUILDFLAG(IS_CHROMEOS_ASH)
Wei Lee01d63ef2019-05-09 09:24:50248 return chromeos_camera::GpuMjpegDecodeAcceleratorFactory::
249 IsAcceleratedJpegDecodeSupported();
250#else
251 return false;
Yuta Hijikata5a9692ce2020-11-20 07:26:16252#endif // BUILDFLAG(IS_CHROMEOS_ASH)
Wei Lee01d63ef2019-05-09 09:24:50253}
254
Dale Curtisa3f8ba32020-04-27 23:01:09255void GetVideoCapabilities(const gpu::GpuPreferences& gpu_preferences,
256 const gpu::GpuDriverBugWorkarounds& gpu_workarounds,
257 gpu::GPUInfo* gpu_info) {
258 // Due to https://crbug.com/709631, we don't want to query Android video
259 // decode/encode capabilities during startup. The renderer needs this info
260 // though, so assume some baseline capabilities.
261#if defined(OS_ANDROID)
262 // Note: Video encoding on Android relies on MediaCodec, so all cases
263 // where it's disabled for decoding it is also disabled for encoding.
264 if (gpu_preferences.disable_accelerated_video_decode ||
265 gpu_preferences.disable_accelerated_video_encode) {
266 return;
267 }
268
269 auto& encoding_profiles =
270 gpu_info->video_encode_accelerator_supported_profiles;
271
272 gpu::VideoEncodeAcceleratorSupportedProfile vea_profile;
273 vea_profile.max_resolution = gfx::Size(1280, 720);
274 vea_profile.max_framerate_numerator = 30;
275 vea_profile.max_framerate_denominator = 1;
276
277 if (media::MediaCodecUtil::IsVp8EncoderAvailable()) {
278 vea_profile.profile = gpu::VP8PROFILE_ANY;
279 encoding_profiles.push_back(vea_profile);
280 }
281
282#if BUILDFLAG(USE_PROPRIETARY_CODECS)
283 if (media::MediaCodecUtil::IsH264EncoderAvailable(/*use_codec_list*/ false)) {
284 vea_profile.profile = gpu::H264PROFILE_BASELINE;
285 encoding_profiles.push_back(vea_profile);
286 }
287#endif
288
289 // Note: Since Android doesn't have to support PPAPI/Flash, we have not
290 // returned the decoder profiles here since https://crrev.com/665999.
291#else
292 gpu_info->video_decode_accelerator_capabilities =
293 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences,
294 gpu_workarounds);
295 gpu_info->video_encode_accelerator_supported_profiles =
296 media::GpuVideoAcceleratorUtil::ConvertMediaToGpuEncodeProfiles(
297 media::GpuVideoEncodeAcceleratorFactory::GetSupportedProfiles(
Miguel Casas0acd0c22020-09-01 17:07:18298 gpu_preferences, gpu_workarounds));
Dale Curtisa3f8ba32020-04-27 23:01:09299#endif
300}
301
sadrul0526dc5e2017-03-28 20:04:29302// Returns a callback which does a PostTask to run |callback| on the |runner|
303// task runner.
Sidney San Martín9387b402018-06-08 18:13:26304template <typename... Params>
305base::OnceCallback<void(Params&&...)> WrapCallback(
sadrul0526dc5e2017-03-28 20:04:29306 scoped_refptr<base::SingleThreadTaskRunner> runner,
Sidney San Martín9387b402018-06-08 18:13:26307 base::OnceCallback<void(Params...)> callback) {
tzik7ea6636332017-08-17 10:19:57308 return base::BindOnce(
309 [](base::SingleThreadTaskRunner* runner,
Sidney San Martín9387b402018-06-08 18:13:26310 base::OnceCallback<void(Params && ...)> callback, Params&&... params) {
311 runner->PostTask(FROM_HERE,
312 base::BindOnce(std::move(callback),
313 std::forward<Params>(params)...));
sadrul0526dc5e2017-03-28 20:04:29314 },
tzik7ea6636332017-08-17 10:19:57315 base::RetainedRef(std::move(runner)), std::move(callback));
sadrul0526dc5e2017-03-28 20:04:29316}
317
sadrul6aafa7d2017-03-22 22:56:35318} // namespace
319
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38320GpuServiceImpl::GpuServiceImpl(
321 const gpu::GPUInfo& gpu_info,
322 std::unique_ptr<gpu::GpuWatchdogThread> watchdog_thread,
323 scoped_refptr<base::SingleThreadTaskRunner> io_runner,
Zhenyao Mo83b895e2017-10-18 18:50:54324 const gpu::GpuFeatureInfo& gpu_feature_info,
Zhenyao Mo68ef56d2018-04-21 01:05:10325 const gpu::GpuPreferences& gpu_preferences,
Zhenyao Mob79101d2018-04-24 21:12:07326 const base::Optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
327 const base::Optional<gpu::GpuFeatureInfo>&
kylechar56ddcdc22018-05-28 16:45:38328 gpu_feature_info_for_hardware_gpu,
Alexander Dunaeve6e09c12020-10-23 16:43:42329 const gfx::GpuExtraInfo& gpu_extra_info,
Peng Huangcfc3180d2018-07-16 21:47:08330 gpu::VulkanImplementation* vulkan_implementation,
Patrick Todf5911c2020-05-12 19:31:42331 base::OnceCallback<void(base::Optional<ExitCode>)> exit_callback)
sadrul0526dc5e2017-03-28 20:04:29332 : main_runner_(base::ThreadTaskRunnerHandle::Get()),
333 io_runner_(std::move(io_runner)),
ericrk32c08ce2016-10-14 15:44:12334 watchdog_thread_(std::move(watchdog_thread)),
Zhenyao Mo83b895e2017-10-18 18:50:54335 gpu_preferences_(gpu_preferences),
sadrul72aae8a2017-01-24 04:52:32336 gpu_info_(gpu_info),
ericrk41a1579e2017-02-10 20:56:28337 gpu_feature_info_(gpu_feature_info),
Zhenyao Mo68ef56d2018-04-21 01:05:10338 gpu_info_for_hardware_gpu_(gpu_info_for_hardware_gpu),
339 gpu_feature_info_for_hardware_gpu_(gpu_feature_info_for_hardware_gpu),
Jonah Ryan-Davisa70577c2019-06-26 18:54:31340 gpu_extra_info_(gpu_extra_info),
Peng Huangcfc3180d2018-07-16 21:47:08341#if BUILDFLAG(ENABLE_VULKAN)
342 vulkan_implementation_(vulkan_implementation),
343#endif
kylechar14fe0ac2019-10-18 18:38:35344 exit_callback_(std::move(exit_callback)) {
sadrul9594ef32017-04-11 22:17:12345 DCHECK(!io_runner_->BelongsToCurrentThread());
Wezd476f83e2018-09-10 21:05:50346 DCHECK(exit_callback_);
347
Yuta Hijikata5a9692ce2020-11-20 07:26:16348#if BUILDFLAG(IS_CHROMEOS_ASH)
Hirokazu Hondac6dac9f2018-03-24 02:07:06349 protected_buffer_manager_ = new arc::ProtectedBufferManager();
Yuta Hijikata5a9692ce2020-11-20 07:26:16350#endif // BUILDFLAG(IS_CHROMEOS_ASH)
Peng Huangcfc3180d2018-07-16 21:47:08351
Khushalcbce6f72020-12-12 03:48:30352 GrContextOptions context_options = GetDefaultGrContextOptions(
353 gpu_preferences_.gr_context_type, gpu_preferences_);
Sergey Ulanov689c31f2020-01-24 23:21:16354 if (gpu_preferences_.force_max_texture_size) {
355 context_options.fMaxTextureSizeOverride =
356 gpu_preferences_.force_max_texture_size;
357 }
Sergey Ulanovd0066bbd2020-01-24 01:13:40358
Peng Huangcfc3180d2018-07-16 21:47:08359#if BUILDFLAG(ENABLE_VULKAN)
360 if (vulkan_implementation_) {
Peng Huangea21d6f2020-05-14 18:22:22361 bool is_native_vulkan =
362 gpu_preferences_.use_vulkan == gpu::VulkanImplementationName::kNative ||
363 gpu_preferences_.use_vulkan ==
364 gpu::VulkanImplementationName::kForcedNative;
365 // With swiftshader the vendor_id is 0xffff. For some tests gpu_info is not
366 // initialized, so the vendor_id is 0.
367 bool is_native_gl =
368 gpu_info_.gpu.vendor_id != 0xffff && gpu_info_.gpu.vendor_id != 0;
369 // If GL is using a real GPU, the gpu_info will be passed in and vulkan will
370 // use the same GPU.
Sergey Ulanovd0066bbd2020-01-24 01:13:40371 vulkan_context_provider_ = VulkanInProcessContextProvider::Create(
Vasiliy Telezhnikov6aee9292020-11-24 22:46:55372 vulkan_implementation_,
Peng Huangea21d6f2020-05-14 18:22:22373 (is_native_vulkan && is_native_gl) ? &gpu_info : nullptr);
Jonathan Backer7f3b64a2019-05-16 21:41:39374 if (vulkan_context_provider_) {
375 // If Vulkan is supported, then OOP-R is supported.
376 gpu_info_.oop_rasterization_supported = true;
377 gpu_feature_info_.status_values[gpu::GPU_FEATURE_TYPE_OOP_RASTERIZATION] =
378 gpu::kGpuFeatureStatusEnabled;
379 } else {
Peng Huang576415de2020-03-02 20:04:32380 DLOG(ERROR) << "Failed to create Vulkan context provider.";
Jonathan Backer7f3b64a2019-05-16 21:41:39381 }
Peng Huangcfc3180d2018-07-16 21:47:08382 }
383#endif
384
Sean Gilhulye5342322019-11-08 16:40:53385#if BUILDFLAG(SKIA_USE_DAWN)
386 if (gpu_preferences_.gr_context_type == gpu::GrContextType::kDawn) {
Stephen White349114a2020-01-27 21:40:24387 dawn_context_provider_ = DawnContextProvider::Create();
Sean Gilhulye5342322019-11-08 16:40:53388 if (dawn_context_provider_) {
389 gpu_info_.oop_rasterization_supported = true;
390 gpu_feature_info_.status_values[gpu::GPU_FEATURE_TYPE_OOP_RASTERIZATION] =
391 gpu::kGpuFeatureStatusEnabled;
392 } else {
Peng Huang576415de2020-03-02 20:04:32393 DLOG(ERROR) << "Failed to create Dawn context provider.";
Sean Gilhulye5342322019-11-08 16:40:53394 }
395 }
396#endif
397
Ted Meyerc80c1362020-10-13 00:43:42398#if BUILDFLAG(USE_VAAPI_IMAGE_CODECS)
Gil Dekel8f4b5642019-07-27 05:27:40399 image_decode_accelerator_worker_ =
400 media::VaapiImageDecodeAcceleratorWorker::Create();
Ted Meyerc80c1362020-10-13 00:43:42401#endif // BUILDFLAG(USE_VAAPI_IMAGE_CODECS)
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:34402
Avi Drissman8fd92172020-07-28 15:49:45403#if defined(OS_APPLE)
Christopher Cameron3a3601892019-05-16 21:43:52404 if (gpu_feature_info_.status_values[gpu::GPU_FEATURE_TYPE_METAL] ==
405 gpu::kGpuFeatureStatusEnabled) {
Sergey Ulanovd0066bbd2020-01-24 01:13:40406 metal_context_provider_ = MetalContextProvider::Create(context_options);
Christopher Cameron3a3601892019-05-16 21:43:52407 }
Christopher Cameron4f7ce362019-04-26 02:21:37408#endif
409
Maggie Chen05364bf32020-06-03 08:34:06410#if defined(OS_WIN)
411 auto info_callback = base::BindRepeating(
412 &GpuServiceImpl::UpdateOverlayAndHDRInfo, weak_ptr_factory_.GetWeakPtr());
413 gl::DirectCompositionSurfaceWin::SetOverlayHDRGpuInfoUpdateCallback(
414 info_callback);
415#endif
416
Sergey Ulanov3a8f8d2d2019-04-22 18:33:11417 gpu_memory_buffer_factory_ =
418 gpu::GpuMemoryBufferFactory::CreateNativeType(vulkan_context_provider());
419
sadrul0526dc5e2017-03-28 20:04:29420 weak_ptr_ = weak_ptr_factory_.GetWeakPtr();
421}
penghuang591151b2016-06-04 13:45:32422
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38423GpuServiceImpl::~GpuServiceImpl() {
sadrul0526dc5e2017-03-28 20:04:29424 DCHECK(main_runner_->BelongsToCurrentThread());
kylechar32f8fbf2019-06-12 16:46:24425
426 // Ensure we don't try to exit when already in the process of exiting.
427 is_exiting_.Set();
428
sadrul74685882017-04-01 01:33:43429 bind_task_tracker_.TryCancelAll();
Dale Curtisef404832020-03-31 23:05:42430 GetLogMessageManager()->ShutdownLogging();
kylechar14fe0ac2019-10-18 18:38:35431
432 // Destroy the receiver on the IO thread.
kylechar56ddcdc22018-05-28 16:45:38433 base::WaitableEvent wait;
kylechar14fe0ac2019-10-18 18:38:35434 auto destroy_receiver_task = base::BindOnce(
435 [](mojo::Receiver<mojom::GpuService>* receiver,
436 base::WaitableEvent* wait) {
437 receiver->reset();
438 wait->Signal();
439 },
440 &receiver_, &wait);
441 if (io_runner_->PostTask(FROM_HERE, std::move(destroy_receiver_task)))
sadrul9594ef32017-04-11 22:17:12442 wait.Wait();
Peng Huangfb0cf372018-03-22 17:29:27443
Maggie Chen83090292019-08-30 05:15:48444 if (watchdog_thread_)
445 watchdog_thread_->OnGpuProcessTearDown();
446
xhwangb2402d92016-10-15 07:29:37447 media_gpu_channel_manager_.reset();
sadrul5b860bd2016-09-26 20:15:34448 gpu_channel_manager_.reset();
Sunny Sachanandanid3d647d82019-01-16 04:52:45449
450 // Scheduler must be destroyed before sync point manager is destroyed.
451 scheduler_.reset();
sadrul5b860bd2016-09-26 20:15:34452 owned_sync_point_manager_.reset();
Peng Huange3b8c1d2019-03-05 17:21:12453 owned_shared_image_manager_.reset();
sadrul08f982ad2016-09-14 03:04:53454
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:34455 // The image decode accelerator worker must outlive the GPU channel manager so
456 // that it doesn't get any decode requests during/after destruction.
457 DCHECK(!gpu_channel_manager_);
Gil Dekel8f4b5642019-07-27 05:27:40458 image_decode_accelerator_worker_.reset();
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:34459
sunnyps41a6a15a2017-03-31 00:38:54460 // Signal this event before destroying the child process. That way all
461 // background threads can cleanup. For example, in the renderer the
462 // RenderThread instances will be able to notice shutdown before the render
463 // process begins waiting for them to exit.
464 if (owned_shutdown_event_)
465 owned_shutdown_event_->Signal();
penghuang591151b2016-06-04 13:45:32466}
fsamuelb25f8542016-04-22 19:14:07467
Zhenyao Mo83b895e2017-10-18 18:50:54468void GpuServiceImpl::UpdateGPUInfo() {
sadrul0526dc5e2017-03-28 20:04:29469 DCHECK(main_runner_->BelongsToCurrentThread());
sadrulb92e8e932016-12-08 20:59:24470 DCHECK(!gpu_host_);
Zhenyao Mod4dba1052017-07-27 23:02:39471 gpu::GpuDriverBugWorkarounds gpu_workarounds(
472 gpu_feature_info_.enabled_gpu_driver_bug_workarounds);
Dale Curtisa3f8ba32020-04-27 23:01:09473
474 GetVideoCapabilities(gpu_preferences_, gpu_workarounds, &gpu_info_);
475
Wei Lee01d63ef2019-05-09 09:24:50476 gpu_info_.jpeg_decode_accelerator_supported =
477 IsAcceleratedJpegDecodeSupported();
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:34478
Gil Dekel8f4b5642019-07-27 05:27:40479 if (image_decode_accelerator_worker_) {
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:34480 gpu_info_.image_decode_accelerator_supported_profiles =
Gil Dekel8f4b5642019-07-27 05:27:40481 image_decode_accelerator_worker_->GetSupportedProfiles();
Andres Calderon Jaramillo12a1c6d2019-06-26 04:42:34482 }
483
sadrul041ca722017-03-24 22:35:35484 // Record initialization only after collecting the GPU info because that can
485 // take a significant amount of time.
486 gpu_info_.initialization_time = base::Time::Now() - start_time_;
487}
488
Jonah Ryan-Davisa54a3002020-12-08 17:34:50489void GpuServiceImpl::UpdateGPUInfoGL() {
490 DCHECK(main_runner_->BelongsToCurrentThread());
491 gpu::CollectGraphicsInfoGL(&gpu_info_);
492 gpu_host_->DidUpdateGPUInfo(gpu_info_);
493}
494
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38495void GpuServiceImpl::InitializeWithHost(
Shimi Zhang88ae9222019-07-19 16:54:16496 mojo::PendingRemote<mojom::GpuHost> pending_gpu_host,
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38497 gpu::GpuProcessActivityFlags activity_flags,
Eric Karl304eff12018-07-27 21:13:35498 scoped_refptr<gl::GLSurface> default_offscreen_surface,
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38499 gpu::SyncPointManager* sync_point_manager,
Peng Huange3b8c1d2019-03-05 17:21:12500 gpu::SharedImageManager* shared_image_manager,
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38501 base::WaitableEvent* shutdown_event) {
sadrul0526dc5e2017-03-28 20:04:29502 DCHECK(main_runner_->BelongsToCurrentThread());
Shimi Zhang88ae9222019-07-19 16:54:16503
504 mojo::Remote<mojom::GpuHost> gpu_host(std::move(pending_gpu_host));
Zhenyao Mo68ef56d2018-04-21 01:05:10505 gpu_host->DidInitialize(gpu_info_, gpu_feature_info_,
506 gpu_info_for_hardware_gpu_,
Jonah Ryan-Davisa70577c2019-06-26 18:54:31507 gpu_feature_info_for_hardware_gpu_, gpu_extra_info_);
Shimi Zhang88ae9222019-07-19 16:54:16508 gpu_host_ = mojo::SharedRemote<mojom::GpuHost>(gpu_host.Unbind(), io_runner_);
Sadrul Habib Chowdhurydbe4da92017-10-12 15:53:15509 if (!in_host_process()) {
sadrul6aafa7d2017-03-22 22:56:35510 // The global callback is reset from the dtor. So Unretained() here is safe.
511 // Note that the callback can be called from any thread. Consequently, the
512 // callback cannot use a WeakPtr.
Dale Curtisef404832020-03-31 23:05:42513 GetLogMessageManager()->InstallPostInitializeLogHandler(base::BindRepeating(
514 &GpuServiceImpl::RecordLogMessage, base::Unretained(this)));
sadrul6aafa7d2017-03-22 22:56:35515 }
516
Peng Huangd5b1caf2019-03-05 20:51:18517 if (!sync_point_manager) {
Jinho Bang24ca9cd2018-01-02 22:01:00518 owned_sync_point_manager_ = std::make_unique<gpu::SyncPointManager>();
Peng Huangd5b1caf2019-03-05 20:51:18519 sync_point_manager = owned_sync_point_manager_.get();
sadrul72aae8a2017-01-24 04:52:32520 }
sadrulb92e8e932016-12-08 20:59:24521
Peng Huange3b8c1d2019-03-05 17:21:12522 if (!shared_image_manager) {
Saman Sami1cf52172020-01-13 19:05:27523 // When using real buffers for testing overlay configurations, we need
524 // access to SharedImageManager on the viz thread to obtain the buffer
525 // corresponding to a mailbox.
526 bool thread_safe_manager = features::ShouldUseRealBuffersForPageFlipTest();
527 owned_shared_image_manager_ = std::make_unique<gpu::SharedImageManager>(
528 thread_safe_manager, false /* display_context_on_another_thread */);
Peng Huange3b8c1d2019-03-05 17:21:12529 shared_image_manager = owned_shared_image_manager_.get();
Saman Sami1cf52172020-01-13 19:05:27530 } else {
531 // With this feature enabled, we don't expect to receive an external
532 // SharedImageManager.
533 DCHECK(!features::ShouldUseRealBuffersForPageFlipTest());
Peng Huange3b8c1d2019-03-05 17:21:12534 }
535
sunnyps41a6a15a2017-03-31 00:38:54536 shutdown_event_ = shutdown_event;
537 if (!shutdown_event_) {
Jinho Bang24ca9cd2018-01-02 22:01:00538 owned_shutdown_event_ = std::make_unique<base::WaitableEvent>(
sunnyps41a6a15a2017-03-31 00:38:54539 base::WaitableEvent::ResetPolicy::MANUAL,
540 base::WaitableEvent::InitialState::NOT_SIGNALED);
541 shutdown_event_ = owned_shutdown_event_.get();
542 }
543
Vasiliy Telezhnikov533c5642019-12-03 15:10:16544 scheduler_ = std::make_unique<gpu::Scheduler>(
Kramer Ge65a4de92020-02-24 20:46:49545 main_runner_, sync_point_manager, gpu_preferences_);
sunnyps8f9139e2017-05-12 17:53:25546
sadrulb92e8e932016-12-08 20:59:24547 // Defer creation of the render thread. This is to prevent it from handling
548 // IPC messages before the sandbox has been enabled and all other necessary
549 // initialization has succeeded.
Peng Huang42c8a1d2018-10-30 18:28:14550 gpu_channel_manager_ = std::make_unique<gpu::GpuChannelManager>(
Zhenyao Mod4dba1052017-07-27 23:02:39551 gpu_preferences_, this, watchdog_thread_.get(), main_runner_, io_runner_,
Kramer Ge65a4de92020-02-24 20:46:49552 scheduler_.get(), sync_point_manager, shared_image_manager,
Peng Huange3b8c1d2019-03-05 17:21:12553 gpu_memory_buffer_factory_.get(), gpu_feature_info_,
Kramer Ge65a4de92020-02-24 20:46:49554 std::move(activity_flags), std::move(default_offscreen_surface),
Gil Dekel8f4b5642019-07-27 05:27:40555 image_decode_accelerator_worker_.get(), vulkan_context_provider(),
Sean Gilhulye5342322019-11-08 16:40:53556 metal_context_provider_.get(), dawn_context_provider());
sadrulb92e8e932016-12-08 20:59:24557
Kramer Ge65a4de92020-02-24 20:46:49558 media_gpu_channel_manager_.reset(
559 new media::MediaGpuChannelManager(gpu_channel_manager_.get()));
560 if (watchdog_thread())
561 watchdog_thread()->AddPowerObserver();
sadrulb92e8e932016-12-08 20:59:24562}
563
kylechar14fe0ac2019-10-18 18:38:35564void GpuServiceImpl::Bind(
565 mojo::PendingReceiver<mojom::GpuService> pending_receiver) {
sadrul0526dc5e2017-03-28 20:04:29566 if (main_runner_->BelongsToCurrentThread()) {
sadrul74685882017-04-01 01:33:43567 bind_task_tracker_.PostTask(
568 io_runner_.get(), FROM_HERE,
Claudio DeSouza591a9972018-02-21 17:27:16569 base::BindOnce(&GpuServiceImpl::Bind, base::Unretained(this),
kylechar14fe0ac2019-10-18 18:38:35570 std::move(pending_receiver)));
sadrul0526dc5e2017-03-28 20:04:29571 return;
572 }
kylechar14fe0ac2019-10-18 18:38:35573 DCHECK(!receiver_.is_bound());
574 receiver_.Bind(std::move(pending_receiver));
sadruld1cca1e2016-09-13 19:51:35575}
576
kylecharf0a99352018-05-31 21:39:32577void GpuServiceImpl::DisableGpuCompositing() {
578 // Can be called from any thread.
Shimi Zhang88ae9222019-07-19 16:54:16579 gpu_host_->DisableGpuCompositing();
kylecharf0a99352018-05-31 21:39:32580}
581
Chris Blume40d79e92019-03-28 19:26:09582scoped_refptr<gpu::SharedContextState> GpuServiceImpl::GetContextState() {
Peng Huangfb0cf372018-03-22 17:29:27583 DCHECK(main_runner_->BelongsToCurrentThread());
Peng Huang42c8a1d2018-10-30 18:28:14584 gpu::ContextResult result;
Peng Huang20361dad12019-01-23 14:48:10585 return gpu_channel_manager_->GetSharedContextState(&result);
Peng Huangcfc3180d2018-07-16 21:47:08586}
587
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38588gpu::ImageFactory* GpuServiceImpl::gpu_image_factory() {
sadrul6d41b822017-04-02 03:38:50589 return gpu_memory_buffer_factory_
590 ? gpu_memory_buffer_factory_->AsImageFactory()
591 : nullptr;
592}
593
Dale Curtis1b6becebb2020-03-30 20:13:35594// static
595void GpuServiceImpl::InstallPreInitializeLogHandler() {
Dale Curtisef404832020-03-31 23:05:42596 GetLogMessageManager()->InstallPreInitializeLogHandler();
Dale Curtis1b6becebb2020-03-30 20:13:35597}
598
599// static
600void GpuServiceImpl::FlushPreInitializeLogMessages(mojom::GpuHost* gpu_host) {
Dale Curtisef404832020-03-31 23:05:42601 GetLogMessageManager()->FlushMessages(gpu_host);
Dale Curtis1b6becebb2020-03-30 20:13:35602}
603
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38604void GpuServiceImpl::RecordLogMessage(int severity,
Dale Curtis1b6becebb2020-03-30 20:13:35605 const std::string& header,
606 const std::string& message) {
sadrul0526dc5e2017-03-28 20:04:29607 // This can be run from any thread.
Dale Curtis1b6becebb2020-03-30 20:13:35608 gpu_host_->RecordLogMessage(severity, std::move(header), std::move(message));
sadrul6aafa7d2017-03-22 22:56:35609}
610
Yuta Hijikata5a9692ce2020-11-20 07:26:16611#if BUILDFLAG(IS_CHROMEOS_ASH)
Hirokazu Honda12eaf9c2017-12-12 02:40:16612void GpuServiceImpl::CreateArcVideoDecodeAccelerator(
Gyuyoung Kima1ddaae2019-09-12 07:30:25613 mojo::PendingReceiver<arc::mojom::VideoDecodeAccelerator> vda_receiver) {
Hirokazu Honda12eaf9c2017-12-12 02:40:16614 DCHECK(io_runner_->BelongsToCurrentThread());
615 main_runner_->PostTask(
616 FROM_HERE,
617 base::BindOnce(
618 &GpuServiceImpl::CreateArcVideoDecodeAcceleratorOnMainThread,
Gyuyoung Kima1ddaae2019-09-12 07:30:25619 weak_ptr_, std::move(vda_receiver)));
Hirokazu Honda12eaf9c2017-12-12 02:40:16620}
621
Peng Huang417f0cf72017-11-20 20:06:16622void GpuServiceImpl::CreateArcVideoEncodeAccelerator(
Gyuyoung Kima1ddaae2019-09-12 07:30:25623 mojo::PendingReceiver<arc::mojom::VideoEncodeAccelerator> vea_receiver) {
Peng Huang417f0cf72017-11-20 20:06:16624 DCHECK(io_runner_->BelongsToCurrentThread());
625 main_runner_->PostTask(
626 FROM_HERE,
627 base::BindOnce(
628 &GpuServiceImpl::CreateArcVideoEncodeAcceleratorOnMainThread,
Gyuyoung Kima1ddaae2019-09-12 07:30:25629 weak_ptr_, std::move(vea_receiver)));
Hirokazu Honda05268502018-03-31 05:18:39630}
631
632void GpuServiceImpl::CreateArcVideoProtectedBufferAllocator(
Gyuyoung Kima1ddaae2019-09-12 07:30:25633 mojo::PendingReceiver<arc::mojom::VideoProtectedBufferAllocator>
634 pba_receiver) {
Hirokazu Honda05268502018-03-31 05:18:39635 DCHECK(io_runner_->BelongsToCurrentThread());
636 main_runner_->PostTask(
637 FROM_HERE,
638 base::BindOnce(
639 &GpuServiceImpl::CreateArcVideoProtectedBufferAllocatorOnMainThread,
Gyuyoung Kima1ddaae2019-09-12 07:30:25640 weak_ptr_, std::move(pba_receiver)));
Peng Huang417f0cf72017-11-20 20:06:16641}
642
643void GpuServiceImpl::CreateArcProtectedBufferManager(
Gyuyoung Kima1ddaae2019-09-12 07:30:25644 mojo::PendingReceiver<arc::mojom::ProtectedBufferManager> pbm_receiver) {
Peng Huang417f0cf72017-11-20 20:06:16645 DCHECK(io_runner_->BelongsToCurrentThread());
646 main_runner_->PostTask(
647 FROM_HERE,
648 base::BindOnce(
649 &GpuServiceImpl::CreateArcProtectedBufferManagerOnMainThread,
Gyuyoung Kima1ddaae2019-09-12 07:30:25650 weak_ptr_, std::move(pbm_receiver)));
Peng Huang417f0cf72017-11-20 20:06:16651}
652
Hirokazu Honda12eaf9c2017-12-12 02:40:16653void GpuServiceImpl::CreateArcVideoDecodeAcceleratorOnMainThread(
Gyuyoung Kima1ddaae2019-09-12 07:30:25654 mojo::PendingReceiver<arc::mojom::VideoDecodeAccelerator> vda_receiver) {
Hirokazu Honda12eaf9c2017-12-12 02:40:16655 DCHECK(main_runner_->BelongsToCurrentThread());
Gyuyoung Kima1ddaae2019-09-12 07:30:25656 mojo::MakeSelfOwnedReceiver(
657 std::make_unique<arc::GpuArcVideoDecodeAccelerator>(
Chunbo Huad90bffb22020-09-28 15:34:36658 gpu_preferences_, gpu_channel_manager_->gpu_driver_bug_workarounds(),
659 protected_buffer_manager_),
Gyuyoung Kima1ddaae2019-09-12 07:30:25660 std::move(vda_receiver));
Hirokazu Honda12eaf9c2017-12-12 02:40:16661}
662
Peng Huang417f0cf72017-11-20 20:06:16663void GpuServiceImpl::CreateArcVideoEncodeAcceleratorOnMainThread(
Gyuyoung Kima1ddaae2019-09-12 07:30:25664 mojo::PendingReceiver<arc::mojom::VideoEncodeAccelerator> vea_receiver) {
Peng Huang417f0cf72017-11-20 20:06:16665 DCHECK(main_runner_->BelongsToCurrentThread());
Gyuyoung Kima1ddaae2019-09-12 07:30:25666 mojo::MakeSelfOwnedReceiver(
Chunbo Huad90bffb22020-09-28 15:34:36667 std::make_unique<arc::GpuArcVideoEncodeAccelerator>(
668 gpu_preferences_, gpu_channel_manager_->gpu_driver_bug_workarounds()),
Gyuyoung Kima1ddaae2019-09-12 07:30:25669 std::move(vea_receiver));
Peng Huang417f0cf72017-11-20 20:06:16670}
671
Hirokazu Honda05268502018-03-31 05:18:39672void GpuServiceImpl::CreateArcVideoProtectedBufferAllocatorOnMainThread(
Gyuyoung Kima1ddaae2019-09-12 07:30:25673 mojo::PendingReceiver<arc::mojom::VideoProtectedBufferAllocator>
674 pba_receiver) {
Hirokazu Honda05268502018-03-31 05:18:39675 DCHECK(main_runner_->BelongsToCurrentThread());
676 auto gpu_arc_video_protected_buffer_allocator =
677 arc::GpuArcVideoProtectedBufferAllocator::Create(
678 protected_buffer_manager_);
679 if (!gpu_arc_video_protected_buffer_allocator)
680 return;
Gyuyoung Kima1ddaae2019-09-12 07:30:25681 mojo::MakeSelfOwnedReceiver(
682 std::move(gpu_arc_video_protected_buffer_allocator),
683 std::move(pba_receiver));
Hirokazu Honda05268502018-03-31 05:18:39684}
685
Peng Huang417f0cf72017-11-20 20:06:16686void GpuServiceImpl::CreateArcProtectedBufferManagerOnMainThread(
Gyuyoung Kima1ddaae2019-09-12 07:30:25687 mojo::PendingReceiver<arc::mojom::ProtectedBufferManager> pbm_receiver) {
Peng Huang417f0cf72017-11-20 20:06:16688 DCHECK(main_runner_->BelongsToCurrentThread());
Gyuyoung Kima1ddaae2019-09-12 07:30:25689 mojo::MakeSelfOwnedReceiver(
Peng Huang417f0cf72017-11-20 20:06:16690 std::make_unique<arc::GpuArcProtectedBufferManagerProxy>(
Hirokazu Hondac6dac9f2018-03-24 02:07:06691 protected_buffer_manager_),
Gyuyoung Kima1ddaae2019-09-12 07:30:25692 std::move(pbm_receiver));
Peng Huang417f0cf72017-11-20 20:06:16693}
Wei Lee377f3a62019-04-17 04:12:11694
Wei Lee0cdabc92019-04-23 04:06:19695void GpuServiceImpl::CreateJpegDecodeAccelerator(
Gyuyoung Kima1ddaae2019-09-12 07:30:25696 mojo::PendingReceiver<chromeos_camera::mojom::MjpegDecodeAccelerator>
697 jda_receiver) {
Wei Lee0cdabc92019-04-23 04:06:19698 DCHECK(io_runner_->BelongsToCurrentThread());
Wei Lee01d63ef2019-05-09 09:24:50699 chromeos_camera::MojoMjpegDecodeAcceleratorService::Create(
Gyuyoung Kima1ddaae2019-09-12 07:30:25700 std::move(jda_receiver));
Wei Lee0cdabc92019-04-23 04:06:19701}
702
Wei Lee377f3a62019-04-17 04:12:11703void GpuServiceImpl::CreateJpegEncodeAccelerator(
Gyuyoung Kima1ddaae2019-09-12 07:30:25704 mojo::PendingReceiver<chromeos_camera::mojom::JpegEncodeAccelerator>
705 jea_receiver) {
Wei Lee377f3a62019-04-17 04:12:11706 DCHECK(io_runner_->BelongsToCurrentThread());
Wei Lee01d63ef2019-05-09 09:24:50707 chromeos_camera::MojoJpegEncodeAcceleratorService::Create(
Gyuyoung Kima1ddaae2019-09-12 07:30:25708 std::move(jea_receiver));
Wei Lee377f3a62019-04-17 04:12:11709}
Yuta Hijikata5a9692ce2020-11-20 07:26:16710#endif // BUILDFLAG(IS_CHROMEOS_ASH)
Peng Huang417f0cf72017-11-20 20:06:16711
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38712void GpuServiceImpl::CreateVideoEncodeAcceleratorProvider(
Gyuyoung Kima1ddaae2019-09-12 07:30:25713 mojo::PendingReceiver<media::mojom::VideoEncodeAcceleratorProvider>
714 vea_provider_receiver) {
Miguel Casas-Sanchezdaca3252017-07-11 08:05:45715 DCHECK(io_runner_->BelongsToCurrentThread());
Miguel Casas-Sanchez7d464702017-07-29 14:19:21716 media::MojoVideoEncodeAcceleratorProvider::Create(
Gyuyoung Kima1ddaae2019-09-12 07:30:25717 std::move(vea_provider_receiver),
kylechard50f57ee2019-01-08 20:23:18718 base::BindRepeating(&media::GpuVideoEncodeAcceleratorFactory::CreateVEA),
Chunbo Huad90bffb22020-09-28 15:34:36719 gpu_preferences_, gpu_channel_manager_->gpu_driver_bug_workarounds());
Miguel Casas-Sanchezdaca3252017-07-11 08:05:45720}
721
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38722void GpuServiceImpl::CreateGpuMemoryBuffer(
penghuang591151b2016-06-04 13:45:32723 gfx::GpuMemoryBufferId id,
sammc813a8eb32016-05-28 03:34:07724 const gfx::Size& size,
penghuang591151b2016-06-04 13:45:32725 gfx::BufferFormat format,
726 gfx::BufferUsage usage,
727 int client_id,
sadrul763bd592016-09-27 14:33:44728 gpu::SurfaceHandle surface_handle,
tzik7ea6636332017-08-17 10:19:57729 CreateGpuMemoryBufferCallback callback) {
sadrul0526dc5e2017-03-28 20:04:29730 DCHECK(io_runner_->BelongsToCurrentThread());
731 // This needs to happen in the IO thread.
Saman Sami640ac4342019-07-11 22:52:48732 gpu_memory_buffer_factory_->CreateGpuMemoryBufferAsync(
733 id, size, format, usage, client_id, surface_handle, std::move(callback));
penghuang6cddff42016-05-13 15:37:46734}
735
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38736void GpuServiceImpl::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
737 int client_id,
738 const gpu::SyncToken& sync_token) {
sadrul0526dc5e2017-03-28 20:04:29739 if (io_runner_->BelongsToCurrentThread()) {
tzik2bcf8e42018-07-31 11:22:15740 main_runner_->PostTask(
741 FROM_HERE, base::BindOnce(&GpuServiceImpl::DestroyGpuMemoryBuffer,
742 weak_ptr_, id, client_id, sync_token));
sadrul0526dc5e2017-03-28 20:04:29743 return;
744 }
sadrul6a900aa2017-04-07 23:30:37745 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token);
penghuang6cddff42016-05-13 15:37:46746}
747
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38748void GpuServiceImpl::GetVideoMemoryUsageStats(
tzik7ea6636332017-08-17 10:19:57749 GetVideoMemoryUsageStatsCallback callback) {
sadrul0526dc5e2017-03-28 20:04:29750 if (io_runner_->BelongsToCurrentThread()) {
tzik7ea6636332017-08-17 10:19:57751 auto wrap_callback = WrapCallback(io_runner_, std::move(callback));
752 main_runner_->PostTask(
753 FROM_HERE, base::BindOnce(&GpuServiceImpl::GetVideoMemoryUsageStats,
754 weak_ptr_, std::move(wrap_callback)));
sadrul0526dc5e2017-03-28 20:04:29755 return;
756 }
sadrul3ab0a5332017-03-01 03:16:45757 gpu::VideoMemoryUsageStats video_memory_usage_stats;
Antoine Labour3b79a442018-07-06 20:19:34758 gpu_channel_manager_->GetVideoMemoryUsageStats(&video_memory_usage_stats);
tzik7ea6636332017-08-17 10:19:57759 std::move(callback).Run(video_memory_usage_stats);
sadrul3ab0a5332017-03-01 03:16:45760}
761
jonrossd2ca5e642019-10-16 12:08:52762void GpuServiceImpl::StartPeakMemoryMonitor(uint32_t sequence_num) {
763 DCHECK(io_runner_->BelongsToCurrentThread());
764 main_runner_->PostTask(
765 FROM_HERE,
766 base::BindOnce(&GpuServiceImpl::StartPeakMemoryMonitorOnMainThread,
767 weak_ptr_, sequence_num));
768}
769
770void GpuServiceImpl::GetPeakMemoryUsage(uint32_t sequence_num,
771 GetPeakMemoryUsageCallback callback) {
772 DCHECK(io_runner_->BelongsToCurrentThread());
773 main_runner_->PostTask(
774 FROM_HERE, base::BindOnce(&GpuServiceImpl::GetPeakMemoryUsageOnMainThread,
775 weak_ptr_, sequence_num, std::move(callback)));
776}
777
Christopher Cameron51e1d9e2017-10-14 00:01:45778void GpuServiceImpl::RequestHDRStatus(RequestHDRStatusCallback callback) {
779 DCHECK(io_runner_->BelongsToCurrentThread());
780 main_runner_->PostTask(
781 FROM_HERE, base::BindOnce(&GpuServiceImpl::RequestHDRStatusOnMainThread,
782 weak_ptr_, std::move(callback)));
783}
784
785void GpuServiceImpl::RequestHDRStatusOnMainThread(
786 RequestHDRStatusCallback callback) {
787 DCHECK(main_runner_->BelongsToCurrentThread());
Maggie Chen05364bf32020-06-03 08:34:06788
Christopher Cameron51e1d9e2017-10-14 00:01:45789#if defined(OS_WIN)
Maggie Chen05364bf32020-06-03 08:34:06790 hdr_enabled_ = gl::DirectCompositionSurfaceWin::IsHDRSupported();
Christopher Cameron51e1d9e2017-10-14 00:01:45791#endif
792 io_runner_->PostTask(FROM_HERE,
Maggie Chen05364bf32020-06-03 08:34:06793 base::BindOnce(std::move(callback), hdr_enabled_));
Christopher Cameron51e1d9e2017-10-14 00:01:45794}
795
kylecharf9148cfb2019-07-22 23:12:07796void GpuServiceImpl::RegisterDisplayContext(
797 gpu::DisplayContext* display_context) {
798 DCHECK(main_runner_->BelongsToCurrentThread());
799 display_contexts_.AddObserver(display_context);
800}
801
802void GpuServiceImpl::UnregisterDisplayContext(
803 gpu::DisplayContext* display_context) {
804 DCHECK(main_runner_->BelongsToCurrentThread());
805 display_contexts_.RemoveObserver(display_context);
806}
807
808void GpuServiceImpl::LoseAllContexts() {
809 DCHECK(main_runner_->BelongsToCurrentThread());
810
811 if (IsExiting())
812 return;
813
814 for (auto& display_context : display_contexts_)
815 display_context.MarkContextLost();
816 gpu_channel_manager_->LoseAllContexts();
817}
818
Bo Liud4194192017-11-28 22:30:14819void GpuServiceImpl::DidCreateContextSuccessfully() {
820 DCHECK(main_runner_->BelongsToCurrentThread());
Shimi Zhang88ae9222019-07-19 16:54:16821 gpu_host_->DidCreateContextSuccessfully();
Bo Liud4194192017-11-28 22:30:14822}
823
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38824void GpuServiceImpl::DidCreateOffscreenContext(const GURL& active_url) {
sadrul0526dc5e2017-03-28 20:04:29825 DCHECK(main_runner_->BelongsToCurrentThread());
Shimi Zhang88ae9222019-07-19 16:54:16826 gpu_host_->DidCreateOffscreenContext(active_url);
fsamuelb25f8542016-04-22 19:14:07827}
828
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38829void GpuServiceImpl::DidDestroyChannel(int client_id) {
sadrul0526dc5e2017-03-28 20:04:29830 DCHECK(main_runner_->BelongsToCurrentThread());
xhwangb2402d92016-10-15 07:29:37831 media_gpu_channel_manager_->RemoveChannel(client_id);
Shimi Zhang88ae9222019-07-19 16:54:16832 gpu_host_->DidDestroyChannel(client_id);
fsamuelb25f8542016-04-22 19:14:07833}
834
Bo Liu95570f32020-03-16 23:42:17835void GpuServiceImpl::DidDestroyAllChannels() {
836 DCHECK(main_runner_->BelongsToCurrentThread());
837 gpu_host_->DidDestroyAllChannels();
838}
839
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38840void GpuServiceImpl::DidDestroyOffscreenContext(const GURL& active_url) {
sadrul0526dc5e2017-03-28 20:04:29841 DCHECK(main_runner_->BelongsToCurrentThread());
Shimi Zhang88ae9222019-07-19 16:54:16842 gpu_host_->DidDestroyOffscreenContext(active_url);
fsamuelb25f8542016-04-22 19:14:07843}
844
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38845void GpuServiceImpl::DidLoseContext(bool offscreen,
846 gpu::error::ContextLostReason reason,
847 const GURL& active_url) {
sadrul0526dc5e2017-03-28 20:04:29848 DCHECK(main_runner_->BelongsToCurrentThread());
Shimi Zhang88ae9222019-07-19 16:54:16849 gpu_host_->DidLoseContext(offscreen, reason, active_url);
fsamuelb25f8542016-04-22 19:14:07850}
851
Maggie Chen8f177172020-02-11 00:02:28852#if defined(OS_WIN)
853void GpuServiceImpl::DidUpdateOverlayInfo(
854 const gpu::OverlayInfo& overlay_info) {
855 gpu_host_->DidUpdateOverlayInfo(gpu_info_.overlay_info);
856}
Maggie Chen05364bf32020-06-03 08:34:06857
858void GpuServiceImpl::DidUpdateHDRStatus(bool hdr_enabled) {
859 gpu_host_->DidUpdateHDRStatus(hdr_enabled);
860}
Maggie Chen8f177172020-02-11 00:02:28861#endif
862
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38863void GpuServiceImpl::StoreShaderToDisk(int client_id,
864 const std::string& key,
865 const std::string& shader) {
sadrul0526dc5e2017-03-28 20:04:29866 DCHECK(main_runner_->BelongsToCurrentThread());
Shimi Zhang88ae9222019-07-19 16:54:16867 gpu_host_->StoreShaderToDisk(client_id, key, shader);
fsamuelb25f8542016-04-22 19:14:07868}
869
kylechar6c0ba1e2019-02-01 23:20:24870void GpuServiceImpl::MaybeExitOnContextLost() {
871 MaybeExit(true);
872}
873
874bool GpuServiceImpl::IsExiting() const {
875 return is_exiting_.IsSet();
kylechar56ddcdc22018-05-28 16:45:38876}
877
fsamuelb25f8542016-04-22 19:14:07878#if defined(OS_WIN)
kylecharcd157cc2018-05-30 17:30:02879void GpuServiceImpl::SendCreatedChildWindow(gpu::SurfaceHandle parent_window,
880 gpu::SurfaceHandle child_window) {
881 // This can be called from main or display compositor thread.
Shimi Zhang88ae9222019-07-19 16:54:16882 gpu_host_->SetChildSurface(parent_window, child_window);
fsamuelb25f8542016-04-22 19:14:07883}
884#endif
885
tzik7ea6636332017-08-17 10:19:57886void GpuServiceImpl::EstablishGpuChannel(int32_t client_id,
887 uint64_t client_tracing_id,
888 bool is_gpu_host,
Khushalc2667e32018-07-20 01:07:18889 bool cache_shaders_on_disk,
tzik7ea6636332017-08-17 10:19:57890 EstablishGpuChannelCallback callback) {
kylecharfa018c92019-10-18 16:09:44891 // This should always be called on the IO thread first.
sadrul0526dc5e2017-03-28 20:04:29892 if (io_runner_->BelongsToCurrentThread()) {
kylecharfa018c92019-10-18 16:09:44893 if (IsExiting()) {
894 // We are already exiting so there is no point in responding. Close the
895 // receiver so we can safely drop the callback.
kylechar14fe0ac2019-10-18 18:38:35896 receiver_.reset();
kylecharfa018c92019-10-18 16:09:44897 return;
898 }
899
900 if (gpu::IsReservedClientId(client_id)) {
901 // This returns a null handle, which is treated by the client as a failure
902 // case.
903 std::move(callback).Run(mojo::ScopedMessagePipeHandle());
904 return;
905 }
906
tzik7ea6636332017-08-17 10:19:57907 EstablishGpuChannelCallback wrap_callback = base::BindOnce(
sadrul0526dc5e2017-03-28 20:04:29908 [](scoped_refptr<base::SingleThreadTaskRunner> runner,
tzik7ea6636332017-08-17 10:19:57909 EstablishGpuChannelCallback cb,
sadrul0526dc5e2017-03-28 20:04:29910 mojo::ScopedMessagePipeHandle handle) {
sadrul0835f282017-03-29 19:41:55911 runner->PostTask(FROM_HERE,
tzik7ea6636332017-08-17 10:19:57912 base::BindOnce(std::move(cb), std::move(handle)));
sadrul0526dc5e2017-03-28 20:04:29913 },
tzik7ea6636332017-08-17 10:19:57914 io_runner_, std::move(callback));
sadrul0526dc5e2017-03-28 20:04:29915 main_runner_->PostTask(
Khushalc2667e32018-07-20 01:07:18916 FROM_HERE,
917 base::BindOnce(&GpuServiceImpl::EstablishGpuChannel, weak_ptr_,
918 client_id, client_tracing_id, is_gpu_host,
919 cache_shaders_on_disk, std::move(wrap_callback)));
sadrul0526dc5e2017-03-28 20:04:29920 return;
921 }
sadrul08f982ad2016-09-14 03:04:53922
sunnyps41a6a15a2017-03-31 00:38:54923 gpu::GpuChannel* gpu_channel = gpu_channel_manager_->EstablishChannel(
Khushalc2667e32018-07-20 01:07:18924 client_id, client_tracing_id, is_gpu_host, cache_shaders_on_disk);
sunnyps41a6a15a2017-03-31 00:38:54925
Vikas Soni76906de2019-03-28 03:18:10926 if (!gpu_channel) {
927 // This returns a null handle, which is treated by the client as a failure
928 // case.
929 std::move(callback).Run(mojo::ScopedMessagePipeHandle());
930 return;
931 }
sunnyps41a6a15a2017-03-31 00:38:54932 mojo::MessagePipe pipe;
Sunny Sachanandanib6b590a2018-09-28 05:20:43933 gpu_channel->Init(pipe.handle0.release(), shutdown_event_);
sunnyps41a6a15a2017-03-31 00:38:54934
xhwangb2402d92016-10-15 07:29:37935 media_gpu_channel_manager_->AddChannel(client_id);
sunnyps41a6a15a2017-03-31 00:38:54936
tzik7ea6636332017-08-17 10:19:57937 std::move(callback).Run(std::move(pipe.handle1));
sadrulad8b21be2016-08-26 00:45:09938}
939
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38940void GpuServiceImpl::CloseChannel(int32_t client_id) {
sadrul0526dc5e2017-03-28 20:04:29941 if (io_runner_->BelongsToCurrentThread()) {
tzik2bcf8e42018-07-31 11:22:15942 main_runner_->PostTask(
943 FROM_HERE,
944 base::BindOnce(&GpuServiceImpl::CloseChannel, weak_ptr_, client_id));
sadrul0526dc5e2017-03-28 20:04:29945 return;
946 }
sadrul9cef0df2017-03-03 18:18:07947 gpu_channel_manager_->RemoveChannel(client_id);
948}
949
Khushalc2667e32018-07-20 01:07:18950void GpuServiceImpl::LoadedShader(int32_t client_id,
951 const std::string& key,
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38952 const std::string& data) {
sadrul0526dc5e2017-03-28 20:04:29953 if (io_runner_->BelongsToCurrentThread()) {
tzik2bcf8e42018-07-31 11:22:15954 main_runner_->PostTask(
955 FROM_HERE, base::BindOnce(&GpuServiceImpl::LoadedShader, weak_ptr_,
956 client_id, key, data));
sadrul0526dc5e2017-03-28 20:04:29957 return;
958 }
Khushalc2667e32018-07-20 01:07:18959 gpu_channel_manager_->PopulateShaderCache(client_id, key, data);
sadrul9cef0df2017-03-03 18:18:07960}
961
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:38962void GpuServiceImpl::WakeUpGpu() {
sadrul0526dc5e2017-03-28 20:04:29963 if (io_runner_->BelongsToCurrentThread()) {
tzik2bcf8e42018-07-31 11:22:15964 main_runner_->PostTask(
965 FROM_HERE, base::BindOnce(&GpuServiceImpl::WakeUpGpu, weak_ptr_));
sadrul0526dc5e2017-03-28 20:04:29966 return;
967 }
sadrul9cef0df2017-03-03 18:18:07968#if defined(OS_ANDROID)
sadrul9cef0df2017-03-03 18:18:07969 gpu_channel_manager_->WakeUpGpu();
970#else
971 NOTREACHED() << "WakeUpGpu() not supported on this platform.";
972#endif
973}
974
Kenneth Russell35d7e732019-11-08 00:47:04975void GpuServiceImpl::GpuSwitched(gl::GpuPreference active_gpu_heuristic) {
sadruld93a2b102017-03-29 04:11:52976 DVLOG(1) << "GPU: GPU has switched";
Jonah Ryan-Davisa54a3002020-12-08 17:34:50977 if (!in_host_process()) {
Kenneth Russell35d7e732019-11-08 00:47:04978 ui::GpuSwitchingManager::GetInstance()->NotifyGpuSwitched(
979 active_gpu_heuristic);
Jonah Ryan-Davisa54a3002020-12-08 17:34:50980 }
981 if (io_runner_->BelongsToCurrentThread()) {
982 main_runner_->PostTask(
983 FROM_HERE, base::BindOnce(&GpuServiceImpl::UpdateGPUInfoGL, weak_ptr_));
984 return;
985 }
986 GpuServiceImpl::UpdateGPUInfoGL();
sadruld93a2b102017-03-29 04:11:52987}
988
Maggie Chenbde7f922020-01-24 17:57:01989void GpuServiceImpl::DisplayAdded() {
Maggie Chen87d01b42020-03-13 19:34:13990 if (io_runner_->BelongsToCurrentThread()) {
991 main_runner_->PostTask(
992 FROM_HERE, base::BindOnce(&GpuServiceImpl::DisplayAdded, weak_ptr_));
993 return;
994 }
Maggie Chenbde7f922020-01-24 17:57:01995 DVLOG(1) << "GPU: A monitor is plugged in";
996
997 if (!in_host_process())
998 ui::GpuSwitchingManager::GetInstance()->NotifyDisplayAdded();
999}
1000
1001void GpuServiceImpl::DisplayRemoved() {
Maggie Chen87d01b42020-03-13 19:34:131002 if (io_runner_->BelongsToCurrentThread()) {
1003 main_runner_->PostTask(
1004 FROM_HERE, base::BindOnce(&GpuServiceImpl::DisplayRemoved, weak_ptr_));
1005 return;
1006 }
Maggie Chenbde7f922020-01-24 17:57:011007 DVLOG(1) << "GPU: A monitor is unplugged ";
1008
1009 if (!in_host_process())
1010 ui::GpuSwitchingManager::GetInstance()->NotifyDisplayRemoved();
1011}
1012
Maggie Chen954dc162020-11-11 22:34:361013void GpuServiceImpl::DisplayMetricsChanged() {
1014 if (io_runner_->BelongsToCurrentThread()) {
1015 main_runner_->PostTask(
1016 FROM_HERE,
1017 base::BindOnce(&GpuServiceImpl::DisplayMetricsChanged, weak_ptr_));
1018 return;
1019 }
1020 DVLOG(1) << "GPU: Display Metrics changed";
1021
1022 if (!in_host_process())
1023 ui::GpuSwitchingManager::GetInstance()->NotifyDisplayMetricsChanged();
1024}
1025
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:381026void GpuServiceImpl::DestroyAllChannels() {
sadrul0526dc5e2017-03-28 20:04:291027 if (io_runner_->BelongsToCurrentThread()) {
1028 main_runner_->PostTask(
tzik2bcf8e42018-07-31 11:22:151029 FROM_HERE,
1030 base::BindOnce(&GpuServiceImpl::DestroyAllChannels, weak_ptr_));
sadrul0526dc5e2017-03-28 20:04:291031 return;
1032 }
sadrulb428f6b2017-03-03 19:28:321033 DVLOG(1) << "GPU: Removing all contexts";
1034 gpu_channel_manager_->DestroyAllChannels();
1035}
1036
Eric Karl033cd992018-05-16 15:32:331037void GpuServiceImpl::OnBackgroundCleanup() {
Eric Karl73cfd3f2018-03-26 22:49:451038// Currently only called on Android.
1039#if defined(OS_ANDROID)
1040 if (io_runner_->BelongsToCurrentThread()) {
1041 main_runner_->PostTask(
Eric Karl033cd992018-05-16 15:32:331042 FROM_HERE,
1043 base::BindOnce(&GpuServiceImpl::OnBackgroundCleanup, weak_ptr_));
Eric Karl73cfd3f2018-03-26 22:49:451044 return;
1045 }
1046 DVLOG(1) << "GPU: Performing background cleanup";
Khushalcbaac9b2018-07-10 06:00:451047 gpu_channel_manager_->OnBackgroundCleanup();
Kramer Ge65a4de92020-02-24 20:46:491048#else
Kramer Gea799e282020-02-19 21:58:531049 NOTREACHED();
Kramer Ge65a4de92020-02-24 20:46:491050#endif
Eric Karl73cfd3f2018-03-26 22:49:451051}
1052
Eric Karl033cd992018-05-16 15:32:331053void GpuServiceImpl::OnBackgrounded() {
Eric Karl614b5b82018-11-10 00:58:341054 DCHECK(io_runner_->BelongsToCurrentThread());
Eric Karl033cd992018-05-16 15:32:331055 if (watchdog_thread_)
1056 watchdog_thread_->OnBackgrounded();
Khushalcbaac9b2018-07-10 06:00:451057
Eric Karl614b5b82018-11-10 00:58:341058 main_runner_->PostTask(
1059 FROM_HERE,
1060 base::BindOnce(&GpuServiceImpl::OnBackgroundedOnMainThread, weak_ptr_));
1061}
1062
1063void GpuServiceImpl::OnBackgroundedOnMainThread() {
Khushalcbaac9b2018-07-10 06:00:451064 gpu_channel_manager_->OnApplicationBackgrounded();
Eric Karl033cd992018-05-16 15:32:331065}
1066
1067void GpuServiceImpl::OnForegrounded() {
1068 if (watchdog_thread_)
1069 watchdog_thread_->OnForegrounded();
1070}
1071
Sebastien Marchand9ea1262c2019-12-09 23:37:431072#if !defined(OS_ANDROID)
1073void GpuServiceImpl::OnMemoryPressure(
1074 ::base::MemoryPressureListener::MemoryPressureLevel level) {
1075 // Forward the notification to the registry of MemoryPressureListeners.
1076 base::MemoryPressureListener::NotifyMemoryPressure(level);
1077}
1078#endif
1079
Avi Drissman8fd92172020-07-28 15:49:451080#if defined(OS_APPLE)
Sidney San Martín9387b402018-06-08 18:13:261081void GpuServiceImpl::BeginCATransaction() {
1082 DCHECK(io_runner_->BelongsToCurrentThread());
1083 main_runner_->PostTask(FROM_HERE, base::BindOnce(&ui::BeginCATransaction));
1084}
1085
1086void GpuServiceImpl::CommitCATransaction(CommitCATransactionCallback callback) {
1087 DCHECK(io_runner_->BelongsToCurrentThread());
1088 main_runner_->PostTaskAndReply(FROM_HERE,
1089 base::BindOnce(&ui::CommitCATransaction),
1090 WrapCallback(io_runner_, std::move(callback)));
1091}
1092#endif
1093
Sebastien Marchand836d04b32020-04-28 18:10:031094#if BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX)
1095void GpuServiceImpl::WriteClangProfilingProfile(
1096 WriteClangProfilingProfileCallback callback) {
1097 base::WriteClangProfilingProfile();
1098 std::move(callback).Run();
1099}
1100#endif
1101
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:381102void GpuServiceImpl::Crash() {
sadrul0526dc5e2017-03-28 20:04:291103 DCHECK(io_runner_->BelongsToCurrentThread());
Kenneth Russell3689f3e2018-08-07 00:45:401104 gl::Crash();
sadrulb428f6b2017-03-03 19:28:321105}
1106
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:381107void GpuServiceImpl::Hang() {
sadrul0526dc5e2017-03-28 20:04:291108 DCHECK(io_runner_->BelongsToCurrentThread());
Zhenyao Mo75bd50d2019-09-09 22:31:181109 main_runner_->PostTask(FROM_HERE, base::BindOnce(&gl::Hang));
sadrulb428f6b2017-03-03 19:28:321110}
1111
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:381112void GpuServiceImpl::ThrowJavaException() {
sadrul0526dc5e2017-03-28 20:04:291113 DCHECK(io_runner_->BelongsToCurrentThread());
sadrulb428f6b2017-03-03 19:28:321114#if defined(OS_ANDROID)
Andrew Grieve26407882018-08-28 15:15:331115 ThrowUncaughtException();
sadrulb428f6b2017-03-03 19:28:321116#else
1117 NOTREACHED() << "Java exception not supported on this platform.";
1118#endif
1119}
1120
tzik7ea6636332017-08-17 10:19:571121void GpuServiceImpl::Stop(StopCallback callback) {
sadrul0526dc5e2017-03-28 20:04:291122 DCHECK(io_runner_->BelongsToCurrentThread());
Gabriel Charette53a9ef812017-07-26 12:36:231123 main_runner_->PostTaskAndReply(
kylechar6c0ba1e2019-02-01 23:20:241124 FROM_HERE, base::BindOnce(&GpuServiceImpl::MaybeExit, weak_ptr_, false),
tzik7ea6636332017-08-17 10:19:571125 std::move(callback));
sadrule6f6e102017-03-11 01:09:561126}
1127
jonrossd2ca5e642019-10-16 12:08:521128void GpuServiceImpl::StartPeakMemoryMonitorOnMainThread(uint32_t sequence_num) {
1129 gpu_channel_manager_->StartPeakMemoryMonitor(sequence_num);
1130}
1131
1132void GpuServiceImpl::GetPeakMemoryUsageOnMainThread(
1133 uint32_t sequence_num,
1134 GetPeakMemoryUsageCallback callback) {
jonross1f2903d2020-03-06 13:59:401135 uint64_t peak_memory = 0u;
1136 auto allocation_per_source =
1137 gpu_channel_manager_->GetPeakMemoryUsage(sequence_num, &peak_memory);
jonrossd2ca5e642019-10-16 12:08:521138 io_runner_->PostTask(FROM_HERE,
jonross1f2903d2020-03-06 13:59:401139 base::BindOnce(std::move(callback), peak_memory,
1140 std::move(allocation_per_source)));
jonrossd2ca5e642019-10-16 12:08:521141}
1142
kylechar6c0ba1e2019-02-01 23:20:241143void GpuServiceImpl::MaybeExit(bool for_context_loss) {
1144 DCHECK(main_runner_->BelongsToCurrentThread());
1145
1146 // We can't restart the GPU process when running in the host process.
1147 if (in_host_process())
1148 return;
1149
kylechar32f8fbf2019-06-12 16:46:241150 if (IsExiting() || !exit_callback_)
1151 return;
1152
1153 if (for_context_loss) {
1154 LOG(ERROR) << "Exiting GPU process because some drivers can't recover "
1155 "from errors. GPU process will restart shortly.";
kylechar6c0ba1e2019-02-01 23:20:241156 }
kylechar32f8fbf2019-06-12 16:46:241157 is_exiting_.Set();
Maggie Chenba08c242020-02-05 21:35:401158 // For the unsandboxed GPU info collection process used for info collection,
1159 // if we exit immediately, then the reply message could be lost. That's why
1160 // the |exit_callback_| takes the boolean argument.
Patrick Todf5911c2020-05-12 19:31:421161 if (for_context_loss)
1162 std::move(exit_callback_)
1163 .Run(ExitCode::RESULT_CODE_GPU_EXIT_ON_CONTEXT_LOST);
1164 else
1165 std::move(exit_callback_).Run(base::nullopt);
kylechar6c0ba1e2019-02-01 23:20:241166}
1167
Vasiliy Telezhnikov1744bd02019-10-31 19:32:221168gpu::Scheduler* GpuServiceImpl::GetGpuScheduler() {
1169 return scheduler_.get();
1170}
1171
Maggie Chen8f177172020-02-11 00:02:281172#if defined(OS_WIN)
Maggie Chen05364bf32020-06-03 08:34:061173void GpuServiceImpl::UpdateOverlayAndHDRInfo() {
Maggie Chen8f177172020-02-11 00:02:281174 gpu::OverlayInfo old_overlay_info = gpu_info_.overlay_info;
1175 gpu::CollectHardwareOverlayInfo(&gpu_info_.overlay_info);
1176
Maggie Chen05364bf32020-06-03 08:34:061177 // Update overlay info in the GPU process and send the updated data back to
1178 // the GPU host in the Browser process through mojom if the info has changed.
Maggie Chen8f177172020-02-11 00:02:281179 if (old_overlay_info != gpu_info_.overlay_info)
1180 DidUpdateOverlayInfo(gpu_info_.overlay_info);
Maggie Chen05364bf32020-06-03 08:34:061181
1182 // Update HDR status in the GPU process through the GPU host mojom.
1183 bool old_hdr_enabled_status = hdr_enabled_;
1184 hdr_enabled_ = gl::DirectCompositionSurfaceWin::IsHDRSupported();
1185 if (old_hdr_enabled_status != hdr_enabled_)
1186 DidUpdateHDRStatus(hdr_enabled_);
Maggie Chen8f177172020-02-11 00:02:281187}
1188#endif
1189
Sadrul Habib Chowdhuryef1abe782017-08-01 17:20:381190} // namespace viz