Skip to content

Commit

Permalink
[Vulkan] Try a desired list of surface formats before picking one at …
Browse files Browse the repository at this point in the history
…random. (flutter#3384)
  • Loading branch information
chinmaygarde authored Feb 1, 2017
1 parent 01afda4 commit dd45346
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
31 changes: 29 additions & 2 deletions vulkan/vulkan_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "flutter/vulkan/vulkan_device.h"

#include <limits>
#include <map>
#include <vector>

#include "flutter/vulkan/vulkan_proc_table.h"
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion vulkan/vulkan_swapchain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit dd45346

Please sign in to comment.