| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
| 10 #include "SkImageCacherator.h" | 10 #include "SkImageCacherator.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return generator->refEncodedData(); | 63 return generator->refEncodedData(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { | 66 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { |
| 67 SkASSERT(bitmap.getGenerationID() == expectedID); | 67 SkASSERT(bitmap.getGenerationID() == expectedID); |
| 68 SkASSERT(bitmap.isImmutable()); | 68 SkASSERT(bitmap.isImmutable()); |
| 69 SkASSERT(bitmap.getPixels()); | 69 SkASSERT(bitmap.getPixels()); |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 static void release_malloc_proc(void* pixels, void* ctx) { | |
| 74 sk_free(pixels); | |
| 75 } | |
| 76 | |
| 77 static bool generate_a_bitmap(SkBitmap* bitmap, SkImageGenerator* gen, const SkI
mageInfo& info) { | |
| 78 const size_t rowBytes = info.minRowBytes(); | |
| 79 const size_t pixelSize = info.getSafeSize(rowBytes); | |
| 80 if (0 == pixelSize) { | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 SkAutoFree pixelStorage(sk_malloc_flags(pixelSize, 0)); | |
| 85 void* pixels = pixelStorage.get(); | |
| 86 if (!pixels) { | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 SkPMColor ctStorage[256]; | |
| 91 int ctCount = 0; | |
| 92 | |
| 93 if (!gen->getPixels(info, pixels, rowBytes, ctStorage, &ctCount)) { | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 SkAutoTUnref<SkColorTable> ctable; | |
| 98 if (ctCount > 0) { | |
| 99 SkASSERT(kIndex_8_SkColorType == info.colorType()); | |
| 100 ctable.reset(new SkColorTable(ctStorage, ctCount)); | |
| 101 } else { | |
| 102 SkASSERT(kIndex_8_SkColorType != info.colorType()); | |
| 103 } | |
| 104 | |
| 105 return bitmap->installPixels(info, pixelStorage.detach(), rowBytes, ctable, | |
| 106 release_malloc_proc, nullptr); | |
| 107 } | |
| 108 | |
| 109 bool SkImageCacherator::generateBitmap(SkBitmap* bitmap) { | 73 bool SkImageCacherator::generateBitmap(SkBitmap* bitmap) { |
| 110 ScopedGenerator generator(this); | 74 ScopedGenerator generator(this); |
| 111 const SkImageInfo& genInfo = generator->getInfo(); | 75 const SkImageInfo& genInfo = generator->getInfo(); |
| 112 if (fInfo.dimensions() == genInfo.dimensions()) { | 76 if (fInfo.dimensions() == genInfo.dimensions()) { |
| 113 SkASSERT(fOrigin.x() == 0 && fOrigin.y() == 0); | 77 SkASSERT(fOrigin.x() == 0 && fOrigin.y() == 0); |
| 114 // fast-case, no copy needed | 78 // fast-case, no copy needed |
| 115 return generate_a_bitmap(bitmap, generator, fInfo); | 79 return generator->tryGenerateBitmap(bitmap, fInfo); |
| 116 } else { | 80 } else { |
| 117 // need to handle subsetting, so we first generate the full size version
, and then | 81 // need to handle subsetting, so we first generate the full size version
, and then |
| 118 // "read" from it to get our subset. See skbug.com/4213 | 82 // "read" from it to get our subset. See skbug.com/4213 |
| 119 | 83 |
| 120 SkBitmap full; | 84 SkBitmap full; |
| 121 if (!generate_a_bitmap(&full, generator, genInfo)) { | 85 if (!generator->tryGenerateBitmap(&full, genInfo)) { |
| 122 return false; | 86 return false; |
| 123 } | 87 } |
| 124 if (!bitmap->tryAllocPixels(fInfo, nullptr, full.getColorTable())) { | 88 if (!bitmap->tryAllocPixels(fInfo, nullptr, full.getColorTable())) { |
| 125 return false; | 89 return false; |
| 126 } | 90 } |
| 127 return full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowB
ytes(), | 91 return full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowB
ytes(), |
| 128 fOrigin.x(), fOrigin.y()); | 92 fOrigin.x(), fOrigin.y()); |
| 129 } | 93 } |
| 130 } | 94 } |
| 131 | 95 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // 5. Ask the generator to return RGB(A) data, which the GPU can convert | 257 // 5. Ask the generator to return RGB(A) data, which the GPU can convert |
| 294 SkBitmap bitmap; | 258 SkBitmap bitmap; |
| 295 if (this->generateBitmap(&bitmap)) { | 259 if (this->generateBitmap(&bitmap)) { |
| 296 return GrRefCachedBitmapTexture(ctx, bitmap, usage); | 260 return GrRefCachedBitmapTexture(ctx, bitmap, usage); |
| 297 } | 261 } |
| 298 #endif | 262 #endif |
| 299 | 263 |
| 300 return nullptr; | 264 return nullptr; |
| 301 } | 265 } |
| 302 | 266 |
| OLD | NEW |