blob: 4685c7699a63941cb12e05a79cb5af1e2eff00c6 [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"
egdaniel378092f2014-12-03 10:40:13 -080015#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070016#include "GrYUVProvider.h"
17
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000018#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000019#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070020#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070021#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080022#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070023#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000024#include "SkMessageBus.h"
25#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080026#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070027#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080028#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070029#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070030#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070031#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080032#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070033#include "effects/GrXfermodeFragmentProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070034#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070035
krajcevski8c111f72014-06-02 13:51:34 -070036#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070037# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070038# include "etc1.h"
39#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon466c2c42015-10-16 12:01:18 -070041GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
42 GrSurfaceDesc desc;
43 desc.fFlags = kNone_GrSurfaceFlags;
44 desc.fWidth = info.width();
45 desc.fHeight = info.height();
46 desc.fConfig = SkImageInfo2GrPixelConfig(info);
47 desc.fSampleCnt = 0;
48 return desc;
49}
50
bsalomon045802d2015-10-20 07:58:01 -070051void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
52 SkASSERT(key);
53 SkASSERT(imageID);
54 SkASSERT(!imageBounds.isEmpty());
55 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
56 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
bsalomon466c2c42015-10-16 12:01:18 -070057 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070058 builder[1] = imageBounds.fLeft;
59 builder[2] = imageBounds.fTop;
60 builder[3] = imageBounds.fRight;
61 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070062}
63
64GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
65 int expectedW, int expectedH,
66 const void** outStartOfDataToUpload) {
67 *outStartOfDataToUpload = nullptr;
68#ifndef SK_IGNORE_ETC1_SUPPORT
69 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
70 return kUnknown_GrPixelConfig;
71 }
72
73 const uint8_t* bytes = data->bytes();
74 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
75 // Does the data match the dimensions of the bitmap? If not,
76 // then we don't know how to scale the image to match it...
77 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
78 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
79 {
80 return kUnknown_GrPixelConfig;
81 }
82
83 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
84 return kETC1_GrPixelConfig;
85 } else if (SkKTXFile::is_ktx(bytes)) {
86 SkKTXFile ktx(data);
87
88 // Is it actually an ETC1 texture?
89 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
90 return kUnknown_GrPixelConfig;
91 }
92
93 // Does the data match the dimensions of the bitmap? If not,
94 // then we don't know how to scale the image to match it...
95 if (ktx.width() != expectedW || ktx.height() != expectedH) {
96 return kUnknown_GrPixelConfig;
97 }
98
99 *outStartOfDataToUpload = ktx.pixelData();
100 return kETC1_GrPixelConfig;
101 }
102#endif
103 return kUnknown_GrPixelConfig;
104}
105
bsalomon045802d2015-10-20 07:58:01 -0700106//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000107
bsalomon045802d2015-10-20 07:58:01 -0700108/**
109 * Fill out buffer with the compressed format Ganesh expects from a colortable
110 * based bitmap. [palette (colortable) + indices].
111 *
112 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
113 * we could detect that the colortable.count is <= 16, and then repack the
114 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
115 * slower than memcpy), so skipping that for now.
116 *
117 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
118 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 */
bsalomone79a2da2014-10-24 12:42:51 -0700120static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -0700121 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000122
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000123 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000125 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 return;
127 }
128
129 SkColorTable* ctable = bitmap.getColorTable();
130 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000131
reed@google.com7111d462014-03-25 16:20:24 +0000132 const int count = ctable->count();
133
134 SkDstPixelInfo dstPI;
135 dstPI.fColorType = kRGBA_8888_SkColorType;
136 dstPI.fAlphaType = kPremul_SkAlphaType;
137 dstPI.fPixels = buffer;
138 dstPI.fRowBytes = count * sizeof(SkPMColor);
139
140 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000141 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000142 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800143 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000144 srcPI.fRowBytes = count * sizeof(SkPMColor);
145
146 srcPI.convertPixelsTo(&dstPI, count, 1);
147
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700149 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000150
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000151 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
153 } else {
154 // need to trim off the extra bytes per row
155 size_t width = bitmap.width();
156 size_t rowBytes = bitmap.rowBytes();
157 const char* src = (const char*)bitmap.getPixels();
158 for (int y = 0; y < bitmap.height(); y++) {
159 memcpy(dst, src, width);
160 src += rowBytes;
161 dst += width;
162 }
163 }
164}
165
bsalomon045802d2015-10-20 07:58:01 -0700166/**
reed43fe6182015-09-08 08:37:36 -0700167 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
168 * be gone from SkPixelRef, and we can remove this subclass entirely.
169 */
170class PixelRef_GrYUVProvider : public GrYUVProvider {
171 SkPixelRef* fPR;
172
173public:
174 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
175
176 uint32_t onGetID() override { return fPR->getGenerationID(); }
177 bool onGetYUVSizes(SkISize sizes[3]) override {
178 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
179 }
180 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
181 SkYUVColorSpace* space) override {
182 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
183 }
184};
krajcevski9c0e6292014-06-02 07:38:14 -0700185
bsalomon045802d2015-10-20 07:58:01 -0700186static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
187 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700188 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700189 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700190 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700191 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800192 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700193 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700194 }
195
reed43fe6182015-09-08 08:37:36 -0700196 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700197
bsalomon045802d2015-10-20 07:58:01 -0700198 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700199}
200
bsalomon045802d2015-10-20 07:58:01 -0700201static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700202 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
203 if (!data) {
204 return nullptr;
205 }
206
207 const void* startOfTexData;
208 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
209 &startOfTexData);
210 if (kUnknown_GrPixelConfig == desc.fConfig) {
211 return nullptr;
212 }
213
bsalomon045802d2015-10-20 07:58:01 -0700214 return ctx->textureProvider()->createTexture(desc, true, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700215}
216
bsalomon045802d2015-10-20 07:58:01 -0700217GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bmp) {
218 SkASSERT(!bmp.getTexture());
219
bsalomon045802d2015-10-20 07:58:01 -0700220 SkBitmap tmpBitmap;
221 const SkBitmap* bitmap = &bmp;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000222
reed3322a812015-09-16 10:09:24 -0700223 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700224 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000225
reed0689d7b2014-06-14 05:30:20 -0700226 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700227 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700228 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
229 bitmap->width(), bitmap->height());
230 SkAutoMalloc storage(imageSize);
bsalomon045802d2015-10-20 07:58:01 -0700231 build_index8_data(storage.get(), bmp);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
233 // our compressed data will be trimmed, so pass width() for its
234 // "rowBytes", since they are the same now.
bsalomon045802d2015-10-20 07:58:01 -0700235 return ctx->textureProvider()->createTexture(desc, true, storage.get(),
236 bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 } else {
bsalomon045802d2015-10-20 07:58:01 -0700238 bmp.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 // now bitmap points to our temp, which has been promoted to 32bits
240 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000241 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 }
reed43fe6182015-09-08 08:37:36 -0700243 } else if (!bitmap->readyToDraw()) {
244 // If the bitmap had compressed data and was then uncompressed, it'll still return
245 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
246 // the bitmap has available pixels, then they might not be what the decompressed
247 // data is.
bsalomon045802d2015-10-20 07:58:01 -0700248
249 // Really?? We aren't doing this with YUV.
250
251 GrTexture *texture = load_etc1_texture(ctx, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700252 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700253 return texture;
254 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000255 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000256
bsalomon045802d2015-10-20 07:58:01 -0700257 GrTexture *texture = create_texture_from_yuv(ctx, *bitmap, desc);
bsalomond2a6f4e2015-02-04 10:55:54 -0800258 if (texture) {
259 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700260 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800261
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000262 SkAutoLockPixels alp(*bitmap);
263 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700264 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000265 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000266
bsalomon045802d2015-10-20 07:58:01 -0700267 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels(),
268 bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800269}
270
bsalomonb4d40ef2015-07-15 10:12:16 -0700271
bsalomon045802d2015-10-20 07:58:01 -0700272////////////////////////////////////////////////////////////////////////////////
273
bsalomon89fe56b2015-10-29 10:49:28 -0700274static void install_bmp_key_invalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
275 class Invalidator : public SkPixelRef::GenIDChangeListener {
276 public:
277 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
278 private:
279 GrUniqueKeyInvalidatedMessage fMsg;
280
281 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
282 };
283
284 pixelRef->addGenIDChangeListener(new Invalidator(key));
285}
286
287class RasterBitmap_GrTextureMaker : public GrTextureMaker {
reedb5d32632015-09-29 13:36:50 -0700288public:
bsalomon89fe56b2015-10-29 10:49:28 -0700289 RasterBitmap_GrTextureMaker(const SkBitmap& bitmap)
reedb5d32632015-09-29 13:36:50 -0700290 : INHERITED(bitmap.width(), bitmap.height())
291 , fBitmap(bitmap)
bsalomon045802d2015-10-20 07:58:01 -0700292 {
bsalomon89fe56b2015-10-29 10:49:28 -0700293 SkASSERT(!bitmap.getTexture());
bsalomon045802d2015-10-20 07:58:01 -0700294 if (!bitmap.isVolatile()) {
295 SkIPoint origin = bitmap.pixelRefOrigin();
296 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
297 bitmap.height());
298 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
299 }
300 }
rileya@google.com24f3ad12012-07-18 21:47:40 +0000301
reedb5d32632015-09-29 13:36:50 -0700302protected:
bsalomon045802d2015-10-20 07:58:01 -0700303 GrTexture* refOriginalTexture(GrContext* ctx) override {
bsalomon89fe56b2015-10-29 10:49:28 -0700304 GrTexture* tex;
bsalomon88425562015-02-04 09:12:46 -0800305
bsalomon045802d2015-10-20 07:58:01 -0700306 if (fOriginalKey.isValid()) {
307 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
308 if (tex) {
309 return tex;
310 }
bsalomon1a197ea2015-10-19 08:24:08 -0700311 }
Brian Salomonbc0bcc02015-10-19 15:12:32 -0400312
bsalomon045802d2015-10-20 07:58:01 -0700313 tex = GrUploadBitmapToTexture(ctx, fBitmap);
314 if (tex && fOriginalKey.isValid()) {
315 tex->resourcePriv().setUniqueKey(fOriginalKey);
bsalomon89fe56b2015-10-29 10:49:28 -0700316 install_bmp_key_invalidator(fOriginalKey, fBitmap.pixelRef());
bsalomon045802d2015-10-20 07:58:01 -0700317 }
318 return tex;
bsalomonfcffaf22015-10-16 13:35:10 -0700319 }
320
bsalomon045802d2015-10-20 07:58:01 -0700321 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) override {
322 if (fOriginalKey.isValid()) {
323 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
324 }
bsalomon1a197ea2015-10-19 08:24:08 -0700325 }
326
bsalomon045802d2015-10-20 07:58:01 -0700327 void didCacheCopy(const GrUniqueKey& copyKey) override {
bsalomon89fe56b2015-10-29 10:49:28 -0700328 install_bmp_key_invalidator(copyKey, fBitmap.pixelRef());
bsalomon045802d2015-10-20 07:58:01 -0700329 }
330
reedb5d32632015-09-29 13:36:50 -0700331private:
bsalomon045802d2015-10-20 07:58:01 -0700332 const SkBitmap fBitmap;
333 GrUniqueKey fOriginalKey;
334
bsalomon89fe56b2015-10-29 10:49:28 -0700335 typedef GrTextureMaker INHERITED;
336};
337
338class TextureBitmap_GrTextureAdjuster : public GrTextureAdjuster {
339public:
340 explicit TextureBitmap_GrTextureAdjuster(const SkBitmap* bmp)
341 : INHERITED(bmp->getTexture(), SkIRect::MakeWH(bmp->width(), bmp->height()))
342 , fBmp(bmp) {}
343
344private:
345 void makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) override {
346 if (fBmp->isVolatile()) {
347 return;
348 }
349 // The texture subset must represent the whole bitmap. Texture-backed bitmaps don't support
350 // extractSubset(). Therefore, either the bitmap and the teture are the same size or the
351 // subset's dimensions are the bitmap's dimensions.
352 GrUniqueKey baseKey;
353 GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(),
354 SkIRect::MakeWH(fBmp->width(), fBmp->height()));
355 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
356 }
357
358 void didCacheCopy(const GrUniqueKey& copyKey) override {
359 install_bmp_key_invalidator(copyKey, fBmp->pixelRef());
360 }
361
362 const SkBitmap* fBmp;
363
364 typedef GrTextureAdjuster INHERITED;
reedb5d32632015-09-29 13:36:50 -0700365};
366
367GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700368 const GrTextureParams& params) {
bsalomon89fe56b2015-10-29 10:49:28 -0700369 if (bitmap.getTexture()) {
370 return TextureBitmap_GrTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
371 }
372 return RasterBitmap_GrTextureMaker(bitmap).refTextureForParams(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000373}
reed8f343722015-08-13 13:32:39 -0700374
rileya@google.com24f3ad12012-07-18 21:47:40 +0000375///////////////////////////////////////////////////////////////////////////////
376
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000377// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
378// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800379GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000380 switch (ct) {
381 case kUnknown_SkColorType:
382 return kUnknown_GrPixelConfig;
383 case kAlpha_8_SkColorType:
384 return kAlpha_8_GrPixelConfig;
385 case kRGB_565_SkColorType:
386 return kRGB_565_GrPixelConfig;
387 case kARGB_4444_SkColorType:
388 return kRGBA_4444_GrPixelConfig;
389 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700390 //if (kSRGB_SkColorProfileType == pt) {
391 // return kSRGBA_8888_GrPixelConfig;
392 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000393 return kRGBA_8888_GrPixelConfig;
394 case kBGRA_8888_SkColorType:
395 return kBGRA_8888_GrPixelConfig;
396 case kIndex_8_SkColorType:
397 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700398 case kGray_8_SkColorType:
399 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000400 }
401 SkASSERT(0); // shouldn't get here
402 return kUnknown_GrPixelConfig;
403}
404
jvanverthfa1e8a72014-12-22 08:31:49 -0800405bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
406 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000407 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800408 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000409 switch (config) {
410 case kAlpha_8_GrPixelConfig:
411 ct = kAlpha_8_SkColorType;
412 break;
413 case kIndex_8_GrPixelConfig:
414 ct = kIndex_8_SkColorType;
415 break;
416 case kRGB_565_GrPixelConfig:
417 ct = kRGB_565_SkColorType;
418 break;
419 case kRGBA_4444_GrPixelConfig:
420 ct = kARGB_4444_SkColorType;
421 break;
422 case kRGBA_8888_GrPixelConfig:
423 ct = kRGBA_8888_SkColorType;
424 break;
425 case kBGRA_8888_GrPixelConfig:
426 ct = kBGRA_8888_SkColorType;
427 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800428 case kSRGBA_8888_GrPixelConfig:
429 ct = kRGBA_8888_SkColorType;
430 pt = kSRGB_SkColorProfileType;
431 break;
reed@google.combf790232013-12-13 19:45:58 +0000432 default:
433 return false;
434 }
435 if (ctOut) {
436 *ctOut = ct;
437 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800438 if (ptOut) {
439 *ptOut = pt;
440 }
reed@google.combf790232013-12-13 19:45:58 +0000441 return true;
442}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000443
bsalomonf1b7a1d2015-09-28 06:26:28 -0700444////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000445
bsalomonaa48d362015-10-01 08:34:17 -0700446static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
447 if (primitiveIsSrc) {
448 return SkXfermode::kSrc_Mode != mode;
449 } else {
450 return SkXfermode::kDst_Mode != mode;
451 }
452}
453
bsalomonf1b7a1d2015-09-28 06:26:28 -0700454static inline bool skpaint_to_grpaint_impl(GrContext* context,
455 const SkPaint& skPaint,
456 const SkMatrix& viewM,
457 const GrFragmentProcessor** shaderProcessor,
458 SkXfermode::Mode* primColorMode,
459 bool primitiveIsSrc,
460 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000461 grPaint->setAntiAlias(skPaint.isAntiAlias());
462
bsalomonf1b7a1d2015-09-28 06:26:28 -0700463 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
464 // of per-vertex colors.
465 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700466 const GrFragmentProcessor* shaderFP = nullptr;
467 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
468 if (shaderProcessor) {
469 shaderFP = *shaderProcessor;
470 } else if (const SkShader* shader = skPaint.getShader()) {
471 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700472 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700473 shaderFP = aufp;
474 if (!shaderFP) {
475 return false;
476 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700477 }
478 }
479
480 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
481 // a known constant value. In that case we can simply apply a color filter during this
482 // conversion without converting the color filter to a GrFragmentProcessor.
483 bool applyColorFilterToPaintColor = false;
484 if (shaderFP) {
485 if (primColorMode) {
486 // There is a blend between the primitive color and the shader color. The shader sees
487 // the opaque paint color. The shader's output is blended using the provided mode by
488 // the primitive color. The blended color is then modulated by the paint's alpha.
489
490 // The geometry processor will insert the primitive color to start the color chain, so
491 // the GrPaint color will be ignored.
492
493 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
494
495 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
496 aufp.reset(shaderFP);
497
498 if (primitiveIsSrc) {
499 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
500 *primColorMode);
501 } else {
502 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
503 *primColorMode);
504 }
505 aufp.reset(shaderFP);
506 // The above may return null if compose results in a pass through of the prim color.
507 if (shaderFP) {
508 grPaint->addColorFragmentProcessor(shaderFP);
509 }
510
511 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
512 if (GrColor_WHITE != paintAlpha) {
513 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
514 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
515 }
516 } else {
517 // The shader's FP sees the paint unpremul color
518 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
519 grPaint->addColorFragmentProcessor(shaderFP);
520 }
521 } else {
522 if (primColorMode) {
523 // There is a blend between the primitive color and the paint color. The blend considers
524 // the opaque paint color. The paint's alpha is applied to the post-blended color.
525 SkAutoTUnref<const GrFragmentProcessor> processor(
526 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
527 GrConstColorProcessor::kIgnore_InputMode));
528 if (primitiveIsSrc) {
529 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
530 *primColorMode));
531 } else {
532 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
533 *primColorMode));
534
535 }
536 if (processor) {
537 grPaint->addColorFragmentProcessor(processor);
538 }
539
bsalomonaa48d362015-10-01 08:34:17 -0700540 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700541
542 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700543 if (GrColor_WHITE != paintAlpha) {
544 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
545 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
546 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700547 } else {
548 // No shader, no primitive color.
549 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
550 applyColorFilterToPaintColor = true;
551 }
552 }
553
554 SkColorFilter* colorFilter = skPaint.getColorFilter();
555 if (colorFilter) {
556 if (applyColorFilterToPaintColor) {
557 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
558 } else {
bsalomone25eea42015-09-29 06:38:55 -0700559 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700560 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700561 if (cfFP) {
562 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700563 } else {
564 return false;
565 }
566 }
567 }
568
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000569 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700570 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800571 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000572 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700573 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800574 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000575 }
egdaniel378092f2014-12-03 10:40:13 -0800576 SkASSERT(xpFactory);
577 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800578
krajcevskif461a8f2014-06-19 14:14:06 -0700579#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700580 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700581 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700582 }
583#endif
bsalomonbed83a62015-04-15 14:18:34 -0700584 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000585}
586
bsalomonf1b7a1d2015-09-28 06:26:28 -0700587bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
588 GrPaint* grPaint) {
589 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
590}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000591
bsalomonf1b7a1d2015-09-28 06:26:28 -0700592/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
593bool SkPaintToGrPaintReplaceShader(GrContext* context,
594 const SkPaint& skPaint,
595 const GrFragmentProcessor* shaderFP,
596 GrPaint* grPaint) {
597 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700598 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000599 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700600 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
601 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000602}
reed8b26b992015-05-07 15:36:17 -0700603
bsalomonf1b7a1d2015-09-28 06:26:28 -0700604/** Ignores the SkShader (if any) on skPaint. */
605bool SkPaintToGrPaintNoShader(GrContext* context,
606 const SkPaint& skPaint,
607 GrPaint* grPaint) {
608 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
609 static const GrFragmentProcessor* kNullShaderFP = nullptr;
610 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
611 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
612 grPaint);
613}
614
615/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
616be setup as a vertex attribute using the specified SkXfermode::Mode. */
617bool SkPaintToGrPaintWithXfermode(GrContext* context,
618 const SkPaint& skPaint,
619 const SkMatrix& viewM,
620 SkXfermode::Mode primColorMode,
621 bool primitiveIsSrc,
622 GrPaint* grPaint) {
623 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
624 grPaint);
625}
626
627
628////////////////////////////////////////////////////////////////////////////////////////////////
629
reed8b26b992015-05-07 15:36:17 -0700630SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
631#ifdef SK_DEBUG
632 const GrSurfaceDesc& desc = tex->desc();
633 SkASSERT(w <= desc.fWidth);
634 SkASSERT(h <= desc.fHeight);
635#endif
636 const GrPixelConfig config = tex->config();
637 SkColorType ct;
638 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700639 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700640 ct = kUnknown_SkColorType;
641 }
642 return SkImageInfo::Make(w, h, ct, at);
643}
644
645
646void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
647 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
648 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700649 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700650}
joshualitt9bc39542015-08-12 12:57:54 -0700651
652GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
653 const SkMatrix& viewM,
654 const SkMatrix& localM,
655 bool* doBicubic) {
656 *doBicubic = false;
657 GrTextureParams::FilterMode textureFilterMode;
658 switch (paintFilterQuality) {
659 case kNone_SkFilterQuality:
660 textureFilterMode = GrTextureParams::kNone_FilterMode;
661 break;
662 case kLow_SkFilterQuality:
663 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
664 break;
665 case kMedium_SkFilterQuality: {
666 SkMatrix matrix;
667 matrix.setConcat(viewM, localM);
668 if (matrix.getMinScale() < SK_Scalar1) {
669 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
670 } else {
671 // Don't trigger MIP level generation unnecessarily.
672 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
673 }
674 break;
675 }
676 case kHigh_SkFilterQuality: {
677 SkMatrix matrix;
678 matrix.setConcat(viewM, localM);
679 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
680 break;
681 }
682 default:
683 SkErrorInternals::SetError( kInvalidPaint_SkError,
684 "Sorry, I don't understand the filtering "
685 "mode you asked for. Falling back to "
686 "MIPMaps.");
687 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
688 break;
689
690 }
691 return textureFilterMode;
692}