• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkImageGenerator.h"
11 
12 #include "GrBackendSurface.h"
13 #include "SkAtomics.h"
14 
15 class GrSemaphore;
16 
17 class GrBackendTextureImageGenerator : public SkImageGenerator {
18 public:
19     static std::unique_ptr<SkImageGenerator> Make(sk_sp<GrTexture>, sk_sp<GrSemaphore>,
20                                                   SkAlphaType, sk_sp<SkColorSpace>);
21 
22     ~GrBackendTextureImageGenerator() override;
23 
24 protected:
25     // 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.
onIsValid(GrContext *)27     bool onIsValid(GrContext*) const override { return true; }
28 
29 #if SK_SUPPORT_GPU
onCanGenerateTexture()30     TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
31     sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&,
32                                             SkTransferFunctionBehavior) override;
33 #endif
34 
35 private:
36     GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*,
37                                    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:
RefHelper(GrTexture * texture,uint32_t owningContextID)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
73