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