blob: 82c9457a51f159dcaa9c531bea847d3736f5a00b [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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 Daniel7e000222018-12-03 10:08:21 -050010#include "GrVkSamplerYcbcrConversion.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrVkUtil.h"
12
Greg Daniel7e000222018-12-03 10:08:21 -050013const GrVkImageView* GrVkImageView::Create(GrVkGpu* gpu, VkImage image, VkFormat format,
14 Type viewType, uint32_t miplevels,
15 const GrVkYcbcrConversionInfo& ycbcrInfo) {
Greg Danielf4bf9732018-12-03 14:34:28 +000016
Greg Daniel7e000222018-12-03 10:08:21 -050017 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 Daniel164a9f02016-02-22 09:56:40 -050037 // Create the VkImageView
38 VkImageViewCreateInfo viewInfo = {
39 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // sType
Greg Daniel7e000222018-12-03 10:08:21 -050040 pNext, // pNext
Greg Daniel164a9f02016-02-22 09:56:40 -050041 0, // flags
42 image, // image
43 VK_IMAGE_VIEW_TYPE_2D, // viewType
44 format, // format
egdaniele7f2fe42016-07-01 08:03:02 -070045 { VK_COMPONENT_SWIZZLE_IDENTITY,
46 VK_COMPONENT_SWIZZLE_IDENTITY,
47 VK_COMPONENT_SWIZZLE_IDENTITY,
48 VK_COMPONENT_SWIZZLE_IDENTITY }, // components
jvanverth62340062016-04-26 08:01:44 -070049 { VK_IMAGE_ASPECT_COLOR_BIT, 0, miplevels, 0, 1 }, // subresourceRange
Greg Daniel164a9f02016-02-22 09:56:40 -050050 };
51 if (kStencil_Type == viewType) {
Greg Daniel164a9f02016-02-22 09:56:40 -050052 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
53 }
halcanary9d524f22016-03-29 09:03:52 -070054
Greg Daniel164a9f02016-02-22 09:56:40 -050055 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateImageView(gpu->device(), &viewInfo,
56 nullptr, &imageView));
57 if (err) {
58 return nullptr;
59 }
halcanary9d524f22016-03-29 09:03:52 -070060
Greg Daniel7e000222018-12-03 10:08:21 -050061 return new GrVkImageView(imageView, ycbcrConversion);
Greg Daniel164a9f02016-02-22 09:56:40 -050062}
63
Ethan Nicholas8e265a72018-12-12 16:22:40 -050064void GrVkImageView::freeGPUData(GrVkGpu* gpu) const {
Greg Daniel164a9f02016-02-22 09:56:40 -050065 GR_VK_CALL(gpu->vkInterface(), DestroyImageView(gpu->device(), fImageView, nullptr));
Greg Daniel7e000222018-12-03 10:08:21 -050066
67 if (fYcbcrConversion) {
68 fYcbcrConversion->unref(gpu);
69 }
Greg Daniel164a9f02016-02-22 09:56:40 -050070}
Greg Daniel7e000222018-12-03 10:08:21 -050071
72void GrVkImageView::abandonGPUData() const {
73 if (fYcbcrConversion) {
74 fYcbcrConversion->unrefAndAbandon();
75 }
76}
77