blob: 2e34ffcd1af7140523366baf06d5ae9e682ff1bf [file] [log] [blame]
reed856e9d92015-09-30 12:21:45 -07001/*
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
Ben Wagner729a23f2019-05-17 16:29:34 -04008#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/core/SkBitmapController.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkColorSpacePriv.h"
11#include "src/core/SkColorSpaceXformSteps.h"
Mike Klein37bc8f92019-10-21 13:10:07 -050012#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkRasterPipeline.h"
14#include "src/core/SkReadBuffer.h"
15#include "src/core/SkWriteBuffer.h"
16#include "src/image/SkImage_Base.h"
17#include "src/shaders/SkBitmapProcShader.h"
18#include "src/shaders/SkEmptyShader.h"
19#include "src/shaders/SkImageShader.h"
reed856e9d92015-09-30 12:21:45 -070020
Mike Reed587d0822017-06-23 16:49:12 -040021/**
22 * We are faster in clamp, so always use that tiling when we can.
23 */
Mike Reedfae8fce2019-04-03 10:27:45 -040024static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040025 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040026#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
27 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
28 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040029 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040030#else
Mike Reedfae8fce2019-04-03 10:27:45 -040031 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040032#endif
Mike Reed587d0822017-06-23 16:49:12 -040033}
34
Mike Klein1f313092018-01-03 10:30:21 -050035SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040036 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -050037 const SkMatrix* localMatrix,
38 bool clampAsIfUnpremul)
39 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070040 , fImage(std::move(img))
Mike Reed587d0822017-06-23 16:49:12 -040041 , fTileModeX(optimize(tmx, fImage->width()))
42 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050043 , fClampAsIfUnpremul(clampAsIfUnpremul)
reed856e9d92015-09-30 12:21:45 -070044{}
45
Mike Klein1f313092018-01-03 10:30:21 -050046// fClampAsIfUnpremul is always false when constructed through public APIs,
47// so there's no need to read or write it here.
48
reed60c9b582016-04-03 09:11:13 -070049sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
Mike Reede25b4472019-04-02 17:49:12 -040050 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
51 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Klein1f313092018-01-03 10:30:21 -050052 SkMatrix localMatrix;
53 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -070054 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -070055 if (!img) {
56 return nullptr;
57 }
Mike Reede25b4472019-04-02 17:49:12 -040058 return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix);
reed856e9d92015-09-30 12:21:45 -070059}
60
61void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -040062 buffer.writeUInt((unsigned)fTileModeX);
63 buffer.writeUInt((unsigned)fTileModeY);
reed856e9d92015-09-30 12:21:45 -070064 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -070065 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -050066 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -070067}
68
69bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -040070 return fImage->isOpaque() &&
71 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -070072}
73
Mike Reede92aae62018-10-17 10:21:51 -040074#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malitaaf2769d2018-04-04 13:46:35 -040075static bool legacy_shader_can_handle(const SkMatrix& inv) {
Mike Klein37bc8f92019-10-21 13:10:07 -050076 if (inv.hasPerspective()) {
77 return false;
78 }
79
80 // Scale+translate methods are always present, but affine might not be.
81 if (!SkOpts::S32_alpha_D32_filter_DXDY && !inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -050082 return false;
83 }
84
85 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
86 // out of range.
87 const SkScalar max_dev_coord = 32767.0f;
Mike Klein37bc8f92019-10-21 13:10:07 -050088 const SkRect src = inv.mapRect(SkRect::MakeWH(max_dev_coord, max_dev_coord));
Mike Reeda12c4192018-02-01 16:34:03 -050089
Mike Reed1eb5ca42018-03-08 14:20:52 -050090 // take 1/4 of max signed 32bits so we have room to subtract local values
91 const SkScalar max_fixed32dot32 = SK_MaxS32 * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -050092 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
Mike Klein37bc8f92019-10-21 13:10:07 -050093 +max_fixed32dot32, +max_fixed32dot32).contains(src)) {
Mike Reeda12c4192018-02-01 16:34:03 -050094 return false;
95 }
96
97 // legacy shader impl should be able to handle these matrices
98 return true;
99}
100
Florin Malita4aed1382017-05-25 10:38:07 -0400101SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
102 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -0400103 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400104 return nullptr;
105 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400106 if (fImage->colorType() != kN32_SkColorType) {
107 return nullptr;
108 }
Bharat Ahujaa7e4fe42019-11-14 23:53:58 +0530109#if !defined(SK_SUPPORT_LEGACY_TILED_BITMAPS)
Florin Malitaaf2769d2018-04-04 13:46:35 -0400110 if (fTileModeX != fTileModeY) {
111 return nullptr;
112 }
Bharat Ahujaa7e4fe42019-11-14 23:53:58 +0530113#endif
Mike Reedfae8fce2019-04-03 10:27:45 -0400114 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400115 return nullptr;
116 }
117
Mike Kleindac694d2018-12-18 10:13:52 -0500118 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500119 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500120 //
Mike Klein67761eb2018-12-18 10:16:53 -0500121 // We back off another bit to 32767 to make small amounts of
122 // intermediate math safe, e.g. in
123 //
124 // SkFixed fx = ...;
125 // fx = tile(fx + SK_Fixed1);
126 //
127 // we want to make sure (fx + SK_Fixed1) never overflows.
128 if (fImage-> width() > 32767 ||
129 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500130 return nullptr;
131 }
132
Florin Malitaaf2769d2018-04-04 13:46:35 -0400133 SkMatrix inv;
134 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
135 !legacy_shader_can_handle(inv)) {
136 return nullptr;
137 }
138
Mike Reed011d1662019-02-28 17:19:25 -0500139 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
140 return nullptr;
141 }
142
reed320a40d2016-08-02 06:12:06 -0700143 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed64acf4f2019-08-01 15:35:20 -0400144 as_IB(fImage.get()), rec, alloc);
reed856e9d92015-09-30 12:21:45 -0700145}
Mike Reede92aae62018-10-17 10:21:51 -0400146#endif
reed856e9d92015-09-30 12:21:45 -0700147
Mike Reedfae8fce2019-04-03 10:27:45 -0400148SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700149 if (texM) {
150 *texM = this->getLocalMatrix();
151 }
152 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400153 xy[0] = fTileModeX;
154 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700155 }
156 return const_cast<SkImage*>(fImage.get());
157}
158
Mike Klein1f313092018-01-03 10:30:21 -0500159sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400160 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -0500161 const SkMatrix* localMatrix,
162 bool clampAsIfUnpremul) {
Mike Kleindac694d2018-12-18 10:13:52 -0500163 if (!image) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000164 return sk_make_sp<SkEmptyShader>();
reed320a40d2016-08-02 06:12:06 -0700165 }
Mike Reede25b4472019-04-02 17:49:12 -0400166 return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) };
reed856e9d92015-09-30 12:21:45 -0700167}
168
reed856e9d92015-09-30 12:21:45 -0700169///////////////////////////////////////////////////////////////////////////////////////////////////
170
171#if SK_SUPPORT_GPU
172
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500173#include "include/private/GrRecordingContext.h"
174#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400175#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500176#include "src/gpu/GrRecordingContextPriv.h"
177#include "src/gpu/SkGr.h"
178#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon3a09a372019-12-19 17:13:26 -0500179#include "src/gpu/effects/GrSimpleTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700180
Mike Reedfae8fce2019-04-03 10:27:45 -0400181static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400182 switch (tileMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400183 case SkTileMode::kClamp:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400184 return GrSamplerState::WrapMode::kClamp;
Mike Reedfae8fce2019-04-03 10:27:45 -0400185 case SkTileMode::kRepeat:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400186 return GrSamplerState::WrapMode::kRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400187 case SkTileMode::kMirror:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400188 return GrSamplerState::WrapMode::kMirrorRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400189 case SkTileMode::kDecal:
Michael Ludwigf23a1522018-12-10 11:36:13 -0500190 return GrSamplerState::WrapMode::kClampToBorder;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400191 }
192 SK_ABORT("Unknown tile mode.");
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400193}
194
Brian Salomonaff329b2017-08-11 09:40:37 -0400195std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500196 const GrFPArgs& args) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -0400197 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700198 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400199 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700200 return nullptr;
201 }
reed856e9d92015-09-30 12:21:45 -0700202
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203 GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX),
204 tile_mode_to_wrap_mode(fTileModeY)};
Michael Ludwigbe315a22018-12-17 09:50:51 -0500205
206 // If either domainX or domainY are un-ignored, a texture domain effect has to be used to
207 // implement the decal mode (while leaving non-decal axes alone). The wrap mode originally
208 // clamp-to-border is reset to clamp since the hw cannot implement it directly.
209 GrTextureDomain::Mode domainX = GrTextureDomain::kIgnore_Mode;
210 GrTextureDomain::Mode domainY = GrTextureDomain::kIgnore_Mode;
Robert Phillips9da87e02019-02-04 13:26:26 -0500211 if (!args.fContext->priv().caps()->clampToBorderSupport()) {
Michael Ludwigf23a1522018-12-10 11:36:13 -0500212 if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500213 domainX = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500214 wrapModes[0] = GrSamplerState::WrapMode::kClamp;
215 }
216 if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500217 domainY = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500218 wrapModes[1] = GrSamplerState::WrapMode::kClamp;
219 }
220 }
reed856e9d92015-09-30 12:21:45 -0700221
222 // Must set wrap and filter on the sampler before requesting a texture. In two places below
223 // we check the matrix scale factors to determine how to interpret the filter quality setting.
224 // This completely ignores the complexity of the drawVertices case where explicit local coords
225 // are provided by the caller.
226 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400227 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600228 fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm,
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400230 GrSamplerState samplerState(wrapModes, textureFilterMode);
Robert Phillips67c18d62017-01-20 12:44:06 -0500231 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Osmane7fd8c32018-10-19 13:30:39 -0400232 sk_sp<GrTextureProxy> proxy(as_IB(fImage)->asTextureProxyRef(args.fContext, samplerState,
Brian Osman6064e1c2018-10-19 14:27:54 -0400233 scaleAdjust));
Robert Phillipsb726d582017-03-09 16:36:32 -0500234 if (!proxy) {
reed856e9d92015-09-30 12:21:45 -0700235 return nullptr;
236 }
237
Brian Salomonfc118442019-11-22 19:09:27 -0500238 SkAlphaType srcAlphaType = fImage->alphaType();
Robert Phillipsb726d582017-03-09 16:36:32 -0500239
Robert Phillips67c18d62017-01-20 12:44:06 -0500240 lmInverse.postScale(scaleAdjust[0], scaleAdjust[1]);
241
Brian Salomonaff329b2017-08-11 09:40:37 -0400242 std::unique_ptr<GrFragmentProcessor> inner;
reed856e9d92015-09-30 12:21:45 -0700243 if (doBicubic) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500244 // domainX and domainY will properly apply the decal effect with the texture domain used in
245 // the bicubic filter if clamp to border was unsupported in hardware
Brian Salomona86fc7a2019-05-28 20:42:58 -0400246 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomonfc118442019-11-22 19:09:27 -0500247 inner = GrBicubicEffect::Make(std::move(proxy), lmInverse, wrapModes, domainX, domainY,
248 kDir, srcAlphaType);
reed856e9d92015-09-30 12:21:45 -0700249 } else {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500250 auto dimensions = proxy->dimensions();
251 inner = GrSimpleTextureEffect::Make(std::move(proxy), srcAlphaType, lmInverse,
252 samplerState);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500253 if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500254 SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(dimensions),
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400255 domainX, domainY);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500256 inner = GrDomainEffect::Make(std::move(inner), domain, domainX, domainY,
257 samplerState.filter());
Michael Ludwigbe315a22018-12-17 09:50:51 -0500258 }
reed856e9d92015-09-30 12:21:45 -0700259 }
Brian Salomonfc118442019-11-22 19:09:27 -0500260 inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(), srcAlphaType,
261 args.fDstColorInfo->colorSpace());
Greg Danielc594e622019-10-15 14:01:49 -0400262
263 bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500264 if (isAlphaOnly) {
bungeman06ca8ec2016-06-09 08:01:03 -0700265 return inner;
Brian Salomonc0d79e52019-04-10 15:02:11 -0400266 } else if (args.fInputColorIsOpaque) {
267 return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700268 }
Mike Reed28eaed22018-02-01 11:24:53 -0500269 return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner));
reed856e9d92015-09-30 12:21:45 -0700270}
271
272#endif
reed320a40d2016-08-02 06:12:06 -0700273
274///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500275#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700276
Mike Reede25b4472019-04-02 17:49:12 -0400277sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy,
278 const SkMatrix* localMatrix, SkCopyPixelsMode cpm) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000279 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm),
280 tmx, tmy, localMatrix);
reed320a40d2016-08-02 06:12:06 -0700281}
282
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400283sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400284 SkTileMode tmx, SkTileMode tmy,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400285 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
286 auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode);
287 if (!s) {
288 return nullptr;
289 }
290 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
291 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
292 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
293 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400294 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400295 }
296 return s;
297}
298
Brian Salomon23356442018-11-30 15:33:19 -0500299void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700300
Mike Reed9318a6c2019-08-16 16:16:25 -0400301class SkImageStageUpdater : public SkStageUpdater {
302public:
Mike Reed8845c372019-12-19 13:22:08 -0500303 SkImageStageUpdater(const SkImageShader* shader, bool usePersp)
304 : fShader(shader), fUsePersp(usePersp)
305 {}
Mike Reed9318a6c2019-08-16 16:16:25 -0400306
Mike Reed8845c372019-12-19 13:22:08 -0500307 const SkImageShader* fShader;
308 const bool fUsePersp; // else use affine
309
310 // large enough for perspective, though often we just use 2x3
311 float fMatrixStorage[9];
Mike Reed9318a6c2019-08-16 16:16:25 -0400312
313#if 0 // TODO: when we support mipmaps
314 SkRasterPipeline_GatherCtx* fGather;
315 SkRasterPipeline_TileCtx* fLimitX;
316 SkRasterPipeline_TileCtx* fLimitY;
317 SkRasterPipeline_DecalTileCtx* fDecal;
318#endif
319
Mike Reed8845c372019-12-19 13:22:08 -0500320 void append_matrix_stage(SkRasterPipeline* p) {
321 if (fUsePersp) {
322 p->append(SkRasterPipeline::matrix_perspective, fMatrixStorage);
323 } else {
324 p->append(SkRasterPipeline::matrix_2x3, fMatrixStorage);
325 }
326 }
327
Mike Reed9318a6c2019-08-16 16:16:25 -0400328 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
329 SkMatrix matrix;
Mike Reed8845c372019-12-19 13:22:08 -0500330 if (fShader->computeTotalInverse(ctm, localM, &matrix)) {
331 if (fUsePersp) {
332 matrix.get9(fMatrixStorage);
333 } else {
334 SkAssertResult(matrix.asAffine(fMatrixStorage));
335 }
336 return true;
337 }
338 return false;
Mike Reed9318a6c2019-08-16 16:16:25 -0400339 }
340};
341
342bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
Mike Reed8845c372019-12-19 13:22:08 -0500343 if (updater && rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality) {
344 // TODO: medium: recall RequestBitmap and update width/height accordingly
Mike Reed9318a6c2019-08-16 16:16:25 -0400345 return false;
346 }
347
Mike Reed1d8c42e2017-08-29 14:58:19 -0400348 SkRasterPipeline* p = rec.fPipeline;
349 SkArenaAlloc* alloc = rec.fAlloc;
Mike Reed9318a6c2019-08-16 16:16:25 -0400350 auto quality = rec.fPaint.getFilterQuality();
Mike Reed1d8c42e2017-08-29 14:58:19 -0400351
Florin Malita7558e4d2018-02-07 10:05:53 -0500352 SkMatrix matrix;
353 if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500354 return false;
355 }
Mike Klein06a65e22016-11-17 12:39:09 -0500356
Mike Reed64acf4f2019-08-01 15:35:20 -0400357 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
358 matrix, quality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500359 if (!state) {
360 return false;
361 }
362
363 const SkPixmap& pm = state->pixmap();
364 matrix = state->invMatrix();
365 quality = state->quality();
366 auto info = pm.info();
367
Mike Kleine8de0242018-03-10 12:37:11 -0500368 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400369
370 if (updater) {
Mike Reed8845c372019-12-19 13:22:08 -0500371 updater->append_matrix_stage(p);
Mike Reed9318a6c2019-08-16 16:16:25 -0400372 } else {
373 // When the matrix is just an integer translate, bilerp == nearest neighbor.
374 if (quality == kLow_SkFilterQuality &&
375 matrix.getType() <= SkMatrix::kTranslate_Mask &&
376 matrix.getTranslateX() == (int)matrix.getTranslateX() &&
377 matrix.getTranslateY() == (int)matrix.getTranslateY()) {
378 quality = kNone_SkFilterQuality;
379 }
380
381 // See skia:4649 and the GM image_scale_aligned.
382 if (quality == kNone_SkFilterQuality) {
383 if (matrix.getScaleX() >= 0) {
384 matrix.setTranslateX(nextafterf(matrix.getTranslateX(),
385 floorf(matrix.getTranslateX())));
386 }
387 if (matrix.getScaleY() >= 0) {
388 matrix.setTranslateY(nextafterf(matrix.getTranslateY(),
389 floorf(matrix.getTranslateY())));
390 }
391 }
392 p->append_matrix(alloc, matrix);
393 }
Mike Klein06a65e22016-11-17 12:39:09 -0500394
Mike Kleinb11ab572018-10-24 06:42:14 -0400395 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500396 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400397 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400398 gather->width = pm.width();
399 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400400
Mike Kleinb11ab572018-10-24 06:42:14 -0400401 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
402 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400403 limit_x->scale = pm.width();
404 limit_x->invScale = 1.0f / pm.width();
405 limit_y->scale = pm.height();
406 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400407
Mike Kleinb11ab572018-10-24 06:42:14 -0400408 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400409 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
410 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400411 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500412 decal_ctx->limit_x = limit_x->scale;
413 decal_ctx->limit_y = limit_y->scale;
414 }
415
Mike Reed9318a6c2019-08-16 16:16:25 -0400416#if 0 // TODO: when we support kMedium
417 if (updator && (quality == kMedium_SkFilterQuality)) {
418 // if we change levels in mipmap, we need to update the scales (and invScales)
419 updator->fGather = gather;
420 updator->fLimitX = limit_x;
421 updator->fLimitY = limit_y;
422 updator->fDecal = decal_ctx;
423 }
424#endif
425
Mike Kleinb04c3522016-11-28 11:55:58 -0500426 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500427 if (decal_x_and_y) {
428 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
429 } else {
430 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400431 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
432 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
433 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
434 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500435 }
436 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400437 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
438 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
439 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
440 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500441 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500442 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500443
Mike Kleinac568a92018-01-25 09:09:32 -0500444 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500445 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500446 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400447 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
448 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500449 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
450 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400451 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
452 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
453 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500454 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
455 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400456 case kR16G16B16A16_unorm_SkColorType:
457 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600458 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500459 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400460 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500461
Mike Kleinb1df5e52018-10-17 17:06:03 -0400462 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
463 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400464
Mike Kleinac568a92018-01-25 09:09:32 -0500465 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
466 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400467
Mike Kleinac568a92018-01-25 09:09:32 -0500468 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
469 p->append(SkRasterPipeline::force_opaque ); break;
470
Mike Klein1a3eb522018-10-18 10:11:00 -0400471 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
472 p->append(SkRasterPipeline::swap_rb ); break;
473
Mike Kleinb70990e2019-02-28 10:03:27 -0600474 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500475 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500476 if (decal_ctx) {
477 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
478 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500479 };
480
Mike Klein1fa9c432017-12-11 09:59:47 -0500481 auto append_misc = [&] {
Mike Klein059e0432020-01-02 16:32:38 -0600482 SkColorSpace* cs = info.colorSpace();
483 SkAlphaType at = info.alphaType();
484
485 // Color for A8 images comes from the paint. TODO: all alpha images? none?
Mike Klein1fa9c432017-12-11 09:59:47 -0500486 if (info.colorType() == kAlpha_8_SkColorType) {
Mike Kleinbe569492018-09-14 09:34:21 -0400487 p->append_set_rgb(alloc, rec.fPaint.getColor4f());
Mike Klein059e0432020-01-02 16:32:38 -0600488 cs = sk_srgb_singleton();
489 at = kUnpremul_SkAlphaType;
Mike Klein47cf0482018-02-09 18:57:54 +0000490 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000491
Mike Klein059e0432020-01-02 16:32:38 -0600492 // This is an inessential optimization... it's logically safe to set this to false.
493 // But if...
494 // - we know the image is definitely normalized, and
495 // - we're doing some color space conversion, and
496 // - sRGB curves are involved,
497 // then we can use slightly faster math that doesn't work well outside [0,1].
498 bool src_is_normalized = SkColorTypeIsNormalized(info.colorType());
499
500 // Bicubic filtering naturally produces out of range values on both sides of [0,1].
Mike Klein250138d2020-01-02 09:39:26 -0600501 if (quality == kHigh_SkFilterQuality) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500502 p->append(SkRasterPipeline::clamp_0);
Mike Klein059e0432020-01-02 16:32:38 -0600503 p->append(at == kUnpremul_SkAlphaType || fClampAsIfUnpremul
504 ? SkRasterPipeline::clamp_1
505 : SkRasterPipeline::clamp_a);
506 src_is_normalized = true;
Mike Klein1fa9c432017-12-11 09:59:47 -0500507 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000508
Mike Klein059e0432020-01-02 16:32:38 -0600509 // 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);
Mike Kleinb82edcc2018-07-10 18:25:03 +0000513
Mike Klein1fa9c432017-12-11 09:59:47 -0500514 return true;
515 };
516
Mike Reed9318a6c2019-08-16 16:16:25 -0400517 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400518 auto ct = info.colorType();
519 if (true
520 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
521 && quality == kLow_SkFilterQuality
Mike Reedfae8fce2019-04-03 10:27:45 -0400522 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500523
524 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400525 if (ct == kBGRA_8888_SkColorType) {
526 p->append(SkRasterPipeline::swap_rb);
527 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500528 return append_misc();
529 }
Mike Reed78eedba2019-07-31 16:39:15 -0400530 if (true
Mike Klein01005622019-08-13 12:22:17 -0400531 && (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
Mike Reed78eedba2019-07-31 16:39:15 -0400547 && (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 }
Mike Klein01005622019-08-13 12:22:17 -0400557 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 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500573
Mike Kleinb11ab572018-10-24 06:42:14 -0400574 SkRasterPipeline_SamplerCtx* sampler = nullptr;
Mike Klein0a904492017-04-12 12:52:48 -0400575 if (quality != kNone_SkFilterQuality) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400576 sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400577 }
578
Mike Kleinb0b17d12016-12-09 16:25:44 -0500579 auto sample = [&](SkRasterPipeline::StockStage setup_x,
580 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400581 p->append(setup_x, sampler);
582 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500583 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400584 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500585 };
586
Mike Kleinf7f883b2016-11-21 15:09:45 -0500587 if (quality == kNone_SkFilterQuality) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500588 append_tiling_and_gather();
Mike Kleinb0b17d12016-12-09 16:25:44 -0500589 } else if (quality == kLow_SkFilterQuality) {
Mike Klein0a904492017-04-12 12:52:48 -0400590 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500591
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);
Mike Klein1fa9c432017-12-11 09:59:47 -0500598
Mike Klein46e66a22016-11-21 16:19:34 -0500599 } else {
Mike Klein250138d2020-01-02 09:39:26 -0600600 SkASSERT(quality == kHigh_SkFilterQuality);
Mike Klein0a904492017-04-12 12:52:48 -0400601 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500602
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
Mike Kleinb04c3522016-11-28 11:55:58 -0500623 p->append(SkRasterPipeline::move_dst_src);
Mike Klein06a65e22016-11-17 12:39:09 -0500624 }
625
Mike Klein1fa9c432017-12-11 09:59:47 -0500626 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500627}
Mike Reed9318a6c2019-08-16 16:16:25 -0400628
629bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
630 return this->doStages(rec, nullptr);
631}
632
633SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
Mike Reed8845c372019-12-19 13:22:08 -0500634 bool usePersp = rec.fCTM.hasPerspective();
635 auto updater = rec.fAlloc->make<SkImageStageUpdater>(this, usePersp);
Mike Reed9318a6c2019-08-16 16:16:25 -0400636 return this->doStages(rec, updater) ? updater : nullptr;
637}
638
Mike Kleinf6a715b2019-12-30 15:24:18 -0600639bool SkImageShader::onProgram(skvm::Builder* p,
640 const SkMatrix& ctm, const SkMatrix* localM,
641 SkFilterQuality quality, SkColorSpace* dstCS,
642 skvm::Uniforms* uniforms,
643 skvm::F32 x, skvm::F32 y,
644 skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const {
645 SkMatrix inv;
646 if (!this->computeTotalInverse(ctm, localM, &inv)) {
647 return false;
648 }
649
650 SkBitmapController::State state{as_IB(fImage.get()), inv, quality};
651 const SkPixmap& pm = state.pixmap();
652 if (!pm.addr()) {
653 return false;
654 }
655 inv = state.invMatrix();
656 quality = state.quality();
657
658 return false;
659}
660