blob: d1cecde1899c5345ca65433ed37014865046d368 [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
reedb5d32632015-09-29 13:36:50 -07008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "SkGr.h"
bsalomon0d996862016-03-09 18:44:43 -080010#include "SkGrPriv.h"
egdaniel378092f2014-12-03 10:40:13 -080011
Brian Osman3b66ab62016-11-28 09:26:31 -050012#include "GrBitmapTextureMaker.h"
bsalomon76228632015-05-29 08:02:10 -070013#include "GrCaps.h"
bsalomonf276ac52015-10-09 13:36:42 -070014#include "GrContext.h"
bsalomon045802d2015-10-20 07:58:01 -070015#include "GrGpuResourcePriv.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050016#include "GrRenderTargetContext.h"
brianosmanfe199872016-06-13 07:59:48 -070017#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080018#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080019#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070020#include "GrYUVProvider.h"
21
reed374772b2016-10-05 17:33:02 -070022#include "SkBlendModePriv.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000023#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000024#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070025#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070026#include "SkData.h"
Robert Phillipsa29a9562016-10-20 09:40:55 -040027#include "SkMaskFilter.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000028#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080029#include "SkMipMap.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000030#include "SkPixelRef.h"
brianosmana4535a32016-06-24 12:50:19 -070031#include "SkPM4fPriv.h"
sugoi692135f2015-01-19 10:10:27 -080032#include "SkResourceCache.h"
cblume55f2d2d2016-02-26 13:20:48 -080033#include "SkTemplates.h"
sugoi692135f2015-01-19 10:10:27 -080034#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070035#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070036#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070037#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080038#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070039#include "effects/GrXfermodeFragmentProcessor.h"
bsalomonf267c1e2016-02-01 13:16:14 -080040#include "effects/GrYUVEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070041
krajcevski8c111f72014-06-02 13:51:34 -070042#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -070043# include "etc1.h"
44#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000045
brianosmana6359362016-03-21 06:55:37 -070046GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) {
bsalomon466c2c42015-10-16 12:01:18 -070047 GrSurfaceDesc desc;
48 desc.fFlags = kNone_GrSurfaceFlags;
49 desc.fWidth = info.width();
50 desc.fHeight = info.height();
brianosmana6359362016-03-21 06:55:37 -070051 desc.fConfig = SkImageInfo2GrPixelConfig(info, caps);
bsalomon466c2c42015-10-16 12:01:18 -070052 desc.fSampleCnt = 0;
53 return desc;
54}
55
bsalomon045802d2015-10-20 07:58:01 -070056void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
57 SkASSERT(key);
58 SkASSERT(imageID);
59 SkASSERT(!imageBounds.isEmpty());
60 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
61 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
bsalomon466c2c42015-10-16 12:01:18 -070062 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070063 builder[1] = imageBounds.fLeft;
64 builder[2] = imageBounds.fTop;
65 builder[3] = imageBounds.fRight;
66 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070067}
68
69GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
70 int expectedW, int expectedH,
71 const void** outStartOfDataToUpload) {
72 *outStartOfDataToUpload = nullptr;
73#ifndef SK_IGNORE_ETC1_SUPPORT
74 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
75 return kUnknown_GrPixelConfig;
76 }
77
78 const uint8_t* bytes = data->bytes();
79 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
80 // Does the data match the dimensions of the bitmap? If not,
81 // then we don't know how to scale the image to match it...
82 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
83 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
84 {
85 return kUnknown_GrPixelConfig;
86 }
87
88 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
89 return kETC1_GrPixelConfig;
bsalomon466c2c42015-10-16 12:01:18 -070090 }
91#endif
92 return kUnknown_GrPixelConfig;
93}
94
bsalomon045802d2015-10-20 07:58:01 -070095//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +000096
bsalomon045802d2015-10-20 07:58:01 -070097/**
98 * Fill out buffer with the compressed format Ganesh expects from a colortable
99 * based bitmap. [palette (colortable) + indices].
100 *
101 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
102 * we could detect that the colortable.count is <= 16, and then repack the
103 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
104 * slower than memcpy), so skipping that for now.
105 *
106 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
107 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 */
bsalomon0d996862016-03-09 18:44:43 -0800109static void build_index8_data(void* buffer, const SkPixmap& pixmap) {
110 SkASSERT(kIndex_8_SkColorType == pixmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000111
bsalomon0d996862016-03-09 18:44:43 -0800112 const SkColorTable* ctable = pixmap.ctable();
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000114
reed@google.com7111d462014-03-25 16:20:24 +0000115 const int count = ctable->count();
116
117 SkDstPixelInfo dstPI;
118 dstPI.fColorType = kRGBA_8888_SkColorType;
119 dstPI.fAlphaType = kPremul_SkAlphaType;
120 dstPI.fPixels = buffer;
121 dstPI.fRowBytes = count * sizeof(SkPMColor);
122
123 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000124 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000125 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800126 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000127 srcPI.fRowBytes = count * sizeof(SkPMColor);
128
129 srcPI.convertPixelsTo(&dstPI, count, 1);
130
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700132 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon0d996862016-03-09 18:44:43 -0800134 if ((unsigned)pixmap.width() == pixmap.rowBytes()) {
135 memcpy(dst, pixmap.addr(), pixmap.getSafeSize());
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 } else {
137 // need to trim off the extra bytes per row
bsalomon0d996862016-03-09 18:44:43 -0800138 size_t width = pixmap.width();
139 size_t rowBytes = pixmap.rowBytes();
140 const uint8_t* src = pixmap.addr8();
141 for (int y = 0; y < pixmap.height(); y++) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 memcpy(dst, src, width);
143 src += rowBytes;
144 dst += width;
145 }
146 }
147}
148
bsalomon045802d2015-10-20 07:58:01 -0700149/**
reed43fe6182015-09-08 08:37:36 -0700150 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
151 * be gone from SkPixelRef, and we can remove this subclass entirely.
152 */
153class PixelRef_GrYUVProvider : public GrYUVProvider {
154 SkPixelRef* fPR;
155
156public:
157 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
158
159 uint32_t onGetID() override { return fPR->getGenerationID(); }
msarett4984c3c2016-03-10 05:44:43 -0800160 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
161 return fPR->queryYUV8(sizeInfo, colorSpace);
reed43fe6182015-09-08 08:37:36 -0700162 }
msarett4984c3c2016-03-10 05:44:43 -0800163 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
164 return fPR->getYUV8Planes(sizeInfo, planes);
reed43fe6182015-09-08 08:37:36 -0700165 }
166};
krajcevski9c0e6292014-06-02 07:38:14 -0700167
robertphillips677da9d2016-05-11 05:15:55 -0700168static sk_sp<GrTexture> create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
169 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700170 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700171 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700172 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700173 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800174 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700175 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700176 }
177
reed43fe6182015-09-08 08:37:36 -0700178 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700179
bsalomon045802d2015-10-20 07:58:01 -0700180 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700181}
182
bsalomon0d996862016-03-09 18:44:43 -0800183GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) {
brianosmana6359362016-03-21 06:55:37 -0700184 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
bsalomon045802d2015-10-20 07:58:01 -0700185
robertphillips677da9d2016-05-11 05:15:55 -0700186 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
187 if (texture) {
188 return texture.release();
bsalomon0d996862016-03-09 18:44:43 -0800189 }
190
191 SkAutoLockPixels alp(bitmap);
192 if (!bitmap.readyToDraw()) {
193 return nullptr;
194 }
195 SkPixmap pixmap;
196 if (!bitmap.peekPixels(&pixmap)) {
197 return nullptr;
198 }
ericrk8bea8902016-03-18 11:52:20 -0700199 return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes);
bsalomon0d996862016-03-09 18:44:43 -0800200}
201
ericrk8bea8902016-03-18 11:52:20 -0700202GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800203 const SkPixmap* pmap = &pixmap;
204 SkPixmap tmpPixmap;
bsalomon045802d2015-10-20 07:58:01 -0700205 SkBitmap tmpBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000206
bsalomon76228632015-05-29 08:02:10 -0700207 const GrCaps* caps = ctx->caps();
brianosmana6359362016-03-21 06:55:37 -0700208 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000209
brianosmanefded512016-07-26 08:11:50 -0700210 if (caps->srgbSupport() &&
211 pixmap.info().colorSpace() && pixmap.info().colorSpace()->gammaCloseToSRGB() &&
212 !(GrPixelConfigIsSRGB(desc.fConfig) ||
213 kRGBA_half_GrPixelConfig == desc.fConfig ||
214 kRGBA_float_GrPixelConfig == desc.fConfig)) {
brianosmanb109b8c2016-06-16 13:03:24 -0700215 // We were supplied an sRGB-like color space, but we don't have a suitable pixel config.
brianosmana6359362016-03-21 06:55:37 -0700216 // Convert to 8888 sRGB so we can handle the data correctly. The raster backend doesn't
217 // handle sRGB Index8 -> sRGB 8888 correctly (yet), so lie about both the source and
218 // destination (claim they're linear):
219 SkImageInfo linSrcInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
220 pixmap.colorType(), pixmap.alphaType());
221 SkPixmap linSrcPixmap(linSrcInfo, pixmap.addr(), pixmap.rowBytes(), pixmap.ctable());
222
brianosmanb109b8c2016-06-16 13:03:24 -0700223 SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
224 kN32_SkColorType, kPremul_SkAlphaType,
225 sk_ref_sp(pixmap.info().colorSpace()));
226
brianosmana6359362016-03-21 06:55:37 -0700227 tmpBitmap.allocPixels(dstInfo);
228
229 SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
230 if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
231 return nullptr;
232 }
233 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
234 return nullptr;
235 }
236 pmap = &tmpPixmap;
237 // must rebuild desc, since we've forced the info to be N32
238 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
brianosman85f92692016-03-24 06:56:32 -0700239 } else if (kGray_8_SkColorType == pixmap.colorType()) {
240 // We don't have Gray8 support as a pixel config, so expand to 8888
241
242 // We should have converted sRGB Gray8 above (if we have sRGB support):
brianosmanb109b8c2016-06-16 13:03:24 -0700243 SkASSERT(!caps->srgbSupport() || !pixmap.info().colorSpace() ||
244 !pixmap.info().colorSpace()->gammaCloseToSRGB());
brianosman85f92692016-03-24 06:56:32 -0700245
246 SkImageInfo info = SkImageInfo::MakeN32(pixmap.width(), pixmap.height(),
247 kOpaque_SkAlphaType);
248 tmpBitmap.allocPixels(info);
249 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
250 return nullptr;
251 }
252 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
253 return nullptr;
254 }
255 pmap = &tmpPixmap;
256 // must rebuild desc, since we've forced the info to be N32
257 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
brianosmana6359362016-03-21 06:55:37 -0700258 } else if (kIndex_8_SkColorType == pixmap.colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700259 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700260 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
bsalomon0d996862016-03-09 18:44:43 -0800261 pixmap.width(), pixmap.height());
bsalomond4cb9222014-08-11 14:19:09 -0700262 SkAutoMalloc storage(imageSize);
bsalomon0d996862016-03-09 18:44:43 -0800263 build_index8_data(storage.get(), pixmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000264
265 // our compressed data will be trimmed, so pass width() for its
266 // "rowBytes", since they are the same now.
ericrk8bea8902016-03-18 11:52:20 -0700267 return ctx->textureProvider()->createTexture(desc, budgeted, storage.get(),
bsalomon0d996862016-03-09 18:44:43 -0800268 pixmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 } else {
bsalomon0d996862016-03-09 18:44:43 -0800270 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
271 tmpBitmap.allocPixels(info);
272 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
273 return nullptr;
274 }
275 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
276 return nullptr;
277 }
278 pmap = &tmpPixmap;
279 // must rebuild desc, since we've forced the info to be N32
brianosmana6359362016-03-21 06:55:37 -0700280 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
krajcevski9c0e6292014-06-02 07:38:14 -0700281 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000282 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000283
ericrk8bea8902016-03-18 11:52:20 -0700284 return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(),
bsalomon0d996862016-03-09 18:44:43 -0800285 pmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800286}
287
bsalomonb4d40ef2015-07-15 10:12:16 -0700288
bsalomon045802d2015-10-20 07:58:01 -0700289////////////////////////////////////////////////////////////////////////////////
290
bsalomonc55271f2015-11-09 11:55:57 -0800291void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700292 class Invalidator : public SkPixelRef::GenIDChangeListener {
293 public:
294 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
295 private:
296 GrUniqueKeyInvalidatedMessage fMsg;
297
298 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
299 };
300
301 pixelRef->addGenIDChangeListener(new Invalidator(key));
302}
303
brianosman982eb7f2016-06-06 13:10:58 -0700304GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap,
Brian Osman61624f02016-12-09 14:51:59 -0500305 SkColorSpace* dstColorSpace)
cblume55f2d2d2016-02-26 13:20:48 -0800306{
Brian Osman61624f02016-12-09 14:51:59 -0500307 SkDestinationSurfaceColorMode colorMode = dstColorSpace
308 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
309 : SkDestinationSurfaceColorMode::kLegacy;
310
brianosmana6359362016-03-21 06:55:37 -0700311 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
robertphillips677da9d2016-05-11 05:15:55 -0700312 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
cblume55f2d2d2016-02-26 13:20:48 -0800313 if (texture) {
robertphillips677da9d2016-05-11 05:15:55 -0700314 return texture.release();
cblume55f2d2d2016-02-26 13:20:48 -0800315 }
316
brianosman7e5e5942016-06-21 12:08:24 -0700317 // We don't support Gray8 directly in the GL backend, so fail-over to GrUploadBitmapToTexture.
318 // That will transform the Gray8 to 8888, then use the driver/GPU to build mipmaps. If we build
319 // the mips on the CPU here, they'll all be Gray8, which isn't useful. (They get treated as A8).
320 // TODO: A better option might be to transform the initial bitmap here to 8888, then run the
321 // CPU mip-mapper on that data before uploading. This is much less code for a rare case though:
322 if (kGray_8_SkColorType == bitmap.colorType()) {
323 return nullptr;
324 }
325
cblume55f2d2d2016-02-26 13:20:48 -0800326 SkASSERT(sizeof(int) <= sizeof(uint32_t));
327 if (bitmap.width() < 0 || bitmap.height() < 0) {
328 return nullptr;
329 }
330
331 SkAutoPixmapUnlock srcUnlocker;
332 if (!bitmap.requestLock(&srcUnlocker)) {
333 return nullptr;
334 }
335 const SkPixmap& pixmap = srcUnlocker.pixmap();
336 // Try to catch where we might have returned nullptr for src crbug.com/492818
337 if (nullptr == pixmap.addr()) {
338 sk_throw();
339 }
340
Brian Osman7b8400d2016-11-08 17:08:54 -0500341 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume55f2d2d2016-02-26 13:20:48 -0800342 if (!mipmaps) {
343 return nullptr;
344 }
345
346 const int mipLevelCount = mipmaps->countLevels() + 1;
347 if (mipLevelCount < 1) {
348 return nullptr;
349 }
350
351 const bool isMipMapped = mipLevelCount > 1;
352 desc.fIsMipMapped = isMipMapped;
353
Ben Wagner7ecc5962016-11-02 17:07:33 -0400354 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume55f2d2d2016-02-26 13:20:48 -0800355
356 texels[0].fPixels = pixmap.addr();
357 texels[0].fRowBytes = pixmap.rowBytes();
358
359 for (int i = 1; i < mipLevelCount; ++i) {
360 SkMipMap::Level generatedMipLevel;
361 mipmaps->getLevel(i - 1, &generatedMipLevel);
362 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
363 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
364 }
365
brianosmanfe199872016-06-13 07:59:48 -0700366 {
367 GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc,
368 SkBudgeted::kYes,
369 texels.get(),
370 mipLevelCount);
brianosman7d2f6072016-06-17 07:03:45 -0700371 if (texture) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500372 texture->texturePriv().setMipColorMode(colorMode);
brianosman7d2f6072016-06-17 07:03:45 -0700373 }
brianosmanfe199872016-06-13 07:59:48 -0700374 return texture;
375 }
cblume55f2d2d2016-02-26 13:20:48 -0800376}
377
cblume186d2d42016-06-03 11:17:42 -0700378GrTexture* GrUploadMipMapToTexture(GrContext* ctx, const SkImageInfo& info,
379 const GrMipLevel* texels, int mipLevelCount) {
380 const GrCaps* caps = ctx->caps();
381 return ctx->textureProvider()->createMipMappedTexture(GrImageInfoToSurfaceDesc(info, *caps),
382 SkBudgeted::kYes, texels,
383 mipLevelCount);
384}
385
reedb5d32632015-09-29 13:36:50 -0700386GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
Brian Osman61624f02016-12-09 14:51:59 -0500387 const GrSamplerParams& params) {
Brian Osman7992da32016-11-18 11:28:24 -0500388 // Caller doesn't care about the texture's color space (they can always get it from the bitmap)
Brian Osman61624f02016-12-09 14:51:59 -0500389 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, nullptr, nullptr);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000390}
reed8f343722015-08-13 13:32:39 -0700391
reedc7ec7c92016-07-25 08:29:10 -0700392sk_sp<GrTexture> GrMakeCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
Brian Osman61624f02016-12-09 14:51:59 -0500393 const GrSamplerParams& params) {
Brian Osman7992da32016-11-18 11:28:24 -0500394 // Caller doesn't care about the texture's color space (they can always get it from the bitmap)
Brian Osman61624f02016-12-09 14:51:59 -0500395 GrTexture* tex = GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, nullptr,
Brian Osman7992da32016-11-18 11:28:24 -0500396 nullptr);
reedc7ec7c92016-07-25 08:29:10 -0700397 return sk_sp<GrTexture>(tex);
398}
399
rileya@google.com24f3ad12012-07-18 21:47:40 +0000400///////////////////////////////////////////////////////////////////////////////
401
Brian Osman72ae4312016-10-20 16:53:45 -0400402GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) {
403 // We want to premultiply after linearizing, so this is easy:
404 return SkColorToUnpremulGrColor4f(c, dstColorSpace).premul();
405}
406
407GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) {
408 if (dstColorSpace) {
Brian Osman526972e2016-10-24 09:24:02 -0400409 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
Brian Osman72ae4312016-10-20 16:53:45 -0400410 auto gamutXform = GrColorSpaceXform::Make(srgbColorSpace.get(), dstColorSpace);
411 return SkColorToUnpremulGrColor4f(c, true, gamutXform.get());
412 } else {
413 return SkColorToUnpremulGrColor4f(c, false, nullptr);
414 }
415}
416
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400417GrColor4f SkColorToPremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
418 // We want to premultiply after linearizing, so this is easy:
419 return SkColorToUnpremulGrColor4f(c, gammaCorrect, gamutXform).premul();
420}
421
422GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
423 // You can't be color-space aware in legacy mode
424 SkASSERT(gammaCorrect || !gamutXform);
425
426 GrColor4f color;
427 if (gammaCorrect) {
428 // SkColor4f::FromColor does sRGB -> Linear
429 color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c));
430 } else {
431 // GrColor4f::FromGrColor just multiplies by 1/255
432 color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c));
433 }
434
435 if (gamutXform) {
436 color = gamutXform->apply(color);
437 }
438
439 return color;
440}
441
442///////////////////////////////////////////////////////////////////////////////
443
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000444// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
445// alpha info, that will be considered.
brianosmanb109b8c2016-06-16 13:03:24 -0700446GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, const SkColorSpace* cs,
brianosmana6359362016-03-21 06:55:37 -0700447 const GrCaps& caps) {
448 // We intentionally ignore profile type for non-8888 formats. Anything we can't support
449 // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture.
brianosmanc571c002016-03-17 13:01:26 -0700450 switch (ct) {
451 case kUnknown_SkColorType:
452 return kUnknown_GrPixelConfig;
453 case kAlpha_8_SkColorType:
454 return kAlpha_8_GrPixelConfig;
455 case kRGB_565_SkColorType:
456 return kRGB_565_GrPixelConfig;
457 case kARGB_4444_SkColorType:
458 return kRGBA_4444_GrPixelConfig;
459 case kRGBA_8888_SkColorType:
brianosmanb109b8c2016-06-16 13:03:24 -0700460 return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB())
brianosmana6359362016-03-21 06:55:37 -0700461 ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700462 case kBGRA_8888_SkColorType:
brianosmanb109b8c2016-06-16 13:03:24 -0700463 return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB())
brianosmana6359362016-03-21 06:55:37 -0700464 ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700465 case kIndex_8_SkColorType:
466 return kIndex_8_GrPixelConfig;
467 case kGray_8_SkColorType:
468 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
469 case kRGBA_F16_SkColorType:
470 return kRGBA_half_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000471 }
472 SkASSERT(0); // shouldn't get here
473 return kUnknown_GrPixelConfig;
474}
475
brianosman396fcdb2016-07-22 06:26:11 -0700476bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) {
reed@google.combf790232013-12-13 19:45:58 +0000477 SkColorType ct;
478 switch (config) {
479 case kAlpha_8_GrPixelConfig:
480 ct = kAlpha_8_SkColorType;
481 break;
482 case kIndex_8_GrPixelConfig:
483 ct = kIndex_8_SkColorType;
484 break;
485 case kRGB_565_GrPixelConfig:
486 ct = kRGB_565_SkColorType;
487 break;
488 case kRGBA_4444_GrPixelConfig:
489 ct = kARGB_4444_SkColorType;
490 break;
491 case kRGBA_8888_GrPixelConfig:
492 ct = kRGBA_8888_SkColorType;
493 break;
494 case kBGRA_8888_GrPixelConfig:
495 ct = kBGRA_8888_SkColorType;
496 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800497 case kSRGBA_8888_GrPixelConfig:
498 ct = kRGBA_8888_SkColorType;
jvanverthfa1e8a72014-12-22 08:31:49 -0800499 break;
brianosmana6359362016-03-21 06:55:37 -0700500 case kSBGRA_8888_GrPixelConfig:
501 ct = kBGRA_8888_SkColorType;
brianosmana6359362016-03-21 06:55:37 -0700502 break;
brianosman9ac5b912016-04-12 13:49:53 -0700503 case kRGBA_half_GrPixelConfig:
504 ct = kRGBA_F16_SkColorType;
505 break;
reed@google.combf790232013-12-13 19:45:58 +0000506 default:
507 return false;
508 }
509 if (ctOut) {
510 *ctOut = ct;
511 }
512 return true;
513}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000514
brianosman27a3aa52016-09-23 07:11:34 -0700515GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* colorSpace) {
brianosman2695eaa2016-09-21 06:45:09 -0700516 if (!colorSpace) {
517 return kRGBA_8888_GrPixelConfig;
518 } else if (colorSpace->gammaIsLinear()) {
519 return kRGBA_half_GrPixelConfig;
520 } else if (colorSpace->gammaCloseToSRGB()) {
521 return kSRGBA_8888_GrPixelConfig;
522 } else {
523 SkDEBUGFAIL("No renderable config exists for color space with strange gamma");
524 return kUnknown_GrPixelConfig;
525 }
526}
527
bsalomonf1b7a1d2015-09-28 06:26:28 -0700528////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000529
Mike Reed7d954ad2016-10-28 15:42:34 -0400530static inline bool blend_requires_shader(const SkBlendMode mode, bool primitiveIsSrc) {
bsalomonaa48d362015-10-01 08:34:17 -0700531 if (primitiveIsSrc) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400532 return SkBlendMode::kSrc != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700533 } else {
Mike Reed7d954ad2016-10-28 15:42:34 -0400534 return SkBlendMode::kDst != mode;
bsalomonaa48d362015-10-01 08:34:17 -0700535 }
536}
537
bsalomonf1b7a1d2015-09-28 06:26:28 -0700538static inline bool skpaint_to_grpaint_impl(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400539 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700540 const SkPaint& skPaint,
541 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -0700542 sk_sp<GrFragmentProcessor>* shaderProcessor,
Mike Reed7d954ad2016-10-28 15:42:34 -0400543 SkBlendMode* primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700544 bool primitiveIsSrc,
545 GrPaint* grPaint) {
Brian Osman11052242016-10-27 14:47:55 -0400546 grPaint->setAllowSRGBInputs(rtc->isGammaCorrect());
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000547
Brian Osmanc68d4aa2016-09-30 11:41:59 -0400548 // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion.
Brian Osman11052242016-10-27 14:47:55 -0400549 GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), rtc->isGammaCorrect(),
550 rtc->getColorXformFromSRGB());
brianosmana4535a32016-06-24 12:50:19 -0700551
bsalomonf1b7a1d2015-09-28 06:26:28 -0700552 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
553 // of per-vertex colors.
bungeman06ca8ec2016-06-09 08:01:03 -0700554 sk_sp<GrFragmentProcessor> shaderFP;
bsalomonaa48d362015-10-01 08:34:17 -0700555 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
556 if (shaderProcessor) {
557 shaderFP = *shaderProcessor;
558 } else if (const SkShader* shader = skPaint.getShader()) {
brianosman839345d2016-07-22 11:04:53 -0700559 shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context, &viewM, nullptr,
560 skPaint.getFilterQuality(),
Brian Osman61624f02016-12-09 14:51:59 -0500561 rtc->getColorSpace()));
bsalomonaa48d362015-10-01 08:34:17 -0700562 if (!shaderFP) {
563 return false;
564 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700565 }
566 }
567
568 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
569 // a known constant value. In that case we can simply apply a color filter during this
570 // conversion without converting the color filter to a GrFragmentProcessor.
571 bool applyColorFilterToPaintColor = false;
572 if (shaderFP) {
573 if (primColorMode) {
574 // There is a blend between the primitive color and the shader color. The shader sees
575 // the opaque paint color. The shader's output is blended using the provided mode by
576 // the primitive color. The blended color is then modulated by the paint's alpha.
577
578 // The geometry processor will insert the primitive color to start the color chain, so
579 // the GrPaint color will be ignored.
580
brianosman4cea3b92016-09-08 09:33:50 -0700581 GrColor4f shaderInput = origColor.opaque();
bsalomonf1b7a1d2015-09-28 06:26:28 -0700582 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700583 if (primitiveIsSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700584 shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP),
585 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700586 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700587 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
588 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700589 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700590 // The above may return null if compose results in a pass through of the prim color.
591 if (shaderFP) {
592 grPaint->addColorFragmentProcessor(shaderFP);
593 }
594
brianosmana4535a32016-06-24 12:50:19 -0700595 // We can ignore origColor here - alpha is unchanged by gamma
bsalomonf1b7a1d2015-09-28 06:26:28 -0700596 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
597 if (GrColor_WHITE != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400598 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
599 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700600 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman618d3042016-10-25 10:51:28 -0400601 GrColor4f::FromGrColor(paintAlpha),
602 GrConstColorProcessor::kModulateRGBA_InputMode));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700603 }
604 } else {
605 // The shader's FP sees the paint unpremul color
brianosmana4535a32016-06-24 12:50:19 -0700606 grPaint->setColor4f(origColor);
bungeman06ca8ec2016-06-09 08:01:03 -0700607 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700608 }
609 } else {
610 if (primColorMode) {
611 // There is a blend between the primitive color and the paint color. The blend considers
612 // the opaque paint color. The paint's alpha is applied to the post-blended color.
bungeman06ca8ec2016-06-09 08:01:03 -0700613 sk_sp<GrFragmentProcessor> processor(
Brian Osman618d3042016-10-25 10:51:28 -0400614 GrConstColorProcessor::Make(origColor.opaque(),
615 GrConstColorProcessor::kIgnore_InputMode));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700616 if (primitiveIsSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700617 processor = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(processor),
618 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700619 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700620 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
621 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700622 }
623 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700624 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700625 }
626
brianosmana4535a32016-06-24 12:50:19 -0700627 grPaint->setColor4f(origColor.opaque());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700628
brianosmana4535a32016-06-24 12:50:19 -0700629 // We can ignore origColor here - alpha is unchanged by gamma
bsalomonf1b7a1d2015-09-28 06:26:28 -0700630 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700631 if (GrColor_WHITE != paintAlpha) {
Brian Osman618d3042016-10-25 10:51:28 -0400632 // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all
633 // color channels. It's value should be treated as the same in ANY color space.
bungeman06ca8ec2016-06-09 08:01:03 -0700634 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
Brian Osman618d3042016-10-25 10:51:28 -0400635 GrColor4f::FromGrColor(paintAlpha),
636 GrConstColorProcessor::kModulateRGBA_InputMode));
bsalomonaa48d362015-10-01 08:34:17 -0700637 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700638 } else {
639 // No shader, no primitive color.
brianosmana4535a32016-06-24 12:50:19 -0700640 grPaint->setColor4f(origColor.premul());
bsalomonf1b7a1d2015-09-28 06:26:28 -0700641 applyColorFilterToPaintColor = true;
642 }
643 }
644
645 SkColorFilter* colorFilter = skPaint.getColorFilter();
646 if (colorFilter) {
647 if (applyColorFilterToPaintColor) {
Brian Osman8bf4e672016-10-17 16:54:49 -0400648 // If we're in legacy mode, we *must* avoid using the 4f version of the color filter,
649 // because that will combine with the linearized version of the stored color.
Brian Osman11052242016-10-27 14:47:55 -0400650 if (rtc->isGammaCorrect()) {
Brian Osman8bf4e672016-10-17 16:54:49 -0400651 grPaint->setColor4f(GrColor4f::FromSkColor4f(
652 colorFilter->filterColor4f(origColor.toSkColor4f())).premul());
653 } else {
654 grPaint->setColor4f(SkColorToPremulGrColor4f(
655 colorFilter->filterColor(skPaint.getColor()), false, nullptr));
656 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700657 } else {
Brian Osman618d3042016-10-25 10:51:28 -0400658 sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context,
Brian Osman11052242016-10-27 14:47:55 -0400659 rtc->getColorSpace()));
bsalomone25eea42015-09-29 06:38:55 -0700660 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700661 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700662 } else {
663 return false;
664 }
665 }
666 }
667
Robert Phillipsa29a9562016-10-20 09:40:55 -0400668 SkMaskFilter* maskFilter = skPaint.getMaskFilter();
669 if (maskFilter) {
670 GrFragmentProcessor* mfFP;
671 if (maskFilter->asFragmentProcessor(&mfFP, nullptr, viewM)) {
672 grPaint->addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor>(mfFP));
673 }
674 }
675
robertphillips4f037942016-02-09 05:09:27 -0800676 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
677 // the GrPaint to also be null (also kSrcOver).
678 SkASSERT(!grPaint->getXPFactory());
reed374772b2016-10-05 17:33:02 -0700679 if (!skPaint.isSrcOver()) {
680 grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode()));
robertphillips4f037942016-02-09 05:09:27 -0800681 }
mtklein775b8192014-12-02 09:11:25 -0800682
krajcevskif461a8f2014-06-19 14:14:06 -0700683#ifndef SK_IGNORE_GPU_DITHER
Brian Osman11052242016-10-27 14:47:55 -0400684 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0 && !rtc->isGammaCorrect()) {
bungeman06ca8ec2016-06-09 08:01:03 -0700685 grPaint->addColorFragmentProcessor(GrDitherEffect::Make());
krajcevskif461a8f2014-06-19 14:14:06 -0700686 }
687#endif
bsalomonbed83a62015-04-15 14:18:34 -0700688 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000689}
690
Brian Osman11052242016-10-27 14:47:55 -0400691bool SkPaintToGrPaint(GrContext* context, GrRenderTargetContext* rtc, const SkPaint& skPaint,
brianosman8fe485b2016-07-25 12:31:51 -0700692 const SkMatrix& viewM, GrPaint* grPaint) {
Brian Osman11052242016-10-27 14:47:55 -0400693 return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, nullptr, false, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700694}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000695
bsalomonf1b7a1d2015-09-28 06:26:28 -0700696/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
697bool SkPaintToGrPaintReplaceShader(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400698 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700699 const SkPaint& skPaint,
bungeman06ca8ec2016-06-09 08:01:03 -0700700 sk_sp<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700701 GrPaint* grPaint) {
702 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700703 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000704 }
Brian Osman11052242016-10-27 14:47:55 -0400705 return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
brianosman8fe485b2016-07-25 12:31:51 -0700706 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000707}
reed8b26b992015-05-07 15:36:17 -0700708
bsalomonf1b7a1d2015-09-28 06:26:28 -0700709/** Ignores the SkShader (if any) on skPaint. */
710bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400711 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700712 const SkPaint& skPaint,
713 GrPaint* grPaint) {
714 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
bungeman06ca8ec2016-06-09 08:01:03 -0700715 static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr);
716 static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP;
Brian Osman11052242016-10-27 14:47:55 -0400717 return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), kIgnoreShader, nullptr,
brianosman8fe485b2016-07-25 12:31:51 -0700718 false, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700719}
720
721/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
Mike Reed7d954ad2016-10-28 15:42:34 -0400722be setup as a vertex attribute using the specified SkBlendMode. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700723bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400724 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700725 const SkPaint& skPaint,
726 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400727 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700728 bool primitiveIsSrc,
729 GrPaint* grPaint) {
Brian Osman11052242016-10-27 14:47:55 -0400730 return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, &primColorMode,
brianosman8fe485b2016-07-25 12:31:51 -0700731 primitiveIsSrc, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700732}
733
joshualitt33a5fce2015-11-18 13:28:51 -0800734bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400735 GrRenderTargetContext* rtc,
joshualitt33a5fce2015-11-18 13:28:51 -0800736 const SkPaint& paint,
737 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -0700738 sk_sp<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800739 bool textureIsAlphaOnly,
740 GrPaint* grPaint) {
bungeman06ca8ec2016-06-09 08:01:03 -0700741 sk_sp<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800742 if (textureIsAlphaOnly) {
743 if (const SkShader* shader = paint.getShader()) {
brianosman839345d2016-07-22 11:04:53 -0700744 shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context,
745 &viewM,
746 nullptr,
747 paint.getFilterQuality(),
Brian Osman61624f02016-12-09 14:51:59 -0500748 rtc->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -0800749 if (!shaderFP) {
750 return false;
751 }
bungeman06ca8ec2016-06-09 08:01:03 -0700752 sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
753 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800754 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700755 shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp);
joshualitt33a5fce2015-11-18 13:28:51 -0800756 }
757 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700758 shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp);
joshualitt33a5fce2015-11-18 13:28:51 -0800759 }
760
Brian Osman11052242016-10-27 14:47:55 -0400761 return SkPaintToGrPaintReplaceShader(context, rtc, paint, std::move(shaderFP), grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800762}
763
bsalomonf1b7a1d2015-09-28 06:26:28 -0700764
765////////////////////////////////////////////////////////////////////////////////////////////////
766
Brian Salomon514baff2016-11-17 15:17:07 -0500767GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
joshualitt9bc39542015-08-12 12:57:54 -0700768 const SkMatrix& viewM,
769 const SkMatrix& localM,
770 bool* doBicubic) {
771 *doBicubic = false;
Brian Salomon514baff2016-11-17 15:17:07 -0500772 GrSamplerParams::FilterMode textureFilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700773 switch (paintFilterQuality) {
774 case kNone_SkFilterQuality:
Brian Salomon514baff2016-11-17 15:17:07 -0500775 textureFilterMode = GrSamplerParams::kNone_FilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700776 break;
777 case kLow_SkFilterQuality:
Brian Salomon514baff2016-11-17 15:17:07 -0500778 textureFilterMode = GrSamplerParams::kBilerp_FilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700779 break;
780 case kMedium_SkFilterQuality: {
781 SkMatrix matrix;
782 matrix.setConcat(viewM, localM);
783 if (matrix.getMinScale() < SK_Scalar1) {
Brian Salomon514baff2016-11-17 15:17:07 -0500784 textureFilterMode = GrSamplerParams::kMipMap_FilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700785 } else {
786 // Don't trigger MIP level generation unnecessarily.
Brian Salomon514baff2016-11-17 15:17:07 -0500787 textureFilterMode = GrSamplerParams::kBilerp_FilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700788 }
789 break;
790 }
791 case kHigh_SkFilterQuality: {
792 SkMatrix matrix;
793 matrix.setConcat(viewM, localM);
794 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
795 break;
796 }
797 default:
Mike Kleine54c75f2016-10-13 14:18:09 -0400798 // Should be unreachable. If not, fall back to mipmaps.
Brian Salomon514baff2016-11-17 15:17:07 -0500799 textureFilterMode = GrSamplerParams::kMipMap_FilterMode;
joshualitt9bc39542015-08-12 12:57:54 -0700800 break;
801
802 }
803 return textureFilterMode;
804}