blob: 2052b77b39b0562489c202a5b63a77bf24026663 [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
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000059static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
60 uint64_t key,
61 const GrTextureParams* params,
62 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +000063 SkAutoLockPixels alp(origBitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000064
reed@google.comac10a2d2010-12-22 21:39:39 +000065 if (!origBitmap.readyToDraw()) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000066 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000067 }
68
69 SkBitmap tmpBitmap;
70
71 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +000072
robertphillips@google.com75b3c962012-06-07 12:08:45 +000073 GrTextureDesc desc;
74 desc.fWidth = bitmap->width();
75 desc.fHeight = bitmap->height();
rileya@google.com24f3ad12012-07-18 21:47:40 +000076 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000077
78 GrCacheData cacheData(key);
bsalomon@google.com5782d712011-01-21 21:03:59 +000079
reed@google.comac10a2d2010-12-22 21:39:39 +000080 if (SkBitmap::kIndex8_Config == bitmap->config()) {
81 // build_compressed_data doesn't do npot->pot expansion
82 // and paletted textures can't be sub-updated
bsalomon@google.comb8670992012-07-25 21:27:09 +000083 if (ctx->supportsIndex8PixelConfig(params,
reed@google.comac10a2d2010-12-22 21:39:39 +000084 bitmap->width(), bitmap->height())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +000085 size_t imagesize = bitmap->width() * bitmap->height() +
bsalomon@google.comfea37b52011-04-25 15:51:06 +000086 kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000087 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +000088
reed@google.comac10a2d2010-12-22 21:39:39 +000089 build_compressed_data(storage.get(), origBitmap);
90
91 // our compressed data will be trimmed, so pass width() for its
92 // "rowBytes", since they are the same now.
junov@google.com4ee7ae52011-06-30 17:30:49 +000093
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000094 if (GrCacheData::kScratch_CacheID != key) {
95 return ctx->createAndLockTexture(params, desc, cacheData,
96 storage.get(),
junov@google.com4ee7ae52011-06-30 17:30:49 +000097 bitmap->width());
98 } else {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000099 GrTexture* result = ctx->lockScratchTexture(desc,
100 GrContext::kExact_ScratchTexMatch);
101 result->writePixels(0, 0, bitmap->width(),
102 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000103 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000104 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000105 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
107 } else {
108 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
109 // now bitmap points to our temp, which has been promoted to 32bits
110 bitmap = &tmpBitmap;
111 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000112 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000113
rileya@google.com24f3ad12012-07-18 21:47:40 +0000114 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000115 if (GrCacheData::kScratch_CacheID != key) {
116 // This texture is likely to be used again so leave it in the cache
117 // but locked.
118 return ctx->createAndLockTexture(params, desc, cacheData,
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000119 bitmap->getPixels(),
120 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000121 } else {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000122 // This texture is unlikely to be used again (in its present form) so
123 // just use a scratch texture. This will remove the texture from the
124 // cache so no one else can find it. Additionally, once unlocked, the
125 // scratch texture will go to the end of the list for purging so will
126 // likely be available for this volatile bitmap the next time around.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000127 GrTexture* result = ctx->lockScratchTexture(desc,
128 GrContext::kExact_ScratchTexMatch);
129 result->writePixels(0, 0,
130 bitmap->width(), bitmap->height(),
131 desc.fConfig,
132 bitmap->getPixels(),
133 bitmap->rowBytes());
134 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000135 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000136}
137
reed@google.comac10a2d2010-12-22 21:39:39 +0000138///////////////////////////////////////////////////////////////////////////////
139
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000140GrTexture* GrLockCachedBitmapTexture(GrContext* ctx,
141 const SkBitmap& bitmap,
142 const GrTextureParams* params) {
143 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000144
145 if (!bitmap.isVolatile()) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000146 // If the bitmap isn't changing try to find a cached copy first
rileya@google.com24f3ad12012-07-18 21:47:40 +0000147 uint64_t key = bitmap.getGenerationID();
148 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
149
150 GrTextureDesc desc;
151 desc.fWidth = bitmap.width();
152 desc.fHeight = bitmap.height();
153 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000154
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000155 GrCacheData cacheData(key);
156
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000157 result = ctx->findAndLockTexture(desc, cacheData, params);
158 if (NULL == result) {
159 result = sk_gr_create_bitmap_texture(ctx, key, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000160 }
161 } else {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000162 result = sk_gr_create_bitmap_texture(ctx, GrCacheData::kScratch_CacheID, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000163 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000164 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000165 GrPrintf("---- failed to create texture for cache [%d %d]\n",
166 bitmap.width(), bitmap.height());
167 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000168 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000169}
170
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000171void GrUnlockCachedBitmapTexture(GrTexture* texture) {
172 GrAssert(NULL != texture->getContext());
173
174 texture->getContext()->unlockTexture(texture);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000175}
176
177///////////////////////////////////////////////////////////////////////////////
178
rileya@google.com24f3ad12012-07-18 21:47:40 +0000179GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000180 switch (config) {
181 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000182 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000184 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000186 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000188 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000190 return kSkia8888_PM_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000191 default:
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000192 // kNo_Config, kA1_Config missing, and kRLE_Index8_Config
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000193 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 }
195}
196