• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 =
27        [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format.fInternalFormat
28                                                           width:width
29                                                          height:height
30                                                       mipmapped:NO];
31    desc.resourceOptions = MTLResourceStorageModePrivate;
32    desc.usage = MTLTextureUsageRenderTarget;
33    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}
63