Debian Codegraph @

src/image/SkImage_Raster.cpp

raw
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 #include "src/image/SkImage_Raster.h"
8
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCPURecorder.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPixelRef.h"
18 #include "include/core/SkPixmap.h"
19 #include "include/core/SkPoint.h"
20 #include "include/core/SkRecorder.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTypes.h"
26 #include "src/base/SkRectMemcpy.h"
27 #include "src/core/SkImageInfoPriv.h"
28 #include "src/image/SkImage_Base.h"
29 #include "src/shaders/SkImageShader.h"
30
31 #include <cstddef>
32 #include <cstdint>
33 #include <utility>
34
35 class GrDirectContext;
36 class SkSurfaceProps;
37
38 // fixes skbug.com/40036261
39 static bool is_not_subset(const SkBitmap& bm) {
40 SkASSERT(bm.pixelRef());
41 SkISize dim = SkISize::Make(bm.pixelRef()->width(), bm.pixelRef()->height());
42 SkASSERT(dim != bm.dimensions() || bm.pixelRefOrigin().isZero());
43 return dim == bm.dimensions();
44 }
45
46 static void release_data(void* addr, void* context) {
47 SkData* data = static_cast<SkData*>(context);
48 data->unref();
49 }
50
51 SkImage_Raster::SkImage_Raster(const SkImageInfo& info,
52 sk_sp<SkData> data,
53 size_t rowBytes,
54 sk_sp<SkMipmap> mips,
55 uint32_t id)
56 : SkImage_Base(info, id), fMips(mips) {
57 void* addr = const_cast<void*>(data->data());
58
59 fBitmap.installPixels(info, addr, rowBytes, release_data, data.release());
60 fBitmap.setImmutable();
61 }
62
63 SkImage_Raster::SkImage_Raster(const SkBitmap& bm, sk_sp<SkMipmap> mips, bool bitmapMayBeMutable)
64 : SkImage_Base(bm.info(),
65 is_not_subset(bm) ? bm.getGenerationID() : (uint32_t)kNeedNewImageUniqueID)
66 , fBitmap(bm)
67 , fMips(mips) {
68 SkASSERT(bitmapMayBeMutable || fBitmap.isImmutable());
69 }
70
71 SkImage_Raster::~SkImage_Raster() {}
72
73 bool SkImage_Raster::onReadPixels(GrDirectContext*,
74 const SkImageInfo& dstInfo,
75 void* dstPixels,
76 size_t dstRowBytes,
77 int srcX,
78 int srcY,
79 CachingHint) const {
80 SkBitmap shallowCopy(fBitmap);
81 return shallowCopy.readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY);
82 }
83
84 bool SkImage_Raster::onPeekPixels(SkPixmap* pm) const {
85 return fBitmap.peekPixels(pm);
86 }
87
88 bool SkImage_Raster::getROPixels(GrDirectContext*, SkBitmap* dst, CachingHint) const {
89 *dst = fBitmap;
90 return true;
91 }
92
93 sk_sp<SkSurface> SkImage_Raster::onMakeSurface(SkRecorder* recorder,
94 const SkImageInfo& info) const {
95 if (!recorder) {
96 // TODO(kjlubick) remove this after old SkImage::makeScaled(image info, sampling) API gone
97 recorder = skcpu::Recorder::TODO();
98 }
99 const SkSurfaceProps* props = nullptr;
100 constexpr size_t rowBytes = 0;
101 return recorder->cpuRecorder()->makeBitmapSurface(info, rowBytes, props);
102 }
103
104 static SkBitmap copy_bitmap_subset(const SkBitmap& orig, const SkIRect& subset) {
105 SkImageInfo info = orig.info().makeDimensions(subset.size());
106 SkBitmap bitmap;
107 if (!bitmap.tryAllocPixels(info)) {
108 return {};
109 }
110
111 void* dst = bitmap.getPixels();
112 void* src = orig.getAddr(subset.x(), subset.y());
113 if (!dst || !src) {
114 SkDEBUGFAIL("SkImage_Raster::onMakeSubset with nullptr src or dst");
115 return {};
116 }
117
118 SkRectMemcpy(dst, bitmap.rowBytes(), src, orig.rowBytes(), bitmap.rowBytes(),
119 subset.height());
120
121 bitmap.setImmutable();
122 return bitmap;
123 }
124
125 static sk_sp<SkMipmap> copy_mipmaps(const SkBitmap& src, SkMipmap* srcMips) {
126 if (!srcMips) {
127 return nullptr;
128 }
129
130 sk_sp<SkMipmap> dst;
131 dst.reset(SkMipmap::Build(src.pixmap(),
132 /* factoryProc= */ nullptr,
133 /* computeContents= */ false));
134 if (!dst) {
135 return nullptr;
136 }
137 for (int i = 0; i < dst->countLevels(); ++i) {
138 SkMipmap::Level srcLevel, dstLevel;
139 srcMips->getLevel(i, &srcLevel);
140 dst->getLevel(i, &dstLevel);
141 srcLevel.fPixmap.readPixels(dstLevel.fPixmap);
142 }
143
144 return dst;
145 }
146
147 sk_sp<SkImage> SkImage_Raster::onMakeSubset(SkRecorder*,
148 const SkIRect& subset,
149 RequiredProperties requiredProperties) const {
150 sk_sp<SkImage> img;
151
152 if (requiredProperties.fMipmapped) {
153 bool fullCopy = subset == SkIRect::MakeSize(fBitmap.dimensions());
154
155 sk_sp<SkMipmap> mips = fullCopy ? copy_mipmaps(fBitmap, fMips.get()) : nullptr;
156
157 // SkImage::withMipmaps will always make a copy for us so we can temporarily share
158 // the pixel ref with fBitmap
159 SkBitmap tmpSubset;
160 if (!fBitmap.extractSubset(&tmpSubset, subset)) {
161 return nullptr;
162 }
163
164 sk_sp<SkImage> tmp(
165 new SkImage_Raster(tmpSubset, /*mips=*/nullptr, /* bitmapMayBeMutable= */ true));
166
167 // withMipmaps will auto generate the mipmaps if a nullptr is passed in
168 SkASSERT(!mips || mips->validForRootLevel(tmp->imageInfo()));
169 img = tmp->withMipmaps(std::move(mips));
170 } else {
171 SkBitmap copy = copy_bitmap_subset(fBitmap, subset);
172 if (!copy.isNull()) {
173 img = SkImages::RasterFromBitmap(copy);
174 }
175 }
176
177 return img;
178 }
179
180 sk_sp<SkImage_Raster> SkImage_Raster::MakeFromBitmap(const SkBitmap& bm,
181 SkCopyPixelsMode cpm,
182 sk_sp<SkMipmap> mips) {
183 if (!SkImageInfoIsValid(bm.info()) || bm.rowBytes() < bm.info().minRowBytes()) {
184 return nullptr;
185 }
186
187 if (!bm.getPixels()) {
188 return nullptr;
189 }
190
191 if (SkCopyPixelsMode::kAlways == cpm ||
192 (!bm.isImmutable() && SkCopyPixelsMode::kNever != cpm)) {
193 size_t size = bm.computeByteSize();
194 if (SkImageInfo::ByteSizeOverflowed(size)) {
195 return nullptr;
196 }
197
198 sk_sp<SkData> data(SkData::MakeWithCopy(bm.getPixels(), size));
199
200 return sk_sp<SkImage_Raster>(new SkImage_Raster(
201 bm.info(), std::move(data), bm.rowBytes(), std::move(mips), kNeedNewImageUniqueID));
202 }
203 return sk_sp<SkImage_Raster>(
204 new SkImage_Raster(bm, std::move(mips), SkCopyPixelsMode::kNever == cpm));
205 }
206
207 bool SkImage_Raster::onAsLegacyBitmap(GrDirectContext*, SkBitmap* bitmap) const {
208 // When we're a snapshot from a surface, our bitmap may not be marked immutable
209 // even though logically always we are, but in that case we can't physically share our
210 // pixelref since the caller might call setImmutable() themselves
211 // (thus changing our state).
212 if (fBitmap.isImmutable()) {
213 SkIPoint origin = fBitmap.pixelRefOrigin();
214 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes());
215 bitmap->setPixelRef(sk_ref_sp(fBitmap.pixelRef()), origin.x(), origin.y());
216 return true;
217 }
218 return this->SkImage_Base::onAsLegacyBitmap(nullptr, bitmap);
219 }
220
221 sk_sp<SkShader> SkImage_Raster::makeShaderForPaint(const SkPaint& paint,
222 SkTileMode tmx,
223 SkTileMode tmy,
224 const SkSamplingOptions& sampling,
225 const SkMatrix* localMatrix) {
226 auto s = SkImageShader::Make(sk_ref_sp<SkImage>(this), tmx, tmy, sampling, localMatrix);
227 if (!s) {
228 return nullptr;
229 }
230 if (SkColorTypeIsAlphaOnly(this->colorType()) && paint.getShader()) {
231 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
232 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
233 // the source image and dst shader (MakeBlend takes dst first, src second).
234 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
235 }
236 return s;
237 }
238
239 sk_sp<SkImage> SkImage_Raster::onMakeWithMipmaps(sk_sp<SkMipmap> mips) const {
240 // It's dangerous to have two SkBitmaps that share a SkPixelRef but have different SkMipmaps
241 // since various caches key on SkPixelRef's generation ID. Also, SkPixelRefs that back
242 // SkSurfaces are marked "temporarily immutable" and making an image that uses the same
243 // SkPixelRef can interact badly with SkSurface/SkImage copy-on-write. So we just always
244 // make a copy with a new ID.
245 if (!mips) {
246 mips.reset(SkMipmap::Build(fBitmap.pixmap(), nullptr));
247 }
248 return SkImage_Raster::MakeFromBitmap(fBitmap, SkCopyPixelsMode::kAlways, std::move(mips));
249 }
250
251 sk_sp<SkImage> SkImage_Raster::makeColorTypeAndColorSpace(SkRecorder*,
252 SkColorType targetColorType,
253 sk_sp<SkColorSpace> targetColorSpace,
254 RequiredProperties) const {
255 SkPixmap src;
256 SkAssertResult(fBitmap.peekPixels(&src));
257
258 SkBitmap dst;
259 if (!dst.tryAllocPixels(
260 fBitmap.info().makeColorType(targetColorType).makeColorSpace(targetColorSpace))) {
261 return nullptr;
262 }
263
264 SkAssertResult(dst.writePixels(src));
265 dst.setImmutable();
266 return SkImages::RasterFromBitmap(dst);
267 }
268
269 sk_sp<SkImage> SkImage_Raster::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
270 // TODO: If our bitmap is immutable, then we could theoretically create another image sharing
271 // our pixelRef. That doesn't work (without more invasive logic), because the image gets its
272 // gen ID from the bitmap, which gets it from the pixelRef.
273 SkPixmap pixmap = fBitmap.pixmap();
274 pixmap.setColorSpace(std::move(newCS));
275 return SkImages::RasterFromPixmapCopy(pixmap);
276 }