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