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