blob: 7505d7527a1ccd69be37ab41f4889234db6af40a [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "SkGr.h"
9
10/* Fill out buffer with the compressed format Ganesh expects from a colortable
11 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000012
13 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000014 we could detect that the colortable.count is <= 16, and then repack the
15 indices as nibbles to save RAM, but it would take more time (i.e. a lot
16 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000017
reed@google.comac10a2d2010-12-22 21:39:39 +000018 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
19 as the colortable.count says it is.
20 */
21static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
22 SkASSERT(SkBitmap::kIndex8_Config == bitmap.config());
bsalomon@google.com5782d712011-01-21 21:03:59 +000023
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000024 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000025 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000026 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000027 return;
28 }
29
30 SkColorTable* ctable = bitmap.getColorTable();
31 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 memcpy(dst, ctable->lockColors(), ctable->count() * sizeof(SkPMColor));
reed@google.com48d9ff52013-10-09 16:49:45 +000034 ctable->unlockColors(false);
bsalomon@google.com5782d712011-01-21 21:03:59 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000037 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000038
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000039 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000040 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
41 } else {
42 // need to trim off the extra bytes per row
43 size_t width = bitmap.width();
44 size_t rowBytes = bitmap.rowBytes();
45 const char* src = (const char*)bitmap.getPixels();
46 for (int y = 0; y < bitmap.height(); y++) {
47 memcpy(dst, src, width);
48 src += rowBytes;
49 dst += width;
50 }
51 }
52}
53
54////////////////////////////////////////////////////////////////////////////////
55
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000056static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) {
57 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
58 // are unique.
59 uint32_t genID = bitmap.getGenerationID();
60 size_t offset = bitmap.pixelRefOffset();
61 int16_t width = static_cast<int16_t>(bitmap.width());
62 int16_t height = static_cast<int16_t>(bitmap.height());
63
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +000064 GrCacheID::Key key;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000065 memcpy(key.fData8, &genID, 4);
66 memcpy(key.fData8 + 4, &width, 2);
67 memcpy(key.fData8 + 6, &height, 2);
68 memcpy(key.fData8 + 8, &offset, sizeof(size_t));
bsalomon@google.com10a9fb82013-01-02 19:29:57 +000069 static const size_t kKeyDataSize = 8 + sizeof(size_t);
70 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000071 GR_STATIC_ASSERT(sizeof(key) >= 8 + sizeof(size_t));
72 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
73 id->reset(gBitmapTextureDomain, key);
74}
75
76static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
77 desc->fFlags = kNone_GrTextureFlags;
78 desc->fWidth = bitmap.width();
79 desc->fHeight = bitmap.height();
80 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
81 desc->fSampleCnt = 0;
82}
83
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000084static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000085 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000086 const GrTextureParams* params,
87 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +000088 SkBitmap tmpBitmap;
89
90 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +000091
robertphillips@google.com75b3c962012-06-07 12:08:45 +000092 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000093 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +000094
reed@google.comac10a2d2010-12-22 21:39:39 +000095 if (SkBitmap::kIndex8_Config == bitmap->config()) {
96 // build_compressed_data doesn't do npot->pot expansion
97 // and paletted textures can't be sub-updated
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000098 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
99 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000101
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 build_compressed_data(storage.get(), origBitmap);
103
104 // our compressed data will be trimmed, so pass width() for its
105 // "rowBytes", since they are the same now.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000107 if (cache) {
108 GrCacheID cacheID;
109 generate_bitmap_cache_id(origBitmap, &cacheID);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000110 return ctx->createTexture(params, desc, cacheID, storage.get(), bitmap->width());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000111 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000112 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000113 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000114 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000115 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000116 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000117 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000118 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 } else {
120 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
121 // now bitmap points to our temp, which has been promoted to 32bits
122 bitmap = &tmpBitmap;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000123 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000125 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000127 SkAutoLockPixels alp(*bitmap);
128 if (!bitmap->readyToDraw()) {
129 return NULL;
130 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000131 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000132 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000133 GrCacheID cacheID;
134 generate_bitmap_cache_id(origBitmap, &cacheID);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000135 return ctx->createTexture(params, desc, cacheID, bitmap->getPixels(), bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000136 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000137 // This texture is unlikely to be used again (in its present form) so
138 // just use a scratch texture. This will remove the texture from the
139 // cache so no one else can find it. Additionally, once unlocked, the
140 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000141 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000142 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000143 result->writePixels(0, 0,
144 bitmap->width(), bitmap->height(),
145 desc.fConfig,
146 bitmap->getPixels(),
147 bitmap->rowBytes());
148 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000149 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000150}
151
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000152bool GrIsBitmapInCache(const GrContext* ctx,
153 const SkBitmap& bitmap,
154 const GrTextureParams* params) {
155 GrCacheID cacheID;
156 generate_bitmap_cache_id(bitmap, &cacheID);
157
158 GrTextureDesc desc;
159 generate_bitmap_texture_desc(bitmap, &desc);
160 return ctx->isTextureInCache(desc, cacheID, params);
161}
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000163GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
164 const SkBitmap& bitmap,
165 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000166 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000167
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000168 bool cache = !bitmap.isVolatile();
169
170 if (cache) {
171 // If the bitmap isn't changing try to find a cached copy first.
172
173 GrCacheID cacheID;
174 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000175
176 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000177 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000178
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000179 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000180 }
181 if (NULL == result) {
182 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000183 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000184 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000185 GrPrintf("---- failed to create texture for cache [%d %d]\n",
186 bitmap.width(), bitmap.height());
187 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000188 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000189}
190
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000191void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000192 SkASSERT(NULL != texture->getContext());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000193
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000194 texture->getContext()->unlockScratchTexture(texture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000195 texture->unref();
rileya@google.com24f3ad12012-07-18 21:47:40 +0000196}
197
198///////////////////////////////////////////////////////////////////////////////
199
rileya@google.com24f3ad12012-07-18 21:47:40 +0000200GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 switch (config) {
202 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000203 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000205 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000207 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000209 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000211 return kSkia8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 default:
reed@google.com2cb14802013-06-26 14:35:02 +0000213 // kNo_Config, kA1_Config missing
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000214 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 }
216}