reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #include "SkGr.h" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame^] | 9 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 10 | #include "SkConfig8888.h" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame^] | 11 | #include "SkGpuDevice.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 12 | #include "SkMessageBus.h" |
| 13 | #include "SkPixelRef.h" |
| 14 | #include "GrResourceCache.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
| 16 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 17 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 18 | |
| 19 | At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | we could detect that the colortable.count is <= 16, and then repack the |
| 21 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 22 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 23 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 25 | as the colortable.count says it is. |
| 26 | */ |
| 27 | static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { |
| 28 | SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 30 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 32 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | |
| 36 | SkColorTable* ctable = bitmap.getColorTable(); |
| 37 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 38 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 39 | const int count = ctable->count(); |
| 40 | |
| 41 | SkDstPixelInfo dstPI; |
| 42 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 43 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 44 | dstPI.fPixels = buffer; |
| 45 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 46 | |
| 47 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 48 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 49 | srcPI.fAlphaType = kPremul_SkAlphaType; |
| 50 | srcPI.fPixels = ctable->lockColors(); |
| 51 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 52 | |
| 53 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 54 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 55 | ctable->unlockColors(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 56 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | // always skip a full 256 number of entries, even if we memcpy'd fewer |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 58 | dst += kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 60 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 61 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 62 | } else { |
| 63 | // need to trim off the extra bytes per row |
| 64 | size_t width = bitmap.width(); |
| 65 | size_t rowBytes = bitmap.rowBytes(); |
| 66 | const char* src = (const char*)bitmap.getPixels(); |
| 67 | for (int y = 0; y < bitmap.height(); y++) { |
| 68 | memcpy(dst, src, width); |
| 69 | src += rowBytes; |
| 70 | dst += width; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | //////////////////////////////////////////////////////////////////////////////// |
| 76 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 77 | static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) { |
| 78 | // Our id includes the offset, width, and height so that bitmaps created by extractSubset() |
| 79 | // are unique. |
| 80 | uint32_t genID = bitmap.getGenerationID(); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 81 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 82 | int16_t width = SkToS16(bitmap.width()); |
| 83 | int16_t height = SkToS16(bitmap.height()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 84 | |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 85 | GrCacheID::Key key; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 86 | memcpy(key.fData8 + 0, &genID, 4); |
| 87 | memcpy(key.fData8 + 4, &origin.fX, 4); |
| 88 | memcpy(key.fData8 + 8, &origin.fY, 4); |
| 89 | memcpy(key.fData8 + 12, &width, 2); |
| 90 | memcpy(key.fData8 + 14, &height, 2); |
| 91 | static const size_t kKeyDataSize = 16; |
bsalomon@google.com | 10a9fb8 | 2013-01-02 19:29:57 +0000 | [diff] [blame] | 92 | memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 93 | GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 94 | static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain(); |
| 95 | id->reset(gBitmapTextureDomain, key); |
| 96 | } |
| 97 | |
| 98 | static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) { |
| 99 | desc->fFlags = kNone_GrTextureFlags; |
| 100 | desc->fWidth = bitmap.width(); |
| 101 | desc->fHeight = bitmap.height(); |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 102 | desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 103 | desc->fSampleCnt = 0; |
| 104 | } |
| 105 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 106 | namespace { |
| 107 | |
| 108 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
| 109 | class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener { |
| 110 | public: |
| 111 | explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {} |
| 112 | private: |
| 113 | GrResourceKey fKey; |
| 114 | |
| 115 | virtual void onChange() SK_OVERRIDE { |
| 116 | const GrResourceInvalidatedMessage message = { fKey }; |
| 117 | SkMessageBus<GrResourceInvalidatedMessage>::Post(message); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | } // namespace |
| 122 | |
| 123 | static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { |
| 124 | SkASSERT(NULL != pixelRef); |
| 125 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); |
| 126 | } |
| 127 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 128 | static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 129 | bool cache, |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 130 | const GrTextureParams* params, |
| 131 | const SkBitmap& origBitmap) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | SkBitmap tmpBitmap; |
| 133 | |
| 134 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 135 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 136 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 137 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 138 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | if (SkBitmap::kIndex8_Config == bitmap->config()) { |
| 140 | // build_compressed_data doesn't do npot->pot expansion |
| 141 | // and paletted textures can't be sub-updated |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 142 | if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) { |
| 143 | size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | SkAutoMalloc storage(imagesize); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 145 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | build_compressed_data(storage.get(), origBitmap); |
| 147 | |
| 148 | // our compressed data will be trimmed, so pass width() for its |
| 149 | // "rowBytes", since they are the same now. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 151 | if (cache) { |
| 152 | GrCacheID cacheID; |
| 153 | generate_bitmap_cache_id(origBitmap, &cacheID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 154 | |
| 155 | GrResourceKey key; |
| 156 | GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 157 | storage.get(), bitmap->width(), &key); |
commit-bot@chromium.org | 3843f3f | 2013-10-31 20:22:47 +0000 | [diff] [blame] | 158 | if (NULL != result) { |
| 159 | add_genID_listener(key, origBitmap.pixelRef()); |
| 160 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 161 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 162 | } else { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 163 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 164 | GrContext::kExact_ScratchTexMatch); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 165 | result->writePixels(0, 0, bitmap->width(), |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 166 | bitmap->height(), desc.fConfig, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 167 | storage.get()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 168 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 169 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 170 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 171 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | // now bitmap points to our temp, which has been promoted to 32bits |
| 173 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 174 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 176 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 178 | SkAutoLockPixels alp(*bitmap); |
| 179 | if (!bitmap->readyToDraw()) { |
| 180 | return NULL; |
| 181 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 182 | if (cache) { |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 183 | // This texture is likely to be used again so leave it in the cache |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 184 | GrCacheID cacheID; |
| 185 | generate_bitmap_cache_id(origBitmap, &cacheID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 186 | |
| 187 | GrResourceKey key; |
| 188 | GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 189 | bitmap->getPixels(), bitmap->rowBytes(), &key); |
commit-bot@chromium.org | 3843f3f | 2013-10-31 20:22:47 +0000 | [diff] [blame] | 190 | if (NULL != result) { |
| 191 | add_genID_listener(key, origBitmap.pixelRef()); |
| 192 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 193 | return result; |
| 194 | } else { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 195 | // This texture is unlikely to be used again (in its present form) so |
| 196 | // just use a scratch texture. This will remove the texture from the |
| 197 | // cache so no one else can find it. Additionally, once unlocked, the |
| 198 | // scratch texture will go to the end of the list for purging so will |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 199 | // likely be available for this volatile bitmap the next time around. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 200 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 201 | result->writePixels(0, 0, |
| 202 | bitmap->width(), bitmap->height(), |
| 203 | desc.fConfig, |
| 204 | bitmap->getPixels(), |
| 205 | bitmap->rowBytes()); |
| 206 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 207 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | } |
| 209 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 210 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 211 | const SkBitmap& bitmap, |
| 212 | const GrTextureParams* params) { |
| 213 | GrCacheID cacheID; |
| 214 | generate_bitmap_cache_id(bitmap, &cacheID); |
| 215 | |
| 216 | GrTextureDesc desc; |
| 217 | generate_bitmap_texture_desc(bitmap, &desc); |
| 218 | return ctx->isTextureInCache(desc, cacheID, params); |
| 219 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 220 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 221 | GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx, |
| 222 | const SkBitmap& bitmap, |
| 223 | const GrTextureParams* params) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 224 | GrTexture* result = NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 225 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 226 | bool cache = !bitmap.isVolatile(); |
| 227 | |
| 228 | if (cache) { |
| 229 | // If the bitmap isn't changing try to find a cached copy first. |
| 230 | |
| 231 | GrCacheID cacheID; |
| 232 | generate_bitmap_cache_id(bitmap, &cacheID); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 233 | |
| 234 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 235 | generate_bitmap_texture_desc(bitmap, &desc); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 236 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 237 | result = ctx->findAndRefTexture(desc, cacheID, params); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 238 | } |
| 239 | if (NULL == result) { |
| 240 | result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 241 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 242 | if (NULL == result) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 243 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 244 | bitmap.width(), bitmap.height()); |
| 245 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 246 | return result; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 247 | } |
| 248 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 249 | void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 250 | SkASSERT(NULL != texture->getContext()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 251 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 252 | texture->getContext()->unlockScratchTexture(texture); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 253 | texture->unref(); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /////////////////////////////////////////////////////////////////////////////// |
| 257 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 258 | GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 259 | switch (config) { |
| 260 | case SkBitmap::kA8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 261 | return kAlpha_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | case SkBitmap::kIndex8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 263 | return kIndex_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 264 | case SkBitmap::kRGB_565_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 265 | return kRGB_565_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 266 | case SkBitmap::kARGB_4444_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 267 | return kRGBA_4444_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 268 | case SkBitmap::kARGB_8888_Config: |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 269 | return kSkia8888_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | default: |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 271 | // kNo_Config, kA1_Config missing |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 272 | return kUnknown_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 275 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 276 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 277 | // alpha info, that will be considered. |
| 278 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) { |
| 279 | switch (ct) { |
| 280 | case kUnknown_SkColorType: |
| 281 | return kUnknown_GrPixelConfig; |
| 282 | case kAlpha_8_SkColorType: |
| 283 | return kAlpha_8_GrPixelConfig; |
| 284 | case kRGB_565_SkColorType: |
| 285 | return kRGB_565_GrPixelConfig; |
| 286 | case kARGB_4444_SkColorType: |
| 287 | return kRGBA_4444_GrPixelConfig; |
| 288 | case kRGBA_8888_SkColorType: |
| 289 | return kRGBA_8888_GrPixelConfig; |
| 290 | case kBGRA_8888_SkColorType: |
| 291 | return kBGRA_8888_GrPixelConfig; |
| 292 | case kIndex_8_SkColorType: |
| 293 | return kIndex_8_GrPixelConfig; |
| 294 | } |
| 295 | SkASSERT(0); // shouldn't get here |
| 296 | return kUnknown_GrPixelConfig; |
| 297 | } |
| 298 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 299 | bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) { |
| 300 | SkColorType ct; |
| 301 | switch (config) { |
| 302 | case kAlpha_8_GrPixelConfig: |
| 303 | ct = kAlpha_8_SkColorType; |
| 304 | break; |
| 305 | case kIndex_8_GrPixelConfig: |
| 306 | ct = kIndex_8_SkColorType; |
| 307 | break; |
| 308 | case kRGB_565_GrPixelConfig: |
| 309 | ct = kRGB_565_SkColorType; |
| 310 | break; |
| 311 | case kRGBA_4444_GrPixelConfig: |
| 312 | ct = kARGB_4444_SkColorType; |
| 313 | break; |
| 314 | case kRGBA_8888_GrPixelConfig: |
| 315 | ct = kRGBA_8888_SkColorType; |
| 316 | break; |
| 317 | case kBGRA_8888_GrPixelConfig: |
| 318 | ct = kBGRA_8888_SkColorType; |
| 319 | break; |
| 320 | default: |
| 321 | return false; |
| 322 | } |
| 323 | if (ctOut) { |
| 324 | *ctOut = ct; |
| 325 | } |
| 326 | return true; |
| 327 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame^] | 328 | |
| 329 | /////////////////////////////////////////////////////////////////////////////// |
| 330 | |
| 331 | void SkPaint2GrPaintNoShader(SkGpuDevice* dev, const SkPaint& skPaint, bool justAlpha, |
| 332 | bool constantColor, GrPaint* grPaint) { |
| 333 | |
| 334 | grPaint->setDither(skPaint.isDither()); |
| 335 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 336 | |
| 337 | SkXfermode::Coeff sm; |
| 338 | SkXfermode::Coeff dm; |
| 339 | |
| 340 | SkXfermode* mode = skPaint.getXfermode(); |
| 341 | GrEffectRef* xferEffect = NULL; |
| 342 | if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) { |
| 343 | if (NULL != xferEffect) { |
| 344 | grPaint->addColorEffect(xferEffect)->unref(); |
| 345 | sm = SkXfermode::kOne_Coeff; |
| 346 | dm = SkXfermode::kZero_Coeff; |
| 347 | } |
| 348 | } else { |
| 349 | //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");) |
| 350 | // Fall back to src-over |
| 351 | sm = SkXfermode::kOne_Coeff; |
| 352 | dm = SkXfermode::kISA_Coeff; |
| 353 | } |
| 354 | grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm)); |
| 355 | |
| 356 | if (justAlpha) { |
| 357 | uint8_t alpha = skPaint.getAlpha(); |
| 358 | grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha)); |
| 359 | // justAlpha is currently set to true only if there is a texture, |
| 360 | // so constantColor should not also be true. |
| 361 | SkASSERT(!constantColor); |
| 362 | } else { |
| 363 | grPaint->setColor(SkColor2GrColor(skPaint.getColor())); |
| 364 | } |
| 365 | |
| 366 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 367 | if (NULL != colorFilter) { |
| 368 | // if the source color is a constant then apply the filter here once rather than per pixel |
| 369 | // in a shader. |
| 370 | if (constantColor) { |
| 371 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 372 | grPaint->setColor(SkColor2GrColor(filtered)); |
| 373 | } else { |
| 374 | SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context())); |
| 375 | if (NULL != effect.get()) { |
| 376 | grPaint->addColorEffect(effect); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void SkPaint2GrPaintShader(SkGpuDevice* dev, const SkPaint& skPaint, |
| 383 | bool constantColor, GrPaint* grPaint) { |
| 384 | SkShader* shader = skPaint.getShader(); |
| 385 | if (NULL == shader) { |
| 386 | SkPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require |
| 391 | // the shader to set a render target. |
| 392 | GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL); |
| 393 | |
| 394 | // setup the shader as the first color effect on the paint |
| 395 | SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint, NULL)); |
| 396 | if (NULL != effect.get()) { |
| 397 | grPaint->addColorEffect(effect); |
| 398 | // Now setup the rest of the paint. |
| 399 | SkPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint); |
| 400 | } else { |
| 401 | // We still don't have SkColorShader::asNewEffect() implemented. |
| 402 | SkShader::GradientInfo info; |
| 403 | SkColor color; |
| 404 | |
| 405 | info.fColors = &color; |
| 406 | info.fColorOffsets = NULL; |
| 407 | info.fColorCount = 1; |
| 408 | if (SkShader::kColor_GradientType == shader->asAGradient(&info)) { |
| 409 | SkPaint copy(skPaint); |
| 410 | copy.setShader(NULL); |
| 411 | // modulate the paint alpha by the shader's solid color alpha |
| 412 | U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); |
| 413 | copy.setColor(SkColorSetA(color, newA)); |
| 414 | SkPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint); |
| 415 | } else { |
| 416 | SkPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |