Flutter Engine
The Flutter Engine
SkSpecialImage.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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
9
18#include "src/core/SkNextID.h"
21
22// Currently, the raster imagefilters can only handle certain imageinfos. Call this to know if
23// a given info is supported.
25 // no support for other swizzles/depths yet
26 return info.colorType() == kN32_SkColorType;
27}
28
30 uint32_t uniqueID,
31 const SkColorInfo& colorInfo,
32 const SkSurfaceProps& props)
33 : fSubset(subset)
34 , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID)
35 , fColorInfo(colorInfo)
36 , fProps(props) {
37}
38
42 const SkPaint* paint, bool strict) const {
43 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height());
44
45 canvas->drawImageRect(this->asImage(), SkRect::Make(this->subset()), dst,
48}
49
50// TODO(skbug.com/12784): Once bitmap images work with SkImageShader::MakeSubset(), this does not
51// need to be virtual anymore.
54 const SkMatrix& lm,
55 bool strict) const {
56 // The special image's logical (0,0) is at its subset's topLeft() so we need to account for
57 // that in the local matrix used when sampling.
58 SkMatrix subsetOrigin = SkMatrix::Translate(-this->subset().topLeft());
59 subsetOrigin.postConcat(lm);
60
61 if (strict) {
62 // However, we don't need to modify the subset itself since that is defined with respect
63 // to the base image, and the local matrix is applied before any tiling/clamping.
64 const SkRect subset = SkRect::Make(this->subset());
65
66 // asImage() w/o a subset makes no copy; create the SkImageShader directly to remember
67 // the subset used to access the image.
69 this->asImage(), subset, tileMode, tileMode, sampling, &subsetOrigin);
70 } else {
71 // Ignore 'subset' other than its origin translation applied to the local matrix.
72 return this->asImage()->makeShader(tileMode, tileMode, sampling, subsetOrigin);
73 }
74}
75
77public:
79 : SkSpecialImage(subset, bm.getGenerationID(), bm.info().colorInfo(), props)
80 , fBitmap(bm) {
81 SkASSERT(bm.pixelRef());
82 SkASSERT(fBitmap.getPixels());
83 }
84
85 bool getROPixels(SkBitmap* bm) const {
86 return fBitmap.extractSubset(bm, this->subset());
87 }
88
89 SkISize backingStoreDimensions() const override { return fBitmap.dimensions(); }
90
91 size_t getSize() const override { return fBitmap.computeByteSize(); }
92
93 sk_sp<SkImage> asImage() const override { return fBitmap.asImage(); }
94
96 // No need to extract subset, onGetROPixels handles that when needed
97 return SkSpecialImages::MakeFromRaster(subset, fBitmap, this->props());
98 }
99
102 const SkMatrix& lm,
103 bool strict) const override {
104 if (strict) {
105 // TODO(skbug.com/12784): SkImage::makeShader() doesn't support a subset yet, but
106 // SkBitmap supports subset views so create the shader from the subset bitmap instead of
107 // fBitmap.
108 SkBitmap subsetBM;
109 if (!this->getROPixels(&subsetBM)) {
110 return nullptr;
111 }
112 return subsetBM.makeShader(tileMode, tileMode, sampling, lm);
113 } else {
114 // The special image's logical (0,0) is at its subset's topLeft() so we need to
115 // account for that in the local matrix used when sampling.
116 SkMatrix subsetOrigin = SkMatrix::Translate(-this->subset().topLeft());
117 subsetOrigin.postConcat(lm);
118 return fBitmap.makeShader(tileMode, tileMode, sampling, subsetOrigin);
119 }
120 }
121
122private:
123 SkBitmap fBitmap;
124};
125
127
129 const SkBitmap& bm,
130 const SkSurfaceProps& props) {
131 SkASSERT(bm.bounds().contains(subset));
132
133 if (!bm.pixelRef()) {
134 return nullptr;
135 }
136
137 const SkBitmap* srcBM = &bm;
138 SkBitmap tmp;
139 // ImageFilters only handle N32 at the moment, so force our src to be that
140 if (!valid_for_imagefilters(bm.info())) {
141 if (!tmp.tryAllocPixels(bm.info().makeColorType(kN32_SkColorType)) ||
142 !bm.readPixels(tmp.info(), tmp.getPixels(), tmp.rowBytes(), 0, 0))
143 {
144 return nullptr;
145 }
146 srcBM = &tmp;
147 }
148 return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props);
149}
150
152 const SkBitmap& bm,
153 const SkSurfaceProps& props) {
154 SkASSERT(bm.bounds().contains(subset));
155
156 if (!bm.pixelRef()) {
157 return nullptr;
158 }
159
160 SkBitmap tmp;
161 SkImageInfo info = bm.info().makeDimensions(subset.size());
162 // As in MakeFromRaster, must force src to N32 for ImageFilters
163 if (!valid_for_imagefilters(bm.info())) {
164 info = info.makeColorType(kN32_SkColorType);
165 }
166 if (!tmp.tryAllocPixels(info)) {
167 return nullptr;
168 }
169 if (!bm.readPixels(tmp.info(), tmp.getPixels(), tmp.rowBytes(), subset.x(), subset.y())) {
170 return nullptr;
171 }
172
173 // Since we're making a copy of the raster, the resulting special image is the exact size
174 // of the requested subset of the original and no longer needs to be offset by subset's left
175 // and top, since those were relative to the original's buffer.
176 return sk_make_sp<SkSpecialImage_Raster>(
177 SkIRect::MakeWH(subset.width(), subset.height()), tmp, props);
178}
179
182 const SkSurfaceProps& props) {
183 if (!image || subset.isEmpty()) {
184 return nullptr;
185 }
186
187 SkASSERT(image->bounds().contains(subset));
189
190 // This will not work if the image is uploaded to a GPU render target.
191 SkBitmap bm;
192 if (as_IB(image)->getROPixels(nullptr, &bm)) {
193 return MakeFromRaster(subset, bm, props);
194 }
195 return nullptr;
196}
197
199 if (!img || img->isGaneshBacked() || img->isGraphiteBacked()) {
200 return false;
201 }
202 auto rasterImg = static_cast<const SkSpecialImage_Raster*>(img);
203 return rasterImg->getROPixels(result);
204}
205
206} // namespace SkSpecialImages
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkImage_Base * as_IB(SkImage *image)
Definition: SkImage_Base.h:201
static bool valid_for_imagefilters(const SkImageInfo &info)
@ kNeedNewImageUniqueID_SpecialImage
SkTileMode
Definition: SkTileMode.h:13
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
SkPixelRef * pixelRef() const
Definition: SkBitmap.h:720
bool extractSubset(SkBitmap *dst, const SkIRect &subset) const
Definition: SkBitmap.cpp:453
SkISize dimensions() const
Definition: SkBitmap.h:388
size_t computeByteSize() const
Definition: SkBitmap.h:293
size_t rowBytes() const
Definition: SkBitmap.h:238
void * getPixels() const
Definition: SkBitmap.h:283
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
const SkImageInfo & info() const
Definition: SkBitmap.h:139
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition: SkBitmap.cpp:488
SkIRect bounds() const
Definition: SkBitmap.h:382
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:271
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1542
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1543
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
static sk_sp< SkShader > MakeSubset(sk_sp< SkImage >, const SkRect &subset, SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix, bool clampAsIfUnpremul=false)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
virtual bool isTextureBacked() const =0
SkIRect bounds() const
Definition: SkImage.h:303
SkMatrix & postConcat(const SkMatrix &other)
Definition: SkMatrix.cpp:683
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
sk_sp< SkShader > asShader(SkTileMode tileMode, const SkSamplingOptions &sampling, const SkMatrix &lm, bool strict) const override
sk_sp< SkSpecialImage > onMakeBackingStoreSubset(const SkIRect &subset) const override
size_t getSize() const override
sk_sp< SkImage > asImage() const override
SkISize backingStoreDimensions() const override
bool getROPixels(SkBitmap *bm) const
SkSpecialImage_Raster(const SkIRect &subset, const SkBitmap &bm, const SkSurfaceProps &props)
int width() const
void draw(SkCanvas *canvas, SkScalar x, SkScalar y, const SkSamplingOptions &sampling, const SkPaint *paint, bool strict=true) const
virtual sk_sp< SkImage > asImage() const =0
int height() const
virtual bool isGaneshBacked() const
SkSpecialImage(const SkIRect &subset, uint32_t uniqueID, const SkColorInfo &, const SkSurfaceProps &)
const SkIRect & subset() const
const SkSurfaceProps & props() const
const SkColorInfo & colorInfo() const
virtual sk_sp< SkShader > asShader(SkTileMode, const SkSamplingOptions &, const SkMatrix &lm, bool strict=true) const
virtual bool isGraphiteBacked() const
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SkSamplingOptions sampling
Definition: SkRecords.h:337
bool AsBitmap(const SkSpecialImage *img, SkBitmap *result)
sk_sp< SkSpecialImage > MakeFromRaster(const SkIRect &subset, const SkBitmap &bm, const SkSurfaceProps &props)
sk_sp< SkSpecialImage > CopyFromRaster(const SkIRect &subset, const SkBitmap &bm, const SkSurfaceProps &props)
dst
Definition: cp.py:12
Definition: SkRect.h:32
constexpr int32_t x() const
Definition: SkRect.h:141
constexpr int32_t y() const
Definition: SkRect.h:148
constexpr SkISize size() const
Definition: SkRect.h:172
constexpr int32_t height() const
Definition: SkRect.h:165
constexpr int32_t width() const
Definition: SkRect.h:158
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
bool isEmpty() const
Definition: SkRect.h:202
bool contains(int32_t x, int32_t y) const
Definition: SkRect.h:463
Definition: SkSize.h:16
SkImageInfo makeDimensions(SkISize newSize) const
Definition: SkImageInfo.h:454
SkImageInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.h:475
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659