blob: 12fe68b9484153ca1499d09678b09c9e4861608a [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
Brian Salomonf856fd12016-12-16 14:24:34 -050011#include "GrAtlasGlyphCache.h"
joshualitt259fbf12015-07-21 11:39:34 -070012#include "GrColor.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050013#include "GrDrawOpAtlas.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "GrMemoryPool.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050015#include "GrTextUtils.h"
joshualitt374b2f72015-07-21 08:05:03 -070016#include "SkDescriptor.h"
17#include "SkMaskFilter.h"
mtklein4e976072016-08-08 09:06:27 -070018#include "SkOpts.h"
bsalomon8b6fa5e2016-05-19 16:23:47 -070019#include "SkPathEffect.h"
20#include "SkRasterizer.h"
joshualitt259fbf12015-07-21 11:39:34 -070021#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070022#include "SkTInternalLList.h"
23
joshualittddd22d82016-02-16 06:47:52 -080024class GrBlobRegenHelper;
joshualitt2e2202e2015-12-10 11:22:08 -080025struct GrDistanceFieldAdjustTable;
joshualitt92303772016-02-10 11:55:52 -080026class GrMemoryPool;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040027class GrLegacyMeshDrawOp;
joshualitt2e2202e2015-12-10 11:22:08 -080028class SkDrawFilter;
29class SkTextBlob;
30class SkTextBlobRunIterator;
31
joshualitt259fbf12015-07-21 11:39:34 -070032// With this flag enabled, the GrAtlasTextContext will, as a sanity check, regenerate every blob
33// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080034#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070035
joshualitt374b2f72015-07-21 08:05:03 -070036/*
37 * A GrAtlasTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
38 * on the GPU. These are initially created with valid positions and colors, but invalid
39 * texture coordinates. The GrAtlasTextBlob itself has a few Blob-wide properties, and also
40 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
41 * reordered.
42 *
43 * The only thing(aside from a memcopy) required to flush a GrAtlasTextBlob is to ensure that
44 * the GrAtlas will not evict anything the Blob needs.
45 *
46 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070047 *
48 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070049 */
joshualitt2e2202e2015-12-10 11:22:08 -080050class GrAtlasTextBlob : public SkNVRefCnt<GrAtlasTextBlob> {
51public:
joshualitt374b2f72015-07-21 08:05:03 -070052 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrAtlasTextBlob);
53
Florin Malitac337c9e2017-03-10 18:02:29 +000054 static sk_sp<GrAtlasTextBlob> Make(GrMemoryPool* pool, int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080055
56 struct Key {
57 Key() {
58 sk_bzero(this, sizeof(Key));
59 }
60 uint32_t fUniqueID;
61 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
62 // Each color is assigned to on of a fixed number of buckets based on its
63 // luminance. For each luminance bucket there is a "canonical color" that
64 // represents the bucket. This functionality is currently only supported for A8
65 SkColor fCanonicalColor;
66 SkPaint::Style fStyle;
67 SkPixelGeometry fPixelGeometry;
68 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070069 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080070
71 bool operator==(const Key& other) const {
72 return 0 == memcmp(this, &other, sizeof(Key));
73 }
74 };
75
joshualitt92303772016-02-10 11:55:52 -080076 void setupKey(const GrAtlasTextBlob::Key& key,
77 const SkMaskFilter::BlurRec& blurRec,
78 const SkPaint& paint) {
79 fKey = key;
80 if (key.fHasBlur) {
81 fBlurRec = blurRec;
82 }
83 if (key.fStyle != SkPaint::kFill_Style) {
84 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
85 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
86 fStrokeInfo.fJoin = paint.getStrokeJoin();
87 }
88 }
89
joshualitt323c2eb2016-01-20 06:48:47 -080090 static const Key& GetKey(const GrAtlasTextBlob& blob) {
91 return blob.fKey;
92 }
93
94 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -070095 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -080096 }
97
98 void operator delete(void* p) {
99 GrAtlasTextBlob* blob = reinterpret_cast<GrAtlasTextBlob*>(p);
100 blob->fPool->release(p);
101 }
102 void* operator new(size_t) {
103 SkFAIL("All blobs are created by placement new.");
104 return sk_malloc_throw(0);
105 }
106
107 void* operator new(size_t, void* p) { return p; }
108 void operator delete(void* target, void* placement) {
109 ::operator delete(target, placement);
110 }
111
112 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
113 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
114 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
115 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
116
joshualittddd22d82016-02-16 06:47:52 -0800117 int runCount() const { return fRunCount; }
118
joshualitt323c2eb2016-01-20 06:48:47 -0800119 void push_back_run(int currRun) {
120 SkASSERT(currRun < fRunCount);
121 if (currRun > 0) {
122 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
123 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
124 newRun.setAsSuccessor(lastRun);
125 }
126 }
127
128 // sets the last subrun of runIndex to use distance field text
Jim Van Verth90e89b32017-07-06 16:36:55 -0400129 void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias) {
joshualitt323c2eb2016-01-20 06:48:47 -0800130 Run& run = fRuns[runIndex];
131 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
132 subRun.setUseLCDText(hasLCD);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400133 subRun.setAntiAliased(isAntiAlias);
joshualitt323c2eb2016-01-20 06:48:47 -0800134 subRun.setDrawAsDistanceFields();
135 }
136
137 void setRunDrawAsPaths(int runIndex) {
138 fRuns[runIndex].fDrawAsPaths = true;
139 }
140
141 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
142 // we init fMaxMinScale and fMinMaxScale in the constructor
143 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
144 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
145 }
146
147 // inits the override descriptor on the current run. All following subruns must use this
148 // descriptor
149 void initOverride(int runIndex) {
150 Run& run = fRuns[runIndex];
151 // Push back a new subrun to fill and set the override descriptor
152 run.push_back();
153 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
154 }
155
156 SkGlyphCache* setupCache(int runIndex,
157 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -0700158 uint32_t scalerContextFlags,
joshualitt323c2eb2016-01-20 06:48:47 -0800159 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -0800160 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800161
162 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
163 // as a path.
164 void appendGlyph(int runIndex,
165 const SkRect& positions,
166 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -0500167 GrAtlasTextStrike* strike,
joshualitt323c2eb2016-01-20 06:48:47 -0800168 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700169 SkGlyphCache*, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500170 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP);
joshualitt323c2eb2016-01-20 06:48:47 -0800171
172 static size_t GetVertexStride(GrMaskFormat maskFormat) {
173 switch (maskFormat) {
174 case kA8_GrMaskFormat:
175 return kGrayTextVASize;
176 case kARGB_GrMaskFormat:
177 return kColorTextVASize;
178 default:
179 return kLCDTextVASize;
180 }
181 }
182
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500183 bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilter::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800184 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
185
186 // flush a GrAtlasTextBlob associated with a SkTextBlob
Brian Salomon82f44312017-01-11 13:42:54 -0500187 void flushCached(GrContext* context, GrRenderTargetContext* rtc, const SkTextBlob* blob,
joshualitt323c2eb2016-01-20 06:48:47 -0800188 const SkSurfaceProps& props,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500189 const GrDistanceFieldAdjustTable* distanceAdjustTable,
190 const GrTextUtils::Paint&, SkDrawFilter* drawFilter, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500191 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800192
193 // flush a throwaway GrAtlasTextBlob *not* associated with an SkTextBlob
Brian Salomon82f44312017-01-11 13:42:54 -0500194 void flushThrowaway(GrContext* context, GrRenderTargetContext* rtc, const SkSurfaceProps& props,
joshualitt323c2eb2016-01-20 06:48:47 -0800195 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500196 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500197 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
198 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800199
joshualitt8e0ef292016-02-19 14:13:03 -0800200 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
201 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittbc811112016-02-11 12:42:02 -0800202 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
203 // into device space.
204 // We handle vertex bounds differently for distance field text and bitmap text because
205 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
206 // from one blob then we are going to pay the price here of mapping the rect for each run.
207 const Run& run = fRuns[runIndex];
208 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
209 *outBounds = subRun.vertexBounds();
210 if (subRun.drawAsDistanceFields()) {
211 // Distance field text is positioned with the (X,Y) as part of the glyph position,
212 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800213 outBounds->offset(x - fInitialX, y - fInitialY);
214 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800215 } else {
216 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
217 // device space.
218 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
219
220 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
221
joshualitt8e0ef292016-02-19 14:13:03 -0800222 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800223
joshualitt8e0ef292016-02-19 14:13:03 -0800224 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800225 boundsMatrix.mapRect(outBounds);
226
227 // Due to floating point numerical inaccuracies, we have to round out here
228 outBounds->roundOut(outBounds);
229 }
230 }
231
joshualitt323c2eb2016-01-20 06:48:47 -0800232 // position + local coord
233 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
234 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
235 static const size_t kLCDTextVASize = kGrayTextVASize;
joshualitt92303772016-02-10 11:55:52 -0800236 static const size_t kMaxVASize = kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800237 static const int kVerticesPerGlyph = 4;
238
joshualitt323c2eb2016-01-20 06:48:47 -0800239 static void AssertEqual(const GrAtlasTextBlob&, const GrAtlasTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800240
241 // The color here is the GrPaint color, and it is used to determine whether we
242 // have to regenerate LCD text blobs.
243 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400244 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix, SkScalar x,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500245 SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400246 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800247 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800248 }
249
joshualitt7481e752016-01-22 06:08:48 -0800250 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
251 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800252 }
253
bsalomond1c71fd2016-05-19 12:51:46 -0700254 /**
Brian Salomon09d994e2016-12-21 11:14:46 -0500255 * Consecutive calls to regenInOp often use the same SkGlyphCache. If the same instance of
256 * SkAutoGlyphCache is passed to multiple calls of regenInOp then it can save the cost of
bsalomond1c71fd2016-05-19 12:51:46 -0700257 * multiple detach/attach operations of SkGlyphCache.
258 */
Brian Salomon09d994e2016-12-21 11:14:46 -0500259 void regenInOp(GrDrawOp::Target* target, GrAtlasGlyphCache* fontCache,
260 GrBlobRegenHelper* helper, int run, int subRun, SkAutoGlyphCache*,
261 size_t vertexStride, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
262 GrColor color, void** vertices, size_t* byteCount, int* glyphCount);
joshualittddd22d82016-02-16 06:47:52 -0800263
joshualitt92303772016-02-10 11:55:52 -0800264 const Key& key() const { return fKey; }
265
266 ~GrAtlasTextBlob() {
267 for (int i = 0; i < fRunCount; i++) {
268 fRuns[i].~Run();
269 }
270 }
271
joshualittbc811112016-02-11 12:42:02 -0800272 ////////////////////////////////////////////////////////////////////////////////////////////////
273 // Internal test methods
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400274 std::unique_ptr<GrLegacyMeshDrawOp> test_makeOp(
275 int glyphCount, int run, int subRun, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
276 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
277 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache);
joshualittbc811112016-02-11 12:42:02 -0800278
joshualitt323c2eb2016-01-20 06:48:47 -0800279private:
joshualitt92303772016-02-10 11:55:52 -0800280 GrAtlasTextBlob()
281 : fMaxMinScale(-SK_ScalarMax)
282 , fMinMaxScale(SK_ScalarMax)
283 , fTextType(0) {}
284
bsalomonc2878e22016-05-17 13:18:03 -0700285 void appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500286 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP);
joshualitt323c2eb2016-01-20 06:48:47 -0800287
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500288 inline void flushRun(GrRenderTargetContext* rtc, const GrClip&, int run,
289 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
290 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
joshualitt323c2eb2016-01-20 06:48:47 -0800291 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500292 GrAtlasGlyphCache* cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800293
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500294 void flushBigGlyphs(GrContext* context, GrRenderTargetContext* rtc, const GrClip& clip,
295 const SkPaint& paint, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt323c2eb2016-01-20 06:48:47 -0800296 const SkIRect& clipBounds);
297
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500298 void flushRunAsPaths(GrContext* context, GrRenderTargetContext* rtc,
299 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
300 const GrClip& clip, const GrTextUtils::Paint& paint,
joshualitt323c2eb2016-01-20 06:48:47 -0800301 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
302 const SkIRect& clipBounds, SkScalar x, SkScalar y);
303
joshualitt8e0ef292016-02-19 14:13:03 -0800304 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800305 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
306 // these numbers. When blobs are reused with new matrices, we need to return to model space so
307 // we can update the vertex bounds appropriately.
308 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800309 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800310 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
311 fInitialViewMatrixInverse = SkMatrix::I();
312 SkDebugf("Could not invert viewmatrix\n");
313 }
joshualitt8e0ef292016-02-19 14:13:03 -0800314 fInitialX = x;
315 fInitialY = y;
316
317 // make sure all initial subruns have the correct VM and X/Y applied
318 for (int i = 0; i < fRunCount; i++) {
319 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
320 }
joshualitt7481e752016-01-22 06:08:48 -0800321 }
322
joshualitt374b2f72015-07-21 08:05:03 -0700323 /*
324 * Each Run inside of the blob can have its texture coordinates regenerated if required.
325 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
326 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
327 * this at a more fine grained level, but its not clear if this is worth it, as evictions
328 * should be fairly rare.
329 *
330 * One additional point, each run can contain glyphs with any of the three mask formats.
331 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
332 * a new subrun each time the mask format changes in a run. In theory, a run can have as
333 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
334 * practice, the vast majority of runs have only a single subrun.
335 *
336 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
337 * handle, we have a bit to mark the run as flusahable via rendering as paths. It is worth
338 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
339 * can flush in this manner, so we always allocate vertices for the run, regardless of
340 * whether or not it is too large. The benefit of this strategy is that we can always reuse
341 * a blob allocation regardless of viewmatrix changes. We could store positions for these
342 * glyphs. However, its not clear if this is a win because we'd still have to either go the
343 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
344 * would greatly increase the memory of these cached items.
345 */
346 struct Run {
347 Run()
joshualittf9e658b2015-12-09 09:26:44 -0800348 : fInitialized(false)
joshualitt374b2f72015-07-21 08:05:03 -0700349 , fDrawAsPaths(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700350 // To ensure we always have one subrun, we push back a fresh run here
351 fSubRunInfo.push_back();
352 }
353 struct SubRunInfo {
354 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500355 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
356 , fVertexStartIndex(0)
357 , fVertexEndIndex(0)
358 , fGlyphStartIndex(0)
359 , fGlyphEndIndex(0)
360 , fColor(GrColor_ILLEGAL)
361 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400362 , fFlags(0) {
joshualitt7481e752016-01-22 06:08:48 -0800363 fVertexBounds.setLargestInverted();
364 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700365 SubRunInfo(const SubRunInfo& that)
366 : fBulkUseToken(that.fBulkUseToken)
367 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800368 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800369 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700370 , fAtlasGeneration(that.fAtlasGeneration)
371 , fVertexStartIndex(that.fVertexStartIndex)
372 , fVertexEndIndex(that.fVertexEndIndex)
373 , fGlyphStartIndex(that.fGlyphStartIndex)
374 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800375 , fX(that.fX)
376 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800377 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700378 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400379 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700380 }
joshualitt3660d532015-12-07 11:32:50 -0800381
joshualitt18b072d2015-12-07 12:26:12 -0800382 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800383 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500384 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Brian Salomonf856fd12016-12-16 14:24:34 -0500385 void setStrike(GrAtlasTextStrike* strike) { fStrike.reset(SkRef(strike)); }
386 GrAtlasTextStrike* strike() const { return fStrike.get(); }
joshualitt3660d532015-12-07 11:32:50 -0800387
388 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
389 uint64_t atlasGeneration() const { return fAtlasGeneration; }
390
391 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800392 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800393 size_t vertexEndIndex() const { return fVertexEndIndex; }
394 void appendVertices(size_t vertexStride) {
395 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
396 }
397
398 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800399 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800400 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
401 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800402 void setColor(GrColor color) { fColor = color; }
403 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800404 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
405 GrMaskFormat maskFormat() const { return fMaskFormat; }
406
joshualitt18b072d2015-12-07 12:26:12 -0800407 void setAsSuccessor(const SubRunInfo& prev) {
408 fGlyphStartIndex = prev.glyphEndIndex();
409 fGlyphEndIndex = prev.glyphEndIndex();
410
411 fVertexStartIndex = prev.vertexEndIndex();
412 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800413
414 // copy over viewmatrix settings
415 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800416 }
417
joshualitt7481e752016-01-22 06:08:48 -0800418 const SkRect& vertexBounds() const { return fVertexBounds; }
419 void joinGlyphBounds(const SkRect& glyphBounds) {
420 fVertexBounds.joinNonEmptyArg(glyphBounds);
421 }
422
joshualitt8e0ef292016-02-19 14:13:03 -0800423 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
424 fCurrentViewMatrix = viewMatrix;
425 fX = x;
426 fY = y;
427 }
428
429 // This function assumes the translation will be applied before it is called again
430 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
431 SkScalar*transX, SkScalar* transY);
432
joshualitt3660d532015-12-07 11:32:50 -0800433 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400434 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
435 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
436 void setUseLCDText(bool useLCDText) {
437 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
438 }
439 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
440 void setAntiAliased(bool antiAliased) {
441 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
442 }
443 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800444
445 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400446 enum Flag {
447 kDrawAsSDF_Flag = 0x1,
448 kUseLCDText_Flag = 0x2,
449 kAntiAliased_Flag = 0x4
450 };
451
Brian Salomon2ee084e2016-12-16 18:59:19 -0500452 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Brian Salomonf856fd12016-12-16 14:24:34 -0500453 sk_sp<GrAtlasTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800454 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800455 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700456 uint64_t fAtlasGeneration;
457 size_t fVertexStartIndex;
458 size_t fVertexEndIndex;
459 uint32_t fGlyphStartIndex;
460 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800461 SkScalar fX;
462 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800463 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700464 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400465 uint32_t fFlags;
joshualitt374b2f72015-07-21 08:05:03 -0700466 };
467
468 SubRunInfo& push_back() {
469 // Forward glyph / vertex information to seed the new sub run
joshualitt374b2f72015-07-21 08:05:03 -0700470 SubRunInfo& newSubRun = fSubRunInfo.push_back();
joshualitt18b072d2015-12-07 12:26:12 -0800471 const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700472
joshualitt18b072d2015-12-07 12:26:12 -0800473 newSubRun.setAsSuccessor(prevSubRun);
joshualitt374b2f72015-07-21 08:05:03 -0700474 return newSubRun;
475 }
476 static const int kMinSubRuns = 1;
Hal Canary144caf52016-11-07 17:57:18 -0500477 sk_sp<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700478 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
479 SkAutoDescriptor fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700480
481 // Effects from the paint that are used to build a SkScalerContext.
482 sk_sp<SkPathEffect> fPathEffect;
483 sk_sp<SkRasterizer> fRasterizer;
484 sk_sp<SkMaskFilter> fMaskFilter;
joshualitt3660d532015-12-07 11:32:50 -0800485
486 // Distance field text cannot draw coloremoji, and so has to fall back. However,
487 // though the distance field text and the coloremoji may share the same run, they
488 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
489 // will be used in place of the run's descriptor to regen texture coords
Ben Wagner145dbcd2016-11-03 14:40:50 -0400490 std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
joshualitt374b2f72015-07-21 08:05:03 -0700491 bool fInitialized;
492 bool fDrawAsPaths;
493 };
494
joshualittddd22d82016-02-16 06:47:52 -0800495 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Brian Salomonf856fd12016-12-16 14:24:34 -0500496 void regenInOp(GrDrawOp::Target* target, GrAtlasGlyphCache* fontCache, GrBlobRegenHelper* helper,
Brian Salomon344ec422016-12-15 10:58:41 -0500497 Run* run, Run::SubRunInfo* info, SkAutoGlyphCache*, int glyphCount,
498 size_t vertexStride, GrColor color, SkScalar transX, SkScalar transY) const;
joshualittddd22d82016-02-16 06:47:52 -0800499
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400500 inline std::unique_ptr<GrLegacyMeshDrawOp> makeOp(
Brian Salomon649a3412017-03-09 13:50:43 -0500501 const Run::SubRunInfo& info, int glyphCount, int run, int subRun,
502 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const GrTextUtils::Paint& paint,
503 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
504 bool useGammaCorrectDistanceTable, GrAtlasGlyphCache* cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800505
joshualitt374b2f72015-07-21 08:05:03 -0700506 struct BigGlyph {
Jim Van Verth08576e62016-11-16 10:15:23 -0500507 BigGlyph(const SkPath& path, SkScalar vx, SkScalar vy, SkScalar scale, bool treatAsBMP)
joshualitt374b2f72015-07-21 08:05:03 -0700508 : fPath(path)
joshualitt0fe04a22015-08-25 12:05:50 -0700509 , fScale(scale)
joshualitt8e0ef292016-02-19 14:13:03 -0800510 , fX(vx)
511 , fY(vy)
Jim Van Verth08576e62016-11-16 10:15:23 -0500512 , fTreatAsBMP(treatAsBMP) {}
joshualitt374b2f72015-07-21 08:05:03 -0700513 SkPath fPath;
joshualitt0fe04a22015-08-25 12:05:50 -0700514 SkScalar fScale;
joshualitt8e0ef292016-02-19 14:13:03 -0800515 SkScalar fX;
516 SkScalar fY;
Jim Van Verth08576e62016-11-16 10:15:23 -0500517 bool fTreatAsBMP;
joshualitt374b2f72015-07-21 08:05:03 -0700518 };
519
joshualitt374b2f72015-07-21 08:05:03 -0700520 struct StrokeInfo {
521 SkScalar fFrameWidth;
522 SkScalar fMiterLimit;
523 SkPaint::Join fJoin;
524 };
525
526 enum TextType {
527 kHasDistanceField_TextType = 0x1,
528 kHasBitmap_TextType = 0x2,
529 };
530
531 // all glyph / vertex offsets are into these pools.
532 unsigned char* fVertices;
533 GrGlyph** fGlyphs;
534 Run* fRuns;
535 GrMemoryPool* fPool;
536 SkMaskFilter::BlurRec fBlurRec;
537 StrokeInfo fStrokeInfo;
538 SkTArray<BigGlyph> fBigGlyphs;
539 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800540 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800541 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800542 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400543 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800544 SkScalar fInitialX;
545 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700546
547 // We can reuse distance field text, but only if the new viewmatrix would not result in
548 // a mip change. Because there can be multiple runs in a blob, we track the overall
549 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
550 SkScalar fMaxMinScale;
551 SkScalar fMinMaxScale;
552 int fRunCount;
553 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700554};
555
556#endif