blob: d9980c53da2b70742738d31554178beee886d852 [file] [log] [blame]
Brian Osman13dddce2017-05-09 13:19:50 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkImageGenerator.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/private/GrResourceKey.h"
13#include "include/private/SkMutex.h"
Brian Osman13dddce2017-05-09 13:19:50 -040014
15class GrSemaphore;
16
Greg Daniel966db9e2018-01-12 13:15:06 -050017/*
18 * This ImageGenerator is used to wrap a texture in one GrContext and can then be used as a source
Greg Danielabba9982018-02-01 10:07:57 -050019 * in another GrContext. It holds onto a semaphore which the producing GrContext will signal and the
Greg Daniel966db9e2018-01-12 13:15:06 -050020 * 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 Danielabba9982018-02-01 10:07:57 -050023 *
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 Daniel966db9e2018-01-12 13:15:06 -050028 */
Brian Osman13dddce2017-05-09 13:19:50 -040029class GrBackendTextureImageGenerator : public SkImageGenerator {
30public:
Robert Phillipsb0e93a22017-08-29 08:26:54 -040031 static std::unique_ptr<SkImageGenerator> Make(sk_sp<GrTexture>, GrSurfaceOrigin,
Greg Daniel301015c2019-11-18 14:06:46 -050032 std::unique_ptr<GrSemaphore>, SkColorType,
Brian Osman13dddce2017-05-09 13:19:50 -040033 SkAlphaType, sk_sp<SkColorSpace>);
34
Stan Iliev7e910df2017-06-02 10:29:21 -040035 ~GrBackendTextureImageGenerator() override;
Brian Osman13dddce2017-05-09 13:19:50 -040036
37protected:
Brian Osman07454222017-05-12 09:46:56 -040038 // 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 Osman13dddce2017-05-09 13:19:50 -040041
Stan Ilievba81af22017-06-08 15:16:53 -040042 TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
Robert Phillips9338c602019-02-19 12:52:29 -050043 sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
44 const SkIPoint&, bool willNeedMipMaps) override;
Brian Osmandd5f6552019-12-17 13:04:30 -050045 bool onTexturesAreCacheable() const override { return false; }
Brian Osman13dddce2017-05-09 13:19:50 -040046
47private:
Robert Phillipsb0e93a22017-08-29 08:26:54 -040048 GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*, GrSurfaceOrigin,
Greg Daniel301015c2019-11-18 14:06:46 -050049 uint32_t owningContextID, std::unique_ptr<GrSemaphore>,
Brian Osman13dddce2017-05-09 13:19:50 -040050 const GrBackendTexture&);
51
52 static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
53
Mike Klein408ef212018-10-30 15:23:00 +000054 class RefHelper : public SkNVRefCnt<RefHelper> {
Brian Osman13dddce2017-05-09 13:19:50 -040055 public:
Greg Daniel301015c2019-11-18 14:06:46 -050056 RefHelper(GrTexture*, uint32_t owningContextID, std::unique_ptr<GrSemaphore>);
Brian Osman13dddce2017-05-09 13:19:50 -040057
58 ~RefHelper();
59
Greg Daniel301015c2019-11-18 14:06:46 -050060 GrTexture* fOriginalTexture;
61 uint32_t fOwningContextID;
Brian Osman13dddce2017-05-09 13:19:50 -040062
Brian Salomonb916b7b2019-04-01 13:34:34 -040063 // We use this key so that we don't rewrap the GrBackendTexture in a GrTexture for each
64 // proxy created from this generator for a particular borrowing context.
Greg Daniel301015c2019-11-18 14:06:46 -050065 GrUniqueKey fBorrowedTextureKey;
Brian Salomonb916b7b2019-04-01 13:34:34 -040066 // There is no ref associated with this pointer. We rely on our atomic bookkeeping with the
67 // context ID to know when this pointer is valid and safe to use. This is used to make sure
68 // all uses of the wrapped texture are finished on the borrowing context before we open
69 // this back up to other contexts. In general a ref to this release proc is owned by all
70 // proxies and gpu uses of the backend texture.
Greg Daniel301015c2019-11-18 14:06:46 -050071 GrRefCntedCallback* fBorrowingContextReleaseProc;
72 uint32_t fBorrowingContextID;
73
74 std::unique_ptr<GrSemaphore> fSemaphore;
Brian Osman13dddce2017-05-09 13:19:50 -040075 };
76
Greg Daniel301015c2019-11-18 14:06:46 -050077 RefHelper* fRefHelper;
Greg Danielabba9982018-02-01 10:07:57 -050078 // This Mutex is used to guard the borrowing of the texture to one GrContext at a time as well
79 // as the creation of the fBorrowingContextReleaseProc. The latter happening if two threads with
80 // the same consuming GrContext try to generate a texture at the same time.
Greg Daniel301015c2019-11-18 14:06:46 -050081 SkMutex fBorrowingMutex;
Brian Osman13dddce2017-05-09 13:19:50 -040082
Greg Daniel301015c2019-11-18 14:06:46 -050083 GrBackendTexture fBackendTexture;
84 GrSurfaceOrigin fSurfaceOrigin;
Brian Osman13dddce2017-05-09 13:19:50 -040085
86 typedef SkImageGenerator INHERITED;
87};
88#endif // GrBackendTextureImageGenerator_DEFINED