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