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 "SkImage.h"
12 #include "SkSurface.h"
13 #include <atomic>
14 
15 #if SK_SUPPORT_GPU
16 #include "GrTextureProxy.h"
17 #include "SkTDArray.h"
18 
19 class GrRecordingContext;
20 class GrTexture;
21 #endif
22 
23 #include <new>
24 
25 class GrSamplerState;
26 class SkCachedData;
27 struct SkYUVASizeInfo;
28 
29 enum {
30     kNeedNewImageUniqueID = 0
31 };
32 
33 class SkImage_Base : public SkImage {
34 public:
35     virtual ~SkImage_Base();
36 
37     // User: returns image info for this SkImage.
38     // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
39     // & unknown color space.
40     virtual SkImageInfo onImageInfo() const = 0;
41 
onGetSubset()42     virtual SkIRect onGetSubset() const {
43         return { 0, 0, this->width(), this->height() };
44     }
45 
onPeekPixels(SkPixmap *)46     virtual bool onPeekPixels(SkPixmap*) const { return false; }
47 
onPeekBitmap()48     virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
49 
50     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
51                               int srcX, int srcY, CachingHint) const = 0;
52 
context()53     virtual GrContext* context() const { return nullptr; }
54 
55 #if SK_SUPPORT_GPU
56     // Return the proxy if this image is backed by a single proxy. For YUVA images, this
57     // will return nullptr unless the YUVA planes have been converted to RGBA in which case
58     // that single backing proxy will be returned.
peekProxy()59     virtual GrTextureProxy* peekProxy() const { return nullptr; }
asTextureProxyRef(GrRecordingContext *)60     virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*) const { return nullptr; }
61     virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*, const GrSamplerState&,
62                                                     SkScalar scaleAdjust[2]) const = 0;
refPinnedTextureProxy(GrRecordingContext *,uint32_t * uniqueID)63     virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(GrRecordingContext*,
64                                                         uint32_t* uniqueID) const {
65         return nullptr;
66     }
isYUVA()67     virtual bool isYUVA() const { return false; }
asYUVATextureProxiesRef(sk_sp<GrTextureProxy>[4],SkYUVAIndex[4],SkYUVColorSpace *)68     virtual bool asYUVATextureProxiesRef(sk_sp<GrTextureProxy>[4], SkYUVAIndex[4],
69                                          SkYUVColorSpace*) const { return false; }
onGetTexture()70     virtual GrTexture* onGetTexture() const { return nullptr; }
71 #endif
72     virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
73                                                  GrSurfaceOrigin* origin) const;
74 
75     // return a read-only copy of the pixels. We promise to not modify them,
76     // but only inspect them (or encode them).
77     virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
78 
79     virtual sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const = 0;
80 
81     virtual sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
82                                           SkYUVColorSpace*, const void* planes[4]);
onRefEncoded()83     virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
84 
85     virtual bool onAsLegacyBitmap(SkBitmap*) const;
86 
87     // True for picture-backed and codec-backed
onIsLazyGenerated()88     virtual bool onIsLazyGenerated() const { return false; }
89 
90     // True for images instantiated in GPU memory
onIsTextureBacked()91     virtual bool onIsTextureBacked() const { return false; }
92 
93     // Call when this image is part of the key to a resourcecache entry. This allows the cache
94     // to know automatically those entries can be purged when this SkImage deleted.
notifyAddedToRasterCache()95     virtual void notifyAddedToRasterCache() const {
96         fAddedToRasterCache.store(true);
97     }
98 
99     virtual bool onIsValid(GrContext*) const = 0;
100 
onPinAsTexture(GrContext *)101     virtual bool onPinAsTexture(GrContext*) const { return false; }
onUnpinAsTexture(GrContext *)102     virtual void onUnpinAsTexture(GrContext*) const {}
103 
104     virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
105                                                         SkColorType, sk_sp<SkColorSpace>) const = 0;
106 protected:
107     SkImage_Base(int width, int height, uint32_t uniqueID);
108 
109 private:
110     // Set true by caches when they cache content that's derived from the current pixels.
111     mutable std::atomic<bool> fAddedToRasterCache;
112 
113     typedef SkImage INHERITED;
114 };
115 
as_IB(SkImage * image)116 static inline SkImage_Base* as_IB(SkImage* image) {
117     return static_cast<SkImage_Base*>(image);
118 }
119 
as_IB(const sk_sp<SkImage> & image)120 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
121     return static_cast<SkImage_Base*>(image.get());
122 }
123 
as_IB(const SkImage * image)124 static inline const SkImage_Base* as_IB(const SkImage* image) {
125     return static_cast<const SkImage_Base*>(image);
126 }
127 
128 #endif
129