Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / ganesh / mtl / GrMtlTexture.mm
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 "src/gpu/ganesh/mtl/GrMtlTexture.h"
9
10 #include "src/gpu/ganesh/GrTexture.h"
11 #include "src/gpu/ganesh/mtl/GrMtlGpu.h"
12 #include "src/gpu/ganesh/mtl/GrMtlUtil.h"
13
14 #if !__has_feature(objc_arc)
15 #error This file must be compiled with Arc. Use -fobjc-arc flag
16 #endif
17
18 GR_NORETAIN_BEGIN
19
20 GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
21                            SkBudgeted budgeted,
22                            SkISize dimensions,
23                            sk_sp<GrMtlAttachment> texture,
24                            GrMipmapStatus mipmapStatus,
25                            std::string_view label)
26         : GrSurface(gpu, dimensions, GrProtected::kNo, label)
27         , INHERITED(gpu, dimensions, GrProtected::kNo, GrTextureType::k2D, mipmapStatus, label)
28         , fTexture(std::move(texture)) {
29     SkDEBUGCODE(id<MTLTexture> mtlTexture = fTexture->mtlTexture();)
30     SkASSERT((GrMipmapStatus::kNotAllocated == mipmapStatus) == (1 == mtlTexture.mipmapLevelCount));
31     if (@available(macOS 10.11, iOS 9.0, *)) {
32         SkASSERT(SkToBool(mtlTexture.usage & MTLTextureUsageShaderRead));
33     }
34     SkASSERT(!mtlTexture.framebufferOnly);
35     this->registerWithCache(budgeted);
36     if (GrMtlFormatIsCompressed(fTexture->mtlFormat())) {
37         this->setReadOnly();
38     }
39 }
40
41 GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
42                            Wrapped,
43                            SkISize dimensions,
44                            sk_sp<GrMtlAttachment> texture,
45                            GrMipmapStatus mipmapStatus,
46                            GrWrapCacheable cacheable,
47                            GrIOType ioType,
48                            std::string_view label)
49         : GrSurface(gpu, dimensions, GrProtected::kNo, label)
50         , INHERITED(gpu, dimensions, GrProtected::kNo, GrTextureType::k2D, mipmapStatus, label)
51         , fTexture(std::move(texture)) {
52     SkDEBUGCODE(id<MTLTexture> mtlTexture = fTexture->mtlTexture();)
53     SkASSERT((GrMipmapStatus::kNotAllocated == mipmapStatus) == (1 == mtlTexture.mipmapLevelCount));
54     if (@available(macOS 10.11, iOS 9.0, *)) {
55         SkASSERT(SkToBool(mtlTexture.usage & MTLTextureUsageShaderRead));
56     }
57     SkASSERT(!mtlTexture.framebufferOnly);
58     if (ioType == kRead_GrIOType) {
59         this->setReadOnly();
60     }
61     this->registerWithCacheWrapped(cacheable);
62 }
63
64 GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
65                            SkISize dimensions,
66                            sk_sp<GrMtlAttachment> texture,
67                            GrMipmapStatus mipmapStatus,
68                            std::string_view label)
69         : GrSurface(gpu, dimensions, GrProtected::kNo, label)
70         , INHERITED(gpu, dimensions, GrProtected::kNo, GrTextureType::k2D, mipmapStatus, label)
71         , fTexture(std::move(texture)) {
72     SkDEBUGCODE(id<MTLTexture> mtlTexture = fTexture->mtlTexture();)
73     SkASSERT((GrMipmapStatus::kNotAllocated == mipmapStatus) == (1 == mtlTexture.mipmapLevelCount));
74     if (@available(macOS 10.11, iOS 9.0, *)) {
75         SkASSERT(SkToBool(mtlTexture.usage & MTLTextureUsageShaderRead));
76     }
77     SkASSERT(!mtlTexture.framebufferOnly);
78 }
79
80 sk_sp<GrMtlTexture> GrMtlTexture::MakeNewTexture(GrMtlGpu* gpu,
81                                                  SkBudgeted budgeted,
82                                                  SkISize dimensions,
83                                                  MTLPixelFormat format,
84                                                  uint32_t mipLevels,
85                                                  GrMipmapStatus mipmapStatus,
86                                                  std::string_view label) {
87     sk_sp<GrMtlAttachment> texture = GrMtlAttachment::MakeTexture(
88             gpu, dimensions, format, mipLevels, GrRenderable::kNo, /*numSamples=*/1, budgeted);
89
90     if (!texture) {
91         return nullptr;
92     }
93     return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, dimensions, std::move(texture),
94                                                 mipmapStatus, label));
95 }
96
97 sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu,
98                                                      SkISize dimensions,
99                                                      id<MTLTexture> texture,
100                                                      GrWrapCacheable cacheable,
101                                                      GrIOType ioType) {
102     SkASSERT(nil != texture);
103     if (@available(macOS 10.11, iOS 9.0, *)) {
104         SkASSERT(SkToBool(texture.usage & MTLTextureUsageShaderRead));
105     }
106     sk_sp<GrMtlAttachment> attachment =
107             GrMtlAttachment::MakeWrapped(gpu, dimensions, texture,
108                                          GrAttachment::UsageFlags::kTexture, cacheable);
109     if (!attachment) {
110         return nullptr;
111     }
112
113     GrMipmapStatus mipmapStatus = texture.mipmapLevelCount > 1 ? GrMipmapStatus::kValid
114                                                                : GrMipmapStatus::kNotAllocated;
115     return sk_sp<GrMtlTexture>(
116             new GrMtlTexture(gpu, kWrapped, dimensions, std::move(attachment), mipmapStatus,
117                              cacheable, ioType, /*label=*/{}));
118 }
119
120 GrMtlTexture::~GrMtlTexture() {
121     SkASSERT(nil == fTexture);
122 }
123
124 GrMtlGpu* GrMtlTexture::getMtlGpu() const {
125     SkASSERT(!this->wasDestroyed());
126     return static_cast<GrMtlGpu*>(this->getGpu());
127 }
128
129 GrBackendTexture GrMtlTexture::getBackendTexture() const {
130     GrMipmapped mipmapped = fTexture->mtlTexture().mipmapLevelCount > 1 ? GrMipmapped::kYes
131                                                                         : GrMipmapped::kNo;
132     GrMtlTextureInfo info;
133     info.fTexture.reset(GrRetainPtrFromId(fTexture->mtlTexture()));
134     return GrBackendTexture(this->width(), this->height(), mipmapped, info);
135 }
136
137 GrBackendFormat GrMtlTexture::backendFormat() const {
138     return GrBackendFormat::MakeMtl(fTexture->mtlFormat());
139 }
140
141 GR_NORETAIN_END