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