Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 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 | |
Timothy Liang | 2df9b45 | 2018-06-27 14:58:12 -0400 | [diff] [blame] | 14 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
| 15 | SkBudgeted budgeted, |
| 16 | const GrSurfaceDesc& desc, |
| 17 | id<MTLTexture> texture, |
| 18 | GrMipMapsStatus mipMapsStatus) |
| 19 | : GrSurface(gpu, desc) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 20 | , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) |
Timothy Liang | 2df9b45 | 2018-06-27 14:58:12 -0400 | [diff] [blame] | 21 | , fTexture(texture) { |
| 22 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
| 23 | this->registerWithCache(budgeted); |
| 24 | } |
| 25 | |
| 26 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 27 | Wrapped, |
| 28 | const GrSurfaceDesc& desc, |
| 29 | id<MTLTexture> texture, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 30 | GrMipMapsStatus mipMapsStatus, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 31 | GrWrapCacheable cacheable, |
| 32 | GrIOType ioType) |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 33 | : GrSurface(gpu, desc) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 34 | , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 35 | , fTexture(texture) { |
| 36 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 37 | if (ioType == kRead_GrIOType) { |
| 38 | this->setReadOnly(); |
| 39 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 40 | this->registerWithCacheWrapped(cacheable); |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
Timothy Liang | 2df9b45 | 2018-06-27 14:58:12 -0400 | [diff] [blame] | 44 | const GrSurfaceDesc& desc, |
| 45 | id<MTLTexture> texture, |
| 46 | GrMipMapsStatus mipMapsStatus) |
| 47 | : GrSurface(gpu, desc) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 48 | , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) |
Timothy Liang | 2df9b45 | 2018-06-27 14:58:12 -0400 | [diff] [blame] | 49 | , fTexture(texture) { |
| 50 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
| 51 | } |
| 52 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 53 | sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted, |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 54 | const GrSurfaceDesc& desc, |
| 55 | MTLTextureDescriptor* texDesc, |
| 56 | GrMipMapsStatus mipMapsStatus) { |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 57 | if (desc.fSampleCnt > 1) { |
| 58 | SkASSERT(false); // Currently we don't support msaa |
| 59 | return nullptr; |
| 60 | } |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 61 | id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:texDesc]; |
| 62 | SkASSERT(nil != texture); |
| 63 | SkASSERT(MTLTextureUsageShaderRead & texture.usage); |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 64 | return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus)); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 67 | sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu, |
| 68 | const GrSurfaceDesc& desc, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 69 | id<MTLTexture> texture, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 70 | GrWrapCacheable cacheable, |
| 71 | GrIOType ioType) { |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 72 | if (desc.fSampleCnt > 1) { |
| 73 | SkASSERT(false); // Currently we don't support msaa |
| 74 | return nullptr; |
| 75 | } |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 76 | SkASSERT(nil != texture); |
| 77 | SkASSERT(MTLTextureUsageShaderRead & texture.usage); |
| 78 | GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid |
| 79 | : GrMipMapsStatus::kNotAllocated; |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 80 | return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus, |
| 81 | cacheable, ioType)); |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 84 | GrMtlTexture::~GrMtlTexture() { |
| 85 | SkASSERT(nil == fTexture); |
| 86 | } |
| 87 | |
| 88 | GrMtlGpu* GrMtlTexture::getMtlGpu() const { |
| 89 | SkASSERT(!this->wasDestroyed()); |
| 90 | return static_cast<GrMtlGpu*>(this->getGpu()); |
| 91 | } |
| 92 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 93 | GrBackendTexture GrMtlTexture::getBackendTexture() const { |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 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); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 99 | } |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 100 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 101 | GrBackendFormat GrMtlTexture::backendFormat() const { |
| 102 | return GrBackendFormat::MakeMtl(fTexture.pixelFormat); |
| 103 | } |
| 104 | |