blob: 782c3c17b31a3b1cfb0ec1b196304f96fdf77346 [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"
joshualitt7c3a2f82015-03-31 13:32:05 -070010#include "GrGpu.h"
11#include "GrRectanizer.h"
12#include "GrSurfacePriv.h"
Hal Canary95e3c052017-01-11 12:44:43 -050013#include "SkAutoMalloc.h"
Brian Osman6382f452017-08-11 13:34:02 -040014#include "SkMathPriv.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070015#include "SkString.h"
16
17#include "SkDistanceFieldGen.h"
joel.liang8cbb4242017-01-09 18:39:43 -080018#include "GrDistanceFieldGenFromVector.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070019
Brian Salomonf856fd12016-12-16 14:24:34 -050020bool GrAtlasGlyphCache::initAtlas(GrMaskFormat format) {
joshualitt62db8ba2015-04-09 08:22:37 -070021 int index = MaskFormatToAtlasIndex(format);
22 if (!fAtlases[index]) {
brianosman86dc2262016-07-08 06:15:45 -070023 GrPixelConfig config = MaskFormatToPixelConfig(format, *fContext->caps());
joshualittda04e0e2015-08-19 08:16:43 -070024 int width = fAtlasConfigs[index].fWidth;
25 int height = fAtlasConfigs[index].fHeight;
26 int numPlotsX = fAtlasConfigs[index].numPlotsX();
27 int numPlotsY = fAtlasConfigs[index].numPlotsY();
joshualitt7c3a2f82015-03-31 13:32:05 -070028
Robert Phillips256c37b2017-03-01 14:32:46 -050029 fAtlases[index] = GrDrawOpAtlas::Make(
Robert Phillips32f28182017-02-28 16:20:03 -050030 fContext, config, width, height, numPlotsX, numPlotsY,
31 &GrAtlasGlyphCache::HandleEviction, (void*)this);
joshualittb356cbc2015-08-05 06:36:39 -070032 if (!fAtlases[index]) {
joshualitt62db8ba2015-04-09 08:22:37 -070033 return false;
joshualitt7c3a2f82015-03-31 13:32:05 -070034 }
35 }
joshualitt62db8ba2015-04-09 08:22:37 -070036 return true;
37}
38
Eric Karl6d342282017-05-03 17:08:42 -070039GrAtlasGlyphCache::GrAtlasGlyphCache(GrContext* context, float maxTextureBytes)
40 : fContext(context), fPreserveStrike(nullptr) {
41 // Calculate RGBA size. Must be between 1024 x 512 and MaxTextureSize x MaxTextureSize / 2
Brian Osman6382f452017-08-11 13:34:02 -040042 int log2MaxTextureSize = SkPrevLog2(context->caps()->maxTextureSize());
Eric Karl6d342282017-05-03 17:08:42 -070043 int log2MaxDim = 10;
44 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;
62 fAtlasConfigs[kA8_GrMaskFormat].fLog2Width = log2MaxDim;
63 fAtlasConfigs[kA8_GrMaskFormat].fLog2Height = log2MaxDim;
64 fAtlasConfigs[kA8_GrMaskFormat].fPlotWidth = maxPlot;
65 fAtlasConfigs[kA8_GrMaskFormat].fPlotHeight = minPlot;
66
67 // A565 and ARGB use maxDim x minDim.
68 fAtlasConfigs[kA565_GrMaskFormat].fWidth = minDim;
69 fAtlasConfigs[kA565_GrMaskFormat].fHeight = maxDim;
70 fAtlasConfigs[kA565_GrMaskFormat].fLog2Width = log2MinDim;
71 fAtlasConfigs[kA565_GrMaskFormat].fLog2Height = log2MaxDim;
72 fAtlasConfigs[kA565_GrMaskFormat].fPlotWidth = minPlot;
73 fAtlasConfigs[kA565_GrMaskFormat].fPlotHeight = minPlot;
74
75 fAtlasConfigs[kARGB_GrMaskFormat].fWidth = minDim;
76 fAtlasConfigs[kARGB_GrMaskFormat].fHeight = maxDim;
77 fAtlasConfigs[kARGB_GrMaskFormat].fLog2Width = log2MinDim;
78 fAtlasConfigs[kARGB_GrMaskFormat].fLog2Height = log2MaxDim;
79 fAtlasConfigs[kARGB_GrMaskFormat].fPlotWidth = minPlot;
80 fAtlasConfigs[kARGB_GrMaskFormat].fPlotHeight = minPlot;
joshualitt7c3a2f82015-03-31 13:32:05 -070081}
82
Brian Salomonf856fd12016-12-16 14:24:34 -050083GrAtlasGlyphCache::~GrAtlasGlyphCache() {
bsalomonc5fd5c42016-05-17 11:58:24 -070084 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070085 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070086 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070087 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070088 ++iter;
89 }
joshualitt7c3a2f82015-03-31 13:32:05 -070090}
91
Brian Salomonf856fd12016-12-16 14:24:34 -050092void GrAtlasGlyphCache::freeAll() {
bsalomonc5fd5c42016-05-17 11:58:24 -070093 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070094 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070095 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070096 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070097 ++iter;
98 }
99 fCache.rewind();
100 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700101 fAtlases[i] = nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700102 }
103}
104
Brian Salomon2ee084e2016-12-16 18:59:19 -0500105void GrAtlasGlyphCache::HandleEviction(GrDrawOpAtlas::AtlasID id, void* ptr) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500106 GrAtlasGlyphCache* fontCache = reinterpret_cast<GrAtlasGlyphCache*>(ptr);
joshualitt7c3a2f82015-03-31 13:32:05 -0700107
bsalomonc5fd5c42016-05-17 11:58:24 -0700108 StrikeHash::Iter iter(&fontCache->fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700109 for (; !iter.done(); ++iter) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500110 GrAtlasTextStrike* strike = &*iter;
joshualitt7c3a2f82015-03-31 13:32:05 -0700111 strike->removeID(id);
112
113 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
114 // triggered the eviction
115 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500116 fontCache->fCache.remove(GrAtlasTextStrike::GetKey(*strike));
joshualittae32c102015-04-21 09:37:57 -0700117 strike->fIsAbandoned = true;
118 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700119 }
120 }
121}
122
Robert Phillips31c26082016-12-14 15:12:15 -0500123#ifdef SK_DEBUG
124#include "GrContextPriv.h"
125#include "GrSurfaceProxy.h"
126#include "GrSurfaceContext.h"
127#include "GrTextureProxy.h"
128
129#include "SkBitmap.h"
130#include "SkImageEncoder.h"
131#include "SkStream.h"
132#include <stdio.h>
133
134/**
135 * Write the contents of the surface proxy to a PNG. Returns true if successful.
136 * @param filename Full path to desired file
137 */
138static bool save_pixels(GrContext* context, GrSurfaceProxy* sProxy, const char* filename) {
Robert Phillips77b3f322017-01-31 18:24:12 -0500139 if (!sProxy) {
140 return false;
141 }
Robert Phillipsc949ce92017-01-19 16:59:04 -0500142
143 SkImageInfo ii = SkImageInfo::Make(sProxy->width(), sProxy->height(),
144 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillips31c26082016-12-14 15:12:15 -0500145 SkBitmap bm;
Robert Phillipsc949ce92017-01-19 16:59:04 -0500146 if (!bm.tryAllocPixels(ii)) {
Robert Phillips31c26082016-12-14 15:12:15 -0500147 return false;
148 }
149
150 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
Robert Phillips2c862492017-01-18 10:08:39 -0500151 sk_ref_sp(sProxy),
152 nullptr));
Robert Phillipsf200a902017-01-30 13:27:37 -0500153 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillips31c26082016-12-14 15:12:15 -0500154 return false;
155 }
156
Robert Phillipsc949ce92017-01-19 16:59:04 -0500157 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0);
Robert Phillips31c26082016-12-14 15:12:15 -0500158 if (!result) {
159 SkDebugf("------ failed to read pixels for %s\n", filename);
160 return false;
161 }
162
163 // remove any previous version of this file
164 remove(filename);
165
166 SkFILEWStream file(filename);
167 if (!file.isValid()) {
168 SkDebugf("------ failed to create file: %s\n", filename);
169 remove(filename); // remove any partial file
170 return false;
171 }
172
173 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
174 SkDebugf("------ failed to encode %s\n", filename);
175 remove(filename); // remove any partial file
176 return false;
177 }
178
179 return true;
180}
181
Brian Salomonf856fd12016-12-16 14:24:34 -0500182void GrAtlasGlyphCache::dump() const {
joshualitt7c3a2f82015-03-31 13:32:05 -0700183 static int gDumpCount = 0;
184 for (int i = 0; i < kMaskFormatCount; ++i) {
185 if (fAtlases[i]) {
Robert Phillips32f28182017-02-28 16:20:03 -0500186 sk_sp<GrTextureProxy> proxy = fAtlases[i]->getProxy();
187 if (proxy) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700188 SkString filename;
189#ifdef SK_BUILD_FOR_ANDROID
190 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
191#else
192 filename.printf("fontcache_%d%d.png", gDumpCount, i);
193#endif
Robert Phillips31c26082016-12-14 15:12:15 -0500194
Robert Phillips32f28182017-02-28 16:20:03 -0500195 save_pixels(fContext, proxy.get(), filename.c_str());
joshualitt7c3a2f82015-03-31 13:32:05 -0700196 }
197 }
198 }
199 ++gDumpCount;
200}
Robert Phillips31c26082016-12-14 15:12:15 -0500201#endif
joshualitt7c3a2f82015-03-31 13:32:05 -0700202
Brian Salomon2ee084e2016-12-16 18:59:19 -0500203void GrAtlasGlyphCache::setAtlasSizes_ForTesting(const GrDrawOpAtlasConfig configs[3]) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500204 // Delete any old atlases.
205 // This should be safe to do as long as we are not in the middle of a flush.
joshualittda04e0e2015-08-19 08:16:43 -0700206 for (int i = 0; i < kMaskFormatCount; i++) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500207 fAtlases[i] = nullptr;
joshualittda04e0e2015-08-19 08:16:43 -0700208 }
209 memcpy(fAtlasConfigs, configs, sizeof(fAtlasConfigs));
210}
211
joshualitt7c3a2f82015-03-31 13:32:05 -0700212///////////////////////////////////////////////////////////////////////////////
213
bsalomonc2878e22016-05-17 13:18:03 -0700214static inline GrMaskFormat get_packed_glyph_mask_format(const SkGlyph& glyph) {
215 SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat);
216 switch (format) {
217 case SkMask::kBW_Format:
218 // fall through to kA8 -- we store BW glyphs in our 8-bit cache
219 case SkMask::kA8_Format:
220 return kA8_GrMaskFormat;
221 case SkMask::kLCD16_Format:
222 return kA565_GrMaskFormat;
223 case SkMask::kARGB32_Format:
224 return kARGB_GrMaskFormat;
225 default:
226 SkDEBUGFAIL("unsupported SkMask::Format");
227 return kA8_GrMaskFormat;
228 }
229}
230
231static inline bool get_packed_glyph_bounds(SkGlyphCache* cache, const SkGlyph& glyph,
232 SkIRect* bounds) {
233#if 1
234 // crbug:510931
235 // Retrieving the image from the cache can actually change the mask format.
236 cache->findImage(glyph);
237#endif
238 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
239
240 return true;
241}
242
243static inline bool get_packed_glyph_df_bounds(SkGlyphCache* cache, const SkGlyph& glyph,
244 SkIRect* bounds) {
245#if 1
246 // crbug:510931
247 // Retrieving the image from the cache can actually change the mask format.
248 cache->findImage(glyph);
249#endif
250 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
251 bounds->outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
252
253 return true;
254}
255
256// expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to
257// A8, RGB565, or RGBA8888.
258template <typename INT_TYPE>
259static void expand_bits(INT_TYPE* dst,
260 const uint8_t* src,
261 int width,
262 int height,
263 int dstRowBytes,
264 int srcRowBytes) {
265 for (int i = 0; i < height; ++i) {
266 int rowWritesLeft = width;
267 const uint8_t* s = src;
268 INT_TYPE* d = dst;
269 while (rowWritesLeft > 0) {
270 unsigned mask = *s++;
271 for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) {
272 *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0;
273 }
274 }
275 dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes);
276 src += srcRowBytes;
277 }
278}
279
280static bool get_packed_glyph_image(SkGlyphCache* cache, const SkGlyph& glyph, int width,
281 int height, int dstRB, GrMaskFormat expectedMaskFormat,
282 void* dst) {
283 SkASSERT(glyph.fWidth == width);
284 SkASSERT(glyph.fHeight == height);
285 const void* src = cache->findImage(glyph);
286 if (nullptr == src) {
287 return false;
288 }
289
290 // crbug:510931
291 // Retrieving the image from the cache can actually change the mask format. This case is very
292 // uncommon so for now we just draw a clear box for these glyphs.
293 if (get_packed_glyph_mask_format(glyph) != expectedMaskFormat) {
294 const int bpp = GrMaskFormatBytesPerPixel(expectedMaskFormat);
295 for (int y = 0; y < height; y++) {
296 sk_bzero(dst, width * bpp);
297 dst = (char*)dst + dstRB;
298 }
299 return true;
300 }
301
302 int srcRB = glyph.rowBytes();
303 // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to
304 // check the glyph's format, not the strike's format, and to be able to convert to any of the
305 // GrMaskFormats.
306 if (SkMask::kBW_Format == glyph.fMaskFormat) {
307 // expand bits to our mask type
308 const uint8_t* bits = reinterpret_cast<const uint8_t*>(src);
309 switch (expectedMaskFormat) {
310 case kA8_GrMaskFormat:{
311 uint8_t* bytes = reinterpret_cast<uint8_t*>(dst);
312 expand_bits(bytes, bits, width, height, dstRB, srcRB);
313 break;
314 }
315 case kA565_GrMaskFormat: {
316 uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst);
317 expand_bits(rgb565, bits, width, height, dstRB, srcRB);
318 break;
319 }
320 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400321 SK_ABORT("Invalid GrMaskFormat");
bsalomonc2878e22016-05-17 13:18:03 -0700322 }
323 } else if (srcRB == dstRB) {
324 memcpy(dst, src, dstRB * height);
325 } else {
326 const int bbp = GrMaskFormatBytesPerPixel(expectedMaskFormat);
327 for (int y = 0; y < height; y++) {
328 memcpy(dst, src, width * bbp);
329 src = (const char*)src + srcRB;
330 dst = (char*)dst + dstRB;
331 }
332 }
333 return true;
334}
335
336static bool get_packed_glyph_df_image(SkGlyphCache* cache, const SkGlyph& glyph,
337 int width, int height, void* dst) {
338 SkASSERT(glyph.fWidth + 2*SK_DistanceFieldPad == width);
339 SkASSERT(glyph.fHeight + 2*SK_DistanceFieldPad == height);
joel.liang8cbb4242017-01-09 18:39:43 -0800340
341#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
342 const SkPath* path = cache->findPath(glyph);
343 if (nullptr == path) {
bsalomonc2878e22016-05-17 13:18:03 -0700344 return false;
345 }
346
joel.liang8cbb4242017-01-09 18:39:43 -0800347 SkDEBUGCODE(SkRect glyphBounds = SkRect::MakeXYWH(glyph.fLeft,
348 glyph.fTop,
349 glyph.fWidth,
350 glyph.fHeight));
351 SkASSERT(glyphBounds.contains(path->getBounds()));
352
353 // now generate the distance field
354 SkASSERT(dst);
355 SkMatrix drawMatrix;
356 drawMatrix.setTranslate((SkScalar)-glyph.fLeft, (SkScalar)-glyph.fTop);
357
358 // Generate signed distance field directly from SkPath
359 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dst,
360 *path, drawMatrix,
361 width, height, width * sizeof(unsigned char));
362
363 if (!succeed) {
364#endif
365 const void* image = cache->findImage(glyph);
366 if (nullptr == image) {
367 return false;
368 }
369
370 // now generate the distance field
371 SkASSERT(dst);
372 SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
373 if (SkMask::kA8_Format == maskFormat) {
374 // make the distance field from the image
375 SkGenerateDistanceFieldFromA8Image((unsigned char*)dst,
376 (unsigned char*)image,
377 glyph.fWidth, glyph.fHeight,
378 glyph.rowBytes());
379 } else if (SkMask::kBW_Format == maskFormat) {
380 // make the distance field from the image
381 SkGenerateDistanceFieldFromBWImage((unsigned char*)dst,
382 (unsigned char*)image,
383 glyph.fWidth, glyph.fHeight,
384 glyph.rowBytes());
385 } else {
386 return false;
387 }
388#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
389 }
390#endif
bsalomonf93f5152016-10-26 08:00:00 -0700391 return true;
bsalomonc2878e22016-05-17 13:18:03 -0700392}
393
394///////////////////////////////////////////////////////////////////////////////
395
joshualitt7c3a2f82015-03-31 13:32:05 -0700396/*
397 The text strike is specific to a given font/style/matrix setup, which is
398 represented by the GrHostFontScaler object we are given in getGlyph().
399
400 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
401 atlas and a position within that texture.
402 */
403
Brian Salomonf856fd12016-12-16 14:24:34 -0500404GrAtlasTextStrike::GrAtlasTextStrike(GrAtlasGlyphCache* owner, const SkDescriptor& key)
bsalomonc5fd5c42016-05-17 11:58:24 -0700405 : fFontScalerKey(key)
joshualitt7c3a2f82015-03-31 13:32:05 -0700406 , fPool(9/*start allocations at 512 bytes*/)
Brian Salomonf856fd12016-12-16 14:24:34 -0500407 , fAtlasGlyphCache(owner) // no need to ref, it won't go away before we do
joshualittae32c102015-04-21 09:37:57 -0700408 , fAtlasedGlyphs(0)
bsalomonc5fd5c42016-05-17 11:58:24 -0700409 , fIsAbandoned(false) {}
joshualitt7c3a2f82015-03-31 13:32:05 -0700410
Brian Salomonf856fd12016-12-16 14:24:34 -0500411GrAtlasTextStrike::~GrAtlasTextStrike() {
joshualitt7c3a2f82015-03-31 13:32:05 -0700412 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
413 while (!iter.done()) {
mtklein852f15d2016-03-17 10:51:27 -0700414 (*iter).reset();
joshualitt7c3a2f82015-03-31 13:32:05 -0700415 ++iter;
416 }
417}
418
Brian Salomonf856fd12016-12-16 14:24:34 -0500419GrGlyph* GrAtlasTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
bsalomonc2878e22016-05-17 13:18:03 -0700420 SkGlyphCache* cache) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700421 SkIRect bounds;
422 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
bsalomonc2878e22016-05-17 13:18:03 -0700423 if (!get_packed_glyph_df_bounds(cache, skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700424 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700425 }
426 } else {
bsalomonc2878e22016-05-17 13:18:03 -0700427 if (!get_packed_glyph_bounds(cache, skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700428 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700429 }
430 }
bsalomonc2878e22016-05-17 13:18:03 -0700431 GrMaskFormat format = get_packed_glyph_mask_format(skGlyph);
joshualitt6c2c2b02015-07-24 10:37:00 -0700432
Herb Derby9428a372017-04-10 11:25:30 -0400433 GrGlyph* glyph = fPool.make<GrGlyph>();
joshualitt7c3a2f82015-03-31 13:32:05 -0700434 glyph->init(packed, bounds, format);
435 fCache.add(glyph);
436 return glyph;
437}
438
Brian Salomon2ee084e2016-12-16 18:59:19 -0500439void GrAtlasTextStrike::removeID(GrDrawOpAtlas::AtlasID id) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700440 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
441 while (!iter.done()) {
442 if (id == (*iter).fID) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500443 (*iter).fID = GrDrawOpAtlas::kInvalidAtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -0700444 fAtlasedGlyphs--;
445 SkASSERT(fAtlasedGlyphs >= 0);
446 }
447 ++iter;
448 }
449}
450
Brian Salomonf856fd12016-12-16 14:24:34 -0500451bool GrAtlasTextStrike::addGlyphToAtlas(GrDrawOp::Target* target,
joshualitt50aa15b2015-11-23 14:09:55 -0800452 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700453 SkGlyphCache* cache,
joshualitt4f19ca32015-07-30 07:59:20 -0700454 GrMaskFormat expectedMaskFormat) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700455 SkASSERT(glyph);
bsalomonc2878e22016-05-17 13:18:03 -0700456 SkASSERT(cache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700457 SkASSERT(fCache.find(glyph->fPackedID));
joshualitt7c3a2f82015-03-31 13:32:05 -0700458
joshualitt4f19ca32015-07-30 07:59:20 -0700459 int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -0700460
461 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700462 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700463
bsalomonc2878e22016-05-17 13:18:03 -0700464 const SkGlyph& skGlyph = GrToSkGlyph(cache, glyph->fPackedID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700465 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
bsalomonc2878e22016-05-17 13:18:03 -0700466 if (!get_packed_glyph_df_image(cache, skGlyph, glyph->width(), glyph->height(),
467 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700468 return false;
469 }
470 } else {
bsalomonc2878e22016-05-17 13:18:03 -0700471 if (!get_packed_glyph_image(cache, skGlyph, glyph->width(), glyph->height(),
472 glyph->width() * bytesPerPixel, expectedMaskFormat,
473 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700474 return false;
475 }
476 }
477
Brian Salomonf856fd12016-12-16 14:24:34 -0500478 bool success = fAtlasGlyphCache->addToAtlas(this, &glyph->fID, target, expectedMaskFormat,
joshualitt7c3a2f82015-03-31 13:32:05 -0700479 glyph->width(), glyph->height(),
480 storage.get(), &glyph->fAtlasLocation);
481 if (success) {
Brian Salomon2ee084e2016-12-16 18:59:19 -0500482 SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != glyph->fID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700483 fAtlasedGlyphs++;
484 }
485 return success;
486}