blob: 23aa49b41948545da65500515238572715c53dd0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#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.com5782d712011-01-21 21:03:59 +000015
16 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000017 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.com5782d712011-01-21 21:03:59 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021 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 */
24static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
25 SkASSERT(SkBitmap::kIndex8_Config == bitmap.config());
bsalomon@google.com5782d712011-01-21 21:03:59 +000026
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000027 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000028 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000029 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000030 return;
31 }
32
33 SkColorTable* ctable = bitmap.getColorTable();
34 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 memcpy(dst, ctable->lockColors(), ctable->count() * sizeof(SkPMColor));
37 ctable->unlockColors(false);
bsalomon@google.com5782d712011-01-21 21:03:59 +000038
reed@google.comac10a2d2010-12-22 21:39:39 +000039 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000040 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000041
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000042 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000043 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.com0797c2c2012-12-20 15:13:01 +000059static 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.com2859eb72012-12-21 02:01:28 +000067 GrCacheID::Key key;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000068 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.com10a9fb82013-01-02 19:29:57 +000072 static const size_t kKeyDataSize = 8 + sizeof(size_t);
73 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000074 GR_STATIC_ASSERT(sizeof(key) >= 8 + sizeof(size_t));
75 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
76 id->reset(gBitmapTextureDomain, key);
77}
78
79static 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.com1f47f4f2012-08-16 14:49:16 +000087static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000088 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000089 const GrTextureParams* params,
90 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +000091 SkBitmap tmpBitmap;
92
93 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +000094
robertphillips@google.com75b3c962012-06-07 12:08:45 +000095 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000096 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +000097
reed@google.comac10a2d2010-12-22 21:39:39 +000098 if (SkBitmap::kIndex8_Config == bitmap->config()) {
99 // build_compressed_data doesn't do npot->pot expansion
100 // and paletted textures can't be sub-updated
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000101 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
102 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000104
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 build_compressed_data(storage.get(), origBitmap);
106
107 // our compressed data will be trimmed, so pass width() for its
108 // "rowBytes", since they are the same now.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000110 if (cache) {
111 GrCacheID cacheID;
112 generate_bitmap_cache_id(origBitmap, &cacheID);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000113 return ctx->createTexture(params, desc, cacheID, storage.get(), bitmap->width());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000114 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000115 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000116 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000117 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000118 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000119 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000120 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000121 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 } else {
123 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
124 // now bitmap points to our temp, which has been promoted to 32bits
125 bitmap = &tmpBitmap;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000126 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000128 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000129
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000130 SkAutoLockPixels alp(*bitmap);
131 if (!bitmap->readyToDraw()) {
132 return NULL;
133 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000134 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000135 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000136 GrCacheID cacheID;
137 generate_bitmap_cache_id(origBitmap, &cacheID);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000138 return ctx->createTexture(params, desc, cacheID, bitmap->getPixels(), bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000139 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000140 // This texture is unlikely to be used again (in its present form) so
141 // just use a scratch texture. This will remove the texture from the
142 // cache so no one else can find it. Additionally, once unlocked, the
143 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000144 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000145 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000146 result->writePixels(0, 0,
147 bitmap->width(), bitmap->height(),
148 desc.fConfig,
149 bitmap->getPixels(),
150 bitmap->rowBytes());
151 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000152 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000153}
154
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155bool GrIsBitmapInCache(const GrContext* ctx,
156 const SkBitmap& bitmap,
157 const GrTextureParams* params) {
158 GrCacheID cacheID;
159 generate_bitmap_cache_id(bitmap, &cacheID);
160
161 GrTextureDesc desc;
162 generate_bitmap_texture_desc(bitmap, &desc);
163 return ctx->isTextureInCache(desc, cacheID, params);
164}
reed@google.comac10a2d2010-12-22 21:39:39 +0000165
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000166GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
167 const SkBitmap& bitmap,
168 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000169 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000170
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000171 bool cache = !bitmap.isVolatile();
172
173 if (cache) {
174 // If the bitmap isn't changing try to find a cached copy first.
175
176 GrCacheID cacheID;
177 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000178
179 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000180 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000181
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000182 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000183 }
184 if (NULL == result) {
185 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000186 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000187 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000188 GrPrintf("---- failed to create texture for cache [%d %d]\n",
189 bitmap.width(), bitmap.height());
190 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000191 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000192}
193
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000194void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000195 GrAssert(NULL != texture->getContext());
196
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000197 texture->getContext()->unlockScratchTexture(texture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000198 texture->unref();
rileya@google.com24f3ad12012-07-18 21:47:40 +0000199}
200
201///////////////////////////////////////////////////////////////////////////////
202
rileya@google.com24f3ad12012-07-18 21:47:40 +0000203GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 switch (config) {
205 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000206 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000208 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000210 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000212 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000214 return kSkia8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 default:
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000216 // kNo_Config, kA1_Config missing, and kRLE_Index8_Config
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000217 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 }
219}