blob: 609a14e3c6b8dd5e78b83c33b99afde8acbf5d67 [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"
Robert Phillips9338c602019-02-19 12:52:29 -050017#include "GrRecordingContext.h"
18#include "GrRecordingContextPriv.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050019#include "GrTextureProxy.h"
cblume55f2d2d2016-02-26 13:20:48 -080020#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080021#include "GrXferProcessor.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "SkAutoMalloc.h"
reed374772b2016-10-05 17:33:02 -070023#include "SkBlendModePriv.h"
Hal Canary95e3c052017-01-11 12:44:43 -050024#include "SkCanvas.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000025#include "SkColorFilter.h"
krajcevski9c0e6292014-06-02 07:38:14 -070026#include "SkData.h"
Robert Phillips7a926392018-02-01 15:49:54 -050027#include "SkImage_Base.h"
Brian Osman4075ec82017-01-17 16:41:03 +000028#include "SkImageInfoPriv.h"
Greg Daniel7e1912a2018-02-08 09:15:33 -050029#include "SkImagePriv.h"
Mike Reed80747ef2018-01-23 15:29:32 -050030#include "SkMaskFilterBase.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000031#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080032#include "SkMipMap.h"
Florin Malitad4a70ee2017-06-19 10:21:43 -040033#include "SkPaintPriv.h"
Hal Canary95e3c052017-01-11 12:44:43 -050034#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080035#include "SkResourceCache.h"
Florin Malita4aed1382017-05-25 10:38:07 -040036#include "SkShaderBase.h"
cblume55f2d2d2016-02-26 13:20:48 -080037#include "SkTemplates.h"
Derek Sollenberger559f5342017-08-17 12:34:54 -040038#include "SkTraceEvent.h"
joshualitt9bc39542015-08-12 12:57:54 -070039#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070040#include "effects/GrConstColorProcessor.h"
egdaniel378092f2014-12-03 10:40:13 -080041#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070042#include "effects/GrXfermodeFragmentProcessor.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040043#include "effects/GrSkSLFP.h"
44
Ethan Nicholas78aceb22018-08-31 16:13:58 -040045#if SK_SUPPORT_GPU
46GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Ethan Nicholas00543112018-07-31 09:44:36 -040047// This controls the range of values added to color channels
48layout(key) in int rangeType;
49
50void 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 Nicholase1f55022019-02-05 17:17:40 -050076 half4 modValues = mod(half4(x, y, x, y), half4(2.0, 2.0, 4.0, 4.0));
Ethan Nicholas00543112018-07-31 09:44:36 -040077 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 Nicholas78aceb22018-08-31 16:13:58 -040085#endif
krajcevski9c0e6292014-06-02 07:38:14 -070086
Brian Osman2b23c4b2018-06-01 12:25:08 -040087GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
bsalomon466c2c42015-10-16 12:01:18 -070088 GrSurfaceDesc desc;
89 desc.fFlags = kNone_GrSurfaceFlags;
90 desc.fWidth = info.width();
91 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040092 desc.fConfig = SkImageInfo2GrPixelConfig(info);
Brian Salomonbdecacf2018-02-02 20:32:49 -050093 desc.fSampleCnt = 1;
bsalomon466c2c42015-10-16 12:01:18 -070094 return desc;
95}
96
bsalomon045802d2015-10-20 07:58:01 -070097void 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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400102 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -0700103 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -0700104 builder[1] = imageBounds.fLeft;
105 builder[2] = imageBounds.fTop;
106 builder[3] = imageBounds.fRight;
107 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700108}
109
bsalomon045802d2015-10-20 07:58:01 -0700110//////////////////////////////////////////////////////////////////////////////
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500111sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider* proxyProvider,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400112 const SkBitmap& bitmap) {
Greg Daniel7619b642018-02-08 15:32:02 -0500113 if (!bitmap.peekPixels(nullptr)) {
Brian Osman4075ec82017-01-17 16:41:03 +0000114 return nullptr;
115 }
Greg Daniel7619b642018-02-08 15:32:02 -0500116
Brian Osman2b23c4b2018-06-01 12:25:08 -0400117 if (!SkImageInfoIsValid(bitmap.info())) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400118 return nullptr;
119 }
Greg Daniel7619b642018-02-08 15:32:02 -0500120
121 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
122 // even if it's mutable. In ddl, if the bitmap is mutable then we must make a copy since the
123 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500124 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
125 : kIfMutable_SkCopyPixelsMode;
Greg Daniel7619b642018-02-08 15:32:02 -0500126 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
127
Brian Salomon58389b92018-03-07 13:01:25 -0500128 return proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
129 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillipsd3749482017-03-14 09:17:43 -0400130}
Brian Osman4075ec82017-01-17 16:41:03 +0000131
bsalomon045802d2015-10-20 07:58:01 -0700132////////////////////////////////////////////////////////////////////////////////
133
Brian Salomon238069b2018-07-11 15:58:57 -0400134void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
135 SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700136 class Invalidator : public SkPixelRef::GenIDChangeListener {
137 public:
Brian Salomon238069b2018-07-11 15:58:57 -0400138 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
139 : fMsg(key, contextUniqueID) {}
140
bsalomon89fe56b2015-10-29 10:49:28 -0700141 private:
142 GrUniqueKeyInvalidatedMessage fMsg;
143
144 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
145 };
146
Brian Salomon238069b2018-07-11 15:58:57 -0400147 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
bsalomon89fe56b2015-10-29 10:49:28 -0700148}
149
Robert Phillips9338c602019-02-19 12:52:29 -0500150sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx,
151 GrTextureProxy* baseProxy) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400152 SkASSERT(baseProxy);
153
Robert Phillips9da87e02019-02-04 13:26:26 -0500154 if (!ctx->priv().caps()->isConfigCopyable(baseProxy->config())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400155 return nullptr;
156 }
157
Robert Phillips9da87e02019-02-04 13:26:26 -0500158 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Greg Daniel55afd6d2017-09-29 09:32:44 -0400159 GrSurfaceDesc desc;
160 desc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400161 desc.fWidth = baseProxy->width();
162 desc.fHeight = baseProxy->height();
163 desc.fConfig = baseProxy->config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500164 desc.fSampleCnt = 1;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400165
Greg Daniel4065d452018-11-16 15:43:41 -0500166 GrBackendFormat format = baseProxy->backendFormat().makeTexture2D();
167 if (!format.isValid()) {
168 return nullptr;
169 }
170
Brian Salomon2a4f9832018-03-03 22:43:43 -0500171 sk_sp<GrTextureProxy> proxy =
Greg Daniel4065d452018-11-16 15:43:41 -0500172 proxyProvider->createMipMapProxy(format, desc, baseProxy->origin(), SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400173 if (!proxy) {
174 return nullptr;
175 }
176
177 // Copy the base layer to our proxy
Robert Phillips9da87e02019-02-04 13:26:26 -0500178 sk_sp<GrSurfaceContext> sContext = ctx->priv().makeWrappedSurfaceContext(proxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400179 SkASSERT(sContext);
180 SkAssertResult(sContext->copy(baseProxy));
181
182 return proxy;
183}
184
Robert Phillips9338c602019-02-19 12:52:29 -0500185sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400186 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400187 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400188 SkScalar scaleAdjust[2]) {
Brian Osman6064e1c2018-10-19 14:27:54 -0400189 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400190}
191
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500192sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500193 const SkBitmap& bitmap,
194 SkBackingFit fit) {
195 if (!bitmap.peekPixels(nullptr)) {
196 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500197 }
198
Greg Daniel7e1912a2018-02-08 09:15:33 -0500199 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
200 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
201 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500202 SkCopyPixelsMode cpyMode = proxyProvider->renderingDirectly() ? kNever_SkCopyPixelsMode
203 : kIfMutable_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500204 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500205
Greg Daniel7e1912a2018-02-08 09:15:33 -0500206 if (!image) {
207 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500208 }
209
Greg Daniel7e1912a2018-02-08 09:15:33 -0500210 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500211}
212
Robert Phillips7a926392018-02-01 15:49:54 -0500213static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
214 if (!image) {
215 result->reset(); // will be invalid
216 return;
217 }
218
219 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000220 if (!bm->isVolatile()) {
221 SkIPoint origin = bm->pixelRefOrigin();
222 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
223 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
224 }
Robert Phillips7a926392018-02-01 15:49:54 -0500225 return;
226 }
227
228 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
229}
230
231sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500232 sk_sp<SkImage> srcImage,
233 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500234 sk_sp<GrTextureProxy> proxy;
235 GrUniqueKey originalKey;
236
237 create_unique_key_for_image(srcImage.get(), &originalKey);
238
239 if (originalKey.isValid()) {
240 proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin);
241 }
242 if (!proxy) {
Greg Daniela4211122018-03-14 19:09:36 +0000243 proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1,
Brian Salomon58389b92018-03-07 13:01:25 -0500244 SkBudgeted::kYes, fit);
Robert Phillips7a926392018-02-01 15:49:54 -0500245 if (proxy && originalKey.isValid()) {
246 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400247 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
248 // When recording DDLs we do not want to install change listeners because doing
249 // so isn't threadsafe.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500250 if (bm && proxyProvider->renderingDirectly()) {
251 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextID(),
Brian Salomon238069b2018-07-11 15:58:57 -0400252 bm->pixelRef());
Greg Daniela4211122018-03-14 19:09:36 +0000253 }
Robert Phillips7a926392018-02-01 15:49:54 -0500254 }
255 }
256
257 return proxy;
258}
259
rileya@google.com24f3ad12012-07-18 21:47:40 +0000260///////////////////////////////////////////////////////////////////////////////
261
Brian Osmanf28e55d2018-10-03 16:35:54 -0400262SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
263 SkColor4f color = SkColor4f::FromColor(c);
264 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
265 color = xform->apply(color);
266 }
267 return color.premul();
268}
269
Brian Osman5c8a6b32018-11-19 11:56:57 -0500270SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorSpaceInfo& colorSpaceInfo,
271 const GrCaps& caps) {
272 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
273 color = xform->apply(color);
274 }
Brian Osmand0626aa2019-03-11 15:28:06 -0400275 // TODO: Should we clamp here if config is kRGBA_half_Clamped_GrPixelConfig?
Brian Osman5c8a6b32018-11-19 11:56:57 -0500276 if (!GrPixelConfigIsFloatingPoint(colorSpaceInfo.config()) ||
277 !caps.halfFloatVertexAttributeSupport()) {
278 color = { SkTPin(color.fR, 0.0f, 1.0f),
279 SkTPin(color.fG, 0.0f, 1.0f),
280 SkTPin(color.fB, 0.0f, 1.0f),
281 color.fA };
282 }
283 return color;
284}
285
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400286///////////////////////////////////////////////////////////////////////////////
287
Brian Osman2b23c4b2018-06-01 12:25:08 -0400288GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400289 switch (type) {
brianosmanc571c002016-03-17 13:01:26 -0700290 case kUnknown_SkColorType:
291 return kUnknown_GrPixelConfig;
292 case kAlpha_8_SkColorType:
293 return kAlpha_8_GrPixelConfig;
294 case kRGB_565_SkColorType:
295 return kRGB_565_GrPixelConfig;
296 case kARGB_4444_SkColorType:
297 return kRGBA_4444_GrPixelConfig;
298 case kRGBA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400299 return kRGBA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500300 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400301 return kRGB_888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700302 case kBGRA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400303 return kBGRA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500304 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500305 return kRGBA_1010102_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500306 case kRGB_101010x_SkColorType:
307 return kUnknown_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700308 case kGray_8_SkColorType:
Brian Osman986563b2017-01-10 14:20:02 -0500309 return kGray_8_GrPixelConfig;
Brian Osmand0626aa2019-03-11 15:28:06 -0400310 case kRGBA_F16Norm_SkColorType:
311 return kRGBA_half_Clamped_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700312 case kRGBA_F16_SkColorType:
313 return kRGBA_half_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -0400314 case kRGBA_F32_SkColorType:
315 return kRGBA_float_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000316 }
317 SkASSERT(0); // shouldn't get here
318 return kUnknown_GrPixelConfig;
319}
320
Brian Osman2b23c4b2018-06-01 12:25:08 -0400321GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
322 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400323}
324
brianosman396fcdb2016-07-22 06:26:11 -0700325bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400326 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
327 if (kUnknown_SkColorType != ct) {
328 if (ctOut) {
329 *ctOut = ct;
330 }
331 return true;
reed@google.combf790232013-12-13 19:45:58 +0000332 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400333 return false;
reed@google.combf790232013-12-13 19:45:58 +0000334}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000335
bsalomonf1b7a1d2015-09-28 06:26:28 -0700336////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000337
Mike Reed185ba212017-04-28 12:31:05 -0400338static inline bool blend_requires_shader(const SkBlendMode mode) {
339 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700340}
341
Ethan Nicholas00543112018-07-31 09:44:36 -0400342#ifndef SK_IGNORE_GPU_DITHER
343static inline int32_t dither_range_type_for_config(GrPixelConfig dstConfig) {
344 switch (dstConfig) {
345 case kGray_8_GrPixelConfig:
346 case kGray_8_as_Lum_GrPixelConfig:
347 case kGray_8_as_Red_GrPixelConfig:
348 case kRGBA_8888_GrPixelConfig:
349 case kRGB_888_GrPixelConfig:
Greg Danielf259b8b2019-02-14 09:03:43 -0500350 case kRGB_888X_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500351 case kRG_88_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400352 case kBGRA_8888_GrPixelConfig:
353 return 0;
354 case kRGB_565_GrPixelConfig:
355 return 1;
356 case kRGBA_4444_GrPixelConfig:
357 return 2;
358 case kUnknown_GrPixelConfig:
359 case kSRGBA_8888_GrPixelConfig:
360 case kSBGRA_8888_GrPixelConfig:
361 case kRGBA_1010102_GrPixelConfig:
362 case kAlpha_half_GrPixelConfig:
363 case kAlpha_half_as_Red_GrPixelConfig:
364 case kRGBA_float_GrPixelConfig:
365 case kRG_float_GrPixelConfig:
366 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400367 case kRGBA_half_Clamped_GrPixelConfig:
Jim Van Verth1676cb92019-01-15 13:24:45 -0500368 case kRGB_ETC1_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400369 case kAlpha_8_GrPixelConfig:
370 case kAlpha_8_as_Alpha_GrPixelConfig:
371 case kAlpha_8_as_Red_GrPixelConfig:
372 return -1;
373 }
374 SkASSERT(false);
375 return 0;
376}
377#endif
378
Robert Phillips9338c602019-02-19 12:52:29 -0500379static inline bool skpaint_to_grpaint_impl(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400380 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700381 const SkPaint& skPaint,
382 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400383 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400384 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700385 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400386 // Convert SkPaint color to 4f format in the destination color space
Brian Osman5c8a6b32018-11-19 11:56:57 -0500387 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), colorSpaceInfo,
Robert Phillips9da87e02019-02-04 13:26:26 -0500388 *context->priv().caps());
brianosmana4535a32016-06-24 12:50:19 -0700389
Mike Reed3bc266b2018-01-20 22:24:41 +0000390 const GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000391
bsalomonf1b7a1d2015-09-28 06:26:28 -0700392 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
393 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400394 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400395 if (!primColorMode || blend_requires_shader(*primColorMode)) {
bsalomonaa48d362015-10-01 08:34:17 -0700396 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400397 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400398 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000399 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500400 if (!shaderFP) {
401 return false;
402 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700403 }
404 }
405
406 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
407 // a known constant value. In that case we can simply apply a color filter during this
408 // conversion without converting the color filter to a GrFragmentProcessor.
409 bool applyColorFilterToPaintColor = false;
410 if (shaderFP) {
411 if (primColorMode) {
412 // There is a blend between the primitive color and the shader color. The shader sees
413 // the opaque paint color. The shader's output is blended using the provided mode by
414 // the primitive color. The blended color is then modulated by the paint's alpha.
415
416 // The geometry processor will insert the primitive color to start the color chain, so
417 // the GrPaint color will be ignored.
418
Brian Osmancb3d0872018-10-16 15:19:28 -0400419 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
Brian Salomonaff329b2017-08-11 09:40:37 -0400420 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400421 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
422 *primColorMode);
423
bsalomonf1b7a1d2015-09-28 06:26:28 -0700424 // The above may return null if compose results in a pass through of the prim color.
425 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400426 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700427 }
428
brianosmana4535a32016-06-24 12:50:19 -0700429 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500430 float paintAlpha = skPaint.getColor4f().fA;
431 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400432 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
433 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700434 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500435 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500436 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700437 }
438 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400439 // The shader's FP sees the paint *unpremul* color
440 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
441 grPaint->setColor4f(origColorAsPM);
bungeman06ca8ec2016-06-09 08:01:03 -0700442 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700443 }
444 } else {
445 if (primColorMode) {
446 // There is a blend between the primitive color and the paint color. The blend considers
447 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400448 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
449 auto processor = GrConstColorProcessor::Make(opaqueColor,
450 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400451 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
452 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700453 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700454 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700455 }
456
Brian Osmancb3d0872018-10-16 15:19:28 -0400457 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700458
brianosmana4535a32016-06-24 12:50:19 -0700459 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500460 float paintAlpha = skPaint.getColor4f().fA;
461 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400462 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
463 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700464 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500465 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500466 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700467 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700468 } else {
469 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700470 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700471 applyColorFilterToPaintColor = true;
472 }
473 }
474
475 SkColorFilter* colorFilter = skPaint.getColorFilter();
476 if (colorFilter) {
477 if (applyColorFilterToPaintColor) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400478 grPaint->setColor4f(
479 colorFilter->filterColor4f(origColor, colorSpaceInfo.colorSpace()).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700480 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400481 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
bsalomone25eea42015-09-29 06:38:55 -0700482 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700483 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700484 } else {
485 return false;
486 }
487 }
488 }
489
Mike Reed80747ef2018-01-23 15:29:32 -0500490 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400491 if (maskFilter) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000492 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
493 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400494 }
495 }
496
robertphillips4f037942016-02-09 05:09:27 -0800497 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
498 // the GrPaint to also be null (also kSrcOver).
499 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700500 if (!skPaint.isSrcOver()) {
501 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800502 }
mtklein775b8192014-12-02 09:11:25 -0800503
krajcevskif461a8f2014-06-19 14:14:06 -0700504#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400505 // Conservative default, in case GrPixelConfigToColorType() fails.
506 SkColorType ct = SkColorType::kRGB_565_SkColorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400507 GrPixelConfigToColorType(colorSpaceInfo.config(), &ct);
Brian Osman34ec3742018-07-03 10:40:57 -0400508 if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400509 int32_t ditherRange = dither_range_type_for_config(colorSpaceInfo.config());
510 if (ditherRange >= 0) {
511 static int ditherIndex = GrSkSLFP::NewIndex();
512 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000513 &ditherRange, sizeof(ditherRange));
Ethan Nicholas00543112018-07-31 09:44:36 -0400514 if (ditherFP) {
515 grPaint->addColorFragmentProcessor(std::move(ditherFP));
516 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000517 }
krajcevskif461a8f2014-06-19 14:14:06 -0700518 }
519#endif
bsalomonbed83a62015-04-15 14:18:34 -0700520 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000521}
522
Robert Phillips9338c602019-02-19 12:52:29 -0500523bool SkPaintToGrPaint(GrRecordingContext* context, const GrColorSpaceInfo& colorSpaceInfo,
Brian Salomonf3569f02017-10-24 12:52:33 -0400524 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
525 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
526 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700527}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000528
bsalomonf1b7a1d2015-09-28 06:26:28 -0700529/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500530bool SkPaintToGrPaintReplaceShader(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400531 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700532 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400533 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700534 GrPaint* grPaint) {
535 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700536 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000537 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400538 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
539 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000540}
reed8b26b992015-05-07 15:36:17 -0700541
bsalomonf1b7a1d2015-09-28 06:26:28 -0700542/** Ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -0500543bool SkPaintToGrPaintNoShader(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400544 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700545 const SkPaint& skPaint,
546 GrPaint* grPaint) {
547 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400548 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
549 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400550 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700551}
552
553/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400554be setup as a vertex attribute using the specified SkBlendMode. */
Robert Phillips9338c602019-02-19 12:52:29 -0500555bool SkPaintToGrPaintWithXfermode(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400556 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700557 const SkPaint& skPaint,
558 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400559 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700560 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400561 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400562 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700563}
564
Robert Phillips9338c602019-02-19 12:52:29 -0500565bool SkPaintToGrPaintWithTexture(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400566 const GrColorSpaceInfo& colorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800567 const SkPaint& paint,
568 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400569 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800570 bool textureIsAlphaOnly,
571 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400572 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800573 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400574 if (const auto* shader = as_SB(paint.getShader())) {
Mike Reede3429e62018-01-19 11:43:34 -0500575 shaderFP = shader->asFragmentProcessor(GrFPArgs(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400576 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800577 if (!shaderFP) {
578 return false;
579 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400580 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700581 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800582 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400583 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800584 }
585 } else {
Mike Reed28eaed22018-02-01 11:24:53 -0500586 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800587 }
588
Brian Salomonf3569f02017-10-24 12:52:33 -0400589 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
590 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800591}
592
bsalomonf1b7a1d2015-09-28 06:26:28 -0700593
594////////////////////////////////////////////////////////////////////////////////////////////////
595
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400596GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
597 const SkMatrix& viewM,
598 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500599 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400600 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700601 *doBicubic = false;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400602 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700603 switch (paintFilterQuality) {
604 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400605 textureFilterMode = GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700606 break;
607 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400608 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700609 break;
610 case kMedium_SkFilterQuality: {
611 SkMatrix matrix;
612 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500613 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
614 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
615 //
616 // Want: 0 = log2(1/s) - 0.5
617 // 0.5 = log2(1/s)
618 // 2^0.5 = 1/s
619 // 1/2^0.5 = s
620 // 2^0.5/2 = s
621 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
622 if (matrix.getMinScale() < mipScale) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400623 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700624 } else {
625 // Don't trigger MIP level generation unnecessarily.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400626 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700627 }
628 break;
629 }
630 case kHigh_SkFilterQuality: {
631 SkMatrix matrix;
632 matrix.setConcat(viewM, localM);
633 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
634 break;
635 }
636 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400637 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400638 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700639 break;
640
641 }
642 return textureFilterMode;
643}