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) |
| 25 | , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig), |
| 26 | mipMapsStatus) |
| 27 | , fTexture(texture) { |
| 28 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
| 29 | this->registerWithCache(budgeted); |
| 30 | } |
| 31 | |
| 32 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
| 33 | const GrSurfaceDesc& desc, |
| 34 | id<MTLTexture> texture, |
| 35 | GrMipMapsStatus mipMapsStatus) |
| 36 | : GrSurface(gpu, desc) |
| 37 | , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig), |
| 38 | mipMapsStatus) |
| 39 | , fTexture(texture) { |
| 40 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount)); |
| 41 | } |
| 42 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 43 | sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 44 | const GrSurfaceDesc& desc, int mipLevels) { |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 45 | MTLPixelFormat format; |
| 46 | if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) { |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
| 50 | MTLTextureDescriptor* descriptor = [[MTLTextureDescriptor alloc] init]; |
| 51 | descriptor.textureType = MTLTextureType2D; |
| 52 | descriptor.pixelFormat = format; |
| 53 | descriptor.width = desc.fWidth; |
| 54 | descriptor.height = desc.fHeight; |
| 55 | descriptor.depth = 1; |
| 56 | descriptor.mipmapLevelCount = mipLevels; |
| 57 | descriptor.sampleCount = 1; |
| 58 | descriptor.arrayLength = 1; |
| 59 | // descriptor.resourceOptions This looks to be set by setting cpuCacheMode and storageModes |
| 60 | descriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined; |
| 61 | // Shared is not available on MacOS. Is there a reason to want managed to allow mapping? |
| 62 | descriptor.storageMode = MTLStorageModePrivate; |
Brian Osman | 9363ac4 | 2018-06-01 16:10:53 -0400 | [diff] [blame] | 63 | descriptor.usage = MTLTextureUsageShaderRead; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 64 | |
| 65 | id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:descriptor]; |
| 66 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 67 | GrMipMapsStatus mipMapsStatus = mipLevels > 1 ? GrMipMapsStatus::kValid |
| 68 | : GrMipMapsStatus::kNotAllocated; |
| 69 | |
| 70 | return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus)); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 73 | GrMtlTexture::~GrMtlTexture() { |
| 74 | SkASSERT(nil == fTexture); |
| 75 | } |
| 76 | |
| 77 | GrMtlGpu* GrMtlTexture::getMtlGpu() const { |
| 78 | SkASSERT(!this->wasDestroyed()); |
| 79 | return static_cast<GrMtlGpu*>(this->getGpu()); |
| 80 | } |
| 81 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 82 | GrBackendTexture GrMtlTexture::getBackendTexture() const { |
| 83 | return GrBackendTexture(); // invalid |
| 84 | } |