Debian Codegraph @

src/shaders/SkImageShader.cpp

raw
1 /*
2 * Copyright 2015 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 #include "src/shaders/SkImageShader.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkBlendMode.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkTileMode.h"
20 #include "include/private/base/SkMath.h"
21 #include "modules/skcms/skcms.h"
22 #include "src/base/SkArenaAlloc.h"
23 #include "src/core/SkBitmapProcState.h"
24 #include "src/core/SkColorSpaceXformSteps.h"
25 #include "src/core/SkEffectPriv.h"
26 #include "src/core/SkImageInfoPriv.h"
27 #include "src/core/SkMipmapAccessor.h"
28 #include "src/core/SkPicturePriv.h"
29 #include "src/core/SkRasterPipeline.h"
30 #include "src/core/SkRasterPipelineOpContexts.h"
31 #include "src/core/SkRasterPipelineOpList.h"
32 #include "src/core/SkReadBuffer.h"
33 #include "src/core/SkSamplingPriv.h"
34 #include "src/core/SkWriteBuffer.h"
35 #include "src/image/SkImage_Base.h"
36
37 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
38 #include "src/shaders/SkBitmapProcShader.h"
39 #endif
40
41 #include <optional>
42 #include <tuple>
43 #include <utility>
44
45 class SkColorSpace;
46
47 SkM44 SkImageShader::CubicResamplerMatrix(float B, float C) {
48 #if 0
49 constexpr SkM44 kMitchell = SkM44( 1.f/18.f, -9.f/18.f, 15.f/18.f, -7.f/18.f,
50 16.f/18.f, 0.f/18.f, -36.f/18.f, 21.f/18.f,
51 1.f/18.f, 9.f/18.f, 27.f/18.f, -21.f/18.f,
52 0.f/18.f, 0.f/18.f, -6.f/18.f, 7.f/18.f);
53
54 constexpr SkM44 kCatmull = SkM44(0.0f, -0.5f, 1.0f, -0.5f,
55 1.0f, 0.0f, -2.5f, 1.5f,
56 0.0f, 0.5f, 2.0f, -1.5f,
57 0.0f, 0.0f, -0.5f, 0.5f);
58
59 if (B == 1.0f/3 && C == 1.0f/3) {
60 return kMitchell;
61 }
62 if (B == 0 && C == 0.5f) {
63 return kCatmull;
64 }
65 #endif
66 return SkM44( (1.f/6)*B, -(3.f/6)*B - C, (3.f/6)*B + 2*C, - (1.f/6)*B - C,
67 1 - (2.f/6)*B, 0, -3 + (12.f/6)*B + C, 2 - (9.f/6)*B - C,
68 (1.f/6)*B, (3.f/6)*B + C, 3 - (15.f/6)*B - 2*C, -2 + (9.f/6)*B + C,
69 0, 0, -C, (1.f/6)*B + C);
70 }
71
72 /**
73 * We are faster in clamp, so always use that tiling when we can.
74 */
75 static SkTileMode optimize(SkTileMode tm, int dimension) {
76 SkASSERT(dimension > 0);
77 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
78 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
79 // for transforming to clamp.
80 return tm;
81 #else
82 // mirror and repeat on a 1px axis are the same as clamping, but decal will still transition to
83 // transparent black.
84 return (tm != SkTileMode::kDecal && dimension == 1) ? SkTileMode::kClamp : tm;
85 #endif
86 }
87
88 #if defined(SK_DEBUG)
89 static bool needs_subset(SkImage* img, const SkRect& subset) {
90 return subset != SkRect::Make(img->dimensions());
91 }
92 #endif
93
94 SkImageShader::SkImageShader(sk_sp<SkImage> img,
95 const SkRect& subset,
96 SkTileMode tmx, SkTileMode tmy,
97 const SkSamplingOptions& sampling,
98 bool raw,
99 bool clampAsIfUnpremul)
100 : fImage(std::move(img))
101 , fSampling(sampling)
102 , fTileModeX(optimize(tmx, fImage->width()))
103 , fTileModeY(optimize(tmy, fImage->height()))
104 , fSubset(subset)
105 , fRaw(raw)
106 , fClampAsIfUnpremul(clampAsIfUnpremul) {
107 // These options should never appear together:
108 SkASSERT(!fRaw || !fClampAsIfUnpremul);
109
110 // Bicubic filtering of raw image shaders would add a surprising clamp - so we don't support it
111 SkASSERT(!fRaw || !fSampling.useCubic);
112 }
113
114 // just used for legacy-unflattening
115 enum class LegacyFilterEnum {
116 kNone,
117 kLow,
118 kMedium,
119 kHigh,
120 // this is the special value for backward compatibility
121 kInheritFromPaint,
122 // this signals we should use the new SkFilterOptions
123 kUseFilterOptions,
124 // use cubic and ignore FilterOptions
125 kUseCubicResampler,
126
127 kLast = kUseCubicResampler,
128 };
129
130 // fClampAsIfUnpremul is always false when constructed through public APIs,
131 // so there's no need to read or write it here.
132
133 sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
134 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
135 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
136
137 SkSamplingOptions sampling;
138 bool readSampling = true;
139 if (buffer.isVersionLT(SkPicturePriv::kNoFilterQualityShaders_Version) &&
140 !buffer.readBool() /* legacy has_sampling */)
141 {
142 readSampling = false;
143 // we just default to Nearest in sampling
144 }
145 if (readSampling) {
146 sampling = buffer.readSampling();
147 }
148
149 SkMatrix localMatrix;
150 if (buffer.isVersionLT(SkPicturePriv::Version::kNoShaderLocalMatrix)) {
151 buffer.readMatrix(&localMatrix);
152 }
153 sk_sp<SkImage> img = buffer.readImage();
154 if (!img) {
155 return nullptr;
156 }
157
158 bool raw = buffer.isVersionLT(SkPicturePriv::Version::kRawImageShaders) ? false
159 : buffer.readBool();
160
161 // TODO(skbug.com/40043877): Subset is not serialized yet; it's only used by special images so it
162 // will never be written to an SKP.
163
164 return raw ? SkImageShader::MakeRaw(std::move(img), tmx, tmy, sampling, &localMatrix)
165 : SkImageShader::Make(std::move(img), tmx, tmy, sampling, &localMatrix);
166 }
167
168 void SkImageShader::flatten(SkWriteBuffer& buffer) const {
169 buffer.writeUInt((unsigned)fTileModeX);
170 buffer.writeUInt((unsigned)fTileModeY);
171
172 buffer.writeSampling(fSampling);
173
174 buffer.writeImage(fImage.get());
175 SkASSERT(fClampAsIfUnpremul == false);
176
177 // TODO(skbug.com/40043877): Subset is not serialized yet; it's only used by special images so it
178 // will never be written to an SKP.
179 SkASSERT(!needs_subset(fImage.get(), fSubset));
180
181 buffer.writeBool(fRaw);
182 }
183
184 bool SkImageShader::isOpaque() const {
185 return fImage->isOpaque() &&
186 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
187 }
188
189 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
190
191 static bool legacy_shader_can_handle(const SkMatrix& inv) {
192 SkASSERT(!inv.hasPerspective());
193
194 // Scale+translate methods are always present, but affine might not be.
195 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
196 return false;
197 }
198
199 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
200 // out of range.
201 const SkScalar max_dev_coord = 32767.0f;
202 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
203
204 // take 1/4 of max signed 32bits so we have room to subtract local values
205 const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f;
206 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
207 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
208 return false;
209 }
210
211 // legacy shader impl should be able to handle these matrices
212 return true;
213 }
214
215 SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
216 SkArenaAlloc* alloc) const {
217 SkASSERT(!needs_subset(fImage.get(), fSubset)); // TODO(skbug.com/40043877)
218 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
219 return nullptr;
220 }
221 if (fImage->colorType() != kN32_SkColorType) {
222 return nullptr;
223 }
224 if (fTileModeX != fTileModeY) {
225 return nullptr;
226 }
227 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
228 return nullptr;
229 }
230
231 SkSamplingOptions sampling = fSampling;
232 if (sampling.isAniso()) {
233 sampling = SkSamplingPriv::AnisoFallback(fImage->hasMipmaps());
234 }
235
236 auto supported = [](const SkSamplingOptions& sampling) {
237 const std::tuple<SkFilterMode,SkMipmapMode> supported[] = {
238 {SkFilterMode::kNearest, SkMipmapMode::kNone}, // legacy None
239 {SkFilterMode::kLinear, SkMipmapMode::kNone}, // legacy Low
240 {SkFilterMode::kLinear, SkMipmapMode::kNearest}, // legacy Medium
241 };
242 for (auto [f, m] : supported) {
243 if (sampling.filter == f && sampling.mipmap == m) {
244 return true;
245 }
246 }
247 return false;
248 };
249 if (sampling.useCubic || !supported(sampling)) {
250 return nullptr;
251 }
252
253 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
254 // so it can't handle bitmaps larger than 65535.
255 //
256 // We back off another bit to 32767 to make small amounts of
257 // intermediate math safe, e.g. in
258 //
259 // SkFixed fx = ...;
260 // fx = tile(fx + SK_Fixed1);
261 //
262 // we want to make sure (fx + SK_Fixed1) never overflows.
263 if (fImage-> width() > 32767 ||
264 fImage->height() > 32767) {
265 return nullptr;
266 }
267
268 auto inv = rec.fMatrixRec.totalInverse();
269 if (!inv || !legacy_shader_can_handle(*inv)) {
270 return nullptr;
271 }
272
273 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
274 return nullptr;
275 }
276
277 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, sampling,
278 as_IB(fImage.get()), rec, alloc);
279 }
280 #endif
281
282 SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
283 if (texM) {
284 *texM = SkMatrix::I();
285 }
286 if (xy) {
287 xy[0] = fTileModeX;
288 xy[1] = fTileModeY;
289 }
290 return const_cast<SkImage*>(fImage.get());
291 }
292
293 sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
294 SkTileMode tmx, SkTileMode tmy,
295 const SkSamplingOptions& options,
296 const SkMatrix* localMatrix,
297 bool clampAsIfUnpremul) {
298 SkRect subset = image ? SkRect::Make(image->dimensions()) : SkRect::MakeEmpty();
299 return MakeSubset(std::move(image), subset, tmx, tmy, options, localMatrix, clampAsIfUnpremul);
300 }
301
302 sk_sp<SkShader> SkImageShader::MakeRaw(sk_sp<SkImage> image,
303 SkTileMode tmx, SkTileMode tmy,
304 const SkSamplingOptions& options,
305 const SkMatrix* localMatrix) {
306 if (options.useCubic) {
307 return nullptr;
308 }
309 if (!image) {
310 return SkShaders::Empty();
311 }
312 auto subset = SkRect::Make(image->dimensions());
313
314 sk_sp<SkShader> s = sk_make_sp<SkImageShader>(image,
315 subset,
316 tmx, tmy,
317 options,
318 /*raw=*/true,
319 /*clampAsIfUnpremul=*/false);
320 return s->makeWithLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I());
321 }
322
323 sk_sp<SkShader> SkImageShader::MakeSubset(sk_sp<SkImage> image,
324 const SkRect& subset,
325 SkTileMode tmx, SkTileMode tmy,
326 const SkSamplingOptions& options,
327 const SkMatrix* localMatrix,
328 bool clampAsIfUnpremul) {
329 auto is_unit = [](float x) {
330 return x >= 0 && x <= 1;
331 };
332 if (options.useCubic) {
333 if (!is_unit(options.cubic.B) || !is_unit(options.cubic.C)) {
334 return nullptr;
335 }
336 }
337 if (!image || subset.isEmpty()) {
338 return SkShaders::Empty();
339 }
340
341 // Validate subset and check if we can drop it
342 if (!SkRect::Make(image->bounds()).contains(subset)) {
343 return nullptr;
344 }
345
346 sk_sp<SkShader> s = sk_make_sp<SkImageShader>(std::move(image),
347 subset,
348 tmx, tmy,
349 options,
350 /*raw=*/false,
351 clampAsIfUnpremul);
352 return s->makeWithLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I());
353 }
354
355 ///////////////////////////////////////////////////////////////////////////////////////////////////
356
357 std::pair<SkRect, sk_sp<SkShader>> SkImageShader::MakeForDrawRect(const SkImage* image,
358 const SkPaint& paint,
359 const SkSamplingOptions& sampling,
360 SkRect src,
361 SkRect dst,
362 bool strictSrcSubset) {
363 SkASSERT(image);
364 // The paint should have already been cleaned for a regular drawImageRect, e.g. no path
365 // effect and is a fill.
366 SkASSERT(paint.getStyle() == SkPaint::kFill_Style && !paint.getPathEffect());
367
368 SkRect imgBounds = SkRect::Make(image->bounds());
369
370 SkASSERT(src.isFinite() && dst.isFinite() && dst.isSorted());
371 SkMatrix localMatrix = SkMatrix::RectToRectOrIdentity(src, dst);
372 if (!imgBounds.contains(src)) {
373 if (!src.intersect(imgBounds)) {
374 return {SkRect::MakeEmpty(), nullptr}; // Nothing to draw for this entry
375 }
376 // Update dst to match smaller src
377 dst = localMatrix.mapRect(src);
378 }
379
380 bool imageIsAlphaOnly = SkColorTypeIsAlphaOnly(image->colorType());
381
382 sk_sp<SkShader> imgShader;
383 if (strictSrcSubset) {
384 imgShader = SkImageShader::MakeSubset(sk_ref_sp(image), src,
385 SkTileMode::kClamp, SkTileMode::kClamp,
386 sampling, &localMatrix);
387 } else {
388 imgShader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
389 sampling, &localMatrix);
390 }
391 if (!imgShader) {
392 return {SkRect::MakeEmpty(), nullptr};
393 }
394 if (imageIsAlphaOnly && paint.getShader()) {
395 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
396 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
397 // the source image and dst shader (MakeBlend takes dst first, src second).
398 imgShader = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(imgShader));
399 }
400 return {dst, std::move(imgShader)};
401 }
402
403 void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
404
405 namespace {
406
407 struct MipLevelHelper {
408 SkPixmap pm;
409 SkMatrix inv;
410 SkRasterPipelineContexts::GatherCtx* gather;
411 SkRasterPipelineContexts::TileCtx* limitX;
412 SkRasterPipelineContexts::TileCtx* limitY;
413 SkRasterPipelineContexts::DecalTileCtx* decalCtx = nullptr;
414
415 void allocAndInit(SkArenaAlloc* alloc,
416 const SkSamplingOptions& sampling,
417 SkTileMode tileModeX,
418 SkTileMode tileModeY) {
419 gather = alloc->make<SkRasterPipelineContexts::GatherCtx>();
420 gather->pixels = pm.addr();
421 gather->stride = pm.rowBytesAsPixels();
422 gather->width = pm.width();
423 gather->height = pm.height();
424
425 if (sampling.useCubic) {
426 SkImageShader::CubicResamplerMatrix(sampling.cubic.B, sampling.cubic.C)
427 .getColMajor(gather->weights);
428 }
429
430 limitX = alloc->make<SkRasterPipelineContexts::TileCtx>();
431 limitY = alloc->make<SkRasterPipelineContexts::TileCtx>();
432 limitX->scale = pm.width();
433 limitX->invScale = 1.0f / pm.width();
434 limitY->scale = pm.height();
435 limitY->invScale = 1.0f / pm.height();
436
437 // We would like an image that is mapped 1:1 with device pixels but at a half pixel offset
438 // to select every pixel from the src image once. Our rasterizer biases upward. That is a
439 // rect from 0.5...1.5 fills pixel 1 and not pixel 0. So we make exact integer pixel sample
440 // values select the pixel to the left/above the integer value.
441 //
442 // Note that a mirror mapping between canvas and image space will not have this property -
443 // on one side of the image a row/column will be skipped and one repeated on the other side.
444 //
445 // The GM nearest_half_pixel_image tests both of the above scenarios.
446 //
447 // The implementation of SkTileMode::kMirror also modifies integer pixel snapping to create
448 // consistency when the sample coords are running backwards and must account for gather
449 // modification we perform here. The GM mirror_tile tests this.
450 if (!sampling.useCubic && sampling.filter == SkFilterMode::kNearest) {
451 gather->roundDownAtInteger = true;
452 limitX->mirrorBiasDir = limitY->mirrorBiasDir = 1;
453 }
454
455 if (tileModeX == SkTileMode::kDecal || tileModeY == SkTileMode::kDecal) {
456 decalCtx = alloc->make<SkRasterPipelineContexts::DecalTileCtx>();
457 decalCtx->limit_x = limitX->scale;
458 decalCtx->limit_y = limitY->scale;
459
460 // When integer sample coords snap left/up then we want the right/bottom edge of the
461 // image bounds to be inside the image rather than the left/top edge, that is (0, w]
462 // rather than [0, w).
463 if (gather->roundDownAtInteger) {
464 decalCtx->inclusiveEdge_x = decalCtx->limit_x;
465 decalCtx->inclusiveEdge_y = decalCtx->limit_y;
466 }
467 }
468 }
469 };
470
471 } // namespace
472
473 static SkSamplingOptions tweak_sampling(SkSamplingOptions sampling, const SkMatrix& matrix) {
474 SkFilterMode filter = sampling.filter;
475
476 // When the matrix is just an integer translate, bilerp == nearest neighbor.
477 if (filter == SkFilterMode::kLinear &&
478 matrix.getType() <= SkMatrix::kTranslate_Mask &&
479 matrix.getTranslateX() == (int)matrix.getTranslateX() &&
480 matrix.getTranslateY() == (int)matrix.getTranslateY()) {
481 filter = SkFilterMode::kNearest;
482 }
483
484 return SkSamplingOptions(filter, sampling.mipmap);
485 }
486
487 bool SkImageShader::appendStages(const SkStageRec& rec, const SkShaders::MatrixRec& mRec) const {
488 SkASSERT(!needs_subset(fImage.get(), fSubset)); // TODO(skbug.com/40043877)
489
490 // We only support certain sampling options in stages so far
491 auto sampling = fSampling;
492 if (sampling.isAniso()) {
493 sampling = SkSamplingPriv::AnisoFallback(fImage->hasMipmaps());
494 }
495
496 SkRasterPipeline* p = rec.fPipeline;
497 SkArenaAlloc* alloc = rec.fAlloc;
498
499 SkMatrix baseInv;
500 // If the total matrix isn't valid then we will always access the base MIP level.
501 if (mRec.totalMatrixIsValid()) {
502 auto inv = mRec.totalInverse();
503 if (!inv) {
504 return false;
505 }
506 baseInv = *inv;
507 baseInv.normalizePerspective();
508 }
509
510 SkASSERT(!sampling.useCubic || sampling.mipmap == SkMipmapMode::kNone);
511 auto* access = SkMipmapAccessor::Make(alloc, fImage.get(), baseInv, sampling.mipmap);
512 if (!access) {
513 return false;
514 }
515
516 MipLevelHelper upper;
517 std::tie(upper.pm, upper.inv) = access->level();
518
519 if (!sampling.useCubic) {
520 // TODO: can tweak_sampling sometimes for cubic too when B=0
521 if (mRec.totalMatrixIsValid()) {
522 sampling = tweak_sampling(sampling, SkMatrix::Concat(upper.inv, baseInv));
523 }
524 }
525
526 if (!mRec.apply(rec, upper.inv)) {
527 return false;
528 }
529
530 upper.allocAndInit(alloc, sampling, fTileModeX, fTileModeY);
531
532 MipLevelHelper lower;
533 SkRasterPipelineContexts::MipmapCtx* mipmapCtx = nullptr;
534 float lowerWeight = access->lowerWeight();
535 if (lowerWeight > 0) {
536 std::tie(lower.pm, lower.inv) = access->lowerLevel();
537 mipmapCtx = alloc->make<SkRasterPipelineContexts::MipmapCtx>();
538 mipmapCtx->lowerWeight = lowerWeight;
539 mipmapCtx->scaleX = static_cast<float>(lower.pm.width()) / upper.pm.width();
540 mipmapCtx->scaleY = static_cast<float>(lower.pm.height()) / upper.pm.height();
541
542 lower.allocAndInit(alloc, sampling, fTileModeX, fTileModeY);
543
544 p->append(SkRasterPipelineOp::mipmap_linear_init, mipmapCtx);
545 }
546
547 const bool decalBothAxes = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
548
549 auto append_tiling_and_gather = [&](const MipLevelHelper* level) {
550 if (decalBothAxes) {
551 p->append(SkRasterPipelineOp::decal_x_and_y, level->decalCtx);
552 } else {
553 switch (fTileModeX) {
554 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */
555 break;
556 case SkTileMode::kMirror:
557 p->append(SkRasterPipelineOp::mirror_x, level->limitX);
558 break;
559 case SkTileMode::kRepeat:
560 p->append(SkRasterPipelineOp::repeat_x, level->limitX);
561 break;
562 case SkTileMode::kDecal:
563 p->append(SkRasterPipelineOp::decal_x, level->decalCtx);
564 break;
565 }
566 switch (fTileModeY) {
567 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */
568 break;
569 case SkTileMode::kMirror:
570 p->append(SkRasterPipelineOp::mirror_y, level->limitY);
571 break;
572 case SkTileMode::kRepeat:
573 p->append(SkRasterPipelineOp::repeat_y, level->limitY);
574 break;
575 case SkTileMode::kDecal:
576 p->append(SkRasterPipelineOp::decal_y, level->decalCtx);
577 break;
578 }
579 }
580
581 void* ctx = level->gather;
582 switch (level->pm.colorType()) {
583 case kAlpha_8_SkColorType: p->append(SkRasterPipelineOp::gather_a8, ctx); break;
584 case kA16_unorm_SkColorType: p->append(SkRasterPipelineOp::gather_a16, ctx); break;
585 case kA16_float_SkColorType: p->append(SkRasterPipelineOp::gather_af16, ctx); break;
586 case kRGB_565_SkColorType: p->append(SkRasterPipelineOp::gather_565, ctx); break;
587 case kARGB_4444_SkColorType: p->append(SkRasterPipelineOp::gather_4444, ctx); break;
588 case kR8G8_unorm_SkColorType: p->append(SkRasterPipelineOp::gather_rg88, ctx); break;
589 case kR16_unorm_SkColorType: p->append(SkRasterPipelineOp::gather_r16, ctx); break;
590 case kR16G16_unorm_SkColorType: p->append(SkRasterPipelineOp::gather_rg1616,ctx); break;
591 case kR16G16_float_SkColorType: p->append(SkRasterPipelineOp::gather_rgf16, ctx); break;
592 case kRGBA_8888_SkColorType: p->append(SkRasterPipelineOp::gather_8888, ctx); break;
593
594 case kRGBA_1010102_SkColorType:
595 p->append(SkRasterPipelineOp::gather_1010102, ctx);
596 break;
597
598 case kR16G16B16A16_unorm_SkColorType:
599 p->append(SkRasterPipelineOp::gather_16161616, ctx);
600 break;
601
602 case kRGBA_F16Norm_SkColorType:
603 case kRGBA_F16_SkColorType: p->append(SkRasterPipelineOp::gather_f16, ctx); break;
604 case kRGBA_F32_SkColorType: p->append(SkRasterPipelineOp::gather_f32, ctx); break;
605 case kBGRA_10101010_XR_SkColorType:
606 p->append(SkRasterPipelineOp::gather_10101010_xr, ctx);
607 p->append(SkRasterPipelineOp::swap_rb);
608 break;
609 case kRGBA_10x6_SkColorType: p->append(SkRasterPipelineOp::gather_10x6, ctx); break;
610
611 case kGray_8_SkColorType: p->append(SkRasterPipelineOp::gather_a8, ctx);
612 p->append(SkRasterPipelineOp::alpha_to_gray ); break;
613
614 case kR8_unorm_SkColorType: p->append(SkRasterPipelineOp::gather_a8, ctx);
615 p->append(SkRasterPipelineOp::alpha_to_red ); break;
616
617 case kRGB_888x_SkColorType: p->append(SkRasterPipelineOp::gather_8888, ctx);
618 p->append(SkRasterPipelineOp::force_opaque ); break;
619 case kRGB_F16F16F16x_SkColorType:
620 p->append(SkRasterPipelineOp::gather_f16, ctx);
621 p->append(SkRasterPipelineOp::force_opaque);
622 break;
623 case kBGRA_1010102_SkColorType:
624 p->append(SkRasterPipelineOp::gather_1010102, ctx);
625 p->append(SkRasterPipelineOp::swap_rb);
626 break;
627
628 case kRGB_101010x_SkColorType:
629 p->append(SkRasterPipelineOp::gather_1010102, ctx);
630 p->append(SkRasterPipelineOp::force_opaque);
631 break;
632
633 case kBGR_101010x_XR_SkColorType:
634 p->append(SkRasterPipelineOp::gather_1010102_xr, ctx);
635 p->append(SkRasterPipelineOp::force_opaque);
636 p->append(SkRasterPipelineOp::swap_rb);
637 break;
638
639 case kBGR_101010x_SkColorType:
640 p->append(SkRasterPipelineOp::gather_1010102, ctx);
641 p->append(SkRasterPipelineOp::force_opaque);
642 p->append(SkRasterPipelineOp::swap_rb);
643 break;
644
645 case kBGRA_8888_SkColorType:
646 p->append(SkRasterPipelineOp::gather_8888, ctx);
647 p->append(SkRasterPipelineOp::swap_rb);
648 break;
649
650 case kSRGBA_8888_SkColorType:
651 p->append(SkRasterPipelineOp::gather_8888, ctx);
652 p->appendTransferFunction(*skcms_sRGB_TransferFunction());
653 break;
654
655 case kUnknown_SkColorType: SkASSERT(false);
656 }
657 if (level->decalCtx) {
658 p->append(SkRasterPipelineOp::check_decal_mask, level->decalCtx);
659 }
660 };
661
662 auto append_misc = [&] {
663 SkColorSpace* cs = upper.pm.colorSpace();
664 SkAlphaType at = upper.pm.alphaType();
665
666 // Color for alpha-only images comes from the paint (already converted to dst color space).
667 // If we were sampled by a runtime effect, the paint color was replaced with transparent
668 // black, so this tinting is effectively suppressed. See also: RuntimeEffectRPCallbacks
669 if (SkColorTypeIsAlphaOnly(upper.pm.colorType()) && !fRaw) {
670 p->appendSetRGB(alloc, rec.fPaintColor);
671
672 cs = rec.fDstCS;
673 at = kUnpremul_SkAlphaType;
674 }
675
676 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
677 if (sampling.useCubic) {
678 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
679 ? SkRasterPipelineOp::clamp_01
680 : SkRasterPipelineOp::clamp_gamut);
681 }
682
683 // Transform color space and alpha type to match shader convention (dst CS, premul alpha).
684 if (!fRaw) {
685 alloc->make<SkColorSpaceXformSteps>(cs, at, rec.fDstCS, kPremul_SkAlphaType)->apply(p);
686 }
687
688 return true;
689 };
690
691 // Check for fast-path stages.
692 // TODO: Could we use the fast-path stages for each level when doing linear mipmap filtering?
693 SkColorType ct = upper.pm.colorType();
694 if (true
695 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
696 && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear
697 && sampling.mipmap != SkMipmapMode::kLinear
698 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
699 // Check bounding box of points we will sample to see if we can use lowp
700 // and not over/under flow.
701 bool shouldUseHighPBilerp = false;
702 if (!rec.fDstBounds.isEmpty()) {
703 std::array<SkPoint, 4> quad = rec.fDstBounds.toQuad();
704 baseInv.mapPoints(quad);
705 SkRect deviceImageSpace;
706 deviceImageSpace.setBounds(quad);
707 for (float val : SkSpan<const float>(deviceImageSpace.asScalars(), 4)) {
708 if (val > INT16_MAX || val < INT16_MIN || !std::isfinite(val)) {
709 shouldUseHighPBilerp = true;
710 break;
711 }
712 }
713 }
714
715 if (shouldUseHighPBilerp) {
716 p->append(SkRasterPipelineOp::bilerp_clamp_8888_force_highp, upper.gather);
717 } else {
718 p->append(SkRasterPipelineOp::bilerp_clamp_8888, upper.gather);
719 }
720
721 if (ct == kBGRA_8888_SkColorType) {
722 p->append(SkRasterPipelineOp::swap_rb);
723 }
724 return append_misc();
725 }
726 if (true
727 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
728 && sampling.useCubic
729 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
730
731 p->append(SkRasterPipelineOp::bicubic_clamp_8888, upper.gather);
732 if (ct == kBGRA_8888_SkColorType) {
733 p->append(SkRasterPipelineOp::swap_rb);
734 }
735 return append_misc();
736 }
737
738 // This context can be shared by both levels when doing linear mipmap filtering
739 SkRasterPipelineContexts::SamplerCtx* sampler =
740 alloc->make<SkRasterPipelineContexts::SamplerCtx>();
741
742 auto sample = [&](SkRasterPipelineOp setup_x,
743 SkRasterPipelineOp setup_y,
744 const MipLevelHelper* level) {
745 p->append(setup_x, sampler);
746 p->append(setup_y, sampler);
747 append_tiling_and_gather(level);
748 p->append(SkRasterPipelineOp::accumulate, sampler);
749 };
750
751 auto sample_level = [&](const MipLevelHelper* level) {
752 if (sampling.useCubic) {
753 CubicResamplerMatrix(sampling.cubic.B, sampling.cubic.C).getColMajor(sampler->weights);
754
755 p->append(SkRasterPipelineOp::bicubic_setup, sampler);
756
757 sample(SkRasterPipelineOp::bicubic_n3x, SkRasterPipelineOp::bicubic_n3y, level);
758 sample(SkRasterPipelineOp::bicubic_n1x, SkRasterPipelineOp::bicubic_n3y, level);
759 sample(SkRasterPipelineOp::bicubic_p1x, SkRasterPipelineOp::bicubic_n3y, level);
760 sample(SkRasterPipelineOp::bicubic_p3x, SkRasterPipelineOp::bicubic_n3y, level);
761
762 sample(SkRasterPipelineOp::bicubic_n3x, SkRasterPipelineOp::bicubic_n1y, level);
763 sample(SkRasterPipelineOp::bicubic_n1x, SkRasterPipelineOp::bicubic_n1y, level);
764 sample(SkRasterPipelineOp::bicubic_p1x, SkRasterPipelineOp::bicubic_n1y, level);
765 sample(SkRasterPipelineOp::bicubic_p3x, SkRasterPipelineOp::bicubic_n1y, level);
766
767 sample(SkRasterPipelineOp::bicubic_n3x, SkRasterPipelineOp::bicubic_p1y, level);
768 sample(SkRasterPipelineOp::bicubic_n1x, SkRasterPipelineOp::bicubic_p1y, level);
769 sample(SkRasterPipelineOp::bicubic_p1x, SkRasterPipelineOp::bicubic_p1y, level);
770 sample(SkRasterPipelineOp::bicubic_p3x, SkRasterPipelineOp::bicubic_p1y, level);
771
772 sample(SkRasterPipelineOp::bicubic_n3x, SkRasterPipelineOp::bicubic_p3y, level);
773 sample(SkRasterPipelineOp::bicubic_n1x, SkRasterPipelineOp::bicubic_p3y, level);
774 sample(SkRasterPipelineOp::bicubic_p1x, SkRasterPipelineOp::bicubic_p3y, level);
775 sample(SkRasterPipelineOp::bicubic_p3x, SkRasterPipelineOp::bicubic_p3y, level);
776
777 p->append(SkRasterPipelineOp::move_dst_src);
778 } else if (sampling.filter == SkFilterMode::kLinear) {
779 p->append(SkRasterPipelineOp::bilinear_setup, sampler);
780
781 sample(SkRasterPipelineOp::bilinear_nx, SkRasterPipelineOp::bilinear_ny, level);
782 sample(SkRasterPipelineOp::bilinear_px, SkRasterPipelineOp::bilinear_ny, level);
783 sample(SkRasterPipelineOp::bilinear_nx, SkRasterPipelineOp::bilinear_py, level);
784 sample(SkRasterPipelineOp::bilinear_px, SkRasterPipelineOp::bilinear_py, level);
785
786 p->append(SkRasterPipelineOp::move_dst_src);
787 } else {
788 append_tiling_and_gather(level);
789 }
790 };
791
792 sample_level(&upper);
793
794 if (mipmapCtx) {
795 p->append(SkRasterPipelineOp::mipmap_linear_update, mipmapCtx);
796 sample_level(&lower);
797 p->append(SkRasterPipelineOp::mipmap_linear_finish, mipmapCtx);
798 }
799
800 return append_misc();
801 }
802
803 namespace SkShaders {
804
805 sk_sp<SkShader> Image(sk_sp<SkImage> image,
806 SkTileMode tmx, SkTileMode tmy,
807 const SkSamplingOptions& options,
808 const SkMatrix* localMatrix) {
809 return SkImageShader::Make(std::move(image), tmx, tmy, options, localMatrix);
810 }
811
812 sk_sp<SkShader> RawImage(sk_sp<SkImage> image,
813 SkTileMode tmx, SkTileMode tmy,
814 const SkSamplingOptions& options,
815 const SkMatrix* localMatrix) {
816 return SkImageShader::MakeRaw(std::move(image), tmx, tmy, options, localMatrix);
817 }
818
819 } // namespace SkShaders