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.
12 #include "GrBitmapTextureMaker.h"
14 #include "GrContext.h"
15 #include "GrGpuResourcePriv.h"
16 #include "GrRenderTargetContext.h"
17 #include "GrTexturePriv.h"
19 #include "GrXferProcessor.h"
20 #include "GrYUVProvider.h"
22 #include "SkBlendModePriv.h"
23 #include "SkColorFilter.h"
24 #include "SkConfig8888.h"
27 #include "SkMaskFilter.h"
28 #include "SkMessageBus.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"
42 #ifndef SK_IGNORE_ETC1_SUPPORT
47 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) {
49 desc.fFlags = kNone_GrSurfaceFlags;
50 desc.fWidth = info.width();
51 desc.fHeight = info.height();
52 desc.fConfig = SkImageInfo2GrPixelConfig(info, caps);
57 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
60 SkASSERT(!imageBounds.isEmpty());
61 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
62 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
64 builder[1] = imageBounds.fLeft;
65 builder[2] = imageBounds.fTop;
66 builder[3] = imageBounds.fRight;
67 builder[4] = imageBounds.fBottom;
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;
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)
86 return kUnknown_GrPixelConfig;
89 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
90 return kETC1_GrPixelConfig;
91 } else if (SkKTXFile::is_ktx(bytes, data->size())) {
94 // Is it actually an ETC1 texture?
95 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
96 return kUnknown_GrPixelConfig;
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;
105 *outStartOfDataToUpload = ktx.pixelData();
106 return kETC1_GrPixelConfig;
109 return kUnknown_GrPixelConfig;
112 //////////////////////////////////////////////////////////////////////////////
115 * Fill out buffer with the compressed format Ganesh expects from a colortable
116 * based bitmap. [palette (colortable) + indices].
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.
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.
126 static void build_index8_data(void* buffer, const SkPixmap& pixmap) {
127 SkASSERT(kIndex_8_SkColorType == pixmap.colorType());
129 const SkColorTable* ctable = pixmap.ctable();
130 char* dst = (char*)buffer;
132 const int count = ctable->count();
134 SkDstPixelInfo dstPI;
135 dstPI.fColorType = kRGBA_8888_SkColorType;
136 dstPI.fAlphaType = kPremul_SkAlphaType;
137 dstPI.fPixels = buffer;
138 dstPI.fRowBytes = count * sizeof(SkPMColor);
140 SkSrcPixelInfo srcPI;
141 srcPI.fColorType = kN32_SkColorType;
142 srcPI.fAlphaType = kPremul_SkAlphaType;
143 srcPI.fPixels = ctable->readColors();
144 srcPI.fRowBytes = count * sizeof(SkPMColor);
146 srcPI.convertPixelsTo(&dstPI, count, 1);
148 // always skip a full 256 number of entries, even if we memcpy'd fewer
149 dst += 256 * sizeof(GrColor);
151 if ((unsigned)pixmap.width() == pixmap.rowBytes()) {
152 memcpy(dst, pixmap.addr(), pixmap.getSafeSize());
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);
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.
170 class PixelRef_GrYUVProvider : public GrYUVProvider {
174 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
176 uint32_t onGetID() override { return fPR->getGenerationID(); }
177 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
178 return fPR->queryYUV8(sizeInfo, colorSpace);
180 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
181 return fPR->getYUV8Planes(sizeInfo, planes);
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())) {
195 PixelRef_GrYUVProvider provider(pixelRef);
197 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
200 GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) {
201 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
203 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
205 return texture.release();
208 SkAutoLockPixels alp(bitmap);
209 if (!bitmap.readyToDraw()) {
213 if (!bitmap.peekPixels(&pixmap)) {
216 return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes);
219 GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) {
220 const SkPixmap* pmap = &pixmap;
224 const GrCaps* caps = ctx->caps();
225 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
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());
240 SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
241 kN32_SkColorType, kPremul_SkAlphaType,
242 sk_ref_sp(pixmap.info().colorSpace()));
244 tmpBitmap.allocPixels(dstInfo);
246 SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
247 if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
250 if (!tmpBitmap.peekPixels(&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
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());
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())) {
269 if (!tmpBitmap.peekPixels(&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);
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(),
287 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
288 tmpBitmap.allocPixels(info);
289 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
292 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
296 // must rebuild desc, since we've forced the info to be N32
297 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
301 return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(),
306 ////////////////////////////////////////////////////////////////////////////////
308 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
309 class Invalidator : public SkPixelRef::GenIDChangeListener {
311 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
313 GrUniqueKeyInvalidatedMessage fMsg;
315 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
318 pixelRef->addGenIDChangeListener(new Invalidator(key));
321 GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap,
322 SkColorSpace* dstColorSpace)
324 SkDestinationSurfaceColorMode colorMode = dstColorSpace
325 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
326 : SkDestinationSurfaceColorMode::kLegacy;
328 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
329 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
331 return texture.release();
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()) {
343 SkASSERT(sizeof(int) <= sizeof(uint32_t));
344 if (bitmap.width() < 0 || bitmap.height() < 0) {
348 SkAutoPixmapUnlock srcUnlocker;
349 if (!bitmap.requestLock(&srcUnlocker)) {
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()) {
358 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
363 const int mipLevelCount = mipmaps->countLevels() + 1;
364 if (mipLevelCount < 1) {
368 const bool isMipMapped = mipLevelCount > 1;
369 desc.fIsMipMapped = isMipMapped;
371 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
373 texels[0].fPixels = pixmap.addr();
374 texels[0].fRowBytes = pixmap.rowBytes();
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();
384 GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc,
389 texture->texturePriv().setMipColorMode(colorMode);
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,
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);
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,
414 return sk_sp<GrTexture>(tex);
417 ///////////////////////////////////////////////////////////////////////////////
419 GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) {
420 // We want to premultiply after linearizing, so this is easy:
421 return SkColorToUnpremulGrColor4f(c, dstColorSpace).premul();
424 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) {
426 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
427 auto gamutXform = GrColorSpaceXform::Make(srgbColorSpace.get(), dstColorSpace);
428 return SkColorToUnpremulGrColor4f(c, true, gamutXform.get());
430 return SkColorToUnpremulGrColor4f(c, false, nullptr);
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();
439 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
440 // You can't be color-space aware in legacy mode
441 SkASSERT(gammaCorrect || !gamutXform);
445 // SkColor4f::FromColor does sRGB -> Linear
446 color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c));
448 // GrColor4f::FromGrColor just multiplies by 1/255
449 color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c));
453 color = gamutXform->apply(color);
459 ///////////////////////////////////////////////////////////////////////////////
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.
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;
489 SkASSERT(0); // shouldn't get here
490 return kUnknown_GrPixelConfig;
493 bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
496 case kAlpha_8_GrPixelConfig:
497 ct = kAlpha_8_SkColorType;
499 case kIndex_8_GrPixelConfig:
500 ct = kIndex_8_SkColorType;
502 case kRGB_565_GrPixelConfig:
503 ct = kRGB_565_SkColorType;
505 case kRGBA_4444_GrPixelConfig:
506 ct = kARGB_4444_SkColorType;
508 case kRGBA_8888_GrPixelConfig:
509 ct = kRGBA_8888_SkColorType;
511 case kBGRA_8888_GrPixelConfig:
512 ct = kBGRA_8888_SkColorType;
514 case kSRGBA_8888_GrPixelConfig:
515 ct = kRGBA_8888_SkColorType;
517 case kSBGRA_8888_GrPixelConfig:
518 ct = kBGRA_8888_SkColorType;
520 case kRGBA_half_GrPixelConfig:
521 ct = kRGBA_F16_SkColorType;
532 GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* 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;
540 SkDEBUGFAIL("No renderable config exists for color space with strange gamma");
541 return kUnknown_GrPixelConfig;
545 ////////////////////////////////////////////////////////////////////////////////////////////////
547 static inline bool blend_requires_shader(const SkBlendMode mode, bool primitiveIsSrc) {
548 if (primitiveIsSrc) {
549 return SkBlendMode::kSrc != mode;
551 return SkBlendMode::kDst != mode;
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,
563 grPaint->setAntiAlias(skPaint.isAntiAlias());
564 grPaint->setAllowSRGBInputs(rtc->isGammaCorrect());
566 // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion.
567 GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), rtc->isGammaCorrect(),
568 rtc->getColorXformFromSRGB());
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()));
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;
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.
596 // The geometry processor will insert the primitive color to start the color chain, so
597 // the GrPaint color will be ignored.
599 GrColor4f shaderInput = origColor.opaque();
600 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
601 if (primitiveIsSrc) {
602 shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP),
605 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
608 // The above may return null if compose results in a pass through of the prim color.
610 grPaint->addColorFragmentProcessor(shaderFP);
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));
623 // The shader's FP sees the paint unpremul color
624 grPaint->setColor4f(origColor);
625 grPaint->addColorFragmentProcessor(std::move(shaderFP));
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),
638 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
642 grPaint->addColorFragmentProcessor(std::move(processor));
645 grPaint->setColor4f(origColor.opaque());
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));
657 // No shader, no primitive color.
658 grPaint->setColor4f(origColor.premul());
659 applyColorFilterToPaintColor = true;
663 SkColorFilter* colorFilter = skPaint.getColorFilter();
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());
672 grPaint->setColor4f(SkColorToPremulGrColor4f(
673 colorFilter->filterColor(skPaint.getColor()), false, nullptr));
676 sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context,
677 rtc->getColorSpace()));
679 grPaint->addColorFragmentProcessor(std::move(cfFP));
686 SkMaskFilter* maskFilter = skPaint.getMaskFilter();
688 GrFragmentProcessor* mfFP;
689 if (maskFilter->asFragmentProcessor(&mfFP, nullptr, viewM)) {
690 grPaint->addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor>(mfFP));
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()));
701 #ifndef SK_IGNORE_GPU_DITHER
702 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0 && !rtc->isGammaCorrect()) {
703 grPaint->addColorFragmentProcessor(GrDitherEffect::Make());
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);
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,
723 return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
727 /** Ignores the SkShader (if any) on skPaint. */
728 bool SkPaintToGrPaintNoShader(GrContext* context,
729 GrRenderTargetContext* rtc,
730 const SkPaint& skPaint,
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,
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,
748 return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, &primColorMode,
749 primitiveIsSrc, grPaint);
752 bool SkPaintToGrPaintWithTexture(GrContext* context,
753 GrRenderTargetContext* rtc,
754 const SkPaint& paint,
755 const SkMatrix& viewM,
756 sk_sp<GrFragmentProcessor> fp,
757 bool textureIsAlphaOnly,
759 sk_sp<GrFragmentProcessor> shaderFP;
760 if (textureIsAlphaOnly) {
761 if (const SkShader* shader = paint.getShader()) {
762 shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context,
765 paint.getFilterQuality(),
766 rtc->getColorSpace()));
770 sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
771 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
773 shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp);
776 shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp);
779 return SkPaintToGrPaintReplaceShader(context, rtc, paint, std::move(shaderFP), grPaint);
783 ////////////////////////////////////////////////////////////////////////////////////////////////
785 GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
786 const SkMatrix& viewM,
787 const SkMatrix& localM,
790 GrSamplerParams::FilterMode textureFilterMode;
791 switch (paintFilterQuality) {
792 case kNone_SkFilterQuality:
793 textureFilterMode = GrSamplerParams::kNone_FilterMode;
795 case kLow_SkFilterQuality:
796 textureFilterMode = GrSamplerParams::kBilerp_FilterMode;
798 case kMedium_SkFilterQuality: {
800 matrix.setConcat(viewM, localM);
801 if (matrix.getMinScale() < SK_Scalar1) {
802 textureFilterMode = GrSamplerParams::kMipMap_FilterMode;
804 // Don't trigger MIP level generation unnecessarily.
805 textureFilterMode = GrSamplerParams::kBilerp_FilterMode;
809 case kHigh_SkFilterQuality: {
811 matrix.setConcat(viewM, localM);
812 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
816 // Should be unreachable. If not, fall back to mipmaps.
817 textureFilterMode = GrSamplerParams::kMipMap_FilterMode;
821 return textureFilterMode;