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
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) {
26    MTLTextureDescriptor* desc = [MTLTextureDescriptor
27                              texture2DDescriptorWithPixelFormat:MTLPixelFormatStencil8
28                              width:width
29                              height:height
30                              mipmapped:NO];
31    desc.resourceOptions = MTLResourceStorageModePrivate;
32    return new GrMtlStencilAttachment(gpu, format, [gpu->device() newTextureWithDescriptor:desc]);
33}
34
35GrMtlStencilAttachment::~GrMtlStencilAttachment() {
36    // should have been released or abandoned first
37    SkASSERT(!fStencilView);
38}
39
40size_t GrMtlStencilAttachment::onGpuMemorySize() const {
41    uint64_t size = this->width();
42    size *= this->height();
43    size *= fFormat.fTotalBits;
44    size *= this->numSamples();
45    return static_cast<size_t>(size / 8);
46}
47
48void GrMtlStencilAttachment::onRelease() {
49    fStencilView = nullptr;
50    GrStencilAttachment::onRelease();
51}
52
53void GrMtlStencilAttachment::onAbandon() {
54    fStencilView = nullptr;
55    GrStencilAttachment::onAbandon();
56}
57
58GrMtlGpu* GrMtlStencilAttachment::getMtlGpu() const {
59    SkASSERT(!this->wasDestroyed());
60    return static_cast<GrMtlGpu*>(this->getGpu());
61}
62