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 "GrVkStencilAttachment.h"
9 #include "GrVkGpu.h"
10 #include "GrVkImage.h"
11 #include "GrVkImageView.h"
12 #include "GrVkUtil.h"
13
14 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
15
GrVkStencilAttachment(GrVkGpu * gpu,const Format & format,const GrVkImage::ImageDesc & desc,const GrVkImageInfo & info,sk_sp<GrVkImageLayout> layout,const GrVkImageView * stencilView)16 GrVkStencilAttachment::GrVkStencilAttachment(GrVkGpu* gpu,
17 const Format& format,
18 const GrVkImage::ImageDesc& desc,
19 const GrVkImageInfo& info,
20 sk_sp<GrVkImageLayout> layout,
21 const GrVkImageView* stencilView)
22 : GrStencilAttachment(gpu, desc.fWidth, desc.fHeight, format.fStencilBits, desc.fSamples)
23 , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned)
24 , fFormat(format)
25 , fStencilView(stencilView) {
26 this->registerWithCache(SkBudgeted::kYes);
27 stencilView->ref();
28 }
29
Create(GrVkGpu * gpu,int width,int height,int sampleCnt,const Format & format)30 GrVkStencilAttachment* GrVkStencilAttachment::Create(GrVkGpu* gpu,
31 int width,
32 int height,
33 int sampleCnt,
34 const Format& format) {
35 GrVkImage::ImageDesc imageDesc;
36 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
37 imageDesc.fFormat = format.fInternalFormat;
38 imageDesc.fWidth = width;
39 imageDesc.fHeight = height;
40 imageDesc.fLevels = 1;
41 imageDesc.fSamples = sampleCnt;
42 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
43 imageDesc.fUsageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
44 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
45 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
46
47 GrVkImageInfo info;
48 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
49 return nullptr;
50 }
51
52 const GrVkImageView* imageView = GrVkImageView::Create(gpu, info.fImage,
53 format.fInternalFormat,
54 GrVkImageView::kStencil_Type, 1,
55 GrVkYcbcrConversionInfo());
56 if (!imageView) {
57 GrVkImage::DestroyImageInfo(gpu, &info);
58 return nullptr;
59 }
60
61 sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
62 GrVkStencilAttachment* stencil = new GrVkStencilAttachment(gpu, format, imageDesc,
63 info, std::move(layout), imageView);
64 imageView->unref(gpu);
65
66 return stencil;
67 }
68
~GrVkStencilAttachment()69 GrVkStencilAttachment::~GrVkStencilAttachment() {
70 // should have been released or abandoned first
71 SkASSERT(!fStencilView);
72 }
73
onGpuMemorySize() const74 size_t GrVkStencilAttachment::onGpuMemorySize() const {
75 uint64_t size = this->width();
76 size *= this->height();
77 size *= fFormat.fTotalBits;
78 size *= this->numSamples();
79 return static_cast<size_t>(size / 8);
80 }
81
onRelease()82 void GrVkStencilAttachment::onRelease() {
83 GrVkGpu* gpu = this->getVkGpu();
84
85 this->releaseImage(gpu);
86
87 fStencilView->unref(gpu);
88 fStencilView = nullptr;
89 GrStencilAttachment::onRelease();
90 }
91
onAbandon()92 void GrVkStencilAttachment::onAbandon() {
93 this->abandonImage();
94 fStencilView->unrefAndAbandon();
95 fStencilView = nullptr;
96 GrStencilAttachment::onAbandon();
97 }
98
getVkGpu() const99 GrVkGpu* GrVkStencilAttachment::getVkGpu() const {
100 SkASSERT(!this->wasDestroyed());
101 return static_cast<GrVkGpu*>(this->getGpu());
102 }
103