1 /*
2 * 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.
6 */
7
8 #include "SkGr.h"
9 #include "GrBitmapTextureMaker.h"
10 #include "GrCaps.h"
11 #include "GrColorSpaceXform.h"
12 #include "GrContext.h"
13 #include "GrContextPriv.h"
14 #include "GrGpuResourcePriv.h"
15 #include "GrPaint.h"
16 #include "GrProxyProvider.h"
17 #include "GrRecordingContext.h"
18 #include "GrRecordingContextPriv.h"
19 #include "GrTextureProxy.h"
20 #include "GrTypes.h"
21 #include "GrXferProcessor.h"
22 #include "SkAutoMalloc.h"
23 #include "SkBlendModePriv.h"
24 #include "SkCanvas.h"
25 #include "SkColorFilter.h"
26 #include "SkData.h"
27 #include "SkImage_Base.h"
28 #include "SkImageInfoPriv.h"
29 #include "SkImagePriv.h"
30 #include "SkMaskFilterBase.h"
31 #include "SkMessageBus.h"
32 #include "SkMipMap.h"
33 #include "SkPaintPriv.h"
34 #include "SkPixelRef.h"
35 #include "SkResourceCache.h"
36 #include "SkShaderBase.h"
37 #include "SkTemplates.h"
38 #include "SkTraceEvent.h"
39 #include "effects/GrBicubicEffect.h"
40 #include "effects/GrConstColorProcessor.h"
41 #include "effects/GrPorterDuffXferProcessor.h"
42 #include "effects/GrXfermodeFragmentProcessor.h"
43 #include "effects/GrSkSLFP.h"
44
45 #if SK_SUPPORT_GPU
46 GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
47 // This controls the range of values added to color channels
48 layout(key) in int rangeType;
49
50 void main(int x, int y, inout half4 color) {
51 half value;
52 half range;
53 @switch (rangeType) {
54 case 0:
55 range = 1.0 / 255.0;
56 break;
57 case 1:
58 range = 1.0 / 63.0;
59 break;
60 default:
61 // Experimentally this looks better than the expected value of 1/15.
62 range = 1.0 / 15.0;
63 break;
64 }
65 @if (sk_Caps.integerSupport) {
66 // This ordered-dither code is lifted from the cpu backend.
67 uint x = uint(x);
68 uint y = uint(y);
69 uint m = (y & 1) << 5 | (x & 1) << 4 |
70 (y & 2) << 2 | (x & 2) << 1 |
71 (y & 4) >> 1 | (x & 4) >> 2;
72 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
73 } else {
74 // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
75 // dither pattern rather than an 8x8 one.
76 half4 modValues = mod(half4(x, y, x, y), half4(2.0, 2.0, 4.0, 4.0));
77 half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
78 value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
79 }
80 // For each color channel, add the random offset to the channel value and then clamp
81 // between 0 and alpha to keep the color premultiplied.
82 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
83 }
84 )";
85 #endif
86
GrImageInfoToSurfaceDesc(const SkImageInfo & info)87 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
88 GrSurfaceDesc desc;
89 desc.fFlags = kNone_GrSurfaceFlags;
90 desc.fWidth = info.width();
91 desc.fHeight = info.height();
92 desc.fConfig = SkImageInfo2GrPixelConfig(info);
93 desc.fSampleCnt = 1;
94 return desc;
95 }
96
GrMakeKeyFromImageID(GrUniqueKey * key,uint32_t imageID,const SkIRect & imageBounds)97 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
98 SkASSERT(key);
99 SkASSERT(imageID);
100 SkASSERT(!imageBounds.isEmpty());
101 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
102 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
103 builder[0] = imageID;
104 builder[1] = imageBounds.fLeft;
105 builder[2] = imageBounds.fTop;
106 builder[3] = imageBounds.fRight;
107 builder[4] = imageBounds.fBottom;
108 }
109
110 //////////////////////////////////////////////////////////////////////////////
GrUploadBitmapToTextureProxy(GrProxyProvider * proxyProvider,const SkBitmap & bitmap)111 sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider* proxyProvider,
112 const SkBitmap& bitmap) {
113 if (!bitmap.peekPixels(nullptr)) {
114 return nullptr;
115 }
116
117 if (!SkImageInfoIsValid(bitmap.info())) {
118 return nullptr;
119 }
120
121 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
122 // even if it's mutable. In ddl, if the bitmap is mutable then we must make a copy since the
123 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
124 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
125 : kIfMutable_SkCopyPixelsMode;
126 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
127
128 return proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
129 SkBudgeted::kYes, SkBackingFit::kExact);
130 }
131
132 ////////////////////////////////////////////////////////////////////////////////
133
GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey & key,uint32_t contextUniqueID,SkPixelRef * pixelRef)134 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
135 SkPixelRef* pixelRef) {
136 class Invalidator : public SkPixelRef::GenIDChangeListener {
137 public:
138 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
139 : fMsg(key, contextUniqueID) {}
140
141 private:
142 GrUniqueKeyInvalidatedMessage fMsg;
143
144 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
145 };
146
147 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
148 }
149
GrCopyBaseMipMapToTextureProxy(GrRecordingContext * ctx,GrTextureProxy * baseProxy)150 sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
151 GrTextureProxy* baseProxy) {
152 SkASSERT(baseProxy);
153
154 if (!ctx->priv().caps()->isConfigCopyable(baseProxy->config())) {
155 return nullptr;
156 }
157
158 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
159 GrSurfaceDesc desc;
160 desc.fFlags = kNone_GrSurfaceFlags;
161 desc.fWidth = baseProxy->width();
162 desc.fHeight = baseProxy->height();
163 desc.fConfig = baseProxy->config();
164 desc.fSampleCnt = 1;
165
166 GrBackendFormat format = baseProxy->backendFormat().makeTexture2D();
167 if (!format.isValid()) {
168 return nullptr;
169 }
170
171 sk_sp<GrTextureProxy> proxy =
172 proxyProvider->createMipMapProxy(format, desc, baseProxy->origin(), SkBudgeted::kYes);
173 if (!proxy) {
174 return nullptr;
175 }
176
177 // Copy the base layer to our proxy
178 sk_sp<GrSurfaceContext> sContext = ctx->priv().makeWrappedSurfaceContext(proxy);
179 SkASSERT(sContext);
180 SkAssertResult(sContext->copy(baseProxy));
181
182 return proxy;
183 }
184
GrRefCachedBitmapTextureProxy(GrRecordingContext * ctx,const SkBitmap & bitmap,const GrSamplerState & params,SkScalar scaleAdjust[2])185 sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
186 const SkBitmap& bitmap,
187 const GrSamplerState& params,
188 SkScalar scaleAdjust[2]) {
189 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
190 }
191
GrMakeCachedBitmapProxy(GrProxyProvider * proxyProvider,const SkBitmap & bitmap,SkBackingFit fit)192 sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
193 const SkBitmap& bitmap,
194 SkBackingFit fit) {
195 if (!bitmap.peekPixels(nullptr)) {
196 return nullptr;
197 }
198
199 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
200 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
201 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
202 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
203 : kIfMutable_SkCopyPixelsMode;
204 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
205
206 if (!image) {
207 return nullptr;
208 }
209
210 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
211 }
212
create_unique_key_for_image(const SkImage * image,GrUniqueKey * result)213 static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
214 if (!image) {
215 result->reset(); // will be invalid
216 return;
217 }
218
219 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
220 if (!bm->isVolatile()) {
221 SkIPoint origin = bm->pixelRefOrigin();
222 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
223 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
224 }
225 return;
226 }
227
228 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
229 }
230
GrMakeCachedImageProxy(GrProxyProvider * proxyProvider,sk_sp<SkImage> srcImage,SkBackingFit fit)231 sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
232 sk_sp<SkImage> srcImage,
233 SkBackingFit fit) {
234 sk_sp<GrTextureProxy> proxy;
235 GrUniqueKey originalKey;
236
237 create_unique_key_for_image(srcImage.get(), &originalKey);
238
239 if (originalKey.isValid()) {
240 proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin);
241 }
242 if (!proxy) {
243 proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1,
244 SkBudgeted::kYes, fit);
245 if (proxy && originalKey.isValid()) {
246 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
247 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
248 // When recording DDLs we do not want to install change listeners because doing
249 // so isn't threadsafe.
250 if (bm && proxyProvider->renderingDirectly()) {
251 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextID(),
252 bm->pixelRef());
253 }
254 }
255 }
256
257 return proxy;
258 }
259
260 ///////////////////////////////////////////////////////////////////////////////
261
SkColorToPMColor4f(SkColor c,const GrColorSpaceInfo & colorSpaceInfo)262 SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
263 SkColor4f color = SkColor4f::FromColor(c);
264 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
265 color = xform->apply(color);
266 }
267 return color.premul();
268 }
269
SkColor4fPrepForDst(SkColor4f color,const GrColorSpaceInfo & colorSpaceInfo,const GrCaps & caps)270 SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorSpaceInfo& colorSpaceInfo,
271 const GrCaps& caps) {
272 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
273 color = xform->apply(color);
274 }
275 // TODO: Should we clamp here if config is kRGBA_half_Clamped_GrPixelConfig?
276 if (!GrPixelConfigIsFloatingPoint(colorSpaceInfo.config()) ||
277 !caps.halfFloatVertexAttributeSupport()) {
278 color = { SkTPin(color.fR, 0.0f, 1.0f),
279 SkTPin(color.fG, 0.0f, 1.0f),
280 SkTPin(color.fB, 0.0f, 1.0f),
281 color.fA };
282 }
283 return color;
284 }
285
286 ///////////////////////////////////////////////////////////////////////////////
287
SkColorType2GrPixelConfig(const SkColorType type)288 GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
289 switch (type) {
290 case kUnknown_SkColorType:
291 return kUnknown_GrPixelConfig;
292 case kAlpha_8_SkColorType:
293 return kAlpha_8_GrPixelConfig;
294 case kRGB_565_SkColorType:
295 return kRGB_565_GrPixelConfig;
296 case kARGB_4444_SkColorType:
297 return kRGBA_4444_GrPixelConfig;
298 case kRGBA_8888_SkColorType:
299 return kRGBA_8888_GrPixelConfig;
300 case kRGB_888x_SkColorType:
301 return kRGB_888_GrPixelConfig;
302 case kBGRA_8888_SkColorType:
303 return kBGRA_8888_GrPixelConfig;
304 case kRGBA_1010102_SkColorType:
305 return kRGBA_1010102_GrPixelConfig;
306 case kRGB_101010x_SkColorType:
307 return kUnknown_GrPixelConfig;
308 case kGray_8_SkColorType:
309 return kGray_8_GrPixelConfig;
310 case kRGBA_F16Norm_SkColorType:
311 return kRGBA_half_Clamped_GrPixelConfig;
312 case kRGBA_F16_SkColorType:
313 return kRGBA_half_GrPixelConfig;
314 case kRGBA_F32_SkColorType:
315 return kRGBA_float_GrPixelConfig;
316 }
317 SkASSERT(0); // shouldn't get here
318 return kUnknown_GrPixelConfig;
319 }
320
SkImageInfo2GrPixelConfig(const SkImageInfo & info)321 GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
322 return SkColorType2GrPixelConfig(info.colorType());
323 }
324
GrPixelConfigToColorType(GrPixelConfig config,SkColorType * ctOut)325 bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
326 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
327 if (kUnknown_SkColorType != ct) {
328 if (ctOut) {
329 *ctOut = ct;
330 }
331 return true;
332 }
333 return false;
334 }
335
336 ////////////////////////////////////////////////////////////////////////////////////////////////
337
blend_requires_shader(const SkBlendMode mode)338 static inline bool blend_requires_shader(const SkBlendMode mode) {
339 return SkBlendMode::kDst != mode;
340 }
341
342 #ifndef SK_IGNORE_GPU_DITHER
dither_range_type_for_config(GrPixelConfig dstConfig)343 static inline int32_t dither_range_type_for_config(GrPixelConfig dstConfig) {
344 switch (dstConfig) {
345 case kGray_8_GrPixelConfig:
346 case kGray_8_as_Lum_GrPixelConfig:
347 case kGray_8_as_Red_GrPixelConfig:
348 case kRGBA_8888_GrPixelConfig:
349 case kRGB_888_GrPixelConfig:
350 case kRGB_888X_GrPixelConfig:
351 case kRG_88_GrPixelConfig:
352 case kBGRA_8888_GrPixelConfig:
353 return 0;
354 case kRGB_565_GrPixelConfig:
355 return 1;
356 case kRGBA_4444_GrPixelConfig:
357 return 2;
358 case kUnknown_GrPixelConfig:
359 case kSRGBA_8888_GrPixelConfig:
360 case kSBGRA_8888_GrPixelConfig:
361 case kRGBA_1010102_GrPixelConfig:
362 case kAlpha_half_GrPixelConfig:
363 case kAlpha_half_as_Red_GrPixelConfig:
364 case kRGBA_float_GrPixelConfig:
365 case kRG_float_GrPixelConfig:
366 case kRGBA_half_GrPixelConfig:
367 case kRGBA_half_Clamped_GrPixelConfig:
368 case kRGB_ETC1_GrPixelConfig:
369 case kAlpha_8_GrPixelConfig:
370 case kAlpha_8_as_Alpha_GrPixelConfig:
371 case kAlpha_8_as_Red_GrPixelConfig:
372 return -1;
373 }
374 SkASSERT(false);
375 return 0;
376 }
377 #endif
378
skpaint_to_grpaint_impl(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,const SkMatrix & viewM,std::unique_ptr<GrFragmentProcessor> * shaderProcessor,SkBlendMode * primColorMode,GrPaint * grPaint)379 static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
380 const GrColorSpaceInfo& colorSpaceInfo,
381 const SkPaint& skPaint,
382 const SkMatrix& viewM,
383 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
384 SkBlendMode* primColorMode,
385 GrPaint* grPaint) {
386 // Convert SkPaint color to 4f format in the destination color space
387 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), colorSpaceInfo,
388 *context->priv().caps());
389
390 GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
391
392 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
393 // of per-vertex colors.
394 std::unique_ptr<GrFragmentProcessor> shaderFP;
395 if (!primColorMode || blend_requires_shader(*primColorMode)) {
396 fpArgs.fInputColorIsOpaque = origColor.isOpaque();
397 if (shaderProcessor) {
398 shaderFP = std::move(*shaderProcessor);
399 } else if (const auto* shader = as_SB(skPaint.getShader())) {
400 shaderFP = shader->asFragmentProcessor(fpArgs);
401 if (!shaderFP) {
402 return false;
403 }
404 }
405 }
406
407 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
408 // a known constant value. In that case we can simply apply a color filter during this
409 // conversion without converting the color filter to a GrFragmentProcessor.
410 bool applyColorFilterToPaintColor = false;
411 if (shaderFP) {
412 if (primColorMode) {
413 // There is a blend between the primitive color and the shader color. The shader sees
414 // the opaque paint color. The shader's output is blended using the provided mode by
415 // the primitive color. The blended color is then modulated by the paint's alpha.
416
417 // The geometry processor will insert the primitive color to start the color chain, so
418 // the GrPaint color will be ignored.
419
420 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
421 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
422 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
423 *primColorMode);
424
425 // The above may return null if compose results in a pass through of the prim color.
426 if (shaderFP) {
427 grPaint->addColorFragmentProcessor(std::move(shaderFP));
428 }
429
430 // We can ignore origColor here - alpha is unchanged by gamma
431 float paintAlpha = skPaint.getColor4f().fA;
432 if (1.0f != paintAlpha) {
433 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
434 // color channels. It's value should be treated as the same in ANY color space.
435 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
436 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
437 GrConstColorProcessor::InputMode::kModulateRGBA));
438 }
439 } else {
440 // The shader's FP sees the paint *unpremul* color
441 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
442 grPaint->setColor4f(origColorAsPM);
443 grPaint->addColorFragmentProcessor(std::move(shaderFP));
444 }
445 } else {
446 if (primColorMode) {
447 // There is a blend between the primitive color and the paint color. The blend considers
448 // the opaque paint color. The paint's alpha is applied to the post-blended color.
449 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
450 auto processor = GrConstColorProcessor::Make(opaqueColor,
451 GrConstColorProcessor::InputMode::kIgnore);
452 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
453 *primColorMode);
454 if (processor) {
455 grPaint->addColorFragmentProcessor(std::move(processor));
456 }
457
458 grPaint->setColor4f(opaqueColor);
459
460 // We can ignore origColor here - alpha is unchanged by gamma
461 float paintAlpha = skPaint.getColor4f().fA;
462 if (1.0f != paintAlpha) {
463 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
464 // color channels. It's value should be treated as the same in ANY color space.
465 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
466 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
467 GrConstColorProcessor::InputMode::kModulateRGBA));
468 }
469 } else {
470 // No shader, no primitive color.
471 grPaint->setColor4f(origColor.premul());
472 applyColorFilterToPaintColor = true;
473 }
474 }
475
476 SkColorFilter* colorFilter = skPaint.getColorFilter();
477 if (colorFilter) {
478 if (applyColorFilterToPaintColor) {
479 grPaint->setColor4f(
480 colorFilter->filterColor4f(origColor, colorSpaceInfo.colorSpace()).premul());
481 } else {
482 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
483 if (cfFP) {
484 grPaint->addColorFragmentProcessor(std::move(cfFP));
485 } else {
486 return false;
487 }
488 }
489 }
490
491 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
492 if (maskFilter) {
493 // We may have set this before passing to the SkShader.
494 fpArgs.fInputColorIsOpaque = false;
495 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
496 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
497 }
498 }
499
500 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
501 // the GrPaint to also be null (also kSrcOver).
502 SkASSERT(!grPaint->getXPFactory());
503 if (!skPaint.isSrcOver()) {
504 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
505 }
506
507 #ifndef SK_IGNORE_GPU_DITHER
508 // Conservative default, in case GrPixelConfigToColorType() fails.
509 SkColorType ct = SkColorType::kRGB_565_SkColorType;
510 GrPixelConfigToColorType(colorSpaceInfo.config(), &ct);
511 if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0) {
512 int32_t ditherRange = dither_range_type_for_config(colorSpaceInfo.config());
513 if (ditherRange >= 0) {
514 static int ditherIndex = GrSkSLFP::NewIndex();
515 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
516 &ditherRange, sizeof(ditherRange));
517 if (ditherFP) {
518 grPaint->addColorFragmentProcessor(std::move(ditherFP));
519 }
520 }
521 }
522 #endif
523 return true;
524 }
525
SkPaintToGrPaint(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,const SkMatrix & viewM,GrPaint * grPaint)526 bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorSpaceInfo& colorSpaceInfo,
527 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
528 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
529 grPaint);
530 }
531
532 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
SkPaintToGrPaintReplaceShader(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,std::unique_ptr<GrFragmentProcessor> shaderFP,GrPaint * grPaint)533 bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
534 const GrColorSpaceInfo& colorSpaceInfo,
535 const SkPaint& skPaint,
536 std::unique_ptr<GrFragmentProcessor> shaderFP,
537 GrPaint* grPaint) {
538 if (!shaderFP) {
539 return false;
540 }
541 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
542 nullptr, grPaint);
543 }
544
545 /** Ignores the SkShader (if any) on skPaint. */
SkPaintToGrPaintNoShader(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,GrPaint * grPaint)546 bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
547 const GrColorSpaceInfo& colorSpaceInfo,
548 const SkPaint& skPaint,
549 GrPaint* grPaint) {
550 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
551 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
552 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
553 nullptr, grPaint);
554 }
555
556 /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
557 be setup as a vertex attribute using the specified SkBlendMode. */
SkPaintToGrPaintWithXfermode(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,const SkMatrix & viewM,SkBlendMode primColorMode,GrPaint * grPaint)558 bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
559 const GrColorSpaceInfo& colorSpaceInfo,
560 const SkPaint& skPaint,
561 const SkMatrix& viewM,
562 SkBlendMode primColorMode,
563 GrPaint* grPaint) {
564 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
565 grPaint);
566 }
567
SkPaintToGrPaintWithTexture(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & paint,const SkMatrix & viewM,std::unique_ptr<GrFragmentProcessor> fp,bool textureIsAlphaOnly,GrPaint * grPaint)568 bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
569 const GrColorSpaceInfo& colorSpaceInfo,
570 const SkPaint& paint,
571 const SkMatrix& viewM,
572 std::unique_ptr<GrFragmentProcessor> fp,
573 bool textureIsAlphaOnly,
574 GrPaint* grPaint) {
575 std::unique_ptr<GrFragmentProcessor> shaderFP;
576 if (textureIsAlphaOnly) {
577 if (const auto* shader = as_SB(paint.getShader())) {
578 shaderFP = shader->asFragmentProcessor(GrFPArgs(
579 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
580 if (!shaderFP) {
581 return false;
582 }
583 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
584 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
585 } else {
586 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
587 }
588 } else {
589 if (paint.getColor4f().isOpaque()) {
590 shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
591 } else {
592 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
593 }
594 }
595
596 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
597 grPaint);
598 }
599
600
601 ////////////////////////////////////////////////////////////////////////////////////////////////
602
GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,const SkMatrix & viewM,const SkMatrix & localM,bool sharpenMipmappedTextures,bool * doBicubic)603 GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
604 const SkMatrix& viewM,
605 const SkMatrix& localM,
606 bool sharpenMipmappedTextures,
607 bool* doBicubic) {
608 *doBicubic = false;
609 GrSamplerState::Filter textureFilterMode;
610 switch (paintFilterQuality) {
611 case kNone_SkFilterQuality:
612 textureFilterMode = GrSamplerState::Filter::kNearest;
613 break;
614 case kLow_SkFilterQuality:
615 textureFilterMode = GrSamplerState::Filter::kBilerp;
616 break;
617 case kMedium_SkFilterQuality: {
618 SkMatrix matrix;
619 matrix.setConcat(viewM, localM);
620 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
621 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
622 //
623 // Want: 0 = log2(1/s) - 0.5
624 // 0.5 = log2(1/s)
625 // 2^0.5 = 1/s
626 // 1/2^0.5 = s
627 // 2^0.5/2 = s
628 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
629 if (matrix.getMinScale() < mipScale) {
630 textureFilterMode = GrSamplerState::Filter::kMipMap;
631 } else {
632 // Don't trigger MIP level generation unnecessarily.
633 textureFilterMode = GrSamplerState::Filter::kBilerp;
634 }
635 break;
636 }
637 case kHigh_SkFilterQuality: {
638 SkMatrix matrix;
639 matrix.setConcat(viewM, localM);
640 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
641 break;
642 }
643 default:
644 // Should be unreachable. If not, fall back to mipmaps.
645 textureFilterMode = GrSamplerState::Filter::kMipMap;
646 break;
647
648 }
649 return textureFilterMode;
650 }
651