blob: 87829a38829f26ef7f0a27eee81e1035fb24c2f1 [file] [log] [blame]
Greg Daniel4a081e22017-08-04 09:34:44 -04001/*
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 "GrMtlTexture.h"
9
10#include "GrMtlGpu.h"
11#include "GrMtlUtil.h"
12#include "GrTexturePriv.h"
13
Timothy Liang2df9b452018-06-27 14:58:12 -040014GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
15 SkBudgeted budgeted,
16 const GrSurfaceDesc& desc,
17 id<MTLTexture> texture,
18 GrMipMapsStatus mipMapsStatus)
19 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040020 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Timothy Liang2df9b452018-06-27 14:58:12 -040021 , fTexture(texture) {
22 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
23 this->registerWithCache(budgeted);
24}
25
26GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
Timothy Liange886e802018-07-02 16:03:28 -040027 Wrapped,
28 const GrSurfaceDesc& desc,
29 id<MTLTexture> texture,
Greg Daniel2268ad22018-11-15 09:27:38 -050030 GrMipMapsStatus mipMapsStatus,
Brian Salomonfa2ebea2019-01-24 15:58:58 -050031 GrWrapCacheable cacheable,
32 GrIOType ioType)
Timothy Liange886e802018-07-02 16:03:28 -040033 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040034 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Timothy Liange886e802018-07-02 16:03:28 -040035 , fTexture(texture) {
36 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
Brian Salomonc67c31c2018-12-06 10:00:03 -050037 if (ioType == kRead_GrIOType) {
38 this->setReadOnly();
39 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -050040 this->registerWithCacheWrapped(cacheable);
Timothy Liange886e802018-07-02 16:03:28 -040041}
42
43GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
Timothy Liang2df9b452018-06-27 14:58:12 -040044 const GrSurfaceDesc& desc,
45 id<MTLTexture> texture,
46 GrMipMapsStatus mipMapsStatus)
47 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040048 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Timothy Liang2df9b452018-06-27 14:58:12 -040049 , fTexture(texture) {
50 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
51}
52
Greg Daniel4a081e22017-08-04 09:34:44 -040053sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted,
Timothy Liang58f153d2018-07-02 17:36:20 -040054 const GrSurfaceDesc& desc,
55 MTLTextureDescriptor* texDesc,
56 GrMipMapsStatus mipMapsStatus) {
Timothy Liange886e802018-07-02 16:03:28 -040057 if (desc.fSampleCnt > 1) {
58 SkASSERT(false); // Currently we don't support msaa
59 return nullptr;
60 }
Timothy Liang58f153d2018-07-02 17:36:20 -040061 id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:texDesc];
62 SkASSERT(nil != texture);
63 SkASSERT(MTLTextureUsageShaderRead & texture.usage);
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040064 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus));
Greg Daniel4a081e22017-08-04 09:34:44 -040065}
66
Timothy Liange886e802018-07-02 16:03:28 -040067sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu,
68 const GrSurfaceDesc& desc,
Greg Daniel2268ad22018-11-15 09:27:38 -050069 id<MTLTexture> texture,
Brian Salomonfa2ebea2019-01-24 15:58:58 -050070 GrWrapCacheable cacheable,
71 GrIOType ioType) {
Timothy Liang58f153d2018-07-02 17:36:20 -040072 if (desc.fSampleCnt > 1) {
73 SkASSERT(false); // Currently we don't support msaa
74 return nullptr;
75 }
Timothy Liange886e802018-07-02 16:03:28 -040076 SkASSERT(nil != texture);
77 SkASSERT(MTLTextureUsageShaderRead & texture.usage);
78 GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid
79 : GrMipMapsStatus::kNotAllocated;
Brian Salomonfa2ebea2019-01-24 15:58:58 -050080 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus,
81 cacheable, ioType));
Timothy Liange886e802018-07-02 16:03:28 -040082}
83
Greg Daniel4a081e22017-08-04 09:34:44 -040084GrMtlTexture::~GrMtlTexture() {
85 SkASSERT(nil == fTexture);
86}
87
88GrMtlGpu* GrMtlTexture::getMtlGpu() const {
89 SkASSERT(!this->wasDestroyed());
90 return static_cast<GrMtlGpu*>(this->getGpu());
91}
92
Robert Phillipsb67821d2017-12-13 15:00:45 -050093GrBackendTexture GrMtlTexture::getBackendTexture() const {
Timothy Liange886e802018-07-02 16:03:28 -040094 GrMipMapped mipMapped = fTexture.mipmapLevelCount > 1 ? GrMipMapped::kYes
95 : GrMipMapped::kNo;
96 GrMtlTextureInfo info;
97 info.fTexture = GrGetPtrFromId(fTexture);
98 return GrBackendTexture(this->width(), this->height(), mipMapped, info);
Robert Phillipsb67821d2017-12-13 15:00:45 -050099}
Timothy Liange886e802018-07-02 16:03:28 -0400100
Greg Daniel4065d452018-11-16 15:43:41 -0500101GrBackendFormat GrMtlTexture::backendFormat() const {
102 return GrBackendFormat::MakeMtl(fTexture.pixelFormat);
103}
104