| 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 "include/core/SkRefCnt.h" |
| 11 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 12 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 13 |
#include "include/private/base/SkMutex.h" |
| 14 |
#include "include/private/gpu/ganesh/GrTextureGenerator.h" |
| 15 |
#include "src/gpu/ResourceKey.h" |
| 16 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
|
| 20 |
class GrRecordingContext; |
| 21 |
class GrSemaphore; |
| 22 |
class GrTexture; |
| 23 |
class SkColorInfo; |
| 24 |
class SkColorSpace; |
| 25 |
class SkRecorder; |
| 26 |
enum GrSurfaceOrigin : int; |
| 27 |
enum SkAlphaType : int; |
| 28 |
enum SkColorType : int; |
| 29 |
enum class GrImageTexGenPolicy : int; |
| 30 |
struct SkImageInfo; |
| 31 |
|
| 32 |
namespace skgpu { |
| 33 |
class RefCntedCallback; |
| 34 |
enum class Mipmapped : bool; |
| 35 |
} // namespace skgpu |
| 36 |
|
| 37 |
/* |
| 38 |
* This ImageGenerator is used to wrap a texture in one GrContext and can then be used as a source |
| 39 |
* in another GrContext. It holds onto a semaphore which the producing GrContext will signal and the |
| 40 |
* consuming GrContext will wait on before using the texture. Only one GrContext can ever be used |
| 41 |
* as a consumer (this is mostly because Vulkan can't allow multiple things to wait on the same |
| 42 |
* semaphore). |
| 43 |
* |
| 44 |
* In practice, this capability is used by clients to create backend-specific texture resources in |
| 45 |
* one thread (with, say, GrContext-A) and then ship them over to another GrContext (say, |
| 46 |
* GrContext-B) which will then use the texture as a source for draws. GrContext-A uses the |
| 47 |
* semaphore to notify GrContext-B when the shared texture is ready to use. |
| 48 |
*/ |
| 49 |
class GrBackendTextureImageGenerator final : public GrTextureGenerator { |
| 50 |
public: |
| 51 |
static std::unique_ptr<GrTextureGenerator> Make(const sk_sp<GrTexture>&, GrSurfaceOrigin, |
| 52 |
std::unique_ptr<GrSemaphore>, SkColorType, |
| 53 |
SkAlphaType, sk_sp<SkColorSpace>); |
| 54 |
|
| 55 |
~GrBackendTextureImageGenerator() override; |
| 56 |
|
| 57 |
protected: |
| 58 |
bool onIsValid(SkRecorder*) const override; |
| 59 |
bool onIsProtected() const override; |
| 60 |
|
| 61 |
GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, |
| 62 |
const SkImageInfo&, |
| 63 |
skgpu::Mipmapped mipmapped, |
| 64 |
GrImageTexGenPolicy) override; |
| 65 |
|
| 66 |
private: |
| 67 |
GrBackendTextureImageGenerator(const SkColorInfo&, |
| 68 |
const sk_sp<GrTexture>&, |
| 69 |
GrSurfaceOrigin, |
| 70 |
GrDirectContext::DirectContextID owningContextID, |
| 71 |
std::unique_ptr<GrSemaphore>); |
| 72 |
|
| 73 |
static void ReleaseRefHelper_TextureReleaseProc(void* ctx); |
| 74 |
|
| 75 |
class RefHelper : public SkNVRefCnt<RefHelper> { |
| 76 |
public: |
| 77 |
RefHelper(sk_sp<GrTexture>, |
| 78 |
GrDirectContext::DirectContextID owningContextID, |
| 79 |
std::unique_ptr<GrSemaphore>); |
| 80 |
|
| 81 |
~RefHelper(); |
| 82 |
|
| 83 |
sk_sp<GrTexture> fOriginalTexture; |
| 84 |
GrDirectContext::DirectContextID fOwningContextID; |
| 85 |
|
| 86 |
// We use this key so that we don't rewrap the GrBackendTexture in a GrTexture for each |
| 87 |
// proxy created from this generator for a particular borrowing context. |
| 88 |
skgpu::UniqueKey fBorrowedTextureKey; |
| 89 |
// There is no ref associated with this pointer. We rely on our atomic bookkeeping with the |
| 90 |
// context ID to know when this pointer is valid and safe to use. This is used to make sure |
| 91 |
// all uses of the wrapped texture are finished on the borrowing context before we open |
| 92 |
// this back up to other contexts. In general a ref to this release proc is owned by all |
| 93 |
// proxies and gpu uses of the backend texture. |
| 94 |
skgpu::RefCntedCallback* fBorrowingContextReleaseProc; |
| 95 |
GrDirectContext::DirectContextID fBorrowingContextID; |
| 96 |
|
| 97 |
std::unique_ptr<GrSemaphore> fSemaphore; |
| 98 |
}; |
| 99 |
|
| 100 |
RefHelper* fRefHelper; |
| 101 |
// This Mutex is used to guard the borrowing of the texture to one GrContext at a time as well |
| 102 |
// as the creation of the fBorrowingContextReleaseProc. The latter happening if two threads with |
| 103 |
// the same consuming GrContext try to generate a texture at the same time. |
| 104 |
SkMutex fBorrowingMutex; |
| 105 |
|
| 106 |
GrBackendTexture fBackendTexture; |
| 107 |
GrSurfaceOrigin fSurfaceOrigin; |
| 108 |
|
| 109 |
using INHERITED = GrTextureGenerator; |
| 110 |
}; |
| 111 |
#endif // GrBackendTextureImageGenerator_DEFINED |