Brian Osman | 13dddce | 2017-05-09 13:19:50 -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 | #ifndef GrBackendTextureImageGenerator_DEFINED |
| 8 | #define GrBackendTextureImageGenerator_DEFINED |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkImageGenerator.h" |
| 11 | #include "include/gpu/GrBackendSurface.h" |
| 12 | #include "include/private/GrResourceKey.h" |
| 13 | #include "include/private/SkMutex.h" |
Greg Daniel | 7c90211 | 2020-03-06 13:07:10 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrTexture.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 15 | |
| 16 | class GrSemaphore; |
| 17 | |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 18 | /* |
| 19 | * This ImageGenerator is used to wrap a texture in one GrContext and can then be used as a source |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 20 | * in another GrContext. It holds onto a semaphore which the producing GrContext will signal and the |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 21 | * consuming GrContext will wait on before using the texture. Only one GrContext can ever be used |
| 22 | * as a consumer (this is mostly because Vulkan can't allow multiple things to wait on the same |
| 23 | * semaphore). |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 24 | * |
| 25 | * In practice, this capability is used by clients to create backend-specific texture resources in |
| 26 | * one thread (with, say, GrContext-A) and then ship them over to another GrContext (say, |
| 27 | * GrContext-B) which will then use the texture as a source for draws. GrContext-A uses the |
| 28 | * semaphore to notify GrContext-B when the shared texture is ready to use. |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 29 | */ |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 30 | class GrBackendTextureImageGenerator : public SkImageGenerator { |
| 31 | public: |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 32 | static std::unique_ptr<SkImageGenerator> Make(sk_sp<GrTexture>, GrSurfaceOrigin, |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 33 | std::unique_ptr<GrSemaphore>, SkColorType, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 34 | SkAlphaType, sk_sp<SkColorSpace>); |
| 35 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 36 | ~GrBackendTextureImageGenerator() override; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 37 | |
| 38 | protected: |
Robert Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame] | 39 | bool onIsValid(GrRecordingContext* context) const override { |
| 40 | if (context && context->abandoned()) { |
| 41 | return false; |
| 42 | } |
| 43 | return true; |
| 44 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 45 | |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 46 | GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, const SkImageInfo&, const SkIPoint&, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame^] | 47 | GrMipmapped mipMapped, GrImageTexGenPolicy) override; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 48 | |
| 49 | private: |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 50 | GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*, GrSurfaceOrigin, |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 51 | uint32_t owningContextID, std::unique_ptr<GrSemaphore>, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 52 | const GrBackendTexture&); |
| 53 | |
| 54 | static void ReleaseRefHelper_TextureReleaseProc(void* ctx); |
| 55 | |
Mike Klein | 408ef21 | 2018-10-30 15:23:00 +0000 | [diff] [blame] | 56 | class RefHelper : public SkNVRefCnt<RefHelper> { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 57 | public: |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 58 | RefHelper(GrTexture*, uint32_t owningContextID, std::unique_ptr<GrSemaphore>); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 59 | |
| 60 | ~RefHelper(); |
| 61 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 62 | GrTexture* fOriginalTexture; |
| 63 | uint32_t fOwningContextID; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 64 | |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 65 | // We use this key so that we don't rewrap the GrBackendTexture in a GrTexture for each |
| 66 | // proxy created from this generator for a particular borrowing context. |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 67 | GrUniqueKey fBorrowedTextureKey; |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 68 | // There is no ref associated with this pointer. We rely on our atomic bookkeeping with the |
| 69 | // context ID to know when this pointer is valid and safe to use. This is used to make sure |
| 70 | // all uses of the wrapped texture are finished on the borrowing context before we open |
| 71 | // this back up to other contexts. In general a ref to this release proc is owned by all |
| 72 | // proxies and gpu uses of the backend texture. |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 73 | GrRefCntedCallback* fBorrowingContextReleaseProc; |
| 74 | uint32_t fBorrowingContextID; |
| 75 | |
| 76 | std::unique_ptr<GrSemaphore> fSemaphore; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 77 | }; |
| 78 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 79 | RefHelper* fRefHelper; |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 80 | // This Mutex is used to guard the borrowing of the texture to one GrContext at a time as well |
| 81 | // as the creation of the fBorrowingContextReleaseProc. The latter happening if two threads with |
| 82 | // the same consuming GrContext try to generate a texture at the same time. |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 83 | SkMutex fBorrowingMutex; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 84 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 85 | GrBackendTexture fBackendTexture; |
| 86 | GrSurfaceOrigin fSurfaceOrigin; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 87 | |
| 88 | typedef SkImageGenerator INHERITED; |
| 89 | }; |
| 90 | #endif // GrBackendTextureImageGenerator_DEFINED |