blob: a2d8fa3539a1e7cea98b5577aee37dfe05f0b757 [file] [log] [blame]
Ethan Nicholas01063512018-10-08 16:58:25 -04001/*
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
11GrMtlStencilAttachment::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
21GrMtlStencilAttachment* GrMtlStencilAttachment::Create(GrMtlGpu* gpu,
22 int width,
23 int height,
24 int sampleCnt,
25 const Format& format) {
Jim Van Verth2e4287f2019-03-05 12:16:11 -050026 MTLTextureDescriptor* desc =
27 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format.fInternalFormat
28 width:width
29 height:height
30 mipmapped:NO];
Ethan Nicholas01063512018-10-08 16:58:25 -040031 desc.resourceOptions = MTLResourceStorageModePrivate;
Jim Van Verth2e4287f2019-03-05 12:16:11 -050032 desc.usage = MTLTextureUsageRenderTarget;
Ethan Nicholas01063512018-10-08 16:58:25 -040033 return new GrMtlStencilAttachment(gpu, format, [gpu->device() newTextureWithDescriptor:desc]);
34}
35
36GrMtlStencilAttachment::~GrMtlStencilAttachment() {
37 // should have been released or abandoned first
38 SkASSERT(!fStencilView);
39}
40
41size_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
49void GrMtlStencilAttachment::onRelease() {
50 fStencilView = nullptr;
51 GrStencilAttachment::onRelease();
52}
53
54void GrMtlStencilAttachment::onAbandon() {
55 fStencilView = nullptr;
56 GrStencilAttachment::onAbandon();
57}
58
59GrMtlGpu* GrMtlStencilAttachment::getMtlGpu() const {
60 SkASSERT(!this->wasDestroyed());
61 return static_cast<GrMtlGpu*>(this->getGpu());
62}