blob: 282fa981415fe97622b7ccf475715e17621bbf1e [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"
Brian Salomon5c6ac642017-12-19 11:09:32 -050020#include "SkPoint3.h"
bsalomon8b6fa5e2016-05-19 16:23:47 -070021#include "SkRasterizer.h"
joshualitt259fbf12015-07-21 11:39:34 -070022#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070023#include "SkTInternalLList.h"
24
joshualitt2e2202e2015-12-10 11:22:08 -080025struct GrDistanceFieldAdjustTable;
joshualitt92303772016-02-10 11:55:52 -080026class GrMemoryPool;
joshualitt2e2202e2015-12-10 11:22:08 -080027class SkDrawFilter;
28class SkTextBlob;
29class SkTextBlobRunIterator;
30
joshualitt259fbf12015-07-21 11:39:34 -070031// With this flag enabled, the GrAtlasTextContext will, as a sanity check, regenerate every blob
32// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080033#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070034
joshualitt374b2f72015-07-21 08:05:03 -070035/*
36 * A GrAtlasTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
37 * on the GPU. These are initially created with valid positions and colors, but invalid
38 * texture coordinates. The GrAtlasTextBlob itself has a few Blob-wide properties, and also
39 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
40 * reordered.
41 *
42 * The only thing(aside from a memcopy) required to flush a GrAtlasTextBlob is to ensure that
43 * the GrAtlas will not evict anything the Blob needs.
44 *
45 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070046 *
47 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070048 */
joshualitt2e2202e2015-12-10 11:22:08 -080049class GrAtlasTextBlob : public SkNVRefCnt<GrAtlasTextBlob> {
50public:
joshualitt374b2f72015-07-21 08:05:03 -070051 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrAtlasTextBlob);
52
Brian Salomon18923f92017-11-06 16:26:02 -050053 class VertexRegenerator;
54
Florin Malitac337c9e2017-03-10 18:02:29 +000055 static sk_sp<GrAtlasTextBlob> Make(GrMemoryPool* pool, int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080056
Brian Salomon5c6ac642017-12-19 11:09:32 -050057 /**
58 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
59 * If we ever change that then the key must contain the perspectiveness when there are distance
60 * fields as perspective distance field use 3 component vertex positions and non-perspective
61 * uses 2.
62 */
joshualitt323c2eb2016-01-20 06:48:47 -080063 struct Key {
64 Key() {
65 sk_bzero(this, sizeof(Key));
66 }
67 uint32_t fUniqueID;
68 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
69 // Each color is assigned to on of a fixed number of buckets based on its
70 // luminance. For each luminance bucket there is a "canonical color" that
71 // represents the bucket. This functionality is currently only supported for A8
72 SkColor fCanonicalColor;
73 SkPaint::Style fStyle;
74 SkPixelGeometry fPixelGeometry;
75 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070076 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080077
78 bool operator==(const Key& other) const {
79 return 0 == memcmp(this, &other, sizeof(Key));
80 }
81 };
82
joshualitt92303772016-02-10 11:55:52 -080083 void setupKey(const GrAtlasTextBlob::Key& key,
84 const SkMaskFilter::BlurRec& blurRec,
85 const SkPaint& paint) {
86 fKey = key;
87 if (key.fHasBlur) {
88 fBlurRec = blurRec;
89 }
90 if (key.fStyle != SkPaint::kFill_Style) {
91 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
92 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
93 fStrokeInfo.fJoin = paint.getStrokeJoin();
94 }
95 }
96
joshualitt323c2eb2016-01-20 06:48:47 -080097 static const Key& GetKey(const GrAtlasTextBlob& blob) {
98 return blob.fKey;
99 }
100
101 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700102 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800103 }
104
105 void operator delete(void* p) {
106 GrAtlasTextBlob* blob = reinterpret_cast<GrAtlasTextBlob*>(p);
107 blob->fPool->release(p);
108 }
109 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400110 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800111 return sk_malloc_throw(0);
112 }
113
114 void* operator new(size_t, void* p) { return p; }
115 void operator delete(void* target, void* placement) {
116 ::operator delete(target, placement);
117 }
118
119 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
120 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
121 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
122 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
123
joshualittddd22d82016-02-16 06:47:52 -0800124 int runCount() const { return fRunCount; }
125
joshualitt323c2eb2016-01-20 06:48:47 -0800126 void push_back_run(int currRun) {
127 SkASSERT(currRun < fRunCount);
128 if (currRun > 0) {
129 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
130 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
131 newRun.setAsSuccessor(lastRun);
132 }
133 }
134
135 // sets the last subrun of runIndex to use distance field text
Brian Salomon5c6ac642017-12-19 11:09:32 -0500136 void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800137 Run& run = fRuns[runIndex];
138 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
139 subRun.setUseLCDText(hasLCD);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400140 subRun.setAntiAliased(isAntiAlias);
joshualitt323c2eb2016-01-20 06:48:47 -0800141 subRun.setDrawAsDistanceFields();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500142 subRun.setHasWCoord(hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800143 }
144
145 void setRunDrawAsPaths(int runIndex) {
146 fRuns[runIndex].fDrawAsPaths = true;
147 }
148
149 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
150 // we init fMaxMinScale and fMinMaxScale in the constructor
151 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
152 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
153 }
154
155 // inits the override descriptor on the current run. All following subruns must use this
156 // descriptor
157 void initOverride(int runIndex) {
158 Run& run = fRuns[runIndex];
159 // Push back a new subrun to fill and set the override descriptor
160 run.push_back();
161 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
162 }
163
164 SkGlyphCache* setupCache(int runIndex,
165 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -0700166 uint32_t scalerContextFlags,
joshualitt323c2eb2016-01-20 06:48:47 -0800167 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -0800168 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800169
170 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
171 // as a path.
172 void appendGlyph(int runIndex,
173 const SkRect& positions,
174 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -0500175 GrAtlasTextStrike* strike,
joshualitt323c2eb2016-01-20 06:48:47 -0800176 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700177 SkGlyphCache*, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500178 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP);
joshualitt323c2eb2016-01-20 06:48:47 -0800179
Brian Salomon5c6ac642017-12-19 11:09:32 -0500180 static size_t GetVertexStride(GrMaskFormat maskFormat, bool isDistanceFieldWithWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800181 switch (maskFormat) {
182 case kA8_GrMaskFormat:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500183 return isDistanceFieldWithWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800184 case kARGB_GrMaskFormat:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500185 SkASSERT(!isDistanceFieldWithWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800186 return kColorTextVASize;
187 default:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500188 SkASSERT(!isDistanceFieldWithWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800189 return kLCDTextVASize;
190 }
191 }
192
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500193 bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilter::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800194 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
195
196 // flush a GrAtlasTextBlob associated with a SkTextBlob
Brian Salomonf18b1d82017-10-27 11:30:49 -0400197 void flushCached(GrContext* context, GrTextUtils::Target*, const SkTextBlob* blob,
joshualitt323c2eb2016-01-20 06:48:47 -0800198 const SkSurfaceProps& props,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500199 const GrDistanceFieldAdjustTable* distanceAdjustTable,
200 const GrTextUtils::Paint&, SkDrawFilter* drawFilter, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500201 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800202
203 // flush a throwaway GrAtlasTextBlob *not* associated with an SkTextBlob
Brian Salomonf18b1d82017-10-27 11:30:49 -0400204 void flushThrowaway(GrContext* context, GrTextUtils::Target*, const SkSurfaceProps& props,
joshualitt323c2eb2016-01-20 06:48:47 -0800205 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500206 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500207 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
208 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800209
joshualitt8e0ef292016-02-19 14:13:03 -0800210 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
211 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittbc811112016-02-11 12:42:02 -0800212 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
213 // into device space.
214 // We handle vertex bounds differently for distance field text and bitmap text because
215 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
216 // from one blob then we are going to pay the price here of mapping the rect for each run.
217 const Run& run = fRuns[runIndex];
218 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
219 *outBounds = subRun.vertexBounds();
220 if (subRun.drawAsDistanceFields()) {
221 // Distance field text is positioned with the (X,Y) as part of the glyph position,
222 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800223 outBounds->offset(x - fInitialX, y - fInitialY);
224 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800225 } else {
226 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
227 // device space.
228 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
229
230 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
231
joshualitt8e0ef292016-02-19 14:13:03 -0800232 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800233
joshualitt8e0ef292016-02-19 14:13:03 -0800234 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800235 boundsMatrix.mapRect(outBounds);
236
237 // Due to floating point numerical inaccuracies, we have to round out here
238 outBounds->roundOut(outBounds);
239 }
240 }
241
joshualitt323c2eb2016-01-20 06:48:47 -0800242 // position + local coord
243 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
244 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500245 static const size_t kGrayTextDFPerspectiveVASize =
246 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800247 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500248 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800249 static const int kVerticesPerGlyph = 4;
250
joshualitt323c2eb2016-01-20 06:48:47 -0800251 static void AssertEqual(const GrAtlasTextBlob&, const GrAtlasTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800252
253 // The color here is the GrPaint color, and it is used to determine whether we
254 // have to regenerate LCD text blobs.
255 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400256 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
257 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400258 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800259 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800260 }
261
joshualitt7481e752016-01-22 06:08:48 -0800262 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
263 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800264 }
265
joshualitt92303772016-02-10 11:55:52 -0800266 const Key& key() const { return fKey; }
267
268 ~GrAtlasTextBlob() {
269 for (int i = 0; i < fRunCount; i++) {
270 fRuns[i].~Run();
271 }
272 }
273
joshualittbc811112016-02-11 12:42:02 -0800274 ////////////////////////////////////////////////////////////////////////////////////////////////
275 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400276 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400277 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
278 const GrTextUtils::Paint&, const SkSurfaceProps&,
279 const GrDistanceFieldAdjustTable*, GrAtlasGlyphCache*,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400280 GrTextUtils::Target*);
joshualittbc811112016-02-11 12:42:02 -0800281
joshualitt323c2eb2016-01-20 06:48:47 -0800282private:
joshualitt92303772016-02-10 11:55:52 -0800283 GrAtlasTextBlob()
284 : fMaxMinScale(-SK_ScalarMax)
285 , fMinMaxScale(SK_ScalarMax)
286 , fTextType(0) {}
287
bsalomonc2878e22016-05-17 13:18:03 -0700288 void appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500289 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP);
joshualitt323c2eb2016-01-20 06:48:47 -0800290
Brian Salomonf18b1d82017-10-27 11:30:49 -0400291 inline void flushRun(GrTextUtils::Target*, const GrClip&, int run, const SkMatrix& viewMatrix,
292 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint,
293 const SkSurfaceProps& props,
joshualitt323c2eb2016-01-20 06:48:47 -0800294 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500295 GrAtlasGlyphCache* cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800296
Brian Salomonf18b1d82017-10-27 11:30:49 -0400297 void flushBigGlyphs(GrContext* context, GrTextUtils::Target*, const GrClip& clip,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500298 const SkPaint& paint, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt323c2eb2016-01-20 06:48:47 -0800299 const SkIRect& clipBounds);
300
Brian Salomonf18b1d82017-10-27 11:30:49 -0400301 void flushRunAsPaths(GrContext* context, GrTextUtils::Target*, const SkSurfaceProps& props,
302 const SkTextBlobRunIterator& it, const GrClip& clip,
303 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
304 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
305 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800306
joshualitt8e0ef292016-02-19 14:13:03 -0800307 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800308 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
309 // these numbers. When blobs are reused with new matrices, we need to return to model space so
310 // we can update the vertex bounds appropriately.
311 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800312 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800313 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
314 fInitialViewMatrixInverse = SkMatrix::I();
315 SkDebugf("Could not invert viewmatrix\n");
316 }
joshualitt8e0ef292016-02-19 14:13:03 -0800317 fInitialX = x;
318 fInitialY = y;
319
320 // make sure all initial subruns have the correct VM and X/Y applied
321 for (int i = 0; i < fRunCount; i++) {
322 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
323 }
joshualitt7481e752016-01-22 06:08:48 -0800324 }
325
joshualitt374b2f72015-07-21 08:05:03 -0700326 /*
327 * Each Run inside of the blob can have its texture coordinates regenerated if required.
328 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
329 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
330 * this at a more fine grained level, but its not clear if this is worth it, as evictions
331 * should be fairly rare.
332 *
333 * One additional point, each run can contain glyphs with any of the three mask formats.
334 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
335 * a new subrun each time the mask format changes in a run. In theory, a run can have as
336 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
337 * practice, the vast majority of runs have only a single subrun.
338 *
339 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
Brian Salomonf18b1d82017-10-27 11:30:49 -0400340 * handle, we have a bit to mark the run as flushable via rendering as paths. It is worth
joshualitt374b2f72015-07-21 08:05:03 -0700341 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
342 * can flush in this manner, so we always allocate vertices for the run, regardless of
343 * whether or not it is too large. The benefit of this strategy is that we can always reuse
344 * a blob allocation regardless of viewmatrix changes. We could store positions for these
345 * glyphs. However, its not clear if this is a win because we'd still have to either go the
346 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
347 * would greatly increase the memory of these cached items.
348 */
349 struct Run {
350 Run()
joshualittf9e658b2015-12-09 09:26:44 -0800351 : fInitialized(false)
joshualitt374b2f72015-07-21 08:05:03 -0700352 , fDrawAsPaths(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700353 // To ensure we always have one subrun, we push back a fresh run here
354 fSubRunInfo.push_back();
355 }
356 struct SubRunInfo {
357 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500358 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
359 , fVertexStartIndex(0)
360 , fVertexEndIndex(0)
361 , fGlyphStartIndex(0)
362 , fGlyphEndIndex(0)
363 , fColor(GrColor_ILLEGAL)
364 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400365 , fFlags(0) {
joshualitt7481e752016-01-22 06:08:48 -0800366 fVertexBounds.setLargestInverted();
367 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700368 SubRunInfo(const SubRunInfo& that)
369 : fBulkUseToken(that.fBulkUseToken)
370 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800371 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800372 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700373 , fAtlasGeneration(that.fAtlasGeneration)
374 , fVertexStartIndex(that.fVertexStartIndex)
375 , fVertexEndIndex(that.fVertexEndIndex)
376 , fGlyphStartIndex(that.fGlyphStartIndex)
377 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800378 , fX(that.fX)
379 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800380 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700381 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400382 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700383 }
joshualitt3660d532015-12-07 11:32:50 -0800384
joshualitt18b072d2015-12-07 12:26:12 -0800385 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800386 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500387 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Brian Salomonf856fd12016-12-16 14:24:34 -0500388 void setStrike(GrAtlasTextStrike* strike) { fStrike.reset(SkRef(strike)); }
389 GrAtlasTextStrike* strike() const { return fStrike.get(); }
joshualitt3660d532015-12-07 11:32:50 -0800390
391 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
392 uint64_t atlasGeneration() const { return fAtlasGeneration; }
393
394 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800395 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800396 size_t vertexEndIndex() const { return fVertexEndIndex; }
397 void appendVertices(size_t vertexStride) {
398 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
399 }
400
401 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800402 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800403 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
404 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800405 void setColor(GrColor color) { fColor = color; }
406 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800407 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
408 GrMaskFormat maskFormat() const { return fMaskFormat; }
409
joshualitt18b072d2015-12-07 12:26:12 -0800410 void setAsSuccessor(const SubRunInfo& prev) {
411 fGlyphStartIndex = prev.glyphEndIndex();
412 fGlyphEndIndex = prev.glyphEndIndex();
413
414 fVertexStartIndex = prev.vertexEndIndex();
415 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800416
417 // copy over viewmatrix settings
418 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800419 }
420
joshualitt7481e752016-01-22 06:08:48 -0800421 const SkRect& vertexBounds() const { return fVertexBounds; }
422 void joinGlyphBounds(const SkRect& glyphBounds) {
423 fVertexBounds.joinNonEmptyArg(glyphBounds);
424 }
425
joshualitt8e0ef292016-02-19 14:13:03 -0800426 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
427 fCurrentViewMatrix = viewMatrix;
428 fX = x;
429 fY = y;
430 }
431
432 // This function assumes the translation will be applied before it is called again
433 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500434 SkScalar* transX, SkScalar* transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800435
joshualitt3660d532015-12-07 11:32:50 -0800436 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400437 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
438 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
439 void setUseLCDText(bool useLCDText) {
440 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
441 }
442 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
443 void setAntiAliased(bool antiAliased) {
444 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
445 }
446 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500447 void setHasWCoord(bool hasW) {
448 fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag;
449 }
450 bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800451
452 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400453 enum Flag {
454 kDrawAsSDF_Flag = 0x1,
455 kUseLCDText_Flag = 0x2,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500456 kAntiAliased_Flag = 0x4,
457 kHasWCoord_Flag = 0x8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400458 };
459
Brian Salomon2ee084e2016-12-16 18:59:19 -0500460 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Brian Salomonf856fd12016-12-16 14:24:34 -0500461 sk_sp<GrAtlasTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800462 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800463 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700464 uint64_t fAtlasGeneration;
465 size_t fVertexStartIndex;
466 size_t fVertexEndIndex;
467 uint32_t fGlyphStartIndex;
468 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800469 SkScalar fX;
470 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800471 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700472 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400473 uint32_t fFlags;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400474 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700475
476 SubRunInfo& push_back() {
477 // Forward glyph / vertex information to seed the new sub run
joshualitt374b2f72015-07-21 08:05:03 -0700478 SubRunInfo& newSubRun = fSubRunInfo.push_back();
joshualitt18b072d2015-12-07 12:26:12 -0800479 const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700480
joshualitt18b072d2015-12-07 12:26:12 -0800481 newSubRun.setAsSuccessor(prevSubRun);
joshualitt374b2f72015-07-21 08:05:03 -0700482 return newSubRun;
483 }
484 static const int kMinSubRuns = 1;
Hal Canary144caf52016-11-07 17:57:18 -0500485 sk_sp<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700486 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
487 SkAutoDescriptor fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700488
489 // Effects from the paint that are used to build a SkScalerContext.
490 sk_sp<SkPathEffect> fPathEffect;
491 sk_sp<SkRasterizer> fRasterizer;
492 sk_sp<SkMaskFilter> fMaskFilter;
joshualitt3660d532015-12-07 11:32:50 -0800493
494 // Distance field text cannot draw coloremoji, and so has to fall back. However,
495 // though the distance field text and the coloremoji may share the same run, they
496 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
497 // will be used in place of the run's descriptor to regen texture coords
Ben Wagner145dbcd2016-11-03 14:40:50 -0400498 std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
joshualitt374b2f72015-07-21 08:05:03 -0700499 bool fInitialized;
500 bool fDrawAsPaths;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400501 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700502
Brian Salomonf18b1d82017-10-27 11:30:49 -0400503 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400504 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400505 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
506 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
507 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
508 GrTextUtils::Target*);
joshualitt323c2eb2016-01-20 06:48:47 -0800509
joshualitt374b2f72015-07-21 08:05:03 -0700510 struct BigGlyph {
Jim Van Verth08576e62016-11-16 10:15:23 -0500511 BigGlyph(const SkPath& path, SkScalar vx, SkScalar vy, SkScalar scale, bool treatAsBMP)
joshualitt374b2f72015-07-21 08:05:03 -0700512 : fPath(path)
joshualitt0fe04a22015-08-25 12:05:50 -0700513 , fScale(scale)
joshualitt8e0ef292016-02-19 14:13:03 -0800514 , fX(vx)
515 , fY(vy)
Jim Van Verth08576e62016-11-16 10:15:23 -0500516 , fTreatAsBMP(treatAsBMP) {}
joshualitt374b2f72015-07-21 08:05:03 -0700517 SkPath fPath;
joshualitt0fe04a22015-08-25 12:05:50 -0700518 SkScalar fScale;
joshualitt8e0ef292016-02-19 14:13:03 -0800519 SkScalar fX;
520 SkScalar fY;
Jim Van Verth08576e62016-11-16 10:15:23 -0500521 bool fTreatAsBMP;
joshualitt374b2f72015-07-21 08:05:03 -0700522 };
523
joshualitt374b2f72015-07-21 08:05:03 -0700524 struct StrokeInfo {
525 SkScalar fFrameWidth;
526 SkScalar fMiterLimit;
527 SkPaint::Join fJoin;
528 };
529
530 enum TextType {
531 kHasDistanceField_TextType = 0x1,
532 kHasBitmap_TextType = 0x2,
533 };
534
535 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500536 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700537 GrGlyph** fGlyphs;
538 Run* fRuns;
539 GrMemoryPool* fPool;
540 SkMaskFilter::BlurRec fBlurRec;
541 StrokeInfo fStrokeInfo;
542 SkTArray<BigGlyph> fBigGlyphs;
543 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800544 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800545 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800546 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400547 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800548 SkScalar fInitialX;
549 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700550
551 // We can reuse distance field text, but only if the new viewmatrix would not result in
552 // a mip change. Because there can be multiple runs in a blob, we track the overall
553 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
554 SkScalar fMaxMinScale;
555 SkScalar fMinMaxScale;
556 int fRunCount;
557 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700558};
559
Brian Salomon18923f92017-11-06 16:26:02 -0500560/**
561 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
562 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
563 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
564 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
565 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
566 * entire sub run has been completed.
567 */
568class GrAtlasTextBlob::VertexRegenerator {
569public:
570 /**
571 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
572 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
573 * SkGlyphCache.
574 */
575 VertexRegenerator(GrAtlasTextBlob* blob, int runIdx, int subRunIdx, const SkMatrix& viewMatrix,
576 SkScalar x, SkScalar y, GrColor color, GrDeferredUploadTarget*,
Brian Salomondeb53cc2017-11-08 13:50:53 -0500577 GrAtlasGlyphCache*, SkAutoGlyphCache*);
Brian Salomon18923f92017-11-06 16:26:02 -0500578
579 struct Result {
580 /**
581 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
582 * draws and call regenerate() again.
583 */
584 bool fFinished = true;
585
586 /**
587 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
588 * fType is kFinished.
589 */
590 int fGlyphsRegenerated = 0;
591
592 /**
593 * Pointer where the caller finds the first regenerated vertex.
594 */
595 const char* fFirstVertex;
596 };
597
598 Result regenerate();
599
600private:
601 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
602 Result doRegen();
603
604 const SkMatrix& fViewMatrix;
605 GrAtlasTextBlob* fBlob;
606 GrDeferredUploadTarget* fUploadTarget;
607 GrAtlasGlyphCache* fGlyphCache;
608 SkAutoGlyphCache* fLazyCache;
609 Run* fRun;
610 Run::SubRunInfo* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500611 GrColor fColor;
612 SkScalar fTransX;
613 SkScalar fTransY;
614
615 uint32_t fRegenFlags = 0;
616 int fCurrGlyph = 0;
617 bool fBrokenRun = false;
618};
619
joshualitt374b2f72015-07-21 08:05:03 -0700620#endif