Plumb dst color space in many places, rather than "mode"
[platform/upstream/libSkiaSharp.git] / src / gpu / SkGr.cpp
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
9 #include "SkGr.h"
10 #include "SkGrPriv.h"
11
12 #include "GrBitmapTextureMaker.h"
13 #include "GrCaps.h"
14 #include "GrContext.h"
15 #include "GrGpuResourcePriv.h"
16 #include "GrRenderTargetContext.h"
17 #include "GrTexturePriv.h"
18 #include "GrTypes.h"
19 #include "GrXferProcessor.h"
20 #include "GrYUVProvider.h"
21
22 #include "SkBlendModePriv.h"
23 #include "SkColorFilter.h"
24 #include "SkConfig8888.h"
25 #include "SkCanvas.h"
26 #include "SkData.h"
27 #include "SkMaskFilter.h"
28 #include "SkMessageBus.h"
29 #include "SkMipMap.h"
30 #include "SkPixelRef.h"
31 #include "SkPM4fPriv.h"
32 #include "SkResourceCache.h"
33 #include "SkTemplates.h"
34 #include "SkYUVPlanesCache.h"
35 #include "effects/GrBicubicEffect.h"
36 #include "effects/GrConstColorProcessor.h"
37 #include "effects/GrDitherEffect.h"
38 #include "effects/GrPorterDuffXferProcessor.h"
39 #include "effects/GrXfermodeFragmentProcessor.h"
40 #include "effects/GrYUVEffect.h"
41
42 #ifndef SK_IGNORE_ETC1_SUPPORT
43 #  include "ktx.h"
44 #  include "etc1.h"
45 #endif
46
47 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) {
48     GrSurfaceDesc desc;
49     desc.fFlags = kNone_GrSurfaceFlags;
50     desc.fWidth = info.width();
51     desc.fHeight = info.height();
52     desc.fConfig = SkImageInfo2GrPixelConfig(info, caps);
53     desc.fSampleCnt = 0;
54     return desc;
55 }
56
57 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
58     SkASSERT(key);
59     SkASSERT(imageID);
60     SkASSERT(!imageBounds.isEmpty());
61     static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
62     GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
63     builder[0] = imageID;
64     builder[1] = imageBounds.fLeft;
65     builder[2] = imageBounds.fTop;
66     builder[3] = imageBounds.fRight;
67     builder[4] = imageBounds.fBottom;
68 }
69
70 GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
71                                                  int expectedW, int expectedH,
72                                                  const void** outStartOfDataToUpload) {
73     *outStartOfDataToUpload = nullptr;
74 #ifndef SK_IGNORE_ETC1_SUPPORT
75     if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
76         return kUnknown_GrPixelConfig;
77     }
78
79     const uint8_t* bytes = data->bytes();
80     if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
81         // Does the data match the dimensions of the bitmap? If not,
82         // then we don't know how to scale the image to match it...
83         if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
84             etc1_pkm_get_height(bytes) != (unsigned)expectedH)
85         {
86             return kUnknown_GrPixelConfig;
87         }
88
89         *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
90         return kETC1_GrPixelConfig;
91     } else if (SkKTXFile::is_ktx(bytes, data->size())) {
92         SkKTXFile ktx(data);
93
94         // Is it actually an ETC1 texture?
95         if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
96             return kUnknown_GrPixelConfig;
97         }
98
99         // Does the data match the dimensions of the bitmap? If not,
100         // then we don't know how to scale the image to match it...
101         if (ktx.width() != expectedW || ktx.height() != expectedH) {
102             return kUnknown_GrPixelConfig;
103         }
104
105         *outStartOfDataToUpload = ktx.pixelData();
106         return kETC1_GrPixelConfig;
107     }
108 #endif
109     return kUnknown_GrPixelConfig;
110 }
111
112 //////////////////////////////////////////////////////////////////////////////
113
114 /**
115  * Fill out buffer with the compressed format Ganesh expects from a colortable
116  * based bitmap. [palette (colortable) + indices].
117  *
118  * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
119  * we could detect that the colortable.count is <= 16, and then repack the
120  * indices as nibbles to save RAM, but it would take more time (i.e. a lot
121  * slower than memcpy), so skipping that for now.
122  *
123  * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
124  * as the colortable.count says it is.
125  */
126 static void build_index8_data(void* buffer, const SkPixmap& pixmap) {
127     SkASSERT(kIndex_8_SkColorType == pixmap.colorType());
128
129     const SkColorTable* ctable = pixmap.ctable();
130     char* dst = (char*)buffer;
131
132     const int count = ctable->count();
133
134     SkDstPixelInfo dstPI;
135     dstPI.fColorType = kRGBA_8888_SkColorType;
136     dstPI.fAlphaType = kPremul_SkAlphaType;
137     dstPI.fPixels = buffer;
138     dstPI.fRowBytes = count * sizeof(SkPMColor);
139
140     SkSrcPixelInfo srcPI;
141     srcPI.fColorType = kN32_SkColorType;
142     srcPI.fAlphaType = kPremul_SkAlphaType;
143     srcPI.fPixels = ctable->readColors();
144     srcPI.fRowBytes = count * sizeof(SkPMColor);
145
146     srcPI.convertPixelsTo(&dstPI, count, 1);
147
148     // always skip a full 256 number of entries, even if we memcpy'd fewer
149     dst += 256 * sizeof(GrColor);
150
151     if ((unsigned)pixmap.width() == pixmap.rowBytes()) {
152         memcpy(dst, pixmap.addr(), pixmap.getSafeSize());
153     } else {
154         // need to trim off the extra bytes per row
155         size_t width = pixmap.width();
156         size_t rowBytes = pixmap.rowBytes();
157         const uint8_t* src = pixmap.addr8();
158         for (int y = 0; y < pixmap.height(); y++) {
159             memcpy(dst, src, width);
160             src += rowBytes;
161             dst += width;
162         }
163     }
164 }
165
166 /**
167  *  Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
168  *  be gone from SkPixelRef, and we can remove this subclass entirely.
169  */
170 class PixelRef_GrYUVProvider : public GrYUVProvider {
171     SkPixelRef* fPR;
172
173 public:
174     PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
175
176     uint32_t onGetID() override { return fPR->getGenerationID(); }
177     bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
178         return fPR->queryYUV8(sizeInfo, colorSpace);
179     }
180     bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
181         return fPR->getYUV8Planes(sizeInfo, planes);
182     }
183 };
184
185 static sk_sp<GrTexture> create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
186                                                 const GrSurfaceDesc& desc) {
187     // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
188     SkPixelRef* pixelRef = bm.pixelRef();
189     if ((nullptr == pixelRef) ||
190         (pixelRef->info().width() != bm.info().width()) ||
191         (pixelRef->info().height() != bm.info().height())) {
192         return nullptr;
193     }
194
195     PixelRef_GrYUVProvider provider(pixelRef);
196
197     return provider.refAsTexture(ctx, desc, !bm.isVolatile());
198 }
199
200 GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) {
201     GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
202
203     sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
204     if (texture) {
205         return texture.release();
206     }
207
208     SkAutoLockPixels alp(bitmap);
209     if (!bitmap.readyToDraw()) {
210         return nullptr;
211     }
212     SkPixmap pixmap;
213     if (!bitmap.peekPixels(&pixmap)) {
214         return nullptr;
215     }
216     return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes);
217 }
218
219 GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) {
220     const SkPixmap* pmap = &pixmap;
221     SkPixmap tmpPixmap;
222     SkBitmap tmpBitmap;
223
224     const GrCaps* caps = ctx->caps();
225     GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
226
227     if (caps->srgbSupport() &&
228         pixmap.info().colorSpace() && pixmap.info().colorSpace()->gammaCloseToSRGB() &&
229         !(GrPixelConfigIsSRGB(desc.fConfig) ||
230           kRGBA_half_GrPixelConfig == desc.fConfig ||
231           kRGBA_float_GrPixelConfig == desc.fConfig)) {
232         // We were supplied an sRGB-like color space, but we don't have a suitable pixel config.
233         // Convert to 8888 sRGB so we can handle the data correctly. The raster backend doesn't
234         // handle sRGB Index8 -> sRGB 8888 correctly (yet), so lie about both the source and
235         // destination (claim they're linear):
236         SkImageInfo linSrcInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
237                                                    pixmap.colorType(), pixmap.alphaType());
238         SkPixmap linSrcPixmap(linSrcInfo, pixmap.addr(), pixmap.rowBytes(), pixmap.ctable());
239
240         SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
241                                                 kN32_SkColorType, kPremul_SkAlphaType,
242                                                 sk_ref_sp(pixmap.info().colorSpace()));
243
244         tmpBitmap.allocPixels(dstInfo);
245
246         SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
247         if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
248             return nullptr;
249         }
250         if (!tmpBitmap.peekPixels(&tmpPixmap)) {
251             return nullptr;
252         }
253         pmap = &tmpPixmap;
254         // must rebuild desc, since we've forced the info to be N32
255         desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
256     } else if (kGray_8_SkColorType == pixmap.colorType()) {
257         // We don't have Gray8 support as a pixel config, so expand to 8888
258
259         // We should have converted sRGB Gray8 above (if we have sRGB support):
260         SkASSERT(!caps->srgbSupport() || !pixmap.info().colorSpace() ||
261                  !pixmap.info().colorSpace()->gammaCloseToSRGB());
262
263         SkImageInfo info = SkImageInfo::MakeN32(pixmap.width(), pixmap.height(),
264                                                 kOpaque_SkAlphaType);
265         tmpBitmap.allocPixels(info);
266         if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
267             return nullptr;
268         }
269         if (!tmpBitmap.peekPixels(&tmpPixmap)) {
270             return nullptr;
271         }
272         pmap = &tmpPixmap;
273         // must rebuild desc, since we've forced the info to be N32
274         desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
275     } else if (kIndex_8_SkColorType == pixmap.colorType()) {
276         if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
277             size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
278                                                           pixmap.width(), pixmap.height());
279             SkAutoMalloc storage(imageSize);
280             build_index8_data(storage.get(), pixmap);
281
282             // our compressed data will be trimmed, so pass width() for its
283             // "rowBytes", since they are the same now.
284             return ctx->textureProvider()->createTexture(desc, budgeted, storage.get(),
285                                                          pixmap.width());
286         } else {
287             SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
288             tmpBitmap.allocPixels(info);
289             if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
290                 return nullptr;
291             }
292             if (!tmpBitmap.peekPixels(&tmpPixmap)) {
293                 return nullptr;
294             }
295             pmap = &tmpPixmap;
296             // must rebuild desc, since we've forced the info to be N32
297             desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
298         }
299     }
300
301     return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(),
302                                                  pmap->rowBytes());
303 }
304
305
306 ////////////////////////////////////////////////////////////////////////////////
307
308 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
309     class Invalidator : public SkPixelRef::GenIDChangeListener {
310     public:
311         explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
312     private:
313         GrUniqueKeyInvalidatedMessage fMsg;
314
315         void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
316     };
317
318     pixelRef->addGenIDChangeListener(new Invalidator(key));
319 }
320
321 GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap,
322                                                SkColorSpace* dstColorSpace)
323 {
324     SkDestinationSurfaceColorMode colorMode = dstColorSpace
325         ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
326         : SkDestinationSurfaceColorMode::kLegacy;
327
328     GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
329     sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
330     if (texture) {
331         return texture.release();
332     }
333
334     // We don't support Gray8 directly in the GL backend, so fail-over to GrUploadBitmapToTexture.
335     // That will transform the Gray8 to 8888, then use the driver/GPU to build mipmaps. If we build
336     // the mips on the CPU here, they'll all be Gray8, which isn't useful. (They get treated as A8).
337     // TODO: A better option might be to transform the initial bitmap here to 8888, then run the
338     // CPU mip-mapper on that data before uploading. This is much less code for a rare case though:
339     if (kGray_8_SkColorType == bitmap.colorType()) {
340         return nullptr;
341     }
342
343     SkASSERT(sizeof(int) <= sizeof(uint32_t));
344     if (bitmap.width() < 0 || bitmap.height() < 0) {
345         return nullptr;
346     }
347
348     SkAutoPixmapUnlock srcUnlocker;
349     if (!bitmap.requestLock(&srcUnlocker)) {
350         return nullptr;
351     }
352     const SkPixmap& pixmap = srcUnlocker.pixmap();
353     // Try to catch where we might have returned nullptr for src crbug.com/492818
354     if (nullptr == pixmap.addr()) {
355         sk_throw();
356     }
357
358     std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
359     if (!mipmaps) {
360         return nullptr;
361     }
362
363     const int mipLevelCount = mipmaps->countLevels() + 1;
364     if (mipLevelCount < 1) {
365         return nullptr;
366     }
367
368     const bool isMipMapped = mipLevelCount > 1;
369     desc.fIsMipMapped = isMipMapped;
370
371     std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
372
373     texels[0].fPixels = pixmap.addr();
374     texels[0].fRowBytes = pixmap.rowBytes();
375
376     for (int i = 1; i < mipLevelCount; ++i) {
377         SkMipMap::Level generatedMipLevel;
378         mipmaps->getLevel(i - 1, &generatedMipLevel);
379         texels[i].fPixels = generatedMipLevel.fPixmap.addr();
380         texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
381     }
382
383     {
384         GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc,
385                                                                             SkBudgeted::kYes,
386                                                                             texels.get(),
387                                                                             mipLevelCount);
388         if (texture) {
389             texture->texturePriv().setMipColorMode(colorMode);
390         }
391         return texture;
392     }
393 }
394
395 GrTexture* GrUploadMipMapToTexture(GrContext* ctx, const SkImageInfo& info,
396                                    const GrMipLevel* texels, int mipLevelCount) {
397     const GrCaps* caps = ctx->caps();
398     return ctx->textureProvider()->createMipMappedTexture(GrImageInfoToSurfaceDesc(info, *caps),
399                                                           SkBudgeted::kYes, texels,
400                                                           mipLevelCount);
401 }
402
403 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
404                                     const GrSamplerParams& params) {
405     // Caller doesn't care about the texture's color space (they can always get it from the bitmap)
406     return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, nullptr, nullptr);
407 }
408
409 sk_sp<GrTexture> GrMakeCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
410                                            const GrSamplerParams& params) {
411     // Caller doesn't care about the texture's color space (they can always get it from the bitmap)
412     GrTexture* tex = GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, nullptr,
413                                                                            nullptr);
414     return sk_sp<GrTexture>(tex);
415 }
416
417 ///////////////////////////////////////////////////////////////////////////////
418
419 GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) {
420     // We want to premultiply after linearizing, so this is easy:
421     return SkColorToUnpremulGrColor4f(c, dstColorSpace).premul();
422 }
423
424 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) {
425     if (dstColorSpace) {
426         auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
427         auto gamutXform = GrColorSpaceXform::Make(srgbColorSpace.get(), dstColorSpace);
428         return SkColorToUnpremulGrColor4f(c, true, gamutXform.get());
429     } else {
430         return SkColorToUnpremulGrColor4f(c, false, nullptr);
431     }
432 }
433
434 GrColor4f SkColorToPremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
435     // We want to premultiply after linearizing, so this is easy:
436     return SkColorToUnpremulGrColor4f(c, gammaCorrect, gamutXform).premul();
437 }
438
439 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
440     // You can't be color-space aware in legacy mode
441     SkASSERT(gammaCorrect || !gamutXform);
442
443     GrColor4f color;
444     if (gammaCorrect) {
445         // SkColor4f::FromColor does sRGB -> Linear
446         color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c));
447     } else {
448         // GrColor4f::FromGrColor just multiplies by 1/255
449         color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c));
450     }
451
452     if (gamutXform) {
453         color = gamutXform->apply(color);
454     }
455
456     return color;
457 }
458
459 ///////////////////////////////////////////////////////////////////////////////
460
461 // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
462 // alpha info, that will be considered.
463 GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, const SkColorSpace* cs,
464                                         const GrCaps& caps) {
465     // We intentionally ignore profile type for non-8888 formats. Anything we can't support
466     // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture.
467     switch (ct) {
468         case kUnknown_SkColorType:
469             return kUnknown_GrPixelConfig;
470         case kAlpha_8_SkColorType:
471             return kAlpha_8_GrPixelConfig;
472         case kRGB_565_SkColorType:
473             return kRGB_565_GrPixelConfig;
474         case kARGB_4444_SkColorType:
475             return kRGBA_4444_GrPixelConfig;
476         case kRGBA_8888_SkColorType:
477             return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB())
478                    ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
479         case kBGRA_8888_SkColorType:
480             return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB())
481                    ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig;
482         case kIndex_8_SkColorType:
483             return kIndex_8_GrPixelConfig;
484         case kGray_8_SkColorType:
485             return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
486         case kRGBA_F16_SkColorType:
487             return kRGBA_half_GrPixelConfig;
488     }
489     SkASSERT(0);    // shouldn't get here
490     return kUnknown_GrPixelConfig;
491 }
492
493 bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
494     SkColorType ct;
495     switch (config) {
496         case kAlpha_8_GrPixelConfig:
497             ct = kAlpha_8_SkColorType;
498             break;
499         case kIndex_8_GrPixelConfig:
500             ct = kIndex_8_SkColorType;
501             break;
502         case kRGB_565_GrPixelConfig:
503             ct = kRGB_565_SkColorType;
504             break;
505         case kRGBA_4444_GrPixelConfig:
506             ct = kARGB_4444_SkColorType;
507             break;
508         case kRGBA_8888_GrPixelConfig:
509             ct = kRGBA_8888_SkColorType;
510             break;
511         case kBGRA_8888_GrPixelConfig:
512             ct = kBGRA_8888_SkColorType;
513             break;
514         case kSRGBA_8888_GrPixelConfig:
515             ct = kRGBA_8888_SkColorType;
516             break;
517         case kSBGRA_8888_GrPixelConfig:
518             ct = kBGRA_8888_SkColorType;
519             break;
520         case kRGBA_half_GrPixelConfig:
521             ct = kRGBA_F16_SkColorType;
522             break;
523         default:
524             return false;
525     }
526     if (ctOut) {
527         *ctOut = ct;
528     }
529     return true;
530 }
531
532 GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* colorSpace) {
533     if (!colorSpace) {
534         return kRGBA_8888_GrPixelConfig;
535     } else if (colorSpace->gammaIsLinear()) {
536         return kRGBA_half_GrPixelConfig;
537     } else if (colorSpace->gammaCloseToSRGB()) {
538         return kSRGBA_8888_GrPixelConfig;
539     } else {
540         SkDEBUGFAIL("No renderable config exists for color space with strange gamma");
541         return kUnknown_GrPixelConfig;
542     }
543 }
544
545 ////////////////////////////////////////////////////////////////////////////////////////////////
546
547 static inline bool blend_requires_shader(const SkBlendMode mode, bool primitiveIsSrc) {
548     if (primitiveIsSrc) {
549         return SkBlendMode::kSrc != mode;
550     } else {
551         return SkBlendMode::kDst != mode;
552     }
553 }
554
555 static inline bool skpaint_to_grpaint_impl(GrContext* context,
556                                            GrRenderTargetContext* rtc,
557                                            const SkPaint& skPaint,
558                                            const SkMatrix& viewM,
559                                            sk_sp<GrFragmentProcessor>* shaderProcessor,
560                                            SkBlendMode* primColorMode,
561                                            bool primitiveIsSrc,
562                                            GrPaint* grPaint) {
563     grPaint->setAntiAlias(skPaint.isAntiAlias());
564     grPaint->setAllowSRGBInputs(rtc->isGammaCorrect());
565
566     // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion.
567     GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), rtc->isGammaCorrect(),
568                                                      rtc->getColorXformFromSRGB());
569
570     // Setup the initial color considering the shader, the SkPaint color, and the presence or not
571     // of per-vertex colors.
572     sk_sp<GrFragmentProcessor> shaderFP;
573     if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
574         if (shaderProcessor) {
575             shaderFP = *shaderProcessor;
576         } else if (const SkShader* shader = skPaint.getShader()) {
577             shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context, &viewM, nullptr,
578                                                                       skPaint.getFilterQuality(),
579                                                                       rtc->getColorSpace()));
580             if (!shaderFP) {
581                 return false;
582             }
583         }
584     }
585
586     // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
587     // a known constant value. In that case we can simply apply a color filter during this
588     // conversion without converting the color filter to a GrFragmentProcessor.
589     bool applyColorFilterToPaintColor = false;
590     if (shaderFP) {
591         if (primColorMode) {
592             // There is a blend between the primitive color and the shader color. The shader sees
593             // the opaque paint color. The shader's output is blended using the provided mode by
594             // the primitive color. The blended color is then modulated by the paint's alpha.
595
596             // The geometry processor will insert the primitive color to start the color chain, so
597             // the GrPaint color will be ignored.
598
599             GrColor4f shaderInput = origColor.opaque();
600             shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
601             if (primitiveIsSrc) {
602                 shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP),
603                                                                              *primColorMode);
604             } else {
605                 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
606                                                                              *primColorMode);
607             }
608             // The above may return null if compose results in a pass through of the prim color.
609             if (shaderFP) {
610                 grPaint->addColorFragmentProcessor(shaderFP);
611             }
612
613             // We can ignore origColor here - alpha is unchanged by gamma
614             GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
615             if (GrColor_WHITE != paintAlpha) {
616                 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
617                 // color channels. It's value should be treated as the same in ANY color space.
618                 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
619                     GrColor4f::FromGrColor(paintAlpha),
620                     GrConstColorProcessor::kModulateRGBA_InputMode));
621             }
622         } else {
623             // The shader's FP sees the paint unpremul color
624             grPaint->setColor4f(origColor);
625             grPaint->addColorFragmentProcessor(std::move(shaderFP));
626         }
627     } else {
628         if (primColorMode) {
629             // There is a blend between the primitive color and the paint color. The blend considers
630             // the opaque paint color. The paint's alpha is applied to the post-blended color.
631             sk_sp<GrFragmentProcessor> processor(
632                 GrConstColorProcessor::Make(origColor.opaque(),
633                                             GrConstColorProcessor::kIgnore_InputMode));
634             if (primitiveIsSrc) {
635                 processor = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(processor),
636                                                                               *primColorMode);
637             } else {
638                 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
639                                                                               *primColorMode);
640             }
641             if (processor) {
642                 grPaint->addColorFragmentProcessor(std::move(processor));
643             }
644
645             grPaint->setColor4f(origColor.opaque());
646
647             // We can ignore origColor here - alpha is unchanged by gamma
648             GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
649             if (GrColor_WHITE != paintAlpha) {
650                 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
651                 // color channels. It's value should be treated as the same in ANY color space.
652                 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
653                     GrColor4f::FromGrColor(paintAlpha),
654                     GrConstColorProcessor::kModulateRGBA_InputMode));
655             }
656         } else {
657             // No shader, no primitive color.
658             grPaint->setColor4f(origColor.premul());
659             applyColorFilterToPaintColor = true;
660         }
661     }
662
663     SkColorFilter* colorFilter = skPaint.getColorFilter();
664     if (colorFilter) {
665         if (applyColorFilterToPaintColor) {
666             // If we're in legacy mode, we *must* avoid using the 4f version of the color filter,
667             // because that will combine with the linearized version of the stored color.
668             if (rtc->isGammaCorrect()) {
669                 grPaint->setColor4f(GrColor4f::FromSkColor4f(
670                     colorFilter->filterColor4f(origColor.toSkColor4f())).premul());
671             } else {
672                 grPaint->setColor4f(SkColorToPremulGrColor4f(
673                     colorFilter->filterColor(skPaint.getColor()), false, nullptr));
674             }
675         } else {
676             sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context,
677                                                                              rtc->getColorSpace()));
678             if (cfFP) {
679                 grPaint->addColorFragmentProcessor(std::move(cfFP));
680             } else {
681                 return false;
682             }
683         }
684     }
685
686     SkMaskFilter* maskFilter = skPaint.getMaskFilter();
687     if (maskFilter) {
688         GrFragmentProcessor* mfFP;
689         if (maskFilter->asFragmentProcessor(&mfFP, nullptr, viewM)) {
690             grPaint->addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor>(mfFP));
691         }
692     }
693
694     // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
695     // the GrPaint to also be null (also kSrcOver).
696     SkASSERT(!grPaint->getXPFactory());
697     if (!skPaint.isSrcOver()) {
698         grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
699     }
700
701 #ifndef SK_IGNORE_GPU_DITHER
702     if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0 && !rtc->isGammaCorrect()) {
703         grPaint->addColorFragmentProcessor(GrDitherEffect::Make());
704     }
705 #endif
706     return true;
707 }
708
709 bool SkPaintToGrPaint(GrContext* context, GrRenderTargetContext* rtc, const SkPaint& skPaint,
710                       const SkMatrix& viewM, GrPaint* grPaint) {
711     return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, nullptr, false, grPaint);
712 }
713
714 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
715 bool SkPaintToGrPaintReplaceShader(GrContext* context,
716                                    GrRenderTargetContext* rtc,
717                                    const SkPaint& skPaint,
718                                    sk_sp<GrFragmentProcessor> shaderFP,
719                                    GrPaint* grPaint) {
720     if (!shaderFP) {
721         return false;
722     }
723     return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
724                                    grPaint);
725 }
726
727 /** Ignores the SkShader (if any) on skPaint. */
728 bool SkPaintToGrPaintNoShader(GrContext* context,
729                               GrRenderTargetContext* rtc,
730                               const SkPaint& skPaint,
731                               GrPaint* grPaint) {
732     // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
733     static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr);
734     static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP;
735     return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), kIgnoreShader, nullptr,
736                                    false, grPaint);
737 }
738
739 /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
740 be setup as a vertex attribute using the specified SkBlendMode. */
741 bool SkPaintToGrPaintWithXfermode(GrContext* context,
742                                   GrRenderTargetContext* rtc,
743                                   const SkPaint& skPaint,
744                                   const SkMatrix& viewM,
745                                   SkBlendMode primColorMode,
746                                   bool primitiveIsSrc,
747                                   GrPaint* grPaint) {
748     return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, &primColorMode,
749                                    primitiveIsSrc, grPaint);
750 }
751
752 bool SkPaintToGrPaintWithTexture(GrContext* context,
753                                  GrRenderTargetContext* rtc,
754                                  const SkPaint& paint,
755                                  const SkMatrix& viewM,
756                                  sk_sp<GrFragmentProcessor> fp,
757                                  bool textureIsAlphaOnly,
758                                  GrPaint* grPaint) {
759     sk_sp<GrFragmentProcessor> shaderFP;
760     if (textureIsAlphaOnly) {
761         if (const SkShader* shader = paint.getShader()) {
762             shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context,
763                                                                       &viewM,
764                                                                       nullptr,
765                                                                       paint.getFilterQuality(),
766                                                                       rtc->getColorSpace()));
767             if (!shaderFP) {
768                 return false;
769             }
770             sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
771             shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
772         } else {
773             shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp);
774         }
775     } else {
776         shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp);
777     }
778
779     return SkPaintToGrPaintReplaceShader(context, rtc, paint, std::move(shaderFP), grPaint);
780 }
781
782
783 ////////////////////////////////////////////////////////////////////////////////////////////////
784
785 GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
786                                                             const SkMatrix& viewM,
787                                                             const SkMatrix& localM,
788                                                             bool* doBicubic) {
789     *doBicubic = false;
790     GrSamplerParams::FilterMode textureFilterMode;
791     switch (paintFilterQuality) {
792         case kNone_SkFilterQuality:
793             textureFilterMode = GrSamplerParams::kNone_FilterMode;
794             break;
795         case kLow_SkFilterQuality:
796             textureFilterMode = GrSamplerParams::kBilerp_FilterMode;
797             break;
798         case kMedium_SkFilterQuality: {
799             SkMatrix matrix;
800             matrix.setConcat(viewM, localM);
801             if (matrix.getMinScale() < SK_Scalar1) {
802                 textureFilterMode = GrSamplerParams::kMipMap_FilterMode;
803             } else {
804                 // Don't trigger MIP level generation unnecessarily.
805                 textureFilterMode = GrSamplerParams::kBilerp_FilterMode;
806             }
807             break;
808         }
809         case kHigh_SkFilterQuality: {
810             SkMatrix matrix;
811             matrix.setConcat(viewM, localM);
812             *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
813             break;
814         }
815         default:
816             // Should be unreachable.  If not, fall back to mipmaps.
817             textureFilterMode = GrSamplerParams::kMipMap_FilterMode;
818             break;
819
820     }
821     return textureFilterMode;
822 }