blob: 084de620c320409fe185599412d2ed60934760f9 [file] [log] [blame]
joshualitt374b2f72015-07-21 08:05:03 -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
8#ifndef GrAtlasTextBlob_DEFINED
9#define GrAtlasTextBlob_DEFINED
10
11#include "GrBatchAtlas.h"
12#include "GrBatchFontCache.h"
joshualitt259fbf12015-07-21 11:39:34 -070013#include "GrColor.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "GrMemoryPool.h"
joshualitt374b2f72015-07-21 08:05:03 -070015#include "SkDescriptor.h"
16#include "SkMaskFilter.h"
joshualitt259fbf12015-07-21 11:39:34 -070017#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070018#include "SkTInternalLList.h"
19
joshualittddd22d82016-02-16 06:47:52 -080020class GrBlobRegenHelper;
joshualitt2e2202e2015-12-10 11:22:08 -080021struct GrDistanceFieldAdjustTable;
joshualitt92303772016-02-10 11:55:52 -080022class GrMemoryPool;
joshualitt2e2202e2015-12-10 11:22:08 -080023class SkDrawFilter;
24class SkTextBlob;
25class SkTextBlobRunIterator;
26
joshualitt259fbf12015-07-21 11:39:34 -070027// With this flag enabled, the GrAtlasTextContext will, as a sanity check, regenerate every blob
28// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080029#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070030
joshualitt374b2f72015-07-21 08:05:03 -070031/*
32 * A GrAtlasTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
33 * on the GPU. These are initially created with valid positions and colors, but invalid
34 * texture coordinates. The GrAtlasTextBlob itself has a few Blob-wide properties, and also
35 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
36 * reordered.
37 *
38 * The only thing(aside from a memcopy) required to flush a GrAtlasTextBlob is to ensure that
39 * the GrAtlas will not evict anything the Blob needs.
40 *
41 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070042 *
43 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070044 */
joshualitt2e2202e2015-12-10 11:22:08 -080045class GrAtlasTextBlob : public SkNVRefCnt<GrAtlasTextBlob> {
46public:
joshualitt374b2f72015-07-21 08:05:03 -070047 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrAtlasTextBlob);
48
joshualitt92303772016-02-10 11:55:52 -080049 static GrAtlasTextBlob* Create(GrMemoryPool* pool, int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080050
51 struct Key {
52 Key() {
53 sk_bzero(this, sizeof(Key));
54 }
55 uint32_t fUniqueID;
56 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
57 // Each color is assigned to on of a fixed number of buckets based on its
58 // luminance. For each luminance bucket there is a "canonical color" that
59 // represents the bucket. This functionality is currently only supported for A8
60 SkColor fCanonicalColor;
61 SkPaint::Style fStyle;
62 SkPixelGeometry fPixelGeometry;
63 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070064 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080065
66 bool operator==(const Key& other) const {
67 return 0 == memcmp(this, &other, sizeof(Key));
68 }
69 };
70
joshualitt92303772016-02-10 11:55:52 -080071 void setupKey(const GrAtlasTextBlob::Key& key,
72 const SkMaskFilter::BlurRec& blurRec,
73 const SkPaint& paint) {
74 fKey = key;
75 if (key.fHasBlur) {
76 fBlurRec = blurRec;
77 }
78 if (key.fStyle != SkPaint::kFill_Style) {
79 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
80 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
81 fStrokeInfo.fJoin = paint.getStrokeJoin();
82 }
83 }
84
joshualitt323c2eb2016-01-20 06:48:47 -080085 static const Key& GetKey(const GrAtlasTextBlob& blob) {
86 return blob.fKey;
87 }
88
89 static uint32_t Hash(const Key& key) {
90 return SkChecksum::Murmur3(&key, sizeof(Key));
91 }
92
93 void operator delete(void* p) {
94 GrAtlasTextBlob* blob = reinterpret_cast<GrAtlasTextBlob*>(p);
95 blob->fPool->release(p);
96 }
97 void* operator new(size_t) {
98 SkFAIL("All blobs are created by placement new.");
99 return sk_malloc_throw(0);
100 }
101
102 void* operator new(size_t, void* p) { return p; }
103 void operator delete(void* target, void* placement) {
104 ::operator delete(target, placement);
105 }
106
107 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
108 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
109 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
110 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
111
joshualittddd22d82016-02-16 06:47:52 -0800112 int runCount() const { return fRunCount; }
113
joshualitt323c2eb2016-01-20 06:48:47 -0800114 void push_back_run(int currRun) {
115 SkASSERT(currRun < fRunCount);
116 if (currRun > 0) {
117 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
118 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
119 newRun.setAsSuccessor(lastRun);
120 }
121 }
122
123 // sets the last subrun of runIndex to use distance field text
124 void setSubRunHasDistanceFields(int runIndex, bool hasLCD) {
125 Run& run = fRuns[runIndex];
126 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
127 subRun.setUseLCDText(hasLCD);
128 subRun.setDrawAsDistanceFields();
129 }
130
131 void setRunDrawAsPaths(int runIndex) {
132 fRuns[runIndex].fDrawAsPaths = true;
133 }
134
135 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
136 // we init fMaxMinScale and fMinMaxScale in the constructor
137 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
138 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
139 }
140
141 // inits the override descriptor on the current run. All following subruns must use this
142 // descriptor
143 void initOverride(int runIndex) {
144 Run& run = fRuns[runIndex];
145 // Push back a new subrun to fill and set the override descriptor
146 run.push_back();
147 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
148 }
149
150 SkGlyphCache* setupCache(int runIndex,
151 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -0700152 uint32_t scalerContextFlags,
joshualitt323c2eb2016-01-20 06:48:47 -0800153 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -0800154 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800155
156 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
157 // as a path.
158 void appendGlyph(int runIndex,
159 const SkRect& positions,
160 GrColor color,
161 GrBatchTextStrike* strike,
162 GrGlyph* glyph,
163 GrFontScaler* scaler, const SkGlyph& skGlyph,
164 SkScalar x, SkScalar y, SkScalar scale, bool applyVM);
165
166 static size_t GetVertexStride(GrMaskFormat maskFormat) {
167 switch (maskFormat) {
168 case kA8_GrMaskFormat:
169 return kGrayTextVASize;
170 case kARGB_GrMaskFormat:
171 return kColorTextVASize;
172 default:
173 return kLCDTextVASize;
174 }
175 }
176
joshualitt8e0ef292016-02-19 14:13:03 -0800177 bool mustRegenerate(const SkPaint& paint, GrColor color, const SkMaskFilter::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800178 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
179
180 // flush a GrAtlasTextBlob associated with a SkTextBlob
181 void flushCached(GrContext* context,
182 GrDrawContext* dc,
183 const SkTextBlob* blob,
184 const SkSurfaceProps& props,
185 const GrDistanceFieldAdjustTable* distanceAdjustTable,
186 const SkPaint& skPaint,
187 const GrPaint& grPaint,
188 SkDrawFilter* drawFilter,
189 const GrClip& clip,
190 const SkMatrix& viewMatrix,
191 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800192 SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800193
194 // flush a throwaway GrAtlasTextBlob *not* associated with an SkTextBlob
195 void flushThrowaway(GrContext* context,
196 GrDrawContext* dc,
197 const SkSurfaceProps& props,
198 const GrDistanceFieldAdjustTable* distanceAdjustTable,
199 const SkPaint& skPaint,
200 const GrPaint& grPaint,
201 const GrClip& clip,
joshualitt8e0ef292016-02-19 14:13:03 -0800202 const SkMatrix& viewMatrix,
203 const SkIRect& clipBounds,
204 SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800205
joshualitt8e0ef292016-02-19 14:13:03 -0800206 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
207 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittbc811112016-02-11 12:42:02 -0800208 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
209 // into device space.
210 // We handle vertex bounds differently for distance field text and bitmap text because
211 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
212 // from one blob then we are going to pay the price here of mapping the rect for each run.
213 const Run& run = fRuns[runIndex];
214 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
215 *outBounds = subRun.vertexBounds();
216 if (subRun.drawAsDistanceFields()) {
217 // Distance field text is positioned with the (X,Y) as part of the glyph position,
218 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800219 outBounds->offset(x - fInitialX, y - fInitialY);
220 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800221 } else {
222 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
223 // device space.
224 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
225
226 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
227
joshualitt8e0ef292016-02-19 14:13:03 -0800228 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800229
joshualitt8e0ef292016-02-19 14:13:03 -0800230 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800231 boundsMatrix.mapRect(outBounds);
232
233 // Due to floating point numerical inaccuracies, we have to round out here
234 outBounds->roundOut(outBounds);
235 }
236 }
237
joshualitt323c2eb2016-01-20 06:48:47 -0800238 // position + local coord
239 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
240 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
241 static const size_t kLCDTextVASize = kGrayTextVASize;
joshualitt92303772016-02-10 11:55:52 -0800242 static const size_t kMaxVASize = kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800243 static const int kVerticesPerGlyph = 4;
244
joshualitt323c2eb2016-01-20 06:48:47 -0800245 static void AssertEqual(const GrAtlasTextBlob&, const GrAtlasTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800246
247 // The color here is the GrPaint color, and it is used to determine whether we
248 // have to regenerate LCD text blobs.
249 // We use this color vs the SkPaint color because it has the colorfilter applied.
250 void initReusableBlob(GrColor color, const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
251 fPaintColor = color;
joshualitt7481e752016-01-22 06:08:48 -0800252 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800253 }
254
joshualitt7481e752016-01-22 06:08:48 -0800255 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
256 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800257 }
258
joshualittddd22d82016-02-16 06:47:52 -0800259 void regenInBatch(GrDrawBatch::Target* target, GrBatchFontCache* fontCache,
260 GrBlobRegenHelper *helper, int run, int subRun, SkGlyphCache** cache,
261 SkTypeface** typeface, GrFontScaler** scaler,
262 const SkDescriptor** desc, size_t vertexStride,
joshualitt8e0ef292016-02-19 14:13:03 -0800263 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
264 GrColor color,
joshualittddd22d82016-02-16 06:47:52 -0800265 void** vertices, size_t* byteCount, int* glyphCount);
266
joshualitt92303772016-02-10 11:55:52 -0800267 const Key& key() const { return fKey; }
268
269 ~GrAtlasTextBlob() {
270 for (int i = 0; i < fRunCount; i++) {
271 fRuns[i].~Run();
272 }
273 }
274
joshualittbc811112016-02-11 12:42:02 -0800275 ////////////////////////////////////////////////////////////////////////////////////////////////
276 // Internal test methods
277 GrDrawBatch* test_createBatch(int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800278 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
joshualittbc811112016-02-11 12:42:02 -0800279 const SkPaint& skPaint, const SkSurfaceProps& props,
280 const GrDistanceFieldAdjustTable* distanceAdjustTable,
281 GrBatchFontCache* cache);
282
joshualitt323c2eb2016-01-20 06:48:47 -0800283private:
joshualitt92303772016-02-10 11:55:52 -0800284 GrAtlasTextBlob()
285 : fMaxMinScale(-SK_ScalarMax)
286 , fMinMaxScale(SK_ScalarMax)
287 , fTextType(0) {}
288
joshualitt323c2eb2016-01-20 06:48:47 -0800289 void appendLargeGlyph(GrGlyph* glyph, GrFontScaler* scaler, const SkGlyph& skGlyph,
290 SkScalar x, SkScalar y, SkScalar scale, bool applyVM);
291
292 inline void flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt8e0ef292016-02-19 14:13:03 -0800293 int run, const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800294 const SkPaint& skPaint, const SkSurfaceProps& props,
295 const GrDistanceFieldAdjustTable* distanceAdjustTable,
296 GrBatchFontCache* cache);
297
298 void flushBigGlyphs(GrContext* context, GrDrawContext* dc,
299 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800300 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt323c2eb2016-01-20 06:48:47 -0800301 const SkIRect& clipBounds);
302
303 void flushRunAsPaths(GrContext* context,
304 GrDrawContext* dc,
305 const SkSurfaceProps& props,
306 const SkTextBlobRunIterator& it,
307 const GrClip& clip, const SkPaint& skPaint,
308 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
309 const SkIRect& clipBounds, SkScalar x, SkScalar y);
310
joshualitt8e0ef292016-02-19 14:13:03 -0800311 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800312 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
313 // these numbers. When blobs are reused with new matrices, we need to return to model space so
314 // we can update the vertex bounds appropriately.
315 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800316 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800317 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
318 fInitialViewMatrixInverse = SkMatrix::I();
319 SkDebugf("Could not invert viewmatrix\n");
320 }
joshualitt8e0ef292016-02-19 14:13:03 -0800321 fInitialX = x;
322 fInitialY = y;
323
324 // make sure all initial subruns have the correct VM and X/Y applied
325 for (int i = 0; i < fRunCount; i++) {
326 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
327 }
joshualitt7481e752016-01-22 06:08:48 -0800328 }
329
joshualitt374b2f72015-07-21 08:05:03 -0700330 /*
331 * Each Run inside of the blob can have its texture coordinates regenerated if required.
332 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
333 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
334 * this at a more fine grained level, but its not clear if this is worth it, as evictions
335 * should be fairly rare.
336 *
337 * One additional point, each run can contain glyphs with any of the three mask formats.
338 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
339 * a new subrun each time the mask format changes in a run. In theory, a run can have as
340 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
341 * practice, the vast majority of runs have only a single subrun.
342 *
343 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
344 * handle, we have a bit to mark the run as flusahable via rendering as paths. It is worth
345 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
346 * can flush in this manner, so we always allocate vertices for the run, regardless of
347 * whether or not it is too large. The benefit of this strategy is that we can always reuse
348 * a blob allocation regardless of viewmatrix changes. We could store positions for these
349 * glyphs. However, its not clear if this is a win because we'd still have to either go the
350 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
351 * would greatly increase the memory of these cached items.
352 */
353 struct Run {
354 Run()
joshualittf9e658b2015-12-09 09:26:44 -0800355 : fInitialized(false)
joshualitt374b2f72015-07-21 08:05:03 -0700356 , fDrawAsPaths(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700357 // To ensure we always have one subrun, we push back a fresh run here
358 fSubRunInfo.push_back();
359 }
360 struct SubRunInfo {
361 SubRunInfo()
362 : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGeneration)
363 , fVertexStartIndex(0)
364 , fVertexEndIndex(0)
365 , fGlyphStartIndex(0)
366 , fGlyphEndIndex(0)
joshualittf9e658b2015-12-09 09:26:44 -0800367 , fColor(GrColor_ILLEGAL)
jvanverthce79a3a2015-09-10 06:31:38 -0700368 , fMaskFormat(kA8_GrMaskFormat)
369 , fDrawAsDistanceFields(false)
joshualitt7481e752016-01-22 06:08:48 -0800370 , fUseLCDText(false) {
371 fVertexBounds.setLargestInverted();
372 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700373 SubRunInfo(const SubRunInfo& that)
374 : fBulkUseToken(that.fBulkUseToken)
375 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800376 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800377 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700378 , fAtlasGeneration(that.fAtlasGeneration)
379 , fVertexStartIndex(that.fVertexStartIndex)
380 , fVertexEndIndex(that.fVertexEndIndex)
381 , fGlyphStartIndex(that.fGlyphStartIndex)
382 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800383 , fX(that.fX)
384 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800385 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700386 , fMaskFormat(that.fMaskFormat)
387 , fDrawAsDistanceFields(that.fDrawAsDistanceFields)
388 , fUseLCDText(that.fUseLCDText) {
389 }
joshualitt3660d532015-12-07 11:32:50 -0800390
joshualitt18b072d2015-12-07 12:26:12 -0800391 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800392 void resetBulkUseToken() { fBulkUseToken.reset(); }
393 GrBatchAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
394 void setStrike(GrBatchTextStrike* strike) { fStrike.reset(SkRef(strike)); }
395 GrBatchTextStrike* strike() const { return fStrike.get(); }
396
397 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
398 uint64_t atlasGeneration() const { return fAtlasGeneration; }
399
400 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800401 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800402 size_t vertexEndIndex() const { return fVertexEndIndex; }
403 void appendVertices(size_t vertexStride) {
404 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
405 }
406
407 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800408 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800409 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
410 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800411 void setColor(GrColor color) { fColor = color; }
412 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800413 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
414 GrMaskFormat maskFormat() const { return fMaskFormat; }
415
joshualitt18b072d2015-12-07 12:26:12 -0800416 void setAsSuccessor(const SubRunInfo& prev) {
417 fGlyphStartIndex = prev.glyphEndIndex();
418 fGlyphEndIndex = prev.glyphEndIndex();
419
420 fVertexStartIndex = prev.vertexEndIndex();
421 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800422
423 // copy over viewmatrix settings
424 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800425 }
426
joshualitt7481e752016-01-22 06:08:48 -0800427 const SkRect& vertexBounds() const { return fVertexBounds; }
428 void joinGlyphBounds(const SkRect& glyphBounds) {
429 fVertexBounds.joinNonEmptyArg(glyphBounds);
430 }
431
joshualitt8e0ef292016-02-19 14:13:03 -0800432 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
433 fCurrentViewMatrix = viewMatrix;
434 fX = x;
435 fY = y;
436 }
437
438 // This function assumes the translation will be applied before it is called again
439 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
440 SkScalar*transX, SkScalar* transY);
441
joshualitt3660d532015-12-07 11:32:50 -0800442 // df properties
443 void setUseLCDText(bool useLCDText) { fUseLCDText = useLCDText; }
444 bool hasUseLCDText() const { return fUseLCDText; }
445 void setDrawAsDistanceFields() { fDrawAsDistanceFields = true; }
446 bool drawAsDistanceFields() const { return fDrawAsDistanceFields; }
447
448 private:
joshualitt374b2f72015-07-21 08:05:03 -0700449 GrBatchAtlas::BulkUseTokenUpdater fBulkUseToken;
joshualitt7e97b0b2015-07-31 15:18:08 -0700450 SkAutoTUnref<GrBatchTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800451 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800452 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700453 uint64_t fAtlasGeneration;
454 size_t fVertexStartIndex;
455 size_t fVertexEndIndex;
456 uint32_t fGlyphStartIndex;
457 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800458 SkScalar fX;
459 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800460 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700461 GrMaskFormat fMaskFormat;
462 bool fDrawAsDistanceFields; // df property
463 bool fUseLCDText; // df property
464 };
465
466 SubRunInfo& push_back() {
467 // Forward glyph / vertex information to seed the new sub run
joshualitt374b2f72015-07-21 08:05:03 -0700468 SubRunInfo& newSubRun = fSubRunInfo.push_back();
joshualitt18b072d2015-12-07 12:26:12 -0800469 const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700470
joshualitt18b072d2015-12-07 12:26:12 -0800471 newSubRun.setAsSuccessor(prevSubRun);
joshualitt374b2f72015-07-21 08:05:03 -0700472 return newSubRun;
473 }
474 static const int kMinSubRuns = 1;
joshualitt374b2f72015-07-21 08:05:03 -0700475 SkAutoTUnref<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700476 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
477 SkAutoDescriptor fDescriptor;
reeda9322c22016-04-12 06:47:05 -0700478 SkScalerContextEffects fEffects;
joshualitt3660d532015-12-07 11:32:50 -0800479
480 // Distance field text cannot draw coloremoji, and so has to fall back. However,
481 // though the distance field text and the coloremoji may share the same run, they
482 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
483 // will be used in place of the run's descriptor to regen texture coords
joshualitt374b2f72015-07-21 08:05:03 -0700484 SkAutoTDelete<SkAutoDescriptor> fOverrideDescriptor; // df properties
joshualitt374b2f72015-07-21 08:05:03 -0700485 bool fInitialized;
486 bool fDrawAsPaths;
487 };
488
joshualittddd22d82016-02-16 06:47:52 -0800489 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
490 void regenInBatch(GrDrawBatch::Target* target,
491 GrBatchFontCache* fontCache,
492 GrBlobRegenHelper* helper,
493 Run* run, Run::SubRunInfo* info, SkGlyphCache** cache,
494 SkTypeface** typeface, GrFontScaler** scaler,
495 const SkDescriptor** desc,
496 int glyphCount, size_t vertexStride,
497 GrColor color, SkScalar transX,
498 SkScalar transY) const;
499
joshualitt323c2eb2016-01-20 06:48:47 -0800500 inline GrDrawBatch* createBatch(const Run::SubRunInfo& info,
501 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800502 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
503 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800504 const SkPaint& skPaint, const SkSurfaceProps& props,
505 const GrDistanceFieldAdjustTable* distanceAdjustTable,
brianosmanb461d342016-04-13 13:10:14 -0700506 bool useGammaCorrectDistanceTable,
joshualitt323c2eb2016-01-20 06:48:47 -0800507 GrBatchFontCache* cache);
508
joshualitt374b2f72015-07-21 08:05:03 -0700509 struct BigGlyph {
joshualitt0fe04a22015-08-25 12:05:50 -0700510 BigGlyph(const SkPath& path, SkScalar vx, SkScalar vy, SkScalar scale, bool applyVM)
joshualitt374b2f72015-07-21 08:05:03 -0700511 : fPath(path)
joshualitt0fe04a22015-08-25 12:05:50 -0700512 , fScale(scale)
joshualitt8e0ef292016-02-19 14:13:03 -0800513 , fX(vx)
514 , fY(vy)
joshualitt0fe04a22015-08-25 12:05:50 -0700515 , fApplyVM(applyVM) {}
joshualitt374b2f72015-07-21 08:05:03 -0700516 SkPath fPath;
joshualitt0fe04a22015-08-25 12:05:50 -0700517 SkScalar fScale;
joshualitt8e0ef292016-02-19 14:13:03 -0800518 SkScalar fX;
519 SkScalar fY;
joshualitt0fe04a22015-08-25 12:05:50 -0700520 bool fApplyVM;
joshualitt374b2f72015-07-21 08:05:03 -0700521 };
522
joshualitt374b2f72015-07-21 08:05:03 -0700523 struct StrokeInfo {
524 SkScalar fFrameWidth;
525 SkScalar fMiterLimit;
526 SkPaint::Join fJoin;
527 };
528
529 enum TextType {
530 kHasDistanceField_TextType = 0x1,
531 kHasBitmap_TextType = 0x2,
532 };
533
534 // all glyph / vertex offsets are into these pools.
535 unsigned char* fVertices;
536 GrGlyph** fGlyphs;
537 Run* fRuns;
538 GrMemoryPool* fPool;
539 SkMaskFilter::BlurRec fBlurRec;
540 StrokeInfo fStrokeInfo;
541 SkTArray<BigGlyph> fBigGlyphs;
542 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800543 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800544 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800545 size_t fSize;
jvanverth0628a522015-08-18 07:44:22 -0700546 GrColor fPaintColor;
joshualitt7481e752016-01-22 06:08:48 -0800547 SkScalar fInitialX;
548 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700549
550 // We can reuse distance field text, but only if the new viewmatrix would not result in
551 // a mip change. Because there can be multiple runs in a blob, we track the overall
552 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
553 SkScalar fMaxMinScale;
554 SkScalar fMinMaxScale;
555 int fRunCount;
556 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700557};
558
559#endif