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"
10 #include "GrVkSamplerYcbcrConversion.h"
11 #include "GrVkUtil.h"
12
Create(GrVkGpu * gpu,VkImage image,VkFormat format,Type viewType,uint32_t miplevels,const GrVkYcbcrConversionInfo & ycbcrInfo)13 const GrVkImageView* GrVkImageView::Create(GrVkGpu* gpu, VkImage image, VkFormat format,
14 Type viewType, uint32_t miplevels,
15 const GrVkYcbcrConversionInfo& ycbcrInfo) {
16
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;
37 // Create the VkImageView
38 VkImageViewCreateInfo viewInfo = {
39 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // sType
40 pNext, // pNext
41 0, // flags
42 image, // image
43 VK_IMAGE_VIEW_TYPE_2D, // viewType
44 format, // format
45 { VK_COMPONENT_SWIZZLE_IDENTITY,
46 VK_COMPONENT_SWIZZLE_IDENTITY,
47 VK_COMPONENT_SWIZZLE_IDENTITY,
48 VK_COMPONENT_SWIZZLE_IDENTITY }, // components
49 { VK_IMAGE_ASPECT_COLOR_BIT, 0, miplevels, 0, 1 }, // subresourceRange
50 };
51 if (kStencil_Type == viewType) {
52 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
53 }
54
55 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateImageView(gpu->device(), &viewInfo,
56 nullptr, &imageView));
57 if (err) {
58 return nullptr;
59 }
60
61 return new GrVkImageView(imageView, ycbcrConversion);
62 }
63
freeGPUData(GrVkGpu * gpu) const64 void GrVkImageView::freeGPUData(GrVkGpu* gpu) const {
65 GR_VK_CALL(gpu->vkInterface(), DestroyImageView(gpu->device(), fImageView, nullptr));
66
67 if (fYcbcrConversion) {
68 fYcbcrConversion->unref(gpu);
69 }
70 }
71
abandonGPUData() const72 void GrVkImageView::abandonGPUData() const {
73 if (fYcbcrConversion) {
74 fYcbcrConversion->unrefAndAbandon();
75 }
76 }
77
78