[YCbCr Samplers] Use helper for CreateSamplerYcbcrConversion

Follow-up from https://dawn-review.googlesource.com/c/dawn/+/186542
to use util helper for CreateSamplerYcbcrConversion and storing the
wgpu::YCbCrVkDescriptor.

Change-Id: If83dd7fbb9b974ecb007f3e701fff747b078f8c4
Bug: dawn:2476
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/187180
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Saifuddin Hitawala <hitawala@chromium.org>
diff --git a/src/dawn/native/vulkan/SamplerVk.cpp b/src/dawn/native/vulkan/SamplerVk.cpp
index ffdf8a2..d1629a6 100644
--- a/src/dawn/native/vulkan/SamplerVk.cpp
+++ b/src/dawn/native/vulkan/SamplerVk.cpp
@@ -122,27 +122,11 @@
     }
 
     VkSamplerYcbcrConversionInfo samplerYCbCrInfo = {};
-    if (auto* yCbCrDescriptor = Unpack(descriptor).Get<YCbCrVkDescriptor>()) {
-        DAWN_TRY_ASSIGN(mYcbcrConversionCreateInfo,
-                        CreateSamplerYCbCrConversionCreateInfo(yCbCrDescriptor));
-
-        mExternalFormat = yCbCrDescriptor->externalFormat;
-#if DAWN_PLATFORM_IS(ANDROID)
-        VkExternalFormatANDROID vulkanExternalFormat;
-        // Chain VkExternalFormatANDROID only if needed.
-        if (mExternalFormat != 0) {
-            vulkanExternalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID;
-            vulkanExternalFormat.pNext = nullptr;
-            vulkanExternalFormat.externalFormat = mExternalFormat;
-
-            mYcbcrConversionCreateInfo.pNext = &vulkanExternalFormat;
-        }
-#endif  // DAWN_PLATFORM_IS(ANDROID)
-
-        DAWN_TRY(CheckVkSuccess(device->fn.CreateSamplerYcbcrConversion(
-                                    device->GetVkDevice(), &mYcbcrConversionCreateInfo, nullptr,
-                                    &*mSamplerYCbCrConversion),
-                                "CreateSamplerYcbcrConversion"));
+    if (auto* yCbCrVkDescriptor = Unpack(descriptor).Get<YCbCrVkDescriptor>()) {
+        mYCbCrVkDescriptor = *yCbCrVkDescriptor;
+        mYCbCrVkDescriptor.nextInChain = nullptr;
+        DAWN_TRY_ASSIGN(mSamplerYCbCrConversion,
+                        CreateSamplerYCbCrConversionCreateInfo(mYCbCrVkDescriptor, device));
 
         samplerYCbCrInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO;
         samplerYCbCrInfo.pNext = nullptr;
diff --git a/src/dawn/native/vulkan/SamplerVk.h b/src/dawn/native/vulkan/SamplerVk.h
index a80724f..c31bacb 100644
--- a/src/dawn/native/vulkan/SamplerVk.h
+++ b/src/dawn/native/vulkan/SamplerVk.h
@@ -54,8 +54,7 @@
 
     VkSampler mHandle = VK_NULL_HANDLE;
     VkSamplerYcbcrConversion mSamplerYCbCrConversion = VK_NULL_HANDLE;
-    VkSamplerYcbcrConversionCreateInfo mYcbcrConversionCreateInfo = {};
-    uint64_t mExternalFormat = 0u;
+    YCbCrVkDescriptor mYCbCrVkDescriptor;
 };
 
 }  // namespace dawn::native::vulkan
diff --git a/src/dawn/native/vulkan/TextureVk.cpp b/src/dawn/native/vulkan/TextureVk.cpp
index c484c2c..42d2f1c 100644
--- a/src/dawn/native/vulkan/TextureVk.cpp
+++ b/src/dawn/native/vulkan/TextureVk.cpp
@@ -1763,27 +1763,11 @@
     createInfo.pNext = &usageInfo;
 
     VkSamplerYcbcrConversionInfo samplerYCbCrInfo = {};
-    if (auto* yCbCrDescriptor = descriptor.Get<YCbCrVkDescriptor>()) {
-        DAWN_TRY_ASSIGN(mYcbcrConversionCreateInfo,
-                        CreateSamplerYCbCrConversionCreateInfo(yCbCrDescriptor));
-
-        mExternalFormat = yCbCrDescriptor->externalFormat;
-#if DAWN_PLATFORM_IS(ANDROID)
-        VkExternalFormatANDROID vulkanExternalFormat;
-        // Chain VkExternalFormatANDROID only if needed.
-        if (mExternalFormat != 0) {
-            vulkanExternalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID;
-            vulkanExternalFormat.pNext = nullptr;
-            vulkanExternalFormat.externalFormat = mExternalFormat;
-
-            mYcbcrConversionCreateInfo.pNext = &vulkanExternalFormat;
-        }
-#endif  // DAWN_PLATFORM_IS(ANDROID)
-
-        DAWN_TRY(CheckVkSuccess(device->fn.CreateSamplerYcbcrConversion(
-                                    device->GetVkDevice(), &mYcbcrConversionCreateInfo, nullptr,
-                                    &*mSamplerYCbCrConversion),
-                                "CreateSamplerYcbcrConversion for vkImageView"));
+    if (auto* yCbCrVkDescriptor = descriptor.Get<YCbCrVkDescriptor>()) {
+        mYCbCrVkDescriptor = *yCbCrVkDescriptor;
+        mYCbCrVkDescriptor.nextInChain = nullptr;
+        DAWN_TRY_ASSIGN(mSamplerYCbCrConversion,
+                        CreateSamplerYCbCrConversionCreateInfo(mYCbCrVkDescriptor, device));
 
         samplerYCbCrInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO;
         samplerYCbCrInfo.pNext = nullptr;
diff --git a/src/dawn/native/vulkan/TextureVk.h b/src/dawn/native/vulkan/TextureVk.h
index fe38192..d3409bc 100644
--- a/src/dawn/native/vulkan/TextureVk.h
+++ b/src/dawn/native/vulkan/TextureVk.h
@@ -258,8 +258,7 @@
     VkImageView mHandle = VK_NULL_HANDLE;
     VkImageView mHandleForBGRA8UnormStorage = VK_NULL_HANDLE;
     VkSamplerYcbcrConversion mSamplerYCbCrConversion = VK_NULL_HANDLE;
-    VkSamplerYcbcrConversionCreateInfo mYcbcrConversionCreateInfo = {};
-    uint64_t mExternalFormat = 0u;
+    YCbCrVkDescriptor mYCbCrVkDescriptor;
     std::vector<VkImageView> mHandlesFor2DViewOn3D;
 };
 
diff --git a/src/dawn/native/vulkan/UtilsVulkan.cpp b/src/dawn/native/vulkan/UtilsVulkan.cpp
index 2baef43..46fd82d 100644
--- a/src/dawn/native/vulkan/UtilsVulkan.cpp
+++ b/src/dawn/native/vulkan/UtilsVulkan.cpp
@@ -327,37 +327,56 @@
     return DAWN_VALIDATION_ERROR("DRM format modifier %u not supported.", modifier);
 }
 
-ResultOrError<VkSamplerYcbcrConversionCreateInfo> CreateSamplerYCbCrConversionCreateInfo(
-    const YCbCrVkDescriptor* yCbCrDescriptor) {
-    uint64_t externalFormat = yCbCrDescriptor->externalFormat;
-    VkFormat vulkanFormat = static_cast<VkFormat>(yCbCrDescriptor->vkFormat);
+ResultOrError<VkSamplerYcbcrConversion> CreateSamplerYCbCrConversionCreateInfo(
+    YCbCrVkDescriptor yCbCrDescriptor,
+    Device* device) {
+    uint64_t externalFormat = yCbCrDescriptor.externalFormat;
+    VkFormat vulkanFormat = static_cast<VkFormat>(yCbCrDescriptor.vkFormat);
     DAWN_INVALID_IF((externalFormat == 0 && vulkanFormat == VK_FORMAT_UNDEFINED),
                     "Both VkFormat and VkExternalFormatANDROID are undefined.");
 
     VkComponentMapping vulkanComponent;
-    vulkanComponent.r = static_cast<VkComponentSwizzle>(yCbCrDescriptor->vkComponentSwizzleRed);
-    vulkanComponent.g = static_cast<VkComponentSwizzle>(yCbCrDescriptor->vkComponentSwizzleGreen);
-    vulkanComponent.b = static_cast<VkComponentSwizzle>(yCbCrDescriptor->vkComponentSwizzleBlue);
-    vulkanComponent.a = static_cast<VkComponentSwizzle>(yCbCrDescriptor->vkComponentSwizzleAlpha);
+    vulkanComponent.r = static_cast<VkComponentSwizzle>(yCbCrDescriptor.vkComponentSwizzleRed);
+    vulkanComponent.g = static_cast<VkComponentSwizzle>(yCbCrDescriptor.vkComponentSwizzleGreen);
+    vulkanComponent.b = static_cast<VkComponentSwizzle>(yCbCrDescriptor.vkComponentSwizzleBlue);
+    vulkanComponent.a = static_cast<VkComponentSwizzle>(yCbCrDescriptor.vkComponentSwizzleAlpha);
 
     VkSamplerYcbcrConversionCreateInfo vulkanYCbCrCreateInfo;
     vulkanYCbCrCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO;
     vulkanYCbCrCreateInfo.pNext = nullptr;
     vulkanYCbCrCreateInfo.format = vulkanFormat;
     vulkanYCbCrCreateInfo.ycbcrModel =
-        static_cast<VkSamplerYcbcrModelConversion>(yCbCrDescriptor->vkYCbCrModel);
+        static_cast<VkSamplerYcbcrModelConversion>(yCbCrDescriptor.vkYCbCrModel);
     vulkanYCbCrCreateInfo.ycbcrRange =
-        static_cast<VkSamplerYcbcrRange>(yCbCrDescriptor->vkYCbCrRange);
+        static_cast<VkSamplerYcbcrRange>(yCbCrDescriptor.vkYCbCrRange);
     vulkanYCbCrCreateInfo.components = vulkanComponent;
     vulkanYCbCrCreateInfo.xChromaOffset =
-        static_cast<VkChromaLocation>(yCbCrDescriptor->vkXChromaOffset);
+        static_cast<VkChromaLocation>(yCbCrDescriptor.vkXChromaOffset);
     vulkanYCbCrCreateInfo.yChromaOffset =
-        static_cast<VkChromaLocation>(yCbCrDescriptor->vkYChromaOffset);
-    vulkanYCbCrCreateInfo.chromaFilter = static_cast<VkFilter>(yCbCrDescriptor->vkChromaFilter);
+        static_cast<VkChromaLocation>(yCbCrDescriptor.vkYChromaOffset);
+    vulkanYCbCrCreateInfo.chromaFilter = static_cast<VkFilter>(yCbCrDescriptor.vkChromaFilter);
     vulkanYCbCrCreateInfo.forceExplicitReconstruction =
-        static_cast<VkBool32>(yCbCrDescriptor->forceExplicitReconstruction);
+        static_cast<VkBool32>(yCbCrDescriptor.forceExplicitReconstruction);
 
-    return vulkanYCbCrCreateInfo;
+#if DAWN_PLATFORM_IS(ANDROID)
+    VkExternalFormatANDROID vulkanExternalFormat;
+    // Chain VkExternalFormatANDROID only if needed.
+    if (externalFormat != 0) {
+        vulkanExternalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID;
+        vulkanExternalFormat.pNext = nullptr;
+        vulkanExternalFormat.externalFormat = externalFormat;
+
+        vulkanYCbCrCreateInfo.pNext = &vulkanExternalFormat;
+    }
+#endif  // DAWN_PLATFORM_IS(ANDROID)
+
+    VkSamplerYcbcrConversion samplerYCbCrConversion = VK_NULL_HANDLE;
+    DAWN_TRY(CheckVkSuccess(
+        device->fn.CreateSamplerYcbcrConversion(device->GetVkDevice(), &vulkanYCbCrCreateInfo,
+                                                nullptr, &*samplerYCbCrConversion),
+        "CreateSamplerYcbcrConversion"));
+
+    return samplerYCbCrConversion;
 }
 
 }  // namespace dawn::native::vulkan
diff --git a/src/dawn/native/vulkan/UtilsVulkan.h b/src/dawn/native/vulkan/UtilsVulkan.h
index 7f08a79..e850472 100644
--- a/src/dawn/native/vulkan/UtilsVulkan.h
+++ b/src/dawn/native/vulkan/UtilsVulkan.h
@@ -184,8 +184,9 @@
     VkFormat format,
     uint64_t modifier);
 
-ResultOrError<VkSamplerYcbcrConversionCreateInfo> CreateSamplerYCbCrConversionCreateInfo(
-    const YCbCrVkDescriptor* yCbCrDescriptor);
+ResultOrError<VkSamplerYcbcrConversion> CreateSamplerYCbCrConversionCreateInfo(
+    YCbCrVkDescriptor yCbCrDescriptor,
+    Device* device);
 
 }  // namespace dawn::native::vulkan