2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/gpu/ganesh/mtl/GrMtlTexture.h"
10 #include "src/gpu/ganesh/GrTexture.h"
11 #include "src/gpu/ganesh/mtl/GrMtlGpu.h"
12 #include "src/gpu/ganesh/mtl/GrMtlUtil.h"
14 #if !__has_feature(objc_arc)
15 #error This file must be compiled with Arc. Use -fobjc-arc flag
20 GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
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));
34 SkASSERT(!mtlTexture.framebufferOnly);
35 this->registerWithCache(budgeted);
36 if (GrMtlFormatIsCompressed(fTexture->mtlFormat())) {
41 GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
44 sk_sp<GrMtlAttachment> texture,
45 GrMipmapStatus mipmapStatus,
46 GrWrapCacheable cacheable,
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));
57 SkASSERT(!mtlTexture.framebufferOnly);
58 if (ioType == kRead_GrIOType) {
61 this->registerWithCacheWrapped(cacheable);
64 GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
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));
77 SkASSERT(!mtlTexture.framebufferOnly);
80 sk_sp<GrMtlTexture> GrMtlTexture::MakeNewTexture(GrMtlGpu* gpu,
83 MTLPixelFormat format,
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);
93 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, dimensions, std::move(texture),
94 mipmapStatus, label));
97 sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu,
99 id<MTLTexture> texture,
100 GrWrapCacheable cacheable,
102 SkASSERT(nil != texture);
103 if (@available(macOS 10.11, iOS 9.0, *)) {
104 SkASSERT(SkToBool(texture.usage & MTLTextureUsageShaderRead));
106 sk_sp<GrMtlAttachment> attachment =
107 GrMtlAttachment::MakeWrapped(gpu, dimensions, texture,
108 GrAttachment::UsageFlags::kTexture, cacheable);
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=*/{}));
120 GrMtlTexture::~GrMtlTexture() {
121 SkASSERT(nil == fTexture);
124 GrMtlGpu* GrMtlTexture::getMtlGpu() const {
125 SkASSERT(!this->wasDestroyed());
126 return static_cast<GrMtlGpu*>(this->getGpu());
129 GrBackendTexture GrMtlTexture::getBackendTexture() const {
130 GrMipmapped mipmapped = fTexture->mtlTexture().mipmapLevelCount > 1 ? GrMipmapped::kYes
132 GrMtlTextureInfo info;
133 info.fTexture.reset(GrRetainPtrFromId(fTexture->mtlTexture()));
134 return GrBackendTexture(this->width(), this->height(), mipmapped, info);
137 GrBackendFormat GrMtlTexture::backendFormat() const {
138 return GrBackendFormat::MakeMtl(fTexture->mtlFormat());