blob: 5348885a6bd1c41fb46ee45d34178fa9442854ba [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 Daniel164a9f02016-02-22 09:56:40 -050020 const GrVkImageView* stencilView)
robertphillips55fdccc2016-06-06 06:16:20 -070021 : GrStencilAttachment(gpu, desc.fWidth, desc.fHeight, format.fStencilBits, desc.fSamples)
Greg Daniel1591c382017-08-17 15:37:20 -040022 , GrVkImage(info, GrBackendObjectOwnership::kOwned)
Greg Daniel164a9f02016-02-22 09:56:40 -050023 , fFormat(format)
Greg Daniel164a9f02016-02-22 09:56:40 -050024 , fStencilView(stencilView) {
kkinnunen2e6055b2016-04-22 01:48:29 -070025 this->registerWithCache(SkBudgeted::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -050026 stencilView->ref();
27}
28
29GrVkStencilAttachment* GrVkStencilAttachment::Create(GrVkGpu* gpu,
Greg Daniel164a9f02016-02-22 09:56:40 -050030 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;
egdaniel58a8d922016-04-21 08:03:10 -070042 imageDesc.fUsageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
43 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -050044 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
45
egdanielb2df0c22016-05-13 11:30:37 -070046 GrVkImageInfo info;
47 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
Greg Daniel164a9f02016-02-22 09:56:40 -050048 return nullptr;
49 }
50
egdanielb2df0c22016-05-13 11:30:37 -070051 const GrVkImageView* imageView = GrVkImageView::Create(gpu, info.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -050052 format.fInternalFormat,
jvanverth62340062016-04-26 08:01:44 -070053 GrVkImageView::kStencil_Type, 1);
Greg Daniel164a9f02016-02-22 09:56:40 -050054 if (!imageView) {
jvanverth1e305ba2016-06-01 09:39:15 -070055 GrVkImage::DestroyImageInfo(gpu, &info);
Greg Daniel164a9f02016-02-22 09:56:40 -050056 return nullptr;
57 }
58
kkinnunen2e6055b2016-04-22 01:48:29 -070059 GrVkStencilAttachment* stencil = new GrVkStencilAttachment(gpu, format, imageDesc,
egdanielb2df0c22016-05-13 11:30:37 -070060 info, imageView);
Greg Daniel164a9f02016-02-22 09:56:40 -050061 imageView->unref(gpu);
halcanary9d524f22016-03-29 09:03:52 -070062
Greg Daniel164a9f02016-02-22 09:56:40 -050063 return stencil;
64}
65
66GrVkStencilAttachment::~GrVkStencilAttachment() {
67 // should have been released or abandoned first
Greg Daniel164a9f02016-02-22 09:56:40 -050068 SkASSERT(!fStencilView);
69}
70
71size_t GrVkStencilAttachment::onGpuMemorySize() const {
72 uint64_t size = this->width();
73 size *= this->height();
74 size *= fFormat.fTotalBits;
Brian Salomon3a2cc2c2018-02-03 00:25:12 +000075 size *= SkTMax(1,this->numSamples());
Greg Daniel164a9f02016-02-22 09:56:40 -050076 return static_cast<size_t>(size / 8);
77}
78
79void GrVkStencilAttachment::onRelease() {
80 GrVkGpu* gpu = this->getVkGpu();
81
egdaniele015c262016-02-26 13:12:00 -080082 this->releaseImage(gpu);
Greg Daniel164a9f02016-02-22 09:56:40 -050083
84 fStencilView->unref(gpu);
85 fStencilView = nullptr;
egdaniele015c262016-02-26 13:12:00 -080086 GrStencilAttachment::onRelease();
Greg Daniel164a9f02016-02-22 09:56:40 -050087}
88
89void GrVkStencilAttachment::onAbandon() {
egdaniele015c262016-02-26 13:12:00 -080090 this->abandonImage();
Greg Daniel164a9f02016-02-22 09:56:40 -050091 fStencilView->unrefAndAbandon();
92 fStencilView = nullptr;
egdaniele015c262016-02-26 13:12:00 -080093 GrStencilAttachment::onAbandon();
Greg Daniel164a9f02016-02-22 09:56:40 -050094}
95
96GrVkGpu* GrVkStencilAttachment::getVkGpu() const {
97 SkASSERT(!this->wasDestroyed());
98 return static_cast<GrVkGpu*>(this->getGpu());
99}