blob: 7c5e83e500e038a4d4eb7e7ac7a148553ea15e39 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
16GrVkStencilAttachment::GrVkStencilAttachment(GrVkGpu* gpu,
Greg Daniel164a9f02016-02-22 09:56:40 -050017 const Format& format,
18 const GrVkImage::ImageDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070019 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040020 sk_sp<GrVkImageLayout> layout,
Greg Daniel164a9f02016-02-22 09:56:40 -050021 const GrVkImageView* stencilView)
robertphillips55fdccc2016-06-06 06:16:20 -070022 : GrStencilAttachment(gpu, desc.fWidth, desc.fHeight, format.fStencilBits, desc.fSamples)
Greg Daniel52e16d92018-04-10 09:34:07 -040023 , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned)
Greg Daniel164a9f02016-02-22 09:56:40 -050024 , fFormat(format)
Greg Daniel164a9f02016-02-22 09:56:40 -050025 , fStencilView(stencilView) {
kkinnunen2e6055b2016-04-22 01:48:29 -070026 this->registerWithCache(SkBudgeted::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -050027 stencilView->ref();
28}
29
30GrVkStencilAttachment* GrVkStencilAttachment::Create(GrVkGpu* gpu,
Greg Daniel164a9f02016-02-22 09:56:40 -050031 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;
egdaniel58a8d922016-04-21 08:03:10 -070043 imageDesc.fUsageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
44 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -050045 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
46
egdanielb2df0c22016-05-13 11:30:37 -070047 GrVkImageInfo info;
48 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
Greg Daniel164a9f02016-02-22 09:56:40 -050049 return nullptr;
50 }
51
egdanielb2df0c22016-05-13 11:30:37 -070052 const GrVkImageView* imageView = GrVkImageView::Create(gpu, info.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -050053 format.fInternalFormat,
jvanverth62340062016-04-26 08:01:44 -070054 GrVkImageView::kStencil_Type, 1);
Greg Daniel164a9f02016-02-22 09:56:40 -050055 if (!imageView) {
jvanverth1e305ba2016-06-01 09:39:15 -070056 GrVkImage::DestroyImageInfo(gpu, &info);
Greg Daniel164a9f02016-02-22 09:56:40 -050057 return nullptr;
58 }
59
Greg Daniel52e16d92018-04-10 09:34:07 -040060 sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
kkinnunen2e6055b2016-04-22 01:48:29 -070061 GrVkStencilAttachment* stencil = new GrVkStencilAttachment(gpu, format, imageDesc,
Greg Daniel52e16d92018-04-10 09:34:07 -040062 info, std::move(layout), imageView);
Greg Daniel164a9f02016-02-22 09:56:40 -050063 imageView->unref(gpu);
halcanary9d524f22016-03-29 09:03:52 -070064
Greg Daniel164a9f02016-02-22 09:56:40 -050065 return stencil;
66}
67
68GrVkStencilAttachment::~GrVkStencilAttachment() {
69 // should have been released or abandoned first
Greg Daniel164a9f02016-02-22 09:56:40 -050070 SkASSERT(!fStencilView);
71}
72
73size_t GrVkStencilAttachment::onGpuMemorySize() const {
74 uint64_t size = this->width();
75 size *= this->height();
76 size *= fFormat.fTotalBits;
Brian Salomonbdecacf2018-02-02 20:32:49 -050077 size *= this->numSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -050078 return static_cast<size_t>(size / 8);
79}
80
81void GrVkStencilAttachment::onRelease() {
82 GrVkGpu* gpu = this->getVkGpu();
83
egdaniele015c262016-02-26 13:12:00 -080084 this->releaseImage(gpu);
Greg Daniel164a9f02016-02-22 09:56:40 -050085
86 fStencilView->unref(gpu);
87 fStencilView = nullptr;
egdaniele015c262016-02-26 13:12:00 -080088 GrStencilAttachment::onRelease();
Greg Daniel164a9f02016-02-22 09:56:40 -050089}
90
91void GrVkStencilAttachment::onAbandon() {
egdaniele015c262016-02-26 13:12:00 -080092 this->abandonImage();
Greg Daniel164a9f02016-02-22 09:56:40 -050093 fStencilView->unrefAndAbandon();
94 fStencilView = nullptr;
egdaniele015c262016-02-26 13:12:00 -080095 GrStencilAttachment::onAbandon();
Greg Daniel164a9f02016-02-22 09:56:40 -050096}
97
98GrVkGpu* GrVkStencilAttachment::getVkGpu() const {
99 SkASSERT(!this->wasDestroyed());
100 return static_cast<GrVkGpu*>(this->getGpu());
101}