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 | |
| 14 | sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 15 | const GrSurfaceDesc& desc, int mipLevels) { |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 16 | MTLPixelFormat format; |
| 17 | if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) { |
| 18 | return nullptr; |
| 19 | } |
| 20 | |
| 21 | MTLTextureDescriptor* descriptor = [[MTLTextureDescriptor alloc] init]; |
| 22 | descriptor.textureType = MTLTextureType2D; |
| 23 | descriptor.pixelFormat = format; |
| 24 | descriptor.width = desc.fWidth; |
| 25 | descriptor.height = desc.fHeight; |
| 26 | descriptor.depth = 1; |
| 27 | descriptor.mipmapLevelCount = mipLevels; |
| 28 | descriptor.sampleCount = 1; |
| 29 | descriptor.arrayLength = 1; |
| 30 | // descriptor.resourceOptions This looks to be set by setting cpuCacheMode and storageModes |
| 31 | descriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined; |
| 32 | // Shared is not available on MacOS. Is there a reason to want managed to allow mapping? |
| 33 | descriptor.storageMode = MTLStorageModePrivate; |
| 34 | |
| 35 | MTLTextureUsage texUsage = MTLTextureUsageShaderRead; |
| 36 | if (GrMTLFormatIsSRGB(format, nullptr)) { |
| 37 | texUsage |= MTLTextureUsagePixelFormatView; |
| 38 | } |
| 39 | descriptor.usage = texUsage; |
| 40 | |
| 41 | id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:descriptor]; |
| 42 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 43 | GrMipMapsStatus mipMapsStatus = mipLevels > 1 ? GrMipMapsStatus::kValid |
| 44 | : GrMipMapsStatus::kNotAllocated; |
| 45 | |
| 46 | return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus)); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // This method parallels GrTextureProxy::highestFilterMode |
Brian Salomon | b3e39f6 | 2017-09-07 13:59:50 -0400 | [diff] [blame] | 50 | static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) { |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 51 | if (GrPixelConfigIsSint(config)) { |
| 52 | // We only ever want to nearest-neighbor sample signed int textures. |
Brian Salomon | b3e39f6 | 2017-09-07 13:59:50 -0400 | [diff] [blame] | 53 | return GrSamplerState::Filter::kNearest; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 54 | } |
Brian Salomon | b3e39f6 | 2017-09-07 13:59:50 -0400 | [diff] [blame] | 55 | return GrSamplerState::Filter::kMipMap; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu, |
| 59 | SkBudgeted budgeted, |
| 60 | const GrSurfaceDesc& desc, |
| 61 | id<MTLTexture> texture, |
Greg Daniel | 18e5cbb | 2017-10-12 12:49:08 -0400 | [diff] [blame] | 62 | GrMipMapsStatus mipMapsStatus) |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 63 | : GrSurface(gpu, desc) |
| 64 | , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig), |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 65 | mipMapsStatus) |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 66 | , fTexture(texture) { |
| 67 | } |
| 68 | |
| 69 | GrMtlTexture::~GrMtlTexture() { |
| 70 | SkASSERT(nil == fTexture); |
| 71 | } |
| 72 | |
| 73 | GrMtlGpu* GrMtlTexture::getMtlGpu() const { |
| 74 | SkASSERT(!this->wasDestroyed()); |
| 75 | return static_cast<GrMtlGpu*>(this->getGpu()); |
| 76 | } |
| 77 | |
| 78 | GrBackendObject GrMtlTexture::getTextureHandle() const { |
| 79 | void* voidTex = (__bridge_retained void*)fTexture; |
| 80 | return (GrBackendObject)voidTex; |
| 81 | } |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame^] | 82 | |
| 83 | GrBackendTexture GrMtlTexture::getBackendTexture() const { |
| 84 | return GrBackendTexture(); // invalid |
| 85 | } |