blob: 70a18982c1ed3f4e8099f620460541497f78a0e9 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkColorFilter.h"
10#include "include/core/SkData.h"
11#include "include/core/SkPixelRef.h"
12#include "include/gpu/GrContext.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/private/SkImageInfoPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTemplates.h"
17#include "src/core/SkAutoMalloc.h"
18#include "src/core/SkBlendModePriv.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMaskFilterBase.h"
Ben Wagner21bca282019-05-15 10:15:52 -040021#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkMipMap.h"
23#include "src/core/SkPaintPriv.h"
24#include "src/core/SkResourceCache.h"
25#include "src/core/SkTraceEvent.h"
26#include "src/gpu/GrBitmapTextureMaker.h"
27#include "src/gpu/GrCaps.h"
28#include "src/gpu/GrColorSpaceXform.h"
29#include "src/gpu/GrContextPriv.h"
30#include "src/gpu/GrGpuResourcePriv.h"
31#include "src/gpu/GrPaint.h"
32#include "src/gpu/GrProxyProvider.h"
33#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040034#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrXferProcessor.h"
36#include "src/gpu/SkGr.h"
37#include "src/gpu/effects/GrBicubicEffect.h"
38#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
39#include "src/gpu/effects/GrSkSLFP.h"
40#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
41#include "src/gpu/effects/generated/GrConstColorProcessor.h"
42#include "src/image/SkImage_Base.h"
43#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040044
Ethan Nicholas78aceb22018-08-31 16:13:58 -040045#if SK_SUPPORT_GPU
46GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Ethan Nicholas00543112018-07-31 09:44:36 -040047// This controls the range of values added to color channels
48layout(key) in int rangeType;
49
50void main(int x, int y, inout half4 color) {
51 half value;
52 half range;
53 @switch (rangeType) {
54 case 0:
55 range = 1.0 / 255.0;
56 break;
57 case 1:
58 range = 1.0 / 63.0;
59 break;
60 default:
61 // Experimentally this looks better than the expected value of 1/15.
62 range = 1.0 / 15.0;
63 break;
64 }
65 @if (sk_Caps.integerSupport) {
66 // This ordered-dither code is lifted from the cpu backend.
67 uint x = uint(x);
68 uint y = uint(y);
69 uint m = (y & 1) << 5 | (x & 1) << 4 |
70 (y & 2) << 2 | (x & 2) << 1 |
71 (y & 4) >> 1 | (x & 4) >> 2;
72 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
73 } else {
74 // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
75 // dither pattern rather than an 8x8 one.
Ethan Nicholase1f55022019-02-05 17:17:40 -050076 half4 modValues = mod(half4(x, y, x, y), half4(2.0, 2.0, 4.0, 4.0));
Ethan Nicholas00543112018-07-31 09:44:36 -040077 half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
78 value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
79 }
80 // For each color channel, add the random offset to the channel value and then clamp
81 // between 0 and alpha to keep the color premultiplied.
82 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
83}
84)";
Ethan Nicholas78aceb22018-08-31 16:13:58 -040085#endif
krajcevski9c0e6292014-06-02 07:38:14 -070086
Brian Osman2b23c4b2018-06-01 12:25:08 -040087GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
bsalomon466c2c42015-10-16 12:01:18 -070088 GrSurfaceDesc desc;
89 desc.fFlags = kNone_GrSurfaceFlags;
90 desc.fWidth = info.width();
91 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040092 desc.fConfig = SkImageInfo2GrPixelConfig(info);
Brian Salomonbdecacf2018-02-02 20:32:49 -050093 desc.fSampleCnt = 1;
bsalomon466c2c42015-10-16 12:01:18 -070094 return desc;
95}
96
bsalomon045802d2015-10-20 07:58:01 -070097void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
98 SkASSERT(key);
99 SkASSERT(imageID);
100 SkASSERT(!imageBounds.isEmpty());
101 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400102 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -0700103 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -0700104 builder[1] = imageBounds.fLeft;
105 builder[2] = imageBounds.fTop;
106 builder[3] = imageBounds.fRight;
107 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700108}
109
bsalomon045802d2015-10-20 07:58:01 -0700110////////////////////////////////////////////////////////////////////////////////
111
Brian Salomon238069b2018-07-11 15:58:57 -0400112void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
113 SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700114 class Invalidator : public SkPixelRef::GenIDChangeListener {
115 public:
Brian Salomon238069b2018-07-11 15:58:57 -0400116 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
117 : fMsg(key, contextUniqueID) {}
118
bsalomon89fe56b2015-10-29 10:49:28 -0700119 private:
120 GrUniqueKeyInvalidatedMessage fMsg;
121
122 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
123 };
124
Brian Salomon238069b2018-07-11 15:58:57 -0400125 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
bsalomon89fe56b2015-10-29 10:49:28 -0700126}
127
Robert Phillips9338c602019-02-19 12:52:29 -0500128sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
129 GrTextureProxy* baseProxy) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400130 SkASSERT(baseProxy);
131
Robert Phillips9da87e02019-02-04 13:26:26 -0500132 if (!ctx->priv().caps()->isConfigCopyable(baseProxy->config())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400133 return nullptr;
134 }
135
Greg Daniel46cfbc62019-06-07 11:43:30 -0400136 return GrSurfaceProxy::Copy(ctx, baseProxy, GrMipMapped::kYes, SkBackingFit::kExact,
137 SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400138}
139
Robert Phillips9338c602019-02-19 12:52:29 -0500140sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400141 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400142 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400143 SkScalar scaleAdjust[2]) {
Brian Osman6064e1c2018-10-19 14:27:54 -0400144 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400145}
146
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500147sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500148 const SkBitmap& bitmap,
149 SkBackingFit fit) {
150 if (!bitmap.peekPixels(nullptr)) {
151 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500152 }
153
Greg Daniel7e1912a2018-02-08 09:15:33 -0500154 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
155 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
156 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500157 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
158 : kIfMutable_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500159 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500160
Greg Daniel7e1912a2018-02-08 09:15:33 -0500161 if (!image) {
162 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500163 }
164
Greg Daniel7e1912a2018-02-08 09:15:33 -0500165 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500166}
167
Robert Phillips7a926392018-02-01 15:49:54 -0500168static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
169 if (!image) {
170 result->reset(); // will be invalid
171 return;
172 }
173
174 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000175 if (!bm->isVolatile()) {
176 SkIPoint origin = bm->pixelRefOrigin();
177 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
178 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
179 }
Robert Phillips7a926392018-02-01 15:49:54 -0500180 return;
181 }
182
183 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
184}
185
186sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500187 sk_sp<SkImage> srcImage,
188 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500189 sk_sp<GrTextureProxy> proxy;
190 GrUniqueKey originalKey;
191
192 create_unique_key_for_image(srcImage.get(), &originalKey);
193
194 if (originalKey.isValid()) {
195 proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin);
196 }
197 if (!proxy) {
Greg Daniela4211122018-03-14 19:09:36 +0000198 proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1,
Brian Salomon58389b92018-03-07 13:01:25 -0500199 SkBudgeted::kYes, fit);
Robert Phillips7a926392018-02-01 15:49:54 -0500200 if (proxy && originalKey.isValid()) {
201 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400202 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
203 // When recording DDLs we do not want to install change listeners because doing
204 // so isn't threadsafe.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500205 if (bm && proxyProvider->renderingDirectly()) {
206 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextID(),
Brian Salomon238069b2018-07-11 15:58:57 -0400207 bm->pixelRef());
Greg Daniela4211122018-03-14 19:09:36 +0000208 }
Robert Phillips7a926392018-02-01 15:49:54 -0500209 }
210 }
211
212 return proxy;
213}
214
rileya@google.com24f3ad12012-07-18 21:47:40 +0000215///////////////////////////////////////////////////////////////////////////////
216
Brian Osmanf28e55d2018-10-03 16:35:54 -0400217SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
218 SkColor4f color = SkColor4f::FromColor(c);
219 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
220 color = xform->apply(color);
221 }
222 return color.premul();
223}
224
Brian Osman8fa7ab42019-03-18 10:22:42 -0400225SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500226 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
227 color = xform->apply(color);
228 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500229 return color;
230}
231
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400232///////////////////////////////////////////////////////////////////////////////
233
Brian Osman2b23c4b2018-06-01 12:25:08 -0400234GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400235 switch (type) {
brianosmanc571c002016-03-17 13:01:26 -0700236 case kUnknown_SkColorType:
237 return kUnknown_GrPixelConfig;
238 case kAlpha_8_SkColorType:
239 return kAlpha_8_GrPixelConfig;
240 case kRGB_565_SkColorType:
241 return kRGB_565_GrPixelConfig;
242 case kARGB_4444_SkColorType:
243 return kRGBA_4444_GrPixelConfig;
244 case kRGBA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400245 return kRGBA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500246 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400247 return kRGB_888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700248 case kBGRA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400249 return kBGRA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500250 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500251 return kRGBA_1010102_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500252 case kRGB_101010x_SkColorType:
253 return kUnknown_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700254 case kGray_8_SkColorType:
Brian Osman986563b2017-01-10 14:20:02 -0500255 return kGray_8_GrPixelConfig;
Brian Osmand0626aa2019-03-11 15:28:06 -0400256 case kRGBA_F16Norm_SkColorType:
257 return kRGBA_half_Clamped_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700258 case kRGBA_F16_SkColorType:
259 return kRGBA_half_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -0400260 case kRGBA_F32_SkColorType:
261 return kRGBA_float_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000262 }
263 SkASSERT(0); // shouldn't get here
264 return kUnknown_GrPixelConfig;
265}
266
Brian Osman2b23c4b2018-06-01 12:25:08 -0400267GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
268 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400269}
270
brianosman396fcdb2016-07-22 06:26:11 -0700271bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400272 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
273 if (kUnknown_SkColorType != ct) {
274 if (ctOut) {
275 *ctOut = ct;
276 }
277 return true;
reed@google.combf790232013-12-13 19:45:58 +0000278 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400279 return false;
reed@google.combf790232013-12-13 19:45:58 +0000280}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000281
bsalomonf1b7a1d2015-09-28 06:26:28 -0700282////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000283
Mike Reed185ba212017-04-28 12:31:05 -0400284static inline bool blend_requires_shader(const SkBlendMode mode) {
285 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700286}
287
Ethan Nicholas00543112018-07-31 09:44:36 -0400288#ifndef SK_IGNORE_GPU_DITHER
289static inline int32_t dither_range_type_for_config(GrPixelConfig dstConfig) {
290 switch (dstConfig) {
291 case kGray_8_GrPixelConfig:
292 case kGray_8_as_Lum_GrPixelConfig:
293 case kGray_8_as_Red_GrPixelConfig:
294 case kRGBA_8888_GrPixelConfig:
295 case kRGB_888_GrPixelConfig:
Greg Danielf259b8b2019-02-14 09:03:43 -0500296 case kRGB_888X_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500297 case kRG_88_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400298 case kBGRA_8888_GrPixelConfig:
Robert Phillipsfe18de52019-06-06 17:21:50 -0400299 case kR_16_GrPixelConfig:
300 case kRG_1616_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -0400301 // Experimental (for Y416 and mutant P016/P010)
302 case kRGBA_16161616_GrPixelConfig:
303 case kRG_half_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400304 return 0;
305 case kRGB_565_GrPixelConfig:
306 return 1;
307 case kRGBA_4444_GrPixelConfig:
308 return 2;
309 case kUnknown_GrPixelConfig:
310 case kSRGBA_8888_GrPixelConfig:
311 case kSBGRA_8888_GrPixelConfig:
312 case kRGBA_1010102_GrPixelConfig:
313 case kAlpha_half_GrPixelConfig:
314 case kAlpha_half_as_Red_GrPixelConfig:
315 case kRGBA_float_GrPixelConfig:
316 case kRG_float_GrPixelConfig:
317 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400318 case kRGBA_half_Clamped_GrPixelConfig:
Jim Van Verth1676cb92019-01-15 13:24:45 -0500319 case kRGB_ETC1_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400320 case kAlpha_8_GrPixelConfig:
321 case kAlpha_8_as_Alpha_GrPixelConfig:
322 case kAlpha_8_as_Red_GrPixelConfig:
323 return -1;
324 }
325 SkASSERT(false);
326 return 0;
327}
328#endif
329
Robert Phillips9338c602019-02-19 12:52:29 -0500330static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400331 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700332 const SkPaint& skPaint,
333 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400334 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400335 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700336 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400337 // Convert SkPaint color to 4f format in the destination color space
Brian Osman8fa7ab42019-03-18 10:22:42 -0400338 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), colorSpaceInfo);
brianosmana4535a32016-06-24 12:50:19 -0700339
Brian Salomonc0d79e52019-04-10 15:02:11 -0400340 GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000341
bsalomonf1b7a1d2015-09-28 06:26:28 -0700342 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
343 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400344 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400345 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400346 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700347 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400348 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400349 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000350 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500351 if (!shaderFP) {
352 return false;
353 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700354 }
355 }
356
357 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
358 // a known constant value. In that case we can simply apply a color filter during this
359 // conversion without converting the color filter to a GrFragmentProcessor.
360 bool applyColorFilterToPaintColor = false;
361 if (shaderFP) {
362 if (primColorMode) {
363 // There is a blend between the primitive color and the shader color. The shader sees
364 // the opaque paint color. The shader's output is blended using the provided mode by
365 // the primitive color. The blended color is then modulated by the paint's alpha.
366
367 // The geometry processor will insert the primitive color to start the color chain, so
368 // the GrPaint color will be ignored.
369
Brian Osmancb3d0872018-10-16 15:19:28 -0400370 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
Brian Salomonaff329b2017-08-11 09:40:37 -0400371 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400372 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
373 *primColorMode);
374
bsalomonf1b7a1d2015-09-28 06:26:28 -0700375 // The above may return null if compose results in a pass through of the prim color.
376 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400377 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700378 }
379
brianosmana4535a32016-06-24 12:50:19 -0700380 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500381 float paintAlpha = skPaint.getColor4f().fA;
382 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400383 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
384 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700385 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500386 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500387 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700388 }
389 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400390 // The shader's FP sees the paint *unpremul* color
391 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
392 grPaint->setColor4f(origColorAsPM);
bungeman06ca8ec2016-06-09 08:01:03 -0700393 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700394 }
395 } else {
396 if (primColorMode) {
397 // There is a blend between the primitive color and the paint color. The blend considers
398 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400399 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
400 auto processor = GrConstColorProcessor::Make(opaqueColor,
401 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400402 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
403 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700404 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700405 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700406 }
407
Brian Osmancb3d0872018-10-16 15:19:28 -0400408 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700409
brianosmana4535a32016-06-24 12:50:19 -0700410 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500411 float paintAlpha = skPaint.getColor4f().fA;
412 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400413 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
414 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700415 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500416 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500417 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700418 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700419 } else {
420 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700421 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700422 applyColorFilterToPaintColor = true;
423 }
424 }
425
426 SkColorFilter* colorFilter = skPaint.getColorFilter();
427 if (colorFilter) {
428 if (applyColorFilterToPaintColor) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400429 grPaint->setColor4f(
430 colorFilter->filterColor4f(origColor, colorSpaceInfo.colorSpace()).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700431 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400432 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
bsalomone25eea42015-09-29 06:38:55 -0700433 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700434 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700435 } else {
436 return false;
437 }
438 }
439 }
440
Mike Reed80747ef2018-01-23 15:29:32 -0500441 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400442 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400443 // We may have set this before passing to the SkShader.
444 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000445 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
446 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400447 }
448 }
449
robertphillips4f037942016-02-09 05:09:27 -0800450 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
451 // the GrPaint to also be null (also kSrcOver).
452 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700453 if (!skPaint.isSrcOver()) {
454 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800455 }
mtklein775b8192014-12-02 09:11:25 -0800456
krajcevskif461a8f2014-06-19 14:14:06 -0700457#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400458 // Conservative default, in case GrPixelConfigToColorType() fails.
459 SkColorType ct = SkColorType::kRGB_565_SkColorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400460 GrPixelConfigToColorType(colorSpaceInfo.config(), &ct);
Brian Osman34ec3742018-07-03 10:40:57 -0400461 if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400462 int32_t ditherRange = dither_range_type_for_config(colorSpaceInfo.config());
463 if (ditherRange >= 0) {
464 static int ditherIndex = GrSkSLFP::NewIndex();
465 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000466 &ditherRange, sizeof(ditherRange));
Ethan Nicholas00543112018-07-31 09:44:36 -0400467 if (ditherFP) {
468 grPaint->addColorFragmentProcessor(std::move(ditherFP));
469 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000470 }
krajcevskif461a8f2014-06-19 14:14:06 -0700471 }
472#endif
bsalomonbed83a62015-04-15 14:18:34 -0700473 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000474}
475
Robert Phillips9338c602019-02-19 12:52:29 -0500476bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorSpaceInfo& colorSpaceInfo,
Brian Salomonf3569f02017-10-24 12:52:33 -0400477 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
478 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
479 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700480}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000481
bsalomonf1b7a1d2015-09-28 06:26:28 -0700482/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500483bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400484 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700485 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400486 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700487 GrPaint* grPaint) {
488 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700489 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000490 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400491 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
492 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000493}
reed8b26b992015-05-07 15:36:17 -0700494
bsalomonf1b7a1d2015-09-28 06:26:28 -0700495/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500496bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400497 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700498 const SkPaint& skPaint,
499 GrPaint* grPaint) {
500 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400501 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
502 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400503 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700504}
505
506/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400507be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500508bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400509 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700510 const SkPaint& skPaint,
511 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400512 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700513 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400514 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400515 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700516}
517
Robert Phillips9338c602019-02-19 12:52:29 -0500518bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400519 const GrColorSpaceInfo& colorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800520 const SkPaint& paint,
521 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400522 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800523 bool textureIsAlphaOnly,
524 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400525 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800526 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400527 if (const auto* shader = as_SB(paint.getShader())) {
Mike Reede3429e62018-01-19 11:43:34 -0500528 shaderFP = shader->asFragmentProcessor(GrFPArgs(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400529 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800530 if (!shaderFP) {
531 return false;
532 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400533 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700534 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800535 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400536 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800537 }
538 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400539 if (paint.getColor4f().isOpaque()) {
540 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
541 } else {
542 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
543 }
joshualitt33a5fce2015-11-18 13:28:51 -0800544 }
545
Brian Salomonf3569f02017-10-24 12:52:33 -0400546 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
547 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800548}
549
bsalomonf1b7a1d2015-09-28 06:26:28 -0700550
551////////////////////////////////////////////////////////////////////////////////////////////////
552
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400553GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
554 const SkMatrix& viewM,
555 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500556 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400557 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700558 *doBicubic = false;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400559 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700560 switch (paintFilterQuality) {
561 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400562 textureFilterMode = GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700563 break;
564 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400565 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700566 break;
567 case kMedium_SkFilterQuality: {
568 SkMatrix matrix;
569 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500570 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
571 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
572 //
573 // Want: 0 = log2(1/s) - 0.5
574 // 0.5 = log2(1/s)
575 // 2^0.5 = 1/s
576 // 1/2^0.5 = s
577 // 2^0.5/2 = s
578 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
579 if (matrix.getMinScale() < mipScale) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400580 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700581 } else {
582 // Don't trigger MIP level generation unnecessarily.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400583 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700584 }
585 break;
586 }
587 case kHigh_SkFilterQuality: {
588 SkMatrix matrix;
589 matrix.setConcat(viewM, localM);
590 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
591 break;
592 }
593 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400594 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400595 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700596 break;
597
598 }
599 return textureFilterMode;
600}