[go: nahoru, domu]

Reland "Use GrShaderCache with Vulkan"

This is a reland of db7626d649e439660fc2c5e6d4c8deb8bdd7dd55

Changes from the original: check if vk_context_provider not null.

Original change's description:
> Use GrShaderCache with Vulkan
>
> VulkanContextProvider is created before GrShaderCache and so cache
> wasn't pass to Skia via GrContextOptions.
>
> This CL defers init of GrContext in VulkanInProcessContextProvider to
> SharedContextState::InitializeGrContext to mitigate this.
>
> Use of cache itself is under feature flag for metrics comparison and
> a kill switch purpose.
>
> Bug: 1151031
> Change-Id: Idc410b9b557f7ca67164eb80d6a37a27ceb0f6d2
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2551245
> Reviewed-by: Peng Huang <penghuang@chromium.org>
> Commit-Queue: Vasiliy Telezhnikov <vasilyt@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#829651}

Bug: 1151031
Change-Id: Ibd4e7d55fdc56c1ea2cc0e82d6cc6c34459165c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2554353
Reviewed-by: Peng Huang <penghuang@chromium.org>
Commit-Queue: Vasiliy Telezhnikov <vasilyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830753}
diff --git a/android_webview/browser/gfx/aw_vulkan_context_provider.cc b/android_webview/browser/gfx/aw_vulkan_context_provider.cc
index d2525ac629..44622b3 100644
--- a/android_webview/browser/gfx/aw_vulkan_context_provider.cc
+++ b/android_webview/browser/gfx/aw_vulkan_context_provider.cc
@@ -215,6 +215,13 @@
   return !!globals_;
 }
 
+bool AwVulkanContextProvider::InitializeGrContext(
+    const GrContextOptions& context_options) {
+  // GrContext is created in Globals, so nothing to do here besides DCHECK.
+  DCHECK(globals_);
+  return globals_->gr_context.get() != nullptr;
+}
+
 void AwVulkanContextProvider::SecondaryCBDrawBegin(
     sk_sp<GrVkSecondaryCBDrawContext> draw_context) {
   DCHECK(draw_context);
diff --git a/android_webview/browser/gfx/aw_vulkan_context_provider.h b/android_webview/browser/gfx/aw_vulkan_context_provider.h
index d8c719bc..39e19b8 100644
--- a/android_webview/browser/gfx/aw_vulkan_context_provider.h
+++ b/android_webview/browser/gfx/aw_vulkan_context_provider.h
@@ -48,6 +48,7 @@
       AwDrawFn_InitVkParams* params);
 
   // viz::VulkanContextProvider implementation:
+  bool InitializeGrContext(const GrContextOptions& context_options) override;
   gpu::VulkanImplementation* GetVulkanImplementation() override;
   gpu::VulkanDeviceQueue* GetDeviceQueue() override;
   GrDirectContext* GetGrContext() override;
diff --git a/components/viz/common/gpu/vulkan_context_provider.h b/components/viz/common/gpu/vulkan_context_provider.h
index fc823ba..70ab11d 100644
--- a/components/viz/common/gpu/vulkan_context_provider.h
+++ b/components/viz/common/gpu/vulkan_context_provider.h
@@ -12,6 +12,7 @@
 #include "components/viz/common/viz_vulkan_context_provider_export.h"
 #include "third_party/vulkan_headers/include/vulkan/vulkan.h"
 
+struct GrContextOptions;
 class GrDirectContext;
 class GrVkSecondaryCBDrawContext;
 
@@ -26,6 +27,7 @@
 class VIZ_VULKAN_CONTEXT_PROVIDER_EXPORT VulkanContextProvider
     : public base::RefCountedThreadSafe<VulkanContextProvider> {
  public:
+  virtual bool InitializeGrContext(const GrContextOptions& context_options) = 0;
   virtual gpu::VulkanImplementation* GetVulkanImplementation() = 0;
   virtual gpu::VulkanDeviceQueue* GetDeviceQueue() = 0;
   virtual GrDirectContext* GetGrContext() = 0;
diff --git a/components/viz/common/gpu/vulkan_in_process_context_provider.cc b/components/viz/common/gpu/vulkan_in_process_context_provider.cc
index 0a30603f..292409d 100644
--- a/components/viz/common/gpu/vulkan_in_process_context_provider.cc
+++ b/components/viz/common/gpu/vulkan_in_process_context_provider.cc
@@ -23,11 +23,10 @@
 scoped_refptr<VulkanInProcessContextProvider>
 VulkanInProcessContextProvider::Create(
     gpu::VulkanImplementation* vulkan_implementation,
-    const GrContextOptions& options,
     const gpu::GPUInfo* gpu_info) {
   scoped_refptr<VulkanInProcessContextProvider> context_provider(
       new VulkanInProcessContextProvider(vulkan_implementation));
-  if (!context_provider->Initialize(options, gpu_info))
+  if (!context_provider->Initialize(gpu_info))
     return nullptr;
   return context_provider;
 }
@@ -41,7 +40,6 @@
 }
 
 bool VulkanInProcessContextProvider::Initialize(
-    const GrContextOptions& context_options,
     const gpu::GPUInfo* gpu_info) {
   DCHECK(!device_queue_);
 
@@ -64,6 +62,11 @@
   if (!device_queue_)
     return false;
 
+  return true;
+}
+
+bool VulkanInProcessContextProvider::InitializeGrContext(
+    const GrContextOptions& context_options) {
   GrVkBackendContext backend_context;
   backend_context.fInstance = device_queue_->GetVulkanInstance();
   backend_context.fPhysicalDevice = device_queue_->GetVulkanPhysicalDevice();
@@ -86,6 +89,10 @@
     return vkGetInstanceProcAddr(instance, proc_name);
   };
 
+  const auto& instance_extensions = vulkan_implementation_->GetVulkanInstance()
+                                        ->vulkan_info()
+                                        .enabled_instance_extensions;
+
   std::vector<const char*> device_extensions;
   device_extensions.reserve(device_queue_->enabled_extensions().size());
   for (const auto& extension : device_queue_->enabled_extensions())
diff --git a/components/viz/common/gpu/vulkan_in_process_context_provider.h b/components/viz/common/gpu/vulkan_in_process_context_provider.h
index ec6676e..9b2a787a 100644
--- a/components/viz/common/gpu/vulkan_in_process_context_provider.h
+++ b/components/viz/common/gpu/vulkan_in_process_context_provider.h
@@ -30,12 +30,12 @@
  public:
   static scoped_refptr<VulkanInProcessContextProvider> Create(
       gpu::VulkanImplementation* vulkan_implementation,
-      const GrContextOptions& context_options = GrContextOptions(),
       const gpu::GPUInfo* gpu_info = nullptr);
 
   void Destroy();
 
   // VulkanContextProvider implementation
+  bool InitializeGrContext(const GrContextOptions& context_options) override;
   gpu::VulkanImplementation* GetVulkanImplementation() override;
   gpu::VulkanDeviceQueue* GetDeviceQueue() override;
   GrDirectContext* GetGrContext() override;
@@ -49,8 +49,7 @@
       gpu::VulkanImplementation* vulkan_implementation);
   ~VulkanInProcessContextProvider() override;
 
-  bool Initialize(const GrContextOptions& context_options,
-                  const gpu::GPUInfo* gpu_info);
+  bool Initialize(const gpu::GPUInfo* gpu_info);
 
 #if BUILDFLAG(ENABLE_VULKAN)
   sk_sp<GrDirectContext> gr_context_;
diff --git a/components/viz/service/gl/gpu_service_impl.cc b/components/viz/service/gl/gpu_service_impl.cc
index 4c35b56..1d42308 100644
--- a/components/viz/service/gl/gpu_service_impl.cc
+++ b/components/viz/service/gl/gpu_service_impl.cc
@@ -369,7 +369,7 @@
     // If GL is using a real GPU, the gpu_info will be passed in and vulkan will
     // use the same GPU.
     vulkan_context_provider_ = VulkanInProcessContextProvider::Create(
-        vulkan_implementation_, context_options,
+        vulkan_implementation_,
         (is_native_vulkan && is_native_gl) ? &gpu_info : nullptr);
     if (vulkan_context_provider_) {
       // If Vulkan is supported, then OOP-R is supported.
diff --git a/gpu/command_buffer/service/shared_context_state.cc b/gpu/command_buffer/service/shared_context_state.cc
index c72b2bc..b3c77b9cd 100644
--- a/gpu/command_buffer/service/shared_context_state.cc
+++ b/gpu/command_buffer/service/shared_context_state.cc
@@ -17,6 +17,7 @@
 #include "gpu/command_buffer/service/service_utils.h"
 #include "gpu/command_buffer/service/skia_utils.h"
 #include "gpu/config/gpu_driver_bug_workarounds.h"
+#include "gpu/config/gpu_finch_features.h"
 #include "gpu/config/skia_limits.h"
 #include "gpu/vulkan/buildflags.h"
 #include "skia/buildflags.h"
@@ -33,10 +34,6 @@
 #include "gpu/vulkan/vulkan_device_queue.h"
 #endif
 
-#if defined(OS_ANDROID)
-#include "gpu/config/gpu_finch_features.h"
-#endif
-
 #if defined(OS_FUCHSIA)
 #include "gpu/vulkan/fuchsia/vulkan_fuchsia_ext.h"
 #endif
@@ -178,12 +175,10 @@
     case GrContextType::kVulkan:
       if (vk_context_provider_) {
 #if BUILDFLAG(ENABLE_VULKAN)
-        gr_context_ = vk_context_provider_->GetGrContext();
         external_semaphore_pool_ =
             std::make_unique<ExternalSemaphorePool>(this);
 #endif
         use_virtualized_gl_contexts_ = false;
-        DCHECK(gr_context_);
       }
       break;
     case GrContextType::kMetal:
@@ -271,6 +266,16 @@
   DetermineGrCacheLimitsFromAvailableMemory(&max_resource_cache_bytes,
                                             &glyph_cache_max_texture_bytes);
 
+  // If you make any changes to the GrContext::Options here that could
+  // affect text rendering, make sure to match the capabilities initialized
+  // in GetCapabilities and ensuring these are also used by the
+  // PaintOpBufferSerializer.
+  GrContextOptions options = GetDefaultGrContextOptions(gr_context_type_);
+  options.fPersistentCache = cache;
+  options.fShaderErrorHandler = this;
+  if (gpu_preferences.force_max_texture_size)
+    options.fMaxTextureSizeOverride = gpu_preferences.force_max_texture_size;
+
   if (gr_context_type_ == GrContextType::kGL) {
     DCHECK(context_->IsCurrent(nullptr));
     bool use_version_es2 = false;
@@ -296,25 +301,32 @@
             glProgramBinary(program, binaryFormat, binary, length);
           };
     }
-    // If you make any changes to the GrContext::Options here that could
-    // affect text rendering, make sure to match the capabilities initialized
-    // in GetCapabilities and ensuring these are also used by the
-    // PaintOpBufferSerializer.
-    GrContextOptions options = GetDefaultGrContextOptions(GrContextType::kGL);
     options.fDriverBugWorkarounds =
         GrDriverBugWorkarounds(workarounds.ToIntSet());
-    options.fPersistentCache = cache;
     options.fAvoidStencilBuffers = workarounds.avoid_stencil_buffers;
     if (workarounds.disable_program_disk_cache) {
       options.fShaderCacheStrategy =
           GrContextOptions::ShaderCacheStrategy::kBackendSource;
     }
-    options.fShaderErrorHandler = this;
-    if (gpu_preferences.force_max_texture_size)
-      options.fMaxTextureSizeOverride = gpu_preferences.force_max_texture_size;
     options.fPreferExternalImagesOverES3 = true;
     owned_gr_context_ = GrDirectContext::MakeGL(std::move(interface), options);
     gr_context_ = owned_gr_context_.get();
+  } else if (gr_context_type_ == GrContextType::kVulkan) {
+#if BUILDFLAG(ENABLE_VULKAN)
+    if (vk_context_provider_) {
+      // TODO(vasilyt): Remove this if there is no problem with caching.
+      if (!base::FeatureList::IsEnabled(
+              features::kEnableGrShaderCacheForVulkan))
+        options.fPersistentCache = nullptr;
+
+      if (!vk_context_provider_->InitializeGrContext(options)) {
+        LOG(ERROR) << "Failed to initialize GrContext for Vulkan.";
+        return false;
+      }
+      gr_context_ = vk_context_provider_->GetGrContext();
+      DCHECK(gr_context_);
+    }
+#endif
   }
 
   if (!gr_context_) {
diff --git a/gpu/config/gpu_finch_features.cc b/gpu/config/gpu_finch_features.cc
index b108ba9..2a6674d7 100644
--- a/gpu/config/gpu_finch_features.cc
+++ b/gpu/config/gpu_finch_features.cc
@@ -123,6 +123,10 @@
 const base::Feature kEnableSharedImageForWebview{
     "EnableSharedImageForWebview", base::FEATURE_ENABLED_BY_DEFAULT};
 
+// Enable GrShaderCache to use with Vulkan backend.
+const base::Feature kEnableGrShaderCacheForVulkan{
+    "EnableGrShaderCacheForVulkan", base::FEATURE_ENABLED_BY_DEFAULT};
+
 bool IsUsingVulkan() {
   bool enable = base::FeatureList::IsEnabled(kVulkan);
 #if defined(OS_ANDROID)
diff --git a/gpu/config/gpu_finch_features.h b/gpu/config/gpu_finch_features.h
index 055c174..eb322d2a 100644
--- a/gpu/config/gpu_finch_features.h
+++ b/gpu/config/gpu_finch_features.h
@@ -53,6 +53,8 @@
 
 GPU_EXPORT extern const base::Feature kEnableSharedImageForWebview;
 
+GPU_EXPORT extern const base::Feature kEnableGrShaderCacheForVulkan;
+
 GPU_EXPORT bool IsUsingVulkan();
 #if defined(OS_ANDROID)
 GPU_EXPORT bool IsAImageReaderEnabled();