joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 8 | #include "GrAtlasGlyphCache.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 9 | #include "GrContext.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 10 | #include "GrDistanceFieldGenFromVector.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 11 | #include "GrGpu.h" |
| 12 | #include "GrRectanizer.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 13 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 14 | #include "SkAutoMalloc.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 15 | #include "SkDistanceFieldGen.h" |
Brian Osman | 6382f45 | 2017-08-11 13:34:02 -0400 | [diff] [blame] | 16 | #include "SkMathPriv.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 17 | #include "SkString.h" |
| 18 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 19 | bool GrAtlasGlyphCache::initAtlas(GrMaskFormat format) { |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 20 | int index = MaskFormatToAtlasIndex(format); |
| 21 | if (!fAtlases[index]) { |
brianosman | 86dc226 | 2016-07-08 06:15:45 -0700 | [diff] [blame] | 22 | GrPixelConfig config = MaskFormatToPixelConfig(format, *fContext->caps()); |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 23 | int width = fAtlasConfigs[index].fWidth; |
| 24 | int height = fAtlasConfigs[index].fHeight; |
| 25 | int numPlotsX = fAtlasConfigs[index].numPlotsX(); |
| 26 | int numPlotsY = fAtlasConfigs[index].numPlotsY(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 27 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 28 | fAtlases[index] = GrDrawOpAtlas::Make( |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 29 | fContext, config, width, height, numPlotsX, numPlotsY, |
| 30 | &GrAtlasGlyphCache::HandleEviction, (void*)this); |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 31 | if (!fAtlases[index]) { |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 32 | return false; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 35 | return true; |
| 36 | } |
| 37 | |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 38 | GrAtlasGlyphCache::GrAtlasGlyphCache(GrContext* context, float maxTextureBytes) |
| 39 | : fContext(context), fPreserveStrike(nullptr) { |
| 40 | // Calculate RGBA size. Must be between 1024 x 512 and MaxTextureSize x MaxTextureSize / 2 |
Brian Osman | 6382f45 | 2017-08-11 13:34:02 -0400 | [diff] [blame] | 41 | int log2MaxTextureSize = SkPrevLog2(context->caps()->maxTextureSize()); |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 42 | int log2MaxDim = 10; |
| 43 | for (; log2MaxDim <= log2MaxTextureSize; ++log2MaxDim) { |
| 44 | int maxDim = 1 << log2MaxDim; |
| 45 | int minDim = 1 << (log2MaxDim - 1); |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 46 | |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 47 | if (maxDim * minDim * 4 >= maxTextureBytes) break; |
| 48 | } |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 49 | |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 50 | int log2MinDim = log2MaxDim - 1; |
| 51 | int maxDim = 1 << log2MaxDim; |
| 52 | int minDim = 1 << log2MinDim; |
| 53 | // Plots are either 256 or 512. |
| 54 | int maxPlot = SkTMin(512, SkTMax(256, 1 << (log2MaxDim - 2))); |
| 55 | int minPlot = SkTMin(512, SkTMax(256, 1 << (log2MaxDim - 3))); |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 56 | |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 57 | // Setup default atlas configs. The A8 atlas uses maxDim for both width and height, as the A8 |
| 58 | // format is already very compact. |
| 59 | fAtlasConfigs[kA8_GrMaskFormat].fWidth = maxDim; |
| 60 | fAtlasConfigs[kA8_GrMaskFormat].fHeight = maxDim; |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 61 | fAtlasConfigs[kA8_GrMaskFormat].fPlotWidth = maxPlot; |
| 62 | fAtlasConfigs[kA8_GrMaskFormat].fPlotHeight = minPlot; |
| 63 | |
| 64 | // A565 and ARGB use maxDim x minDim. |
| 65 | fAtlasConfigs[kA565_GrMaskFormat].fWidth = minDim; |
| 66 | fAtlasConfigs[kA565_GrMaskFormat].fHeight = maxDim; |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 67 | fAtlasConfigs[kA565_GrMaskFormat].fPlotWidth = minPlot; |
| 68 | fAtlasConfigs[kA565_GrMaskFormat].fPlotHeight = minPlot; |
| 69 | |
| 70 | fAtlasConfigs[kARGB_GrMaskFormat].fWidth = minDim; |
| 71 | fAtlasConfigs[kARGB_GrMaskFormat].fHeight = maxDim; |
Eric Karl | 6d34228 | 2017-05-03 17:08:42 -0700 | [diff] [blame] | 72 | fAtlasConfigs[kARGB_GrMaskFormat].fPlotWidth = minPlot; |
| 73 | fAtlasConfigs[kARGB_GrMaskFormat].fPlotHeight = minPlot; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 76 | GrAtlasGlyphCache::~GrAtlasGlyphCache() { |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 77 | StrikeHash::Iter iter(&fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 78 | while (!iter.done()) { |
joshualitt | a5f1d5a | 2015-05-22 13:09:57 -0700 | [diff] [blame] | 79 | (*iter).fIsAbandoned = true; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 80 | (*iter).unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 81 | ++iter; |
| 82 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 85 | void GrAtlasGlyphCache::freeAll() { |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 86 | StrikeHash::Iter iter(&fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 87 | while (!iter.done()) { |
joshualitt | a5f1d5a | 2015-05-22 13:09:57 -0700 | [diff] [blame] | 88 | (*iter).fIsAbandoned = true; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 89 | (*iter).unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 90 | ++iter; |
| 91 | } |
| 92 | fCache.rewind(); |
| 93 | for (int i = 0; i < kMaskFormatCount; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 94 | fAtlases[i] = nullptr; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 98 | void GrAtlasGlyphCache::HandleEviction(GrDrawOpAtlas::AtlasID id, void* ptr) { |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 99 | GrAtlasGlyphCache* fontCache = reinterpret_cast<GrAtlasGlyphCache*>(ptr); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 100 | |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 101 | StrikeHash::Iter iter(&fontCache->fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 102 | for (; !iter.done(); ++iter) { |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 103 | GrAtlasTextStrike* strike = &*iter; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 104 | strike->removeID(id); |
| 105 | |
| 106 | // clear out any empty strikes. We will preserve the strike whose call to addToAtlas |
| 107 | // triggered the eviction |
| 108 | if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) { |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 109 | fontCache->fCache.remove(GrAtlasTextStrike::GetKey(*strike)); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 110 | strike->fIsAbandoned = true; |
| 111 | strike->unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 116 | #ifdef SK_DEBUG |
| 117 | #include "GrContextPriv.h" |
| 118 | #include "GrSurfaceProxy.h" |
| 119 | #include "GrSurfaceContext.h" |
| 120 | #include "GrTextureProxy.h" |
| 121 | |
| 122 | #include "SkBitmap.h" |
| 123 | #include "SkImageEncoder.h" |
| 124 | #include "SkStream.h" |
| 125 | #include <stdio.h> |
| 126 | |
| 127 | /** |
| 128 | * Write the contents of the surface proxy to a PNG. Returns true if successful. |
| 129 | * @param filename Full path to desired file |
| 130 | */ |
| 131 | static bool save_pixels(GrContext* context, GrSurfaceProxy* sProxy, const char* filename) { |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 132 | if (!sProxy) { |
| 133 | return false; |
| 134 | } |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 135 | |
| 136 | SkImageInfo ii = SkImageInfo::Make(sProxy->width(), sProxy->height(), |
| 137 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 138 | SkBitmap bm; |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 139 | if (!bm.tryAllocPixels(ii)) { |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
| 143 | sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext( |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 144 | sk_ref_sp(sProxy), |
| 145 | nullptr)); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 146 | if (!sContext || !sContext->asTextureProxy()) { |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 150 | bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0); |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 151 | if (!result) { |
| 152 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // remove any previous version of this file |
| 157 | remove(filename); |
| 158 | |
| 159 | SkFILEWStream file(filename); |
| 160 | if (!file.isValid()) { |
| 161 | SkDebugf("------ failed to create file: %s\n", filename); |
| 162 | remove(filename); // remove any partial file |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) { |
| 167 | SkDebugf("------ failed to encode %s\n", filename); |
| 168 | remove(filename); // remove any partial file |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 175 | void GrAtlasGlyphCache::dump() const { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 176 | static int gDumpCount = 0; |
| 177 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 178 | if (fAtlases[i]) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 179 | const sk_sp<GrTextureProxy>* proxies = fAtlases[i]->getProxies(); |
| 180 | for (int pageIdx = 0; pageIdx < GrDrawOpAtlas::kMaxPages; ++pageIdx) { |
| 181 | if (proxies[pageIdx]) { |
| 182 | SkString filename; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 183 | #ifdef SK_BUILD_FOR_ANDROID |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 184 | filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 185 | #else |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 186 | filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 187 | #endif |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 188 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 189 | save_pixels(fContext, proxies[pageIdx].get(), filename.c_str()); |
| 190 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | ++gDumpCount; |
| 195 | } |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 196 | #endif |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 197 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 198 | void GrAtlasGlyphCache::setAtlasSizes_ForTesting(const GrDrawOpAtlasConfig configs[3]) { |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 199 | // Delete any old atlases. |
| 200 | // This should be safe to do as long as we are not in the middle of a flush. |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 201 | for (int i = 0; i < kMaskFormatCount; i++) { |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 202 | fAtlases[i] = nullptr; |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 203 | } |
| 204 | memcpy(fAtlasConfigs, configs, sizeof(fAtlasConfigs)); |
| 205 | } |
| 206 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 207 | /////////////////////////////////////////////////////////////////////////////// |
| 208 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 209 | static inline GrMaskFormat get_packed_glyph_mask_format(const SkGlyph& glyph) { |
| 210 | SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat); |
| 211 | switch (format) { |
| 212 | case SkMask::kBW_Format: |
| 213 | // fall through to kA8 -- we store BW glyphs in our 8-bit cache |
| 214 | case SkMask::kA8_Format: |
| 215 | return kA8_GrMaskFormat; |
| 216 | case SkMask::kLCD16_Format: |
| 217 | return kA565_GrMaskFormat; |
| 218 | case SkMask::kARGB32_Format: |
| 219 | return kARGB_GrMaskFormat; |
| 220 | default: |
| 221 | SkDEBUGFAIL("unsupported SkMask::Format"); |
| 222 | return kA8_GrMaskFormat; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static inline bool get_packed_glyph_bounds(SkGlyphCache* cache, const SkGlyph& glyph, |
| 227 | SkIRect* bounds) { |
| 228 | #if 1 |
| 229 | // crbug:510931 |
| 230 | // Retrieving the image from the cache can actually change the mask format. |
| 231 | cache->findImage(glyph); |
| 232 | #endif |
| 233 | bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight); |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | static inline bool get_packed_glyph_df_bounds(SkGlyphCache* cache, const SkGlyph& glyph, |
| 239 | SkIRect* bounds) { |
| 240 | #if 1 |
| 241 | // crbug:510931 |
| 242 | // Retrieving the image from the cache can actually change the mask format. |
| 243 | cache->findImage(glyph); |
| 244 | #endif |
| 245 | bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight); |
| 246 | bounds->outset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | // expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to |
| 252 | // A8, RGB565, or RGBA8888. |
| 253 | template <typename INT_TYPE> |
| 254 | static void expand_bits(INT_TYPE* dst, |
| 255 | const uint8_t* src, |
| 256 | int width, |
| 257 | int height, |
| 258 | int dstRowBytes, |
| 259 | int srcRowBytes) { |
| 260 | for (int i = 0; i < height; ++i) { |
| 261 | int rowWritesLeft = width; |
| 262 | const uint8_t* s = src; |
| 263 | INT_TYPE* d = dst; |
| 264 | while (rowWritesLeft > 0) { |
| 265 | unsigned mask = *s++; |
| 266 | for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { |
| 267 | *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0; |
| 268 | } |
| 269 | } |
| 270 | dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes); |
| 271 | src += srcRowBytes; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static bool get_packed_glyph_image(SkGlyphCache* cache, const SkGlyph& glyph, int width, |
| 276 | int height, int dstRB, GrMaskFormat expectedMaskFormat, |
| 277 | void* dst) { |
| 278 | SkASSERT(glyph.fWidth == width); |
| 279 | SkASSERT(glyph.fHeight == height); |
| 280 | const void* src = cache->findImage(glyph); |
| 281 | if (nullptr == src) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // crbug:510931 |
| 286 | // Retrieving the image from the cache can actually change the mask format. This case is very |
| 287 | // uncommon so for now we just draw a clear box for these glyphs. |
| 288 | if (get_packed_glyph_mask_format(glyph) != expectedMaskFormat) { |
| 289 | const int bpp = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
| 290 | for (int y = 0; y < height; y++) { |
| 291 | sk_bzero(dst, width * bpp); |
| 292 | dst = (char*)dst + dstRB; |
| 293 | } |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | int srcRB = glyph.rowBytes(); |
| 298 | // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to |
| 299 | // check the glyph's format, not the strike's format, and to be able to convert to any of the |
| 300 | // GrMaskFormats. |
| 301 | if (SkMask::kBW_Format == glyph.fMaskFormat) { |
| 302 | // expand bits to our mask type |
| 303 | const uint8_t* bits = reinterpret_cast<const uint8_t*>(src); |
| 304 | switch (expectedMaskFormat) { |
| 305 | case kA8_GrMaskFormat:{ |
| 306 | uint8_t* bytes = reinterpret_cast<uint8_t*>(dst); |
| 307 | expand_bits(bytes, bits, width, height, dstRB, srcRB); |
| 308 | break; |
| 309 | } |
| 310 | case kA565_GrMaskFormat: { |
| 311 | uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst); |
| 312 | expand_bits(rgb565, bits, width, height, dstRB, srcRB); |
| 313 | break; |
| 314 | } |
| 315 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 316 | SK_ABORT("Invalid GrMaskFormat"); |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 317 | } |
| 318 | } else if (srcRB == dstRB) { |
| 319 | memcpy(dst, src, dstRB * height); |
| 320 | } else { |
| 321 | const int bbp = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
| 322 | for (int y = 0; y < height; y++) { |
| 323 | memcpy(dst, src, width * bbp); |
| 324 | src = (const char*)src + srcRB; |
| 325 | dst = (char*)dst + dstRB; |
| 326 | } |
| 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | static bool get_packed_glyph_df_image(SkGlyphCache* cache, const SkGlyph& glyph, |
| 332 | int width, int height, void* dst) { |
| 333 | SkASSERT(glyph.fWidth + 2*SK_DistanceFieldPad == width); |
| 334 | SkASSERT(glyph.fHeight + 2*SK_DistanceFieldPad == height); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 335 | |
| 336 | #ifndef SK_USE_LEGACY_DISTANCE_FIELDS |
| 337 | const SkPath* path = cache->findPath(glyph); |
| 338 | if (nullptr == path) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 342 | SkDEBUGCODE(SkRect glyphBounds = SkRect::MakeXYWH(glyph.fLeft, |
| 343 | glyph.fTop, |
| 344 | glyph.fWidth, |
| 345 | glyph.fHeight)); |
| 346 | SkASSERT(glyphBounds.contains(path->getBounds())); |
| 347 | |
| 348 | // now generate the distance field |
| 349 | SkASSERT(dst); |
| 350 | SkMatrix drawMatrix; |
| 351 | drawMatrix.setTranslate((SkScalar)-glyph.fLeft, (SkScalar)-glyph.fTop); |
| 352 | |
| 353 | // Generate signed distance field directly from SkPath |
| 354 | bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dst, |
| 355 | *path, drawMatrix, |
| 356 | width, height, width * sizeof(unsigned char)); |
| 357 | |
| 358 | if (!succeed) { |
| 359 | #endif |
| 360 | const void* image = cache->findImage(glyph); |
| 361 | if (nullptr == image) { |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | // now generate the distance field |
| 366 | SkASSERT(dst); |
| 367 | SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat); |
| 368 | if (SkMask::kA8_Format == maskFormat) { |
| 369 | // make the distance field from the image |
| 370 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dst, |
| 371 | (unsigned char*)image, |
| 372 | glyph.fWidth, glyph.fHeight, |
| 373 | glyph.rowBytes()); |
| 374 | } else if (SkMask::kBW_Format == maskFormat) { |
| 375 | // make the distance field from the image |
| 376 | SkGenerateDistanceFieldFromBWImage((unsigned char*)dst, |
| 377 | (unsigned char*)image, |
| 378 | glyph.fWidth, glyph.fHeight, |
| 379 | glyph.rowBytes()); |
| 380 | } else { |
| 381 | return false; |
| 382 | } |
| 383 | #ifndef SK_USE_LEGACY_DISTANCE_FIELDS |
| 384 | } |
| 385 | #endif |
bsalomon | f93f515 | 2016-10-26 08:00:00 -0700 | [diff] [blame] | 386 | return true; |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /////////////////////////////////////////////////////////////////////////////// |
| 390 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 391 | /* |
| 392 | The text strike is specific to a given font/style/matrix setup, which is |
| 393 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 394 | |
| 395 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 396 | atlas and a position within that texture. |
| 397 | */ |
| 398 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 399 | GrAtlasTextStrike::GrAtlasTextStrike(GrAtlasGlyphCache* owner, const SkDescriptor& key) |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 400 | : fFontScalerKey(key) |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 401 | , fPool(9/*start allocations at 512 bytes*/) |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 402 | , fAtlasGlyphCache(owner) // no need to ref, it won't go away before we do |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 403 | , fAtlasedGlyphs(0) |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 404 | , fIsAbandoned(false) {} |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 405 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 406 | GrAtlasTextStrike::~GrAtlasTextStrike() { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 407 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 408 | while (!iter.done()) { |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 409 | (*iter).reset(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 410 | ++iter; |
| 411 | } |
| 412 | } |
| 413 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 414 | GrGlyph* GrAtlasTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed, |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 415 | SkGlyphCache* cache) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 416 | SkIRect bounds; |
| 417 | if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 418 | if (!get_packed_glyph_df_bounds(cache, skGlyph, &bounds)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 419 | return nullptr; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 420 | } |
| 421 | } else { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 422 | if (!get_packed_glyph_bounds(cache, skGlyph, &bounds)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 423 | return nullptr; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 426 | GrMaskFormat format = get_packed_glyph_mask_format(skGlyph); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 427 | |
Herb Derby | 9428a37 | 2017-04-10 11:25:30 -0400 | [diff] [blame] | 428 | GrGlyph* glyph = fPool.make<GrGlyph>(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 429 | glyph->init(packed, bounds, format); |
| 430 | fCache.add(glyph); |
| 431 | return glyph; |
| 432 | } |
| 433 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 434 | void GrAtlasTextStrike::removeID(GrDrawOpAtlas::AtlasID id) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 435 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 436 | while (!iter.done()) { |
| 437 | if (id == (*iter).fID) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 438 | (*iter).fID = GrDrawOpAtlas::kInvalidAtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 439 | fAtlasedGlyphs--; |
| 440 | SkASSERT(fAtlasedGlyphs >= 0); |
| 441 | } |
| 442 | ++iter; |
| 443 | } |
| 444 | } |
| 445 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 446 | bool GrAtlasTextStrike::addGlyphToAtlas(GrDeferredUploadTarget* target, |
joshualitt | 50aa15b | 2015-11-23 14:09:55 -0800 | [diff] [blame] | 447 | GrGlyph* glyph, |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 448 | SkGlyphCache* cache, |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 449 | GrMaskFormat expectedMaskFormat) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 450 | SkASSERT(glyph); |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 451 | SkASSERT(cache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 452 | SkASSERT(fCache.find(glyph->fPackedID)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 453 | |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 454 | int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 455 | |
| 456 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 457 | SkAutoSMalloc<1024> storage(size); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 458 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 459 | const SkGlyph& skGlyph = GrToSkGlyph(cache, glyph->fPackedID); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 460 | if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 461 | if (!get_packed_glyph_df_image(cache, skGlyph, glyph->width(), glyph->height(), |
| 462 | storage.get())) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 463 | return false; |
| 464 | } |
| 465 | } else { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 466 | if (!get_packed_glyph_image(cache, skGlyph, glyph->width(), glyph->height(), |
| 467 | glyph->width() * bytesPerPixel, expectedMaskFormat, |
| 468 | storage.get())) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 473 | bool success = fAtlasGlyphCache->addToAtlas(this, &glyph->fID, target, expectedMaskFormat, |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 474 | glyph->width(), glyph->height(), |
| 475 | storage.get(), &glyph->fAtlasLocation); |
| 476 | if (success) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 477 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != glyph->fID); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 478 | fAtlasedGlyphs++; |
| 479 | } |
| 480 | return success; |
| 481 | } |