blob: 7a1b3f7e8abd9c619d02f382baa41543df75f261 [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"
Robert Phillips46a13382018-08-23 13:53:01 -040012#include "GrShape.h"
Robert Phillips137ca522018-08-15 10:14:33 -040013#include "GrStyle.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040014#include "GrTextTarget.h"
joshualitt2e2202e2015-12-10 11:22:08 -080015#include "SkColorFilter.h"
joshualitte76b4bb32015-12-28 07:23:58 -080016#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050017#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050018#include "SkPaintPriv.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,
Herb Derbyae64e492018-08-06 14:58:25 -040077 GrGlyph* glyph, bool preTransformed) {
joshualitta06e6ab2015-12-10 08:54:41 -080078
joshualittf528e0d2015-12-09 06:42:52 -080079 Run& run = fRuns[runIndex];
80 GrMaskFormat format = glyph->fMaskFormat;
81
82 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
83 if (run.fInitialized && subRun->maskFormat() != format) {
84 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050085 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -080086 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050087 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -080088 }
89
90 run.fInitialized = true;
91
Brian Salomon5c6ac642017-12-19 11:09:32 -050092 bool hasW = subRun->hasWCoord();
Jim Van Verthb515ae72018-05-23 16:44:55 -040093 // glyphs drawn in perspective must always have a w coord.
94 SkASSERT(hasW || !fInitialViewMatrix.hasPerspective());
Brian Salomon5c6ac642017-12-19 11:09:32 -050095
96 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -080097
98 subRun->setMaskFormat(format);
99
joshualitt7481e752016-01-22 06:08:48 -0800100 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800101 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800102
103 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
104
Brian Salomon5c6ac642017-12-19 11:09:32 -0500105 // We always write the third position component used by SDFs. If it is unused it gets
106 // overwritten. Similarly, we always write the color and the blob will later overwrite it
107 // with texture coords if it is unused.
108 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
109 // V0
110 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
111 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
112 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800113
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114 // V1
115 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
116 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
117 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800118
Brian Salomon5c6ac642017-12-19 11:09:32 -0500119 // V2
120 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
121 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
122 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800123
Brian Salomon5c6ac642017-12-19 11:09:32 -0500124 // V3
125 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
126 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800127
joshualitt18b072d2015-12-07 12:26:12 -0800128 subRun->appendVertices(vertexStride);
129 fGlyphs[subRun->glyphEndIndex()] = glyph;
130 subRun->glyphAppended();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400131 subRun->setNeedsTransform(!preTransformed);
joshualitt18b072d2015-12-07 12:26:12 -0800132}
133
Herb Derby86240592018-05-24 16:12:31 -0400134void GrTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500135 SkScalar scale, bool preTransformed) {
136 Run& run = fRuns[runIndex];
Herb Derby86240592018-05-24 16:12:31 -0400137 run.fPathGlyphs.push_back(GrTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800138}
139
Herb Derbybc6f9c92018-08-08 13:58:45 -0400140bool GrTextBlob::mustRegenerate(const SkPaint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500141 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800142 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
143 // If we have LCD text then our canonical color will be set to transparent, in this case we have
144 // to regenerate the blob on any color change
145 // We use the grPaint to get any color filter effects
146 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400147 fLuminanceColor != paint.computeLuminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800148 return true;
149 }
150
joshualitt8e0ef292016-02-19 14:13:03 -0800151 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800152 return true;
153 }
154
Brian Salomon5c6ac642017-12-19 11:09:32 -0500155 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800156 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800157 return true;
158 }
159
160 // We only cache one masked version
161 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400162 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800163 return true;
164 }
165
166 // Similarly, we only cache one version for each style
167 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400168 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
169 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
170 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800171 return true;
172 }
173
174 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
175 // for mixed blobs if this becomes an issue.
176 if (this->hasBitmap() && this->hasDistanceField()) {
177 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800178 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800179 return false;
180 }
181 return true;
182 }
183
184 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800185 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
186 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
187 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
188 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800189 return true;
190 }
191
192 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
193 // but only for integer translations.
194 // This cool bit of math will determine the necessary translation to apply to the already
195 // generated vertex coordinates to move them to the correct position
196 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800197 viewMatrix.getScaleX() * (x - fInitialX) +
198 viewMatrix.getSkewX() * (y - fInitialY) -
199 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800200 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800201 viewMatrix.getSkewY() * (x - fInitialX) +
202 viewMatrix.getScaleY() * (y - fInitialY) -
203 fInitialViewMatrix.getTranslateY();
204 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800205 return true;
206 }
joshualittfd5f6c12015-12-10 07:44:50 -0800207 } else if (this->hasDistanceField()) {
208 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
209 // distance field being generated, so we have to regenerate in those cases
210 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800211 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800212 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
213 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
214 return true;
215 }
joshualittfd5f6c12015-12-10 07:44:50 -0800216 }
217
joshualittfd5f6c12015-12-10 07:44:50 -0800218 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
219 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
220 // the blob anyways at flush time, so no need to regenerate explicitly
221 return false;
222}
223
Herb Derby86240592018-05-24 16:12:31 -0400224inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400225 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400226 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400227 const SkPaint& paint, GrColor filteredColor, const SkSurfaceProps& props,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400228 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800229 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800230
Brian Salomon44acb5b2017-07-18 19:59:24 -0400231 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400232 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500233 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800234 if (info.drawAsDistanceFields()) {
Brian Osman34ec3742018-07-03 10:40:57 -0400235 // TODO: Can we be even smarter based on the dest transfer function?
Brian Salomon44acb5b2017-07-18 19:59:24 -0400236 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips7c525e62018-06-12 10:11:12 -0400237 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400238 target->colorSpaceInfo().isLinearlyBlended(), paint.computeLuminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400239 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800240 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400241 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400242 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800243 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500244 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800245 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400246 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800247 geometry.fBlob = SkRef(this);
248 geometry.fRun = run;
249 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500250 geometry.fColor =
Herb Derbybc6f9c92018-08-08 13:58:45 -0400251 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : filteredColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800252 geometry.fX = x;
253 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500254 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400255 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800256}
257
joshualitt8e0ef292016-02-19 14:13:03 -0800258static void calculate_translation(bool applyVM,
259 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
260 const SkMatrix& currentViewMatrix, SkScalar currentX,
261 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
262 if (applyVM) {
263 *transX = newViewMatrix.getTranslateX() +
264 newViewMatrix.getScaleX() * (newX - currentX) +
265 newViewMatrix.getSkewX() * (newY - currentY) -
266 currentViewMatrix.getTranslateX();
267
268 *transY = newViewMatrix.getTranslateY() +
269 newViewMatrix.getSkewY() * (newX - currentX) +
270 newViewMatrix.getScaleY() * (newY - currentY) -
271 currentViewMatrix.getTranslateY();
272 } else {
273 *transX = newX - currentX;
274 *transY = newY - currentY;
275 }
276}
277
Herb Derbyc1b482c2018-08-09 15:02:27 -0400278void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
279 const GrDistanceFieldAdjustTable* distanceAdjustTable,
280 const SkPaint& paint, GrColor filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400281 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500282
Herb Derby86240592018-05-24 16:12:31 -0400283 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500284 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
285 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Herb Derby9f491482018-08-08 11:53:00 -0400286 SkPaint runPaint{paint};
Jim Van Verth54d9c882018-02-08 16:14:48 -0500287 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
288 Run& run = fRuns[runIndex];
289
290 // first flush any path glyphs
291 if (run.fPathGlyphs.count()) {
292 SkScalar transX, transY;
293 uint16_t paintFlags = run.fPaintFlags;
Herb Derby9f491482018-08-08 11:53:00 -0400294 runPaint.setFlags((runPaint.getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
295
Jim Van Verth54d9c882018-02-08 16:14:48 -0500296 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400297 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500298 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
299 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
Robert Phillips137ca522018-08-15 10:14:33 -0400300
301 const SkMatrix* ctm = pathGlyph.fPreTransformed ? &SkMatrix::I() : &viewMatrix;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500302 SkMatrix pathMatrix;
303 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
304 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
Robert Phillips137ca522018-08-15 10:14:33 -0400305
306 const SkPath* path = &pathGlyph.fPath;
Robert Phillips137ca522018-08-15 10:14:33 -0400307 SkTLazy<SkPath> tmpPath;
308
309 GrStyle style(runPaint);
310
311 // Styling, blurs, and shading are supposed to be applied *after* the pathMatrix.
312 if (!runPaint.getMaskFilter() && !runPaint.getShader() && !style.applies()) {
313 pathMatrix.postConcat(*ctm);
314 ctm = &pathMatrix;
315 } else {
316 SkPath* result = tmpPath.init();
317 path->transform(pathMatrix, result);
318 result->setIsVolatile(true);
319 path = result;
Robert Phillips137ca522018-08-15 10:14:33 -0400320 }
321
Robert Phillips46a13382018-08-23 13:53:01 -0400322 // TODO: we are losing the mutability of the path here
323 GrShape shape(*path, paint);
324
325 target->drawShape(clip, runPaint, *ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500326 }
joshualitt2e2202e2015-12-10 11:22:08 -0800327 }
joshualitt2e2202e2015-12-10 11:22:08 -0800328
Jim Van Verth54d9c882018-02-08 16:14:48 -0500329 // then flush each subrun, if any
330 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000331 continue;
332 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500333 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
334 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
335 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
336 int glyphCount = info.glyphCount();
337 if (0 == glyphCount) {
338 continue;
339 }
340
341 bool skipClip = false;
342 bool submitOp = true;
343 SkIRect clipRect = SkIRect::MakeEmpty();
344 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
345 SkRRect clipRRect;
346 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400347 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500348 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400349 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500350 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500351 clipRRect.isRect() && GrAA::kNo == aa) {
352 skipClip = true;
353 // We only need to do clipping work if the subrun isn't contained by the clip
354 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400355 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
356 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500357 if (!clipRRect.getBounds().contains(subRunBounds)) {
358 // If the subrun is completely outside, don't add an op for it
359 if (!clipRRect.getBounds().intersects(subRunBounds)) {
360 submitOp = false;
361 }
362 else {
363 clipRRect.getBounds().round(&clipRect);
364 }
365 }
366 }
367
368 if (submitOp) {
369 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400370 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500371 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500372 if (op) {
373 if (skipClip) {
374 target->addDrawOp(GrNoClip(), std::move(op));
375 }
376 else {
377 target->addDrawOp(clip, std::move(op));
378 }
379 }
380 }
381 }
382
Jim Van Verth89737de2018-02-06 21:30:20 +0000383 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000384}
385
Herb Derby86240592018-05-24 16:12:31 -0400386std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400387 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400388 SkScalar x, SkScalar y, const SkPaint& paint, GrColor filteredColor,
389 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400390 GrTextTarget* target) {
Herb Derby86240592018-05-24 16:12:31 -0400391 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400392 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derbybc6f9c92018-08-08 13:58:45 -0400393 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect,
394 paint, filteredColor, props, distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800395}
joshualitt2e2202e2015-12-10 11:22:08 -0800396
Herb Derby86240592018-05-24 16:12:31 -0400397void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800398 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700399
joshualitt2f2ee832016-02-10 08:52:24 -0800400 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
401 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700402
joshualitt2f2ee832016-02-10 08:52:24 -0800403 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
404 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
405 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700406
joshualitt2f2ee832016-02-10 08:52:24 -0800407 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800408 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
409 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
410 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
411 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700412
joshualitt2f2ee832016-02-10 08:52:24 -0800413 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700414 for (int i = 0; i < l.fRunCount; i++) {
415 const Run& lRun = l.fRuns[i];
416 const Run& rRun = r.fRuns[i];
417
joshualitt259fbf12015-07-21 11:39:34 -0700418 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800419 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500420 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700421 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800422 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700423 }
424
joshualitt259fbf12015-07-21 11:39:34 -0700425
joshualitt2f2ee832016-02-10 08:52:24 -0800426 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
427 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700428 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700429
430 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800431 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
432 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700433 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
434 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700435 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800436 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700437 }
438
439 // color can be changed
440 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800441 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700442
joshualitt2f2ee832016-02-10 08:52:24 -0800443 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700444 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
445 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
446 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
447
joshualitt2f2ee832016-02-10 08:52:24 -0800448 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
449 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700450
joshualitt2f2ee832016-02-10 08:52:24 -0800451 if (lSubRun.strike()) {
452 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500453 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
454 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800455
456 } else {
457 SkASSERT_RELEASE(!rSubRun.strike());
458 }
459
460 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
461 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
462 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
463 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
464 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
465 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
466 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700467 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500468
469 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
470 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
471 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
472 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
473
474 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
475 // We can't assert that these have the same translations
476 }
joshualitt259fbf12015-07-21 11:39:34 -0700477 }
478}
joshualitt8e0ef292016-02-19 14:13:03 -0800479
Herb Derby86240592018-05-24 16:12:31 -0400480void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800481 SkScalar x, SkScalar y, SkScalar* transX,
482 SkScalar* transY) {
483 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
484 fCurrentViewMatrix, fX, fY, transX, transY);
485 fCurrentViewMatrix = viewMatrix;
486 fX = x;
487 fY = y;
488}