blob: db41c02239b0ad3245489c85d7270371070207d8 [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
joshualitt6e8cd962015-03-20 10:30:14 -070023GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(GrContext* context,
24 SkGpuDevice* gpuDevice,
25 const SkDeviceProperties& properties)
26 : GrTextContext(context, gpuDevice, properties)
cdalton20b373c2014-12-01 08:38:55 -080027 , fStroke(SkStrokeRec::kFill_InitStyle)
28 , fQueuedGlyphCount(0)
29 , fFallbackGlyphsIdx(kGlyphBufferSize) {
kkinnunenc6cb56f2014-06-24 00:12:27 -070030}
31
joshualitt6e8cd962015-03-20 10:30:14 -070032GrStencilAndCoverTextContext*
33GrStencilAndCoverTextContext::Create(GrContext* context, SkGpuDevice* gpuDevice,
34 const SkDeviceProperties& props) {
jvanverth8c27a182014-10-14 08:45:50 -070035 GrStencilAndCoverTextContext* textContext = SkNEW_ARGS(GrStencilAndCoverTextContext,
joshualitt6e8cd962015-03-20 10:30:14 -070036 (context, gpuDevice, props));
37 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, gpuDevice, props);
jvanverth8c27a182014-10-14 08:45:50 -070038
39 return textContext;
40}
41
kkinnunenc6cb56f2014-06-24 00:12:27 -070042GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
43}
44
joshualitt5531d512014-12-17 15:50:11 -080045bool GrStencilAndCoverTextContext::canDraw(const SkPaint& paint, const SkMatrix& viewMatrix) {
jvanverth0fedb192014-10-08 09:07:27 -070046 if (paint.getRasterizer()) {
47 return false;
48 }
49 if (paint.getMaskFilter()) {
50 return false;
51 }
52 if (paint.getPathEffect()) {
53 return false;
54 }
55
56 // No hairlines unless we can map the 1 px width to the object space.
57 if (paint.getStyle() == SkPaint::kStroke_Style
58 && paint.getStrokeWidth() == 0
joshualitt5531d512014-12-17 15:50:11 -080059 && viewMatrix.hasPerspective()) {
jvanverth0fedb192014-10-08 09:07:27 -070060 return false;
61 }
62
63 // No color bitmap fonts.
64 SkScalerContext::Rec rec;
65 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
66 return rec.getFormat() != SkMask::kARGB32_Format;
67}
68
joshualitt25d9c152015-02-18 12:29:52 -080069void GrStencilAndCoverTextContext::onDrawText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080070 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -080071 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -080072 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -080073 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -080074 const char text[],
75 size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070076 SkScalar x, SkScalar y,
77 const SkIRect& regionClipBounds) {
jvanverthaab626c2014-10-16 08:04:39 -070078 SkASSERT(byteLength == 0 || text != NULL);
79
80 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
81 return;
82 }
83
84 // This is the slow path, mainly used by Skia unit tests. The other
85 // backends (8888, gpu, ...) use device-space dependent glyph caches. In
86 // order to match the glyph positions that the other code paths produce, we
87 // must also use device-space dependent glyph cache. This has the
88 // side-effect that the glyph shape outline will be in device-space,
89 // too. This in turn has the side-effect that NVPR can not stroke the paths,
90 // as the stroke in NVPR is defined in object-space.
91 // NOTE: here we have following coincidence that works at the moment:
92 // - When using the device-space glyphs, the transforms we pass to NVPR
93 // instanced drawing are the global transforms, and the view transform is
94 // identity. NVPR can not use non-affine transforms in the instanced
95 // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it
96 // will turn off the use of device-space glyphs when perspective transforms
97 // are in use.
98
joshualitt6e8cd962015-03-20 10:30:14 -070099 this->init(rt, clip, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix,
100 regionClipBounds);
jvanverthaab626c2014-10-16 08:04:39 -0700101
102 // Transform our starting point.
cdalton20b373c2014-12-01 08:38:55 -0800103 if (fUsingDeviceSpaceGlyphs) {
jvanverthaab626c2014-10-16 08:04:39 -0700104 SkPoint loc;
105 fContextInitialMatrix.mapXY(x, y, &loc);
106 x = loc.fX;
107 y = loc.fY;
108 }
109
110 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
111
jvanverthaab626c2014-10-16 08:04:39 -0700112 const char* stop = text + byteLength;
113
114 // Measure first if needed.
115 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
116 SkFixed stopX = 0;
117 SkFixed stopY = 0;
118
119 const char* textPtr = text;
120 while (textPtr < stop) {
121 // We don't need x, y here, since all subpixel variants will have the
122 // same advance.
123 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0);
124
125 stopX += glyph.fAdvanceX;
126 stopY += glyph.fAdvanceY;
127 }
128 SkASSERT(textPtr == stop);
129
130 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
131 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
132
133 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
134 alignX = SkScalarHalf(alignX);
135 alignY = SkScalarHalf(alignY);
136 }
137
138 x -= alignX;
139 y -= alignY;
140 }
141
142 SkAutoKern autokern;
143
144 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
145
146 SkFixed fx = SkScalarToFixed(x);
147 SkFixed fy = SkScalarToFixed(y);
148 while (text < stop) {
149 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
bungemand709ea82015-03-17 07:23:39 -0700150 fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700151 if (glyph.fWidth) {
cdalton20b373c2014-12-01 08:38:55 -0800152 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)));
jvanverthaab626c2014-10-16 08:04:39 -0700153 }
154
bungemand709ea82015-03-17 07:23:39 -0700155 fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio);
156 fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700157 }
158
159 this->finish();
160}
161
joshualitt25d9c152015-02-18 12:29:52 -0800162void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800163 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800164 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -0800165 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800166 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -0800167 const char text[],
168 size_t byteLength,
169 const SkScalar pos[],
170 int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -0700171 const SkPoint& offset,
172 const SkIRect& regionClipBounds) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700173 SkASSERT(byteLength == 0 || text != NULL);
174 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
175
176 // nothing to draw
177 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
178 return;
179 }
180
181 // This is the fast path. Here we do not bake in the device-transform to
182 // the glyph outline or the advances. This is because we do not need to
183 // position the glyphs at all, since the caller has done the positioning.
184 // The positioning is based on SkPaint::measureText of individual
185 // glyphs. That already uses glyph cache without device transforms. Device
186 // transform is not part of SkPaint::measureText API, and thus we use the
187 // same glyphs as what were measured.
kkinnunenc6cb56f2014-06-24 00:12:27 -0700188
joshualitt6e8cd962015-03-20 10:30:14 -0700189 this->init(rt, clip, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix,
190 regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700191
192 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
193
kkinnunenc6cb56f2014-06-24 00:12:27 -0700194 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700195
cdalton38e13ad2014-11-07 06:02:15 -0800196 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
bungeman79738cc2015-03-11 14:05:29 -0700197 SkTextAlignProc alignProc(fSkPaint.getTextAlign());
cdalton38e13ad2014-11-07 06:02:15 -0800198 while (text < stop) {
199 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
200 if (glyph.fWidth) {
201 SkPoint tmsLoc;
202 tmsProc(pos, &tmsLoc);
203 SkPoint loc;
204 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700205
cdalton20b373c2014-12-01 08:38:55 -0800206 this->appendGlyph(glyph, loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700207 }
cdalton38e13ad2014-11-07 06:02:15 -0800208 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700209 }
210
211 this->finish();
212}
213
cdalton855d83f2014-09-18 13:51:53 -0700214static GrPathRange* get_gr_glyphs(GrContext* ctx,
215 const SkTypeface* typeface,
216 const SkDescriptor* desc,
217 const SkStrokeRec& stroke) {
bsalomon8718aaf2015-02-19 07:24:21 -0800218 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
219 GrUniqueKey key;
220 GrUniqueKey::Builder builder(&key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800221 struct GlyphKey {
222 uint32_t fChecksum;
223 uint32_t fTypeface;
224 uint64_t fStroke;
225 };
226 GlyphKey* glyphKey = reinterpret_cast<GlyphKey*>(&builder[0]);
227 glyphKey->fChecksum = desc ? desc->getChecksum() : 0;
228 glyphKey->fTypeface = typeface ? typeface->uniqueID() : 0;
229 glyphKey->fStroke = GrPath::ComputeStrokeKey(stroke);
230 builder.finish();
cdalton855d83f2014-09-18 13:51:53 -0700231
232 SkAutoTUnref<GrPathRange> glyphs(
bsalomon24db3b12015-01-23 04:24:04 -0800233 static_cast<GrPathRange*>(ctx->findAndRefCachedResource(key)));
cdalton855d83f2014-09-18 13:51:53 -0700234 if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) {
235 glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke));
bsalomon24db3b12015-01-23 04:24:04 -0800236 ctx->addResourceToCache(key, glyphs);
cdalton855d83f2014-09-18 13:51:53 -0700237 }
238
239 return glyphs.detach();
240}
241
joshualitt25d9c152015-02-18 12:29:52 -0800242void GrStencilAndCoverTextContext::init(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800243 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800244 const GrPaint& paint,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700245 const SkPaint& skPaint,
cdaltonb2808cd2014-07-25 14:13:57 -0700246 size_t textByteLength,
joshualitt5531d512014-12-17 15:50:11 -0800247 RenderMode renderMode,
joshualitt6e8cd962015-03-20 10:30:14 -0700248 const SkMatrix& viewMatrix,
249 const SkIRect& regionClipBounds) {
250 GrTextContext::init(rt, clip, paint, skPaint, regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700251
joshualitt5531d512014-12-17 15:50:11 -0800252 fContextInitialMatrix = viewMatrix;
253 fViewMatrix = viewMatrix;
joshualitt290c09b2014-12-19 13:45:20 -0800254 fLocalMatrix = SkMatrix::I();
cdaltonb2808cd2014-07-25 14:13:57 -0700255
cdalton855d83f2014-09-18 13:51:53 -0700256 const bool otherBackendsWillDrawAsPaths =
cdaltonb2808cd2014-07-25 14:13:57 -0700257 SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700258
cdalton20b373c2014-12-01 08:38:55 -0800259 fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths &&
cdalton855d83f2014-09-18 13:51:53 -0700260 kMaxAccuracy_RenderMode == renderMode &&
261 SkToBool(fContextInitialMatrix.getType() &
262 (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700263
cdalton20b373c2014-12-01 08:38:55 -0800264 if (fUsingDeviceSpaceGlyphs) {
cdalton855d83f2014-09-18 13:51:53 -0700265 // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms.
266 SkASSERT(!fContextInitialMatrix.hasPerspective());
cdaltonb2808cd2014-07-25 14:13:57 -0700267
cdalton20b373c2014-12-01 08:38:55 -0800268 // The whole shape (including stroke) will be baked into the glyph outlines. Make
269 // NVPR just fill the baked shapes.
270 fStroke = SkStrokeRec(SkStrokeRec::kFill_InitStyle);
271
cdalton855d83f2014-09-18 13:51:53 -0700272 fTextRatio = fTextInverseRatio = 1.0f;
273
274 // Glyphs loaded by GPU path rendering have an inverted y-direction.
275 SkMatrix m;
276 m.setScale(1, -1);
joshualitt5531d512014-12-17 15:50:11 -0800277 fViewMatrix = m;
cdalton855d83f2014-09-18 13:51:53 -0700278
279 // Post-flip the initial matrix so we're left with just the flip after
280 // the paint preConcats the inverse.
281 m = fContextInitialMatrix;
282 m.postScale(1, -1);
joshualitt290c09b2014-12-19 13:45:20 -0800283 if (!m.invert(&fLocalMatrix)) {
284 SkDebugf("Not invertible!\n");
285 return;
286 }
cdalton855d83f2014-09-18 13:51:53 -0700287
cdaltone05162d2014-12-01 08:57:33 -0800288 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix,
289 true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700290 fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800291 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700292 } else {
cdalton855d83f2014-09-18 13:51:53 -0700293 // Don't bake strokes into the glyph outlines. We will stroke the glyphs
294 // using the GPU instead. This is the fast path.
cdalton20b373c2014-12-01 08:38:55 -0800295 fStroke = SkStrokeRec(fSkPaint);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700296 fSkPaint.setStyle(SkPaint::kFill_Style);
cdalton855d83f2014-09-18 13:51:53 -0700297
cdalton20b373c2014-12-01 08:38:55 -0800298 if (fStroke.isHairlineStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700299 // Approximate hairline stroke.
300 SkScalar strokeWidth = SK_Scalar1 /
301 (SkVector::Make(fContextInitialMatrix.getScaleX(),
302 fContextInitialMatrix.getSkewY()).length());
cdalton20b373c2014-12-01 08:38:55 -0800303 fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700304
305 } else if (fSkPaint.isFakeBoldText() &&
306#ifdef SK_USE_FREETYPE_EMBOLDEN
307 kMaxPerformance_RenderMode == renderMode &&
308#endif
cdalton20b373c2014-12-01 08:38:55 -0800309 SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700310
311 // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke.
312 SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(),
313 kStdFakeBoldInterpKeys,
314 kStdFakeBoldInterpValues,
315 kStdFakeBoldInterpLength);
316 SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale);
cdalton20b373c2014-12-01 08:38:55 -0800317 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
318 true /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700319
320 fSkPaint.setFakeBoldText(false);
321 }
322
323 bool canUseRawPaths;
324
325 if (otherBackendsWillDrawAsPaths || kMaxPerformance_RenderMode == renderMode) {
326 // We can draw the glyphs from canonically sized paths.
327 fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
328 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize();
329
330 // Compensate for the glyphs being scaled by fTextRatio.
cdalton20b373c2014-12-01 08:38:55 -0800331 if (!fStroke.isFillStyle()) {
332 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
333 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
cdalton855d83f2014-09-18 13:51:53 -0700334 }
335
336 fSkPaint.setLinearText(true);
337 fSkPaint.setLCDRenderText(false);
338 fSkPaint.setAutohinted(false);
339 fSkPaint.setHinting(SkPaint::kNo_Hinting);
340 fSkPaint.setSubpixelText(true);
341 fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
342
343 canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() &&
344 0 == fSkPaint.getTextSkewX() &&
345 !fSkPaint.isFakeBoldText() &&
346 !fSkPaint.isVerticalText();
347 } else {
348 fTextRatio = fTextInverseRatio = 1.0f;
349 canUseRawPaths = false;
350 }
351
352 SkMatrix textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700353 // Glyphs loaded by GPU path rendering have an inverted y-direction.
cdalton38e13ad2014-11-07 06:02:15 -0800354 textMatrix.setScale(fTextRatio, -fTextRatio);
joshualitt5531d512014-12-17 15:50:11 -0800355 fViewMatrix.preConcat(textMatrix);
joshualitt290c09b2014-12-19 13:45:20 -0800356 fLocalMatrix = textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700357
cdaltone05162d2014-12-01 08:57:33 -0800358 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700359 fGlyphs = canUseRawPaths ?
cdalton20b373c2014-12-01 08:38:55 -0800360 get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) :
cdalton855d83f2014-09-18 13:51:53 -0700361 get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800362 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700363 }
cdalton855d83f2014-09-18 13:51:53 -0700364
egdaniel8dd688b2015-01-22 10:16:09 -0800365 fStateRestore.set(&fPipelineBuilder);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700366
joshualitt44701df2015-02-23 14:44:57 -0800367 fPipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700368
369 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
370 kZero_StencilOp,
371 kZero_StencilOp,
372 kNotEqual_StencilFunc,
373 0xffff,
374 0x0000,
375 0xffff);
376
egdaniel8dd688b2015-01-22 10:16:09 -0800377 *fPipelineBuilder.stencil() = kStencilPass;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700378
cdalton20b373c2014-12-01 08:38:55 -0800379 SkASSERT(0 == fQueuedGlyphCount);
380 SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700381}
382
joshualitt5531d512014-12-17 15:50:11 -0800383bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) {
cdalton20b373c2014-12-01 08:38:55 -0800384 // The current view matrix is flipped because GPU path rendering glyphs have an
385 // inverted y-direction. Unflip the view matrix for the fallback context. If using
386 // device-space glyphs, we'll also need to restore the original view matrix since
387 // we moved that transfomation into our local glyph cache for this scenario. Also
388 // track the inverse operation so the caller can unmap the paint and glyph positions.
389 if (fUsingDeviceSpaceGlyphs) {
joshualitt5531d512014-12-17 15:50:11 -0800390 fViewMatrix = fContextInitialMatrix;
cdalton20b373c2014-12-01 08:38:55 -0800391 if (!fContextInitialMatrix.invert(inverse)) {
392 return false;
393 }
394 inverse->preScale(1, -1);
395 } else {
396 inverse->setScale(1, -1);
397 const SkMatrix& unflip = *inverse; // unflip is equal to its own inverse.
joshualitt5531d512014-12-17 15:50:11 -0800398 fViewMatrix.preConcat(unflip);
cdalton20b373c2014-12-01 08:38:55 -0800399 }
400 return true;
401}
402
403inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) {
404 if (fQueuedGlyphCount >= fFallbackGlyphsIdx) {
405 SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx);
cdaltonb2808cd2014-07-25 14:13:57 -0700406 this->flush();
407 }
408
cdalton20b373c2014-12-01 08:38:55 -0800409 // Stick the glyphs we can't draw at the end of the buffer, growing backwards.
410 int index = (SkMask::kARGB32_Format == glyph.fMaskFormat) ?
411 --fFallbackGlyphsIdx : fQueuedGlyphCount++;
cdaltonb85a0aa2014-07-21 15:32:44 -0700412
cdalton20b373c2014-12-01 08:38:55 -0800413 fGlyphIndices[index] = glyph.getGlyphID();
414 fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y());
415}
416
417static const SkScalar* get_xy_scalar_array(const SkPoint* pointArray) {
418 GR_STATIC_ASSERT(2 * sizeof(SkScalar) == sizeof(SkPoint));
419 GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
420
421 return &pointArray[0].fX;
cdaltonb85a0aa2014-07-21 15:32:44 -0700422}
423
424void GrStencilAndCoverTextContext::flush() {
cdalton20b373c2014-12-01 08:38:55 -0800425 if (fQueuedGlyphCount > 0) {
joshualitt8059eb92014-12-29 15:10:07 -0800426 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(),
427 fViewMatrix,
428 fLocalMatrix));
egdaniel8dd688b2015-01-22 10:16:09 -0800429 fDrawTarget->drawPaths(&fPipelineBuilder, pp, fGlyphs,
cdalton20b373c2014-12-01 08:38:55 -0800430 fGlyphIndices, GrPathRange::kU16_PathIndexType,
431 get_xy_scalar_array(fGlyphPositions),
432 GrPathRendering::kTranslate_PathTransformType,
433 fQueuedGlyphCount, GrPathRendering::kWinding_FillType);
434
435 fQueuedGlyphCount = 0;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700436 }
437
cdalton20b373c2014-12-01 08:38:55 -0800438 if (fFallbackGlyphsIdx < kGlyphBufferSize) {
439 int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx;
cdaltonb85a0aa2014-07-21 15:32:44 -0700440
cdalton20b373c2014-12-01 08:38:55 -0800441 GrPaint paintFallback(fPaint);
442
443 SkPaint skPaintFallback(fSkPaint);
444 if (!fUsingDeviceSpaceGlyphs) {
445 fStroke.applyToPaint(&skPaintFallback);
446 }
447 skPaintFallback.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for.
448 skPaintFallback.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
449
cdalton20b373c2014-12-01 08:38:55 -0800450 SkMatrix inverse;
joshualitt5531d512014-12-17 15:50:11 -0800451 if (this->mapToFallbackContext(&inverse)) {
cdalton20b373c2014-12-01 08:38:55 -0800452 inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount);
453 }
454
joshualitt570d2f82015-02-25 13:19:48 -0800455 fFallbackTextContext->drawPosText(fRenderTarget, fClip, paintFallback, skPaintFallback,
joshualitt25d9c152015-02-18 12:29:52 -0800456 fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx],
cdalton20b373c2014-12-01 08:38:55 -0800457 2 * fallbackGlyphCount,
458 get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]),
joshualitt6e8cd962015-03-20 10:30:14 -0700459 2, SkPoint::Make(0, 0), fRegionClipBounds);
cdalton20b373c2014-12-01 08:38:55 -0800460
461 fFallbackGlyphsIdx = kGlyphBufferSize;
462 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700463}
464
465void GrStencilAndCoverTextContext::finish() {
cdaltonb85a0aa2014-07-21 15:32:44 -0700466 this->flush();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700467
cdalton855d83f2014-09-18 13:51:53 -0700468 fGlyphs->unref();
cdaltonb85a0aa2014-07-21 15:32:44 -0700469 fGlyphs = NULL;
cdalton855d83f2014-09-18 13:51:53 -0700470
471 SkGlyphCache::AttachCache(fGlyphCache);
cdaltonb85a0aa2014-07-21 15:32:44 -0700472 fGlyphCache = NULL;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700473
egdaniel8dd688b2015-01-22 10:16:09 -0800474 fPipelineBuilder.stencil()->setDisabled();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700475 fStateRestore.set(NULL);
joshualitt5531d512014-12-17 15:50:11 -0800476 fViewMatrix = fContextInitialMatrix;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700477 GrTextContext::finish();
478}
479