blob: e5908feb9cb898e040e178468272e4de258cf485 [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
Herb Derby2bb343c2018-11-08 15:03:48 -0500135 int runCountLimit() const { return fRunCountLimit; }
joshualittddd22d82016-02-16 06:47:52 -0800136
Herb Derby2bb343c2018-11-08 15:03:48 -0500137 Run* pushBackRun() {
138 SkASSERT(fRunCount < fRunCountLimit);
139 if (fRunCount > 0) {
140 Run::SubRunInfo& newRun = fRuns[fRunCount].fSubRunInfo.back();
141 Run::SubRunInfo& lastRun = fRuns[fRunCount - 1].fSubRunInfo.back();
joshualitt323c2eb2016-01-20 06:48:47 -0800142 newRun.setAsSuccessor(lastRun);
143 }
joshualitt323c2eb2016-01-20 06:48:47 -0800144
Herb Derby2bb343c2018-11-08 15:03:48 -0500145 fRunCount++;
146 return &fRuns[fRunCount - 1];
Jim Van Verth89737de2018-02-06 21:30:20 +0000147 }
148
joshualitt323c2eb2016-01-20 06:48:47 -0800149 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
150 // we init fMaxMinScale and fMinMaxScale in the constructor
151 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
152 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
153 }
154
Jim Van Verthb515ae72018-05-23 16:44:55 -0400155 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800156 switch (maskFormat) {
157 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400158 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800159 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400160 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800161 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400162 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800163 return kLCDTextVASize;
164 }
165 }
166
Herb Derby0edb2142018-10-16 17:04:11 -0400167 bool mustRegenerate(const SkPaint&, bool, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800168 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
169
Herb Derbyc1b482c2018-08-09 15:02:27 -0400170 void flush(GrTextTarget*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500171 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400172 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400173 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800174
joshualitt8e0ef292016-02-19 14:13:03 -0800175 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
Jim Van Verth70276912018-06-01 13:46:46 -0400176 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
177 bool needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800178 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
179 // into device space.
180 // We handle vertex bounds differently for distance field text and bitmap text because
181 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
182 // from one blob then we are going to pay the price here of mapping the rect for each run.
183 const Run& run = fRuns[runIndex];
184 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
185 *outBounds = subRun.vertexBounds();
Jim Van Verth70276912018-06-01 13:46:46 -0400186 if (needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800187 // Distance field text is positioned with the (X,Y) as part of the glyph position,
188 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800189 outBounds->offset(x - fInitialX, y - fInitialY);
190 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800191 } else {
192 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
193 // device space.
194 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
195
196 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
197
joshualitt8e0ef292016-02-19 14:13:03 -0800198 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800199
joshualitt8e0ef292016-02-19 14:13:03 -0800200 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800201 boundsMatrix.mapRect(outBounds);
202
203 // Due to floating point numerical inaccuracies, we have to round out here
204 outBounds->roundOut(outBounds);
205 }
206 }
207
joshualitt323c2eb2016-01-20 06:48:47 -0800208 // position + local coord
209 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400210 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800211 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500212 static const size_t kGrayTextDFPerspectiveVASize =
213 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800214 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500215 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800216 static const int kVerticesPerGlyph = 4;
217
Herb Derby86240592018-05-24 16:12:31 -0400218 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800219
220 // The color here is the GrPaint color, and it is used to determine whether we
221 // have to regenerate LCD text blobs.
222 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400223 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
224 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400225 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800226 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800227 }
228
joshualitt7481e752016-01-22 06:08:48 -0800229 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
230 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800231 }
232
joshualitt92303772016-02-10 11:55:52 -0800233 const Key& key() const { return fKey; }
234
Herb Derbyb12175f2018-05-23 16:38:09 -0400235 size_t size() const { return fSize; }
236
Herb Derby86240592018-05-24 16:12:31 -0400237 ~GrTextBlob() {
Herb Derby2bb343c2018-11-08 15:03:48 -0500238 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt92303772016-02-10 11:55:52 -0800239 fRuns[i].~Run();
240 }
241 }
242
joshualittbc811112016-02-11 12:42:02 -0800243 ////////////////////////////////////////////////////////////////////////////////////////////////
244 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400245 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400246 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Osmancf860852018-10-31 14:04:39 -0400247 const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400248 const SkSurfaceProps&, const GrDistanceFieldAdjustTable*,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400249 GrTextTarget*);
joshualittbc811112016-02-11 12:42:02 -0800250
joshualitt323c2eb2016-01-20 06:48:47 -0800251private:
Herb Derby86240592018-05-24 16:12:31 -0400252 GrTextBlob()
joshualitt92303772016-02-10 11:55:52 -0800253 : fMaxMinScale(-SK_ScalarMax)
254 , fMinMaxScale(SK_ScalarMax)
255 , fTextType(0) {}
256
joshualitt323c2eb2016-01-20 06:48:47 -0800257
joshualitt8e0ef292016-02-19 14:13:03 -0800258 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800259 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
260 // these numbers. When blobs are reused with new matrices, we need to return to model space so
261 // we can update the vertex bounds appropriately.
262 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800263 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800264 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
265 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800266 }
joshualitt8e0ef292016-02-19 14:13:03 -0800267 fInitialX = x;
268 fInitialY = y;
269
270 // make sure all initial subruns have the correct VM and X/Y applied
Herb Derby2bb343c2018-11-08 15:03:48 -0500271 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt8e0ef292016-02-19 14:13:03 -0800272 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
273 }
joshualitt7481e752016-01-22 06:08:48 -0800274 }
275
joshualitt374b2f72015-07-21 08:05:03 -0700276 /*
277 * Each Run inside of the blob can have its texture coordinates regenerated if required.
278 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
279 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
280 * this at a more fine grained level, but its not clear if this is worth it, as evictions
281 * should be fairly rare.
282 *
283 * One additional point, each run can contain glyphs with any of the three mask formats.
284 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
285 * a new subrun each time the mask format changes in a run. In theory, a run can have as
286 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
287 * practice, the vast majority of runs have only a single subrun.
288 *
Herb Derby26cbe512018-05-24 14:39:01 -0400289 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500290 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
291 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700292 * can flush in this manner, so we always allocate vertices for the run, regardless of
293 * whether or not it is too large. The benefit of this strategy is that we can always reuse
294 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500295 * 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 -0700296 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
297 * would greatly increase the memory of these cached items.
298 */
299 struct Run {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500300 Run() : fPaintFlags(0)
301 , fInitialized(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700302 // To ensure we always have one subrun, we push back a fresh run here
303 fSubRunInfo.push_back();
304 }
305 struct SubRunInfo {
306 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500307 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
308 , fVertexStartIndex(0)
309 , fVertexEndIndex(0)
310 , fGlyphStartIndex(0)
311 , fGlyphEndIndex(0)
312 , fColor(GrColor_ILLEGAL)
313 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400314 , fFlags(0) {
Mike Reed274218e2018-01-08 15:05:02 -0500315 fVertexBounds = SkRectPriv::MakeLargestInverted();
joshualitt7481e752016-01-22 06:08:48 -0800316 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700317 SubRunInfo(const SubRunInfo& that)
318 : fBulkUseToken(that.fBulkUseToken)
319 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800320 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800321 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700322 , fAtlasGeneration(that.fAtlasGeneration)
323 , fVertexStartIndex(that.fVertexStartIndex)
324 , fVertexEndIndex(that.fVertexEndIndex)
325 , fGlyphStartIndex(that.fGlyphStartIndex)
326 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800327 , fX(that.fX)
328 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800329 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700330 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400331 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700332 }
joshualitt3660d532015-12-07 11:32:50 -0800333
joshualitt18b072d2015-12-07 12:26:12 -0800334 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800335 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500336 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500337 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
338 GrTextStrike* strike() const { return fStrike.get(); }
339 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
joshualitt3660d532015-12-07 11:32:50 -0800340
341 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
342 uint64_t atlasGeneration() const { return fAtlasGeneration; }
343
344 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800345 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800346 size_t vertexEndIndex() const { return fVertexEndIndex; }
347 void appendVertices(size_t vertexStride) {
348 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
349 }
350
351 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800352 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800353 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
354 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800355 void setColor(GrColor color) { fColor = color; }
356 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800357 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
358 GrMaskFormat maskFormat() const { return fMaskFormat; }
359
joshualitt18b072d2015-12-07 12:26:12 -0800360 void setAsSuccessor(const SubRunInfo& prev) {
361 fGlyphStartIndex = prev.glyphEndIndex();
362 fGlyphEndIndex = prev.glyphEndIndex();
363
364 fVertexStartIndex = prev.vertexEndIndex();
365 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800366
367 // copy over viewmatrix settings
368 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800369 }
370
joshualitt7481e752016-01-22 06:08:48 -0800371 const SkRect& vertexBounds() const { return fVertexBounds; }
372 void joinGlyphBounds(const SkRect& glyphBounds) {
373 fVertexBounds.joinNonEmptyArg(glyphBounds);
374 }
375
joshualitt8e0ef292016-02-19 14:13:03 -0800376 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
377 fCurrentViewMatrix = viewMatrix;
378 fX = x;
379 fY = y;
380 }
381
382 // This function assumes the translation will be applied before it is called again
383 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500384 SkScalar* transX, SkScalar* transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800385
joshualitt3660d532015-12-07 11:32:50 -0800386 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400387 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
388 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
389 void setUseLCDText(bool useLCDText) {
390 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
391 }
392 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
393 void setAntiAliased(bool antiAliased) {
394 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
395 }
396 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500397 void setHasWCoord(bool hasW) {
398 fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag;
399 }
400 bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400401 void setNeedsTransform(bool needsTransform) {
402 fFlags = needsTransform ? (fFlags | kNeedsTransform_Flag)
403 : fFlags & ~kNeedsTransform_Flag;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500404 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400405 bool needsTransform() const { return SkToBool(fFlags & kNeedsTransform_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800406
407 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400408 enum Flag {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500409 kDrawAsSDF_Flag = 0x01,
410 kUseLCDText_Flag = 0x02,
411 kAntiAliased_Flag = 0x04,
412 kHasWCoord_Flag = 0x08,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400413 kNeedsTransform_Flag = 0x10
Jim Van Verth90e89b32017-07-06 16:36:55 -0400414 };
415
Brian Salomon2ee084e2016-12-16 18:59:19 -0500416 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500417 sk_sp<GrTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800418 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800419 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700420 uint64_t fAtlasGeneration;
421 size_t fVertexStartIndex;
422 size_t fVertexEndIndex;
423 uint32_t fGlyphStartIndex;
424 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800425 SkScalar fX;
426 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800427 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700428 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400429 uint32_t fFlags;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400430 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700431
Herb Derby2bb343c2018-11-08 15:03:48 -0500432 // sets the last subrun of runIndex to use w values
433 void setSubRunHasW(bool hasWCoord) {
434 Run::SubRunInfo& subRun = this->fSubRunInfo.back();
435 subRun.setHasWCoord(hasWCoord);
436 }
437
438 // inits the override descriptor on the current run. All following subruns must use this
439 // descriptor
440 void initOverride() {
441 // Push back a new subrun to fill and set the override descriptor
442 this->pushBackSubRun();
443 fOverrideDescriptor.reset(new SkAutoDescriptor);
444 }
445
446 // Appends a glyph to the blob as a path only.
447 void appendPathGlyph(
448 const SkPath& path, SkPoint position, SkScalar scale, bool preTransformed);
449
450 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
451 // as a path.
452 void appendGlyph(GrTextBlob* blob,
453 const sk_sp<GrTextStrike>& strike,
454 const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
455 SkPoint origin,
456 const SkPMColor4f& color, SkGlyphCache* skGlyphCache,
457 SkScalar textRatio, bool needsTransform);
458
459 SkExclusiveStrikePtr setupCache(const SkPaint& skPaint,
460 const SkSurfaceProps& props,
461 SkScalerContextFlags scalerContextFlags,
462 const SkMatrix& viewMatrix);
463
464 void setRunPaintFlags(uint16_t paintFlags) {
465 fPaintFlags = paintFlags & Run::kPaintFlagsMask;
466 }
467
468 // sets the last subrun of runIndex to use distance field text
469 void setSubRunHasDistanceFields(bool hasLCD, bool isAntiAlias, bool hasWCoord) {
470 Run::SubRunInfo& subRun = fSubRunInfo.back();
471 subRun.setUseLCDText(hasLCD);
472 subRun.setAntiAliased(isAntiAlias);
473 subRun.setDrawAsDistanceFields();
474 subRun.setHasWCoord(hasWCoord);
475 }
476
477 SubRunInfo& pushBackSubRun() {
joshualitt374b2f72015-07-21 08:05:03 -0700478 // 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;
Herb Derby2bb343c2018-11-08 15:03:48 -0500562 int fRunCount{0};
563 int fRunCountLimit;
joshualitt374b2f72015-07-21 08:05:03 -0700564 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700565};
566
Brian Salomon18923f92017-11-06 16:26:02 -0500567/**
568 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
569 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
570 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
571 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
572 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
573 * entire sub run has been completed.
574 */
Herb Derby86240592018-05-24 16:12:31 -0400575class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500576public:
577 /**
578 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
579 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
580 * SkGlyphCache.
581 */
Herb Derby86240592018-05-24 16:12:31 -0400582 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500583 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500584 GrDeferredUploadTarget*, GrGlyphCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400585 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500586
587 struct Result {
588 /**
589 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
590 * draws and call regenerate() again.
591 */
592 bool fFinished = true;
593
594 /**
595 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
596 * fType is kFinished.
597 */
598 int fGlyphsRegenerated = 0;
599
600 /**
601 * Pointer where the caller finds the first regenerated vertex.
602 */
603 const char* fFirstVertex;
604 };
605
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500606 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500607
608private:
609 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500610 bool doRegen(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500611
Robert Phillips4bc70112018-03-01 10:24:02 -0500612 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500613 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400614 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500615 GrDeferredUploadTarget* fUploadTarget;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500616 GrGlyphCache* fGlyphCache;
617 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500618 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500619 Run* fRun;
620 Run::SubRunInfo* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500621 GrColor fColor;
622 SkScalar fTransX;
623 SkScalar fTransY;
624
625 uint32_t fRegenFlags = 0;
626 int fCurrGlyph = 0;
627 bool fBrokenRun = false;
628};
629
Herb Derby86240592018-05-24 16:12:31 -0400630#endif // GrTextBlob_DEFINED