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