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