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 <atomic>
14
15 #if SK_SUPPORT_GPU
16 #include "include/private/SkTDArray.h"
17 #include "src/gpu/GrTextureProxy.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
onGetSubset()37 virtual SkIRect onGetSubset() const {
38 return { 0, 0, this->width(), this->height() };
39 }
40
onPeekPixels(SkPixmap *)41 virtual bool onPeekPixels(SkPixmap*) const { return false; }
42
onPeekBitmap()43 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
44
45 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
46 int srcX, int srcY, CachingHint) const = 0;
47
context()48 virtual GrContext* context() const { return nullptr; }
49
50 #if SK_SUPPORT_GPU
onFlush(GrContext * context,const GrFlushInfo &)51 virtual GrSemaphoresSubmitted onFlush(GrContext* context, const GrFlushInfo&) {
52 return GrSemaphoresSubmitted::kNo;
53 }
54
55 // Return the proxy if this image is backed by a single proxy. For YUVA images, this
56 // will return nullptr unless the YUVA planes have been converted to RGBA in which case
57 // that single backing proxy will be returned.
peekProxy()58 virtual GrTextureProxy* peekProxy() const { return nullptr; }
asTextureProxyRef(GrRecordingContext *)59 virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*) const { return nullptr; }
60 virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*, const GrSamplerState&,
61 SkScalar scaleAdjust[2]) const = 0;
refPinnedTextureProxy(GrRecordingContext *,uint32_t * uniqueID)62 virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(GrRecordingContext*,
63 uint32_t* uniqueID) const {
64 return nullptr;
65 }
isYUVA()66 virtual bool isYUVA() const { return false; }
onGetTexture()67 virtual GrTexture* onGetTexture() const { return nullptr; }
68 #endif
69 virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
70 GrSurfaceOrigin* origin) const;
71
72 // return a read-only copy of the pixels. We promise to not modify them,
73 // but only inspect them (or encode them).
74 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
75
76 virtual sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const = 0;
77
78 virtual sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
79 SkYUVColorSpace*, const void* planes[4]);
onRefEncoded()80 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
81
82 virtual bool onAsLegacyBitmap(SkBitmap*) const;
83
84 // True for picture-backed and codec-backed
onIsLazyGenerated()85 virtual bool onIsLazyGenerated() const { return false; }
86
87 // True for images instantiated in GPU memory
onIsTextureBacked()88 virtual bool onIsTextureBacked() const { return false; }
89
90 // Call when this image is part of the key to a resourcecache entry. This allows the cache
91 // to know automatically those entries can be purged when this SkImage deleted.
notifyAddedToRasterCache()92 virtual void notifyAddedToRasterCache() const {
93 fAddedToRasterCache.store(true);
94 }
95
96 virtual bool onIsValid(GrContext*) const = 0;
97
onPinAsTexture(GrContext *)98 virtual bool onPinAsTexture(GrContext*) const { return false; }
onUnpinAsTexture(GrContext *)99 virtual void onUnpinAsTexture(GrContext*) const {}
100
101 virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
102 SkColorType, sk_sp<SkColorSpace>) const = 0;
103
104 virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
105
106 protected:
107 SkImage_Base(const SkImageInfo& info, 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