• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "include/private/SkMutex.h"
12 #include "src/image/SkImage_Base.h"
13 
14 #if SK_SUPPORT_GPU
15 #include "src/gpu/GrTextureMaker.h"
16 #endif
17 
18 class SharedGenerator;
19 
20 class SkImage_Lazy : public SkImage_Base {
21 public:
22     struct Validator {
23         Validator(sk_sp<SharedGenerator>, const SkIRect* subset, const SkColorType* colorType,
24                   sk_sp<SkColorSpace> colorSpace);
25 
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 
onGetSubset()38     SkIRect onGetSubset() const override {
39         return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, this->width(), this->height());
40     }
41 
42     bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
43                       CachingHint) const override;
44 #if SK_SUPPORT_GPU
45     GrSurfaceProxyView refView(GrRecordingContext*, GrSamplerState,
46                                SkScalar scaleAdjust[2]) const override;
47     sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
48                                   SkYUVColorSpace*, const void* planes[4]) override;
49 #endif
50     sk_sp<SkData> onRefEncoded() const override;
51     sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const override;
52     bool getROPixels(SkBitmap*, CachingHint) const override;
onIsLazyGenerated()53     bool onIsLazyGenerated() const override { return true; }
54     sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
55                                                 SkColorType, sk_sp<SkColorSpace>) const override;
56     sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final;
57 
58     bool onIsValid(GrContext*) const override;
59 
60 #if SK_SUPPORT_GPU
61     // Returns the texture proxy. If we're going to generate and cache the texture, we should use
62     // the passed in key (if the key is valid). If genType is AllowedTexGenType::kCheap and the
63     // texture is not trivial to construct, returns nullptr.
64     GrSurfaceProxyView lockTextureProxyView(GrRecordingContext*,
65                                             const GrUniqueKey& key,
66                                             SkImage::CachingHint,
67                                             bool willBeMipped,
68                                             GrTextureMaker::AllowedTexGenType genType) const;
69 
70     // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This
71     // may be different from the color type on the image in the case where we need up upload CPU
72     // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert
73     // the data to RGBA_8888 unorm on the CPU then upload that.
74     GrColorType colorTypeOfLockTextureProxy(const GrCaps* caps) const;
75 
76     void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) const;
77 #endif
78 
79 private:
80     class ScopedGenerator;
81 
82     // Note that this->imageInfo() is not necessarily the info from the generator. It may be
83     // cropped by onMakeSubset and its color type/space may be changed by
84     // onMakeColorTypeAndColorSpace.
85     sk_sp<SharedGenerator> fSharedGenerator;
86     const SkIPoint         fOrigin;
87 
88     uint32_t fUniqueID;
89 
90     // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs
91     // and SkImage_Lazy instances. Cache the result of the last successful call.
92     mutable SkMutex             fOnMakeColorTypeAndSpaceMutex;
93     mutable sk_sp<SkImage>      fOnMakeColorTypeAndSpaceResult;
94 
95 #if SK_SUPPORT_GPU
96     // When the SkImage_Lazy goes away, we will iterate over all the unique keys we've used and
97     // send messages to the GrContexts to say the unique keys are no longer valid. The GrContexts
98     // can then release the resources, conntected with the those unique keys, from their caches.
99     mutable SkTDArray<GrUniqueKeyInvalidatedMessage*> fUniqueKeyInvalidatedMessages;
100 #endif
101 
102     typedef SkImage_Base INHERITED;
103 };
104 
105 #endif
106