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" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | |
| 11 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 12 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 13 | |
| 14 | 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] | 15 | we could detect that the colortable.count is <= 16, and then repack the |
| 16 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 17 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 18 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 20 | as the colortable.count says it is. |
| 21 | */ |
| 22 | static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { |
| 23 | SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 25 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 27 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | return; |
| 29 | } |
| 30 | |
| 31 | SkColorTable* ctable = bitmap.getColorTable(); |
| 32 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 33 | |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame^] | 34 | uint32_t* colorTableDst = reinterpret_cast<uint32_t*>(dst); |
| 35 | const uint32_t* colorTableSrc = reinterpret_cast<const uint32_t*>(ctable->lockColors()); |
| 36 | SkConvertConfig8888Pixels(colorTableDst, 0, SkCanvas::kRGBA_Premul_Config8888, |
| 37 | colorTableSrc, 0, SkCanvas::kNative_Premul_Config8888, |
| 38 | ctable->count(), 1); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 39 | ctable->unlockColors(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 40 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | // 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] | 42 | dst += kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 44 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 46 | } else { |
| 47 | // need to trim off the extra bytes per row |
| 48 | size_t width = bitmap.width(); |
| 49 | size_t rowBytes = bitmap.rowBytes(); |
| 50 | const char* src = (const char*)bitmap.getPixels(); |
| 51 | for (int y = 0; y < bitmap.height(); y++) { |
| 52 | memcpy(dst, src, width); |
| 53 | src += rowBytes; |
| 54 | dst += width; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | //////////////////////////////////////////////////////////////////////////////// |
| 60 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 61 | static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) { |
| 62 | // Our id includes the offset, width, and height so that bitmaps created by extractSubset() |
| 63 | // are unique. |
| 64 | uint32_t genID = bitmap.getGenerationID(); |
| 65 | size_t offset = bitmap.pixelRefOffset(); |
| 66 | int16_t width = static_cast<int16_t>(bitmap.width()); |
| 67 | int16_t height = static_cast<int16_t>(bitmap.height()); |
| 68 | |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 69 | GrCacheID::Key key; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 70 | memcpy(key.fData8, &genID, 4); |
| 71 | memcpy(key.fData8 + 4, &width, 2); |
| 72 | memcpy(key.fData8 + 6, &height, 2); |
| 73 | memcpy(key.fData8 + 8, &offset, sizeof(size_t)); |
bsalomon@google.com | 10a9fb8 | 2013-01-02 19:29:57 +0000 | [diff] [blame] | 74 | static const size_t kKeyDataSize = 8 + sizeof(size_t); |
| 75 | memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 76 | GR_STATIC_ASSERT(sizeof(key) >= 8 + sizeof(size_t)); |
| 77 | static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain(); |
| 78 | id->reset(gBitmapTextureDomain, key); |
| 79 | } |
| 80 | |
| 81 | static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) { |
| 82 | desc->fFlags = kNone_GrTextureFlags; |
| 83 | desc->fWidth = bitmap.width(); |
| 84 | desc->fHeight = bitmap.height(); |
| 85 | desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| 86 | desc->fSampleCnt = 0; |
| 87 | } |
| 88 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 89 | static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 90 | bool cache, |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 91 | const GrTextureParams* params, |
| 92 | const SkBitmap& origBitmap) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | SkBitmap tmpBitmap; |
| 94 | |
| 95 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 96 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 97 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 98 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 99 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | if (SkBitmap::kIndex8_Config == bitmap->config()) { |
| 101 | // build_compressed_data doesn't do npot->pot expansion |
| 102 | // and paletted textures can't be sub-updated |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 103 | if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) { |
| 104 | size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | SkAutoMalloc storage(imagesize); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 106 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | build_compressed_data(storage.get(), origBitmap); |
| 108 | |
| 109 | // our compressed data will be trimmed, so pass width() for its |
| 110 | // "rowBytes", since they are the same now. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 111 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 112 | if (cache) { |
| 113 | GrCacheID cacheID; |
| 114 | generate_bitmap_cache_id(origBitmap, &cacheID); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 115 | return ctx->createTexture(params, desc, cacheID, storage.get(), bitmap->width()); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 116 | } else { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 117 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 118 | GrContext::kExact_ScratchTexMatch); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 119 | result->writePixels(0, 0, bitmap->width(), |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 120 | bitmap->height(), desc.fConfig, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 121 | storage.get()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 122 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 123 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | } else { |
| 125 | origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); |
| 126 | // now bitmap points to our temp, which has been promoted to 32bits |
| 127 | bitmap = &tmpBitmap; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 128 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 130 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 132 | SkAutoLockPixels alp(*bitmap); |
| 133 | if (!bitmap->readyToDraw()) { |
| 134 | return NULL; |
| 135 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 136 | if (cache) { |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 137 | // 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] | 138 | GrCacheID cacheID; |
| 139 | generate_bitmap_cache_id(origBitmap, &cacheID); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 140 | return ctx->createTexture(params, desc, cacheID, bitmap->getPixels(), bitmap->rowBytes()); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 141 | } else { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 142 | // This texture is unlikely to be used again (in its present form) so |
| 143 | // just use a scratch texture. This will remove the texture from the |
| 144 | // cache so no one else can find it. Additionally, once unlocked, the |
| 145 | // 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] | 146 | // likely be available for this volatile bitmap the next time around. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 147 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 148 | result->writePixels(0, 0, |
| 149 | bitmap->width(), bitmap->height(), |
| 150 | desc.fConfig, |
| 151 | bitmap->getPixels(), |
| 152 | bitmap->rowBytes()); |
| 153 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 154 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 157 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 158 | const SkBitmap& bitmap, |
| 159 | const GrTextureParams* params) { |
| 160 | GrCacheID cacheID; |
| 161 | generate_bitmap_cache_id(bitmap, &cacheID); |
| 162 | |
| 163 | GrTextureDesc desc; |
| 164 | generate_bitmap_texture_desc(bitmap, &desc); |
| 165 | return ctx->isTextureInCache(desc, cacheID, params); |
| 166 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 168 | GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx, |
| 169 | const SkBitmap& bitmap, |
| 170 | const GrTextureParams* params) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 171 | GrTexture* result = NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 172 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 173 | bool cache = !bitmap.isVolatile(); |
| 174 | |
| 175 | if (cache) { |
| 176 | // If the bitmap isn't changing try to find a cached copy first. |
| 177 | |
| 178 | GrCacheID cacheID; |
| 179 | generate_bitmap_cache_id(bitmap, &cacheID); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 180 | |
| 181 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 182 | generate_bitmap_texture_desc(bitmap, &desc); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 183 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 184 | result = ctx->findAndRefTexture(desc, cacheID, params); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 185 | } |
| 186 | if (NULL == result) { |
| 187 | result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 188 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 189 | if (NULL == result) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 190 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 191 | bitmap.width(), bitmap.height()); |
| 192 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 193 | return result; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 194 | } |
| 195 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 196 | void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 197 | SkASSERT(NULL != texture->getContext()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 198 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 199 | texture->getContext()->unlockScratchTexture(texture); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 200 | texture->unref(); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /////////////////////////////////////////////////////////////////////////////// |
| 204 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 205 | GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 206 | switch (config) { |
| 207 | case SkBitmap::kA8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 208 | return kAlpha_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | case SkBitmap::kIndex8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 210 | return kIndex_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | case SkBitmap::kRGB_565_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 212 | return kRGB_565_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 213 | case SkBitmap::kARGB_4444_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 214 | return kRGBA_4444_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 215 | case SkBitmap::kARGB_8888_Config: |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 216 | return kSkia8888_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | default: |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 218 | // kNo_Config, kA1_Config missing |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 219 | return kUnknown_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 220 | } |
| 221 | } |