blob: b75eb88dc281f6b99bb1352e2c6fe1647a3e6c05 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/SkGr.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkColorFilter.h"
12#include "include/core/SkData.h"
13#include "include/core/SkPixelRef.h"
Brian Osmanee426f22020-01-02 11:55:24 -050014#include "include/effects/SkRuntimeEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040016#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/GrTypes.h"
Brian Salomon71fe9452020-03-02 16:59:40 -050018#include "include/private/SkIDChangeListener.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/private/SkImageInfoPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/private/SkTemplates.h"
21#include "src/core/SkAutoMalloc.h"
22#include "src/core/SkBlendModePriv.h"
Mike Reedb11e6272020-06-24 16:56:33 -040023#include "src/core/SkColorFilterBase.h"
Mike Reedbd843302019-09-27 21:05:24 -040024#include "src/core/SkColorSpacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkImagePriv.h"
26#include "src/core/SkMaskFilterBase.h"
Ben Wagner21bca282019-05-15 10:15:52 -040027#include "src/core/SkMessageBus.h"
Mike Reed13711eb2020-07-14 17:16:32 -040028#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/core/SkPaintPriv.h"
30#include "src/core/SkResourceCache.h"
31#include "src/core/SkTraceEvent.h"
32#include "src/gpu/GrBitmapTextureMaker.h"
33#include "src/gpu/GrCaps.h"
34#include "src/gpu/GrColorSpaceXform.h"
35#include "src/gpu/GrContextPriv.h"
36#include "src/gpu/GrGpuResourcePriv.h"
37#include "src/gpu/GrPaint.h"
38#include "src/gpu/GrProxyProvider.h"
39#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040040#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrXferProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/effects/GrBicubicEffect.h"
43#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050045#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/effects/generated/GrConstColorProcessor.h"
John Stiles53ee0fc2020-07-09 15:29:33 -040047#include "src/gpu/effects/generated/GrDitherEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/image/SkImage_Base.h"
49#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040050
bsalomon045802d2015-10-20 07:58:01 -070051void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
52 SkASSERT(key);
53 SkASSERT(imageID);
54 SkASSERT(!imageBounds.isEmpty());
55 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040056 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -070057 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070058 builder[1] = imageBounds.fLeft;
59 builder[2] = imageBounds.fTop;
60 builder[3] = imageBounds.fRight;
61 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070062}
63
bsalomon045802d2015-10-20 07:58:01 -070064////////////////////////////////////////////////////////////////////////////////
65
Brian Salomon99a813c2020-03-02 12:50:47 -050066sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key,
67 uint32_t contextID) {
68 class Listener : public SkIDChangeListener {
69 public:
70 Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {}
71
72 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
73
74 private:
75 GrUniqueKeyInvalidatedMessage fMsg;
76 };
77
78 auto listener = sk_make_sp<Listener>(*key, contextID);
79
80 // We stick a SkData on the key that calls invalidateListener in its destructor.
81 auto invalidateListener = [](const void* ptr, void* /*context*/) {
82 auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr);
83 (*listener)->markShouldDeregister();
84 delete listener;
85 };
86 auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener),
87 sizeof(sk_sp<Listener>),
88 invalidateListener,
89 nullptr);
90 SkASSERT(!key->getCustomData());
91 key->setCustomData(std::move(data));
92 return std::move(listener);
93}
94
Brian Salomonc5243782020-04-02 12:50:34 -040095sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
96 GrSurfaceProxy* baseProxy,
97 GrSurfaceOrigin origin,
98 SkBudgeted budgeted) {
Greg Daniel55afd6d2017-09-29 09:32:44 -040099 SkASSERT(baseProxy);
100
Greg Daniel6980c4e2019-07-30 16:18:18 -0400101 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Greg Danielcc104db2020-02-03 14:17:08 -0500102 return {};
Greg Danielbb76ace2017-09-29 15:58:22 -0400103 }
Brian Salomonc5243782020-04-02 12:50:34 -0400104 auto copy = GrSurfaceProxy::Copy(ctx, baseProxy, origin, GrMipMapped::kYes,
105 SkBackingFit::kExact, budgeted);
106 if (!copy) {
107 return {};
108 }
109 SkASSERT(copy->asTextureProxy());
110 return copy;
111}
112
113GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext* context,
114 GrSurfaceProxyView src,
115 SkBudgeted budgeted) {
116 auto origin = src.origin();
117 auto swizzle = src.swizzle();
118 auto* proxy = src.proxy();
119 return {GrCopyBaseMipMapToTextureProxy(context, proxy, origin, budgeted), origin, swizzle};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400120}
121
Greg Danielcc21d0c2020-02-05 16:58:40 -0500122GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap,
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500123 GrMipMapped mipMapped) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400124 GrBitmapTextureMaker maker(ctx, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500125 return maker.view(mipMapped);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400126}
127
Brian Salomonbc074a62020-03-18 10:06:13 -0400128GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context,
129 const SkBitmap& bitmap) {
Greg Daniel7e1912a2018-02-08 09:15:33 -0500130 if (!bitmap.peekPixels(nullptr)) {
Greg Danielc52db712020-01-28 17:03:46 -0500131 return {};
Robert Phillipse14d3052017-02-15 13:18:21 -0500132 }
133
Brian Salomonbc074a62020-03-18 10:06:13 -0400134 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500135 return maker.view(GrMipMapped::kNo);
Robert Phillips7a926392018-02-01 15:49:54 -0500136}
137
rileya@google.com24f3ad12012-07-18 21:47:40 +0000138///////////////////////////////////////////////////////////////////////////////
139
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400140SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400141 SkColor4f color = SkColor4f::FromColor(c);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400142 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400143 color = xform->apply(color);
144 }
145 return color.premul();
146}
147
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400148SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) {
149 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500150 color = xform->apply(color);
151 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500152 return color;
153}
154
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400155///////////////////////////////////////////////////////////////////////////////
156
Mike Reed185ba212017-04-28 12:31:05 -0400157static inline bool blend_requires_shader(const SkBlendMode mode) {
158 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700159}
160
Ethan Nicholas00543112018-07-31 09:44:36 -0400161#ifndef SK_IGNORE_GPU_DITHER
Michael Ludwig87f57552020-05-27 14:32:38 -0400162static inline float dither_range_for_config(GrColorType dstColorType) {
163 // We use 1 / (2^bitdepth-1) as the range since each channel can hold 2^bitdepth values
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400164 switch (dstColorType) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400165 // 4 bit
166 case GrColorType::kABGR_4444:
Greg Daniel746460e2020-06-30 13:13:53 -0400167 case GrColorType::kARGB_4444:
168 case GrColorType::kBGRA_4444:
Michael Ludwig87f57552020-05-27 14:32:38 -0400169 return 1 / 15.f;
170 // 6 bit
171 case GrColorType::kBGR_565:
172 return 1 / 63.f;
173 // 8 bit
Brian Salomon85c3d682019-11-04 15:04:54 -0500174 case GrColorType::kUnknown:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400175 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400176 case GrColorType::kAlpha_8xxx:
Michael Ludwig87f57552020-05-27 14:32:38 -0400177 case GrColorType::kGray_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400178 case GrColorType::kGray_8xxx:
Brian Salomon85c3d682019-11-04 15:04:54 -0500179 case GrColorType::kR_8:
Michael Ludwig87f57552020-05-27 14:32:38 -0400180 case GrColorType::kRG_88:
181 case GrColorType::kRGB_888:
182 case GrColorType::kRGB_888x:
183 case GrColorType::kRGBA_8888:
184 case GrColorType::kRGBA_8888_SRGB:
185 case GrColorType::kBGRA_8888:
186 return 1 / 255.f;
187 // 10 bit
188 case GrColorType::kRGBA_1010102:
189 case GrColorType::kBGRA_1010102:
190 return 1 / 1023.f;
191 // 16 bit
192 case GrColorType::kAlpha_16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500193 case GrColorType::kR_16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400194 case GrColorType::kRG_1616:
195 case GrColorType::kRGBA_16161616:
196 return 1 / 32767.f;
197 // Half
198 case GrColorType::kAlpha_F16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500199 case GrColorType::kGray_F16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400200 case GrColorType::kR_F16:
201 case GrColorType::kRG_F16:
202 case GrColorType::kRGBA_F16:
203 case GrColorType::kRGBA_F16_Clamped:
204 // Float
205 case GrColorType::kAlpha_F32xxx:
206 case GrColorType::kRGBA_F32:
207 return 0.f; // no dithering
Ethan Nicholas00543112018-07-31 09:44:36 -0400208 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400209 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400210}
211#endif
212
Robert Phillips9338c602019-02-19 12:52:29 -0500213static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400214 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700215 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400216 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400217 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400218 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700219 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400220 // Convert SkPaint color to 4f format in the destination color space
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400221 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo);
brianosmana4535a32016-06-24 12:50:19 -0700222
Brian Osman449b1152020-04-15 16:43:00 -0400223 GrFPArgs fpArgs(context, matrixProvider, skPaint.getFilterQuality(), &dstColorInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000224
bsalomonf1b7a1d2015-09-28 06:26:28 -0700225 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
226 // of per-vertex colors.
John Stiles7f9aa5a2020-06-23 11:28:50 -0400227 std::unique_ptr<GrFragmentProcessor> paintFP;
Mike Reed185ba212017-04-28 12:31:05 -0400228 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400229 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700230 if (shaderProcessor) {
John Stiles7f9aa5a2020-06-23 11:28:50 -0400231 paintFP = std::move(*shaderProcessor);
232 } else {
233 if (const SkShaderBase* shader = as_SB(skPaint.getShader())) {
234 paintFP = shader->asFragmentProcessor(fpArgs);
235 if (paintFP == nullptr) {
236 return false;
237 }
Yuqian Liaad2ec62018-02-26 10:34:52 -0500238 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700239 }
240 }
241
242 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
243 // a known constant value. In that case we can simply apply a color filter during this
244 // conversion without converting the color filter to a GrFragmentProcessor.
245 bool applyColorFilterToPaintColor = false;
John Stiles7f9aa5a2020-06-23 11:28:50 -0400246 if (paintFP) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700247 if (primColorMode) {
248 // There is a blend between the primitive color and the shader color. The shader sees
249 // the opaque paint color. The shader's output is blended using the provided mode by
250 // the primitive color. The blended color is then modulated by the paint's alpha.
251
252 // The geometry processor will insert the primitive color to start the color chain, so
253 // the GrPaint color will be ignored.
254
Brian Osmancb3d0872018-10-16 15:19:28 -0400255 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
John Stiles7f9aa5a2020-06-23 11:28:50 -0400256 paintFP = GrFragmentProcessor::OverrideInput(std::move(paintFP), shaderInput);
John Stiles5c57e882020-06-30 14:05:03 -0400257 paintFP = GrXfermodeFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
258 *primColorMode);
Mike Reed185ba212017-04-28 12:31:05 -0400259
brianosmana4535a32016-06-24 12:50:19 -0700260 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500261 float paintAlpha = skPaint.getColor4f().fA;
262 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400263 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
264 // color channels. It's value should be treated as the same in ANY color space.
John Stiles85894302020-07-13 11:39:52 -0400265 paintFP = GrFragmentProcessor::ModulateRGBA(
John Stiles7c196772020-07-13 10:00:16 -0400266 std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha});
John Stilese3a39f72020-06-15 13:58:48 -0400267 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700268 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400269 // The shader's FP sees the paint *unpremul* color
270 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
271 grPaint->setColor4f(origColorAsPM);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700272 }
273 } else {
274 if (primColorMode) {
275 // There is a blend between the primitive color and the paint color. The blend considers
276 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400277 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
John Stiles7c196772020-07-13 10:00:16 -0400278 paintFP = GrConstColorProcessor::Make(opaqueColor);
John Stiles5c57e882020-06-30 14:05:03 -0400279 paintFP = GrXfermodeFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
280 *primColorMode);
Brian Osmancb3d0872018-10-16 15:19:28 -0400281 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700282
brianosmana4535a32016-06-24 12:50:19 -0700283 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500284 float paintAlpha = skPaint.getColor4f().fA;
285 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400286 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
287 // color channels. It's value should be treated as the same in ANY color space.
John Stiles85894302020-07-13 11:39:52 -0400288 paintFP = GrFragmentProcessor::ModulateRGBA(
John Stiles7c196772020-07-13 10:00:16 -0400289 std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha});
John Stilese3a39f72020-06-15 13:58:48 -0400290 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700291 } else {
292 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700293 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700294 applyColorFilterToPaintColor = true;
295 }
296 }
297
298 SkColorFilter* colorFilter = skPaint.getColorFilter();
299 if (colorFilter) {
300 if (applyColorFilterToPaintColor) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400301 SkColorSpace* dstCS = dstColorInfo.colorSpace();
Mike Reedbd843302019-09-27 21:05:24 -0400302 grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700303 } else {
John Stilesc36b8aa2020-06-25 12:47:52 -0400304 auto [success, fp] = as_CFB(colorFilter)->asFragmentProcessor(std::move(paintFP),
305 context, dstColorInfo);
306 if (!success) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700307 return false;
308 }
John Stilesc36b8aa2020-06-25 12:47:52 -0400309 paintFP = std::move(fp);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700310 }
311 }
312
Mike Reed80747ef2018-01-23 15:29:32 -0500313 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400314 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400315 // We may have set this before passing to the SkShader.
316 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000317 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
318 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400319 }
320 }
321
robertphillips4f037942016-02-09 05:09:27 -0800322 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
323 // the GrPaint to also be null (also kSrcOver).
324 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700325 if (!skPaint.isSrcOver()) {
326 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800327 }
mtklein775b8192014-12-02 09:11:25 -0800328
krajcevskif461a8f2014-06-19 14:14:06 -0700329#ifndef SK_IGNORE_GPU_DITHER
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400330 GrColorType ct = dstColorInfo.colorType();
John Stiles53ee0fc2020-07-09 15:29:33 -0400331 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) && paintFP != nullptr) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400332 float ditherRange = dither_range_for_config(ct);
John Stiles53ee0fc2020-07-09 15:29:33 -0400333 paintFP = GrDitherEffect::Make(std::move(paintFP), ditherRange);
krajcevskif461a8f2014-06-19 14:14:06 -0700334 }
335#endif
John Stiles53ee0fc2020-07-09 15:29:33 -0400336
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400337 if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) {
John Stilesd4eb2ec2020-07-09 15:38:08 -0400338 if (paintFP != nullptr) {
339 paintFP = GrClampFragmentProcessor::Make(std::move(paintFP), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400340 } else {
341 auto color = grPaint->getColor4f();
342 grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f),
343 SkTPin(color.fG, 0.f, 1.f),
344 SkTPin(color.fB, 0.f, 1.f),
345 SkTPin(color.fA, 0.f, 1.f)});
346 }
347 }
John Stilesd4eb2ec2020-07-09 15:38:08 -0400348
349 if (paintFP) {
350 grPaint->addColorFragmentProcessor(std::move(paintFP));
351 }
352
bsalomonbed83a62015-04-15 14:18:34 -0700353 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000354}
355
Brian Osman449b1152020-04-15 16:43:00 -0400356bool SkPaintToGrPaint(GrRecordingContext* context,
357 const GrColorInfo& dstColorInfo,
358 const SkPaint& skPaint,
359 const SkMatrixProvider& matrixProvider,
360 GrPaint* grPaint) {
361 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, nullptr, nullptr,
Brian Salomonf3569f02017-10-24 12:52:33 -0400362 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700363}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000364
bsalomonf1b7a1d2015-09-28 06:26:28 -0700365/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500366bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400367 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700368 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400369 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400370 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700371 GrPaint* grPaint) {
372 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700373 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000374 }
Brian Osman449b1152020-04-15 16:43:00 -0400375 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400376 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000377}
reed8b26b992015-05-07 15:36:17 -0700378
bsalomonf1b7a1d2015-09-28 06:26:28 -0700379/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500380bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400381 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700382 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400383 const SkMatrixProvider& matrixProvider,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700384 GrPaint* grPaint) {
385 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400386 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
Brian Osman449b1152020-04-15 16:43:00 -0400387 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400388 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700389}
390
391/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400392be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500393bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400394 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700395 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400396 const SkMatrixProvider& matrixProvider,
Mike Reed7d954ad2016-10-28 15:42:34 -0400397 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700398 GrPaint* grPaint) {
Brian Osman449b1152020-04-15 16:43:00 -0400399 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, nullptr,
400 &primColorMode, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700401}
402
Robert Phillips9338c602019-02-19 12:52:29 -0500403bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400404 const GrColorInfo& dstColorInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800405 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400406 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400407 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800408 bool textureIsAlphaOnly,
409 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400410 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800411 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400412 if (const auto* shader = as_SB(paint.getShader())) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400413 shaderFP = shader->asFragmentProcessor(
Brian Osman449b1152020-04-15 16:43:00 -0400414 GrFPArgs(context, matrixProvider, paint.getFilterQuality(), &dstColorInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800415 if (!shaderFP) {
416 return false;
417 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400418 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700419 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800420 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800422 }
423 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400424 if (paint.getColor4f().isOpaque()) {
425 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
426 } else {
427 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
428 }
joshualitt33a5fce2015-11-18 13:28:51 -0800429 }
430
Brian Osman449b1152020-04-15 16:43:00 -0400431 return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, matrixProvider,
432 std::move(shaderFP), grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800433}
434
bsalomonf1b7a1d2015-09-28 06:26:28 -0700435////////////////////////////////////////////////////////////////////////////////////////////////
436
Chris Dalton309c6c02019-08-13 10:32:47 -0600437GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
438 SkFilterQuality paintFilterQuality,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400439 const SkMatrix& viewM,
440 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500441 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400442 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700443 *doBicubic = false;
Chris Dalton309c6c02019-08-13 10:32:47 -0600444 if (imageWidth <= 1 && imageHeight <= 1) {
445 return GrSamplerState::Filter::kNearest;
446 }
joshualitt9bc39542015-08-12 12:57:54 -0700447 switch (paintFilterQuality) {
448 case kNone_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600449 return GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700450 case kLow_SkFilterQuality:
Brian Salomona3b02f52020-07-15 16:02:01 -0400451 return GrSamplerState::Filter::kLinear;
joshualitt9bc39542015-08-12 12:57:54 -0700452 case kMedium_SkFilterQuality: {
453 SkMatrix matrix;
454 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500455 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
456 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
457 //
458 // Want: 0 = log2(1/s) - 0.5
459 // 0.5 = log2(1/s)
460 // 2^0.5 = 1/s
461 // 1/2^0.5 = s
462 // 2^0.5/2 = s
463 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
464 if (matrix.getMinScale() < mipScale) {
Chris Dalton309c6c02019-08-13 10:32:47 -0600465 return GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700466 } else {
467 // Don't trigger MIP level generation unnecessarily.
Brian Salomona3b02f52020-07-15 16:02:01 -0400468 return GrSamplerState::Filter::kLinear;
joshualitt9bc39542015-08-12 12:57:54 -0700469 }
joshualitt9bc39542015-08-12 12:57:54 -0700470 }
471 case kHigh_SkFilterQuality: {
472 SkMatrix matrix;
473 matrix.setConcat(viewM, localM);
Chris Dalton309c6c02019-08-13 10:32:47 -0600474 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700475 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
Chris Dalton309c6c02019-08-13 10:32:47 -0600476 return textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700477 }
joshualitt9bc39542015-08-12 12:57:54 -0700478 }
Chris Dalton309c6c02019-08-13 10:32:47 -0600479 SkUNREACHABLE;
joshualitt9bc39542015-08-12 12:57:54 -0700480}