blob: 6db4e3714fca132d5ad826f89d1fd4011a8c08f1 [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
Mike Reed8c31f2b2019-07-16 16:50:14 -040050void main(float x, float y, inout half4 color) {
Ethan Nicholas00543112018-07-31 09:44:36 -040051 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.
Mike Reed8c31f2b2019-07-16 16:50:14 -040076 half4 modValues = mod(half4(half(x), half(y), half(x), half(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;
bsalomon466c2c42015-10-16 12:01:18 -070089 desc.fWidth = info.width();
90 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040091 desc.fConfig = SkImageInfo2GrPixelConfig(info);
bsalomon466c2c42015-10-16 12:01:18 -070092 return desc;
93}
94
bsalomon045802d2015-10-20 07:58:01 -070095void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
96 SkASSERT(key);
97 SkASSERT(imageID);
98 SkASSERT(!imageBounds.isEmpty());
99 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400100 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -0700101 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -0700102 builder[1] = imageBounds.fLeft;
103 builder[2] = imageBounds.fTop;
104 builder[3] = imageBounds.fRight;
105 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700106}
107
bsalomon045802d2015-10-20 07:58:01 -0700108////////////////////////////////////////////////////////////////////////////////
109
Brian Salomon238069b2018-07-11 15:58:57 -0400110void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
111 SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700112 class Invalidator : public SkPixelRef::GenIDChangeListener {
113 public:
Brian Salomon238069b2018-07-11 15:58:57 -0400114 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
115 : fMsg(key, contextUniqueID) {}
116
bsalomon89fe56b2015-10-29 10:49:28 -0700117 private:
118 GrUniqueKeyInvalidatedMessage fMsg;
119
120 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
121 };
122
Brian Salomon238069b2018-07-11 15:58:57 -0400123 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
bsalomon89fe56b2015-10-29 10:49:28 -0700124}
125
Robert Phillips9338c602019-02-19 12:52:29 -0500126sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
127 GrTextureProxy* baseProxy) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400128 SkASSERT(baseProxy);
129
Greg Daniel6980c4e2019-07-30 16:18:18 -0400130 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400131 return nullptr;
132 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400133 return GrSurfaceProxy::Copy(ctx, baseProxy, GrMipMapped::kYes, SkBackingFit::kExact,
134 SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400135}
136
Robert Phillips9338c602019-02-19 12:52:29 -0500137sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400138 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400139 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400140 SkScalar scaleAdjust[2]) {
Brian Osman6064e1c2018-10-19 14:27:54 -0400141 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400142}
143
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500144sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500145 const SkBitmap& bitmap,
146 SkBackingFit fit) {
147 if (!bitmap.peekPixels(nullptr)) {
148 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500149 }
150
Greg Daniel7e1912a2018-02-08 09:15:33 -0500151 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
152 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
153 // 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 -0500154 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
155 : kIfMutable_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500156 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500157
Greg Daniel7e1912a2018-02-08 09:15:33 -0500158 if (!image) {
159 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500160 }
161
Greg Daniel7e1912a2018-02-08 09:15:33 -0500162 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500163}
164
Robert Phillips7a926392018-02-01 15:49:54 -0500165static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
166 if (!image) {
167 result->reset(); // will be invalid
168 return;
169 }
170
171 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000172 if (!bm->isVolatile()) {
173 SkIPoint origin = bm->pixelRefOrigin();
174 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
175 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
176 }
Robert Phillips7a926392018-02-01 15:49:54 -0500177 return;
178 }
179
180 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
181}
182
183sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500184 sk_sp<SkImage> srcImage,
185 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500186 sk_sp<GrTextureProxy> proxy;
187 GrUniqueKey originalKey;
188
189 create_unique_key_for_image(srcImage.get(), &originalKey);
190
191 if (originalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -0400192 proxy = proxyProvider->findOrCreateProxyByUniqueKey(
193 originalKey, SkColorTypeToGrColorType(srcImage->colorType()),
194 kTopLeft_GrSurfaceOrigin);
Robert Phillips7a926392018-02-01 15:49:54 -0500195 }
196 if (!proxy) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400197 proxy = proxyProvider->createTextureProxy(srcImage, GrRenderable::kNo, 1, SkBudgeted::kYes,
198 fit);
Robert Phillips7a926392018-02-01 15:49:54 -0500199 if (proxy && originalKey.isValid()) {
200 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400201 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
202 // When recording DDLs we do not want to install change listeners because doing
203 // so isn't threadsafe.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500204 if (bm && proxyProvider->renderingDirectly()) {
205 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextID(),
Brian Salomon238069b2018-07-11 15:58:57 -0400206 bm->pixelRef());
Greg Daniela4211122018-03-14 19:09:36 +0000207 }
Robert Phillips7a926392018-02-01 15:49:54 -0500208 }
209 }
210
211 return proxy;
212}
213
rileya@google.com24f3ad12012-07-18 21:47:40 +0000214///////////////////////////////////////////////////////////////////////////////
215
Brian Osmanf28e55d2018-10-03 16:35:54 -0400216SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
217 SkColor4f color = SkColor4f::FromColor(c);
218 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
219 color = xform->apply(color);
220 }
221 return color.premul();
222}
223
Brian Osman8fa7ab42019-03-18 10:22:42 -0400224SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500225 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
226 color = xform->apply(color);
227 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500228 return color;
229}
230
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400231///////////////////////////////////////////////////////////////////////////////
232
Brian Osman2b23c4b2018-06-01 12:25:08 -0400233GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400234 switch (type) {
brianosmanc571c002016-03-17 13:01:26 -0700235 case kUnknown_SkColorType:
236 return kUnknown_GrPixelConfig;
237 case kAlpha_8_SkColorType:
238 return kAlpha_8_GrPixelConfig;
239 case kRGB_565_SkColorType:
240 return kRGB_565_GrPixelConfig;
241 case kARGB_4444_SkColorType:
242 return kRGBA_4444_GrPixelConfig;
243 case kRGBA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400244 return kRGBA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500245 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400246 return kRGB_888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700247 case kBGRA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400248 return kBGRA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500249 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500250 return kRGBA_1010102_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500251 case kRGB_101010x_SkColorType:
252 return kUnknown_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700253 case kGray_8_SkColorType:
Brian Osman986563b2017-01-10 14:20:02 -0500254 return kGray_8_GrPixelConfig;
Brian Osmand0626aa2019-03-11 15:28:06 -0400255 case kRGBA_F16Norm_SkColorType:
256 return kRGBA_half_Clamped_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700257 case kRGBA_F16_SkColorType:
258 return kRGBA_half_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -0400259 case kRGBA_F32_SkColorType:
260 return kRGBA_float_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000261 }
262 SkASSERT(0); // shouldn't get here
263 return kUnknown_GrPixelConfig;
264}
265
Brian Osman2b23c4b2018-06-01 12:25:08 -0400266GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
267 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400268}
269
brianosman396fcdb2016-07-22 06:26:11 -0700270bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400271 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
272 if (kUnknown_SkColorType != ct) {
273 if (ctOut) {
274 *ctOut = ct;
275 }
276 return true;
reed@google.combf790232013-12-13 19:45:58 +0000277 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400278 return false;
reed@google.combf790232013-12-13 19:45:58 +0000279}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000280
bsalomonf1b7a1d2015-09-28 06:26:28 -0700281////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000282
Mike Reed185ba212017-04-28 12:31:05 -0400283static inline bool blend_requires_shader(const SkBlendMode mode) {
284 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700285}
286
Ethan Nicholas00543112018-07-31 09:44:36 -0400287#ifndef SK_IGNORE_GPU_DITHER
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400288static inline int32_t dither_range_type_for_config(GrColorType dstColorType) {
289 switch (dstColorType) {
290 case GrColorType::kGray_8:
291 case GrColorType::kRGBA_8888:
292 case GrColorType::kRGB_888x:
293 case GrColorType::kRG_88:
294 case GrColorType::kBGRA_8888:
295 case GrColorType::kR_16:
296 case GrColorType::kRG_1616:
Robert Phillips66a46032019-06-18 08:00:42 -0400297 // Experimental (for Y416 and mutant P016/P010)
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400298 case GrColorType::kRGBA_16161616:
299 case GrColorType::kRG_F16:
Ethan Nicholas00543112018-07-31 09:44:36 -0400300 return 0;
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400301 case GrColorType::kBGR_565:
Ethan Nicholas00543112018-07-31 09:44:36 -0400302 return 1;
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400303 case GrColorType::kABGR_4444:
Ethan Nicholas00543112018-07-31 09:44:36 -0400304 return 2;
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400305 case GrColorType::kUnknown:
Greg Daniele877dce2019-07-11 10:52:43 -0400306 case GrColorType::kRGBA_8888_SRGB:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400307 case GrColorType::kRGBA_1010102:
308 case GrColorType::kAlpha_F16:
309 case GrColorType::kRGBA_F32:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400310 case GrColorType::kRGBA_F16:
311 case GrColorType::kRGBA_F16_Clamped:
312 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400313 case GrColorType::kAlpha_8xxx:
314 case GrColorType::kAlpha_F32xxx:
315 case GrColorType::kGray_8xxx:
Ethan Nicholas00543112018-07-31 09:44:36 -0400316 return -1;
317 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400318 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400319}
320#endif
321
Robert Phillips9338c602019-02-19 12:52:29 -0500322static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400323 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700324 const SkPaint& skPaint,
325 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400326 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400327 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700328 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400329 // Convert SkPaint color to 4f format in the destination color space
Brian Osman8fa7ab42019-03-18 10:22:42 -0400330 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), colorSpaceInfo);
brianosmana4535a32016-06-24 12:50:19 -0700331
Brian Salomonc0d79e52019-04-10 15:02:11 -0400332 GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000333
bsalomonf1b7a1d2015-09-28 06:26:28 -0700334 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
335 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400336 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400337 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400338 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700339 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400340 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400341 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000342 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500343 if (!shaderFP) {
344 return false;
345 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700346 }
347 }
348
349 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
350 // a known constant value. In that case we can simply apply a color filter during this
351 // conversion without converting the color filter to a GrFragmentProcessor.
352 bool applyColorFilterToPaintColor = false;
353 if (shaderFP) {
354 if (primColorMode) {
355 // There is a blend between the primitive color and the shader color. The shader sees
356 // the opaque paint color. The shader's output is blended using the provided mode by
357 // the primitive color. The blended color is then modulated by the paint's alpha.
358
359 // The geometry processor will insert the primitive color to start the color chain, so
360 // the GrPaint color will be ignored.
361
Brian Osmancb3d0872018-10-16 15:19:28 -0400362 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
Brian Salomonaff329b2017-08-11 09:40:37 -0400363 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400364 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
365 *primColorMode);
366
bsalomonf1b7a1d2015-09-28 06:26:28 -0700367 // The above may return null if compose results in a pass through of the prim color.
368 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400369 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700370 }
371
brianosmana4535a32016-06-24 12:50:19 -0700372 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500373 float paintAlpha = skPaint.getColor4f().fA;
374 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400375 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
376 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700377 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500378 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500379 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700380 }
381 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400382 // The shader's FP sees the paint *unpremul* color
383 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
384 grPaint->setColor4f(origColorAsPM);
bungeman06ca8ec2016-06-09 08:01:03 -0700385 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700386 }
387 } else {
388 if (primColorMode) {
389 // There is a blend between the primitive color and the paint color. The blend considers
390 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400391 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
392 auto processor = GrConstColorProcessor::Make(opaqueColor,
393 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400394 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
395 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700396 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700397 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700398 }
399
Brian Osmancb3d0872018-10-16 15:19:28 -0400400 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700401
brianosmana4535a32016-06-24 12:50:19 -0700402 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500403 float paintAlpha = skPaint.getColor4f().fA;
404 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400405 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
406 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700407 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500408 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500409 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700410 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700411 } else {
412 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700413 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700414 applyColorFilterToPaintColor = true;
415 }
416 }
417
418 SkColorFilter* colorFilter = skPaint.getColorFilter();
419 if (colorFilter) {
420 if (applyColorFilterToPaintColor) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400421 grPaint->setColor4f(
422 colorFilter->filterColor4f(origColor, colorSpaceInfo.colorSpace()).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700423 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400424 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
bsalomone25eea42015-09-29 06:38:55 -0700425 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700426 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700427 } else {
428 return false;
429 }
430 }
431 }
432
Mike Reed80747ef2018-01-23 15:29:32 -0500433 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400434 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400435 // We may have set this before passing to the SkShader.
436 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000437 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
438 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400439 }
440 }
441
robertphillips4f037942016-02-09 05:09:27 -0800442 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
443 // the GrPaint to also be null (also kSrcOver).
444 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700445 if (!skPaint.isSrcOver()) {
446 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800447 }
mtklein775b8192014-12-02 09:11:25 -0800448
krajcevskif461a8f2014-06-19 14:14:06 -0700449#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400450 // Conservative default, in case GrPixelConfigToColorType() fails.
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400451 GrColorType ct = colorSpaceInfo.colorType();
452 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) &&
453 grPaint->numColorFragmentProcessors() > 0) {
454 int32_t ditherRange = dither_range_type_for_config(ct);
Ethan Nicholas00543112018-07-31 09:44:36 -0400455 if (ditherRange >= 0) {
456 static int ditherIndex = GrSkSLFP::NewIndex();
457 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000458 &ditherRange, sizeof(ditherRange));
Ethan Nicholas00543112018-07-31 09:44:36 -0400459 if (ditherFP) {
460 grPaint->addColorFragmentProcessor(std::move(ditherFP));
461 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000462 }
krajcevskif461a8f2014-06-19 14:14:06 -0700463 }
464#endif
bsalomonbed83a62015-04-15 14:18:34 -0700465 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000466}
467
Robert Phillips9338c602019-02-19 12:52:29 -0500468bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorSpaceInfo& colorSpaceInfo,
Brian Salomonf3569f02017-10-24 12:52:33 -0400469 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
470 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
471 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700472}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000473
bsalomonf1b7a1d2015-09-28 06:26:28 -0700474/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500475bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400476 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700477 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400478 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700479 GrPaint* grPaint) {
480 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700481 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000482 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400483 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
484 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000485}
reed8b26b992015-05-07 15:36:17 -0700486
bsalomonf1b7a1d2015-09-28 06:26:28 -0700487/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500488bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400489 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700490 const SkPaint& skPaint,
491 GrPaint* grPaint) {
492 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400493 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
494 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400495 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700496}
497
498/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400499be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500500bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400501 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700502 const SkPaint& skPaint,
503 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400504 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700505 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400506 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400507 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700508}
509
Robert Phillips9338c602019-02-19 12:52:29 -0500510bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400511 const GrColorSpaceInfo& colorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800512 const SkPaint& paint,
513 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400514 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800515 bool textureIsAlphaOnly,
516 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400517 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800518 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400519 if (const auto* shader = as_SB(paint.getShader())) {
Mike Reede3429e62018-01-19 11:43:34 -0500520 shaderFP = shader->asFragmentProcessor(GrFPArgs(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400521 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800522 if (!shaderFP) {
523 return false;
524 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400525 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700526 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800527 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400528 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800529 }
530 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400531 if (paint.getColor4f().isOpaque()) {
532 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
533 } else {
534 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
535 }
joshualitt33a5fce2015-11-18 13:28:51 -0800536 }
537
Brian Salomonf3569f02017-10-24 12:52:33 -0400538 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
539 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800540}
541
bsalomonf1b7a1d2015-09-28 06:26:28 -0700542
543////////////////////////////////////////////////////////////////////////////////////////////////
544
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400545GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
546 const SkMatrix& viewM,
547 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500548 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400549 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700550 *doBicubic = false;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400551 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700552 switch (paintFilterQuality) {
553 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400554 textureFilterMode = GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700555 break;
556 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400557 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700558 break;
559 case kMedium_SkFilterQuality: {
560 SkMatrix matrix;
561 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500562 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
563 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
564 //
565 // Want: 0 = log2(1/s) - 0.5
566 // 0.5 = log2(1/s)
567 // 2^0.5 = 1/s
568 // 1/2^0.5 = s
569 // 2^0.5/2 = s
570 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
571 if (matrix.getMinScale() < mipScale) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400572 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700573 } else {
574 // Don't trigger MIP level generation unnecessarily.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400575 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700576 }
577 break;
578 }
579 case kHigh_SkFilterQuality: {
580 SkMatrix matrix;
581 matrix.setConcat(viewM, localM);
582 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
583 break;
584 }
585 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400586 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400587 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700588 break;
589
590 }
591 return textureFilterMode;
592}