blob: bebb736750323941186b03f41d10a84dd765905b [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 Phillipsb0e93a22017-08-29 08:26:54 -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&,
Greg Danielf88c12e2017-10-09 09:57:35 -040033 SkTransferFunctionBehavior,
34 bool willNeedMipMaps) override;
Brian Osman13dddce2017-05-09 13:19:50 -040035#endif
36
37private:
Robert Phillipsb0e93a22017-08-29 08:26:54 -040038 GrBackendTextureImageGenerator(const SkImageInfo& info, GrTexture*, GrSurfaceOrigin,
Brian Osman13dddce2017-05-09 13:19:50 -040039 uint32_t owningContextID, sk_sp<GrSemaphore>,
40 const GrBackendTexture&);
41
42 static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
43
44 class RefHelper : public SkNVRefCnt<RefHelper> {
45 public:
46 RefHelper(GrTexture* texture, uint32_t owningContextID)
47 : fOriginalTexture(texture)
48 , fOwningContextID(owningContextID)
49 , fBorrowedTexture(nullptr)
50 , fBorrowingContextID(SK_InvalidGenID) { }
51
52 ~RefHelper();
53
54 GrTexture* fOriginalTexture;
55 uint32_t fOwningContextID;
56
57 // There is never a ref associated with this pointer. We rely on our atomic bookkeeping
58 // with the context ID to know when this pointer is valid and safe to use. This lets us
59 // avoid releasing a ref from another thread, or get into races during context shutdown.
60 GrTexture* fBorrowedTexture;
61 SkAtomic<uint32_t> fBorrowingContextID;
62 };
63
64 RefHelper* fRefHelper;
65
66 sk_sp<GrSemaphore> fSemaphore;
67 uint32_t fLastBorrowingContextID;
68
69 GrBackendTexture fBackendTexture;
70 GrSurfaceOrigin fSurfaceOrigin;
71
72 typedef SkImageGenerator INHERITED;
73};
74#endif // GrBackendTextureImageGenerator_DEFINED