blob: 450e256d38d2dc0b17ace6f90a0703966333de7d [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
8#ifndef GrAtlasTextBlob_DEFINED
9#define GrAtlasTextBlob_DEFINED
10
Brian Salomonf856fd12016-12-16 14:24:34 -050011#include "GrAtlasGlyphCache.h"
joshualitt259fbf12015-07-21 11:39:34 -070012#include "GrColor.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050013#include "GrDrawOpAtlas.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "GrMemoryPool.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050015#include "GrTextUtils.h"
joshualitt374b2f72015-07-21 08:05:03 -070016#include "SkDescriptor.h"
17#include "SkMaskFilter.h"
mtklein4e976072016-08-08 09:06:27 -070018#include "SkOpts.h"
bsalomon8b6fa5e2016-05-19 16:23:47 -070019#include "SkPathEffect.h"
20#include "SkRasterizer.h"
joshualitt259fbf12015-07-21 11:39:34 -070021#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070022#include "SkTInternalLList.h"
23
joshualitt2e2202e2015-12-10 11:22:08 -080024struct GrDistanceFieldAdjustTable;
joshualitt92303772016-02-10 11:55:52 -080025class GrMemoryPool;
joshualitt2e2202e2015-12-10 11:22:08 -080026class SkDrawFilter;
27class SkTextBlob;
28class SkTextBlobRunIterator;
29
joshualitt259fbf12015-07-21 11:39:34 -070030// With this flag enabled, the GrAtlasTextContext will, as a sanity check, regenerate every blob
31// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080032#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070033
joshualitt374b2f72015-07-21 08:05:03 -070034/*
35 * A GrAtlasTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
36 * on the GPU. These are initially created with valid positions and colors, but invalid
37 * texture coordinates. The GrAtlasTextBlob itself has a few Blob-wide properties, and also
38 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
39 * reordered.
40 *
41 * The only thing(aside from a memcopy) required to flush a GrAtlasTextBlob is to ensure that
42 * the GrAtlas will not evict anything the Blob needs.
43 *
44 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070045 *
46 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070047 */
joshualitt2e2202e2015-12-10 11:22:08 -080048class GrAtlasTextBlob : public SkNVRefCnt<GrAtlasTextBlob> {
49public:
joshualitt374b2f72015-07-21 08:05:03 -070050 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrAtlasTextBlob);
51
Brian Salomon18923f92017-11-06 16:26:02 -050052 class VertexRegenerator;
53
Florin Malitac337c9e2017-03-10 18:02:29 +000054 static sk_sp<GrAtlasTextBlob> Make(GrMemoryPool* pool, int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080055
56 struct Key {
57 Key() {
58 sk_bzero(this, sizeof(Key));
59 }
60 uint32_t fUniqueID;
61 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
62 // Each color is assigned to on of a fixed number of buckets based on its
63 // luminance. For each luminance bucket there is a "canonical color" that
64 // represents the bucket. This functionality is currently only supported for A8
65 SkColor fCanonicalColor;
66 SkPaint::Style fStyle;
67 SkPixelGeometry fPixelGeometry;
68 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070069 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080070
71 bool operator==(const Key& other) const {
72 return 0 == memcmp(this, &other, sizeof(Key));
73 }
74 };
75
joshualitt92303772016-02-10 11:55:52 -080076 void setupKey(const GrAtlasTextBlob::Key& key,
77 const SkMaskFilter::BlurRec& blurRec,
78 const SkPaint& paint) {
79 fKey = key;
80 if (key.fHasBlur) {
81 fBlurRec = blurRec;
82 }
83 if (key.fStyle != SkPaint::kFill_Style) {
84 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
85 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
86 fStrokeInfo.fJoin = paint.getStrokeJoin();
87 }
88 }
89
joshualitt323c2eb2016-01-20 06:48:47 -080090 static const Key& GetKey(const GrAtlasTextBlob& blob) {
91 return blob.fKey;
92 }
93
94 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -070095 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -080096 }
97
98 void operator delete(void* p) {
99 GrAtlasTextBlob* blob = reinterpret_cast<GrAtlasTextBlob*>(p);
100 blob->fPool->release(p);
101 }
102 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400103 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800104 return sk_malloc_throw(0);
105 }
106
107 void* operator new(size_t, void* p) { return p; }
108 void operator delete(void* target, void* placement) {
109 ::operator delete(target, placement);
110 }
111
112 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
113 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
114 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
115 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
116
joshualittddd22d82016-02-16 06:47:52 -0800117 int runCount() const { return fRunCount; }
118
joshualitt323c2eb2016-01-20 06:48:47 -0800119 void push_back_run(int currRun) {
120 SkASSERT(currRun < fRunCount);
121 if (currRun > 0) {
122 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
123 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
124 newRun.setAsSuccessor(lastRun);
125 }
126 }
127
128 // sets the last subrun of runIndex to use distance field text
Jim Van Verth90e89b32017-07-06 16:36:55 -0400129 void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias) {
joshualitt323c2eb2016-01-20 06:48:47 -0800130 Run& run = fRuns[runIndex];
131 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
132 subRun.setUseLCDText(hasLCD);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400133 subRun.setAntiAliased(isAntiAlias);
joshualitt323c2eb2016-01-20 06:48:47 -0800134 subRun.setDrawAsDistanceFields();
135 }
136
137 void setRunDrawAsPaths(int runIndex) {
138 fRuns[runIndex].fDrawAsPaths = true;
139 }
140
141 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
142 // we init fMaxMinScale and fMinMaxScale in the constructor
143 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
144 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
145 }
146
147 // inits the override descriptor on the current run. All following subruns must use this
148 // descriptor
149 void initOverride(int runIndex) {
150 Run& run = fRuns[runIndex];
151 // Push back a new subrun to fill and set the override descriptor
152 run.push_back();
153 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
154 }
155
156 SkGlyphCache* setupCache(int runIndex,
157 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -0700158 uint32_t scalerContextFlags,
joshualitt323c2eb2016-01-20 06:48:47 -0800159 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -0800160 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800161
162 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
163 // as a path.
164 void appendGlyph(int runIndex,
165 const SkRect& positions,
166 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -0500167 GrAtlasTextStrike* strike,
joshualitt323c2eb2016-01-20 06:48:47 -0800168 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700169 SkGlyphCache*, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500170 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP);
joshualitt323c2eb2016-01-20 06:48:47 -0800171
172 static size_t GetVertexStride(GrMaskFormat maskFormat) {
173 switch (maskFormat) {
174 case kA8_GrMaskFormat:
175 return kGrayTextVASize;
176 case kARGB_GrMaskFormat:
177 return kColorTextVASize;
178 default:
179 return kLCDTextVASize;
180 }
181 }
182
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500183 bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilter::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800184 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
185
186 // flush a GrAtlasTextBlob associated with a SkTextBlob
Brian Salomonf18b1d82017-10-27 11:30:49 -0400187 void flushCached(GrContext* context, GrTextUtils::Target*, const SkTextBlob* blob,
joshualitt323c2eb2016-01-20 06:48:47 -0800188 const SkSurfaceProps& props,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500189 const GrDistanceFieldAdjustTable* distanceAdjustTable,
190 const GrTextUtils::Paint&, SkDrawFilter* drawFilter, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500191 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800192
193 // flush a throwaway GrAtlasTextBlob *not* associated with an SkTextBlob
Brian Salomonf18b1d82017-10-27 11:30:49 -0400194 void flushThrowaway(GrContext* context, GrTextUtils::Target*, const SkSurfaceProps& props,
joshualitt323c2eb2016-01-20 06:48:47 -0800195 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500196 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500197 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
198 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800199
joshualitt8e0ef292016-02-19 14:13:03 -0800200 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
201 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittbc811112016-02-11 12:42:02 -0800202 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
203 // into device space.
204 // We handle vertex bounds differently for distance field text and bitmap text because
205 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
206 // from one blob then we are going to pay the price here of mapping the rect for each run.
207 const Run& run = fRuns[runIndex];
208 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
209 *outBounds = subRun.vertexBounds();
210 if (subRun.drawAsDistanceFields()) {
211 // Distance field text is positioned with the (X,Y) as part of the glyph position,
212 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800213 outBounds->offset(x - fInitialX, y - fInitialY);
214 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800215 } else {
216 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
217 // device space.
218 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
219
220 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
221
joshualitt8e0ef292016-02-19 14:13:03 -0800222 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800223
joshualitt8e0ef292016-02-19 14:13:03 -0800224 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800225 boundsMatrix.mapRect(outBounds);
226
227 // Due to floating point numerical inaccuracies, we have to round out here
228 outBounds->roundOut(outBounds);
229 }
230 }
231
joshualitt323c2eb2016-01-20 06:48:47 -0800232 // position + local coord
233 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
234 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
235 static const size_t kLCDTextVASize = kGrayTextVASize;
joshualitt92303772016-02-10 11:55:52 -0800236 static const size_t kMaxVASize = kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800237 static const int kVerticesPerGlyph = 4;
238
joshualitt323c2eb2016-01-20 06:48:47 -0800239 static void AssertEqual(const GrAtlasTextBlob&, const GrAtlasTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800240
241 // The color here is the GrPaint color, and it is used to determine whether we
242 // have to regenerate LCD text blobs.
243 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400244 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
245 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400246 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800247 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800248 }
249
joshualitt7481e752016-01-22 06:08:48 -0800250 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
251 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800252 }
253
joshualitt92303772016-02-10 11:55:52 -0800254 const Key& key() const { return fKey; }
255
256 ~GrAtlasTextBlob() {
257 for (int i = 0; i < fRunCount; i++) {
258 fRuns[i].~Run();
259 }
260 }
261
joshualittbc811112016-02-11 12:42:02 -0800262 ////////////////////////////////////////////////////////////////////////////////////////////////
263 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400264 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400265 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
266 const GrTextUtils::Paint&, const SkSurfaceProps&,
267 const GrDistanceFieldAdjustTable*, GrAtlasGlyphCache*,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400268 GrTextUtils::Target*);
joshualittbc811112016-02-11 12:42:02 -0800269
joshualitt323c2eb2016-01-20 06:48:47 -0800270private:
joshualitt92303772016-02-10 11:55:52 -0800271 GrAtlasTextBlob()
272 : fMaxMinScale(-SK_ScalarMax)
273 , fMinMaxScale(SK_ScalarMax)
274 , fTextType(0) {}
275
bsalomonc2878e22016-05-17 13:18:03 -0700276 void appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500277 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP);
joshualitt323c2eb2016-01-20 06:48:47 -0800278
Brian Salomonf18b1d82017-10-27 11:30:49 -0400279 inline void flushRun(GrTextUtils::Target*, const GrClip&, int run, const SkMatrix& viewMatrix,
280 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint,
281 const SkSurfaceProps& props,
joshualitt323c2eb2016-01-20 06:48:47 -0800282 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500283 GrAtlasGlyphCache* cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800284
Brian Salomonf18b1d82017-10-27 11:30:49 -0400285 void flushBigGlyphs(GrContext* context, GrTextUtils::Target*, const GrClip& clip,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500286 const SkPaint& paint, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt323c2eb2016-01-20 06:48:47 -0800287 const SkIRect& clipBounds);
288
Brian Salomonf18b1d82017-10-27 11:30:49 -0400289 void flushRunAsPaths(GrContext* context, GrTextUtils::Target*, const SkSurfaceProps& props,
290 const SkTextBlobRunIterator& it, const GrClip& clip,
291 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
292 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
293 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800294
joshualitt8e0ef292016-02-19 14:13:03 -0800295 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800296 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
297 // these numbers. When blobs are reused with new matrices, we need to return to model space so
298 // we can update the vertex bounds appropriately.
299 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800300 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800301 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
302 fInitialViewMatrixInverse = SkMatrix::I();
303 SkDebugf("Could not invert viewmatrix\n");
304 }
joshualitt8e0ef292016-02-19 14:13:03 -0800305 fInitialX = x;
306 fInitialY = y;
307
308 // make sure all initial subruns have the correct VM and X/Y applied
309 for (int i = 0; i < fRunCount; i++) {
310 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
311 }
joshualitt7481e752016-01-22 06:08:48 -0800312 }
313
joshualitt374b2f72015-07-21 08:05:03 -0700314 /*
315 * Each Run inside of the blob can have its texture coordinates regenerated if required.
316 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
317 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
318 * this at a more fine grained level, but its not clear if this is worth it, as evictions
319 * should be fairly rare.
320 *
321 * One additional point, each run can contain glyphs with any of the three mask formats.
322 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
323 * a new subrun each time the mask format changes in a run. In theory, a run can have as
324 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
325 * practice, the vast majority of runs have only a single subrun.
326 *
327 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
Brian Salomonf18b1d82017-10-27 11:30:49 -0400328 * handle, we have a bit to mark the run as flushable via rendering as paths. It is worth
joshualitt374b2f72015-07-21 08:05:03 -0700329 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
330 * can flush in this manner, so we always allocate vertices for the run, regardless of
331 * whether or not it is too large. The benefit of this strategy is that we can always reuse
332 * a blob allocation regardless of viewmatrix changes. We could store positions for these
333 * glyphs. However, its not clear if this is a win because we'd still have to either go the
334 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
335 * would greatly increase the memory of these cached items.
336 */
337 struct Run {
338 Run()
joshualittf9e658b2015-12-09 09:26:44 -0800339 : fInitialized(false)
joshualitt374b2f72015-07-21 08:05:03 -0700340 , fDrawAsPaths(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700341 // To ensure we always have one subrun, we push back a fresh run here
342 fSubRunInfo.push_back();
343 }
344 struct SubRunInfo {
345 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500346 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
347 , fVertexStartIndex(0)
348 , fVertexEndIndex(0)
349 , fGlyphStartIndex(0)
350 , fGlyphEndIndex(0)
351 , fColor(GrColor_ILLEGAL)
352 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400353 , fFlags(0) {
joshualitt7481e752016-01-22 06:08:48 -0800354 fVertexBounds.setLargestInverted();
355 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700356 SubRunInfo(const SubRunInfo& that)
357 : fBulkUseToken(that.fBulkUseToken)
358 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800359 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800360 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700361 , fAtlasGeneration(that.fAtlasGeneration)
362 , fVertexStartIndex(that.fVertexStartIndex)
363 , fVertexEndIndex(that.fVertexEndIndex)
364 , fGlyphStartIndex(that.fGlyphStartIndex)
365 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800366 , fX(that.fX)
367 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800368 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700369 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400370 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700371 }
joshualitt3660d532015-12-07 11:32:50 -0800372
joshualitt18b072d2015-12-07 12:26:12 -0800373 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800374 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500375 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Brian Salomonf856fd12016-12-16 14:24:34 -0500376 void setStrike(GrAtlasTextStrike* strike) { fStrike.reset(SkRef(strike)); }
377 GrAtlasTextStrike* strike() const { return fStrike.get(); }
joshualitt3660d532015-12-07 11:32:50 -0800378
379 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
380 uint64_t atlasGeneration() const { return fAtlasGeneration; }
381
382 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800383 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800384 size_t vertexEndIndex() const { return fVertexEndIndex; }
385 void appendVertices(size_t vertexStride) {
386 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
387 }
388
389 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800390 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800391 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
392 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800393 void setColor(GrColor color) { fColor = color; }
394 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800395 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
396 GrMaskFormat maskFormat() const { return fMaskFormat; }
397
joshualitt18b072d2015-12-07 12:26:12 -0800398 void setAsSuccessor(const SubRunInfo& prev) {
399 fGlyphStartIndex = prev.glyphEndIndex();
400 fGlyphEndIndex = prev.glyphEndIndex();
401
402 fVertexStartIndex = prev.vertexEndIndex();
403 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800404
405 // copy over viewmatrix settings
406 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800407 }
408
joshualitt7481e752016-01-22 06:08:48 -0800409 const SkRect& vertexBounds() const { return fVertexBounds; }
410 void joinGlyphBounds(const SkRect& glyphBounds) {
411 fVertexBounds.joinNonEmptyArg(glyphBounds);
412 }
413
joshualitt8e0ef292016-02-19 14:13:03 -0800414 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
415 fCurrentViewMatrix = viewMatrix;
416 fX = x;
417 fY = y;
418 }
419
420 // This function assumes the translation will be applied before it is called again
421 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
422 SkScalar*transX, SkScalar* transY);
423
joshualitt3660d532015-12-07 11:32:50 -0800424 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400425 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
426 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
427 void setUseLCDText(bool useLCDText) {
428 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
429 }
430 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
431 void setAntiAliased(bool antiAliased) {
432 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
433 }
434 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800435
436 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400437 enum Flag {
438 kDrawAsSDF_Flag = 0x1,
439 kUseLCDText_Flag = 0x2,
440 kAntiAliased_Flag = 0x4
441 };
442
Brian Salomon2ee084e2016-12-16 18:59:19 -0500443 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Brian Salomonf856fd12016-12-16 14:24:34 -0500444 sk_sp<GrAtlasTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800445 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800446 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700447 uint64_t fAtlasGeneration;
448 size_t fVertexStartIndex;
449 size_t fVertexEndIndex;
450 uint32_t fGlyphStartIndex;
451 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800452 SkScalar fX;
453 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800454 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700455 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400456 uint32_t fFlags;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400457 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700458
459 SubRunInfo& push_back() {
460 // Forward glyph / vertex information to seed the new sub run
joshualitt374b2f72015-07-21 08:05:03 -0700461 SubRunInfo& newSubRun = fSubRunInfo.push_back();
joshualitt18b072d2015-12-07 12:26:12 -0800462 const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700463
joshualitt18b072d2015-12-07 12:26:12 -0800464 newSubRun.setAsSuccessor(prevSubRun);
joshualitt374b2f72015-07-21 08:05:03 -0700465 return newSubRun;
466 }
467 static const int kMinSubRuns = 1;
Hal Canary144caf52016-11-07 17:57:18 -0500468 sk_sp<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700469 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
470 SkAutoDescriptor fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700471
472 // Effects from the paint that are used to build a SkScalerContext.
473 sk_sp<SkPathEffect> fPathEffect;
474 sk_sp<SkRasterizer> fRasterizer;
475 sk_sp<SkMaskFilter> fMaskFilter;
joshualitt3660d532015-12-07 11:32:50 -0800476
477 // Distance field text cannot draw coloremoji, and so has to fall back. However,
478 // though the distance field text and the coloremoji may share the same run, they
479 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
480 // will be used in place of the run's descriptor to regen texture coords
Ben Wagner145dbcd2016-11-03 14:40:50 -0400481 std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
joshualitt374b2f72015-07-21 08:05:03 -0700482 bool fInitialized;
483 bool fDrawAsPaths;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400484 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700485
Brian Salomonf18b1d82017-10-27 11:30:49 -0400486 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400487 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400488 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
489 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
490 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
491 GrTextUtils::Target*);
joshualitt323c2eb2016-01-20 06:48:47 -0800492
joshualitt374b2f72015-07-21 08:05:03 -0700493 struct BigGlyph {
Jim Van Verth08576e62016-11-16 10:15:23 -0500494 BigGlyph(const SkPath& path, SkScalar vx, SkScalar vy, SkScalar scale, bool treatAsBMP)
joshualitt374b2f72015-07-21 08:05:03 -0700495 : fPath(path)
joshualitt0fe04a22015-08-25 12:05:50 -0700496 , fScale(scale)
joshualitt8e0ef292016-02-19 14:13:03 -0800497 , fX(vx)
498 , fY(vy)
Jim Van Verth08576e62016-11-16 10:15:23 -0500499 , fTreatAsBMP(treatAsBMP) {}
joshualitt374b2f72015-07-21 08:05:03 -0700500 SkPath fPath;
joshualitt0fe04a22015-08-25 12:05:50 -0700501 SkScalar fScale;
joshualitt8e0ef292016-02-19 14:13:03 -0800502 SkScalar fX;
503 SkScalar fY;
Jim Van Verth08576e62016-11-16 10:15:23 -0500504 bool fTreatAsBMP;
joshualitt374b2f72015-07-21 08:05:03 -0700505 };
506
joshualitt374b2f72015-07-21 08:05:03 -0700507 struct StrokeInfo {
508 SkScalar fFrameWidth;
509 SkScalar fMiterLimit;
510 SkPaint::Join fJoin;
511 };
512
513 enum TextType {
514 kHasDistanceField_TextType = 0x1,
515 kHasBitmap_TextType = 0x2,
516 };
517
518 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500519 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700520 GrGlyph** fGlyphs;
521 Run* fRuns;
522 GrMemoryPool* fPool;
523 SkMaskFilter::BlurRec fBlurRec;
524 StrokeInfo fStrokeInfo;
525 SkTArray<BigGlyph> fBigGlyphs;
526 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800527 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800528 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800529 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400530 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800531 SkScalar fInitialX;
532 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700533
534 // We can reuse distance field text, but only if the new viewmatrix would not result in
535 // a mip change. Because there can be multiple runs in a blob, we track the overall
536 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
537 SkScalar fMaxMinScale;
538 SkScalar fMinMaxScale;
539 int fRunCount;
540 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700541};
542
Brian Salomon18923f92017-11-06 16:26:02 -0500543/**
544 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
545 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
546 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
547 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
548 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
549 * entire sub run has been completed.
550 */
551class GrAtlasTextBlob::VertexRegenerator {
552public:
553 /**
554 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
555 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
556 * SkGlyphCache.
557 */
558 VertexRegenerator(GrAtlasTextBlob* blob, int runIdx, int subRunIdx, const SkMatrix& viewMatrix,
559 SkScalar x, SkScalar y, GrColor color, GrDeferredUploadTarget*,
Brian Salomondeb53cc2017-11-08 13:50:53 -0500560 GrAtlasGlyphCache*, SkAutoGlyphCache*);
Brian Salomon18923f92017-11-06 16:26:02 -0500561
562 struct Result {
563 /**
564 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
565 * draws and call regenerate() again.
566 */
567 bool fFinished = true;
568
569 /**
570 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
571 * fType is kFinished.
572 */
573 int fGlyphsRegenerated = 0;
574
575 /**
576 * Pointer where the caller finds the first regenerated vertex.
577 */
578 const char* fFirstVertex;
579 };
580
581 Result regenerate();
582
583private:
584 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
585 Result doRegen();
586
587 const SkMatrix& fViewMatrix;
588 GrAtlasTextBlob* fBlob;
589 GrDeferredUploadTarget* fUploadTarget;
590 GrAtlasGlyphCache* fGlyphCache;
591 SkAutoGlyphCache* fLazyCache;
592 Run* fRun;
593 Run::SubRunInfo* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500594 GrColor fColor;
595 SkScalar fTransX;
596 SkScalar fTransY;
597
598 uint32_t fRegenFlags = 0;
599 int fCurrGlyph = 0;
600 bool fBrokenRun = false;
601};
602
joshualitt374b2f72015-07-21 08:05:03 -0700603#endif