| 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 "SkColorFilter.h" | 9 #include "SkColorFilter.h" |
| 10 #include "SkConfig8888.h" | 10 #include "SkConfig8888.h" |
| 11 #include "SkData.h" | |
| 12 #include "SkMessageBus.h" | 11 #include "SkMessageBus.h" |
| 13 #include "SkPixelRef.h" | 12 #include "SkPixelRef.h" |
| 14 #include "GrResourceCache.h" | 13 #include "GrResourceCache.h" |
| 15 #include "GrGpu.h" | |
| 16 #include "GrDrawTargetCaps.h" | |
| 17 | |
| 18 #include "etc1.h" | |
| 19 | 14 |
| 20 /* Fill out buffer with the compressed format Ganesh expects from a colortable | 15 /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 21 based bitmap. [palette (colortable) + indices]. | 16 based bitmap. [palette (colortable) + indices]. |
| 22 | 17 |
| 23 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others | 18 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
| 24 we could detect that the colortable.count is <= 16, and then repack the | 19 we could detect that the colortable.count is <= 16, and then repack the |
| 25 indices as nibbles to save RAM, but it would take more time (i.e. a lot | 20 indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 26 slower than memcpy), so skipping that for now. | 21 slower than memcpy), so skipping that for now. |
| 27 | 22 |
| 28 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big | 23 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 117 } |
| 123 }; | 118 }; |
| 124 | 119 |
| 125 } // namespace | 120 } // namespace |
| 126 | 121 |
| 127 static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { | 122 static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { |
| 128 SkASSERT(NULL != pixelRef); | 123 SkASSERT(NULL != pixelRef); |
| 129 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); | 124 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); |
| 130 } | 125 } |
| 131 | 126 |
| 132 static GrTexture *load_etc1_texture(GrContext* ctx, | |
| 133 const GrTextureParams* params, | |
| 134 const SkBitmap &bm, GrTextureDesc desc) { | |
| 135 SkData *data = bm.pixelRef()->refEncodedData(); | |
| 136 | |
| 137 // Is this even encoded data? | |
| 138 if (NULL == data) { | |
| 139 return NULL; | |
| 140 } | |
| 141 | |
| 142 // Is this a valid PKM encoded data? | |
| 143 const uint8_t *bytes = data->bytes(); | |
| 144 if (!etc1_pkm_is_valid(bytes)) { | |
| 145 return NULL; | |
| 146 } | |
| 147 | |
| 148 uint32_t encodedWidth = etc1_pkm_get_width(bytes); | |
| 149 uint32_t encodedHeight = etc1_pkm_get_height(bytes); | |
| 150 | |
| 151 // Does the data match the dimensions of the bitmap? If not, | |
| 152 // then we don't know how to scale the image to match it... | |
| 153 if (encodedWidth != static_cast<uint32_t>(bm.width()) || | |
| 154 encodedHeight != static_cast<uint32_t>(bm.height())) { | |
| 155 return NULL; | |
| 156 } | |
| 157 | |
| 158 // Everything seems good... skip ahead to the data. | |
| 159 bytes += ETC_PKM_HEADER_SIZE; | |
| 160 desc.fConfig = kETC1_GrPixelConfig; | |
| 161 | |
| 162 // This texture is likely to be used again so leave it in the cache | |
| 163 GrCacheID cacheID; | |
| 164 generate_bitmap_cache_id(bm, &cacheID); | |
| 165 | |
| 166 GrResourceKey key; | |
| 167 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key
); | |
| 168 if (NULL != result) { | |
| 169 add_genID_listener(key, bm.pixelRef()); | |
| 170 } | |
| 171 return result; | |
| 172 } | |
| 173 | |
| 174 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, | 127 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
| 175 bool cache, | 128 bool cache, |
| 176 const GrTextureParams* params, | 129 const GrTextureParams* params, |
| 177 const SkBitmap& origBitmap) { | 130 const SkBitmap& origBitmap) { |
| 178 SkBitmap tmpBitmap; | 131 SkBitmap tmpBitmap; |
| 179 | 132 |
| 180 const SkBitmap* bitmap = &origBitmap; | 133 const SkBitmap* bitmap = &origBitmap; |
| 181 | 134 |
| 182 GrTextureDesc desc; | 135 GrTextureDesc desc; |
| 183 generate_bitmap_texture_desc(*bitmap, &desc); | 136 generate_bitmap_texture_desc(*bitmap, &desc); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 212 bitmap->height(), desc.fConfig, | 165 bitmap->height(), desc.fConfig, |
| 213 storage.get()); | 166 storage.get()); |
| 214 return result; | 167 return result; |
| 215 } | 168 } |
| 216 } else { | 169 } else { |
| 217 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); | 170 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
| 218 // now bitmap points to our temp, which has been promoted to 32bits | 171 // now bitmap points to our temp, which has been promoted to 32bits |
| 219 bitmap = &tmpBitmap; | 172 bitmap = &tmpBitmap; |
| 220 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); | 173 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
| 221 } | 174 } |
| 222 | |
| 223 // Is this an ETC1 encoded texture? | |
| 224 } else if (cache && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelC
onfig)) { | |
| 225 GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc); | |
| 226 if (NULL != texture) { | |
| 227 return texture; | |
| 228 } | |
| 229 } | 175 } |
| 230 | 176 |
| 231 SkAutoLockPixels alp(*bitmap); | 177 SkAutoLockPixels alp(*bitmap); |
| 232 if (!bitmap->readyToDraw()) { | 178 if (!bitmap->readyToDraw()) { |
| 233 return NULL; | 179 return NULL; |
| 234 } | 180 } |
| 235 if (cache) { | 181 if (cache) { |
| 236 // This texture is likely to be used again so leave it in the cache | 182 // This texture is likely to be used again so leave it in the cache |
| 237 GrCacheID cacheID; | 183 GrCacheID cacheID; |
| 238 generate_bitmap_cache_id(origBitmap, &cacheID); | 184 generate_bitmap_cache_id(origBitmap, &cacheID); |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 copy.setShader(NULL); | 432 copy.setShader(NULL); |
| 487 // modulate the paint alpha by the shader's solid color alpha | 433 // modulate the paint alpha by the shader's solid color alpha |
| 488 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); | 434 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); |
| 489 copy.setColor(SkColorSetA(color, newA)); | 435 copy.setColor(SkColorSetA(color, newA)); |
| 490 SkPaint2GrPaintNoShader(context, copy, false, constantColor, grPaint
); | 436 SkPaint2GrPaintNoShader(context, copy, false, constantColor, grPaint
); |
| 491 } else { | 437 } else { |
| 492 SkPaint2GrPaintNoShader(context, skPaint, false, constantColor, grPa
int); | 438 SkPaint2GrPaintNoShader(context, skPaint, false, constantColor, grPa
int); |
| 493 } | 439 } |
| 494 } | 440 } |
| 495 } | 441 } |
| OLD | NEW |