blob: 62f7d74f57c8f5a88faf5559f891463001b74853 [file] [log] [blame]
joshualitt259fbf12015-07-21 11:39:34 -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#include "GrTextBlob.h"
joshualitt2e2202e2015-12-10 11:22:08 -08009#include "GrBlurUtils.h"
Jim Van Verth58c3cce2017-10-19 15:50:24 -040010#include "GrClip.h"
joshualitt2e2202e2015-12-10 11:22:08 -080011#include "GrContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080012#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080013#include "SkColorFilter.h"
14#include "SkDrawFilter.h"
joshualitte76b4bb32015-12-28 07:23:58 -080015#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050016#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050017#include "SkPaintPriv.h"
joshualitt2e2202e2015-12-10 11:22:08 -080018#include "SkTextBlobRunIterator.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050019#include "SkTextToPathIter.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrAtlasTextOp.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040021#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080022
Herb Derby86240592018-05-24 16:12:31 -040023sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
24 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080025 // and size for the glyphIds array.
26 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Herb Derby86240592018-05-24 16:12:31 -040027 size_t size = sizeof(GrTextBlob) +
joshualitt92303772016-02-10 11:55:52 -080028 verticesCount +
29 glyphCount * sizeof(GrGlyph**) +
Herb Derby86240592018-05-24 16:12:31 -040030 sizeof(GrTextBlob::Run) * runCount;
joshualitt92303772016-02-10 11:55:52 -080031
Herb Derbyb12175f2018-05-23 16:38:09 -040032 void* allocation = ::operator new (size);
33
joshualitt92303772016-02-10 11:55:52 -080034 if (CACHE_SANITY_CHECK) {
35 sk_bzero(allocation, size);
36 }
37
Herb Derby86240592018-05-24 16:12:31 -040038 sk_sp<GrTextBlob> cacheBlob(new (allocation) GrTextBlob);
joshualitt92303772016-02-10 11:55:52 -080039 cacheBlob->fSize = size;
40
41 // setup offsets for vertices / glyphs
Herb Derby86240592018-05-24 16:12:31 -040042 cacheBlob->fVertices = sizeof(GrTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080043 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
Herb Derby86240592018-05-24 16:12:31 -040044 cacheBlob->fRuns = reinterpret_cast<GrTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualitt92303772016-02-10 11:55:52 -080045
46 // Initialize runs
47 for (int i = 0; i < runCount; i++) {
Herb Derby86240592018-05-24 16:12:31 -040048 new (&cacheBlob->fRuns[i]) GrTextBlob::Run;
joshualitt92303772016-02-10 11:55:52 -080049 }
50 cacheBlob->fRunCount = runCount;
joshualitt92303772016-02-10 11:55:52 -080051 return cacheBlob;
52}
53
Herb Derby86240592018-05-24 16:12:31 -040054SkExclusiveStrikePtr GrTextBlob::setupCache(int runIndex,
Herb Derby526819d2018-03-09 12:51:12 -050055 const SkSurfaceProps& props,
56 SkScalerContextFlags scalerContextFlags,
57 const SkPaint& skPaint,
58 const SkMatrix* viewMatrix) {
Herb Derby86240592018-05-24 16:12:31 -040059 GrTextBlob::Run* run = &fRuns[runIndex];
joshualitte76b4bb32015-12-28 07:23:58 -080060
61 // if we have an override descriptor for the run, then we should use that
62 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
63 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050065 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
66 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050067 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070068 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070069 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derbyfa996902018-04-18 11:36:12 -040070 return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *run->fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080071}
72
Herb Derby86240592018-05-24 16:12:31 -040073void GrTextBlob::appendGlyph(int runIndex,
joshualittf528e0d2015-12-09 06:42:52 -080074 const SkRect& positions,
75 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050076 sk_sp<GrTextStrike> strike,
joshualitta06e6ab2015-12-10 08:54:41 -080077 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070078 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050079 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
Lee Salzman26d3f212017-01-27 14:31:10 -050080 if (positions.isEmpty()) {
81 return;
82 }
joshualitta06e6ab2015-12-10 08:54:41 -080083
84 // If the glyph is too large we fall back to paths
85 if (glyph->fTooLargeForAtlas) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050086 if (nullptr == glyph->fPath) {
87 const SkPath* glyphPath = cache->findPath(skGlyph);
88 if (!glyphPath) {
89 return;
90 }
91
92 glyph->fPath = new SkPath(*glyphPath);
93 }
94 this->appendPathGlyph(runIndex, *glyph->fPath, x, y, scale, preTransformed);
joshualitta06e6ab2015-12-10 08:54:41 -080095 return;
96 }
97
joshualittf528e0d2015-12-09 06:42:52 -080098 Run& run = fRuns[runIndex];
99 GrMaskFormat format = glyph->fMaskFormat;
100
101 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
102 if (run.fInitialized && subRun->maskFormat() != format) {
103 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500104 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800105 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500106 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800107 }
108
109 run.fInitialized = true;
110
Brian Salomon5c6ac642017-12-19 11:09:32 -0500111 bool hasW = subRun->hasWCoord();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400112 // glyphs drawn in perspective must always have a w coord.
113 SkASSERT(hasW || !fInitialViewMatrix.hasPerspective());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114
115 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800116
117 subRun->setMaskFormat(format);
118
joshualitt7481e752016-01-22 06:08:48 -0800119 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800120 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800121
122 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
123
Brian Salomon5c6ac642017-12-19 11:09:32 -0500124 // We always write the third position component used by SDFs. If it is unused it gets
125 // overwritten. Similarly, we always write the color and the blob will later overwrite it
126 // with texture coords if it is unused.
127 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
128 // V0
129 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
130 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
131 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800132
Brian Salomon5c6ac642017-12-19 11:09:32 -0500133 // V1
134 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
135 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
136 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800137
Brian Salomon5c6ac642017-12-19 11:09:32 -0500138 // V2
139 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
140 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
141 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800142
Brian Salomon5c6ac642017-12-19 11:09:32 -0500143 // V3
144 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
145 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800146
joshualitt18b072d2015-12-07 12:26:12 -0800147 subRun->appendVertices(vertexStride);
148 fGlyphs[subRun->glyphEndIndex()] = glyph;
149 subRun->glyphAppended();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400150 subRun->setNeedsTransform(!preTransformed);
joshualitt18b072d2015-12-07 12:26:12 -0800151}
152
Herb Derby86240592018-05-24 16:12:31 -0400153void GrTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500154 SkScalar scale, bool preTransformed) {
155 Run& run = fRuns[runIndex];
Herb Derby86240592018-05-24 16:12:31 -0400156 run.fPathGlyphs.push_back(GrTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800157}
158
Herb Derby86240592018-05-24 16:12:31 -0400159bool GrTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500160 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800161 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
162 // If we have LCD text then our canonical color will be set to transparent, in this case we have
163 // to regenerate the blob on any color change
164 // We use the grPaint to get any color filter effects
165 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400166 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800167 return true;
168 }
169
joshualitt8e0ef292016-02-19 14:13:03 -0800170 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800171 return true;
172 }
173
Brian Salomon5c6ac642017-12-19 11:09:32 -0500174 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800175 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800176 return true;
177 }
178
179 // We only cache one masked version
180 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400181 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800182 return true;
183 }
184
185 // Similarly, we only cache one version for each style
186 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500187 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
188 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
189 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800190 return true;
191 }
192
193 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
194 // for mixed blobs if this becomes an issue.
195 if (this->hasBitmap() && this->hasDistanceField()) {
196 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800197 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800198 return false;
199 }
200 return true;
201 }
202
203 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800204 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
205 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
206 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
207 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800208 return true;
209 }
210
211 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
212 // but only for integer translations.
213 // This cool bit of math will determine the necessary translation to apply to the already
214 // generated vertex coordinates to move them to the correct position
215 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800216 viewMatrix.getScaleX() * (x - fInitialX) +
217 viewMatrix.getSkewX() * (y - fInitialY) -
218 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800219 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800220 viewMatrix.getSkewY() * (x - fInitialX) +
221 viewMatrix.getScaleY() * (y - fInitialY) -
222 fInitialViewMatrix.getTranslateY();
223 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800224 return true;
225 }
joshualittfd5f6c12015-12-10 07:44:50 -0800226 } else if (this->hasDistanceField()) {
227 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
228 // distance field being generated, so we have to regenerate in those cases
229 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800230 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800231 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
232 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
233 return true;
234 }
joshualittfd5f6c12015-12-10 07:44:50 -0800235 }
236
joshualittfd5f6c12015-12-10 07:44:50 -0800237 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
238 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
239 // the blob anyways at flush time, so no need to regenerate explicitly
240 return false;
241}
242
Herb Derby86240592018-05-24 16:12:31 -0400243inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400244 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400245 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
246 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500247 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800248 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800249
Brian Salomon44acb5b2017-07-18 19:59:24 -0400250 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400251 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500252 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800253 if (info.drawAsDistanceFields()) {
Brian Osman34ec3742018-07-03 10:40:57 -0400254 // TODO: Can we be even smarter based on the dest transfer function?
Brian Salomon44acb5b2017-07-18 19:59:24 -0400255 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips7c525e62018-06-12 10:11:12 -0400256 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
Brian Osman34ec3742018-07-03 10:40:57 -0400257 target->colorSpaceInfo().isLinearlyBlended(), paint.luminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400258 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800259 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400260 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400261 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800262 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500263 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800264 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400265 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800266 geometry.fBlob = SkRef(this);
267 geometry.fRun = run;
268 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500269 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400270 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800271 geometry.fX = x;
272 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500273 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400274 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800275}
276
joshualitt8e0ef292016-02-19 14:13:03 -0800277static void calculate_translation(bool applyVM,
278 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
279 const SkMatrix& currentViewMatrix, SkScalar currentX,
280 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
281 if (applyVM) {
282 *transX = newViewMatrix.getTranslateX() +
283 newViewMatrix.getScaleX() * (newX - currentX) +
284 newViewMatrix.getSkewX() * (newY - currentY) -
285 currentViewMatrix.getTranslateX();
286
287 *transY = newViewMatrix.getTranslateY() +
288 newViewMatrix.getSkewY() * (newX - currentX) +
289 newViewMatrix.getScaleY() * (newY - currentY) -
290 currentViewMatrix.getTranslateY();
291 } else {
292 *transX = newX - currentX;
293 *transY = newY - currentY;
294 }
295}
296
Herb Derby86240592018-05-24 16:12:31 -0400297void GrTextBlob::flush(GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500298 const GrDistanceFieldAdjustTable* distanceAdjustTable,
299 const GrTextUtils::Paint& paint, const GrClip& clip,
300 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
301 SkScalar x, SkScalar y) {
302
Herb Derby86240592018-05-24 16:12:31 -0400303 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500304 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
305 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Ben Wagnerd234afd2018-04-13 15:50:01 -0400306 GrTextUtils::RunPaint runPaint(&paint, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500307 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
308 Run& run = fRuns[runIndex];
309
310 // first flush any path glyphs
311 if (run.fPathGlyphs.count()) {
312 SkScalar transX, transY;
313 uint16_t paintFlags = run.fPaintFlags;
314 if (!runPaint.modifyForRun(
315 [paintFlags](SkPaint* p) {
316 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
317 })) {
318 continue;
319 }
320 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400321 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500322 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
323 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
324 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
325 SkMatrix pathMatrix;
326 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
327 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
328 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
329 }
joshualitt2e2202e2015-12-10 11:22:08 -0800330 }
joshualitt2e2202e2015-12-10 11:22:08 -0800331
Jim Van Verth54d9c882018-02-08 16:14:48 -0500332 // then flush each subrun, if any
333 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000334 continue;
335 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500336 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
337 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
338 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
339 int glyphCount = info.glyphCount();
340 if (0 == glyphCount) {
341 continue;
342 }
343
344 bool skipClip = false;
345 bool submitOp = true;
346 SkIRect clipRect = SkIRect::MakeEmpty();
347 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
348 SkRRect clipRRect;
349 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400350 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500351 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400352 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500353 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500354 clipRRect.isRect() && GrAA::kNo == aa) {
355 skipClip = true;
356 // We only need to do clipping work if the subrun isn't contained by the clip
357 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400358 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
359 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500360 if (!clipRRect.getBounds().contains(subRunBounds)) {
361 // If the subrun is completely outside, don't add an op for it
362 if (!clipRRect.getBounds().intersects(subRunBounds)) {
363 submitOp = false;
364 }
365 else {
366 clipRRect.getBounds().round(&clipRect);
367 }
368 }
369 }
370
371 if (submitOp) {
372 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
373 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500374 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500375 if (op) {
376 if (skipClip) {
377 target->addDrawOp(GrNoClip(), std::move(op));
378 }
379 else {
380 target->addDrawOp(clip, std::move(op));
381 }
382 }
383 }
384 }
385
Jim Van Verth89737de2018-02-06 21:30:20 +0000386 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000387}
388
Herb Derby86240592018-05-24 16:12:31 -0400389std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400390 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
391 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500392 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
Herb Derby86240592018-05-24 16:12:31 -0400393 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400394 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400395 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500396 distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800397}
joshualitt2e2202e2015-12-10 11:22:08 -0800398
Herb Derby86240592018-05-24 16:12:31 -0400399void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800400 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700401
joshualitt2f2ee832016-02-10 08:52:24 -0800402 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
403 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700404
joshualitt2f2ee832016-02-10 08:52:24 -0800405 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
406 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
407 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700408
joshualitt2f2ee832016-02-10 08:52:24 -0800409 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800410 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
411 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
412 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
413 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700414
joshualitt2f2ee832016-02-10 08:52:24 -0800415 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700416 for (int i = 0; i < l.fRunCount; i++) {
417 const Run& lRun = l.fRuns[i];
418 const Run& rRun = r.fRuns[i];
419
joshualitt259fbf12015-07-21 11:39:34 -0700420 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800421 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500422 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700423 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800424 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700425 }
426
joshualitt259fbf12015-07-21 11:39:34 -0700427
joshualitt2f2ee832016-02-10 08:52:24 -0800428 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
429 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700430 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700431
432 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800433 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
434 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700435 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
436 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700437 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800438 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700439 }
440
441 // color can be changed
442 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800443 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700444
joshualitt2f2ee832016-02-10 08:52:24 -0800445 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700446 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
447 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
448 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
449
joshualitt2f2ee832016-02-10 08:52:24 -0800450 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
451 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700452
joshualitt2f2ee832016-02-10 08:52:24 -0800453 if (lSubRun.strike()) {
454 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500455 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
456 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800457
458 } else {
459 SkASSERT_RELEASE(!rSubRun.strike());
460 }
461
462 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
463 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
464 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
465 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
466 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
467 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
468 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700469 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500470
471 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
472 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
473 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
474 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
475
476 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
477 // We can't assert that these have the same translations
478 }
joshualitt259fbf12015-07-21 11:39:34 -0700479 }
480}
joshualitt8e0ef292016-02-19 14:13:03 -0800481
Herb Derby86240592018-05-24 16:12:31 -0400482void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800483 SkScalar x, SkScalar y, SkScalar* transX,
484 SkScalar* transY) {
485 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
486 fCurrentViewMatrix, fX, fY, transX, transY);
487 fCurrentViewMatrix = viewMatrix;
488 fX = x;
489 fY = y;
490}