blob: 22549b407a3026292de761060ede8a546179922d [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,
Greg Daniel7e000222018-12-03 10:08:21 -050054 GrVkImageView::kStencil_Type, 1,
55 GrVkYcbcrConversionInfo());
Greg Daniel164a9f02016-02-22 09:56:40 -050056 if (!imageView) {
jvanverth1e305ba2016-06-01 09:39:15 -070057 GrVkImage::DestroyImageInfo(gpu, &info);
Greg Daniel164a9f02016-02-22 09:56:40 -050058 return nullptr;
59 }
60
Greg Daniel52e16d92018-04-10 09:34:07 -040061 sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
kkinnunen2e6055b2016-04-22 01:48:29 -070062 GrVkStencilAttachment* stencil = new GrVkStencilAttachment(gpu, format, imageDesc,
Greg Daniel52e16d92018-04-10 09:34:07 -040063 info, std::move(layout), imageView);
Greg Daniel164a9f02016-02-22 09:56:40 -050064 imageView->unref(gpu);
halcanary9d524f22016-03-29 09:03:52 -070065
Greg Daniel164a9f02016-02-22 09:56:40 -050066 return stencil;
67}
68
69GrVkStencilAttachment::~GrVkStencilAttachment() {
70 // should have been released or abandoned first
Greg Daniel164a9f02016-02-22 09:56:40 -050071 SkASSERT(!fStencilView);
72}
73
74size_t GrVkStencilAttachment::onGpuMemorySize() const {
75 uint64_t size = this->width();
76 size *= this->height();
77 size *= fFormat.fTotalBits;
Brian Salomonbdecacf2018-02-02 20:32:49 -050078 size *= this->numSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -050079 return static_cast<size_t>(size / 8);
80}
81
82void GrVkStencilAttachment::onRelease() {
83 GrVkGpu* gpu = this->getVkGpu();
84
egdaniele015c262016-02-26 13:12:00 -080085 this->releaseImage(gpu);
Greg Daniel164a9f02016-02-22 09:56:40 -050086
87 fStencilView->unref(gpu);
88 fStencilView = nullptr;
egdaniele015c262016-02-26 13:12:00 -080089 GrStencilAttachment::onRelease();
Greg Daniel164a9f02016-02-22 09:56:40 -050090}
91
92void GrVkStencilAttachment::onAbandon() {
egdaniele015c262016-02-26 13:12:00 -080093 this->abandonImage();
Greg Daniel164a9f02016-02-22 09:56:40 -050094 fStencilView->unrefAndAbandon();
95 fStencilView = nullptr;
egdaniele015c262016-02-26 13:12:00 -080096 GrStencilAttachment::onAbandon();
Greg Daniel164a9f02016-02-22 09:56:40 -050097}
98
99GrVkGpu* GrVkStencilAttachment::getVkGpu() const {
100 SkASSERT(!this->wasDestroyed());
101 return static_cast<GrVkGpu*>(this->getGpu());
102}