2 * Copyright 2010 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
13 #include "GrContext.h"
14 #include "GrDrawContext.h"
15 #include "GrGpuResourcePriv.h"
16 #include "GrImageIDTextureAdjuster.h"
17 #include "GrTextureParamsAdjuster.h"
18 #include "GrTexturePriv.h"
20 #include "GrXferProcessor.h"
21 #include "GrYUVProvider.h"
23 #include "SkBlendModePriv.h"
24 #include "SkColorFilter.h"
25 #include "SkConfig8888.h"
28 #include "SkErrorInternals.h"
29 #include "SkMessageBus.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"
43 #ifndef SK_IGNORE_ETC1_SUPPORT
48 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) {
50 desc.fFlags = kNone_GrSurfaceFlags;
51 desc.fWidth = info.width();
52 desc.fHeight = info.height();
53 desc.fConfig = SkImageInfo2GrPixelConfig(info, caps);
58 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
61 SkASSERT(!imageBounds.isEmpty());
62 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
63 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
65 builder[1] = imageBounds.fLeft;
66 builder[2] = imageBounds.fTop;
67 builder[3] = imageBounds.fRight;
68 builder[4] = imageBounds.fBottom;
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;
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)
87 return kUnknown_GrPixelConfig;
90 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
91 return kETC1_GrPixelConfig;
92 } else if (SkKTXFile::is_ktx(bytes, data->size())) {
95 // Is it actually an ETC1 texture?
96 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
97 return kUnknown_GrPixelConfig;
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;
106 *outStartOfDataToUpload = ktx.pixelData();
107 return kETC1_GrPixelConfig;
110 return kUnknown_GrPixelConfig;
113 //////////////////////////////////////////////////////////////////////////////
116 * Fill out buffer with the compressed format Ganesh expects from a colortable
117 * based bitmap. [palette (colortable) + indices].
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.
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.
127 static void build_index8_data(void* buffer, const SkPixmap& pixmap) {
128 SkASSERT(kIndex_8_SkColorType == pixmap.colorType());
130 const SkColorTable* ctable = pixmap.ctable();
131 char* dst = (char*)buffer;
133 const int count = ctable->count();
135 SkDstPixelInfo dstPI;
136 dstPI.fColorType = kRGBA_8888_SkColorType;
137 dstPI.fAlphaType = kPremul_SkAlphaType;
138 dstPI.fPixels = buffer;
139 dstPI.fRowBytes = count * sizeof(SkPMColor);
141 SkSrcPixelInfo srcPI;
142 srcPI.fColorType = kN32_SkColorType;
143 srcPI.fAlphaType = kPremul_SkAlphaType;
144 srcPI.fPixels = ctable->readColors();
145 srcPI.fRowBytes = count * sizeof(SkPMColor);
147 srcPI.convertPixelsTo(&dstPI, count, 1);
149 // always skip a full 256 number of entries, even if we memcpy'd fewer
150 dst += 256 * sizeof(GrColor);
152 if ((unsigned)pixmap.width() == pixmap.rowBytes()) {
153 memcpy(dst, pixmap.addr(), pixmap.getSafeSize());
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);
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.
171 class PixelRef_GrYUVProvider : public GrYUVProvider {
175 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
177 uint32_t onGetID() override { return fPR->getGenerationID(); }
178 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
179 return fPR->queryYUV8(sizeInfo, colorSpace);
181 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
182 return fPR->getYUV8Planes(sizeInfo, planes);
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())) {
196 PixelRef_GrYUVProvider provider(pixelRef);
198 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
201 static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
202 sk_sp<SkData> data(bm.pixelRef()->refEncodedData());
207 const void* startOfTexData;
208 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data.get(), bm.width(), bm.height(),
210 if (kUnknown_GrPixelConfig == desc.fConfig) {
214 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0);
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)) {
223 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
225 return texture.release();
228 SkAutoLockPixels alp(bitmap);
229 if (!bitmap.readyToDraw()) {
233 if (!bitmap.peekPixels(&pixmap)) {
236 return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes);
239 GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) {
240 const SkPixmap* pmap = &pixmap;
244 const GrCaps* caps = ctx->caps();
245 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
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());
260 SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
261 kN32_SkColorType, kPremul_SkAlphaType,
262 sk_ref_sp(pixmap.info().colorSpace()));
264 tmpBitmap.allocPixels(dstInfo);
266 SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
267 if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
270 if (!tmpBitmap.peekPixels(&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
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());
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())) {
289 if (!tmpBitmap.peekPixels(&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);
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(),
307 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
308 tmpBitmap.allocPixels(info);
309 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
312 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
316 // must rebuild desc, since we've forced the info to be N32
317 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
321 return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(),
326 ////////////////////////////////////////////////////////////////////////////////
328 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
329 class Invalidator : public SkPixelRef::GenIDChangeListener {
331 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
333 GrUniqueKeyInvalidatedMessage fMsg;
335 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
338 pixelRef->addGenIDChangeListener(new Invalidator(key));
341 GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap,
342 SkSourceGammaTreatment gammaTreatment)
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);
352 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
354 return texture.release();
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()) {
366 SkASSERT(sizeof(int) <= sizeof(uint32_t));
367 if (bitmap.width() < 0 || bitmap.height() < 0) {
371 SkAutoPixmapUnlock srcUnlocker;
372 if (!bitmap.requestLock(&srcUnlocker)) {
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()) {
381 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment, nullptr));
386 const int mipLevelCount = mipmaps->countLevels() + 1;
387 if (mipLevelCount < 1) {
391 const bool isMipMapped = mipLevelCount > 1;
392 desc.fIsMipMapped = isMipMapped;
394 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
396 texels[0].fPixels = pixmap.addr();
397 texels[0].fRowBytes = pixmap.rowBytes();
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();
407 GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc,
412 texture->texturePriv().setGammaTreatment(gammaTreatment);
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,
426 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
427 const GrTextureParams& params,
428 SkSourceGammaTreatment gammaTreatment) {
429 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, gammaTreatment);
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);
439 ///////////////////////////////////////////////////////////////////////////////
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();
446 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
447 // You can't be color-space aware in legacy mode
448 SkASSERT(gammaCorrect || !gamutXform);
452 // SkColor4f::FromColor does sRGB -> Linear
453 color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c));
455 // GrColor4f::FromGrColor just multiplies by 1/255
456 color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c));
460 color = gamutXform->apply(color);
466 ///////////////////////////////////////////////////////////////////////////////
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.
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;
496 SkASSERT(0); // shouldn't get here
497 return kUnknown_GrPixelConfig;
500 bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
503 case kAlpha_8_GrPixelConfig:
504 ct = kAlpha_8_SkColorType;
506 case kIndex_8_GrPixelConfig:
507 ct = kIndex_8_SkColorType;
509 case kRGB_565_GrPixelConfig:
510 ct = kRGB_565_SkColorType;
512 case kRGBA_4444_GrPixelConfig:
513 ct = kARGB_4444_SkColorType;
515 case kRGBA_8888_GrPixelConfig:
516 ct = kRGBA_8888_SkColorType;
518 case kBGRA_8888_GrPixelConfig:
519 ct = kBGRA_8888_SkColorType;
521 case kSRGBA_8888_GrPixelConfig:
522 ct = kRGBA_8888_SkColorType;
524 case kSBGRA_8888_GrPixelConfig:
525 ct = kBGRA_8888_SkColorType;
527 case kRGBA_half_GrPixelConfig:
528 ct = kRGBA_F16_SkColorType;
539 GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* 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;
547 SkDEBUGFAIL("No renderable config exists for color space with strange gamma");
548 return kUnknown_GrPixelConfig;
552 ////////////////////////////////////////////////////////////////////////////////////////////////
554 static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
555 if (primitiveIsSrc) {
556 return SkXfermode::kSrc_Mode != mode;
558 return SkXfermode::kDst_Mode != mode;
562 static inline bool skpaint_to_grpaint_impl(GrContext* context,
564 const SkPaint& skPaint,
565 const SkMatrix& viewM,
566 sk_sp<GrFragmentProcessor>* shaderProcessor,
567 SkXfermode::Mode* primColorMode,
570 grPaint->setAntiAlias(skPaint.isAntiAlias());
571 grPaint->setAllowSRGBInputs(dc->isGammaCorrect());
573 // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion.
574 GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), dc->isGammaCorrect(),
575 dc->getColorXformFromSRGB());
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(),
587 dc->sourceGammaTreatment()));
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;
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.
604 // The geometry processor will insert the primitive color to start the color chain, so
605 // the GrPaint color will be ignored.
607 GrColor4f shaderInput = origColor.opaque();
608 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
609 if (primitiveIsSrc) {
610 shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP),
613 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
616 // The above may return null if compose results in a pass through of the prim color.
618 grPaint->addColorFragmentProcessor(shaderFP);
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));
628 // The shader's FP sees the paint unpremul color
629 grPaint->setColor4f(origColor);
630 grPaint->addColorFragmentProcessor(std::move(shaderFP));
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),
644 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
648 grPaint->addColorFragmentProcessor(std::move(processor));
651 grPaint->setColor4f(origColor.opaque());
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));
660 // No shader, no primitive color.
661 grPaint->setColor4f(origColor.premul());
662 applyColorFilterToPaintColor = true;
666 SkColorFilter* colorFilter = skPaint.getColorFilter();
668 if (applyColorFilterToPaintColor) {
669 grPaint->setColor4f(GrColor4f::FromSkColor4f(
670 colorFilter->filterColor4f(origColor.toSkColor4f())).premul());
672 sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context));
674 grPaint->addColorFragmentProcessor(std::move(cfFP));
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()));
688 #ifndef SK_IGNORE_GPU_DITHER
689 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0 && !dc->isGammaCorrect()) {
690 grPaint->addColorFragmentProcessor(GrDitherEffect::Make());
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);
701 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
702 bool SkPaintToGrPaintReplaceShader(GrContext* context,
704 const SkPaint& skPaint,
705 sk_sp<GrFragmentProcessor> shaderFP,
710 return skpaint_to_grpaint_impl(context, dc, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
714 /** Ignores the SkShader (if any) on skPaint. */
715 bool SkPaintToGrPaintNoShader(GrContext* context,
717 const SkPaint& skPaint,
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,
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,
730 const SkPaint& skPaint,
731 const SkMatrix& viewM,
732 SkXfermode::Mode primColorMode,
735 return skpaint_to_grpaint_impl(context, dc, skPaint, viewM, nullptr, &primColorMode,
736 primitiveIsSrc, grPaint);
739 bool SkPaintToGrPaintWithTexture(GrContext* context,
741 const SkPaint& paint,
742 const SkMatrix& viewM,
743 sk_sp<GrFragmentProcessor> fp,
744 bool textureIsAlphaOnly,
746 sk_sp<GrFragmentProcessor> shaderFP;
747 if (textureIsAlphaOnly) {
748 if (const SkShader* shader = paint.getShader()) {
749 shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context,
752 paint.getFilterQuality(),
754 dc->sourceGammaTreatment()));
758 sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
759 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
761 shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp);
764 shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp);
767 return SkPaintToGrPaintReplaceShader(context, dc, paint, std::move(shaderFP), grPaint);
771 ////////////////////////////////////////////////////////////////////////////////////////////////
773 GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
774 const SkMatrix& viewM,
775 const SkMatrix& localM,
778 GrTextureParams::FilterMode textureFilterMode;
779 switch (paintFilterQuality) {
780 case kNone_SkFilterQuality:
781 textureFilterMode = GrTextureParams::kNone_FilterMode;
783 case kLow_SkFilterQuality:
784 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
786 case kMedium_SkFilterQuality: {
788 matrix.setConcat(viewM, localM);
789 if (matrix.getMinScale() < SK_Scalar1) {
790 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
792 // Don't trigger MIP level generation unnecessarily.
793 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
797 case kHigh_SkFilterQuality: {
799 matrix.setConcat(viewM, localM);
800 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
804 SkErrorInternals::SetError( kInvalidPaint_SkError,
805 "Sorry, I don't understand the filtering "
806 "mode you asked for. Falling back to "
808 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
812 return textureFilterMode;