• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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    GrMipMapsStatus mipMapsStatus = mipLevels > 1 ? GrMipMapsStatus::kValid
44                                                  : GrMipMapsStatus::kNotAllocated;
45
46    return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus));
47}
48
49// This method parallels GrTextureProxy::highestFilterMode
50static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) {
51    if (GrPixelConfigIsSint(config)) {
52        // We only ever want to nearest-neighbor sample signed int textures.
53        return GrSamplerState::Filter::kNearest;
54    }
55    return GrSamplerState::Filter::kMipMap;
56}
57
58GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
59                           SkBudgeted budgeted,
60                           const GrSurfaceDesc& desc,
61                           id<MTLTexture> texture,
62                           GrMipMapsStatus mipMapsStatus)
63        : GrSurface(gpu, desc)
64        , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig),
65                    mipMapsStatus)
66        , 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}
82
83GrBackendTexture GrMtlTexture::getBackendTexture() const {
84    return GrBackendTexture(); // invalid
85}
86