blob: cc686f558af46c5469eaba5c5ef7e5340f13c49e [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"
Ben Wagner75d6db72018-09-20 14:39:39 -040021
Mike Klein79aea6a2018-06-11 10:45:26 -040022#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080023
Ben Wagner75d6db72018-09-20 14:39:39 -040024template <size_t N> static size_t sk_align(size_t s) {
25 return ((s + (N-1)) / N) * N;
26}
27
Herb Derby86240592018-05-24 16:12:31 -040028sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
29 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080030 // and size for the glyphIds array.
31 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Ben Wagner75d6db72018-09-20 14:39:39 -040032
33 size_t blob = 0;
34 size_t vertex = sk_align<alignof(char)> (blob + sizeof(GrTextBlob) * 1);
35 size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount);
36 size_t runs = sk_align<alignof(GrTextBlob::Run)>(glyphs + sizeof(GrGlyph*) * glyphCount);
37 size_t size = (runs + sizeof(GrTextBlob::Run) * runCount);
joshualitt92303772016-02-10 11:55:52 -080038
Herb Derbyb12175f2018-05-23 16:38:09 -040039 void* allocation = ::operator new (size);
40
joshualitt92303772016-02-10 11:55:52 -080041 if (CACHE_SANITY_CHECK) {
42 sk_bzero(allocation, size);
43 }
44
Herb Derby86240592018-05-24 16:12:31 -040045 sk_sp<GrTextBlob> cacheBlob(new (allocation) GrTextBlob);
joshualitt92303772016-02-10 11:55:52 -080046 cacheBlob->fSize = size;
47
48 // setup offsets for vertices / glyphs
Ben Wagner75d6db72018-09-20 14:39:39 -040049 cacheBlob->fVertices = SkTAddOffset<char>(cacheBlob.get(), vertex);
50 cacheBlob->fGlyphs = SkTAddOffset<GrGlyph*>(cacheBlob.get(), glyphs);
51 cacheBlob->fRuns = SkTAddOffset<GrTextBlob::Run>(cacheBlob.get(), runs);
joshualitt92303772016-02-10 11:55:52 -080052
53 // Initialize runs
54 for (int i = 0; i < runCount; i++) {
Herb Derby86240592018-05-24 16:12:31 -040055 new (&cacheBlob->fRuns[i]) GrTextBlob::Run;
joshualitt92303772016-02-10 11:55:52 -080056 }
57 cacheBlob->fRunCount = runCount;
joshualitt92303772016-02-10 11:55:52 -080058 return cacheBlob;
59}
60
Herb Derby86240592018-05-24 16:12:31 -040061SkExclusiveStrikePtr GrTextBlob::setupCache(int runIndex,
Herb Derby526819d2018-03-09 12:51:12 -050062 const SkSurfaceProps& props,
63 SkScalerContextFlags scalerContextFlags,
64 const SkPaint& skPaint,
65 const SkMatrix* viewMatrix) {
Herb Derby86240592018-05-24 16:12:31 -040066 GrTextBlob::Run* run = &fRuns[runIndex];
joshualitte76b4bb32015-12-28 07:23:58 -080067
68 // if we have an override descriptor for the run, then we should use that
69 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
70 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070071 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050072 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
73 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050074 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070075 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070076 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derbyfa996902018-04-18 11:36:12 -040077 return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *run->fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080078}
79
Herb Derby86240592018-05-24 16:12:31 -040080void GrTextBlob::appendGlyph(int runIndex,
Herb Derbycf370b12018-09-19 14:42:45 -040081 const SkRect& positions,
Brian Osmancf860852018-10-31 14:04:39 -040082 const SkPMColor4f& color4f,
Herb Derbycf370b12018-09-19 14:42:45 -040083 const sk_sp<GrTextStrike>& strike,
84 GrGlyph* glyph, bool preTransformed) {
Brian Osman1be2b7c2018-10-29 16:07:15 -040085 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -040086 GrColor color = color4f.toBytes_RGBA();
joshualitta06e6ab2015-12-10 08:54:41 -080087
joshualittf528e0d2015-12-09 06:42:52 -080088 Run& run = fRuns[runIndex];
89 GrMaskFormat format = glyph->fMaskFormat;
90
91 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
92 if (run.fInitialized && subRun->maskFormat() != format) {
93 subRun = &run.push_back();
Herb Derbycf370b12018-09-19 14:42:45 -040094 subRun->setStrike(strike);
joshualittf528e0d2015-12-09 06:42:52 -080095 } else if (!run.fInitialized) {
Herb Derbycf370b12018-09-19 14:42:45 -040096 subRun->setStrike(strike);
joshualittf528e0d2015-12-09 06:42:52 -080097 }
98
99 run.fInitialized = true;
100
Brian Salomon5c6ac642017-12-19 11:09:32 -0500101 bool hasW = subRun->hasWCoord();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400102 // glyphs drawn in perspective must always have a w coord.
103 SkASSERT(hasW || !fInitialViewMatrix.hasPerspective());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500104
105 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800106
107 subRun->setMaskFormat(format);
108
joshualitt7481e752016-01-22 06:08:48 -0800109 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800110 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800111
112 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
113
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114 // We always write the third position component used by SDFs. If it is unused it gets
115 // overwritten. Similarly, we always write the color and the blob will later overwrite it
116 // with texture coords if it is unused.
117 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
118 // V0
119 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
120 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
121 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800122
Brian Salomon5c6ac642017-12-19 11:09:32 -0500123 // V1
124 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
125 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
126 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800127
Brian Salomon5c6ac642017-12-19 11:09:32 -0500128 // V2
129 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, 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 // V3
134 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
135 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800136
joshualitt18b072d2015-12-07 12:26:12 -0800137 subRun->appendVertices(vertexStride);
138 fGlyphs[subRun->glyphEndIndex()] = glyph;
139 subRun->glyphAppended();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400140 subRun->setNeedsTransform(!preTransformed);
joshualitt18b072d2015-12-07 12:26:12 -0800141}
142
Herb Derby86240592018-05-24 16:12:31 -0400143void GrTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500144 SkScalar scale, bool preTransformed) {
145 Run& run = fRuns[runIndex];
Herb Derby86240592018-05-24 16:12:31 -0400146 run.fPathGlyphs.push_back(GrTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800147}
148
Herb Derby0edb2142018-10-16 17:04:11 -0400149bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
150 const SkMaskFilterBase::BlurRec& blurRec,
151 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittfd5f6c12015-12-10 07:44:50 -0800152 // If we have LCD text then our canonical color will be set to transparent, in this case we have
153 // to regenerate the blob on any color change
154 // We use the grPaint to get any color filter effects
155 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400156 fLuminanceColor != paint.computeLuminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800157 return true;
158 }
159
joshualitt8e0ef292016-02-19 14:13:03 -0800160 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800161 return true;
162 }
163
Brian Salomon5c6ac642017-12-19 11:09:32 -0500164 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800165 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800166 return true;
167 }
168
169 // We only cache one masked version
170 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400171 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800172 return true;
173 }
174
175 // Similarly, we only cache one version for each style
176 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400177 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
178 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
179 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800180 return true;
181 }
182
183 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
184 // for mixed blobs if this becomes an issue.
185 if (this->hasBitmap() && this->hasDistanceField()) {
186 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800187 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800188 return false;
189 }
190 return true;
191 }
192
193 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800194 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
195 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
196 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
197 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800198 return true;
199 }
200
Herb Derbycb65ce72018-10-17 11:39:41 -0400201 // If the text blob only has full pixel glyphs, then fractional part of the position does
202 // not affect the SkGlyphs used.
203 if (anyRunHasSubpixelPosition) {
204 // We can update the positions in the text blob without regenerating the whole
Herb Derby0edb2142018-10-16 17:04:11 -0400205 // blob, but only for integer translations.
206 // This cool bit of math will determine the necessary translation to apply to the
207 // already generated vertex coordinates to move them to the correct position.
208 SkScalar transX = viewMatrix.getTranslateX() +
209 viewMatrix.getScaleX() * (x - fInitialX) +
210 viewMatrix.getSkewX() * (y - fInitialY) -
211 fInitialViewMatrix.getTranslateX();
212 SkScalar transY = viewMatrix.getTranslateY() +
213 viewMatrix.getSkewY() * (x - fInitialX) +
214 viewMatrix.getScaleY() * (y - fInitialY) -
215 fInitialViewMatrix.getTranslateY();
216 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
217 return true;
218 }
joshualittfd5f6c12015-12-10 07:44:50 -0800219 }
joshualittfd5f6c12015-12-10 07:44:50 -0800220 } else if (this->hasDistanceField()) {
221 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
222 // distance field being generated, so we have to regenerate in those cases
223 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800224 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800225 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
226 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
227 return true;
228 }
joshualittfd5f6c12015-12-10 07:44:50 -0800229 }
230
joshualittfd5f6c12015-12-10 07:44:50 -0800231 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
232 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
233 // the blob anyways at flush time, so no need to regenerate explicitly
234 return false;
235}
236
Herb Derby86240592018-05-24 16:12:31 -0400237inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400238 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400239 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400240 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400241 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800242 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800243
Brian Salomon44acb5b2017-07-18 19:59:24 -0400244 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400245 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500246 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800247 if (info.drawAsDistanceFields()) {
Brian Osman34ec3742018-07-03 10:40:57 -0400248 // TODO: Can we be even smarter based on the dest transfer function?
Brian Salomon44acb5b2017-07-18 19:59:24 -0400249 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips7c525e62018-06-12 10:11:12 -0400250 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400251 target->colorSpaceInfo().isLinearlyBlended(), paint.computeLuminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400252 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800253 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400254 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400255 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800256 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500257 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800258 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400259 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800260 geometry.fBlob = SkRef(this);
261 geometry.fRun = run;
262 geometry.fSubRun = subRun;
Brian Osmancf860852018-10-31 14:04:39 -0400263 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800264 geometry.fX = x;
265 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500266 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400267 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800268}
269
joshualitt8e0ef292016-02-19 14:13:03 -0800270static void calculate_translation(bool applyVM,
271 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
272 const SkMatrix& currentViewMatrix, SkScalar currentX,
273 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
274 if (applyVM) {
275 *transX = newViewMatrix.getTranslateX() +
276 newViewMatrix.getScaleX() * (newX - currentX) +
277 newViewMatrix.getSkewX() * (newY - currentY) -
278 currentViewMatrix.getTranslateX();
279
280 *transY = newViewMatrix.getTranslateY() +
281 newViewMatrix.getSkewY() * (newX - currentX) +
282 newViewMatrix.getScaleY() * (newY - currentY) -
283 currentViewMatrix.getTranslateY();
284 } else {
285 *transX = newX - currentX;
286 *transY = newY - currentY;
287 }
288}
289
Herb Derbyc1b482c2018-08-09 15:02:27 -0400290void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
291 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400292 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400293 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500294
Herb Derby86240592018-05-24 16:12:31 -0400295 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500296 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
297 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Herb Derbydac1ed52018-09-12 17:04:21 -0400298 // For each run in the GrTextBlob we're going to churn through all the glyphs.
299 // Each run is broken into a path part and a Mask / DFT / ARGB part.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500300 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400301
Jim Van Verth54d9c882018-02-08 16:14:48 -0500302 Run& run = fRuns[runIndex];
303
304 // first flush any path glyphs
305 if (run.fPathGlyphs.count()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400306 SkPaint runPaint{paint};
307 runPaint.setFlags((runPaint.getFlags() & ~Run::kPaintFlagsMask) | run.fPaintFlags);
Herb Derby9f491482018-08-08 11:53:00 -0400308
Jim Van Verth54d9c882018-02-08 16:14:48 -0500309 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400310 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Robert Phillips137ca522018-08-15 10:14:33 -0400311
Herb Derbydac1ed52018-09-12 17:04:21 -0400312 SkMatrix ctm;
Robert Phillips137ca522018-08-15 10:14:33 -0400313 const SkPath* path = &pathGlyph.fPath;
Herb Derbydac1ed52018-09-12 17:04:21 -0400314
315 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400316 SkTLazy<SkPath> tmpPath;
317
Herb Derbydac1ed52018-09-12 17:04:21 -0400318 // The glyph positions and glyph outlines are either in device space or in source
319 // space based on fPreTransformed.
320 if (!pathGlyph.fPreTransformed) {
321 // Positions and outlines are in source space.
Robert Phillips137ca522018-08-15 10:14:33 -0400322
Herb Derbydac1ed52018-09-12 17:04:21 -0400323 ctm = viewMatrix;
Robert Phillipsd20d2612018-08-28 10:09:01 -0400324
Herb Derbydac1ed52018-09-12 17:04:21 -0400325 SkMatrix pathMatrix = SkMatrix::MakeScale(pathGlyph.fScale, pathGlyph.fScale);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400326
Herb Derbydac1ed52018-09-12 17:04:21 -0400327 // The origin for the blob may have changed, so figure out the delta.
328 SkVector originShift = SkPoint{x, y} - SkPoint{fInitialX, fInitialY};
329
330 // Shift the original glyph location in source space to the position of the new
331 // blob.
332 pathMatrix.postTranslate(originShift.x() + pathGlyph.fX,
333 originShift.y() + pathGlyph.fY);
334
335 // If there are shaders, blurs or styles, the path must be scaled into source
336 // space independently of the CTM. This allows the CTM to be correct for the
337 // different effects.
338 GrStyle style(runPaint);
339 bool scalePath = runPaint.getShader()
340 || style.applies()
341 || runPaint.getMaskFilter();
342 if (!scalePath) {
343 // Scale can be applied to CTM -- no effects.
344
345 ctm.preConcat(pathMatrix);
346 } else {
347 // Scale the outline into source space.
348
349 // Transform the path form the normalized outline to source space. This
350 // way the CTM will remain the same so it can be used by the effects.
351 SkPath* sourceOutline = tmpPath.init();
352 path->transform(pathMatrix, sourceOutline);
353 sourceOutline->setIsVolatile(true);
354 path = sourceOutline;
355 }
356
357
Robert Phillipsd20d2612018-08-28 10:09:01 -0400358 } else {
Herb Derbydac1ed52018-09-12 17:04:21 -0400359 // Positions and outlines are in device space.
360
361 SkPoint originalOrigin = {fInitialX, fInitialY};
362 fInitialViewMatrix.mapPoints(&originalOrigin, 1);
363
364 SkPoint newOrigin = {x, y};
365 viewMatrix.mapPoints(&newOrigin, 1);
366
367 // The origin shift in device space.
368 SkPoint originShift = newOrigin - originalOrigin;
369
370 // Shift the original glyph location in device space to the position of the
371 // new blob.
372 ctm = SkMatrix::MakeTrans(originShift.x() + pathGlyph.fX,
373 originShift.y() + pathGlyph.fY);
Robert Phillips137ca522018-08-15 10:14:33 -0400374 }
375
Robert Phillips46a13382018-08-23 13:53:01 -0400376 // TODO: we are losing the mutability of the path here
377 GrShape shape(*path, paint);
378
Herb Derbydac1ed52018-09-12 17:04:21 -0400379 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500380 }
joshualitt2e2202e2015-12-10 11:22:08 -0800381 }
joshualitt2e2202e2015-12-10 11:22:08 -0800382
Jim Van Verth54d9c882018-02-08 16:14:48 -0500383 // then flush each subrun, if any
384 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000385 continue;
386 }
Herb Derbydac1ed52018-09-12 17:04:21 -0400387
Jim Van Verth54d9c882018-02-08 16:14:48 -0500388 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
389 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
390 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
391 int glyphCount = info.glyphCount();
392 if (0 == glyphCount) {
393 continue;
394 }
395
396 bool skipClip = false;
397 bool submitOp = true;
398 SkIRect clipRect = SkIRect::MakeEmpty();
399 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
400 SkRRect clipRRect;
401 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400402 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500403 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400404 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500405 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500406 clipRRect.isRect() && GrAA::kNo == aa) {
407 skipClip = true;
408 // We only need to do clipping work if the subrun isn't contained by the clip
409 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400410 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
411 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500412 if (!clipRRect.getBounds().contains(subRunBounds)) {
413 // If the subrun is completely outside, don't add an op for it
414 if (!clipRRect.getBounds().intersects(subRunBounds)) {
415 submitOp = false;
416 }
417 else {
418 clipRRect.getBounds().round(&clipRect);
419 }
420 }
421 }
422
423 if (submitOp) {
424 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400425 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500426 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500427 if (op) {
428 if (skipClip) {
429 target->addDrawOp(GrNoClip(), std::move(op));
430 }
431 else {
432 target->addDrawOp(clip, std::move(op));
433 }
434 }
435 }
436 }
437
Jim Van Verth89737de2018-02-06 21:30:20 +0000438 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000439}
440
Herb Derby86240592018-05-24 16:12:31 -0400441std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400442 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
Brian Osmancf860852018-10-31 14:04:39 -0400443 SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400444 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400445 GrTextTarget* target) {
Herb Derby86240592018-05-24 16:12:31 -0400446 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400447 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derbybc6f9c92018-08-08 13:58:45 -0400448 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect,
449 paint, filteredColor, props, distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800450}
joshualitt2e2202e2015-12-10 11:22:08 -0800451
Herb Derby86240592018-05-24 16:12:31 -0400452void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800453 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700454
joshualitt2f2ee832016-02-10 08:52:24 -0800455 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
456 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700457
joshualitt2f2ee832016-02-10 08:52:24 -0800458 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
459 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
460 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700461
joshualitt2f2ee832016-02-10 08:52:24 -0800462 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800463 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
464 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
465 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
466 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700467
joshualitt2f2ee832016-02-10 08:52:24 -0800468 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700469 for (int i = 0; i < l.fRunCount; i++) {
470 const Run& lRun = l.fRuns[i];
471 const Run& rRun = r.fRuns[i];
472
joshualitt259fbf12015-07-21 11:39:34 -0700473 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800474 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500475 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700476 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800477 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700478 }
479
joshualitt259fbf12015-07-21 11:39:34 -0700480
joshualitt2f2ee832016-02-10 08:52:24 -0800481 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
482 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700483 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700484
485 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800486 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
487 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700488 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
489 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700490 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800491 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700492 }
493
494 // color can be changed
495 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800496 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700497
joshualitt2f2ee832016-02-10 08:52:24 -0800498 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700499 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
500 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
501 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
502
joshualitt2f2ee832016-02-10 08:52:24 -0800503 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
504 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700505
joshualitt2f2ee832016-02-10 08:52:24 -0800506 if (lSubRun.strike()) {
507 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500508 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
509 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800510
511 } else {
512 SkASSERT_RELEASE(!rSubRun.strike());
513 }
514
515 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
516 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
517 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
518 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
519 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
520 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
521 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700522 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500523
524 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
525 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
526 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
527 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
528
529 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
530 // We can't assert that these have the same translations
531 }
joshualitt259fbf12015-07-21 11:39:34 -0700532 }
533}
joshualitt8e0ef292016-02-19 14:13:03 -0800534
Herb Derby86240592018-05-24 16:12:31 -0400535void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800536 SkScalar x, SkScalar y, SkScalar* transX,
537 SkScalar* transY) {
538 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
539 fCurrentViewMatrix, fX, fY, transX, transY);
540 fCurrentViewMatrix = viewMatrix;
541 fX = x;
542 fY = y;
543}