blob: 80f4314b91f80721cc95fa0e3b1a4d737437df55 [file] [log] [blame]
joshualitt7c3a2f82015-03-31 13:32:05 -07001/*
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 Salomonf856fd12016-12-16 14:24:34 -05008#include "GrAtlasGlyphCache.h"
kkinnunencabe20c2015-06-01 01:37:26 -07009#include "GrContext.h"
Robert Phillipsf95b1752017-08-31 08:56:07 -040010#include "GrDistanceFieldGenFromVector.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070011#include "GrGpu.h"
12#include "GrRectanizer.h"
Robert Phillipsf95b1752017-08-31 08:56:07 -040013
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "SkAutoMalloc.h"
Robert Phillipsf95b1752017-08-31 08:56:07 -040015#include "SkDistanceFieldGen.h"
Brian Osman6382f452017-08-11 13:34:02 -040016#include "SkMathPriv.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070017#include "SkString.h"
18
Brian Salomonf856fd12016-12-16 14:24:34 -050019bool GrAtlasGlyphCache::initAtlas(GrMaskFormat format) {
joshualitt62db8ba2015-04-09 08:22:37 -070020 int index = MaskFormatToAtlasIndex(format);
21 if (!fAtlases[index]) {
brianosman86dc2262016-07-08 06:15:45 -070022 GrPixelConfig config = MaskFormatToPixelConfig(format, *fContext->caps());
joshualittda04e0e2015-08-19 08:16:43 -070023 int width = fAtlasConfigs[index].fWidth;
24 int height = fAtlasConfigs[index].fHeight;
25 int numPlotsX = fAtlasConfigs[index].numPlotsX();
26 int numPlotsY = fAtlasConfigs[index].numPlotsY();
joshualitt7c3a2f82015-03-31 13:32:05 -070027
Brian Salomon9f545bc2017-11-06 10:36:57 -050028 fAtlases[index] = GrDrawOpAtlas::Make(fContext, config, width, height, numPlotsX, numPlotsY,
29 fAllowMultitexturing,
30 &GrAtlasGlyphCache::HandleEviction, (void*)this);
joshualittb356cbc2015-08-05 06:36:39 -070031 if (!fAtlases[index]) {
joshualitt62db8ba2015-04-09 08:22:37 -070032 return false;
joshualitt7c3a2f82015-03-31 13:32:05 -070033 }
34 }
joshualitt62db8ba2015-04-09 08:22:37 -070035 return true;
36}
37
Brian Salomon9f545bc2017-11-06 10:36:57 -050038GrAtlasGlyphCache::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 Osman6382f452017-08-11 13:34:02 -040042 int log2MaxTextureSize = SkPrevLog2(context->caps()->maxTextureSize());
Brian Salomon9f545bc2017-11-06 10:36:57 -050043 int log2MaxDim = 9;
Eric Karl6d342282017-05-03 17:08:42 -070044 for (; log2MaxDim <= log2MaxTextureSize; ++log2MaxDim) {
45 int maxDim = 1 << log2MaxDim;
46 int minDim = 1 << (log2MaxDim - 1);
joshualittda04e0e2015-08-19 08:16:43 -070047
Eric Karl6d342282017-05-03 17:08:42 -070048 if (maxDim * minDim * 4 >= maxTextureBytes) break;
49 }
joshualittda04e0e2015-08-19 08:16:43 -070050
Eric Karl6d342282017-05-03 17:08:42 -070051 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)));
joshualittda04e0e2015-08-19 08:16:43 -070057
Eric Karl6d342282017-05-03 17:08:42 -070058 // 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 Karl6d342282017-05-03 17:08:42 -070062 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 Karl6d342282017-05-03 17:08:42 -070068 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 Karl6d342282017-05-03 17:08:42 -070073 fAtlasConfigs[kARGB_GrMaskFormat].fPlotWidth = minPlot;
74 fAtlasConfigs[kARGB_GrMaskFormat].fPlotHeight = minPlot;
joshualitt7c3a2f82015-03-31 13:32:05 -070075}
76
Brian Salomonf856fd12016-12-16 14:24:34 -050077GrAtlasGlyphCache::~GrAtlasGlyphCache() {
bsalomonc5fd5c42016-05-17 11:58:24 -070078 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070079 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070080 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070081 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070082 ++iter;
83 }
joshualitt7c3a2f82015-03-31 13:32:05 -070084}
85
Brian Salomonf856fd12016-12-16 14:24:34 -050086void GrAtlasGlyphCache::freeAll() {
bsalomonc5fd5c42016-05-17 11:58:24 -070087 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070088 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070089 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070090 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070091 ++iter;
92 }
93 fCache.rewind();
94 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070095 fAtlases[i] = nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -070096 }
97}
98
Brian Salomon2ee084e2016-12-16 18:59:19 -050099void GrAtlasGlyphCache::HandleEviction(GrDrawOpAtlas::AtlasID id, void* ptr) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500100 GrAtlasGlyphCache* fontCache = reinterpret_cast<GrAtlasGlyphCache*>(ptr);
joshualitt7c3a2f82015-03-31 13:32:05 -0700101
bsalomonc5fd5c42016-05-17 11:58:24 -0700102 StrikeHash::Iter iter(&fontCache->fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700103 for (; !iter.done(); ++iter) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500104 GrAtlasTextStrike* strike = &*iter;
joshualitt7c3a2f82015-03-31 13:32:05 -0700105 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 Salomonf856fd12016-12-16 14:24:34 -0500110 fontCache->fCache.remove(GrAtlasTextStrike::GetKey(*strike));
joshualittae32c102015-04-21 09:37:57 -0700111 strike->fIsAbandoned = true;
112 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700113 }
114 }
115}
116
Robert Phillips31c26082016-12-14 15:12:15 -0500117#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 */
132static bool save_pixels(GrContext* context, GrSurfaceProxy* sProxy, const char* filename) {
Robert Phillips77b3f322017-01-31 18:24:12 -0500133 if (!sProxy) {
134 return false;
135 }
Robert Phillipsc949ce92017-01-19 16:59:04 -0500136
137 SkImageInfo ii = SkImageInfo::Make(sProxy->width(), sProxy->height(),
138 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillips31c26082016-12-14 15:12:15 -0500139 SkBitmap bm;
Robert Phillipsc949ce92017-01-19 16:59:04 -0500140 if (!bm.tryAllocPixels(ii)) {
Robert Phillips31c26082016-12-14 15:12:15 -0500141 return false;
142 }
143
144 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillips2c862492017-01-18 10:08:39 -0500145 sk_ref_sp(sProxy),
146 nullptr));
Robert Phillipsf200a902017-01-30 13:27:37 -0500147 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillips31c26082016-12-14 15:12:15 -0500148 return false;
149 }
150
Robert Phillipsc949ce92017-01-19 16:59:04 -0500151 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0);
Robert Phillips31c26082016-12-14 15:12:15 -0500152 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 Salomonf856fd12016-12-16 14:24:34 -0500176void GrAtlasGlyphCache::dump() const {
joshualitt7c3a2f82015-03-31 13:32:05 -0700177 static int gDumpCount = 0;
178 for (int i = 0; i < kMaskFormatCount; ++i) {
179 if (fAtlases[i]) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400180 const sk_sp<GrTextureProxy>* proxies = fAtlases[i]->getProxies();
Brian Salomon9f545bc2017-11-06 10:36:57 -0500181 for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->pageCount(); ++pageIdx) {
182 SkASSERT(proxies[pageIdx]);
183 SkString filename;
joshualitt7c3a2f82015-03-31 13:32:05 -0700184#ifdef SK_BUILD_FOR_ANDROID
Brian Salomon9f545bc2017-11-06 10:36:57 -0500185 filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
joshualitt7c3a2f82015-03-31 13:32:05 -0700186#else
Brian Salomon9f545bc2017-11-06 10:36:57 -0500187 filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
joshualitt7c3a2f82015-03-31 13:32:05 -0700188#endif
Robert Phillips31c26082016-12-14 15:12:15 -0500189
Brian Salomon9f545bc2017-11-06 10:36:57 -0500190 save_pixels(fContext, proxies[pageIdx].get(), filename.c_str());
joshualitt7c3a2f82015-03-31 13:32:05 -0700191 }
192 }
193 }
194 ++gDumpCount;
195}
Robert Phillips31c26082016-12-14 15:12:15 -0500196#endif
joshualitt7c3a2f82015-03-31 13:32:05 -0700197
Brian Salomon2ee084e2016-12-16 18:59:19 -0500198void GrAtlasGlyphCache::setAtlasSizes_ForTesting(const GrDrawOpAtlasConfig configs[3]) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500199 // Delete any old atlases.
200 // This should be safe to do as long as we are not in the middle of a flush.
joshualittda04e0e2015-08-19 08:16:43 -0700201 for (int i = 0; i < kMaskFormatCount; i++) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500202 fAtlases[i] = nullptr;
joshualittda04e0e2015-08-19 08:16:43 -0700203 }
204 memcpy(fAtlasConfigs, configs, sizeof(fAtlasConfigs));
205}
206
joshualitt7c3a2f82015-03-31 13:32:05 -0700207///////////////////////////////////////////////////////////////////////////////
208
bsalomonc2878e22016-05-17 13:18:03 -0700209static 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
226static 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
238static 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.
253template <typename INT_TYPE>
254static 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
275static 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 Wagnerb4aab9a2017-08-16 10:53:04 -0400316 SK_ABORT("Invalid GrMaskFormat");
bsalomonc2878e22016-05-17 13:18:03 -0700317 }
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
331static 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.liang8cbb4242017-01-09 18:39:43 -0800335
336#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
337 const SkPath* path = cache->findPath(glyph);
338 if (nullptr == path) {
bsalomonc2878e22016-05-17 13:18:03 -0700339 return false;
340 }
341
joel.liang8cbb4242017-01-09 18:39:43 -0800342 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
bsalomonf93f5152016-10-26 08:00:00 -0700386 return true;
bsalomonc2878e22016-05-17 13:18:03 -0700387}
388
389///////////////////////////////////////////////////////////////////////////////
390
joshualitt7c3a2f82015-03-31 13:32:05 -0700391/*
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 Salomonf856fd12016-12-16 14:24:34 -0500399GrAtlasTextStrike::GrAtlasTextStrike(GrAtlasGlyphCache* owner, const SkDescriptor& key)
bsalomonc5fd5c42016-05-17 11:58:24 -0700400 : fFontScalerKey(key)
joshualitt7c3a2f82015-03-31 13:32:05 -0700401 , fPool(9/*start allocations at 512 bytes*/)
Brian Salomonf856fd12016-12-16 14:24:34 -0500402 , fAtlasGlyphCache(owner) // no need to ref, it won't go away before we do
joshualittae32c102015-04-21 09:37:57 -0700403 , fAtlasedGlyphs(0)
bsalomonc5fd5c42016-05-17 11:58:24 -0700404 , fIsAbandoned(false) {}
joshualitt7c3a2f82015-03-31 13:32:05 -0700405
Brian Salomonf856fd12016-12-16 14:24:34 -0500406GrAtlasTextStrike::~GrAtlasTextStrike() {
joshualitt7c3a2f82015-03-31 13:32:05 -0700407 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
408 while (!iter.done()) {
mtklein852f15d2016-03-17 10:51:27 -0700409 (*iter).reset();
joshualitt7c3a2f82015-03-31 13:32:05 -0700410 ++iter;
411 }
412}
413
Brian Salomonf856fd12016-12-16 14:24:34 -0500414GrGlyph* GrAtlasTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
bsalomonc2878e22016-05-17 13:18:03 -0700415 SkGlyphCache* cache) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700416 SkIRect bounds;
417 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
bsalomonc2878e22016-05-17 13:18:03 -0700418 if (!get_packed_glyph_df_bounds(cache, skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700419 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700420 }
421 } else {
bsalomonc2878e22016-05-17 13:18:03 -0700422 if (!get_packed_glyph_bounds(cache, skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700423 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700424 }
425 }
bsalomonc2878e22016-05-17 13:18:03 -0700426 GrMaskFormat format = get_packed_glyph_mask_format(skGlyph);
joshualitt6c2c2b02015-07-24 10:37:00 -0700427
Herb Derby9428a372017-04-10 11:25:30 -0400428 GrGlyph* glyph = fPool.make<GrGlyph>();
joshualitt7c3a2f82015-03-31 13:32:05 -0700429 glyph->init(packed, bounds, format);
430 fCache.add(glyph);
431 return glyph;
432}
433
Brian Salomon2ee084e2016-12-16 18:59:19 -0500434void GrAtlasTextStrike::removeID(GrDrawOpAtlas::AtlasID id) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700435 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
436 while (!iter.done()) {
437 if (id == (*iter).fID) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500438 (*iter).fID = GrDrawOpAtlas::kInvalidAtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -0700439 fAtlasedGlyphs--;
440 SkASSERT(fAtlasedGlyphs >= 0);
441 }
442 ++iter;
443 }
444}
445
Brian Salomon29b60c92017-10-31 14:42:10 -0400446bool GrAtlasTextStrike::addGlyphToAtlas(GrDeferredUploadTarget* target,
joshualitt50aa15b2015-11-23 14:09:55 -0800447 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700448 SkGlyphCache* cache,
joshualitt4f19ca32015-07-30 07:59:20 -0700449 GrMaskFormat expectedMaskFormat) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700450 SkASSERT(glyph);
bsalomonc2878e22016-05-17 13:18:03 -0700451 SkASSERT(cache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700452 SkASSERT(fCache.find(glyph->fPackedID));
joshualitt7c3a2f82015-03-31 13:32:05 -0700453
joshualitt4f19ca32015-07-30 07:59:20 -0700454 int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -0700455
456 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700457 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700458
bsalomonc2878e22016-05-17 13:18:03 -0700459 const SkGlyph& skGlyph = GrToSkGlyph(cache, glyph->fPackedID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700460 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
bsalomonc2878e22016-05-17 13:18:03 -0700461 if (!get_packed_glyph_df_image(cache, skGlyph, glyph->width(), glyph->height(),
462 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700463 return false;
464 }
465 } else {
bsalomonc2878e22016-05-17 13:18:03 -0700466 if (!get_packed_glyph_image(cache, skGlyph, glyph->width(), glyph->height(),
467 glyph->width() * bytesPerPixel, expectedMaskFormat,
468 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700469 return false;
470 }
471 }
472
Brian Salomonf856fd12016-12-16 14:24:34 -0500473 bool success = fAtlasGlyphCache->addToAtlas(this, &glyph->fID, target, expectedMaskFormat,
joshualitt7c3a2f82015-03-31 13:32:05 -0700474 glyph->width(), glyph->height(),
475 storage.get(), &glyph->fAtlasLocation);
476 if (success) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500477 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != glyph->fID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700478 fAtlasedGlyphs++;
479 }
480 return success;
481}