blob: b79f01fa9416b7bf3c76d26b51f3dce3742a5258 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "SkGr.h"
Brian Osman3b66ab62016-11-28 09:26:31 -05009#include "GrBitmapTextureMaker.h"
bsalomon76228632015-05-29 08:02:10 -070010#include "GrCaps.h"
Brian Osman1cb41712017-10-19 12:54:52 -040011#include "GrColorSpaceXform.h"
bsalomonf276ac52015-10-09 13:36:42 -070012#include "GrContext.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040013#include "GrContextPriv.h"
bsalomon045802d2015-10-20 07:58:01 -070014#include "GrGpuResourcePriv.h"
Brian Salomonf3569f02017-10-24 12:52:33 -040015#include "GrPaint.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrProxyProvider.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050017#include "GrTextureProxy.h"
cblume55f2d2d2016-02-26 13:20:48 -080018#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080019#include "GrXferProcessor.h"
Hal Canary95e3c052017-01-11 12:44:43 -050020#include "SkAutoMalloc.h"
reed374772b2016-10-05 17:33:02 -070021#include "SkBlendModePriv.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "SkCanvas.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000023#include "SkColorFilter.h"
krajcevski9c0e6292014-06-02 07:38:14 -070024#include "SkData.h"
Robert Phillips7a926392018-02-01 15:49:54 -050025#include "SkImage_Base.h"
Brian Osman4075ec82017-01-17 16:41:03 +000026#include "SkImageInfoPriv.h"
Greg Daniel7e1912a2018-02-08 09:15:33 -050027#include "SkImagePriv.h"
Mike Reed80747ef2018-01-23 15:29:32 -050028#include "SkMaskFilterBase.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000029#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080030#include "SkMipMap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000031#include "SkPM4fPriv.h"
Florin Malitad4a70ee2017-06-19 10:21:43 -040032#include "SkPaintPriv.h"
Hal Canary95e3c052017-01-11 12:44:43 -050033#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080034#include "SkResourceCache.h"
Florin Malita4aed1382017-05-25 10:38:07 -040035#include "SkShaderBase.h"
cblume55f2d2d2016-02-26 13:20:48 -080036#include "SkTemplates.h"
Derek Sollenberger559f5342017-08-17 12:34:54 -040037#include "SkTraceEvent.h"
joshualitt9bc39542015-08-12 12:57:54 -070038#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070039#include "effects/GrConstColorProcessor.h"
egdaniel378092f2014-12-03 10:40:13 -080040#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070041#include "effects/GrXfermodeFragmentProcessor.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040042#include "effects/GrSkSLFP.h"
43
Ethan Nicholas78aceb22018-08-31 16:13:58 -040044#if SK_SUPPORT_GPU
45GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Ethan Nicholas00543112018-07-31 09:44:36 -040046// This controls the range of values added to color channels
47layout(key) in int rangeType;
48
49void main(int x, int y, inout half4 color) {
50 half value;
51 half range;
52 @switch (rangeType) {
53 case 0:
54 range = 1.0 / 255.0;
55 break;
56 case 1:
57 range = 1.0 / 63.0;
58 break;
59 default:
60 // Experimentally this looks better than the expected value of 1/15.
61 range = 1.0 / 15.0;
62 break;
63 }
64 @if (sk_Caps.integerSupport) {
65 // This ordered-dither code is lifted from the cpu backend.
66 uint x = uint(x);
67 uint y = uint(y);
68 uint m = (y & 1) << 5 | (x & 1) << 4 |
69 (y & 2) << 2 | (x & 2) << 1 |
70 (y & 4) >> 1 | (x & 4) >> 2;
71 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
72 } else {
73 // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
74 // dither pattern rather than an 8x8 one.
75 half4 modValues = mod(float4(x, y, x, y), half4(2.0, 2.0, 4.0, 4.0));
76 half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
77 value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
78 }
79 // For each color channel, add the random offset to the channel value and then clamp
80 // between 0 and alpha to keep the color premultiplied.
81 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
82}
83)";
Ethan Nicholas78aceb22018-08-31 16:13:58 -040084#endif
krajcevski9c0e6292014-06-02 07:38:14 -070085
Brian Osman2b23c4b2018-06-01 12:25:08 -040086GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
bsalomon466c2c42015-10-16 12:01:18 -070087 GrSurfaceDesc desc;
88 desc.fFlags = kNone_GrSurfaceFlags;
89 desc.fWidth = info.width();
90 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040091 desc.fConfig = SkImageInfo2GrPixelConfig(info);
Brian Salomonbdecacf2018-02-02 20:32:49 -050092 desc.fSampleCnt = 1;
bsalomon466c2c42015-10-16 12:01:18 -070093 return desc;
94}
95
bsalomon045802d2015-10-20 07:58:01 -070096void 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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400101 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -0700102 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -0700103 builder[1] = imageBounds.fLeft;
104 builder[2] = imageBounds.fTop;
105 builder[3] = imageBounds.fRight;
106 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700107}
108
bsalomon045802d2015-10-20 07:58:01 -0700109//////////////////////////////////////////////////////////////////////////////
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider* proxyProvider,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400111 const SkBitmap& bitmap) {
Greg Daniel7619b642018-02-08 15:32:02 -0500112 if (!bitmap.peekPixels(nullptr)) {
Brian Osman4075ec82017-01-17 16:41:03 +0000113 return nullptr;
114 }
Greg Daniel7619b642018-02-08 15:32:02 -0500115
Brian Osman2b23c4b2018-06-01 12:25:08 -0400116 if (!SkImageInfoIsValid(bitmap.info())) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400117 return nullptr;
118 }
Greg Daniel7619b642018-02-08 15:32:02 -0500119
120 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
121 // even if it's mutable. In ddl, if the bitmap is mutable then we must make a copy since the
122 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400123 SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode
124 : kNever_SkCopyPixelsMode;
Greg Daniel7619b642018-02-08 15:32:02 -0500125 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
126
Brian Salomon58389b92018-03-07 13:01:25 -0500127 return proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
128 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillipsd3749482017-03-14 09:17:43 -0400129}
Brian Osman4075ec82017-01-17 16:41:03 +0000130
bsalomon045802d2015-10-20 07:58:01 -0700131////////////////////////////////////////////////////////////////////////////////
132
Brian Salomon238069b2018-07-11 15:58:57 -0400133void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
134 SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700135 class Invalidator : public SkPixelRef::GenIDChangeListener {
136 public:
Brian Salomon238069b2018-07-11 15:58:57 -0400137 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
138 : fMsg(key, contextUniqueID) {}
139
bsalomon89fe56b2015-10-29 10:49:28 -0700140 private:
141 GrUniqueKeyInvalidatedMessage fMsg;
142
143 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
144 };
145
Brian Salomon238069b2018-07-11 15:58:57 -0400146 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
bsalomon89fe56b2015-10-29 10:49:28 -0700147}
148
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500149sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext* ctx, GrTextureProxy* baseProxy) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400150 SkASSERT(baseProxy);
151
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400152 if (!ctx->contextPriv().caps()->isConfigCopyable(baseProxy->config())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400153 return nullptr;
154 }
155
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500156 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Greg Daniel55afd6d2017-09-29 09:32:44 -0400157 GrSurfaceDesc desc;
158 desc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400159 desc.fWidth = baseProxy->width();
160 desc.fHeight = baseProxy->height();
161 desc.fConfig = baseProxy->config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500162 desc.fSampleCnt = 1;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400163
Brian Salomon2a4f9832018-03-03 22:43:43 -0500164 sk_sp<GrTextureProxy> proxy =
165 proxyProvider->createMipMapProxy(desc, baseProxy->origin(), SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400166 if (!proxy) {
167 return nullptr;
168 }
169
170 // Copy the base layer to our proxy
Brian Salomon366093f2018-02-13 09:25:22 -0500171 sk_sp<GrSurfaceContext> sContext =
Brian Osman9363ac42018-06-01 16:10:53 -0400172 ctx->contextPriv().makeWrappedSurfaceContext(proxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400173 SkASSERT(sContext);
174 SkAssertResult(sContext->copy(baseProxy));
175
176 return proxy;
177}
178
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400179sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext* ctx,
180 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400181 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400182 SkScalar scaleAdjust[2]) {
183 // Caller doesn't care about the texture's color space (they can always get it from the bitmap)
Robert Phillips3798c862017-03-27 11:08:16 -0400184 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, nullptr,
185 nullptr, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400186}
187
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500188sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500189 const SkBitmap& bitmap,
190 SkBackingFit fit) {
191 if (!bitmap.peekPixels(nullptr)) {
192 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500193 }
194
Greg Daniel7e1912a2018-02-08 09:15:33 -0500195 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
196 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
197 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400198 SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode
199 : kNever_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500200 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500201
Greg Daniel7e1912a2018-02-08 09:15:33 -0500202 if (!image) {
203 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500204 }
205
Greg Daniel7e1912a2018-02-08 09:15:33 -0500206 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500207}
208
Robert Phillips7a926392018-02-01 15:49:54 -0500209static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
210 if (!image) {
211 result->reset(); // will be invalid
212 return;
213 }
214
215 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000216 if (!bm->isVolatile()) {
217 SkIPoint origin = bm->pixelRefOrigin();
218 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
219 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
220 }
Robert Phillips7a926392018-02-01 15:49:54 -0500221 return;
222 }
223
224 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
225}
226
227sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500228 sk_sp<SkImage> srcImage,
229 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500230 sk_sp<GrTextureProxy> proxy;
231 GrUniqueKey originalKey;
232
233 create_unique_key_for_image(srcImage.get(), &originalKey);
234
235 if (originalKey.isValid()) {
236 proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin);
237 }
238 if (!proxy) {
Greg Daniela4211122018-03-14 19:09:36 +0000239 proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1,
Brian Salomon58389b92018-03-07 13:01:25 -0500240 SkBudgeted::kYes, fit);
Robert Phillips7a926392018-02-01 15:49:54 -0500241 if (proxy && originalKey.isValid()) {
242 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400243 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
244 // When recording DDLs we do not want to install change listeners because doing
245 // so isn't threadsafe.
246 if (bm && !proxyProvider->recordingDDL()) {
Brian Salomon238069b2018-07-11 15:58:57 -0400247 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextUniqueID(),
248 bm->pixelRef());
Greg Daniela4211122018-03-14 19:09:36 +0000249 }
Robert Phillips7a926392018-02-01 15:49:54 -0500250 }
251 }
252
253 return proxy;
254}
255
rileya@google.com24f3ad12012-07-18 21:47:40 +0000256///////////////////////////////////////////////////////////////////////////////
257
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400258GrColor4f SkColorToPremulGrColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman08a50e02018-06-15 15:06:48 -0400259 // We want to premultiply after color space conversion, so this is easy:
Brian Osmanb0f4d0a2018-09-05 14:16:27 +0000260 return SkColor4fToUnpremulGrColor4f(SkColor4f::FromColor(c), colorSpaceInfo).premul();
Brian Osman72ae4312016-10-20 16:53:45 -0400261}
262
Brian Osmanb0f4d0a2018-09-05 14:16:27 +0000263GrColor4f SkColor4fToPremulGrColor4fLegacy(SkColor4f c) {
264 return GrColor4f::FromSkColor4f(c).premul();
Brian Osman72ae4312016-10-20 16:53:45 -0400265}
266
Brian Osmanb0f4d0a2018-09-05 14:16:27 +0000267GrColor4f SkColor4fToUnpremulGrColor4f(SkColor4f c, const GrColorSpaceInfo& colorSpaceInfo) {
268 GrColor4f color = GrColor4f::FromSkColor4f(c);
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400269 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
Brian Osman3567c142018-06-18 10:20:32 -0400270 color = xform->apply(color);
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400271 }
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400272 return color;
273}
274
275///////////////////////////////////////////////////////////////////////////////
276
Brian Osman2b23c4b2018-06-01 12:25:08 -0400277GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400278 switch (type) {
brianosmanc571c002016-03-17 13:01:26 -0700279 case kUnknown_SkColorType:
280 return kUnknown_GrPixelConfig;
281 case kAlpha_8_SkColorType:
282 return kAlpha_8_GrPixelConfig;
283 case kRGB_565_SkColorType:
284 return kRGB_565_GrPixelConfig;
285 case kARGB_4444_SkColorType:
286 return kRGBA_4444_GrPixelConfig;
287 case kRGBA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400288 return kRGBA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500289 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400290 return kRGB_888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700291 case kBGRA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400292 return kBGRA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500293 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500294 return kRGBA_1010102_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500295 case kRGB_101010x_SkColorType:
296 return kUnknown_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700297 case kGray_8_SkColorType:
Brian Osman986563b2017-01-10 14:20:02 -0500298 return kGray_8_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700299 case kRGBA_F16_SkColorType:
300 return kRGBA_half_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -0400301 case kRGBA_F32_SkColorType:
302 return kRGBA_float_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000303 }
304 SkASSERT(0); // shouldn't get here
305 return kUnknown_GrPixelConfig;
306}
307
Brian Osman2b23c4b2018-06-01 12:25:08 -0400308GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
309 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400310}
311
brianosman396fcdb2016-07-22 06:26:11 -0700312bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400313 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
314 if (kUnknown_SkColorType != ct) {
315 if (ctOut) {
316 *ctOut = ct;
317 }
318 return true;
reed@google.combf790232013-12-13 19:45:58 +0000319 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400320 return false;
reed@google.combf790232013-12-13 19:45:58 +0000321}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000322
bsalomonf1b7a1d2015-09-28 06:26:28 -0700323////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000324
Mike Reed185ba212017-04-28 12:31:05 -0400325static inline bool blend_requires_shader(const SkBlendMode mode) {
326 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700327}
328
Ethan Nicholas00543112018-07-31 09:44:36 -0400329#ifndef SK_IGNORE_GPU_DITHER
330static inline int32_t dither_range_type_for_config(GrPixelConfig dstConfig) {
331 switch (dstConfig) {
332 case kGray_8_GrPixelConfig:
333 case kGray_8_as_Lum_GrPixelConfig:
334 case kGray_8_as_Red_GrPixelConfig:
335 case kRGBA_8888_GrPixelConfig:
336 case kRGB_888_GrPixelConfig:
337 case kBGRA_8888_GrPixelConfig:
338 return 0;
339 case kRGB_565_GrPixelConfig:
340 return 1;
341 case kRGBA_4444_GrPixelConfig:
342 return 2;
343 case kUnknown_GrPixelConfig:
344 case kSRGBA_8888_GrPixelConfig:
345 case kSBGRA_8888_GrPixelConfig:
346 case kRGBA_1010102_GrPixelConfig:
347 case kAlpha_half_GrPixelConfig:
348 case kAlpha_half_as_Red_GrPixelConfig:
349 case kRGBA_float_GrPixelConfig:
350 case kRG_float_GrPixelConfig:
351 case kRGBA_half_GrPixelConfig:
352 case kAlpha_8_GrPixelConfig:
353 case kAlpha_8_as_Alpha_GrPixelConfig:
354 case kAlpha_8_as_Red_GrPixelConfig:
355 return -1;
356 }
357 SkASSERT(false);
358 return 0;
359}
360#endif
361
bsalomonf1b7a1d2015-09-28 06:26:28 -0700362static inline bool skpaint_to_grpaint_impl(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400363 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700364 const SkPaint& skPaint,
365 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400366 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400367 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700368 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400369 // Convert SkPaint color to 4f format in the destination color space
Brian Osmanb0f4d0a2018-09-05 14:16:27 +0000370 GrColor4f origColor = SkColor4fToUnpremulGrColor4f(skPaint.getColor4f(), colorSpaceInfo);
brianosmana4535a32016-06-24 12:50:19 -0700371
Mike Reed3bc266b2018-01-20 22:24:41 +0000372 const GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000373
bsalomonf1b7a1d2015-09-28 06:26:28 -0700374 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
375 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400376 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400377 if (!primColorMode || blend_requires_shader(*primColorMode)) {
bsalomonaa48d362015-10-01 08:34:17 -0700378 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400379 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400380 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000381 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500382 if (!shaderFP) {
383 return false;
384 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700385 }
386 }
387
388 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
389 // a known constant value. In that case we can simply apply a color filter during this
390 // conversion without converting the color filter to a GrFragmentProcessor.
391 bool applyColorFilterToPaintColor = false;
392 if (shaderFP) {
393 if (primColorMode) {
394 // There is a blend between the primitive color and the shader color. The shader sees
395 // the opaque paint color. The shader's output is blended using the provided mode by
396 // the primitive color. The blended color is then modulated by the paint's alpha.
397
398 // The geometry processor will insert the primitive color to start the color chain, so
399 // the GrPaint color will be ignored.
400
brianosman4cea3b92016-09-08 09:33:50 -0700401 GrColor4f shaderInput = origColor.opaque();
Brian Salomonaff329b2017-08-11 09:40:37 -0400402 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400403 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
404 *primColorMode);
405
bsalomonf1b7a1d2015-09-28 06:26:28 -0700406 // The above may return null if compose results in a pass through of the prim color.
407 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400408 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700409 }
410
brianosmana4535a32016-06-24 12:50:19 -0700411 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osmanb0f4d0a2018-09-05 14:16:27 +0000412 GrColor paintAlpha = GrColorPackA4(skPaint.getAlpha());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700413 if (GrColor_WHITE != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400414 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
415 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700416 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman618d3042016-10-25 10:51:28 -0400417 GrColor4f::FromGrColor(paintAlpha),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500418 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700419 }
420 } else {
421 // The shader's FP sees the paint unpremul color
brianosmana4535a32016-06-24 12:50:19 -0700422 grPaint->setColor4f(origColor);
bungeman06ca8ec2016-06-09 08:01:03 -0700423 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700424 }
425 } else {
426 if (primColorMode) {
427 // There is a blend between the primitive color and the paint color. The blend considers
428 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Salomonaff329b2017-08-11 09:40:37 -0400429 auto processor = GrConstColorProcessor::Make(origColor.opaque(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500430 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400431 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
432 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700433 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700434 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700435 }
436
brianosmana4535a32016-06-24 12:50:19 -0700437 grPaint->setColor4f(origColor.opaque());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700438
brianosmana4535a32016-06-24 12:50:19 -0700439 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osmanb0f4d0a2018-09-05 14:16:27 +0000440 GrColor paintAlpha = GrColorPackA4(skPaint.getAlpha());
bsalomonaa48d362015-10-01 08:34:17 -0700441 if (GrColor_WHITE != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400442 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
443 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700444 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman618d3042016-10-25 10:51:28 -0400445 GrColor4f::FromGrColor(paintAlpha),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500446 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700447 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700448 } else {
449 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700450 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700451 applyColorFilterToPaintColor = true;
452 }
453 }
454
455 SkColorFilter* colorFilter = skPaint.getColorFilter();
456 if (colorFilter) {
457 if (applyColorFilterToPaintColor) {
Brian Osman8f9c3252018-06-05 11:12:31 -0400458 grPaint->setColor4f(GrColor4f::FromSkColor4f(
Brian Osman04ae8ee2018-08-01 13:29:27 -0400459 colorFilter->filterColor4f(origColor.toSkColor4f(),
460 colorSpaceInfo.colorSpace())).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700461 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400462 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
bsalomone25eea42015-09-29 06:38:55 -0700463 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700464 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700465 } else {
466 return false;
467 }
468 }
469 }
470
Mike Reed80747ef2018-01-23 15:29:32 -0500471 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400472 if (maskFilter) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000473 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
474 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400475 }
476 }
477
robertphillips4f037942016-02-09 05:09:27 -0800478 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
479 // the GrPaint to also be null (also kSrcOver).
480 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700481 if (!skPaint.isSrcOver()) {
482 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800483 }
mtklein775b8192014-12-02 09:11:25 -0800484
krajcevskif461a8f2014-06-19 14:14:06 -0700485#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400486 // Conservative default, in case GrPixelConfigToColorType() fails.
487 SkColorType ct = SkColorType::kRGB_565_SkColorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400488 GrPixelConfigToColorType(colorSpaceInfo.config(), &ct);
Brian Osman34ec3742018-07-03 10:40:57 -0400489 if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400490 int32_t ditherRange = dither_range_type_for_config(colorSpaceInfo.config());
491 if (ditherRange >= 0) {
492 static int ditherIndex = GrSkSLFP::NewIndex();
493 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
494 &ditherRange, sizeof(ditherRange));
495 if (ditherFP) {
496 grPaint->addColorFragmentProcessor(std::move(ditherFP));
497 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000498 }
krajcevskif461a8f2014-06-19 14:14:06 -0700499 }
500#endif
bsalomonbed83a62015-04-15 14:18:34 -0700501 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000502}
503
Brian Salomonf3569f02017-10-24 12:52:33 -0400504bool SkPaintToGrPaint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
505 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
506 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
507 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700508}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000509
bsalomonf1b7a1d2015-09-28 06:26:28 -0700510/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
511bool SkPaintToGrPaintReplaceShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400512 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700513 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400514 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700515 GrPaint* grPaint) {
516 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700517 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000518 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400519 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
520 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000521}
reed8b26b992015-05-07 15:36:17 -0700522
bsalomonf1b7a1d2015-09-28 06:26:28 -0700523/** Ignores the SkShader (if any) on skPaint. */
524bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400525 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700526 const SkPaint& skPaint,
527 GrPaint* grPaint) {
528 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400529 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
530 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400531 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700532}
533
534/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400535be setup as a vertex attribute using the specified SkBlendMode. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700536bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400537 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700538 const SkPaint& skPaint,
539 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400540 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700541 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400542 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400543 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700544}
545
joshualitt33a5fce2015-11-18 13:28:51 -0800546bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400547 const GrColorSpaceInfo& colorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800548 const SkPaint& paint,
549 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400550 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800551 bool textureIsAlphaOnly,
552 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400553 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800554 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400555 if (const auto* shader = as_SB(paint.getShader())) {
Mike Reede3429e62018-01-19 11:43:34 -0500556 shaderFP = shader->asFragmentProcessor(GrFPArgs(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400557 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800558 if (!shaderFP) {
559 return false;
560 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400561 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700562 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800563 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400564 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800565 }
566 } else {
Mike Reed28eaed22018-02-01 11:24:53 -0500567 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800568 }
569
Brian Salomonf3569f02017-10-24 12:52:33 -0400570 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
571 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800572}
573
bsalomonf1b7a1d2015-09-28 06:26:28 -0700574
575////////////////////////////////////////////////////////////////////////////////////////////////
576
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400577GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
578 const SkMatrix& viewM,
579 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500580 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400581 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700582 *doBicubic = false;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400583 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700584 switch (paintFilterQuality) {
585 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400586 textureFilterMode = GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700587 break;
588 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400589 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700590 break;
591 case kMedium_SkFilterQuality: {
592 SkMatrix matrix;
593 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500594 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
595 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
596 //
597 // Want: 0 = log2(1/s) - 0.5
598 // 0.5 = log2(1/s)
599 // 2^0.5 = 1/s
600 // 1/2^0.5 = s
601 // 2^0.5/2 = s
602 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
603 if (matrix.getMinScale() < mipScale) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400604 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700605 } else {
606 // Don't trigger MIP level generation unnecessarily.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400607 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700608 }
609 break;
610 }
611 case kHigh_SkFilterQuality: {
612 SkMatrix matrix;
613 matrix.setConcat(viewM, localM);
614 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
615 break;
616 }
617 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400618 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400619 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700620 break;
621
622 }
623 return textureFilterMode;
624}