blob: f773d0b8ee17114194ec118f1c7cd8509e3ddaf3 [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"
Herb Derbyc1b482c2018-08-09 15:02:27 -040014#include "GrTextTarget.h"
Herb Derbydc214c22018-11-08 13:31:39 -050015#include "text/GrTextContext.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"
Herb Derbyc1b482c2018-08-09 15:02:27 -040022#include "SkStrikeCache.h"
joshualitt259fbf12015-07-21 11:39:34 -070023#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070024#include "SkTInternalLList.h"
25
Robert Phillipsc4039ea2018-03-01 11:36:45 -050026class GrAtlasManager;
joshualitt2e2202e2015-12-10 11:22:08 -080027struct GrDistanceFieldAdjustTable;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050028struct GrGlyph;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050029
joshualitt2e2202e2015-12-10 11:22:08 -080030class SkTextBlob;
31class SkTextBlobRunIterator;
32
Herb Derby26cbe512018-05-24 14:39:01 -040033// With this flag enabled, the GrTextContext will, as a sanity check, regenerate every blob
joshualitt259fbf12015-07-21 11:39:34 -070034// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080035#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070036
joshualitt374b2f72015-07-21 08:05:03 -070037/*
Herb Derby86240592018-05-24 16:12:31 -040038 * A GrTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
joshualitt374b2f72015-07-21 08:05:03 -070039 * on the GPU. These are initially created with valid positions and colors, but invalid
Herb Derby86240592018-05-24 16:12:31 -040040 * texture coordinates. The GrTextBlob itself has a few Blob-wide properties, and also
joshualitt374b2f72015-07-21 08:05:03 -070041 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
42 * reordered.
43 *
Herb Derby86240592018-05-24 16:12:31 -040044 * The only thing(aside from a memcopy) required to flush a GrTextBlob is to ensure that
joshualitt374b2f72015-07-21 08:05:03 -070045 * the GrAtlas will not evict anything the Blob needs.
46 *
47 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070048 *
49 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070050 */
Mike Klein408ef212018-10-30 15:23:00 +000051class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
Herb Derbydc214c22018-11-08 13:31:39 -050052 struct Run;
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 Derbydc214c22018-11-08 13:31:39 -050058 void generateFromGlyphRunList(GrGlyphCache* glyphCache,
59 const GrShaderCaps& shaderCaps,
60 const GrTextContext::Options& options,
61 const SkPaint& paint,
62 const SkPMColor4f& filteredColor,
63 SkScalerContextFlags scalerContextFlags,
64 const SkMatrix& viewMatrix,
65 const SkSurfaceProps& props,
66 const SkGlyphRunList& glyphRunList,
67 SkGlyphRunListPainter* glyphPainter);
68
Herb Derby86240592018-05-24 16:12:31 -040069 static sk_sp<GrTextBlob> Make(int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080070
Brian Salomon5c6ac642017-12-19 11:09:32 -050071 /**
72 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
73 * If we ever change that then the key must contain the perspectiveness when there are distance
74 * fields as perspective distance field use 3 component vertex positions and non-perspective
75 * uses 2.
76 */
joshualitt323c2eb2016-01-20 06:48:47 -080077 struct Key {
78 Key() {
79 sk_bzero(this, sizeof(Key));
80 }
81 uint32_t fUniqueID;
82 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
83 // Each color is assigned to on of a fixed number of buckets based on its
84 // luminance. For each luminance bucket there is a "canonical color" that
85 // represents the bucket. This functionality is currently only supported for A8
86 SkColor fCanonicalColor;
87 SkPaint::Style fStyle;
88 SkPixelGeometry fPixelGeometry;
89 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070090 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080091
92 bool operator==(const Key& other) const {
93 return 0 == memcmp(this, &other, sizeof(Key));
94 }
95 };
96
Herb Derby86240592018-05-24 16:12:31 -040097 void setupKey(const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050098 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -080099 const SkPaint& paint) {
100 fKey = key;
101 if (key.fHasBlur) {
102 fBlurRec = blurRec;
103 }
104 if (key.fStyle != SkPaint::kFill_Style) {
105 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
106 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
107 fStrokeInfo.fJoin = paint.getStrokeJoin();
108 }
109 }
110
Herb Derby86240592018-05-24 16:12:31 -0400111 static const Key& GetKey(const GrTextBlob& blob) {
joshualitt323c2eb2016-01-20 06:48:47 -0800112 return blob.fKey;
113 }
114
115 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700116 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800117 }
118
119 void operator delete(void* p) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400120 ::operator delete(p);
joshualitt323c2eb2016-01-20 06:48:47 -0800121 }
Herb Derbyb12175f2018-05-23 16:38:09 -0400122
joshualitt323c2eb2016-01-20 06:48:47 -0800123 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400124 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800125 return sk_malloc_throw(0);
126 }
127
128 void* operator new(size_t, void* p) { return p; }
joshualitt323c2eb2016-01-20 06:48:47 -0800129
130 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
131 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
132 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
133 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
134
joshualittddd22d82016-02-16 06:47:52 -0800135 int runCount() const { return fRunCount; }
136
Herb Derbydc214c22018-11-08 13:31:39 -0500137 void pushBackRun(int currRun) {
joshualitt323c2eb2016-01-20 06:48:47 -0800138 SkASSERT(currRun < fRunCount);
139 if (currRun > 0) {
140 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
141 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
142 newRun.setAsSuccessor(lastRun);
143 }
144 }
145
146 // sets the last subrun of runIndex to use distance field text
Brian Salomon5c6ac642017-12-19 11:09:32 -0500147 void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800148 Run& run = fRuns[runIndex];
149 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
150 subRun.setUseLCDText(hasLCD);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400151 subRun.setAntiAliased(isAntiAlias);
joshualitt323c2eb2016-01-20 06:48:47 -0800152 subRun.setDrawAsDistanceFields();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500153 subRun.setHasWCoord(hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800154 }
155
Jim Van Verthb515ae72018-05-23 16:44:55 -0400156 // sets the last subrun of runIndex to use w values
157 void setSubRunHasW(int runIndex, bool hasWCoord) {
158 Run& run = fRuns[runIndex];
159 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
160 subRun.setHasWCoord(hasWCoord);
161 }
162
Jim Van Verth54d9c882018-02-08 16:14:48 -0500163 void setRunPaintFlags(int runIndex, uint16_t paintFlags) {
164 fRuns[runIndex].fPaintFlags = paintFlags & Run::kPaintFlagsMask;
Jim Van Verth89737de2018-02-06 21:30:20 +0000165 }
166
joshualitt323c2eb2016-01-20 06:48:47 -0800167 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
168 // we init fMaxMinScale and fMinMaxScale in the constructor
169 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
170 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
171 }
172
173 // inits the override descriptor on the current run. All following subruns must use this
174 // descriptor
175 void initOverride(int runIndex) {
176 Run& run = fRuns[runIndex];
177 // Push back a new subrun to fill and set the override descriptor
178 run.push_back();
179 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
180 }
181
Herb Derby526819d2018-03-09 12:51:12 -0500182 SkExclusiveStrikePtr setupCache(int runIndex,
Herb Derbyb07aba42018-11-08 16:40:26 -0500183 const SkPaint& skPaint,
Herb Derby526819d2018-03-09 12:51:12 -0500184 const SkSurfaceProps& props,
185 SkScalerContextFlags scalerContextFlags,
Herb Derbyb07aba42018-11-08 16:40:26 -0500186 const SkMatrix& viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800187
188 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
189 // as a path.
190 void appendGlyph(int runIndex,
191 const SkRect& positions,
Brian Osmancf860852018-10-31 14:04:39 -0400192 const SkPMColor4f& color,
Herb Derbycf370b12018-09-19 14:42:45 -0400193 const sk_sp<GrTextStrike>& strike,
Herb Derbyae64e492018-08-06 14:58:25 -0400194 GrGlyph* glyph, bool preTransformed);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500195
196 // Appends a glyph to the blob as a path only.
197 void appendPathGlyph(int runIndex, const SkPath& path,
198 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
joshualitt323c2eb2016-01-20 06:48:47 -0800199
Jim Van Verthb515ae72018-05-23 16:44:55 -0400200 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800201 switch (maskFormat) {
202 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400203 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800204 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400205 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800206 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400207 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800208 return kLCDTextVASize;
209 }
210 }
211
Herb Derby0edb2142018-10-16 17:04:11 -0400212 bool mustRegenerate(const SkPaint&, bool, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800213 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
214
Herb Derbyc1b482c2018-08-09 15:02:27 -0400215 void flush(GrTextTarget*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500216 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400217 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400218 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800219
joshualitt8e0ef292016-02-19 14:13:03 -0800220 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
Jim Van Verth70276912018-06-01 13:46:46 -0400221 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
222 bool needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800223 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
224 // into device space.
225 // We handle vertex bounds differently for distance field text and bitmap text because
226 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
227 // from one blob then we are going to pay the price here of mapping the rect for each run.
228 const Run& run = fRuns[runIndex];
229 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
230 *outBounds = subRun.vertexBounds();
Jim Van Verth70276912018-06-01 13:46:46 -0400231 if (needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800232 // Distance field text is positioned with the (X,Y) as part of the glyph position,
233 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800234 outBounds->offset(x - fInitialX, y - fInitialY);
235 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800236 } else {
237 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
238 // device space.
239 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
240
241 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
242
joshualitt8e0ef292016-02-19 14:13:03 -0800243 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800244
joshualitt8e0ef292016-02-19 14:13:03 -0800245 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800246 boundsMatrix.mapRect(outBounds);
247
248 // Due to floating point numerical inaccuracies, we have to round out here
249 outBounds->roundOut(outBounds);
250 }
251 }
252
joshualitt323c2eb2016-01-20 06:48:47 -0800253 // position + local coord
254 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400255 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800256 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500257 static const size_t kGrayTextDFPerspectiveVASize =
258 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800259 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500260 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800261 static const int kVerticesPerGlyph = 4;
262
Herb Derby86240592018-05-24 16:12:31 -0400263 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800264
265 // The color here is the GrPaint color, and it is used to determine whether we
266 // have to regenerate LCD text blobs.
267 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400268 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
269 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400270 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800271 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800272 }
273
joshualitt7481e752016-01-22 06:08:48 -0800274 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
275 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800276 }
277
joshualitt92303772016-02-10 11:55:52 -0800278 const Key& key() const { return fKey; }
279
Herb Derbyb12175f2018-05-23 16:38:09 -0400280 size_t size() const { return fSize; }
281
Herb Derby86240592018-05-24 16:12:31 -0400282 ~GrTextBlob() {
joshualitt92303772016-02-10 11:55:52 -0800283 for (int i = 0; i < fRunCount; i++) {
284 fRuns[i].~Run();
285 }
286 }
287
joshualittbc811112016-02-11 12:42:02 -0800288 ////////////////////////////////////////////////////////////////////////////////////////////////
289 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400290 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400291 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Osmancf860852018-10-31 14:04:39 -0400292 const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400293 const SkSurfaceProps&, const GrDistanceFieldAdjustTable*,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400294 GrTextTarget*);
joshualittbc811112016-02-11 12:42:02 -0800295
joshualitt323c2eb2016-01-20 06:48:47 -0800296private:
Herb Derby86240592018-05-24 16:12:31 -0400297 GrTextBlob()
joshualitt92303772016-02-10 11:55:52 -0800298 : fMaxMinScale(-SK_ScalarMax)
299 , fMinMaxScale(SK_ScalarMax)
300 , fTextType(0) {}
301
joshualitt323c2eb2016-01-20 06:48:47 -0800302
joshualitt8e0ef292016-02-19 14:13:03 -0800303 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800304 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
305 // these numbers. When blobs are reused with new matrices, we need to return to model space so
306 // we can update the vertex bounds appropriately.
307 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800308 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800309 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
310 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800311 }
joshualitt8e0ef292016-02-19 14:13:03 -0800312 fInitialX = x;
313 fInitialY = y;
314
315 // make sure all initial subruns have the correct VM and X/Y applied
316 for (int i = 0; i < fRunCount; i++) {
317 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
318 }
joshualitt7481e752016-01-22 06:08:48 -0800319 }
320
joshualitt374b2f72015-07-21 08:05:03 -0700321 /*
322 * Each Run inside of the blob can have its texture coordinates regenerated if required.
323 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
324 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
325 * this at a more fine grained level, but its not clear if this is worth it, as evictions
326 * should be fairly rare.
327 *
328 * One additional point, each run can contain glyphs with any of the three mask formats.
329 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
330 * a new subrun each time the mask format changes in a run. In theory, a run can have as
331 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
332 * practice, the vast majority of runs have only a single subrun.
333 *
Herb Derby26cbe512018-05-24 14:39:01 -0400334 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500335 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
336 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700337 * can flush in this manner, so we always allocate vertices for the run, regardless of
338 * whether or not it is too large. The benefit of this strategy is that we can always reuse
339 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500340 * 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 -0700341 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
342 * would greatly increase the memory of these cached items.
343 */
344 struct Run {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500345 Run() : fPaintFlags(0)
346 , fInitialized(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700347 // To ensure we always have one subrun, we push back a fresh run here
348 fSubRunInfo.push_back();
349 }
350 struct SubRunInfo {
351 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500352 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
353 , fVertexStartIndex(0)
354 , fVertexEndIndex(0)
355 , fGlyphStartIndex(0)
356 , fGlyphEndIndex(0)
357 , fColor(GrColor_ILLEGAL)
358 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400359 , fFlags(0) {
Mike Reed274218e2018-01-08 15:05:02 -0500360 fVertexBounds = SkRectPriv::MakeLargestInverted();
joshualitt7481e752016-01-22 06:08:48 -0800361 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700362 SubRunInfo(const SubRunInfo& that)
363 : fBulkUseToken(that.fBulkUseToken)
364 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800365 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800366 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700367 , fAtlasGeneration(that.fAtlasGeneration)
368 , fVertexStartIndex(that.fVertexStartIndex)
369 , fVertexEndIndex(that.fVertexEndIndex)
370 , fGlyphStartIndex(that.fGlyphStartIndex)
371 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800372 , fX(that.fX)
373 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800374 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700375 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400376 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700377 }
joshualitt3660d532015-12-07 11:32:50 -0800378
joshualitt18b072d2015-12-07 12:26:12 -0800379 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800380 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500381 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500382 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
383 GrTextStrike* strike() const { return fStrike.get(); }
384 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
joshualitt3660d532015-12-07 11:32:50 -0800385
386 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
387 uint64_t atlasGeneration() const { return fAtlasGeneration; }
388
389 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800390 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800391 size_t vertexEndIndex() const { return fVertexEndIndex; }
392 void appendVertices(size_t vertexStride) {
393 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
394 }
395
396 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800397 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800398 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
399 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800400 void setColor(GrColor color) { fColor = color; }
401 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800402 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
403 GrMaskFormat maskFormat() const { return fMaskFormat; }
404
joshualitt18b072d2015-12-07 12:26:12 -0800405 void setAsSuccessor(const SubRunInfo& prev) {
406 fGlyphStartIndex = prev.glyphEndIndex();
407 fGlyphEndIndex = prev.glyphEndIndex();
408
409 fVertexStartIndex = prev.vertexEndIndex();
410 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800411
412 // copy over viewmatrix settings
413 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800414 }
415
joshualitt7481e752016-01-22 06:08:48 -0800416 const SkRect& vertexBounds() const { return fVertexBounds; }
417 void joinGlyphBounds(const SkRect& glyphBounds) {
418 fVertexBounds.joinNonEmptyArg(glyphBounds);
419 }
420
joshualitt8e0ef292016-02-19 14:13:03 -0800421 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
422 fCurrentViewMatrix = viewMatrix;
423 fX = x;
424 fY = y;
425 }
426
427 // This function assumes the translation will be applied before it is called again
428 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500429 SkScalar* transX, SkScalar* transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800430
joshualitt3660d532015-12-07 11:32:50 -0800431 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400432 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
433 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
434 void setUseLCDText(bool useLCDText) {
435 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
436 }
437 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
438 void setAntiAliased(bool antiAliased) {
439 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
440 }
441 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500442 void setHasWCoord(bool hasW) {
443 fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag;
444 }
445 bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400446 void setNeedsTransform(bool needsTransform) {
447 fFlags = needsTransform ? (fFlags | kNeedsTransform_Flag)
448 : fFlags & ~kNeedsTransform_Flag;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500449 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400450 bool needsTransform() const { return SkToBool(fFlags & kNeedsTransform_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800451
452 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400453 enum Flag {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500454 kDrawAsSDF_Flag = 0x01,
455 kUseLCDText_Flag = 0x02,
456 kAntiAliased_Flag = 0x04,
457 kHasWCoord_Flag = 0x08,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400458 kNeedsTransform_Flag = 0x10
Jim Van Verth90e89b32017-07-06 16:36:55 -0400459 };
460
Brian Salomon2ee084e2016-12-16 18:59:19 -0500461 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500462 sk_sp<GrTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800463 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800464 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700465 uint64_t fAtlasGeneration;
466 size_t fVertexStartIndex;
467 size_t fVertexEndIndex;
468 uint32_t fGlyphStartIndex;
469 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800470 SkScalar fX;
471 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800472 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700473 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400474 uint32_t fFlags;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400475 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700476
477 SubRunInfo& push_back() {
478 // Forward glyph / vertex information to seed the new sub run
joshualitt374b2f72015-07-21 08:05:03 -0700479 SubRunInfo& newSubRun = fSubRunInfo.push_back();
joshualitt18b072d2015-12-07 12:26:12 -0800480 const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700481
joshualitt18b072d2015-12-07 12:26:12 -0800482 newSubRun.setAsSuccessor(prevSubRun);
joshualitt374b2f72015-07-21 08:05:03 -0700483 return newSubRun;
484 }
485 static const int kMinSubRuns = 1;
Hal Canary144caf52016-11-07 17:57:18 -0500486 sk_sp<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700487 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
488 SkAutoDescriptor fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700489
490 // Effects from the paint that are used to build a SkScalerContext.
491 sk_sp<SkPathEffect> fPathEffect;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700492 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
Jim Van Verth54d9c882018-02-08 16:14:48 -0500499
500 // Any glyphs that can't be rendered with the base or override descriptor
501 // are rendered as paths
502 struct PathGlyph {
503 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
504 : fPath(path)
505 , fX(x)
506 , fY(y)
507 , fScale(scale)
508 , fPreTransformed(preXformed) {}
509 SkPath fPath;
510 SkScalar fX;
511 SkScalar fY;
512 SkScalar fScale;
513 bool fPreTransformed;
514 };
515
516 SkTArray<PathGlyph> fPathGlyphs;
517
518 struct {
519 unsigned fPaintFlags : 16; // needed mainly for rendering paths
520 bool fInitialized : 1;
521 };
522 // the only flags we need to set
523 static constexpr auto kPaintFlagsMask = SkPaint::kAntiAlias_Flag;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400524 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700525
Brian Salomonf18b1d82017-10-27 11:30:49 -0400526 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400527 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400528 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400529 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps&,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400530 const GrDistanceFieldAdjustTable*, GrTextTarget*);
joshualitt323c2eb2016-01-20 06:48:47 -0800531
joshualitt374b2f72015-07-21 08:05:03 -0700532 struct StrokeInfo {
533 SkScalar fFrameWidth;
534 SkScalar fMiterLimit;
535 SkPaint::Join fJoin;
536 };
537
538 enum TextType {
539 kHasDistanceField_TextType = 0x1,
540 kHasBitmap_TextType = 0x2,
541 };
542
543 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500544 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700545 GrGlyph** fGlyphs;
546 Run* fRuns;
Mike Reed80747ef2018-01-23 15:29:32 -0500547 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700548 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700549 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800550 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800551 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800552 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400553 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800554 SkScalar fInitialX;
555 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700556
557 // We can reuse distance field text, but only if the new viewmatrix would not result in
558 // a mip change. Because there can be multiple runs in a blob, we track the overall
559 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
560 SkScalar fMaxMinScale;
561 SkScalar fMinMaxScale;
562 int fRunCount;
563 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700564};
565
Brian Salomon18923f92017-11-06 16:26:02 -0500566/**
567 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
568 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
569 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
570 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
571 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
572 * entire sub run has been completed.
573 */
Herb Derby86240592018-05-24 16:12:31 -0400574class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500575public:
576 /**
577 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
578 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
579 * SkGlyphCache.
580 */
Herb Derby86240592018-05-24 16:12:31 -0400581 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500582 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500583 GrDeferredUploadTarget*, GrGlyphCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400584 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500585
586 struct Result {
587 /**
588 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
589 * draws and call regenerate() again.
590 */
591 bool fFinished = true;
592
593 /**
594 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
595 * fType is kFinished.
596 */
597 int fGlyphsRegenerated = 0;
598
599 /**
600 * Pointer where the caller finds the first regenerated vertex.
601 */
602 const char* fFirstVertex;
603 };
604
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500605 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500606
607private:
608 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500609 bool doRegen(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500610
Robert Phillips4bc70112018-03-01 10:24:02 -0500611 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500612 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400613 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500614 GrDeferredUploadTarget* fUploadTarget;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500615 GrGlyphCache* fGlyphCache;
616 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500617 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500618 Run* fRun;
619 Run::SubRunInfo* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500620 GrColor fColor;
621 SkScalar fTransX;
622 SkScalar fTransY;
623
624 uint32_t fRegenFlags = 0;
625 int fCurrGlyph = 0;
626 bool fBrokenRun = false;
627};
628
Herb Derby86240592018-05-24 16:12:31 -0400629#endif // GrTextBlob_DEFINED