blob: a3848c77e01de508f9c22a96e8c4b2d0f69d8454 [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 "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080014#include "GrImageIDTextureAdjuster.h"
cblume55f2d2d2016-02-26 13:20:48 -080015#include "GrTextureParamsAdjuster.h"
16#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080017#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070018#include "GrYUVProvider.h"
19
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000020#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000021#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070022#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070023#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080024#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070025#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000026#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080027#include "SkMipMap.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000028#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080029#include "SkResourceCache.h"
cblume55f2d2d2016-02-26 13:20:48 -080030#include "SkTemplates.h"
krajcevski40a1e112014-08-05 14:13:36 -070031#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080032#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070033#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070034#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070035#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080036#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070037#include "effects/GrXfermodeFragmentProcessor.h"
bsalomonf267c1e2016-02-01 13:16:14 -080038#include "effects/GrYUVEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070039
krajcevski8c111f72014-06-02 13:51:34 -070040#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070041# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070042# include "etc1.h"
43#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000044
bsalomon466c2c42015-10-16 12:01:18 -070045GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
46 GrSurfaceDesc desc;
47 desc.fFlags = kNone_GrSurfaceFlags;
48 desc.fWidth = info.width();
49 desc.fHeight = info.height();
50 desc.fConfig = SkImageInfo2GrPixelConfig(info);
51 desc.fSampleCnt = 0;
52 return desc;
53}
54
bsalomon045802d2015-10-20 07:58:01 -070055void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
56 SkASSERT(key);
57 SkASSERT(imageID);
58 SkASSERT(!imageBounds.isEmpty());
59 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
60 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
bsalomon466c2c42015-10-16 12:01:18 -070061 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070062 builder[1] = imageBounds.fLeft;
63 builder[2] = imageBounds.fTop;
64 builder[3] = imageBounds.fRight;
65 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070066}
67
68GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
69 int expectedW, int expectedH,
70 const void** outStartOfDataToUpload) {
71 *outStartOfDataToUpload = nullptr;
72#ifndef SK_IGNORE_ETC1_SUPPORT
73 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
74 return kUnknown_GrPixelConfig;
75 }
76
77 const uint8_t* bytes = data->bytes();
78 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
79 // Does the data match the dimensions of the bitmap? If not,
80 // then we don't know how to scale the image to match it...
81 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
82 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
83 {
84 return kUnknown_GrPixelConfig;
85 }
86
87 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
88 return kETC1_GrPixelConfig;
89 } else if (SkKTXFile::is_ktx(bytes)) {
90 SkKTXFile ktx(data);
91
92 // Is it actually an ETC1 texture?
93 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
94 return kUnknown_GrPixelConfig;
95 }
96
97 // Does the data match the dimensions of the bitmap? If not,
98 // then we don't know how to scale the image to match it...
99 if (ktx.width() != expectedW || ktx.height() != expectedH) {
100 return kUnknown_GrPixelConfig;
101 }
102
103 *outStartOfDataToUpload = ktx.pixelData();
104 return kETC1_GrPixelConfig;
105 }
106#endif
107 return kUnknown_GrPixelConfig;
108}
109
bsalomon045802d2015-10-20 07:58:01 -0700110//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000111
bsalomon045802d2015-10-20 07:58:01 -0700112/**
113 * Fill out buffer with the compressed format Ganesh expects from a colortable
114 * based bitmap. [palette (colortable) + indices].
115 *
116 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
117 * we could detect that the colortable.count is <= 16, and then repack the
118 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
119 * slower than memcpy), so skipping that for now.
120 *
121 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
122 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 */
bsalomone79a2da2014-10-24 12:42:51 -0700124static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -0700125 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000126
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000127 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000129 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 return;
131 }
132
133 SkColorTable* ctable = bitmap.getColorTable();
134 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000135
reed@google.com7111d462014-03-25 16:20:24 +0000136 const int count = ctable->count();
137
138 SkDstPixelInfo dstPI;
139 dstPI.fColorType = kRGBA_8888_SkColorType;
140 dstPI.fAlphaType = kPremul_SkAlphaType;
141 dstPI.fPixels = buffer;
142 dstPI.fRowBytes = count * sizeof(SkPMColor);
143
144 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000145 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000146 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800147 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000148 srcPI.fRowBytes = count * sizeof(SkPMColor);
149
150 srcPI.convertPixelsTo(&dstPI, count, 1);
151
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700153 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000155 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000156 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
157 } else {
158 // need to trim off the extra bytes per row
159 size_t width = bitmap.width();
160 size_t rowBytes = bitmap.rowBytes();
161 const char* src = (const char*)bitmap.getPixels();
162 for (int y = 0; y < bitmap.height(); y++) {
163 memcpy(dst, src, width);
164 src += rowBytes;
165 dst += width;
166 }
167 }
168}
169
bsalomon045802d2015-10-20 07:58:01 -0700170/**
reed43fe6182015-09-08 08:37:36 -0700171 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
172 * be gone from SkPixelRef, and we can remove this subclass entirely.
173 */
174class PixelRef_GrYUVProvider : public GrYUVProvider {
175 SkPixelRef* fPR;
176
177public:
178 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
179
180 uint32_t onGetID() override { return fPR->getGenerationID(); }
181 bool onGetYUVSizes(SkISize sizes[3]) override {
182 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
183 }
184 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
185 SkYUVColorSpace* space) override {
186 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
187 }
188};
krajcevski9c0e6292014-06-02 07:38:14 -0700189
bsalomon045802d2015-10-20 07:58:01 -0700190static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
191 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700192 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700193 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700194 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700195 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800196 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700197 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700198 }
199
reed43fe6182015-09-08 08:37:36 -0700200 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700201
bsalomon045802d2015-10-20 07:58:01 -0700202 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700203}
204
bsalomon045802d2015-10-20 07:58:01 -0700205static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700206 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
207 if (!data) {
208 return nullptr;
209 }
210
211 const void* startOfTexData;
212 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
213 &startOfTexData);
214 if (kUnknown_GrPixelConfig == desc.fConfig) {
215 return nullptr;
216 }
217
bsalomon5ec26ae2016-02-25 08:33:02 -0800218 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700219}
220
bsalomon045802d2015-10-20 07:58:01 -0700221GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bmp) {
222 SkASSERT(!bmp.getTexture());
223
bsalomon045802d2015-10-20 07:58:01 -0700224 SkBitmap tmpBitmap;
225 const SkBitmap* bitmap = &bmp;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000226
reed3322a812015-09-16 10:09:24 -0700227 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700228 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000229
reed0689d7b2014-06-14 05:30:20 -0700230 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700231 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700232 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
233 bitmap->width(), bitmap->height());
234 SkAutoMalloc storage(imageSize);
bsalomon045802d2015-10-20 07:58:01 -0700235 build_index8_data(storage.get(), bmp);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
237 // our compressed data will be trimmed, so pass width() for its
238 // "rowBytes", since they are the same now.
bsalomon5ec26ae2016-02-25 08:33:02 -0800239 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, storage.get(),
bsalomon045802d2015-10-20 07:58:01 -0700240 bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 } else {
bsalomon045802d2015-10-20 07:58:01 -0700242 bmp.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 // now bitmap points to our temp, which has been promoted to 32bits
244 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000245 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 }
reed43fe6182015-09-08 08:37:36 -0700247 } else if (!bitmap->readyToDraw()) {
248 // If the bitmap had compressed data and was then uncompressed, it'll still return
249 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
250 // the bitmap has available pixels, then they might not be what the decompressed
251 // data is.
bsalomon045802d2015-10-20 07:58:01 -0700252
253 // Really?? We aren't doing this with YUV.
254
255 GrTexture *texture = load_etc1_texture(ctx, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700256 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700257 return texture;
258 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000259 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
bsalomon045802d2015-10-20 07:58:01 -0700261 GrTexture *texture = create_texture_from_yuv(ctx, *bitmap, desc);
bsalomond2a6f4e2015-02-04 10:55:54 -0800262 if (texture) {
263 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700264 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800265
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000266 SkAutoLockPixels alp(*bitmap);
267 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700268 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000269 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000270
bsalomon5ec26ae2016-02-25 08:33:02 -0800271 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, bitmap->getPixels(),
bsalomon045802d2015-10-20 07:58:01 -0700272 bitmap->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:
jvanverth99babf22015-05-22 06:06:40 -0700373 //if (kSRGB_SkColorProfileType == pt) {
374 // return kSRGBA_8888_GrPixelConfig;
375 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000376 return kRGBA_8888_GrPixelConfig;
377 case kBGRA_8888_SkColorType:
378 return kBGRA_8888_GrPixelConfig;
379 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}