blob: 27b4248c0d78032acb9bcee4823396908b947cd3 [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"
Florin Malitad4a70ee2017-06-19 10:21:43 -040031#include "SkPaintPriv.h"
Hal Canary95e3c052017-01-11 12:44:43 -050032#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080033#include "SkResourceCache.h"
Florin Malita4aed1382017-05-25 10:38:07 -040034#include "SkShaderBase.h"
cblume55f2d2d2016-02-26 13:20:48 -080035#include "SkTemplates.h"
Derek Sollenberger559f5342017-08-17 12:34:54 -040036#include "SkTraceEvent.h"
joshualitt9bc39542015-08-12 12:57:54 -070037#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070038#include "effects/GrConstColorProcessor.h"
egdaniel378092f2014-12-03 10:40:13 -080039#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070040#include "effects/GrXfermodeFragmentProcessor.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040041#include "effects/GrSkSLFP.h"
42
Ethan Nicholas78aceb22018-08-31 16:13:58 -040043#if SK_SUPPORT_GPU
44GR_FP_SRC_STRING SKSL_DITHER_SRC = R"(
Ethan Nicholas00543112018-07-31 09:44:36 -040045// This controls the range of values added to color channels
46layout(key) in int rangeType;
47
48void main(int x, int y, inout half4 color) {
49 half value;
50 half range;
51 @switch (rangeType) {
52 case 0:
53 range = 1.0 / 255.0;
54 break;
55 case 1:
56 range = 1.0 / 63.0;
57 break;
58 default:
59 // Experimentally this looks better than the expected value of 1/15.
60 range = 1.0 / 15.0;
61 break;
62 }
63 @if (sk_Caps.integerSupport) {
64 // This ordered-dither code is lifted from the cpu backend.
65 uint x = uint(x);
66 uint y = uint(y);
67 uint m = (y & 1) << 5 | (x & 1) << 4 |
68 (y & 2) << 2 | (x & 2) << 1 |
69 (y & 4) >> 1 | (x & 4) >> 2;
70 value = half(m) * 1.0 / 64.0 - 63.0 / 128.0;
71 } else {
72 // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
73 // dither pattern rather than an 8x8 one.
74 half4 modValues = mod(float4(x, y, x, y), half4(2.0, 2.0, 4.0, 4.0));
75 half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
76 value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
77 }
78 // For each color channel, add the random offset to the channel value and then clamp
79 // between 0 and alpha to keep the color premultiplied.
80 color = half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);
81}
82)";
Ethan Nicholas78aceb22018-08-31 16:13:58 -040083#endif
krajcevski9c0e6292014-06-02 07:38:14 -070084
Brian Osman2b23c4b2018-06-01 12:25:08 -040085GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
bsalomon466c2c42015-10-16 12:01:18 -070086 GrSurfaceDesc desc;
87 desc.fFlags = kNone_GrSurfaceFlags;
88 desc.fWidth = info.width();
89 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040090 desc.fConfig = SkImageInfo2GrPixelConfig(info);
Brian Salomonbdecacf2018-02-02 20:32:49 -050091 desc.fSampleCnt = 1;
bsalomon466c2c42015-10-16 12:01:18 -070092 return desc;
93}
94
bsalomon045802d2015-10-20 07:58:01 -070095void 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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400100 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -0700101 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -0700102 builder[1] = imageBounds.fLeft;
103 builder[2] = imageBounds.fTop;
104 builder[3] = imageBounds.fRight;
105 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -0700106}
107
bsalomon045802d2015-10-20 07:58:01 -0700108//////////////////////////////////////////////////////////////////////////////
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider* proxyProvider,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400110 const SkBitmap& bitmap) {
Greg Daniel7619b642018-02-08 15:32:02 -0500111 if (!bitmap.peekPixels(nullptr)) {
Brian Osman4075ec82017-01-17 16:41:03 +0000112 return nullptr;
113 }
Greg Daniel7619b642018-02-08 15:32:02 -0500114
Brian Osman2b23c4b2018-06-01 12:25:08 -0400115 if (!SkImageInfoIsValid(bitmap.info())) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400116 return nullptr;
117 }
Greg Daniel7619b642018-02-08 15:32:02 -0500118
119 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
120 // even if it's mutable. In ddl, if the bitmap is mutable then we must make a copy since the
121 // 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 -0400122 SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode
123 : kNever_SkCopyPixelsMode;
Greg Daniel7619b642018-02-08 15:32:02 -0500124 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
125
Brian Salomon58389b92018-03-07 13:01:25 -0500126 return proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
127 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillipsd3749482017-03-14 09:17:43 -0400128}
Brian Osman4075ec82017-01-17 16:41:03 +0000129
bsalomon045802d2015-10-20 07:58:01 -0700130////////////////////////////////////////////////////////////////////////////////
131
Brian Salomon238069b2018-07-11 15:58:57 -0400132void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
133 SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700134 class Invalidator : public SkPixelRef::GenIDChangeListener {
135 public:
Brian Salomon238069b2018-07-11 15:58:57 -0400136 explicit Invalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
137 : fMsg(key, contextUniqueID) {}
138
bsalomon89fe56b2015-10-29 10:49:28 -0700139 private:
140 GrUniqueKeyInvalidatedMessage fMsg;
141
142 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
143 };
144
Brian Salomon238069b2018-07-11 15:58:57 -0400145 pixelRef->addGenIDChangeListener(new Invalidator(key, contextUniqueID));
bsalomon89fe56b2015-10-29 10:49:28 -0700146}
147
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500148sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext* ctx, GrTextureProxy* baseProxy) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400149 SkASSERT(baseProxy);
150
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400151 if (!ctx->contextPriv().caps()->isConfigCopyable(baseProxy->config())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400152 return nullptr;
153 }
154
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500155 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Greg Daniel55afd6d2017-09-29 09:32:44 -0400156 GrSurfaceDesc desc;
157 desc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400158 desc.fWidth = baseProxy->width();
159 desc.fHeight = baseProxy->height();
160 desc.fConfig = baseProxy->config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500161 desc.fSampleCnt = 1;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400162
Greg Daniel4065d452018-11-16 15:43:41 -0500163 GrBackendFormat format = baseProxy->backendFormat().makeTexture2D();
164 if (!format.isValid()) {
165 return nullptr;
166 }
167
Brian Salomon2a4f9832018-03-03 22:43:43 -0500168 sk_sp<GrTextureProxy> proxy =
Greg Daniel4065d452018-11-16 15:43:41 -0500169 proxyProvider->createMipMapProxy(format, desc, baseProxy->origin(), SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400170 if (!proxy) {
171 return nullptr;
172 }
173
174 // Copy the base layer to our proxy
Brian Salomon366093f2018-02-13 09:25:22 -0500175 sk_sp<GrSurfaceContext> sContext =
Brian Osman9363ac42018-06-01 16:10:53 -0400176 ctx->contextPriv().makeWrappedSurfaceContext(proxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400177 SkASSERT(sContext);
178 SkAssertResult(sContext->copy(baseProxy));
179
180 return proxy;
181}
182
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400183sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext* ctx,
184 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400185 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400186 SkScalar scaleAdjust[2]) {
Brian Osman6064e1c2018-10-19 14:27:54 -0400187 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400188}
189
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500190sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500191 const SkBitmap& bitmap,
192 SkBackingFit fit) {
193 if (!bitmap.peekPixels(nullptr)) {
194 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500195 }
196
Greg Daniel7e1912a2018-02-08 09:15:33 -0500197 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
198 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
199 // 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 -0400200 SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode
201 : kNever_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500202 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500203
Greg Daniel7e1912a2018-02-08 09:15:33 -0500204 if (!image) {
205 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500206 }
207
Greg Daniel7e1912a2018-02-08 09:15:33 -0500208 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500209}
210
Robert Phillips7a926392018-02-01 15:49:54 -0500211static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
212 if (!image) {
213 result->reset(); // will be invalid
214 return;
215 }
216
217 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000218 if (!bm->isVolatile()) {
219 SkIPoint origin = bm->pixelRefOrigin();
220 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
221 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
222 }
Robert Phillips7a926392018-02-01 15:49:54 -0500223 return;
224 }
225
226 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
227}
228
229sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500230 sk_sp<SkImage> srcImage,
231 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500232 sk_sp<GrTextureProxy> proxy;
233 GrUniqueKey originalKey;
234
235 create_unique_key_for_image(srcImage.get(), &originalKey);
236
237 if (originalKey.isValid()) {
238 proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin);
239 }
240 if (!proxy) {
Greg Daniela4211122018-03-14 19:09:36 +0000241 proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1,
Brian Salomon58389b92018-03-07 13:01:25 -0500242 SkBudgeted::kYes, fit);
Robert Phillips7a926392018-02-01 15:49:54 -0500243 if (proxy && originalKey.isValid()) {
244 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400245 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
246 // When recording DDLs we do not want to install change listeners because doing
247 // so isn't threadsafe.
248 if (bm && !proxyProvider->recordingDDL()) {
Brian Salomon238069b2018-07-11 15:58:57 -0400249 GrInstallBitmapUniqueKeyInvalidator(originalKey, proxyProvider->contextUniqueID(),
250 bm->pixelRef());
Greg Daniela4211122018-03-14 19:09:36 +0000251 }
Robert Phillips7a926392018-02-01 15:49:54 -0500252 }
253 }
254
255 return proxy;
256}
257
rileya@google.com24f3ad12012-07-18 21:47:40 +0000258///////////////////////////////////////////////////////////////////////////////
259
Brian Osmanf28e55d2018-10-03 16:35:54 -0400260SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
261 SkColor4f color = SkColor4f::FromColor(c);
262 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
263 color = xform->apply(color);
264 }
265 return color.premul();
266}
267
Brian Osman5c8a6b32018-11-19 11:56:57 -0500268SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorSpaceInfo& colorSpaceInfo,
269 const GrCaps& caps) {
270 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
271 color = xform->apply(color);
272 }
273 if (!GrPixelConfigIsFloatingPoint(colorSpaceInfo.config()) ||
274 !caps.halfFloatVertexAttributeSupport()) {
275 color = { SkTPin(color.fR, 0.0f, 1.0f),
276 SkTPin(color.fG, 0.0f, 1.0f),
277 SkTPin(color.fB, 0.0f, 1.0f),
278 color.fA };
279 }
280 return color;
281}
282
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400283///////////////////////////////////////////////////////////////////////////////
284
Brian Osman2b23c4b2018-06-01 12:25:08 -0400285GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400286 switch (type) {
brianosmanc571c002016-03-17 13:01:26 -0700287 case kUnknown_SkColorType:
288 return kUnknown_GrPixelConfig;
289 case kAlpha_8_SkColorType:
290 return kAlpha_8_GrPixelConfig;
291 case kRGB_565_SkColorType:
292 return kRGB_565_GrPixelConfig;
293 case kARGB_4444_SkColorType:
294 return kRGBA_4444_GrPixelConfig;
295 case kRGBA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400296 return kRGBA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500297 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400298 return kRGB_888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700299 case kBGRA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400300 return kBGRA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500301 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500302 return kRGBA_1010102_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500303 case kRGB_101010x_SkColorType:
304 return kUnknown_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700305 case kGray_8_SkColorType:
Brian Osman986563b2017-01-10 14:20:02 -0500306 return kGray_8_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700307 case kRGBA_F16_SkColorType:
308 return kRGBA_half_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -0400309 case kRGBA_F32_SkColorType:
310 return kRGBA_float_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000311 }
312 SkASSERT(0); // shouldn't get here
313 return kUnknown_GrPixelConfig;
314}
315
Brian Osman2b23c4b2018-06-01 12:25:08 -0400316GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
317 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400318}
319
brianosman396fcdb2016-07-22 06:26:11 -0700320bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400321 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
322 if (kUnknown_SkColorType != ct) {
323 if (ctOut) {
324 *ctOut = ct;
325 }
326 return true;
reed@google.combf790232013-12-13 19:45:58 +0000327 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400328 return false;
reed@google.combf790232013-12-13 19:45:58 +0000329}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000330
bsalomonf1b7a1d2015-09-28 06:26:28 -0700331////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000332
Mike Reed185ba212017-04-28 12:31:05 -0400333static inline bool blend_requires_shader(const SkBlendMode mode) {
334 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700335}
336
Ethan Nicholas00543112018-07-31 09:44:36 -0400337#ifndef SK_IGNORE_GPU_DITHER
338static inline int32_t dither_range_type_for_config(GrPixelConfig dstConfig) {
339 switch (dstConfig) {
340 case kGray_8_GrPixelConfig:
341 case kGray_8_as_Lum_GrPixelConfig:
342 case kGray_8_as_Red_GrPixelConfig:
343 case kRGBA_8888_GrPixelConfig:
344 case kRGB_888_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500345 case kRG_88_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400346 case kBGRA_8888_GrPixelConfig:
347 return 0;
348 case kRGB_565_GrPixelConfig:
349 return 1;
350 case kRGBA_4444_GrPixelConfig:
351 return 2;
352 case kUnknown_GrPixelConfig:
353 case kSRGBA_8888_GrPixelConfig:
354 case kSBGRA_8888_GrPixelConfig:
355 case kRGBA_1010102_GrPixelConfig:
356 case kAlpha_half_GrPixelConfig:
357 case kAlpha_half_as_Red_GrPixelConfig:
358 case kRGBA_float_GrPixelConfig:
359 case kRG_float_GrPixelConfig:
360 case kRGBA_half_GrPixelConfig:
Jim Van Verth1676cb92019-01-15 13:24:45 -0500361 case kRGB_ETC1_GrPixelConfig:
Ethan Nicholas00543112018-07-31 09:44:36 -0400362 case kAlpha_8_GrPixelConfig:
363 case kAlpha_8_as_Alpha_GrPixelConfig:
364 case kAlpha_8_as_Red_GrPixelConfig:
365 return -1;
366 }
367 SkASSERT(false);
368 return 0;
369}
370#endif
371
bsalomonf1b7a1d2015-09-28 06:26:28 -0700372static inline bool skpaint_to_grpaint_impl(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400373 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700374 const SkPaint& skPaint,
375 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400376 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400377 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700378 GrPaint* grPaint) {
Brian Osman08a50e02018-06-15 15:06:48 -0400379 // Convert SkPaint color to 4f format in the destination color space
Brian Osman5c8a6b32018-11-19 11:56:57 -0500380 SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), colorSpaceInfo,
381 *context->contextPriv().caps());
brianosmana4535a32016-06-24 12:50:19 -0700382
Mike Reed3bc266b2018-01-20 22:24:41 +0000383 const GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000384
bsalomonf1b7a1d2015-09-28 06:26:28 -0700385 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
386 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400387 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400388 if (!primColorMode || blend_requires_shader(*primColorMode)) {
bsalomonaa48d362015-10-01 08:34:17 -0700389 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400390 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400391 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000392 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500393 if (!shaderFP) {
394 return false;
395 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700396 }
397 }
398
399 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
400 // a known constant value. In that case we can simply apply a color filter during this
401 // conversion without converting the color filter to a GrFragmentProcessor.
402 bool applyColorFilterToPaintColor = false;
403 if (shaderFP) {
404 if (primColorMode) {
405 // There is a blend between the primitive color and the shader color. The shader sees
406 // the opaque paint color. The shader's output is blended using the provided mode by
407 // the primitive color. The blended color is then modulated by the paint's alpha.
408
409 // The geometry processor will insert the primitive color to start the color chain, so
410 // the GrPaint color will be ignored.
411
Brian Osmancb3d0872018-10-16 15:19:28 -0400412 SkPMColor4f shaderInput = origColor.makeOpaque().premul();
Brian Salomonaff329b2017-08-11 09:40:37 -0400413 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400414 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
415 *primColorMode);
416
bsalomonf1b7a1d2015-09-28 06:26:28 -0700417 // The above may return null if compose results in a pass through of the prim color.
418 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400419 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700420 }
421
brianosmana4535a32016-06-24 12:50:19 -0700422 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500423 float paintAlpha = skPaint.getColor4f().fA;
424 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400425 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
426 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700427 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500428 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500429 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700430 }
431 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400432 // The shader's FP sees the paint *unpremul* color
433 SkPMColor4f origColorAsPM = { origColor.fR, origColor.fG, origColor.fB, origColor.fA };
434 grPaint->setColor4f(origColorAsPM);
bungeman06ca8ec2016-06-09 08:01:03 -0700435 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700436 }
437 } else {
438 if (primColorMode) {
439 // There is a blend between the primitive color and the paint color. The blend considers
440 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Osmancb3d0872018-10-16 15:19:28 -0400441 SkPMColor4f opaqueColor = origColor.makeOpaque().premul();
442 auto processor = GrConstColorProcessor::Make(opaqueColor,
443 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400444 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
445 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700446 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700447 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700448 }
449
Brian Osmancb3d0872018-10-16 15:19:28 -0400450 grPaint->setColor4f(opaqueColor);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700451
brianosmana4535a32016-06-24 12:50:19 -0700452 // We can ignore origColor here - alpha is unchanged by gamma
Brian Osman00b29392018-11-05 15:42:43 -0500453 float paintAlpha = skPaint.getColor4f().fA;
454 if (1.0f != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400455 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
456 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700457 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman00b29392018-11-05 15:42:43 -0500458 { paintAlpha, paintAlpha, paintAlpha, paintAlpha },
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500459 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700460 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700461 } else {
462 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700463 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700464 applyColorFilterToPaintColor = true;
465 }
466 }
467
468 SkColorFilter* colorFilter = skPaint.getColorFilter();
469 if (colorFilter) {
470 if (applyColorFilterToPaintColor) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400471 grPaint->setColor4f(
472 colorFilter->filterColor4f(origColor, colorSpaceInfo.colorSpace()).premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700473 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400474 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
bsalomone25eea42015-09-29 06:38:55 -0700475 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700476 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700477 } else {
478 return false;
479 }
480 }
481 }
482
Mike Reed80747ef2018-01-23 15:29:32 -0500483 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400484 if (maskFilter) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000485 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
486 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400487 }
488 }
489
robertphillips4f037942016-02-09 05:09:27 -0800490 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
491 // the GrPaint to also be null (also kSrcOver).
492 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700493 if (!skPaint.isSrcOver()) {
494 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800495 }
mtklein775b8192014-12-02 09:11:25 -0800496
krajcevskif461a8f2014-06-19 14:14:06 -0700497#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400498 // Conservative default, in case GrPixelConfigToColorType() fails.
499 SkColorType ct = SkColorType::kRGB_565_SkColorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400500 GrPixelConfigToColorType(colorSpaceInfo.config(), &ct);
Brian Osman34ec3742018-07-03 10:40:57 -0400501 if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400502 int32_t ditherRange = dither_range_type_for_config(colorSpaceInfo.config());
503 if (ditherRange >= 0) {
504 static int ditherIndex = GrSkSLFP::NewIndex();
505 auto ditherFP = GrSkSLFP::Make(context, ditherIndex, "Dither", SKSL_DITHER_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000506 &ditherRange, sizeof(ditherRange));
Ethan Nicholas00543112018-07-31 09:44:36 -0400507 if (ditherFP) {
508 grPaint->addColorFragmentProcessor(std::move(ditherFP));
509 }
Brian Salomon0c15ae82017-07-19 15:39:56 +0000510 }
krajcevskif461a8f2014-06-19 14:14:06 -0700511 }
512#endif
bsalomonbed83a62015-04-15 14:18:34 -0700513 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000514}
515
Brian Salomonf3569f02017-10-24 12:52:33 -0400516bool SkPaintToGrPaint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
517 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
518 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
519 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700520}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000521
bsalomonf1b7a1d2015-09-28 06:26:28 -0700522/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
523bool SkPaintToGrPaintReplaceShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400524 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700525 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400526 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700527 GrPaint* grPaint) {
528 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700529 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000530 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400531 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
532 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000533}
reed8b26b992015-05-07 15:36:17 -0700534
bsalomonf1b7a1d2015-09-28 06:26:28 -0700535/** Ignores the SkShader (if any) on skPaint. */
536bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400537 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700538 const SkPaint& skPaint,
539 GrPaint* grPaint) {
540 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400541 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
542 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400543 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700544}
545
546/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400547be setup as a vertex attribute using the specified SkBlendMode. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700548bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400549 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700550 const SkPaint& skPaint,
551 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400552 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700553 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400554 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400555 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700556}
557
joshualitt33a5fce2015-11-18 13:28:51 -0800558bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400559 const GrColorSpaceInfo& colorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800560 const SkPaint& paint,
561 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400562 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800563 bool textureIsAlphaOnly,
564 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400565 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800566 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400567 if (const auto* shader = as_SB(paint.getShader())) {
Mike Reede3429e62018-01-19 11:43:34 -0500568 shaderFP = shader->asFragmentProcessor(GrFPArgs(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400569 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800570 if (!shaderFP) {
571 return false;
572 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400573 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700574 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800575 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400576 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800577 }
578 } else {
Mike Reed28eaed22018-02-01 11:24:53 -0500579 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800580 }
581
Brian Salomonf3569f02017-10-24 12:52:33 -0400582 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
583 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800584}
585
bsalomonf1b7a1d2015-09-28 06:26:28 -0700586
587////////////////////////////////////////////////////////////////////////////////////////////////
588
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400589GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
590 const SkMatrix& viewM,
591 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500592 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400593 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700594 *doBicubic = false;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400595 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700596 switch (paintFilterQuality) {
597 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400598 textureFilterMode = GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700599 break;
600 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400601 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700602 break;
603 case kMedium_SkFilterQuality: {
604 SkMatrix matrix;
605 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500606 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
607 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
608 //
609 // Want: 0 = log2(1/s) - 0.5
610 // 0.5 = log2(1/s)
611 // 2^0.5 = 1/s
612 // 1/2^0.5 = s
613 // 2^0.5/2 = s
614 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
615 if (matrix.getMinScale() < mipScale) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400616 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700617 } else {
618 // Don't trigger MIP level generation unnecessarily.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400619 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700620 }
621 break;
622 }
623 case kHigh_SkFilterQuality: {
624 SkMatrix matrix;
625 matrix.setConcat(viewM, localM);
626 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
627 break;
628 }
629 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400630 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400631 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700632 break;
633
634 }
635 return textureFilterMode;
636}