blob: 51b893aa288039ba6d763833a2dc0b61ca14b7ab [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 Osmanf48c9962019-01-14 11:15:50 -050023 Validator(sk_sp<SharedGenerator>, const SkIRect* subset, const SkColorType* colorType,
24 sk_sp<SkColorSpace> colorSpace);
Brian Osmanbd659552018-09-11 10:03:19 -040025
26 operator bool() const { return fSharedGenerator.get(); }
27
28 sk_sp<SharedGenerator> fSharedGenerator;
29 SkImageInfo fInfo;
30 SkIPoint fOrigin;
31 sk_sp<SkColorSpace> fColorSpace;
32 uint32_t fUniqueID;
33 };
34
35 SkImage_Lazy(Validator* validator);
36 ~SkImage_Lazy() override;
37
38 SkImageInfo onImageInfo() const override {
39 return fInfo;
40 }
Brian Osmanbd659552018-09-11 10:03:19 -040041
42 SkIRect onGetSubset() const override {
43 return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height());
44 }
45
46 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
47 CachingHint) const override;
48#if SK_SUPPORT_GPU
49 sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*,
Brian Osmane7fd8c32018-10-19 13:30:39 -040050 const GrSamplerState&,
Brian Osmanbd659552018-09-11 10:03:19 -040051 SkScalar scaleAdjust[2]) const override;
Jim Van Verthe24b5872018-10-29 16:26:02 -040052 sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
Jim Van Verth8f11e432018-10-18 14:36:59 -040053 SkYUVColorSpace*, const void* planes[4]) override;
Robert Phillips4524e252018-09-21 14:20:38 -040054#endif
Brian Osmanbd659552018-09-11 10:03:19 -040055 sk_sp<SkData> onRefEncoded() const override;
56 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
Brian Osmane50cdf02018-10-19 13:02:14 -040057 bool getROPixels(SkBitmap*, CachingHint) const override;
Brian Osmanbd659552018-09-11 10:03:19 -040058 bool onIsLazyGenerated() const override { return true; }
Brian Osmanf48c9962019-01-14 11:15:50 -050059 sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>) const override;
Brian Osmanbd659552018-09-11 10:03:19 -040060
61 bool onIsValid(GrContext*) const override;
62
Brian Osmanbd659552018-09-11 10:03:19 -040063#if SK_SUPPORT_GPU
64 // Returns the texture proxy. If we're going to generate and cache the texture, we should use
65 // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the
66 // texture is not trivial to construct, returns nullptr.
67 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
68 const GrUniqueKey& key,
69 SkImage::CachingHint,
70 bool willBeMipped,
Brian Osmanbd659552018-09-11 10:03:19 -040071 GrTextureMaker::AllowedTexGenType genType) const;
72
Brian Osmanbd659552018-09-11 10:03:19 -040073 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const;
74#endif
75
76private:
77 class ScopedGenerator;
78
Brian Osmanbd659552018-09-11 10:03:19 -040079 sk_sp<SharedGenerator> fSharedGenerator;
80 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
Brian Osmanf48c9962019-01-14 11:15:50 -050081 // onMakeSubset and its color type/space may be changed by onMakeColorTypeAndColorSpace.
Brian Osmanbd659552018-09-11 10:03:19 -040082 const SkImageInfo fInfo;
83 const SkIPoint fOrigin;
84
85 uint32_t fUniqueID;
86
Brian Osmanf48c9962019-01-14 11:15:50 -050087 // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs
88 // and SkImage_Lazy instances. Cache the result of the last successful call.
89 mutable SkMutex fOnMakeColorTypeAndSpaceMutex;
90 mutable sk_sp<SkImage> fOnMakeColorTypeAndSpaceResult;
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