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 "GrMtlTexture.h" 9 10#include "GrMtlGpu.h" 11#include "GrMtlUtil.h" 12#include "GrTexturePriv.h" 13 14GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, 15 SkBudgeted budgeted, 16 const GrSurfaceDesc& desc, 17 id<MTLTexture> texture, 18 GrMipMapsStatus mipMapsStatus) 19 : GrSurface(gpu, desc) 20 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) 21 , fTexture(texture) { 22 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); 23 this->registerWithCache(budgeted); 24} 25 26GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, 27 Wrapped, 28 const GrSurfaceDesc& desc, 29 id<MTLTexture> texture, 30 GrMipMapsStatus mipMapsStatus, 31 GrWrapCacheable cacheable, 32 GrIOType ioType) 33 : GrSurface(gpu, desc) 34 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) 35 , fTexture(texture) { 36 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); 37 if (ioType == kRead_GrIOType) { 38 this->setReadOnly(); 39 } 40 this->registerWithCacheWrapped(cacheable); 41} 42 43GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, 44 const GrSurfaceDesc& desc, 45 id<MTLTexture> texture, 46 GrMipMapsStatus mipMapsStatus) 47 : GrSurface(gpu, desc) 48 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) 49 , fTexture(texture) { 50 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); 51} 52 53sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted, 54 const GrSurfaceDesc& desc, 55 MTLTextureDescriptor* texDesc, 56 GrMipMapsStatus mipMapsStatus) { 57 if (desc.fSampleCnt > 1) { 58 SkASSERT(false); // Currently we don't support msaa 59 return nullptr; 60 } 61 id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:texDesc]; 62 SkASSERT(nil != texture); 63 SkASSERT(MTLTextureUsageShaderRead & texture.usage); 64 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus)); 65} 66 67sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu, 68 const GrSurfaceDesc& desc, 69 id<MTLTexture> texture, 70 GrWrapCacheable cacheable, 71 GrIOType ioType) { 72 if (desc.fSampleCnt > 1) { 73 SkASSERT(false); // Currently we don't support msaa 74 return nullptr; 75 } 76 SkASSERT(nil != texture); 77 SkASSERT(MTLTextureUsageShaderRead & texture.usage); 78 GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid 79 : GrMipMapsStatus::kNotAllocated; 80 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus, 81 cacheable, ioType)); 82} 83 84GrMtlTexture::~GrMtlTexture() { 85 SkASSERT(nil == fTexture); 86} 87 88GrMtlGpu* GrMtlTexture::getMtlGpu() const { 89 SkASSERT(!this->wasDestroyed()); 90 return static_cast<GrMtlGpu*>(this->getGpu()); 91} 92 93GrBackendTexture GrMtlTexture::getBackendTexture() const { 94 GrMipMapped mipMapped = fTexture.mipmapLevelCount > 1 ? GrMipMapped::kYes 95 : GrMipMapped::kNo; 96 GrMtlTextureInfo info; 97 info.fTexture = GrGetPtrFromId(fTexture); 98 return GrBackendTexture(this->width(), this->height(), mipMapped, info); 99} 100 101GrBackendFormat GrMtlTexture::backendFormat() const { 102 return GrBackendFormat::MakeMtl(fTexture.pixelFormat); 103} 104 105