blob: f5289920f7a11bdf3f605e97979887ec69e84177 [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"
16#include "include/gpu/GrTypes.h"
17#include "include/private/GrRecordingContext.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 Reedbd843302019-09-27 21:05:24 -040023#include "src/core/SkColorSpacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkImagePriv.h"
25#include "src/core/SkMaskFilterBase.h"
Ben Wagner21bca282019-05-15 10:15:52 -040026#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/core/SkMipMap.h"
28#include "src/core/SkPaintPriv.h"
29#include "src/core/SkResourceCache.h"
30#include "src/core/SkTraceEvent.h"
31#include "src/gpu/GrBitmapTextureMaker.h"
32#include "src/gpu/GrCaps.h"
33#include "src/gpu/GrColorSpaceXform.h"
34#include "src/gpu/GrContextPriv.h"
35#include "src/gpu/GrGpuResourcePriv.h"
36#include "src/gpu/GrPaint.h"
37#include "src/gpu/GrProxyProvider.h"
38#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040039#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrXferProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/effects/GrBicubicEffect.h"
42#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
43#include "src/gpu/effects/GrSkSLFP.h"
44#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"
47#include "src/image/SkImage_Base.h"
48#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040049
Ethan Nicholas78aceb22018-08-31 16:13:58 -040050GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Ethan Nicholas00543112018-07-31 09:44:36 -040051// This controls the range of values added to color channels
Ethan Nicholas31cff272019-09-26 13:04:48 -040052in int rangeType;
Ethan Nicholas00543112018-07-31 09:44:36 -040053
Brian Osman7353dc52020-02-07 13:37:12 -050054void main(float2 p, inout half4 color) {
Ethan Nicholas00543112018-07-31 09:44:36 -040055 half value;
56 half range;
57 @switch (rangeType) {
58 case 0:
59 range = 1.0 / 255.0;
60 break;
61 case 1:
62 range = 1.0 / 63.0;
63 break;
64 default:
65 // Experimentally this looks better than the expected value of 1/15.
66 range = 1.0 / 15.0;
67 break;
68 }
69 @if (sk_Caps.integerSupport) {
70 // This ordered-dither code is lifted from the cpu backend.
Brian Osman7353dc52020-02-07 13:37:12 -050071 uint x = uint(p.x);
72 uint y = uint(p.y);
Ethan Nicholas00543112018-07-31 09:44:36 -040073 uint m = (y & 1) << 5 | (x & 1) << 4 |
74 (y & 2) << 2 | (x & 2) << 1 |
75 (y & 4) >> 1 | (x & 4) >> 2;
76 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
77 } else {
78 // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
79 // dither pattern rather than an 8x8 one.
Brian Osman7353dc52020-02-07 13:37:12 -050080 half4 modValues = mod(half4(half(p.x), half(p.y), half(p.x), half(p.y)), half4(2.0, 2.0, 4.0, 4.0));
Ethan Nicholas00543112018-07-31 09:44:36 -040081 half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
82 value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
83 }
84 // For each color channel, add the random offset to the channel value and then clamp
85 // between 0 and alpha to keep the color premultiplied.
86 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
87}
88)";
krajcevski9c0e6292014-06-02 07:38:14 -070089
bsalomon045802d2015-10-20 07:58:01 -070090void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
91 SkASSERT(key);
92 SkASSERT(imageID);
93 SkASSERT(!imageBounds.isEmpty());
94 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040095 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -070096 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070097 builder[1] = imageBounds.fLeft;
98 builder[2] = imageBounds.fTop;
99 builder[3] = imageBounds.fRight;
100 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700101}
102
bsalomon045802d2015-10-20 07:58:01 -0700103////////////////////////////////////////////////////////////////////////////////
104
Brian Salomon99a813c2020-03-02 12:50:47 -0500105sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key,
106 uint32_t contextID) {
107 class Listener : public SkIDChangeListener {
108 public:
109 Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {}
110
111 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
112
113 private:
114 GrUniqueKeyInvalidatedMessage fMsg;
115 };
116
117 auto listener = sk_make_sp<Listener>(*key, contextID);
118
119 // We stick a SkData on the key that calls invalidateListener in its destructor.
120 auto invalidateListener = [](const void* ptr, void* /*context*/) {
121 auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr);
122 (*listener)->markShouldDeregister();
123 delete listener;
124 };
125 auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener),
126 sizeof(sk_sp<Listener>),
127 invalidateListener,
128 nullptr);
129 SkASSERT(!key->getCustomData());
130 key->setCustomData(std::move(data));
131 return std::move(listener);
132}
133
Greg Danielcc104db2020-02-03 14:17:08 -0500134GrSurfaceProxyView GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
135 GrSurfaceProxy* baseProxy,
136 GrSurfaceOrigin origin,
137 GrColorType srcColorType) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400138 SkASSERT(baseProxy);
139
Greg Daniel6980c4e2019-07-30 16:18:18 -0400140 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Greg Danielcc104db2020-02-03 14:17:08 -0500141 return {};
Greg Danielbb76ace2017-09-29 15:58:22 -0400142 }
Greg Daniel40903af2020-01-30 14:55:05 -0500143 GrSurfaceProxyView view = GrSurfaceProxy::Copy(ctx, baseProxy, origin, srcColorType,
144 GrMipMapped::kYes, SkBackingFit::kExact,
145 SkBudgeted::kYes);
Greg Danielcc104db2020-02-03 14:17:08 -0500146 SkASSERT(!view.proxy() || view.asTextureProxy());
147 return view;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400148}
149
Greg Danielcc21d0c2020-02-05 16:58:40 -0500150GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap,
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500151 GrMipMapped mipMapped) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500152 GrBitmapTextureMaker maker(ctx, bitmap, GrBitmapTextureMaker::Cached::kYes);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500153 return maker.view(mipMapped);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400154}
155
Greg Danielc52db712020-01-28 17:03:46 -0500156GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context, const SkBitmap& bitmap,
157 SkBackingFit fit) {
Greg Daniel7e1912a2018-02-08 09:15:33 -0500158 if (!bitmap.peekPixels(nullptr)) {
Greg Danielc52db712020-01-28 17:03:46 -0500159 return {};
Robert Phillipse14d3052017-02-15 13:18:21 -0500160 }
161
Greg Daniel6f5441a2020-01-28 17:02:49 -0500162 GrBitmapTextureMaker maker(context, bitmap, GrBitmapTextureMaker::Cached::kYes, fit);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500163 return maker.view(GrMipMapped::kNo);
Robert Phillips7a926392018-02-01 15:49:54 -0500164}
165
rileya@google.com24f3ad12012-07-18 21:47:40 +0000166///////////////////////////////////////////////////////////////////////////////
167
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400168SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400169 SkColor4f color = SkColor4f::FromColor(c);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400170 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400171 color = xform->apply(color);
172 }
173 return color.premul();
174}
175
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400176SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) {
177 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500178 color = xform->apply(color);
179 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500180 return color;
181}
182
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400183///////////////////////////////////////////////////////////////////////////////
184
Mike Reed185ba212017-04-28 12:31:05 -0400185static inline bool blend_requires_shader(const SkBlendMode mode) {
186 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700187}
188
Ethan Nicholas00543112018-07-31 09:44:36 -0400189#ifndef SK_IGNORE_GPU_DITHER
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400190static inline int32_t dither_range_type_for_config(GrColorType dstColorType) {
191 switch (dstColorType) {
Brian Salomon85c3d682019-11-04 15:04:54 -0500192 case GrColorType::kUnknown:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400193 case GrColorType::kGray_8:
194 case GrColorType::kRGBA_8888:
195 case GrColorType::kRGB_888x:
196 case GrColorType::kRG_88:
197 case GrColorType::kBGRA_8888:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400198 case GrColorType::kRG_1616:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400199 case GrColorType::kRGBA_16161616:
200 case GrColorType::kRG_F16:
Greg Daniele877dce2019-07-11 10:52:43 -0400201 case GrColorType::kRGBA_8888_SRGB:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400202 case GrColorType::kRGBA_1010102:
203 case GrColorType::kAlpha_F16:
204 case GrColorType::kRGBA_F32:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400205 case GrColorType::kRGBA_F16:
206 case GrColorType::kRGBA_F16_Clamped:
207 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400208 case GrColorType::kAlpha_8xxx:
Robert Phillips429f0d32019-09-11 17:03:28 -0400209 case GrColorType::kAlpha_16:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400210 case GrColorType::kAlpha_F32xxx:
211 case GrColorType::kGray_8xxx:
Brian Salomon85c3d682019-11-04 15:04:54 -0500212 case GrColorType::kRGB_888:
213 case GrColorType::kR_8:
214 case GrColorType::kR_16:
215 case GrColorType::kR_F16:
216 case GrColorType::kGray_F16:
217 return 0;
218 case GrColorType::kBGR_565:
219 return 1;
220 case GrColorType::kABGR_4444:
221 return 2;
Ethan Nicholas00543112018-07-31 09:44:36 -0400222 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400223 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400224}
225#endif
226
Robert Phillips9338c602019-02-19 12:52:29 -0500227static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400228 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700229 const SkPaint& skPaint,
230 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400231 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400232 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700233 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400234 // Convert SkPaint color to 4f format in the destination color space
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400235 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo);
brianosmana4535a32016-06-24 12:50:19 -0700236
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400237 GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &dstColorInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000238
bsalomonf1b7a1d2015-09-28 06:26:28 -0700239 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
240 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400241 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400242 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400243 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700244 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400245 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400246 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000247 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500248 if (!shaderFP) {
249 return false;
250 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700251 }
252 }
253
254 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
255 // a known constant value. In that case we can simply apply a color filter during this
256 // conversion without converting the color filter to a GrFragmentProcessor.
257 bool applyColorFilterToPaintColor = false;
258 if (shaderFP) {
259 if (primColorMode) {
260 // There is a blend between the primitive color and the shader color. The shader sees
261 // the opaque paint color. The shader's output is blended using the provided mode by
262 // the primitive color. The blended color is then modulated by the paint's alpha.
263
264 // The geometry processor will insert the primitive color to start the color chain, so
265 // the GrPaint color will be ignored.
266
Brian Osmancb3d0872018-10-16 15:19:28 -0400267 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
Brian Salomonaff329b2017-08-11 09:40:37 -0400268 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400269 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
270 *primColorMode);
271
bsalomonf1b7a1d2015-09-28 06:26:28 -0700272 // The above may return null if compose results in a pass through of the prim color.
273 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400274 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700275 }
276
brianosmana4535a32016-06-24 12:50:19 -0700277 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500278 float paintAlpha = skPaint.getColor4f().fA;
279 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400280 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
281 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700282 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500283 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500284 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700285 }
286 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400287 // The shader's FP sees the paint *unpremul* color
288 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
289 grPaint->setColor4f(origColorAsPM);
bungeman06ca8ec2016-06-09 08:01:03 -0700290 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700291 }
292 } else {
293 if (primColorMode) {
294 // There is a blend between the primitive color and the paint color. The blend considers
295 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400296 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
297 auto processor = GrConstColorProcessor::Make(opaqueColor,
298 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400299 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
300 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700301 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700302 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700303 }
304
Brian Osmancb3d0872018-10-16 15:19:28 -0400305 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700306
brianosmana4535a32016-06-24 12:50:19 -0700307 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500308 float paintAlpha = skPaint.getColor4f().fA;
309 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400310 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
311 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700312 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500313 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500314 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700315 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700316 } else {
317 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700318 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700319 applyColorFilterToPaintColor = true;
320 }
321 }
322
323 SkColorFilter* colorFilter = skPaint.getColorFilter();
324 if (colorFilter) {
325 if (applyColorFilterToPaintColor) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400326 SkColorSpace* dstCS = dstColorInfo.colorSpace();
Mike Reedbd843302019-09-27 21:05:24 -0400327 grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700328 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400329 auto cfFP = colorFilter->asFragmentProcessor(context, dstColorInfo);
bsalomone25eea42015-09-29 06:38:55 -0700330 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700331 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700332 } else {
333 return false;
334 }
335 }
336 }
337
Mike Reed80747ef2018-01-23 15:29:32 -0500338 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400339 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400340 // We may have set this before passing to the SkShader.
341 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000342 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
343 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400344 }
345 }
346
robertphillips4f037942016-02-09 05:09:27 -0800347 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
348 // the GrPaint to also be null (also kSrcOver).
349 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700350 if (!skPaint.isSrcOver()) {
351 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800352 }
mtklein775b8192014-12-02 09:11:25 -0800353
krajcevskif461a8f2014-06-19 14:14:06 -0700354#ifndef SK_IGNORE_GPU_DITHER
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400355 GrColorType ct = dstColorInfo.colorType();
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400356 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) &&
357 grPaint->numColorFragmentProcessors() > 0) {
358 int32_t ditherRange = dither_range_type_for_config(ct);
Ethan Nicholas00543112018-07-31 09:44:36 -0400359 if (ditherRange >= 0) {
Brian Osman088913a2019-12-19 15:44:56 -0500360 static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_DITHER_SRC)));
361 auto ditherFP = GrSkSLFP::Make(context, effect, "Dither",
Brian Osman9bfd5952020-02-05 10:51:44 -0500362 SkData::MakeWithCopy(&ditherRange, sizeof(ditherRange)));
Ethan Nicholas00543112018-07-31 09:44:36 -0400363 if (ditherFP) {
364 grPaint->addColorFragmentProcessor(std::move(ditherFP));
365 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000366 }
krajcevskif461a8f2014-06-19 14:14:06 -0700367 }
368#endif
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400369 if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400370 if (grPaint->numColorFragmentProcessors()) {
Brian Osman6f5e9402020-01-22 10:39:31 -0500371 grPaint->addColorFragmentProcessor(GrClampFragmentProcessor::Make(false));
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400372 } else {
373 auto color = grPaint->getColor4f();
374 grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f),
375 SkTPin(color.fG, 0.f, 1.f),
376 SkTPin(color.fB, 0.f, 1.f),
377 SkTPin(color.fA, 0.f, 1.f)});
378 }
379 }
bsalomonbed83a62015-04-15 14:18:34 -0700380 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000381}
382
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400383bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorInfo& dstColorInfo,
Brian Salomonf3569f02017-10-24 12:52:33 -0400384 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400385 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, viewM, nullptr, nullptr,
Brian Salomonf3569f02017-10-24 12:52:33 -0400386 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700387}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000388
bsalomonf1b7a1d2015-09-28 06:26:28 -0700389/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500390bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400391 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700392 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400393 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700394 GrPaint* grPaint) {
395 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700396 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000397 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400398 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, SkMatrix::I(), &shaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400399 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000400}
reed8b26b992015-05-07 15:36:17 -0700401
bsalomonf1b7a1d2015-09-28 06:26:28 -0700402/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500403bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400404 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700405 const SkPaint& skPaint,
406 GrPaint* grPaint) {
407 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400408 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400409 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400410 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700411}
412
413/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400414be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500415bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400416 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700417 const SkPaint& skPaint,
418 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400419 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700420 GrPaint* grPaint) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400421 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400422 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700423}
424
Robert Phillips9338c602019-02-19 12:52:29 -0500425bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400426 const GrColorInfo& dstColorInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800427 const SkPaint& paint,
428 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400429 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800430 bool textureIsAlphaOnly,
431 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400432 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800433 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400434 if (const auto* shader = as_SB(paint.getShader())) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400435 shaderFP = shader->asFragmentProcessor(
436 GrFPArgs(context, &viewM, paint.getFilterQuality(), &dstColorInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800437 if (!shaderFP) {
438 return false;
439 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400440 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700441 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800442 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400443 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800444 }
445 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400446 if (paint.getColor4f().isOpaque()) {
447 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
448 } else {
449 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
450 }
joshualitt33a5fce2015-11-18 13:28:51 -0800451 }
452
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400453 return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, std::move(shaderFP),
Brian Salomonf3569f02017-10-24 12:52:33 -0400454 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800455}
456
bsalomonf1b7a1d2015-09-28 06:26:28 -0700457////////////////////////////////////////////////////////////////////////////////////////////////
458
Chris Dalton309c6c02019-08-13 10:32:47 -0600459GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
460 SkFilterQuality paintFilterQuality,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400461 const SkMatrix& viewM,
462 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500463 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400464 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700465 *doBicubic = false;
Chris Dalton309c6c02019-08-13 10:32:47 -0600466 if (imageWidth <= 1 && imageHeight <= 1) {
467 return GrSamplerState::Filter::kNearest;
468 }
joshualitt9bc39542015-08-12 12:57:54 -0700469 switch (paintFilterQuality) {
470 case kNone_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600471 return GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700472 case kLow_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600473 return GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700474 case kMedium_SkFilterQuality: {
475 SkMatrix matrix;
476 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500477 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
478 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
479 //
480 // Want: 0 = log2(1/s) - 0.5
481 // 0.5 = log2(1/s)
482 // 2^0.5 = 1/s
483 // 1/2^0.5 = s
484 // 2^0.5/2 = s
485 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
486 if (matrix.getMinScale() < mipScale) {
Chris Dalton309c6c02019-08-13 10:32:47 -0600487 return GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700488 } else {
489 // Don't trigger MIP level generation unnecessarily.
Chris Dalton309c6c02019-08-13 10:32:47 -0600490 return GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700491 }
joshualitt9bc39542015-08-12 12:57:54 -0700492 }
493 case kHigh_SkFilterQuality: {
494 SkMatrix matrix;
495 matrix.setConcat(viewM, localM);
Chris Dalton309c6c02019-08-13 10:32:47 -0600496 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700497 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
Chris Dalton309c6c02019-08-13 10:32:47 -0600498 return textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700499 }
joshualitt9bc39542015-08-12 12:57:54 -0700500 }
Chris Dalton309c6c02019-08-13 10:32:47 -0600501 SkUNREACHABLE;
joshualitt9bc39542015-08-12 12:57:54 -0700502}