blob: ec7f3b1750a40ec40b15fdc9bb2c0ada94b9c40a [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,
15 const GrSurfaceDesc& desc, int mipLevels) {
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
43 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipLevels > 1));
44}
45
46// This method parallels GrTextureProxy::highestFilterMode
47static inline GrSamplerParams::FilterMode highest_filter_mode(GrPixelConfig config) {
48 if (GrPixelConfigIsSint(config)) {
49 // We only ever want to nearest-neighbor sample signed int textures.
50 return GrSamplerParams::kNone_FilterMode;
51 }
52 return GrSamplerParams::kMipMap_FilterMode;
53}
54
55GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
56 SkBudgeted budgeted,
57 const GrSurfaceDesc& desc,
58 id<MTLTexture> texture,
59 bool isMipMapped)
60 : GrSurface(gpu, desc)
61 , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig),
62 isMipMapped)
63 , fTexture(texture) {
64}
65
66GrMtlTexture::~GrMtlTexture() {
67 SkASSERT(nil == fTexture);
68}
69
70GrMtlGpu* GrMtlTexture::getMtlGpu() const {
71 SkASSERT(!this->wasDestroyed());
72 return static_cast<GrMtlGpu*>(this->getGpu());
73}
74
75GrBackendObject GrMtlTexture::getTextureHandle() const {
76 void* voidTex = (__bridge_retained void*)fTexture;
77 return (GrBackendObject)voidTex;
78}