1 | /* |
2 | * Copyright 2012 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_Base_DEFINED |
9 | #define SkImage_Base_DEFINED |
10 | |
11 | #include "include/core/SkImage.h" |
12 | #include "include/core/SkSurface.h" |
13 | #include "src/core/SkMipmap.h" |
14 | #include <atomic> |
15 | |
16 | #if SK_SUPPORT_GPU |
17 | #include "include/private/SkTDArray.h" |
18 | #include "src/gpu/GrSurfaceProxyView.h" |
19 | #include "src/gpu/GrTextureProxy.h" |
20 | |
21 | class GrTexture; |
22 | #endif |
23 | |
24 | #include <new> |
25 | |
26 | class GrDirectContext; |
27 | class GrSamplerState; |
28 | class SkCachedData; |
29 | struct SkYUVASizeInfo; |
30 | |
31 | enum { |
32 | kNeedNewImageUniqueID = 0 |
33 | }; |
34 | |
35 | class SkImage_Base : public SkImage { |
36 | public: |
37 | ~SkImage_Base() override; |
38 | |
39 | virtual bool onPeekPixels(SkPixmap*) const { return false; } |
40 | |
41 | virtual const SkBitmap* onPeekBitmap() const { return nullptr; } |
42 | |
43 | virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
44 | int srcX, int srcY, CachingHint) const = 0; |
45 | |
46 | virtual SkMipmap* onPeekMips() const { return nullptr; } |
47 | |
48 | sk_sp<SkMipmap> refMips() const { |
49 | return sk_ref_sp(this->onPeekMips()); |
50 | } |
51 | |
52 | /** |
53 | * Default implementation does a rescale/read and then calls the callback. |
54 | */ |
55 | virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&, |
56 | const SkIRect& srcRect, |
57 | RescaleGamma, |
58 | SkFilterQuality, |
59 | ReadPixelsCallback, |
60 | ReadPixelsContext); |
61 | /** |
62 | * Default implementation does a rescale/read/yuv conversion and then calls the callback. |
63 | */ |
64 | virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, |
65 | sk_sp<SkColorSpace> dstColorSpace, |
66 | const SkIRect& srcRect, |
67 | const SkISize& dstSize, |
68 | RescaleGamma, |
69 | SkFilterQuality, |
70 | ReadPixelsCallback, |
71 | ReadPixelsContext); |
72 | |
73 | virtual GrContext* context() const { return nullptr; } |
74 | |
75 | #if SK_SUPPORT_GPU |
76 | virtual GrSemaphoresSubmitted onFlush(GrDirectContext*, const GrFlushInfo&) { |
77 | return GrSemaphoresSubmitted::kNo; |
78 | } |
79 | |
80 | // Return the proxy if this image is backed by a single proxy. For YUVA images, this |
81 | // will return nullptr unless the YUVA planes have been converted to RGBA in which case |
82 | // that single backing proxy will be returned. |
83 | virtual GrTextureProxy* peekProxy() const { return nullptr; } |
84 | |
85 | // If it exists, this returns a pointer to the GrSurfaceProxyView of image. The caller does not |
86 | // own the returned view and must copy it if they want to gain a ref to the internal proxy. |
87 | // If the returned view is not null, then it is guaranteed to have a valid proxy. Additionally |
88 | // this call will flatten a SkImage_GpuYUV to a single texture. |
89 | virtual const GrSurfaceProxyView* view(GrRecordingContext*) const { return nullptr; } |
90 | |
91 | virtual GrSurfaceProxyView refView(GrRecordingContext*, GrMipmapped) const = 0; |
92 | virtual GrSurfaceProxyView refPinnedView(GrRecordingContext*, uint32_t* uniqueID) const { |
93 | return {}; |
94 | } |
95 | virtual bool isYUVA() const { return false; } |
96 | #endif |
97 | virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO, |
98 | GrSurfaceOrigin* origin) const; |
99 | |
100 | // return a read-only copy of the pixels. We promise to not modify them, |
101 | // but only inspect them (or encode them). |
102 | virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0; |
103 | |
104 | virtual sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const = 0; |
105 | |
106 | virtual sk_sp<SkData> onRefEncoded() const { return nullptr; } |
107 | |
108 | virtual bool onAsLegacyBitmap(SkBitmap*) const; |
109 | |
110 | // True for picture-backed and codec-backed |
111 | virtual bool onIsLazyGenerated() const { return false; } |
112 | |
113 | // True for images instantiated in GPU memory |
114 | virtual bool onIsTextureBacked() const { return false; } |
115 | |
116 | // Call when this image is part of the key to a resourcecache entry. This allows the cache |
117 | // to know automatically those entries can be purged when this SkImage deleted. |
118 | virtual void notifyAddedToRasterCache() const { |
119 | fAddedToRasterCache.store(true); |
120 | } |
121 | |
122 | virtual bool onIsValid(GrRecordingContext*) const = 0; |
123 | |
124 | virtual bool onPinAsTexture(GrRecordingContext*) const { return false; } |
125 | virtual void onUnpinAsTexture(GrRecordingContext*) const {} |
126 | |
127 | virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>, |
128 | GrDirectContext*) const = 0; |
129 | |
130 | virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0; |
131 | |
132 | // on failure, returns nullptr |
133 | virtual sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const { |
134 | return nullptr; |
135 | } |
136 | |
137 | protected: |
138 | SkImage_Base(const SkImageInfo& info, uint32_t uniqueID); |
139 | |
140 | private: |
141 | // Set true by caches when they cache content that's derived from the current pixels. |
142 | mutable std::atomic<bool> fAddedToRasterCache; |
143 | |
144 | typedef SkImage INHERITED; |
145 | }; |
146 | |
147 | static inline SkImage_Base* as_IB(SkImage* image) { |
148 | return static_cast<SkImage_Base*>(image); |
149 | } |
150 | |
151 | static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) { |
152 | return static_cast<SkImage_Base*>(image.get()); |
153 | } |
154 | |
155 | static inline const SkImage_Base* as_IB(const SkImage* image) { |
156 | return static_cast<const SkImage_Base*>(image); |
157 | } |
158 | |
159 | #endif |
160 | |