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