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