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 | // This method parallels GrTextureProxy::highestFilterMode |
| 15 | static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) { |
| 16 | return GrSamplerState::Filter::kMipMap; |
| 17 | } |
| 18 | |
| 19 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
| 20 | SkBudgeted budgeted, |
| 21 | const GrSurfaceDesc& desc, |
| 22 | id<MTLTexture> texture, |
| 23 | GrMipMapsStatus mipMapsStatus) |
| 24 | : GrSurface(gpu, desc) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 25 | , INHERITED(gpu, desc, GrTextureType::k2D, highest_filter_mode(desc.fConfig), mipMapsStatus) |
Timothy Liang | 2df9b45 | 2018-06-27 14:58:12 -0400 | [diff] [blame] | 26 | , fTexture(texture) { |
| 27 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
| 28 | this->registerWithCache(budgeted); |
| 29 | } |
| 30 | |
| 31 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 32 | Wrapped, |
| 33 | const GrSurfaceDesc& desc, |
| 34 | id<MTLTexture> texture, |
| 35 | GrMipMapsStatus mipMapsStatus) |
| 36 | : GrSurface(gpu, desc) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 37 | , INHERITED(gpu, desc, GrTextureType::k2D, highest_filter_mode(desc.fConfig), mipMapsStatus) |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 38 | , fTexture(texture) { |
| 39 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
| 40 | this->registerWithCacheWrapped(); |
| 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 | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 48 | , INHERITED(gpu, desc, GrTextureType::k2D, highest_filter_mode(desc.fConfig), 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, |
| 69 | id<MTLTexture> texture) { |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 70 | if (desc.fSampleCnt > 1) { |
| 71 | SkASSERT(false); // Currently we don't support msaa |
| 72 | return nullptr; |
| 73 | } |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 74 | SkASSERT(nil != texture); |
| 75 | SkASSERT(MTLTextureUsageShaderRead & texture.usage); |
| 76 | GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid |
| 77 | : GrMipMapsStatus::kNotAllocated; |
| 78 | return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus)); |
| 79 | } |
| 80 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 81 | GrMtlTexture::~GrMtlTexture() { |
| 82 | SkASSERT(nil == fTexture); |
| 83 | } |
| 84 | |
| 85 | GrMtlGpu* GrMtlTexture::getMtlGpu() const { |
| 86 | SkASSERT(!this->wasDestroyed()); |
| 87 | return static_cast<GrMtlGpu*>(this->getGpu()); |
| 88 | } |
| 89 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 90 | GrBackendTexture GrMtlTexture::getBackendTexture() const { |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 91 | GrMipMapped mipMapped = fTexture.mipmapLevelCount > 1 ? GrMipMapped::kYes |
| 92 | : GrMipMapped::kNo; |
| 93 | GrMtlTextureInfo info; |
| 94 | info.fTexture = GrGetPtrFromId(fTexture); |
| 95 | return GrBackendTexture(this->width(), this->height(), mipMapped, info); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 96 | } |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 97 | |