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