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