blob: dbec740ea7fe9cbb2188f3602053fbe3747d1390 [file] [log] [blame]
Brian Osmanbd659552018-09-11 10:03:19 -04001/*
2 * Copyright 2018 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
8#ifndef SkImage_Lazy_DEFINED
9#define SkImage_Lazy_DEFINED
10
11#include "SkImage_Base.h"
12#include "SkMutex.h"
13
14#if SK_SUPPORT_GPU
15#include "GrTextureMaker.h"
16#endif
17
18class SharedGenerator;
19
20class SkImage_Lazy : public SkImage_Base {
21public:
22 struct Validator {
Brian Osman9a97c962019-01-12 17:38:16 +000023 Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace);
Brian Osmanbd659552018-09-11 10:03:19 -040024
25 operator bool() const { return fSharedGenerator.get(); }
26
27 sk_sp<SharedGenerator> fSharedGenerator;
28 SkImageInfo fInfo;
29 SkIPoint fOrigin;
30 sk_sp<SkColorSpace> fColorSpace;
31 uint32_t fUniqueID;
32 };
33
34 SkImage_Lazy(Validator* validator);
35 ~SkImage_Lazy() override;
36
37 SkImageInfo onImageInfo() const override {
38 return fInfo;
39 }
Brian Osmanbd659552018-09-11 10:03:19 -040040
41 SkIRect onGetSubset() const override {
42 return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height());
43 }
44
45 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
46 CachingHint) const override;
47#if SK_SUPPORT_GPU
48 sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*,
Brian Osmane7fd8c32018-10-19 13:30:39 -040049 const GrSamplerState&,
Brian Osmanbd659552018-09-11 10:03:19 -040050 SkScalar scaleAdjust[2]) const override;
Jim Van Verthe24b5872018-10-29 16:26:02 -040051 sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
Jim Van Verth8f11e432018-10-18 14:36:59 -040052 SkYUVColorSpace*, const void* planes[4]) override;
Robert Phillips4524e252018-09-21 14:20:38 -040053#endif
Brian Osmanbd659552018-09-11 10:03:19 -040054 sk_sp<SkData> onRefEncoded() const override;
55 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
Brian Osmane50cdf02018-10-19 13:02:14 -040056 bool getROPixels(SkBitmap*, CachingHint) const override;
Brian Osmanbd659552018-09-11 10:03:19 -040057 bool onIsLazyGenerated() const override { return true; }
Brian Osman9a97c962019-01-12 17:38:16 +000058 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const override;
Brian Osmanbd659552018-09-11 10:03:19 -040059
60 bool onIsValid(GrContext*) const override;
61
Brian Osmanbd659552018-09-11 10:03:19 -040062#if SK_SUPPORT_GPU
63 // Returns the texture proxy. If we're going to generate and cache the texture, we should use
64 // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the
65 // texture is not trivial to construct, returns nullptr.
66 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
67 const GrUniqueKey& key,
68 SkImage::CachingHint,
69 bool willBeMipped,
Brian Osmanbd659552018-09-11 10:03:19 -040070 GrTextureMaker::AllowedTexGenType genType) const;
71
Brian Osmanbd659552018-09-11 10:03:19 -040072 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const;
73#endif
74
75private:
76 class ScopedGenerator;
77
Brian Osmanbd659552018-09-11 10:03:19 -040078 sk_sp<SharedGenerator> fSharedGenerator;
79 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
Brian Osman9a97c962019-01-12 17:38:16 +000080 // onMakeSubset and its color space may be changed by onMakeColorSpace.
Brian Osmanbd659552018-09-11 10:03:19 -040081 const SkImageInfo fInfo;
82 const SkIPoint fOrigin;
83
84 uint32_t fUniqueID;
85
Brian Osman9a97c962019-01-12 17:38:16 +000086 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
87 // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call.
88 mutable SkMutex fOnMakeColorSpaceMutex;
89 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
90 mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
Brian Osmanbd659552018-09-11 10:03:19 -040091
92#if SK_SUPPORT_GPU
93 // When the SkImage_Lazy goes away, we will iterate over all the unique keys we've used and
94 // send messages to the GrContexts to say the unique keys are no longer valid. The GrContexts
95 // can then release the resources, conntected with the those unique keys, from their caches.
96 mutable SkTDArray<GrUniqueKeyInvalidatedMessage*> fUniqueKeyInvalidatedMessages;
97#endif
98
99 typedef SkImage_Base INHERITED;
100};
101
102#endif