blob: 49615a92edf3ca4c8405ab52b0fca8dabda6f393 [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"
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"
Mike Reed80747ef2018-01-23 15:29:32 -050017#include "SkMaskFilterBase.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"
Mike Reed274218e2018-01-08 15:05:02 -050021#include "SkRectPriv.h"
joshualitt259fbf12015-07-21 11:39:34 -070022#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070023#include "SkTInternalLList.h"
24
Robert Phillipsc4039ea2018-03-01 11:36:45 -050025class GrAtlasManager;
joshualitt2e2202e2015-12-10 11:22:08 -080026struct GrDistanceFieldAdjustTable;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050027struct GrGlyph;
joshualitt92303772016-02-10 11:55:52 -080028class GrMemoryPool;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050029
joshualitt2e2202e2015-12-10 11:22:08 -080030class SkDrawFilter;
31class SkTextBlob;
32class SkTextBlobRunIterator;
33
Herb Derby26cbe512018-05-24 14:39:01 -040034// With this flag enabled, the GrTextContext will, as a sanity check, regenerate every blob
joshualitt259fbf12015-07-21 11:39:34 -070035// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080036#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070037
joshualitt374b2f72015-07-21 08:05:03 -070038/*
Herb Derby86240592018-05-24 16:12:31 -040039 * A GrTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
joshualitt374b2f72015-07-21 08:05:03 -070040 * on the GPU. These are initially created with valid positions and colors, but invalid
Herb Derby86240592018-05-24 16:12:31 -040041 * texture coordinates. The GrTextBlob itself has a few Blob-wide properties, and also
joshualitt374b2f72015-07-21 08:05:03 -070042 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
43 * reordered.
44 *
Herb Derby86240592018-05-24 16:12:31 -040045 * The only thing(aside from a memcopy) required to flush a GrTextBlob is to ensure that
joshualitt374b2f72015-07-21 08:05:03 -070046 * the GrAtlas will not evict anything the Blob needs.
47 *
48 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070049 *
50 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070051 */
Herb Derby86240592018-05-24 16:12:31 -040052class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
joshualitt2e2202e2015-12-10 11:22:08 -080053public:
Herb Derby86240592018-05-24 16:12:31 -040054 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrTextBlob);
joshualitt374b2f72015-07-21 08:05:03 -070055
Brian Salomon18923f92017-11-06 16:26:02 -050056 class VertexRegenerator;
57
Herb Derby86240592018-05-24 16:12:31 -040058 static sk_sp<GrTextBlob> Make(int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080059
Brian Salomon5c6ac642017-12-19 11:09:32 -050060 /**
61 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
62 * If we ever change that then the key must contain the perspectiveness when there are distance
63 * fields as perspective distance field use 3 component vertex positions and non-perspective
64 * uses 2.
65 */
joshualitt323c2eb2016-01-20 06:48:47 -080066 struct Key {
67 Key() {
68 sk_bzero(this, sizeof(Key));
69 }
70 uint32_t fUniqueID;
71 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
72 // Each color is assigned to on of a fixed number of buckets based on its
73 // luminance. For each luminance bucket there is a "canonical color" that
74 // represents the bucket. This functionality is currently only supported for A8
75 SkColor fCanonicalColor;
76 SkPaint::Style fStyle;
77 SkPixelGeometry fPixelGeometry;
78 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070079 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080080
81 bool operator==(const Key& other) const {
82 return 0 == memcmp(this, &other, sizeof(Key));
83 }
84 };
85
Herb Derby86240592018-05-24 16:12:31 -040086 void setupKey(const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050087 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -080088 const SkPaint& paint) {
89 fKey = key;
90 if (key.fHasBlur) {
91 fBlurRec = blurRec;
92 }
93 if (key.fStyle != SkPaint::kFill_Style) {
94 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
95 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
96 fStrokeInfo.fJoin = paint.getStrokeJoin();
97 }
98 }
99
Herb Derby86240592018-05-24 16:12:31 -0400100 static const Key& GetKey(const GrTextBlob& blob) {
joshualitt323c2eb2016-01-20 06:48:47 -0800101 return blob.fKey;
102 }
103
104 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700105 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800106 }
107
108 void operator delete(void* p) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400109 ::operator delete(p);
joshualitt323c2eb2016-01-20 06:48:47 -0800110 }
Herb Derbyb12175f2018-05-23 16:38:09 -0400111
joshualitt323c2eb2016-01-20 06:48:47 -0800112 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400113 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800114 return sk_malloc_throw(0);
115 }
116
117 void* operator new(size_t, void* p) { return p; }
joshualitt323c2eb2016-01-20 06:48:47 -0800118
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
Jim Van Verthb515ae72018-05-23 16:44:55 -0400145 // sets the last subrun of runIndex to use w values
146 void setSubRunHasW(int runIndex, bool hasWCoord) {
147 Run& run = fRuns[runIndex];
148 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
149 subRun.setHasWCoord(hasWCoord);
150 }
151
Jim Van Verth54d9c882018-02-08 16:14:48 -0500152 void setRunPaintFlags(int runIndex, uint16_t paintFlags) {
153 fRuns[runIndex].fPaintFlags = paintFlags & Run::kPaintFlagsMask;
Jim Van Verth89737de2018-02-06 21:30:20 +0000154 }
155
joshualitt323c2eb2016-01-20 06:48:47 -0800156 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
157 // we init fMaxMinScale and fMinMaxScale in the constructor
158 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
159 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
160 }
161
162 // inits the override descriptor on the current run. All following subruns must use this
163 // descriptor
164 void initOverride(int runIndex) {
165 Run& run = fRuns[runIndex];
166 // Push back a new subrun to fill and set the override descriptor
167 run.push_back();
168 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
169 }
170
Herb Derby526819d2018-03-09 12:51:12 -0500171 SkExclusiveStrikePtr setupCache(int runIndex,
172 const SkSurfaceProps& props,
173 SkScalerContextFlags scalerContextFlags,
174 const SkPaint& skPaint,
175 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800176
177 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
178 // as a path.
179 void appendGlyph(int runIndex,
180 const SkRect& positions,
181 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500182 sk_sp<GrTextStrike> strike,
joshualitt323c2eb2016-01-20 06:48:47 -0800183 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700184 SkGlyphCache*, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500185 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
186
187 // Appends a glyph to the blob as a path only.
188 void appendPathGlyph(int runIndex, const SkPath& path,
189 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
joshualitt323c2eb2016-01-20 06:48:47 -0800190
Jim Van Verthb515ae72018-05-23 16:44:55 -0400191 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800192 switch (maskFormat) {
193 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400194 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800195 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400196 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800197 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400198 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800199 return kLCDTextVASize;
200 }
201 }
202
Mike Reed80747ef2018-01-23 15:29:32 -0500203 bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800204 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
205
Robert Phillips5a66efb2018-03-07 15:13:18 -0500206 void flush(GrTextUtils::Target*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500207 const GrDistanceFieldAdjustTable* distanceAdjustTable,
208 const GrTextUtils::Paint& paint, const GrClip& clip,
209 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
210 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800211
joshualitt8e0ef292016-02-19 14:13:03 -0800212 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
213 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittbc811112016-02-11 12:42:02 -0800214 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
215 // into device space.
216 // We handle vertex bounds differently for distance field text and bitmap text because
217 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
218 // from one blob then we are going to pay the price here of mapping the rect for each run.
219 const Run& run = fRuns[runIndex];
220 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
221 *outBounds = subRun.vertexBounds();
222 if (subRun.drawAsDistanceFields()) {
223 // Distance field text is positioned with the (X,Y) as part of the glyph position,
224 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800225 outBounds->offset(x - fInitialX, y - fInitialY);
226 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800227 } else {
228 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
229 // device space.
230 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
231
232 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
233
joshualitt8e0ef292016-02-19 14:13:03 -0800234 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800235
joshualitt8e0ef292016-02-19 14:13:03 -0800236 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800237 boundsMatrix.mapRect(outBounds);
238
239 // Due to floating point numerical inaccuracies, we have to round out here
240 outBounds->roundOut(outBounds);
241 }
242 }
243
joshualitt323c2eb2016-01-20 06:48:47 -0800244 // position + local coord
245 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400246 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800247 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500248 static const size_t kGrayTextDFPerspectiveVASize =
249 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800250 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500251 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800252 static const int kVerticesPerGlyph = 4;
253
Herb Derby86240592018-05-24 16:12:31 -0400254 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800255
256 // The color here is the GrPaint color, and it is used to determine whether we
257 // have to regenerate LCD text blobs.
258 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400259 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
260 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400261 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800262 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800263 }
264
joshualitt7481e752016-01-22 06:08:48 -0800265 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
266 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800267 }
268
joshualitt92303772016-02-10 11:55:52 -0800269 const Key& key() const { return fKey; }
270
Herb Derbyb12175f2018-05-23 16:38:09 -0400271 size_t size() const { return fSize; }
272
Herb Derby86240592018-05-24 16:12:31 -0400273 ~GrTextBlob() {
joshualitt92303772016-02-10 11:55:52 -0800274 for (int i = 0; i < fRunCount; i++) {
275 fRuns[i].~Run();
276 }
277 }
278
joshualittbc811112016-02-11 12:42:02 -0800279 ////////////////////////////////////////////////////////////////////////////////////////////////
280 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400281 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400282 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
283 const GrTextUtils::Paint&, const SkSurfaceProps&,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500284 const GrDistanceFieldAdjustTable*,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500285 GrTextUtils::Target*);
joshualittbc811112016-02-11 12:42:02 -0800286
joshualitt323c2eb2016-01-20 06:48:47 -0800287private:
Herb Derby86240592018-05-24 16:12:31 -0400288 GrTextBlob()
joshualitt92303772016-02-10 11:55:52 -0800289 : fMaxMinScale(-SK_ScalarMax)
290 , fMinMaxScale(SK_ScalarMax)
291 , fTextType(0) {}
292
joshualitt323c2eb2016-01-20 06:48:47 -0800293
joshualitt8e0ef292016-02-19 14:13:03 -0800294 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800295 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
296 // these numbers. When blobs are reused with new matrices, we need to return to model space so
297 // we can update the vertex bounds appropriately.
298 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800299 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800300 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
301 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800302 }
joshualitt8e0ef292016-02-19 14:13:03 -0800303 fInitialX = x;
304 fInitialY = y;
305
306 // make sure all initial subruns have the correct VM and X/Y applied
307 for (int i = 0; i < fRunCount; i++) {
308 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
309 }
joshualitt7481e752016-01-22 06:08:48 -0800310 }
311
joshualitt374b2f72015-07-21 08:05:03 -0700312 /*
313 * Each Run inside of the blob can have its texture coordinates regenerated if required.
314 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
315 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
316 * this at a more fine grained level, but its not clear if this is worth it, as evictions
317 * should be fairly rare.
318 *
319 * One additional point, each run can contain glyphs with any of the three mask formats.
320 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
321 * a new subrun each time the mask format changes in a run. In theory, a run can have as
322 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
323 * practice, the vast majority of runs have only a single subrun.
324 *
Herb Derby26cbe512018-05-24 14:39:01 -0400325 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500326 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
327 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700328 * can flush in this manner, so we always allocate vertices for the run, regardless of
329 * whether or not it is too large. The benefit of this strategy is that we can always reuse
330 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500331 * 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 -0700332 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
333 * would greatly increase the memory of these cached items.
334 */
335 struct Run {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500336 Run() : fPaintFlags(0)
337 , fInitialized(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700338 // To ensure we always have one subrun, we push back a fresh run here
339 fSubRunInfo.push_back();
340 }
341 struct SubRunInfo {
342 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500343 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
344 , fVertexStartIndex(0)
345 , fVertexEndIndex(0)
346 , fGlyphStartIndex(0)
347 , fGlyphEndIndex(0)
348 , fColor(GrColor_ILLEGAL)
349 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400350 , fFlags(0) {
Mike Reed274218e2018-01-08 15:05:02 -0500351 fVertexBounds = SkRectPriv::MakeLargestInverted();
joshualitt7481e752016-01-22 06:08:48 -0800352 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700353 SubRunInfo(const SubRunInfo& that)
354 : fBulkUseToken(that.fBulkUseToken)
355 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800356 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800357 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700358 , fAtlasGeneration(that.fAtlasGeneration)
359 , fVertexStartIndex(that.fVertexStartIndex)
360 , fVertexEndIndex(that.fVertexEndIndex)
361 , fGlyphStartIndex(that.fGlyphStartIndex)
362 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800363 , fX(that.fX)
364 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800365 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700366 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400367 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700368 }
joshualitt3660d532015-12-07 11:32:50 -0800369
joshualitt18b072d2015-12-07 12:26:12 -0800370 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800371 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500372 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500373 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
374 GrTextStrike* strike() const { return fStrike.get(); }
375 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
joshualitt3660d532015-12-07 11:32:50 -0800376
377 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
378 uint64_t atlasGeneration() const { return fAtlasGeneration; }
379
380 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800381 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800382 size_t vertexEndIndex() const { return fVertexEndIndex; }
383 void appendVertices(size_t vertexStride) {
384 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
385 }
386
387 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800388 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800389 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
390 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800391 void setColor(GrColor color) { fColor = color; }
392 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800393 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
394 GrMaskFormat maskFormat() const { return fMaskFormat; }
395
joshualitt18b072d2015-12-07 12:26:12 -0800396 void setAsSuccessor(const SubRunInfo& prev) {
397 fGlyphStartIndex = prev.glyphEndIndex();
398 fGlyphEndIndex = prev.glyphEndIndex();
399
400 fVertexStartIndex = prev.vertexEndIndex();
401 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800402
403 // copy over viewmatrix settings
404 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800405 }
406
joshualitt7481e752016-01-22 06:08:48 -0800407 const SkRect& vertexBounds() const { return fVertexBounds; }
408 void joinGlyphBounds(const SkRect& glyphBounds) {
409 fVertexBounds.joinNonEmptyArg(glyphBounds);
410 }
411
joshualitt8e0ef292016-02-19 14:13:03 -0800412 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
413 fCurrentViewMatrix = viewMatrix;
414 fX = x;
415 fY = y;
416 }
417
418 // This function assumes the translation will be applied before it is called again
419 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500420 SkScalar* transX, SkScalar* transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800421
joshualitt3660d532015-12-07 11:32:50 -0800422 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400423 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
424 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
425 void setUseLCDText(bool useLCDText) {
426 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
427 }
428 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
429 void setAntiAliased(bool antiAliased) {
430 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
431 }
432 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500433 void setHasWCoord(bool hasW) {
434 fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag;
435 }
436 bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400437 void setNeedsTransform(bool needsTransform) {
438 fFlags = needsTransform ? (fFlags | kNeedsTransform_Flag)
439 : fFlags & ~kNeedsTransform_Flag;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500440 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400441 bool needsTransform() const { return SkToBool(fFlags & kNeedsTransform_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800442
443 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400444 enum Flag {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500445 kDrawAsSDF_Flag = 0x01,
446 kUseLCDText_Flag = 0x02,
447 kAntiAliased_Flag = 0x04,
448 kHasWCoord_Flag = 0x08,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400449 kNeedsTransform_Flag = 0x10
Jim Van Verth90e89b32017-07-06 16:36:55 -0400450 };
451
Brian Salomon2ee084e2016-12-16 18:59:19 -0500452 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500453 sk_sp<GrTextStrike> 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;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400466 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700467
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;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700483 sk_sp<SkMaskFilter> fMaskFilter;
joshualitt3660d532015-12-07 11:32:50 -0800484
485 // Distance field text cannot draw coloremoji, and so has to fall back. However,
486 // though the distance field text and the coloremoji may share the same run, they
487 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
488 // will be used in place of the run's descriptor to regen texture coords
Ben Wagner145dbcd2016-11-03 14:40:50 -0400489 std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
Jim Van Verth54d9c882018-02-08 16:14:48 -0500490
491 // Any glyphs that can't be rendered with the base or override descriptor
492 // are rendered as paths
493 struct PathGlyph {
494 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
495 : fPath(path)
496 , fX(x)
497 , fY(y)
498 , fScale(scale)
499 , fPreTransformed(preXformed) {}
500 SkPath fPath;
501 SkScalar fX;
502 SkScalar fY;
503 SkScalar fScale;
504 bool fPreTransformed;
505 };
506
507 SkTArray<PathGlyph> fPathGlyphs;
508
509 struct {
510 unsigned fPaintFlags : 16; // needed mainly for rendering paths
511 bool fInitialized : 1;
512 };
513 // the only flags we need to set
514 static constexpr auto kPaintFlagsMask = SkPaint::kAntiAlias_Flag;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400515 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700516
Brian Salomonf18b1d82017-10-27 11:30:49 -0400517 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400518 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400519 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500520 const GrTextUtils::Paint&, const SkSurfaceProps&,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500521 const GrDistanceFieldAdjustTable*, GrTextUtils::Target*);
joshualitt323c2eb2016-01-20 06:48:47 -0800522
joshualitt374b2f72015-07-21 08:05:03 -0700523 struct StrokeInfo {
524 SkScalar fFrameWidth;
525 SkScalar fMiterLimit;
526 SkPaint::Join fJoin;
527 };
528
529 enum TextType {
530 kHasDistanceField_TextType = 0x1,
531 kHasBitmap_TextType = 0x2,
532 };
533
534 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500535 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700536 GrGlyph** fGlyphs;
537 Run* fRuns;
Mike Reed80747ef2018-01-23 15:29:32 -0500538 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700539 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700540 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800541 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800542 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800543 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400544 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800545 SkScalar fInitialX;
546 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700547
548 // We can reuse distance field text, but only if the new viewmatrix would not result in
549 // a mip change. Because there can be multiple runs in a blob, we track the overall
550 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
551 SkScalar fMaxMinScale;
552 SkScalar fMinMaxScale;
553 int fRunCount;
554 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700555};
556
Brian Salomon18923f92017-11-06 16:26:02 -0500557/**
558 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
559 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
560 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
561 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
562 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
563 * entire sub run has been completed.
564 */
Herb Derby86240592018-05-24 16:12:31 -0400565class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500566public:
567 /**
568 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
569 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
570 * SkGlyphCache.
571 */
Herb Derby86240592018-05-24 16:12:31 -0400572 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500573 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500574 GrDeferredUploadTarget*, GrGlyphCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400575 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500576
577 struct Result {
578 /**
579 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
580 * draws and call regenerate() again.
581 */
582 bool fFinished = true;
583
584 /**
585 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
586 * fType is kFinished.
587 */
588 int fGlyphsRegenerated = 0;
589
590 /**
591 * Pointer where the caller finds the first regenerated vertex.
592 */
593 const char* fFirstVertex;
594 };
595
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500596 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500597
598private:
599 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500600 bool doRegen(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500601
Robert Phillips4bc70112018-03-01 10:24:02 -0500602 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500603 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400604 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500605 GrDeferredUploadTarget* fUploadTarget;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500606 GrGlyphCache* fGlyphCache;
607 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500608 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500609 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
Herb Derby86240592018-05-24 16:12:31 -0400620#endif // GrTextBlob_DEFINED