blob: bdb3c3c95d7bf924c79c04d875eb7f8b5d745985 [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"
14#include "include/gpu/GrContext.h"
15#include "include/gpu/GrTypes.h"
16#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkImageInfoPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkTemplates.h"
19#include "src/core/SkAutoMalloc.h"
20#include "src/core/SkBlendModePriv.h"
Mike Reedbd843302019-09-27 21:05:24 -040021#include "src/core/SkColorSpacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkImagePriv.h"
23#include "src/core/SkMaskFilterBase.h"
Ben Wagner21bca282019-05-15 10:15:52 -040024#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkMipMap.h"
26#include "src/core/SkPaintPriv.h"
27#include "src/core/SkResourceCache.h"
28#include "src/core/SkTraceEvent.h"
29#include "src/gpu/GrBitmapTextureMaker.h"
30#include "src/gpu/GrCaps.h"
31#include "src/gpu/GrColorSpaceXform.h"
32#include "src/gpu/GrContextPriv.h"
33#include "src/gpu/GrGpuResourcePriv.h"
34#include "src/gpu/GrPaint.h"
35#include "src/gpu/GrProxyProvider.h"
36#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040037#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrXferProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/effects/GrBicubicEffect.h"
40#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
41#include "src/gpu/effects/GrSkSLFP.h"
42#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
43#include "src/gpu/effects/generated/GrConstColorProcessor.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040044#include "src/gpu/effects/generated/GrSaturateProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/image/SkImage_Base.h"
46#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040047
Ethan Nicholas78aceb22018-08-31 16:13:58 -040048GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Ethan Nicholas00543112018-07-31 09:44:36 -040049// This controls the range of values added to color channels
Ethan Nicholas31cff272019-09-26 13:04:48 -040050in int rangeType;
Ethan Nicholas00543112018-07-31 09:44:36 -040051
Mike Reed8c31f2b2019-07-16 16:50:14 -040052void main(float x, float y, inout half4 color) {
Ethan Nicholas00543112018-07-31 09:44:36 -040053 half value;
54 half range;
55 @switch (rangeType) {
56 case 0:
57 range = 1.0 / 255.0;
58 break;
59 case 1:
60 range = 1.0 / 63.0;
61 break;
62 default:
63 // Experimentally this looks better than the expected value of 1/15.
64 range = 1.0 / 15.0;
65 break;
66 }
67 @if (sk_Caps.integerSupport) {
68 // This ordered-dither code is lifted from the cpu backend.
69 uint x = uint(x);
70 uint y = uint(y);
71 uint m = (y & 1) << 5 | (x & 1) << 4 |
72 (y & 2) << 2 | (x & 2) << 1 |
73 (y & 4) >> 1 | (x & 4) >> 2;
74 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
75 } else {
76 // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
77 // dither pattern rather than an 8x8 one.
Mike Reed8c31f2b2019-07-16 16:50:14 -040078 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 -040079 half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
80 value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
81 }
82 // For each color channel, add the random offset to the channel value and then clamp
83 // between 0 and alpha to keep the color premultiplied.
84 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
85}
86)";
krajcevski9c0e6292014-06-02 07:38:14 -070087
Brian Osman2b23c4b2018-06-01 12:25:08 -040088GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
bsalomon466c2c42015-10-16 12:01:18 -070089 GrSurfaceDesc desc;
bsalomon466c2c42015-10-16 12:01:18 -070090 desc.fWidth = info.width();
91 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040092 desc.fConfig = SkImageInfo2GrPixelConfig(info);
bsalomon466c2c42015-10-16 12:01:18 -070093 return desc;
94}
95
bsalomon045802d2015-10-20 07:58:01 -070096void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
97 SkASSERT(key);
98 SkASSERT(imageID);
99 SkASSERT(!imageBounds.isEmpty());
100 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400101 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -0700102 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -0700103 builder[1] = imageBounds.fLeft;
104 builder[2] = imageBounds.fTop;
105 builder[3] = imageBounds.fRight;
106 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700107}
108
bsalomon045802d2015-10-20 07:58:01 -0700109////////////////////////////////////////////////////////////////////////////////
110
Brian Salomon238069b2018-07-11 15:58:57 -0400111void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
112 SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700113 class Invalidator : public SkPixelRef::GenIDChangeListener {
114 public:
Brian Salomon238069b2018-07-11 15:58:57 -0400115 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
116 : fMsg(key, contextUniqueID) {}
117
bsalomon89fe56b2015-10-29 10:49:28 -0700118 private:
119 GrUniqueKeyInvalidatedMessage fMsg;
120
121 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
122 };
123
Brian Salomon238069b2018-07-11 15:58:57 -0400124 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
bsalomon89fe56b2015-10-29 10:49:28 -0700125}
126
Robert Phillips9338c602019-02-19 12:52:29 -0500127sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
Greg Danielc594e622019-10-15 14:01:49 -0400128 GrTextureProxy* baseProxy,
129 GrColorType srcColorType) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400130 SkASSERT(baseProxy);
131
Greg Daniel6980c4e2019-07-30 16:18:18 -0400132 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400133 return nullptr;
134 }
Greg Danielc594e622019-10-15 14:01:49 -0400135 return GrSurfaceProxy::Copy(ctx, baseProxy, srcColorType, GrMipMapped::kYes,
136 SkBackingFit::kExact, SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400137}
138
Robert Phillips9338c602019-02-19 12:52:29 -0500139sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400140 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400141 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400142 SkScalar scaleAdjust[2]) {
Brian Osman6064e1c2018-10-19 14:27:54 -0400143 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400144}
145
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500146sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500147 const SkBitmap& bitmap,
148 SkBackingFit fit) {
149 if (!bitmap.peekPixels(nullptr)) {
150 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500151 }
152
Greg Daniel7e1912a2018-02-08 09:15:33 -0500153 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
154 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
155 // 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 -0500156 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
157 : kIfMutable_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500158 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500159
Greg Daniel7e1912a2018-02-08 09:15:33 -0500160 if (!image) {
161 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500162 }
163
Greg Daniel7e1912a2018-02-08 09:15:33 -0500164 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500165}
166
Robert Phillips7a926392018-02-01 15:49:54 -0500167static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
168 if (!image) {
169 result->reset(); // will be invalid
170 return;
171 }
172
173 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000174 if (!bm->isVolatile()) {
175 SkIPoint origin = bm->pixelRefOrigin();
176 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
177 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
178 }
Robert Phillips7a926392018-02-01 15:49:54 -0500179 return;
180 }
181
182 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
183}
184
185sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500186 sk_sp<SkImage> srcImage,
187 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500188 sk_sp<GrTextureProxy> proxy;
189 GrUniqueKey originalKey;
190
191 create_unique_key_for_image(srcImage.get(), &originalKey);
192
193 if (originalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -0400194 proxy = proxyProvider->findOrCreateProxyByUniqueKey(
195 originalKey, SkColorTypeToGrColorType(srcImage->colorType()),
196 kTopLeft_GrSurfaceOrigin);
Robert Phillips7a926392018-02-01 15:49:54 -0500197 }
198 if (!proxy) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400199 proxy = proxyProvider->createTextureProxy(srcImage, 1, 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 Salomon4bc0c1f2019-09-30 15:12:27 -0400217SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400218 SkColor4f color = SkColor4f::FromColor(c);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400219 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400220 color = xform->apply(color);
221 }
222 return color.premul();
223}
224
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400225SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) {
226 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500227 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:
Brian Salomondcbc3592019-09-27 14:40:20 -0400261 return kUnknown_GrPixelConfig;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400262 case kR8G8_unorm_SkColorType:
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400263 return kRG_88_GrPixelConfig;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400264 case kR16G16_unorm_SkColorType:
Robert Phillips429f0d32019-09-11 17:03:28 -0400265 return kRG_1616_GrPixelConfig;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400266 case kA16_unorm_SkColorType:
Robert Phillips429f0d32019-09-11 17:03:28 -0400267 return kAlpha_16_GrPixelConfig;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400268 case kA16_float_SkColorType:
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400269 return kAlpha_half_GrPixelConfig;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400270 case kR16G16_float_SkColorType:
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400271 return kRG_half_GrPixelConfig;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400272 case kR16G16B16A16_unorm_SkColorType:
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400273 return kRGBA_16161616_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000274 }
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400275 SkUNREACHABLE;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000276}
277
Brian Osman2b23c4b2018-06-01 12:25:08 -0400278GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
279 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400280}
281
brianosman396fcdb2016-07-22 06:26:11 -0700282bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400283 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
284 if (kUnknown_SkColorType != ct) {
285 if (ctOut) {
286 *ctOut = ct;
287 }
288 return true;
reed@google.combf790232013-12-13 19:45:58 +0000289 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400290 return false;
reed@google.combf790232013-12-13 19:45:58 +0000291}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000292
bsalomonf1b7a1d2015-09-28 06:26:28 -0700293////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000294
Mike Reed185ba212017-04-28 12:31:05 -0400295static inline bool blend_requires_shader(const SkBlendMode mode) {
296 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700297}
298
Ethan Nicholas00543112018-07-31 09:44:36 -0400299#ifndef SK_IGNORE_GPU_DITHER
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400300static inline int32_t dither_range_type_for_config(GrColorType dstColorType) {
301 switch (dstColorType) {
302 case GrColorType::kGray_8:
303 case GrColorType::kRGBA_8888:
304 case GrColorType::kRGB_888x:
305 case GrColorType::kRG_88:
306 case GrColorType::kBGRA_8888:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400307 case GrColorType::kRG_1616:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400308 case GrColorType::kRGBA_16161616:
309 case GrColorType::kRG_F16:
Ethan Nicholas00543112018-07-31 09:44:36 -0400310 return 0;
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400311 case GrColorType::kBGR_565:
Ethan Nicholas00543112018-07-31 09:44:36 -0400312 return 1;
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400313 case GrColorType::kABGR_4444:
Ethan Nicholas00543112018-07-31 09:44:36 -0400314 return 2;
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400315 case GrColorType::kUnknown:
Greg Daniele877dce2019-07-11 10:52:43 -0400316 case GrColorType::kRGBA_8888_SRGB:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400317 case GrColorType::kRGBA_1010102:
318 case GrColorType::kAlpha_F16:
319 case GrColorType::kRGBA_F32:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400320 case GrColorType::kRGBA_F16:
321 case GrColorType::kRGBA_F16_Clamped:
322 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400323 case GrColorType::kAlpha_8xxx:
Robert Phillips429f0d32019-09-11 17:03:28 -0400324 case GrColorType::kAlpha_16:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400325 case GrColorType::kAlpha_F32xxx:
326 case GrColorType::kGray_8xxx:
Ethan Nicholas00543112018-07-31 09:44:36 -0400327 return -1;
328 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400329 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400330}
331#endif
332
Robert Phillips9338c602019-02-19 12:52:29 -0500333static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400334 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700335 const SkPaint& skPaint,
336 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400337 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400338 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700339 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400340 // Convert SkPaint color to 4f format in the destination color space
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400341 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo);
brianosmana4535a32016-06-24 12:50:19 -0700342
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400343 GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &dstColorInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000344
bsalomonf1b7a1d2015-09-28 06:26:28 -0700345 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
346 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400347 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400348 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400349 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700350 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400351 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400352 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000353 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500354 if (!shaderFP) {
355 return false;
356 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700357 }
358 }
359
360 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
361 // a known constant value. In that case we can simply apply a color filter during this
362 // conversion without converting the color filter to a GrFragmentProcessor.
363 bool applyColorFilterToPaintColor = false;
364 if (shaderFP) {
365 if (primColorMode) {
366 // There is a blend between the primitive color and the shader color. The shader sees
367 // the opaque paint color. The shader's output is blended using the provided mode by
368 // the primitive color. The blended color is then modulated by the paint's alpha.
369
370 // The geometry processor will insert the primitive color to start the color chain, so
371 // the GrPaint color will be ignored.
372
Brian Osmancb3d0872018-10-16 15:19:28 -0400373 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
Brian Salomonaff329b2017-08-11 09:40:37 -0400374 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400375 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
376 *primColorMode);
377
bsalomonf1b7a1d2015-09-28 06:26:28 -0700378 // The above may return null if compose results in a pass through of the prim color.
379 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400380 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700381 }
382
brianosmana4535a32016-06-24 12:50:19 -0700383 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500384 float paintAlpha = skPaint.getColor4f().fA;
385 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400386 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
387 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700388 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500389 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500390 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700391 }
392 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400393 // The shader's FP sees the paint *unpremul* color
394 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
395 grPaint->setColor4f(origColorAsPM);
bungeman06ca8ec2016-06-09 08:01:03 -0700396 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700397 }
398 } else {
399 if (primColorMode) {
400 // There is a blend between the primitive color and the paint color. The blend considers
401 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400402 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
403 auto processor = GrConstColorProcessor::Make(opaqueColor,
404 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400405 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
406 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700407 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700408 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700409 }
410
Brian Osmancb3d0872018-10-16 15:19:28 -0400411 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700412
brianosmana4535a32016-06-24 12:50:19 -0700413 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500414 float paintAlpha = skPaint.getColor4f().fA;
415 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400416 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
417 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700418 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500419 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500420 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700421 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700422 } else {
423 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700424 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700425 applyColorFilterToPaintColor = true;
426 }
427 }
428
429 SkColorFilter* colorFilter = skPaint.getColorFilter();
430 if (colorFilter) {
431 if (applyColorFilterToPaintColor) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400432 SkColorSpace* dstCS = dstColorInfo.colorSpace();
Mike Reedbd843302019-09-27 21:05:24 -0400433 grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700434 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400435 auto cfFP = colorFilter->asFragmentProcessor(context, dstColorInfo);
bsalomone25eea42015-09-29 06:38:55 -0700436 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700437 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700438 } else {
439 return false;
440 }
441 }
442 }
443
Mike Reed80747ef2018-01-23 15:29:32 -0500444 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400445 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400446 // We may have set this before passing to the SkShader.
447 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000448 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
449 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400450 }
451 }
452
robertphillips4f037942016-02-09 05:09:27 -0800453 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
454 // the GrPaint to also be null (also kSrcOver).
455 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700456 if (!skPaint.isSrcOver()) {
457 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800458 }
mtklein775b8192014-12-02 09:11:25 -0800459
krajcevskif461a8f2014-06-19 14:14:06 -0700460#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400461 // Conservative default, in case GrPixelConfigToColorType() fails.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400462 GrColorType ct = dstColorInfo.colorType();
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400463 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) &&
464 grPaint->numColorFragmentProcessors() > 0) {
465 int32_t ditherRange = dither_range_type_for_config(ct);
Ethan Nicholas00543112018-07-31 09:44:36 -0400466 if (ditherRange >= 0) {
467 static int ditherIndex = GrSkSLFP::NewIndex();
468 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000469 &ditherRange, sizeof(ditherRange));
Ethan Nicholas00543112018-07-31 09:44:36 -0400470 if (ditherFP) {
471 grPaint->addColorFragmentProcessor(std::move(ditherFP));
472 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000473 }
krajcevskif461a8f2014-06-19 14:14:06 -0700474 }
475#endif
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400476 if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400477 if (grPaint->numColorFragmentProcessors()) {
478 grPaint->addColorFragmentProcessor(GrSaturateProcessor::Make());
479 } else {
480 auto color = grPaint->getColor4f();
481 grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f),
482 SkTPin(color.fG, 0.f, 1.f),
483 SkTPin(color.fB, 0.f, 1.f),
484 SkTPin(color.fA, 0.f, 1.f)});
485 }
486 }
bsalomonbed83a62015-04-15 14:18:34 -0700487 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000488}
489
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400490bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorInfo& dstColorInfo,
Brian Salomonf3569f02017-10-24 12:52:33 -0400491 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400492 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, viewM, nullptr, nullptr,
Brian Salomonf3569f02017-10-24 12:52:33 -0400493 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700494}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000495
bsalomonf1b7a1d2015-09-28 06:26:28 -0700496/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500497bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400498 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700499 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400500 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700501 GrPaint* grPaint) {
502 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700503 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000504 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400505 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, SkMatrix::I(), &shaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400506 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000507}
reed8b26b992015-05-07 15:36:17 -0700508
bsalomonf1b7a1d2015-09-28 06:26:28 -0700509/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500510bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400511 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700512 const SkPaint& skPaint,
513 GrPaint* grPaint) {
514 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400515 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400516 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400517 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700518}
519
520/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400521be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500522bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400523 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700524 const SkPaint& skPaint,
525 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400526 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700527 GrPaint* grPaint) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400528 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400529 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700530}
531
Robert Phillips9338c602019-02-19 12:52:29 -0500532bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400533 const GrColorInfo& dstColorInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800534 const SkPaint& paint,
535 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400536 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800537 bool textureIsAlphaOnly,
538 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400539 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800540 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400541 if (const auto* shader = as_SB(paint.getShader())) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400542 shaderFP = shader->asFragmentProcessor(
543 GrFPArgs(context, &viewM, paint.getFilterQuality(), &dstColorInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800544 if (!shaderFP) {
545 return false;
546 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400547 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700548 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800549 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400550 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800551 }
552 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400553 if (paint.getColor4f().isOpaque()) {
554 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
555 } else {
556 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
557 }
joshualitt33a5fce2015-11-18 13:28:51 -0800558 }
559
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400560 return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, std::move(shaderFP),
Brian Salomonf3569f02017-10-24 12:52:33 -0400561 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800562}
563
bsalomonf1b7a1d2015-09-28 06:26:28 -0700564////////////////////////////////////////////////////////////////////////////////////////////////
565
Chris Dalton309c6c02019-08-13 10:32:47 -0600566GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
567 SkFilterQuality paintFilterQuality,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400568 const SkMatrix& viewM,
569 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500570 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400571 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700572 *doBicubic = false;
Chris Dalton309c6c02019-08-13 10:32:47 -0600573 if (imageWidth <= 1 && imageHeight <= 1) {
574 return GrSamplerState::Filter::kNearest;
575 }
joshualitt9bc39542015-08-12 12:57:54 -0700576 switch (paintFilterQuality) {
577 case kNone_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600578 return GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700579 case kLow_SkFilterQuality:
Chris Dalton309c6c02019-08-13 10:32:47 -0600580 return GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700581 case kMedium_SkFilterQuality: {
582 SkMatrix matrix;
583 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500584 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
585 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
586 //
587 // Want: 0 = log2(1/s) - 0.5
588 // 0.5 = log2(1/s)
589 // 2^0.5 = 1/s
590 // 1/2^0.5 = s
591 // 2^0.5/2 = s
592 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
593 if (matrix.getMinScale() < mipScale) {
Chris Dalton309c6c02019-08-13 10:32:47 -0600594 return GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700595 } else {
596 // Don't trigger MIP level generation unnecessarily.
Chris Dalton309c6c02019-08-13 10:32:47 -0600597 return GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700598 }
joshualitt9bc39542015-08-12 12:57:54 -0700599 }
600 case kHigh_SkFilterQuality: {
601 SkMatrix matrix;
602 matrix.setConcat(viewM, localM);
Chris Dalton309c6c02019-08-13 10:32:47 -0600603 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700604 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
Chris Dalton309c6c02019-08-13 10:32:47 -0600605 return textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700606 }
joshualitt9bc39542015-08-12 12:57:54 -0700607 }
Chris Dalton309c6c02019-08-13 10:32:47 -0600608 SkUNREACHABLE;
joshualitt9bc39542015-08-12 12:57:54 -0700609}