Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / graphite / mtl / MtlTexture.mm
1 /*
2  * Copyright 2021 Google LLC
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 "src/gpu/graphite/mtl/MtlTexture.h"
9
10 #include "include/gpu/graphite/mtl/MtlTypes.h"
11 #include "include/private/gpu/graphite/MtlTypesPriv.h"
12 #include "src/gpu/graphite/mtl/MtlCaps.h"
13 #include "src/gpu/graphite/mtl/MtlGpu.h"
14 #include "src/gpu/graphite/mtl/MtlUtils.h"
15
16 namespace skgpu::graphite {
17
18 sk_cfp<id<MTLTexture>> MtlTexture::MakeMtlTexture(const MtlGpu* gpu,
19                                                   SkISize dimensions,
20                                                   const TextureInfo& info) {
21     const skgpu::graphite::Caps* caps = gpu->caps();
22     if (dimensions.width() > caps->maxTextureSize() ||
23         dimensions.height() > caps->maxTextureSize()) {
24         return nullptr;
25     }
26
27     const MtlTextureSpec& mtlSpec = info.mtlTextureSpec();
28     SkASSERT(!mtlSpec.fFramebufferOnly);
29
30     if (mtlSpec.fUsage & MTLTextureUsageShaderRead && !caps->isTexturable(info)) {
31         return nullptr;
32     }
33
34     if (mtlSpec.fUsage & MTLTextureUsageRenderTarget &&
35         !(caps->isRenderable(info) || MtlFormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat))) {
36         return nullptr;
37     }
38
39     sk_cfp<MTLTextureDescriptor*> desc([[MTLTextureDescriptor alloc] init]);
40     (*desc).textureType = (info.numSamples() > 1) ? MTLTextureType2DMultisample : MTLTextureType2D;
41     (*desc).pixelFormat = (MTLPixelFormat)mtlSpec.fFormat;
42     (*desc).width = dimensions.width();
43     (*desc).height = dimensions.height();
44     (*desc).depth = 1;
45     (*desc).mipmapLevelCount = info.numMipLevels();
46     (*desc).sampleCount = info.numSamples();
47     (*desc).arrayLength = 1;
48     (*desc).usage = mtlSpec.fUsage;
49     (*desc).storageMode = (MTLStorageMode)mtlSpec.fStorageMode;
50
51     sk_cfp<id<MTLTexture>> texture([gpu->device() newTextureWithDescriptor:desc.get()]);
52 #ifdef SK_ENABLE_MTL_DEBUG_INFO
53     if (mtlSpec.fUsage & MTLTextureUsageRenderTarget) {
54         if (MtlFormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat)) {
55             (*texture).label = @"DepthStencil";
56         } else {
57             if (info.numSamples() > 1) {
58                 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) {
59                     (*texture).label = @"MSAA SampledTexture-ColorAttachment";
60                 } else {
61                     (*texture).label = @"MSAA ColorAttachment";
62                 }
63             } else {
64                 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) {
65                     (*texture).label = @"SampledTexture-ColorAttachment";
66                 } else {
67                     (*texture).label = @"ColorAttachment";
68                 }
69             }
70         }
71     } else {
72         SkASSERT(mtlSpec.fUsage & MTLTextureUsageShaderRead);
73         (*texture).label = @"SampledTexture";
74     }
75 #endif
76
77     return texture;
78 }
79
80 MtlTexture::MtlTexture(const MtlGpu* gpu,
81                        SkISize dimensions,
82                        const TextureInfo& info,
83                        sk_cfp<id<MTLTexture>> texture,
84                        Ownership ownership,
85                        SkBudgeted budgeted)
86         : Texture(gpu, dimensions, info, ownership, budgeted)
87         , fTexture(std::move(texture)) {}
88
89 sk_sp<Texture> MtlTexture::Make(const MtlGpu* gpu,
90                                 SkISize dimensions,
91                                 const TextureInfo& info,
92                                 SkBudgeted budgeted) {
93     sk_cfp<id<MTLTexture>> texture = MakeMtlTexture(gpu, dimensions, info);
94     if (!texture) {
95         return nullptr;
96     }
97     return sk_sp<Texture>(new MtlTexture(gpu,
98                                          dimensions,
99                                          info,
100                                          std::move(texture),
101                                          Ownership::kOwned,
102                                          budgeted));
103 }
104
105 sk_sp<Texture> MtlTexture::MakeWrapped(const MtlGpu* gpu,
106                                        SkISize dimensions,
107                                        const TextureInfo& info,
108                                        sk_cfp<id<MTLTexture>> texture) {
109     return sk_sp<Texture>(new MtlTexture(gpu,
110                                          dimensions,
111                                          info,
112                                          std::move(texture),
113                                          Ownership::kWrapped,
114                                          SkBudgeted::kNo));
115 }
116
117 void MtlTexture::freeGpuData() {
118     fTexture.reset();
119 }
120
121 } // namespace skgpu::graphite
122