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/GrSurfaceProxyView.h"
18 #include "src/gpu/GrTextureProxy.h"
19
20 class GrRecordingContext;
21 class GrTexture;
22 #endif
23
24 #include <new>
25
26 class GrSamplerState;
27 class SkCachedData;
28 struct SkYUVASizeInfo;
29
30 enum {
31 kNeedNewImageUniqueID = 0
32 };
33
34 class SkImage_Base : public SkImage {
35 public:
36 virtual ~SkImage_Base();
37
onGetSubset()38 virtual SkIRect onGetSubset() const {
39 return { 0, 0, this->width(), this->height() };
40 }
41
onPeekPixels(SkPixmap *)42 virtual bool onPeekPixels(SkPixmap*) const { return false; }
43
onPeekBitmap()44 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
45
46 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
47 int srcX, int srcY, CachingHint) const = 0;
48
context()49 virtual GrContext* context() const { return nullptr; }
50
51 #if SK_SUPPORT_GPU
onFlush(GrContext * context,const GrFlushInfo &)52 virtual GrSemaphoresSubmitted onFlush(GrContext* context, const GrFlushInfo&) {
53 return GrSemaphoresSubmitted::kNo;
54 }
55
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; }
60
61 // If it exists, this returns a pointer to the GrSurfaceProxyView of image. The caller does not
62 // own the returned view and must copy it if they want to gain a ref to the internal proxy.
63 // If the returned view is not null, then it is guaranteed to have a valid proxy. Additionally
64 // this call will flatten a SkImage_GpuYUV to a single texture.
view(GrRecordingContext *)65 virtual const GrSurfaceProxyView* view(GrRecordingContext*) const { return nullptr; }
66
67 virtual GrSurfaceProxyView refView(GrRecordingContext*, GrSamplerState,
68 SkScalar scaleAdjust[2]) const = 0;
refPinnedView(GrRecordingContext *,uint32_t * uniqueID)69 virtual GrSurfaceProxyView refPinnedView(GrRecordingContext*, uint32_t* uniqueID) const {
70 return {};
71 }
isYUVA()72 virtual bool isYUVA() const { return false; }
onGetTexture()73 virtual GrTexture* onGetTexture() const { return nullptr; }
74 #endif
75 virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
76 GrSurfaceOrigin* origin) const;
77
78 // return a read-only copy of the pixels. We promise to not modify them,
79 // but only inspect them (or encode them).
80 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
81
82 virtual sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const = 0;
83
84 virtual sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
85 SkYUVColorSpace*, const void* planes[4]);
onRefEncoded()86 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
87
88 virtual bool onAsLegacyBitmap(SkBitmap*) const;
89
90 // True for picture-backed and codec-backed
onIsLazyGenerated()91 virtual bool onIsLazyGenerated() const { return false; }
92
93 // True for images instantiated in GPU memory
onIsTextureBacked()94 virtual bool onIsTextureBacked() const { return false; }
95
96 // Call when this image is part of the key to a resourcecache entry. This allows the cache
97 // to know automatically those entries can be purged when this SkImage deleted.
notifyAddedToRasterCache()98 virtual void notifyAddedToRasterCache() const {
99 fAddedToRasterCache.store(true);
100 }
101
102 virtual bool onIsValid(GrContext*) const = 0;
103
onPinAsTexture(GrContext *)104 virtual bool onPinAsTexture(GrContext*) const { return false; }
onUnpinAsTexture(GrContext *)105 virtual void onUnpinAsTexture(GrContext*) const {}
106
107 virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
108 SkColorType, sk_sp<SkColorSpace>) const = 0;
109
110 virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
111
112 protected:
113 SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
114
115 private:
116 // Set true by caches when they cache content that's derived from the current pixels.
117 mutable std::atomic<bool> fAddedToRasterCache;
118
119 typedef SkImage INHERITED;
120 };
121
as_IB(SkImage * image)122 static inline SkImage_Base* as_IB(SkImage* image) {
123 return static_cast<SkImage_Base*>(image);
124 }
125
as_IB(const sk_sp<SkImage> & image)126 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
127 return static_cast<SkImage_Base*>(image.get());
128 }
129
as_IB(const SkImage * image)130 static inline const SkImage_Base* as_IB(const SkImage* image) {
131 return static_cast<const SkImage_Base*>(image);
132 }
133
134 #endif
135