blob: 9c3ba6af8a4b50738bcad8201840a8010f99634a [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"
jvanverth8c27a182014-10-14 08:45:50 -07009#include "GrBitmapTextContext.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"
jvanverthaab626c2014-10-16 08:04:39 -070014#include "SkAutoKern.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070015#include "SkDraw.h"
16#include "SkDrawProcs.h"
17#include "SkGlyphCache.h"
18#include "SkGpuDevice.h"
19#include "SkPath.h"
20#include "SkTextMapStateProc.h"
cdalton855d83f2014-09-18 13:51:53 -070021#include "SkTextFormatParams.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070022
23GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(
24 GrContext* context, const SkDeviceProperties& properties)
25 : GrTextContext(context, properties)
cdalton20b373c2014-12-01 08:38:55 -080026 , fStroke(SkStrokeRec::kFill_InitStyle)
27 , fQueuedGlyphCount(0)
28 , fFallbackGlyphsIdx(kGlyphBufferSize) {
kkinnunenc6cb56f2014-06-24 00:12:27 -070029}
30
jvanverth8c27a182014-10-14 08:45:50 -070031GrStencilAndCoverTextContext* GrStencilAndCoverTextContext::Create(GrContext* context,
32 const SkDeviceProperties& props) {
33 GrStencilAndCoverTextContext* textContext = SkNEW_ARGS(GrStencilAndCoverTextContext,
34 (context, props));
35 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, props);
36
37 return textContext;
38}
39
kkinnunenc6cb56f2014-06-24 00:12:27 -070040GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
41}
42
joshualitt5531d512014-12-17 15:50:11 -080043bool GrStencilAndCoverTextContext::canDraw(const SkPaint& paint, const SkMatrix& viewMatrix) {
jvanverth0fedb192014-10-08 09:07:27 -070044 if (paint.getRasterizer()) {
45 return false;
46 }
47 if (paint.getMaskFilter()) {
48 return false;
49 }
50 if (paint.getPathEffect()) {
51 return false;
52 }
53
54 // No hairlines unless we can map the 1 px width to the object space.
55 if (paint.getStyle() == SkPaint::kStroke_Style
56 && paint.getStrokeWidth() == 0
joshualitt5531d512014-12-17 15:50:11 -080057 && viewMatrix.hasPerspective()) {
jvanverth0fedb192014-10-08 09:07:27 -070058 return false;
59 }
60
61 // No color bitmap fonts.
62 SkScalerContext::Rec rec;
63 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
64 return rec.getFormat() != SkMask::kARGB32_Format;
65}
66
joshualitt25d9c152015-02-18 12:29:52 -080067void GrStencilAndCoverTextContext::onDrawText(GrRenderTarget* rt,
68 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -080069 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -080070 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -080071 const char text[],
72 size_t byteLength,
73 SkScalar x, SkScalar y) {
jvanverthaab626c2014-10-16 08:04:39 -070074 SkASSERT(byteLength == 0 || text != NULL);
75
76 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
77 return;
78 }
79
80 // This is the slow path, mainly used by Skia unit tests. The other
81 // backends (8888, gpu, ...) use device-space dependent glyph caches. In
82 // order to match the glyph positions that the other code paths produce, we
83 // must also use device-space dependent glyph cache. This has the
84 // side-effect that the glyph shape outline will be in device-space,
85 // too. This in turn has the side-effect that NVPR can not stroke the paths,
86 // as the stroke in NVPR is defined in object-space.
87 // NOTE: here we have following coincidence that works at the moment:
88 // - When using the device-space glyphs, the transforms we pass to NVPR
89 // instanced drawing are the global transforms, and the view transform is
90 // identity. NVPR can not use non-affine transforms in the instanced
91 // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it
92 // will turn off the use of device-space glyphs when perspective transforms
93 // are in use.
94
joshualitt25d9c152015-02-18 12:29:52 -080095 this->init(rt, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix);
jvanverthaab626c2014-10-16 08:04:39 -070096
97 // Transform our starting point.
cdalton20b373c2014-12-01 08:38:55 -080098 if (fUsingDeviceSpaceGlyphs) {
jvanverthaab626c2014-10-16 08:04:39 -070099 SkPoint loc;
100 fContextInitialMatrix.mapXY(x, y, &loc);
101 x = loc.fX;
102 y = loc.fY;
103 }
104
105 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
106
jvanverthaab626c2014-10-16 08:04:39 -0700107 const char* stop = text + byteLength;
108
109 // Measure first if needed.
110 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
111 SkFixed stopX = 0;
112 SkFixed stopY = 0;
113
114 const char* textPtr = text;
115 while (textPtr < stop) {
116 // We don't need x, y here, since all subpixel variants will have the
117 // same advance.
118 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0);
119
120 stopX += glyph.fAdvanceX;
121 stopY += glyph.fAdvanceY;
122 }
123 SkASSERT(textPtr == stop);
124
125 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
126 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
127
128 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
129 alignX = SkScalarHalf(alignX);
130 alignY = SkScalarHalf(alignY);
131 }
132
133 x -= alignX;
134 y -= alignY;
135 }
136
137 SkAutoKern autokern;
138
139 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
140
141 SkFixed fx = SkScalarToFixed(x);
142 SkFixed fy = SkScalarToFixed(y);
143 while (text < stop) {
144 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
145 fx += SkFixedMul_portable(autokern.adjust(glyph), fixedSizeRatio);
146 if (glyph.fWidth) {
cdalton20b373c2014-12-01 08:38:55 -0800147 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)));
jvanverthaab626c2014-10-16 08:04:39 -0700148 }
149
150 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedSizeRatio);
151 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedSizeRatio);
152 }
153
154 this->finish();
155}
156
joshualitt25d9c152015-02-18 12:29:52 -0800157void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt,
158 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -0800159 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800160 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -0800161 const char text[],
162 size_t byteLength,
163 const SkScalar pos[],
164 int scalarsPerPosition,
165 const SkPoint& offset) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700166 SkASSERT(byteLength == 0 || text != NULL);
167 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
168
169 // nothing to draw
170 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
171 return;
172 }
173
174 // This is the fast path. Here we do not bake in the device-transform to
175 // the glyph outline or the advances. This is because we do not need to
176 // position the glyphs at all, since the caller has done the positioning.
177 // The positioning is based on SkPaint::measureText of individual
178 // glyphs. That already uses glyph cache without device transforms. Device
179 // transform is not part of SkPaint::measureText API, and thus we use the
180 // same glyphs as what were measured.
kkinnunenc6cb56f2014-06-24 00:12:27 -0700181
joshualitt25d9c152015-02-18 12:29:52 -0800182 this->init(rt, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700183
184 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
185
kkinnunenc6cb56f2014-06-24 00:12:27 -0700186 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700187
cdalton38e13ad2014-11-07 06:02:15 -0800188 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
189 SkTextAlignProcScalar alignProc(fSkPaint.getTextAlign());
190 while (text < stop) {
191 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
192 if (glyph.fWidth) {
193 SkPoint tmsLoc;
194 tmsProc(pos, &tmsLoc);
195 SkPoint loc;
196 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700197
cdalton20b373c2014-12-01 08:38:55 -0800198 this->appendGlyph(glyph, loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700199 }
cdalton38e13ad2014-11-07 06:02:15 -0800200 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700201 }
202
203 this->finish();
204}
205
cdalton855d83f2014-09-18 13:51:53 -0700206static GrPathRange* get_gr_glyphs(GrContext* ctx,
207 const SkTypeface* typeface,
208 const SkDescriptor* desc,
209 const SkStrokeRec& stroke) {
bsalomon8718aaf2015-02-19 07:24:21 -0800210 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
211 GrUniqueKey key;
212 GrUniqueKey::Builder builder(&key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800213 struct GlyphKey {
214 uint32_t fChecksum;
215 uint32_t fTypeface;
216 uint64_t fStroke;
217 };
218 GlyphKey* glyphKey = reinterpret_cast<GlyphKey*>(&builder[0]);
219 glyphKey->fChecksum = desc ? desc->getChecksum() : 0;
220 glyphKey->fTypeface = typeface ? typeface->uniqueID() : 0;
221 glyphKey->fStroke = GrPath::ComputeStrokeKey(stroke);
222 builder.finish();
cdalton855d83f2014-09-18 13:51:53 -0700223
224 SkAutoTUnref<GrPathRange> glyphs(
bsalomon24db3b12015-01-23 04:24:04 -0800225 static_cast<GrPathRange*>(ctx->findAndRefCachedResource(key)));
cdalton855d83f2014-09-18 13:51:53 -0700226 if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) {
227 glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke));
bsalomon24db3b12015-01-23 04:24:04 -0800228 ctx->addResourceToCache(key, glyphs);
cdalton855d83f2014-09-18 13:51:53 -0700229 }
230
231 return glyphs.detach();
232}
233
joshualitt25d9c152015-02-18 12:29:52 -0800234void GrStencilAndCoverTextContext::init(GrRenderTarget* rt,
235 const GrPaint& paint,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700236 const SkPaint& skPaint,
cdaltonb2808cd2014-07-25 14:13:57 -0700237 size_t textByteLength,
joshualitt5531d512014-12-17 15:50:11 -0800238 RenderMode renderMode,
239 const SkMatrix& viewMatrix) {
joshualitt25d9c152015-02-18 12:29:52 -0800240 GrTextContext::init(rt, paint, skPaint);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700241
joshualitt5531d512014-12-17 15:50:11 -0800242 fContextInitialMatrix = viewMatrix;
243 fViewMatrix = viewMatrix;
joshualitt290c09b2014-12-19 13:45:20 -0800244 fLocalMatrix = SkMatrix::I();
cdaltonb2808cd2014-07-25 14:13:57 -0700245
cdalton855d83f2014-09-18 13:51:53 -0700246 const bool otherBackendsWillDrawAsPaths =
cdaltonb2808cd2014-07-25 14:13:57 -0700247 SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700248
cdalton20b373c2014-12-01 08:38:55 -0800249 fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths &&
cdalton855d83f2014-09-18 13:51:53 -0700250 kMaxAccuracy_RenderMode == renderMode &&
251 SkToBool(fContextInitialMatrix.getType() &
252 (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700253
cdalton20b373c2014-12-01 08:38:55 -0800254 if (fUsingDeviceSpaceGlyphs) {
cdalton855d83f2014-09-18 13:51:53 -0700255 // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms.
256 SkASSERT(!fContextInitialMatrix.hasPerspective());
cdaltonb2808cd2014-07-25 14:13:57 -0700257
cdalton20b373c2014-12-01 08:38:55 -0800258 // The whole shape (including stroke) will be baked into the glyph outlines. Make
259 // NVPR just fill the baked shapes.
260 fStroke = SkStrokeRec(SkStrokeRec::kFill_InitStyle);
261
cdalton855d83f2014-09-18 13:51:53 -0700262 fTextRatio = fTextInverseRatio = 1.0f;
263
264 // Glyphs loaded by GPU path rendering have an inverted y-direction.
265 SkMatrix m;
266 m.setScale(1, -1);
joshualitt5531d512014-12-17 15:50:11 -0800267 fViewMatrix = m;
cdalton855d83f2014-09-18 13:51:53 -0700268
269 // Post-flip the initial matrix so we're left with just the flip after
270 // the paint preConcats the inverse.
271 m = fContextInitialMatrix;
272 m.postScale(1, -1);
joshualitt290c09b2014-12-19 13:45:20 -0800273 if (!m.invert(&fLocalMatrix)) {
274 SkDebugf("Not invertible!\n");
275 return;
276 }
cdalton855d83f2014-09-18 13:51:53 -0700277
cdaltone05162d2014-12-01 08:57:33 -0800278 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix,
279 true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700280 fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800281 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700282 } else {
cdalton855d83f2014-09-18 13:51:53 -0700283 // Don't bake strokes into the glyph outlines. We will stroke the glyphs
284 // using the GPU instead. This is the fast path.
cdalton20b373c2014-12-01 08:38:55 -0800285 fStroke = SkStrokeRec(fSkPaint);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700286 fSkPaint.setStyle(SkPaint::kFill_Style);
cdalton855d83f2014-09-18 13:51:53 -0700287
cdalton20b373c2014-12-01 08:38:55 -0800288 if (fStroke.isHairlineStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700289 // Approximate hairline stroke.
290 SkScalar strokeWidth = SK_Scalar1 /
291 (SkVector::Make(fContextInitialMatrix.getScaleX(),
292 fContextInitialMatrix.getSkewY()).length());
cdalton20b373c2014-12-01 08:38:55 -0800293 fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700294
295 } else if (fSkPaint.isFakeBoldText() &&
296#ifdef SK_USE_FREETYPE_EMBOLDEN
297 kMaxPerformance_RenderMode == renderMode &&
298#endif
cdalton20b373c2014-12-01 08:38:55 -0800299 SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700300
301 // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke.
302 SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(),
303 kStdFakeBoldInterpKeys,
304 kStdFakeBoldInterpValues,
305 kStdFakeBoldInterpLength);
306 SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale);
cdalton20b373c2014-12-01 08:38:55 -0800307 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
308 true /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700309
310 fSkPaint.setFakeBoldText(false);
311 }
312
313 bool canUseRawPaths;
314
315 if (otherBackendsWillDrawAsPaths || kMaxPerformance_RenderMode == renderMode) {
316 // We can draw the glyphs from canonically sized paths.
317 fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
318 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize();
319
320 // Compensate for the glyphs being scaled by fTextRatio.
cdalton20b373c2014-12-01 08:38:55 -0800321 if (!fStroke.isFillStyle()) {
322 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
323 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
cdalton855d83f2014-09-18 13:51:53 -0700324 }
325
326 fSkPaint.setLinearText(true);
327 fSkPaint.setLCDRenderText(false);
328 fSkPaint.setAutohinted(false);
329 fSkPaint.setHinting(SkPaint::kNo_Hinting);
330 fSkPaint.setSubpixelText(true);
331 fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
332
333 canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() &&
334 0 == fSkPaint.getTextSkewX() &&
335 !fSkPaint.isFakeBoldText() &&
336 !fSkPaint.isVerticalText();
337 } else {
338 fTextRatio = fTextInverseRatio = 1.0f;
339 canUseRawPaths = false;
340 }
341
342 SkMatrix textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700343 // Glyphs loaded by GPU path rendering have an inverted y-direction.
cdalton38e13ad2014-11-07 06:02:15 -0800344 textMatrix.setScale(fTextRatio, -fTextRatio);
joshualitt5531d512014-12-17 15:50:11 -0800345 fViewMatrix.preConcat(textMatrix);
joshualitt290c09b2014-12-19 13:45:20 -0800346 fLocalMatrix = textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700347
cdaltone05162d2014-12-01 08:57:33 -0800348 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700349 fGlyphs = canUseRawPaths ?
cdalton20b373c2014-12-01 08:38:55 -0800350 get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) :
cdalton855d83f2014-09-18 13:51:53 -0700351 get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800352 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700353 }
cdalton855d83f2014-09-18 13:51:53 -0700354
egdaniel8dd688b2015-01-22 10:16:09 -0800355 fStateRestore.set(&fPipelineBuilder);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700356
joshualitt25d9c152015-02-18 12:29:52 -0800357 fPipelineBuilder.setFromPaint(fPaint, fRenderTarget);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700358
359 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
360 kZero_StencilOp,
361 kZero_StencilOp,
362 kNotEqual_StencilFunc,
363 0xffff,
364 0x0000,
365 0xffff);
366
egdaniel8dd688b2015-01-22 10:16:09 -0800367 *fPipelineBuilder.stencil() = kStencilPass;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700368
cdalton20b373c2014-12-01 08:38:55 -0800369 SkASSERT(0 == fQueuedGlyphCount);
370 SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700371}
372
joshualitt5531d512014-12-17 15:50:11 -0800373bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) {
cdalton20b373c2014-12-01 08:38:55 -0800374 // The current view matrix is flipped because GPU path rendering glyphs have an
375 // inverted y-direction. Unflip the view matrix for the fallback context. If using
376 // device-space glyphs, we'll also need to restore the original view matrix since
377 // we moved that transfomation into our local glyph cache for this scenario. Also
378 // track the inverse operation so the caller can unmap the paint and glyph positions.
379 if (fUsingDeviceSpaceGlyphs) {
joshualitt5531d512014-12-17 15:50:11 -0800380 fViewMatrix = fContextInitialMatrix;
cdalton20b373c2014-12-01 08:38:55 -0800381 if (!fContextInitialMatrix.invert(inverse)) {
382 return false;
383 }
384 inverse->preScale(1, -1);
385 } else {
386 inverse->setScale(1, -1);
387 const SkMatrix& unflip = *inverse; // unflip is equal to its own inverse.
joshualitt5531d512014-12-17 15:50:11 -0800388 fViewMatrix.preConcat(unflip);
cdalton20b373c2014-12-01 08:38:55 -0800389 }
390 return true;
391}
392
393inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) {
394 if (fQueuedGlyphCount >= fFallbackGlyphsIdx) {
395 SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx);
cdaltonb2808cd2014-07-25 14:13:57 -0700396 this->flush();
397 }
398
cdalton20b373c2014-12-01 08:38:55 -0800399 // Stick the glyphs we can't draw at the end of the buffer, growing backwards.
400 int index = (SkMask::kARGB32_Format == glyph.fMaskFormat) ?
401 --fFallbackGlyphsIdx : fQueuedGlyphCount++;
cdaltonb85a0aa2014-07-21 15:32:44 -0700402
cdalton20b373c2014-12-01 08:38:55 -0800403 fGlyphIndices[index] = glyph.getGlyphID();
404 fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y());
405}
406
407static const SkScalar* get_xy_scalar_array(const SkPoint* pointArray) {
408 GR_STATIC_ASSERT(2 * sizeof(SkScalar) == sizeof(SkPoint));
409 GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
410
411 return &pointArray[0].fX;
cdaltonb85a0aa2014-07-21 15:32:44 -0700412}
413
414void GrStencilAndCoverTextContext::flush() {
cdalton20b373c2014-12-01 08:38:55 -0800415 if (fQueuedGlyphCount > 0) {
joshualitt8059eb92014-12-29 15:10:07 -0800416 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(),
417 fViewMatrix,
418 fLocalMatrix));
egdaniel8dd688b2015-01-22 10:16:09 -0800419 fDrawTarget->drawPaths(&fPipelineBuilder, pp, fGlyphs,
cdalton20b373c2014-12-01 08:38:55 -0800420 fGlyphIndices, GrPathRange::kU16_PathIndexType,
421 get_xy_scalar_array(fGlyphPositions),
422 GrPathRendering::kTranslate_PathTransformType,
423 fQueuedGlyphCount, GrPathRendering::kWinding_FillType);
424
425 fQueuedGlyphCount = 0;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700426 }
427
cdalton20b373c2014-12-01 08:38:55 -0800428 if (fFallbackGlyphsIdx < kGlyphBufferSize) {
429 int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx;
cdaltonb85a0aa2014-07-21 15:32:44 -0700430
cdalton20b373c2014-12-01 08:38:55 -0800431 GrPaint paintFallback(fPaint);
432
433 SkPaint skPaintFallback(fSkPaint);
434 if (!fUsingDeviceSpaceGlyphs) {
435 fStroke.applyToPaint(&skPaintFallback);
436 }
437 skPaintFallback.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for.
438 skPaintFallback.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
439
cdalton20b373c2014-12-01 08:38:55 -0800440 SkMatrix inverse;
joshualitt5531d512014-12-17 15:50:11 -0800441 if (this->mapToFallbackContext(&inverse)) {
cdalton20b373c2014-12-01 08:38:55 -0800442 inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount);
443 }
444
joshualitt25d9c152015-02-18 12:29:52 -0800445 fFallbackTextContext->drawPosText(fRenderTarget, paintFallback, skPaintFallback,
446 fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx],
cdalton20b373c2014-12-01 08:38:55 -0800447 2 * fallbackGlyphCount,
448 get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]),
449 2, SkPoint::Make(0, 0));
450
451 fFallbackGlyphsIdx = kGlyphBufferSize;
452 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700453}
454
455void GrStencilAndCoverTextContext::finish() {
cdaltonb85a0aa2014-07-21 15:32:44 -0700456 this->flush();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700457
cdalton855d83f2014-09-18 13:51:53 -0700458 fGlyphs->unref();
cdaltonb85a0aa2014-07-21 15:32:44 -0700459 fGlyphs = NULL;
cdalton855d83f2014-09-18 13:51:53 -0700460
461 SkGlyphCache::AttachCache(fGlyphCache);
cdaltonb85a0aa2014-07-21 15:32:44 -0700462 fGlyphCache = NULL;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700463
egdaniel8dd688b2015-01-22 10:16:09 -0800464 fPipelineBuilder.stencil()->setDisabled();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700465 fStateRestore.set(NULL);
joshualitt5531d512014-12-17 15:50:11 -0800466 fViewMatrix = fContextInitialMatrix;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700467 GrTextContext::finish();
468}
469