OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkGr.h" | 8 #include "SkGr.h" |
9 #include "SkConfig8888.h" | 9 #include "SkConfig8888.h" |
| 10 #include "SkMessageBus.h" |
| 11 #include "SkPixelRef.h" |
| 12 #include "GrResourceCache.h" |
10 | 13 |
11 /* Fill out buffer with the compressed format Ganesh expects from a colortable | 14 /* Fill out buffer with the compressed format Ganesh expects from a colortable |
12 based bitmap. [palette (colortable) + indices]. | 15 based bitmap. [palette (colortable) + indices]. |
13 | 16 |
14 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others | 17 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
15 we could detect that the colortable.count is <= 16, and then repack the | 18 we could detect that the colortable.count is <= 16, and then repack the |
16 indices as nibbles to save RAM, but it would take more time (i.e. a lot | 19 indices as nibbles to save RAM, but it would take more time (i.e. a lot |
17 slower than memcpy), so skipping that for now. | 20 slower than memcpy), so skipping that for now. |
18 | 21 |
19 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big | 22 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 82 } |
80 | 83 |
81 static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc*
desc) { | 84 static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc*
desc) { |
82 desc->fFlags = kNone_GrTextureFlags; | 85 desc->fFlags = kNone_GrTextureFlags; |
83 desc->fWidth = bitmap.width(); | 86 desc->fWidth = bitmap.width(); |
84 desc->fHeight = bitmap.height(); | 87 desc->fHeight = bitmap.height(); |
85 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); | 88 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
86 desc->fSampleCnt = 0; | 89 desc->fSampleCnt = 0; |
87 } | 90 } |
88 | 91 |
| 92 namespace { |
| 93 |
| 94 // When the SkPixelRef genID changes, invalidate a corresponding GrResource desc
ribed by key. |
| 95 class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener { |
| 96 public: |
| 97 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {} |
| 98 private: |
| 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 |
| 109 static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { |
| 110 SkASSERT(NULL != pixelRef); |
| 111 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); |
| 112 } |
| 113 |
89 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, | 114 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
90 bool cache, | 115 bool cache, |
91 const GrTextureParams* params, | 116 const GrTextureParams* params, |
92 const SkBitmap& origBitmap) { | 117 const SkBitmap& origBitmap) { |
93 SkBitmap tmpBitmap; | 118 SkBitmap tmpBitmap; |
94 | 119 |
95 const SkBitmap* bitmap = &origBitmap; | 120 const SkBitmap* bitmap = &origBitmap; |
96 | 121 |
97 GrTextureDesc desc; | 122 GrTextureDesc desc; |
98 generate_bitmap_texture_desc(*bitmap, &desc); | 123 generate_bitmap_texture_desc(*bitmap, &desc); |
99 | 124 |
100 if (SkBitmap::kIndex8_Config == bitmap->config()) { | 125 if (SkBitmap::kIndex8_Config == bitmap->config()) { |
101 // build_compressed_data doesn't do npot->pot expansion | 126 // build_compressed_data doesn't do npot->pot expansion |
102 // and paletted textures can't be sub-updated | 127 // and paletted textures can't be sub-updated |
103 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->heig
ht())) { | 128 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->heig
ht())) { |
104 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTabl
eSize; | 129 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTabl
eSize; |
105 SkAutoMalloc storage(imagesize); | 130 SkAutoMalloc storage(imagesize); |
106 | 131 |
107 build_compressed_data(storage.get(), origBitmap); | 132 build_compressed_data(storage.get(), origBitmap); |
108 | 133 |
109 // our compressed data will be trimmed, so pass width() for its | 134 // our compressed data will be trimmed, so pass width() for its |
110 // "rowBytes", since they are the same now. | 135 // "rowBytes", since they are the same now. |
111 | 136 |
112 if (cache) { | 137 if (cache) { |
113 GrCacheID cacheID; | 138 GrCacheID cacheID; |
114 generate_bitmap_cache_id(origBitmap, &cacheID); | 139 generate_bitmap_cache_id(origBitmap, &cacheID); |
115 return ctx->createTexture(params, desc, cacheID, storage.get(),
bitmap->width()); | 140 |
| 141 GrResourceKey key; |
| 142 GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 143 storage.get(), bitmap->wi
dth(), &key); |
| 144 add_genID_listener(key, origBitmap.pixelRef()); |
| 145 return result; |
116 } else { | 146 } else { |
117 GrTexture* result = ctx->lockAndRefScratchTexture(desc, | 147 GrTexture* result = ctx->lockAndRefScratchTexture(desc, |
118 GrContext::kExact_Sc
ratchTexMatch); | 148 GrContext::kExact_Sc
ratchTexMatch); |
119 result->writePixels(0, 0, bitmap->width(), | 149 result->writePixels(0, 0, bitmap->width(), |
120 bitmap->height(), desc.fConfig, | 150 bitmap->height(), desc.fConfig, |
121 storage.get()); | 151 storage.get()); |
122 return result; | 152 return result; |
123 } | 153 } |
124 } else { | 154 } else { |
125 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); | 155 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); |
126 // now bitmap points to our temp, which has been promoted to 32bits | 156 // now bitmap points to our temp, which has been promoted to 32bits |
127 bitmap = &tmpBitmap; | 157 bitmap = &tmpBitmap; |
128 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); | 158 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); |
129 } | 159 } |
130 } | 160 } |
131 | 161 |
132 SkAutoLockPixels alp(*bitmap); | 162 SkAutoLockPixels alp(*bitmap); |
133 if (!bitmap->readyToDraw()) { | 163 if (!bitmap->readyToDraw()) { |
134 return NULL; | 164 return NULL; |
135 } | 165 } |
136 if (cache) { | 166 if (cache) { |
137 // This texture is likely to be used again so leave it in the cache | 167 // This texture is likely to be used again so leave it in the cache |
138 GrCacheID cacheID; | 168 GrCacheID cacheID; |
139 generate_bitmap_cache_id(origBitmap, &cacheID); | 169 generate_bitmap_cache_id(origBitmap, &cacheID); |
140 return ctx->createTexture(params, desc, cacheID, bitmap->getPixels(), bi
tmap->rowBytes()); | 170 |
141 } else { | 171 GrResourceKey key; |
| 172 GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 173 bitmap->getPixels(), bitmap->rowB
ytes(), &key); |
| 174 add_genID_listener(key, origBitmap.pixelRef()); |
| 175 return result; |
| 176 } else { |
142 // This texture is unlikely to be used again (in its present form) so | 177 // This texture is unlikely to be used again (in its present form) so |
143 // just use a scratch texture. This will remove the texture from the | 178 // just use a scratch texture. This will remove the texture from the |
144 // cache so no one else can find it. Additionally, once unlocked, the | 179 // cache so no one else can find it. Additionally, once unlocked, the |
145 // scratch texture will go to the end of the list for purging so will | 180 // scratch texture will go to the end of the list for purging so will |
146 // likely be available for this volatile bitmap the next time around. | 181 // likely be available for this volatile bitmap the next time around. |
147 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExac
t_ScratchTexMatch); | 182 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExac
t_ScratchTexMatch); |
148 result->writePixels(0, 0, | 183 result->writePixels(0, 0, |
149 bitmap->width(), bitmap->height(), | 184 bitmap->width(), bitmap->height(), |
150 desc.fConfig, | 185 desc.fConfig, |
151 bitmap->getPixels(), | 186 bitmap->getPixels(), |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 return kRGB_565_GrPixelConfig; | 247 return kRGB_565_GrPixelConfig; |
213 case SkBitmap::kARGB_4444_Config: | 248 case SkBitmap::kARGB_4444_Config: |
214 return kRGBA_4444_GrPixelConfig; | 249 return kRGBA_4444_GrPixelConfig; |
215 case SkBitmap::kARGB_8888_Config: | 250 case SkBitmap::kARGB_8888_Config: |
216 return kSkia8888_GrPixelConfig; | 251 return kSkia8888_GrPixelConfig; |
217 default: | 252 default: |
218 // kNo_Config, kA1_Config missing | 253 // kNo_Config, kA1_Config missing |
219 return kUnknown_GrPixelConfig; | 254 return kUnknown_GrPixelConfig; |
220 } | 255 } |
221 } | 256 } |
OLD | NEW |