1/* 2 * Copyright 2017 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 "GrMtlRenderTarget.h" 9 10#include "GrMtlGpu.h" 11#include "GrMtlUtil.h" 12 13sk_sp<GrMtlRenderTarget> GrMtlRenderTarget::CreateNewRenderTarget(GrMtlGpu* gpu, 14 const GrSurfaceDesc& desc, 15 SkBudgeted budgeted) { 16 MTLPixelFormat format; 17 if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) { 18 return nullptr; 19 } 20 21 if (desc.fSampleCnt) { 22 return nullptr; 23 } 24 25 MTLTextureDescriptor* descriptor = [[MTLTextureDescriptor alloc] init]; 26 descriptor.textureType = MTLTextureType2D; 27 descriptor.pixelFormat = format; 28 descriptor.width = desc.fWidth; 29 descriptor.height = desc.fHeight; 30 descriptor.depth = 1; 31 descriptor.mipmapLevelCount = 1; 32 descriptor.sampleCount = 1; 33 descriptor.arrayLength = 1; 34 // descriptor.resourceOptions This looks to be set by setting cpuCacheMode and storageModes 35 descriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined; 36 // RenderTargets never need to be mapped so their storage mode is set to private 37 descriptor.storageMode = MTLStorageModePrivate; 38 39 descriptor.usage = MTLTextureUsageRenderTarget; 40 41 id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:descriptor]; 42 43 return sk_sp<GrMtlRenderTarget>(new GrMtlRenderTarget(gpu, desc, budgeted, texture)); 44} 45 46GrMtlRenderTarget::GrMtlRenderTarget(GrMtlGpu* gpu, 47 const GrSurfaceDesc& desc, 48 SkBudgeted budgeted, 49 id<MTLTexture> renderTexture) 50 : GrSurface(gpu, desc) 51 , GrRenderTarget(gpu, desc) 52 , fRenderTexture(renderTexture) 53 , fResolveTexture(nil) { 54} 55 56GrMtlRenderTarget::~GrMtlRenderTarget() { 57 SkASSERT(nil == fRenderTexture); 58 SkASSERT(nil == fResolveTexture); 59} 60 61GrMtlGpu* GrMtlRenderTarget::getMtlGpu() const { 62 SkASSERT(!this->wasDestroyed()); 63 return static_cast<GrMtlGpu*>(this->getGpu()); 64} 65 66GrBackendObject GrMtlRenderTarget::getRenderTargetHandle() const { 67 void* voidRT = (__bridge_retained void*)fRenderTexture; 68 return (GrBackendObject)voidRT; 69} 70 71void GrMtlRenderTarget::onAbandon() { 72 fRenderTexture = nil; 73 fResolveTexture = nil; 74} 75 76void GrMtlRenderTarget::onRelease() { 77 fRenderTexture = nil; 78 fResolveTexture = nil; 79} 80 81bool completeStencilAttachment() { 82 // TODO: fill this out 83 return true; 84} 85 86 87