blob: 686dac4eb13a9983ba568bececf023dd9f8c8487 [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
Herb Derby86240592018-05-24 16:12:31 -04008#ifndef GrTextBlob_DEFINED
9#define GrTextBlob_DEFINED
joshualitt374b2f72015-07-21 08:05:03 -070010
joshualitt259fbf12015-07-21 11:39:34 -070011#include "GrColor.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050012#include "GrDrawOpAtlas.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050013#include "GrGlyphCache.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050014#include "GrTextUtils.h"
joshualitt374b2f72015-07-21 08:05:03 -070015#include "SkDescriptor.h"
Mike Reed80747ef2018-01-23 15:29:32 -050016#include "SkMaskFilterBase.h"
mtklein4e976072016-08-08 09:06:27 -070017#include "SkOpts.h"
bsalomon8b6fa5e2016-05-19 16:23:47 -070018#include "SkPathEffect.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050019#include "SkPoint3.h"
Mike Reed274218e2018-01-08 15:05:02 -050020#include "SkRectPriv.h"
joshualitt259fbf12015-07-21 11:39:34 -070021#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070022#include "SkTInternalLList.h"
23
Robert Phillipsc4039ea2018-03-01 11:36:45 -050024class GrAtlasManager;
joshualitt2e2202e2015-12-10 11:22:08 -080025struct GrDistanceFieldAdjustTable;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050026struct GrGlyph;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050027
joshualitt2e2202e2015-12-10 11:22:08 -080028class SkTextBlob;
29class SkTextBlobRunIterator;
30
Herb Derby26cbe512018-05-24 14:39:01 -040031// With this flag enabled, the GrTextContext will, as a sanity check, regenerate every blob
joshualitt259fbf12015-07-21 11:39:34 -070032// 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/*
Herb Derby86240592018-05-24 16:12:31 -040036 * A GrTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
joshualitt374b2f72015-07-21 08:05:03 -070037 * on the GPU. These are initially created with valid positions and colors, but invalid
Herb Derby86240592018-05-24 16:12:31 -040038 * texture coordinates. The GrTextBlob itself has a few Blob-wide properties, and also
joshualitt374b2f72015-07-21 08:05:03 -070039 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
40 * reordered.
41 *
Herb Derby86240592018-05-24 16:12:31 -040042 * The only thing(aside from a memcopy) required to flush a GrTextBlob is to ensure that
joshualitt374b2f72015-07-21 08:05:03 -070043 * 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 */
Herb Derby86240592018-05-24 16:12:31 -040049class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
joshualitt2e2202e2015-12-10 11:22:08 -080050public:
Herb Derby86240592018-05-24 16:12:31 -040051 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrTextBlob);
joshualitt374b2f72015-07-21 08:05:03 -070052
Brian Salomon18923f92017-11-06 16:26:02 -050053 class VertexRegenerator;
54
Herb Derby86240592018-05-24 16:12:31 -040055 static sk_sp<GrTextBlob> Make(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
Herb Derby86240592018-05-24 16:12:31 -040083 void setupKey(const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050084 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -080085 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
Herb Derby86240592018-05-24 16:12:31 -040097 static const Key& GetKey(const GrTextBlob& blob) {
joshualitt323c2eb2016-01-20 06:48:47 -080098 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) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400106 ::operator delete(p);
joshualitt323c2eb2016-01-20 06:48:47 -0800107 }
Herb Derbyb12175f2018-05-23 16:38:09 -0400108
joshualitt323c2eb2016-01-20 06:48:47 -0800109 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; }
joshualitt323c2eb2016-01-20 06:48:47 -0800115
116 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
117 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
118 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
119 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
120
joshualittddd22d82016-02-16 06:47:52 -0800121 int runCount() const { return fRunCount; }
122
joshualitt323c2eb2016-01-20 06:48:47 -0800123 void push_back_run(int currRun) {
124 SkASSERT(currRun < fRunCount);
125 if (currRun > 0) {
126 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
127 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
128 newRun.setAsSuccessor(lastRun);
129 }
130 }
131
132 // sets the last subrun of runIndex to use distance field text
Brian Salomon5c6ac642017-12-19 11:09:32 -0500133 void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800134 Run& run = fRuns[runIndex];
135 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
136 subRun.setUseLCDText(hasLCD);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400137 subRun.setAntiAliased(isAntiAlias);
joshualitt323c2eb2016-01-20 06:48:47 -0800138 subRun.setDrawAsDistanceFields();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500139 subRun.setHasWCoord(hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800140 }
141
Jim Van Verthb515ae72018-05-23 16:44:55 -0400142 // sets the last subrun of runIndex to use w values
143 void setSubRunHasW(int runIndex, bool hasWCoord) {
144 Run& run = fRuns[runIndex];
145 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
146 subRun.setHasWCoord(hasWCoord);
147 }
148
Jim Van Verth54d9c882018-02-08 16:14:48 -0500149 void setRunPaintFlags(int runIndex, uint16_t paintFlags) {
150 fRuns[runIndex].fPaintFlags = paintFlags & Run::kPaintFlagsMask;
Jim Van Verth89737de2018-02-06 21:30:20 +0000151 }
152
joshualitt323c2eb2016-01-20 06:48:47 -0800153 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
154 // we init fMaxMinScale and fMinMaxScale in the constructor
155 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
156 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
157 }
158
159 // inits the override descriptor on the current run. All following subruns must use this
160 // descriptor
161 void initOverride(int runIndex) {
162 Run& run = fRuns[runIndex];
163 // Push back a new subrun to fill and set the override descriptor
164 run.push_back();
165 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
166 }
167
Herb Derby526819d2018-03-09 12:51:12 -0500168 SkExclusiveStrikePtr setupCache(int runIndex,
169 const SkSurfaceProps& props,
170 SkScalerContextFlags scalerContextFlags,
171 const SkPaint& skPaint,
172 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800173
174 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
175 // as a path.
176 void appendGlyph(int runIndex,
177 const SkRect& positions,
178 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500179 sk_sp<GrTextStrike> strike,
joshualitt323c2eb2016-01-20 06:48:47 -0800180 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700181 SkGlyphCache*, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500182 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
183
184 // Appends a glyph to the blob as a path only.
185 void appendPathGlyph(int runIndex, const SkPath& path,
186 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
joshualitt323c2eb2016-01-20 06:48:47 -0800187
Jim Van Verthb515ae72018-05-23 16:44:55 -0400188 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800189 switch (maskFormat) {
190 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400191 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800192 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400193 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800194 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400195 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800196 return kLCDTextVASize;
197 }
198 }
199
Mike Reed80747ef2018-01-23 15:29:32 -0500200 bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800201 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
202
Robert Phillips5a66efb2018-03-07 15:13:18 -0500203 void flush(GrTextUtils::Target*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500204 const GrDistanceFieldAdjustTable* distanceAdjustTable,
205 const GrTextUtils::Paint& paint, const GrClip& clip,
206 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
207 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800208
joshualitt8e0ef292016-02-19 14:13:03 -0800209 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
Jim Van Verth70276912018-06-01 13:46:46 -0400210 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
211 bool needsGlyphTransform) {
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();
Jim Van Verth70276912018-06-01 13:46:46 -0400220 if (needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800221 // 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);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400244 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800245 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500246 static const size_t kGrayTextDFPerspectiveVASize =
247 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800248 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500249 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800250 static const int kVerticesPerGlyph = 4;
251
Herb Derby86240592018-05-24 16:12:31 -0400252 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800253
254 // The color here is the GrPaint color, and it is used to determine whether we
255 // have to regenerate LCD text blobs.
256 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400257 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
258 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400259 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800260 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800261 }
262
joshualitt7481e752016-01-22 06:08:48 -0800263 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
264 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800265 }
266
joshualitt92303772016-02-10 11:55:52 -0800267 const Key& key() const { return fKey; }
268
Herb Derbyb12175f2018-05-23 16:38:09 -0400269 size_t size() const { return fSize; }
270
Herb Derby86240592018-05-24 16:12:31 -0400271 ~GrTextBlob() {
joshualitt92303772016-02-10 11:55:52 -0800272 for (int i = 0; i < fRunCount; i++) {
273 fRuns[i].~Run();
274 }
275 }
276
joshualittbc811112016-02-11 12:42:02 -0800277 ////////////////////////////////////////////////////////////////////////////////////////////////
278 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400279 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400280 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
281 const GrTextUtils::Paint&, const SkSurfaceProps&,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500282 const GrDistanceFieldAdjustTable*,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500283 GrTextUtils::Target*);
joshualittbc811112016-02-11 12:42:02 -0800284
joshualitt323c2eb2016-01-20 06:48:47 -0800285private:
Herb Derby86240592018-05-24 16:12:31 -0400286 GrTextBlob()
joshualitt92303772016-02-10 11:55:52 -0800287 : fMaxMinScale(-SK_ScalarMax)
288 , fMinMaxScale(SK_ScalarMax)
289 , fTextType(0) {}
290
joshualitt323c2eb2016-01-20 06:48:47 -0800291
joshualitt8e0ef292016-02-19 14:13:03 -0800292 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800293 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
294 // these numbers. When blobs are reused with new matrices, we need to return to model space so
295 // we can update the vertex bounds appropriately.
296 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800297 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800298 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
299 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800300 }
joshualitt8e0ef292016-02-19 14:13:03 -0800301 fInitialX = x;
302 fInitialY = y;
303
304 // make sure all initial subruns have the correct VM and X/Y applied
305 for (int i = 0; i < fRunCount; i++) {
306 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
307 }
joshualitt7481e752016-01-22 06:08:48 -0800308 }
309
joshualitt374b2f72015-07-21 08:05:03 -0700310 /*
311 * Each Run inside of the blob can have its texture coordinates regenerated if required.
312 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
313 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
314 * this at a more fine grained level, but its not clear if this is worth it, as evictions
315 * should be fairly rare.
316 *
317 * One additional point, each run can contain glyphs with any of the three mask formats.
318 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
319 * a new subrun each time the mask format changes in a run. In theory, a run can have as
320 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
321 * practice, the vast majority of runs have only a single subrun.
322 *
Herb Derby26cbe512018-05-24 14:39:01 -0400323 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500324 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
325 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700326 * can flush in this manner, so we always allocate vertices for the run, regardless of
327 * whether or not it is too large. The benefit of this strategy is that we can always reuse
328 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500329 * glyphs, however, it's not clear if this is a win because we'd still have to either go to the
joshualitt374b2f72015-07-21 08:05:03 -0700330 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
331 * would greatly increase the memory of these cached items.
332 */
333 struct Run {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500334 Run() : fPaintFlags(0)
335 , fInitialized(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700336 // To ensure we always have one subrun, we push back a fresh run here
337 fSubRunInfo.push_back();
338 }
339 struct SubRunInfo {
340 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500341 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
342 , fVertexStartIndex(0)
343 , fVertexEndIndex(0)
344 , fGlyphStartIndex(0)
345 , fGlyphEndIndex(0)
346 , fColor(GrColor_ILLEGAL)
347 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400348 , fFlags(0) {
Mike Reed274218e2018-01-08 15:05:02 -0500349 fVertexBounds = SkRectPriv::MakeLargestInverted();
joshualitt7481e752016-01-22 06:08:48 -0800350 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700351 SubRunInfo(const SubRunInfo& that)
352 : fBulkUseToken(that.fBulkUseToken)
353 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800354 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800355 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700356 , fAtlasGeneration(that.fAtlasGeneration)
357 , fVertexStartIndex(that.fVertexStartIndex)
358 , fVertexEndIndex(that.fVertexEndIndex)
359 , fGlyphStartIndex(that.fGlyphStartIndex)
360 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800361 , fX(that.fX)
362 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800363 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700364 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400365 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700366 }
joshualitt3660d532015-12-07 11:32:50 -0800367
joshualitt18b072d2015-12-07 12:26:12 -0800368 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800369 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500370 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500371 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
372 GrTextStrike* strike() const { return fStrike.get(); }
373 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
joshualitt3660d532015-12-07 11:32:50 -0800374
375 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
376 uint64_t atlasGeneration() const { return fAtlasGeneration; }
377
378 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800379 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800380 size_t vertexEndIndex() const { return fVertexEndIndex; }
381 void appendVertices(size_t vertexStride) {
382 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
383 }
384
385 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800386 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800387 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
388 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800389 void setColor(GrColor color) { fColor = color; }
390 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800391 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
392 GrMaskFormat maskFormat() const { return fMaskFormat; }
393
joshualitt18b072d2015-12-07 12:26:12 -0800394 void setAsSuccessor(const SubRunInfo& prev) {
395 fGlyphStartIndex = prev.glyphEndIndex();
396 fGlyphEndIndex = prev.glyphEndIndex();
397
398 fVertexStartIndex = prev.vertexEndIndex();
399 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800400
401 // copy over viewmatrix settings
402 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800403 }
404
joshualitt7481e752016-01-22 06:08:48 -0800405 const SkRect& vertexBounds() const { return fVertexBounds; }
406 void joinGlyphBounds(const SkRect& glyphBounds) {
407 fVertexBounds.joinNonEmptyArg(glyphBounds);
408 }
409
joshualitt8e0ef292016-02-19 14:13:03 -0800410 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
411 fCurrentViewMatrix = viewMatrix;
412 fX = x;
413 fY = y;
414 }
415
416 // This function assumes the translation will be applied before it is called again
417 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500418 SkScalar* transX, SkScalar* transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800419
joshualitt3660d532015-12-07 11:32:50 -0800420 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400421 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
422 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
423 void setUseLCDText(bool useLCDText) {
424 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
425 }
426 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
427 void setAntiAliased(bool antiAliased) {
428 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
429 }
430 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500431 void setHasWCoord(bool hasW) {
432 fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag;
433 }
434 bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400435 void setNeedsTransform(bool needsTransform) {
436 fFlags = needsTransform ? (fFlags | kNeedsTransform_Flag)
437 : fFlags & ~kNeedsTransform_Flag;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500438 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400439 bool needsTransform() const { return SkToBool(fFlags & kNeedsTransform_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800440
441 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400442 enum Flag {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500443 kDrawAsSDF_Flag = 0x01,
444 kUseLCDText_Flag = 0x02,
445 kAntiAliased_Flag = 0x04,
446 kHasWCoord_Flag = 0x08,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400447 kNeedsTransform_Flag = 0x10
Jim Van Verth90e89b32017-07-06 16:36:55 -0400448 };
449
Brian Salomon2ee084e2016-12-16 18:59:19 -0500450 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500451 sk_sp<GrTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800452 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800453 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700454 uint64_t fAtlasGeneration;
455 size_t fVertexStartIndex;
456 size_t fVertexEndIndex;
457 uint32_t fGlyphStartIndex;
458 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800459 SkScalar fX;
460 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800461 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700462 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400463 uint32_t fFlags;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400464 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700465
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;
Hal Canary144caf52016-11-07 17:57:18 -0500475 sk_sp<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700476 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
477 SkAutoDescriptor fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700478
479 // Effects from the paint that are used to build a SkScalerContext.
480 sk_sp<SkPathEffect> fPathEffect;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700481 sk_sp<SkMaskFilter> fMaskFilter;
joshualitt3660d532015-12-07 11:32:50 -0800482
483 // Distance field text cannot draw coloremoji, and so has to fall back. However,
484 // though the distance field text and the coloremoji may share the same run, they
485 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
486 // will be used in place of the run's descriptor to regen texture coords
Ben Wagner145dbcd2016-11-03 14:40:50 -0400487 std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
Jim Van Verth54d9c882018-02-08 16:14:48 -0500488
489 // Any glyphs that can't be rendered with the base or override descriptor
490 // are rendered as paths
491 struct PathGlyph {
492 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
493 : fPath(path)
494 , fX(x)
495 , fY(y)
496 , fScale(scale)
497 , fPreTransformed(preXformed) {}
498 SkPath fPath;
499 SkScalar fX;
500 SkScalar fY;
501 SkScalar fScale;
502 bool fPreTransformed;
503 };
504
505 SkTArray<PathGlyph> fPathGlyphs;
506
507 struct {
508 unsigned fPaintFlags : 16; // needed mainly for rendering paths
509 bool fInitialized : 1;
510 };
511 // the only flags we need to set
512 static constexpr auto kPaintFlagsMask = SkPaint::kAntiAlias_Flag;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400513 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700514
Brian Salomonf18b1d82017-10-27 11:30:49 -0400515 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400516 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400517 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500518 const GrTextUtils::Paint&, const SkSurfaceProps&,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500519 const GrDistanceFieldAdjustTable*, GrTextUtils::Target*);
joshualitt323c2eb2016-01-20 06:48:47 -0800520
joshualitt374b2f72015-07-21 08:05:03 -0700521 struct StrokeInfo {
522 SkScalar fFrameWidth;
523 SkScalar fMiterLimit;
524 SkPaint::Join fJoin;
525 };
526
527 enum TextType {
528 kHasDistanceField_TextType = 0x1,
529 kHasBitmap_TextType = 0x2,
530 };
531
532 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500533 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700534 GrGlyph** fGlyphs;
535 Run* fRuns;
Mike Reed80747ef2018-01-23 15:29:32 -0500536 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700537 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700538 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800539 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800540 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800541 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400542 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800543 SkScalar fInitialX;
544 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700545
546 // We can reuse distance field text, but only if the new viewmatrix would not result in
547 // a mip change. Because there can be multiple runs in a blob, we track the overall
548 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
549 SkScalar fMaxMinScale;
550 SkScalar fMinMaxScale;
551 int fRunCount;
552 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700553};
554
Brian Salomon18923f92017-11-06 16:26:02 -0500555/**
556 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
557 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
558 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
559 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
560 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
561 * entire sub run has been completed.
562 */
Herb Derby86240592018-05-24 16:12:31 -0400563class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500564public:
565 /**
566 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
567 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
568 * SkGlyphCache.
569 */
Herb Derby86240592018-05-24 16:12:31 -0400570 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500571 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500572 GrDeferredUploadTarget*, GrGlyphCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400573 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500574
575 struct Result {
576 /**
577 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
578 * draws and call regenerate() again.
579 */
580 bool fFinished = true;
581
582 /**
583 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
584 * fType is kFinished.
585 */
586 int fGlyphsRegenerated = 0;
587
588 /**
589 * Pointer where the caller finds the first regenerated vertex.
590 */
591 const char* fFirstVertex;
592 };
593
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500594 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500595
596private:
597 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500598 bool doRegen(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500599
Robert Phillips4bc70112018-03-01 10:24:02 -0500600 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500601 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400602 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500603 GrDeferredUploadTarget* fUploadTarget;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500604 GrGlyphCache* fGlyphCache;
605 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500606 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500607 Run* fRun;
608 Run::SubRunInfo* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500609 GrColor fColor;
610 SkScalar fTransX;
611 SkScalar fTransY;
612
613 uint32_t fRegenFlags = 0;
614 int fCurrGlyph = 0;
615 bool fBrokenRun = false;
616};
617
Herb Derby86240592018-05-24 16:12:31 -0400618#endif // GrTextBlob_DEFINED