Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | class GrMtlGpu; |
| 16 | |
| 17 | class GrMtlTexture : public GrTexture { |
| 18 | public: |
| 19 | static sk_sp<GrMtlTexture> CreateNewTexture(GrMtlGpu*, SkBudgeted budgeted, |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 20 | const GrSurfaceDesc&, |
| 21 | MTLTextureDescriptor*, |
| 22 | GrMipMapsStatus); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 23 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 24 | static sk_sp<GrMtlTexture> MakeWrappedTexture(GrMtlGpu*, const GrSurfaceDesc&, id<MTLTexture>, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 25 | GrWrapCacheable, GrIOType); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 26 | |
| 27 | ~GrMtlTexture() override; |
| 28 | |
| 29 | id<MTLTexture> mtlTexture() const { return fTexture; } |
| 30 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 31 | GrBackendTexture getBackendTexture() const override; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 32 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 33 | GrBackendFormat backendFormat() const override; |
| 34 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 35 | void textureParamsModified() override {} |
| 36 | |
| 37 | bool reallocForMipmap(GrMtlGpu* gpu, uint32_t mipLevels); |
| 38 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 39 | void setIdleProc(IdleProc proc, void* context) override { |
| 40 | fIdleProc = proc; |
| 41 | fIdleProcContext = context; |
| 42 | } |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 43 | void* idleContext() const override { return fIdleProcContext; } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 44 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 45 | protected: |
Timothy Liang | 2df9b45 | 2018-06-27 14:58:12 -0400 | [diff] [blame] | 46 | GrMtlTexture(GrMtlGpu*, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 47 | |
| 48 | GrMtlGpu* getMtlGpu() const; |
| 49 | |
| 50 | void onAbandon() override { |
| 51 | fTexture = nil; |
Greg Daniel | c91162d | 2019-02-05 09:40:21 -0500 | [diff] [blame] | 52 | INHERITED::onAbandon(); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 53 | } |
| 54 | void onRelease() override { |
| 55 | fTexture = nil; |
Greg Daniel | c91162d | 2019-02-05 09:40:21 -0500 | [diff] [blame] | 56 | INHERITED::onRelease(); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Brian Salomon | 3727136 | 2017-10-13 12:52:17 -0400 | [diff] [blame] | 59 | bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { |
| 60 | return false; |
| 61 | } |
| 62 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 63 | private: |
| 64 | enum Wrapped { kWrapped }; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 65 | |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 66 | // 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 Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 70 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame^] | 71 | void willRemoveLastRefOrPendingIO() override { |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 72 | if (fIdleProc) { |
| 73 | fIdleProc(fIdleProcContext); |
| 74 | fIdleProc = nullptr; |
| 75 | fIdleProcContext = nullptr; |
| 76 | } |
| 77 | } |
| 78 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 79 | GrMtlTexture(GrMtlGpu*, SkBudgeted, const GrSurfaceDesc&, id<MTLTexture>, |
| 80 | GrMipMapsStatus); |
| 81 | |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 82 | GrMtlTexture(GrMtlGpu*, Wrapped, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 83 | GrWrapCacheable, GrIOType); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 84 | |
| 85 | id<MTLTexture> fTexture; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 86 | sk_sp<GrReleaseProcHelper> fReleaseHelper; |
| 87 | IdleProc* fIdleProc = nullptr; |
| 88 | void* fIdleProcContext = nullptr; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 89 | |
| 90 | typedef GrTexture INHERITED; |
| 91 | }; |
| 92 | |
| 93 | #endif |