blob: 26e5918faf341465425b079dd9a6ae4a25fe13ee [file] [log] [blame]
Greg Daniel4a081e22017-08-04 09:34:44 -04001/*
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
14sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040015 const GrSurfaceDesc& desc, int mipLevels) {
Greg Daniel4a081e22017-08-04 09:34:44 -040016 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 Daniel0fc4d2d2017-10-12 11:23:36 -040043 GrMipMapsStatus mipMapsStatus = mipLevels > 1 ? GrMipMapsStatus::kValid
44 : GrMipMapsStatus::kNotAllocated;
45
46 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus));
Greg Daniel4a081e22017-08-04 09:34:44 -040047}
48
49// This method parallels GrTextureProxy::highestFilterMode
Brian Salomonb3e39f62017-09-07 13:59:50 -040050static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) {
Greg Daniel4a081e22017-08-04 09:34:44 -040051 if (GrPixelConfigIsSint(config)) {
52 // We only ever want to nearest-neighbor sample signed int textures.
Brian Salomonb3e39f62017-09-07 13:59:50 -040053 return GrSamplerState::Filter::kNearest;
Greg Daniel4a081e22017-08-04 09:34:44 -040054 }
Brian Salomonb3e39f62017-09-07 13:59:50 -040055 return GrSamplerState::Filter::kMipMap;
Greg Daniel4a081e22017-08-04 09:34:44 -040056}
57
58GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
59 SkBudgeted budgeted,
60 const GrSurfaceDesc& desc,
61 id<MTLTexture> texture,
Greg Daniel18e5cbb2017-10-12 12:49:08 -040062 GrMipMapsStatus mipMapsStatus)
Greg Daniel4a081e22017-08-04 09:34:44 -040063 : GrSurface(gpu, desc)
64 , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040065 mipMapsStatus)
Greg Daniel4a081e22017-08-04 09:34:44 -040066 , fTexture(texture) {
67}
68
69GrMtlTexture::~GrMtlTexture() {
70 SkASSERT(nil == fTexture);
71}
72
73GrMtlGpu* GrMtlTexture::getMtlGpu() const {
74 SkASSERT(!this->wasDestroyed());
75 return static_cast<GrMtlGpu*>(this->getGpu());
76}
77
78GrBackendObject GrMtlTexture::getTextureHandle() const {
79 void* voidTex = (__bridge_retained void*)fTexture;
80 return (GrBackendObject)voidTex;
81}
Robert Phillipsb67821d2017-12-13 15:00:45 -050082
83GrBackendTexture GrMtlTexture::getBackendTexture() const {
84 return GrBackendTexture(); // invalid
85}