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