blob: 62b7ff14ea74ad4c6398e63386f4cccd37cd5e08 [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"
egdaniel378092f2014-12-03 10:40:13 -080010
bsalomon76228632015-05-29 08:02:10 -070011#include "GrCaps.h"
bsalomonf276ac52015-10-09 13:36:42 -070012#include "GrContext.h"
bsalomon045802d2015-10-20 07:58:01 -070013#include "GrTextureParamsAdjuster.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
egdaniel378092f2014-12-03 10:40:13 -080016#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070017#include "GrYUVProvider.h"
18
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000019#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000020#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070021#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070022#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080023#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070024#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000025#include "SkMessageBus.h"
26#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080027#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070028#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080029#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070030#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070031#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070032#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080033#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070034#include "effects/GrXfermodeFragmentProcessor.h"
bsalomonf267c1e2016-02-01 13:16:14 -080035#include "effects/GrYUVEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070036
krajcevski8c111f72014-06-02 13:51:34 -070037#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070038# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070039# include "etc1.h"
40#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomon466c2c42015-10-16 12:01:18 -070042GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
43 GrSurfaceDesc desc;
44 desc.fFlags = kNone_GrSurfaceFlags;
45 desc.fWidth = info.width();
46 desc.fHeight = info.height();
47 desc.fConfig = SkImageInfo2GrPixelConfig(info);
48 desc.fSampleCnt = 0;
49 return desc;
50}
51
bsalomon045802d2015-10-20 07:58:01 -070052void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
53 SkASSERT(key);
54 SkASSERT(imageID);
55 SkASSERT(!imageBounds.isEmpty());
56 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
57 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
bsalomon466c2c42015-10-16 12:01:18 -070058 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070059 builder[1] = imageBounds.fLeft;
60 builder[2] = imageBounds.fTop;
61 builder[3] = imageBounds.fRight;
62 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070063}
64
65GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
66 int expectedW, int expectedH,
67 const void** outStartOfDataToUpload) {
68 *outStartOfDataToUpload = nullptr;
69#ifndef SK_IGNORE_ETC1_SUPPORT
70 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
71 return kUnknown_GrPixelConfig;
72 }
73
74 const uint8_t* bytes = data->bytes();
75 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
76 // Does the data match the dimensions of the bitmap? If not,
77 // then we don't know how to scale the image to match it...
78 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
79 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
80 {
81 return kUnknown_GrPixelConfig;
82 }
83
84 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
85 return kETC1_GrPixelConfig;
86 } else if (SkKTXFile::is_ktx(bytes)) {
87 SkKTXFile ktx(data);
88
89 // Is it actually an ETC1 texture?
90 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
91 return kUnknown_GrPixelConfig;
92 }
93
94 // Does the data match the dimensions of the bitmap? If not,
95 // then we don't know how to scale the image to match it...
96 if (ktx.width() != expectedW || ktx.height() != expectedH) {
97 return kUnknown_GrPixelConfig;
98 }
99
100 *outStartOfDataToUpload = ktx.pixelData();
101 return kETC1_GrPixelConfig;
102 }
103#endif
104 return kUnknown_GrPixelConfig;
105}
106
bsalomon045802d2015-10-20 07:58:01 -0700107//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000108
bsalomon045802d2015-10-20 07:58:01 -0700109/**
110 * Fill out buffer with the compressed format Ganesh expects from a colortable
111 * based bitmap. [palette (colortable) + indices].
112 *
113 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
114 * we could detect that the colortable.count is <= 16, and then repack the
115 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
116 * slower than memcpy), so skipping that for now.
117 *
118 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
119 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 */
bsalomone79a2da2014-10-24 12:42:51 -0700121static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -0700122 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000123
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000124 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000126 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 return;
128 }
129
130 SkColorTable* ctable = bitmap.getColorTable();
131 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000132
reed@google.com7111d462014-03-25 16:20:24 +0000133 const int count = ctable->count();
134
135 SkDstPixelInfo dstPI;
136 dstPI.fColorType = kRGBA_8888_SkColorType;
137 dstPI.fAlphaType = kPremul_SkAlphaType;
138 dstPI.fPixels = buffer;
139 dstPI.fRowBytes = count * sizeof(SkPMColor);
140
141 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000142 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000143 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800144 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000145 srcPI.fRowBytes = count * sizeof(SkPMColor);
146
147 srcPI.convertPixelsTo(&dstPI, count, 1);
148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700150 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000152 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
154 } else {
155 // need to trim off the extra bytes per row
156 size_t width = bitmap.width();
157 size_t rowBytes = bitmap.rowBytes();
158 const char* src = (const char*)bitmap.getPixels();
159 for (int y = 0; y < bitmap.height(); y++) {
160 memcpy(dst, src, width);
161 src += rowBytes;
162 dst += width;
163 }
164 }
165}
166
bsalomon045802d2015-10-20 07:58:01 -0700167/**
reed43fe6182015-09-08 08:37:36 -0700168 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
169 * be gone from SkPixelRef, and we can remove this subclass entirely.
170 */
171class PixelRef_GrYUVProvider : public GrYUVProvider {
172 SkPixelRef* fPR;
173
174public:
175 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
176
177 uint32_t onGetID() override { return fPR->getGenerationID(); }
178 bool onGetYUVSizes(SkISize sizes[3]) override {
179 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
180 }
181 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
182 SkYUVColorSpace* space) override {
183 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
184 }
185};
krajcevski9c0e6292014-06-02 07:38:14 -0700186
bsalomon045802d2015-10-20 07:58:01 -0700187static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
188 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700189 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700190 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700191 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700192 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800193 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700195 }
196
reed43fe6182015-09-08 08:37:36 -0700197 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700198
bsalomon045802d2015-10-20 07:58:01 -0700199 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700200}
201
bsalomon045802d2015-10-20 07:58:01 -0700202static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700203 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
204 if (!data) {
205 return nullptr;
206 }
207
208 const void* startOfTexData;
209 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
210 &startOfTexData);
211 if (kUnknown_GrPixelConfig == desc.fConfig) {
212 return nullptr;
213 }
214
bsalomon045802d2015-10-20 07:58:01 -0700215 return ctx->textureProvider()->createTexture(desc, true, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700216}
217
bsalomon045802d2015-10-20 07:58:01 -0700218GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bmp) {
219 SkASSERT(!bmp.getTexture());
220
bsalomon045802d2015-10-20 07:58:01 -0700221 SkBitmap tmpBitmap;
222 const SkBitmap* bitmap = &bmp;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000223
reed3322a812015-09-16 10:09:24 -0700224 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700225 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000226
reed0689d7b2014-06-14 05:30:20 -0700227 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700228 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700229 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
230 bitmap->width(), bitmap->height());
231 SkAutoMalloc storage(imageSize);
bsalomon045802d2015-10-20 07:58:01 -0700232 build_index8_data(storage.get(), bmp);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
234 // our compressed data will be trimmed, so pass width() for its
235 // "rowBytes", since they are the same now.
bsalomon045802d2015-10-20 07:58:01 -0700236 return ctx->textureProvider()->createTexture(desc, true, storage.get(),
237 bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 } else {
bsalomon045802d2015-10-20 07:58:01 -0700239 bmp.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 // now bitmap points to our temp, which has been promoted to 32bits
241 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000242 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 }
reed43fe6182015-09-08 08:37:36 -0700244 } else if (!bitmap->readyToDraw()) {
245 // If the bitmap had compressed data and was then uncompressed, it'll still return
246 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
247 // the bitmap has available pixels, then they might not be what the decompressed
248 // data is.
bsalomon045802d2015-10-20 07:58:01 -0700249
250 // Really?? We aren't doing this with YUV.
251
252 GrTexture *texture = load_etc1_texture(ctx, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700253 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700254 return texture;
255 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000256 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000257
bsalomon045802d2015-10-20 07:58:01 -0700258 GrTexture *texture = create_texture_from_yuv(ctx, *bitmap, desc);
bsalomond2a6f4e2015-02-04 10:55:54 -0800259 if (texture) {
260 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700261 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800262
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000263 SkAutoLockPixels alp(*bitmap);
264 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700265 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000266 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000267
bsalomon045802d2015-10-20 07:58:01 -0700268 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels(),
269 bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800270}
271
bsalomonb4d40ef2015-07-15 10:12:16 -0700272
bsalomon045802d2015-10-20 07:58:01 -0700273////////////////////////////////////////////////////////////////////////////////
274
bsalomonc55271f2015-11-09 11:55:57 -0800275void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700276 class Invalidator : public SkPixelRef::GenIDChangeListener {
277 public:
278 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
279 private:
280 GrUniqueKeyInvalidatedMessage fMsg;
281
282 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
283 };
284
285 pixelRef->addGenIDChangeListener(new Invalidator(key));
286}
287
reedb5d32632015-09-29 13:36:50 -0700288GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700289 const GrTextureParams& params) {
bsalomon89fe56b2015-10-29 10:49:28 -0700290 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800291 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
bsalomon89fe56b2015-10-29 10:49:28 -0700292 }
bsalomonb1b01992015-11-18 10:56:08 -0800293 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000294}
reed8f343722015-08-13 13:32:39 -0700295
rileya@google.com24f3ad12012-07-18 21:47:40 +0000296///////////////////////////////////////////////////////////////////////////////
297
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000298// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
299// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800300GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000301 switch (ct) {
302 case kUnknown_SkColorType:
303 return kUnknown_GrPixelConfig;
304 case kAlpha_8_SkColorType:
305 return kAlpha_8_GrPixelConfig;
306 case kRGB_565_SkColorType:
307 return kRGB_565_GrPixelConfig;
308 case kARGB_4444_SkColorType:
309 return kRGBA_4444_GrPixelConfig;
310 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700311 //if (kSRGB_SkColorProfileType == pt) {
312 // return kSRGBA_8888_GrPixelConfig;
313 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000314 return kRGBA_8888_GrPixelConfig;
315 case kBGRA_8888_SkColorType:
316 return kBGRA_8888_GrPixelConfig;
317 case kIndex_8_SkColorType:
318 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700319 case kGray_8_SkColorType:
320 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000321 }
322 SkASSERT(0); // shouldn't get here
323 return kUnknown_GrPixelConfig;
324}
325
jvanverthfa1e8a72014-12-22 08:31:49 -0800326bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
327 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000328 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800329 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000330 switch (config) {
331 case kAlpha_8_GrPixelConfig:
332 ct = kAlpha_8_SkColorType;
333 break;
334 case kIndex_8_GrPixelConfig:
335 ct = kIndex_8_SkColorType;
336 break;
337 case kRGB_565_GrPixelConfig:
338 ct = kRGB_565_SkColorType;
339 break;
340 case kRGBA_4444_GrPixelConfig:
341 ct = kARGB_4444_SkColorType;
342 break;
343 case kRGBA_8888_GrPixelConfig:
344 ct = kRGBA_8888_SkColorType;
345 break;
346 case kBGRA_8888_GrPixelConfig:
347 ct = kBGRA_8888_SkColorType;
348 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800349 case kSRGBA_8888_GrPixelConfig:
350 ct = kRGBA_8888_SkColorType;
351 pt = kSRGB_SkColorProfileType;
352 break;
reed@google.combf790232013-12-13 19:45:58 +0000353 default:
354 return false;
355 }
356 if (ctOut) {
357 *ctOut = ct;
358 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800359 if (ptOut) {
360 *ptOut = pt;
361 }
reed@google.combf790232013-12-13 19:45:58 +0000362 return true;
363}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000364
bsalomonf1b7a1d2015-09-28 06:26:28 -0700365////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000366
bsalomonaa48d362015-10-01 08:34:17 -0700367static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
368 if (primitiveIsSrc) {
369 return SkXfermode::kSrc_Mode != mode;
370 } else {
371 return SkXfermode::kDst_Mode != mode;
372 }
373}
374
bsalomonf1b7a1d2015-09-28 06:26:28 -0700375static inline bool skpaint_to_grpaint_impl(GrContext* context,
376 const SkPaint& skPaint,
377 const SkMatrix& viewM,
378 const GrFragmentProcessor** shaderProcessor,
379 SkXfermode::Mode* primColorMode,
380 bool primitiveIsSrc,
381 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000382 grPaint->setAntiAlias(skPaint.isAntiAlias());
383
bsalomonf1b7a1d2015-09-28 06:26:28 -0700384 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
385 // of per-vertex colors.
386 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700387 const GrFragmentProcessor* shaderFP = nullptr;
388 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
389 if (shaderProcessor) {
390 shaderFP = *shaderProcessor;
391 } else if (const SkShader* shader = skPaint.getShader()) {
392 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700393 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700394 shaderFP = aufp;
395 if (!shaderFP) {
396 return false;
397 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700398 }
399 }
400
401 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
402 // a known constant value. In that case we can simply apply a color filter during this
403 // conversion without converting the color filter to a GrFragmentProcessor.
404 bool applyColorFilterToPaintColor = false;
405 if (shaderFP) {
406 if (primColorMode) {
407 // There is a blend between the primitive color and the shader color. The shader sees
408 // the opaque paint color. The shader's output is blended using the provided mode by
409 // the primitive color. The blended color is then modulated by the paint's alpha.
410
411 // The geometry processor will insert the primitive color to start the color chain, so
412 // the GrPaint color will be ignored.
413
414 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
415
416 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
417 aufp.reset(shaderFP);
418
419 if (primitiveIsSrc) {
420 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
421 *primColorMode);
422 } else {
423 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
424 *primColorMode);
425 }
426 aufp.reset(shaderFP);
427 // The above may return null if compose results in a pass through of the prim color.
428 if (shaderFP) {
429 grPaint->addColorFragmentProcessor(shaderFP);
430 }
431
432 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
433 if (GrColor_WHITE != paintAlpha) {
434 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
435 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
436 }
437 } else {
438 // The shader's FP sees the paint unpremul color
439 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
440 grPaint->addColorFragmentProcessor(shaderFP);
441 }
442 } else {
443 if (primColorMode) {
444 // There is a blend between the primitive color and the paint color. The blend considers
445 // the opaque paint color. The paint's alpha is applied to the post-blended color.
446 SkAutoTUnref<const GrFragmentProcessor> processor(
447 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
448 GrConstColorProcessor::kIgnore_InputMode));
449 if (primitiveIsSrc) {
450 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
451 *primColorMode));
452 } else {
453 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
454 *primColorMode));
455
456 }
457 if (processor) {
458 grPaint->addColorFragmentProcessor(processor);
459 }
460
bsalomonaa48d362015-10-01 08:34:17 -0700461 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700462
463 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700464 if (GrColor_WHITE != paintAlpha) {
465 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
466 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
467 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700468 } else {
469 // No shader, no primitive color.
470 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
471 applyColorFilterToPaintColor = true;
472 }
473 }
474
475 SkColorFilter* colorFilter = skPaint.getColorFilter();
476 if (colorFilter) {
477 if (applyColorFilterToPaintColor) {
478 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
479 } else {
bsalomone25eea42015-09-29 06:38:55 -0700480 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700481 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700482 if (cfFP) {
483 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700484 } else {
485 return false;
486 }
487 }
488 }
489
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000490 SkXfermode* mode = skPaint.getXfermode();
egdanielf2342722015-11-20 15:12:59 -0800491 GrXPFactory* xpFactory = nullptr;
egdanielc4b72722015-11-23 13:20:41 -0800492 SkXfermode::AsXPFactory(mode, &xpFactory);
493 SkSafeUnref(grPaint->setXPFactory(xpFactory));
mtklein775b8192014-12-02 09:11:25 -0800494
krajcevskif461a8f2014-06-19 14:14:06 -0700495#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700496 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700497 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700498 }
499#endif
bsalomonbed83a62015-04-15 14:18:34 -0700500 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000501}
502
bsalomonf1b7a1d2015-09-28 06:26:28 -0700503bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
504 GrPaint* grPaint) {
505 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
506}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000507
bsalomonf1b7a1d2015-09-28 06:26:28 -0700508/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
509bool SkPaintToGrPaintReplaceShader(GrContext* context,
510 const SkPaint& skPaint,
511 const GrFragmentProcessor* shaderFP,
512 GrPaint* grPaint) {
513 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700514 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000515 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700516 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
517 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000518}
reed8b26b992015-05-07 15:36:17 -0700519
bsalomonf1b7a1d2015-09-28 06:26:28 -0700520/** Ignores the SkShader (if any) on skPaint. */
521bool SkPaintToGrPaintNoShader(GrContext* context,
522 const SkPaint& skPaint,
523 GrPaint* grPaint) {
524 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
525 static const GrFragmentProcessor* kNullShaderFP = nullptr;
526 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
527 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
528 grPaint);
529}
530
531/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
532be setup as a vertex attribute using the specified SkXfermode::Mode. */
533bool SkPaintToGrPaintWithXfermode(GrContext* context,
534 const SkPaint& skPaint,
535 const SkMatrix& viewM,
536 SkXfermode::Mode primColorMode,
537 bool primitiveIsSrc,
538 GrPaint* grPaint) {
539 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
540 grPaint);
541}
542
joshualitt33a5fce2015-11-18 13:28:51 -0800543bool SkPaintToGrPaintWithTexture(GrContext* context,
544 const SkPaint& paint,
545 const SkMatrix& viewM,
546 const GrFragmentProcessor* fp,
547 bool textureIsAlphaOnly,
548 GrPaint* grPaint) {
549 SkAutoTUnref<const GrFragmentProcessor> shaderFP;
550 if (textureIsAlphaOnly) {
551 if (const SkShader* shader = paint.getShader()) {
552 shaderFP.reset(shader->asFragmentProcessor(context,
553 viewM,
554 nullptr,
555 paint.getFilterQuality()));
556 if (!shaderFP) {
557 return false;
558 }
559 const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp };
560 shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2));
561 } else {
562 shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
563 }
564 } else {
565 shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
566 }
567
568 return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), grPaint);
569}
570
bsalomonf1b7a1d2015-09-28 06:26:28 -0700571
572////////////////////////////////////////////////////////////////////////////////////////////////
573
reed8b26b992015-05-07 15:36:17 -0700574SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
575#ifdef SK_DEBUG
576 const GrSurfaceDesc& desc = tex->desc();
577 SkASSERT(w <= desc.fWidth);
578 SkASSERT(h <= desc.fHeight);
579#endif
580 const GrPixelConfig config = tex->config();
581 SkColorType ct;
582 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700583 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700584 ct = kUnknown_SkColorType;
585 }
586 return SkImageInfo::Make(w, h, ct, at);
587}
588
589
590void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
591 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
592 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700593 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700594}
joshualitt9bc39542015-08-12 12:57:54 -0700595
596GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
597 const SkMatrix& viewM,
598 const SkMatrix& localM,
599 bool* doBicubic) {
600 *doBicubic = false;
601 GrTextureParams::FilterMode textureFilterMode;
602 switch (paintFilterQuality) {
603 case kNone_SkFilterQuality:
604 textureFilterMode = GrTextureParams::kNone_FilterMode;
605 break;
606 case kLow_SkFilterQuality:
607 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
608 break;
609 case kMedium_SkFilterQuality: {
610 SkMatrix matrix;
611 matrix.setConcat(viewM, localM);
612 if (matrix.getMinScale() < SK_Scalar1) {
613 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
614 } else {
615 // Don't trigger MIP level generation unnecessarily.
616 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
617 }
618 break;
619 }
620 case kHigh_SkFilterQuality: {
621 SkMatrix matrix;
622 matrix.setConcat(viewM, localM);
623 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
624 break;
625 }
626 default:
627 SkErrorInternals::SetError( kInvalidPaint_SkError,
628 "Sorry, I don't understand the filtering "
629 "mode you asked for. Falling back to "
630 "MIPMaps.");
631 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
632 break;
633
634 }
635 return textureFilterMode;
636}