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/SkIDChangeListener.h" |
12 | #include "include/private/SkMutex.h" |
13 | #include "src/gpu/SkGr.h" |
14 | #include "src/image/SkImage_Base.h" |
15 | |
16 | #if SK_SUPPORT_GPU |
17 | #include "src/gpu/GrTextureMaker.h" |
18 | #endif |
19 | |
20 | class SharedGenerator; |
21 | |
22 | class SkImage_Lazy : public SkImage_Base { |
23 | public: |
24 | struct Validator { |
25 | Validator(sk_sp<SharedGenerator>, const SkColorType*, sk_sp<SkColorSpace>); |
26 | |
27 | operator bool() const { return fSharedGenerator.get(); } |
28 | |
29 | sk_sp<SharedGenerator> fSharedGenerator; |
30 | SkImageInfo fInfo; |
31 | sk_sp<SkColorSpace> fColorSpace; |
32 | uint32_t fUniqueID; |
33 | }; |
34 | |
35 | SkImage_Lazy(Validator* validator); |
36 | |
37 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, |
38 | CachingHint) const override; |
39 | #if SK_SUPPORT_GPU |
40 | GrSurfaceProxyView refView(GrRecordingContext*, GrMipmapped) const override; |
41 | #endif |
42 | sk_sp<SkData> onRefEncoded() const override; |
43 | sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const override; |
44 | bool getROPixels(SkBitmap*, CachingHint) const override; |
45 | bool onIsLazyGenerated() const override { return true; } |
46 | sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>, |
47 | GrDirectContext*) const override; |
48 | sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final; |
49 | |
50 | bool onIsValid(GrRecordingContext*) const override; |
51 | |
52 | #if SK_SUPPORT_GPU |
53 | // Returns the texture proxy. CachingHint refers to whether the generator's output should be |
54 | // cached in CPU memory. We will always cache the generated texture on success. |
55 | GrSurfaceProxyView lockTextureProxyView(GrRecordingContext*, |
56 | GrImageTexGenPolicy, |
57 | GrMipmapped) const; |
58 | |
59 | // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This |
60 | // may be different from the color type on the image in the case where we need up upload CPU |
61 | // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert |
62 | // the data to RGBA_8888 unorm on the CPU then upload that. |
63 | GrColorType colorTypeOfLockTextureProxy(const GrCaps* caps) const; |
64 | #endif |
65 | |
66 | private: |
67 | void addUniqueIDListener(sk_sp<SkIDChangeListener>) const; |
68 | #if SK_SUPPORT_GPU |
69 | sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, |
70 | SkYUVAIndex[4], |
71 | SkYUVColorSpace*, |
72 | const void* planes[4]) const; |
73 | GrSurfaceProxyView textureProxyViewFromPlanes(GrRecordingContext*, SkBudgeted) const; |
74 | #endif |
75 | |
76 | class ScopedGenerator; |
77 | |
78 | // Note that this->imageInfo() is not necessarily the info from the generator. It may be |
79 | // cropped by onMakeSubset and its color type/space may be changed by |
80 | // onMakeColorTypeAndColorSpace. |
81 | sk_sp<SharedGenerator> fSharedGenerator; |
82 | |
83 | // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs |
84 | // and SkImage_Lazy instances. Cache the result of the last successful call. |
85 | mutable SkMutex fOnMakeColorTypeAndSpaceMutex; |
86 | mutable sk_sp<SkImage> fOnMakeColorTypeAndSpaceResult; |
87 | |
88 | #if SK_SUPPORT_GPU |
89 | // When the SkImage_Lazy goes away, we will iterate over all the listeners to inform them |
90 | // of the unique ID's demise. This is used to remove cached textures from GrContext. |
91 | mutable SkIDChangeListener::List fUniqueIDListeners; |
92 | #endif |
93 | |
94 | typedef SkImage_Base INHERITED; |
95 | }; |
96 | |
97 | #endif |
98 | |