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