1 // Copyright 2019 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 #include "components/viz/service/display_embedder/skia_output_surface_dependency_impl.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/callback_helpers.h"
11 #include "base/threading/thread_task_runner_handle.h"
12 #include "build/build_config.h"
13 #include "components/viz/service/gl/gpu_service_impl.h"
14 #include "gpu/command_buffer/service/scheduler.h"
15 #include "gpu/ipc/gpu_task_scheduler_helper.h"
16 #include "gpu/ipc/scheduler_sequence.h"
17 #include "gpu/ipc/service/image_transport_surface.h"
18 #include "ui/gl/init/gl_factory.h"
19 
20 namespace viz {
21 
SkiaOutputSurfaceDependencyImpl(GpuServiceImpl * gpu_service_impl,gpu::SurfaceHandle surface_handle)22 SkiaOutputSurfaceDependencyImpl::SkiaOutputSurfaceDependencyImpl(
23     GpuServiceImpl* gpu_service_impl,
24     gpu::SurfaceHandle surface_handle)
25     : gpu_service_impl_(gpu_service_impl),
26       surface_handle_(surface_handle),
27       client_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
28 
29 SkiaOutputSurfaceDependencyImpl::~SkiaOutputSurfaceDependencyImpl() = default;
30 
31 std::unique_ptr<gpu::SingleTaskSequence>
CreateSequence()32 SkiaOutputSurfaceDependencyImpl::CreateSequence() {
33   return std::make_unique<gpu::SchedulerSequence>(
34       gpu_service_impl_->GetGpuScheduler());
35 }
36 
37 gpu::SharedImageManager*
GetSharedImageManager()38 SkiaOutputSurfaceDependencyImpl::GetSharedImageManager() {
39   return gpu_service_impl_->shared_image_manager();
40 }
41 
GetSyncPointManager()42 gpu::SyncPointManager* SkiaOutputSurfaceDependencyImpl::GetSyncPointManager() {
43   return gpu_service_impl_->sync_point_manager();
44 }
45 
46 const gpu::GpuDriverBugWorkarounds&
GetGpuDriverBugWorkarounds()47 SkiaOutputSurfaceDependencyImpl::GetGpuDriverBugWorkarounds() {
48   return gpu_service_impl_->gpu_channel_manager()->gpu_driver_bug_workarounds();
49 }
50 
51 scoped_refptr<gpu::SharedContextState>
GetSharedContextState()52 SkiaOutputSurfaceDependencyImpl::GetSharedContextState() {
53   return gpu_service_impl_->GetContextState();
54 }
55 
56 gpu::raster::GrShaderCache*
GetGrShaderCache()57 SkiaOutputSurfaceDependencyImpl::GetGrShaderCache() {
58   return gpu_service_impl_->gr_shader_cache();
59 }
60 
61 VulkanContextProvider*
GetVulkanContextProvider()62 SkiaOutputSurfaceDependencyImpl::GetVulkanContextProvider() {
63   return gpu_service_impl_->vulkan_context_provider();
64 }
65 
GetDawnContextProvider()66 DawnContextProvider* SkiaOutputSurfaceDependencyImpl::GetDawnContextProvider() {
67   return gpu_service_impl_->dawn_context_provider();
68 }
69 
GetGpuPreferences() const70 const gpu::GpuPreferences& SkiaOutputSurfaceDependencyImpl::GetGpuPreferences()
71     const {
72   return gpu_service_impl_->gpu_preferences();
73 }
74 
75 const gpu::GpuFeatureInfo&
GetGpuFeatureInfo()76 SkiaOutputSurfaceDependencyImpl::GetGpuFeatureInfo() {
77   return gpu_service_impl_->gpu_feature_info();
78 }
79 
GetMailboxManager()80 gpu::MailboxManager* SkiaOutputSurfaceDependencyImpl::GetMailboxManager() {
81   return gpu_service_impl_->mailbox_manager();
82 }
83 
GetGpuImageFactory()84 gpu::ImageFactory* SkiaOutputSurfaceDependencyImpl::GetGpuImageFactory() {
85   return gpu_service_impl_->gpu_image_factory();
86 }
87 
IsOffscreen()88 bool SkiaOutputSurfaceDependencyImpl::IsOffscreen() {
89   return surface_handle_ == gpu::kNullSurfaceHandle;
90 }
91 
GetSurfaceHandle()92 gpu::SurfaceHandle SkiaOutputSurfaceDependencyImpl::GetSurfaceHandle() {
93   return surface_handle_;
94 }
95 
CreateGLSurface(base::WeakPtr<gpu::ImageTransportSurfaceDelegate> stub,gl::GLSurfaceFormat format)96 scoped_refptr<gl::GLSurface> SkiaOutputSurfaceDependencyImpl::CreateGLSurface(
97     base::WeakPtr<gpu::ImageTransportSurfaceDelegate> stub,
98     gl::GLSurfaceFormat format) {
99   if (IsOffscreen()) {
100     return gl::init::CreateOffscreenGLSurfaceWithFormat(gfx::Size(), format);
101   } else {
102     return gpu::ImageTransportSurface::CreateNativeSurface(
103         stub, surface_handle_, format);
104   }
105 }
106 
CacheGLSurface(gl::GLSurface * surface)107 base::ScopedClosureRunner SkiaOutputSurfaceDependencyImpl::CacheGLSurface(
108     gl::GLSurface* surface) {
109   gpu_service_impl_->main_runner()->PostTask(
110       FROM_HERE,
111       base::BindOnce(&gl::GLSurface::AddRef, base::Unretained(surface)));
112   auto release_callback = base::BindOnce(
113       [](const scoped_refptr<base::SequencedTaskRunner>& runner,
114          gl::GLSurface* surface) {
115         runner->PostTask(FROM_HERE, base::BindOnce(&gl::GLSurface::Release,
116                                                    base::Unretained(surface)));
117       },
118       gpu_service_impl_->main_runner(), base::Unretained(surface));
119   return base::ScopedClosureRunner(std::move(release_callback));
120 }
121 
PostTaskToClientThread(base::OnceClosure closure)122 void SkiaOutputSurfaceDependencyImpl::PostTaskToClientThread(
123     base::OnceClosure closure) {
124   client_thread_task_runner_->PostTask(FROM_HERE, std::move(closure));
125 }
126 
ScheduleGrContextCleanup()127 void SkiaOutputSurfaceDependencyImpl::ScheduleGrContextCleanup() {
128   gpu_service_impl_->gpu_channel_manager()->ScheduleGrContextCleanup();
129 }
130 
ScheduleDelayedGPUTaskFromGPUThread(base::OnceClosure task)131 void SkiaOutputSurfaceDependencyImpl::ScheduleDelayedGPUTaskFromGPUThread(
132     base::OnceClosure task) {
133   DCHECK(gpu_service_impl_->main_runner()->BelongsToCurrentThread());
134 
135   constexpr base::TimeDelta kDelayForDelayedWork =
136       base::TimeDelta::FromMilliseconds(2);
137   gpu_service_impl_->main_runner()->PostDelayedTask(FROM_HERE, std::move(task),
138                                                     kDelayForDelayedWork);
139 }
140 
141 #if defined(OS_WIN)
DidCreateAcceleratedSurfaceChildWindow(gpu::SurfaceHandle parent_window,gpu::SurfaceHandle child_window)142 void SkiaOutputSurfaceDependencyImpl::DidCreateAcceleratedSurfaceChildWindow(
143     gpu::SurfaceHandle parent_window,
144     gpu::SurfaceHandle child_window) {
145   gpu_service_impl_->SendCreatedChildWindow(parent_window, child_window);
146 }
147 #endif
148 
RegisterDisplayContext(gpu::DisplayContext * display_context)149 void SkiaOutputSurfaceDependencyImpl::RegisterDisplayContext(
150     gpu::DisplayContext* display_context) {
151   gpu_service_impl_->RegisterDisplayContext(display_context);
152 }
153 
UnregisterDisplayContext(gpu::DisplayContext * display_context)154 void SkiaOutputSurfaceDependencyImpl::UnregisterDisplayContext(
155     gpu::DisplayContext* display_context) {
156   gpu_service_impl_->UnregisterDisplayContext(display_context);
157 }
158 
DidLoseContext(gpu::error::ContextLostReason reason,const GURL & active_url)159 void SkiaOutputSurfaceDependencyImpl::DidLoseContext(
160     gpu::error::ContextLostReason reason,
161     const GURL& active_url) {
162   // |offscreen| is used to determine if it's compositing context or not to
163   // decide if we need to disable webgl and canvas.
164   gpu_service_impl_->DidLoseContext(/*offscreen=*/false, reason, active_url);
165 }
166 
167 base::TimeDelta
GetGpuBlockedTimeSinceLastSwap()168 SkiaOutputSurfaceDependencyImpl::GetGpuBlockedTimeSinceLastSwap() {
169   return gpu_service_impl_->GetGpuScheduler()->TakeTotalBlockingTime();
170 }
171 
NeedsSupportForExternalStencil()172 bool SkiaOutputSurfaceDependencyImpl::NeedsSupportForExternalStencil() {
173   return false;
174 }
175 
176 }  // namespace viz
177