Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "GrMtlGpu.h" |
| 9 | #include "GrMtlUtil.h" |
| 10 | |
| 11 | GrMtlStencilAttachment::GrMtlStencilAttachment(GrMtlGpu* gpu, |
| 12 | const Format& format, |
| 13 | const id<MTLTexture> stencilView) |
| 14 | : GrStencilAttachment(gpu, stencilView.width, stencilView.height, format.fStencilBits, |
| 15 | stencilView.sampleCount) |
| 16 | , fFormat(format) |
| 17 | , fStencilView(stencilView) { |
| 18 | this->registerWithCache(SkBudgeted::kYes); |
| 19 | } |
| 20 | |
| 21 | GrMtlStencilAttachment* GrMtlStencilAttachment::Create(GrMtlGpu* gpu, |
| 22 | int width, |
| 23 | int height, |
| 24 | int sampleCnt, |
| 25 | const Format& format) { |
Jim Van Verth | 2e4287f | 2019-03-05 12:16:11 -0500 | [diff] [blame] | 26 | MTLTextureDescriptor* desc = |
| 27 | [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format.fInternalFormat |
| 28 | width:width |
| 29 | height:height |
| 30 | mipmapped:NO]; |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 31 | desc.resourceOptions = MTLResourceStorageModePrivate; |
Jim Van Verth | 2e4287f | 2019-03-05 12:16:11 -0500 | [diff] [blame] | 32 | desc.usage = MTLTextureUsageRenderTarget; |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 33 | return new GrMtlStencilAttachment(gpu, format, [gpu->device() newTextureWithDescriptor:desc]); |
| 34 | } |
| 35 | |
| 36 | GrMtlStencilAttachment::~GrMtlStencilAttachment() { |
| 37 | // should have been released or abandoned first |
| 38 | SkASSERT(!fStencilView); |
| 39 | } |
| 40 | |
| 41 | size_t GrMtlStencilAttachment::onGpuMemorySize() const { |
| 42 | uint64_t size = this->width(); |
| 43 | size *= this->height(); |
| 44 | size *= fFormat.fTotalBits; |
| 45 | size *= this->numSamples(); |
| 46 | return static_cast<size_t>(size / 8); |
| 47 | } |
| 48 | |
| 49 | void GrMtlStencilAttachment::onRelease() { |
| 50 | fStencilView = nullptr; |
| 51 | GrStencilAttachment::onRelease(); |
| 52 | } |
| 53 | |
| 54 | void GrMtlStencilAttachment::onAbandon() { |
| 55 | fStencilView = nullptr; |
| 56 | GrStencilAttachment::onAbandon(); |
| 57 | } |
| 58 | |
| 59 | GrMtlGpu* GrMtlStencilAttachment::getMtlGpu() const { |
| 60 | SkASSERT(!this->wasDestroyed()); |
| 61 | return static_cast<GrMtlGpu*>(this->getGpu()); |
| 62 | } |