| 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 | |
| 42 | if (bitmap.width() == bitmap.rowBytes()) { |
| 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 | |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 59 | static GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx, |
| robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 60 | uint64_t key, |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 61 | const GrTextureParams* params, |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 62 | const SkBitmap& origBitmap) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | SkAutoLockPixels alp(origBitmap); |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 64 | GrContext::TextureCacheEntry entry; |
| 65 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | if (!origBitmap.readyToDraw()) { |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 67 | return entry; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SkBitmap tmpBitmap; |
| 71 | |
| 72 | const SkBitmap* bitmap = &origBitmap; |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 73 | |
| robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 74 | GrTextureDesc desc; |
| 75 | desc.fWidth = bitmap->width(); |
| 76 | desc.fHeight = bitmap->height(); |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 77 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 78 | |
| 79 | GrCacheData cacheData(key); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 80 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | if (SkBitmap::kIndex8_Config == bitmap->config()) { |
| 82 | // build_compressed_data doesn't do npot->pot expansion |
| 83 | // and paletted textures can't be sub-updated |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 84 | if (ctx->supportsIndex8PixelConfig(params, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | bitmap->width(), bitmap->height())) { |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 86 | size_t imagesize = bitmap->width() * bitmap->height() + |
| bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 87 | kGrColorTableSize; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | SkAutoMalloc storage(imagesize); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 89 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | build_compressed_data(storage.get(), origBitmap); |
| 91 | |
| 92 | // our compressed data will be trimmed, so pass width() for its |
| 93 | // "rowBytes", since they are the same now. |
| junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 94 | |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 95 | if (GrCacheData::kScratch_CacheID != key) { |
| 96 | return ctx->createAndLockTexture(params, desc, cacheData, |
| 97 | storage.get(), |
| junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 98 | bitmap->width()); |
| 99 | } else { |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 100 | entry = ctx->lockScratchTexture(desc, |
| 101 | GrContext::kExact_ScratchTexMatch); |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 102 | entry.texture()->writePixels(0, 0, bitmap->width(), |
| 103 | bitmap->height(), desc.fConfig, |
| 104 | storage.get(), 0); |
| junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 105 | return entry; |
| 106 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | |
| 108 | } else { |
| 109 | origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); |
| 110 | // now bitmap points to our temp, which has been promoted to 32bits |
| 111 | bitmap = &tmpBitmap; |
| 112 | } |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 113 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 115 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 116 | if (GrCacheData::kScratch_CacheID != key) { |
| 117 | // This texture is likely to be used again so leave it in the cache |
| 118 | // but locked. |
| 119 | return ctx->createAndLockTexture(params, desc, cacheData, |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 120 | bitmap->getPixels(), |
| 121 | bitmap->rowBytes()); |
| junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 122 | } else { |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 123 | // This texture is unlikely to be used again (in its present form) so |
| 124 | // just use a scratch texture. This will remove the texture from the |
| 125 | // cache so no one else can find it. Additionally, once unlocked, the |
| 126 | // scratch texture will go to the end of the list for purging so will |
| 127 | // likely be available for this volatile bitmap the next time around. |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 128 | entry = ctx->lockScratchTexture(desc, |
| 129 | GrContext::kExact_ScratchTexMatch); |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 130 | entry.texture()->writePixels(0, 0, |
| 131 | bitmap->width(), bitmap->height(), |
| 132 | desc.fConfig, |
| 133 | bitmap->getPixels(), |
| 134 | bitmap->rowBytes()); |
| junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 135 | return entry; |
| 136 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 141 | GrContext::TextureCacheEntry GrLockCachedBitmapTexture(GrContext* ctx, |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 142 | const SkBitmap& bitmap, |
| 143 | const GrTextureParams* params) { |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 144 | GrContext::TextureCacheEntry entry; |
| 145 | |
| 146 | if (!bitmap.isVolatile()) { |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 147 | // If the bitmap isn't changing try to find a cached copy first |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 148 | uint64_t key = bitmap.getGenerationID(); |
| 149 | key |= ((uint64_t) bitmap.pixelRefOffset()) << 32; |
| 150 | |
| 151 | GrTextureDesc desc; |
| 152 | desc.fWidth = bitmap.width(); |
| 153 | desc.fHeight = bitmap.height(); |
| 154 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 155 | |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 156 | GrCacheData cacheData(key); |
| 157 | |
| 158 | entry = ctx->findAndLockTexture(desc, cacheData, params); |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 159 | if (NULL == entry.texture()) { |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 160 | entry = sk_gr_create_bitmap_texture(ctx, key, params, bitmap); |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 161 | } |
| 162 | } else { |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame^] | 163 | entry = sk_gr_create_bitmap_texture(ctx, GrCacheData::kScratch_CacheID, params, bitmap); |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 164 | } |
| 165 | if (NULL == entry.texture()) { |
| 166 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 167 | bitmap.width(), bitmap.height()); |
| 168 | } |
| 169 | return entry; |
| 170 | } |
| 171 | |
| 172 | void GrUnlockCachedBitmapTexture(GrContext* ctx, GrContext::TextureCacheEntry cache) { |
| 173 | ctx->unlockTexture(cache); |
| 174 | } |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 178 | GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | switch (config) { |
| 180 | case SkBitmap::kA8_Config: |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 181 | return kAlpha_8_GrPixelConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | case SkBitmap::kIndex8_Config: |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 183 | return kIndex_8_GrPixelConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | case SkBitmap::kRGB_565_Config: |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 185 | return kRGB_565_GrPixelConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | case SkBitmap::kARGB_4444_Config: |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 187 | return kRGBA_4444_GrPixelConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | case SkBitmap::kARGB_8888_Config: |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 189 | return kSkia8888_PM_GrPixelConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | default: |
| robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 191 | // kNo_Config, kA1_Config missing, and kRLE_Index8_Config |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 192 | return kUnknown_GrPixelConfig; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |