blob: 81a3502455765c78ffff32b11c2132d78e208a75 [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
bsalomon76228632015-05-29 08:02:10 -070012#include "GrCaps.h"
bsalomonf276ac52015-10-09 13:36:42 -070013#include "GrContext.h"
bsalomon045802d2015-10-20 07:58:01 -070014#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
cblume55f2d2d2016-02-26 13:20:48 -080016#include "GrTextureParamsAdjuster.h"
17#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080018#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070019#include "GrYUVProvider.h"
20
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000021#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000022#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070023#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070024#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080025#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070026#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000027#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080028#include "SkMipMap.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000029#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080030#include "SkResourceCache.h"
cblume55f2d2d2016-02-26 13:20:48 -080031#include "SkTemplates.h"
krajcevski40a1e112014-08-05 14:13:36 -070032#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080033#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070034#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070035#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070036#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080037#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070038#include "effects/GrXfermodeFragmentProcessor.h"
bsalomonf267c1e2016-02-01 13:16:14 -080039#include "effects/GrYUVEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070040
krajcevski8c111f72014-06-02 13:51:34 -070041#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070042# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070043# include "etc1.h"
44#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomon466c2c42015-10-16 12:01:18 -070046GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
47 GrSurfaceDesc desc;
48 desc.fFlags = kNone_GrSurfaceFlags;
49 desc.fWidth = info.width();
50 desc.fHeight = info.height();
51 desc.fConfig = SkImageInfo2GrPixelConfig(info);
52 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;
90 } else if (SkKTXFile::is_ktx(bytes)) {
91 SkKTXFile ktx(data);
92
93 // Is it actually an ETC1 texture?
94 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
95 return kUnknown_GrPixelConfig;
96 }
97
98 // Does the data match the dimensions of the bitmap? If not,
99 // then we don't know how to scale the image to match it...
100 if (ktx.width() != expectedW || ktx.height() != expectedH) {
101 return kUnknown_GrPixelConfig;
102 }
103
104 *outStartOfDataToUpload = ktx.pixelData();
105 return kETC1_GrPixelConfig;
106 }
107#endif
108 return kUnknown_GrPixelConfig;
109}
110
bsalomon045802d2015-10-20 07:58:01 -0700111//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000112
bsalomon045802d2015-10-20 07:58:01 -0700113/**
114 * Fill out buffer with the compressed format Ganesh expects from a colortable
115 * based bitmap. [palette (colortable) + indices].
116 *
117 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
118 * we could detect that the colortable.count is <= 16, and then repack the
119 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
120 * slower than memcpy), so skipping that for now.
121 *
122 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
123 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 */
bsalomon0d996862016-03-09 18:44:43 -0800125static void build_index8_data(void* buffer, const SkPixmap& pixmap) {
126 SkASSERT(kIndex_8_SkColorType == pixmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000127
bsalomon0d996862016-03-09 18:44:43 -0800128 const SkColorTable* ctable = pixmap.ctable();
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000130
reed@google.com7111d462014-03-25 16:20:24 +0000131 const int count = ctable->count();
132
133 SkDstPixelInfo dstPI;
134 dstPI.fColorType = kRGBA_8888_SkColorType;
135 dstPI.fAlphaType = kPremul_SkAlphaType;
136 dstPI.fPixels = buffer;
137 dstPI.fRowBytes = count * sizeof(SkPMColor);
138
139 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000140 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000141 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800142 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000143 srcPI.fRowBytes = count * sizeof(SkPMColor);
144
145 srcPI.convertPixelsTo(&dstPI, count, 1);
146
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700148 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000149
bsalomon0d996862016-03-09 18:44:43 -0800150 if ((unsigned)pixmap.width() == pixmap.rowBytes()) {
151 memcpy(dst, pixmap.addr(), pixmap.getSafeSize());
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 } else {
153 // need to trim off the extra bytes per row
bsalomon0d996862016-03-09 18:44:43 -0800154 size_t width = pixmap.width();
155 size_t rowBytes = pixmap.rowBytes();
156 const uint8_t* src = pixmap.addr8();
157 for (int y = 0; y < pixmap.height(); y++) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 memcpy(dst, src, width);
159 src += rowBytes;
160 dst += width;
161 }
162 }
163}
164
bsalomon045802d2015-10-20 07:58:01 -0700165/**
reed43fe6182015-09-08 08:37:36 -0700166 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
167 * be gone from SkPixelRef, and we can remove this subclass entirely.
168 */
169class PixelRef_GrYUVProvider : public GrYUVProvider {
170 SkPixelRef* fPR;
171
172public:
173 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
174
175 uint32_t onGetID() override { return fPR->getGenerationID(); }
msarett1590f3b2016-03-07 09:16:52 -0800176 bool onGetYUVSizes(SkISize sizes[3]) override {
177 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
reed43fe6182015-09-08 08:37:36 -0700178 }
msarett1590f3b2016-03-07 09:16:52 -0800179 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
180 SkYUVColorSpace* space) override {
181 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
reed43fe6182015-09-08 08:37:36 -0700182 }
183};
krajcevski9c0e6292014-06-02 07:38:14 -0700184
bsalomon045802d2015-10-20 07:58:01 -0700185static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
186 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700187 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700188 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700189 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700190 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800191 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700193 }
194
reed43fe6182015-09-08 08:37:36 -0700195 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700196
bsalomon045802d2015-10-20 07:58:01 -0700197 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700198}
199
bsalomon045802d2015-10-20 07:58:01 -0700200static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700201 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
202 if (!data) {
203 return nullptr;
204 }
205
206 const void* startOfTexData;
207 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
208 &startOfTexData);
209 if (kUnknown_GrPixelConfig == desc.fConfig) {
210 return nullptr;
211 }
212
bsalomon5ec26ae2016-02-25 08:33:02 -0800213 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700214}
215
bsalomon0d996862016-03-09 18:44:43 -0800216GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) {
217 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
218 if (GrTexture *texture = load_etc1_texture(ctx, bitmap, desc)) {
219 return texture;
220 }
bsalomon045802d2015-10-20 07:58:01 -0700221
bsalomon0d996862016-03-09 18:44:43 -0800222 if (GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc)) {
223 return texture;
224 }
225
226 SkAutoLockPixels alp(bitmap);
227 if (!bitmap.readyToDraw()) {
228 return nullptr;
229 }
230 SkPixmap pixmap;
231 if (!bitmap.peekPixels(&pixmap)) {
232 return nullptr;
233 }
234 return GrUploadPixmapToTexture(ctx, pixmap);
235}
236
237GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap) {
238 const SkPixmap* pmap = &pixmap;
239 SkPixmap tmpPixmap;
bsalomon045802d2015-10-20 07:58:01 -0700240 SkBitmap tmpBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000241
bsalomon0d996862016-03-09 18:44:43 -0800242 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info());
bsalomon76228632015-05-29 08:02:10 -0700243 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000244
bsalomon0d996862016-03-09 18:44:43 -0800245 if (kIndex_8_SkColorType == pixmap.colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700246 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700247 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
bsalomon0d996862016-03-09 18:44:43 -0800248 pixmap.width(), pixmap.height());
bsalomond4cb9222014-08-11 14:19:09 -0700249 SkAutoMalloc storage(imageSize);
bsalomon0d996862016-03-09 18:44:43 -0800250 build_index8_data(storage.get(), pixmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
252 // our compressed data will be trimmed, so pass width() for its
253 // "rowBytes", since they are the same now.
bsalomon5ec26ae2016-02-25 08:33:02 -0800254 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, storage.get(),
bsalomon0d996862016-03-09 18:44:43 -0800255 pixmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 } else {
bsalomon0d996862016-03-09 18:44:43 -0800257 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
258 tmpBitmap.allocPixels(info);
259 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
260 return nullptr;
261 }
262 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
263 return nullptr;
264 }
265 pmap = &tmpPixmap;
266 // must rebuild desc, since we've forced the info to be N32
267 desc = GrImageInfoToSurfaceDesc(pmap->info());
krajcevski9c0e6292014-06-02 07:38:14 -0700268 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000269 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
bsalomon0d996862016-03-09 18:44:43 -0800271 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, pmap->addr(),
272 pmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800273}
274
bsalomonb4d40ef2015-07-15 10:12:16 -0700275
bsalomon045802d2015-10-20 07:58:01 -0700276////////////////////////////////////////////////////////////////////////////////
277
bsalomonc55271f2015-11-09 11:55:57 -0800278void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700279 class Invalidator : public SkPixelRef::GenIDChangeListener {
280 public:
281 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
282 private:
283 GrUniqueKeyInvalidatedMessage fMsg;
284
285 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
286 };
287
288 pixelRef->addGenIDChangeListener(new Invalidator(key));
289}
290
cblume55f2d2d2016-02-26 13:20:48 -0800291GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap)
292{
293 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
294 if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) {
295 GrTexture* texture = load_etc1_texture(ctx, bitmap, desc);
296 if (texture) {
297 return texture;
298 }
299 }
300
301 GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc);
302 if (texture) {
303 return texture;
304 }
305
306 SkASSERT(sizeof(int) <= sizeof(uint32_t));
307 if (bitmap.width() < 0 || bitmap.height() < 0) {
308 return nullptr;
309 }
310
311 SkAutoPixmapUnlock srcUnlocker;
312 if (!bitmap.requestLock(&srcUnlocker)) {
313 return nullptr;
314 }
315 const SkPixmap& pixmap = srcUnlocker.pixmap();
316 // Try to catch where we might have returned nullptr for src crbug.com/492818
317 if (nullptr == pixmap.addr()) {
318 sk_throw();
319 }
320
321 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
322 if (!mipmaps) {
323 return nullptr;
324 }
325
326 const int mipLevelCount = mipmaps->countLevels() + 1;
327 if (mipLevelCount < 1) {
328 return nullptr;
329 }
330
331 const bool isMipMapped = mipLevelCount > 1;
332 desc.fIsMipMapped = isMipMapped;
333
334 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
335
336 texels[0].fPixels = pixmap.addr();
337 texels[0].fRowBytes = pixmap.rowBytes();
338
339 for (int i = 1; i < mipLevelCount; ++i) {
340 SkMipMap::Level generatedMipLevel;
341 mipmaps->getLevel(i - 1, &generatedMipLevel);
342 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
343 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
344 }
345
346 return ctx->textureProvider()->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(),
347 mipLevelCount);
348}
349
reedb5d32632015-09-29 13:36:50 -0700350GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700351 const GrTextureParams& params) {
bsalomon89fe56b2015-10-29 10:49:28 -0700352 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800353 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
bsalomon89fe56b2015-10-29 10:49:28 -0700354 }
bsalomonb1b01992015-11-18 10:56:08 -0800355 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000356}
reed8f343722015-08-13 13:32:39 -0700357
rileya@google.com24f3ad12012-07-18 21:47:40 +0000358///////////////////////////////////////////////////////////////////////////////
359
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000360// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
361// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800362GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000363 switch (ct) {
364 case kUnknown_SkColorType:
365 return kUnknown_GrPixelConfig;
366 case kAlpha_8_SkColorType:
367 return kAlpha_8_GrPixelConfig;
368 case kRGB_565_SkColorType:
369 return kRGB_565_GrPixelConfig;
370 case kARGB_4444_SkColorType:
371 return kRGBA_4444_GrPixelConfig;
372 case kRGBA_8888_SkColorType:
brianosmane79586b2016-03-03 12:52:54 -0800373 //if (kSRGB_SkColorProfileType == pt) {
374 // return kSRGBA_8888_GrPixelConfig;
375 //}
brianosman744898a2016-03-01 13:44:28 -0800376 return kRGBA_8888_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000377 case kBGRA_8888_SkColorType:
brianosman744898a2016-03-01 13:44:28 -0800378 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000379 case kIndex_8_SkColorType:
380 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700381 case kGray_8_SkColorType:
382 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
reed3601f282016-02-05 11:18:39 -0800383 case kRGBA_F16_SkColorType:
384 return kRGBA_half_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000385 }
386 SkASSERT(0); // shouldn't get here
387 return kUnknown_GrPixelConfig;
388}
389
jvanverthfa1e8a72014-12-22 08:31:49 -0800390bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
391 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000392 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800393 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000394 switch (config) {
395 case kAlpha_8_GrPixelConfig:
396 ct = kAlpha_8_SkColorType;
397 break;
398 case kIndex_8_GrPixelConfig:
399 ct = kIndex_8_SkColorType;
400 break;
401 case kRGB_565_GrPixelConfig:
402 ct = kRGB_565_SkColorType;
403 break;
404 case kRGBA_4444_GrPixelConfig:
405 ct = kARGB_4444_SkColorType;
406 break;
407 case kRGBA_8888_GrPixelConfig:
408 ct = kRGBA_8888_SkColorType;
409 break;
410 case kBGRA_8888_GrPixelConfig:
411 ct = kBGRA_8888_SkColorType;
412 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800413 case kSRGBA_8888_GrPixelConfig:
414 ct = kRGBA_8888_SkColorType;
415 pt = kSRGB_SkColorProfileType;
416 break;
reed@google.combf790232013-12-13 19:45:58 +0000417 default:
418 return false;
419 }
420 if (ctOut) {
421 *ctOut = ct;
422 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800423 if (ptOut) {
424 *ptOut = pt;
425 }
reed@google.combf790232013-12-13 19:45:58 +0000426 return true;
427}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000428
bsalomonf1b7a1d2015-09-28 06:26:28 -0700429////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000430
bsalomonaa48d362015-10-01 08:34:17 -0700431static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
432 if (primitiveIsSrc) {
433 return SkXfermode::kSrc_Mode != mode;
434 } else {
435 return SkXfermode::kDst_Mode != mode;
436 }
437}
438
bsalomonf1b7a1d2015-09-28 06:26:28 -0700439static inline bool skpaint_to_grpaint_impl(GrContext* context,
440 const SkPaint& skPaint,
441 const SkMatrix& viewM,
442 const GrFragmentProcessor** shaderProcessor,
443 SkXfermode::Mode* primColorMode,
444 bool primitiveIsSrc,
445 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000446 grPaint->setAntiAlias(skPaint.isAntiAlias());
447
bsalomonf1b7a1d2015-09-28 06:26:28 -0700448 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
449 // of per-vertex colors.
450 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700451 const GrFragmentProcessor* shaderFP = nullptr;
452 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
453 if (shaderProcessor) {
454 shaderFP = *shaderProcessor;
455 } else if (const SkShader* shader = skPaint.getShader()) {
456 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700457 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700458 shaderFP = aufp;
459 if (!shaderFP) {
460 return false;
461 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700462 }
463 }
464
465 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
466 // a known constant value. In that case we can simply apply a color filter during this
467 // conversion without converting the color filter to a GrFragmentProcessor.
468 bool applyColorFilterToPaintColor = false;
469 if (shaderFP) {
470 if (primColorMode) {
471 // There is a blend between the primitive color and the shader color. The shader sees
472 // the opaque paint color. The shader's output is blended using the provided mode by
473 // the primitive color. The blended color is then modulated by the paint's alpha.
474
475 // The geometry processor will insert the primitive color to start the color chain, so
476 // the GrPaint color will be ignored.
477
478 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
479
480 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
481 aufp.reset(shaderFP);
482
483 if (primitiveIsSrc) {
484 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
485 *primColorMode);
486 } else {
487 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
488 *primColorMode);
489 }
490 aufp.reset(shaderFP);
491 // The above may return null if compose results in a pass through of the prim color.
492 if (shaderFP) {
493 grPaint->addColorFragmentProcessor(shaderFP);
494 }
495
496 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
497 if (GrColor_WHITE != paintAlpha) {
498 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
499 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
500 }
501 } else {
502 // The shader's FP sees the paint unpremul color
503 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
504 grPaint->addColorFragmentProcessor(shaderFP);
505 }
506 } else {
507 if (primColorMode) {
508 // There is a blend between the primitive color and the paint color. The blend considers
509 // the opaque paint color. The paint's alpha is applied to the post-blended color.
510 SkAutoTUnref<const GrFragmentProcessor> processor(
511 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
512 GrConstColorProcessor::kIgnore_InputMode));
513 if (primitiveIsSrc) {
514 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
515 *primColorMode));
516 } else {
517 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
518 *primColorMode));
519
520 }
521 if (processor) {
522 grPaint->addColorFragmentProcessor(processor);
523 }
524
bsalomonaa48d362015-10-01 08:34:17 -0700525 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700526
527 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700528 if (GrColor_WHITE != paintAlpha) {
529 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
530 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
531 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700532 } else {
533 // No shader, no primitive color.
534 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
535 applyColorFilterToPaintColor = true;
536 }
537 }
538
539 SkColorFilter* colorFilter = skPaint.getColorFilter();
540 if (colorFilter) {
541 if (applyColorFilterToPaintColor) {
542 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
543 } else {
bsalomone25eea42015-09-29 06:38:55 -0700544 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700545 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700546 if (cfFP) {
547 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700548 } else {
549 return false;
550 }
551 }
552 }
553
robertphillips4f037942016-02-09 05:09:27 -0800554 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
555 // the GrPaint to also be null (also kSrcOver).
556 SkASSERT(!grPaint->getXPFactory());
557 SkXfermode* xfermode = skPaint.getXfermode();
558 if (xfermode) {
559 // SafeUnref in case a new xfermode is added that returns null.
560 // In such cases we will fall back to kSrcOver_Mode.
561 SkSafeUnref(grPaint->setXPFactory(xfermode->asXPFactory()));
562 }
mtklein775b8192014-12-02 09:11:25 -0800563
krajcevskif461a8f2014-06-19 14:14:06 -0700564#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700565 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700566 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700567 }
568#endif
bsalomonbed83a62015-04-15 14:18:34 -0700569 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000570}
571
bsalomonf1b7a1d2015-09-28 06:26:28 -0700572bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
573 GrPaint* grPaint) {
574 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
575}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000576
bsalomonf1b7a1d2015-09-28 06:26:28 -0700577/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
578bool SkPaintToGrPaintReplaceShader(GrContext* context,
579 const SkPaint& skPaint,
580 const GrFragmentProcessor* shaderFP,
581 GrPaint* grPaint) {
582 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700583 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000584 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700585 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
586 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000587}
reed8b26b992015-05-07 15:36:17 -0700588
bsalomonf1b7a1d2015-09-28 06:26:28 -0700589/** Ignores the SkShader (if any) on skPaint. */
590bool SkPaintToGrPaintNoShader(GrContext* context,
591 const SkPaint& skPaint,
592 GrPaint* grPaint) {
593 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
594 static const GrFragmentProcessor* kNullShaderFP = nullptr;
595 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
596 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
597 grPaint);
598}
599
600/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
601be setup as a vertex attribute using the specified SkXfermode::Mode. */
602bool SkPaintToGrPaintWithXfermode(GrContext* context,
603 const SkPaint& skPaint,
604 const SkMatrix& viewM,
605 SkXfermode::Mode primColorMode,
606 bool primitiveIsSrc,
607 GrPaint* grPaint) {
608 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
609 grPaint);
610}
611
joshualitt33a5fce2015-11-18 13:28:51 -0800612bool SkPaintToGrPaintWithTexture(GrContext* context,
613 const SkPaint& paint,
614 const SkMatrix& viewM,
615 const GrFragmentProcessor* fp,
616 bool textureIsAlphaOnly,
617 GrPaint* grPaint) {
618 SkAutoTUnref<const GrFragmentProcessor> shaderFP;
619 if (textureIsAlphaOnly) {
620 if (const SkShader* shader = paint.getShader()) {
621 shaderFP.reset(shader->asFragmentProcessor(context,
622 viewM,
623 nullptr,
624 paint.getFilterQuality()));
625 if (!shaderFP) {
626 return false;
627 }
628 const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp };
629 shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2));
630 } else {
631 shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
632 }
633 } else {
634 shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
635 }
636
637 return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), grPaint);
638}
639
bsalomonf1b7a1d2015-09-28 06:26:28 -0700640
641////////////////////////////////////////////////////////////////////////////////////////////////
642
reed8b26b992015-05-07 15:36:17 -0700643SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
644#ifdef SK_DEBUG
645 const GrSurfaceDesc& desc = tex->desc();
646 SkASSERT(w <= desc.fWidth);
647 SkASSERT(h <= desc.fHeight);
648#endif
649 const GrPixelConfig config = tex->config();
650 SkColorType ct;
651 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700652 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700653 ct = kUnknown_SkColorType;
654 }
655 return SkImageInfo::Make(w, h, ct, at);
656}
657
658
659void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
660 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
661 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700662 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700663}
joshualitt9bc39542015-08-12 12:57:54 -0700664
665GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
666 const SkMatrix& viewM,
667 const SkMatrix& localM,
668 bool* doBicubic) {
669 *doBicubic = false;
670 GrTextureParams::FilterMode textureFilterMode;
671 switch (paintFilterQuality) {
672 case kNone_SkFilterQuality:
673 textureFilterMode = GrTextureParams::kNone_FilterMode;
674 break;
675 case kLow_SkFilterQuality:
676 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
677 break;
678 case kMedium_SkFilterQuality: {
679 SkMatrix matrix;
680 matrix.setConcat(viewM, localM);
681 if (matrix.getMinScale() < SK_Scalar1) {
682 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
683 } else {
684 // Don't trigger MIP level generation unnecessarily.
685 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
686 }
687 break;
688 }
689 case kHigh_SkFilterQuality: {
690 SkMatrix matrix;
691 matrix.setConcat(viewM, localM);
692 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
693 break;
694 }
695 default:
696 SkErrorInternals::SetError( kInvalidPaint_SkError,
697 "Sorry, I don't understand the filtering "
698 "mode you asked for. Falling back to "
699 "MIPMaps.");
700 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
701 break;
702
703 }
704 return textureFilterMode;
705}