blob: ae283bda1c652b03d9612900c5d72c1467484993 [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#ifndef GrMtlTexture_DEFINED
9#define GrMtlTexture_DEFINED
10
11#include "GrTexture.h"
12
13#import <Metal/Metal.h>
14
15class GrMtlGpu;
16
17class GrMtlTexture : public GrTexture {
18public:
19 static sk_sp<GrMtlTexture> CreateNewTexture(GrMtlGpu*, SkBudgeted budgeted,
Timothy Liang58f153d2018-07-02 17:36:20 -040020 const GrSurfaceDesc&,
21 MTLTextureDescriptor*,
22 GrMipMapsStatus);
Greg Daniel4a081e22017-08-04 09:34:44 -040023
Brian Salomonc67c31c2018-12-06 10:00:03 -050024 static sk_sp<GrMtlTexture> MakeWrappedTexture(GrMtlGpu*, const GrSurfaceDesc&, id<MTLTexture>,
Brian Salomonfa2ebea2019-01-24 15:58:58 -050025 GrWrapCacheable, GrIOType);
Greg Daniel4a081e22017-08-04 09:34:44 -040026
27 ~GrMtlTexture() override;
28
29 id<MTLTexture> mtlTexture() const { return fTexture; }
30
Robert Phillipsb67821d2017-12-13 15:00:45 -050031 GrBackendTexture getBackendTexture() const override;
Greg Daniel4a081e22017-08-04 09:34:44 -040032
Greg Daniel4065d452018-11-16 15:43:41 -050033 GrBackendFormat backendFormat() const override;
34
Greg Daniel4a081e22017-08-04 09:34:44 -040035 void textureParamsModified() override {}
36
37 bool reallocForMipmap(GrMtlGpu* gpu, uint32_t mipLevels);
38
Brian Salomon614c1a82018-12-19 15:42:06 -050039 void setIdleProc(IdleProc proc, void* context) override {
40 fIdleProc = proc;
41 fIdleProcContext = context;
42 }
Brian Salomon1bf0ed82019-01-16 13:51:35 -050043 void* idleContext() const override { return fIdleProcContext; }
Brian Salomon614c1a82018-12-19 15:42:06 -050044
Greg Daniel4a081e22017-08-04 09:34:44 -040045protected:
Timothy Liang2df9b452018-06-27 14:58:12 -040046 GrMtlTexture(GrMtlGpu*, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus);
Greg Daniel4a081e22017-08-04 09:34:44 -040047
48 GrMtlGpu* getMtlGpu() const;
49
50 void onAbandon() override {
51 fTexture = nil;
Greg Danielc91162d2019-02-05 09:40:21 -050052 INHERITED::onAbandon();
Greg Daniel4a081e22017-08-04 09:34:44 -040053 }
54 void onRelease() override {
55 fTexture = nil;
Greg Danielc91162d2019-02-05 09:40:21 -050056 INHERITED::onRelease();
Greg Daniel4a081e22017-08-04 09:34:44 -040057 }
58
Brian Salomon37271362017-10-13 12:52:17 -040059 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
60 return false;
61 }
62
Greg Daniel4a081e22017-08-04 09:34:44 -040063private:
64 enum Wrapped { kWrapped };
Brian Salomon614c1a82018-12-19 15:42:06 -050065
Greg Daniel2d35a1c2019-02-01 14:48:10 -050066 // Since all MTLResources are inherently ref counted, we can call the Release proc when we
67 // delete the GrMtlTexture without worry of the MTLTexture getting deleted before it is done on
68 // the GPU. Thus we do nothing special here with the releaseHelper.
69 void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {}
Brian Salomon614c1a82018-12-19 15:42:06 -050070
Brian Salomon8cabb322019-02-22 10:44:19 -050071 void willRemoveLastRefOrPendingIO() override {
Brian Salomon614c1a82018-12-19 15:42:06 -050072 if (fIdleProc) {
73 fIdleProc(fIdleProcContext);
74 fIdleProc = nullptr;
75 fIdleProcContext = nullptr;
76 }
77 }
78
Timothy Liange886e802018-07-02 16:03:28 -040079 GrMtlTexture(GrMtlGpu*, SkBudgeted, const GrSurfaceDesc&, id<MTLTexture>,
80 GrMipMapsStatus);
81
Greg Daniel2268ad22018-11-15 09:27:38 -050082 GrMtlTexture(GrMtlGpu*, Wrapped, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus,
Brian Salomonfa2ebea2019-01-24 15:58:58 -050083 GrWrapCacheable, GrIOType);
Greg Daniel4a081e22017-08-04 09:34:44 -040084
85 id<MTLTexture> fTexture;
Brian Salomon614c1a82018-12-19 15:42:06 -050086 sk_sp<GrReleaseProcHelper> fReleaseHelper;
87 IdleProc* fIdleProc = nullptr;
88 void* fIdleProcContext = nullptr;
Greg Daniel4a081e22017-08-04 09:34:44 -040089
90 typedef GrTexture INHERITED;
91};
92
93#endif