blob: 540edb6a38e728c6a5e1f018a2dc3840fda965a8 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reedb5d32632015-09-29 13:36:50 -07008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "SkGr.h"
egdaniel378092f2014-12-03 10:40:13 -080010
bsalomon76228632015-05-29 08:02:10 -070011#include "GrCaps.h"
bsalomonf276ac52015-10-09 13:36:42 -070012#include "GrContext.h"
bsalomon045802d2015-10-20 07:58:01 -070013#include "GrTextureParamsAdjuster.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
egdaniel378092f2014-12-03 10:40:13 -080016#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070017#include "GrYUVProvider.h"
18
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000019#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000020#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070021#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070022#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080023#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070024#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000025#include "SkMessageBus.h"
26#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080027#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070028#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080029#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070030#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070031#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070032#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080033#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070034#include "effects/GrXfermodeFragmentProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070035#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070036
krajcevski8c111f72014-06-02 13:51:34 -070037#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070038# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070039# include "etc1.h"
40#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomon466c2c42015-10-16 12:01:18 -070042GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
43 GrSurfaceDesc desc;
44 desc.fFlags = kNone_GrSurfaceFlags;
45 desc.fWidth = info.width();
46 desc.fHeight = info.height();
47 desc.fConfig = SkImageInfo2GrPixelConfig(info);
48 desc.fSampleCnt = 0;
49 return desc;
50}
51
bsalomon045802d2015-10-20 07:58:01 -070052void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
53 SkASSERT(key);
54 SkASSERT(imageID);
55 SkASSERT(!imageBounds.isEmpty());
56 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
57 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
bsalomon466c2c42015-10-16 12:01:18 -070058 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070059 builder[1] = imageBounds.fLeft;
60 builder[2] = imageBounds.fTop;
61 builder[3] = imageBounds.fRight;
62 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070063}
64
65GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
66 int expectedW, int expectedH,
67 const void** outStartOfDataToUpload) {
68 *outStartOfDataToUpload = nullptr;
69#ifndef SK_IGNORE_ETC1_SUPPORT
70 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
71 return kUnknown_GrPixelConfig;
72 }
73
74 const uint8_t* bytes = data->bytes();
75 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
76 // Does the data match the dimensions of the bitmap? If not,
77 // then we don't know how to scale the image to match it...
78 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
79 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
80 {
81 return kUnknown_GrPixelConfig;
82 }
83
84 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
85 return kETC1_GrPixelConfig;
86 } else if (SkKTXFile::is_ktx(bytes)) {
87 SkKTXFile ktx(data);
88
89 // Is it actually an ETC1 texture?
90 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
91 return kUnknown_GrPixelConfig;
92 }
93
94 // Does the data match the dimensions of the bitmap? If not,
95 // then we don't know how to scale the image to match it...
96 if (ktx.width() != expectedW || ktx.height() != expectedH) {
97 return kUnknown_GrPixelConfig;
98 }
99
100 *outStartOfDataToUpload = ktx.pixelData();
101 return kETC1_GrPixelConfig;
102 }
103#endif
104 return kUnknown_GrPixelConfig;
105}
106
bsalomon045802d2015-10-20 07:58:01 -0700107//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000108
bsalomon045802d2015-10-20 07:58:01 -0700109/**
110 * Fill out buffer with the compressed format Ganesh expects from a colortable
111 * based bitmap. [palette (colortable) + indices].
112 *
113 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
114 * we could detect that the colortable.count is <= 16, and then repack the
115 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
116 * slower than memcpy), so skipping that for now.
117 *
118 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
119 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 */
bsalomone79a2da2014-10-24 12:42:51 -0700121static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -0700122 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000123
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000124 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000126 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 return;
128 }
129
130 SkColorTable* ctable = bitmap.getColorTable();
131 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000132
reed@google.com7111d462014-03-25 16:20:24 +0000133 const int count = ctable->count();
134
135 SkDstPixelInfo dstPI;
136 dstPI.fColorType = kRGBA_8888_SkColorType;
137 dstPI.fAlphaType = kPremul_SkAlphaType;
138 dstPI.fPixels = buffer;
139 dstPI.fRowBytes = count * sizeof(SkPMColor);
140
141 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000142 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000143 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800144 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000145 srcPI.fRowBytes = count * sizeof(SkPMColor);
146
147 srcPI.convertPixelsTo(&dstPI, count, 1);
148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700150 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000152 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
154 } else {
155 // need to trim off the extra bytes per row
156 size_t width = bitmap.width();
157 size_t rowBytes = bitmap.rowBytes();
158 const char* src = (const char*)bitmap.getPixels();
159 for (int y = 0; y < bitmap.height(); y++) {
160 memcpy(dst, src, width);
161 src += rowBytes;
162 dst += width;
163 }
164 }
165}
166
bsalomon045802d2015-10-20 07:58:01 -0700167/**
reed43fe6182015-09-08 08:37:36 -0700168 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
169 * be gone from SkPixelRef, and we can remove this subclass entirely.
170 */
171class PixelRef_GrYUVProvider : public GrYUVProvider {
172 SkPixelRef* fPR;
173
174public:
175 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
176
177 uint32_t onGetID() override { return fPR->getGenerationID(); }
178 bool onGetYUVSizes(SkISize sizes[3]) override {
179 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
180 }
181 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
182 SkYUVColorSpace* space) override {
183 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
184 }
185};
krajcevski9c0e6292014-06-02 07:38:14 -0700186
bsalomon045802d2015-10-20 07:58:01 -0700187static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
188 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700189 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700190 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700191 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700192 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800193 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700195 }
196
reed43fe6182015-09-08 08:37:36 -0700197 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700198
bsalomon045802d2015-10-20 07:58:01 -0700199 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700200}
201
bsalomon045802d2015-10-20 07:58:01 -0700202static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700203 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
204 if (!data) {
205 return nullptr;
206 }
207
208 const void* startOfTexData;
209 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
210 &startOfTexData);
211 if (kUnknown_GrPixelConfig == desc.fConfig) {
212 return nullptr;
213 }
214
bsalomon045802d2015-10-20 07:58:01 -0700215 return ctx->textureProvider()->createTexture(desc, true, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700216}
217
bsalomon045802d2015-10-20 07:58:01 -0700218GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bmp) {
219 SkASSERT(!bmp.getTexture());
220
bsalomon045802d2015-10-20 07:58:01 -0700221 SkBitmap tmpBitmap;
222 const SkBitmap* bitmap = &bmp;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000223
reed3322a812015-09-16 10:09:24 -0700224 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700225 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000226
reed0689d7b2014-06-14 05:30:20 -0700227 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700228 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700229 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
230 bitmap->width(), bitmap->height());
231 SkAutoMalloc storage(imageSize);
bsalomon045802d2015-10-20 07:58:01 -0700232 build_index8_data(storage.get(), bmp);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
234 // our compressed data will be trimmed, so pass width() for its
235 // "rowBytes", since they are the same now.
bsalomon045802d2015-10-20 07:58:01 -0700236 return ctx->textureProvider()->createTexture(desc, true, storage.get(),
237 bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 } else {
bsalomon045802d2015-10-20 07:58:01 -0700239 bmp.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 // now bitmap points to our temp, which has been promoted to 32bits
241 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000242 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 }
reed43fe6182015-09-08 08:37:36 -0700244 } else if (!bitmap->readyToDraw()) {
245 // If the bitmap had compressed data and was then uncompressed, it'll still return
246 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
247 // the bitmap has available pixels, then they might not be what the decompressed
248 // data is.
bsalomon045802d2015-10-20 07:58:01 -0700249
250 // Really?? We aren't doing this with YUV.
251
252 GrTexture *texture = load_etc1_texture(ctx, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700253 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700254 return texture;
255 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000256 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000257
bsalomon045802d2015-10-20 07:58:01 -0700258 GrTexture *texture = create_texture_from_yuv(ctx, *bitmap, desc);
bsalomond2a6f4e2015-02-04 10:55:54 -0800259 if (texture) {
260 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700261 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800262
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000263 SkAutoLockPixels alp(*bitmap);
264 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700265 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000266 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000267
bsalomon045802d2015-10-20 07:58:01 -0700268 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels(),
269 bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800270}
271
bsalomonb4d40ef2015-07-15 10:12:16 -0700272
bsalomon045802d2015-10-20 07:58:01 -0700273////////////////////////////////////////////////////////////////////////////////
274
bsalomonc55271f2015-11-09 11:55:57 -0800275void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700276 class Invalidator : public SkPixelRef::GenIDChangeListener {
277 public:
278 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
279 private:
280 GrUniqueKeyInvalidatedMessage fMsg;
281
282 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
283 };
284
285 pixelRef->addGenIDChangeListener(new Invalidator(key));
286}
287
288class RasterBitmap_GrTextureMaker : public GrTextureMaker {
reedb5d32632015-09-29 13:36:50 -0700289public:
bsalomon89fe56b2015-10-29 10:49:28 -0700290 RasterBitmap_GrTextureMaker(const SkBitmap& bitmap)
reedb5d32632015-09-29 13:36:50 -0700291 : INHERITED(bitmap.width(), bitmap.height())
292 , fBitmap(bitmap)
bsalomon045802d2015-10-20 07:58:01 -0700293 {
bsalomon89fe56b2015-10-29 10:49:28 -0700294 SkASSERT(!bitmap.getTexture());
bsalomon045802d2015-10-20 07:58:01 -0700295 if (!bitmap.isVolatile()) {
296 SkIPoint origin = bitmap.pixelRefOrigin();
297 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
298 bitmap.height());
299 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
300 }
301 }
rileya@google.com24f3ad12012-07-18 21:47:40 +0000302
reedb5d32632015-09-29 13:36:50 -0700303protected:
bsalomon045802d2015-10-20 07:58:01 -0700304 GrTexture* refOriginalTexture(GrContext* ctx) override {
bsalomon89fe56b2015-10-29 10:49:28 -0700305 GrTexture* tex;
bsalomon88425562015-02-04 09:12:46 -0800306
bsalomon045802d2015-10-20 07:58:01 -0700307 if (fOriginalKey.isValid()) {
308 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
309 if (tex) {
310 return tex;
311 }
bsalomon1a197ea2015-10-19 08:24:08 -0700312 }
Brian Salomonbc0bcc02015-10-19 15:12:32 -0400313
bsalomon045802d2015-10-20 07:58:01 -0700314 tex = GrUploadBitmapToTexture(ctx, fBitmap);
315 if (tex && fOriginalKey.isValid()) {
316 tex->resourcePriv().setUniqueKey(fOriginalKey);
bsalomonc55271f2015-11-09 11:55:57 -0800317 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
bsalomon045802d2015-10-20 07:58:01 -0700318 }
319 return tex;
bsalomonfcffaf22015-10-16 13:35:10 -0700320 }
321
bsalomon045802d2015-10-20 07:58:01 -0700322 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) override {
323 if (fOriginalKey.isValid()) {
324 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
325 }
bsalomon1a197ea2015-10-19 08:24:08 -0700326 }
327
bsalomon045802d2015-10-20 07:58:01 -0700328 void didCacheCopy(const GrUniqueKey& copyKey) override {
bsalomonc55271f2015-11-09 11:55:57 -0800329 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
bsalomon045802d2015-10-20 07:58:01 -0700330 }
331
reedb5d32632015-09-29 13:36:50 -0700332private:
bsalomon045802d2015-10-20 07:58:01 -0700333 const SkBitmap fBitmap;
334 GrUniqueKey fOriginalKey;
335
bsalomon89fe56b2015-10-29 10:49:28 -0700336 typedef GrTextureMaker INHERITED;
337};
338
reedb5d32632015-09-29 13:36:50 -0700339GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700340 const GrTextureParams& params) {
bsalomon89fe56b2015-10-29 10:49:28 -0700341 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800342 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
bsalomon89fe56b2015-10-29 10:49:28 -0700343 }
344 return RasterBitmap_GrTextureMaker(bitmap).refTextureForParams(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000345}
reed8f343722015-08-13 13:32:39 -0700346
rileya@google.com24f3ad12012-07-18 21:47:40 +0000347///////////////////////////////////////////////////////////////////////////////
348
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000349// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
350// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800351GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000352 switch (ct) {
353 case kUnknown_SkColorType:
354 return kUnknown_GrPixelConfig;
355 case kAlpha_8_SkColorType:
356 return kAlpha_8_GrPixelConfig;
357 case kRGB_565_SkColorType:
358 return kRGB_565_GrPixelConfig;
359 case kARGB_4444_SkColorType:
360 return kRGBA_4444_GrPixelConfig;
361 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700362 //if (kSRGB_SkColorProfileType == pt) {
363 // return kSRGBA_8888_GrPixelConfig;
364 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000365 return kRGBA_8888_GrPixelConfig;
366 case kBGRA_8888_SkColorType:
367 return kBGRA_8888_GrPixelConfig;
368 case kIndex_8_SkColorType:
369 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700370 case kGray_8_SkColorType:
371 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000372 }
373 SkASSERT(0); // shouldn't get here
374 return kUnknown_GrPixelConfig;
375}
376
jvanverthfa1e8a72014-12-22 08:31:49 -0800377bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
378 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000379 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800380 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000381 switch (config) {
382 case kAlpha_8_GrPixelConfig:
383 ct = kAlpha_8_SkColorType;
384 break;
385 case kIndex_8_GrPixelConfig:
386 ct = kIndex_8_SkColorType;
387 break;
388 case kRGB_565_GrPixelConfig:
389 ct = kRGB_565_SkColorType;
390 break;
391 case kRGBA_4444_GrPixelConfig:
392 ct = kARGB_4444_SkColorType;
393 break;
394 case kRGBA_8888_GrPixelConfig:
395 ct = kRGBA_8888_SkColorType;
396 break;
397 case kBGRA_8888_GrPixelConfig:
398 ct = kBGRA_8888_SkColorType;
399 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800400 case kSRGBA_8888_GrPixelConfig:
401 ct = kRGBA_8888_SkColorType;
402 pt = kSRGB_SkColorProfileType;
403 break;
reed@google.combf790232013-12-13 19:45:58 +0000404 default:
405 return false;
406 }
407 if (ctOut) {
408 *ctOut = ct;
409 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800410 if (ptOut) {
411 *ptOut = pt;
412 }
reed@google.combf790232013-12-13 19:45:58 +0000413 return true;
414}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000415
bsalomonf1b7a1d2015-09-28 06:26:28 -0700416////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000417
bsalomonaa48d362015-10-01 08:34:17 -0700418static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
419 if (primitiveIsSrc) {
420 return SkXfermode::kSrc_Mode != mode;
421 } else {
422 return SkXfermode::kDst_Mode != mode;
423 }
424}
425
bsalomonf1b7a1d2015-09-28 06:26:28 -0700426static inline bool skpaint_to_grpaint_impl(GrContext* context,
427 const SkPaint& skPaint,
428 const SkMatrix& viewM,
429 const GrFragmentProcessor** shaderProcessor,
430 SkXfermode::Mode* primColorMode,
431 bool primitiveIsSrc,
432 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000433 grPaint->setAntiAlias(skPaint.isAntiAlias());
434
bsalomonf1b7a1d2015-09-28 06:26:28 -0700435 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
436 // of per-vertex colors.
437 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700438 const GrFragmentProcessor* shaderFP = nullptr;
439 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
440 if (shaderProcessor) {
441 shaderFP = *shaderProcessor;
442 } else if (const SkShader* shader = skPaint.getShader()) {
443 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700444 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700445 shaderFP = aufp;
446 if (!shaderFP) {
447 return false;
448 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700449 }
450 }
451
452 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
453 // a known constant value. In that case we can simply apply a color filter during this
454 // conversion without converting the color filter to a GrFragmentProcessor.
455 bool applyColorFilterToPaintColor = false;
456 if (shaderFP) {
457 if (primColorMode) {
458 // There is a blend between the primitive color and the shader color. The shader sees
459 // the opaque paint color. The shader's output is blended using the provided mode by
460 // the primitive color. The blended color is then modulated by the paint's alpha.
461
462 // The geometry processor will insert the primitive color to start the color chain, so
463 // the GrPaint color will be ignored.
464
465 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
466
467 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
468 aufp.reset(shaderFP);
469
470 if (primitiveIsSrc) {
471 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
472 *primColorMode);
473 } else {
474 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
475 *primColorMode);
476 }
477 aufp.reset(shaderFP);
478 // The above may return null if compose results in a pass through of the prim color.
479 if (shaderFP) {
480 grPaint->addColorFragmentProcessor(shaderFP);
481 }
482
483 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
484 if (GrColor_WHITE != paintAlpha) {
485 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
486 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
487 }
488 } else {
489 // The shader's FP sees the paint unpremul color
490 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
491 grPaint->addColorFragmentProcessor(shaderFP);
492 }
493 } else {
494 if (primColorMode) {
495 // There is a blend between the primitive color and the paint color. The blend considers
496 // the opaque paint color. The paint's alpha is applied to the post-blended color.
497 SkAutoTUnref<const GrFragmentProcessor> processor(
498 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
499 GrConstColorProcessor::kIgnore_InputMode));
500 if (primitiveIsSrc) {
501 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
502 *primColorMode));
503 } else {
504 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
505 *primColorMode));
506
507 }
508 if (processor) {
509 grPaint->addColorFragmentProcessor(processor);
510 }
511
bsalomonaa48d362015-10-01 08:34:17 -0700512 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700513
514 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700515 if (GrColor_WHITE != paintAlpha) {
516 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
517 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
518 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700519 } else {
520 // No shader, no primitive color.
521 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
522 applyColorFilterToPaintColor = true;
523 }
524 }
525
526 SkColorFilter* colorFilter = skPaint.getColorFilter();
527 if (colorFilter) {
528 if (applyColorFilterToPaintColor) {
529 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
530 } else {
bsalomone25eea42015-09-29 06:38:55 -0700531 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700532 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700533 if (cfFP) {
534 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700535 } else {
536 return false;
537 }
538 }
539 }
540
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000541 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700542 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800543 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000544 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700545 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800546 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000547 }
egdaniel378092f2014-12-03 10:40:13 -0800548 SkASSERT(xpFactory);
549 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800550
krajcevskif461a8f2014-06-19 14:14:06 -0700551#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700552 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700553 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700554 }
555#endif
bsalomonbed83a62015-04-15 14:18:34 -0700556 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000557}
558
bsalomonf1b7a1d2015-09-28 06:26:28 -0700559bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
560 GrPaint* grPaint) {
561 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
562}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000563
bsalomonf1b7a1d2015-09-28 06:26:28 -0700564/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
565bool SkPaintToGrPaintReplaceShader(GrContext* context,
566 const SkPaint& skPaint,
567 const GrFragmentProcessor* shaderFP,
568 GrPaint* grPaint) {
569 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700570 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000571 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700572 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
573 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000574}
reed8b26b992015-05-07 15:36:17 -0700575
bsalomonf1b7a1d2015-09-28 06:26:28 -0700576/** Ignores the SkShader (if any) on skPaint. */
577bool SkPaintToGrPaintNoShader(GrContext* context,
578 const SkPaint& skPaint,
579 GrPaint* grPaint) {
580 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
581 static const GrFragmentProcessor* kNullShaderFP = nullptr;
582 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
583 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
584 grPaint);
585}
586
587/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
588be setup as a vertex attribute using the specified SkXfermode::Mode. */
589bool SkPaintToGrPaintWithXfermode(GrContext* context,
590 const SkPaint& skPaint,
591 const SkMatrix& viewM,
592 SkXfermode::Mode primColorMode,
593 bool primitiveIsSrc,
594 GrPaint* grPaint) {
595 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
596 grPaint);
597}
598
599
600////////////////////////////////////////////////////////////////////////////////////////////////
601
reed8b26b992015-05-07 15:36:17 -0700602SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
603#ifdef SK_DEBUG
604 const GrSurfaceDesc& desc = tex->desc();
605 SkASSERT(w <= desc.fWidth);
606 SkASSERT(h <= desc.fHeight);
607#endif
608 const GrPixelConfig config = tex->config();
609 SkColorType ct;
610 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700611 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700612 ct = kUnknown_SkColorType;
613 }
614 return SkImageInfo::Make(w, h, ct, at);
615}
616
617
618void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
619 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
620 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700621 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700622}
joshualitt9bc39542015-08-12 12:57:54 -0700623
624GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
625 const SkMatrix& viewM,
626 const SkMatrix& localM,
627 bool* doBicubic) {
628 *doBicubic = false;
629 GrTextureParams::FilterMode textureFilterMode;
630 switch (paintFilterQuality) {
631 case kNone_SkFilterQuality:
632 textureFilterMode = GrTextureParams::kNone_FilterMode;
633 break;
634 case kLow_SkFilterQuality:
635 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
636 break;
637 case kMedium_SkFilterQuality: {
638 SkMatrix matrix;
639 matrix.setConcat(viewM, localM);
640 if (matrix.getMinScale() < SK_Scalar1) {
641 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
642 } else {
643 // Don't trigger MIP level generation unnecessarily.
644 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
645 }
646 break;
647 }
648 case kHigh_SkFilterQuality: {
649 SkMatrix matrix;
650 matrix.setConcat(viewM, localM);
651 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
652 break;
653 }
654 default:
655 SkErrorInternals::SetError( kInvalidPaint_SkError,
656 "Sorry, I don't understand the filtering "
657 "mode you asked for. Falling back to "
658 "MIPMaps.");
659 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
660 break;
661
662 }
663 return textureFilterMode;
664}