blob: a3f0eefa91f85a54b99565014f08ce92c799c2d4 [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"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +00009#include "SkConfig8888.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000010#include "SkMessageBus.h"
11#include "SkPixelRef.h"
12#include "GrResourceCache.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
14/* Fill out buffer with the compressed format Ganesh expects from a colortable
15 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000016
17 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000018 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.com5782d712011-01-21 21:03:59 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022 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 */
25static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
26 SkASSERT(SkBitmap::kIndex8_Config == bitmap.config());
bsalomon@google.com5782d712011-01-21 21:03:59 +000027
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000028 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000029 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000030 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000031 return;
32 }
33
34 SkColorTable* ctable = bitmap.getColorTable();
35 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000036
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000037 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.com0a6151d2013-10-10 14:44:56 +000042 ctable->unlockColors();
bsalomon@google.com5782d712011-01-21 21:03:59 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000045 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000046
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000047 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000048 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.com0797c2c2012-12-20 15:13:01 +000064static 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.com2859eb72012-12-21 02:01:28 +000072 GrCacheID::Key key;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000073 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.com10a9fb82013-01-02 19:29:57 +000077 static const size_t kKeyDataSize = 8 + sizeof(size_t);
78 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000079 GR_STATIC_ASSERT(sizeof(key) >= 8 + sizeof(size_t));
80 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
81 id->reset(gBitmapTextureDomain, key);
82}
83
84static 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.org50a30432013-10-24 17:44:27 +000092namespace {
93
94// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
95class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
96public:
97 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
98private:
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
109static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
110 SkASSERT(NULL != pixelRef);
111 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
112}
113
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000114static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000115 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000116 const GrTextureParams* params,
117 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 SkBitmap tmpBitmap;
119
120 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000121
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000122 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000123 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000124
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 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.com7f4ad5a2013-05-07 19:36:43 +0000128 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
129 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 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.comfbfcd562012-08-23 18:09:54 +0000136
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000137 if (cache) {
138 GrCacheID cacheID;
139 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000140
141 GrResourceKey key;
142 GrTexture* result = ctx->createTexture(params, desc, cacheID,
143 storage.get(), bitmap->width(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000144 if (NULL != result) {
145 add_genID_listener(key, origBitmap.pixelRef());
146 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000147 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000148 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000149 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000150 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000151 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000152 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000153 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000154 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000155 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000156 } else {
157 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
158 // now bitmap points to our temp, which has been promoted to 32bits
159 bitmap = &tmpBitmap;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000160 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000162 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000163
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000164 SkAutoLockPixels alp(*bitmap);
165 if (!bitmap->readyToDraw()) {
166 return NULL;
167 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000168 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000169 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000170 GrCacheID cacheID;
171 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000172
173 GrResourceKey key;
174 GrTexture* result = ctx->createTexture(params, desc, cacheID,
175 bitmap->getPixels(), bitmap->rowBytes(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000176 if (NULL != result) {
177 add_genID_listener(key, origBitmap.pixelRef());
178 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000179 return result;
180 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000181 // This texture is unlikely to be used again (in its present form) so
182 // just use a scratch texture. This will remove the texture from the
183 // cache so no one else can find it. Additionally, once unlocked, the
184 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000185 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000186 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000187 result->writePixels(0, 0,
188 bitmap->width(), bitmap->height(),
189 desc.fConfig,
190 bitmap->getPixels(),
191 bitmap->rowBytes());
192 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000193 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000194}
195
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000196bool GrIsBitmapInCache(const GrContext* ctx,
197 const SkBitmap& bitmap,
198 const GrTextureParams* params) {
199 GrCacheID cacheID;
200 generate_bitmap_cache_id(bitmap, &cacheID);
201
202 GrTextureDesc desc;
203 generate_bitmap_texture_desc(bitmap, &desc);
204 return ctx->isTextureInCache(desc, cacheID, params);
205}
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000207GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
208 const SkBitmap& bitmap,
209 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000210 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000211
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000212 bool cache = !bitmap.isVolatile();
213
214 if (cache) {
215 // If the bitmap isn't changing try to find a cached copy first.
216
217 GrCacheID cacheID;
218 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000219
220 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000221 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000222
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000223 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000224 }
225 if (NULL == result) {
226 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000227 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000228 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000229 GrPrintf("---- failed to create texture for cache [%d %d]\n",
230 bitmap.width(), bitmap.height());
231 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000232 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000233}
234
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000235void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000236 SkASSERT(NULL != texture->getContext());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000237
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000238 texture->getContext()->unlockScratchTexture(texture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000239 texture->unref();
rileya@google.com24f3ad12012-07-18 21:47:40 +0000240}
241
242///////////////////////////////////////////////////////////////////////////////
243
rileya@google.com24f3ad12012-07-18 21:47:40 +0000244GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 switch (config) {
246 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000247 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000249 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000251 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000253 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000255 return kSkia8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 default:
reed@google.com2cb14802013-06-26 14:35:02 +0000257 // kNo_Config, kA1_Config missing
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000258 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 }
260}
reed@google.combf790232013-12-13 19:45:58 +0000261
262bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) {
263 SkColorType ct;
264 switch (config) {
265 case kAlpha_8_GrPixelConfig:
266 ct = kAlpha_8_SkColorType;
267 break;
268 case kIndex_8_GrPixelConfig:
269 ct = kIndex_8_SkColorType;
270 break;
271 case kRGB_565_GrPixelConfig:
272 ct = kRGB_565_SkColorType;
273 break;
274 case kRGBA_4444_GrPixelConfig:
275 ct = kARGB_4444_SkColorType;
276 break;
277 case kRGBA_8888_GrPixelConfig:
278 ct = kRGBA_8888_SkColorType;
279 break;
280 case kBGRA_8888_GrPixelConfig:
281 ct = kBGRA_8888_SkColorType;
282 break;
283 default:
284 return false;
285 }
286 if (ctOut) {
287 *ctOut = ct;
288 }
289 return true;
290}