blob: 3f3cd0ceb28d3b6ac194196b06623e1c6c9595a8 [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"
179#include "src/gpu/effects/generated/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 Salomon7a538b12019-11-21 22:03:09 -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 Salomon7a538b12019-11-21 22:03:09 -0500247 inner = GrBicubicEffect::Make(std::move(proxy), lmInverse, wrapModes, domainX, domainY,
248 kDir, srcAlphaType);
reed856e9d92015-09-30 12:21:45 -0700249 } else {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500250 if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400251 SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(proxy->dimensions()),
252 domainX, domainY);
Brian Salomon7a538b12019-11-21 22:03:09 -0500253 inner = GrTextureDomainEffect::Make(std::move(proxy), srcAlphaType, lmInverse, domain,
Michael Ludwig170de012019-11-15 21:55:18 +0000254 domainX, domainY, samplerState);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500255 } else {
Brian Salomon7a538b12019-11-21 22:03:09 -0500256 inner = GrSimpleTextureEffect::Make(std::move(proxy), srcAlphaType, lmInverse,
Greg Danielc594e622019-10-15 14:01:49 -0400257 samplerState);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500258 }
reed856e9d92015-09-30 12:21:45 -0700259 }
Brian Salomon7a538b12019-11-21 22:03:09 -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:
303 const SkImageShader* fShader;
304
305 float fMatrixStorage[6];
306
307#if 0 // TODO: when we support mipmaps
308 SkRasterPipeline_GatherCtx* fGather;
309 SkRasterPipeline_TileCtx* fLimitX;
310 SkRasterPipeline_TileCtx* fLimitY;
311 SkRasterPipeline_DecalTileCtx* fDecal;
312#endif
313
314 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
315 SkMatrix matrix;
316 return fShader->computeTotalInverse(ctm, localM, &matrix) &&
317 matrix.asAffine(fMatrixStorage);
318 }
319};
320
321bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
322 if (updater &&
323 (rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality ||
324 rec.fCTM.hasPerspective()))
325 {
326 // TODO: handle these cases
327 // medium: recall RequestBitmap and update width/height accordingly
328 // perspt: store 9 floats and use persp stage
329 return false;
330 }
331
Mike Reed1d8c42e2017-08-29 14:58:19 -0400332 SkRasterPipeline* p = rec.fPipeline;
333 SkArenaAlloc* alloc = rec.fAlloc;
Mike Reed9318a6c2019-08-16 16:16:25 -0400334 auto quality = rec.fPaint.getFilterQuality();
Mike Reed1d8c42e2017-08-29 14:58:19 -0400335
Florin Malita7558e4d2018-02-07 10:05:53 -0500336 SkMatrix matrix;
337 if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500338 return false;
339 }
Mike Klein06a65e22016-11-17 12:39:09 -0500340
Mike Reed64acf4f2019-08-01 15:35:20 -0400341 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
342 matrix, quality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500343 if (!state) {
344 return false;
345 }
346
347 const SkPixmap& pm = state->pixmap();
348 matrix = state->invMatrix();
349 quality = state->quality();
350 auto info = pm.info();
351
Mike Kleine8de0242018-03-10 12:37:11 -0500352 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400353
354 if (updater) {
355 p->append(SkRasterPipeline::matrix_2x3, updater->fMatrixStorage);
356 } else {
357 // When the matrix is just an integer translate, bilerp == nearest neighbor.
358 if (quality == kLow_SkFilterQuality &&
359 matrix.getType() <= SkMatrix::kTranslate_Mask &&
360 matrix.getTranslateX() == (int)matrix.getTranslateX() &&
361 matrix.getTranslateY() == (int)matrix.getTranslateY()) {
362 quality = kNone_SkFilterQuality;
363 }
364
365 // See skia:4649 and the GM image_scale_aligned.
366 if (quality == kNone_SkFilterQuality) {
367 if (matrix.getScaleX() >= 0) {
368 matrix.setTranslateX(nextafterf(matrix.getTranslateX(),
369 floorf(matrix.getTranslateX())));
370 }
371 if (matrix.getScaleY() >= 0) {
372 matrix.setTranslateY(nextafterf(matrix.getTranslateY(),
373 floorf(matrix.getTranslateY())));
374 }
375 }
376 p->append_matrix(alloc, matrix);
377 }
Mike Klein06a65e22016-11-17 12:39:09 -0500378
Mike Kleinb11ab572018-10-24 06:42:14 -0400379 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500380 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400381 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400382 gather->width = pm.width();
383 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400384
Mike Kleinb11ab572018-10-24 06:42:14 -0400385 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
386 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400387 limit_x->scale = pm.width();
388 limit_x->invScale = 1.0f / pm.width();
389 limit_y->scale = pm.height();
390 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400391
Mike Kleinb11ab572018-10-24 06:42:14 -0400392 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400393 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
394 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400395 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500396 decal_ctx->limit_x = limit_x->scale;
397 decal_ctx->limit_y = limit_y->scale;
398 }
399
Mike Reed9318a6c2019-08-16 16:16:25 -0400400#if 0 // TODO: when we support kMedium
401 if (updator && (quality == kMedium_SkFilterQuality)) {
402 // if we change levels in mipmap, we need to update the scales (and invScales)
403 updator->fGather = gather;
404 updator->fLimitX = limit_x;
405 updator->fLimitY = limit_y;
406 updator->fDecal = decal_ctx;
407 }
408#endif
409
Mike Kleinb04c3522016-11-28 11:55:58 -0500410 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500411 if (decal_x_and_y) {
412 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
413 } else {
414 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400415 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
416 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
417 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
418 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500419 }
420 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400421 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
422 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
423 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
424 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500425 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500426 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500427
Mike Kleinac568a92018-01-25 09:09:32 -0500428 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500429 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500430 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400431 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
432 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500433 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
434 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400435 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
436 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
437 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500438 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
439 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400440 case kR16G16B16A16_unorm_SkColorType:
441 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600442 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500443 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400444 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500445
Mike Kleinb1df5e52018-10-17 17:06:03 -0400446 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
447 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400448
Mike Kleinac568a92018-01-25 09:09:32 -0500449 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
450 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400451
Mike Kleinac568a92018-01-25 09:09:32 -0500452 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
453 p->append(SkRasterPipeline::force_opaque ); break;
454
Mike Klein1a3eb522018-10-18 10:11:00 -0400455 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
456 p->append(SkRasterPipeline::swap_rb ); break;
457
Mike Kleinb70990e2019-02-28 10:03:27 -0600458 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500459 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500460 if (decal_ctx) {
461 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
462 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500463 };
464
Mike Klein1fa9c432017-12-11 09:59:47 -0500465 auto append_misc = [&] {
Mike Kleinb82edcc2018-07-10 18:25:03 +0000466 // TODO: if ref.fDstCS isn't null, we'll premul here then immediately unpremul
467 // to do the color space transformation. Might be possible to streamline.
Mike Klein1fa9c432017-12-11 09:59:47 -0500468 if (info.colorType() == kAlpha_8_SkColorType) {
Mike Kleinb82edcc2018-07-10 18:25:03 +0000469 // The color for A8 images comes from the (sRGB) paint color.
Mike Kleinbe569492018-09-14 09:34:21 -0400470 p->append_set_rgb(alloc, rec.fPaint.getColor4f());
Mike Kleinb82edcc2018-07-10 18:25:03 +0000471 p->append(SkRasterPipeline::premul);
472 } else if (info.alphaType() == kUnpremul_SkAlphaType) {
473 // Convert unpremul images to premul before we carry on with the rest of the pipeline.
Mike Klein47cf0482018-02-09 18:57:54 +0000474 p->append(SkRasterPipeline::premul);
475 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000476
Mike Klein1fa9c432017-12-11 09:59:47 -0500477 if (quality > kLow_SkFilterQuality) {
478 // Bicubic filtering naturally produces out of range values on both sides.
479 p->append(SkRasterPipeline::clamp_0);
Mike Klein1f313092018-01-03 10:30:21 -0500480 p->append(fClampAsIfUnpremul ? SkRasterPipeline::clamp_1
481 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500482 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000483
484 if (rec.fDstCS) {
485 // If color managed, convert from premul source all the way to premul dst color space.
486 auto srcCS = info.colorSpace();
487 if (!srcCS || info.colorType() == kAlpha_8_SkColorType) {
488 // We treat untagged images as sRGB.
489 // A8 images get their r,g,b from the paint color, so they're also sRGB.
Mike Kleine28a6b52018-07-25 13:05:17 -0400490 srcCS = sk_srgb_singleton();
Mike Kleinb82edcc2018-07-10 18:25:03 +0000491 }
Mike Klein8f3d36c2018-08-14 10:28:05 -0400492 alloc->make<SkColorSpaceXformSteps>(srcCS , kPremul_SkAlphaType,
493 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinc23d1ab2018-10-29 09:39:52 -0400494 ->apply(p, info.colorType());
Mike Kleinb82edcc2018-07-10 18:25:03 +0000495 }
496
Mike Klein1fa9c432017-12-11 09:59:47 -0500497 return true;
498 };
499
Mike Reed9318a6c2019-08-16 16:16:25 -0400500 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400501 auto ct = info.colorType();
502 if (true
503 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
504 && quality == kLow_SkFilterQuality
Mike Reedfae8fce2019-04-03 10:27:45 -0400505 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500506
507 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400508 if (ct == kBGRA_8888_SkColorType) {
509 p->append(SkRasterPipeline::swap_rb);
510 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500511 return append_misc();
512 }
Mike Reed78eedba2019-07-31 16:39:15 -0400513 if (true
Mike Klein01005622019-08-13 12:22:17 -0400514 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
515 && quality == kLow_SkFilterQuality
516 && fTileModeX != SkTileMode::kDecal // TODO decal too?
517 && fTileModeY != SkTileMode::kDecal) {
518
519 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
520 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
521 ctx->ct = ct;
522 ctx->tileX = fTileModeX;
523 ctx->tileY = fTileModeY;
524 ctx->invWidth = 1.0f / ctx->width;
525 ctx->invHeight = 1.0f / ctx->height;
526 p->append(SkRasterPipeline::bilinear, ctx);
527 return append_misc();
528 }
529 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400530 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
531 && quality == kHigh_SkFilterQuality
532 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
533
534 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
535 if (ct == kBGRA_8888_SkColorType) {
536 p->append(SkRasterPipeline::swap_rb);
537 }
538 return append_misc();
539 }
Mike Klein01005622019-08-13 12:22:17 -0400540 if (true
541 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
542 && quality == kHigh_SkFilterQuality
543 && fTileModeX != SkTileMode::kDecal // TODO decal too?
544 && fTileModeY != SkTileMode::kDecal) {
545
546 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
547 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
548 ctx->ct = ct;
549 ctx->tileX = fTileModeX;
550 ctx->tileY = fTileModeY;
551 ctx->invWidth = 1.0f / ctx->width;
552 ctx->invHeight = 1.0f / ctx->height;
553 p->append(SkRasterPipeline::bicubic, ctx);
554 return append_misc();
555 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500556
Mike Kleinb11ab572018-10-24 06:42:14 -0400557 SkRasterPipeline_SamplerCtx* sampler = nullptr;
Mike Klein0a904492017-04-12 12:52:48 -0400558 if (quality != kNone_SkFilterQuality) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400559 sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400560 }
561
Mike Kleinb0b17d12016-12-09 16:25:44 -0500562 auto sample = [&](SkRasterPipeline::StockStage setup_x,
563 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400564 p->append(setup_x, sampler);
565 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500566 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400567 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500568 };
569
Mike Kleinf7f883b2016-11-21 15:09:45 -0500570 if (quality == kNone_SkFilterQuality) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500571 append_tiling_and_gather();
Mike Kleinb0b17d12016-12-09 16:25:44 -0500572 } else if (quality == kLow_SkFilterQuality) {
Mike Klein0a904492017-04-12 12:52:48 -0400573 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500574
575 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
576 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
577 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
578 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
579
580 p->append(SkRasterPipeline::move_dst_src);
Mike Klein1fa9c432017-12-11 09:59:47 -0500581
Mike Klein46e66a22016-11-21 16:19:34 -0500582 } else {
Mike Klein0a904492017-04-12 12:52:48 -0400583 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500584
585 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
586 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
587 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
588 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
589
590 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
591 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
592 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
593 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
594
595 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
596 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
597 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
598 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
599
600 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
601 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
602 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
603 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
604
Mike Kleinb04c3522016-11-28 11:55:58 -0500605 p->append(SkRasterPipeline::move_dst_src);
Mike Klein06a65e22016-11-17 12:39:09 -0500606 }
607
Mike Klein1fa9c432017-12-11 09:59:47 -0500608 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500609}
Mike Reed9318a6c2019-08-16 16:16:25 -0400610
611bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
612 return this->doStages(rec, nullptr);
613}
614
615SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
616 auto updater = rec.fAlloc->make<SkImageStageUpdater>();
617 updater->fShader = this;
618 return this->doStages(rec, updater) ? updater : nullptr;
619}
620