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