blob: 90bfcb38fb32672cac2ee0345e649311faeedeab [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
Timothy Liang2df9b452018-06-27 14:58:12 -040014// This method parallels GrTextureProxy::highestFilterMode
15static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) {
16 return GrSamplerState::Filter::kMipMap;
17}
18
19GrMtlTexture::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
32GrMtlTexture::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 Daniel4a081e22017-08-04 09:34:44 -040043sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040044 const GrSurfaceDesc& desc, int mipLevels) {
Greg Daniel4a081e22017-08-04 09:34:44 -040045 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 Osman9363ac42018-06-01 16:10:53 -040063 descriptor.usage = MTLTextureUsageShaderRead;
Greg Daniel4a081e22017-08-04 09:34:44 -040064
65 id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:descriptor];
66
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040067 GrMipMapsStatus mipMapsStatus = mipLevels > 1 ? GrMipMapsStatus::kValid
68 : GrMipMapsStatus::kNotAllocated;
69
70 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus));
Greg Daniel4a081e22017-08-04 09:34:44 -040071}
72
Greg Daniel4a081e22017-08-04 09:34:44 -040073GrMtlTexture::~GrMtlTexture() {
74 SkASSERT(nil == fTexture);
75}
76
77GrMtlGpu* GrMtlTexture::getMtlGpu() const {
78 SkASSERT(!this->wasDestroyed());
79 return static_cast<GrMtlGpu*>(this->getGpu());
80}
81
Robert Phillipsb67821d2017-12-13 15:00:45 -050082GrBackendTexture GrMtlTexture::getBackendTexture() const {
83 return GrBackendTexture(); // invalid
84}