blob: 028027d66316d53b681ba1282e43260b86f10070 [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"
12#include "src/core/SkRasterPipeline.h"
13#include "src/core/SkReadBuffer.h"
14#include "src/core/SkWriteBuffer.h"
15#include "src/image/SkImage_Base.h"
16#include "src/shaders/SkBitmapProcShader.h"
17#include "src/shaders/SkEmptyShader.h"
18#include "src/shaders/SkImageShader.h"
reed856e9d92015-09-30 12:21:45 -070019
Mike Reed587d0822017-06-23 16:49:12 -040020/**
21 * We are faster in clamp, so always use that tiling when we can.
22 */
Mike Reedfae8fce2019-04-03 10:27:45 -040023static SkTileMode optimize(SkTileMode tm, int dimension) {
Mike Reed587d0822017-06-23 16:49:12 -040024 SkASSERT(dimension > 0);
Mike Reed2e3c9552017-06-23 21:33:58 -040025#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
26 // need to update frameworks/base/libs/hwui/tests/unit/SkiaBehaviorTests.cpp:55 to allow
27 // for transforming to clamp.
Mike Reedfae8fce2019-04-03 10:27:45 -040028 return tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040029#else
Mike Reedfae8fce2019-04-03 10:27:45 -040030 return dimension == 1 ? SkTileMode::kClamp : tm;
Mike Reed2e3c9552017-06-23 21:33:58 -040031#endif
Mike Reed587d0822017-06-23 16:49:12 -040032}
33
Mike Klein1f313092018-01-03 10:30:21 -050034SkImageShader::SkImageShader(sk_sp<SkImage> img,
Mike Reede25b4472019-04-02 17:49:12 -040035 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -050036 const SkMatrix* localMatrix,
37 bool clampAsIfUnpremul)
38 : INHERITED(localMatrix)
reed6b2d7ac2016-08-11 06:42:26 -070039 , fImage(std::move(img))
Mike Reed587d0822017-06-23 16:49:12 -040040 , fTileModeX(optimize(tmx, fImage->width()))
41 , fTileModeY(optimize(tmy, fImage->height()))
Mike Klein1f313092018-01-03 10:30:21 -050042 , fClampAsIfUnpremul(clampAsIfUnpremul)
reed856e9d92015-09-30 12:21:45 -070043{}
44
Mike Klein1f313092018-01-03 10:30:21 -050045// fClampAsIfUnpremul is always false when constructed through public APIs,
46// so there's no need to read or write it here.
47
reed60c9b582016-04-03 09:11:13 -070048sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
Mike Reede25b4472019-04-02 17:49:12 -040049 auto tmx = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
50 auto tmy = buffer.read32LE<SkTileMode>(SkTileMode::kLastTileMode);
Mike Klein1f313092018-01-03 10:30:21 -050051 SkMatrix localMatrix;
52 buffer.readMatrix(&localMatrix);
reeda9ca05c2016-08-11 03:55:15 -070053 sk_sp<SkImage> img = buffer.readImage();
reed856e9d92015-09-30 12:21:45 -070054 if (!img) {
55 return nullptr;
56 }
Mike Reede25b4472019-04-02 17:49:12 -040057 return SkImageShader::Make(std::move(img), tmx, tmy, &localMatrix);
reed856e9d92015-09-30 12:21:45 -070058}
59
60void SkImageShader::flatten(SkWriteBuffer& buffer) const {
Mike Reedfae8fce2019-04-03 10:27:45 -040061 buffer.writeUInt((unsigned)fTileModeX);
62 buffer.writeUInt((unsigned)fTileModeY);
reed856e9d92015-09-30 12:21:45 -070063 buffer.writeMatrix(this->getLocalMatrix());
reed6b2d7ac2016-08-11 06:42:26 -070064 buffer.writeImage(fImage.get());
Mike Klein1f313092018-01-03 10:30:21 -050065 SkASSERT(fClampAsIfUnpremul == false);
reed856e9d92015-09-30 12:21:45 -070066}
67
68bool SkImageShader::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -040069 return fImage->isOpaque() &&
70 fTileModeX != SkTileMode::kDecal && fTileModeY != SkTileMode::kDecal;
reed856e9d92015-09-30 12:21:45 -070071}
72
Mike Reede92aae62018-10-17 10:21:51 -040073#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malitaaf2769d2018-04-04 13:46:35 -040074static bool legacy_shader_can_handle(const SkMatrix& inv) {
75 if (!inv.isScaleTranslate()) {
Mike Reeda12c4192018-02-01 16:34:03 -050076 return false;
77 }
78
79 // legacy code uses SkFixed 32.32, so ensure the inverse doesn't map device coordinates
80 // out of range.
81 const SkScalar max_dev_coord = 32767.0f;
82 SkRect src;
83 SkAssertResult(inv.mapRect(&src, SkRect::MakeWH(max_dev_coord, max_dev_coord)));
84
Mike Reed1eb5ca42018-03-08 14:20:52 -050085 // take 1/4 of max signed 32bits so we have room to subtract local values
86 const SkScalar max_fixed32dot32 = SK_MaxS32 * 0.25f;
Mike Reeda12c4192018-02-01 16:34:03 -050087 if (!SkRect::MakeLTRB(-max_fixed32dot32, -max_fixed32dot32,
88 max_fixed32dot32, max_fixed32dot32).contains(src)) {
89 return false;
90 }
91
92 // legacy shader impl should be able to handle these matrices
93 return true;
94}
95
Florin Malita4aed1382017-05-25 10:38:07 -040096SkShaderBase::Context* SkImageShader::onMakeContext(const ContextRec& rec,
97 SkArenaAlloc* alloc) const {
Brian Osman0e189372018-10-19 11:58:29 -040098 if (fImage->alphaType() == kUnpremul_SkAlphaType) {
Florin Malitaaf2769d2018-04-04 13:46:35 -040099 return nullptr;
100 }
Brian Osmanb70fd912018-10-22 16:10:44 -0400101 if (fImage->colorType() != kN32_SkColorType) {
102 return nullptr;
103 }
Florin Malitaaf2769d2018-04-04 13:46:35 -0400104 if (fTileModeX != fTileModeY) {
105 return nullptr;
106 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400107 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400108 return nullptr;
109 }
110
Mike Kleindac694d2018-12-18 10:13:52 -0500111 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer,
Mike Klein67761eb2018-12-18 10:16:53 -0500112 // so it can't handle bitmaps larger than 65535.
Mike Kleindac694d2018-12-18 10:13:52 -0500113 //
Mike Klein67761eb2018-12-18 10:16:53 -0500114 // We back off another bit to 32767 to make small amounts of
115 // intermediate math safe, e.g. in
116 //
117 // SkFixed fx = ...;
118 // fx = tile(fx + SK_Fixed1);
119 //
120 // we want to make sure (fx + SK_Fixed1) never overflows.
121 if (fImage-> width() > 32767 ||
122 fImage->height() > 32767) {
Mike Kleindac694d2018-12-18 10:13:52 -0500123 return nullptr;
124 }
125
Florin Malitaaf2769d2018-04-04 13:46:35 -0400126 SkMatrix inv;
127 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &inv) ||
128 !legacy_shader_can_handle(inv)) {
129 return nullptr;
130 }
131
Mike Reed011d1662019-02-28 17:19:25 -0500132 if (!rec.isLegacyCompatible(fImage->colorSpace())) {
133 return nullptr;
134 }
135
reed320a40d2016-08-02 06:12:06 -0700136 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
Mike Reed64acf4f2019-08-01 15:35:20 -0400137 as_IB(fImage.get()), rec, alloc);
reed856e9d92015-09-30 12:21:45 -0700138}
Mike Reede92aae62018-10-17 10:21:51 -0400139#endif
reed856e9d92015-09-30 12:21:45 -0700140
Mike Reedfae8fce2019-04-03 10:27:45 -0400141SkImage* SkImageShader::onIsAImage(SkMatrix* texM, SkTileMode xy[]) const {
reedf1ac1822016-08-01 11:24:14 -0700142 if (texM) {
143 *texM = this->getLocalMatrix();
144 }
145 if (xy) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400146 xy[0] = fTileModeX;
147 xy[1] = fTileModeY;
reedf1ac1822016-08-01 11:24:14 -0700148 }
149 return const_cast<SkImage*>(fImage.get());
150}
151
Mike Klein1f313092018-01-03 10:30:21 -0500152sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image,
Mike Reede25b4472019-04-02 17:49:12 -0400153 SkTileMode tmx, SkTileMode tmy,
Mike Klein1f313092018-01-03 10:30:21 -0500154 const SkMatrix* localMatrix,
155 bool clampAsIfUnpremul) {
Mike Kleindac694d2018-12-18 10:13:52 -0500156 if (!image) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000157 return sk_make_sp<SkEmptyShader>();
reed320a40d2016-08-02 06:12:06 -0700158 }
Mike Reede25b4472019-04-02 17:49:12 -0400159 return sk_sp<SkShader>{ new SkImageShader(image, tmx, tmy, localMatrix, clampAsIfUnpremul) };
reed856e9d92015-09-30 12:21:45 -0700160}
161
reed856e9d92015-09-30 12:21:45 -0700162///////////////////////////////////////////////////////////////////////////////////////////////////
163
164#if SK_SUPPORT_GPU
165
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500166#include "include/private/GrRecordingContext.h"
167#include "src/gpu/GrCaps.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400168#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500169#include "src/gpu/GrRecordingContextPriv.h"
170#include "src/gpu/SkGr.h"
171#include "src/gpu/effects/GrBicubicEffect.h"
172#include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
reed856e9d92015-09-30 12:21:45 -0700173
Mike Reedfae8fce2019-04-03 10:27:45 -0400174static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkTileMode tileMode) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400175 switch (tileMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400176 case SkTileMode::kClamp:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400177 return GrSamplerState::WrapMode::kClamp;
Mike Reedfae8fce2019-04-03 10:27:45 -0400178 case SkTileMode::kRepeat:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400179 return GrSamplerState::WrapMode::kRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400180 case SkTileMode::kMirror:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400181 return GrSamplerState::WrapMode::kMirrorRepeat;
Mike Reedfae8fce2019-04-03 10:27:45 -0400182 case SkTileMode::kDecal:
Michael Ludwigf23a1522018-12-10 11:36:13 -0500183 return GrSamplerState::WrapMode::kClampToBorder;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400184 }
185 SK_ABORT("Unknown tile mode.");
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400186}
187
Brian Salomonaff329b2017-08-11 09:40:37 -0400188std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500189 const GrFPArgs& args) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -0400190 const auto lm = this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix);
reed856e9d92015-09-30 12:21:45 -0700191 SkMatrix lmInverse;
Florin Malitac6c5ead2018-04-11 15:33:40 -0400192 if (!lm->invert(&lmInverse)) {
reed856e9d92015-09-30 12:21:45 -0700193 return nullptr;
194 }
reed856e9d92015-09-30 12:21:45 -0700195
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400196 GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX),
197 tile_mode_to_wrap_mode(fTileModeY)};
Michael Ludwigbe315a22018-12-17 09:50:51 -0500198
199 // If either domainX or domainY are un-ignored, a texture domain effect has to be used to
200 // implement the decal mode (while leaving non-decal axes alone). The wrap mode originally
201 // clamp-to-border is reset to clamp since the hw cannot implement it directly.
202 GrTextureDomain::Mode domainX = GrTextureDomain::kIgnore_Mode;
203 GrTextureDomain::Mode domainY = GrTextureDomain::kIgnore_Mode;
Robert Phillips9da87e02019-02-04 13:26:26 -0500204 if (!args.fContext->priv().caps()->clampToBorderSupport()) {
Michael Ludwigf23a1522018-12-10 11:36:13 -0500205 if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500206 domainX = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500207 wrapModes[0] = GrSamplerState::WrapMode::kClamp;
208 }
209 if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500210 domainY = GrTextureDomain::kDecal_Mode;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500211 wrapModes[1] = GrSamplerState::WrapMode::kClamp;
212 }
213 }
reed856e9d92015-09-30 12:21:45 -0700214
215 // Must set wrap and filter on the sampler before requesting a texture. In two places below
216 // we check the matrix scale factors to determine how to interpret the filter quality setting.
217 // This completely ignores the complexity of the drawVertices case where explicit local coords
218 // are provided by the caller.
219 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400220 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600221 fImage->width(), fImage->height(), args.fFilterQuality, *args.fViewMatrix, *lm,
Robert Phillips9da87e02019-02-04 13:26:26 -0500222 args.fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 GrSamplerState samplerState(wrapModes, textureFilterMode);
Robert Phillips67c18d62017-01-20 12:44:06 -0500224 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Osmane7fd8c32018-10-19 13:30:39 -0400225 sk_sp<GrTextureProxy> proxy(as_IB(fImage)->asTextureProxyRef(args.fContext, samplerState,
Brian Osman6064e1c2018-10-19 14:27:54 -0400226 scaleAdjust));
Robert Phillipsb726d582017-03-09 16:36:32 -0500227 if (!proxy) {
reed856e9d92015-09-30 12:21:45 -0700228 return nullptr;
229 }
230
Greg Danielc594e622019-10-15 14:01:49 -0400231 GrColorType srcColorType = SkColorTypeToGrColorType(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500232
Robert Phillips67c18d62017-01-20 12:44:06 -0500233 lmInverse.postScale(scaleAdjust[0], scaleAdjust[1]);
234
Brian Salomonaff329b2017-08-11 09:40:37 -0400235 std::unique_ptr<GrFragmentProcessor> inner;
reed856e9d92015-09-30 12:21:45 -0700236 if (doBicubic) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500237 // domainX and domainY will properly apply the decal effect with the texture domain used in
238 // the bicubic filter if clamp to border was unsupported in hardware
Brian Salomona86fc7a2019-05-28 20:42:58 -0400239 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Greg Danielc594e622019-10-15 14:01:49 -0400240 inner = GrBicubicEffect::Make(std::move(proxy), srcColorType, lmInverse, wrapModes, domainX,
241 domainY, kDir, fImage->alphaType());
reed856e9d92015-09-30 12:21:45 -0700242 } else {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500243 if (domainX != GrTextureDomain::kIgnore_Mode || domainY != GrTextureDomain::kIgnore_Mode) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400244 SkRect domain = GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(proxy->dimensions()),
245 domainX, domainY);
Greg Danielc594e622019-10-15 14:01:49 -0400246 inner = GrTextureDomainEffect::Make(std::move(proxy), srcColorType, lmInverse, domain,
Michael Ludwigbe315a22018-12-17 09:50:51 -0500247 domainX, domainY, samplerState);
248 } else {
Greg Danielc594e622019-10-15 14:01:49 -0400249 inner = GrSimpleTextureEffect::Make(std::move(proxy), srcColorType, lmInverse,
250 samplerState);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500251 }
reed856e9d92015-09-30 12:21:45 -0700252 }
Brian Osman6064e1c2018-10-19 14:27:54 -0400253 inner = GrColorSpaceXformEffect::Make(std::move(inner), fImage->colorSpace(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400254 fImage->alphaType(), args.fDstColorInfo->colorSpace());
Greg Danielc594e622019-10-15 14:01:49 -0400255
256 bool isAlphaOnly = SkColorTypeIsAlphaOnly(fImage->colorType());
Robert Phillipsb726d582017-03-09 16:36:32 -0500257 if (isAlphaOnly) {
bungeman06ca8ec2016-06-09 08:01:03 -0700258 return inner;
Brian Salomonc0d79e52019-04-10 15:02:11 -0400259 } else if (args.fInputColorIsOpaque) {
260 return GrFragmentProcessor::OverrideInput(std::move(inner), SK_PMColor4fWHITE, false);
reed856e9d92015-09-30 12:21:45 -0700261 }
Mike Reed28eaed22018-02-01 11:24:53 -0500262 return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner));
reed856e9d92015-09-30 12:21:45 -0700263}
264
265#endif
reed320a40d2016-08-02 06:12:06 -0700266
267///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500268#include "src/core/SkImagePriv.h"
reed320a40d2016-08-02 06:12:06 -0700269
Mike Reede25b4472019-04-02 17:49:12 -0400270sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode tmx, SkTileMode tmy,
271 const SkMatrix* localMatrix, SkCopyPixelsMode cpm) {
Herb Derbybfdc87a2017-02-14 15:06:23 +0000272 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm),
273 tmx, tmy, localMatrix);
reed320a40d2016-08-02 06:12:06 -0700274}
275
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400276sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
Mike Reede25b4472019-04-02 17:49:12 -0400277 SkTileMode tmx, SkTileMode tmy,
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400278 const SkMatrix* localMatrix, SkCopyPixelsMode mode) {
279 auto s = SkMakeBitmapShader(src, tmx, tmy, localMatrix, mode);
280 if (!s) {
281 return nullptr;
282 }
283 if (src.colorType() == kAlpha_8_SkColorType && paint.getShader()) {
284 // Compose the image shader with the paint's shader. Alpha images+shaders should output the
285 // texture's alpha multiplied by the shader's color. DstIn (d*sa) will achieve this with
286 // the source image and dst shader (MakeBlend takes dst first, src second).
Mike Reedc8bea7d2019-04-09 13:55:36 -0400287 s = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(s));
Michael Ludwigc47e81b2019-04-02 15:18:02 -0400288 }
289 return s;
290}
291
Brian Salomon23356442018-11-30 15:33:19 -0500292void SkShaderBase::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkImageShader); }
reed320a40d2016-08-02 06:12:06 -0700293
Mike Reed9318a6c2019-08-16 16:16:25 -0400294class SkImageStageUpdater : public SkStageUpdater {
295public:
296 const SkImageShader* fShader;
297
298 float fMatrixStorage[6];
299
300#if 0 // TODO: when we support mipmaps
301 SkRasterPipeline_GatherCtx* fGather;
302 SkRasterPipeline_TileCtx* fLimitX;
303 SkRasterPipeline_TileCtx* fLimitY;
304 SkRasterPipeline_DecalTileCtx* fDecal;
305#endif
306
307 bool update(const SkMatrix& ctm, const SkMatrix* localM) override {
308 SkMatrix matrix;
309 return fShader->computeTotalInverse(ctm, localM, &matrix) &&
310 matrix.asAffine(fMatrixStorage);
311 }
312};
313
314bool SkImageShader::doStages(const SkStageRec& rec, SkImageStageUpdater* updater) const {
315 if (updater &&
316 (rec.fPaint.getFilterQuality() == kMedium_SkFilterQuality ||
317 rec.fCTM.hasPerspective()))
318 {
319 // TODO: handle these cases
320 // medium: recall RequestBitmap and update width/height accordingly
321 // perspt: store 9 floats and use persp stage
322 return false;
323 }
324
Mike Reed1d8c42e2017-08-29 14:58:19 -0400325 SkRasterPipeline* p = rec.fPipeline;
326 SkArenaAlloc* alloc = rec.fAlloc;
Mike Reed9318a6c2019-08-16 16:16:25 -0400327 auto quality = rec.fPaint.getFilterQuality();
Mike Reed1d8c42e2017-08-29 14:58:19 -0400328
Florin Malita7558e4d2018-02-07 10:05:53 -0500329 SkMatrix matrix;
330 if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) {
Mike Klein06a65e22016-11-17 12:39:09 -0500331 return false;
332 }
Mike Klein06a65e22016-11-17 12:39:09 -0500333
Mike Reed64acf4f2019-08-01 15:35:20 -0400334 const auto* state = SkBitmapController::RequestBitmap(as_IB(fImage.get()),
335 matrix, quality, alloc);
Mike Kleinf447dee2016-11-29 16:37:12 -0500336 if (!state) {
337 return false;
338 }
339
340 const SkPixmap& pm = state->pixmap();
341 matrix = state->invMatrix();
342 quality = state->quality();
343 auto info = pm.info();
344
Mike Kleine8de0242018-03-10 12:37:11 -0500345 p->append(SkRasterPipeline::seed_shader);
Mike Reed9318a6c2019-08-16 16:16:25 -0400346
347 if (updater) {
348 p->append(SkRasterPipeline::matrix_2x3, updater->fMatrixStorage);
349 } else {
350 // When the matrix is just an integer translate, bilerp == nearest neighbor.
351 if (quality == kLow_SkFilterQuality &&
352 matrix.getType() <= SkMatrix::kTranslate_Mask &&
353 matrix.getTranslateX() == (int)matrix.getTranslateX() &&
354 matrix.getTranslateY() == (int)matrix.getTranslateY()) {
355 quality = kNone_SkFilterQuality;
356 }
357
358 // See skia:4649 and the GM image_scale_aligned.
359 if (quality == kNone_SkFilterQuality) {
360 if (matrix.getScaleX() >= 0) {
361 matrix.setTranslateX(nextafterf(matrix.getTranslateX(),
362 floorf(matrix.getTranslateX())));
363 }
364 if (matrix.getScaleY() >= 0) {
365 matrix.setTranslateY(nextafterf(matrix.getTranslateY(),
366 floorf(matrix.getTranslateY())));
367 }
368 }
369 p->append_matrix(alloc, matrix);
370 }
Mike Klein06a65e22016-11-17 12:39:09 -0500371
Mike Kleinb11ab572018-10-24 06:42:14 -0400372 auto gather = alloc->make<SkRasterPipeline_GatherCtx>();
Mike Klein1fa9c432017-12-11 09:59:47 -0500373 gather->pixels = pm.addr();
Mike Klein968af432017-07-18 16:31:55 -0400374 gather->stride = pm.rowBytesAsPixels();
Mike Kleinf3b4e162017-09-22 15:32:59 -0400375 gather->width = pm.width();
376 gather->height = pm.height();
Mike Klein0a904492017-04-12 12:52:48 -0400377
Mike Kleinb11ab572018-10-24 06:42:14 -0400378 auto limit_x = alloc->make<SkRasterPipeline_TileCtx>(),
379 limit_y = alloc->make<SkRasterPipeline_TileCtx>();
Mike Reed51e46d52017-06-23 14:21:25 -0400380 limit_x->scale = pm.width();
381 limit_x->invScale = 1.0f / pm.width();
382 limit_y->scale = pm.height();
383 limit_y->invScale = 1.0f / pm.height();
Mike Kleinfc84dc52017-05-11 15:29:31 -0400384
Mike Kleinb11ab572018-10-24 06:42:14 -0400385 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reedfae8fce2019-04-03 10:27:45 -0400386 bool decal_x_and_y = fTileModeX == SkTileMode::kDecal && fTileModeY == SkTileMode::kDecal;
387 if (fTileModeX == SkTileMode::kDecal || fTileModeY == SkTileMode::kDecal) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400388 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reeddfc0e912018-02-16 12:40:18 -0500389 decal_ctx->limit_x = limit_x->scale;
390 decal_ctx->limit_y = limit_y->scale;
391 }
392
Mike Reed9318a6c2019-08-16 16:16:25 -0400393#if 0 // TODO: when we support kMedium
394 if (updator && (quality == kMedium_SkFilterQuality)) {
395 // if we change levels in mipmap, we need to update the scales (and invScales)
396 updator->fGather = gather;
397 updator->fLimitX = limit_x;
398 updator->fLimitY = limit_y;
399 updator->fDecal = decal_ctx;
400 }
401#endif
402
Mike Kleinb04c3522016-11-28 11:55:58 -0500403 auto append_tiling_and_gather = [&] {
Mike Reeddfc0e912018-02-16 12:40:18 -0500404 if (decal_x_and_y) {
405 p->append(SkRasterPipeline::decal_x_and_y, decal_ctx);
406 } else {
407 switch (fTileModeX) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400408 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
409 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x, limit_x); break;
410 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x, limit_x); break;
411 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_x, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500412 }
413 switch (fTileModeY) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400414 case SkTileMode::kClamp: /* The gather_xxx stage will clamp for us. */ break;
415 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_y, limit_y); break;
416 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_y, limit_y); break;
417 case SkTileMode::kDecal: p->append(SkRasterPipeline::decal_y, decal_ctx); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500418 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500419 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500420
Mike Kleinac568a92018-01-25 09:09:32 -0500421 void* ctx = gather;
Mike Kleinf7f883b2016-11-21 15:09:45 -0500422 switch (info.colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500423 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400424 case kA16_unorm_SkColorType: p->append(SkRasterPipeline::gather_a16, ctx); break;
425 case kA16_float_SkColorType: p->append(SkRasterPipeline::gather_af16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500426 case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break;
427 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400428 case kR8G8_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg88, ctx); break;
429 case kR16G16_unorm_SkColorType: p->append(SkRasterPipeline::gather_rg1616, ctx); break;
430 case kR16G16_float_SkColorType: p->append(SkRasterPipeline::gather_rgf16, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500431 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break;
432 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400433 case kR16G16B16A16_unorm_SkColorType:
434 p->append(SkRasterPipeline::gather_16161616,ctx); break;
Mike Kleinb70990e2019-02-28 10:03:27 -0600435 case kRGBA_F16Norm_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500436 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break;
Mike Klein37854712018-06-26 11:43:06 -0400437 case kRGBA_F32_SkColorType: p->append(SkRasterPipeline::gather_f32, ctx); break;
Mike Kleinac568a92018-01-25 09:09:32 -0500438
Mike Kleinb1df5e52018-10-17 17:06:03 -0400439 case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx);
440 p->append(SkRasterPipeline::alpha_to_gray ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400441
Mike Kleinac568a92018-01-25 09:09:32 -0500442 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
443 p->append(SkRasterPipeline::force_opaque ); break;
Mike Klein1a3eb522018-10-18 10:11:00 -0400444
Mike Kleinac568a92018-01-25 09:09:32 -0500445 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx);
446 p->append(SkRasterPipeline::force_opaque ); break;
447
Mike Klein1a3eb522018-10-18 10:11:00 -0400448 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx);
449 p->append(SkRasterPipeline::swap_rb ); break;
450
Mike Kleinb70990e2019-02-28 10:03:27 -0600451 case kUnknown_SkColorType: SkASSERT(false);
Mike Kleinb04c3522016-11-28 11:55:58 -0500452 }
Mike Reeddfc0e912018-02-16 12:40:18 -0500453 if (decal_ctx) {
454 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
455 }
Mike Kleinf7f883b2016-11-21 15:09:45 -0500456 };
457
Mike Klein1fa9c432017-12-11 09:59:47 -0500458 auto append_misc = [&] {
Mike Kleinb82edcc2018-07-10 18:25:03 +0000459 // TODO: if ref.fDstCS isn't null, we'll premul here then immediately unpremul
460 // to do the color space transformation. Might be possible to streamline.
Mike Klein1fa9c432017-12-11 09:59:47 -0500461 if (info.colorType() == kAlpha_8_SkColorType) {
Mike Kleinb82edcc2018-07-10 18:25:03 +0000462 // The color for A8 images comes from the (sRGB) paint color.
Mike Kleinbe569492018-09-14 09:34:21 -0400463 p->append_set_rgb(alloc, rec.fPaint.getColor4f());
Mike Kleinb82edcc2018-07-10 18:25:03 +0000464 p->append(SkRasterPipeline::premul);
465 } else if (info.alphaType() == kUnpremul_SkAlphaType) {
466 // Convert unpremul images to premul before we carry on with the rest of the pipeline.
Mike Klein47cf0482018-02-09 18:57:54 +0000467 p->append(SkRasterPipeline::premul);
468 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000469
Mike Klein1fa9c432017-12-11 09:59:47 -0500470 if (quality > kLow_SkFilterQuality) {
471 // Bicubic filtering naturally produces out of range values on both sides.
472 p->append(SkRasterPipeline::clamp_0);
Mike Klein1f313092018-01-03 10:30:21 -0500473 p->append(fClampAsIfUnpremul ? SkRasterPipeline::clamp_1
474 : SkRasterPipeline::clamp_a);
Mike Klein1fa9c432017-12-11 09:59:47 -0500475 }
Mike Kleinb82edcc2018-07-10 18:25:03 +0000476
477 if (rec.fDstCS) {
478 // If color managed, convert from premul source all the way to premul dst color space.
479 auto srcCS = info.colorSpace();
480 if (!srcCS || info.colorType() == kAlpha_8_SkColorType) {
481 // We treat untagged images as sRGB.
482 // A8 images get their r,g,b from the paint color, so they're also sRGB.
Mike Kleine28a6b52018-07-25 13:05:17 -0400483 srcCS = sk_srgb_singleton();
Mike Kleinb82edcc2018-07-10 18:25:03 +0000484 }
Mike Klein8f3d36c2018-08-14 10:28:05 -0400485 alloc->make<SkColorSpaceXformSteps>(srcCS , kPremul_SkAlphaType,
486 rec.fDstCS, kPremul_SkAlphaType)
Mike Kleinc23d1ab2018-10-29 09:39:52 -0400487 ->apply(p, info.colorType());
Mike Kleinb82edcc2018-07-10 18:25:03 +0000488 }
489
Mike Klein1fa9c432017-12-11 09:59:47 -0500490 return true;
491 };
492
Mike Reed9318a6c2019-08-16 16:16:25 -0400493 // Check for fast-path stages.
Mike Klein8e3426f2018-04-16 12:56:24 -0400494 auto ct = info.colorType();
495 if (true
496 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
497 && quality == kLow_SkFilterQuality
Mike Reedfae8fce2019-04-03 10:27:45 -0400498 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
Mike Klein1fa9c432017-12-11 09:59:47 -0500499
500 p->append(SkRasterPipeline::bilerp_clamp_8888, gather);
Mike Klein8e3426f2018-04-16 12:56:24 -0400501 if (ct == kBGRA_8888_SkColorType) {
502 p->append(SkRasterPipeline::swap_rb);
503 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500504 return append_misc();
505 }
Mike Reed78eedba2019-07-31 16:39:15 -0400506 if (true
Mike Klein01005622019-08-13 12:22:17 -0400507 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
508 && quality == kLow_SkFilterQuality
509 && fTileModeX != SkTileMode::kDecal // TODO decal too?
510 && fTileModeY != SkTileMode::kDecal) {
511
512 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
513 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
514 ctx->ct = ct;
515 ctx->tileX = fTileModeX;
516 ctx->tileY = fTileModeY;
517 ctx->invWidth = 1.0f / ctx->width;
518 ctx->invHeight = 1.0f / ctx->height;
519 p->append(SkRasterPipeline::bilinear, ctx);
520 return append_misc();
521 }
522 if (true
Mike Reed78eedba2019-07-31 16:39:15 -0400523 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType)
524 && quality == kHigh_SkFilterQuality
525 && fTileModeX == SkTileMode::kClamp && fTileModeY == SkTileMode::kClamp) {
526
527 p->append(SkRasterPipeline::bicubic_clamp_8888, gather);
528 if (ct == kBGRA_8888_SkColorType) {
529 p->append(SkRasterPipeline::swap_rb);
530 }
531 return append_misc();
532 }
Mike Klein01005622019-08-13 12:22:17 -0400533 if (true
534 && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) // TODO: all formats
535 && quality == kHigh_SkFilterQuality
536 && fTileModeX != SkTileMode::kDecal // TODO decal too?
537 && fTileModeY != SkTileMode::kDecal) {
538
539 auto ctx = alloc->make<SkRasterPipeline_SamplerCtx2>();
540 *(SkRasterPipeline_GatherCtx*)(ctx) = *gather;
541 ctx->ct = ct;
542 ctx->tileX = fTileModeX;
543 ctx->tileY = fTileModeY;
544 ctx->invWidth = 1.0f / ctx->width;
545 ctx->invHeight = 1.0f / ctx->height;
546 p->append(SkRasterPipeline::bicubic, ctx);
547 return append_misc();
548 }
Mike Klein1fa9c432017-12-11 09:59:47 -0500549
Mike Kleinb11ab572018-10-24 06:42:14 -0400550 SkRasterPipeline_SamplerCtx* sampler = nullptr;
Mike Klein0a904492017-04-12 12:52:48 -0400551 if (quality != kNone_SkFilterQuality) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400552 sampler = alloc->make<SkRasterPipeline_SamplerCtx>();
Mike Klein0a904492017-04-12 12:52:48 -0400553 }
554
Mike Kleinb0b17d12016-12-09 16:25:44 -0500555 auto sample = [&](SkRasterPipeline::StockStage setup_x,
556 SkRasterPipeline::StockStage setup_y) {
Mike Klein0a904492017-04-12 12:52:48 -0400557 p->append(setup_x, sampler);
558 p->append(setup_y, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500559 append_tiling_and_gather();
Mike Klein0a904492017-04-12 12:52:48 -0400560 p->append(SkRasterPipeline::accumulate, sampler);
Mike Klein886cf532016-12-06 11:31:25 -0500561 };
562
Mike Kleinf7f883b2016-11-21 15:09:45 -0500563 if (quality == kNone_SkFilterQuality) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500564 append_tiling_and_gather();
Mike Kleinb0b17d12016-12-09 16:25:44 -0500565 } else if (quality == kLow_SkFilterQuality) {
Mike Klein0a904492017-04-12 12:52:48 -0400566 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500567
568 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny);
569 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny);
570 sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py);
571 sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py);
572
573 p->append(SkRasterPipeline::move_dst_src);
Mike Klein1fa9c432017-12-11 09:59:47 -0500574
Mike Klein46e66a22016-11-21 16:19:34 -0500575 } else {
Mike Klein0a904492017-04-12 12:52:48 -0400576 p->append(SkRasterPipeline::save_xy, sampler);
Mike Kleinb0b17d12016-12-09 16:25:44 -0500577
578 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y);
579 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y);
580 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y);
581 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y);
582
583 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y);
584 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y);
585 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y);
586 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y);
587
588 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y);
589 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y);
590 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y);
591 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y);
592
593 sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y);
594 sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y);
595 sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y);
596 sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y);
597
Mike Kleinb04c3522016-11-28 11:55:58 -0500598 p->append(SkRasterPipeline::move_dst_src);
Mike Klein06a65e22016-11-17 12:39:09 -0500599 }
600
Mike Klein1fa9c432017-12-11 09:59:47 -0500601 return append_misc();
Mike Klein06a65e22016-11-17 12:39:09 -0500602}
Mike Reed9318a6c2019-08-16 16:16:25 -0400603
604bool SkImageShader::onAppendStages(const SkStageRec& rec) const {
605 return this->doStages(rec, nullptr);
606}
607
608SkStageUpdater* SkImageShader::onAppendUpdatableStages(const SkStageRec& rec) const {
609 auto updater = rec.fAlloc->make<SkImageStageUpdater>();
610 updater->fShader = this;
611 return this->doStages(rec, updater) ? updater : nullptr;
612}
613