• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/gpu/GrBackendSurface.h"
9 #include "include/gpu/vk/GrVkBackendContext.h"
10 #include "include/gpu/vk/GrVkExtensions.h"
11 #include "src/gpu/GrRenderTarget.h"
12 #include "src/gpu/GrRenderTargetProxy.h"
13 #include "src/gpu/GrShaderCaps.h"
14 #include "src/gpu/GrUtil.h"
15 #include "src/gpu/SkGr.h"
16 #include "src/gpu/vk/GrVkCaps.h"
17 #include "src/gpu/vk/GrVkInterface.h"
18 #include "src/gpu/vk/GrVkTexture.h"
19 #include "src/gpu/vk/GrVkUniformHandler.h"
20 #include "src/gpu/vk/GrVkUtil.h"
21 
22 #ifdef SK_BUILD_FOR_ANDROID
23 #include <sys/system_properties.h>
24 #endif
25 
GrVkCaps(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t instanceVersion,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions,GrProtected isProtected)26 GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
27                    VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
28                    uint32_t instanceVersion, uint32_t physicalDeviceVersion,
29                    const GrVkExtensions& extensions, GrProtected isProtected)
30         : INHERITED(contextOptions) {
31     /**************************************************************************
32      * GrCaps fields
33      **************************************************************************/
34     fMipMapSupport = true;   // always available in Vulkan
35     fSRGBSupport = true;   // always available in Vulkan
36     fNPOTTextureTileSupport = true;  // always available in Vulkan
37     fReuseScratchTextures = true; //TODO: figure this out
38     fGpuTracingSupport = false; //TODO: figure this out
39     fOversizedStencilSupport = false; //TODO: figure this out
40     fInstanceAttribSupport = true;
41 
42     fSemaphoreSupport = true;   // always available in Vulkan
43     fFenceSyncSupport = true;   // always available in Vulkan
44     fCrossContextTextureSupport = true;
45     fHalfFloatVertexAttributeSupport = true;
46 
47     // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
48     fReadPixelsRowBytesSupport = true;
49     fWritePixelsRowBytesSupport = true;
50 
51     fTransferBufferSupport = true;
52 
53     fMaxRenderTargetSize = 4096; // minimum required by spec
54     fMaxTextureSize = 4096; // minimum required by spec
55 
56     fDynamicStateArrayGeometryProcessorTextureSupport = true;
57 
58     fShaderCaps.reset(new GrShaderCaps(contextOptions));
59 
60     this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions,
61                isProtected);
62 }
63 
64 namespace {
65 /**
66  * This comes from section 37.1.6 of the Vulkan spec. Format is
67  * (<bits>|<tag>)_<block_size>_<texels_per_block>.
68  */
69 enum class FormatCompatibilityClass {
70     k8_1_1,
71     k16_2_1,
72     k24_3_1,
73     k32_4_1,
74     k64_8_1,
75     k128_16_1,
76     kETC2_RGB_8_16,
77 };
78 }  // anonymous namespace
79 
format_compatibility_class(VkFormat format)80 static FormatCompatibilityClass format_compatibility_class(VkFormat format) {
81     switch (format) {
82         case VK_FORMAT_B8G8R8A8_UNORM:
83         case VK_FORMAT_R8G8B8A8_UNORM:
84         case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
85         case VK_FORMAT_R8G8B8A8_SRGB:
86         case VK_FORMAT_R16G16_UNORM:
87         case VK_FORMAT_R16G16_SFLOAT:
88             return FormatCompatibilityClass::k32_4_1;
89 
90         case VK_FORMAT_R8_UNORM:
91             return FormatCompatibilityClass::k8_1_1;
92 
93         case VK_FORMAT_R5G6B5_UNORM_PACK16:
94         case VK_FORMAT_R16_SFLOAT:
95         case VK_FORMAT_R8G8_UNORM:
96         case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
97         case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
98         case VK_FORMAT_R16_UNORM:
99             return FormatCompatibilityClass::k16_2_1;
100 
101         case VK_FORMAT_R16G16B16A16_SFLOAT:
102         case VK_FORMAT_R16G16B16A16_UNORM:
103             return FormatCompatibilityClass::k64_8_1;
104 
105         case VK_FORMAT_R8G8B8_UNORM:
106             return FormatCompatibilityClass::k24_3_1;
107 
108         case VK_FORMAT_R32G32B32A32_SFLOAT:
109             return FormatCompatibilityClass::k128_16_1;
110         case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
111             return FormatCompatibilityClass::kETC2_RGB_8_16;
112 
113         default:
114             SK_ABORT("Unsupported VkFormat");
115     }
116 }
117 
canCopyImage(VkFormat dstFormat,int dstSampleCnt,bool dstHasYcbcr,VkFormat srcFormat,int srcSampleCnt,bool srcHasYcbcr) const118 bool GrVkCaps::canCopyImage(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
119                             VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
120     if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
121         return false;
122     }
123 
124     if (dstHasYcbcr || srcHasYcbcr) {
125         return false;
126     }
127 
128     // We require that all Vulkan GrSurfaces have been created with transfer_dst and transfer_src
129     // as image usage flags.
130     return format_compatibility_class(srcFormat) == format_compatibility_class(dstFormat);
131 }
132 
canCopyAsBlit(VkFormat dstFormat,int dstSampleCnt,bool dstIsLinear,bool dstHasYcbcr,VkFormat srcFormat,int srcSampleCnt,bool srcIsLinear,bool srcHasYcbcr) const133 bool GrVkCaps::canCopyAsBlit(VkFormat dstFormat, int dstSampleCnt, bool dstIsLinear,
134                              bool dstHasYcbcr, VkFormat srcFormat, int srcSampleCnt,
135                              bool srcIsLinear, bool srcHasYcbcr) const {
136     // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
137     // as image usage flags.
138     if (!this->formatCanBeDstofBlit(dstFormat, dstIsLinear) ||
139         !this->formatCanBeSrcofBlit(srcFormat, srcIsLinear)) {
140         return false;
141     }
142 
143     // We cannot blit images that are multisampled. Will need to figure out if we can blit the
144     // resolved msaa though.
145     if (dstSampleCnt > 1 || srcSampleCnt > 1) {
146         return false;
147     }
148 
149     if (dstHasYcbcr || srcHasYcbcr) {
150         return false;
151     }
152 
153     return true;
154 }
155 
canCopyAsResolve(VkFormat dstFormat,int dstSampleCnt,bool dstHasYcbcr,VkFormat srcFormat,int srcSampleCnt,bool srcHasYcbcr) const156 bool GrVkCaps::canCopyAsResolve(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
157                                 VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
158     // The src surface must be multisampled.
159     if (srcSampleCnt <= 1) {
160         return false;
161     }
162 
163     // The dst must not be multisampled.
164     if (dstSampleCnt > 1) {
165         return false;
166     }
167 
168     // Surfaces must have the same format.
169     if (srcFormat != dstFormat) {
170         return false;
171     }
172 
173     if (dstHasYcbcr || srcHasYcbcr) {
174         return false;
175     }
176 
177     return true;
178 }
179 
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint) const180 bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
181                                 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
182     if (src->isProtected() && !dst->isProtected()) {
183         return false;
184     }
185 
186     // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
187     // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
188     // tracking of external or rectangle textures in GL? For now we don't create linear textures
189     // internally, and I don't believe anyone is wrapping them.
190     bool srcIsLinear = false;
191     bool dstIsLinear = false;
192 
193     int dstSampleCnt = 0;
194     int srcSampleCnt = 0;
195     if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
196         // Copying to or from render targets that wrap a secondary command buffer is not allowed
197         // since they would require us to know the VkImage, which we don't have, as well as need us
198         // to stop and start the VkRenderPass which we don't have access to.
199         if (rtProxy->wrapsVkSecondaryCB()) {
200             return false;
201         }
202         dstSampleCnt = rtProxy->numSamples();
203     }
204     if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
205         // Copying to or from render targets that wrap a secondary command buffer is not allowed
206         // since they would require us to know the VkImage, which we don't have, as well as need us
207         // to stop and start the VkRenderPass which we don't have access to.
208         if (rtProxy->wrapsVkSecondaryCB()) {
209             return false;
210         }
211         srcSampleCnt = rtProxy->numSamples();
212     }
213     SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
214     SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
215 
216     bool dstHasYcbcr = false;
217     if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
218         if (ycbcr->isValid()) {
219             dstHasYcbcr = true;
220         }
221     }
222 
223     bool srcHasYcbcr = false;
224     if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
225         if (ycbcr->isValid()) {
226             srcHasYcbcr = true;
227         }
228     }
229 
230     VkFormat dstFormat, srcFormat;
231     SkAssertResult(dst->backendFormat().asVkFormat(&dstFormat));
232     SkAssertResult(src->backendFormat().asVkFormat(&srcFormat));
233 
234     return this->canCopyImage(dstFormat, dstSampleCnt, dstHasYcbcr,
235                               srcFormat, srcSampleCnt, srcHasYcbcr) ||
236            this->canCopyAsBlit(dstFormat, dstSampleCnt, dstIsLinear, dstHasYcbcr,
237                                srcFormat, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
238            this->canCopyAsResolve(dstFormat, dstSampleCnt, dstHasYcbcr,
239                                   srcFormat, srcSampleCnt, srcHasYcbcr);
240 }
241 
get_extension_feature_struct(const VkPhysicalDeviceFeatures2 & features,VkStructureType type)242 template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
243                                                      VkStructureType type) {
244     // All Vulkan structs that could be part of the features chain will start with the
245     // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
246     // so we can get access to the pNext for the next struct.
247     struct CommonVulkanHeader {
248         VkStructureType sType;
249         void*           pNext;
250     };
251 
252     void* pNext = features.pNext;
253     while (pNext) {
254         CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
255         if (header->sType == type) {
256             return static_cast<T*>(pNext);
257         }
258         pNext = header->pNext;
259     }
260     return nullptr;
261 }
262 
init(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions,GrProtected isProtected)263 void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
264                     VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
265                     uint32_t physicalDeviceVersion, const GrVkExtensions& extensions,
266                     GrProtected isProtected) {
267     VkPhysicalDeviceProperties properties;
268     GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
269 
270     VkPhysicalDeviceMemoryProperties memoryProperties;
271     GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
272 
273     SkASSERT(physicalDeviceVersion <= properties.apiVersion);
274 
275     if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
276         fSupportsSwapchain = true;
277     }
278 
279     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
280         extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
281         fSupportsPhysicalDeviceProperties2 = true;
282     }
283 
284     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
285         extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
286         fSupportsMemoryRequirements2 = true;
287     }
288 
289     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
290         extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
291         fSupportsBindMemory2 = true;
292     }
293 
294     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
295         extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
296         fSupportsMaintenance1 = true;
297     }
298 
299     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
300         extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
301         fSupportsMaintenance2 = true;
302     }
303 
304     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
305         extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
306         fSupportsMaintenance3 = true;
307     }
308 
309     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
310         (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
311          this->supportsMemoryRequirements2())) {
312         fSupportsDedicatedAllocation = true;
313     }
314 
315     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
316         (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
317          this->supportsPhysicalDeviceProperties2() &&
318          extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
319          this->supportsDedicatedAllocation())) {
320         fSupportsExternalMemory = true;
321     }
322 
323 #ifdef SK_BUILD_FOR_ANDROID
324     // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
325     // do we don't explicitly require it here even the spec says it is required.
326     if (extensions.hasExtension(
327             VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
328        /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
329         this->supportsExternalMemory() &&
330         this->supportsBindMemory2()) {
331         fSupportsAndroidHWBExternalMemory = true;
332         fSupportsAHardwareBufferImages = true;
333     }
334 #endif
335 
336     auto ycbcrFeatures =
337             get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
338                     features, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
339     if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
340         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
341          (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
342           this->supportsMaintenance1() && this->supportsBindMemory2() &&
343           this->supportsMemoryRequirements2() && this->supportsPhysicalDeviceProperties2()))) {
344         fSupportsYcbcrConversion = true;
345     }
346 
347     // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
348     // will return a key of 0.
349     fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
350 
351     if ((isProtected == GrProtected::kYes) &&
352         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0))) {
353         fSupportsProtectedMemory = true;
354         fAvoidUpdateBuffers = true;
355         fShouldAlwaysUseDedicatedImageMemory = true;
356     }
357 
358     this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
359     this->initShaderCaps(properties, features);
360 
361     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
362 #if defined(SK_CPU_X86)
363         // We need to do this before initing the config table since it uses fSRGBSupport
364         if (kImagination_VkVendor == properties.vendorID) {
365             fSRGBSupport = false;
366         }
367 #endif
368     }
369 
370     if (kQualcomm_VkVendor == properties.vendorID) {
371         // A "clear" load for the CCPR atlas runs faster on QC than a "discard" load followed by a
372         // scissored clear.
373         // On NVIDIA and Intel, the discard load followed by clear is faster.
374         // TODO: Evaluate on ARM, Imagination, and ATI.
375         fPreferFullscreenClears = true;
376     }
377 
378     if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
379         // On Qualcomm and ARM mapping a gpu buffer and doing both reads and writes to it is slow.
380         // Thus for index and vertex buffers we will force to use a cpu side buffer and then copy
381         // the whole buffer up to the gpu.
382         fBufferMapThreshold = SK_MaxS32;
383     }
384 
385     if (kQualcomm_VkVendor == properties.vendorID) {
386         // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
387         // even for small sizes.
388         fAvoidUpdateBuffers = true;
389     }
390 
391     if (kARM_VkVendor == properties.vendorID) {
392         // ARM seems to do better with more fine triangles as opposed to using the sample mask.
393         // (At least in our current round rect op.)
394         fPreferTrianglesOverSampleMask = true;
395     }
396 
397     this->initFormatTable(vkInterface, physDev, properties);
398     this->initStencilFormat(vkInterface, physDev);
399 
400     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
401         this->applyDriverCorrectnessWorkarounds(properties);
402     }
403 
404     this->applyOptionsOverrides(contextOptions);
405     fShaderCaps->applyOptionsOverrides(contextOptions);
406 }
407 
applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties & properties)408 void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
409     if (kQualcomm_VkVendor == properties.vendorID) {
410         fMustDoCopiesFromOrigin = true;
411         // Transfer doesn't support this workaround.
412         fTransferBufferSupport = false;
413     }
414 
415 #if defined(SK_BUILD_FOR_WIN)
416     if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
417         fMustSleepOnTearDown = true;
418     }
419 #elif defined(SK_BUILD_FOR_ANDROID)
420     if (kImagination_VkVendor == properties.vendorID) {
421         fMustSleepOnTearDown = true;
422     }
423 #endif
424 
425 #if defined(SK_BUILD_FOR_ANDROID)
426     // Protected memory features have problems in Android P and earlier.
427     if (fSupportsProtectedMemory && (kQualcomm_VkVendor == properties.vendorID)) {
428         char androidAPIVersion[PROP_VALUE_MAX];
429         int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersion);
430         if (strLength == 0 || atoi(androidAPIVersion) <= 28) {
431             fSupportsProtectedMemory = false;
432         }
433     }
434 #endif
435 
436     // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
437     if (kARM_VkVendor == properties.vendorID) {
438         fShouldAlwaysUseDedicatedImageMemory = true;
439     }
440 
441     ////////////////////////////////////////////////////////////////////////////
442     // GrCaps workarounds
443     ////////////////////////////////////////////////////////////////////////////
444 
445     if (kARM_VkVendor == properties.vendorID) {
446         fInstanceAttribSupport = false;
447         fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
448     }
449 
450     // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
451     if (kAMD_VkVendor == properties.vendorID) {
452         fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
453     }
454 
455     ////////////////////////////////////////////////////////////////////////////
456     // GrShaderCaps workarounds
457     ////////////////////////////////////////////////////////////////////////////
458 
459     if (kImagination_VkVendor == properties.vendorID) {
460         fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
461     }
462 }
463 
get_max_sample_count(VkSampleCountFlags flags)464 int get_max_sample_count(VkSampleCountFlags flags) {
465     SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
466     if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
467         return 0;
468     }
469     if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
470         return 2;
471     }
472     if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
473         return 4;
474     }
475     if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
476         return 8;
477     }
478     if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
479         return 16;
480     }
481     if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
482         return 32;
483     }
484     return 64;
485 }
486 
initGrCaps(const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceMemoryProperties & memoryProperties,const VkPhysicalDeviceFeatures2 & features,const GrVkExtensions & extensions)487 void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
488                           VkPhysicalDevice physDev,
489                           const VkPhysicalDeviceProperties& properties,
490                           const VkPhysicalDeviceMemoryProperties& memoryProperties,
491                           const VkPhysicalDeviceFeatures2& features,
492                           const GrVkExtensions& extensions) {
493     // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
494     // need for us ever to support that amount, and it makes tests which tests all the vertex
495     // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
496     // we ever find that need.
497     static const uint32_t kMaxVertexAttributes = 64;
498     fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
499 
500     // We could actually query and get a max size for each config, however maxImageDimension2D will
501     // give the minimum max size across all configs. So for simplicity we will use that for now.
502     fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
503     fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
504     if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
505         fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
506     }
507     // Our render targets are always created with textures as the color
508     // attachment, hence this min:
509     fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
510 
511     // TODO: check if RT's larger than 4k incur a performance cost on ARM.
512     fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
513 
514     // Assuming since we will always map in the end to upload the data we might as well just map
515     // from the get go. There is no hard data to suggest this is faster or slower.
516     fBufferMapThreshold = 0;
517 
518     fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
519 
520     fOversizedStencilSupport = true;
521 
522     if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
523         this->supportsPhysicalDeviceProperties2()) {
524 
525         VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
526         blendProps.sType =
527                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
528         blendProps.pNext = nullptr;
529 
530         VkPhysicalDeviceProperties2 props;
531         props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
532         props.pNext = &blendProps;
533 
534         GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
535 
536         if (blendProps.advancedBlendAllOperations == VK_TRUE) {
537             fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
538 
539             auto blendFeatures =
540                 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
541                     features,
542                     VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
543             if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
544                 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
545             } else {
546                 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
547                 // require us to support self dependencies in our render passes.
548                 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
549             }
550         }
551     }
552 }
553 
initShaderCaps(const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceFeatures2 & features)554 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
555                               const VkPhysicalDeviceFeatures2& features) {
556     GrShaderCaps* shaderCaps = fShaderCaps.get();
557     shaderCaps->fVersionDeclString = "#version 330\n";
558 
559     // Vulkan is based off ES 3.0 so the following should all be supported
560     shaderCaps->fUsesPrecisionModifiers = true;
561     shaderCaps->fFlatInterpolationSupport = true;
562     // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
563     // to be true with Vulkan as well.
564     shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
565 
566     // GrShaderCaps
567 
568     shaderCaps->fShaderDerivativeSupport = true;
569 
570     // FIXME: http://skbug.com/7733: Disable geometry shaders until Intel/Radeon GMs draw correctly.
571     // shaderCaps->fGeometryShaderSupport =
572     //         shaderCaps->fGSInvocationsSupport = features.features.geometryShader;
573 
574     shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
575 
576     shaderCaps->fIntegerSupport = true;
577     shaderCaps->fVertexIDSupport = true;
578     shaderCaps->fFPManipulationSupport = true;
579 
580     // Assume the minimum precisions mandated by the SPIR-V spec.
581     shaderCaps->fFloatIs32Bits = true;
582     shaderCaps->fHalfIs32Bits = false;
583 
584     shaderCaps->fMaxFragmentSamplers = SkTMin(
585                                        SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
586                                               properties.limits.maxPerStageDescriptorSamplers),
587                                               (uint32_t)INT_MAX);
588 }
589 
stencil_format_supported(const GrVkInterface * interface,VkPhysicalDevice physDev,VkFormat format)590 bool stencil_format_supported(const GrVkInterface* interface,
591                               VkPhysicalDevice physDev,
592                               VkFormat format) {
593     VkFormatProperties props;
594     memset(&props, 0, sizeof(VkFormatProperties));
595     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
596     return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
597 }
598 
initStencilFormat(const GrVkInterface * interface,VkPhysicalDevice physDev)599 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
600     // List of legal stencil formats (though perhaps not supported on
601     // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
602     // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
603     // can optionally have 24 unused bits at the end so we assume the total bits is 64.
604     static const StencilFormat
605                   // internal Format             stencil bits      total bits        packed?
606         gS8    = { VK_FORMAT_S8_UINT,            8,                 8,               false },
607         gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT,  8,                32,               true },
608         gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8,                64,               true };
609 
610     if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
611         fPreferredStencilFormat = gS8;
612     } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
613         fPreferredStencilFormat = gD24S8;
614     } else {
615         SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
616         fPreferredStencilFormat = gD32S8;
617     }
618 }
619 
format_is_srgb(VkFormat format)620 static bool format_is_srgb(VkFormat format) {
621     SkASSERT(GrVkFormatIsSupported(format));
622 
623     switch (format) {
624         case VK_FORMAT_R8G8B8A8_SRGB:
625             return true;
626         default:
627             return false;
628     }
629 }
630 
631 // These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
632 // frequently used to least to improve look up times in arrays.
633 static constexpr VkFormat kVkFormats[] = {
634     VK_FORMAT_R8G8B8A8_UNORM,
635     VK_FORMAT_R8_UNORM,
636     VK_FORMAT_B8G8R8A8_UNORM,
637     VK_FORMAT_R5G6B5_UNORM_PACK16,
638     VK_FORMAT_R16G16B16A16_SFLOAT,
639     VK_FORMAT_R16_SFLOAT,
640     VK_FORMAT_R8G8B8_UNORM,
641     VK_FORMAT_R8G8_UNORM,
642     VK_FORMAT_A2B10G10R10_UNORM_PACK32,
643     VK_FORMAT_B4G4R4A4_UNORM_PACK16,
644     VK_FORMAT_R4G4B4A4_UNORM_PACK16,
645     VK_FORMAT_R32G32B32A32_SFLOAT,
646     VK_FORMAT_R8G8B8A8_SRGB,
647     VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
648     VK_FORMAT_R16_UNORM,
649     VK_FORMAT_R16G16_UNORM,
650     VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
651     VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
652     // Experimental (for Y416 and mutant P016/P010)
653     VK_FORMAT_R16G16B16A16_UNORM,
654     VK_FORMAT_R16G16_SFLOAT,
655 };
656 
setColorType(GrColorType colorType,std::initializer_list<VkFormat> formats)657 void GrVkCaps::setColorType(GrColorType colorType, std::initializer_list<VkFormat> formats) {
658 #ifdef SK_DEBUG
659     for (size_t i = 0; i < kNumVkFormats; ++i) {
660         const auto& formatInfo = fFormatTable[i];
661         for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
662             const auto& ctInfo = formatInfo.fColorTypeInfos[j];
663             if (ctInfo.fColorType == colorType &&
664                 !SkToBool(ctInfo.fFlags & ColorTypeInfo::kWrappedOnly_Flag)) {
665                 bool found = false;
666                 for (auto it = formats.begin(); it != formats.end(); ++it) {
667                     if (kVkFormats[i] == *it) {
668                         found = true;
669                     }
670                 }
671                 SkASSERT(found);
672             }
673         }
674     }
675 #endif
676     int idx = static_cast<int>(colorType);
677     for (auto it = formats.begin(); it != formats.end(); ++it) {
678         const auto& info = this->getFormatInfo(*it);
679         for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
680             if (info.fColorTypeInfos[i].fColorType == colorType) {
681                 fColorTypeToFormatTable[idx] = *it;
682                 return;
683             }
684         }
685     }
686 }
687 
getFormatInfo(VkFormat format) const688 const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
689     GrVkCaps* nonConstThis = const_cast<GrVkCaps*>(this);
690     return nonConstThis->getFormatInfo(format);
691 }
692 
getFormatInfo(VkFormat format)693 GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) {
694     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
695                   "Size of VkFormats array must match static value in header");
696     for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
697         if (kVkFormats[i] == format) {
698             return fFormatTable[i];
699         }
700     }
701     static FormatInfo kInvalidFormat;
702     return kInvalidFormat;
703 }
704 
initFormatTable(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties)705 void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
706                                const VkPhysicalDeviceProperties& properties) {
707     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
708                   "Size of VkFormats array must match static value in header");
709 
710     std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, VK_FORMAT_UNDEFINED);
711 
712     // Go through all the formats and init their support surface and data GrColorTypes.
713     // Format: VK_FORMAT_R8G8B8A8_UNORM
714     {
715         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
716         auto& info = this->getFormatInfo(format);
717         info.init(interface, physDev, properties, format);
718         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
719             info.fColorTypeInfoCount = 2;
720             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
721             int ctIdx = 0;
722             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
723             {
724                 constexpr GrColorType ct = GrColorType::kRGBA_8888;
725                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
726                 ctInfo.fColorType = ct;
727                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
728             }
729             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
730             {
731                 constexpr GrColorType ct = GrColorType::kRGB_888x;
732                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
733                 ctInfo.fColorType = ct;
734                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
735                 ctInfo.fTextureSwizzle = GrSwizzle::RGB1();
736             }
737         }
738     }
739 
740     // Format: VK_FORMAT_R8_UNORM
741     {
742         constexpr VkFormat format = VK_FORMAT_R8_UNORM;
743         auto& info = this->getFormatInfo(format);
744         info.init(interface, physDev, properties, format);
745         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
746             info.fColorTypeInfoCount = 2;
747             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
748             int ctIdx = 0;
749             // Format: VK_FORMAT_R8_UNORM, Surface: kAlpha_8
750             {
751                 constexpr GrColorType ct = GrColorType::kAlpha_8;
752                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
753                 ctInfo.fColorType = ct;
754                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
755                 ctInfo.fTextureSwizzle = GrSwizzle::RRRR();
756                 ctInfo.fOutputSwizzle = GrSwizzle::AAAA();
757             }
758             // Format: VK_FORMAT_R8_UNORM, Surface: kGray_8
759             {
760                 constexpr GrColorType ct = GrColorType::kGray_8;
761                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
762                 ctInfo.fColorType = ct;
763                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
764                 ctInfo.fTextureSwizzle = GrSwizzle("rrr1");
765             }
766         }
767     }
768     // Format: VK_FORMAT_B8G8R8A8_UNORM
769     {
770         constexpr VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
771         auto& info = this->getFormatInfo(format);
772         info.init(interface, physDev, properties, format);
773         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
774             info.fColorTypeInfoCount = 1;
775             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
776             int ctIdx = 0;
777             // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
778             {
779                 constexpr GrColorType ct = GrColorType::kBGRA_8888;
780                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
781                 ctInfo.fColorType = ct;
782                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
783             }
784         }
785     }
786     // Format: VK_FORMAT_R5G6B5_UNORM_PACK16
787     {
788         constexpr VkFormat format = VK_FORMAT_R5G6B5_UNORM_PACK16;
789         auto& info = this->getFormatInfo(format);
790         info.init(interface, physDev, properties, format);
791         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
792             info.fColorTypeInfoCount = 1;
793             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
794             int ctIdx = 0;
795             // Format: VK_FORMAT_R5G6B5_UNORM_PACK16, Surface: kBGR_565
796             {
797                 constexpr GrColorType ct = GrColorType::kBGR_565;
798                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
799                 ctInfo.fColorType = ct;
800                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
801             }
802         }
803     }
804     // Format: VK_FORMAT_R16G16B16A16_SFLOAT
805     {
806         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_SFLOAT;
807         auto& info = this->getFormatInfo(format);
808         info.init(interface, physDev, properties, format);
809         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
810             info.fColorTypeInfoCount = 2;
811             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
812             int ctIdx = 0;
813             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16
814             {
815                 constexpr GrColorType ct = GrColorType::kRGBA_F16;
816                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
817                 ctInfo.fColorType = ct;
818                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
819             }
820             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16_Clamped
821             {
822                 constexpr GrColorType ct = GrColorType::kRGBA_F16_Clamped;
823                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
824                 ctInfo.fColorType = ct;
825                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
826             }
827         }
828     }
829     // Format: VK_FORMAT_R16_SFLOAT
830     {
831         constexpr VkFormat format = VK_FORMAT_R16_SFLOAT;
832         auto& info = this->getFormatInfo(format);
833         info.init(interface, physDev, properties, format);
834         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
835             info.fColorTypeInfoCount = 1;
836             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
837             int ctIdx = 0;
838             // Format: VK_FORMAT_R16_SFLOAT, Surface: kAlpha_F16
839             {
840                 constexpr GrColorType ct = GrColorType::kAlpha_F16;
841                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
842                 ctInfo.fColorType = ct;
843                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
844                 ctInfo.fTextureSwizzle = GrSwizzle::RRRR();
845                 ctInfo.fOutputSwizzle = GrSwizzle::AAAA();
846             }
847         }
848     }
849     // Format: VK_FORMAT_R8G8B8_UNORM
850     {
851         constexpr VkFormat format = VK_FORMAT_R8G8B8_UNORM;
852         auto& info = this->getFormatInfo(format);
853         info.init(interface, physDev, properties, format);
854         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
855             info.fColorTypeInfoCount = 1;
856             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
857             int ctIdx = 0;
858             // Format: VK_FORMAT_R8G8B8_UNORM, Surface: kRGB_888x
859             {
860                 constexpr GrColorType ct = GrColorType::kRGB_888x;
861                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
862                 ctInfo.fColorType = ct;
863                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
864             }
865         }
866     }
867     // Format: VK_FORMAT_R8G8_UNORM
868     {
869         constexpr VkFormat format = VK_FORMAT_R8G8_UNORM;
870         auto& info = this->getFormatInfo(format);
871         info.init(interface, physDev, properties, format);
872         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
873             info.fColorTypeInfoCount = 1;
874             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
875             int ctIdx = 0;
876             // Format: VK_FORMAT_R8G8_UNORM, Surface: kRG_88
877             {
878                 constexpr GrColorType ct = GrColorType::kRG_88;
879                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
880                 ctInfo.fColorType = ct;
881                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
882             }
883         }
884     }
885     // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32
886     {
887         constexpr VkFormat format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
888         auto& info = this->getFormatInfo(format);
889         info.init(interface, physDev, properties, format);
890         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
891             info.fColorTypeInfoCount = 1;
892             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
893             int ctIdx = 0;
894             // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGBA_1010102
895             {
896                 constexpr GrColorType ct = GrColorType::kRGBA_1010102;
897                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
898                 ctInfo.fColorType = ct;
899                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
900             }
901         }
902     }
903     // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16
904     {
905         constexpr VkFormat format = VK_FORMAT_B4G4R4A4_UNORM_PACK16;
906         auto& info = this->getFormatInfo(format);
907         info.init(interface, physDev, properties, format);
908         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
909             info.fColorTypeInfoCount = 1;
910             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
911             int ctIdx = 0;
912             // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16, Surface: kABGR_4444
913             {
914                 constexpr GrColorType ct = GrColorType::kABGR_4444;
915                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
916                 ctInfo.fColorType = ct;
917                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
918                 ctInfo.fTextureSwizzle = GrSwizzle::BGRA();
919                 ctInfo.fOutputSwizzle = GrSwizzle::BGRA();
920             }
921         }
922     }
923     // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16
924     {
925         constexpr VkFormat format = VK_FORMAT_R4G4B4A4_UNORM_PACK16;
926         auto& info = this->getFormatInfo(format);
927         info.init(interface, physDev, properties, format);
928         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
929             info.fColorTypeInfoCount = 1;
930             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
931             int ctIdx = 0;
932             // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16, Surface: kABGR_4444
933             {
934                 constexpr GrColorType ct = GrColorType::kABGR_4444;
935                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
936                 ctInfo.fColorType = ct;
937                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
938             }
939         }
940     }
941     // Format: VK_FORMAT_R32G32B32A32_SFLOAT
942     {
943         constexpr VkFormat format = VK_FORMAT_R32G32B32A32_SFLOAT;
944         auto& info = this->getFormatInfo(format);
945         info.init(interface, physDev, properties, format);
946         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
947             info.fColorTypeInfoCount = 1;
948             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
949             int ctIdx = 0;
950             // Format: VK_FORMAT_R32G32B32A32_SFLOAT, Surface: kRGBA_F32
951             {
952                 constexpr GrColorType ct = GrColorType::kRGBA_F32;
953                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
954                 ctInfo.fColorType = ct;
955                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
956             }
957         }
958     }
959     // Format: VK_FORMAT_R8G8B8A8_SRGB
960     {
961         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
962         auto& info = this->getFormatInfo(format);
963         if (fSRGBSupport) {
964             info.init(interface, physDev, properties, format);
965         }
966         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
967             info.fColorTypeInfoCount = 1;
968             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
969             int ctIdx = 0;
970             // Format: VK_FORMAT_R8G8B8A8_SRGB, Surface: kRGBA_8888_SRGB
971             {
972                 constexpr GrColorType ct = GrColorType::kRGBA_8888_SRGB;
973                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
974                 ctInfo.fColorType = ct;
975                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
976             }
977         }
978     }
979     // Format: VK_FORMAT_R16_UNORM
980     {
981         constexpr VkFormat format = VK_FORMAT_R16_UNORM;
982         auto& info = this->getFormatInfo(format);
983         info.init(interface, physDev, properties, format);
984         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
985             info.fColorTypeInfoCount = 1;
986             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
987             int ctIdx = 0;
988             // Format: VK_FORMAT_R16_UNORM, Surface: kR_16
989             {
990                 constexpr GrColorType ct = GrColorType::kR_16;
991                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
992                 ctInfo.fColorType = ct;
993                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
994             }
995         }
996     }
997     // Format: VK_FORMAT_R16G16_UNORM
998     {
999         constexpr VkFormat format = VK_FORMAT_R16G16_UNORM;
1000         auto& info = this->getFormatInfo(format);
1001         info.init(interface, physDev, properties, format);
1002         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1003             info.fColorTypeInfoCount = 1;
1004             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1005             int ctIdx = 0;
1006             // Format: VK_FORMAT_R16G16_UNORM, Surface: kRG_1616
1007             {
1008                 constexpr GrColorType ct = GrColorType::kRG_1616;
1009                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1010                 ctInfo.fColorType = ct;
1011                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1012             }
1013         }
1014     }
1015     // Format: VK_FORMAT_R16G16B16A16_UNORM
1016     {
1017         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_UNORM;
1018         auto& info = this->getFormatInfo(format);
1019         info.init(interface, physDev, properties, format);
1020         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1021             info.fColorTypeInfoCount = 1;
1022             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1023             int ctIdx = 0;
1024             // Format: VK_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
1025             {
1026                 constexpr GrColorType ct = GrColorType::kRGBA_16161616;
1027                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1028                 ctInfo.fColorType = ct;
1029                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1030             }
1031         }
1032     }
1033     // Format: VK_FORMAT_R16G16_SFLOAT
1034     {
1035         constexpr VkFormat format = VK_FORMAT_R16G16_SFLOAT;
1036         auto& info = this->getFormatInfo(format);
1037         info.init(interface, physDev, properties, format);
1038         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1039             info.fColorTypeInfoCount = 1;
1040             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1041             int ctIdx = 0;
1042             // Format: VK_FORMAT_R16G16_SFLOAT, Surface: kRG_F16
1043             {
1044                 constexpr GrColorType ct = GrColorType::kRG_F16;
1045                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1046                 ctInfo.fColorType = ct;
1047                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1048             }
1049         }
1050     }
1051     // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
1052     {
1053         constexpr VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
1054         auto& info = this->getFormatInfo(format);
1055         if (fSupportsYcbcrConversion) {
1056             info.init(interface, physDev, properties, format);
1057         }
1058         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1059             info.fColorTypeInfoCount = 1;
1060             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1061             int ctIdx = 0;
1062             // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, Surface: kRGB_888x
1063             {
1064                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1065                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1066                 ctInfo.fColorType = ct;
1067                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1068             }
1069         }
1070     }
1071     // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
1072     {
1073         constexpr VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
1074         auto& info = this->getFormatInfo(format);
1075         if (fSupportsYcbcrConversion) {
1076             info.init(interface, physDev, properties, format);
1077         }
1078         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1079             info.fColorTypeInfoCount = 1;
1080             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1081             int ctIdx = 0;
1082             // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, Surface: kRGB_888x
1083             {
1084                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1085                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1086                 ctInfo.fColorType = ct;
1087                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1088             }
1089         }
1090     }
1091     // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
1092     {
1093         constexpr VkFormat format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1094         auto& info = this->getFormatInfo(format);
1095         info.init(interface, physDev, properties, format);
1096         // No supported GrColorTypes.
1097     }
1098 
1099     ////////////////////////////////////////////////////////////////////////////
1100     // Map GrColorTypes (used for creating GrSurfaces) to VkFormats. The order in which the formats
1101     // are passed into the setColorType function indicates the priority in selecting which format
1102     // we use for a given GrcolorType.
1103 
1104     this->setColorType(GrColorType::kAlpha_8,          { VK_FORMAT_R8_UNORM });
1105     this->setColorType(GrColorType::kBGR_565,          { VK_FORMAT_R5G6B5_UNORM_PACK16 });
1106     this->setColorType(GrColorType::kABGR_4444,        { VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1107                                                          VK_FORMAT_B4G4R4A4_UNORM_PACK16 });
1108     this->setColorType(GrColorType::kRGBA_8888,        { VK_FORMAT_R8G8B8A8_UNORM });
1109     this->setColorType(GrColorType::kRGBA_8888_SRGB,   { VK_FORMAT_R8G8B8A8_SRGB });
1110     this->setColorType(GrColorType::kRGB_888x,         { VK_FORMAT_R8G8B8_UNORM,
1111                                                          VK_FORMAT_R8G8B8A8_UNORM });
1112     this->setColorType(GrColorType::kRG_88,            { VK_FORMAT_R8G8_UNORM });
1113     this->setColorType(GrColorType::kBGRA_8888,        { VK_FORMAT_B8G8R8A8_UNORM });
1114     this->setColorType(GrColorType::kRGBA_1010102,     { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1115     this->setColorType(GrColorType::kGray_8,           { VK_FORMAT_R8_UNORM });
1116     this->setColorType(GrColorType::kAlpha_F16,        { VK_FORMAT_R16_SFLOAT });
1117     this->setColorType(GrColorType::kRGBA_F16,         { VK_FORMAT_R16G16B16A16_SFLOAT });
1118     this->setColorType(GrColorType::kRGBA_F16_Clamped, { VK_FORMAT_R16G16B16A16_SFLOAT });
1119     this->setColorType(GrColorType::kRGBA_F32,         { VK_FORMAT_R32G32B32A32_SFLOAT });
1120     this->setColorType(GrColorType::kR_16,             { VK_FORMAT_R16_UNORM });
1121     this->setColorType(GrColorType::kRG_1616,          { VK_FORMAT_R16G16_UNORM });
1122     this->setColorType(GrColorType::kRGBA_16161616,    { VK_FORMAT_R16G16B16A16_UNORM });
1123     this->setColorType(GrColorType::kRG_F16,           { VK_FORMAT_R16G16_SFLOAT });
1124 }
1125 
InitFormatFlags(VkFormatFeatureFlags vkFlags,uint16_t * flags)1126 void GrVkCaps::FormatInfo::InitFormatFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
1127     if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
1128         SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
1129         *flags = *flags | kTexturable_Flag;
1130 
1131         // Ganesh assumes that all renderable surfaces are also texturable
1132         if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
1133             *flags = *flags | kRenderable_Flag;
1134         }
1135     }
1136 
1137     if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
1138         *flags = *flags | kBlitSrc_Flag;
1139     }
1140 
1141     if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
1142         *flags = *flags | kBlitDst_Flag;
1143     }
1144 }
1145 
initSampleCounts(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & physProps,VkFormat format)1146 void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
1147                                             VkPhysicalDevice physDev,
1148                                             const VkPhysicalDeviceProperties& physProps,
1149                                             VkFormat format) {
1150     VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1151                               VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1152                               VK_IMAGE_USAGE_SAMPLED_BIT |
1153                               VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1154     VkImageFormatProperties properties;
1155     GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
1156                                                                  format,
1157                                                                  VK_IMAGE_TYPE_2D,
1158                                                                  VK_IMAGE_TILING_OPTIMAL,
1159                                                                  usage,
1160                                                                  0,  // createFlags
1161                                                                  &properties));
1162     VkSampleCountFlags flags = properties.sampleCounts;
1163     if (flags & VK_SAMPLE_COUNT_1_BIT) {
1164         fColorSampleCounts.push_back(1);
1165     }
1166     if (kImagination_VkVendor == physProps.vendorID) {
1167         // MSAA does not work on imagination
1168         return;
1169     }
1170     if (kIntel_VkVendor == physProps.vendorID) {
1171         // MSAA doesn't work well on Intel GPUs chromium:527565, chromium:983926
1172         return;
1173     }
1174     if (flags & VK_SAMPLE_COUNT_2_BIT) {
1175         fColorSampleCounts.push_back(2);
1176     }
1177     if (flags & VK_SAMPLE_COUNT_4_BIT) {
1178         fColorSampleCounts.push_back(4);
1179     }
1180     if (flags & VK_SAMPLE_COUNT_8_BIT) {
1181         fColorSampleCounts.push_back(8);
1182     }
1183     if (flags & VK_SAMPLE_COUNT_16_BIT) {
1184         fColorSampleCounts.push_back(16);
1185     }
1186     if (flags & VK_SAMPLE_COUNT_32_BIT) {
1187         fColorSampleCounts.push_back(32);
1188     }
1189     if (flags & VK_SAMPLE_COUNT_64_BIT) {
1190         fColorSampleCounts.push_back(64);
1191     }
1192 }
1193 
init(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,VkFormat format)1194 void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
1195                                 VkPhysicalDevice physDev,
1196                                 const VkPhysicalDeviceProperties& properties,
1197                                 VkFormat format) {
1198     VkFormatProperties props;
1199     memset(&props, 0, sizeof(VkFormatProperties));
1200     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
1201     InitFormatFlags(props.linearTilingFeatures, &fLinearFlags);
1202     InitFormatFlags(props.optimalTilingFeatures, &fOptimalFlags);
1203     if (fOptimalFlags & kRenderable_Flag) {
1204         this->initSampleCounts(interface, physDev, properties, format);
1205     }
1206 }
1207 
isFormatSRGB(const GrBackendFormat & format) const1208 bool GrVkCaps::isFormatSRGB(const GrBackendFormat& format) const {
1209     VkFormat vkFormat;
1210     if (!format.asVkFormat(&vkFormat)) {
1211         return false;
1212     }
1213 
1214     return format_is_srgb(vkFormat);
1215 }
1216 
isFormatCompressed(const GrBackendFormat & format) const1217 bool GrVkCaps::isFormatCompressed(const GrBackendFormat& format) const {
1218     VkFormat vkFormat;
1219     if (!format.asVkFormat(&vkFormat)) {
1220         return false;
1221     }
1222 
1223     SkASSERT(GrVkFormatIsSupported(vkFormat));
1224 
1225     return vkFormat == VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1226 }
1227 
isFormatTexturableAndUploadable(GrColorType ct,const GrBackendFormat & format) const1228 bool GrVkCaps::isFormatTexturableAndUploadable(GrColorType ct,
1229                                                const GrBackendFormat& format) const {
1230     VkFormat vkFormat;
1231     if (!format.asVkFormat(&vkFormat)) {
1232         return false;
1233     }
1234 
1235     uint32_t ctFlags = this->getFormatInfo(vkFormat).colorTypeFlags(ct);
1236     return this->isVkFormatTexturable(vkFormat) &&
1237            SkToBool(ctFlags & ColorTypeInfo::kUploadData_Flag);
1238 }
1239 
isFormatTexturable(const GrBackendFormat & format) const1240 bool GrVkCaps::isFormatTexturable(const GrBackendFormat& format) const {
1241     VkFormat vkFormat;
1242     if (!format.asVkFormat(&vkFormat)) {
1243         return false;
1244     }
1245     return this->isVkFormatTexturable(vkFormat);
1246 }
1247 
isVkFormatTexturable(VkFormat format) const1248 bool GrVkCaps::isVkFormatTexturable(VkFormat format) const {
1249     const FormatInfo& info = this->getFormatInfo(format);
1250     return SkToBool(FormatInfo::kTexturable_Flag & info.fOptimalFlags);
1251 }
1252 
isFormatAsColorTypeRenderable(GrColorType ct,const GrBackendFormat & format,int sampleCount) const1253 bool GrVkCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
1254                                              int sampleCount) const {
1255     if (!this->isFormatRenderable(format, sampleCount)) {
1256         return false;
1257     }
1258     VkFormat vkFormat;
1259     if (!format.asVkFormat(&vkFormat)) {
1260         return false;
1261     }
1262     const auto& info = this->getFormatInfo(vkFormat);
1263     if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
1264         return false;
1265     }
1266     return true;
1267 }
1268 
isFormatRenderable(const GrBackendFormat & format,int sampleCount) const1269 bool GrVkCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
1270     VkFormat vkFormat;
1271     if (!format.asVkFormat(&vkFormat)) {
1272         return false;
1273     }
1274     return this->isFormatRenderable(vkFormat, sampleCount);
1275 }
1276 
isFormatRenderable(VkFormat format,int sampleCount) const1277 bool GrVkCaps::isFormatRenderable(VkFormat format, int sampleCount) const {
1278     return sampleCount <= this->maxRenderTargetSampleCount(format);
1279 }
1280 
getRenderTargetSampleCount(int requestedCount,const GrBackendFormat & format) const1281 int GrVkCaps::getRenderTargetSampleCount(int requestedCount,
1282                                          const GrBackendFormat& format) const {
1283     VkFormat vkFormat;
1284     if (!format.asVkFormat(&vkFormat)) {
1285         return 0;
1286     }
1287 
1288     return this->getRenderTargetSampleCount(requestedCount, vkFormat);
1289 }
1290 
getRenderTargetSampleCount(int requestedCount,VkFormat format) const1291 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
1292     requestedCount = SkTMax(1, requestedCount);
1293 
1294     const FormatInfo& info = this->getFormatInfo(format);
1295 
1296     int count = info.fColorSampleCounts.count();
1297 
1298     if (!count) {
1299         return 0;
1300     }
1301 
1302     if (1 == requestedCount) {
1303         SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
1304         return 1;
1305     }
1306 
1307     for (int i = 0; i < count; ++i) {
1308         if (info.fColorSampleCounts[i] >= requestedCount) {
1309             return info.fColorSampleCounts[i];
1310         }
1311     }
1312     return 0;
1313 }
1314 
maxRenderTargetSampleCount(const GrBackendFormat & format) const1315 int GrVkCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
1316     VkFormat vkFormat;
1317     if (!format.asVkFormat(&vkFormat)) {
1318         return 0;
1319     }
1320     return this->maxRenderTargetSampleCount(vkFormat);
1321 }
1322 
maxRenderTargetSampleCount(VkFormat format) const1323 int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
1324     const FormatInfo& info = this->getFormatInfo(format);
1325 
1326     const auto& table = info.fColorSampleCounts;
1327     if (!table.count()) {
1328         return 0;
1329     }
1330     return table[table.count() - 1];
1331 }
1332 
align_to_4(size_t v)1333 static inline size_t align_to_4(size_t v) {
1334     switch (v & 0b11) {
1335         // v is already a multiple of 4.
1336         case 0:     return v;
1337         // v is a multiple of 2 but not 4.
1338         case 2:     return 2 * v;
1339         // v is not a multiple of 2.
1340         default:    return 4 * v;
1341     }
1342 }
1343 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType) const1344 GrCaps::SupportedWrite GrVkCaps::supportedWritePixelsColorType(GrColorType surfaceColorType,
1345                                                                const GrBackendFormat& surfaceFormat,
1346                                                                GrColorType srcColorType) const {
1347     VkFormat vkFormat;
1348     if (!surfaceFormat.asVkFormat(&vkFormat)) {
1349         return {GrColorType::kUnknown, 0};
1350     }
1351 
1352 
1353     if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1354         return {GrColorType::kUnknown, 0};
1355     }
1356 
1357     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1358     size_t offsetAlignment = align_to_4(GrVkBytesPerFormat(vkFormat));
1359 
1360     const auto& info = this->getFormatInfo(vkFormat);
1361     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1362         const auto& ctInfo = info.fColorTypeInfos[i];
1363         if (ctInfo.fColorType == surfaceColorType) {
1364             return {surfaceColorType, offsetAlignment};
1365         }
1366     }
1367     return {GrColorType::kUnknown, 0};
1368 }
1369 
surfaceSupportsReadPixels(const GrSurface * surface) const1370 GrCaps::SurfaceReadPixelsSupport GrVkCaps::surfaceSupportsReadPixels(
1371         const GrSurface* surface) const {
1372     if (surface->isProtected()) {
1373         return SurfaceReadPixelsSupport::kUnsupported;
1374     }
1375     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1376         // We can't directly read from a VkImage that has a ycbcr sampler.
1377         if (tex->ycbcrConversionInfo().isValid()) {
1378             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1379         }
1380         // We can't directly read from a compressed format
1381         SkImage::CompressionType compressionType;
1382         if (GrVkFormatToCompressionType(tex->imageFormat(), &compressionType)) {
1383             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1384         }
1385     }
1386     return SurfaceReadPixelsSupport::kSupported;
1387 }
1388 
onSurfaceSupportsWritePixels(const GrSurface * surface) const1389 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
1390     if (auto rt = surface->asRenderTarget()) {
1391         return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
1392     }
1393     // We can't write to a texture that has a ycbcr sampler.
1394     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1395         // We can't directly read from a VkImage that has a ycbcr sampler.
1396         if (tex->ycbcrConversionInfo().isValid()) {
1397             return false;
1398         }
1399     }
1400     return true;
1401 }
1402 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format) const1403 bool GrVkCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1404                                                  const GrBackendFormat& format) const {
1405     VkFormat vkFormat;
1406     if (!format.asVkFormat(&vkFormat)) {
1407         return false;
1408     }
1409     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1410     SkASSERT(ycbcrInfo);
1411 
1412     if (ycbcrInfo->isValid() && !GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1413         // Format may be undefined for external images, which are required to have YCbCr conversion.
1414         if (VK_FORMAT_UNDEFINED == vkFormat) {
1415             return true;
1416         }
1417         return false;
1418     }
1419 
1420     const auto& info = this->getFormatInfo(vkFormat);
1421     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1422         if (info.fColorTypeInfos[i].fColorType == ct) {
1423             return true;
1424         }
1425     }
1426     return false;
1427 }
1428 
validate_image_info(VkFormat format,GrColorType ct,bool hasYcbcrConversion)1429 static GrPixelConfig validate_image_info(VkFormat format, GrColorType ct, bool hasYcbcrConversion) {
1430     if (hasYcbcrConversion) {
1431         if (GrVkFormatNeedsYcbcrSampler(format)) {
1432             return kRGB_888X_GrPixelConfig;
1433         }
1434 
1435         // Format may be undefined for external images, which are required to have YCbCr conversion.
1436         if (VK_FORMAT_UNDEFINED == format) {
1437             // We don't actually care what the color type or config are since we won't use those
1438             // values for external textures. However, for read pixels we will draw to a non ycbcr
1439             // texture of this config so we set RGBA here for that.
1440             return kRGBA_8888_GrPixelConfig;
1441         }
1442 
1443         return kUnknown_GrPixelConfig;
1444     }
1445 
1446     if (VK_FORMAT_UNDEFINED == format) {
1447         return kUnknown_GrPixelConfig;
1448     }
1449 
1450     switch (ct) {
1451         case GrColorType::kUnknown:
1452             break;
1453         case GrColorType::kAlpha_8:
1454             if (VK_FORMAT_R8_UNORM == format) {
1455                 return kAlpha_8_as_Red_GrPixelConfig;
1456             }
1457             break;
1458         case GrColorType::kBGR_565:
1459             if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
1460                 return kRGB_565_GrPixelConfig;
1461             }
1462             break;
1463         case GrColorType::kABGR_4444:
1464             if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format ||
1465                 VK_FORMAT_R4G4B4A4_UNORM_PACK16 == format) {
1466                 return kRGBA_4444_GrPixelConfig;
1467             }
1468             break;
1469         case GrColorType::kRGBA_8888:
1470             if (VK_FORMAT_R8G8B8A8_UNORM == format) {
1471                 return kRGBA_8888_GrPixelConfig;
1472             }
1473             break;
1474         case GrColorType::kRGBA_8888_SRGB:
1475             if (VK_FORMAT_R8G8B8A8_SRGB == format) {
1476                 return kSRGBA_8888_GrPixelConfig;
1477             }
1478             break;
1479         case GrColorType::kRGB_888x:
1480             if (VK_FORMAT_R8G8B8_UNORM == format) {
1481                 return kRGB_888_GrPixelConfig;
1482             } else if (VK_FORMAT_R8G8B8A8_UNORM == format) {
1483                 return kRGB_888X_GrPixelConfig;
1484             } else if (VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK == format) {
1485                 return kRGB_ETC1_GrPixelConfig;
1486             }
1487             break;
1488         case GrColorType::kRG_88:
1489             if (VK_FORMAT_R8G8_UNORM == format) {
1490                 return kRG_88_GrPixelConfig;
1491             }
1492             break;
1493         case GrColorType::kBGRA_8888:
1494             if (VK_FORMAT_B8G8R8A8_UNORM == format) {
1495                 return kBGRA_8888_GrPixelConfig;
1496             }
1497             break;
1498         case GrColorType::kRGBA_1010102:
1499             if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
1500                 return kRGBA_1010102_GrPixelConfig;
1501             }
1502             break;
1503         case GrColorType::kGray_8:
1504             if (VK_FORMAT_R8_UNORM == format) {
1505                 return kGray_8_as_Red_GrPixelConfig;
1506             }
1507             break;
1508         case GrColorType::kAlpha_F16:
1509             if (VK_FORMAT_R16_SFLOAT == format) {
1510                 return kAlpha_half_as_Red_GrPixelConfig;
1511             }
1512             break;
1513         case GrColorType::kRGBA_F16:
1514             if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
1515                 return kRGBA_half_GrPixelConfig;
1516             }
1517             break;
1518         case GrColorType::kRGBA_F16_Clamped:
1519             if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
1520                 return kRGBA_half_Clamped_GrPixelConfig;
1521             }
1522             break;
1523         case GrColorType::kRGBA_F32:
1524             if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
1525                 return kRGBA_float_GrPixelConfig;
1526             }
1527             break;
1528         case GrColorType::kR_16:
1529             if (VK_FORMAT_R16_UNORM == format) {
1530                 return kR_16_GrPixelConfig;
1531             }
1532             break;
1533         case GrColorType::kRG_1616:
1534             if (VK_FORMAT_R16G16_UNORM == format) {
1535                 return kRG_1616_GrPixelConfig;
1536             }
1537             break;
1538         case GrColorType::kRGBA_16161616:
1539             if (VK_FORMAT_R16G16B16A16_UNORM == format) {
1540                 return kRGBA_16161616_GrPixelConfig;
1541             }
1542             break;
1543         case GrColorType::kRG_F16:
1544             if (VK_FORMAT_R16G16_SFLOAT == format) {
1545                 return kRG_half_GrPixelConfig;
1546             }
1547             break;
1548         // These have no equivalent:
1549         case GrColorType::kAlpha_8xxx:
1550         case GrColorType::kAlpha_F32xxx:
1551         case GrColorType::kGray_8xxx:
1552             break;
1553     }
1554 
1555     return kUnknown_GrPixelConfig;
1556 }
1557 
onGetConfigFromBackendFormat(const GrBackendFormat & format,GrColorType ct) const1558 GrPixelConfig GrVkCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format,
1559                                                      GrColorType ct) const {
1560     VkFormat vkFormat;
1561     if (!format.asVkFormat(&vkFormat)) {
1562         return kUnknown_GrPixelConfig;
1563     }
1564     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1565     SkASSERT(ycbcrInfo);
1566     return validate_image_info(vkFormat, ct, ycbcrInfo->isValid());
1567 }
1568 
getYUVAColorTypeFromBackendFormat(const GrBackendFormat & format,bool isAlphaChannel) const1569 GrColorType GrVkCaps::getYUVAColorTypeFromBackendFormat(const GrBackendFormat& format,
1570                                                         bool isAlphaChannel) const {
1571     VkFormat vkFormat;
1572     if (!format.asVkFormat(&vkFormat)) {
1573         return GrColorType::kUnknown;
1574     }
1575 
1576     switch (vkFormat) {
1577         case VK_FORMAT_R8_UNORM:                 return isAlphaChannel ? GrColorType::kAlpha_8
1578                                                                        : GrColorType::kGray_8;
1579         case VK_FORMAT_R8G8B8A8_UNORM:           return GrColorType::kRGBA_8888;
1580         case VK_FORMAT_R8G8B8_UNORM:             return GrColorType::kRGB_888x;
1581         case VK_FORMAT_R8G8_UNORM:               return GrColorType::kRG_88;
1582         case VK_FORMAT_B8G8R8A8_UNORM:           return GrColorType::kBGRA_8888;
1583         case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return GrColorType::kRGBA_1010102;
1584         case VK_FORMAT_R16_UNORM:                return GrColorType::kR_16;
1585         case VK_FORMAT_R16G16_UNORM:             return GrColorType::kRG_1616;
1586         // Experimental (for Y416 and mutant P016/P010)
1587         case VK_FORMAT_R16G16B16A16_UNORM:       return GrColorType::kRGBA_16161616;
1588         case VK_FORMAT_R16G16_SFLOAT:            return GrColorType::kRG_F16;
1589         default:                                 return GrColorType::kUnknown;
1590     }
1591 
1592     SkUNREACHABLE;
1593 }
1594 
onGetDefaultBackendFormat(GrColorType ct,GrRenderable renderable) const1595 GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct,
1596                                                     GrRenderable renderable) const {
1597     VkFormat format = this->getFormatFromColorType(ct);
1598     if (format == VK_FORMAT_UNDEFINED) {
1599         return GrBackendFormat();
1600     }
1601     return GrBackendFormat::MakeVk(format);
1602 }
1603 
getBackendFormatFromCompressionType(SkImage::CompressionType compressionType) const1604 GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType(
1605         SkImage::CompressionType compressionType) const {
1606     switch (compressionType) {
1607         case SkImage::kETC1_CompressionType:
1608             return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
1609     }
1610     SK_ABORT("Invalid compression type");
1611 }
1612 
canClearTextureOnCreation() const1613 bool GrVkCaps::canClearTextureOnCreation() const { return true; }
1614 
getTextureSwizzle(const GrBackendFormat & format,GrColorType colorType) const1615 GrSwizzle GrVkCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1616     VkFormat vkFormat;
1617     SkAssertResult(format.asVkFormat(&vkFormat));
1618     const auto& info = this->getFormatInfo(vkFormat);
1619     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1620         const auto& ctInfo = info.fColorTypeInfos[i];
1621         if (ctInfo.fColorType == colorType) {
1622             return ctInfo.fTextureSwizzle;
1623         }
1624     }
1625     return GrSwizzle::RGBA();
1626 }
1627 
getOutputSwizzle(const GrBackendFormat & format,GrColorType colorType) const1628 GrSwizzle GrVkCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1629     VkFormat vkFormat;
1630     SkAssertResult(format.asVkFormat(&vkFormat));
1631     const auto& info = this->getFormatInfo(vkFormat);
1632     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1633         const auto& ctInfo = info.fColorTypeInfos[i];
1634         if (ctInfo.fColorType == colorType) {
1635             return ctInfo.fOutputSwizzle;
1636         }
1637     }
1638     return GrSwizzle::RGBA();
1639 }
1640 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat & srcBackendFormat,GrColorType dstColorType) const1641 GrCaps::SupportedRead GrVkCaps::onSupportedReadPixelsColorType(
1642         GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1643         GrColorType dstColorType) const {
1644     VkFormat vkFormat;
1645     if (!srcBackendFormat.asVkFormat(&vkFormat)) {
1646         return {GrColorType::kUnknown, 0};
1647     }
1648 
1649     if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1650         return {GrColorType::kUnknown, 0};
1651     }
1652 
1653     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1654     size_t offsetAlignment = align_to_4(GrVkBytesPerFormat(vkFormat));
1655 
1656     const auto& info = this->getFormatInfo(vkFormat);
1657     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1658         const auto& ctInfo = info.fColorTypeInfos[i];
1659         if (ctInfo.fColorType == srcColorType) {
1660             return {srcColorType, offsetAlignment};
1661         }
1662     }
1663     return {GrColorType::kUnknown, 0};
1664 }
1665 
getFragmentUniformBinding() const1666 int GrVkCaps::getFragmentUniformBinding() const {
1667     return GrVkUniformHandler::kUniformBinding;
1668 }
1669 
getFragmentUniformSet() const1670 int GrVkCaps::getFragmentUniformSet() const {
1671     return GrVkUniformHandler::kUniformBufferDescSet;
1672 }
1673 
1674 #if GR_TEST_UTILS
getTestingCombinations() const1675 std::vector<GrCaps::TestFormatColorTypeCombination> GrVkCaps::getTestingCombinations() const {
1676     std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
1677         { GrColorType::kAlpha_8,          GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1678         { GrColorType::kBGR_565,          GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16)  },
1679         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_R4G4B4A4_UNORM_PACK16)},
1680         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_B4G4R4A4_UNORM_PACK16)},
1681         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1682         { GrColorType::kRGBA_8888_SRGB,   GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_SRGB)        },
1683         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1684         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM)         },
1685         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)},
1686         { GrColorType::kRG_88,            GrBackendFormat::MakeVk(VK_FORMAT_R8G8_UNORM)           },
1687         { GrColorType::kBGRA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_UNORM)       },
1688         { GrColorType::kRGBA_1010102,     GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32)},
1689         { GrColorType::kGray_8,           GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1690         { GrColorType::kAlpha_F16,        GrBackendFormat::MakeVk(VK_FORMAT_R16_SFLOAT)           },
1691         { GrColorType::kRGBA_F16,         GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1692         { GrColorType::kRGBA_F16_Clamped, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1693         { GrColorType::kRGBA_F32,         GrBackendFormat::MakeVk(VK_FORMAT_R32G32B32A32_SFLOAT)  },
1694         { GrColorType::kR_16,             GrBackendFormat::MakeVk(VK_FORMAT_R16_UNORM)            },
1695         { GrColorType::kRG_1616,          GrBackendFormat::MakeVk(VK_FORMAT_R16G16_UNORM)         },
1696         { GrColorType::kRGBA_16161616,    GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_UNORM)   },
1697         { GrColorType::kRG_F16,           GrBackendFormat::MakeVk(VK_FORMAT_R16G16_SFLOAT)        },
1698     };
1699 
1700     return combos;
1701 }
1702 #endif
1703