• 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 "src/gpu/mtl/GrMtlGpu.h"
9#include "src/gpu/mtl/GrMtlUtil.h"
10
11#if !__has_feature(objc_arc)
12#error This file must be compiled with Arc. Use -fobjc-arc flag
13#endif
14
15GrMtlStencilAttachment::GrMtlStencilAttachment(GrMtlGpu* gpu,
16                                               const Format& format,
17                                               const id<MTLTexture> stencilView)
18    : GrStencilAttachment(gpu, stencilView.width, stencilView.height, format.fStencilBits,
19                          stencilView.sampleCount)
20    , fFormat(format)
21    , fStencilView(stencilView) {
22    this->registerWithCache(SkBudgeted::kYes);
23}
24
25GrMtlStencilAttachment* GrMtlStencilAttachment::Create(GrMtlGpu* gpu,
26                                                       int width,
27                                                       int height,
28                                                       int sampleCnt,
29                                                       const Format& format) {
30    MTLTextureDescriptor* desc =
31        [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format.fInternalFormat
32                                                           width:width
33                                                          height:height
34                                                       mipmapped:NO];
35    if (@available(macOS 10.11, iOS 9.0, *)) {
36        desc.storageMode = MTLStorageModePrivate;
37        desc.usage = MTLTextureUsageRenderTarget;
38    }
39    desc.sampleCount = sampleCnt;
40    if (sampleCnt > 1) {
41        desc.textureType = MTLTextureType2DMultisample;
42    }
43    return new GrMtlStencilAttachment(gpu, format, [gpu->device() newTextureWithDescriptor:desc]);
44}
45
46GrMtlStencilAttachment::~GrMtlStencilAttachment() {
47    // should have been released or abandoned first
48    SkASSERT(!fStencilView);
49}
50
51size_t GrMtlStencilAttachment::onGpuMemorySize() const {
52    uint64_t size = this->width();
53    size *= this->height();
54    size *= fFormat.fTotalBits;
55    size *= this->numSamples();
56    return static_cast<size_t>(size / 8);
57}
58
59void GrMtlStencilAttachment::onRelease() {
60    fStencilView = nullptr;
61    GrStencilAttachment::onRelease();
62}
63
64void GrMtlStencilAttachment::onAbandon() {
65    fStencilView = nullptr;
66    GrStencilAttachment::onAbandon();
67}
68
69GrMtlGpu* GrMtlStencilAttachment::getMtlGpu() const {
70    SkASSERT(!this->wasDestroyed());
71    return static_cast<GrMtlGpu*>(this->getGpu());
72}
73