From e6e3d4c3b61c9e26f0f6074dc605724534e1ec3b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 1 Feb 2017 14:54:10 -0800 Subject: [PATCH] [Vulkan] Try a desired list of surface formats before picking one at random. (flutter/engine#3384) --- engine/src/flutter/vulkan/vulkan_device.cc | 31 +++++++++++++++++-- engine/src/flutter/vulkan/vulkan_swapchain.cc | 19 +++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/vulkan/vulkan_device.cc b/engine/src/flutter/vulkan/vulkan_device.cc index e489f9cc72..d8474efdbc 100644 --- a/engine/src/flutter/vulkan/vulkan_device.cc +++ b/engine/src/flutter/vulkan/vulkan_device.cc @@ -5,6 +5,7 @@ #include "flutter/vulkan/vulkan_device.h" #include +#include #include #include "flutter/vulkan/vulkan_proc_table.h" @@ -273,14 +274,40 @@ bool VulkanDevice::ChooseSurfaceFormat(const VulkanSurface& surface, return false; } + std::map supported_formats; + for (uint32_t i = 0; i < format_count; i++) { if (GrVkFormatToPixelConfig(formats[i].format, nullptr /* dont care */)) { - *format = formats[i]; + supported_formats[formats[i].format] = formats[i]; + } + } + + if (supported_formats.size() == 0) { + return false; + } + + const std::vector desired_formats = { + VK_FORMAT_R8G8B8A8_SRGB, // kSRGBA_8888_GrPixelConfig + VK_FORMAT_B8G8R8A8_SRGB, // kSBGRA_8888_GrPixelConfig + VK_FORMAT_R16G16B16A16_SFLOAT // kRGBA_half_GrPixelConfig + VK_FORMAT_R8G8B8A8_UNORM, // kRGBA_8888_GrPixelConfig + VK_FORMAT_B8G8R8A8_UNORM, // kBGRA_8888_GrPixelConfig + }; + + // Try to find the first supported format in the list of desired formats. + for (VkFormat current_format : desired_formats) { + auto found = supported_formats.find(current_format); + if (found != supported_formats.end()) { + *format = found->second; return true; } } - return false; + // None of the desired formats were supported. Return the first supported + // format even if we don't like it all that much (it has already returned true + // for GrVkFormatToPixelConfig). + *format = supported_formats.begin()->second; + return true; } bool VulkanDevice::ChoosePresentMode(const VulkanSurface& surface, diff --git a/engine/src/flutter/vulkan/vulkan_swapchain.cc b/engine/src/flutter/vulkan/vulkan_swapchain.cc index a42d3fe2e4..3b6213aa93 100644 --- a/engine/src/flutter/vulkan/vulkan_swapchain.cc +++ b/engine/src/flutter/vulkan/vulkan_swapchain.cc @@ -170,6 +170,18 @@ SkISize VulkanSwapchain::GetSize() const { return SkISize::Make(extents.width, extents.height); } +static sk_sp SkColorSpaceFromVkFormat(VkFormat format) { + if (GrVkFormatIsSRGB(format, nullptr /* dont care */)) { + return SkColorSpace::MakeNamed(SkColorSpace::Named::kSRGB_Named); + } + + if (format == VK_FORMAT_R16G16B16A16_SFLOAT) { + return SkColorSpace::MakeNamed(SkColorSpace::Named::kSRGBLinear_Named); + } + + return nullptr; +} + sk_sp VulkanSwapchain::CreateSkiaSurface(GrContext* gr_context, VkImage image, const SkISize& size) const { @@ -207,7 +219,12 @@ sk_sp VulkanSwapchain::CreateSkiaSurface(GrContext* gr_context, SkSurfaceProps props(SkSurfaceProps::InitType::kLegacyFontHost_InitType); - return SkSurface::MakeFromBackendRenderTarget(gr_context, desc, &props); + return SkSurface::MakeFromBackendRenderTarget( + gr_context, // context + desc, // backend render target description + SkColorSpaceFromVkFormat(surface_format_.format), // colorspace + &props // surface properties + ); } bool VulkanSwapchain::CreateSwapchainImages(GrContext* skia_context) {