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