diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index b6d5dbce78b8c..b6275cbb34843 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -671,6 +671,8 @@ FILE: ../../../flutter/shell/common/animator_unittests.cc FILE: ../../../flutter/shell/common/canvas_spy.cc FILE: ../../../flutter/shell/common/canvas_spy.h FILE: ../../../flutter/shell/common/canvas_spy_unittests.cc +FILE: ../../../flutter/shell/common/context_options.cc +FILE: ../../../flutter/shell/common/context_options.h FILE: ../../../flutter/shell/common/display.h FILE: ../../../flutter/shell/common/display_manager.cc FILE: ../../../flutter/shell/common/display_manager.h diff --git a/shell/common/BUILD.gn b/shell/common/BUILD.gn index 0e24c721d7019..d7c4f09eabf49 100644 --- a/shell/common/BUILD.gn +++ b/shell/common/BUILD.gn @@ -64,6 +64,8 @@ source_set("common") { "animator.h", "canvas_spy.cc", "canvas_spy.h", + "context_options.cc", + "context_options.h", "display.h", "display_manager.cc", "display_manager.h", diff --git a/shell/common/context_options.cc b/shell/common/context_options.cc new file mode 100644 index 0000000000000..4fea8fe7d4834 --- /dev/null +++ b/shell/common/context_options.cc @@ -0,0 +1,39 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/common/context_options.h" + +#include "flutter/common/graphics/persistent_cache.h" + +namespace flutter { + +GrContextOptions MakeDefaultContextOptions(ContextType type, + std::optional api) { + GrContextOptions options; + + if (PersistentCache::cache_sksl()) { + options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL; + } + PersistentCache::MarkStrategySet(); + options.fPersistentCache = PersistentCache::GetCacheForProcess(); + + if (api.has_value() && api.value() == GrBackendApi::kOpenGL) { + options.fAvoidStencilBuffers = true; + + // To get video playback on the widest range of devices, we limit Skia to + // ES2 shading language when the ES3 external image extension is missing. + options.fPreferExternalImagesOverES3 = true; + } + + // TODO(goderbauer): remove option when skbug.com/7523 is fixed. + options.fDisableGpuYUVConversion = true; + + options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; + + options.fReducedShaderVariations = true; + + return options; +}; + +} // namespace flutter diff --git a/shell/common/context_options.h b/shell/common/context_options.h new file mode 100644 index 0000000000000..bae0b2fb8e4e4 --- /dev/null +++ b/shell/common/context_options.h @@ -0,0 +1,43 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_COMMON_CONTEXT_OPTIONS_H_ +#define FLUTTER_SHELL_COMMON_CONTEXT_OPTIONS_H_ + +#include + +#include "flutter/fml/macros.h" +#include "third_party/skia/include/gpu/GrContextOptions.h" + +namespace flutter { + +enum class ContextType { + /// The context is used to render to a texture or renderbuffer. + kRender, + /// The context will only be used to transfer resources to and from device + /// memory. No rendering will be performed using this context. + kResource, +}; + +//------------------------------------------------------------------------------ +/// @brief Initializes GrContextOptions with values suitable for Flutter. +/// The options can be further tweaked before a GrContext is created +/// from these options. +/// +/// @param[in] type The type of context that will be created using these +/// options. +/// @param[in] type The client rendering API that will be wrapped using a +/// context with these options. This argument is only required +/// if the context is going to be used with a particular +/// client rendering API. +/// +/// @return The default graphics context options. +/// +GrContextOptions MakeDefaultContextOptions( + ContextType type, + std::optional api = std::nullopt); + +} // namespace flutter + +#endif // FLUTTER_SHELL_COMMON_CONTEXT_OPTIONS_H_ diff --git a/shell/common/platform_view.cc b/shell/common/platform_view.cc index 7c7b4dd1d421e..b2c552599e547 100644 --- a/shell/common/platform_view.cc +++ b/shell/common/platform_view.cc @@ -11,7 +11,6 @@ #include "flutter/shell/common/rasterizer.h" #include "flutter/shell/common/shell.h" #include "flutter/shell/common/vsync_waiter_fallback.h" -#include "third_party/skia/include/gpu/GrContextOptions.h" #include "third_party/skia/include/gpu/gl/GrGLInterface.h" namespace flutter { diff --git a/shell/common/shell_io_manager.cc b/shell/common/shell_io_manager.cc index cf90a73bfe1a5..264ea5a32d8ce 100644 --- a/shell/common/shell_io_manager.cc +++ b/shell/common/shell_io_manager.cc @@ -7,6 +7,7 @@ #include "flutter/common/graphics/persistent_cache.h" #include "flutter/fml/build_config.h" #include "flutter/fml/message_loop.h" +#include "flutter/shell/common/context_options.h" #include "third_party/skia/include/gpu/gl/GrGLInterface.h" namespace flutter { @@ -18,30 +19,7 @@ sk_sp ShellIOManager::CreateCompatibleResourceLoadingContext( return nullptr; } - GrContextOptions options = {}; - - if (PersistentCache::cache_sksl()) { - FML_LOG(INFO) << "Cache SkSL"; - options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL; - } - PersistentCache::MarkStrategySet(); - - options.fPersistentCache = PersistentCache::GetCacheForProcess(); - - // There is currently a bug with doing GPU YUV to RGB conversions on the IO - // thread. The necessary work isn't being flushed or synchronized with the - // other threads correctly, so the textures end up blank. For now, suppress - // that feature, which will cause texture uploads to do CPU YUV conversion. - // A similar work-around is also used in shell/gpu/gpu_surface_gl.cc. - options.fDisableGpuYUVConversion = true; - - // To get video playback on the widest range of devices, we limit Skia to - // ES2 shading language when the ES3 external image extension is missing. - options.fPreferExternalImagesOverES3 = true; - - // Enabling this flag can increase VRAM consumption. Turn it off until - // further notice. - options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; + const auto options = MakeDefaultContextOptions(ContextType::kResource); #if !OS_FUCHSIA if (auto context = GrDirectContext::MakeGL(gl_interface, options)) { diff --git a/shell/common/shell_test_platform_view_vulkan.cc b/shell/common/shell_test_platform_view_vulkan.cc index 29f6aba2becc1..754133c514f0e 100644 --- a/shell/common/shell_test_platform_view_vulkan.cc +++ b/shell/common/shell_test_platform_view_vulkan.cc @@ -5,6 +5,7 @@ #include "flutter/shell/common/shell_test_platform_view_vulkan.h" #include "flutter/common/graphics/persistent_cache.h" +#include "flutter/shell/common/context_options.h" #include "flutter/vulkan/vulkan_utilities.h" namespace flutter { @@ -113,13 +114,8 @@ bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaGrContext() { return false; } - GrContextOptions options; - if (PersistentCache::cache_sksl()) { - options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL; - } - PersistentCache::MarkStrategySet(); - options.fPersistentCache = PersistentCache::GetCacheForProcess(); - options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; + const auto options = + MakeDefaultContextOptions(ContextType::kRender, GrBackendApi::kVulkan); sk_sp context = GrDirectContext::MakeVulkan(backend_context, options); diff --git a/shell/gpu/gpu_surface_gl.cc b/shell/gpu/gpu_surface_gl.cc index e04a9a2556189..e78135663764b 100644 --- a/shell/gpu/gpu_surface_gl.cc +++ b/shell/gpu/gpu_surface_gl.cc @@ -9,6 +9,7 @@ #include "flutter/fml/logging.h" #include "flutter/fml/size.h" #include "flutter/fml/trace_event.h" +#include "flutter/shell/common/context_options.h" #include "third_party/skia/include/core/SkColorFilter.h" #include "third_party/skia/include/core/SkSurface.h" #include "third_party/skia/include/gpu/GrBackendSurface.h" @@ -43,26 +44,8 @@ sk_sp GPUSurfaceGL::MakeGLContext( return nullptr; } - GrContextOptions options; - - if (PersistentCache::cache_sksl()) { - FML_LOG(INFO) << "Cache SkSL"; - options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL; - } - PersistentCache::MarkStrategySet(); - options.fPersistentCache = PersistentCache::GetCacheForProcess(); - - options.fAvoidStencilBuffers = true; - - // To get video playback on the widest range of devices, we limit Skia to - // ES2 shading language when the ES3 external image extension is missing. - options.fPreferExternalImagesOverES3 = true; - - // TODO(goderbauer): remove option when skbug.com/7523 is fixed. - // A similar work-around is also used in shell/common/io_manager.cc. - options.fDisableGpuYUVConversion = true; - - options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; + const auto options = + MakeDefaultContextOptions(ContextType::kRender, GrBackendApi::kOpenGL); auto context = GrDirectContext::MakeGL(delegate->GetGLInterface(), options); diff --git a/shell/platform/darwin/graphics/BUILD.gn b/shell/platform/darwin/graphics/BUILD.gn index 7dd998a48f6a9..bf49f796af3b8 100644 --- a/shell/platform/darwin/graphics/BUILD.gn +++ b/shell/platform/darwin/graphics/BUILD.gn @@ -20,6 +20,7 @@ source_set("graphics") { deps = [ "//flutter/common/graphics", "//flutter/fml", + "//flutter/shell/common", "//flutter/shell/platform/darwin/common:framework_shared", ] diff --git a/shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm b/shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm index 6beab111e2ed8..8c90d4bacf15f 100644 --- a/shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm +++ b/shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm @@ -7,21 +7,10 @@ #include "flutter/common/graphics/persistent_cache.h" #include "flutter/fml/logging.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" -#include "third_party/skia/include/gpu/GrContextOptions.h" +#include "shell/common/context_options.h" FLUTTER_ASSERT_ARC -static GrContextOptions CreateMetalGrContextOptions() { - GrContextOptions options = {}; - if (flutter::PersistentCache::cache_sksl()) { - options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL; - } - flutter::PersistentCache::MarkStrategySet(); - options.fPersistentCache = flutter::PersistentCache::GetCacheForProcess(); - options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; - return options; -} - @implementation FlutterDarwinContextMetal - (instancetype)initWithDefaultMTLDevice { @@ -77,7 +66,8 @@ - (instancetype)initWithMTLDevice:(id)device } - (sk_sp)createGrContext { - auto contextOptions = CreateMetalGrContextOptions(); + const auto contextOptions = + flutter::MakeDefaultContextOptions(flutter::ContextType::kRender, GrBackendApi::kMetal); id device = _device; id commandQueue = _commandQueue; return [FlutterDarwinContextMetal createGrContext:device commandQueue:commandQueue]; @@ -85,7 +75,8 @@ - (instancetype)initWithMTLDevice:(id)device + (sk_sp)createGrContext:(id)device commandQueue:(id)commandQueue { - auto contextOptions = CreateMetalGrContextOptions(); + const auto contextOptions = + flutter::MakeDefaultContextOptions(flutter::ContextType::kRender, GrBackendApi::kMetal); // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later // when the GrDirectContext is collected. return GrDirectContext::MakeMetal((__bridge_retained void*)device, diff --git a/shell/platform/embedder/fixtures/gradient.png b/shell/platform/embedder/fixtures/gradient.png index 27b69ecbb85c3..bf9647518e08b 100644 Binary files a/shell/platform/embedder/fixtures/gradient.png and b/shell/platform/embedder/fixtures/gradient.png differ diff --git a/shell/platform/embedder/fixtures/gradient_xform.png b/shell/platform/embedder/fixtures/gradient_xform.png index 186b98e87e69a..b51524e6b203d 100644 Binary files a/shell/platform/embedder/fixtures/gradient_xform.png and b/shell/platform/embedder/fixtures/gradient_xform.png differ diff --git a/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/shell/platform/fuchsia/flutter/platform_view_unittest.cc index e0e8c37ebd271..f26e29226e5fd 100644 --- a/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -20,6 +20,7 @@ #include "flutter/flow/embedded_views.h" #include "flutter/lib/ui/window/platform_message.h" #include "flutter/lib/ui/window/viewport_metrics.h" +#include "flutter/shell/common/context_options.h" #include "flutter/shell/platform/fuchsia/flutter/platform_view.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -347,8 +348,9 @@ TEST_F(PlatformViewTests, CreateSurfaceTest) { ); // Test create surface callback function. - sk_sp gr_context = - GrDirectContext::MakeMock(nullptr, GrContextOptions()); + sk_sp gr_context = GrDirectContext::MakeMock( + nullptr, + flutter::MakeDefaultContextOptions(flutter::ContextType::kRender)); std::shared_ptr view_embedder = std::make_shared(); auto CreateSurfaceCallback = [&view_embedder, gr_context]() { diff --git a/shell/platform/fuchsia/flutter/vulkan_surface_producer.cc b/shell/platform/fuchsia/flutter/vulkan_surface_producer.cc index 53bf3b3eae6cf..7cd757e5cc737 100644 --- a/shell/platform/fuchsia/flutter/vulkan_surface_producer.cc +++ b/shell/platform/fuchsia/flutter/vulkan_surface_producer.cc @@ -12,6 +12,7 @@ #include #include "flutter/fml/trace_event.h" +#include "flutter/shell/common/context_options.h" #include "third_party/skia/include/gpu/GrBackendSemaphore.h" #include "third_party/skia/include/gpu/GrBackendSurface.h" #include "third_party/skia/include/gpu/GrDirectContext.h" @@ -137,9 +138,8 @@ bool VulkanSurfaceProducer::Initialize(scenic::Session* scenic_session) { backend_context.fPhysicalDevice, 0, nullptr, countof(device_extensions), device_extensions); backend_context.fVkExtensions = &vk_extensions; - GrContextOptions options; - options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; - + const auto options = flutter::MakeDefaultContextOptions( + flutter::ContextType::kRender, GrBackendApi::kVulkan); context_ = GrDirectContext::MakeVulkan(backend_context, options); if (context_ == nullptr) {