blob: 7cee9741a3b0529405bdac7633a851d90acc3ecd [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
reed@google.comac10a2d2010-12-22 21:39:39 +000027 SkAutoLockPixels apl(bitmap);
28 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
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
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
67 GrCacheID::Key key;
68 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));
72 GR_STATIC_ASSERT(sizeof(key) >= 8 + sizeof(size_t));
73 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
74 id->reset(gBitmapTextureDomain, key);
75}
76
77static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
78 desc->fFlags = kNone_GrTextureFlags;
79 desc->fWidth = bitmap.width();
80 desc->fHeight = bitmap.height();
81 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
82 desc->fSampleCnt = 0;
83}
84
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000085static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000086 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087 const GrTextureParams* params,
88 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +000089 SkAutoLockPixels alp(origBitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090
reed@google.comac10a2d2010-12-22 21:39:39 +000091 if (!origBitmap.readyToDraw()) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000092 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000093 }
94
95 SkBitmap tmpBitmap;
96
97 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +000098
robertphillips@google.com75b3c962012-06-07 12:08:45 +000099 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000100 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000101
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 if (SkBitmap::kIndex8_Config == bitmap->config()) {
103 // build_compressed_data doesn't do npot->pot expansion
104 // and paletted textures can't be sub-updated
bsalomon@google.comb8670992012-07-25 21:27:09 +0000105 if (ctx->supportsIndex8PixelConfig(params,
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 bitmap->width(), bitmap->height())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000107 size_t imagesize = bitmap->width() * bitmap->height() +
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000108 kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 build_compressed_data(storage.get(), origBitmap);
112
113 // our compressed data will be trimmed, so pass width() for its
114 // "rowBytes", since they are the same now.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000116 if (cache) {
117 GrCacheID cacheID;
118 generate_bitmap_cache_id(origBitmap, &cacheID);
119 return ctx->createTexture(params, desc, cacheID,
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000120 storage.get(),
121 bitmap->width());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000122 } else {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000123 GrTexture* result = ctx->lockScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000124 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000126 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000127 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000128 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000129 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 } else {
131 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
132 // now bitmap points to our temp, which has been promoted to 32bits
133 bitmap = &tmpBitmap;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000134 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000136 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000138 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000139 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000140 GrCacheID cacheID;
141 generate_bitmap_cache_id(origBitmap, &cacheID);
142 return ctx->createTexture(params, desc, cacheID,
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000143 bitmap->getPixels(),
144 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000145 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000146 // This texture is unlikely to be used again (in its present form) so
147 // just use a scratch texture. This will remove the texture from the
148 // cache so no one else can find it. Additionally, once unlocked, the
149 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000150 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000151 GrTexture* result = ctx->lockScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000152 result->writePixels(0, 0,
153 bitmap->width(), bitmap->height(),
154 desc.fConfig,
155 bitmap->getPixels(),
156 bitmap->rowBytes());
157 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000158 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000159}
160
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000161bool GrIsBitmapInCache(const GrContext* ctx,
162 const SkBitmap& bitmap,
163 const GrTextureParams* params) {
164 GrCacheID cacheID;
165 generate_bitmap_cache_id(bitmap, &cacheID);
166
167 GrTextureDesc desc;
168 generate_bitmap_texture_desc(bitmap, &desc);
169 return ctx->isTextureInCache(desc, cacheID, params);
170}
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000172GrTexture* GrLockCachedBitmapTexture(GrContext* ctx,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000173 const SkBitmap& bitmap,
174 const GrTextureParams* params) {
175 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000176
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000177 bool cache = !bitmap.isVolatile();
178
179 if (cache) {
180 // If the bitmap isn't changing try to find a cached copy first.
181
182 GrCacheID cacheID;
183 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000184
185 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000186 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000187
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000188 result = ctx->findTexture(desc, cacheID, params);
189 }
190 if (NULL == result) {
191 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000192 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000193 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000194 GrPrintf("---- failed to create texture for cache [%d %d]\n",
195 bitmap.width(), bitmap.height());
196 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000197 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000198}
199
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000200void GrUnlockCachedBitmapTexture(GrTexture* texture) {
201 GrAssert(NULL != texture->getContext());
202
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000203 texture->getContext()->unlockScratchTexture(texture);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000204}
205
206///////////////////////////////////////////////////////////////////////////////
207
rileya@google.com24f3ad12012-07-18 21:47:40 +0000208GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 switch (config) {
210 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000211 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000213 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000215 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000217 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000219 return kSkia8888_PM_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 default:
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000221 // kNo_Config, kA1_Config missing, and kRLE_Index8_Config
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000222 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 }
224}
225