| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 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_Raster_DEFINED |
| 9 |
#define SkImage_Raster_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkPixelRef.h" |
| 14 |
#include "include/core/SkRecorder.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkTypes.h" |
| 17 |
#include "include/private/base/SkTo.h" |
| 18 |
#include "src/core/SkMipmap.h" |
| 19 |
#include "src/image/SkImage_Base.h" |
| 20 |
|
| 21 |
#include <cstddef> |
| 22 |
#include <cstdint> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
class GrDirectContext; |
| 26 |
class SkColorSpace; |
| 27 |
class SkData; |
| 28 |
class SkPixmap; |
| 29 |
class SkSurface; |
| 30 |
class SkImageShader; |
| 31 |
enum SkColorType : int; |
| 32 |
struct SkIRect; |
| 33 |
struct SkImageInfo; |
| 34 |
|
| 35 |
enum class SkCopyPixelsMode { |
| 36 |
kIfMutable, //!< only copy src pixels if they are marked mutable |
| 37 |
kAlways, //!< always copy src pixels (even if they are marked immutable) |
| 38 |
kNever, //!< never copy src pixels (even if they are marked mutable) |
| 39 |
}; |
| 40 |
|
| 41 |
class SkImage_Raster : public SkImage_Base { |
| 42 |
public: |
| 43 |
SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb, sk_sp<SkMipmap>, uint32_t id); |
| 44 |
~SkImage_Raster() override; |
| 45 |
|
| 46 |
// From SkImage.h |
| 47 |
bool isValid(SkRecorder* recorder) const override { |
| 48 |
if (!recorder) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
if (!recorder->cpuRecorder()) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
return true; |
| 55 |
} |
| 56 |
sk_sp<SkImage> makeColorTypeAndColorSpace(SkRecorder*, |
| 57 |
SkColorType targetColorType, |
| 58 |
sk_sp<SkColorSpace> targetColorSpace, |
| 59 |
RequiredProperties) const override; |
| 60 |
|
| 61 |
// From SkImage_Base.h |
| 62 |
bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY, |
| 63 |
CachingHint) const override; |
| 64 |
bool onPeekPixels(SkPixmap*) const override; |
| 65 |
const SkBitmap* onPeekBitmap() const override { return &fBitmap; } |
| 66 |
|
| 67 |
bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override; |
| 68 |
|
| 69 |
sk_sp<SkImage> onMakeSubset(SkRecorder*, const SkIRect&, RequiredProperties) const override; |
| 70 |
|
| 71 |
sk_sp<SkSurface> onMakeSurface(SkRecorder*, const SkImageInfo&) const final; |
| 72 |
|
| 73 |
SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
| 74 |
|
| 75 |
bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const override; |
| 76 |
|
| 77 |
sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const override; |
| 78 |
|
| 79 |
void notifyAddedToRasterCache() const override { |
| 80 |
// We explicitly DON'T want to call INHERITED::notifyAddedToRasterCache. That ties the |
| 81 |
// lifetime of derived/cached resources to the image. In this case, we only want cached |
| 82 |
// data (eg mips) tied to the lifetime of the underlying pixelRef. |
| 83 |
SkASSERT(fBitmap.pixelRef()); |
| 84 |
fBitmap.pixelRef()->notifyAddedToCache(); |
| 85 |
} |
| 86 |
|
| 87 |
bool onHasMipmaps() const override { return SkToBool(fMips); } |
| 88 |
bool onIsProtected() const override { return false; } |
| 89 |
|
| 90 |
SkMipmap* onPeekMips() const override { return fMips.get(); } |
| 91 |
|
| 92 |
sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const override; |
| 93 |
|
| 94 |
SkImage_Base::Type type() const override { return SkImage_Base::Type::kRaster; } |
| 95 |
|
| 96 |
SkBitmap bitmap() const { return fBitmap; } |
| 97 |
|
| 98 |
// Convenience method to return a shader that implements the shader+image behavior defined for |
| 99 |
// drawImage/Bitmap where the paint's shader is ignored when the bitmap is a color image, but |
| 100 |
// properly compose them together when it is an alpha image. This allows the returned paint to |
| 101 |
// be assigned to a paint clone without discarding the original behavior. |
| 102 |
sk_sp<SkShader> makeShaderForPaint(const SkPaint& paint, |
| 103 |
SkTileMode tmx, |
| 104 |
SkTileMode tmy, |
| 105 |
const SkSamplingOptions& sampling, |
| 106 |
const SkMatrix* localMatrix); |
| 107 |
|
| 108 |
/** |
| 109 |
* Examines the bitmap to decide if it can share the existing pixelRef, or |
| 110 |
* if it needs to make a deep-copy of the pixels. |
| 111 |
* |
| 112 |
* The bitmap's pixelref will be shared if either the bitmap is marked as |
| 113 |
* immutable, or CopyPixelsMode allows it. |
| 114 |
* |
| 115 |
* If the bitmap's colortype cannot be converted into a corresponding |
| 116 |
* SkImageInfo, or the bitmap's pixels cannot be accessed, this will return |
| 117 |
* nullptr. |
| 118 |
*/ |
| 119 |
static sk_sp<SkImage_Raster> MakeFromBitmap(const SkBitmap&, |
| 120 |
SkCopyPixelsMode, |
| 121 |
sk_sp<SkMipmap> = nullptr); |
| 122 |
|
| 123 |
protected: |
| 124 |
SkImage_Raster(const SkBitmap&, sk_sp<SkMipmap>, bool bitmapMayBeMutable); |
| 125 |
|
| 126 |
private: |
| 127 |
SkBitmap fBitmap; |
| 128 |
sk_sp<SkMipmap> fMips; |
| 129 |
}; |
| 130 |
|
| 131 |
#endif // SkImage_Raster_DEFINED |