| 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/SkData.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkTypes.h" |
| 15 |
#include "src/core/SkMipmap.h" |
| 16 |
|
| 17 |
#include <atomic> |
| 18 |
#include <cstddef> |
| 19 |
#include <cstdint> |
| 20 |
|
| 21 |
class GrDirectContext; |
| 22 |
class GrImageContext; |
| 23 |
class SkBitmap; |
| 24 |
class SkColorSpace; |
| 25 |
class SkPixmap; |
| 26 |
class SkRecorder; |
| 27 |
class SkSurface; |
| 28 |
enum SkYUVColorSpace : int; |
| 29 |
struct SkIRect; |
| 30 |
struct SkISize; |
| 31 |
struct SkImageInfo; |
| 32 |
|
| 33 |
enum { |
| 34 |
kNeedNewImageUniqueID = 0 |
| 35 |
}; |
| 36 |
|
| 37 |
class SkImage_Base : public SkImage { |
| 38 |
public: |
| 39 |
~SkImage_Base() override; |
| 40 |
|
| 41 |
// From SkImage.h |
| 42 |
sk_sp<SkImage> makeColorSpace(SkRecorder*, |
| 43 |
sk_sp<SkColorSpace>, |
| 44 |
RequiredProperties) const override; |
| 45 |
|
| 46 |
sk_sp<SkImage> makeSubset(SkRecorder*, const SkIRect&, RequiredProperties) const override; |
| 47 |
size_t textureSize() const override { return 0; } |
| 48 |
|
| 49 |
// Methods that we want to use elsewhere in Skia, but not be a part of the public API. |
| 50 |
virtual bool onPeekPixels(SkPixmap*) const { return false; } |
| 51 |
|
| 52 |
virtual const SkBitmap* onPeekBitmap() const { return nullptr; } |
| 53 |
|
| 54 |
virtual bool onReadPixels(GrDirectContext*, |
| 55 |
const SkImageInfo& dstInfo, |
| 56 |
void* dstPixels, |
| 57 |
size_t dstRowBytes, |
| 58 |
int srcX, |
| 59 |
int srcY, |
| 60 |
CachingHint) const = 0; |
| 61 |
|
| 62 |
virtual bool readPixelsGraphite(SkRecorder*, const SkPixmap& dst, int srcX, int srcY) const { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
virtual bool onHasMipmaps() const = 0; |
| 67 |
virtual bool onIsProtected() const = 0; |
| 68 |
|
| 69 |
virtual SkMipmap* onPeekMips() const { return nullptr; } |
| 70 |
|
| 71 |
sk_sp<SkMipmap> refMips() const { |
| 72 |
return sk_ref_sp(this->onPeekMips()); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Default implementation does a rescale/read and then calls the callback. |
| 77 |
*/ |
| 78 |
virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&, |
| 79 |
SkIRect srcRect, |
| 80 |
RescaleGamma, |
| 81 |
RescaleMode, |
| 82 |
ReadPixelsCallback, |
| 83 |
ReadPixelsContext) const; |
| 84 |
/** |
| 85 |
* Default implementation does a rescale/read/yuv conversion and then calls the callback. |
| 86 |
*/ |
| 87 |
virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, |
| 88 |
bool readAlpha, |
| 89 |
sk_sp<SkColorSpace> dstColorSpace, |
| 90 |
SkIRect srcRect, |
| 91 |
SkISize dstSize, |
| 92 |
RescaleGamma, |
| 93 |
RescaleMode, |
| 94 |
ReadPixelsCallback, |
| 95 |
ReadPixelsContext) const; |
| 96 |
|
| 97 |
virtual GrImageContext* context() const { return nullptr; } |
| 98 |
|
| 99 |
/** this->context() try-casted to GrDirectContext. Useful for migrations – avoid otherwise! */ |
| 100 |
virtual GrDirectContext* directContext() const { return nullptr; } |
| 101 |
|
| 102 |
// If this image is the current cached image snapshot of a surface then this is called when the |
| 103 |
// surface is destroyed to indicate no further writes may happen to surface backing store. |
| 104 |
virtual void generatingSurfaceIsDeleted() {} |
| 105 |
|
| 106 |
// return a read-only copy of the pixels. We promise to not modify them, |
| 107 |
// but only inspect them (or encode them). |
| 108 |
virtual bool getROPixels(GrDirectContext*, SkBitmap*, |
| 109 |
CachingHint = kAllow_CachingHint) const = 0; |
| 110 |
|
| 111 |
virtual sk_sp<SkImage> onMakeSubset(SkRecorder*, const SkIRect&, RequiredProperties) const = 0; |
| 112 |
|
| 113 |
virtual sk_sp<const SkData> onRefEncoded() const { return nullptr; } |
| 114 |
|
| 115 |
virtual bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const; |
| 116 |
|
| 117 |
// Create the surface used by makeScaled. If this is a GPU backed image, the surface |
| 118 |
// should be Ganesh or Graphite backed (as appropriate), otherwise this can raster backed. |
| 119 |
virtual sk_sp<SkSurface> onMakeSurface(SkRecorder*, const SkImageInfo&) const = 0; |
| 120 |
|
| 121 |
enum class Type { |
| 122 |
kRaster, |
| 123 |
kRasterPinnable, |
| 124 |
kLazy, |
| 125 |
kLazyPicture, |
| 126 |
kLazyTexture, |
| 127 |
kGanesh, |
| 128 |
kGaneshYUVA, |
| 129 |
kGraphite, |
| 130 |
kGraphiteYUVA, |
| 131 |
}; |
| 132 |
|
| 133 |
virtual Type type() const = 0; |
| 134 |
|
| 135 |
// True for picture-backed and codec-backed |
| 136 |
bool isLazyGenerated() const override { |
| 137 |
return this->type() == Type::kLazy || this->type() == Type::kLazyPicture || |
| 138 |
this->type() == Type::kLazyTexture; |
| 139 |
} |
| 140 |
|
| 141 |
bool isRasterBacked() const { |
| 142 |
return this->type() == Type::kRaster || this->type() == Type::kRasterPinnable; |
| 143 |
} |
| 144 |
|
| 145 |
// True for images instantiated by Ganesh in GPU memory |
| 146 |
bool isGaneshBacked() const { |
| 147 |
return this->type() == Type::kGanesh || this->type() == Type::kGaneshYUVA; |
| 148 |
} |
| 149 |
|
| 150 |
// True for images instantiated by Graphite in GPU memory |
| 151 |
bool isGraphiteBacked() const { |
| 152 |
return this->type() == Type::kGraphite || this->type() == Type::kGraphiteYUVA; |
| 153 |
} |
| 154 |
|
| 155 |
bool isYUVA() const { |
| 156 |
return this->type() == Type::kGaneshYUVA || this->type() == Type::kGraphiteYUVA; |
| 157 |
} |
| 158 |
|
| 159 |
bool isTextureBacked() const override { |
| 160 |
return this->isGaneshBacked() || this->isGraphiteBacked(); |
| 161 |
} |
| 162 |
|
| 163 |
// Call when this image is part of the key to a resourcecache entry. This allows the cache |
| 164 |
// to know automatically those entries can be purged when this SkImage deleted. |
| 165 |
virtual void notifyAddedToRasterCache() const { |
| 166 |
fAddedToRasterCache.store(true); |
| 167 |
} |
| 168 |
|
| 169 |
virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0; |
| 170 |
|
| 171 |
// on failure, returns nullptr |
| 172 |
// NOLINTNEXTLINE(performance-unnecessary-value-param) |
| 173 |
virtual sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const { |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
|
| 177 |
protected: |
| 178 |
SkImage_Base(const SkImageInfo& info, uint32_t uniqueID); |
| 179 |
|
| 180 |
private: |
| 181 |
// Set true by caches when they cache content that's derived from the current pixels. |
| 182 |
|
| 183 |
mutable std::atomic<bool> fAddedToRasterCache; |
| 184 |
}; |
| 185 |
|
| 186 |
static inline SkImage_Base* as_IB(SkImage* image) { |
| 187 |
return static_cast<SkImage_Base*>(image); |
| 188 |
} |
| 189 |
|
| 190 |
static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) { |
| 191 |
return static_cast<SkImage_Base*>(image.get()); |
| 192 |
} |
| 193 |
|
| 194 |
static inline const SkImage_Base* as_IB(const SkImage* image) { |
| 195 |
return static_cast<const SkImage_Base*>(image); |
| 196 |
} |
| 197 |
|
| 198 |
static inline const SkImage_Base* as_IB(const sk_sp<const SkImage>& image) { |
| 199 |
return static_cast<const SkImage_Base*>(image.get()); |
| 200 |
} |
| 201 |
|
| 202 |
#endif |