blob: 73b71299e6705aa17a08312dc160ac794811119d [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,
Herb Derbydc214c22018-11-08 13:31:39 -050062 SkScalerContextFlags scalerContextFlags,
63 const SkMatrix& viewMatrix,
64 const SkSurfaceProps& props,
65 const SkGlyphRunList& glyphRunList,
66 SkGlyphRunListPainter* glyphPainter);
67
Herb Derby5424a5e2018-11-14 12:04:38 -050068 static sk_sp<GrTextBlob> Make(int glyphCount, int runCount, GrColor color);
joshualitt323c2eb2016-01-20 06:48:47 -080069
Brian Salomon5c6ac642017-12-19 11:09:32 -050070 /**
71 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
72 * If we ever change that then the key must contain the perspectiveness when there are distance
73 * fields as perspective distance field use 3 component vertex positions and non-perspective
74 * uses 2.
75 */
joshualitt323c2eb2016-01-20 06:48:47 -080076 struct Key {
77 Key() {
78 sk_bzero(this, sizeof(Key));
79 }
80 uint32_t fUniqueID;
81 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
82 // Each color is assigned to on of a fixed number of buckets based on its
83 // luminance. For each luminance bucket there is a "canonical color" that
84 // represents the bucket. This functionality is currently only supported for A8
85 SkColor fCanonicalColor;
86 SkPaint::Style fStyle;
87 SkPixelGeometry fPixelGeometry;
88 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070089 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080090
91 bool operator==(const Key& other) const {
92 return 0 == memcmp(this, &other, sizeof(Key));
93 }
94 };
95
Herb Derby86240592018-05-24 16:12:31 -040096 void setupKey(const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050097 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -080098 const SkPaint& paint) {
99 fKey = key;
100 if (key.fHasBlur) {
101 fBlurRec = blurRec;
102 }
103 if (key.fStyle != SkPaint::kFill_Style) {
104 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
105 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
106 fStrokeInfo.fJoin = paint.getStrokeJoin();
107 }
108 }
109
Herb Derby86240592018-05-24 16:12:31 -0400110 static const Key& GetKey(const GrTextBlob& blob) {
joshualitt323c2eb2016-01-20 06:48:47 -0800111 return blob.fKey;
112 }
113
114 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700115 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800116 }
117
118 void operator delete(void* p) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400119 ::operator delete(p);
joshualitt323c2eb2016-01-20 06:48:47 -0800120 }
Herb Derbyb12175f2018-05-23 16:38:09 -0400121
joshualitt323c2eb2016-01-20 06:48:47 -0800122 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400123 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800124 return sk_malloc_throw(0);
125 }
126
127 void* operator new(size_t, void* p) { return p; }
joshualitt323c2eb2016-01-20 06:48:47 -0800128
129 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
130 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
131 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
132 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
133
Herb Derby2bb343c2018-11-08 15:03:48 -0500134 int runCountLimit() const { return fRunCountLimit; }
joshualittddd22d82016-02-16 06:47:52 -0800135
Herb Derby2bb343c2018-11-08 15:03:48 -0500136 Run* pushBackRun() {
137 SkASSERT(fRunCount < fRunCountLimit);
Herb Derby6dff60e2018-11-12 15:45:49 -0500138
139 // If there is more run, then connect up the subruns.
Herb Derby2bb343c2018-11-08 15:03:48 -0500140 if (fRunCount > 0) {
Herb Derby69ff8952018-11-12 11:39:12 -0500141 SubRun& newRun = fRuns[fRunCount].fSubRunInfo.back();
142 SubRun& lastRun = fRuns[fRunCount - 1].fSubRunInfo.back();
joshualitt323c2eb2016-01-20 06:48:47 -0800143 newRun.setAsSuccessor(lastRun);
144 }
joshualitt323c2eb2016-01-20 06:48:47 -0800145
Herb Derby2bb343c2018-11-08 15:03:48 -0500146 fRunCount++;
147 return &fRuns[fRunCount - 1];
Jim Van Verth89737de2018-02-06 21:30:20 +0000148 }
149
joshualitt323c2eb2016-01-20 06:48:47 -0800150 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
151 // we init fMaxMinScale and fMinMaxScale in the constructor
152 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
153 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
154 }
155
Jim Van Verthb515ae72018-05-23 16:44:55 -0400156 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800157 switch (maskFormat) {
158 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400159 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800160 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400161 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800162 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400163 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800164 return kLCDTextVASize;
165 }
166 }
167
Herb Derby0edb2142018-10-16 17:04:11 -0400168 bool mustRegenerate(const SkPaint&, bool, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800169 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
170
Herb Derbyc1b482c2018-08-09 15:02:27 -0400171 void flush(GrTextTarget*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500172 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400173 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400174 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800175
joshualitt8e0ef292016-02-19 14:13:03 -0800176 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
Jim Van Verth70276912018-06-01 13:46:46 -0400177 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
178 bool needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800179 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
180 // into device space.
181 // We handle vertex bounds differently for distance field text and bitmap text because
182 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
183 // from one blob then we are going to pay the price here of mapping the rect for each run.
184 const Run& run = fRuns[runIndex];
Herb Derby69ff8952018-11-12 11:39:12 -0500185 const SubRun& subRun = run.fSubRunInfo[subRunIndex];
joshualittbc811112016-02-11 12:42:02 -0800186 *outBounds = subRun.vertexBounds();
Jim Van Verth70276912018-06-01 13:46:46 -0400187 if (needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800188 // Distance field text is positioned with the (X,Y) as part of the glyph position,
189 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800190 outBounds->offset(x - fInitialX, y - fInitialY);
191 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800192 } else {
193 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
194 // device space.
195 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
196
197 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
198
joshualitt8e0ef292016-02-19 14:13:03 -0800199 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800200
joshualitt8e0ef292016-02-19 14:13:03 -0800201 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800202 boundsMatrix.mapRect(outBounds);
203
204 // Due to floating point numerical inaccuracies, we have to round out here
205 outBounds->roundOut(outBounds);
206 }
207 }
208
joshualitt323c2eb2016-01-20 06:48:47 -0800209 // position + local coord
210 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400211 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800212 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500213 static const size_t kGrayTextDFPerspectiveVASize =
214 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800215 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500216 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800217 static const int kVerticesPerGlyph = 4;
218
Herb Derby86240592018-05-24 16:12:31 -0400219 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800220
221 // The color here is the GrPaint color, and it is used to determine whether we
222 // have to regenerate LCD text blobs.
223 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400224 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
225 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400226 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800227 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800228 }
229
joshualitt7481e752016-01-22 06:08:48 -0800230 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
231 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800232 }
233
joshualitt92303772016-02-10 11:55:52 -0800234 const Key& key() const { return fKey; }
235
Herb Derbyb12175f2018-05-23 16:38:09 -0400236 size_t size() const { return fSize; }
237
Herb Derby86240592018-05-24 16:12:31 -0400238 ~GrTextBlob() {
Herb Derby2bb343c2018-11-08 15:03:48 -0500239 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt92303772016-02-10 11:55:52 -0800240 fRuns[i].~Run();
241 }
242 }
243
joshualittbc811112016-02-11 12:42:02 -0800244 ////////////////////////////////////////////////////////////////////////////////////////////////
245 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400246 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400247 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Osmancf860852018-10-31 14:04:39 -0400248 const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400249 const SkSurfaceProps&, const GrDistanceFieldAdjustTable*,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400250 GrTextTarget*);
Herbert Derbyb2fc3b82019-01-02 13:37:14 -0500251 struct BothCaches {
252 SkExclusiveStrikePtr skCache;
253 sk_sp<GrTextStrike> grCache;
254 };
joshualittbc811112016-02-11 12:42:02 -0800255
joshualitt323c2eb2016-01-20 06:48:47 -0800256private:
Herb Derby86240592018-05-24 16:12:31 -0400257 GrTextBlob()
joshualitt92303772016-02-10 11:55:52 -0800258 : fMaxMinScale(-SK_ScalarMax)
259 , fMinMaxScale(SK_ScalarMax)
260 , fTextType(0) {}
261
joshualitt8e0ef292016-02-19 14:13:03 -0800262 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800263 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
264 // these numbers. When blobs are reused with new matrices, we need to return to model space so
265 // we can update the vertex bounds appropriately.
266 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800267 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800268 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
269 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800270 }
joshualitt8e0ef292016-02-19 14:13:03 -0800271 fInitialX = x;
272 fInitialY = y;
273
274 // make sure all initial subruns have the correct VM and X/Y applied
Herb Derby2bb343c2018-11-08 15:03:48 -0500275 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt8e0ef292016-02-19 14:13:03 -0800276 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
277 }
joshualitt7481e752016-01-22 06:08:48 -0800278 }
279
Herb Derby69ff8952018-11-12 11:39:12 -0500280 class SubRun {
281 public:
Herb Derby5424a5e2018-11-14 12:04:38 -0500282 SubRun(Run* run, const SkAutoDescriptor& desc, GrColor color)
283 : fColor{color}
284 , fRun{run}
Herb Derby317adf72018-11-16 17:29:29 -0500285 , fDesc{desc} {}
Herb Derby69ff8952018-11-12 11:39:12 -0500286
Herb Derby5424a5e2018-11-14 12:04:38 -0500287 // When used with emplace_back, this constructs a SubRun from the last SubRun in an array.
288 //SubRun(SkSTArray<1, SubRun>* subRunList)
289 // : fColor{subRunList->fromBack(1).fColor} { }
290
Herb Derby438ea542018-12-19 18:25:11 -0500291 void appendGlyph(GrGlyph* glyph, SkRect dstRect);
Herb Derby96a17092018-11-12 12:45:21 -0500292
Herb Derby69ff8952018-11-12 11:39:12 -0500293 // TODO when this object is more internal, drop the privacy
294 void resetBulkUseToken() { fBulkUseToken.reset(); }
295 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
296 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
297 GrTextStrike* strike() const { return fStrike.get(); }
298 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
299
300 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
301 uint64_t atlasGeneration() const { return fAtlasGeneration; }
302
303 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
304 size_t vertexStartIndex() const { return fVertexStartIndex; }
305 size_t vertexEndIndex() const { return fVertexEndIndex; }
Herb Derby69ff8952018-11-12 11:39:12 -0500306
307 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
308 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
309 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
Herb Derby69ff8952018-11-12 11:39:12 -0500310 void setColor(GrColor color) { fColor = color; }
311 GrColor color() const { return fColor; }
312 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
313 GrMaskFormat maskFormat() const { return fMaskFormat; }
314
315 void setAsSuccessor(const SubRun& prev) {
316 fGlyphStartIndex = prev.glyphEndIndex();
Herb Derby6dff60e2018-11-12 15:45:49 -0500317 fGlyphEndIndex = fGlyphStartIndex;
Herb Derby69ff8952018-11-12 11:39:12 -0500318
319 fVertexStartIndex = prev.vertexEndIndex();
Herb Derby6dff60e2018-11-12 15:45:49 -0500320 fVertexEndIndex = fVertexStartIndex;
Herb Derby69ff8952018-11-12 11:39:12 -0500321
322 // copy over viewmatrix settings
323 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
324 }
325
326 const SkRect& vertexBounds() const { return fVertexBounds; }
327 void joinGlyphBounds(const SkRect& glyphBounds) {
328 fVertexBounds.joinNonEmptyArg(glyphBounds);
329 }
330
331 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
332 fCurrentViewMatrix = viewMatrix;
333 fX = x;
334 fY = y;
335 }
336
337 // This function assumes the translation will be applied before it is called again
338 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
339 SkScalar* transX, SkScalar* transY);
340
341 // df properties
herbc5df7cb2018-11-15 17:08:26 -0500342 void setDrawAsDistanceFields() { fFlags.drawAsSdf = true; }
343 bool drawAsDistanceFields() const { return fFlags.drawAsSdf; }
344 void setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
345 bool hasUseLCDText() const { return fFlags.useLCDText; }
346 void setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
347 bool isAntiAliased() const { return fFlags.antiAliased; }
348 void setHasWCoord(bool hasW) { fFlags.hasWCoord = hasW; }
349 bool hasWCoord() const { return fFlags.hasWCoord; }
350 void setNeedsTransform(bool needsTransform) { fFlags.needsTransform = needsTransform; }
351 bool needsTransform() const { return fFlags.needsTransform; }
Herb Derby5f7b0142018-12-14 16:47:45 -0500352 void setFallback() { fFlags.argbFallback = true; }
353 bool isFallback() { return fFlags.argbFallback; }
Herb Derby317adf72018-11-16 17:29:29 -0500354
355 const SkDescriptor* desc() const { return fDesc.getDesc(); }
Herb Derby69ff8952018-11-12 11:39:12 -0500356
357 private:
Herb Derby69ff8952018-11-12 11:39:12 -0500358 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
359 sk_sp<GrTextStrike> fStrike;
360 SkMatrix fCurrentViewMatrix;
Herb Derby6dff60e2018-11-12 15:45:49 -0500361 SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
362 uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
363 size_t fVertexStartIndex{0};
364 size_t fVertexEndIndex{0};
365 uint32_t fGlyphStartIndex{0};
366 uint32_t fGlyphEndIndex{0};
Herb Derby69ff8952018-11-12 11:39:12 -0500367 SkScalar fX;
368 SkScalar fY;
Herb Derby6dff60e2018-11-12 15:45:49 -0500369 GrColor fColor{GrColor_ILLEGAL};
370 GrMaskFormat fMaskFormat{kA8_GrMaskFormat};
herbc5df7cb2018-11-15 17:08:26 -0500371 struct {
372 bool drawAsSdf:1;
373 bool useLCDText:1;
374 bool antiAliased:1;
375 bool hasWCoord:1;
376 bool needsTransform:1;
Herb Derby317adf72018-11-16 17:29:29 -0500377 bool argbFallback:1;
378 } fFlags{false, false, false, false, false, false};
Herb Derbyf7d5d742018-11-16 13:24:32 -0500379 Run* const fRun;
Herb Derby317adf72018-11-16 17:29:29 -0500380 const SkAutoDescriptor& fDesc;
Herb Derby69ff8952018-11-12 11:39:12 -0500381 }; // SubRunInfo
382
joshualitt374b2f72015-07-21 08:05:03 -0700383 /*
384 * Each Run inside of the blob can have its texture coordinates regenerated if required.
385 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
386 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
387 * this at a more fine grained level, but its not clear if this is worth it, as evictions
388 * should be fairly rare.
389 *
390 * One additional point, each run can contain glyphs with any of the three mask formats.
391 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
392 * a new subrun each time the mask format changes in a run. In theory, a run can have as
393 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
394 * practice, the vast majority of runs have only a single subrun.
395 *
Herb Derby26cbe512018-05-24 14:39:01 -0400396 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500397 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
398 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700399 * can flush in this manner, so we always allocate vertices for the run, regardless of
400 * whether or not it is too large. The benefit of this strategy is that we can always reuse
401 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500402 * 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 -0700403 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
404 * would greatly increase the memory of these cached items.
405 */
406 struct Run {
Herb Derby5424a5e2018-11-14 12:04:38 -0500407 explicit Run(GrTextBlob* blob, GrColor color)
408 : fBlob{blob}, fColor{color} {
joshualitt374b2f72015-07-21 08:05:03 -0700409 // To ensure we always have one subrun, we push back a fresh run here
Herb Derby5424a5e2018-11-14 12:04:38 -0500410 fSubRunInfo.emplace_back(this, fDescriptor, color);
joshualitt374b2f72015-07-21 08:05:03 -0700411 }
joshualitt374b2f72015-07-21 08:05:03 -0700412
Herb Derby2bb343c2018-11-08 15:03:48 -0500413 // sets the last subrun of runIndex to use w values
414 void setSubRunHasW(bool hasWCoord) {
Herb Derby69ff8952018-11-12 11:39:12 -0500415 SubRun& subRun = this->fSubRunInfo.back();
Herb Derby2bb343c2018-11-08 15:03:48 -0500416 subRun.setHasWCoord(hasWCoord);
417 }
418
419 // inits the override descriptor on the current run. All following subruns must use this
420 // descriptor
Herb Derby317adf72018-11-16 17:29:29 -0500421 SubRun* initARGBFallback() {
422 fARGBFallbackDescriptor.reset(new SkAutoDescriptor{});
Herb Derby2bb343c2018-11-08 15:03:48 -0500423 // Push back a new subrun to fill and set the override descriptor
Herb Derby5424a5e2018-11-14 12:04:38 -0500424 SubRun* subRun = this->pushBackSubRun(*fARGBFallbackDescriptor, fColor);
Herb Derby317adf72018-11-16 17:29:29 -0500425 subRun->setMaskFormat(kARGB_GrMaskFormat);
426 subRun->setFallback();
427 return subRun;
Herb Derby2bb343c2018-11-08 15:03:48 -0500428 }
429
430 // Appends a glyph to the blob as a path only.
431 void appendPathGlyph(
432 const SkPath& path, SkPoint position, SkScalar scale, bool preTransformed);
433
Herb Derby438ea542018-12-19 18:25:11 -0500434 // Append a glyph to the sub run taking care to switch the glyph if needed.
435 void switchSubRunIfNeededAndAppendGlyph(GrGlyph* glyph,
436 const sk_sp<GrTextStrike>& strike,
437 const SkRect& destRect,
438 bool needsTransform);
439
440 // Used when the glyph in the cache has the CTM already applied, therefore no transform
441 // is needed during rendering.
442 void appendDeviceSpaceGlyph(const sk_sp<GrTextStrike>& strike,
443 const SkGlyph& skGlyph,
444 SkPoint origin);
445
446 // The glyph is oriented upright in the cache and needs to be transformed onto the screen.
447 void appendSourceSpaceGlyph(const sk_sp<GrTextStrike>& strike,
448 const SkGlyph& skGlyph,
449 SkPoint origin,
450 SkScalar textScale);
Herb Derby2bb343c2018-11-08 15:03:48 -0500451
Herbert Derby86dbae92019-01-03 13:10:59 -0500452 BothCaches lookupCache(const SkPaint& skPaint,
453 const SkFont& skFont,
454 const SkSurfaceProps& props,
455 SkScalerContextFlags scalerContextFlags,
456 const SkMatrix& viewMatrix,
457 GrGlyphCache* grGlyphCache);
458
459 void setupFont(const SkPaint& skPaint,
460 const SkFont& skFont,
461 const SkDescriptor& skCache);
Herb Derby2bb343c2018-11-08 15:03:48 -0500462
Mike Reed48b958b2018-12-03 13:09:02 -0500463 void setRunFontAntiAlias(bool aa) {
464 fAntiAlias = aa;
Herb Derby2bb343c2018-11-08 15:03:48 -0500465 }
466
467 // sets the last subrun of runIndex to use distance field text
468 void setSubRunHasDistanceFields(bool hasLCD, bool isAntiAlias, bool hasWCoord) {
Herb Derby69ff8952018-11-12 11:39:12 -0500469 SubRun& subRun = fSubRunInfo.back();
Herb Derby2bb343c2018-11-08 15:03:48 -0500470 subRun.setUseLCDText(hasLCD);
471 subRun.setAntiAliased(isAntiAlias);
472 subRun.setDrawAsDistanceFields();
473 subRun.setHasWCoord(hasWCoord);
474 }
475
Herb Derby5424a5e2018-11-14 12:04:38 -0500476 SubRun* pushBackSubRun(const SkAutoDescriptor& desc, GrColor color) {
joshualitt374b2f72015-07-21 08:05:03 -0700477 // Forward glyph / vertex information to seed the new sub run
Herb Derby5424a5e2018-11-14 12:04:38 -0500478 SubRun& newSubRun = fSubRunInfo.emplace_back(this, desc, color);
479
Herb Derby69ff8952018-11-12 11:39:12 -0500480 const SubRun& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700481
Herb Derby5424a5e2018-11-14 12:04:38 -0500482 // Forward glyph / vertex information to seed the new sub run
joshualitt18b072d2015-12-07 12:26:12 -0800483 newSubRun.setAsSuccessor(prevSubRun);
Herb Derby317adf72018-11-16 17:29:29 -0500484 return &newSubRun;
joshualitt374b2f72015-07-21 08:05:03 -0700485 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500486
487 // Any glyphs that can't be rendered with the base or override descriptor
488 // are rendered as paths
489 struct PathGlyph {
490 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
491 : fPath(path)
492 , fX(x)
493 , fY(y)
494 , fScale(scale)
495 , fPreTransformed(preXformed) {}
496 SkPath fPath;
497 SkScalar fX;
498 SkScalar fY;
499 SkScalar fScale;
500 bool fPreTransformed;
501 };
502
Herb Derbyf7d5d742018-11-16 13:24:32 -0500503
504 sk_sp<SkTypeface> fTypeface;
505 SkSTArray<1, SubRun> fSubRunInfo;
506 SkAutoDescriptor fDescriptor;
507
508 // Effects from the paint that are used to build a SkScalerContext.
509 sk_sp<SkPathEffect> fPathEffect;
510 sk_sp<SkMaskFilter> fMaskFilter;
511
512 // Distance field text cannot draw coloremoji, and so has to fall back. However,
513 // though the distance field text and the coloremoji may share the same run, they
Herb Derby317adf72018-11-16 17:29:29 -0500514 // will have different descriptors. If fARGBFallbackDescriptor is non-nullptr, then it
Herb Derbyf7d5d742018-11-16 13:24:32 -0500515 // will be used in place of the run's descriptor to regen texture coords
Herb Derby317adf72018-11-16 17:29:29 -0500516 std::unique_ptr<SkAutoDescriptor> fARGBFallbackDescriptor;
Herb Derbyf7d5d742018-11-16 13:24:32 -0500517
Jim Van Verth54d9c882018-02-08 16:14:48 -0500518 SkTArray<PathGlyph> fPathGlyphs;
519
Mike Reed48b958b2018-12-03 13:09:02 -0500520 bool fAntiAlias{false}; // needed mainly for rendering paths
Herb Derbyf7d5d742018-11-16 13:24:32 -0500521 bool fInitialized{false};
522
523 GrTextBlob* const fBlob;
Herb Derby5424a5e2018-11-14 12:04:38 -0500524 GrColor fColor;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400525 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700526
Brian Salomonf18b1d82017-10-27 11:30:49 -0400527 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Herb Derby69ff8952018-11-12 11:39:12 -0500528 const SubRun& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400529 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400530 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps&,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400531 const GrDistanceFieldAdjustTable*, GrTextTarget*);
joshualitt323c2eb2016-01-20 06:48:47 -0800532
joshualitt374b2f72015-07-21 08:05:03 -0700533 struct StrokeInfo {
534 SkScalar fFrameWidth;
535 SkScalar fMiterLimit;
536 SkPaint::Join fJoin;
537 };
538
539 enum TextType {
540 kHasDistanceField_TextType = 0x1,
541 kHasBitmap_TextType = 0x2,
542 };
543
544 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500545 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700546 GrGlyph** fGlyphs;
547 Run* fRuns;
Mike Reed80747ef2018-01-23 15:29:32 -0500548 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700549 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700550 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800551 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800552 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800553 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400554 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800555 SkScalar fInitialX;
556 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700557
558 // We can reuse distance field text, but only if the new viewmatrix would not result in
559 // a mip change. Because there can be multiple runs in a blob, we track the overall
560 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
561 SkScalar fMaxMinScale;
562 SkScalar fMinMaxScale;
Herb Derby2bb343c2018-11-08 15:03:48 -0500563 int fRunCount{0};
564 int fRunCountLimit;
joshualitt374b2f72015-07-21 08:05:03 -0700565 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700566};
567
Brian Salomon18923f92017-11-06 16:26:02 -0500568/**
569 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
570 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
571 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
572 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
573 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
574 * entire sub run has been completed.
575 */
Herb Derby86240592018-05-24 16:12:31 -0400576class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500577public:
578 /**
579 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
580 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
581 * SkGlyphCache.
582 */
Herb Derby86240592018-05-24 16:12:31 -0400583 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500584 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500585 GrDeferredUploadTarget*, GrGlyphCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400586 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500587
588 struct Result {
589 /**
590 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
591 * draws and call regenerate() again.
592 */
593 bool fFinished = true;
594
595 /**
596 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
597 * fType is kFinished.
598 */
599 int fGlyphsRegenerated = 0;
600
601 /**
602 * Pointer where the caller finds the first regenerated vertex.
603 */
604 const char* fFirstVertex;
605 };
606
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500607 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500608
609private:
610 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500611 bool doRegen(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500612
Robert Phillips4bc70112018-03-01 10:24:02 -0500613 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500614 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400615 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500616 GrDeferredUploadTarget* fUploadTarget;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500617 GrGlyphCache* fGlyphCache;
618 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500619 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500620 Run* fRun;
Herb Derby69ff8952018-11-12 11:39:12 -0500621 SubRun* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500622 GrColor fColor;
623 SkScalar fTransX;
624 SkScalar fTransY;
625
626 uint32_t fRegenFlags = 0;
627 int fCurrGlyph = 0;
628 bool fBrokenRun = false;
629};
630
Herb Derby86240592018-05-24 16:12:31 -0400631#endif // GrTextBlob_DEFINED