blob: 866f34d06c0201590d232abe90f62f282a03474c [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
brianosman9e3f1bf2016-03-17 12:26: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();
brianosman9e3f1bf2016-03-17 12:26: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;
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(); }
msarett4984c3c2016-03-10 05:44:43 -0800176 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
177 return fPR->queryYUV8(sizeInfo, colorSpace);
reed43fe6182015-09-08 08:37:36 -0700178 }
msarett4984c3c2016-03-10 05:44:43 -0800179 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
180 return fPR->getYUV8Planes(sizeInfo, planes);
reed43fe6182015-09-08 08:37:36 -0700181 }
182};
krajcevski9c0e6292014-06-02 07:38:14 -0700183
bsalomon045802d2015-10-20 07:58:01 -0700184static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
185 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700186 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700187 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700188 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700189 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800190 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700192 }
193
reed43fe6182015-09-08 08:37:36 -0700194 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700195
bsalomon045802d2015-10-20 07:58:01 -0700196 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700197}
198
bsalomon045802d2015-10-20 07:58:01 -0700199static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700200 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
201 if (!data) {
202 return nullptr;
203 }
204
205 const void* startOfTexData;
206 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
207 &startOfTexData);
208 if (kUnknown_GrPixelConfig == desc.fConfig) {
209 return nullptr;
210 }
211
bsalomon5ec26ae2016-02-25 08:33:02 -0800212 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700213}
214
bsalomon0d996862016-03-09 18:44:43 -0800215GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) {
brianosman9e3f1bf2016-03-17 12:26:37 -0700216 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
bsalomon0d996862016-03-09 18:44:43 -0800217 if (GrTexture *texture = load_etc1_texture(ctx, bitmap, desc)) {
218 return texture;
219 }
bsalomon045802d2015-10-20 07:58:01 -0700220
bsalomon0d996862016-03-09 18:44:43 -0800221 if (GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc)) {
222 return texture;
223 }
224
225 SkAutoLockPixels alp(bitmap);
226 if (!bitmap.readyToDraw()) {
227 return nullptr;
228 }
229 SkPixmap pixmap;
230 if (!bitmap.peekPixels(&pixmap)) {
231 return nullptr;
232 }
233 return GrUploadPixmapToTexture(ctx, pixmap);
234}
235
236GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap) {
237 const SkPixmap* pmap = &pixmap;
238 SkPixmap tmpPixmap;
bsalomon045802d2015-10-20 07:58:01 -0700239 SkBitmap tmpBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000240
bsalomon76228632015-05-29 08:02:10 -0700241 const GrCaps* caps = ctx->caps();
brianosman9e3f1bf2016-03-17 12:26:37 -0700242 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000243
bsalomon0d996862016-03-09 18:44:43 -0800244 if (kIndex_8_SkColorType == pixmap.colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700245 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700246 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
bsalomon0d996862016-03-09 18:44:43 -0800247 pixmap.width(), pixmap.height());
bsalomond4cb9222014-08-11 14:19:09 -0700248 SkAutoMalloc storage(imageSize);
bsalomon0d996862016-03-09 18:44:43 -0800249 build_index8_data(storage.get(), pixmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
251 // our compressed data will be trimmed, so pass width() for its
252 // "rowBytes", since they are the same now.
bsalomon5ec26ae2016-02-25 08:33:02 -0800253 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, storage.get(),
bsalomon0d996862016-03-09 18:44:43 -0800254 pixmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 } else {
bsalomon0d996862016-03-09 18:44:43 -0800256 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
257 tmpBitmap.allocPixels(info);
258 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
259 return nullptr;
260 }
261 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
262 return nullptr;
263 }
264 pmap = &tmpPixmap;
265 // must rebuild desc, since we've forced the info to be N32
brianosman9e3f1bf2016-03-17 12:26:37 -0700266 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
krajcevski9c0e6292014-06-02 07:38:14 -0700267 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon0d996862016-03-09 18:44:43 -0800270 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, pmap->addr(),
271 pmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800272}
273
bsalomonb4d40ef2015-07-15 10:12:16 -0700274
bsalomon045802d2015-10-20 07:58:01 -0700275////////////////////////////////////////////////////////////////////////////////
276
bsalomonc55271f2015-11-09 11:55:57 -0800277void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700278 class Invalidator : public SkPixelRef::GenIDChangeListener {
279 public:
280 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
281 private:
282 GrUniqueKeyInvalidatedMessage fMsg;
283
284 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
285 };
286
287 pixelRef->addGenIDChangeListener(new Invalidator(key));
288}
289
cblume55f2d2d2016-02-26 13:20:48 -0800290GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap)
291{
brianosman9e3f1bf2016-03-17 12:26:37 -0700292 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
cblume55f2d2d2016-02-26 13:20:48 -0800293 if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) {
294 GrTexture* texture = load_etc1_texture(ctx, bitmap, desc);
295 if (texture) {
296 return texture;
297 }
298 }
299
300 GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc);
301 if (texture) {
302 return texture;
303 }
304
305 SkASSERT(sizeof(int) <= sizeof(uint32_t));
306 if (bitmap.width() < 0 || bitmap.height() < 0) {
307 return nullptr;
308 }
309
310 SkAutoPixmapUnlock srcUnlocker;
311 if (!bitmap.requestLock(&srcUnlocker)) {
312 return nullptr;
313 }
314 const SkPixmap& pixmap = srcUnlocker.pixmap();
315 // Try to catch where we might have returned nullptr for src crbug.com/492818
316 if (nullptr == pixmap.addr()) {
317 sk_throw();
318 }
319
320 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
321 if (!mipmaps) {
322 return nullptr;
323 }
324
325 const int mipLevelCount = mipmaps->countLevels() + 1;
326 if (mipLevelCount < 1) {
327 return nullptr;
328 }
329
330 const bool isMipMapped = mipLevelCount > 1;
331 desc.fIsMipMapped = isMipMapped;
332
333 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
334
335 texels[0].fPixels = pixmap.addr();
336 texels[0].fRowBytes = pixmap.rowBytes();
337
338 for (int i = 1; i < mipLevelCount; ++i) {
339 SkMipMap::Level generatedMipLevel;
340 mipmaps->getLevel(i - 1, &generatedMipLevel);
341 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
342 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
343 }
344
345 return ctx->textureProvider()->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(),
346 mipLevelCount);
347}
348
reedb5d32632015-09-29 13:36:50 -0700349GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700350 const GrTextureParams& params) {
bsalomon89fe56b2015-10-29 10:49:28 -0700351 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800352 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
bsalomon89fe56b2015-10-29 10:49:28 -0700353 }
bsalomonb1b01992015-11-18 10:56:08 -0800354 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000355}
reed8f343722015-08-13 13:32:39 -0700356
rileya@google.com24f3ad12012-07-18 21:47:40 +0000357///////////////////////////////////////////////////////////////////////////////
358
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000359// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
360// alpha info, that will be considered.
brianosman9e3f1bf2016-03-17 12:26:37 -0700361GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt,
362 const GrCaps& caps) {
363 if (kSRGB_SkColorProfileType == pt && caps.srgbSupport()) {
364 switch (ct) {
365 case kRGBA_8888_SkColorType:
366 return kSRGBA_8888_GrPixelConfig;
367 case kBGRA_8888_SkColorType:
368 return kSBGRA_8888_GrPixelConfig;
369 default:
370 break;
371 }
372 } else {
373 switch (ct) {
374 case kUnknown_SkColorType:
375 return kUnknown_GrPixelConfig;
376 case kAlpha_8_SkColorType:
377 return kAlpha_8_GrPixelConfig;
378 case kRGB_565_SkColorType:
379 return kRGB_565_GrPixelConfig;
380 case kARGB_4444_SkColorType:
381 return kRGBA_4444_GrPixelConfig;
382 case kRGBA_8888_SkColorType:
383 return kRGBA_8888_GrPixelConfig;
384 case kBGRA_8888_SkColorType:
385 return kBGRA_8888_GrPixelConfig;
386 case kIndex_8_SkColorType:
387 return kIndex_8_GrPixelConfig;
388 case kGray_8_SkColorType:
389 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
390 case kRGBA_F16_SkColorType:
391 return kRGBA_half_GrPixelConfig;
392 }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000393 }
394 SkASSERT(0); // shouldn't get here
395 return kUnknown_GrPixelConfig;
396}
397
jvanverthfa1e8a72014-12-22 08:31:49 -0800398bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
399 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000400 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800401 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000402 switch (config) {
403 case kAlpha_8_GrPixelConfig:
404 ct = kAlpha_8_SkColorType;
405 break;
406 case kIndex_8_GrPixelConfig:
407 ct = kIndex_8_SkColorType;
408 break;
409 case kRGB_565_GrPixelConfig:
410 ct = kRGB_565_SkColorType;
411 break;
412 case kRGBA_4444_GrPixelConfig:
413 ct = kARGB_4444_SkColorType;
414 break;
415 case kRGBA_8888_GrPixelConfig:
416 ct = kRGBA_8888_SkColorType;
417 break;
418 case kBGRA_8888_GrPixelConfig:
419 ct = kBGRA_8888_SkColorType;
420 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800421 case kSRGBA_8888_GrPixelConfig:
422 ct = kRGBA_8888_SkColorType;
423 pt = kSRGB_SkColorProfileType;
424 break;
brianosman9e3f1bf2016-03-17 12:26:37 -0700425 case kSBGRA_8888_GrPixelConfig:
426 ct = kBGRA_8888_SkColorType;
427 pt = kSRGB_SkColorProfileType;
428 break;
reed@google.combf790232013-12-13 19:45:58 +0000429 default:
430 return false;
431 }
432 if (ctOut) {
433 *ctOut = ct;
434 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800435 if (ptOut) {
436 *ptOut = pt;
437 }
reed@google.combf790232013-12-13 19:45:58 +0000438 return true;
439}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000440
bsalomonf1b7a1d2015-09-28 06:26:28 -0700441////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000442
bsalomonaa48d362015-10-01 08:34:17 -0700443static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
444 if (primitiveIsSrc) {
445 return SkXfermode::kSrc_Mode != mode;
446 } else {
447 return SkXfermode::kDst_Mode != mode;
448 }
449}
450
bsalomonf1b7a1d2015-09-28 06:26:28 -0700451static inline bool skpaint_to_grpaint_impl(GrContext* context,
452 const SkPaint& skPaint,
453 const SkMatrix& viewM,
454 const GrFragmentProcessor** shaderProcessor,
455 SkXfermode::Mode* primColorMode,
456 bool primitiveIsSrc,
457 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000458 grPaint->setAntiAlias(skPaint.isAntiAlias());
459
bsalomonf1b7a1d2015-09-28 06:26:28 -0700460 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
461 // of per-vertex colors.
462 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700463 const GrFragmentProcessor* shaderFP = nullptr;
464 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
465 if (shaderProcessor) {
466 shaderFP = *shaderProcessor;
467 } else if (const SkShader* shader = skPaint.getShader()) {
468 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700469 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700470 shaderFP = aufp;
471 if (!shaderFP) {
472 return false;
473 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700474 }
475 }
476
477 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
478 // a known constant value. In that case we can simply apply a color filter during this
479 // conversion without converting the color filter to a GrFragmentProcessor.
480 bool applyColorFilterToPaintColor = false;
481 if (shaderFP) {
482 if (primColorMode) {
483 // There is a blend between the primitive color and the shader color. The shader sees
484 // the opaque paint color. The shader's output is blended using the provided mode by
485 // the primitive color. The blended color is then modulated by the paint's alpha.
486
487 // The geometry processor will insert the primitive color to start the color chain, so
488 // the GrPaint color will be ignored.
489
490 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
491
492 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
493 aufp.reset(shaderFP);
494
495 if (primitiveIsSrc) {
496 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
497 *primColorMode);
498 } else {
499 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
500 *primColorMode);
501 }
502 aufp.reset(shaderFP);
503 // The above may return null if compose results in a pass through of the prim color.
504 if (shaderFP) {
505 grPaint->addColorFragmentProcessor(shaderFP);
506 }
507
508 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
509 if (GrColor_WHITE != paintAlpha) {
510 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
511 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
512 }
513 } else {
514 // The shader's FP sees the paint unpremul color
515 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
516 grPaint->addColorFragmentProcessor(shaderFP);
517 }
518 } else {
519 if (primColorMode) {
520 // There is a blend between the primitive color and the paint color. The blend considers
521 // the opaque paint color. The paint's alpha is applied to the post-blended color.
522 SkAutoTUnref<const GrFragmentProcessor> processor(
523 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
524 GrConstColorProcessor::kIgnore_InputMode));
525 if (primitiveIsSrc) {
526 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
527 *primColorMode));
528 } else {
529 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
530 *primColorMode));
531
532 }
533 if (processor) {
534 grPaint->addColorFragmentProcessor(processor);
535 }
536
bsalomonaa48d362015-10-01 08:34:17 -0700537 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700538
539 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700540 if (GrColor_WHITE != paintAlpha) {
541 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
542 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
543 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700544 } else {
545 // No shader, no primitive color.
546 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
547 applyColorFilterToPaintColor = true;
548 }
549 }
550
551 SkColorFilter* colorFilter = skPaint.getColorFilter();
552 if (colorFilter) {
553 if (applyColorFilterToPaintColor) {
554 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
555 } else {
bsalomone25eea42015-09-29 06:38:55 -0700556 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700557 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700558 if (cfFP) {
559 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700560 } else {
561 return false;
562 }
563 }
564 }
565
robertphillips4f037942016-02-09 05:09:27 -0800566 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
567 // the GrPaint to also be null (also kSrcOver).
568 SkASSERT(!grPaint->getXPFactory());
569 SkXfermode* xfermode = skPaint.getXfermode();
570 if (xfermode) {
571 // SafeUnref in case a new xfermode is added that returns null.
572 // In such cases we will fall back to kSrcOver_Mode.
573 SkSafeUnref(grPaint->setXPFactory(xfermode->asXPFactory()));
574 }
mtklein775b8192014-12-02 09:11:25 -0800575
krajcevskif461a8f2014-06-19 14:14:06 -0700576#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700577 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700578 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700579 }
580#endif
bsalomonbed83a62015-04-15 14:18:34 -0700581 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000582}
583
bsalomonf1b7a1d2015-09-28 06:26:28 -0700584bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
585 GrPaint* grPaint) {
586 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
587}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000588
bsalomonf1b7a1d2015-09-28 06:26:28 -0700589/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
590bool SkPaintToGrPaintReplaceShader(GrContext* context,
591 const SkPaint& skPaint,
592 const GrFragmentProcessor* shaderFP,
593 GrPaint* grPaint) {
594 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700595 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000596 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700597 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
598 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000599}
reed8b26b992015-05-07 15:36:17 -0700600
bsalomonf1b7a1d2015-09-28 06:26:28 -0700601/** Ignores the SkShader (if any) on skPaint. */
602bool SkPaintToGrPaintNoShader(GrContext* context,
603 const SkPaint& skPaint,
604 GrPaint* grPaint) {
605 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
606 static const GrFragmentProcessor* kNullShaderFP = nullptr;
607 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
608 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
609 grPaint);
610}
611
612/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
613be setup as a vertex attribute using the specified SkXfermode::Mode. */
614bool SkPaintToGrPaintWithXfermode(GrContext* context,
615 const SkPaint& skPaint,
616 const SkMatrix& viewM,
617 SkXfermode::Mode primColorMode,
618 bool primitiveIsSrc,
619 GrPaint* grPaint) {
620 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
621 grPaint);
622}
623
joshualitt33a5fce2015-11-18 13:28:51 -0800624bool SkPaintToGrPaintWithTexture(GrContext* context,
625 const SkPaint& paint,
626 const SkMatrix& viewM,
627 const GrFragmentProcessor* fp,
628 bool textureIsAlphaOnly,
629 GrPaint* grPaint) {
630 SkAutoTUnref<const GrFragmentProcessor> shaderFP;
631 if (textureIsAlphaOnly) {
632 if (const SkShader* shader = paint.getShader()) {
633 shaderFP.reset(shader->asFragmentProcessor(context,
634 viewM,
635 nullptr,
636 paint.getFilterQuality()));
637 if (!shaderFP) {
638 return false;
639 }
640 const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp };
641 shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2));
642 } else {
643 shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
644 }
645 } else {
646 shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
647 }
648
649 return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), grPaint);
650}
651
bsalomonf1b7a1d2015-09-28 06:26:28 -0700652
653////////////////////////////////////////////////////////////////////////////////////////////////
654
reed8b26b992015-05-07 15:36:17 -0700655SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
656#ifdef SK_DEBUG
657 const GrSurfaceDesc& desc = tex->desc();
658 SkASSERT(w <= desc.fWidth);
659 SkASSERT(h <= desc.fHeight);
660#endif
661 const GrPixelConfig config = tex->config();
662 SkColorType ct;
663 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700664 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700665 ct = kUnknown_SkColorType;
666 }
667 return SkImageInfo::Make(w, h, ct, at);
668}
669
670
671void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
672 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
673 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700674 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700675}
joshualitt9bc39542015-08-12 12:57:54 -0700676
677GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
678 const SkMatrix& viewM,
679 const SkMatrix& localM,
680 bool* doBicubic) {
681 *doBicubic = false;
682 GrTextureParams::FilterMode textureFilterMode;
683 switch (paintFilterQuality) {
684 case kNone_SkFilterQuality:
685 textureFilterMode = GrTextureParams::kNone_FilterMode;
686 break;
687 case kLow_SkFilterQuality:
688 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
689 break;
690 case kMedium_SkFilterQuality: {
691 SkMatrix matrix;
692 matrix.setConcat(viewM, localM);
693 if (matrix.getMinScale() < SK_Scalar1) {
694 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
695 } else {
696 // Don't trigger MIP level generation unnecessarily.
697 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
698 }
699 break;
700 }
701 case kHigh_SkFilterQuality: {
702 SkMatrix matrix;
703 matrix.setConcat(viewM, localM);
704 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
705 break;
706 }
707 default:
708 SkErrorInternals::SetError( kInvalidPaint_SkError,
709 "Sorry, I don't understand the filtering "
710 "mode you asked for. Falling back to "
711 "MIPMaps.");
712 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
713 break;
714
715 }
716 return textureFilterMode;
717}