GrBackendTextureImageGenerator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "include/core/SkImageGenerator.h"
  10. #include "include/gpu/GrBackendSurface.h"
  11. #include "include/private/GrResourceKey.h"
  12. #include "include/private/SkMutex.h"
  13. class GrSemaphore;
  14. /*
  15. * This ImageGenerator is used to wrap a texture in one GrContext and can then be used as a source
  16. * in another GrContext. It holds onto a semaphore which the producing GrContext will signal and the
  17. * consuming GrContext will wait on before using the texture. Only one GrContext can ever be used
  18. * as a consumer (this is mostly because Vulkan can't allow multiple things to wait on the same
  19. * semaphore).
  20. *
  21. * In practice, this capability is used by clients to create backend-specific texture resources in
  22. * one thread (with, say, GrContext-A) and then ship them over to another GrContext (say,
  23. * GrContext-B) which will then use the texture as a source for draws. GrContext-A uses the
  24. * semaphore to notify GrContext-B when the shared texture is ready to use.
  25. */
  26. class GrBackendTextureImageGenerator : public SkImageGenerator {
  27. public:
  28. static std::unique_ptr<SkImageGenerator> Make(sk_sp<GrTexture>, GrSurfaceOrigin,
  29. sk_sp<GrSemaphore>, SkColorType,
  30. SkAlphaType, sk_sp<SkColorSpace>);
  31. ~GrBackendTextureImageGenerator() override;
  32. protected:
  33. // NOTE: We would like to validate that the owning context hasn't been abandoned, but we can't
  34. // do that safely (we might be on another thread). So assume everything is fine.
  35. bool onIsValid(GrContext*) const override { return true; }
  36. TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
  37. sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
  38. const SkIPoint&, bool willNeedMipMaps) override;
  39. private:
  40. GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*, GrSurfaceOrigin,
  41. uint32_t owningContextID, sk_sp<GrSemaphore>,
  42. const GrBackendTexture&);
  43. static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
  44. class RefHelper : public SkNVRefCnt<RefHelper> {
  45. public:
  46. RefHelper(GrTexture*, uint32_t owningContextID);
  47. ~RefHelper();
  48. GrTexture* fOriginalTexture;
  49. uint32_t fOwningContextID;
  50. // We use this key so that we don't rewrap the GrBackendTexture in a GrTexture for each
  51. // proxy created from this generator for a particular borrowing context.
  52. GrUniqueKey fBorrowedTextureKey;
  53. // There is no ref associated with this pointer. We rely on our atomic bookkeeping with the
  54. // context ID to know when this pointer is valid and safe to use. This is used to make sure
  55. // all uses of the wrapped texture are finished on the borrowing context before we open
  56. // this back up to other contexts. In general a ref to this release proc is owned by all
  57. // proxies and gpu uses of the backend texture.
  58. GrRefCntedCallback* fBorrowingContextReleaseProc;
  59. uint32_t fBorrowingContextID;
  60. };
  61. RefHelper* fRefHelper;
  62. // This Mutex is used to guard the borrowing of the texture to one GrContext at a time as well
  63. // as the creation of the fBorrowingContextReleaseProc. The latter happening if two threads with
  64. // the same consuming GrContext try to generate a texture at the same time.
  65. SkMutex fBorrowingMutex;
  66. sk_sp<GrSemaphore> fSemaphore;
  67. GrBackendTexture fBackendTexture;
  68. GrSurfaceOrigin fSurfaceOrigin;
  69. typedef SkImageGenerator INHERITED;
  70. };
  71. #endif // GrBackendTextureImageGenerator_DEFINED