blob: b26d65f5ce67f8a0ecad51fd435a5c54612bbffc [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
24 static sk_sp<GrMtlTexture> MakeWrappedTexture(GrMtlGpu*, const GrSurfaceDesc&,
Greg Daniel2268ad22018-11-15 09:27:38 -050025 id<MTLTexture>, bool purgeImmediately);
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
33 void textureParamsModified() override {}
34
35 bool reallocForMipmap(GrMtlGpu* gpu, uint32_t mipLevels);
36
Greg Daniel6a0176b2018-01-30 09:28:44 -050037 void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {
Greg Daniel4a081e22017-08-04 09:34:44 -040038 // Since all MTLResources are inherently ref counted, we can call the Release proc when we
39 // delete the GrMtlTexture without worry of the MTLTexture getting deleted before it is done
40 // on the GPU.
Greg Daniel6a0176b2018-01-30 09:28:44 -050041 fReleaseHelper = std::move(releaseHelper);
Greg Daniel4a081e22017-08-04 09:34:44 -040042 }
43
44protected:
Timothy Liang2df9b452018-06-27 14:58:12 -040045 GrMtlTexture(GrMtlGpu*, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus);
Greg Daniel4a081e22017-08-04 09:34:44 -040046
47 GrMtlGpu* getMtlGpu() const;
48
49 void onAbandon() override {
50 fTexture = nil;
51 }
52 void onRelease() override {
53 fTexture = nil;
54 }
55
Brian Salomon37271362017-10-13 12:52:17 -040056 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
57 return false;
58 }
59
Greg Daniel4a081e22017-08-04 09:34:44 -040060private:
61 enum Wrapped { kWrapped };
Timothy Liange886e802018-07-02 16:03:28 -040062 GrMtlTexture(GrMtlGpu*, SkBudgeted, const GrSurfaceDesc&, id<MTLTexture>,
63 GrMipMapsStatus);
64
Greg Daniel2268ad22018-11-15 09:27:38 -050065 GrMtlTexture(GrMtlGpu*, Wrapped, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus,
66 bool purgeImmediately);
Greg Daniel4a081e22017-08-04 09:34:44 -040067
68 id<MTLTexture> fTexture;
69
Greg Daniel6a0176b2018-01-30 09:28:44 -050070 sk_sp<GrReleaseProcHelper> fReleaseHelper;
Greg Daniel4a081e22017-08-04 09:34:44 -040071
72 typedef GrTexture INHERITED;
73};
74
75#endif