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, |
| 19 | const GrVkImage::Resource* imageResource, |
| 20 | const GrVkImageView* stencilView) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 21 | : GrStencilAttachment(gpu, desc.fWidth, desc.fHeight, |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 22 | format.fStencilBits, desc.fSamples) |
| 23 | , GrVkImage(imageResource) |
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 | |
| 47 | const GrVkImage::Resource* imageResource = GrVkImage::CreateResource(gpu, imageDesc); |
| 48 | if (!imageResource) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, imageResource->fImage, |
| 53 | format.fInternalFormat, |
| 54 | GrVkImageView::kStencil_Type); |
| 55 | if (!imageView) { |
| 56 | imageResource->unref(gpu); |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 60 | GrVkStencilAttachment* stencil = new GrVkStencilAttachment(gpu, format, imageDesc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 61 | imageResource, imageView); |
| 62 | imageResource->unref(gpu); |
| 63 | imageView->unref(gpu); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 64 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 65 | return stencil; |
| 66 | } |
| 67 | |
| 68 | GrVkStencilAttachment::~GrVkStencilAttachment() { |
| 69 | // should have been released or abandoned first |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 70 | SkASSERT(!fStencilView); |
| 71 | } |
| 72 | |
| 73 | size_t GrVkStencilAttachment::onGpuMemorySize() const { |
| 74 | uint64_t size = this->width(); |
| 75 | size *= this->height(); |
| 76 | size *= fFormat.fTotalBits; |
| 77 | size *= SkTMax(1,this->numSamples()); |
| 78 | return static_cast<size_t>(size / 8); |
| 79 | } |
| 80 | |
| 81 | void GrVkStencilAttachment::onRelease() { |
| 82 | GrVkGpu* gpu = this->getVkGpu(); |
| 83 | |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 84 | this->releaseImage(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 85 | |
| 86 | fStencilView->unref(gpu); |
| 87 | fStencilView = nullptr; |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 88 | GrStencilAttachment::onRelease(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void GrVkStencilAttachment::onAbandon() { |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 92 | this->abandonImage(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | fStencilView->unrefAndAbandon(); |
| 94 | fStencilView = nullptr; |
egdaniel | e015c26 | 2016-02-26 13:12:00 -0800 | [diff] [blame] | 95 | GrStencilAttachment::onAbandon(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | GrVkGpu* GrVkStencilAttachment::getVkGpu() const { |
| 99 | SkASSERT(!this->wasDestroyed()); |
| 100 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 101 | } |