blob: 7456a8e4dc6b77cc429712b8993c7c8cac1fd5da [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
Robert Phillips256c37b2017-03-01 14:32:46 -050028 fAtlases[index] = GrDrawOpAtlas::Make(
Robert Phillips32f28182017-02-28 16:20:03 -050029 fContext, config, width, height, numPlotsX, numPlotsY,
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
Eric Karl6d342282017-05-03 17:08:42 -070038GrAtlasGlyphCache::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 Osman6382f452017-08-11 13:34:02 -040041 int log2MaxTextureSize = SkPrevLog2(context->caps()->maxTextureSize());
Eric Karl6d342282017-05-03 17:08:42 -070042 int log2MaxDim = 10;
43 for (; log2MaxDim <= log2MaxTextureSize; ++log2MaxDim) {
44 int maxDim = 1 << log2MaxDim;
45 int minDim = 1 << (log2MaxDim - 1);
joshualittda04e0e2015-08-19 08:16:43 -070046
Eric Karl6d342282017-05-03 17:08:42 -070047 if (maxDim * minDim * 4 >= maxTextureBytes) break;
48 }
joshualittda04e0e2015-08-19 08:16:43 -070049
Eric Karl6d342282017-05-03 17:08:42 -070050 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)));
joshualittda04e0e2015-08-19 08:16:43 -070056
Eric Karl6d342282017-05-03 17:08:42 -070057 // 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 Karl6d342282017-05-03 17:08:42 -070061 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 Karl6d342282017-05-03 17:08:42 -070067 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 Karl6d342282017-05-03 17:08:42 -070072 fAtlasConfigs[kARGB_GrMaskFormat].fPlotWidth = minPlot;
73 fAtlasConfigs[kARGB_GrMaskFormat].fPlotHeight = minPlot;
joshualitt7c3a2f82015-03-31 13:32:05 -070074}
75
Brian Salomonf856fd12016-12-16 14:24:34 -050076GrAtlasGlyphCache::~GrAtlasGlyphCache() {
bsalomonc5fd5c42016-05-17 11:58:24 -070077 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070078 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070079 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070080 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070081 ++iter;
82 }
joshualitt7c3a2f82015-03-31 13:32:05 -070083}
84
Brian Salomonf856fd12016-12-16 14:24:34 -050085void GrAtlasGlyphCache::freeAll() {
bsalomonc5fd5c42016-05-17 11:58:24 -070086 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070087 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070088 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070089 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070090 ++iter;
91 }
92 fCache.rewind();
93 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 fAtlases[i] = nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -070095 }
96}
97
Brian Salomon2ee084e2016-12-16 18:59:19 -050098void GrAtlasGlyphCache::HandleEviction(GrDrawOpAtlas::AtlasID id, void* ptr) {
Brian Salomonf856fd12016-12-16 14:24:34 -050099 GrAtlasGlyphCache* fontCache = reinterpret_cast<GrAtlasGlyphCache*>(ptr);
joshualitt7c3a2f82015-03-31 13:32:05 -0700100
bsalomonc5fd5c42016-05-17 11:58:24 -0700101 StrikeHash::Iter iter(&fontCache->fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700102 for (; !iter.done(); ++iter) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500103 GrAtlasTextStrike* strike = &*iter;
joshualitt7c3a2f82015-03-31 13:32:05 -0700104 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 Salomonf856fd12016-12-16 14:24:34 -0500109 fontCache->fCache.remove(GrAtlasTextStrike::GetKey(*strike));
joshualittae32c102015-04-21 09:37:57 -0700110 strike->fIsAbandoned = true;
111 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700112 }
113 }
114}
115
Robert Phillips31c26082016-12-14 15:12:15 -0500116#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 */
131static bool save_pixels(GrContext* context, GrSurfaceProxy* sProxy, const char* filename) {
Robert Phillips77b3f322017-01-31 18:24:12 -0500132 if (!sProxy) {
133 return false;
134 }
Robert Phillipsc949ce92017-01-19 16:59:04 -0500135
136 SkImageInfo ii = SkImageInfo::Make(sProxy->width(), sProxy->height(),
137 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillips31c26082016-12-14 15:12:15 -0500138 SkBitmap bm;
Robert Phillipsc949ce92017-01-19 16:59:04 -0500139 if (!bm.tryAllocPixels(ii)) {
Robert Phillips31c26082016-12-14 15:12:15 -0500140 return false;
141 }
142
143 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillips2c862492017-01-18 10:08:39 -0500144 sk_ref_sp(sProxy),
145 nullptr));
Robert Phillipsf200a902017-01-30 13:27:37 -0500146 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillips31c26082016-12-14 15:12:15 -0500147 return false;
148 }
149
Robert Phillipsc949ce92017-01-19 16:59:04 -0500150 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0);
Robert Phillips31c26082016-12-14 15:12:15 -0500151 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 Salomonf856fd12016-12-16 14:24:34 -0500175void GrAtlasGlyphCache::dump() const {
joshualitt7c3a2f82015-03-31 13:32:05 -0700176 static int gDumpCount = 0;
177 for (int i = 0; i < kMaskFormatCount; ++i) {
178 if (fAtlases[i]) {
Robert Phillips32f28182017-02-28 16:20:03 -0500179 sk_sp<GrTextureProxy> proxy = fAtlases[i]->getProxy();
180 if (proxy) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700181 SkString filename;
182#ifdef SK_BUILD_FOR_ANDROID
183 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
184#else
185 filename.printf("fontcache_%d%d.png", gDumpCount, i);
186#endif
Robert Phillips31c26082016-12-14 15:12:15 -0500187
Robert Phillips32f28182017-02-28 16:20:03 -0500188 save_pixels(fContext, proxy.get(), filename.c_str());
joshualitt7c3a2f82015-03-31 13:32:05 -0700189 }
190 }
191 }
192 ++gDumpCount;
193}
Robert Phillips31c26082016-12-14 15:12:15 -0500194#endif
joshualitt7c3a2f82015-03-31 13:32:05 -0700195
Brian Salomon2ee084e2016-12-16 18:59:19 -0500196void GrAtlasGlyphCache::setAtlasSizes_ForTesting(const GrDrawOpAtlasConfig configs[3]) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500197 // Delete any old atlases.
198 // This should be safe to do as long as we are not in the middle of a flush.
joshualittda04e0e2015-08-19 08:16:43 -0700199 for (int i = 0; i < kMaskFormatCount; i++) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500200 fAtlases[i] = nullptr;
joshualittda04e0e2015-08-19 08:16:43 -0700201 }
202 memcpy(fAtlasConfigs, configs, sizeof(fAtlasConfigs));
203}
204
joshualitt7c3a2f82015-03-31 13:32:05 -0700205///////////////////////////////////////////////////////////////////////////////
206
bsalomonc2878e22016-05-17 13:18:03 -0700207static inline GrMaskFormat get_packed_glyph_mask_format(const SkGlyph& glyph) {
208 SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat);
209 switch (format) {
210 case SkMask::kBW_Format:
211 // fall through to kA8 -- we store BW glyphs in our 8-bit cache
212 case SkMask::kA8_Format:
213 return kA8_GrMaskFormat;
214 case SkMask::kLCD16_Format:
215 return kA565_GrMaskFormat;
216 case SkMask::kARGB32_Format:
217 return kARGB_GrMaskFormat;
218 default:
219 SkDEBUGFAIL("unsupported SkMask::Format");
220 return kA8_GrMaskFormat;
221 }
222}
223
224static inline bool get_packed_glyph_bounds(SkGlyphCache* cache, const SkGlyph& glyph,
225 SkIRect* bounds) {
226#if 1
227 // crbug:510931
228 // Retrieving the image from the cache can actually change the mask format.
229 cache->findImage(glyph);
230#endif
231 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
232
233 return true;
234}
235
236static inline bool get_packed_glyph_df_bounds(SkGlyphCache* cache, const SkGlyph& glyph,
237 SkIRect* bounds) {
238#if 1
239 // crbug:510931
240 // Retrieving the image from the cache can actually change the mask format.
241 cache->findImage(glyph);
242#endif
243 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
244 bounds->outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
245
246 return true;
247}
248
249// expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to
250// A8, RGB565, or RGBA8888.
251template <typename INT_TYPE>
252static void expand_bits(INT_TYPE* dst,
253 const uint8_t* src,
254 int width,
255 int height,
256 int dstRowBytes,
257 int srcRowBytes) {
258 for (int i = 0; i < height; ++i) {
259 int rowWritesLeft = width;
260 const uint8_t* s = src;
261 INT_TYPE* d = dst;
262 while (rowWritesLeft > 0) {
263 unsigned mask = *s++;
264 for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) {
265 *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0;
266 }
267 }
268 dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes);
269 src += srcRowBytes;
270 }
271}
272
273static bool get_packed_glyph_image(SkGlyphCache* cache, const SkGlyph& glyph, int width,
274 int height, int dstRB, GrMaskFormat expectedMaskFormat,
275 void* dst) {
276 SkASSERT(glyph.fWidth == width);
277 SkASSERT(glyph.fHeight == height);
278 const void* src = cache->findImage(glyph);
279 if (nullptr == src) {
280 return false;
281 }
282
283 // crbug:510931
284 // Retrieving the image from the cache can actually change the mask format. This case is very
285 // uncommon so for now we just draw a clear box for these glyphs.
286 if (get_packed_glyph_mask_format(glyph) != expectedMaskFormat) {
287 const int bpp = GrMaskFormatBytesPerPixel(expectedMaskFormat);
288 for (int y = 0; y < height; y++) {
289 sk_bzero(dst, width * bpp);
290 dst = (char*)dst + dstRB;
291 }
292 return true;
293 }
294
295 int srcRB = glyph.rowBytes();
296 // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to
297 // check the glyph's format, not the strike's format, and to be able to convert to any of the
298 // GrMaskFormats.
299 if (SkMask::kBW_Format == glyph.fMaskFormat) {
300 // expand bits to our mask type
301 const uint8_t* bits = reinterpret_cast<const uint8_t*>(src);
302 switch (expectedMaskFormat) {
303 case kA8_GrMaskFormat:{
304 uint8_t* bytes = reinterpret_cast<uint8_t*>(dst);
305 expand_bits(bytes, bits, width, height, dstRB, srcRB);
306 break;
307 }
308 case kA565_GrMaskFormat: {
309 uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst);
310 expand_bits(rgb565, bits, width, height, dstRB, srcRB);
311 break;
312 }
313 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400314 SK_ABORT("Invalid GrMaskFormat");
bsalomonc2878e22016-05-17 13:18:03 -0700315 }
316 } else if (srcRB == dstRB) {
317 memcpy(dst, src, dstRB * height);
318 } else {
319 const int bbp = GrMaskFormatBytesPerPixel(expectedMaskFormat);
320 for (int y = 0; y < height; y++) {
321 memcpy(dst, src, width * bbp);
322 src = (const char*)src + srcRB;
323 dst = (char*)dst + dstRB;
324 }
325 }
326 return true;
327}
328
329static bool get_packed_glyph_df_image(SkGlyphCache* cache, const SkGlyph& glyph,
330 int width, int height, void* dst) {
331 SkASSERT(glyph.fWidth + 2*SK_DistanceFieldPad == width);
332 SkASSERT(glyph.fHeight + 2*SK_DistanceFieldPad == height);
joel.liang8cbb4242017-01-09 18:39:43 -0800333
334#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
335 const SkPath* path = cache->findPath(glyph);
336 if (nullptr == path) {
bsalomonc2878e22016-05-17 13:18:03 -0700337 return false;
338 }
339
joel.liang8cbb4242017-01-09 18:39:43 -0800340 SkDEBUGCODE(SkRect glyphBounds = SkRect::MakeXYWH(glyph.fLeft,
341 glyph.fTop,
342 glyph.fWidth,
343 glyph.fHeight));
344 SkASSERT(glyphBounds.contains(path->getBounds()));
345
346 // now generate the distance field
347 SkASSERT(dst);
348 SkMatrix drawMatrix;
349 drawMatrix.setTranslate((SkScalar)-glyph.fLeft, (SkScalar)-glyph.fTop);
350
351 // Generate signed distance field directly from SkPath
352 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dst,
353 *path, drawMatrix,
354 width, height, width * sizeof(unsigned char));
355
356 if (!succeed) {
357#endif
358 const void* image = cache->findImage(glyph);
359 if (nullptr == image) {
360 return false;
361 }
362
363 // now generate the distance field
364 SkASSERT(dst);
365 SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
366 if (SkMask::kA8_Format == maskFormat) {
367 // make the distance field from the image
368 SkGenerateDistanceFieldFromA8Image((unsigned char*)dst,
369 (unsigned char*)image,
370 glyph.fWidth, glyph.fHeight,
371 glyph.rowBytes());
372 } else if (SkMask::kBW_Format == maskFormat) {
373 // make the distance field from the image
374 SkGenerateDistanceFieldFromBWImage((unsigned char*)dst,
375 (unsigned char*)image,
376 glyph.fWidth, glyph.fHeight,
377 glyph.rowBytes());
378 } else {
379 return false;
380 }
381#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
382 }
383#endif
bsalomonf93f5152016-10-26 08:00:00 -0700384 return true;
bsalomonc2878e22016-05-17 13:18:03 -0700385}
386
387///////////////////////////////////////////////////////////////////////////////
388
joshualitt7c3a2f82015-03-31 13:32:05 -0700389/*
390 The text strike is specific to a given font/style/matrix setup, which is
391 represented by the GrHostFontScaler object we are given in getGlyph().
392
393 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
394 atlas and a position within that texture.
395 */
396
Brian Salomonf856fd12016-12-16 14:24:34 -0500397GrAtlasTextStrike::GrAtlasTextStrike(GrAtlasGlyphCache* owner, const SkDescriptor& key)
bsalomonc5fd5c42016-05-17 11:58:24 -0700398 : fFontScalerKey(key)
joshualitt7c3a2f82015-03-31 13:32:05 -0700399 , fPool(9/*start allocations at 512 bytes*/)
Brian Salomonf856fd12016-12-16 14:24:34 -0500400 , fAtlasGlyphCache(owner) // no need to ref, it won't go away before we do
joshualittae32c102015-04-21 09:37:57 -0700401 , fAtlasedGlyphs(0)
bsalomonc5fd5c42016-05-17 11:58:24 -0700402 , fIsAbandoned(false) {}
joshualitt7c3a2f82015-03-31 13:32:05 -0700403
Brian Salomonf856fd12016-12-16 14:24:34 -0500404GrAtlasTextStrike::~GrAtlasTextStrike() {
joshualitt7c3a2f82015-03-31 13:32:05 -0700405 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
406 while (!iter.done()) {
mtklein852f15d2016-03-17 10:51:27 -0700407 (*iter).reset();
joshualitt7c3a2f82015-03-31 13:32:05 -0700408 ++iter;
409 }
410}
411
Brian Salomonf856fd12016-12-16 14:24:34 -0500412GrGlyph* GrAtlasTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
bsalomonc2878e22016-05-17 13:18:03 -0700413 SkGlyphCache* cache) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700414 SkIRect bounds;
415 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
bsalomonc2878e22016-05-17 13:18:03 -0700416 if (!get_packed_glyph_df_bounds(cache, skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700417 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700418 }
419 } else {
bsalomonc2878e22016-05-17 13:18:03 -0700420 if (!get_packed_glyph_bounds(cache, skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700421 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700422 }
423 }
bsalomonc2878e22016-05-17 13:18:03 -0700424 GrMaskFormat format = get_packed_glyph_mask_format(skGlyph);
joshualitt6c2c2b02015-07-24 10:37:00 -0700425
Herb Derby9428a372017-04-10 11:25:30 -0400426 GrGlyph* glyph = fPool.make<GrGlyph>();
joshualitt7c3a2f82015-03-31 13:32:05 -0700427 glyph->init(packed, bounds, format);
428 fCache.add(glyph);
429 return glyph;
430}
431
Brian Salomon2ee084e2016-12-16 18:59:19 -0500432void GrAtlasTextStrike::removeID(GrDrawOpAtlas::AtlasID id) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700433 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
434 while (!iter.done()) {
435 if (id == (*iter).fID) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500436 (*iter).fID = GrDrawOpAtlas::kInvalidAtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -0700437 fAtlasedGlyphs--;
438 SkASSERT(fAtlasedGlyphs >= 0);
439 }
440 ++iter;
441 }
442}
443
Brian Salomonf856fd12016-12-16 14:24:34 -0500444bool GrAtlasTextStrike::addGlyphToAtlas(GrDrawOp::Target* target,
joshualitt50aa15b2015-11-23 14:09:55 -0800445 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700446 SkGlyphCache* cache,
joshualitt4f19ca32015-07-30 07:59:20 -0700447 GrMaskFormat expectedMaskFormat) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700448 SkASSERT(glyph);
bsalomonc2878e22016-05-17 13:18:03 -0700449 SkASSERT(cache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700450 SkASSERT(fCache.find(glyph->fPackedID));
joshualitt7c3a2f82015-03-31 13:32:05 -0700451
joshualitt4f19ca32015-07-30 07:59:20 -0700452 int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -0700453
454 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700455 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700456
bsalomonc2878e22016-05-17 13:18:03 -0700457 const SkGlyph& skGlyph = GrToSkGlyph(cache, glyph->fPackedID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700458 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
bsalomonc2878e22016-05-17 13:18:03 -0700459 if (!get_packed_glyph_df_image(cache, skGlyph, glyph->width(), glyph->height(),
460 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700461 return false;
462 }
463 } else {
bsalomonc2878e22016-05-17 13:18:03 -0700464 if (!get_packed_glyph_image(cache, skGlyph, glyph->width(), glyph->height(),
465 glyph->width() * bytesPerPixel, expectedMaskFormat,
466 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700467 return false;
468 }
469 }
470
Brian Salomonf856fd12016-12-16 14:24:34 -0500471 bool success = fAtlasGlyphCache->addToAtlas(this, &glyph->fID, target, expectedMaskFormat,
joshualitt7c3a2f82015-03-31 13:32:05 -0700472 glyph->width(), glyph->height(),
473 storage.get(), &glyph->fAtlasLocation);
474 if (success) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500475 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != glyph->fID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700476 fAtlasedGlyphs++;
477 }
478 return success;
479}