blob: 1b5ffcbb3302ee453c24b992afd7f56cbe7452f5 [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 -040014// This method parallels GrTextureProxy::highestFilterMode
15static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) {
16 return GrSamplerState::Filter::kMipMap;
17}
18
19GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
20 SkBudgeted budgeted,
21 const GrSurfaceDesc& desc,
22 id<MTLTexture> texture,
23 GrMipMapsStatus mipMapsStatus)
24 : GrSurface(gpu, desc)
Brian Salomon60dd8c72018-07-30 10:24:13 -040025 , INHERITED(gpu, desc, GrTextureType::k2D, highest_filter_mode(desc.fConfig), mipMapsStatus)
Timothy Liang2df9b452018-06-27 14:58:12 -040026 , fTexture(texture) {
27 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
28 this->registerWithCache(budgeted);
29}
30
31GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
Timothy Liange886e802018-07-02 16:03:28 -040032 Wrapped,
33 const GrSurfaceDesc& desc,
34 id<MTLTexture> texture,
35 GrMipMapsStatus mipMapsStatus)
36 : GrSurface(gpu, desc)
Brian Salomon60dd8c72018-07-30 10:24:13 -040037 , INHERITED(gpu, desc, GrTextureType::k2D, highest_filter_mode(desc.fConfig), mipMapsStatus)
Timothy Liange886e802018-07-02 16:03:28 -040038 , fTexture(texture) {
39 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
40 this->registerWithCacheWrapped();
41}
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 Salomon60dd8c72018-07-30 10:24:13 -040048 , INHERITED(gpu, desc, GrTextureType::k2D, highest_filter_mode(desc.fConfig), 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,
69 id<MTLTexture> texture) {
Timothy Liang58f153d2018-07-02 17:36:20 -040070 if (desc.fSampleCnt > 1) {
71 SkASSERT(false); // Currently we don't support msaa
72 return nullptr;
73 }
Timothy Liange886e802018-07-02 16:03:28 -040074 SkASSERT(nil != texture);
75 SkASSERT(MTLTextureUsageShaderRead & texture.usage);
76 GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid
77 : GrMipMapsStatus::kNotAllocated;
78 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus));
79}
80
Greg Daniel4a081e22017-08-04 09:34:44 -040081GrMtlTexture::~GrMtlTexture() {
82 SkASSERT(nil == fTexture);
83}
84
85GrMtlGpu* GrMtlTexture::getMtlGpu() const {
86 SkASSERT(!this->wasDestroyed());
87 return static_cast<GrMtlGpu*>(this->getGpu());
88}
89
Robert Phillipsb67821d2017-12-13 15:00:45 -050090GrBackendTexture GrMtlTexture::getBackendTexture() const {
Timothy Liange886e802018-07-02 16:03:28 -040091 GrMipMapped mipMapped = fTexture.mipmapLevelCount > 1 ? GrMipMapped::kYes
92 : GrMipMapped::kNo;
93 GrMtlTextureInfo info;
94 info.fTexture = GrGetPtrFromId(fTexture);
95 return GrBackendTexture(this->width(), this->height(), mipMapped, info);
Robert Phillipsb67821d2017-12-13 15:00:45 -050096}
Timothy Liange886e802018-07-02 16:03:28 -040097