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 | |
| 10 | #include "SkImageGenerator.h" |
| 11 | |
| 12 | #include "GrBackendSurface.h" |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 13 | #include "SkMutex.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 14 | |
| 15 | class GrSemaphore; |
| 16 | |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 17 | /* |
| 18 | * 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] | 19 | * 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] | 20 | * consuming GrContext will wait on before using the texture. Only one GrContext can ever be used |
| 21 | * as a consumer (this is mostly because Vulkan can't allow multiple things to wait on the same |
| 22 | * semaphore). |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 23 | * |
| 24 | * In practice, this capability is used by clients to create backend-specific texture resources in |
| 25 | * one thread (with, say, GrContext-A) and then ship them over to another GrContext (say, |
| 26 | * GrContext-B) which will then use the texture as a source for draws. GrContext-A uses the |
| 27 | * 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] | 28 | */ |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 29 | class GrBackendTextureImageGenerator : public SkImageGenerator { |
| 30 | public: |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 31 | static std::unique_ptr<SkImageGenerator> Make(sk_sp<GrTexture>, GrSurfaceOrigin, |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 32 | sk_sp<GrSemaphore>, SkColorType, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 33 | SkAlphaType, sk_sp<SkColorSpace>); |
| 34 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 35 | ~GrBackendTextureImageGenerator() override; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 36 | |
| 37 | protected: |
Brian Osman | 0745422 | 2017-05-12 09:46:56 -0400 | [diff] [blame] | 38 | // NOTE: We would like to validate that the owning context hasn't been abandoned, but we can't |
| 39 | // do that safely (we might be on another thread). So assume everything is fine. |
| 40 | bool onIsValid(GrContext*) const override { return true; } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 41 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 42 | TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; } |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 43 | sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&, |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 44 | bool willNeedMipMaps) override; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 45 | |
| 46 | private: |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 47 | GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*, GrSurfaceOrigin, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 48 | uint32_t owningContextID, sk_sp<GrSemaphore>, |
| 49 | const GrBackendTexture&); |
| 50 | |
| 51 | static void ReleaseRefHelper_TextureReleaseProc(void* ctx); |
| 52 | |
| 53 | class RefHelper : public SkNVRefCnt<RefHelper> { |
| 54 | public: |
| 55 | RefHelper(GrTexture* texture, uint32_t owningContextID) |
| 56 | : fOriginalTexture(texture) |
| 57 | , fOwningContextID(owningContextID) |
| 58 | , fBorrowedTexture(nullptr) |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 59 | , fBorrowingContextReleaseProc(nullptr) |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 60 | , fBorrowingContextID(SK_InvalidGenID) {} |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 61 | |
| 62 | ~RefHelper(); |
| 63 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 64 | GrTexture* fOriginalTexture; |
| 65 | uint32_t fOwningContextID; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 66 | |
| 67 | // There is never a ref associated with this pointer. We rely on our atomic bookkeeping |
| 68 | // with the context ID to know when this pointer is valid and safe to use. This lets us |
| 69 | // avoid releasing a ref from another thread, or get into races during context shutdown. |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 70 | GrTexture* fBorrowedTexture; |
| 71 | // For the same reason as the fBorrowedTexture, there is no ref associated with this |
| 72 | // pointer. The fBorrowingContextReleaseProc is used to make sure all uses of the wrapped |
| 73 | // texture are finished on the borrowing context before we open this back up to other |
| 74 | // contexts. In general a ref to this release proc is owned by all proxies and gpu uses of |
| 75 | // the backend texture. |
| 76 | GrReleaseProcHelper* fBorrowingContextReleaseProc; |
| 77 | uint32_t fBorrowingContextID; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 78 | }; |
| 79 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 80 | RefHelper* fRefHelper; |
| 81 | // This Mutex is used to guard the borrowing of the texture to one GrContext at a time as well |
| 82 | // as the creation of the fBorrowingContextReleaseProc. The latter happening if two threads with |
| 83 | // the same consuming GrContext try to generate a texture at the same time. |
| 84 | SkMutex fBorrowingMutex; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 85 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 86 | sk_sp<GrSemaphore> fSemaphore; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 87 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 88 | GrBackendTexture fBackendTexture; |
| 89 | GrPixelConfig fConfig; |
| 90 | GrSurfaceOrigin fSurfaceOrigin; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 91 | |
| 92 | typedef SkImageGenerator INHERITED; |
| 93 | }; |
| 94 | #endif // GrBackendTextureImageGenerator_DEFINED |