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
20class GrRecordingContext;
21class GrTexture;
22#endif
23
24#include <new>
25
26class GrSamplerState;
27class SkCachedData;
28struct SkYUVASizeInfo;
29
30enum {
31 kNeedNewImageUniqueID = 0
32};
33
34class SkImage_Base : public SkImage {
35public:
36 virtual ~SkImage_Base();
37
38 virtual SkIRect onGetSubset() const {
39 return { 0, 0, this->width(), this->height() };
40 }
41
42 virtual bool onPeekPixels(SkPixmap*) const { return false; }
43
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
49 virtual GrContext* context() const { return nullptr; }
50
51#if SK_SUPPORT_GPU
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.
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.
65 virtual const GrSurfaceProxyView* view(GrRecordingContext*) const { return nullptr; }
66
67 virtual GrSurfaceProxyView refView(GrRecordingContext*, GrMipMapped) const = 0;
68 virtual GrSurfaceProxyView refPinnedView(GrRecordingContext*, uint32_t* uniqueID) const {
69 return {};
70 }
71 virtual bool isYUVA() const { return false; }
72#endif
73 virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
74 GrSurfaceOrigin* origin) const;
75
76 // return a read-only copy of the pixels. We promise to not modify them,
77 // but only inspect them (or encode them).
78 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
79
80 virtual sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect&) const = 0;
81
82 virtual sk_sp<SkCachedData> getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
83 SkYUVColorSpace*, const void* planes[4]);
84 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
85
86 virtual bool onAsLegacyBitmap(SkBitmap*) const;
87
88 // True for picture-backed and codec-backed
89 virtual bool onIsLazyGenerated() const { return false; }
90
91 // True for images instantiated in GPU memory
92 virtual bool onIsTextureBacked() const { return false; }
93
94 // Call when this image is part of the key to a resourcecache entry. This allows the cache
95 // to know automatically those entries can be purged when this SkImage deleted.
96 virtual void notifyAddedToRasterCache() const {
97 fAddedToRasterCache.store(true);
98 }
99
100 virtual bool onIsValid(GrContext*) const = 0;
101
102 virtual bool onPinAsTexture(GrContext*) const { return false; }
103 virtual void onUnpinAsTexture(GrContext*) const {}
104
105 virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
106 SkColorType, sk_sp<SkColorSpace>) const = 0;
107
108 virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
109
110protected:
111 SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
112
113private:
114 // Set true by caches when they cache content that's derived from the current pixels.
115 mutable std::atomic<bool> fAddedToRasterCache;
116
117 typedef SkImage INHERITED;
118};
119
120static inline SkImage_Base* as_IB(SkImage* image) {
121 return static_cast<SkImage_Base*>(image);
122}
123
124static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
125 return static_cast<SkImage_Base*>(image.get());
126}
127
128static inline const SkImage_Base* as_IB(const SkImage* image) {
129 return static_cast<const SkImage_Base*>(image);
130}
131
132#endif
133