blob: 53b3b4d74fcabfccd5c8a1c0ca38699290b38cc2 [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"
Herb Derbyc1b482c2018-08-09 15:02:27 -040012#include "GrTextTarget.h"
joshualitt2e2202e2015-12-10 11:22:08 -080013#include "SkColorFilter.h"
joshualitte76b4bb32015-12-28 07:23:58 -080014#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050015#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050016#include "SkPaintPriv.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050017#include "SkTextToPathIter.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrAtlasTextOp.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040019#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080020
Herb Derby86240592018-05-24 16:12:31 -040021sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
22 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080023 // and size for the glyphIds array.
24 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Herb Derby86240592018-05-24 16:12:31 -040025 size_t size = sizeof(GrTextBlob) +
joshualitt92303772016-02-10 11:55:52 -080026 verticesCount +
27 glyphCount * sizeof(GrGlyph**) +
Herb Derby86240592018-05-24 16:12:31 -040028 sizeof(GrTextBlob::Run) * runCount;
joshualitt92303772016-02-10 11:55:52 -080029
Herb Derbyb12175f2018-05-23 16:38:09 -040030 void* allocation = ::operator new (size);
31
joshualitt92303772016-02-10 11:55:52 -080032 if (CACHE_SANITY_CHECK) {
33 sk_bzero(allocation, size);
34 }
35
Herb Derby86240592018-05-24 16:12:31 -040036 sk_sp<GrTextBlob> cacheBlob(new (allocation) GrTextBlob);
joshualitt92303772016-02-10 11:55:52 -080037 cacheBlob->fSize = size;
38
39 // setup offsets for vertices / glyphs
Herb Derby86240592018-05-24 16:12:31 -040040 cacheBlob->fVertices = sizeof(GrTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080041 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
Herb Derby86240592018-05-24 16:12:31 -040042 cacheBlob->fRuns = reinterpret_cast<GrTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualitt92303772016-02-10 11:55:52 -080043
44 // Initialize runs
45 for (int i = 0; i < runCount; i++) {
Herb Derby86240592018-05-24 16:12:31 -040046 new (&cacheBlob->fRuns[i]) GrTextBlob::Run;
joshualitt92303772016-02-10 11:55:52 -080047 }
48 cacheBlob->fRunCount = runCount;
joshualitt92303772016-02-10 11:55:52 -080049 return cacheBlob;
50}
51
Herb Derby86240592018-05-24 16:12:31 -040052SkExclusiveStrikePtr GrTextBlob::setupCache(int runIndex,
Herb Derby526819d2018-03-09 12:51:12 -050053 const SkSurfaceProps& props,
54 SkScalerContextFlags scalerContextFlags,
55 const SkPaint& skPaint,
56 const SkMatrix* viewMatrix) {
Herb Derby86240592018-05-24 16:12:31 -040057 GrTextBlob::Run* run = &fRuns[runIndex];
joshualitte76b4bb32015-12-28 07:23:58 -080058
59 // if we have an override descriptor for the run, then we should use that
60 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
61 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070062 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050063 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
64 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050065 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070066 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070067 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derbyfa996902018-04-18 11:36:12 -040068 return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *run->fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080069}
70
Herb Derby86240592018-05-24 16:12:31 -040071void GrTextBlob::appendGlyph(int runIndex,
joshualittf528e0d2015-12-09 06:42:52 -080072 const SkRect& positions,
73 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050074 sk_sp<GrTextStrike> strike,
Herb Derbyae64e492018-08-06 14:58:25 -040075 GrGlyph* glyph, bool preTransformed) {
joshualitta06e6ab2015-12-10 08:54:41 -080076
joshualittf528e0d2015-12-09 06:42:52 -080077 Run& run = fRuns[runIndex];
78 GrMaskFormat format = glyph->fMaskFormat;
79
80 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
81 if (run.fInitialized && subRun->maskFormat() != format) {
82 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050083 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -080084 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050085 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -080086 }
87
88 run.fInitialized = true;
89
Brian Salomon5c6ac642017-12-19 11:09:32 -050090 bool hasW = subRun->hasWCoord();
Jim Van Verthb515ae72018-05-23 16:44:55 -040091 // glyphs drawn in perspective must always have a w coord.
92 SkASSERT(hasW || !fInitialViewMatrix.hasPerspective());
Brian Salomon5c6ac642017-12-19 11:09:32 -050093
94 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -080095
96 subRun->setMaskFormat(format);
97
joshualitt7481e752016-01-22 06:08:48 -080098 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -080099 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800100
101 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
102
Brian Salomon5c6ac642017-12-19 11:09:32 -0500103 // We always write the third position component used by SDFs. If it is unused it gets
104 // overwritten. Similarly, we always write the color and the blob will later overwrite it
105 // with texture coords if it is unused.
106 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
107 // V0
108 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
109 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
110 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800111
Brian Salomon5c6ac642017-12-19 11:09:32 -0500112 // V1
113 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
114 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
115 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800116
Brian Salomon5c6ac642017-12-19 11:09:32 -0500117 // V2
118 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
119 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
120 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800121
Brian Salomon5c6ac642017-12-19 11:09:32 -0500122 // V3
123 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
124 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800125
joshualitt18b072d2015-12-07 12:26:12 -0800126 subRun->appendVertices(vertexStride);
127 fGlyphs[subRun->glyphEndIndex()] = glyph;
128 subRun->glyphAppended();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400129 subRun->setNeedsTransform(!preTransformed);
joshualitt18b072d2015-12-07 12:26:12 -0800130}
131
Herb Derby86240592018-05-24 16:12:31 -0400132void GrTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500133 SkScalar scale, bool preTransformed) {
134 Run& run = fRuns[runIndex];
Herb Derby86240592018-05-24 16:12:31 -0400135 run.fPathGlyphs.push_back(GrTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800136}
137
Herb Derbybc6f9c92018-08-08 13:58:45 -0400138bool GrTextBlob::mustRegenerate(const SkPaint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500139 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800140 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
141 // If we have LCD text then our canonical color will be set to transparent, in this case we have
142 // to regenerate the blob on any color change
143 // We use the grPaint to get any color filter effects
144 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400145 fLuminanceColor != paint.computeLuminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800146 return true;
147 }
148
joshualitt8e0ef292016-02-19 14:13:03 -0800149 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800150 return true;
151 }
152
Brian Salomon5c6ac642017-12-19 11:09:32 -0500153 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800154 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800155 return true;
156 }
157
158 // We only cache one masked version
159 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400160 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800161 return true;
162 }
163
164 // Similarly, we only cache one version for each style
165 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400166 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
167 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
168 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800169 return true;
170 }
171
172 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
173 // for mixed blobs if this becomes an issue.
174 if (this->hasBitmap() && this->hasDistanceField()) {
175 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800176 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800177 return false;
178 }
179 return true;
180 }
181
182 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800183 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
184 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
185 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
186 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800187 return true;
188 }
189
190 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
191 // but only for integer translations.
192 // This cool bit of math will determine the necessary translation to apply to the already
193 // generated vertex coordinates to move them to the correct position
194 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800195 viewMatrix.getScaleX() * (x - fInitialX) +
196 viewMatrix.getSkewX() * (y - fInitialY) -
197 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800198 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800199 viewMatrix.getSkewY() * (x - fInitialX) +
200 viewMatrix.getScaleY() * (y - fInitialY) -
201 fInitialViewMatrix.getTranslateY();
202 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800203 return true;
204 }
joshualittfd5f6c12015-12-10 07:44:50 -0800205 } else if (this->hasDistanceField()) {
206 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
207 // distance field being generated, so we have to regenerate in those cases
208 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800209 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800210 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
211 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
212 return true;
213 }
joshualittfd5f6c12015-12-10 07:44:50 -0800214 }
215
joshualittfd5f6c12015-12-10 07:44:50 -0800216 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
217 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
218 // the blob anyways at flush time, so no need to regenerate explicitly
219 return false;
220}
221
Herb Derby86240592018-05-24 16:12:31 -0400222inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400223 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400224 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400225 const SkPaint& paint, GrColor filteredColor, const SkSurfaceProps& props,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400226 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800227 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800228
Brian Salomon44acb5b2017-07-18 19:59:24 -0400229 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400230 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500231 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800232 if (info.drawAsDistanceFields()) {
Brian Osman34ec3742018-07-03 10:40:57 -0400233 // TODO: Can we be even smarter based on the dest transfer function?
Brian Salomon44acb5b2017-07-18 19:59:24 -0400234 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips7c525e62018-06-12 10:11:12 -0400235 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400236 target->colorSpaceInfo().isLinearlyBlended(), paint.computeLuminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400237 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800238 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400239 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400240 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800241 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500242 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800243 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400244 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800245 geometry.fBlob = SkRef(this);
246 geometry.fRun = run;
247 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500248 geometry.fColor =
Herb Derbybc6f9c92018-08-08 13:58:45 -0400249 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : filteredColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800250 geometry.fX = x;
251 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500252 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400253 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800254}
255
joshualitt8e0ef292016-02-19 14:13:03 -0800256static void calculate_translation(bool applyVM,
257 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
258 const SkMatrix& currentViewMatrix, SkScalar currentX,
259 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
260 if (applyVM) {
261 *transX = newViewMatrix.getTranslateX() +
262 newViewMatrix.getScaleX() * (newX - currentX) +
263 newViewMatrix.getSkewX() * (newY - currentY) -
264 currentViewMatrix.getTranslateX();
265
266 *transY = newViewMatrix.getTranslateY() +
267 newViewMatrix.getSkewY() * (newX - currentX) +
268 newViewMatrix.getScaleY() * (newY - currentY) -
269 currentViewMatrix.getTranslateY();
270 } else {
271 *transX = newX - currentX;
272 *transY = newY - currentY;
273 }
274}
275
Herb Derbyc1b482c2018-08-09 15:02:27 -0400276void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
277 const GrDistanceFieldAdjustTable* distanceAdjustTable,
278 const SkPaint& paint, GrColor filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400279 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500280
Herb Derby86240592018-05-24 16:12:31 -0400281 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500282 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
283 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Herb Derby9f491482018-08-08 11:53:00 -0400284 SkPaint runPaint{paint};
Jim Van Verth54d9c882018-02-08 16:14:48 -0500285 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
286 Run& run = fRuns[runIndex];
287
288 // first flush any path glyphs
289 if (run.fPathGlyphs.count()) {
290 SkScalar transX, transY;
291 uint16_t paintFlags = run.fPaintFlags;
Herb Derby9f491482018-08-08 11:53:00 -0400292 runPaint.setFlags((runPaint.getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
293
Jim Van Verth54d9c882018-02-08 16:14:48 -0500294 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400295 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500296 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
297 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
298 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
299 SkMatrix pathMatrix;
300 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
301 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
Robert Phillipse4643cc2018-08-14 13:01:29 -0400302 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500303 }
joshualitt2e2202e2015-12-10 11:22:08 -0800304 }
joshualitt2e2202e2015-12-10 11:22:08 -0800305
Jim Van Verth54d9c882018-02-08 16:14:48 -0500306 // then flush each subrun, if any
307 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000308 continue;
309 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500310 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
311 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
312 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
313 int glyphCount = info.glyphCount();
314 if (0 == glyphCount) {
315 continue;
316 }
317
318 bool skipClip = false;
319 bool submitOp = true;
320 SkIRect clipRect = SkIRect::MakeEmpty();
321 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
322 SkRRect clipRRect;
323 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400324 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500325 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400326 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500327 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500328 clipRRect.isRect() && GrAA::kNo == aa) {
329 skipClip = true;
330 // We only need to do clipping work if the subrun isn't contained by the clip
331 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400332 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
333 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500334 if (!clipRRect.getBounds().contains(subRunBounds)) {
335 // If the subrun is completely outside, don't add an op for it
336 if (!clipRRect.getBounds().intersects(subRunBounds)) {
337 submitOp = false;
338 }
339 else {
340 clipRRect.getBounds().round(&clipRect);
341 }
342 }
343 }
344
345 if (submitOp) {
346 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400347 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500348 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500349 if (op) {
350 if (skipClip) {
351 target->addDrawOp(GrNoClip(), std::move(op));
352 }
353 else {
354 target->addDrawOp(clip, std::move(op));
355 }
356 }
357 }
358 }
359
Jim Van Verth89737de2018-02-06 21:30:20 +0000360 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000361}
362
Herb Derby86240592018-05-24 16:12:31 -0400363std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400364 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400365 SkScalar x, SkScalar y, const SkPaint& paint, GrColor filteredColor,
366 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400367 GrTextTarget* target) {
Herb Derby86240592018-05-24 16:12:31 -0400368 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400369 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derbybc6f9c92018-08-08 13:58:45 -0400370 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect,
371 paint, filteredColor, props, distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800372}
joshualitt2e2202e2015-12-10 11:22:08 -0800373
Herb Derby86240592018-05-24 16:12:31 -0400374void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800375 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700376
joshualitt2f2ee832016-02-10 08:52:24 -0800377 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
378 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700379
joshualitt2f2ee832016-02-10 08:52:24 -0800380 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
381 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
382 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700383
joshualitt2f2ee832016-02-10 08:52:24 -0800384 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800385 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
386 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
387 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
388 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700389
joshualitt2f2ee832016-02-10 08:52:24 -0800390 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700391 for (int i = 0; i < l.fRunCount; i++) {
392 const Run& lRun = l.fRuns[i];
393 const Run& rRun = r.fRuns[i];
394
joshualitt259fbf12015-07-21 11:39:34 -0700395 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800396 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500397 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700398 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800399 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700400 }
401
joshualitt259fbf12015-07-21 11:39:34 -0700402
joshualitt2f2ee832016-02-10 08:52:24 -0800403 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
404 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700405 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700406
407 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800408 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
409 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700410 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
411 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700412 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800413 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700414 }
415
416 // color can be changed
417 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800418 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700419
joshualitt2f2ee832016-02-10 08:52:24 -0800420 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700421 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
422 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
423 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
424
joshualitt2f2ee832016-02-10 08:52:24 -0800425 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
426 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700427
joshualitt2f2ee832016-02-10 08:52:24 -0800428 if (lSubRun.strike()) {
429 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500430 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
431 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800432
433 } else {
434 SkASSERT_RELEASE(!rSubRun.strike());
435 }
436
437 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
438 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
439 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
440 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
441 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
442 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
443 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700444 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500445
446 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
447 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
448 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
449 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
450
451 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
452 // We can't assert that these have the same translations
453 }
joshualitt259fbf12015-07-21 11:39:34 -0700454 }
455}
joshualitt8e0ef292016-02-19 14:13:03 -0800456
Herb Derby86240592018-05-24 16:12:31 -0400457void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800458 SkScalar x, SkScalar y, SkScalar* transX,
459 SkScalar* transY) {
460 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
461 fCurrentViewMatrix, fX, fY, transX, transY);
462 fCurrentViewMatrix = viewMatrix;
463 fX = x;
464 fY = y;
465}