• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkCanvas.h"
9 #include "include/core/SkColorFilter.h"
10 #include "include/core/SkData.h"
11 #include "include/core/SkPixelRef.h"
12 #include "include/gpu/GrContext.h"
13 #include "include/gpu/GrTypes.h"
14 #include "include/private/GrRecordingContext.h"
15 #include "include/private/SkImageInfoPriv.h"
16 #include "include/private/SkTemplates.h"
17 #include "src/core/SkAutoMalloc.h"
18 #include "src/core/SkBlendModePriv.h"
19 #include "src/core/SkImagePriv.h"
20 #include "src/core/SkMaskFilterBase.h"
21 #include "src/core/SkMessageBus.h"
22 #include "src/core/SkMipMap.h"
23 #include "src/core/SkPaintPriv.h"
24 #include "src/core/SkResourceCache.h"
25 #include "src/core/SkTraceEvent.h"
26 #include "src/gpu/GrBitmapTextureMaker.h"
27 #include "src/gpu/GrCaps.h"
28 #include "src/gpu/GrColorSpaceXform.h"
29 #include "src/gpu/GrContextPriv.h"
30 #include "src/gpu/GrGpuResourcePriv.h"
31 #include "src/gpu/GrPaint.h"
32 #include "src/gpu/GrProxyProvider.h"
33 #include "src/gpu/GrRecordingContextPriv.h"
34 #include "src/gpu/GrTextureProxy.h"
35 #include "src/gpu/GrXferProcessor.h"
36 #include "src/gpu/SkGr.h"
37 #include "src/gpu/effects/GrBicubicEffect.h"
38 #include "src/gpu/effects/GrPorterDuffXferProcessor.h"
39 #include "src/gpu/effects/GrSkSLFP.h"
40 #include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
41 #include "src/gpu/effects/generated/GrConstColorProcessor.h"
42 #include "src/image/SkImage_Base.h"
43 #include "src/shaders/SkShaderBase.h"
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(float x, float 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(half(x), half(y), half(x), half(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.fWidth = info.width();
90     desc.fHeight = info.height();
91     desc.fConfig = SkImageInfo2GrPixelConfig(info);
92     return desc;
93 }
94 
GrMakeKeyFromImageID(GrUniqueKey * key,uint32_t imageID,const SkIRect & imageBounds)95 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
96     SkASSERT(key);
97     SkASSERT(imageID);
98     SkASSERT(!imageBounds.isEmpty());
99     static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
100     GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
101     builder[0] = imageID;
102     builder[1] = imageBounds.fLeft;
103     builder[2] = imageBounds.fTop;
104     builder[3] = imageBounds.fRight;
105     builder[4] = imageBounds.fBottom;
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 
GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey & key,uint32_t contextUniqueID,SkPixelRef * pixelRef)110 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
111                                          SkPixelRef* pixelRef) {
112     class Invalidator : public SkPixelRef::GenIDChangeListener {
113     public:
114         explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
115                 : fMsg(key, contextUniqueID) {}
116 
117     private:
118         GrUniqueKeyInvalidatedMessage fMsg;
119 
120         void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
121     };
122 
123     pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
124 }
125 
GrCopyBaseMipMapToTextureProxy(GrRecordingContext * ctx,GrTextureProxy * baseProxy)126 sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
127                                                      GrTextureProxy* baseProxy) {
128     SkASSERT(baseProxy);
129 
130     if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) {
131         return nullptr;
132     }
133     return GrSurfaceProxy::Copy(ctx, baseProxy, GrMipMapped::kYes, SkBackingFit::kExact,
134                                 SkBudgeted::kYes);
135 }
136 
GrRefCachedBitmapTextureProxy(GrRecordingContext * ctx,const SkBitmap & bitmap,const GrSamplerState & params,SkScalar scaleAdjust[2])137 sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
138                                                     const SkBitmap& bitmap,
139                                                     const GrSamplerState& params,
140                                                     SkScalar scaleAdjust[2]) {
141     return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
142 }
143 
GrMakeCachedBitmapProxy(GrProxyProvider * proxyProvider,const SkBitmap & bitmap,SkBackingFit fit)144 sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
145                                               const SkBitmap& bitmap,
146                                               SkBackingFit fit) {
147     if (!bitmap.peekPixels(nullptr)) {
148         return nullptr;
149     }
150 
151     // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
152     // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
153     // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
154     SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
155                                                                   : kIfMutable_SkCopyPixelsMode;
156     sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
157 
158     if (!image) {
159         return nullptr;
160     }
161 
162     return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
163 }
164 
create_unique_key_for_image(const SkImage * image,GrUniqueKey * result)165 static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
166     if (!image) {
167         result->reset(); // will be invalid
168         return;
169     }
170 
171     if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
172         if (!bm->isVolatile()) {
173             SkIPoint origin = bm->pixelRefOrigin();
174             SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
175             GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
176         }
177         return;
178     }
179 
180     GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
181 }
182 
GrMakeCachedImageProxy(GrProxyProvider * proxyProvider,sk_sp<SkImage> srcImage,SkBackingFit fit)183 sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
184                                              sk_sp<SkImage> srcImage,
185                                              SkBackingFit fit) {
186     sk_sp<GrTextureProxy> proxy;
187     GrUniqueKey originalKey;
188 
189     create_unique_key_for_image(srcImage.get(), &originalKey);
190 
191     if (originalKey.isValid()) {
192         proxy = proxyProvider->findOrCreateProxyByUniqueKey(
193                 originalKey, SkColorTypeToGrColorType(srcImage->colorType()),
194                 kTopLeft_GrSurfaceOrigin);
195     }
196     if (!proxy) {
197         proxy = proxyProvider->createTextureProxy(srcImage, 1, SkBudgeted::kYes, fit);
198         if (proxy && originalKey.isValid()) {
199             proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
200             const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
201             // When recording DDLs we do not want to install change listeners because doing
202             // so isn't threadsafe.
203             if (bm && proxyProvider->renderingDirectly()) {
204                 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextID(),
205                                                     bm->pixelRef());
206             }
207         }
208     }
209 
210     return proxy;
211 }
212 
213 ///////////////////////////////////////////////////////////////////////////////
214 
SkColorToPMColor4f(SkColor c,const GrColorSpaceInfo & colorSpaceInfo)215 SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
216     SkColor4f color = SkColor4f::FromColor(c);
217     if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
218         color = xform->apply(color);
219     }
220     return color.premul();
221 }
222 
SkColor4fPrepForDst(SkColor4f color,const GrColorSpaceInfo & colorSpaceInfo)223 SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorSpaceInfo& colorSpaceInfo) {
224     if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
225         color = xform->apply(color);
226     }
227     return color;
228 }
229 
230 ///////////////////////////////////////////////////////////////////////////////
231 
SkColorType2GrPixelConfig(const SkColorType type)232 GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
233     switch (type) {
234         case kUnknown_SkColorType:
235             return kUnknown_GrPixelConfig;
236         case kAlpha_8_SkColorType:
237             return kAlpha_8_GrPixelConfig;
238         case kRGB_565_SkColorType:
239             return kRGB_565_GrPixelConfig;
240         case kARGB_4444_SkColorType:
241             return kRGBA_4444_GrPixelConfig;
242         case kRGBA_8888_SkColorType:
243             return kRGBA_8888_GrPixelConfig;
244         case kRGB_888x_SkColorType:
245             return kRGB_888_GrPixelConfig;
246         case kBGRA_8888_SkColorType:
247             return kBGRA_8888_GrPixelConfig;
248         case kRGBA_1010102_SkColorType:
249             return kRGBA_1010102_GrPixelConfig;
250         case kRGB_101010x_SkColorType:
251             return kUnknown_GrPixelConfig;
252         case kGray_8_SkColorType:
253             return kGray_8_GrPixelConfig;
254         case kRGBA_F16Norm_SkColorType:
255             return kRGBA_half_Clamped_GrPixelConfig;
256         case kRGBA_F16_SkColorType:
257             return kRGBA_half_GrPixelConfig;
258         case kRGBA_F32_SkColorType:
259             return kRGBA_float_GrPixelConfig;
260     }
261     SkASSERT(0);    // shouldn't get here
262     return kUnknown_GrPixelConfig;
263 }
264 
SkImageInfo2GrPixelConfig(const SkImageInfo & info)265 GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
266     return SkColorType2GrPixelConfig(info.colorType());
267 }
268 
GrPixelConfigToColorType(GrPixelConfig config,SkColorType * ctOut)269 bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
270     SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
271     if (kUnknown_SkColorType != ct) {
272         if (ctOut) {
273             *ctOut = ct;
274         }
275         return true;
276     }
277     return false;
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////////////////////
281 
blend_requires_shader(const SkBlendMode mode)282 static inline bool blend_requires_shader(const SkBlendMode mode) {
283     return SkBlendMode::kDst != mode;
284 }
285 
286 #ifndef SK_IGNORE_GPU_DITHER
dither_range_type_for_config(GrColorType dstColorType)287 static inline int32_t dither_range_type_for_config(GrColorType dstColorType) {
288     switch (dstColorType) {
289         case GrColorType::kGray_8:
290         case GrColorType::kRGBA_8888:
291         case GrColorType::kRGB_888x:
292         case GrColorType::kRG_88:
293         case GrColorType::kBGRA_8888:
294         case GrColorType::kR_16:
295         case GrColorType::kRG_1616:
296         // Experimental (for Y416 and mutant P016/P010)
297         case GrColorType::kRGBA_16161616:
298         case GrColorType::kRG_F16:
299             return 0;
300         case GrColorType::kBGR_565:
301             return 1;
302         case GrColorType::kABGR_4444:
303             return 2;
304         case GrColorType::kUnknown:
305         case GrColorType::kRGBA_8888_SRGB:
306         case GrColorType::kRGBA_1010102:
307         case GrColorType::kAlpha_F16:
308         case GrColorType::kRGBA_F32:
309         case GrColorType::kRGBA_F16:
310         case GrColorType::kRGBA_F16_Clamped:
311         case GrColorType::kAlpha_8:
312         case GrColorType::kAlpha_8xxx:
313         case GrColorType::kAlpha_F32xxx:
314         case GrColorType::kGray_8xxx:
315             return -1;
316     }
317     SkUNREACHABLE;
318 }
319 #endif
320 
skpaint_to_grpaint_impl(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,const SkMatrix & viewM,std::unique_ptr<GrFragmentProcessor> * shaderProcessor,SkBlendMode * primColorMode,GrPaint * grPaint)321 static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
322                                            const GrColorSpaceInfo& colorSpaceInfo,
323                                            const SkPaint& skPaint,
324                                            const SkMatrix& viewM,
325                                            std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
326                                            SkBlendMode* primColorMode,
327                                            GrPaint* grPaint) {
328     // Convert SkPaint color to 4f format in the destination color space
329     SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), colorSpaceInfo);
330 
331     GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
332 
333     // Setup the initial color considering the shader, the SkPaint color, and the presence or not
334     // of per-vertex colors.
335     std::unique_ptr<GrFragmentProcessor> shaderFP;
336     if (!primColorMode || blend_requires_shader(*primColorMode)) {
337         fpArgs.fInputColorIsOpaque = origColor.isOpaque();
338         if (shaderProcessor) {
339             shaderFP = std::move(*shaderProcessor);
340         } else if (const auto* shader = as_SB(skPaint.getShader())) {
341             shaderFP = shader->asFragmentProcessor(fpArgs);
342             if (!shaderFP) {
343                 return false;
344             }
345         }
346     }
347 
348     // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
349     // a known constant value. In that case we can simply apply a color filter during this
350     // conversion without converting the color filter to a GrFragmentProcessor.
351     bool applyColorFilterToPaintColor = false;
352     if (shaderFP) {
353         if (primColorMode) {
354             // There is a blend between the primitive color and the shader color. The shader sees
355             // the opaque paint color. The shader's output is blended using the provided mode by
356             // the primitive color. The blended color is then modulated by the paint's alpha.
357 
358             // The geometry processor will insert the primitive color to start the color chain, so
359             // the GrPaint color will be ignored.
360 
361             SkPMColor4f shaderInput = origColor.makeOpaque().premul();
362             shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
363             shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
364                                                                          *primColorMode);
365 
366             // The above may return null if compose results in a pass through of the prim color.
367             if (shaderFP) {
368                 grPaint->addColorFragmentProcessor(std::move(shaderFP));
369             }
370 
371             // We can ignore origColor here - alpha is unchanged by gamma
372             float paintAlpha = skPaint.getColor4f().fA;
373             if (1.0f != paintAlpha) {
374                 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
375                 // color channels. It's value should be treated as the same in ANY color space.
376                 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
377                     { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
378                     GrConstColorProcessor::InputMode::kModulateRGBA));
379             }
380         } else {
381             // The shader's FP sees the paint *unpremul* color
382             SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
383             grPaint->setColor4f(origColorAsPM);
384             grPaint->addColorFragmentProcessor(std::move(shaderFP));
385         }
386     } else {
387         if (primColorMode) {
388             // There is a blend between the primitive color and the paint color. The blend considers
389             // the opaque paint color. The paint's alpha is applied to the post-blended color.
390             SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
391             auto processor = GrConstColorProcessor::Make(opaqueColor,
392                                                          GrConstColorProcessor::InputMode::kIgnore);
393             processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
394                                                                           *primColorMode);
395             if (processor) {
396                 grPaint->addColorFragmentProcessor(std::move(processor));
397             }
398 
399             grPaint->setColor4f(opaqueColor);
400 
401             // We can ignore origColor here - alpha is unchanged by gamma
402             float paintAlpha = skPaint.getColor4f().fA;
403             if (1.0f != paintAlpha) {
404                 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
405                 // color channels. It's value should be treated as the same in ANY color space.
406                 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
407                     { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
408                     GrConstColorProcessor::InputMode::kModulateRGBA));
409             }
410         } else {
411             // No shader, no primitive color.
412             grPaint->setColor4f(origColor.premul());
413             applyColorFilterToPaintColor = true;
414         }
415     }
416 
417     SkColorFilter* colorFilter = skPaint.getColorFilter();
418     if (colorFilter) {
419         if (applyColorFilterToPaintColor) {
420             grPaint->setColor4f(
421                     colorFilter->filterColor4f(origColor, colorSpaceInfo.colorSpace()).premul());
422         } else {
423             auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
424             if (cfFP) {
425                 grPaint->addColorFragmentProcessor(std::move(cfFP));
426             } else {
427                 return false;
428             }
429         }
430     }
431 
432     SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
433     if (maskFilter) {
434         // We may have set this before passing to the SkShader.
435         fpArgs.fInputColorIsOpaque = false;
436         if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
437             grPaint->addCoverageFragmentProcessor(std::move(mfFP));
438         }
439     }
440 
441     // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
442     // the GrPaint to also be null (also kSrcOver).
443     SkASSERT(!grPaint->getXPFactory());
444     if (!skPaint.isSrcOver()) {
445         grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
446     }
447 
448 #ifndef SK_IGNORE_GPU_DITHER
449     // Conservative default, in case GrPixelConfigToColorType() fails.
450     GrColorType ct = colorSpaceInfo.colorType();
451     if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) &&
452         grPaint->numColorFragmentProcessors() > 0) {
453         int32_t ditherRange = dither_range_type_for_config(ct);
454         if (ditherRange >= 0) {
455             static int ditherIndex = GrSkSLFP::NewIndex();
456             auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
457                                            &ditherRange, sizeof(ditherRange));
458             if (ditherFP) {
459                 grPaint->addColorFragmentProcessor(std::move(ditherFP));
460             }
461         }
462     }
463 #endif
464     return true;
465 }
466 
SkPaintToGrPaint(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,const SkMatrix & viewM,GrPaint * grPaint)467 bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorSpaceInfo& colorSpaceInfo,
468                       const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
469     return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
470                                    grPaint);
471 }
472 
473 /** 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)474 bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
475                                    const GrColorSpaceInfo& colorSpaceInfo,
476                                    const SkPaint& skPaint,
477                                    std::unique_ptr<GrFragmentProcessor> shaderFP,
478                                    GrPaint* grPaint) {
479     if (!shaderFP) {
480         return false;
481     }
482     return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
483                                    nullptr, grPaint);
484 }
485 
486 /** Ignores the SkShader (if any) on skPaint. */
SkPaintToGrPaintNoShader(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & skPaint,GrPaint * grPaint)487 bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
488                               const GrColorSpaceInfo& colorSpaceInfo,
489                               const SkPaint& skPaint,
490                               GrPaint* grPaint) {
491     // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
492     std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
493     return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
494                                    nullptr, grPaint);
495 }
496 
497 /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
498 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)499 bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
500                                   const GrColorSpaceInfo& colorSpaceInfo,
501                                   const SkPaint& skPaint,
502                                   const SkMatrix& viewM,
503                                   SkBlendMode primColorMode,
504                                   GrPaint* grPaint) {
505     return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
506                                    grPaint);
507 }
508 
SkPaintToGrPaintWithTexture(GrRecordingContext * context,const GrColorSpaceInfo & colorSpaceInfo,const SkPaint & paint,const SkMatrix & viewM,std::unique_ptr<GrFragmentProcessor> fp,bool textureIsAlphaOnly,GrPaint * grPaint)509 bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
510                                  const GrColorSpaceInfo& colorSpaceInfo,
511                                  const SkPaint& paint,
512                                  const SkMatrix& viewM,
513                                  std::unique_ptr<GrFragmentProcessor> fp,
514                                  bool textureIsAlphaOnly,
515                                  GrPaint* grPaint) {
516     std::unique_ptr<GrFragmentProcessor> shaderFP;
517     if (textureIsAlphaOnly) {
518         if (const auto* shader = as_SB(paint.getShader())) {
519             shaderFP = shader->asFragmentProcessor(GrFPArgs(
520                     context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
521             if (!shaderFP) {
522                 return false;
523             }
524             std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
525             shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
526         } else {
527             shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
528         }
529     } else {
530         if (paint.getColor4f().isOpaque()) {
531             shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
532         } else {
533             shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
534         }
535     }
536 
537     return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
538                                          grPaint);
539 }
540 
541 
542 ////////////////////////////////////////////////////////////////////////////////////////////////
543 
GrSkFilterQualityToGrFilterMode(int imageWidth,int imageHeight,SkFilterQuality paintFilterQuality,const SkMatrix & viewM,const SkMatrix & localM,bool sharpenMipmappedTextures,bool * doBicubic)544 GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
545                                                        SkFilterQuality paintFilterQuality,
546                                                        const SkMatrix& viewM,
547                                                        const SkMatrix& localM,
548                                                        bool sharpenMipmappedTextures,
549                                                        bool* doBicubic) {
550     *doBicubic = false;
551     if (imageWidth <= 1 && imageHeight <= 1) {
552         return GrSamplerState::Filter::kNearest;
553     }
554     switch (paintFilterQuality) {
555         case kNone_SkFilterQuality:
556             return GrSamplerState::Filter::kNearest;
557         case kLow_SkFilterQuality:
558             return GrSamplerState::Filter::kBilerp;
559         case kMedium_SkFilterQuality: {
560             SkMatrix matrix;
561             matrix.setConcat(viewM, localM);
562             // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
563             // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
564             //
565             // Want:  0       = log2(1/s) - 0.5
566             //        0.5     = log2(1/s)
567             //        2^0.5   = 1/s
568             //        1/2^0.5 = s
569             //        2^0.5/2 = s
570             SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
571             if (matrix.getMinScale() < mipScale) {
572                 return GrSamplerState::Filter::kMipMap;
573             } else {
574                 // Don't trigger MIP level generation unnecessarily.
575                 return GrSamplerState::Filter::kBilerp;
576             }
577         }
578         case kHigh_SkFilterQuality: {
579             SkMatrix matrix;
580             matrix.setConcat(viewM, localM);
581             GrSamplerState::Filter textureFilterMode;
582             *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
583             return textureFilterMode;
584         }
585     }
586     SkUNREACHABLE;
587 }
588