Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "GrVkImageView.h" |
| 9 | #include "GrVkGpu.h" |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 10 | #include "GrVkSamplerYcbcrConversion.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrVkUtil.h" |
| 12 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 13 | const GrVkImageView* GrVkImageView::Create(GrVkGpu* gpu, VkImage image, VkFormat format, |
| 14 | Type viewType, uint32_t miplevels, |
| 15 | const GrVkYcbcrConversionInfo& ycbcrInfo) { |
Greg Daniel | f4bf973 | 2018-12-03 14:34:28 +0000 | [diff] [blame] | 16 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 17 | void* pNext = nullptr; |
| 18 | VkSamplerYcbcrConversionInfo conversionInfo; |
| 19 | GrVkSamplerYcbcrConversion* ycbcrConversion = nullptr; |
| 20 | |
| 21 | if (ycbcrInfo.isValid()) { |
| 22 | SkASSERT(gpu->vkCaps().supportsYcbcrConversion() && format == VK_FORMAT_UNDEFINED); |
| 23 | |
| 24 | ycbcrConversion = |
| 25 | gpu->resourceProvider().findOrCreateCompatibleSamplerYcbcrConversion(ycbcrInfo); |
| 26 | if (!ycbcrConversion) { |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
| 30 | conversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO; |
| 31 | conversionInfo.pNext = nullptr; |
| 32 | conversionInfo.conversion = ycbcrConversion->ycbcrConversion(); |
| 33 | pNext = &conversionInfo; |
| 34 | } |
| 35 | |
| 36 | VkImageView imageView; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 37 | // Create the VkImageView |
| 38 | VkImageViewCreateInfo viewInfo = { |
| 39 | VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // sType |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 40 | pNext, // pNext |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | 0, // flags |
| 42 | image, // image |
| 43 | VK_IMAGE_VIEW_TYPE_2D, // viewType |
| 44 | format, // format |
egdaniel | e7f2fe4 | 2016-07-01 08:03:02 -0700 | [diff] [blame] | 45 | { VK_COMPONENT_SWIZZLE_IDENTITY, |
| 46 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 47 | VK_COMPONENT_SWIZZLE_IDENTITY, |
| 48 | VK_COMPONENT_SWIZZLE_IDENTITY }, // components |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 49 | { VK_IMAGE_ASPECT_COLOR_BIT, 0, miplevels, 0, 1 }, // subresourceRange |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 50 | }; |
| 51 | if (kStencil_Type == viewType) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 52 | viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 53 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 54 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 55 | VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateImageView(gpu->device(), &viewInfo, |
| 56 | nullptr, &imageView)); |
| 57 | if (err) { |
| 58 | return nullptr; |
| 59 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 60 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 61 | return new GrVkImageView(imageView, ycbcrConversion); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 64 | void GrVkImageView::freeGPUData(GrVkGpu* gpu) const { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 65 | GR_VK_CALL(gpu->vkInterface(), DestroyImageView(gpu->device(), fImageView, nullptr)); |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 66 | |
| 67 | if (fYcbcrConversion) { |
| 68 | fYcbcrConversion->unref(gpu); |
| 69 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 70 | } |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 71 | |
| 72 | void GrVkImageView::abandonGPUData() const { |
| 73 | if (fYcbcrConversion) { |
| 74 | fYcbcrConversion->unrefAndAbandon(); |
| 75 | } |
| 76 | } |
| 77 | |