blob: 1124b9628b3d22da1b07923ef114fb8ea8ffe6cc [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
10#include "SkImageGenerator.h"
11
12#include "GrBackendSurface.h"
13#include "SkAtomics.h"
14
15class GrSemaphore;
16
17class GrBackendTextureImageGenerator : public SkImageGenerator {
18public:
Robert Phillips7294b852017-08-01 13:51:44 +000019 static std::unique_ptr<SkImageGenerator> Make(sk_sp<GrTexture>, sk_sp<GrSemaphore>,
Brian Osman13dddce2017-05-09 13:19:50 -040020 SkAlphaType, sk_sp<SkColorSpace>);
21
Stan Iliev7e910df2017-06-02 10:29:21 -040022 ~GrBackendTextureImageGenerator() override;
Brian Osman13dddce2017-05-09 13:19:50 -040023
24protected:
Brian Osman07454222017-05-12 09:46:56 -040025 // NOTE: We would like to validate that the owning context hasn't been abandoned, but we can't
26 // do that safely (we might be on another thread). So assume everything is fine.
27 bool onIsValid(GrContext*) const override { return true; }
Brian Osman13dddce2017-05-09 13:19:50 -040028
29#if SK_SUPPORT_GPU
Stan Ilievba81af22017-06-08 15:16:53 -040030 TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
Christopher Cameron77e96662017-07-08 01:47:47 -070031 sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&,
32 SkTransferFunctionBehavior) override;
Brian Osman13dddce2017-05-09 13:19:50 -040033#endif
34
35private:
Robert Phillips7294b852017-08-01 13:51:44 +000036 GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*,
Brian Osman13dddce2017-05-09 13:19:50 -040037 uint32_t owningContextID, sk_sp<GrSemaphore>,
38 const GrBackendTexture&);
39
40 static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
41
42 class RefHelper : public SkNVRefCnt<RefHelper> {
43 public:
44 RefHelper(GrTexture* texture, uint32_t owningContextID)
45 : fOriginalTexture(texture)
46 , fOwningContextID(owningContextID)
47 , fBorrowedTexture(nullptr)
48 , fBorrowingContextID(SK_InvalidGenID) { }
49
50 ~RefHelper();
51
52 GrTexture* fOriginalTexture;
53 uint32_t fOwningContextID;
54
55 // There is never a ref associated with this pointer. We rely on our atomic bookkeeping
56 // with the context ID to know when this pointer is valid and safe to use. This lets us
57 // avoid releasing a ref from another thread, or get into races during context shutdown.
58 GrTexture* fBorrowedTexture;
59 SkAtomic<uint32_t> fBorrowingContextID;
60 };
61
62 RefHelper* fRefHelper;
63
64 sk_sp<GrSemaphore> fSemaphore;
65 uint32_t fLastBorrowingContextID;
66
67 GrBackendTexture fBackendTexture;
68 GrSurfaceOrigin fSurfaceOrigin;
69
70 typedef SkImageGenerator INHERITED;
71};
72#endif // GrBackendTextureImageGenerator_DEFINED