blob: 783be5cb6cf54d73c81e2f3d6c9e33648c378e40 [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"
krajcevskif461a8f2014-06-19 14:14:06 -070040#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080041#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070042#include "effects/GrXfermodeFragmentProcessor.h"
krajcevski9c0e6292014-06-02 07:38:14 -070043
Brian Osman2b23c4b2018-06-01 12:25:08 -040044GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
bsalomon466c2c42015-10-16 12:01:18 -070045 GrSurfaceDesc desc;
46 desc.fFlags = kNone_GrSurfaceFlags;
47 desc.fWidth = info.width();
48 desc.fHeight = info.height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040049 desc.fConfig = SkImageInfo2GrPixelConfig(info);
Brian Salomonbdecacf2018-02-02 20:32:49 -050050 desc.fSampleCnt = 1;
bsalomon466c2c42015-10-16 12:01:18 -070051 return desc;
52}
53
bsalomon045802d2015-10-20 07:58:01 -070054void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
55 SkASSERT(key);
56 SkASSERT(imageID);
57 SkASSERT(!imageBounds.isEmpty());
58 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040059 GrUniqueKey::Builder builder(key, kImageIDDomain, 5, "Image");
bsalomon466c2c42015-10-16 12:01:18 -070060 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070061 builder[1] = imageBounds.fLeft;
62 builder[2] = imageBounds.fTop;
63 builder[3] = imageBounds.fRight;
64 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070065}
66
bsalomon045802d2015-10-20 07:58:01 -070067//////////////////////////////////////////////////////////////////////////////
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider* proxyProvider,
Brian Osman2b23c4b2018-06-01 12:25:08 -040069 const SkBitmap& bitmap) {
Greg Daniel7619b642018-02-08 15:32:02 -050070 if (!bitmap.peekPixels(nullptr)) {
Brian Osman4075ec82017-01-17 16:41:03 +000071 return nullptr;
72 }
Greg Daniel7619b642018-02-08 15:32:02 -050073
Brian Osman2b23c4b2018-06-01 12:25:08 -040074 if (!SkImageInfoIsValid(bitmap.info())) {
Robert Phillipsd3749482017-03-14 09:17:43 -040075 return nullptr;
76 }
Greg Daniel7619b642018-02-08 15:32:02 -050077
78 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
79 // even if it's mutable. In ddl, if the bitmap is mutable then we must make a copy since the
80 // 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 -040081 SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode
82 : kNever_SkCopyPixelsMode;
Greg Daniel7619b642018-02-08 15:32:02 -050083 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
84
Brian Salomon58389b92018-03-07 13:01:25 -050085 return proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
86 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillipsd3749482017-03-14 09:17:43 -040087}
Brian Osman4075ec82017-01-17 16:41:03 +000088
bsalomon045802d2015-10-20 07:58:01 -070089////////////////////////////////////////////////////////////////////////////////
90
bsalomonc55271f2015-11-09 11:55:57 -080091void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -070092 class Invalidator : public SkPixelRef::GenIDChangeListener {
93 public:
94 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
95 private:
96 GrUniqueKeyInvalidatedMessage fMsg;
97
98 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
99 };
100
101 pixelRef->addGenIDChangeListener(new Invalidator(key));
102}
103
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500104sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext* ctx, GrTextureProxy* baseProxy) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400105 SkASSERT(baseProxy);
106
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400107 if (!ctx->contextPriv().caps()->isConfigCopyable(baseProxy->config())) {
Greg Danielbb76ace2017-09-29 15:58:22 -0400108 return nullptr;
109 }
110
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500111 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Greg Daniel55afd6d2017-09-29 09:32:44 -0400112 GrSurfaceDesc desc;
113 desc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400114 desc.fWidth = baseProxy->width();
115 desc.fHeight = baseProxy->height();
116 desc.fConfig = baseProxy->config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500117 desc.fSampleCnt = 1;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400118
Brian Salomon2a4f9832018-03-03 22:43:43 -0500119 sk_sp<GrTextureProxy> proxy =
120 proxyProvider->createMipMapProxy(desc, baseProxy->origin(), SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400121 if (!proxy) {
122 return nullptr;
123 }
124
125 // Copy the base layer to our proxy
Brian Salomon366093f2018-02-13 09:25:22 -0500126 sk_sp<GrSurfaceContext> sContext =
Brian Osman9363ac42018-06-01 16:10:53 -0400127 ctx->contextPriv().makeWrappedSurfaceContext(proxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400128 SkASSERT(sContext);
129 SkAssertResult(sContext->copy(baseProxy));
130
131 return proxy;
132}
133
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400134sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext* ctx,
135 const SkBitmap& bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400136 const GrSamplerState& params,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400137 SkScalar scaleAdjust[2]) {
138 // 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 -0400139 return GrBitmapTextureMaker(ctx, bitmap).refTextureProxyForParams(params, nullptr,
140 nullptr, scaleAdjust);
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400141}
142
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500143sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider* proxyProvider,
Greg Daniel7e1912a2018-02-08 09:15:33 -0500144 const SkBitmap& bitmap,
145 SkBackingFit fit) {
146 if (!bitmap.peekPixels(nullptr)) {
147 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500148 }
149
Greg Daniel7e1912a2018-02-08 09:15:33 -0500150 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
151 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
152 // 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 -0400153 SkCopyPixelsMode cpyMode = proxyProvider->recordingDDL() ? kIfMutable_SkCopyPixelsMode
154 : kNever_SkCopyPixelsMode;
Greg Daniel7e1912a2018-02-08 09:15:33 -0500155 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, cpyMode);
Robert Phillipse14d3052017-02-15 13:18:21 -0500156
Greg Daniel7e1912a2018-02-08 09:15:33 -0500157 if (!image) {
158 return nullptr;
Robert Phillipse14d3052017-02-15 13:18:21 -0500159 }
160
Greg Daniel7e1912a2018-02-08 09:15:33 -0500161 return GrMakeCachedImageProxy(proxyProvider, std::move(image), fit);
Robert Phillipse14d3052017-02-15 13:18:21 -0500162}
163
Robert Phillips7a926392018-02-01 15:49:54 -0500164static void create_unique_key_for_image(const SkImage* image, GrUniqueKey* result) {
165 if (!image) {
166 result->reset(); // will be invalid
167 return;
168 }
169
170 if (const SkBitmap* bm = as_IB(image)->onPeekBitmap()) {
Greg Daniela4211122018-03-14 19:09:36 +0000171 if (!bm->isVolatile()) {
172 SkIPoint origin = bm->pixelRefOrigin();
173 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bm->width(), bm->height());
174 GrMakeKeyFromImageID(result, bm->getGenerationID(), subset);
175 }
Robert Phillips7a926392018-02-01 15:49:54 -0500176 return;
177 }
178
179 GrMakeKeyFromImageID(result, image->uniqueID(), image->bounds());
180}
181
182sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider* proxyProvider,
Greg Daniel490695b2018-02-05 09:34:02 -0500183 sk_sp<SkImage> srcImage,
184 SkBackingFit fit) {
Robert Phillips7a926392018-02-01 15:49:54 -0500185 sk_sp<GrTextureProxy> proxy;
186 GrUniqueKey originalKey;
187
188 create_unique_key_for_image(srcImage.get(), &originalKey);
189
190 if (originalKey.isValid()) {
191 proxy = proxyProvider->findOrCreateProxyByUniqueKey(originalKey, kTopLeft_GrSurfaceOrigin);
192 }
193 if (!proxy) {
Greg Daniela4211122018-03-14 19:09:36 +0000194 proxy = proxyProvider->createTextureProxy(srcImage, kNone_GrSurfaceFlags, 1,
Brian Salomon58389b92018-03-07 13:01:25 -0500195 SkBudgeted::kYes, fit);
Robert Phillips7a926392018-02-01 15:49:54 -0500196 if (proxy && originalKey.isValid()) {
197 proxyProvider->assignUniqueKeyToProxy(originalKey, proxy.get());
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400198 const SkBitmap* bm = as_IB(srcImage.get())->onPeekBitmap();
199 // When recording DDLs we do not want to install change listeners because doing
200 // so isn't threadsafe.
201 if (bm && !proxyProvider->recordingDDL()) {
Greg Daniela4211122018-03-14 19:09:36 +0000202 GrInstallBitmapUniqueKeyInvalidator(originalKey, bm->pixelRef());
203 }
Robert Phillips7a926392018-02-01 15:49:54 -0500204 }
205 }
206
207 return proxy;
208}
209
rileya@google.com24f3ad12012-07-18 21:47:40 +0000210///////////////////////////////////////////////////////////////////////////////
211
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400212GrColor4f SkColorToPremulGrColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman72ae4312016-10-20 16:53:45 -0400213 // We want to premultiply after linearizing, so this is easy:
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400214 return SkColorToUnpremulGrColor4f(c, colorSpaceInfo).premul();
Brian Osman72ae4312016-10-20 16:53:45 -0400215}
216
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400217GrColor4f SkColorToPremulGrColor4fLegacy(SkColor c) {
218 return GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c)).premul();
Brian Osman72ae4312016-10-20 16:53:45 -0400219}
220
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400221GrColor4f SkColorToUnpremulGrColor4f(SkColor c, const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400222 GrColor4f color;
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400223 if (colorSpaceInfo.colorSpace()) {
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400224 // SkColor4f::FromColor does sRGB -> Linear
225 color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c));
226 } else {
227 // GrColor4f::FromGrColor just multiplies by 1/255
228 color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c));
229 }
230
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400231 if (auto* xform = colorSpaceInfo.colorSpaceXformFromSRGB()) {
232 color = xform->clampedXform(color);
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400233 }
234
235 return color;
236}
237
238///////////////////////////////////////////////////////////////////////////////
239
Brian Osman2b23c4b2018-06-01 12:25:08 -0400240GrPixelConfig SkColorType2GrPixelConfig(const SkColorType type) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400241 switch (type) {
brianosmanc571c002016-03-17 13:01:26 -0700242 case kUnknown_SkColorType:
243 return kUnknown_GrPixelConfig;
244 case kAlpha_8_SkColorType:
245 return kAlpha_8_GrPixelConfig;
246 case kRGB_565_SkColorType:
247 return kRGB_565_GrPixelConfig;
248 case kARGB_4444_SkColorType:
249 return kRGBA_4444_GrPixelConfig;
250 case kRGBA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400251 return kRGBA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500252 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400253 return kRGB_888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700254 case kBGRA_8888_SkColorType:
Brian Osman2b23c4b2018-06-01 12:25:08 -0400255 return kBGRA_8888_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500256 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500257 return kRGBA_1010102_GrPixelConfig;
Brian Salomone41e1762018-01-25 14:07:47 -0500258 case kRGB_101010x_SkColorType:
259 return kUnknown_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700260 case kGray_8_SkColorType:
Brian Osman986563b2017-01-10 14:20:02 -0500261 return kGray_8_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700262 case kRGBA_F16_SkColorType:
263 return kRGBA_half_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000264 }
265 SkASSERT(0); // shouldn't get here
266 return kUnknown_GrPixelConfig;
267}
268
Brian Osman2b23c4b2018-06-01 12:25:08 -0400269GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info) {
270 return SkColorType2GrPixelConfig(info.colorType());
Greg Daniel81e7bf82017-07-19 14:47:42 -0400271}
272
brianosman396fcdb2016-07-22 06:26:11 -0700273bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400274 SkColorType ct = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
275 if (kUnknown_SkColorType != ct) {
276 if (ctOut) {
277 *ctOut = ct;
278 }
279 return true;
reed@google.combf790232013-12-13 19:45:58 +0000280 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400281 return false;
reed@google.combf790232013-12-13 19:45:58 +0000282}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000283
brianosman27a3aa52016-09-23 07:11:34 -0700284GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* colorSpace) {
Matt Sarettf3880932017-03-24 10:06:03 -0400285 if (!colorSpace) {
brianosman2695eaa2016-09-21 06:45:09 -0700286 return kRGBA_8888_GrPixelConfig;
287 } else if (colorSpace->gammaIsLinear()) {
Mike Kleince4cf722018-05-10 11:29:15 -0400288 // TODO
brianosman2695eaa2016-09-21 06:45:09 -0700289 return kRGBA_half_GrPixelConfig;
290 } else if (colorSpace->gammaCloseToSRGB()) {
291 return kSRGBA_8888_GrPixelConfig;
292 } else {
293 SkDEBUGFAIL("No renderable config exists for color space with strange gamma");
294 return kUnknown_GrPixelConfig;
295 }
296}
297
bsalomonf1b7a1d2015-09-28 06:26:28 -0700298////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000299
Mike Reed185ba212017-04-28 12:31:05 -0400300static inline bool blend_requires_shader(const SkBlendMode mode) {
301 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700302}
303
bsalomonf1b7a1d2015-09-28 06:26:28 -0700304static inline bool skpaint_to_grpaint_impl(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400305 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700306 const SkPaint& skPaint,
307 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400308 std::unique_ptr<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400309 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700310 GrPaint* grPaint) {
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400311 // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion.
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400312 GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), colorSpaceInfo);
brianosmana4535a32016-06-24 12:50:19 -0700313
Mike Reed3bc266b2018-01-20 22:24:41 +0000314 const GrFPArgs fpArgs(context, &viewM, skPaint.getFilterQuality(), &colorSpaceInfo);
Mike Reedbfadcf02018-01-20 22:24:21 +0000315
bsalomonf1b7a1d2015-09-28 06:26:28 -0700316 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
317 // of per-vertex colors.
Brian Salomonaff329b2017-08-11 09:40:37 -0400318 std::unique_ptr<GrFragmentProcessor> shaderFP;
Mike Reed185ba212017-04-28 12:31:05 -0400319 if (!primColorMode || blend_requires_shader(*primColorMode)) {
bsalomonaa48d362015-10-01 08:34:17 -0700320 if (shaderProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400321 shaderFP = std::move(*shaderProcessor);
Florin Malita4aed1382017-05-25 10:38:07 -0400322 } else if (const auto* shader = as_SB(skPaint.getShader())) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000323 shaderFP = shader->asFragmentProcessor(fpArgs);
Yuqian Liaad2ec62018-02-26 10:34:52 -0500324 if (!shaderFP) {
325 return false;
326 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700327 }
328 }
329
330 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
331 // a known constant value. In that case we can simply apply a color filter during this
332 // conversion without converting the color filter to a GrFragmentProcessor.
333 bool applyColorFilterToPaintColor = false;
334 if (shaderFP) {
335 if (primColorMode) {
336 // There is a blend between the primitive color and the shader color. The shader sees
337 // the opaque paint color. The shader's output is blended using the provided mode by
338 // the primitive color. The blended color is then modulated by the paint's alpha.
339
340 // The geometry processor will insert the primitive color to start the color chain, so
341 // the GrPaint color will be ignored.
342
brianosman4cea3b92016-09-08 09:33:50 -0700343 GrColor4f shaderInput = origColor.opaque();
Brian Salomonaff329b2017-08-11 09:40:37 -0400344 shaderFP = GrFragmentProcessor::OverrideInput(std::move(shaderFP), shaderInput);
Mike Reed185ba212017-04-28 12:31:05 -0400345 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
346 *primColorMode);
347
bsalomonf1b7a1d2015-09-28 06:26:28 -0700348 // The above may return null if compose results in a pass through of the prim color.
349 if (shaderFP) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400350 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700351 }
352
brianosmana4535a32016-06-24 12:50:19 -0700353 // We can ignore origColor here - alpha is unchanged by gamma
bsalomonf1b7a1d2015-09-28 06:26:28 -0700354 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
355 if (GrColor_WHITE != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400356 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
357 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700358 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman618d3042016-10-25 10:51:28 -0400359 GrColor4f::FromGrColor(paintAlpha),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500360 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700361 }
362 } else {
363 // The shader's FP sees the paint unpremul color
brianosmana4535a32016-06-24 12:50:19 -0700364 grPaint->setColor4f(origColor);
bungeman06ca8ec2016-06-09 08:01:03 -0700365 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700366 }
367 } else {
368 if (primColorMode) {
369 // There is a blend between the primitive color and the paint color. The blend considers
370 // the opaque paint color. The paint's alpha is applied to the post-blended color.
Brian Salomonaff329b2017-08-11 09:40:37 -0400371 auto processor = GrConstColorProcessor::Make(origColor.opaque(),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500372 GrConstColorProcessor::InputMode::kIgnore);
Mike Reed185ba212017-04-28 12:31:05 -0400373 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
374 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700375 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700376 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700377 }
378
brianosmana4535a32016-06-24 12:50:19 -0700379 grPaint->setColor4f(origColor.opaque());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700380
brianosmana4535a32016-06-24 12:50:19 -0700381 // We can ignore origColor here - alpha is unchanged by gamma
bsalomonf1b7a1d2015-09-28 06:26:28 -0700382 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700383 if (GrColor_WHITE != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400384 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
385 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700386 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman618d3042016-10-25 10:51:28 -0400387 GrColor4f::FromGrColor(paintAlpha),
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500388 GrConstColorProcessor::InputMode::kModulateRGBA));
bsalomonaa48d362015-10-01 08:34:17 -0700389 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700390 } else {
391 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700392 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700393 applyColorFilterToPaintColor = true;
394 }
395 }
396
397 SkColorFilter* colorFilter = skPaint.getColorFilter();
398 if (colorFilter) {
399 if (applyColorFilterToPaintColor) {
Brian Osman8bf4e672016-10-17 16:54:49 -0400400 // If we're in legacy mode, we *must* avoid using the 4f version of the color filter,
401 // because that will combine with the linearized version of the stored color.
Brian Salomonf3569f02017-10-24 12:52:33 -0400402 if (colorSpaceInfo.isGammaCorrect()) {
Brian Osman8bf4e672016-10-17 16:54:49 -0400403 grPaint->setColor4f(GrColor4f::FromSkColor4f(
404 colorFilter->filterColor4f(origColor.toSkColor4f())).premul());
405 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400406 grPaint->setColor4f(SkColorToPremulGrColor4fLegacy(
407 colorFilter->filterColor(skPaint.getColor())));
Brian Osman8bf4e672016-10-17 16:54:49 -0400408 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700409 } else {
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400410 auto cfFP = colorFilter->asFragmentProcessor(context, colorSpaceInfo);
bsalomone25eea42015-09-29 06:38:55 -0700411 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700412 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700413 } else {
414 return false;
415 }
416 }
417 }
418
Mike Reed80747ef2018-01-23 15:29:32 -0500419 SkMaskFilterBase* maskFilter = as_MFB(skPaint.getMaskFilter());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400420 if (maskFilter) {
Mike Reedbfadcf02018-01-20 22:24:21 +0000421 if (auto mfFP = maskFilter->asFragmentProcessor(fpArgs)) {
422 grPaint->addCoverageFragmentProcessor(std::move(mfFP));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400423 }
424 }
425
robertphillips4f037942016-02-09 05:09:27 -0800426 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
427 // the GrPaint to also be null (also kSrcOver).
428 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700429 if (!skPaint.isSrcOver()) {
430 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800431 }
mtklein775b8192014-12-02 09:11:25 -0800432
krajcevskif461a8f2014-06-19 14:14:06 -0700433#ifndef SK_IGNORE_GPU_DITHER
Florin Malitad4a70ee2017-06-19 10:21:43 -0400434 // Conservative default, in case GrPixelConfigToColorType() fails.
435 SkColorType ct = SkColorType::kRGB_565_SkColorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400436 GrPixelConfigToColorType(colorSpaceInfo.config(), &ct);
437 if (SkPaintPriv::ShouldDither(skPaint, ct) && grPaint->numColorFragmentProcessors() > 0 &&
438 !colorSpaceInfo.isGammaCorrect()) {
439 auto ditherFP = GrDitherEffect::Make(colorSpaceInfo.config());
Brian Salomon0c15ae82017-07-19 15:39:56 +0000440 if (ditherFP) {
441 grPaint->addColorFragmentProcessor(std::move(ditherFP));
442 }
krajcevskif461a8f2014-06-19 14:14:06 -0700443 }
444#endif
bsalomonbed83a62015-04-15 14:18:34 -0700445 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000446}
447
Brian Salomonf3569f02017-10-24 12:52:33 -0400448bool SkPaintToGrPaint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
449 const SkPaint& skPaint, const SkMatrix& viewM, GrPaint* grPaint) {
450 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, nullptr,
451 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700452}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000453
bsalomonf1b7a1d2015-09-28 06:26:28 -0700454/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
455bool SkPaintToGrPaintReplaceShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400456 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700457 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400458 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700459 GrPaint* grPaint) {
460 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700461 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000462 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400463 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &shaderFP,
464 nullptr, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000465}
reed8b26b992015-05-07 15:36:17 -0700466
bsalomonf1b7a1d2015-09-28 06:26:28 -0700467/** Ignores the SkShader (if any) on skPaint. */
468bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400469 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700470 const SkPaint& skPaint,
471 GrPaint* grPaint) {
472 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
Robert Phillipsd3b37a12018-03-27 09:53:35 -0400473 std::unique_ptr<GrFragmentProcessor> nullShaderFP(nullptr);
474 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, SkMatrix::I(), &nullShaderFP,
Brian Salomonf3569f02017-10-24 12:52:33 -0400475 nullptr, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700476}
477
478/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400479be setup as a vertex attribute using the specified SkBlendMode. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700480bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400481 const GrColorSpaceInfo& colorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700482 const SkPaint& skPaint,
483 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400484 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700485 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400486 return skpaint_to_grpaint_impl(context, colorSpaceInfo, skPaint, viewM, nullptr, &primColorMode,
Mike Reed185ba212017-04-28 12:31:05 -0400487 grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700488}
489
joshualitt33a5fce2015-11-18 13:28:51 -0800490bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400491 const GrColorSpaceInfo& colorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800492 const SkPaint& paint,
493 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400494 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800495 bool textureIsAlphaOnly,
496 GrPaint* grPaint) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400497 std::unique_ptr<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800498 if (textureIsAlphaOnly) {
Florin Malita4aed1382017-05-25 10:38:07 -0400499 if (const auto* shader = as_SB(paint.getShader())) {
Mike Reede3429e62018-01-19 11:43:34 -0500500 shaderFP = shader->asFragmentProcessor(GrFPArgs(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400501 context, &viewM, paint.getFilterQuality(), &colorSpaceInfo));
joshualitt33a5fce2015-11-18 13:28:51 -0800502 if (!shaderFP) {
503 return false;
504 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400505 std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
bungeman06ca8ec2016-06-09 08:01:03 -0700506 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800507 } else {
Brian Salomonaff329b2017-08-11 09:40:37 -0400508 shaderFP = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800509 }
510 } else {
Mike Reed28eaed22018-02-01 11:24:53 -0500511 shaderFP = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
joshualitt33a5fce2015-11-18 13:28:51 -0800512 }
513
Brian Salomonf3569f02017-10-24 12:52:33 -0400514 return SkPaintToGrPaintReplaceShader(context, colorSpaceInfo, paint, std::move(shaderFP),
515 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800516}
517
bsalomonf1b7a1d2015-09-28 06:26:28 -0700518
519////////////////////////////////////////////////////////////////////////////////////////////////
520
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400521GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
522 const SkMatrix& viewM,
523 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500524 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400525 bool* doBicubic) {
joshualitt9bc39542015-08-12 12:57:54 -0700526 *doBicubic = false;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400527 GrSamplerState::Filter textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700528 switch (paintFilterQuality) {
529 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400530 textureFilterMode = GrSamplerState::Filter::kNearest;
joshualitt9bc39542015-08-12 12:57:54 -0700531 break;
532 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400533 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700534 break;
535 case kMedium_SkFilterQuality: {
536 SkMatrix matrix;
537 matrix.setConcat(viewM, localM);
Brian Osmandb78cba2018-02-15 10:09:48 -0500538 // With sharp mips, we bias lookups by -0.5. That means our final LOD is >= 0 until the
539 // computed LOD is >= 0.5. At what scale factor does a texture get an LOD of 0.5?
540 //
541 // Want: 0 = log2(1/s) - 0.5
542 // 0.5 = log2(1/s)
543 // 2^0.5 = 1/s
544 // 1/2^0.5 = s
545 // 2^0.5/2 = s
546 SkScalar mipScale = sharpenMipmappedTextures ? SK_ScalarRoot2Over2 : SK_Scalar1;
547 if (matrix.getMinScale() < mipScale) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400548 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700549 } else {
550 // Don't trigger MIP level generation unnecessarily.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400551 textureFilterMode = GrSamplerState::Filter::kBilerp;
joshualitt9bc39542015-08-12 12:57:54 -0700552 }
553 break;
554 }
555 case kHigh_SkFilterQuality: {
556 SkMatrix matrix;
557 matrix.setConcat(viewM, localM);
558 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
559 break;
560 }
561 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400562 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400563 textureFilterMode = GrSamplerState::Filter::kMipMap;
joshualitt9bc39542015-08-12 12:57:54 -0700564 break;
565
566 }
567 return textureFilterMode;
568}