[Vulkan] Try a desired list of surface formats before picking one at random. (flutter/engine#3384)
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "flutter/vulkan/vulkan_device.h"
|
||||
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/vulkan/vulkan_proc_table.h"
|
||||
@@ -273,14 +274,40 @@ bool VulkanDevice::ChooseSurfaceFormat(const VulkanSurface& surface,
|
||||
return false;
|
||||
}
|
||||
|
||||
std::map<VkFormat, VkSurfaceFormatKHR> 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<VkFormat> 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,
|
||||
|
||||
@@ -170,6 +170,18 @@ SkISize VulkanSwapchain::GetSize() const {
|
||||
return SkISize::Make(extents.width, extents.height);
|
||||
}
|
||||
|
||||
static sk_sp<SkColorSpace> 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<SkSurface> VulkanSwapchain::CreateSkiaSurface(GrContext* gr_context,
|
||||
VkImage image,
|
||||
const SkISize& size) const {
|
||||
@@ -207,7 +219,12 @@ sk_sp<SkSurface> 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) {
|
||||
|
||||
Reference in New Issue
Block a user