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 | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 9 | #include "SkConfig8888.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 10 | #include "SkMessageBus.h" |
| 11 | #include "SkPixelRef.h" |
| 12 | #include "GrResourceCache.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
| 14 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 15 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 16 | |
| 17 | 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] | 18 | we could detect that the colortable.count is <= 16, and then repack the |
| 19 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 20 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 21 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 23 | as the colortable.count says it is. |
| 24 | */ |
| 25 | static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { |
| 26 | SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 28 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 30 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | return; |
| 32 | } |
| 33 | |
| 34 | SkColorTable* ctable = bitmap.getColorTable(); |
| 35 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 36 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 37 | const int count = ctable->count(); |
| 38 | |
| 39 | SkDstPixelInfo dstPI; |
| 40 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 41 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 42 | dstPI.fPixels = buffer; |
| 43 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 44 | |
| 45 | SkSrcPixelInfo srcPI; |
| 46 | srcPI.fColorType = kPMColor_SkColorType; |
| 47 | srcPI.fAlphaType = kPremul_SkAlphaType; |
| 48 | srcPI.fPixels = ctable->lockColors(); |
| 49 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 50 | |
| 51 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 52 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 53 | ctable->unlockColors(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 54 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | // 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] | 56 | dst += kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 58 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 60 | } else { |
| 61 | // need to trim off the extra bytes per row |
| 62 | size_t width = bitmap.width(); |
| 63 | size_t rowBytes = bitmap.rowBytes(); |
| 64 | const char* src = (const char*)bitmap.getPixels(); |
| 65 | for (int y = 0; y < bitmap.height(); y++) { |
| 66 | memcpy(dst, src, width); |
| 67 | src += rowBytes; |
| 68 | dst += width; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | //////////////////////////////////////////////////////////////////////////////// |
| 74 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 75 | static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) { |
| 76 | // Our id includes the offset, width, and height so that bitmaps created by extractSubset() |
| 77 | // are unique. |
| 78 | uint32_t genID = bitmap.getGenerationID(); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 79 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 80 | int16_t width = SkToS16(bitmap.width()); |
| 81 | int16_t height = SkToS16(bitmap.height()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 82 | |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 83 | GrCacheID::Key key; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 84 | memcpy(key.fData8 + 0, &genID, 4); |
| 85 | memcpy(key.fData8 + 4, &origin.fX, 4); |
| 86 | memcpy(key.fData8 + 8, &origin.fY, 4); |
| 87 | memcpy(key.fData8 + 12, &width, 2); |
| 88 | memcpy(key.fData8 + 14, &height, 2); |
| 89 | static const size_t kKeyDataSize = 16; |
bsalomon@google.com | 10a9fb8 | 2013-01-02 19:29:57 +0000 | [diff] [blame] | 90 | memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 91 | GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 92 | static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain(); |
| 93 | id->reset(gBitmapTextureDomain, key); |
| 94 | } |
| 95 | |
| 96 | static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) { |
| 97 | desc->fFlags = kNone_GrTextureFlags; |
| 98 | desc->fWidth = bitmap.width(); |
| 99 | desc->fHeight = bitmap.height(); |
| 100 | desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| 101 | desc->fSampleCnt = 0; |
| 102 | } |
| 103 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 104 | namespace { |
| 105 | |
| 106 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
| 107 | class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener { |
| 108 | public: |
| 109 | explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {} |
| 110 | private: |
| 111 | GrResourceKey fKey; |
| 112 | |
| 113 | virtual void onChange() SK_OVERRIDE { |
| 114 | const GrResourceInvalidatedMessage message = { fKey }; |
| 115 | SkMessageBus<GrResourceInvalidatedMessage>::Post(message); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | } // namespace |
| 120 | |
| 121 | static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { |
| 122 | SkASSERT(NULL != pixelRef); |
| 123 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); |
| 124 | } |
| 125 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 126 | static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 127 | bool cache, |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 128 | const GrTextureParams* params, |
| 129 | const SkBitmap& origBitmap) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | SkBitmap tmpBitmap; |
| 131 | |
| 132 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 133 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 134 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 135 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 136 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | if (SkBitmap::kIndex8_Config == bitmap->config()) { |
| 138 | // build_compressed_data doesn't do npot->pot expansion |
| 139 | // and paletted textures can't be sub-updated |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 140 | if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) { |
| 141 | size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 142 | SkAutoMalloc storage(imagesize); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 143 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | build_compressed_data(storage.get(), origBitmap); |
| 145 | |
| 146 | // our compressed data will be trimmed, so pass width() for its |
| 147 | // "rowBytes", since they are the same now. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 148 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 149 | if (cache) { |
| 150 | GrCacheID cacheID; |
| 151 | generate_bitmap_cache_id(origBitmap, &cacheID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 152 | |
| 153 | GrResourceKey key; |
| 154 | GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 155 | storage.get(), bitmap->width(), &key); |
commit-bot@chromium.org | 3843f3f | 2013-10-31 20:22:47 +0000 | [diff] [blame] | 156 | if (NULL != result) { |
| 157 | add_genID_listener(key, origBitmap.pixelRef()); |
| 158 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 159 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 160 | } else { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 161 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 162 | GrContext::kExact_ScratchTexMatch); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 163 | result->writePixels(0, 0, bitmap->width(), |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 164 | bitmap->height(), desc.fConfig, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 165 | storage.get()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 166 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 167 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | } else { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 169 | origBitmap.copyTo(&tmpBitmap, kPMColor_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 170 | // now bitmap points to our temp, which has been promoted to 32bits |
| 171 | bitmap = &tmpBitmap; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 172 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 174 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 176 | SkAutoLockPixels alp(*bitmap); |
| 177 | if (!bitmap->readyToDraw()) { |
| 178 | return NULL; |
| 179 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 180 | if (cache) { |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 181 | // 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] | 182 | GrCacheID cacheID; |
| 183 | generate_bitmap_cache_id(origBitmap, &cacheID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 184 | |
| 185 | GrResourceKey key; |
| 186 | GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 187 | bitmap->getPixels(), bitmap->rowBytes(), &key); |
commit-bot@chromium.org | 3843f3f | 2013-10-31 20:22:47 +0000 | [diff] [blame] | 188 | if (NULL != result) { |
| 189 | add_genID_listener(key, origBitmap.pixelRef()); |
| 190 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 191 | return result; |
| 192 | } else { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 193 | // This texture is unlikely to be used again (in its present form) so |
| 194 | // just use a scratch texture. This will remove the texture from the |
| 195 | // cache so no one else can find it. Additionally, once unlocked, the |
| 196 | // 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] | 197 | // likely be available for this volatile bitmap the next time around. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 198 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 199 | result->writePixels(0, 0, |
| 200 | bitmap->width(), bitmap->height(), |
| 201 | desc.fConfig, |
| 202 | bitmap->getPixels(), |
| 203 | bitmap->rowBytes()); |
| 204 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 205 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 206 | } |
| 207 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 208 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 209 | const SkBitmap& bitmap, |
| 210 | const GrTextureParams* params) { |
| 211 | GrCacheID cacheID; |
| 212 | generate_bitmap_cache_id(bitmap, &cacheID); |
| 213 | |
| 214 | GrTextureDesc desc; |
| 215 | generate_bitmap_texture_desc(bitmap, &desc); |
| 216 | return ctx->isTextureInCache(desc, cacheID, params); |
| 217 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 219 | GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx, |
| 220 | const SkBitmap& bitmap, |
| 221 | const GrTextureParams* params) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 222 | GrTexture* result = NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 223 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 224 | bool cache = !bitmap.isVolatile(); |
| 225 | |
| 226 | if (cache) { |
| 227 | // If the bitmap isn't changing try to find a cached copy first. |
| 228 | |
| 229 | GrCacheID cacheID; |
| 230 | generate_bitmap_cache_id(bitmap, &cacheID); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 231 | |
| 232 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 233 | generate_bitmap_texture_desc(bitmap, &desc); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 234 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 235 | result = ctx->findAndRefTexture(desc, cacheID, params); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 236 | } |
| 237 | if (NULL == result) { |
| 238 | result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 239 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 240 | if (NULL == result) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 241 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 242 | bitmap.width(), bitmap.height()); |
| 243 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 244 | return result; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 245 | } |
| 246 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 247 | void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 248 | SkASSERT(NULL != texture->getContext()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 249 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 250 | texture->getContext()->unlockScratchTexture(texture); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 251 | texture->unref(); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /////////////////////////////////////////////////////////////////////////////// |
| 255 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 256 | GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | switch (config) { |
| 258 | case SkBitmap::kA8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 259 | return kAlpha_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 260 | case SkBitmap::kIndex8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 261 | return kIndex_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | case SkBitmap::kRGB_565_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 263 | return kRGB_565_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 264 | case SkBitmap::kARGB_4444_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 265 | return kRGBA_4444_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 266 | case SkBitmap::kARGB_8888_Config: |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 267 | return kSkia8888_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 268 | default: |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 269 | // kNo_Config, kA1_Config missing |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 270 | return kUnknown_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 273 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 274 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 275 | // alpha info, that will be considered. |
| 276 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) { |
| 277 | switch (ct) { |
| 278 | case kUnknown_SkColorType: |
| 279 | return kUnknown_GrPixelConfig; |
| 280 | case kAlpha_8_SkColorType: |
| 281 | return kAlpha_8_GrPixelConfig; |
| 282 | case kRGB_565_SkColorType: |
| 283 | return kRGB_565_GrPixelConfig; |
| 284 | case kARGB_4444_SkColorType: |
| 285 | return kRGBA_4444_GrPixelConfig; |
| 286 | case kRGBA_8888_SkColorType: |
| 287 | return kRGBA_8888_GrPixelConfig; |
| 288 | case kBGRA_8888_SkColorType: |
| 289 | return kBGRA_8888_GrPixelConfig; |
| 290 | case kIndex_8_SkColorType: |
| 291 | return kIndex_8_GrPixelConfig; |
| 292 | } |
| 293 | SkASSERT(0); // shouldn't get here |
| 294 | return kUnknown_GrPixelConfig; |
| 295 | } |
| 296 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 297 | bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) { |
| 298 | SkColorType ct; |
| 299 | switch (config) { |
| 300 | case kAlpha_8_GrPixelConfig: |
| 301 | ct = kAlpha_8_SkColorType; |
| 302 | break; |
| 303 | case kIndex_8_GrPixelConfig: |
| 304 | ct = kIndex_8_SkColorType; |
| 305 | break; |
| 306 | case kRGB_565_GrPixelConfig: |
| 307 | ct = kRGB_565_SkColorType; |
| 308 | break; |
| 309 | case kRGBA_4444_GrPixelConfig: |
| 310 | ct = kARGB_4444_SkColorType; |
| 311 | break; |
| 312 | case kRGBA_8888_GrPixelConfig: |
| 313 | ct = kRGBA_8888_SkColorType; |
| 314 | break; |
| 315 | case kBGRA_8888_GrPixelConfig: |
| 316 | ct = kBGRA_8888_SkColorType; |
| 317 | break; |
| 318 | default: |
| 319 | return false; |
| 320 | } |
| 321 | if (ctOut) { |
| 322 | *ctOut = ct; |
| 323 | } |
| 324 | return true; |
| 325 | } |