reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #include "SkGr.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 9 | |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 10 | #include "GrBitmapTextureMaker.h" |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 14 | #include "GrRenderTargetContext.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 15 | #include "GrResourceProvider.h" |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 16 | #include "GrTextureProxy.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 17 | #include "GrTypes.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 18 | #include "GrXferProcessor.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 19 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 20 | #include "SkAutoMalloc.h" |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 21 | #include "SkBlendModePriv.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 22 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 23 | #include "SkColorFilter.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 24 | #include "SkConvertPixels.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 25 | #include "SkData.h" |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 26 | #include "SkImageInfoPriv.h" |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 27 | #include "SkMaskFilter.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 28 | #include "SkMessageBus.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 29 | #include "SkMipMap.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 30 | #include "SkPM4fPriv.h" |
Florin Malita | d4a70ee | 2017-06-19 10:21:43 -0400 | [diff] [blame] | 31 | #include "SkPaintPriv.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 32 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 33 | #include "SkResourceCache.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 34 | #include "SkShaderBase.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 35 | #include "SkTemplates.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 36 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 37 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 38 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 39 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 40 | #include "effects/GrXfermodeFragmentProcessor.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 41 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 42 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 43 | GrSurfaceDesc desc; |
| 44 | desc.fFlags = kNone_GrSurfaceFlags; |
| 45 | desc.fWidth = info.width(); |
| 46 | desc.fHeight = info.height(); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 47 | desc.fConfig = SkImageInfo2GrPixelConfig(info, caps); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 48 | desc.fSampleCnt = 0; |
| 49 | return desc; |
| 50 | } |
| 51 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 52 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) { |
| 53 | SkASSERT(key); |
| 54 | SkASSERT(imageID); |
| 55 | SkASSERT(!imageBounds.isEmpty()); |
| 56 | static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain(); |
| 57 | GrUniqueKey::Builder builder(key, kImageIDDomain, 5); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 58 | builder[0] = imageID; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 59 | builder[1] = imageBounds.fLeft; |
| 60 | builder[2] = imageBounds.fTop; |
| 61 | builder[3] = imageBounds.fRight; |
| 62 | builder[4] = imageBounds.fBottom; |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 65 | ////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 66 | sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrResourceProvider* resourceProvider, |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 67 | const SkBitmap& bitmap, |
| 68 | SkColorSpace* dstColorSpace) { |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 69 | if (!bitmap.readyToDraw()) { |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 70 | return nullptr; |
| 71 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 72 | SkPixmap pixmap; |
| 73 | if (!bitmap.peekPixels(&pixmap)) { |
| 74 | return nullptr; |
| 75 | } |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 76 | return GrUploadPixmapToTextureProxy(resourceProvider, pixmap, SkBudgeted::kYes, dstColorSpace); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 77 | } |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 78 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 79 | static const SkPixmap* compute_desc(const GrCaps& caps, const SkPixmap& pixmap, |
| 80 | GrSurfaceDesc* desc, |
| 81 | SkBitmap* tmpBitmap, SkPixmap* tmpPixmap) { |
| 82 | const SkPixmap* pmap = &pixmap; |
| 83 | |
| 84 | *desc = GrImageInfoToSurfaceDesc(pixmap.info(), caps); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 85 | |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 86 | // TODO: We're checking for srgbSupport, but we can then end up picking sBGRA as our pixel |
| 87 | // config (which may not be supported). We need better fallback management here. |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 88 | SkColorSpace* colorSpace = pixmap.colorSpace(); |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 89 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 90 | if (caps.srgbSupport() && |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 91 | colorSpace && colorSpace->gammaCloseToSRGB() && !GrPixelConfigIsSRGB(desc->fConfig)) { |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 92 | // We were supplied an sRGB-like color space, but we don't have a suitable pixel config. |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 93 | // Convert to 8888 sRGB so we can handle the data correctly. The raster backend doesn't |
| 94 | // handle sRGB Index8 -> sRGB 8888 correctly (yet), so lie about both the source and |
| 95 | // destination (claim they're linear): |
| 96 | SkImageInfo linSrcInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(), |
| 97 | pixmap.colorType(), pixmap.alphaType()); |
| 98 | SkPixmap linSrcPixmap(linSrcInfo, pixmap.addr(), pixmap.rowBytes(), pixmap.ctable()); |
| 99 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 100 | SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(), |
| 101 | kN32_SkColorType, kPremul_SkAlphaType, |
Robert Phillips | 70b49fd | 2017-01-13 11:21:36 -0500 | [diff] [blame] | 102 | pixmap.info().refColorSpace()); |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 103 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 104 | tmpBitmap->allocPixels(dstInfo); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 105 | |
| 106 | SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height()); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 107 | if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap->getPixels(), tmpBitmap->rowBytes())) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 108 | return nullptr; |
| 109 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 110 | if (!tmpBitmap->peekPixels(tmpPixmap)) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 111 | return nullptr; |
| 112 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 113 | pmap = tmpPixmap; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 114 | // must rebuild desc, since we've forced the info to be N32 |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 115 | *desc = GrImageInfoToSurfaceDesc(pmap->info(), caps); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 116 | } else if (kIndex_8_SkColorType == pixmap.colorType()) { |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 117 | SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height()); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 118 | tmpBitmap->allocPixels(info); |
| 119 | if (!pixmap.readPixels(info, tmpBitmap->getPixels(), tmpBitmap->rowBytes())) { |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 120 | return nullptr; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 121 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 122 | if (!tmpBitmap->peekPixels(tmpPixmap)) { |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 123 | return nullptr; |
| 124 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 125 | pmap = tmpPixmap; |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 126 | // must rebuild desc, since we've forced the info to be N32 |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 127 | *desc = GrImageInfoToSurfaceDesc(pmap->info(), caps); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 128 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 130 | return pmap; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 133 | sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxy(GrResourceProvider* resourceProvider, |
| 134 | const SkPixmap& pixmap, |
| 135 | SkBudgeted budgeted, |
| 136 | SkColorSpace* dstColorSpace) { |
| 137 | SkDestinationSurfaceColorMode colorMode = dstColorSpace |
| 138 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 139 | : SkDestinationSurfaceColorMode::kLegacy; |
| 140 | |
| 141 | if (!SkImageInfoIsValid(pixmap.info(), colorMode)) { |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 145 | SkBitmap tmpBitmap; |
| 146 | SkPixmap tmpPixmap; |
| 147 | GrSurfaceDesc desc; |
| 148 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 149 | if (const SkPixmap* pmap = compute_desc(*resourceProvider->caps(), pixmap, &desc, |
| 150 | &tmpBitmap, &tmpPixmap)) { |
| 151 | return GrSurfaceProxy::MakeDeferred(resourceProvider, desc, |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 152 | budgeted, pmap->addr(), pmap->rowBytes()); |
| 153 | } |
| 154 | |
| 155 | return nullptr; |
| 156 | } |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 157 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 158 | //////////////////////////////////////////////////////////////////////////////// |
| 159 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 160 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 161 | class Invalidator : public SkPixelRef::GenIDChangeListener { |
| 162 | public: |
| 163 | explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 164 | private: |
| 165 | GrUniqueKeyInvalidatedMessage fMsg; |
| 166 | |
| 167 | void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 168 | }; |
| 169 | |
| 170 | pixelRef->addGenIDChangeListener(new Invalidator(key)); |
| 171 | } |
| 172 | |
Robert Phillips | 7807580 | 2017-03-23 11:11:59 -0400 | [diff] [blame] | 173 | sk_sp<GrTextureProxy> GrGenerateMipMapsAndUploadToTextureProxy(GrContext* ctx, |
| 174 | const SkBitmap& bitmap, |
| 175 | SkColorSpace* dstColorSpace) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 176 | SkDestinationSurfaceColorMode colorMode = dstColorSpace |
| 177 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 178 | : SkDestinationSurfaceColorMode::kLegacy; |
| 179 | |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 180 | if (!SkImageInfoIsValid(bitmap.info(), colorMode)) { |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 181 | return nullptr; |
| 182 | } |
| 183 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 184 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 185 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 186 | SkPixmap pixmap; |
| 187 | if (!bitmap.peekPixels(&pixmap)) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 188 | return nullptr; |
| 189 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 190 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 191 | std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 192 | if (!mipmaps) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
| 196 | const int mipLevelCount = mipmaps->countLevels() + 1; |
| 197 | if (mipLevelCount < 1) { |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | const bool isMipMapped = mipLevelCount > 1; |
| 202 | desc.fIsMipMapped = isMipMapped; |
| 203 | |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 204 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 205 | |
| 206 | texels[0].fPixels = pixmap.addr(); |
| 207 | texels[0].fRowBytes = pixmap.rowBytes(); |
| 208 | |
| 209 | for (int i = 1; i < mipLevelCount; ++i) { |
| 210 | SkMipMap::Level generatedMipLevel; |
| 211 | mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 212 | texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 213 | texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 214 | } |
| 215 | |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 216 | return GrSurfaceProxy::MakeDeferredMipMap(ctx->resourceProvider(), |
| 217 | desc, |
| 218 | SkBudgeted::kYes, |
| 219 | texels.get(), |
| 220 | mipLevelCount, |
| 221 | colorMode); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 224 | sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext* ctx, const SkImageInfo& info, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame^] | 225 | const GrMipLevel texels[], |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 226 | int mipLevelCount, |
| 227 | SkDestinationSurfaceColorMode colorMode) { |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 228 | if (!SkImageInfoIsValid(info, colorMode)) { |
Robert Phillips | 7807580 | 2017-03-23 11:11:59 -0400 | [diff] [blame] | 229 | return nullptr; |
| 230 | } |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 231 | |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 232 | return GrSurfaceProxy::MakeDeferredMipMap(ctx->resourceProvider(), |
| 233 | GrImageInfoToSurfaceDesc(info, *ctx->caps()), |
| 234 | SkBudgeted::kYes, texels, |
| 235 | mipLevelCount, colorMode); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 238 | sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext* ctx, |
| 239 | const SkBitmap& bitmap, |
| 240 | const GrSamplerParams& params, |
| 241 | SkScalar scaleAdjust[2]) { |
| 242 | // Caller doesn't care about the texture's color space (they can always get it from the bitmap) |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 243 | return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, nullptr, |
| 244 | nullptr, scaleAdjust); |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 247 | sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrResourceProvider* resourceProvider, |
| 248 | const SkBitmap& bitmap) { |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 249 | GrUniqueKey originalKey; |
| 250 | |
| 251 | if (!bitmap.isVolatile()) { |
| 252 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 253 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(), bitmap.height()); |
| 254 | GrMakeKeyFromImageID(&originalKey, bitmap.pixelRef()->getGenerationID(), subset); |
| 255 | } |
| 256 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 257 | sk_sp<GrTextureProxy> proxy; |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 258 | |
| 259 | if (originalKey.isValid()) { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 260 | proxy = resourceProvider->findProxyByUniqueKey(originalKey); |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 261 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 262 | if (!proxy) { |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 263 | // Pass nullptr for |dstColorSpace|. This is lenient - we allow a wider range of |
| 264 | // color spaces in legacy mode. Unfortunately, we have to be lenient here, since |
| 265 | // we can't necessarily know the |dstColorSpace| at this time. |
| 266 | proxy = GrUploadBitmapToTextureProxy(resourceProvider, bitmap, nullptr); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 267 | if (proxy && originalKey.isValid()) { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 268 | resourceProvider->assignUniqueKeyToProxy(originalKey, proxy.get()); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 269 | // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 270 | GrInstallBitmapUniqueKeyInvalidator(originalKey, bitmap.pixelRef()); |
| 271 | } |
| 272 | } |
| 273 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 274 | return proxy; |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 275 | } |
| 276 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 277 | /////////////////////////////////////////////////////////////////////////////// |
| 278 | |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 279 | GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) { |
| 280 | // We want to premultiply after linearizing, so this is easy: |
| 281 | return SkColorToUnpremulGrColor4f(c, dstColorSpace).premul(); |
| 282 | } |
| 283 | |
| 284 | GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) { |
| 285 | if (dstColorSpace) { |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 286 | auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 287 | auto gamutXform = GrColorSpaceXform::Make(srgbColorSpace.get(), dstColorSpace); |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 288 | return SkColorToUnpremulGrColor4f(c, dstColorSpace, gamutXform.get()); |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 289 | } else { |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 290 | return SkColorToUnpremulGrColor4f(c, nullptr, nullptr); |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 294 | GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace, |
| 295 | GrColorSpaceXform* gamutXform) { |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 296 | // We want to premultiply after linearizing, so this is easy: |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 297 | return SkColorToUnpremulGrColor4f(c, dstColorSpace, gamutXform).premul(); |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 300 | GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace, |
| 301 | GrColorSpaceXform* gamutXform) { |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 302 | // You can't be color-space aware in legacy mode |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 303 | SkASSERT(dstColorSpace || !gamutXform); |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 304 | |
| 305 | GrColor4f color; |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 306 | if (dstColorSpace) { |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 307 | // SkColor4f::FromColor does sRGB -> Linear |
| 308 | color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c)); |
| 309 | } else { |
| 310 | // GrColor4f::FromGrColor just multiplies by 1/255 |
| 311 | color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c)); |
| 312 | } |
| 313 | |
| 314 | if (gamutXform) { |
| 315 | color = gamutXform->apply(color); |
| 316 | } |
| 317 | |
| 318 | return color; |
| 319 | } |
| 320 | |
| 321 | /////////////////////////////////////////////////////////////////////////////// |
| 322 | |
Brian Osman | 0c2997b | 2017-01-11 16:58:42 -0500 | [diff] [blame] | 323 | GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info, const GrCaps& caps) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 324 | // We intentionally ignore profile type for non-8888 formats. Anything we can't support |
| 325 | // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture. |
Brian Osman | 0c2997b | 2017-01-11 16:58:42 -0500 | [diff] [blame] | 326 | SkColorSpace* cs = info.colorSpace(); |
| 327 | switch (info.colorType()) { |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 328 | case kUnknown_SkColorType: |
| 329 | return kUnknown_GrPixelConfig; |
| 330 | case kAlpha_8_SkColorType: |
| 331 | return kAlpha_8_GrPixelConfig; |
| 332 | case kRGB_565_SkColorType: |
| 333 | return kRGB_565_GrPixelConfig; |
| 334 | case kARGB_4444_SkColorType: |
| 335 | return kRGBA_4444_GrPixelConfig; |
| 336 | case kRGBA_8888_SkColorType: |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 337 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 338 | ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 339 | case kBGRA_8888_SkColorType: |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 340 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 341 | ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 342 | case kIndex_8_SkColorType: |
Brian Osman | d0be1ef | 2017-01-11 16:57:15 -0500 | [diff] [blame] | 343 | return kSkia8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 344 | case kGray_8_SkColorType: |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 345 | return kGray_8_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 346 | case kRGBA_F16_SkColorType: |
| 347 | return kRGBA_half_GrPixelConfig; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 348 | } |
| 349 | SkASSERT(0); // shouldn't get here |
| 350 | return kUnknown_GrPixelConfig; |
| 351 | } |
| 352 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 353 | bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 354 | SkColorType ct; |
| 355 | switch (config) { |
| 356 | case kAlpha_8_GrPixelConfig: |
| 357 | ct = kAlpha_8_SkColorType; |
| 358 | break; |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 359 | case kGray_8_GrPixelConfig: |
| 360 | ct = kGray_8_SkColorType; |
| 361 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 362 | case kRGB_565_GrPixelConfig: |
| 363 | ct = kRGB_565_SkColorType; |
| 364 | break; |
| 365 | case kRGBA_4444_GrPixelConfig: |
| 366 | ct = kARGB_4444_SkColorType; |
| 367 | break; |
| 368 | case kRGBA_8888_GrPixelConfig: |
| 369 | ct = kRGBA_8888_SkColorType; |
| 370 | break; |
| 371 | case kBGRA_8888_GrPixelConfig: |
| 372 | ct = kBGRA_8888_SkColorType; |
| 373 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 374 | case kSRGBA_8888_GrPixelConfig: |
| 375 | ct = kRGBA_8888_SkColorType; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 376 | break; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 377 | case kSBGRA_8888_GrPixelConfig: |
| 378 | ct = kBGRA_8888_SkColorType; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 379 | break; |
brianosman | 9ac5b91 | 2016-04-12 13:49:53 -0700 | [diff] [blame] | 380 | case kRGBA_half_GrPixelConfig: |
| 381 | ct = kRGBA_F16_SkColorType; |
| 382 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 383 | default: |
| 384 | return false; |
| 385 | } |
| 386 | if (ctOut) { |
| 387 | *ctOut = ct; |
| 388 | } |
| 389 | return true; |
| 390 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 391 | |
brianosman | 27a3aa5 | 2016-09-23 07:11:34 -0700 | [diff] [blame] | 392 | GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* colorSpace) { |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 393 | if (!colorSpace) { |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 394 | return kRGBA_8888_GrPixelConfig; |
| 395 | } else if (colorSpace->gammaIsLinear()) { |
| 396 | return kRGBA_half_GrPixelConfig; |
| 397 | } else if (colorSpace->gammaCloseToSRGB()) { |
| 398 | return kSRGBA_8888_GrPixelConfig; |
| 399 | } else { |
| 400 | SkDEBUGFAIL("No renderable config exists for color space with strange gamma"); |
| 401 | return kUnknown_GrPixelConfig; |
| 402 | } |
| 403 | } |
| 404 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 405 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 406 | |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 407 | static inline bool blend_requires_shader(const SkBlendMode mode) { |
| 408 | return SkBlendMode::kDst != mode; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 409 | } |
| 410 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 411 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 412 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 413 | const SkPaint& skPaint, |
| 414 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 415 | sk_sp<GrFragmentProcessor>* shaderProcessor, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 416 | SkBlendMode* primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 417 | GrPaint* grPaint) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 418 | grPaint->setAllowSRGBInputs(rtc->isGammaCorrect()); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 419 | |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 420 | // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion. |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 421 | GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), rtc->getColorSpace(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 422 | rtc->getColorXformFromSRGB()); |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 423 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 424 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 425 | // of per-vertex colors. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 426 | sk_sp<GrFragmentProcessor> shaderFP; |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 427 | if (!primColorMode || blend_requires_shader(*primColorMode)) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 428 | if (shaderProcessor) { |
| 429 | shaderFP = *shaderProcessor; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 430 | } else if (const auto* shader = as_SB(skPaint.getShader())) { |
| 431 | shaderFP = shader->asFragmentProcessor( |
| 432 | SkShaderBase::AsFPArgs(context, &viewM, nullptr, skPaint.getFilterQuality(), |
| 433 | rtc->getColorSpace())); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 434 | if (!shaderFP) { |
| 435 | return false; |
| 436 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 441 | // a known constant value. In that case we can simply apply a color filter during this |
| 442 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 443 | bool applyColorFilterToPaintColor = false; |
| 444 | if (shaderFP) { |
| 445 | if (primColorMode) { |
| 446 | // There is a blend between the primitive color and the shader color. The shader sees |
| 447 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 448 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 449 | |
| 450 | // The geometry processor will insert the primitive color to start the color chain, so |
| 451 | // the GrPaint color will be ignored. |
| 452 | |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 453 | GrColor4f shaderInput = origColor.opaque(); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 454 | shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput); |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 455 | shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP), |
| 456 | *primColorMode); |
| 457 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 458 | // The above may return null if compose results in a pass through of the prim color. |
| 459 | if (shaderFP) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 460 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 461 | } |
| 462 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 463 | // We can ignore origColor here - alpha is unchanged by gamma |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 464 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 465 | if (GrColor_WHITE != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 466 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 467 | // color channels. It's value should be treated as the same in ANY color space. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 468 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 469 | GrColor4f::FromGrColor(paintAlpha), |
| 470 | GrConstColorProcessor::kModulateRGBA_InputMode)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 471 | } |
| 472 | } else { |
| 473 | // The shader's FP sees the paint unpremul color |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 474 | grPaint->setColor4f(origColor); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 475 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 476 | } |
| 477 | } else { |
| 478 | if (primColorMode) { |
| 479 | // There is a blend between the primitive color and the paint color. The blend considers |
| 480 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 481 | sk_sp<GrFragmentProcessor> processor( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 482 | GrConstColorProcessor::Make(origColor.opaque(), |
| 483 | GrConstColorProcessor::kIgnore_InputMode)); |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 484 | processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor), |
| 485 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 486 | if (processor) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 487 | grPaint->addColorFragmentProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 490 | grPaint->setColor4f(origColor.opaque()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 491 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 492 | // We can ignore origColor here - alpha is unchanged by gamma |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 493 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 494 | if (GrColor_WHITE != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 495 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 496 | // color channels. It's value should be treated as the same in ANY color space. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 497 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 498 | GrColor4f::FromGrColor(paintAlpha), |
| 499 | GrConstColorProcessor::kModulateRGBA_InputMode)); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 500 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 501 | } else { |
| 502 | // No shader, no primitive color. |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 503 | grPaint->setColor4f(origColor.premul()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 504 | applyColorFilterToPaintColor = true; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 509 | if (colorFilter) { |
| 510 | if (applyColorFilterToPaintColor) { |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 511 | // If we're in legacy mode, we *must* avoid using the 4f version of the color filter, |
| 512 | // because that will combine with the linearized version of the stored color. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 513 | if (rtc->isGammaCorrect()) { |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 514 | grPaint->setColor4f(GrColor4f::FromSkColor4f( |
| 515 | colorFilter->filterColor4f(origColor.toSkColor4f())).premul()); |
| 516 | } else { |
| 517 | grPaint->setColor4f(SkColorToPremulGrColor4f( |
Brian Osman | 4729914 | 2017-03-07 13:22:22 -0500 | [diff] [blame] | 518 | colorFilter->filterColor(skPaint.getColor()), nullptr, nullptr)); |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 519 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 520 | } else { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 521 | sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 522 | rtc->getColorSpace())); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 523 | if (cfFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 524 | grPaint->addColorFragmentProcessor(std::move(cfFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 525 | } else { |
| 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 531 | SkMaskFilter* maskFilter = skPaint.getMaskFilter(); |
| 532 | if (maskFilter) { |
| 533 | GrFragmentProcessor* mfFP; |
Robert Phillips | d9d8485 | 2017-06-09 10:48:29 -0400 | [diff] [blame] | 534 | if (maskFilter->asFragmentProcessor(&mfFP)) { |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 535 | grPaint->addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor>(mfFP)); |
| 536 | } |
| 537 | } |
| 538 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 539 | // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on |
| 540 | // the GrPaint to also be null (also kSrcOver). |
| 541 | SkASSERT(!grPaint->getXPFactory()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 542 | if (!skPaint.isSrcOver()) { |
| 543 | grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode())); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 544 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 545 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 546 | #ifndef SK_IGNORE_GPU_DITHER |
Florin Malita | d4a70ee | 2017-06-19 10:21:43 -0400 | [diff] [blame] | 547 | // Conservative default, in case GrPixelConfigToColorType() fails. |
| 548 | SkColorType ct = SkColorType::kRGB_565_SkColorType; |
| 549 | GrPixelConfigToColorType(rtc->config(), &ct); |
| 550 | if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0 |
| 551 | && !rtc->isGammaCorrect()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 552 | grPaint->addColorFragmentProcessor(GrDitherEffect::Make()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 553 | } |
| 554 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 555 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 558 | bool SkPaintToGrPaint(GrContext* context, GrRenderTargetContext* rtc, const SkPaint& skPaint, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 559 | const SkMatrix& viewM, GrPaint* grPaint) { |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 560 | return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, nullptr, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 561 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 562 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 563 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 564 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 565 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 566 | const SkPaint& skPaint, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 567 | sk_sp<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 568 | GrPaint* grPaint) { |
| 569 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 570 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 571 | } |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 572 | return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), &shaderFP, nullptr, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 573 | grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 574 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 575 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 576 | /** Ignores the SkShader (if any) on skPaint. */ |
| 577 | bool SkPaintToGrPaintNoShader(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 578 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 579 | const SkPaint& skPaint, |
| 580 | GrPaint* grPaint) { |
| 581 | // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 582 | static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr); |
| 583 | static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 584 | return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 585 | grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 589 | be setup as a vertex attribute using the specified SkBlendMode. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 590 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 591 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 592 | const SkPaint& skPaint, |
| 593 | const SkMatrix& viewM, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 594 | SkBlendMode primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 595 | GrPaint* grPaint) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 596 | return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, &primColorMode, |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 597 | grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 598 | } |
| 599 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 600 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 601 | GrRenderTargetContext* rtc, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 602 | const SkPaint& paint, |
| 603 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 604 | sk_sp<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 605 | bool textureIsAlphaOnly, |
| 606 | GrPaint* grPaint) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 607 | sk_sp<GrFragmentProcessor> shaderFP; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 608 | if (textureIsAlphaOnly) { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 609 | if (const auto* shader = as_SB(paint.getShader())) { |
| 610 | shaderFP = shader->asFragmentProcessor( |
| 611 | SkShaderBase::AsFPArgs(context, &viewM, nullptr, paint.getFilterQuality(), |
| 612 | rtc->getColorSpace())); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 613 | if (!shaderFP) { |
| 614 | return false; |
| 615 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 616 | sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) }; |
| 617 | shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 618 | } else { |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 619 | shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(fp); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 620 | } |
| 621 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 622 | shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 625 | return SkPaintToGrPaintReplaceShader(context, rtc, paint, std::move(shaderFP), grPaint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 626 | } |
| 627 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 628 | |
| 629 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 630 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 631 | GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 632 | const SkMatrix& viewM, |
| 633 | const SkMatrix& localM, |
| 634 | bool* doBicubic) { |
| 635 | *doBicubic = false; |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 636 | GrSamplerParams::FilterMode textureFilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 637 | switch (paintFilterQuality) { |
| 638 | case kNone_SkFilterQuality: |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 639 | textureFilterMode = GrSamplerParams::kNone_FilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 640 | break; |
| 641 | case kLow_SkFilterQuality: |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 642 | textureFilterMode = GrSamplerParams::kBilerp_FilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 643 | break; |
| 644 | case kMedium_SkFilterQuality: { |
| 645 | SkMatrix matrix; |
| 646 | matrix.setConcat(viewM, localM); |
| 647 | if (matrix.getMinScale() < SK_Scalar1) { |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 648 | textureFilterMode = GrSamplerParams::kMipMap_FilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 649 | } else { |
| 650 | // Don't trigger MIP level generation unnecessarily. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 651 | textureFilterMode = GrSamplerParams::kBilerp_FilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 652 | } |
| 653 | break; |
| 654 | } |
| 655 | case kHigh_SkFilterQuality: { |
| 656 | SkMatrix matrix; |
| 657 | matrix.setConcat(viewM, localM); |
| 658 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 659 | break; |
| 660 | } |
| 661 | default: |
Mike Klein | e54c75f | 2016-10-13 14:18:09 -0400 | [diff] [blame] | 662 | // Should be unreachable. If not, fall back to mipmaps. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 663 | textureFilterMode = GrSamplerParams::kMipMap_FilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | } |
| 667 | return textureFilterMode; |
| 668 | } |