1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
6 #define COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
7 
8 #include "build/build_config.h"
9 #include "components/viz/common/resources/resource_format.h"
10 #include "components/viz/common/viz_resource_format_export.h"
11 #include "gpu/vulkan/buildflags.h"
12 #include "skia/buildflags.h"
13 #include "third_party/dawn/src/include/dawn/webgpu.h"
14 #include "third_party/dawn/src/include/dawn/webgpu_cpp.h"
15 #include "third_party/skia/include/core/SkImageInfo.h"
16 #include "third_party/skia/include/gpu/GrTypes.h"
17 #include "ui/gfx/buffer_types.h"
18 
19 #if BUILDFLAG(ENABLE_VULKAN)
20 #include "third_party/vulkan_headers/include/vulkan/vulkan.h"  // nogncheck
21 #endif
22 
23 namespace viz {
24 
25 VIZ_RESOURCE_FORMAT_EXPORT SkColorType
26 ResourceFormatToClosestSkColorType(bool gpu_compositing, ResourceFormat format);
27 VIZ_RESOURCE_FORMAT_EXPORT int BitsPerPixel(ResourceFormat format);
28 VIZ_RESOURCE_FORMAT_EXPORT bool HasAlpha(ResourceFormat format);
29 
30 // The following functions use unsigned int instead of GLenum, since including
31 // third_party/khronos/GLES2/gl2.h causes redefinition errors as
32 // macros/functions defined in it conflict with macros/functions defined in
33 // ui/gl/gl_bindings.h. See http://crbug.com/512833 for more information.
34 VIZ_RESOURCE_FORMAT_EXPORT unsigned int GLDataType(ResourceFormat format);
35 VIZ_RESOURCE_FORMAT_EXPORT unsigned int GLDataFormat(ResourceFormat format);
36 VIZ_RESOURCE_FORMAT_EXPORT unsigned int GLInternalFormat(ResourceFormat format);
37 VIZ_RESOURCE_FORMAT_EXPORT unsigned int GLCopyTextureInternalFormat(
38     ResourceFormat format);
39 
40 // Returns the pixel format of the resource when mapped into client-side memory.
41 // Returns a default value when IsGpuMemoryBufferFormatSupported() returns false
42 // for a given format, as in this case the resource will not be mapped into
43 // client-side memory, and the returned value is not used.
44 VIZ_RESOURCE_FORMAT_EXPORT gfx::BufferFormat BufferFormat(
45     ResourceFormat format);
46 VIZ_RESOURCE_FORMAT_EXPORT bool IsResourceFormatCompressed(
47     ResourceFormat format);
48 VIZ_RESOURCE_FORMAT_EXPORT unsigned int TextureStorageFormat(
49     ResourceFormat format);
50 
51 // Returns whether the format can be used with GpuMemoryBuffer texture storage,
52 // allocated through TexStorage2DImageCHROMIUM.
53 VIZ_RESOURCE_FORMAT_EXPORT bool IsGpuMemoryBufferFormatSupported(
54     ResourceFormat format);
55 
56 // Returns whether the format can be used as a software bitmap for export to the
57 // display compositor.
58 VIZ_RESOURCE_FORMAT_EXPORT bool IsBitmapFormatSupported(ResourceFormat format);
59 
60 VIZ_RESOURCE_FORMAT_EXPORT ResourceFormat
61 GetResourceFormat(gfx::BufferFormat format);
62 
63 VIZ_RESOURCE_FORMAT_EXPORT bool GLSupportsFormat(ResourceFormat format);
64 
65 #if BUILDFLAG(ENABLE_VULKAN)
66 VIZ_RESOURCE_FORMAT_EXPORT bool HasVkFormat(ResourceFormat format);
67 VIZ_RESOURCE_FORMAT_EXPORT VkFormat ToVkFormat(ResourceFormat format);
68 #endif
69 
70 VIZ_RESOURCE_FORMAT_EXPORT wgpu::TextureFormat ToDawnFormat(
71     ResourceFormat format);
72 // Same as ToDawnFormat, except it casts from wgpu::TextureFormat to
73 // WGPUTextureFormat instead.
74 VIZ_RESOURCE_FORMAT_EXPORT WGPUTextureFormat
75 ToWGPUFormat(ResourceFormat format);
76 
77 #if defined(OS_APPLE)
78 VIZ_RESOURCE_FORMAT_EXPORT unsigned int ToMTLPixelFormat(ResourceFormat format);
79 #endif
80 
81 }  // namespace viz
82 
83 #endif  // COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_FORMAT_UTILS_H_
84