blob: 739b5b50ab35a9ac87570f59b7e0ec08ceabb865 [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 {
23 Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace);
24
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*,
49 const GrSamplerState&, SkColorSpace*,
50 sk_sp<SkColorSpace>*,
51 SkScalar scaleAdjust[2]) const override;
Jim Van Verth8f11e432018-10-18 14:36:59 -040052 sk_sp<SkCachedData> getPlanes(SkYUVSizeInfo*, SkYUVAIndex[4],
53 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 Osman15f0f292018-10-01 14:14:46 -040059 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const override;
Brian Osmanbd659552018-09-11 10:03:19 -040060
61 bool onIsValid(GrContext*) const override;
62
Brian Osmana0dc3d22018-10-09 15:17:38 -040063 // Only return true if the generate has already been cached, in a format that matches the info.
64 bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*, const SkImageInfo&) const;
Brian Osmanbd659552018-09-11 10:03:19 -040065 // Call the underlying generator directly
66 bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
67 int srcX, int srcY) const;
68
69#if SK_SUPPORT_GPU
70 // Returns the texture proxy. If we're going to generate and cache the texture, we should use
71 // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the
72 // texture is not trivial to construct, returns nullptr.
73 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
74 const GrUniqueKey& key,
75 SkImage::CachingHint,
76 bool willBeMipped,
77 SkColorSpace* dstColorSpace,
78 GrTextureMaker::AllowedTexGenType genType) const;
79
80 // TODO: Need to pass in dstColorSpace to fold into key here?
81 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const;
82#endif
83
84private:
85 class ScopedGenerator;
86
87 /**
88 * On success (true), bitmap will point to the pixels for this generator. If this returns
89 * false, the bitmap will be reset to empty.
90 * TODO: Pass in dstColorSpace to ensure bitmap is compatible?
91 */
92 bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, const SkImageInfo&) const;
93
94 sk_sp<SharedGenerator> fSharedGenerator;
95 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
96 // onMakeSubset and its color space may be changed by onMakeColorSpace.
97 const SkImageInfo fInfo;
98 const SkIPoint fOrigin;
99
100 uint32_t fUniqueID;
101
102 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
103 // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call.
104 mutable SkMutex fOnMakeColorSpaceMutex;
105 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
106 mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
107
108#if SK_SUPPORT_GPU
109 // When the SkImage_Lazy goes away, we will iterate over all the unique keys we've used and
110 // send messages to the GrContexts to say the unique keys are no longer valid. The GrContexts
111 // can then release the resources, conntected with the those unique keys, from their caches.
112 mutable SkTDArray<GrUniqueKeyInvalidatedMessage*> fUniqueKeyInvalidatedMessages;
113#endif
114
115 typedef SkImage_Base INHERITED;
116};
117
118#endif