Commit d4216c01 authored by Brian Ho's avatar Brian Ho Committed by Commit Bot

Refactor viz format to wgpu format code

Currently, we have duplicate implementations of viz::ResourceFormat
to WGPUTextureFormat conversion. This CL exposes a new ToWGPUFormat
function in resource_format_utils and removes duplicate code.

Note: ToWGPUFormat calls ToDawnFormat under the hood and casts from
dawn::TextureFormat to WGPUTextureFormat. Looking at the
definitions [1][2], it seems like they are generated from the same
place, so this cast is safe?

[1] https://cs.chromium.org/chromium/src/out/Debug/gen/third_party/dawn/src/include/dawn/webgpu.h?type=cs&q=WGPUTextureFormat&sq=package:chromium&g=0&l=252
[2] https://cs.chromium.org/chromium/src/out/Debug/gen/third_party/dawn/src/include/dawn/webgpu_cpp.h?type=cs&q=BGRA8Unorm+f:dawn&sq=package:chromium&g=0&l=176

Change-Id: I6159240c40c084a6d9fd3c8f7b4ff66ab03063f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1950750Reviewed-by: default avatarEric Karl <ericrk@chromium.org>
Reviewed-by: default avatarKenneth Russell <kbr@chromium.org>
Reviewed-by: default avatarCorentin Wallez <cwallez@chromium.org>
Commit-Queue: Brian Ho <hob@chromium.org>
Cr-Commit-Position: refs/heads/master@{#722620}
parent ec7383d8
...@@ -38,11 +38,8 @@ viz_component("resource_format_utils") { ...@@ -38,11 +38,8 @@ viz_component("resource_format_utils") {
"//skia", "//skia",
"//ui/gfx:buffer_types", "//ui/gfx:buffer_types",
"//ui/gfx/geometry:geometry", "//ui/gfx/geometry:geometry",
"//third_party/dawn/src/dawn:dawncpp_headers",
] ]
if (skia_use_dawn) {
deps += [ "//third_party/dawn/src/dawn:dawncpp_headers" ]
}
} }
# TODO(sgilhuly): To reduce link times, merge these context provider components # TODO(sgilhuly): To reduce link times, merge these context provider components
......
...@@ -488,25 +488,24 @@ VkFormat ToVkFormat(ResourceFormat format) { ...@@ -488,25 +488,24 @@ VkFormat ToVkFormat(ResourceFormat format) {
} }
#endif #endif
#if BUILDFLAG(SKIA_USE_DAWN) wgpu::TextureFormat ToDawnFormat(ResourceFormat format) {
dawn::TextureFormat ToDawnFormat(ResourceFormat format) {
switch (format) { switch (format) {
case RGBA_8888: case RGBA_8888:
case RGBX_8888: case RGBX_8888:
return dawn::TextureFormat::RGBA8Unorm; return wgpu::TextureFormat::RGBA8Unorm;
case BGRA_8888: case BGRA_8888:
case BGRX_8888: case BGRX_8888:
return dawn::TextureFormat::BGRA8Unorm; return wgpu::TextureFormat::BGRA8Unorm;
case RED_8: case RED_8:
case ALPHA_8: case ALPHA_8:
case LUMINANCE_8: case LUMINANCE_8:
return dawn::TextureFormat::R8Unorm; return wgpu::TextureFormat::R8Unorm;
case RG_88: case RG_88:
return dawn::TextureFormat::RG8Unorm; return wgpu::TextureFormat::RG8Unorm;
case RGBA_F16: case RGBA_F16:
return dawn::TextureFormat::RGBA16Float; return wgpu::TextureFormat::RGBA16Float;
case RGBX_1010102: case RGBX_1010102:
return dawn::TextureFormat::RGB10A2Unorm; return wgpu::TextureFormat::RGB10A2Unorm;
case RGBA_4444: case RGBA_4444:
case RGB_565: case RGB_565:
case BGR_565: case BGR_565:
...@@ -520,8 +519,11 @@ dawn::TextureFormat ToDawnFormat(ResourceFormat format) { ...@@ -520,8 +519,11 @@ dawn::TextureFormat ToDawnFormat(ResourceFormat format) {
break; break;
} }
NOTREACHED() << "Unsupported format " << format; NOTREACHED() << "Unsupported format " << format;
return dawn::TextureFormat::Undefined; return wgpu::TextureFormat::Undefined;
}
WGPUTextureFormat ToWGPUFormat(ResourceFormat format) {
return static_cast<WGPUTextureFormat>(ToDawnFormat(format));
} }
#endif
} // namespace viz } // namespace viz
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#ifndef COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_ #ifndef COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
#define COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_ #define COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
#include <dawn/webgpu_cpp.h>
#include <dawn/webgpu.h>
#include "components/viz/common/resources/resource_format.h" #include "components/viz/common/resources/resource_format.h"
#include "components/viz/common/viz_resource_format_export.h" #include "components/viz/common/viz_resource_format_export.h"
#include "gpu/vulkan/buildflags.h" #include "gpu/vulkan/buildflags.h"
...@@ -17,10 +20,6 @@ ...@@ -17,10 +20,6 @@
#include "third_party/vulkan/include/vulkan/vulkan.h" #include "third_party/vulkan/include/vulkan/vulkan.h"
#endif #endif
#if BUILDFLAG(SKIA_USE_DAWN)
#include <dawn/dawncpp.h>
#endif
namespace viz { namespace viz {
VIZ_RESOURCE_FORMAT_EXPORT SkColorType VIZ_RESOURCE_FORMAT_EXPORT SkColorType
...@@ -67,10 +66,12 @@ VIZ_RESOURCE_FORMAT_EXPORT bool GLSupportsFormat(ResourceFormat format); ...@@ -67,10 +66,12 @@ VIZ_RESOURCE_FORMAT_EXPORT bool GLSupportsFormat(ResourceFormat format);
VIZ_RESOURCE_FORMAT_EXPORT VkFormat ToVkFormat(ResourceFormat format); VIZ_RESOURCE_FORMAT_EXPORT VkFormat ToVkFormat(ResourceFormat format);
#endif #endif
#if BUILDFLAG(SKIA_USE_DAWN) VIZ_RESOURCE_FORMAT_EXPORT wgpu::TextureFormat ToDawnFormat(
VIZ_RESOURCE_FORMAT_EXPORT dawn::TextureFormat ToDawnFormat(
ResourceFormat format); ResourceFormat format);
#endif // Same as ToDawnFormat, except it casts from wgpu::TextureFormat to
// WGPUTextureFormat instead.
VIZ_RESOURCE_FORMAT_EXPORT WGPUTextureFormat
ToWGPUFormat(ResourceFormat format);
} // namespace viz } // namespace viz
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/system/sys_info.h" #include "base/system/sys_info.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/viz/common/resources/resource_format_utils.h"
#include "components/viz/common/resources/resource_sizes.h" #include "components/viz/common/resources/resource_sizes.h"
#include "gpu/command_buffer/service/external_vk_image_gl_representation.h" #include "gpu/command_buffer/service/external_vk_image_gl_representation.h"
#include "gpu/command_buffer/service/external_vk_image_skia_representation.h" #include "gpu/command_buffer/service/external_vk_image_skia_representation.h"
...@@ -169,23 +170,6 @@ class ScopedPixelStore { ...@@ -169,23 +170,6 @@ class ScopedPixelStore {
DISALLOW_COPY_AND_ASSIGN(ScopedPixelStore); DISALLOW_COPY_AND_ASSIGN(ScopedPixelStore);
}; };
base::Optional<WGPUTextureFormat> GetWGPUFormat(viz::ResourceFormat format) {
switch (format) {
case viz::RED_8:
case viz::ALPHA_8:
case viz::LUMINANCE_8:
return WGPUTextureFormat_R8Unorm;
case viz::RG_88:
return WGPUTextureFormat_RG8Unorm;
case viz::RGBA_8888:
return WGPUTextureFormat_RGBA8Unorm;
case viz::BGRA_8888:
return WGPUTextureFormat_BGRA8Unorm;
default:
return {};
}
}
} // namespace } // namespace
// static // static
...@@ -262,10 +246,14 @@ std::unique_ptr<ExternalVkImageBacking> ExternalVkImageBacking::Create( ...@@ -262,10 +246,14 @@ std::unique_ptr<ExternalVkImageBacking> ExternalVkImageBacking::Create(
return nullptr; return nullptr;
} }
base::Optional<WGPUTextureFormat> wgpu_format = viz::ToWGPUFormat(format);
if (wgpu_format.value() == WGPUTextureFormat_Undefined) {
wgpu_format = base::nullopt;
}
auto backing = base::WrapUnique(new ExternalVkImageBacking( auto backing = base::WrapUnique(new ExternalVkImageBacking(
mailbox, format, size, color_space, usage, context_state, image, memory, mailbox, format, size, color_space, usage, context_state, image, memory,
requirements.size, vk_format, command_pool, GrVkYcbcrConversionInfo(), requirements.size, vk_format, command_pool, GrVkYcbcrConversionInfo(),
GetWGPUFormat(format), mem_alloc_info.memoryTypeIndex)); wgpu_format, mem_alloc_info.memoryTypeIndex));
if (!pixel_data.empty()) { if (!pixel_data.empty()) {
backing->WritePixels( backing->WritePixels(
...@@ -327,10 +315,15 @@ std::unique_ptr<ExternalVkImageBacking> ExternalVkImageBacking::CreateFromGMB( ...@@ -327,10 +315,15 @@ std::unique_ptr<ExternalVkImageBacking> ExternalVkImageBacking::CreateFromGMB(
->GetVulkanPhysicalDevice(), ->GetVulkanPhysicalDevice(),
vk_image_info.tiling, ycbcr_info); vk_image_info.tiling, ycbcr_info);
base::Optional<WGPUTextureFormat> wgpu_format =
viz::ToWGPUFormat(resource_format);
if (wgpu_format.value() == WGPUTextureFormat_Undefined) {
wgpu_format = base::nullopt;
}
return base::WrapUnique(new ExternalVkImageBacking( return base::WrapUnique(new ExternalVkImageBacking(
mailbox, resource_format, size, color_space, usage, context_state, mailbox, resource_format, size, color_space, usage, context_state,
vk_image, vk_device_memory, memory_size, vk_image_info.format, vk_image, vk_device_memory, memory_size, vk_image_info.format,
command_pool, gr_ycbcr_info, GetWGPUFormat(resource_format), {})); command_pool, gr_ycbcr_info, wgpu_format, {}));
} }
if (gfx::NumberOfPlanesForLinearBufferFormat(buffer_format) != 1) { if (gfx::NumberOfPlanesForLinearBufferFormat(buffer_format) != 1) {
......
...@@ -94,23 +94,6 @@ base::Optional<DXGI_FORMAT> VizFormatToDXGIFormat( ...@@ -94,23 +94,6 @@ base::Optional<DXGI_FORMAT> VizFormatToDXGIFormat(
} }
} }
#if BUILDFLAG(USE_DAWN)
base::Optional<WGPUTextureFormat> VizResourceFormatToWGPUTextureFormat(
viz::ResourceFormat viz_resource_format) {
switch (viz_resource_format) {
case viz::RGBA_F16:
return WGPUTextureFormat_RGBA16Float;
case viz::BGRA_8888:
return WGPUTextureFormat_BGRA8Unorm;
case viz::RGBA_8888:
return WGPUTextureFormat_RGBA8Unorm;
default:
NOTREACHED();
return {};
}
}
#endif // BUILDFLAG(USE_DAWN)
} // anonymous namespace } // anonymous namespace
// Representation of a SharedImageBackingD3D as a GL Texture. // Representation of a SharedImageBackingD3D as a GL Texture.
...@@ -449,9 +432,8 @@ WGPUTexture SharedImageRepresentationDawnD3D::BeginAccess( ...@@ -449,9 +432,8 @@ WGPUTexture SharedImageRepresentationDawnD3D::BeginAccess(
const HANDLE shared_handle = d3d_image_backing->GetSharedHandle(); const HANDLE shared_handle = d3d_image_backing->GetSharedHandle();
const viz::ResourceFormat viz_resource_format = d3d_image_backing->format(); const viz::ResourceFormat viz_resource_format = d3d_image_backing->format();
const base::Optional<WGPUTextureFormat> wgpu_texture_format = WGPUTextureFormat wgpu_format = viz::ToWGPUFormat(viz_resource_format);
VizResourceFormatToWGPUTextureFormat(viz_resource_format); if (wgpu_format == WGPUTextureFormat_Undefined) {
if (!wgpu_texture_format.has_value()) {
DLOG(ERROR) << "Unsupported viz format found: " << viz_resource_format; DLOG(ERROR) << "Unsupported viz format found: " << viz_resource_format;
return nullptr; return nullptr;
} }
...@@ -463,7 +445,7 @@ WGPUTexture SharedImageRepresentationDawnD3D::BeginAccess( ...@@ -463,7 +445,7 @@ WGPUTexture SharedImageRepresentationDawnD3D::BeginAccess(
WGPUTextureDescriptor desc; WGPUTextureDescriptor desc;
desc.nextInChain = nullptr; desc.nextInChain = nullptr;
desc.format = wgpu_texture_format.value(); desc.format = wgpu_format;
desc.usage = usage; desc.usage = usage;
desc.dimension = WGPUTextureDimension_2D; desc.dimension = WGPUTextureDimension_2D;
desc.size = {size().width(), size().height(), 1}; desc.size = {size().width(), size().height(), 1};
......
...@@ -88,22 +88,6 @@ void FlushIOSurfaceGLOperations() { ...@@ -88,22 +88,6 @@ void FlushIOSurfaceGLOperations() {
api->glFlushFn(); api->glFlushFn();
} }
base::Optional<WGPUTextureFormat> GetWGPUFormat(viz::ResourceFormat format) {
switch (format) {
case viz::RED_8:
case viz::ALPHA_8:
case viz::LUMINANCE_8:
return WGPUTextureFormat_R8Unorm;
case viz::RG_88:
return WGPUTextureFormat_RG8Unorm;
case viz::RGBA_8888:
case viz::BGRA_8888:
return WGPUTextureFormat_BGRA8Unorm;
default:
return {};
}
}
base::Optional<WGPUTextureFormat> GetWGPUFormat(gfx::BufferFormat format) { base::Optional<WGPUTextureFormat> GetWGPUFormat(gfx::BufferFormat format) {
switch (format) { switch (format) {
case gfx::BufferFormat::R_8: case gfx::BufferFormat::R_8:
...@@ -643,9 +627,18 @@ SharedImageBackingFactoryIOSurface::CreateSharedImage( ...@@ -643,9 +627,18 @@ SharedImageBackingFactoryIOSurface::CreateSharedImage(
gfx::IOSurfaceSetColorSpace(io_surface, color_space); gfx::IOSurfaceSetColorSpace(io_surface, color_space);
// OpenGL textures bound to IOSurfaces won't work on macOS unless the internal
// format is BGRA, so force a BGRA internal format for viz::RGBA_8888.
base::Optional<WGPUTextureFormat> wgpu_format =
format == viz::RGBA_8888 ? WGPUTextureFormat_BGRA8Unorm
: viz::ToWGPUFormat(format);
if (wgpu_format.value() == WGPUTextureFormat_Undefined) {
wgpu_format = base::nullopt;
}
return std::make_unique<SharedImageBackingIOSurface>( return std::make_unique<SharedImageBackingIOSurface>(
mailbox, format, size, color_space, usage, std::move(io_surface), mailbox, format, size, color_space, usage, std::move(io_surface),
GetWGPUFormat(format), estimated_size); wgpu_format, estimated_size);
} }
std::unique_ptr<SharedImageBacking> std::unique_ptr<SharedImageBacking>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment