blob: 89220b3b3e9debe6cf76de48ecc3af9068d7e6f2 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/SkGr.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkColorFilter.h"
12#include "include/core/SkData.h"
13#include "include/core/SkPixelRef.h"
Brian Osmanee426f22020-01-02 11:55:24 -050014#include "include/effects/SkRuntimeEffect.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040015#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/gpu/GrTypes.h"
Brian Salomon71fe9452020-03-02 16:59:40 -050017#include "include/private/SkIDChangeListener.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkImageInfoPriv.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050019#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/private/SkTemplates.h"
21#include "src/core/SkAutoMalloc.h"
22#include "src/core/SkBlendModePriv.h"
Mike Reedb11e6272020-06-24 16:56:33 -040023#include "src/core/SkColorFilterBase.h"
Mike Reedbd843302019-09-27 21:05:24 -040024#include "src/core/SkColorSpacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkImagePriv.h"
26#include "src/core/SkMaskFilterBase.h"
Ben Wagner21bca282019-05-15 10:15:52 -040027#include "src/core/SkMessageBus.h"
Mike Reed13711eb2020-07-14 17:16:32 -040028#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/core/SkPaintPriv.h"
30#include "src/core/SkResourceCache.h"
31#include "src/core/SkTraceEvent.h"
32#include "src/gpu/GrBitmapTextureMaker.h"
33#include "src/gpu/GrCaps.h"
34#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrGpuResourcePriv.h"
36#include "src/gpu/GrPaint.h"
37#include "src/gpu/GrProxyProvider.h"
38#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040039#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrXferProcessor.h"
Brian Salomone69b9ef2020-07-22 11:18:06 -040041#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -040043#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050045#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/effects/generated/GrConstColorProcessor.h"
John Stiles53ee0fc2020-07-09 15:29:33 -040047#include "src/gpu/effects/generated/GrDitherEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/image/SkImage_Base.h"
49#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040050
bsalomon045802d2015-10-20 07:58:01 -070051void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
52 SkASSERT(key);
53 SkASSERT(imageID);
54 SkASSERT(!imageBounds.isEmpty());
55 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040056 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -070057 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070058 builder[1] = imageBounds.fLeft;
59 builder[2] = imageBounds.fTop;
60 builder[3] = imageBounds.fRight;
61 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070062}
63
bsalomon045802d2015-10-20 07:58:01 -070064////////////////////////////////////////////////////////////////////////////////
65
Brian Salomon99a813c2020-03-02 12:50:47 -050066sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key,
67 uint32_t contextID) {
68 class Listener : public SkIDChangeListener {
69 public:
70 Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {}
71
Robert Phillipse7a959d2021-03-11 14:44:42 -050072 void changed() override {
73 SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg);
74 }
Brian Salomon99a813c2020-03-02 12:50:47 -050075
76 private:
77 GrUniqueKeyInvalidatedMessage fMsg;
78 };
79
80 auto listener = sk_make_sp<Listener>(*key, contextID);
81
82 // We stick a SkData on the key that calls invalidateListener in its destructor.
83 auto invalidateListener = [](const void* ptr, void* /*context*/) {
84 auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr);
85 (*listener)->markShouldDeregister();
86 delete listener;
87 };
88 auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener),
89 sizeof(sk_sp<Listener>),
90 invalidateListener,
91 nullptr);
92 SkASSERT(!key->getCustomData());
93 key->setCustomData(std::move(data));
94 return std::move(listener);
95}
96
Brian Salomonc5243782020-04-02 12:50:34 -040097sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
Brian Salomon982127b2021-01-21 10:43:35 -050098 sk_sp<GrSurfaceProxy> baseProxy,
Brian Salomonc5243782020-04-02 12:50:34 -040099 GrSurfaceOrigin origin,
100 SkBudgeted budgeted) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400101 SkASSERT(baseProxy);
102
Adlai Holler55aaefe2021-03-03 16:12:56 -0700103 // We don't allow this for promise proxies i.e. if they need mips they need to give them
104 // to us upfront.
105 if (baseProxy->isPromiseProxy()) {
106 return nullptr;
107 }
Greg Daniel6980c4e2019-07-30 16:18:18 -0400108 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Adlai Holler55aaefe2021-03-03 16:12:56 -0700109 return nullptr;
Greg Danielbb76ace2017-09-29 15:58:22 -0400110 }
Brian Salomon982127b2021-01-21 10:43:35 -0500111 auto copy = GrSurfaceProxy::Copy(ctx, std::move(baseProxy), origin, GrMipmapped::kYes,
Brian Salomonc5243782020-04-02 12:50:34 -0400112 SkBackingFit::kExact, budgeted);
113 if (!copy) {
Adlai Holler55aaefe2021-03-03 16:12:56 -0700114 return nullptr;
Brian Salomonc5243782020-04-02 12:50:34 -0400115 }
116 SkASSERT(copy->asTextureProxy());
117 return copy;
118}
119
120GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext* context,
121 GrSurfaceProxyView src,
122 SkBudgeted budgeted) {
123 auto origin = src.origin();
124 auto swizzle = src.swizzle();
Brian Salomon982127b2021-01-21 10:43:35 -0500125 auto proxy = src.refProxy();
Brian Salomonc5243782020-04-02 12:50:34 -0400126 return {GrCopyBaseMipMapToTextureProxy(context, proxy, origin, budgeted), origin, swizzle};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400127}
128
Greg Danielcc21d0c2020-02-05 16:58:40 -0500129GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400130 GrMipmapped mipMapped) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400131 GrBitmapTextureMaker maker(ctx, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500132 return maker.view(mipMapped);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400133}
134
Brian Salomonbc074a62020-03-18 10:06:13 -0400135GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context,
136 const SkBitmap& bitmap) {
Greg Daniel7e1912a2018-02-08 09:15:33 -0500137 if (!bitmap.peekPixels(nullptr)) {
Greg Danielc52db712020-01-28 17:03:46 -0500138 return {};
Robert Phillipse14d3052017-02-15 13:18:21 -0500139 }
140
Brian Salomonbc074a62020-03-18 10:06:13 -0400141 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400142 return maker.view(GrMipmapped::kNo);
Robert Phillips7a926392018-02-01 15:49:54 -0500143}
144
rileya@google.com24f3ad12012-07-18 21:47:40 +0000145///////////////////////////////////////////////////////////////////////////////
146
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400147SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400148 SkColor4f color = SkColor4f::FromColor(c);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400149 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400150 color = xform->apply(color);
151 }
152 return color.premul();
153}
154
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400155SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) {
156 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500157 color = xform->apply(color);
158 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500159 return color;
160}
161
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400162///////////////////////////////////////////////////////////////////////////////
163
Mike Reed185ba212017-04-28 12:31:05 -0400164static inline bool blend_requires_shader(const SkBlendMode mode) {
165 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700166}
167
Ethan Nicholas00543112018-07-31 09:44:36 -0400168#ifndef SK_IGNORE_GPU_DITHER
Michael Ludwig87f57552020-05-27 14:32:38 -0400169static inline float dither_range_for_config(GrColorType dstColorType) {
170 // We use 1 / (2^bitdepth-1) as the range since each channel can hold 2^bitdepth values
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400171 switch (dstColorType) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400172 // 4 bit
173 case GrColorType::kABGR_4444:
Greg Daniel746460e2020-06-30 13:13:53 -0400174 case GrColorType::kARGB_4444:
175 case GrColorType::kBGRA_4444:
Michael Ludwig87f57552020-05-27 14:32:38 -0400176 return 1 / 15.f;
177 // 6 bit
178 case GrColorType::kBGR_565:
179 return 1 / 63.f;
180 // 8 bit
Brian Salomon85c3d682019-11-04 15:04:54 -0500181 case GrColorType::kUnknown:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400182 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400183 case GrColorType::kAlpha_8xxx:
Michael Ludwig87f57552020-05-27 14:32:38 -0400184 case GrColorType::kGray_8:
Brian Salomon01ff5382020-12-15 16:06:26 -0500185 case GrColorType::kGrayAlpha_88:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400186 case GrColorType::kGray_8xxx:
Brian Salomon85c3d682019-11-04 15:04:54 -0500187 case GrColorType::kR_8:
Michael Ludwig87f57552020-05-27 14:32:38 -0400188 case GrColorType::kRG_88:
189 case GrColorType::kRGB_888:
190 case GrColorType::kRGB_888x:
191 case GrColorType::kRGBA_8888:
192 case GrColorType::kRGBA_8888_SRGB:
193 case GrColorType::kBGRA_8888:
194 return 1 / 255.f;
195 // 10 bit
196 case GrColorType::kRGBA_1010102:
197 case GrColorType::kBGRA_1010102:
198 return 1 / 1023.f;
199 // 16 bit
200 case GrColorType::kAlpha_16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500201 case GrColorType::kR_16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400202 case GrColorType::kRG_1616:
203 case GrColorType::kRGBA_16161616:
204 return 1 / 32767.f;
205 // Half
206 case GrColorType::kAlpha_F16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500207 case GrColorType::kGray_F16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400208 case GrColorType::kR_F16:
209 case GrColorType::kRG_F16:
210 case GrColorType::kRGBA_F16:
211 case GrColorType::kRGBA_F16_Clamped:
212 // Float
213 case GrColorType::kAlpha_F32xxx:
214 case GrColorType::kRGBA_F32:
215 return 0.f; // no dithering
Ethan Nicholas00543112018-07-31 09:44:36 -0400216 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400217 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400218}
219#endif
220
Robert Phillips9338c602019-02-19 12:52:29 -0500221static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400222 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700223 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400224 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400225 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400226 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700227 GrPaint* grPaint) {
Mike Reed52130b02020-12-28 15:33:13 -0500228 // TODO: take sampling directly
229 SkSamplingOptions sampling(SkPaintPriv::GetFQ(skPaint),
230 SkSamplingOptions::kMedium_asMipmapLinear);
231
Brian Osman08a50e02018-06-15 15:06:48 -0400232 // Convert SkPaint color to 4f format in the destination color space
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400233 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo);
brianosmana4535a32016-06-24 12:50:19 -0700234
Mike Reed52130b02020-12-28 15:33:13 -0500235 GrFPArgs fpArgs(context, matrixProvider, sampling, &dstColorInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000236
bsalomonf1b7a1d2015-09-28 06:26:28 -0700237 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
238 // of per-vertex colors.
John Stiles7f9aa5a2020-06-23 11:28:50 -0400239 std::unique_ptr<GrFragmentProcessor> paintFP;
Mike Reed185ba212017-04-28 12:31:05 -0400240 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400241 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700242 if (shaderProcessor) {
John Stiles7f9aa5a2020-06-23 11:28:50 -0400243 paintFP = std::move(*shaderProcessor);
244 } else {
245 if (const SkShaderBase* shader = as_SB(skPaint.getShader())) {
246 paintFP = shader->asFragmentProcessor(fpArgs);
247 if (paintFP == nullptr) {
248 return false;
249 }
Yuqian Liaad2ec62018-02-26 10:34:52 -0500250 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700251 }
252 }
253
254 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
255 // a known constant value. In that case we can simply apply a color filter during this
256 // conversion without converting the color filter to a GrFragmentProcessor.
257 bool applyColorFilterToPaintColor = false;
John Stiles7f9aa5a2020-06-23 11:28:50 -0400258 if (paintFP) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700259 if (primColorMode) {
260 // There is a blend between the primitive color and the shader color. The shader sees
261 // the opaque paint color. The shader's output is blended using the provided mode by
262 // the primitive color. The blended color is then modulated by the paint's alpha.
263
264 // The geometry processor will insert the primitive color to start the color chain, so
265 // the GrPaint color will be ignored.
266
Brian Osmancb3d0872018-10-16 15:19:28 -0400267 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
John Stiles7f9aa5a2020-06-23 11:28:50 -0400268 paintFP = GrFragmentProcessor::OverrideInput(std::move(paintFP), shaderInput);
John Stilesf743d4e2020-07-23 11:35:08 -0400269 paintFP = GrBlendFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
270 *primColorMode);
Mike Reed185ba212017-04-28 12:31:05 -0400271
brianosmana4535a32016-06-24 12:50:19 -0700272 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500273 float paintAlpha = skPaint.getColor4f().fA;
274 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400275 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
276 // color channels. It's value should be treated as the same in ANY color space.
John Stiles85894302020-07-13 11:39:52 -0400277 paintFP = GrFragmentProcessor::ModulateRGBA(
John Stiles7c196772020-07-13 10:00:16 -0400278 std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha});
John Stilese3a39f72020-06-15 13:58:48 -0400279 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700280 } else {
Brian Osmane7809c72020-07-30 16:49:54 -0400281 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700282 }
283 } else {
284 if (primColorMode) {
285 // There is a blend between the primitive color and the paint color. The blend considers
286 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400287 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
John Stiles7c196772020-07-13 10:00:16 -0400288 paintFP = GrConstColorProcessor::Make(opaqueColor);
John Stilesf743d4e2020-07-23 11:35:08 -0400289 paintFP = GrBlendFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
290 *primColorMode);
Brian Osmancb3d0872018-10-16 15:19:28 -0400291 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700292
brianosmana4535a32016-06-24 12:50:19 -0700293 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500294 float paintAlpha = skPaint.getColor4f().fA;
295 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400296 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
297 // color channels. It's value should be treated as the same in ANY color space.
John Stiles85894302020-07-13 11:39:52 -0400298 paintFP = GrFragmentProcessor::ModulateRGBA(
John Stiles7c196772020-07-13 10:00:16 -0400299 std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha});
John Stilese3a39f72020-06-15 13:58:48 -0400300 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700301 } else {
302 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700303 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700304 applyColorFilterToPaintColor = true;
305 }
306 }
307
308 SkColorFilter* colorFilter = skPaint.getColorFilter();
309 if (colorFilter) {
310 if (applyColorFilterToPaintColor) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400311 SkColorSpace* dstCS = dstColorInfo.colorSpace();
Mike Reedbd843302019-09-27 21:05:24 -0400312 grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700313 } else {
John Stilesc36b8aa2020-06-25 12:47:52 -0400314 auto [success, fp] = as_CFB(colorFilter)->asFragmentProcessor(std::move(paintFP),
315 context, dstColorInfo);
316 if (!success) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700317 return false;
318 }
John Stilesc36b8aa2020-06-25 12:47:52 -0400319 paintFP = std::move(fp);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700320 }
321 }
322
Mike Reed80747ef2018-01-23 15:29:32 -0500323 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400324 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400325 // We may have set this before passing to the SkShader.
326 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000327 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
John Stiles41d91b62020-07-21 14:39:40 -0400328 grPaint->setCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400329 }
330 }
331
robertphillips4f037942016-02-09 05:09:27 -0800332 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
333 // the GrPaint to also be null (also kSrcOver).
334 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700335 if (!skPaint.isSrcOver()) {
336 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800337 }
mtklein775b8192014-12-02 09:11:25 -0800338
krajcevskif461a8f2014-06-19 14:14:06 -0700339#ifndef SK_IGNORE_GPU_DITHER
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400340 GrColorType ct = dstColorInfo.colorType();
John Stiles53ee0fc2020-07-09 15:29:33 -0400341 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) && paintFP != nullptr) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400342 float ditherRange = dither_range_for_config(ct);
John Stiles53ee0fc2020-07-09 15:29:33 -0400343 paintFP = GrDitherEffect::Make(std::move(paintFP), ditherRange);
krajcevskif461a8f2014-06-19 14:14:06 -0700344 }
345#endif
John Stiles53ee0fc2020-07-09 15:29:33 -0400346
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400347 if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) {
John Stilesd4eb2ec2020-07-09 15:38:08 -0400348 if (paintFP != nullptr) {
349 paintFP = GrClampFragmentProcessor::Make(std::move(paintFP), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400350 } else {
351 auto color = grPaint->getColor4f();
352 grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f),
353 SkTPin(color.fG, 0.f, 1.f),
354 SkTPin(color.fB, 0.f, 1.f),
355 SkTPin(color.fA, 0.f, 1.f)});
356 }
357 }
John Stilesd4eb2ec2020-07-09 15:38:08 -0400358
359 if (paintFP) {
John Stiles5933d7d2020-07-21 12:28:35 -0400360 grPaint->setColorFragmentProcessor(std::move(paintFP));
John Stilesd4eb2ec2020-07-09 15:38:08 -0400361 }
362
bsalomonbed83a62015-04-15 14:18:34 -0700363 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000364}
365
Brian Osman449b1152020-04-15 16:43:00 -0400366bool SkPaintToGrPaint(GrRecordingContext* context,
367 const GrColorInfo& dstColorInfo,
368 const SkPaint& skPaint,
369 const SkMatrixProvider& matrixProvider,
370 GrPaint* grPaint) {
John Stilesf743d4e2020-07-23 11:35:08 -0400371 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider,
372 /*shaderProcessor=*/nullptr, /*primColorMode=*/nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700373}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000374
bsalomonf1b7a1d2015-09-28 06:26:28 -0700375/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500376bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400377 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700378 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400379 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700381 GrPaint* grPaint) {
382 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700383 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000384 }
Brian Osman449b1152020-04-15 16:43:00 -0400385 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP,
John Stilesf743d4e2020-07-23 11:35:08 -0400386 /*primColorMode=*/nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000387}
reed8b26b992015-05-07 15:36:17 -0700388
bsalomonf1b7a1d2015-09-28 06:26:28 -0700389/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500390bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400391 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700392 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400393 const SkMatrixProvider& matrixProvider,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700394 GrPaint* grPaint) {
395 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400396 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
Brian Osman449b1152020-04-15 16:43:00 -0400397 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &nullShaderFP,
John Stilesf743d4e2020-07-23 11:35:08 -0400398 /*primColorMode=*/nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700399}
400
401/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
John Stilesf2f9b0d2020-08-25 13:33:45 -0400402 be setup as a vertex attribute using the specified SkBlendMode. */
John Stilesf743d4e2020-07-23 11:35:08 -0400403bool SkPaintToGrPaintWithBlend(GrRecordingContext* context,
404 const GrColorInfo& dstColorInfo,
405 const SkPaint& skPaint,
406 const SkMatrixProvider& matrixProvider,
407 SkBlendMode primColorMode,
408 GrPaint* grPaint) {
409 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider,
410 /*shaderProcessor=*/nullptr, &primColorMode, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700411}
412
John Stilesf2f9b0d2020-08-25 13:33:45 -0400413/** Blends the passed-in shader with a per-primitive color which must be setup as a vertex attribute
414 using the specified SkBlendMode. */
415bool SkPaintToGrPaintWithBlendReplaceShader(GrRecordingContext* context,
416 const GrColorInfo& dstColorInfo,
417 const SkPaint& skPaint,
418 const SkMatrixProvider& matrixProvider,
419 std::unique_ptr<GrFragmentProcessor> shaderFP,
420 SkBlendMode primColorMode,
421 GrPaint* grPaint) {
422 if (!shaderFP) {
423 return false;
424 }
425 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP,
426 &primColorMode, grPaint);
427}
428
Robert Phillips9338c602019-02-19 12:52:29 -0500429bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400430 const GrColorInfo& dstColorInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800431 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400432 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400433 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800434 bool textureIsAlphaOnly,
435 GrPaint* grPaint) {
Mike Reed52130b02020-12-28 15:33:13 -0500436 // TODO: take sampling directly
437 SkSamplingOptions sampling(SkPaintPriv::GetFQ(paint),
438 SkSamplingOptions::kMedium_asMipmapLinear);
439
Brian Salomonaff329b2017-08-11 09:40:37 -0400440 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800441 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400442 if (const auto* shader = as_SB(paint.getShader())) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400443 shaderFP = shader->asFragmentProcessor(
Mike Reed52130b02020-12-28 15:33:13 -0500444 GrFPArgs(context, matrixProvider, sampling, &dstColorInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800445 if (!shaderFP) {
446 return false;
447 }
John Stiles024d7452020-07-22 19:07:15 -0400448 shaderFP = GrFragmentProcessor::Compose(std::move(shaderFP), std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800449 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400450 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800451 }
452 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400453 if (paint.getColor4f().isOpaque()) {
454 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
455 } else {
456 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
457 }
joshualitt33a5fce2015-11-18 13:28:51 -0800458 }
459
Brian Osman449b1152020-04-15 16:43:00 -0400460 return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, matrixProvider,
461 std::move(shaderFP), grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800462}
463
bsalomonf1b7a1d2015-09-28 06:26:28 -0700464////////////////////////////////////////////////////////////////////////////////////////////////
465
Brian Salomone69b9ef2020-07-22 11:18:06 -0400466std::tuple<GrSamplerState::Filter,
467 GrSamplerState::MipmapMode,
Mike Reed52130b02020-12-28 15:33:13 -0500468 SkCubicResampler>
469GrInterpretSamplingOptions(SkISize imageDims,
470 const SkSamplingOptions& sampling,
471 const SkMatrix& viewM,
472 const SkMatrix& localM,
473 bool sharpenMipmappedTextures,
474 bool allowFilterQualityReduction) {
Mike Reed95b5fb92020-11-23 19:51:24 +0000475 using Filter = GrSamplerState::Filter;
476 using MipmapMode = GrSamplerState::MipmapMode;
Mike Reed52130b02020-12-28 15:33:13 -0500477
478 if (sampling.useCubic) {
479 SkASSERT(GrValidCubicResampler(sampling.cubic));
Mike Reed6e734042021-02-12 15:30:19 -0500480 return {Filter::kNearest, MipmapMode::kNone, sampling.cubic};
joshualitt9bc39542015-08-12 12:57:54 -0700481 }
Mike Reed52130b02020-12-28 15:33:13 -0500482
Mike Reedfe4611c2020-12-29 12:23:46 -0500483 Filter f = sampling.filter;
484 MipmapMode m = sampling.mipmap;
485 if (allowFilterQualityReduction && (m != MipmapMode::kNone)) {
Mike Reedffe5d542020-12-28 20:49:40 -0500486 SkMatrix matrix;
487 matrix.setConcat(viewM, localM);
488 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until
489 // the computed LOD is >= 0.5. At what scale factor does a texture get an LOD of
490 // 0.5?
491 //
492 // Want: 0 = log2(1/s) - 0.5
493 // 0.5 = log2(1/s)
494 // 2^0.5 = 1/s
495 // 1/2^0.5 = s
496 // 2^0.5/2 = s
497 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
498 if (matrix.getMinScale() >= mipScale) {
499 m = MipmapMode::kNone;
Mike Reed52130b02020-12-28 15:33:13 -0500500 }
501 }
502 return {f, m, kInvalidCubicResampler};
joshualitt9bc39542015-08-12 12:57:54 -0700503}