blob: 6c0a2b5c14045b5a21e269a47cd2bb2c185e8dc2 [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 Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkMipMap.h"
29#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"
44#include "src/gpu/effects/GrSkSLFP.h"
45#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050046#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/effects/generated/GrConstColorProcessor.h"
48#include "src/image/SkImage_Base.h"
49#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040050
Ethan Nicholas78aceb22018-08-31 16:13:58 -040051GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Michael Ludwig87f57552020-05-27 14:32:38 -040052// This controls the range of values added to color channels and is based on the destination color
53// type; as such it doesn't really affect our program cache to have a variant per-range.
54in half range;
Ethan Nicholas00543112018-07-31 09:44:36 -040055
Michael Ludwig87f57552020-05-27 14:32:38 -040056void main(inout half4 color) {
Ethan Nicholas00543112018-07-31 09:44:36 -040057 half value;
Michael Ludwig87f57552020-05-27 14:32:38 -040058 @if (sk_Caps.integerSupport)
59 {
Ethan Nicholas00543112018-07-31 09:44:36 -040060 // This ordered-dither code is lifted from the cpu backend.
Michael Ludwig87f57552020-05-27 14:32:38 -040061 uint x = uint(sk_FragCoord.x);
62 uint y = uint(sk_FragCoord.y) ^ x;
Ethan Nicholas00543112018-07-31 09:44:36 -040063 uint m = (y & 1) << 5 | (x & 1) << 4 |
64 (y & 2) << 2 | (x & 2) << 1 |
65 (y & 4) >> 1 | (x & 4) >> 2;
66 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
67 } else {
Michael Ludwig87f57552020-05-27 14:32:38 -040068 // Simulate the integer effect used above using step/mod/abs. For speed, simulates a 4x4
69 // dither pattern rather than an 8x8 one. Since it's 4x4, this is effectively computing:
70 // uint m = (y & 1) << 3 | (x & 1) << 2 |
71 // (y & 2) << 0 | (x & 2) >> 1;
72 // where 'y' has already been XOR'ed with 'x' as in the integer-supported case.
73
74 // To get the low bit of p.x and p.y, we compute mod 2.0; for the high bit, we mod 4.0
75 half4 bits = mod(half4(sk_FragCoord.yxyx), half4(2.0, 2.0, 4.0, 4.0));
76 // Use step to convert the 0-3 value in bits.zw into a 0|1 value. bits.xy is already 0|1.
77 bits.zw = step(2.0, bits.zw);
78 // bits was constructed such that the p.x bits were already in the right place for
79 // interleaving (in bits.yw). We just need to update the other bits from p.y to (p.x ^ p.y).
80 // These are in bits.xz. Since the values are 0|1, we can simulate ^ as abs(y - x).
81 bits.xz = abs(bits.xz - bits.yw);
82
83 // Manual binary sum, divide by N^2, and offset
84 value = dot(bits, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
Ethan Nicholas00543112018-07-31 09:44:36 -040085 }
86 // For each color channel, add the random offset to the channel value and then clamp
87 // between 0 and alpha to keep the color premultiplied.
88 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
89}
90)";
krajcevski9c0e6292014-06-02 07:38:14 -070091
bsalomon045802d2015-10-20 07:58:01 -070092void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
93 SkASSERT(key);
94 SkASSERT(imageID);
95 SkASSERT(!imageBounds.isEmpty());
96 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040097 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -070098 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070099 builder[1] = imageBounds.fLeft;
100 builder[2] = imageBounds.fTop;
101 builder[3] = imageBounds.fRight;
102 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700103}
104
bsalomon045802d2015-10-20 07:58:01 -0700105////////////////////////////////////////////////////////////////////////////////
106
Brian Salomon99a813c2020-03-02 12:50:47 -0500107sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key,
108 uint32_t contextID) {
109 class Listener : public SkIDChangeListener {
110 public:
111 Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {}
112
113 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
114
115 private:
116 GrUniqueKeyInvalidatedMessage fMsg;
117 };
118
119 auto listener = sk_make_sp<Listener>(*key, contextID);
120
121 // We stick a SkData on the key that calls invalidateListener in its destructor.
122 auto invalidateListener = [](const void* ptr, void* /*context*/) {
123 auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr);
124 (*listener)->markShouldDeregister();
125 delete listener;
126 };
127 auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener),
128 sizeof(sk_sp<Listener>),
129 invalidateListener,
130 nullptr);
131 SkASSERT(!key->getCustomData());
132 key->setCustomData(std::move(data));
133 return std::move(listener);
134}
135
Brian Salomonc5243782020-04-02 12:50:34 -0400136sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
137 GrSurfaceProxy* baseProxy,
138 GrSurfaceOrigin origin,
139 SkBudgeted budgeted) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400140 SkASSERT(baseProxy);
141
Greg Daniel6980c4e2019-07-30 16:18:18 -0400142 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Greg Danielcc104db2020-02-03 14:17:08 -0500143 return {};
Greg Danielbb76ace2017-09-29 15:58:22 -0400144 }
Brian Salomonc5243782020-04-02 12:50:34 -0400145 auto copy = GrSurfaceProxy::Copy(ctx, baseProxy, origin, GrMipMapped::kYes,
146 SkBackingFit::kExact, budgeted);
147 if (!copy) {
148 return {};
149 }
150 SkASSERT(copy->asTextureProxy());
151 return copy;
152}
153
154GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext* context,
155 GrSurfaceProxyView src,
156 SkBudgeted budgeted) {
157 auto origin = src.origin();
158 auto swizzle = src.swizzle();
159 auto* proxy = src.proxy();
160 return {GrCopyBaseMipMapToTextureProxy(context, proxy, origin, budgeted), origin, swizzle};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400161}
162
Greg Danielcc21d0c2020-02-05 16:58:40 -0500163GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap,
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500164 GrMipMapped mipMapped) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400165 GrBitmapTextureMaker maker(ctx, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500166 return maker.view(mipMapped);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400167}
168
Brian Salomonbc074a62020-03-18 10:06:13 -0400169GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context,
170 const SkBitmap& bitmap) {
Greg Daniel7e1912a2018-02-08 09:15:33 -0500171 if (!bitmap.peekPixels(nullptr)) {
Greg Danielc52db712020-01-28 17:03:46 -0500172 return {};
Robert Phillipse14d3052017-02-15 13:18:21 -0500173 }
174
Brian Salomonbc074a62020-03-18 10:06:13 -0400175 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500176 return maker.view(GrMipMapped::kNo);
Robert Phillips7a926392018-02-01 15:49:54 -0500177}
178
rileya@google.com24f3ad12012-07-18 21:47:40 +0000179///////////////////////////////////////////////////////////////////////////////
180
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400181SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400182 SkColor4f color = SkColor4f::FromColor(c);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400183 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400184 color = xform->apply(color);
185 }
186 return color.premul();
187}
188
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400189SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) {
190 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500191 color = xform->apply(color);
192 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500193 return color;
194}
195
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400196///////////////////////////////////////////////////////////////////////////////
197
Mike Reed185ba212017-04-28 12:31:05 -0400198static inline bool blend_requires_shader(const SkBlendMode mode) {
199 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700200}
201
Ethan Nicholas00543112018-07-31 09:44:36 -0400202#ifndef SK_IGNORE_GPU_DITHER
Michael Ludwig87f57552020-05-27 14:32:38 -0400203static inline float dither_range_for_config(GrColorType dstColorType) {
204 // We use 1 / (2^bitdepth-1) as the range since each channel can hold 2^bitdepth values
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400205 switch (dstColorType) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400206 // 4 bit
207 case GrColorType::kABGR_4444:
Greg Daniel746460e2020-06-30 13:13:53 -0400208 case GrColorType::kARGB_4444:
209 case GrColorType::kBGRA_4444:
Michael Ludwig87f57552020-05-27 14:32:38 -0400210 return 1 / 15.f;
211 // 6 bit
212 case GrColorType::kBGR_565:
213 return 1 / 63.f;
214 // 8 bit
Brian Salomon85c3d682019-11-04 15:04:54 -0500215 case GrColorType::kUnknown:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400216 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400217 case GrColorType::kAlpha_8xxx:
Michael Ludwig87f57552020-05-27 14:32:38 -0400218 case GrColorType::kGray_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400219 case GrColorType::kGray_8xxx:
Brian Salomon85c3d682019-11-04 15:04:54 -0500220 case GrColorType::kR_8:
Michael Ludwig87f57552020-05-27 14:32:38 -0400221 case GrColorType::kRG_88:
222 case GrColorType::kRGB_888:
223 case GrColorType::kRGB_888x:
224 case GrColorType::kRGBA_8888:
225 case GrColorType::kRGBA_8888_SRGB:
226 case GrColorType::kBGRA_8888:
227 return 1 / 255.f;
228 // 10 bit
229 case GrColorType::kRGBA_1010102:
230 case GrColorType::kBGRA_1010102:
231 return 1 / 1023.f;
232 // 16 bit
233 case GrColorType::kAlpha_16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500234 case GrColorType::kR_16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400235 case GrColorType::kRG_1616:
236 case GrColorType::kRGBA_16161616:
237 return 1 / 32767.f;
238 // Half
239 case GrColorType::kAlpha_F16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500240 case GrColorType::kGray_F16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400241 case GrColorType::kR_F16:
242 case GrColorType::kRG_F16:
243 case GrColorType::kRGBA_F16:
244 case GrColorType::kRGBA_F16_Clamped:
245 // Float
246 case GrColorType::kAlpha_F32xxx:
247 case GrColorType::kRGBA_F32:
248 return 0.f; // no dithering
Ethan Nicholas00543112018-07-31 09:44:36 -0400249 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400250 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400251}
252#endif
253
Robert Phillips9338c602019-02-19 12:52:29 -0500254static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400255 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700256 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400257 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400258 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400259 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700260 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400261 // Convert SkPaint color to 4f format in the destination color space
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400262 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo);
brianosmana4535a32016-06-24 12:50:19 -0700263
Brian Osman449b1152020-04-15 16:43:00 -0400264 GrFPArgs fpArgs(context, matrixProvider, skPaint.getFilterQuality(), &dstColorInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000265
bsalomonf1b7a1d2015-09-28 06:26:28 -0700266 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
267 // of per-vertex colors.
John Stiles7f9aa5a2020-06-23 11:28:50 -0400268 std::unique_ptr<GrFragmentProcessor> paintFP;
Mike Reed185ba212017-04-28 12:31:05 -0400269 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400270 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700271 if (shaderProcessor) {
John Stiles7f9aa5a2020-06-23 11:28:50 -0400272 paintFP = std::move(*shaderProcessor);
273 } else {
274 if (const SkShaderBase* shader = as_SB(skPaint.getShader())) {
275 paintFP = shader->asFragmentProcessor(fpArgs);
276 if (paintFP == nullptr) {
277 return false;
278 }
Yuqian Liaad2ec62018-02-26 10:34:52 -0500279 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700280 }
281 }
282
283 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
284 // a known constant value. In that case we can simply apply a color filter during this
285 // conversion without converting the color filter to a GrFragmentProcessor.
286 bool applyColorFilterToPaintColor = false;
John Stiles7f9aa5a2020-06-23 11:28:50 -0400287 if (paintFP) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700288 if (primColorMode) {
289 // There is a blend between the primitive color and the shader color. The shader sees
290 // the opaque paint color. The shader's output is blended using the provided mode by
291 // the primitive color. The blended color is then modulated by the paint's alpha.
292
293 // The geometry processor will insert the primitive color to start the color chain, so
294 // the GrPaint color will be ignored.
295
Brian Osmancb3d0872018-10-16 15:19:28 -0400296 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
John Stiles7f9aa5a2020-06-23 11:28:50 -0400297 paintFP = GrFragmentProcessor::OverrideInput(std::move(paintFP), shaderInput);
John Stiles5c57e882020-06-30 14:05:03 -0400298 paintFP = GrXfermodeFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
299 *primColorMode);
Mike Reed185ba212017-04-28 12:31:05 -0400300
brianosmana4535a32016-06-24 12:50:19 -0700301 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500302 float paintAlpha = skPaint.getColor4f().fA;
303 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400304 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
305 // color channels. It's value should be treated as the same in ANY color space.
John Stiles7f9aa5a2020-06-23 11:28:50 -0400306 paintFP = GrConstColorProcessor::Make(
307 std::move(paintFP), { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
John Stilese3a39f72020-06-15 13:58:48 -0400308 GrConstColorProcessor::InputMode::kModulateRGBA);
309 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700310 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400311 // The shader's FP sees the paint *unpremul* color
312 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
313 grPaint->setColor4f(origColorAsPM);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700314 }
315 } else {
316 if (primColorMode) {
317 // There is a blend between the primitive color and the paint color. The blend considers
318 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400319 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
John Stiles7f9aa5a2020-06-23 11:28:50 -0400320 paintFP = GrConstColorProcessor::Make(/*inputFP=*/nullptr, opaqueColor,
321 GrConstColorProcessor::InputMode::kIgnore);
John Stiles5c57e882020-06-30 14:05:03 -0400322 paintFP = GrXfermodeFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
323 *primColorMode);
Brian Osmancb3d0872018-10-16 15:19:28 -0400324 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700325
brianosmana4535a32016-06-24 12:50:19 -0700326 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500327 float paintAlpha = skPaint.getColor4f().fA;
328 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400329 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
330 // color channels. It's value should be treated as the same in ANY color space.
John Stiles7f9aa5a2020-06-23 11:28:50 -0400331 paintFP = GrConstColorProcessor::Make(
332 std::move(paintFP), { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
John Stilese3a39f72020-06-15 13:58:48 -0400333 GrConstColorProcessor::InputMode::kModulateRGBA);
334 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700335 } else {
336 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700337 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700338 applyColorFilterToPaintColor = true;
339 }
340 }
341
342 SkColorFilter* colorFilter = skPaint.getColorFilter();
343 if (colorFilter) {
344 if (applyColorFilterToPaintColor) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400345 SkColorSpace* dstCS = dstColorInfo.colorSpace();
Mike Reedbd843302019-09-27 21:05:24 -0400346 grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700347 } else {
John Stilesc36b8aa2020-06-25 12:47:52 -0400348 auto [success, fp] = as_CFB(colorFilter)->asFragmentProcessor(std::move(paintFP),
349 context, dstColorInfo);
350 if (!success) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700351 return false;
352 }
John Stilesc36b8aa2020-06-25 12:47:52 -0400353 paintFP = std::move(fp);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700354 }
355 }
356
John Stilesc36b8aa2020-06-25 12:47:52 -0400357 if (paintFP) {
358 grPaint->addColorFragmentProcessor(std::move(paintFP));
359 }
360
Mike Reed80747ef2018-01-23 15:29:32 -0500361 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400362 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400363 // We may have set this before passing to the SkShader.
364 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000365 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
366 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400367 }
368 }
369
robertphillips4f037942016-02-09 05:09:27 -0800370 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
371 // the GrPaint to also be null (also kSrcOver).
372 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700373 if (!skPaint.isSrcOver()) {
374 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800375 }
mtklein775b8192014-12-02 09:11:25 -0800376
krajcevskif461a8f2014-06-19 14:14:06 -0700377#ifndef SK_IGNORE_GPU_DITHER
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400378 GrColorType ct = dstColorInfo.colorType();
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400379 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) &&
380 grPaint->numColorFragmentProcessors() > 0) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400381 float ditherRange = dither_range_for_config(ct);
382 if (ditherRange > 0.f) {
Brian Osman088913a2019-12-19 15:44:56 -0500383 static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_DITHER_SRC)));
384 auto ditherFP = GrSkSLFP::Make(context, effect, "Dither",
Brian Osman9bfd5952020-02-05 10:51:44 -0500385 SkData::MakeWithCopy(&ditherRange, sizeof(ditherRange)));
Ethan Nicholas00543112018-07-31 09:44:36 -0400386 if (ditherFP) {
387 grPaint->addColorFragmentProcessor(std::move(ditherFP));
388 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000389 }
krajcevskif461a8f2014-06-19 14:14:06 -0700390 }
391#endif
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400392 if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400393 if (grPaint->numColorFragmentProcessors()) {
John Stiles5a2a7b32020-06-04 10:57:21 -0400394 grPaint->addColorFragmentProcessor(
395 GrClampFragmentProcessor::Make(/*inputFP=*/nullptr, /*clampToPremul=*/false));
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400396 } else {
397 auto color = grPaint->getColor4f();
398 grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f),
399 SkTPin(color.fG, 0.f, 1.f),
400 SkTPin(color.fB, 0.f, 1.f),
401 SkTPin(color.fA, 0.f, 1.f)});
402 }
403 }
bsalomonbed83a62015-04-15 14:18:34 -0700404 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000405}
406
Brian Osman449b1152020-04-15 16:43:00 -0400407bool SkPaintToGrPaint(GrRecordingContext* context,
408 const GrColorInfo& dstColorInfo,
409 const SkPaint& skPaint,
410 const SkMatrixProvider& matrixProvider,
411 GrPaint* grPaint) {
412 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, nullptr, nullptr,
Brian Salomonf3569f02017-10-24 12:52:33 -0400413 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700414}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000415
bsalomonf1b7a1d2015-09-28 06:26:28 -0700416/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500417bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400418 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700419 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400420 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700422 GrPaint* grPaint) {
423 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700424 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000425 }
Brian Osman449b1152020-04-15 16:43:00 -0400426 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400427 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000428}
reed8b26b992015-05-07 15:36:17 -0700429
bsalomonf1b7a1d2015-09-28 06:26:28 -0700430/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500431bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400432 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700433 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400434 const SkMatrixProvider& matrixProvider,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700435 GrPaint* grPaint) {
436 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400437 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
Brian Osman449b1152020-04-15 16:43:00 -0400438 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400439 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700440}
441
442/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400443be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500444bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400445 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700446 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400447 const SkMatrixProvider& matrixProvider,
Mike Reed7d954ad2016-10-28 15:42:34 -0400448 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700449 GrPaint* grPaint) {
Brian Osman449b1152020-04-15 16:43:00 -0400450 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, nullptr,
451 &primColorMode, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700452}
453
Robert Phillips9338c602019-02-19 12:52:29 -0500454bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400455 const GrColorInfo& dstColorInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800456 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400457 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400458 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800459 bool textureIsAlphaOnly,
460 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400461 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800462 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400463 if (const auto* shader = as_SB(paint.getShader())) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400464 shaderFP = shader->asFragmentProcessor(
Brian Osman449b1152020-04-15 16:43:00 -0400465 GrFPArgs(context, matrixProvider, paint.getFilterQuality(), &dstColorInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800466 if (!shaderFP) {
467 return false;
468 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400469 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700470 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800471 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400472 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800473 }
474 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400475 if (paint.getColor4f().isOpaque()) {
476 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
477 } else {
478 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
479 }
joshualitt33a5fce2015-11-18 13:28:51 -0800480 }
481
Brian Osman449b1152020-04-15 16:43:00 -0400482 return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, matrixProvider,
483 std::move(shaderFP), grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800484}
485
bsalomonf1b7a1d2015-09-28 06:26:28 -0700486////////////////////////////////////////////////////////////////////////////////////////////////
487
Chris Dalton309c6c02019-08-13 10:32:47 -0600488GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
489 SkFilterQuality paintFilterQuality,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400490 const SkMatrix& viewM,
491 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500492 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400493 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700494 *doBicubic = false;
Chris Dalton309c6c02019-08-13 10:32:47 -0600495 if (imageWidth <= 1 && imageHeight <= 1) {
496 return GrSamplerState::Filter::kNearest;
497 }
joshualitt9bc39542015-08-12 12:57:54 -0700498 switch (paintFilterQuality) {
499 case kNone_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600500 return GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700501 case kLow_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600502 return GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700503 case kMedium_SkFilterQuality: {
504 SkMatrix matrix;
505 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500506 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
507 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
508 //
509 // Want: 0 = log2(1/s) - 0.5
510 // 0.5 = log2(1/s)
511 // 2^0.5 = 1/s
512 // 1/2^0.5 = s
513 // 2^0.5/2 = s
514 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
515 if (matrix.getMinScale() < mipScale) {
Chris Dalton309c6c02019-08-13 10:32:47 -0600516 return GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700517 } else {
518 // Don't trigger MIP level generation unnecessarily.
Chris Dalton309c6c02019-08-13 10:32:47 -0600519 return GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700520 }
joshualitt9bc39542015-08-12 12:57:54 -0700521 }
522 case kHigh_SkFilterQuality: {
523 SkMatrix matrix;
524 matrix.setConcat(viewM, localM);
Chris Dalton309c6c02019-08-13 10:32:47 -0600525 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700526 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
Chris Dalton309c6c02019-08-13 10:32:47 -0600527 return textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700528 }
joshualitt9bc39542015-08-12 12:57:54 -0700529 }
Chris Dalton309c6c02019-08-13 10:32:47 -0600530 SkUNREACHABLE;
joshualitt9bc39542015-08-12 12:57:54 -0700531}