blob: 4e615b160c240487fe56231efea03e72c2a28ee3 [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"
Brian Salomone69b9ef2020-07-22 11:18:06 -040027#include "src/core/SkMatrixPriv.h"
Ben Wagner21bca282019-05-15 10:15:52 -040028#include "src/core/SkMessageBus.h"
Mike Reed13711eb2020-07-14 17:16:32 -040029#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/core/SkPaintPriv.h"
31#include "src/core/SkResourceCache.h"
32#include "src/core/SkTraceEvent.h"
33#include "src/gpu/GrBitmapTextureMaker.h"
34#include "src/gpu/GrCaps.h"
35#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrGpuResourcePriv.h"
37#include "src/gpu/GrPaint.h"
38#include "src/gpu/GrProxyProvider.h"
39#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040040#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrXferProcessor.h"
Brian Salomone69b9ef2020-07-22 11:18:06 -040042#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -040044#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050046#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/effects/generated/GrConstColorProcessor.h"
John Stiles53ee0fc2020-07-09 15:29:33 -040048#include "src/gpu/effects/generated/GrDitherEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/image/SkImage_Base.h"
50#include "src/shaders/SkShaderBase.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040051
bsalomon045802d2015-10-20 07:58:01 -070052void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
53 SkASSERT(key);
54 SkASSERT(imageID);
55 SkASSERT(!imageBounds.isEmpty());
56 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040057 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -070058 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070059 builder[1] = imageBounds.fLeft;
60 builder[2] = imageBounds.fTop;
61 builder[3] = imageBounds.fRight;
62 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070063}
64
bsalomon045802d2015-10-20 07:58:01 -070065////////////////////////////////////////////////////////////////////////////////
66
Brian Salomon99a813c2020-03-02 12:50:47 -050067sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key,
68 uint32_t contextID) {
69 class Listener : public SkIDChangeListener {
70 public:
71 Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {}
72
73 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
74
75 private:
76 GrUniqueKeyInvalidatedMessage fMsg;
77 };
78
79 auto listener = sk_make_sp<Listener>(*key, contextID);
80
81 // We stick a SkData on the key that calls invalidateListener in its destructor.
82 auto invalidateListener = [](const void* ptr, void* /*context*/) {
83 auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr);
84 (*listener)->markShouldDeregister();
85 delete listener;
86 };
87 auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener),
88 sizeof(sk_sp<Listener>),
89 invalidateListener,
90 nullptr);
91 SkASSERT(!key->getCustomData());
92 key->setCustomData(std::move(data));
93 return std::move(listener);
94}
95
Brian Salomonc5243782020-04-02 12:50:34 -040096sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
97 GrSurfaceProxy* baseProxy,
98 GrSurfaceOrigin origin,
99 SkBudgeted budgeted) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400100 SkASSERT(baseProxy);
101
Greg Daniel6980c4e2019-07-30 16:18:18 -0400102 if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
Greg Danielcc104db2020-02-03 14:17:08 -0500103 return {};
Greg Danielbb76ace2017-09-29 15:58:22 -0400104 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400105 auto copy = GrSurfaceProxy::Copy(ctx, baseProxy, origin, GrMipmapped::kYes,
Brian Salomonc5243782020-04-02 12:50:34 -0400106 SkBackingFit::kExact, budgeted);
107 if (!copy) {
108 return {};
109 }
110 SkASSERT(copy->asTextureProxy());
111 return copy;
112}
113
114GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext* context,
115 GrSurfaceProxyView src,
116 SkBudgeted budgeted) {
117 auto origin = src.origin();
118 auto swizzle = src.swizzle();
119 auto* proxy = src.proxy();
120 return {GrCopyBaseMipMapToTextureProxy(context, proxy, origin, budgeted), origin, swizzle};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400121}
122
Greg Danielcc21d0c2020-02-05 16:58:40 -0500123GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400124 GrMipmapped mipMapped) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400125 GrBitmapTextureMaker maker(ctx, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500126 return maker.view(mipMapped);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400127}
128
Brian Salomonbc074a62020-03-18 10:06:13 -0400129GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context,
130 const SkBitmap& bitmap) {
Greg Daniel7e1912a2018-02-08 09:15:33 -0500131 if (!bitmap.peekPixels(nullptr)) {
Greg Danielc52db712020-01-28 17:03:46 -0500132 return {};
Robert Phillipse14d3052017-02-15 13:18:21 -0500133 }
134
Brian Salomonbc074a62020-03-18 10:06:13 -0400135 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kDraw);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400136 return maker.view(GrMipmapped::kNo);
Robert Phillips7a926392018-02-01 15:49:54 -0500137}
138
rileya@google.com24f3ad12012-07-18 21:47:40 +0000139///////////////////////////////////////////////////////////////////////////////
140
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400141SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400142 SkColor4f color = SkColor4f::FromColor(c);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400143 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400144 color = xform->apply(color);
145 }
146 return color.premul();
147}
148
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400149SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) {
150 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
Brian Osman5c8a6b32018-11-19 11:56:57 -0500151 color = xform->apply(color);
152 }
Brian Osman5c8a6b32018-11-19 11:56:57 -0500153 return color;
154}
155
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400156///////////////////////////////////////////////////////////////////////////////
157
Mike Reed185ba212017-04-28 12:31:05 -0400158static inline bool blend_requires_shader(const SkBlendMode mode) {
159 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700160}
161
Ethan Nicholas00543112018-07-31 09:44:36 -0400162#ifndef SK_IGNORE_GPU_DITHER
Michael Ludwig87f57552020-05-27 14:32:38 -0400163static inline float dither_range_for_config(GrColorType dstColorType) {
164 // We use 1 / (2^bitdepth-1) as the range since each channel can hold 2^bitdepth values
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400165 switch (dstColorType) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400166 // 4 bit
167 case GrColorType::kABGR_4444:
Greg Daniel746460e2020-06-30 13:13:53 -0400168 case GrColorType::kARGB_4444:
169 case GrColorType::kBGRA_4444:
Michael Ludwig87f57552020-05-27 14:32:38 -0400170 return 1 / 15.f;
171 // 6 bit
172 case GrColorType::kBGR_565:
173 return 1 / 63.f;
174 // 8 bit
Brian Salomon85c3d682019-11-04 15:04:54 -0500175 case GrColorType::kUnknown:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400176 case GrColorType::kAlpha_8:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400177 case GrColorType::kAlpha_8xxx:
Michael Ludwig87f57552020-05-27 14:32:38 -0400178 case GrColorType::kGray_8:
Brian Salomon01ff5382020-12-15 16:06:26 -0500179 case GrColorType::kGrayAlpha_88:
Brian Salomon8f8354a2019-07-31 20:12:02 -0400180 case GrColorType::kGray_8xxx:
Brian Salomon85c3d682019-11-04 15:04:54 -0500181 case GrColorType::kR_8:
Michael Ludwig87f57552020-05-27 14:32:38 -0400182 case GrColorType::kRG_88:
183 case GrColorType::kRGB_888:
184 case GrColorType::kRGB_888x:
185 case GrColorType::kRGBA_8888:
186 case GrColorType::kRGBA_8888_SRGB:
187 case GrColorType::kBGRA_8888:
188 return 1 / 255.f;
189 // 10 bit
190 case GrColorType::kRGBA_1010102:
191 case GrColorType::kBGRA_1010102:
192 return 1 / 1023.f;
193 // 16 bit
194 case GrColorType::kAlpha_16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500195 case GrColorType::kR_16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400196 case GrColorType::kRG_1616:
197 case GrColorType::kRGBA_16161616:
198 return 1 / 32767.f;
199 // Half
200 case GrColorType::kAlpha_F16:
Brian Salomon85c3d682019-11-04 15:04:54 -0500201 case GrColorType::kGray_F16:
Michael Ludwig87f57552020-05-27 14:32:38 -0400202 case GrColorType::kR_F16:
203 case GrColorType::kRG_F16:
204 case GrColorType::kRGBA_F16:
205 case GrColorType::kRGBA_F16_Clamped:
206 // Float
207 case GrColorType::kAlpha_F32xxx:
208 case GrColorType::kRGBA_F32:
209 return 0.f; // no dithering
Ethan Nicholas00543112018-07-31 09:44:36 -0400210 }
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400211 SkUNREACHABLE;
Ethan Nicholas00543112018-07-31 09:44:36 -0400212}
213#endif
214
Robert Phillips9338c602019-02-19 12:52:29 -0500215static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400216 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700217 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400218 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400219 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400220 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700221 GrPaint* grPaint) {
Mike Reed52130b02020-12-28 15:33:13 -0500222 // TODO: take sampling directly
223 SkSamplingOptions sampling(SkPaintPriv::GetFQ(skPaint),
224 SkSamplingOptions::kMedium_asMipmapLinear);
225
Brian Osman08a50e02018-06-15 15:06:48 -0400226 // Convert SkPaint color to 4f format in the destination color space
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400227 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo);
brianosmana4535a32016-06-24 12:50:19 -0700228
Mike Reed52130b02020-12-28 15:33:13 -0500229 GrFPArgs fpArgs(context, matrixProvider, sampling, &dstColorInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000230
bsalomonf1b7a1d2015-09-28 06:26:28 -0700231 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
232 // of per-vertex colors.
John Stiles7f9aa5a2020-06-23 11:28:50 -0400233 std::unique_ptr<GrFragmentProcessor> paintFP;
Mike Reed185ba212017-04-28 12:31:05 -0400234 if (!primColorMode || blend_requires_shader(*primColorMode)) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400235 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
bsalomonaa48d362015-10-01 08:34:17 -0700236 if (shaderProcessor) {
John Stiles7f9aa5a2020-06-23 11:28:50 -0400237 paintFP = std::move(*shaderProcessor);
238 } else {
239 if (const SkShaderBase* shader = as_SB(skPaint.getShader())) {
240 paintFP = shader->asFragmentProcessor(fpArgs);
241 if (paintFP == nullptr) {
242 return false;
243 }
Yuqian Liaad2ec62018-02-26 10:34:52 -0500244 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700245 }
246 }
247
248 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
249 // a known constant value. In that case we can simply apply a color filter during this
250 // conversion without converting the color filter to a GrFragmentProcessor.
251 bool applyColorFilterToPaintColor = false;
John Stiles7f9aa5a2020-06-23 11:28:50 -0400252 if (paintFP) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700253 if (primColorMode) {
254 // There is a blend between the primitive color and the shader color. The shader sees
255 // the opaque paint color. The shader's output is blended using the provided mode by
256 // the primitive color. The blended color is then modulated by the paint's alpha.
257
258 // The geometry processor will insert the primitive color to start the color chain, so
259 // the GrPaint color will be ignored.
260
Brian Osmancb3d0872018-10-16 15:19:28 -0400261 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
John Stiles7f9aa5a2020-06-23 11:28:50 -0400262 paintFP = GrFragmentProcessor::OverrideInput(std::move(paintFP), shaderInput);
John Stilesf743d4e2020-07-23 11:35:08 -0400263 paintFP = GrBlendFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
264 *primColorMode);
Mike Reed185ba212017-04-28 12:31:05 -0400265
brianosmana4535a32016-06-24 12:50:19 -0700266 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500267 float paintAlpha = skPaint.getColor4f().fA;
268 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400269 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
270 // color channels. It's value should be treated as the same in ANY color space.
John Stiles85894302020-07-13 11:39:52 -0400271 paintFP = GrFragmentProcessor::ModulateRGBA(
John Stiles7c196772020-07-13 10:00:16 -0400272 std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha});
John Stilese3a39f72020-06-15 13:58:48 -0400273 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700274 } else {
Brian Osmane7809c72020-07-30 16:49:54 -0400275 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700276 }
277 } else {
278 if (primColorMode) {
279 // There is a blend between the primitive color and the paint color. The blend considers
280 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400281 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
John Stiles7c196772020-07-13 10:00:16 -0400282 paintFP = GrConstColorProcessor::Make(opaqueColor);
John Stilesf743d4e2020-07-23 11:35:08 -0400283 paintFP = GrBlendFragmentProcessor::Make(std::move(paintFP), /*dst=*/nullptr,
284 *primColorMode);
Brian Osmancb3d0872018-10-16 15:19:28 -0400285 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700286
brianosmana4535a32016-06-24 12:50:19 -0700287 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500288 float paintAlpha = skPaint.getColor4f().fA;
289 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400290 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
291 // color channels. It's value should be treated as the same in ANY color space.
John Stiles85894302020-07-13 11:39:52 -0400292 paintFP = GrFragmentProcessor::ModulateRGBA(
John Stiles7c196772020-07-13 10:00:16 -0400293 std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha});
John Stilese3a39f72020-06-15 13:58:48 -0400294 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700295 } else {
296 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700297 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700298 applyColorFilterToPaintColor = true;
299 }
300 }
301
302 SkColorFilter* colorFilter = skPaint.getColorFilter();
303 if (colorFilter) {
304 if (applyColorFilterToPaintColor) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400305 SkColorSpace* dstCS = dstColorInfo.colorSpace();
Mike Reedbd843302019-09-27 21:05:24 -0400306 grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700307 } else {
John Stilesc36b8aa2020-06-25 12:47:52 -0400308 auto [success, fp] = as_CFB(colorFilter)->asFragmentProcessor(std::move(paintFP),
309 context, dstColorInfo);
310 if (!success) {
bsalomonf1b7a1d2015-09-28 06:26:28 -0700311 return false;
312 }
John Stilesc36b8aa2020-06-25 12:47:52 -0400313 paintFP = std::move(fp);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700314 }
315 }
316
Mike Reed80747ef2018-01-23 15:29:32 -0500317 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400318 if (maskFilter) {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400319 // We may have set this before passing to the SkShader.
320 fpArgs.fInputColorIsOpaque = false;
Mike Reedbfadcf02018-01-20 22:24:21 +0000321 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
John Stiles41d91b62020-07-21 14:39:40 -0400322 grPaint->setCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400323 }
324 }
325
robertphillips4f037942016-02-09 05:09:27 -0800326 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
327 // the GrPaint to also be null (also kSrcOver).
328 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700329 if (!skPaint.isSrcOver()) {
330 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800331 }
mtklein775b8192014-12-02 09:11:25 -0800332
krajcevskif461a8f2014-06-19 14:14:06 -0700333#ifndef SK_IGNORE_GPU_DITHER
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400334 GrColorType ct = dstColorInfo.colorType();
John Stiles53ee0fc2020-07-09 15:29:33 -0400335 if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) && paintFP != nullptr) {
Michael Ludwig87f57552020-05-27 14:32:38 -0400336 float ditherRange = dither_range_for_config(ct);
John Stiles53ee0fc2020-07-09 15:29:33 -0400337 paintFP = GrDitherEffect::Make(std::move(paintFP), ditherRange);
krajcevskif461a8f2014-06-19 14:14:06 -0700338 }
339#endif
John Stiles53ee0fc2020-07-09 15:29:33 -0400340
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400341 if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) {
John Stilesd4eb2ec2020-07-09 15:38:08 -0400342 if (paintFP != nullptr) {
343 paintFP = GrClampFragmentProcessor::Make(std::move(paintFP), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400344 } else {
345 auto color = grPaint->getColor4f();
346 grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f),
347 SkTPin(color.fG, 0.f, 1.f),
348 SkTPin(color.fB, 0.f, 1.f),
349 SkTPin(color.fA, 0.f, 1.f)});
350 }
351 }
John Stilesd4eb2ec2020-07-09 15:38:08 -0400352
353 if (paintFP) {
John Stiles5933d7d2020-07-21 12:28:35 -0400354 grPaint->setColorFragmentProcessor(std::move(paintFP));
John Stilesd4eb2ec2020-07-09 15:38:08 -0400355 }
356
bsalomonbed83a62015-04-15 14:18:34 -0700357 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000358}
359
Brian Osman449b1152020-04-15 16:43:00 -0400360bool SkPaintToGrPaint(GrRecordingContext* context,
361 const GrColorInfo& dstColorInfo,
362 const SkPaint& skPaint,
363 const SkMatrixProvider& matrixProvider,
364 GrPaint* grPaint) {
John Stilesf743d4e2020-07-23 11:35:08 -0400365 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider,
366 /*shaderProcessor=*/nullptr, /*primColorMode=*/nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700367}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000368
bsalomonf1b7a1d2015-09-28 06:26:28 -0700369/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500370bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400371 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700372 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400373 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400374 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700375 GrPaint* grPaint) {
376 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700377 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000378 }
Brian Osman449b1152020-04-15 16:43:00 -0400379 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP,
John Stilesf743d4e2020-07-23 11:35:08 -0400380 /*primColorMode=*/nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000381}
reed8b26b992015-05-07 15:36:17 -0700382
bsalomonf1b7a1d2015-09-28 06:26:28 -0700383/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500384bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400385 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700386 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400387 const SkMatrixProvider& matrixProvider,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700388 GrPaint* grPaint) {
389 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400390 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
Brian Osman449b1152020-04-15 16:43:00 -0400391 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &nullShaderFP,
John Stilesf743d4e2020-07-23 11:35:08 -0400392 /*primColorMode=*/nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700393}
394
395/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
John Stilesf2f9b0d2020-08-25 13:33:45 -0400396 be setup as a vertex attribute using the specified SkBlendMode. */
John Stilesf743d4e2020-07-23 11:35:08 -0400397bool SkPaintToGrPaintWithBlend(GrRecordingContext* context,
398 const GrColorInfo& dstColorInfo,
399 const SkPaint& skPaint,
400 const SkMatrixProvider& matrixProvider,
401 SkBlendMode primColorMode,
402 GrPaint* grPaint) {
403 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider,
404 /*shaderProcessor=*/nullptr, &primColorMode, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700405}
406
John Stilesf2f9b0d2020-08-25 13:33:45 -0400407/** Blends the passed-in shader with a per-primitive color which must be setup as a vertex attribute
408 using the specified SkBlendMode. */
409bool SkPaintToGrPaintWithBlendReplaceShader(GrRecordingContext* context,
410 const GrColorInfo& dstColorInfo,
411 const SkPaint& skPaint,
412 const SkMatrixProvider& matrixProvider,
413 std::unique_ptr<GrFragmentProcessor> shaderFP,
414 SkBlendMode primColorMode,
415 GrPaint* grPaint) {
416 if (!shaderFP) {
417 return false;
418 }
419 return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP,
420 &primColorMode, grPaint);
421}
422
Robert Phillips9338c602019-02-19 12:52:29 -0500423bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400424 const GrColorInfo& dstColorInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800425 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400426 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400427 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800428 bool textureIsAlphaOnly,
429 GrPaint* grPaint) {
Mike Reed52130b02020-12-28 15:33:13 -0500430 // TODO: take sampling directly
431 SkSamplingOptions sampling(SkPaintPriv::GetFQ(paint),
432 SkSamplingOptions::kMedium_asMipmapLinear);
433
Brian Salomonaff329b2017-08-11 09:40:37 -0400434 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800435 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400436 if (const auto* shader = as_SB(paint.getShader())) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400437 shaderFP = shader->asFragmentProcessor(
Mike Reed52130b02020-12-28 15:33:13 -0500438 GrFPArgs(context, matrixProvider, sampling, &dstColorInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800439 if (!shaderFP) {
440 return false;
441 }
John Stiles024d7452020-07-22 19:07:15 -0400442 shaderFP = GrFragmentProcessor::Compose(std::move(shaderFP), std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800443 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400444 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800445 }
446 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -0400447 if (paint.getColor4f().isOpaque()) {
448 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
449 } else {
450 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
451 }
joshualitt33a5fce2015-11-18 13:28:51 -0800452 }
453
Brian Osman449b1152020-04-15 16:43:00 -0400454 return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, matrixProvider,
455 std::move(shaderFP), grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800456}
457
bsalomonf1b7a1d2015-09-28 06:26:28 -0700458////////////////////////////////////////////////////////////////////////////////////////////////
459
Brian Salomone69b9ef2020-07-22 11:18:06 -0400460std::tuple<GrSamplerState::Filter,
461 GrSamplerState::MipmapMode,
Mike Reed52130b02020-12-28 15:33:13 -0500462 SkCubicResampler>
463GrInterpretSamplingOptions(SkISize imageDims,
464 const SkSamplingOptions& sampling,
465 const SkMatrix& viewM,
466 const SkMatrix& localM,
467 bool sharpenMipmappedTextures,
468 bool allowFilterQualityReduction) {
Mike Reed95b5fb92020-11-23 19:51:24 +0000469 using Filter = GrSamplerState::Filter;
470 using MipmapMode = GrSamplerState::MipmapMode;
Mike Reed52130b02020-12-28 15:33:13 -0500471
472 if (sampling.useCubic) {
473 SkASSERT(GrValidCubicResampler(sampling.cubic));
474 if (allowFilterQualityReduction) {
475 SkMatrix matrix;
476 matrix.setConcat(viewM, localM);
477 switch (SkMatrixPriv::AdjustHighQualityFilterLevel(matrix)) {
478 case kNone_SkFilterQuality: return {Filter::kNearest, MipmapMode::kNone , kInvalidCubicResampler};
479 case kLow_SkFilterQuality: return {Filter::kLinear , MipmapMode::kNone , kInvalidCubicResampler};
480 case kMedium_SkFilterQuality: return {Filter::kLinear , MipmapMode::kLinear, kInvalidCubicResampler};
481 case kHigh_SkFilterQuality: return {Filter::kNearest, MipmapMode::kNone , sampling.cubic};
joshualitt9bc39542015-08-12 12:57:54 -0700482 }
Mike Reed52130b02020-12-28 15:33:13 -0500483 } else {
484 return {Filter::kNearest, MipmapMode::kNone, sampling.cubic};
Mike Reed95b5fb92020-11-23 19:51:24 +0000485 }
joshualitt9bc39542015-08-12 12:57:54 -0700486 }
Mike Reed52130b02020-12-28 15:33:13 -0500487
Mike Reedfe4611c2020-12-29 12:23:46 -0500488 Filter f = sampling.filter;
489 MipmapMode m = sampling.mipmap;
490 if (allowFilterQualityReduction && (m != MipmapMode::kNone)) {
Mike Reedffe5d542020-12-28 20:49:40 -0500491 SkMatrix matrix;
492 matrix.setConcat(viewM, localM);
493 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until
494 // the computed LOD is >= 0.5. At what scale factor does a texture get an LOD of
495 // 0.5?
496 //
497 // Want: 0 = log2(1/s) - 0.5
498 // 0.5 = log2(1/s)
499 // 2^0.5 = 1/s
500 // 1/2^0.5 = s
501 // 2^0.5/2 = s
502 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
503 if (matrix.getMinScale() >= mipScale) {
504 m = MipmapMode::kNone;
Mike Reed52130b02020-12-28 15:33:13 -0500505 }
506 }
507 return {f, m, kInvalidCubicResampler};
joshualitt9bc39542015-08-12 12:57:54 -0700508}