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" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 9 | #include "GrBitmapTextureMaker.h" |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 11 | #include "GrColorSpaceXform.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 14 | #include "GrGpuResourcePriv.h" |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 15 | #include "GrPaint.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 16 | #include "GrProxyProvider.h" |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 17 | #include "GrTextureProxy.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 18 | #include "GrTypes.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 19 | #include "GrXferProcessor.h" |
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" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 24 | #include "SkData.h" |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 25 | #include "SkImage_Base.h" |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 26 | #include "SkImageInfoPriv.h" |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 27 | #include "SkImagePriv.h" |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 28 | #include "SkMaskFilterBase.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 29 | #include "SkMessageBus.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 30 | #include "SkMipMap.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 31 | #include "SkPM4fPriv.h" |
Florin Malita | d4a70ee | 2017-06-19 10:21:43 -0400 | [diff] [blame] | 32 | #include "SkPaintPriv.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 33 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 34 | #include "SkResourceCache.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 35 | #include "SkShaderBase.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 36 | #include "SkTemplates.h" |
Derek Sollenberger | 559f534 | 2017-08-17 12:34:54 -0400 | [diff] [blame] | 37 | #include "SkTraceEvent.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 38 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 39 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 40 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 41 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 42 | #include "effects/GrXfermodeFragmentProcessor.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [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); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 50 | desc.fSampleCnt = 1; |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 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(); |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 59 | GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image"); |
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 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 67 | ////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 68 | sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider* proxyProvider, |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 69 | const SkBitmap& bitmap, |
| 70 | SkColorSpace* dstColorSpace) { |
Greg Daniel | 7619b64 | 2018-02-08 15:32:02 -0500 | [diff] [blame] | 71 | if (!bitmap.peekPixels(nullptr)) { |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 72 | return nullptr; |
| 73 | } |
Greg Daniel | 7619b64 | 2018-02-08 15:32:02 -0500 | [diff] [blame] | 74 | |
| 75 | SkDestinationSurfaceColorMode colorMode = dstColorSpace |
| 76 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 77 | : SkDestinationSurfaceColorMode::kLegacy; |
| 78 | |
| 79 | if (!SkImageInfoIsValid(bitmap.info(), colorMode)) { |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 80 | return nullptr; |
| 81 | } |
Greg Daniel | 7619b64 | 2018-02-08 15:32:02 -0500 | [diff] [blame] | 82 | |
| 83 | // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap |
| 84 | // even if it's mutable. In ddl, if the bitmap is mutable then we must make a copy since the |
| 85 | // upload of the data to the gpu can happen at anytime and the bitmap may change by then. |
Robert Phillips | 5c4b33b | 2018-03-20 16:23:08 -0400 | [diff] [blame] | 86 | SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode |
| 87 | : kNever_SkCopyPixelsMode; |
Greg Daniel | 7619b64 | 2018-02-08 15:32:02 -0500 | [diff] [blame] | 88 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode); |
| 89 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 90 | return proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1, |
| 91 | SkBudgeted::kYes, SkBackingFit::kExact); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 92 | } |
Brian Osman | 4075ec8 | 2017-01-17 16:41:03 +0000 | [diff] [blame] | 93 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 94 | //////////////////////////////////////////////////////////////////////////////// |
| 95 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 96 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 97 | class Invalidator : public SkPixelRef::GenIDChangeListener { |
| 98 | public: |
| 99 | explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 100 | private: |
| 101 | GrUniqueKeyInvalidatedMessage fMsg; |
| 102 | |
| 103 | void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 104 | }; |
| 105 | |
| 106 | pixelRef->addGenIDChangeListener(new Invalidator(key)); |
| 107 | } |
| 108 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 109 | sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext* ctx, GrTextureProxy* baseProxy) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 110 | SkASSERT(baseProxy); |
| 111 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 112 | if (!ctx->contextPriv().caps()->isConfigCopyable(baseProxy->config())) { |
Greg Daniel | bb76ace | 2017-09-29 15:58:22 -0400 | [diff] [blame] | 113 | return nullptr; |
| 114 | } |
| 115 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 116 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 117 | GrSurfaceDesc desc; |
| 118 | desc.fFlags = kNone_GrSurfaceFlags; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 119 | desc.fWidth = baseProxy->width(); |
| 120 | desc.fHeight = baseProxy->height(); |
| 121 | desc.fConfig = baseProxy->config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 122 | desc.fSampleCnt = 1; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 123 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 124 | sk_sp<GrTextureProxy> proxy = |
| 125 | proxyProvider->createMipMapProxy(desc, baseProxy->origin(), SkBudgeted::kYes); |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 126 | if (!proxy) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | // Copy the base layer to our proxy |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 131 | sk_sp<SkColorSpace> colorSpace; |
| 132 | if (GrPixelConfigIsSRGB(proxy->config())) { |
| 133 | colorSpace = SkColorSpace::MakeSRGB(); |
| 134 | } |
| 135 | sk_sp<GrSurfaceContext> sContext = |
| 136 | ctx->contextPriv().makeWrappedSurfaceContext(proxy, std::move(colorSpace)); |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 137 | SkASSERT(sContext); |
| 138 | SkAssertResult(sContext->copy(baseProxy)); |
| 139 | |
| 140 | return proxy; |
| 141 | } |
| 142 | |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 143 | sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext* ctx, |
| 144 | const SkBitmap& bitmap, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 145 | const GrSamplerState& params, |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 146 | SkScalar scaleAdjust[2]) { |
| 147 | // Caller doesn't care about the texture's color space (they can always get it from the bitmap) |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 148 | return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, nullptr, |
| 149 | nullptr, scaleAdjust); |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 152 | sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider, |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 153 | const SkBitmap& bitmap, |
| 154 | SkBackingFit fit) { |
| 155 | if (!bitmap.peekPixels(nullptr)) { |
| 156 | return nullptr; |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 159 | // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap |
| 160 | // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the |
| 161 | // upload of the data to the gpu can happen at anytime and the bitmap may change by then. |
Robert Phillips | 5c4b33b | 2018-03-20 16:23:08 -0400 | [diff] [blame] | 162 | SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode |
| 163 | : kNever_SkCopyPixelsMode; |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 164 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode); |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 165 | |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 166 | if (!image) { |
| 167 | return nullptr; |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 170 | return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit); |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 173 | static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) { |
| 174 | if (!image) { |
| 175 | result->reset(); // will be invalid |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) { |
Greg Daniel | a421112 | 2018-03-14 19:09:36 +0000 | [diff] [blame] | 180 | if (!bm->isVolatile()) { |
| 181 | SkIPoint origin = bm->pixelRefOrigin(); |
| 182 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height()); |
| 183 | GrMakeKeyFromImageID(result, bm->getGenerationID(), subset); |
| 184 | } |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds()); |
| 189 | } |
| 190 | |
| 191 | sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider, |
Greg Daniel | 490695b | 2018-02-05 09:34:02 -0500 | [diff] [blame] | 192 | sk_sp<SkImage> srcImage, |
| 193 | SkBackingFit fit) { |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 194 | sk_sp<GrTextureProxy> proxy; |
| 195 | GrUniqueKey originalKey; |
| 196 | |
| 197 | create_unique_key_for_image(srcImage.get(), &originalKey); |
| 198 | |
| 199 | if (originalKey.isValid()) { |
| 200 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin); |
| 201 | } |
| 202 | if (!proxy) { |
Greg Daniel | a421112 | 2018-03-14 19:09:36 +0000 | [diff] [blame] | 203 | proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 204 | SkBudgeted::kYes, fit); |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 205 | if (proxy && originalKey.isValid()) { |
| 206 | proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get()); |
Robert Phillips | 5c4b33b | 2018-03-20 16:23:08 -0400 | [diff] [blame] | 207 | const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap(); |
| 208 | // When recording DDLs we do not want to install change listeners because doing |
| 209 | // so isn't threadsafe. |
| 210 | if (bm && !proxyProvider->recordingDDL()) { |
Greg Daniel | a421112 | 2018-03-14 19:09:36 +0000 | [diff] [blame] | 211 | GrInstallBitmapUniqueKeyInvalidator(originalKey, bm->pixelRef()); |
| 212 | } |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | return proxy; |
| 217 | } |
| 218 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 219 | /////////////////////////////////////////////////////////////////////////////// |
| 220 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 221 | GrColor4f SkColorToPremulGrColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) { |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 222 | // We want to premultiply after linearizing, so this is easy: |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 223 | return SkColorToUnpremulGrColor4f(c, colorSpaceInfo).premul(); |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 226 | GrColor4f SkColorToPremulGrColor4fLegacy(SkColor c) { |
| 227 | return GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c)).premul(); |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 230 | GrColor4f SkColorToUnpremulGrColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) { |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 231 | GrColor4f color; |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 232 | if (colorSpaceInfo.colorSpace()) { |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 233 | // SkColor4f::FromColor does sRGB -> Linear |
| 234 | color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c)); |
| 235 | } else { |
| 236 | // GrColor4f::FromGrColor just multiplies by 1/255 |
| 237 | color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c)); |
| 238 | } |
| 239 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 240 | if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) { |
| 241 | color = xform->clampedXform(color); |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return color; |
| 245 | } |
| 246 | |
| 247 | /////////////////////////////////////////////////////////////////////////////// |
| 248 | |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 249 | GrPixelConfig SkImageInfo2GrPixelConfig(const SkColorType type, SkColorSpace* cs, |
| 250 | const GrCaps& caps) { |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 251 | switch (type) { |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 252 | case kUnknown_SkColorType: |
| 253 | return kUnknown_GrPixelConfig; |
| 254 | case kAlpha_8_SkColorType: |
| 255 | return kAlpha_8_GrPixelConfig; |
| 256 | case kRGB_565_SkColorType: |
| 257 | return kRGB_565_GrPixelConfig; |
| 258 | case kARGB_4444_SkColorType: |
| 259 | return kRGBA_4444_GrPixelConfig; |
| 260 | case kRGBA_8888_SkColorType: |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 261 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 262 | ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig; |
Brian Osman | 9d44081 | 2018-01-04 09:35:04 -0500 | [diff] [blame] | 263 | // TODO: We're checking for srgbSupport, but we can then end up picking sBGRA as our pixel |
| 264 | // config (which may not be supported). We need a better test here. |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 265 | case kRGB_888x_SkColorType: |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 266 | return kRGB_888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 267 | case kBGRA_8888_SkColorType: |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 268 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 269 | ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig; |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 270 | case kRGBA_1010102_SkColorType: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 271 | return kRGBA_1010102_GrPixelConfig; |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 272 | case kRGB_101010x_SkColorType: |
| 273 | return kUnknown_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 274 | case kGray_8_SkColorType: |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 275 | return kGray_8_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 276 | case kRGBA_F16_SkColorType: |
| 277 | return kRGBA_half_GrPixelConfig; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 278 | } |
| 279 | SkASSERT(0); // shouldn't get here |
| 280 | return kUnknown_GrPixelConfig; |
| 281 | } |
| 282 | |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 283 | GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info, const GrCaps& caps) { |
| 284 | return SkImageInfo2GrPixelConfig(info.colorType(), info.colorSpace(), caps); |
| 285 | } |
| 286 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 287 | bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 288 | SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config)); |
| 289 | if (kUnknown_SkColorType != ct) { |
| 290 | if (ctOut) { |
| 291 | *ctOut = ct; |
| 292 | } |
| 293 | return true; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 294 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 295 | return false; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 296 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 297 | |
brianosman | 27a3aa5 | 2016-09-23 07:11:34 -0700 | [diff] [blame] | 298 | GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* colorSpace) { |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 299 | if (!colorSpace) { |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 300 | return kRGBA_8888_GrPixelConfig; |
| 301 | } else if (colorSpace->gammaIsLinear()) { |
Mike Klein | ce4cf72 | 2018-05-10 11:29:15 -0400 | [diff] [blame] | 302 | // TODO |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 303 | return kRGBA_half_GrPixelConfig; |
| 304 | } else if (colorSpace->gammaCloseToSRGB()) { |
| 305 | return kSRGBA_8888_GrPixelConfig; |
| 306 | } else { |
| 307 | SkDEBUGFAIL("No renderable config exists for color space with strange gamma"); |
| 308 | return kUnknown_GrPixelConfig; |
| 309 | } |
| 310 | } |
| 311 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 312 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 313 | |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 314 | static inline bool blend_requires_shader(const SkBlendMode mode) { |
| 315 | return SkBlendMode::kDst != mode; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 316 | } |
| 317 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 318 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 319 | const GrColorSpaceInfo& colorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 320 | const SkPaint& skPaint, |
| 321 | const SkMatrix& viewM, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 322 | std::unique_ptr<GrFragmentProcessor>* shaderProcessor, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 323 | SkBlendMode* primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 324 | GrPaint* grPaint) { |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 325 | grPaint->setAllowSRGBInputs(colorSpaceInfo.isGammaCorrect()); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 326 | |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 327 | // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion. |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 328 | GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), colorSpaceInfo); |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 329 | |
Mike Reed | 3bc266b | 2018-01-20 22:24:41 +0000 | [diff] [blame] | 330 | const GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo); |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 331 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 332 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 333 | // of per-vertex colors. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 334 | std::unique_ptr<GrFragmentProcessor> shaderFP; |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 335 | if (!primColorMode || blend_requires_shader(*primColorMode)) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 336 | if (shaderProcessor) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 337 | shaderFP = std::move(*shaderProcessor); |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 338 | } else if (const auto* shader = as_SB(skPaint.getShader())) { |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 339 | shaderFP = shader->asFragmentProcessor(fpArgs); |
Yuqian Li | aad2ec6 | 2018-02-26 10:34:52 -0500 | [diff] [blame] | 340 | if (!shaderFP) { |
| 341 | return false; |
| 342 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 347 | // a known constant value. In that case we can simply apply a color filter during this |
| 348 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 349 | bool applyColorFilterToPaintColor = false; |
| 350 | if (shaderFP) { |
| 351 | if (primColorMode) { |
| 352 | // There is a blend between the primitive color and the shader color. The shader sees |
| 353 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 354 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 355 | |
| 356 | // The geometry processor will insert the primitive color to start the color chain, so |
| 357 | // the GrPaint color will be ignored. |
| 358 | |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 359 | GrColor4f shaderInput = origColor.opaque(); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 360 | shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput); |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 361 | shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP), |
| 362 | *primColorMode); |
| 363 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 364 | // The above may return null if compose results in a pass through of the prim color. |
| 365 | if (shaderFP) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 366 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 367 | } |
| 368 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 369 | // We can ignore origColor here - alpha is unchanged by gamma |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 370 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 371 | if (GrColor_WHITE != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 372 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 373 | // 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] | 374 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 375 | GrColor4f::FromGrColor(paintAlpha), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 376 | GrConstColorProcessor::InputMode::kModulateRGBA)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 377 | } |
| 378 | } else { |
| 379 | // The shader's FP sees the paint unpremul color |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 380 | grPaint->setColor4f(origColor); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 381 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 382 | } |
| 383 | } else { |
| 384 | if (primColorMode) { |
| 385 | // There is a blend between the primitive color and the paint color. The blend considers |
| 386 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 387 | auto processor = GrConstColorProcessor::Make(origColor.opaque(), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 388 | GrConstColorProcessor::InputMode::kIgnore); |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 389 | processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor), |
| 390 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 391 | if (processor) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 392 | grPaint->addColorFragmentProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 393 | } |
| 394 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 395 | grPaint->setColor4f(origColor.opaque()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 396 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 397 | // We can ignore origColor here - alpha is unchanged by gamma |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 398 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 399 | if (GrColor_WHITE != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 400 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 401 | // 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] | 402 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 403 | GrColor4f::FromGrColor(paintAlpha), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 404 | GrConstColorProcessor::InputMode::kModulateRGBA)); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 405 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 406 | } else { |
| 407 | // No shader, no primitive color. |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 408 | grPaint->setColor4f(origColor.premul()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 409 | applyColorFilterToPaintColor = true; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 414 | if (colorFilter) { |
| 415 | if (applyColorFilterToPaintColor) { |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 416 | // If we're in legacy mode, we *must* avoid using the 4f version of the color filter, |
| 417 | // because that will combine with the linearized version of the stored color. |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 418 | if (colorSpaceInfo.isGammaCorrect()) { |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 419 | grPaint->setColor4f(GrColor4f::FromSkColor4f( |
| 420 | colorFilter->filterColor4f(origColor.toSkColor4f())).premul()); |
| 421 | } else { |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 422 | grPaint->setColor4f(SkColorToPremulGrColor4fLegacy( |
| 423 | colorFilter->filterColor(skPaint.getColor()))); |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 424 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 425 | } else { |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 426 | auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 427 | if (cfFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 428 | grPaint->addColorFragmentProcessor(std::move(cfFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 429 | } else { |
| 430 | return false; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 435 | SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter()); |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 436 | if (maskFilter) { |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 437 | if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) { |
| 438 | grPaint->addCoverageFragmentProcessor(std::move(mfFP)); |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 442 | // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on |
| 443 | // the GrPaint to also be null (also kSrcOver). |
| 444 | SkASSERT(!grPaint->getXPFactory()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 445 | if (!skPaint.isSrcOver()) { |
| 446 | grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode())); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 447 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 448 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 449 | #ifndef SK_IGNORE_GPU_DITHER |
Florin Malita | d4a70ee | 2017-06-19 10:21:43 -0400 | [diff] [blame] | 450 | // Conservative default, in case GrPixelConfigToColorType() fails. |
| 451 | SkColorType ct = SkColorType::kRGB_565_SkColorType; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 452 | GrPixelConfigToColorType(colorSpaceInfo.config(), &ct); |
| 453 | if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0 && |
| 454 | !colorSpaceInfo.isGammaCorrect()) { |
| 455 | auto ditherFP = GrDitherEffect::Make(colorSpaceInfo.config()); |
Brian Salomon | 0c15ae8 | 2017-07-19 15:39:56 +0000 | [diff] [blame] | 456 | if (ditherFP) { |
| 457 | grPaint->addColorFragmentProcessor(std::move(ditherFP)); |
| 458 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 459 | } |
| 460 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 461 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 464 | bool SkPaintToGrPaint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo, |
| 465 | const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) { |
| 466 | return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr, |
| 467 | grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 468 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 469 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 470 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 471 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 472 | const GrColorSpaceInfo& colorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 473 | const SkPaint& skPaint, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 474 | std::unique_ptr<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 475 | GrPaint* grPaint) { |
| 476 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 477 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 478 | } |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 479 | return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP, |
| 480 | nullptr, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 481 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 482 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 483 | /** Ignores the SkShader (if any) on skPaint. */ |
| 484 | bool SkPaintToGrPaintNoShader(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 485 | const GrColorSpaceInfo& colorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 486 | const SkPaint& skPaint, |
| 487 | GrPaint* grPaint) { |
| 488 | // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced. |
Robert Phillips | d3b37a1 | 2018-03-27 09:53:35 -0400 | [diff] [blame] | 489 | std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr); |
| 490 | return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 491 | nullptr, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** 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] | 495 | be setup as a vertex attribute using the specified SkBlendMode. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 496 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 497 | const GrColorSpaceInfo& colorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 498 | const SkPaint& skPaint, |
| 499 | const SkMatrix& viewM, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 500 | SkBlendMode primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 501 | GrPaint* grPaint) { |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 502 | return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode, |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 503 | grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 504 | } |
| 505 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 506 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 507 | const GrColorSpaceInfo& colorSpaceInfo, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 508 | const SkPaint& paint, |
| 509 | const SkMatrix& viewM, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 510 | std::unique_ptr<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 511 | bool textureIsAlphaOnly, |
| 512 | GrPaint* grPaint) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 513 | std::unique_ptr<GrFragmentProcessor> shaderFP; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 514 | if (textureIsAlphaOnly) { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 515 | if (const auto* shader = as_SB(paint.getShader())) { |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 516 | shaderFP = shader->asFragmentProcessor(GrFPArgs( |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 517 | context, &viewM, paint.getFilterQuality(), &colorSpaceInfo)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 518 | if (!shaderFP) { |
| 519 | return false; |
| 520 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 521 | std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) }; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 522 | shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 523 | } else { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 524 | shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 525 | } |
| 526 | } else { |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 527 | shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 530 | return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP), |
| 531 | grPaint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 532 | } |
| 533 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 534 | |
| 535 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 536 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 537 | GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 538 | const SkMatrix& viewM, |
| 539 | const SkMatrix& localM, |
Brian Osman | db78cba | 2018-02-15 10:09:48 -0500 | [diff] [blame] | 540 | bool sharpenMipmappedTextures, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 541 | bool* doBicubic) { |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 542 | *doBicubic = false; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 543 | GrSamplerState::Filter textureFilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 544 | switch (paintFilterQuality) { |
| 545 | case kNone_SkFilterQuality: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 546 | textureFilterMode = GrSamplerState::Filter::kNearest; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 547 | break; |
| 548 | case kLow_SkFilterQuality: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 549 | textureFilterMode = GrSamplerState::Filter::kBilerp; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 550 | break; |
| 551 | case kMedium_SkFilterQuality: { |
| 552 | SkMatrix matrix; |
| 553 | matrix.setConcat(viewM, localM); |
Brian Osman | db78cba | 2018-02-15 10:09:48 -0500 | [diff] [blame] | 554 | // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the |
| 555 | // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5? |
| 556 | // |
| 557 | // Want: 0 = log2(1/s) - 0.5 |
| 558 | // 0.5 = log2(1/s) |
| 559 | // 2^0.5 = 1/s |
| 560 | // 1/2^0.5 = s |
| 561 | // 2^0.5/2 = s |
| 562 | SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1; |
| 563 | if (matrix.getMinScale() < mipScale) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 564 | textureFilterMode = GrSamplerState::Filter::kMipMap; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 565 | } else { |
| 566 | // Don't trigger MIP level generation unnecessarily. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 567 | textureFilterMode = GrSamplerState::Filter::kBilerp; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 568 | } |
| 569 | break; |
| 570 | } |
| 571 | case kHigh_SkFilterQuality: { |
| 572 | SkMatrix matrix; |
| 573 | matrix.setConcat(viewM, localM); |
| 574 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 575 | break; |
| 576 | } |
| 577 | default: |
Mike Klein | e54c75f | 2016-10-13 14:18:09 -0400 | [diff] [blame] | 578 | // Should be unreachable. If not, fall back to mipmaps. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 579 | textureFilterMode = GrSamplerState::Filter::kMipMap; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 580 | break; |
| 581 | |
| 582 | } |
| 583 | return textureFilterMode; |
| 584 | } |