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
20class SharedGenerator;
21
22class SkImage_Lazy : public SkImage_Base {
23public:
24 struct Validator {
25 Validator(sk_sp<SharedGenerator>, const SkIRect* subset, const SkColorType* colorType,
26 sk_sp<SkColorSpace> colorSpace);
27
28 operator bool() const { return fSharedGenerator.get(); }
29
30 sk_sp<SharedGenerator> fSharedGenerator;
31 SkImageInfo fInfo;
32 SkIPoint fOrigin;
33 sk_sp<SkColorSpace> fColorSpace;
34 uint32_t fUniqueID;
35 };
36
37 SkImage_Lazy(Validator* validator);
38
39 SkIRect onGetSubset() const override {
40 return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, this->width(), this->height());
41 }
42
43 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
44 CachingHint) const override;
45#if SK_SUPPORT_GPU
46 GrSurfaceProxyView refView(GrRecordingContext*, GrMipMapped) 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;
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. CachingHint refers to whether the generator's output should be
62 // cached in CPU memory. We will always cache the generated texture on success.
63 GrSurfaceProxyView lockTextureProxyView(GrRecordingContext*,
64 GrImageTexGenPolicy,
65 GrMipMapped) const;
66
67 // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This
68 // may be different from the color type on the image in the case where we need up upload CPU
69 // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert
70 // the data to RGBA_8888 unorm on the CPU then upload that.
71 GrColorType colorTypeOfLockTextureProxy(const GrCaps* caps) const;
72#endif
73
74private:
75 void addUniqueIDListener(sk_sp<SkIDChangeListener>) const;
76
77 class ScopedGenerator;
78
79 // Note that this->imageInfo() is not necessarily the info from the generator. It may be
80 // cropped by onMakeSubset and its color type/space may be changed by
81 // onMakeColorTypeAndColorSpace.
82 sk_sp<SharedGenerator> fSharedGenerator;
83 const SkIPoint fOrigin;
84
85 uint32_t fUniqueID;
86
87 // 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;
91
92#if SK_SUPPORT_GPU
93 // When the SkImage_Lazy goes away, we will iterate over all the listeners to inform them
94 // of the unique ID's demise. This is used to remove cached textures from GrContext.
95 mutable SkIDChangeListener::List fUniqueIDListeners;
96#endif
97
98 typedef SkImage_Base INHERITED;
99};
100
101#endif
102