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