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 | |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 8 | #include "src/gpu/SkGr.h" |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColorFilter.h" |
| 12 | #include "include/core/SkData.h" |
| 13 | #include "include/core/SkPixelRef.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 14 | #include "include/effects/SkRuntimeEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/gpu/GrContext.h" |
| 16 | #include "include/gpu/GrTypes.h" |
| 17 | #include "include/private/GrRecordingContext.h" |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 18 | #include "include/private/SkIDChangeListener.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/private/SkImageInfoPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/private/SkTemplates.h" |
| 21 | #include "src/core/SkAutoMalloc.h" |
| 22 | #include "src/core/SkBlendModePriv.h" |
Mike Reed | bd84330 | 2019-09-27 21:05:24 -0400 | [diff] [blame] | 23 | #include "src/core/SkColorSpacePriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/core/SkImagePriv.h" |
| 25 | #include "src/core/SkMaskFilterBase.h" |
Ben Wagner | 21bca28 | 2019-05-15 10:15:52 -0400 | [diff] [blame] | 26 | #include "src/core/SkMessageBus.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/core/SkMipMap.h" |
| 28 | #include "src/core/SkPaintPriv.h" |
| 29 | #include "src/core/SkResourceCache.h" |
| 30 | #include "src/core/SkTraceEvent.h" |
| 31 | #include "src/gpu/GrBitmapTextureMaker.h" |
| 32 | #include "src/gpu/GrCaps.h" |
| 33 | #include "src/gpu/GrColorSpaceXform.h" |
| 34 | #include "src/gpu/GrContextPriv.h" |
| 35 | #include "src/gpu/GrGpuResourcePriv.h" |
| 36 | #include "src/gpu/GrPaint.h" |
| 37 | #include "src/gpu/GrProxyProvider.h" |
| 38 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 39 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | #include "src/gpu/GrXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 41 | #include "src/gpu/effects/GrBicubicEffect.h" |
| 42 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
| 43 | #include "src/gpu/effects/GrSkSLFP.h" |
| 44 | #include "src/gpu/effects/GrXfermodeFragmentProcessor.h" |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 45 | #include "src/gpu/effects/generated/GrClampFragmentProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
| 47 | #include "src/image/SkImage_Base.h" |
| 48 | #include "src/shaders/SkShaderBase.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 49 | |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 50 | GR_FP_SRC_STRING SKSL_DITHER_SRC = R"( |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 51 | // This controls the range of values added to color channels and is based on the destination color |
| 52 | // type; as such it doesn't really affect our program cache to have a variant per-range. |
| 53 | in half range; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 54 | |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 55 | void main(inout half4 color) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 56 | half value; |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 57 | @if (sk_Caps.integerSupport) |
| 58 | { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 59 | // This ordered-dither code is lifted from the cpu backend. |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 60 | uint x = uint(sk_FragCoord.x); |
| 61 | uint y = uint(sk_FragCoord.y) ^ x; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 62 | uint m = (y & 1) << 5 | (x & 1) << 4 | |
| 63 | (y & 2) << 2 | (x & 2) << 1 | |
| 64 | (y & 4) >> 1 | (x & 4) >> 2; |
| 65 | value = half(m) * 1.0 / 64.0 - 63.0 / 128.0; |
| 66 | } else { |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 67 | // Simulate the integer effect used above using step/mod/abs. For speed, simulates a 4x4 |
| 68 | // dither pattern rather than an 8x8 one. Since it's 4x4, this is effectively computing: |
| 69 | // uint m = (y & 1) << 3 | (x & 1) << 2 | |
| 70 | // (y & 2) << 0 | (x & 2) >> 1; |
| 71 | // where 'y' has already been XOR'ed with 'x' as in the integer-supported case. |
| 72 | |
| 73 | // To get the low bit of p.x and p.y, we compute mod 2.0; for the high bit, we mod 4.0 |
| 74 | half4 bits = mod(half4(sk_FragCoord.yxyx), half4(2.0, 2.0, 4.0, 4.0)); |
| 75 | // Use step to convert the 0-3 value in bits.zw into a 0|1 value. bits.xy is already 0|1. |
| 76 | bits.zw = step(2.0, bits.zw); |
| 77 | // bits was constructed such that the p.x bits were already in the right place for |
| 78 | // interleaving (in bits.yw). We just need to update the other bits from p.y to (p.x ^ p.y). |
| 79 | // These are in bits.xz. Since the values are 0|1, we can simulate ^ as abs(y - x). |
| 80 | bits.xz = abs(bits.xz - bits.yw); |
| 81 | |
| 82 | // Manual binary sum, divide by N^2, and offset |
| 83 | value = dot(bits, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 84 | } |
| 85 | // For each color channel, add the random offset to the channel value and then clamp |
| 86 | // between 0 and alpha to keep the color premultiplied. |
| 87 | color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a); |
| 88 | } |
| 89 | )"; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 90 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 91 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) { |
| 92 | SkASSERT(key); |
| 93 | SkASSERT(imageID); |
| 94 | SkASSERT(!imageBounds.isEmpty()); |
| 95 | static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain(); |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 96 | GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image"); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 97 | builder[0] = imageID; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 98 | builder[1] = imageBounds.fLeft; |
| 99 | builder[2] = imageBounds.fTop; |
| 100 | builder[3] = imageBounds.fRight; |
| 101 | builder[4] = imageBounds.fBottom; |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 102 | } |
| 103 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 104 | //////////////////////////////////////////////////////////////////////////////// |
| 105 | |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 106 | sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key, |
| 107 | uint32_t contextID) { |
| 108 | class Listener : public SkIDChangeListener { |
| 109 | public: |
| 110 | Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {} |
| 111 | |
| 112 | void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 113 | |
| 114 | private: |
| 115 | GrUniqueKeyInvalidatedMessage fMsg; |
| 116 | }; |
| 117 | |
| 118 | auto listener = sk_make_sp<Listener>(*key, contextID); |
| 119 | |
| 120 | // We stick a SkData on the key that calls invalidateListener in its destructor. |
| 121 | auto invalidateListener = [](const void* ptr, void* /*context*/) { |
| 122 | auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr); |
| 123 | (*listener)->markShouldDeregister(); |
| 124 | delete listener; |
| 125 | }; |
| 126 | auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener), |
| 127 | sizeof(sk_sp<Listener>), |
| 128 | invalidateListener, |
| 129 | nullptr); |
| 130 | SkASSERT(!key->getCustomData()); |
| 131 | key->setCustomData(std::move(data)); |
| 132 | return std::move(listener); |
| 133 | } |
| 134 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 135 | sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx, |
| 136 | GrSurfaceProxy* baseProxy, |
| 137 | GrSurfaceOrigin origin, |
| 138 | SkBudgeted budgeted) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 139 | SkASSERT(baseProxy); |
| 140 | |
Greg Daniel | 6980c4e | 2019-07-30 16:18:18 -0400 | [diff] [blame] | 141 | if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 142 | return {}; |
Greg Daniel | bb76ace | 2017-09-29 15:58:22 -0400 | [diff] [blame] | 143 | } |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 144 | auto copy = GrSurfaceProxy::Copy(ctx, baseProxy, origin, GrMipMapped::kYes, |
| 145 | SkBackingFit::kExact, budgeted); |
| 146 | if (!copy) { |
| 147 | return {}; |
| 148 | } |
| 149 | SkASSERT(copy->asTextureProxy()); |
| 150 | return copy; |
| 151 | } |
| 152 | |
| 153 | GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext* context, |
| 154 | GrSurfaceProxyView src, |
| 155 | SkBudgeted budgeted) { |
| 156 | auto origin = src.origin(); |
| 157 | auto swizzle = src.swizzle(); |
| 158 | auto* proxy = src.proxy(); |
| 159 | return {GrCopyBaseMipMapToTextureProxy(context, proxy, origin, budgeted), origin, swizzle}; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Greg Daniel | cc21d0c | 2020-02-05 16:58:40 -0500 | [diff] [blame] | 162 | GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap, |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 163 | GrMipMapped mipMapped) { |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 164 | GrBitmapTextureMaker maker(ctx, bitmap, GrImageTexGenPolicy::kDraw); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 165 | return maker.view(mipMapped); |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 168 | GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context, |
| 169 | const SkBitmap& bitmap) { |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 170 | if (!bitmap.peekPixels(nullptr)) { |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 171 | return {}; |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 174 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kDraw); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 175 | return maker.view(GrMipMapped::kNo); |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 176 | } |
| 177 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 180 | SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) { |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 181 | SkColor4f color = SkColor4f::FromColor(c); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 182 | if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) { |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 183 | color = xform->apply(color); |
| 184 | } |
| 185 | return color.premul(); |
| 186 | } |
| 187 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 188 | SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) { |
| 189 | if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) { |
Brian Osman | 5c8a6b3 | 2018-11-19 11:56:57 -0500 | [diff] [blame] | 190 | color = xform->apply(color); |
| 191 | } |
Brian Osman | 5c8a6b3 | 2018-11-19 11:56:57 -0500 | [diff] [blame] | 192 | return color; |
| 193 | } |
| 194 | |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 197 | static inline bool blend_requires_shader(const SkBlendMode mode) { |
| 198 | return SkBlendMode::kDst != mode; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 201 | #ifndef SK_IGNORE_GPU_DITHER |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 202 | static inline float dither_range_for_config(GrColorType dstColorType) { |
| 203 | // We use 1 / (2^bitdepth-1) as the range since each channel can hold 2^bitdepth values |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 204 | switch (dstColorType) { |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 205 | // 4 bit |
| 206 | case GrColorType::kABGR_4444: |
| 207 | return 1 / 15.f; |
| 208 | // 6 bit |
| 209 | case GrColorType::kBGR_565: |
| 210 | return 1 / 63.f; |
| 211 | // 8 bit |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 212 | case GrColorType::kUnknown: |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 213 | case GrColorType::kAlpha_8: |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 214 | case GrColorType::kAlpha_8xxx: |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 215 | case GrColorType::kGray_8: |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 216 | case GrColorType::kGray_8xxx: |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 217 | case GrColorType::kR_8: |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 218 | case GrColorType::kRG_88: |
| 219 | case GrColorType::kRGB_888: |
| 220 | case GrColorType::kRGB_888x: |
| 221 | case GrColorType::kRGBA_8888: |
| 222 | case GrColorType::kRGBA_8888_SRGB: |
| 223 | case GrColorType::kBGRA_8888: |
| 224 | return 1 / 255.f; |
| 225 | // 10 bit |
| 226 | case GrColorType::kRGBA_1010102: |
| 227 | case GrColorType::kBGRA_1010102: |
| 228 | return 1 / 1023.f; |
| 229 | // 16 bit |
| 230 | case GrColorType::kAlpha_16: |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 231 | case GrColorType::kR_16: |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 232 | case GrColorType::kRG_1616: |
| 233 | case GrColorType::kRGBA_16161616: |
| 234 | return 1 / 32767.f; |
| 235 | // Half |
| 236 | case GrColorType::kAlpha_F16: |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 237 | case GrColorType::kGray_F16: |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 238 | case GrColorType::kR_F16: |
| 239 | case GrColorType::kRG_F16: |
| 240 | case GrColorType::kRGBA_F16: |
| 241 | case GrColorType::kRGBA_F16_Clamped: |
| 242 | // Float |
| 243 | case GrColorType::kAlpha_F32xxx: |
| 244 | case GrColorType::kRGBA_F32: |
| 245 | return 0.f; // no dithering |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 246 | } |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 247 | SkUNREACHABLE; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 248 | } |
| 249 | #endif |
| 250 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 251 | static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 252 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 253 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 254 | const SkMatrixProvider& matrixProvider, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 255 | std::unique_ptr<GrFragmentProcessor>* shaderProcessor, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 256 | SkBlendMode* primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 257 | GrPaint* grPaint) { |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 258 | // Convert SkPaint color to 4f format in the destination color space |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 259 | SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo); |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 260 | |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 261 | GrFPArgs fpArgs(context, matrixProvider, skPaint.getFilterQuality(), &dstColorInfo); |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 262 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 263 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 264 | // of per-vertex colors. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 265 | std::unique_ptr<GrFragmentProcessor> shaderFP; |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 266 | if (!primColorMode || blend_requires_shader(*primColorMode)) { |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 267 | fpArgs.fInputColorIsOpaque = origColor.isOpaque(); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 268 | if (shaderProcessor) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 269 | shaderFP = std::move(*shaderProcessor); |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 270 | } else if (const auto* shader = as_SB(skPaint.getShader())) { |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 271 | shaderFP = shader->asFragmentProcessor(fpArgs); |
Yuqian Li | aad2ec6 | 2018-02-26 10:34:52 -0500 | [diff] [blame] | 272 | if (!shaderFP) { |
| 273 | return false; |
| 274 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 279 | // a known constant value. In that case we can simply apply a color filter during this |
| 280 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 281 | bool applyColorFilterToPaintColor = false; |
| 282 | if (shaderFP) { |
| 283 | if (primColorMode) { |
| 284 | // There is a blend between the primitive color and the shader color. The shader sees |
| 285 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 286 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 287 | |
| 288 | // The geometry processor will insert the primitive color to start the color chain, so |
| 289 | // the GrPaint color will be ignored. |
| 290 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 291 | SkPMColor4f shaderInput = origColor.makeOpaque().premul(); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 292 | shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput); |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 293 | shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP), |
| 294 | *primColorMode); |
| 295 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 296 | // The above may return null if compose results in a pass through of the prim color. |
| 297 | if (shaderFP) { |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 298 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 299 | } |
| 300 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 301 | // We can ignore origColor here - alpha is unchanged by gamma |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 302 | float paintAlpha = skPaint.getColor4f().fA; |
| 303 | if (1.0f != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 304 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 305 | // 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] | 306 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 307 | { paintAlpha, paintAlpha, paintAlpha, paintAlpha }, |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 308 | GrConstColorProcessor::InputMode::kModulateRGBA)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 309 | } |
| 310 | } else { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 311 | // The shader's FP sees the paint *unpremul* color |
| 312 | SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA }; |
| 313 | grPaint->setColor4f(origColorAsPM); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 314 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 315 | } |
| 316 | } else { |
| 317 | if (primColorMode) { |
| 318 | // There is a blend between the primitive color and the paint color. The blend considers |
| 319 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 320 | SkPMColor4f opaqueColor = origColor.makeOpaque().premul(); |
| 321 | auto processor = GrConstColorProcessor::Make(opaqueColor, |
| 322 | GrConstColorProcessor::InputMode::kIgnore); |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 323 | processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor), |
| 324 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 325 | if (processor) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 326 | grPaint->addColorFragmentProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 329 | grPaint->setColor4f(opaqueColor); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 330 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 331 | // We can ignore origColor here - alpha is unchanged by gamma |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 332 | float paintAlpha = skPaint.getColor4f().fA; |
| 333 | if (1.0f != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 334 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 335 | // 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] | 336 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 337 | { paintAlpha, paintAlpha, paintAlpha, paintAlpha }, |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 338 | GrConstColorProcessor::InputMode::kModulateRGBA)); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 339 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 340 | } else { |
| 341 | // No shader, no primitive color. |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 342 | grPaint->setColor4f(origColor.premul()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 343 | applyColorFilterToPaintColor = true; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 348 | if (colorFilter) { |
| 349 | if (applyColorFilterToPaintColor) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 350 | SkColorSpace* dstCS = dstColorInfo.colorSpace(); |
Mike Reed | bd84330 | 2019-09-27 21:05:24 -0400 | [diff] [blame] | 351 | grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 352 | } else { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 353 | auto cfFP = colorFilter->asFragmentProcessor(context, dstColorInfo); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 354 | if (cfFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 355 | grPaint->addColorFragmentProcessor(std::move(cfFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 356 | } else { |
| 357 | return false; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 362 | SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter()); |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 363 | if (maskFilter) { |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 364 | // We may have set this before passing to the SkShader. |
| 365 | fpArgs.fInputColorIsOpaque = false; |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 366 | if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) { |
| 367 | grPaint->addCoverageFragmentProcessor(std::move(mfFP)); |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 371 | // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on |
| 372 | // the GrPaint to also be null (also kSrcOver). |
| 373 | SkASSERT(!grPaint->getXPFactory()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 374 | if (!skPaint.isSrcOver()) { |
| 375 | grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode())); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 376 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 377 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 378 | #ifndef SK_IGNORE_GPU_DITHER |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 379 | GrColorType ct = dstColorInfo.colorType(); |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 380 | if (SkPaintPriv::ShouldDither(skPaint, GrColorTypeToSkColorType(ct)) && |
| 381 | grPaint->numColorFragmentProcessors() > 0) { |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 382 | float ditherRange = dither_range_for_config(ct); |
| 383 | if (ditherRange > 0.f) { |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 384 | static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_DITHER_SRC))); |
| 385 | auto ditherFP = GrSkSLFP::Make(context, effect, "Dither", |
Brian Osman | 9bfd595 | 2020-02-05 10:51:44 -0500 | [diff] [blame] | 386 | SkData::MakeWithCopy(&ditherRange, sizeof(ditherRange))); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 387 | if (ditherFP) { |
Michael Ludwig | 87f5755 | 2020-05-27 14:32:38 -0400 | [diff] [blame^] | 388 | // The dither shader doesn't actually use input coordinates, but if we don't set |
| 389 | // this flag, the generated shader includes an extra local coord varying. |
| 390 | ditherFP->setSampledWithExplicitCoords(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 391 | grPaint->addColorFragmentProcessor(std::move(ditherFP)); |
| 392 | } |
Brian Salomon | 0c15ae8 | 2017-07-19 15:39:56 +0000 | [diff] [blame] | 393 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 394 | } |
| 395 | #endif |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 396 | if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) { |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 397 | if (grPaint->numColorFragmentProcessors()) { |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 398 | grPaint->addColorFragmentProcessor(GrClampFragmentProcessor::Make(false)); |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 399 | } else { |
| 400 | auto color = grPaint->getColor4f(); |
| 401 | grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f), |
| 402 | SkTPin(color.fG, 0.f, 1.f), |
| 403 | SkTPin(color.fB, 0.f, 1.f), |
| 404 | SkTPin(color.fA, 0.f, 1.f)}); |
| 405 | } |
| 406 | } |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 407 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 410 | bool SkPaintToGrPaint(GrRecordingContext* context, |
| 411 | const GrColorInfo& dstColorInfo, |
| 412 | const SkPaint& skPaint, |
| 413 | const SkMatrixProvider& matrixProvider, |
| 414 | GrPaint* grPaint) { |
| 415 | return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, nullptr, nullptr, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 416 | grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 417 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 418 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 419 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 420 | bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 421 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 422 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 423 | const SkMatrixProvider& matrixProvider, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 424 | std::unique_ptr<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 425 | GrPaint* grPaint) { |
| 426 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 427 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 428 | } |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 429 | return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &shaderFP, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 430 | nullptr, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 431 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 432 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 433 | /** Ignores the SkShader (if any) on skPaint. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 434 | bool SkPaintToGrPaintNoShader(GrRecordingContext* context, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 435 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 436 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 437 | const SkMatrixProvider& matrixProvider, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 438 | GrPaint* grPaint) { |
| 439 | // 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] | 440 | std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr); |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 441 | return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, &nullShaderFP, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 442 | nullptr, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** 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] | 446 | be setup as a vertex attribute using the specified SkBlendMode. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 447 | bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 448 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 449 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 450 | const SkMatrixProvider& matrixProvider, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 451 | SkBlendMode primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 452 | GrPaint* grPaint) { |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 453 | return skpaint_to_grpaint_impl(context, dstColorInfo, skPaint, matrixProvider, nullptr, |
| 454 | &primColorMode, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 457 | bool SkPaintToGrPaintWithTexture(GrRecordingContext* context, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 458 | const GrColorInfo& dstColorInfo, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 459 | const SkPaint& paint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 460 | const SkMatrixProvider& matrixProvider, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 461 | std::unique_ptr<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 462 | bool textureIsAlphaOnly, |
| 463 | GrPaint* grPaint) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 464 | std::unique_ptr<GrFragmentProcessor> shaderFP; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 465 | if (textureIsAlphaOnly) { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 466 | if (const auto* shader = as_SB(paint.getShader())) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 467 | shaderFP = shader->asFragmentProcessor( |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 468 | GrFPArgs(context, matrixProvider, paint.getFilterQuality(), &dstColorInfo)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 469 | if (!shaderFP) { |
| 470 | return false; |
| 471 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 472 | std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) }; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 473 | shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 474 | } else { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 475 | shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 476 | } |
| 477 | } else { |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 478 | if (paint.getColor4f().isOpaque()) { |
| 479 | shaderFP = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false); |
| 480 | } else { |
| 481 | shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp)); |
| 482 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 485 | return SkPaintToGrPaintReplaceShader(context, dstColorInfo, paint, matrixProvider, |
| 486 | std::move(shaderFP), grPaint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 487 | } |
| 488 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 489 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 490 | |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 491 | GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight, |
| 492 | SkFilterQuality paintFilterQuality, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 493 | const SkMatrix& viewM, |
| 494 | const SkMatrix& localM, |
Brian Osman | db78cba | 2018-02-15 10:09:48 -0500 | [diff] [blame] | 495 | bool sharpenMipmappedTextures, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 496 | bool* doBicubic) { |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 497 | *doBicubic = false; |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 498 | if (imageWidth <= 1 && imageHeight <= 1) { |
| 499 | return GrSamplerState::Filter::kNearest; |
| 500 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 501 | switch (paintFilterQuality) { |
| 502 | case kNone_SkFilterQuality: |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 503 | return GrSamplerState::Filter::kNearest; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 504 | case kLow_SkFilterQuality: |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 505 | return GrSamplerState::Filter::kBilerp; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 506 | case kMedium_SkFilterQuality: { |
| 507 | SkMatrix matrix; |
| 508 | matrix.setConcat(viewM, localM); |
Brian Osman | db78cba | 2018-02-15 10:09:48 -0500 | [diff] [blame] | 509 | // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the |
| 510 | // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5? |
| 511 | // |
| 512 | // Want: 0 = log2(1/s) - 0.5 |
| 513 | // 0.5 = log2(1/s) |
| 514 | // 2^0.5 = 1/s |
| 515 | // 1/2^0.5 = s |
| 516 | // 2^0.5/2 = s |
| 517 | SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1; |
| 518 | if (matrix.getMinScale() < mipScale) { |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 519 | return GrSamplerState::Filter::kMipMap; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 520 | } else { |
| 521 | // Don't trigger MIP level generation unnecessarily. |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 522 | return GrSamplerState::Filter::kBilerp; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 523 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 524 | } |
| 525 | case kHigh_SkFilterQuality: { |
| 526 | SkMatrix matrix; |
| 527 | matrix.setConcat(viewM, localM); |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 528 | GrSamplerState::Filter textureFilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 529 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 530 | return textureFilterMode; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 531 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 532 | } |
Chris Dalton | 309c6c0 | 2019-08-13 10:32:47 -0600 | [diff] [blame] | 533 | SkUNREACHABLE; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 534 | } |