blob: 7dd6f59c6f16a063ebcfce5d6b8454db46555f23 [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,
30 GrMipMapsStatus mipMapsStatus)
31 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040032 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Timothy Liange886e802018-07-02 16:03:28 -040033 , fTexture(texture) {
34 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
35 this->registerWithCacheWrapped();
36}
37
38GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
Timothy Liang2df9b452018-06-27 14:58:12 -040039 const GrSurfaceDesc& desc,
40 id<MTLTexture> texture,
41 GrMipMapsStatus mipMapsStatus)
42 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040043 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Timothy Liang2df9b452018-06-27 14:58:12 -040044 , fTexture(texture) {
45 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == texture.mipmapLevelCount));
46}
47
Greg Daniel4a081e22017-08-04 09:34:44 -040048sk_sp<GrMtlTexture> GrMtlTexture::CreateNewTexture(GrMtlGpu* gpu, SkBudgeted budgeted,
Timothy Liang58f153d2018-07-02 17:36:20 -040049 const GrSurfaceDesc& desc,
50 MTLTextureDescriptor* texDesc,
51 GrMipMapsStatus mipMapsStatus) {
Timothy Liange886e802018-07-02 16:03:28 -040052 if (desc.fSampleCnt > 1) {
53 SkASSERT(false); // Currently we don't support msaa
54 return nullptr;
55 }
Timothy Liang58f153d2018-07-02 17:36:20 -040056 id<MTLTexture> texture = [gpu->device() newTextureWithDescriptor:texDesc];
57 SkASSERT(nil != texture);
58 SkASSERT(MTLTextureUsageShaderRead & texture.usage);
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040059 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, budgeted, desc, texture, mipMapsStatus));
Greg Daniel4a081e22017-08-04 09:34:44 -040060}
61
Timothy Liange886e802018-07-02 16:03:28 -040062sk_sp<GrMtlTexture> GrMtlTexture::MakeWrappedTexture(GrMtlGpu* gpu,
63 const GrSurfaceDesc& desc,
64 id<MTLTexture> texture) {
Timothy Liang58f153d2018-07-02 17:36:20 -040065 if (desc.fSampleCnt > 1) {
66 SkASSERT(false); // Currently we don't support msaa
67 return nullptr;
68 }
Timothy Liange886e802018-07-02 16:03:28 -040069 SkASSERT(nil != texture);
70 SkASSERT(MTLTextureUsageShaderRead & texture.usage);
71 GrMipMapsStatus mipMapsStatus = texture.mipmapLevelCount > 1 ? GrMipMapsStatus::kValid
72 : GrMipMapsStatus::kNotAllocated;
73 return sk_sp<GrMtlTexture>(new GrMtlTexture(gpu, kWrapped, desc, texture, mipMapsStatus));
74}
75
Greg Daniel4a081e22017-08-04 09:34:44 -040076GrMtlTexture::~GrMtlTexture() {
77 SkASSERT(nil == fTexture);
78}
79
80GrMtlGpu* GrMtlTexture::getMtlGpu() const {
81 SkASSERT(!this->wasDestroyed());
82 return static_cast<GrMtlGpu*>(this->getGpu());
83}
84
Robert Phillipsb67821d2017-12-13 15:00:45 -050085GrBackendTexture GrMtlTexture::getBackendTexture() const {
Timothy Liange886e802018-07-02 16:03:28 -040086 GrMipMapped mipMapped = fTexture.mipmapLevelCount > 1 ? GrMipMapped::kYes
87 : GrMipMapped::kNo;
88 GrMtlTextureInfo info;
89 info.fTexture = GrGetPtrFromId(fTexture);
90 return GrBackendTexture(this->width(), this->height(), mipMapped, info);
Robert Phillipsb67821d2017-12-13 15:00:45 -050091}
Timothy Liange886e802018-07-02 16:03:28 -040092