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