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 desc.resourceOptions = MTLResourceStorageModePrivate; 36 desc.usage = MTLTextureUsageRenderTarget; 37 desc.sampleCount = sampleCnt; 38 if (sampleCnt > 1) { 39 desc.textureType = MTLTextureType2DMultisample; 40 } 41 return new GrMtlStencilAttachment(gpu, format, [gpu->device() newTextureWithDescriptor:desc]); 42} 43 44GrMtlStencilAttachment::~GrMtlStencilAttachment() { 45 // should have been released or abandoned first 46 SkASSERT(!fStencilView); 47} 48 49size_t GrMtlStencilAttachment::onGpuMemorySize() const { 50 uint64_t size = this->width(); 51 size *= this->height(); 52 size *= fFormat.fTotalBits; 53 size *= this->numSamples(); 54 return static_cast<size_t>(size / 8); 55} 56 57void GrMtlStencilAttachment::onRelease() { 58 fStencilView = nullptr; 59 GrStencilAttachment::onRelease(); 60} 61 62void GrMtlStencilAttachment::onAbandon() { 63 fStencilView = nullptr; 64 GrStencilAttachment::onAbandon(); 65} 66 67GrMtlGpu* GrMtlStencilAttachment::getMtlGpu() const { 68 SkASSERT(!this->wasDestroyed()); 69 return static_cast<GrMtlGpu*>(this->getGpu()); 70} 71