blob: 6545f8478025502663113d4a35bc90c052027c65 [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"
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,
joshualitta06e6ab2015-12-10 08:54:41 -080075 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070076 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050077 SkScalar x, SkScalar y, SkScalar scale, 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 Derby86240592018-05-24 16:12:31 -0400140bool GrTextBlob::mustRegenerate(const GrTextUtils::Paint& 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 &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400147 fLuminanceColor != paint.luminanceColor()) {
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 &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500168 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
169 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
170 fStrokeInfo.fJoin != paint.skPaint().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,
227 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500228 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* 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,
Brian Osman34ec3742018-07-03 10:40:57 -0400238 target->colorSpaceInfo().isLinearlyBlended(), paint.luminanceColor(),
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 =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400251 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
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 Derby86240592018-05-24 16:12:31 -0400278void GrTextBlob::flush(GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500279 const GrDistanceFieldAdjustTable* distanceAdjustTable,
280 const GrTextUtils::Paint& paint, const GrClip& clip,
281 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
282 SkScalar x, SkScalar y) {
283
Herb Derby86240592018-05-24 16:12:31 -0400284 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500285 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
286 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Ben Wagner2c312c42018-06-27 14:46:46 -0400287 GrTextUtils::RunPaint runPaint(&paint);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500288 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
289 Run& run = fRuns[runIndex];
290
291 // first flush any path glyphs
292 if (run.fPathGlyphs.count()) {
293 SkScalar transX, transY;
294 uint16_t paintFlags = run.fPaintFlags;
295 if (!runPaint.modifyForRun(
296 [paintFlags](SkPaint* p) {
297 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
298 })) {
299 continue;
300 }
301 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400302 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500303 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
304 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
305 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
306 SkMatrix pathMatrix;
307 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
308 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
309 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
310 }
joshualitt2e2202e2015-12-10 11:22:08 -0800311 }
joshualitt2e2202e2015-12-10 11:22:08 -0800312
Jim Van Verth54d9c882018-02-08 16:14:48 -0500313 // then flush each subrun, if any
314 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000315 continue;
316 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500317 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
318 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
319 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
320 int glyphCount = info.glyphCount();
321 if (0 == glyphCount) {
322 continue;
323 }
324
325 bool skipClip = false;
326 bool submitOp = true;
327 SkIRect clipRect = SkIRect::MakeEmpty();
328 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
329 SkRRect clipRRect;
330 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400331 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500332 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400333 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500334 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500335 clipRRect.isRect() && GrAA::kNo == aa) {
336 skipClip = true;
337 // We only need to do clipping work if the subrun isn't contained by the clip
338 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400339 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
340 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500341 if (!clipRRect.getBounds().contains(subRunBounds)) {
342 // If the subrun is completely outside, don't add an op for it
343 if (!clipRRect.getBounds().intersects(subRunBounds)) {
344 submitOp = false;
345 }
346 else {
347 clipRRect.getBounds().round(&clipRect);
348 }
349 }
350 }
351
352 if (submitOp) {
353 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
354 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500355 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500356 if (op) {
357 if (skipClip) {
358 target->addDrawOp(GrNoClip(), std::move(op));
359 }
360 else {
361 target->addDrawOp(clip, std::move(op));
362 }
363 }
364 }
365 }
366
Jim Van Verth89737de2018-02-06 21:30:20 +0000367 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000368}
369
Herb Derby86240592018-05-24 16:12:31 -0400370std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400371 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
372 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500373 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
Herb Derby86240592018-05-24 16:12:31 -0400374 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400375 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400376 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500377 distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800378}
joshualitt2e2202e2015-12-10 11:22:08 -0800379
Herb Derby86240592018-05-24 16:12:31 -0400380void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800381 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700382
joshualitt2f2ee832016-02-10 08:52:24 -0800383 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
384 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700385
joshualitt2f2ee832016-02-10 08:52:24 -0800386 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
387 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
388 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700389
joshualitt2f2ee832016-02-10 08:52:24 -0800390 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800391 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
392 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
393 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
394 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700395
joshualitt2f2ee832016-02-10 08:52:24 -0800396 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700397 for (int i = 0; i < l.fRunCount; i++) {
398 const Run& lRun = l.fRuns[i];
399 const Run& rRun = r.fRuns[i];
400
joshualitt259fbf12015-07-21 11:39:34 -0700401 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800402 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500403 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700404 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800405 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700406 }
407
joshualitt259fbf12015-07-21 11:39:34 -0700408
joshualitt2f2ee832016-02-10 08:52:24 -0800409 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
410 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700411 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700412
413 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800414 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
415 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700416 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
417 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700418 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800419 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700420 }
421
422 // color can be changed
423 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800424 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700425
joshualitt2f2ee832016-02-10 08:52:24 -0800426 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700427 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
428 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
429 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
430
joshualitt2f2ee832016-02-10 08:52:24 -0800431 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
432 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700433
joshualitt2f2ee832016-02-10 08:52:24 -0800434 if (lSubRun.strike()) {
435 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500436 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
437 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800438
439 } else {
440 SkASSERT_RELEASE(!rSubRun.strike());
441 }
442
443 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
444 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
445 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
446 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
447 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
448 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
449 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700450 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500451
452 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
453 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
454 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
455 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
456
457 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
458 // We can't assert that these have the same translations
459 }
joshualitt259fbf12015-07-21 11:39:34 -0700460 }
461}
joshualitt8e0ef292016-02-19 14:13:03 -0800462
Herb Derby86240592018-05-24 16:12:31 -0400463void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800464 SkScalar x, SkScalar y, SkScalar* transX,
465 SkScalar* transY) {
466 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
467 fCurrentViewMatrix, fX, fY, transX, transY);
468 fCurrentViewMatrix = viewMatrix;
469 fX = x;
470 fY = y;
471}