blob: a2abfa7fa98bb8df5ab0e680d83392071a35bac4 [file] [log] [blame]
kkinnunenc6cb56f2014-06-24 00:12:27 -07001/*
2 * Copyright 2014 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
8#include "GrStencilAndCoverTextContext.h"
joshualitt1d89e8d2015-04-01 12:40:54 -07009#include "GrAtlasTextContext.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070010#include "GrDrawTarget.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070011#include "GrGpu.h"
12#include "GrPath.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070013#include "GrPathRange.h"
bsalomond309e7a2015-04-30 14:18:54 -070014#include "GrResourceProvider.h"
jvanverthaab626c2014-10-16 08:04:39 -070015#include "SkAutoKern.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070016#include "SkDraw.h"
17#include "SkDrawProcs.h"
18#include "SkGlyphCache.h"
19#include "SkGpuDevice.h"
20#include "SkPath.h"
21#include "SkTextMapStateProc.h"
cdalton855d83f2014-09-18 13:51:53 -070022#include "SkTextFormatParams.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070023
joshualitt6e8cd962015-03-20 10:30:14 -070024GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(GrContext* context,
25 SkGpuDevice* gpuDevice,
26 const SkDeviceProperties& properties)
27 : GrTextContext(context, gpuDevice, properties)
cdalton20b373c2014-12-01 08:38:55 -080028 , fStroke(SkStrokeRec::kFill_InitStyle)
29 , fQueuedGlyphCount(0)
30 , fFallbackGlyphsIdx(kGlyphBufferSize) {
kkinnunenc6cb56f2014-06-24 00:12:27 -070031}
32
joshualitt6e8cd962015-03-20 10:30:14 -070033GrStencilAndCoverTextContext*
34GrStencilAndCoverTextContext::Create(GrContext* context, SkGpuDevice* gpuDevice,
35 const SkDeviceProperties& props) {
jvanverth8c27a182014-10-14 08:45:50 -070036 GrStencilAndCoverTextContext* textContext = SkNEW_ARGS(GrStencilAndCoverTextContext,
joshualitt6e8cd962015-03-20 10:30:14 -070037 (context, gpuDevice, props));
joshualitt9bd2daf2015-04-17 09:30:06 -070038 textContext->fFallbackTextContext = GrAtlasTextContext::Create(context, gpuDevice, props,
39 false);
jvanverth8c27a182014-10-14 08:45:50 -070040
41 return textContext;
42}
43
kkinnunenc6cb56f2014-06-24 00:12:27 -070044GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
45}
46
cdaltone68f7362015-03-25 14:02:37 -070047bool GrStencilAndCoverTextContext::canDraw(const GrRenderTarget* rt,
48 const GrClip& clip,
49 const GrPaint& paint,
50 const SkPaint& skPaint,
51 const SkMatrix& viewMatrix) {
52 if (skPaint.getRasterizer()) {
jvanverth0fedb192014-10-08 09:07:27 -070053 return false;
54 }
cdaltone68f7362015-03-25 14:02:37 -070055 if (skPaint.getMaskFilter()) {
jvanverth0fedb192014-10-08 09:07:27 -070056 return false;
57 }
cdaltone68f7362015-03-25 14:02:37 -070058 if (skPaint.getPathEffect()) {
jvanverth0fedb192014-10-08 09:07:27 -070059 return false;
60 }
61
62 // No hairlines unless we can map the 1 px width to the object space.
cdaltone68f7362015-03-25 14:02:37 -070063 if (skPaint.getStyle() == SkPaint::kStroke_Style
64 && skPaint.getStrokeWidth() == 0
joshualitt5531d512014-12-17 15:50:11 -080065 && viewMatrix.hasPerspective()) {
jvanverth0fedb192014-10-08 09:07:27 -070066 return false;
67 }
68
69 // No color bitmap fonts.
70 SkScalerContext::Rec rec;
cdaltone68f7362015-03-25 14:02:37 -070071 SkScalerContext::MakeRec(skPaint, &fDeviceProperties, NULL, &rec);
jvanverth0fedb192014-10-08 09:07:27 -070072 return rec.getFormat() != SkMask::kARGB32_Format;
73}
74
joshualitt25d9c152015-02-18 12:29:52 -080075void GrStencilAndCoverTextContext::onDrawText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080076 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -080077 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -080078 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -080079 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -080080 const char text[],
81 size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070082 SkScalar x, SkScalar y,
83 const SkIRect& regionClipBounds) {
jvanverthaab626c2014-10-16 08:04:39 -070084 SkASSERT(byteLength == 0 || text != NULL);
85
86 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
87 return;
88 }
89
90 // This is the slow path, mainly used by Skia unit tests. The other
91 // backends (8888, gpu, ...) use device-space dependent glyph caches. In
92 // order to match the glyph positions that the other code paths produce, we
93 // must also use device-space dependent glyph cache. This has the
94 // side-effect that the glyph shape outline will be in device-space,
95 // too. This in turn has the side-effect that NVPR can not stroke the paths,
96 // as the stroke in NVPR is defined in object-space.
97 // NOTE: here we have following coincidence that works at the moment:
98 // - When using the device-space glyphs, the transforms we pass to NVPR
99 // instanced drawing are the global transforms, and the view transform is
100 // identity. NVPR can not use non-affine transforms in the instanced
101 // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it
102 // will turn off the use of device-space glyphs when perspective transforms
103 // are in use.
104
joshualitt6e8cd962015-03-20 10:30:14 -0700105 this->init(rt, clip, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix,
106 regionClipBounds);
jvanverthaab626c2014-10-16 08:04:39 -0700107
108 // Transform our starting point.
cdalton20b373c2014-12-01 08:38:55 -0800109 if (fUsingDeviceSpaceGlyphs) {
jvanverthaab626c2014-10-16 08:04:39 -0700110 SkPoint loc;
111 fContextInitialMatrix.mapXY(x, y, &loc);
112 x = loc.fX;
113 y = loc.fY;
114 }
115
116 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
117
jvanverthaab626c2014-10-16 08:04:39 -0700118 const char* stop = text + byteLength;
119
120 // Measure first if needed.
121 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
122 SkFixed stopX = 0;
123 SkFixed stopY = 0;
124
125 const char* textPtr = text;
126 while (textPtr < stop) {
127 // We don't need x, y here, since all subpixel variants will have the
128 // same advance.
129 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0);
130
131 stopX += glyph.fAdvanceX;
132 stopY += glyph.fAdvanceY;
133 }
134 SkASSERT(textPtr == stop);
135
136 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
137 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
138
139 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
140 alignX = SkScalarHalf(alignX);
141 alignY = SkScalarHalf(alignY);
142 }
143
144 x -= alignX;
145 y -= alignY;
146 }
147
148 SkAutoKern autokern;
149
150 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
151
152 SkFixed fx = SkScalarToFixed(x);
153 SkFixed fy = SkScalarToFixed(y);
154 while (text < stop) {
155 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
bungemand709ea82015-03-17 07:23:39 -0700156 fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700157 if (glyph.fWidth) {
cdalton20b373c2014-12-01 08:38:55 -0800158 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)));
jvanverthaab626c2014-10-16 08:04:39 -0700159 }
160
bungemand709ea82015-03-17 07:23:39 -0700161 fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio);
162 fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700163 }
164
165 this->finish();
166}
167
joshualitt25d9c152015-02-18 12:29:52 -0800168void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800169 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800170 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -0800171 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800172 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -0800173 const char text[],
174 size_t byteLength,
175 const SkScalar pos[],
176 int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -0700177 const SkPoint& offset,
178 const SkIRect& regionClipBounds) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700179 SkASSERT(byteLength == 0 || text != NULL);
180 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
181
182 // nothing to draw
183 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
184 return;
185 }
186
187 // This is the fast path. Here we do not bake in the device-transform to
188 // the glyph outline or the advances. This is because we do not need to
189 // position the glyphs at all, since the caller has done the positioning.
190 // The positioning is based on SkPaint::measureText of individual
191 // glyphs. That already uses glyph cache without device transforms. Device
192 // transform is not part of SkPaint::measureText API, and thus we use the
193 // same glyphs as what were measured.
kkinnunenc6cb56f2014-06-24 00:12:27 -0700194
joshualitt6e8cd962015-03-20 10:30:14 -0700195 this->init(rt, clip, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix,
196 regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700197
198 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
199
kkinnunenc6cb56f2014-06-24 00:12:27 -0700200 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700201
cdalton38e13ad2014-11-07 06:02:15 -0800202 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
bungeman79738cc2015-03-11 14:05:29 -0700203 SkTextAlignProc alignProc(fSkPaint.getTextAlign());
cdalton38e13ad2014-11-07 06:02:15 -0800204 while (text < stop) {
205 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
206 if (glyph.fWidth) {
207 SkPoint tmsLoc;
208 tmsProc(pos, &tmsLoc);
209 SkPoint loc;
210 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700211
cdalton20b373c2014-12-01 08:38:55 -0800212 this->appendGlyph(glyph, loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700213 }
cdalton38e13ad2014-11-07 06:02:15 -0800214 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700215 }
216
217 this->finish();
218}
219
cdalton855d83f2014-09-18 13:51:53 -0700220static GrPathRange* get_gr_glyphs(GrContext* ctx,
221 const SkTypeface* typeface,
222 const SkDescriptor* desc,
223 const SkStrokeRec& stroke) {
bsalomon8718aaf2015-02-19 07:24:21 -0800224 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
225 GrUniqueKey key;
226 GrUniqueKey::Builder builder(&key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800227 struct GlyphKey {
228 uint32_t fChecksum;
229 uint32_t fTypeface;
230 uint64_t fStroke;
231 };
232 GlyphKey* glyphKey = reinterpret_cast<GlyphKey*>(&builder[0]);
233 glyphKey->fChecksum = desc ? desc->getChecksum() : 0;
234 glyphKey->fTypeface = typeface ? typeface->uniqueID() : 0;
235 glyphKey->fStroke = GrPath::ComputeStrokeKey(stroke);
236 builder.finish();
cdalton855d83f2014-09-18 13:51:53 -0700237
238 SkAutoTUnref<GrPathRange> glyphs(
bsalomond309e7a2015-04-30 14:18:54 -0700239 static_cast<GrPathRange*>(ctx->resourceProvider()->findAndRefResourceByUniqueKey(key)));
cdalton855d83f2014-09-18 13:51:53 -0700240 if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) {
241 glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke));
bsalomond309e7a2015-04-30 14:18:54 -0700242 ctx->resourceProvider()->assignUniqueKeyToResource(key, glyphs);
cdalton855d83f2014-09-18 13:51:53 -0700243 }
244
245 return glyphs.detach();
246}
247
joshualitt25d9c152015-02-18 12:29:52 -0800248void GrStencilAndCoverTextContext::init(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800249 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800250 const GrPaint& paint,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700251 const SkPaint& skPaint,
cdaltonb2808cd2014-07-25 14:13:57 -0700252 size_t textByteLength,
joshualitt5531d512014-12-17 15:50:11 -0800253 RenderMode renderMode,
joshualitt6e8cd962015-03-20 10:30:14 -0700254 const SkMatrix& viewMatrix,
255 const SkIRect& regionClipBounds) {
256 GrTextContext::init(rt, clip, paint, skPaint, regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700257
joshualitt5531d512014-12-17 15:50:11 -0800258 fContextInitialMatrix = viewMatrix;
259 fViewMatrix = viewMatrix;
joshualitt290c09b2014-12-19 13:45:20 -0800260 fLocalMatrix = SkMatrix::I();
cdaltonb2808cd2014-07-25 14:13:57 -0700261
cdalton855d83f2014-09-18 13:51:53 -0700262 const bool otherBackendsWillDrawAsPaths =
cdaltonb2808cd2014-07-25 14:13:57 -0700263 SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700264
cdalton20b373c2014-12-01 08:38:55 -0800265 fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths &&
cdalton855d83f2014-09-18 13:51:53 -0700266 kMaxAccuracy_RenderMode == renderMode &&
267 SkToBool(fContextInitialMatrix.getType() &
268 (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700269
cdalton20b373c2014-12-01 08:38:55 -0800270 if (fUsingDeviceSpaceGlyphs) {
cdalton855d83f2014-09-18 13:51:53 -0700271 // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms.
272 SkASSERT(!fContextInitialMatrix.hasPerspective());
cdaltonb2808cd2014-07-25 14:13:57 -0700273
cdalton20b373c2014-12-01 08:38:55 -0800274 // The whole shape (including stroke) will be baked into the glyph outlines. Make
275 // NVPR just fill the baked shapes.
276 fStroke = SkStrokeRec(SkStrokeRec::kFill_InitStyle);
277
cdalton855d83f2014-09-18 13:51:53 -0700278 fTextRatio = fTextInverseRatio = 1.0f;
279
280 // Glyphs loaded by GPU path rendering have an inverted y-direction.
281 SkMatrix m;
282 m.setScale(1, -1);
joshualitt5531d512014-12-17 15:50:11 -0800283 fViewMatrix = m;
cdalton855d83f2014-09-18 13:51:53 -0700284
285 // Post-flip the initial matrix so we're left with just the flip after
286 // the paint preConcats the inverse.
287 m = fContextInitialMatrix;
288 m.postScale(1, -1);
joshualitt290c09b2014-12-19 13:45:20 -0800289 if (!m.invert(&fLocalMatrix)) {
290 SkDebugf("Not invertible!\n");
291 return;
292 }
cdalton855d83f2014-09-18 13:51:53 -0700293
cdaltone05162d2014-12-01 08:57:33 -0800294 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix,
295 true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700296 fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800297 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700298 } else {
cdalton855d83f2014-09-18 13:51:53 -0700299 // Don't bake strokes into the glyph outlines. We will stroke the glyphs
300 // using the GPU instead. This is the fast path.
cdalton20b373c2014-12-01 08:38:55 -0800301 fStroke = SkStrokeRec(fSkPaint);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700302 fSkPaint.setStyle(SkPaint::kFill_Style);
cdalton855d83f2014-09-18 13:51:53 -0700303
cdalton20b373c2014-12-01 08:38:55 -0800304 if (fStroke.isHairlineStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700305 // Approximate hairline stroke.
306 SkScalar strokeWidth = SK_Scalar1 /
307 (SkVector::Make(fContextInitialMatrix.getScaleX(),
308 fContextInitialMatrix.getSkewY()).length());
cdalton20b373c2014-12-01 08:38:55 -0800309 fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700310
311 } else if (fSkPaint.isFakeBoldText() &&
312#ifdef SK_USE_FREETYPE_EMBOLDEN
313 kMaxPerformance_RenderMode == renderMode &&
314#endif
cdalton20b373c2014-12-01 08:38:55 -0800315 SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700316
317 // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke.
318 SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(),
319 kStdFakeBoldInterpKeys,
320 kStdFakeBoldInterpValues,
321 kStdFakeBoldInterpLength);
322 SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale);
cdalton20b373c2014-12-01 08:38:55 -0800323 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
324 true /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700325
326 fSkPaint.setFakeBoldText(false);
327 }
328
329 bool canUseRawPaths;
330
331 if (otherBackendsWillDrawAsPaths || kMaxPerformance_RenderMode == renderMode) {
332 // We can draw the glyphs from canonically sized paths.
333 fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
334 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize();
335
336 // Compensate for the glyphs being scaled by fTextRatio.
cdalton20b373c2014-12-01 08:38:55 -0800337 if (!fStroke.isFillStyle()) {
338 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
339 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
cdalton855d83f2014-09-18 13:51:53 -0700340 }
341
342 fSkPaint.setLinearText(true);
343 fSkPaint.setLCDRenderText(false);
344 fSkPaint.setAutohinted(false);
345 fSkPaint.setHinting(SkPaint::kNo_Hinting);
346 fSkPaint.setSubpixelText(true);
347 fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
348
349 canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() &&
350 0 == fSkPaint.getTextSkewX() &&
351 !fSkPaint.isFakeBoldText() &&
352 !fSkPaint.isVerticalText();
353 } else {
354 fTextRatio = fTextInverseRatio = 1.0f;
355 canUseRawPaths = false;
356 }
357
358 SkMatrix textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700359 // Glyphs loaded by GPU path rendering have an inverted y-direction.
cdalton38e13ad2014-11-07 06:02:15 -0800360 textMatrix.setScale(fTextRatio, -fTextRatio);
joshualitt5531d512014-12-17 15:50:11 -0800361 fViewMatrix.preConcat(textMatrix);
joshualitt290c09b2014-12-19 13:45:20 -0800362 fLocalMatrix = textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700363
cdaltone05162d2014-12-01 08:57:33 -0800364 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700365 fGlyphs = canUseRawPaths ?
cdalton20b373c2014-12-01 08:38:55 -0800366 get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) :
cdalton855d83f2014-09-18 13:51:53 -0700367 get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800368 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700369 }
cdalton855d83f2014-09-18 13:51:53 -0700370
egdaniel8dd688b2015-01-22 10:16:09 -0800371 fStateRestore.set(&fPipelineBuilder);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700372
joshualitt44701df2015-02-23 14:44:57 -0800373 fPipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700374
375 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
376 kZero_StencilOp,
377 kZero_StencilOp,
378 kNotEqual_StencilFunc,
379 0xffff,
380 0x0000,
381 0xffff);
382
egdaniel8dd688b2015-01-22 10:16:09 -0800383 *fPipelineBuilder.stencil() = kStencilPass;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700384
cdalton20b373c2014-12-01 08:38:55 -0800385 SkASSERT(0 == fQueuedGlyphCount);
386 SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700387}
388
joshualitt5531d512014-12-17 15:50:11 -0800389bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) {
cdalton20b373c2014-12-01 08:38:55 -0800390 // The current view matrix is flipped because GPU path rendering glyphs have an
391 // inverted y-direction. Unflip the view matrix for the fallback context. If using
392 // device-space glyphs, we'll also need to restore the original view matrix since
393 // we moved that transfomation into our local glyph cache for this scenario. Also
394 // track the inverse operation so the caller can unmap the paint and glyph positions.
395 if (fUsingDeviceSpaceGlyphs) {
joshualitt5531d512014-12-17 15:50:11 -0800396 fViewMatrix = fContextInitialMatrix;
cdalton20b373c2014-12-01 08:38:55 -0800397 if (!fContextInitialMatrix.invert(inverse)) {
398 return false;
399 }
400 inverse->preScale(1, -1);
401 } else {
402 inverse->setScale(1, -1);
403 const SkMatrix& unflip = *inverse; // unflip is equal to its own inverse.
joshualitt5531d512014-12-17 15:50:11 -0800404 fViewMatrix.preConcat(unflip);
cdalton20b373c2014-12-01 08:38:55 -0800405 }
406 return true;
407}
408
409inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) {
410 if (fQueuedGlyphCount >= fFallbackGlyphsIdx) {
411 SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx);
cdaltonb2808cd2014-07-25 14:13:57 -0700412 this->flush();
413 }
414
cdalton20b373c2014-12-01 08:38:55 -0800415 // Stick the glyphs we can't draw at the end of the buffer, growing backwards.
416 int index = (SkMask::kARGB32_Format == glyph.fMaskFormat) ?
417 --fFallbackGlyphsIdx : fQueuedGlyphCount++;
cdaltonb85a0aa2014-07-21 15:32:44 -0700418
cdalton20b373c2014-12-01 08:38:55 -0800419 fGlyphIndices[index] = glyph.getGlyphID();
420 fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y());
421}
422
423static const SkScalar* get_xy_scalar_array(const SkPoint* pointArray) {
424 GR_STATIC_ASSERT(2 * sizeof(SkScalar) == sizeof(SkPoint));
425 GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
426
427 return &pointArray[0].fX;
cdaltonb85a0aa2014-07-21 15:32:44 -0700428}
429
430void GrStencilAndCoverTextContext::flush() {
cdalton20b373c2014-12-01 08:38:55 -0800431 if (fQueuedGlyphCount > 0) {
joshualitt8059eb92014-12-29 15:10:07 -0800432 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(),
433 fViewMatrix,
434 fLocalMatrix));
egdaniel8dd688b2015-01-22 10:16:09 -0800435 fDrawTarget->drawPaths(&fPipelineBuilder, pp, fGlyphs,
cdalton20b373c2014-12-01 08:38:55 -0800436 fGlyphIndices, GrPathRange::kU16_PathIndexType,
437 get_xy_scalar_array(fGlyphPositions),
438 GrPathRendering::kTranslate_PathTransformType,
439 fQueuedGlyphCount, GrPathRendering::kWinding_FillType);
440
441 fQueuedGlyphCount = 0;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700442 }
443
cdalton20b373c2014-12-01 08:38:55 -0800444 if (fFallbackGlyphsIdx < kGlyphBufferSize) {
445 int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx;
cdaltonb85a0aa2014-07-21 15:32:44 -0700446
cdalton20b373c2014-12-01 08:38:55 -0800447 GrPaint paintFallback(fPaint);
448
449 SkPaint skPaintFallback(fSkPaint);
450 if (!fUsingDeviceSpaceGlyphs) {
451 fStroke.applyToPaint(&skPaintFallback);
452 }
453 skPaintFallback.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for.
454 skPaintFallback.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
455
cdalton20b373c2014-12-01 08:38:55 -0800456 SkMatrix inverse;
joshualitt5531d512014-12-17 15:50:11 -0800457 if (this->mapToFallbackContext(&inverse)) {
cdalton20b373c2014-12-01 08:38:55 -0800458 inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount);
459 }
460
joshualitt570d2f82015-02-25 13:19:48 -0800461 fFallbackTextContext->drawPosText(fRenderTarget, fClip, paintFallback, skPaintFallback,
joshualitt25d9c152015-02-18 12:29:52 -0800462 fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx],
cdalton20b373c2014-12-01 08:38:55 -0800463 2 * fallbackGlyphCount,
464 get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]),
joshualitt6e8cd962015-03-20 10:30:14 -0700465 2, SkPoint::Make(0, 0), fRegionClipBounds);
cdalton20b373c2014-12-01 08:38:55 -0800466
467 fFallbackGlyphsIdx = kGlyphBufferSize;
468 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700469}
470
471void GrStencilAndCoverTextContext::finish() {
cdaltonb85a0aa2014-07-21 15:32:44 -0700472 this->flush();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700473
cdalton855d83f2014-09-18 13:51:53 -0700474 fGlyphs->unref();
cdaltonb85a0aa2014-07-21 15:32:44 -0700475 fGlyphs = NULL;
cdalton855d83f2014-09-18 13:51:53 -0700476
477 SkGlyphCache::AttachCache(fGlyphCache);
cdaltonb85a0aa2014-07-21 15:32:44 -0700478 fGlyphCache = NULL;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700479
egdaniel8dd688b2015-01-22 10:16:09 -0800480 fPipelineBuilder.stencil()->setDisabled();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700481 fStateRestore.set(NULL);
joshualitt5531d512014-12-17 15:50:11 -0800482 fViewMatrix = fContextInitialMatrix;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700483 GrTextContext::finish();
484}
485