aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/mtl/GrMtlTextureRenderTarget.mm
blob: 99d1c9e3583634933dc637584609d4fd1e536333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrMtlTextureRenderTarget.h"
#include "GrMtlGpu.h"
#include "GrMtlUtil.h"

GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
                                                   SkBudgeted budgeted,
                                                   const GrSurfaceDesc& desc,
                                                   id<MTLTexture> renderTexture,
                                                   GrMipMapsStatus mipMapsStatus)
        : GrSurface(gpu, desc)
        , GrMtlTexture(gpu, desc, renderTexture, mipMapsStatus)
        , GrMtlRenderTarget(gpu, desc, renderTexture) {
    this->registerWithCache(budgeted);
}

sk_sp<GrMtlTextureRenderTarget>
GrMtlTextureRenderTarget::Make(GrMtlGpu* gpu,
                               const GrSurfaceDesc& desc,
                               id<MTLTexture> renderTexture,
                               int mipLevels,
                               SkBudgeted budgeted,
                               bool isWrapped) {
    SkASSERT(nil != renderTexture);
    if (desc.fSampleCnt > 1) {
        SkASSERT(false); // Currently don't support MSAA
        return nullptr;
    }
    GrMipMapsStatus mipMapsStatus = mipLevels > 1 ? GrMipMapsStatus::kValid
                                                  : GrMipMapsStatus::kNotAllocated;
    if (!isWrapped) {
        return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(gpu,
                                                                            budgeted,
                                                                            desc,
                                                                            renderTexture,
                                                                            mipMapsStatus));
    } else {
        return nullptr; // Currently don't support wrapped TextureRenderTargets
    }
}


sk_sp<GrMtlTextureRenderTarget>
GrMtlTextureRenderTarget::CreateNewTextureRenderTarget(GrMtlGpu* gpu,
                                                       SkBudgeted budgeted,
                                                       const GrSurfaceDesc& desc,
                                                       int mipLevels) {
    MTLPixelFormat format;
    if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) {
        return nullptr;
    }

    MTLTextureDescriptor* descriptor = [[MTLTextureDescriptor alloc] init];
    descriptor.textureType = MTLTextureType2D;
    descriptor.pixelFormat = format;
    descriptor.width = desc.fWidth;
    descriptor.height = desc.fHeight;
    descriptor.depth = 1;
    descriptor.mipmapLevelCount = mipLevels;
    descriptor.sampleCount = 1;
    descriptor.arrayLength = 1;
    // descriptor.resourceOptions This looks to be set by setting cpuCacheMode and storageModes
    descriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined;
    // RenderTargets never need to be mapped so their storage mode is set to private
    descriptor.storageMode = MTLStorageModePrivate;

    descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;

    id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:descriptor];

    return Make(gpu, desc, texture, mipLevels, budgeted, false);
}

sk_sp<GrMtlTextureRenderTarget>
GrMtlTextureRenderTarget::MakeWrappedTextureRenderTarget(GrMtlGpu* gpu,
                                                         const GrSurfaceDesc& desc,
                                                         GrWrapOwnership wrapOwnership) {
    return nullptr;
}