blob: a1b379fe8c83dd562bd22cd793a67fd2e0e772aa [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
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "SkGr.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00009#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000010#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070011#include "SkData.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000012#include "SkMessageBus.h"
13#include "SkPixelRef.h"
14#include "GrResourceCache.h"
krajcevski9c0e6292014-06-02 07:38:14 -070015#include "GrGpu.h"
krajcevskif461a8f2014-06-19 14:14:06 -070016#include "effects/GrDitherEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070017#include "GrDrawTargetCaps.h"
18
krajcevski8c111f72014-06-02 13:51:34 -070019#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070020# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070021# include "etc1.h"
22#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24/* Fill out buffer with the compressed format Ganesh expects from a colortable
25 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000026
27 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000028 we could detect that the colortable.count is <= 16, and then repack the
29 indices as nibbles to save RAM, but it would take more time (i.e. a lot
30 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000031
reed@google.comac10a2d2010-12-22 21:39:39 +000032 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
33 as the colortable.count says it is.
34 */
35static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070036 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000037
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000038 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000039 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000040 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000041 return;
42 }
43
44 SkColorTable* ctable = bitmap.getColorTable();
45 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000046
reed@google.com7111d462014-03-25 16:20:24 +000047 const int count = ctable->count();
48
49 SkDstPixelInfo dstPI;
50 dstPI.fColorType = kRGBA_8888_SkColorType;
51 dstPI.fAlphaType = kPremul_SkAlphaType;
52 dstPI.fPixels = buffer;
53 dstPI.fRowBytes = count * sizeof(SkPMColor);
54
55 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000056 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000057 srcPI.fAlphaType = kPremul_SkAlphaType;
58 srcPI.fPixels = ctable->lockColors();
59 srcPI.fRowBytes = count * sizeof(SkPMColor);
60
61 srcPI.convertPixelsTo(&dstPI, count, 1);
62
reed@google.com0a6151d2013-10-10 14:44:56 +000063 ctable->unlockColors();
bsalomon@google.com5782d712011-01-21 21:03:59 +000064
reed@google.comac10a2d2010-12-22 21:39:39 +000065 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000066 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000067
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000068 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000069 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
70 } else {
71 // need to trim off the extra bytes per row
72 size_t width = bitmap.width();
73 size_t rowBytes = bitmap.rowBytes();
74 const char* src = (const char*)bitmap.getPixels();
75 for (int y = 0; y < bitmap.height(); y++) {
76 memcpy(dst, src, width);
77 src += rowBytes;
78 dst += width;
79 }
80 }
81}
82
83////////////////////////////////////////////////////////////////////////////////
84
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000085static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) {
86 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
87 // are unique.
88 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +000089 SkIPoint origin = bitmap.pixelRefOrigin();
90 int16_t width = SkToS16(bitmap.width());
91 int16_t height = SkToS16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000092
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +000093 GrCacheID::Key key;
reed@google.com672588b2014-01-08 15:42:01 +000094 memcpy(key.fData8 + 0, &genID, 4);
95 memcpy(key.fData8 + 4, &origin.fX, 4);
96 memcpy(key.fData8 + 8, &origin.fY, 4);
97 memcpy(key.fData8 + 12, &width, 2);
98 memcpy(key.fData8 + 14, &height, 2);
99 static const size_t kKeyDataSize = 16;
bsalomon@google.com10a9fb82013-01-02 19:29:57 +0000100 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
reed@google.com672588b2014-01-08 15:42:01 +0000101 GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000102 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
103 id->reset(gBitmapTextureDomain, key);
104}
105
106static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
107 desc->fFlags = kNone_GrTextureFlags;
108 desc->fWidth = bitmap.width();
109 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000110 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000111 desc->fSampleCnt = 0;
112}
113
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000114namespace {
115
116// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
117class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
118public:
119 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
120private:
121 GrResourceKey fKey;
122
123 virtual void onChange() SK_OVERRIDE {
124 const GrResourceInvalidatedMessage message = { fKey };
125 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
126 }
127};
128
129} // namespace
130
131static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
132 SkASSERT(NULL != pixelRef);
133 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
134}
135
krajcevski8c111f72014-06-02 13:51:34 -0700136#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700137static GrTexture *load_etc1_texture(GrContext* ctx,
138 const GrTextureParams* params,
139 const SkBitmap &bm, GrTextureDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700140 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700141
142 // Is this even encoded data?
143 if (NULL == data) {
144 return NULL;
145 }
146
147 // Is this a valid PKM encoded data?
148 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700149 if (etc1_pkm_is_valid(bytes)) {
150 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
151 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
152
153 // Does the data match the dimensions of the bitmap? If not,
154 // then we don't know how to scale the image to match it...
155 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
156 encodedHeight != static_cast<uint32_t>(bm.height())) {
157 return NULL;
158 }
159
160 // Everything seems good... skip ahead to the data.
161 bytes += ETC_PKM_HEADER_SIZE;
162 desc.fConfig = kETC1_GrPixelConfig;
163 } else if (SkKTXFile::is_ktx(bytes)) {
164 SkKTXFile ktx(data);
165
166 // Is it actually an ETC1 texture?
167 if (!ktx.isETC1()) {
168 return NULL;
169 }
170
171 // Does the data match the dimensions of the bitmap? If not,
172 // then we don't know how to scale the image to match it...
173 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
174 return NULL;
175 }
176
177 bytes = ktx.pixelData();
178 desc.fConfig = kETC1_GrPixelConfig;
179 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700180 return NULL;
181 }
182
krajcevski9c0e6292014-06-02 07:38:14 -0700183 // This texture is likely to be used again so leave it in the cache
184 GrCacheID cacheID;
185 generate_bitmap_cache_id(bm, &cacheID);
186
187 GrResourceKey key;
188 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key);
189 if (NULL != result) {
190 add_genID_listener(key, bm.pixelRef());
191 }
192 return result;
193}
krajcevski8c111f72014-06-02 13:51:34 -0700194#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700195
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000196static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000197 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000198 const GrTextureParams* params,
199 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 SkBitmap tmpBitmap;
201
202 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000203
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000204 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000205 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000206
reed0689d7b2014-06-14 05:30:20 -0700207 if (kIndex_8_SkColorType == bitmap->colorType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 // build_compressed_data doesn't do npot->pot expansion
209 // and paletted textures can't be sub-updated
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000210 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
211 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000213
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 build_compressed_data(storage.get(), origBitmap);
215
216 // our compressed data will be trimmed, so pass width() for its
217 // "rowBytes", since they are the same now.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000218
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000219 if (cache) {
220 GrCacheID cacheID;
221 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000222
223 GrResourceKey key;
224 GrTexture* result = ctx->createTexture(params, desc, cacheID,
225 storage.get(), bitmap->width(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000226 if (NULL != result) {
227 add_genID_listener(key, origBitmap.pixelRef());
228 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000229 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000230 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000231 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000232 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000233 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000234 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000235 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000236 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000237 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000239 origBitmap.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 }
krajcevski309e8692014-06-02 08:02:45 -0700244 }
krajcevski9c0e6292014-06-02 07:38:14 -0700245
246 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700247#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9a3cdbb2014-06-05 07:03:39 -0700248 else if (
249 // We do not support scratch ETC1 textures, hence they should all be at least
250 // trying to go to the cache.
251 cache
252 // Make sure that the underlying device supports ETC1 textures before we go ahead
253 // and check the data.
254 && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig)
255 // If the bitmap had compressed data and was then uncompressed, it'll still return
256 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
257 // the bitmap has available pixels, then they might not be what the decompressed
258 // data is.
259 && !(bitmap->readyToDraw())) {
krajcevski9c0e6292014-06-02 07:38:14 -0700260 GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc);
261 if (NULL != texture) {
262 return texture;
263 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000264 }
krajcevski8c111f72014-06-02 13:51:34 -0700265#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000266
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000267 SkAutoLockPixels alp(*bitmap);
268 if (!bitmap->readyToDraw()) {
269 return NULL;
270 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000271 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000272 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000273 GrCacheID cacheID;
274 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000275
276 GrResourceKey key;
277 GrTexture* result = ctx->createTexture(params, desc, cacheID,
278 bitmap->getPixels(), bitmap->rowBytes(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000279 if (NULL != result) {
280 add_genID_listener(key, origBitmap.pixelRef());
281 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000282 return result;
283 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284 // This texture is unlikely to be used again (in its present form) so
285 // just use a scratch texture. This will remove the texture from the
286 // cache so no one else can find it. Additionally, once unlocked, the
287 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000288 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000289 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000290 result->writePixels(0, 0,
291 bitmap->width(), bitmap->height(),
292 desc.fConfig,
293 bitmap->getPixels(),
294 bitmap->rowBytes());
295 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000296 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000297}
298
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000299bool GrIsBitmapInCache(const GrContext* ctx,
300 const SkBitmap& bitmap,
301 const GrTextureParams* params) {
302 GrCacheID cacheID;
303 generate_bitmap_cache_id(bitmap, &cacheID);
304
305 GrTextureDesc desc;
306 generate_bitmap_texture_desc(bitmap, &desc);
307 return ctx->isTextureInCache(desc, cacheID, params);
308}
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000310GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
311 const SkBitmap& bitmap,
312 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000313 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000314
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000315 bool cache = !bitmap.isVolatile();
316
317 if (cache) {
318 // If the bitmap isn't changing try to find a cached copy first.
319
320 GrCacheID cacheID;
321 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000322
323 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000324 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000325
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000326 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000327 }
328 if (NULL == result) {
329 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000330 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000331 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000332 GrPrintf("---- failed to create texture for cache [%d %d]\n",
333 bitmap.width(), bitmap.height());
334 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000335 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000336}
337
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000338void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000339 SkASSERT(NULL != texture->getContext());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000340
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000341 texture->getContext()->unlockScratchTexture(texture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000342 texture->unref();
rileya@google.com24f3ad12012-07-18 21:47:40 +0000343}
344
345///////////////////////////////////////////////////////////////////////////////
346
reedc3b32662014-06-17 08:38:31 -0700347#ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
rileya@google.com24f3ad12012-07-18 21:47:40 +0000348GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 switch (config) {
350 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000351 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000353 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000355 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000356 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000357 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000358 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000359 return kSkia8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000360 default:
reed@google.com2cb14802013-06-26 14:35:02 +0000361 // kNo_Config, kA1_Config missing
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000362 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 }
364}
reedc3b32662014-06-17 08:38:31 -0700365#endif
reed@google.combf790232013-12-13 19:45:58 +0000366
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000367// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
368// alpha info, that will be considered.
369GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) {
370 switch (ct) {
371 case kUnknown_SkColorType:
372 return kUnknown_GrPixelConfig;
373 case kAlpha_8_SkColorType:
374 return kAlpha_8_GrPixelConfig;
375 case kRGB_565_SkColorType:
376 return kRGB_565_GrPixelConfig;
377 case kARGB_4444_SkColorType:
378 return kRGBA_4444_GrPixelConfig;
379 case kRGBA_8888_SkColorType:
380 return kRGBA_8888_GrPixelConfig;
381 case kBGRA_8888_SkColorType:
382 return kBGRA_8888_GrPixelConfig;
383 case kIndex_8_SkColorType:
384 return kIndex_8_GrPixelConfig;
385 }
386 SkASSERT(0); // shouldn't get here
387 return kUnknown_GrPixelConfig;
388}
389
reed@google.combf790232013-12-13 19:45:58 +0000390bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) {
391 SkColorType ct;
392 switch (config) {
393 case kAlpha_8_GrPixelConfig:
394 ct = kAlpha_8_SkColorType;
395 break;
396 case kIndex_8_GrPixelConfig:
397 ct = kIndex_8_SkColorType;
398 break;
399 case kRGB_565_GrPixelConfig:
400 ct = kRGB_565_SkColorType;
401 break;
402 case kRGBA_4444_GrPixelConfig:
403 ct = kARGB_4444_SkColorType;
404 break;
405 case kRGBA_8888_GrPixelConfig:
406 ct = kRGBA_8888_SkColorType;
407 break;
408 case kBGRA_8888_GrPixelConfig:
409 ct = kBGRA_8888_SkColorType;
410 break;
411 default:
412 return false;
413 }
414 if (ctOut) {
415 *ctOut = ct;
416 }
417 return true;
418}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000419
420///////////////////////////////////////////////////////////////////////////////
421
dandov9de5b512014-06-10 14:38:28 -0700422void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor grColor,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000423 bool constantColor, GrPaint* grPaint) {
424
425 grPaint->setDither(skPaint.isDither());
426 grPaint->setAntiAlias(skPaint.isAntiAlias());
427
428 SkXfermode::Coeff sm;
429 SkXfermode::Coeff dm;
430
431 SkXfermode* mode = skPaint.getXfermode();
432 GrEffectRef* xferEffect = NULL;
433 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
434 if (NULL != xferEffect) {
435 grPaint->addColorEffect(xferEffect)->unref();
436 sm = SkXfermode::kOne_Coeff;
437 dm = SkXfermode::kZero_Coeff;
438 }
439 } else {
440 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
441 // Fall back to src-over
442 sm = SkXfermode::kOne_Coeff;
443 dm = SkXfermode::kISA_Coeff;
444 }
445 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
dandov9de5b512014-06-10 14:38:28 -0700446
447 //set the color of the paint to the one of the parameter
448 grPaint->setColor(grColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000449
450 SkColorFilter* colorFilter = skPaint.getColorFilter();
451 if (NULL != colorFilter) {
452 // if the source color is a constant then apply the filter here once rather than per pixel
453 // in a shader.
454 if (constantColor) {
455 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
456 grPaint->setColor(SkColor2GrColor(filtered));
457 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000458 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(context));
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000459 if (NULL != effect.get()) {
460 grPaint->addColorEffect(effect);
461 }
462 }
463 }
krajcevskif461a8f2014-06-19 14:14:06 -0700464
465#ifndef SK_IGNORE_GPU_DITHER
466 // If the dither flag is set, then we need to see if the underlying context
467 // supports it. If not, then install a dither effect.
468 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
469 // What are we rendering into?
470 const GrRenderTarget *target = context->getRenderTarget();
471 SkASSERT(NULL != target);
472
473 // Suspect the dithering flag has no effect on these configs, otherwise
474 // fall back on setting the appropriate state.
475 if (target->config() == kRGBA_8888_GrPixelConfig ||
476 target->config() == kBGRA_8888_GrPixelConfig) {
477 // The dither flag is set and the target is likely
478 // not going to be dithered by the GPU.
479 SkAutoTUnref<GrEffectRef> effect(GrDitherEffect::Create());
480 if (NULL != effect.get()) {
481 grPaint->addColorEffect(effect);
482 grPaint->setDither(false);
483 }
484 }
485 }
486#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000487}
488
commit-bot@chromium.org9e967ad2014-05-20 15:06:29 +0000489/**
490 * Unlike GrContext::AutoMatrix, this doesn't require setting a new matrix. GrContext::AutoMatrix
491 * likes to set the new matrix in its constructor because it is usually necessary to simulataneously
492 * update a GrPaint. This AutoMatrix is used while initially setting up GrPaint, however.
493 */
494class AutoMatrix {
495public:
496 AutoMatrix(GrContext* context) {
497 fMatrix = context->getMatrix();
498 fContext = context;
499 }
500 ~AutoMatrix() {
501 SkASSERT(NULL != fContext);
502 fContext->setMatrix(fMatrix);
503 }
504private:
505 GrContext* fContext;
506 SkMatrix fMatrix;
507};
508
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000509void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000510 bool constantColor, GrPaint* grPaint) {
511 SkShader* shader = skPaint.getShader();
512 if (NULL == shader) {
dandov9de5b512014-06-10 14:38:28 -0700513 SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
514 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000515 return;
516 }
517
commit-bot@chromium.org9e967ad2014-05-20 15:06:29 +0000518 // SkShader::asNewEffect() may do offscreen rendering. Save off the current RT, clip, and
519 // matrix. We don't reset the matrix on the context because SkShader::asNewEffect may use
520 // GrContext::getMatrix() to know the transformation from local coords to device space.
dandov9de5b512014-06-10 14:38:28 -0700521 GrColor grColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700522
523 // Start a new block here in order to preserve our context state after calling
524 // asNewEffect(). Since these calls get passed back to the client, we don't really
525 // want them messing around with the context.
526 {
527 GrContext::AutoRenderTarget art(context, NULL);
528 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
529 AutoMatrix am(context);
530
531 // setup the shader as the first color effect on the paint
532 // the default grColor is the paint's color
533 GrEffectRef* grEffect = NULL;
534 if (shader->asNewEffect(context, skPaint, NULL, &grColor, &grEffect) && NULL != grEffect) {
535 SkAutoTUnref<GrEffectRef> effect(grEffect);
536 grPaint->addColorEffect(effect);
537 constantColor = false;
538 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000539 }
krajcevskif461a8f2014-06-19 14:14:06 -0700540
dandov9de5b512014-06-10 14:38:28 -0700541 // The grcolor is automatically set when calling asneweffect.
542 // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
543 SkPaint2GrPaintNoShader(context, skPaint, grColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000544}