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