Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
| 16 | GrVkStencilAttachment::GrVkStencilAttachment(GrVkGpu* gpu, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 17 | const Format& format, |
| 18 | const GrVkImage::ImageDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 19 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 20 | sk_sp<GrVkImageLayout> layout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 21 | const GrVkImageView* stencilView) |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 22 | : GrStencilAttachment(gpu, desc.fWidth, desc.fHeight, format.fStencilBits, desc.fSamples) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 23 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 24 | , fFormat(format) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 25 | , fStencilView(stencilView) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 26 | this->registerWithCache(SkBudgeted::kYes); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | stencilView->ref(); |
| 28 | } |
| 29 | |
| 30 | GrVkStencilAttachment* GrVkStencilAttachment::Create(GrVkGpu* gpu, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 43 | imageDesc.fUsageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 44 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 45 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 46 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 47 | GrVkImageInfo info; |
| 48 | if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 49 | return nullptr; |
| 50 | } |
| 51 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 52 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, info.fImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 53 | format.fInternalFormat, |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 54 | GrVkImageView::kStencil_Type, 1, |
| 55 | GrVkYcbcrConversionInfo()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 56 | if (!imageView) { |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 57 | GrVkImage::DestroyImageInfo(gpu, &info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 58 | return nullptr; |
| 59 | } |
| 60 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 61 | sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout)); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 62 | GrVkStencilAttachment* stencil = new GrVkStencilAttachment(gpu, format, imageDesc, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 63 | info, std::move(layout), imageView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 64 | imageView->unref(gpu); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 65 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 66 | return stencil; |
| 67 | } |
| 68 | |
| 69 | GrVkStencilAttachment::~GrVkStencilAttachment() { |
| 70 | // should have been released or abandoned first |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | SkASSERT(!fStencilView); |
| 72 | } |
| 73 | |
| 74 | size_t GrVkStencilAttachment::onGpuMemorySize() const { |
| 75 | uint64_t size = this->width(); |
| 76 | size *= this->height(); |
| 77 | size *= fFormat.fTotalBits; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 78 | size *= this->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 79 | return static_cast<size_t>(size / 8); |
| 80 | } |
| 81 | |
| 82 | void GrVkStencilAttachment::onRelease() { |
| 83 | GrVkGpu* gpu = this->getVkGpu(); |
| 84 | |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 85 | this->releaseImage(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 86 | |
| 87 | fStencilView->unref(gpu); |
| 88 | fStencilView = nullptr; |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 89 | GrStencilAttachment::onRelease(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void GrVkStencilAttachment::onAbandon() { |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 93 | this->abandonImage(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 94 | fStencilView->unrefAndAbandon(); |
| 95 | fStencilView = nullptr; |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 96 | GrStencilAttachment::onAbandon(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | GrVkGpu* GrVkStencilAttachment::getVkGpu() const { |
| 100 | SkASSERT(!this->wasDestroyed()); |
| 101 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 102 | } |