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/core/SkArenaAlloc.h" |
9 | #include "src/core/SkBitmapController.h" |
10 | #include "src/core/SkColorSpacePriv.h" |
11 | #include "src/core/SkColorSpaceXformSteps.h" |
12 | #include "src/core/SkOpts.h" |
13 | #include "src/core/SkRasterPipeline.h" |
14 | #include "src/core/SkReadBuffer.h" |
15 | #include "src/core/SkVM.h" |
16 | #include "src/core/SkWriteBuffer.h" |
17 | #include "src/image/SkImage_Base.h" |
18 | #include "src/shaders/SkBitmapProcShader.h" |
19 | #include "src/shaders/SkEmptyShader.h" |
20 | #include "src/shaders/SkImageShader.h" |
21 | |
22 | /** |
23 | * We are faster in clamp, so always use that tiling when we can. |
24 | */ |
25 | static SkTileMode optimize(SkTileMode tm, int dimension) { |
26 | SkASSERT(dimension > 0); |
27 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
28 | // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow |
29 | // for transforming to clamp. |
30 | return tm; |
31 | #else |
32 | return dimension == 1 ? SkTileMode::kClamp : tm; |
33 | #endif |
34 | } |
35 | |
36 | SkImageShader::SkImageShader(sk_sp<SkImage> img, |
37 | SkTileMode tmx, SkTileMode tmy, |
38 | const SkMatrix* localMatrix, |
39 | bool clampAsIfUnpremul) |
40 | : INHERITED(localMatrix) |
41 | , fImage(std::move(img)) |
42 | , fTileModeX(optimize(tmx, fImage->width())) |
43 | , fTileModeY(optimize(tmy, fImage->height())) |
44 | , fClampAsIfUnpremul(clampAsIfUnpremul) |
45 | {} |
46 | |
47 | // fClampAsIfUnpremul is always false when constructed through public APIs, |
48 | // so there's no need to read or write it here. |
49 | |
50 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
51 | auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
52 | auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode); |
53 | SkMatrix localMatrix; |
54 | buffer.readMatrix(&localMatrix); |
55 | sk_sp<SkImage> img = buffer.readImage(); |
56 | if (!img) { |
57 | return nullptr; |
58 | } |
59 | return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix); |
60 | } |
61 | |
62 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
63 | buffer.writeUInt((unsigned)fTileModeX); |
64 | buffer.writeUInt((unsigned)fTileModeY); |
65 | buffer.writeMatrix(this->getLocalMatrix()); |
66 | buffer.writeImage(fImage.get()); |
67 | SkASSERT(fClampAsIfUnpremul == false); |
68 | } |
69 | |
70 | bool SkImageShader::isOpaque() const { |
71 | return fImage->isOpaque() && |
72 | fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal; |
73 | } |
74 | |
75 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
76 | static bool legacy_shader_can_handle(const SkMatrix& inv) { |
77 | if (inv.hasPerspective()) { |
78 | return false; |
79 | } |
80 | |
81 | // Scale+translate methods are always present, but affine might not be. |
82 | if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) { |
83 | return false; |
84 | } |
85 | |
86 | // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates |
87 | // out of range. |
88 | const SkScalar max_dev_coord = 32767.0f; |
89 | const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord)); |
90 | |
91 | // take 1/4 of max signed 32bits so we have room to subtract local values |
92 | const SkScalar max_fixed32dot32 = float(SK_MaxS32) * 0.25f; |
93 | if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32, |
94 | +max_fixed32dot32, +max_fixed32dot32).contains(src)) { |
95 | return false; |
96 | } |
97 | |
98 | // legacy shader impl should be able to handle these matrices |
99 | return true; |
100 | } |
101 | |
102 | SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec, |
103 | SkArenaAlloc* alloc) const { |
104 | if (fImage->alphaType() == kUnpremul_SkAlphaType) { |
105 | return nullptr; |
106 | } |
107 | if (fImage->colorType() != kN32_SkColorType) { |
108 | return nullptr; |
109 | } |
110 | if (fTileModeX != fTileModeY) { |
111 | return nullptr; |
112 | } |
113 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
114 | return nullptr; |
115 | } |
116 | |
117 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, |
118 | // so it can't handle bitmaps larger than 65535. |
119 | // |
120 | // We back off another bit to 32767 to make small amounts of |
121 | // intermediate math safe, e.g. in |
122 | // |
123 | // SkFixed fx = ...; |
124 | // fx = tile(fx + SK_Fixed1); |
125 | // |
126 | // we want to make sure (fx + SK_Fixed1) never overflows. |
127 | if (fImage-> width() > 32767 || |
128 | fImage->height() > 32767) { |
129 | return nullptr; |
130 | } |
131 | |
132 | SkMatrix inv; |
133 | if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) || |
134 | !legacy_shader_can_handle(inv)) { |
135 | return nullptr; |
136 | } |
137 | |
138 | if (!rec.isLegacyCompatible(fImage->colorSpace())) { |
139 | return nullptr; |
140 | } |
141 | |
142 | return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, |
143 | as_IB(fImage.get()), rec, alloc); |
144 | } |
145 | #endif |
146 | |
147 | SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const { |
148 | if (texM) { |
149 | *texM = this->getLocalMatrix(); |
150 | } |
151 | if (xy) { |
152 | xy[0] = fTileModeX; |
153 | xy[1] = fTileModeY; |
154 | } |
155 | return const_cast<SkImage*>(fImage.get()); |
156 | } |
157 | |
158 | sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, |
159 | SkTileMode tmx, SkTileMode tmy, |
160 | const SkMatrix* localMatrix, |
161 | bool clampAsIfUnpremul) { |
162 | if (!image) { |
163 | return sk_make_sp<SkEmptyShader>(); |
164 | } |
165 | return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) }; |
166 | } |
167 | |
168 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
169 | |
170 | #if SK_SUPPORT_GPU |
171 | |
172 | #include "include/private/GrRecordingContext.h" |
173 | #include "src/gpu/GrCaps.h" |
174 | #include "src/gpu/GrColorInfo.h" |
175 | #include "src/gpu/GrRecordingContextPriv.h" |
176 | #include "src/gpu/SkGr.h" |
177 | #include "src/gpu/effects/GrBicubicEffect.h" |
178 | #include "src/gpu/effects/GrTextureEffect.h" |
179 | |
180 | static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) { |
181 | switch (tileMode) { |
182 | case SkTileMode::kClamp: |
183 | return GrSamplerState::WrapMode::kClamp; |
184 | case SkTileMode::kRepeat: |
185 | return GrSamplerState::WrapMode::kRepeat; |
186 | case SkTileMode::kMirror: |
187 | return GrSamplerState::WrapMode::kMirrorRepeat; |
188 | case SkTileMode::kDecal: |
189 | return GrSamplerState::WrapMode::kClampToBorder; |
190 | } |
191 | SK_ABORT("Unknown tile mode." ); |
192 | } |
193 | |
194 | std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor( |
195 | const GrFPArgs& args) const { |
196 | const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix); |
197 | SkMatrix lmInverse; |
198 | if (!lm->invert(&lmInverse)) { |
199 | return nullptr; |
200 | } |
201 | |
202 | GrSamplerState::WrapMode wmX = tile_mode_to_wrap_mode(fTileModeX), |
203 | wmY = tile_mode_to_wrap_mode(fTileModeY); |
204 | |
205 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
206 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
207 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
208 | // are provided by the caller. |
209 | bool doBicubic; |
210 | GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode( |
211 | fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm, |
212 | args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic); |
213 | GrMipMapped mipMapped = GrMipMapped::kNo; |
214 | if (textureFilterMode == GrSamplerState::Filter::kMipMap) { |
215 | mipMapped = GrMipMapped::kYes; |
216 | } |
217 | GrSurfaceProxyView view = as_IB(fImage)->refView(args.fContext, mipMapped); |
218 | if (!view) { |
219 | return nullptr; |
220 | } |
221 | |
222 | SkAlphaType srcAlphaType = fImage->alphaType(); |
223 | |
224 | const auto& caps = *args.fContext->priv().caps(); |
225 | |
226 | std::unique_ptr<GrFragmentProcessor> inner; |
227 | if (doBicubic) { |
228 | static constexpr auto kDir = GrBicubicEffect::Direction::kXY; |
229 | inner = GrBicubicEffect::Make(std::move(view), srcAlphaType, lmInverse, wmX, wmY, kDir, |
230 | caps); |
231 | } else { |
232 | GrSamplerState samplerState(wmX, wmY, textureFilterMode); |
233 | inner = GrTextureEffect::Make(std::move(view), srcAlphaType, lmInverse, samplerState, caps); |
234 | } |
235 | inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), srcAlphaType, |
236 | args.fDstColorInfo->colorSpace()); |
237 | |
238 | bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType()); |
239 | if (isAlphaOnly) { |
240 | return inner; |
241 | } else if (args.fInputColorIsOpaque) { |
242 | return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false); |
243 | } |
244 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
245 | } |
246 | |
247 | #endif |
248 | |
249 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
250 | #include "src/core/SkImagePriv.h" |
251 | |
252 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy, |
253 | const SkMatrix* localMatrix, SkCopyPixelsMode cpm) { |
254 | return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm), |
255 | tmx, tmy, localMatrix); |
256 | } |
257 | |
258 | sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src, |
259 | SkTileMode tmx, SkTileMode tmy, |
260 | const SkMatrix* localMatrix, SkCopyPixelsMode mode) { |
261 | auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode); |
262 | if (!s) { |
263 | return nullptr; |
264 | } |
265 | if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) { |
266 | // Compose the image shader with the paint's shader. Alpha images+shaders should output the |
267 | // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with |
268 | // the source image and dst shader (MakeBlend takes dst first, src second). |
269 | s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s)); |
270 | } |
271 | return s; |
272 | } |
273 | |
274 | void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); } |
275 | |
276 | class SkImageStageUpdater : public SkStageUpdater { |
277 | public: |
278 | SkImageStageUpdater(const SkImageShader* shader, bool usePersp) |
279 | : fShader(shader) |
280 | , fUsePersp(usePersp || as_SB(shader)->getLocalMatrix().hasPerspective()) |
281 | {} |
282 | |
283 | const SkImageShader* fShader; |
284 | const bool fUsePersp; // else use affine |
285 | |
286 | // large enough for perspective, though often we just use 2x3 |
287 | float fMatrixStorage[9]; |
288 | |
289 | #if 0 // TODO: when we support mipmaps |
290 | SkRasterPipeline_GatherCtx* fGather; |
291 | SkRasterPipeline_TileCtx* fLimitX; |
292 | SkRasterPipeline_TileCtx* fLimitY; |
293 | SkRasterPipeline_DecalTileCtx* fDecal; |
294 | #endif |
295 | |
296 | void append_matrix_stage(SkRasterPipeline* p) { |
297 | if (fUsePersp) { |
298 | p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage); |
299 | } else { |
300 | p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage); |
301 | } |
302 | } |
303 | |
304 | bool update(const SkMatrix& ctm, const SkMatrix* localM) override { |
305 | SkMatrix matrix; |
306 | if (fShader->computeTotalInverse(ctm, localM, &matrix)) { |
307 | if (fUsePersp) { |
308 | matrix.get9(fMatrixStorage); |
309 | } else { |
310 | // if we get here, matrix should be affine. If it isn't, then defensively we |
311 | // won't draw (by returning false), but we should work to never let this |
312 | // happen (i.e. better preflight by the caller to know ahead of time that we |
313 | // may encounter perspective, either in the CTM, or in the localM). |
314 | // |
315 | // See https://bugs.chromium.org/p/skia/issues/detail?id=10004 |
316 | // |
317 | if (!matrix.asAffine(fMatrixStorage)) { |
318 | SkASSERT(false); |
319 | return false; |
320 | } |
321 | } |
322 | return true; |
323 | } |
324 | return false; |
325 | } |
326 | }; |
327 | |
328 | static void tweak_quality_and_inv_matrix(SkFilterQuality* quality, SkMatrix* matrix) { |
329 | // When the matrix is just an integer translate, bilerp == nearest neighbor. |
330 | if (*quality == kLow_SkFilterQuality && |
331 | matrix->getType() <= SkMatrix::kTranslate_Mask && |
332 | matrix->getTranslateX() == (int)matrix->getTranslateX() && |
333 | matrix->getTranslateY() == (int)matrix->getTranslateY()) { |
334 | *quality = kNone_SkFilterQuality; |
335 | } |
336 | |
337 | // See skia:4649 and the GM image_scale_aligned. |
338 | if (*quality == kNone_SkFilterQuality) { |
339 | if (matrix->getScaleX() >= 0) { |
340 | matrix->setTranslateX(nextafterf(matrix->getTranslateX(), |
341 | floorf(matrix->getTranslateX()))); |
342 | } |
343 | if (matrix->getScaleY() >= 0) { |
344 | matrix->setTranslateY(nextafterf(matrix->getTranslateY(), |
345 | floorf(matrix->getTranslateY()))); |
346 | } |
347 | } |
348 | } |
349 | |
350 | bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const { |
351 | if (updater && rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality) { |
352 | // TODO: medium: recall RequestBitmap and update width/height accordingly |
353 | return false; |
354 | } |
355 | |
356 | SkRasterPipeline* p = rec.fPipeline; |
357 | SkArenaAlloc* alloc = rec.fAlloc; |
358 | auto quality = rec.fPaint.getFilterQuality(); |
359 | |
360 | SkMatrix matrix; |
361 | if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) { |
362 | return false; |
363 | } |
364 | |
365 | const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), |
366 | matrix, quality, alloc); |
367 | if (!state) { |
368 | return false; |
369 | } |
370 | |
371 | const SkPixmap& pm = state->pixmap(); |
372 | matrix = state->invMatrix(); |
373 | quality = state->quality(); |
374 | auto info = pm.info(); |
375 | |
376 | p->append(SkRasterPipeline::seed_shader); |
377 | |
378 | if (updater) { |
379 | updater->append_matrix_stage(p); |
380 | } else { |
381 | tweak_quality_and_inv_matrix(&quality, &matrix); |
382 | p->append_matrix(alloc, matrix); |
383 | } |
384 | |
385 | auto gather = alloc->make<SkRasterPipeline_GatherCtx>(); |
386 | gather->pixels = pm.addr(); |
387 | gather->stride = pm.rowBytesAsPixels(); |
388 | gather->width = pm.width(); |
389 | gather->height = pm.height(); |
390 | |
391 | auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(), |
392 | limit_y = alloc->make<SkRasterPipeline_TileCtx>(); |
393 | limit_x->scale = pm.width(); |
394 | limit_x->invScale = 1.0f / pm.width(); |
395 | limit_y->scale = pm.height(); |
396 | limit_y->invScale = 1.0f / pm.height(); |
397 | |
398 | SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr; |
399 | bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal; |
400 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
401 | decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>(); |
402 | decal_ctx->limit_x = limit_x->scale; |
403 | decal_ctx->limit_y = limit_y->scale; |
404 | } |
405 | |
406 | #if 0 // TODO: when we support kMedium |
407 | if (updator && (quality == kMedium_SkFilterQuality)) { |
408 | // if we change levels in mipmap, we need to update the scales (and invScales) |
409 | updator->fGather = gather; |
410 | updator->fLimitX = limit_x; |
411 | updator->fLimitY = limit_y; |
412 | updator->fDecal = decal_ctx; |
413 | } |
414 | #endif |
415 | |
416 | auto append_tiling_and_gather = [&] { |
417 | if (decal_x_and_y) { |
418 | p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); |
419 | } else { |
420 | switch (fTileModeX) { |
421 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
422 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break; |
423 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break; |
424 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break; |
425 | } |
426 | switch (fTileModeY) { |
427 | case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break; |
428 | case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break; |
429 | case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break; |
430 | case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break; |
431 | } |
432 | } |
433 | |
434 | void* ctx = gather; |
435 | switch (info.colorType()) { |
436 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; |
437 | case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break; |
438 | case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break; |
439 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; |
440 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; |
441 | case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break; |
442 | case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break; |
443 | case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break; |
444 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; |
445 | case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; |
446 | case kR16G16B16A16_unorm_SkColorType: |
447 | p->append(SkRasterPipeline::gather_16161616,ctx); break; |
448 | case kRGBA_F16Norm_SkColorType: |
449 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; |
450 | case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break; |
451 | |
452 | case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); |
453 | p->append(SkRasterPipeline::alpha_to_gray ); break; |
454 | |
455 | case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
456 | p->append(SkRasterPipeline::force_opaque ); break; |
457 | |
458 | case kBGRA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
459 | p->append(SkRasterPipeline::swap_rb ); break; |
460 | |
461 | case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
462 | p->append(SkRasterPipeline::force_opaque ); break; |
463 | |
464 | case kBGR_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); |
465 | p->append(SkRasterPipeline::force_opaque ); |
466 | p->append(SkRasterPipeline::swap_rb ); break; |
467 | |
468 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); |
469 | p->append(SkRasterPipeline::swap_rb ); break; |
470 | |
471 | case kUnknown_SkColorType: SkASSERT(false); |
472 | } |
473 | if (decal_ctx) { |
474 | p->append(SkRasterPipeline::check_decal_mask, decal_ctx); |
475 | } |
476 | }; |
477 | |
478 | auto append_misc = [&] { |
479 | // This is an inessential optimization... it's logically safe to set this to false. |
480 | // But if... |
481 | // - we know the image is definitely normalized, and |
482 | // - we're doing some color space conversion, and |
483 | // - sRGB curves are involved, |
484 | // then we can use slightly faster math that doesn't work well outside [0,1]. |
485 | bool src_is_normalized = SkColorTypeIsNormalized(info.colorType()); |
486 | |
487 | SkColorSpace* cs = info.colorSpace(); |
488 | SkAlphaType at = info.alphaType(); |
489 | |
490 | // Color for A8 images comes from the paint. TODO: all alpha images? none? |
491 | if (info.colorType() == kAlpha_8_SkColorType) { |
492 | SkColor4f rgb = rec.fPaint.getColor4f(); |
493 | p->append_set_rgb(alloc, rgb); |
494 | |
495 | src_is_normalized = rgb.fitsInBytes(); |
496 | cs = sk_srgb_singleton(); |
497 | at = kUnpremul_SkAlphaType; |
498 | } |
499 | |
500 | // Bicubic filtering naturally produces out of range values on both sides of [0,1]. |
501 | if (quality == kHigh_SkFilterQuality) { |
502 | p->append(SkRasterPipeline::clamp_0); |
503 | p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul |
504 | ? SkRasterPipeline::clamp_1 |
505 | : SkRasterPipeline::clamp_a); |
506 | src_is_normalized = true; |
507 | } |
508 | |
509 | // Transform color space and alpha type to match shader convention (dst CS, premul alpha). |
510 | alloc->make<SkColorSpaceXformSteps>(cs, at, |
511 | rec.fDstCS, kPremul_SkAlphaType) |
512 | ->apply(p, src_is_normalized); |
513 | |
514 | return true; |
515 | }; |
516 | |
517 | // Check for fast-path stages. |
518 | auto ct = info.colorType(); |
519 | if (true |
520 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
521 | && quality == kLow_SkFilterQuality |
522 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
523 | |
524 | p->append(SkRasterPipeline::bilerp_clamp_8888, gather); |
525 | if (ct == kBGRA_8888_SkColorType) { |
526 | p->append(SkRasterPipeline::swap_rb); |
527 | } |
528 | return append_misc(); |
529 | } |
530 | if (true |
531 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
532 | && quality == kLow_SkFilterQuality |
533 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
534 | && fTileModeY != SkTileMode::kDecal) { |
535 | |
536 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
537 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
538 | ctx->ct = ct; |
539 | ctx->tileX = fTileModeX; |
540 | ctx->tileY = fTileModeY; |
541 | ctx->invWidth = 1.0f / ctx->width; |
542 | ctx->invHeight = 1.0f / ctx->height; |
543 | p->append(SkRasterPipeline::bilinear, ctx); |
544 | return append_misc(); |
545 | } |
546 | if (true |
547 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) |
548 | && quality == kHigh_SkFilterQuality |
549 | && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) { |
550 | |
551 | p->append(SkRasterPipeline::bicubic_clamp_8888, gather); |
552 | if (ct == kBGRA_8888_SkColorType) { |
553 | p->append(SkRasterPipeline::swap_rb); |
554 | } |
555 | return append_misc(); |
556 | } |
557 | if (true |
558 | && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats |
559 | && quality == kHigh_SkFilterQuality |
560 | && fTileModeX != SkTileMode::kDecal // TODO decal too? |
561 | && fTileModeY != SkTileMode::kDecal) { |
562 | |
563 | auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>(); |
564 | *(SkRasterPipeline_GatherCtx*)(ctx) = *gather; |
565 | ctx->ct = ct; |
566 | ctx->tileX = fTileModeX; |
567 | ctx->tileY = fTileModeY; |
568 | ctx->invWidth = 1.0f / ctx->width; |
569 | ctx->invHeight = 1.0f / ctx->height; |
570 | p->append(SkRasterPipeline::bicubic, ctx); |
571 | return append_misc(); |
572 | } |
573 | |
574 | SkRasterPipeline_SamplerCtx* sampler = nullptr; |
575 | if (quality != kNone_SkFilterQuality) { |
576 | sampler = alloc->make<SkRasterPipeline_SamplerCtx>(); |
577 | } |
578 | |
579 | auto sample = [&](SkRasterPipeline::StockStage setup_x, |
580 | SkRasterPipeline::StockStage setup_y) { |
581 | p->append(setup_x, sampler); |
582 | p->append(setup_y, sampler); |
583 | append_tiling_and_gather(); |
584 | p->append(SkRasterPipeline::accumulate, sampler); |
585 | }; |
586 | |
587 | if (quality == kNone_SkFilterQuality) { |
588 | append_tiling_and_gather(); |
589 | } else if (quality == kLow_SkFilterQuality) { |
590 | p->append(SkRasterPipeline::save_xy, sampler); |
591 | |
592 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); |
593 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); |
594 | sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); |
595 | sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); |
596 | |
597 | p->append(SkRasterPipeline::move_dst_src); |
598 | |
599 | } else { |
600 | SkASSERT(quality == kHigh_SkFilterQuality); |
601 | p->append(SkRasterPipeline::save_xy, sampler); |
602 | |
603 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); |
604 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); |
605 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); |
606 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); |
607 | |
608 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); |
609 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); |
610 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); |
611 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); |
612 | |
613 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); |
614 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); |
615 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); |
616 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); |
617 | |
618 | sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); |
619 | sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); |
620 | sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); |
621 | sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); |
622 | |
623 | p->append(SkRasterPipeline::move_dst_src); |
624 | } |
625 | |
626 | return append_misc(); |
627 | } |
628 | |
629 | bool SkImageShader::onAppendStages(const SkStageRec& rec) const { |
630 | return this->doStages(rec, nullptr); |
631 | } |
632 | |
633 | SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const { |
634 | bool usePersp = rec.fCTM.hasPerspective(); |
635 | auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp); |
636 | return this->doStages(rec, updater) ? updater : nullptr; |
637 | } |
638 | |
639 | skvm::Color SkImageShader::onProgram(skvm::Builder* p, skvm::F32 x, skvm::F32 y, skvm::Color paint, |
640 | const SkMatrix& ctm, const SkMatrix* localM, |
641 | SkFilterQuality quality, const SkColorInfo& dst, |
642 | skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const { |
643 | SkMatrix inv; |
644 | if (!this->computeTotalInverse(ctm, localM, &inv)) { |
645 | return {}; |
646 | } |
647 | |
648 | // We use RequestBitmap() to make sure our SkBitmapController::State lives in the alloc. |
649 | // This lets the SkVMBlitter hang on to this state and keep our image alive. |
650 | auto state = SkBitmapController::RequestBitmap(as_IB(fImage.get()), inv, quality, alloc); |
651 | if (!state) { |
652 | return {}; |
653 | } |
654 | const SkPixmap& pm = state->pixmap(); |
655 | inv = state->invMatrix(); |
656 | quality = state->quality(); |
657 | tweak_quality_and_inv_matrix(&quality, &inv); |
658 | inv.normalizePerspective(); |
659 | |
660 | // Apply matrix to convert dst coords to sample center coords. |
661 | SkShaderBase::ApplyMatrix(p, inv, &x,&y,uniforms); |
662 | |
663 | // Bail out if sample() can't yet handle our image's color type. |
664 | switch (pm.colorType()) { |
665 | default: return {}; |
666 | case kGray_8_SkColorType: |
667 | case kAlpha_8_SkColorType: |
668 | case kRGB_565_SkColorType: |
669 | case kRGB_888x_SkColorType: |
670 | case kRGBA_8888_SkColorType: |
671 | case kBGRA_8888_SkColorType: |
672 | case kRGBA_1010102_SkColorType: |
673 | case kBGRA_1010102_SkColorType: |
674 | case kRGB_101010x_SkColorType: |
675 | case kBGR_101010x_SkColorType: break; |
676 | } |
677 | |
678 | // We can exploit image opacity to skip work unpacking alpha channels. |
679 | const bool input_is_opaque = SkAlphaTypeIsOpaque(pm.alphaType()) |
680 | || SkColorTypeIsAlwaysOpaque(pm.colorType()); |
681 | |
682 | // Each call to sample() will try to rewrite the same uniforms over and over, |
683 | // so remember where we start and reset back there each time. That way each |
684 | // sample() call uses the same uniform offsets. |
685 | const size_t uniforms_before_sample = uniforms->buf.size(); |
686 | |
687 | auto sample = [&](skvm::F32 sx, skvm::F32 sy) -> skvm::Color { |
688 | uniforms->buf.resize(uniforms_before_sample); |
689 | |
690 | // repeat() and mirror() are written assuming they'll be followed by a [0,scale) clamp. |
691 | auto repeat = [&](skvm::F32 v, float scale) { |
692 | skvm::F32 S = p->uniformF(uniforms->pushF( scale)), |
693 | I = p->uniformF(uniforms->pushF(1.0f/scale)); |
694 | return v - floor(v * I) * S; |
695 | }; |
696 | auto mirror = [&](skvm::F32 v, float scale) { |
697 | skvm::F32 S = p->uniformF(uniforms->pushF( scale)), |
698 | I2 = p->uniformF(uniforms->pushF(0.5f/scale)); |
699 | // abs( (v-scale) - (2*scale)*floor((v-scale)*(0.5f/scale)) - scale ) |
700 | // {---A---} {------------------B------------------} |
701 | skvm::F32 A = v - S, |
702 | B = (S + S) * floor(A * I2); |
703 | return abs(A - B - S); |
704 | }; |
705 | switch (fTileModeX) { |
706 | case SkTileMode::kDecal: /* handled after gather */ break; |
707 | case SkTileMode::kClamp: /* we always clamp */ break; |
708 | case SkTileMode::kRepeat: sx = repeat(sx, pm.width()); break; |
709 | case SkTileMode::kMirror: sx = mirror(sx, pm.width()); break; |
710 | } |
711 | switch (fTileModeY) { |
712 | case SkTileMode::kDecal: /* handled after gather */ break; |
713 | case SkTileMode::kClamp: /* we always clamp */ break; |
714 | case SkTileMode::kRepeat: sy = repeat(sy, pm.height()); break; |
715 | case SkTileMode::kMirror: sy = mirror(sy, pm.height()); break; |
716 | } |
717 | |
718 | // Always clamp sample coordinates to [0,width), [0,height), both for memory |
719 | // safety and to handle the clamps still needed by kClamp, kRepeat, and kMirror. |
720 | auto clamp0x = [&](skvm::F32 v, float limit) { |
721 | // Subtract an ulp so the upper clamp limit excludes limit itself. |
722 | int bits; |
723 | memcpy(&bits, &limit, 4); |
724 | return clamp(v, 0.0f, p->uniformF(uniforms->push(bits-1))); |
725 | }; |
726 | skvm::F32 clamped_x = clamp0x(sx, pm. width()), |
727 | clamped_y = clamp0x(sy, pm.height()); |
728 | |
729 | // Load pixels from pm.addr()[(int)sx + (int)sy*stride]. |
730 | skvm::Uniform img = uniforms->pushPtr(pm.addr()); |
731 | skvm::I32 index = trunc(clamped_x) + |
732 | trunc(clamped_y) * p->uniform32(uniforms->push(pm.rowBytesAsPixels())); |
733 | skvm::Color c; |
734 | switch (pm.colorType()) { |
735 | default: SkUNREACHABLE; |
736 | |
737 | case kGray_8_SkColorType: c.r = c.g = c.b = from_unorm(8, gather8(img, index)); |
738 | c.a = p->splat(1.0f); |
739 | break; |
740 | |
741 | case kAlpha_8_SkColorType: c.r = c.g = c.b = p->splat(0.0f); |
742 | c.a = from_unorm(8, gather8(img, index)); |
743 | break; |
744 | |
745 | case kRGB_565_SkColorType: c = unpack_565 (gather16(img, index)); break; |
746 | |
747 | case kRGB_888x_SkColorType: [[fallthrough]]; |
748 | case kRGBA_8888_SkColorType: c = unpack_8888(gather32(img, index)); |
749 | break; |
750 | case kBGRA_8888_SkColorType: c = unpack_8888(gather32(img, index)); |
751 | std::swap(c.r, c.b); |
752 | break; |
753 | |
754 | case kRGB_101010x_SkColorType: [[fallthrough]]; |
755 | case kRGBA_1010102_SkColorType: c = unpack_1010102(gather32(img, index)); |
756 | break; |
757 | |
758 | case kBGR_101010x_SkColorType: [[fallthrough]]; |
759 | case kBGRA_1010102_SkColorType: c = unpack_1010102(gather32(img, index)); |
760 | std::swap(c.r, c.b); |
761 | break; |
762 | } |
763 | // If we know the image is opaque, jump right to alpha = 1.0f, skipping work to unpack it. |
764 | if (input_is_opaque) { |
765 | c.a = p->splat(1.0f); |
766 | } |
767 | |
768 | // Mask away any pixels that we tried to sample outside the bounds in kDecal. |
769 | if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) { |
770 | skvm::I32 mask = p->splat(~0); |
771 | if (fTileModeX == SkTileMode::kDecal) { mask &= (sx == clamped_x); } |
772 | if (fTileModeY == SkTileMode::kDecal) { mask &= (sy == clamped_y); } |
773 | c.r = bit_cast(p->bit_and(mask, bit_cast(c.r))); |
774 | c.g = bit_cast(p->bit_and(mask, bit_cast(c.g))); |
775 | c.b = bit_cast(p->bit_and(mask, bit_cast(c.b))); |
776 | c.a = bit_cast(p->bit_and(mask, bit_cast(c.a))); |
777 | // Notice that even if input_is_opaque, c.a might now be 0. |
778 | } |
779 | |
780 | return c; |
781 | }; |
782 | |
783 | skvm::Color c; |
784 | |
785 | if (quality == kNone_SkFilterQuality) { |
786 | c = sample(x,y); |
787 | } else if (quality == kLow_SkFilterQuality) { |
788 | // Our four sample points are the corners of a logical 1x1 pixel |
789 | // box surrounding (x,y) at (0.5,0.5) off-center. |
790 | skvm::F32 left = x - 0.5f, |
791 | top = y - 0.5f, |
792 | right = x + 0.5f, |
793 | bottom = y + 0.5f; |
794 | |
795 | // The fractional parts of right and bottom are our lerp factors in x and y respectively. |
796 | skvm::F32 fx = fract(right ), |
797 | fy = fract(bottom); |
798 | |
799 | c = lerp(lerp(sample(left,top ), sample(right,top ), fx), |
800 | lerp(sample(left,bottom), sample(right,bottom), fx), fy); |
801 | } else { |
802 | SkASSERT(quality == kHigh_SkFilterQuality); |
803 | |
804 | // All bicubic samples have the same fractional offset (fx,fy) from the center. |
805 | // They're either the 16 corners of a 3x3 grid/ surrounding (x,y) at (0.5,0.5) off-center. |
806 | skvm::F32 fx = fract(x + 0.5f), |
807 | fy = fract(y + 0.5f); |
808 | |
809 | // See GrCubicEffect for details of these weights. |
810 | // TODO: these maybe don't seem right looking at gm/bicubic and GrBicubicEffect. |
811 | auto near = [&](skvm::F32 t) { |
812 | // 1/18 + 9/18t + 27/18t^2 - 21/18t^3 == t ( t ( -21/18t + 27/18) + 9/18) + 1/18 |
813 | return t * (t * (t * (-21/18.0f) + 27/18.0f) + 9/18.0f) + 1/18.0f; |
814 | }; |
815 | auto far = [&](skvm::F32 t) { |
816 | // 0/18 + 0/18*t - 6/18t^2 + 7/18t^3 == t^2 (7/18t - 6/18) |
817 | return t * t * (t * (7/18.0f) - 6/18.0f); |
818 | }; |
819 | const skvm::F32 wx[] = { |
820 | far (1.0f - fx), |
821 | near(1.0f - fx), |
822 | near( fx), |
823 | far ( fx), |
824 | }; |
825 | const skvm::F32 wy[] = { |
826 | far (1.0f - fy), |
827 | near(1.0f - fy), |
828 | near( fy), |
829 | far ( fy), |
830 | }; |
831 | |
832 | c.r = c.g = c.b = c.a = p->splat(0.0f); |
833 | |
834 | skvm::F32 sy = y - 1.5f; |
835 | for (int j = 0; j < 4; j++, sy += 1.0f) { |
836 | skvm::F32 sx = x - 1.5f; |
837 | for (int i = 0; i < 4; i++, sx += 1.0f) { |
838 | skvm::Color s = sample(sx,sy); |
839 | skvm::F32 w = wx[i] * wy[j]; |
840 | |
841 | c.r += s.r * w; |
842 | c.g += s.g * w; |
843 | c.b += s.b * w; |
844 | c.a += s.a * w; |
845 | } |
846 | } |
847 | } |
848 | |
849 | // If the input is opaque and we're not in decal mode, that means the output is too. |
850 | // Forcing *a to 1.0 here will retroactively skip any work we did to interpolate sample alphas. |
851 | if (input_is_opaque |
852 | && fTileModeX != SkTileMode::kDecal |
853 | && fTileModeY != SkTileMode::kDecal) { |
854 | c.a = p->splat(1.0f); |
855 | } |
856 | |
857 | // Alpha-only images get their color from the paint (already converted to dst color space). |
858 | SkColorSpace* cs = pm.colorSpace(); |
859 | SkAlphaType at = pm.alphaType(); |
860 | if (SkColorTypeIsAlphaOnly(pm.colorType())) { |
861 | c.r = paint.r; |
862 | c.g = paint.g; |
863 | c.b = paint.b; |
864 | |
865 | cs = dst.colorSpace(); |
866 | at = kUnpremul_SkAlphaType; |
867 | } |
868 | |
869 | if (quality == kHigh_SkFilterQuality) { |
870 | // Bicubic filtering naturally produces out of range values on both sides of [0,1]. |
871 | c.a = clamp01(c.a); |
872 | |
873 | skvm::F32 limit = (at == kUnpremul_SkAlphaType || fClampAsIfUnpremul) |
874 | ? p->splat(1.0f) |
875 | : c.a; |
876 | c.r = clamp(c.r, 0.0f, limit); |
877 | c.g = clamp(c.g, 0.0f, limit); |
878 | c.b = clamp(c.b, 0.0f, limit); |
879 | } |
880 | |
881 | SkColorSpaceXformSteps steps{cs,at, dst.colorSpace(),kPremul_SkAlphaType}; |
882 | return steps.program(p, uniforms, c); |
883 | } |
884 | |
885 | |