blob: bf47abd731bc37370d9517ec0d395e24a3e9b3b1 [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));
joshualitt7c3a2f82015-03-31 13:32:05 -070037#ifdef USE_BITMAP_TEXTBLOBS
38 textContext->fFallbackTextContext = GrBitmapTextContextB::Create(context, gpuDevice, props);
39#else
joshualitt6e8cd962015-03-20 10:30:14 -070040 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, gpuDevice, props);
joshualitt7c3a2f82015-03-31 13:32:05 -070041#endif
jvanverth8c27a182014-10-14 08:45:50 -070042
43 return textContext;
44}
45
kkinnunenc6cb56f2014-06-24 00:12:27 -070046GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
47}
48
cdaltone68f7362015-03-25 14:02:37 -070049bool GrStencilAndCoverTextContext::canDraw(const GrRenderTarget* rt,
50 const GrClip& clip,
51 const GrPaint& paint,
52 const SkPaint& skPaint,
53 const SkMatrix& viewMatrix) {
54 if (skPaint.getRasterizer()) {
jvanverth0fedb192014-10-08 09:07:27 -070055 return false;
56 }
cdaltone68f7362015-03-25 14:02:37 -070057 if (skPaint.getMaskFilter()) {
jvanverth0fedb192014-10-08 09:07:27 -070058 return false;
59 }
cdaltone68f7362015-03-25 14:02:37 -070060 if (skPaint.getPathEffect()) {
jvanverth0fedb192014-10-08 09:07:27 -070061 return false;
62 }
63
64 // No hairlines unless we can map the 1 px width to the object space.
cdaltone68f7362015-03-25 14:02:37 -070065 if (skPaint.getStyle() == SkPaint::kStroke_Style
66 && skPaint.getStrokeWidth() == 0
joshualitt5531d512014-12-17 15:50:11 -080067 && viewMatrix.hasPerspective()) {
jvanverth0fedb192014-10-08 09:07:27 -070068 return false;
69 }
70
71 // No color bitmap fonts.
72 SkScalerContext::Rec rec;
cdaltone68f7362015-03-25 14:02:37 -070073 SkScalerContext::MakeRec(skPaint, &fDeviceProperties, NULL, &rec);
jvanverth0fedb192014-10-08 09:07:27 -070074 return rec.getFormat() != SkMask::kARGB32_Format;
75}
76
joshualitt25d9c152015-02-18 12:29:52 -080077void GrStencilAndCoverTextContext::onDrawText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080078 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -080079 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -080080 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -080081 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -080082 const char text[],
83 size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070084 SkScalar x, SkScalar y,
85 const SkIRect& regionClipBounds) {
jvanverthaab626c2014-10-16 08:04:39 -070086 SkASSERT(byteLength == 0 || text != NULL);
87
88 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
89 return;
90 }
91
92 // This is the slow path, mainly used by Skia unit tests. The other
93 // backends (8888, gpu, ...) use device-space dependent glyph caches. In
94 // order to match the glyph positions that the other code paths produce, we
95 // must also use device-space dependent glyph cache. This has the
96 // side-effect that the glyph shape outline will be in device-space,
97 // too. This in turn has the side-effect that NVPR can not stroke the paths,
98 // as the stroke in NVPR is defined in object-space.
99 // NOTE: here we have following coincidence that works at the moment:
100 // - When using the device-space glyphs, the transforms we pass to NVPR
101 // instanced drawing are the global transforms, and the view transform is
102 // identity. NVPR can not use non-affine transforms in the instanced
103 // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it
104 // will turn off the use of device-space glyphs when perspective transforms
105 // are in use.
106
joshualitt6e8cd962015-03-20 10:30:14 -0700107 this->init(rt, clip, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix,
108 regionClipBounds);
jvanverthaab626c2014-10-16 08:04:39 -0700109
110 // Transform our starting point.
cdalton20b373c2014-12-01 08:38:55 -0800111 if (fUsingDeviceSpaceGlyphs) {
jvanverthaab626c2014-10-16 08:04:39 -0700112 SkPoint loc;
113 fContextInitialMatrix.mapXY(x, y, &loc);
114 x = loc.fX;
115 y = loc.fY;
116 }
117
118 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
119
jvanverthaab626c2014-10-16 08:04:39 -0700120 const char* stop = text + byteLength;
121
122 // Measure first if needed.
123 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
124 SkFixed stopX = 0;
125 SkFixed stopY = 0;
126
127 const char* textPtr = text;
128 while (textPtr < stop) {
129 // We don't need x, y here, since all subpixel variants will have the
130 // same advance.
131 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0);
132
133 stopX += glyph.fAdvanceX;
134 stopY += glyph.fAdvanceY;
135 }
136 SkASSERT(textPtr == stop);
137
138 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
139 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
140
141 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
142 alignX = SkScalarHalf(alignX);
143 alignY = SkScalarHalf(alignY);
144 }
145
146 x -= alignX;
147 y -= alignY;
148 }
149
150 SkAutoKern autokern;
151
152 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
153
154 SkFixed fx = SkScalarToFixed(x);
155 SkFixed fy = SkScalarToFixed(y);
156 while (text < stop) {
157 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
bungemand709ea82015-03-17 07:23:39 -0700158 fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700159 if (glyph.fWidth) {
cdalton20b373c2014-12-01 08:38:55 -0800160 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)));
jvanverthaab626c2014-10-16 08:04:39 -0700161 }
162
bungemand709ea82015-03-17 07:23:39 -0700163 fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio);
164 fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700165 }
166
167 this->finish();
168}
169
joshualitt25d9c152015-02-18 12:29:52 -0800170void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800171 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800172 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -0800173 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800174 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -0800175 const char text[],
176 size_t byteLength,
177 const SkScalar pos[],
178 int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -0700179 const SkPoint& offset,
180 const SkIRect& regionClipBounds) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700181 SkASSERT(byteLength == 0 || text != NULL);
182 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
183
184 // nothing to draw
185 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
186 return;
187 }
188
189 // This is the fast path. Here we do not bake in the device-transform to
190 // the glyph outline or the advances. This is because we do not need to
191 // position the glyphs at all, since the caller has done the positioning.
192 // The positioning is based on SkPaint::measureText of individual
193 // glyphs. That already uses glyph cache without device transforms. Device
194 // transform is not part of SkPaint::measureText API, and thus we use the
195 // same glyphs as what were measured.
kkinnunenc6cb56f2014-06-24 00:12:27 -0700196
joshualitt6e8cd962015-03-20 10:30:14 -0700197 this->init(rt, clip, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix,
198 regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700199
200 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
201
kkinnunenc6cb56f2014-06-24 00:12:27 -0700202 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700203
cdalton38e13ad2014-11-07 06:02:15 -0800204 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
bungeman79738cc2015-03-11 14:05:29 -0700205 SkTextAlignProc alignProc(fSkPaint.getTextAlign());
cdalton38e13ad2014-11-07 06:02:15 -0800206 while (text < stop) {
207 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
208 if (glyph.fWidth) {
209 SkPoint tmsLoc;
210 tmsProc(pos, &tmsLoc);
211 SkPoint loc;
212 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700213
cdalton20b373c2014-12-01 08:38:55 -0800214 this->appendGlyph(glyph, loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700215 }
cdalton38e13ad2014-11-07 06:02:15 -0800216 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700217 }
218
219 this->finish();
220}
221
cdalton855d83f2014-09-18 13:51:53 -0700222static GrPathRange* get_gr_glyphs(GrContext* ctx,
223 const SkTypeface* typeface,
224 const SkDescriptor* desc,
225 const SkStrokeRec& stroke) {
bsalomon8718aaf2015-02-19 07:24:21 -0800226 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
227 GrUniqueKey key;
228 GrUniqueKey::Builder builder(&key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800229 struct GlyphKey {
230 uint32_t fChecksum;
231 uint32_t fTypeface;
232 uint64_t fStroke;
233 };
234 GlyphKey* glyphKey = reinterpret_cast<GlyphKey*>(&builder[0]);
235 glyphKey->fChecksum = desc ? desc->getChecksum() : 0;
236 glyphKey->fTypeface = typeface ? typeface->uniqueID() : 0;
237 glyphKey->fStroke = GrPath::ComputeStrokeKey(stroke);
238 builder.finish();
cdalton855d83f2014-09-18 13:51:53 -0700239
240 SkAutoTUnref<GrPathRange> glyphs(
bsalomon24db3b12015-01-23 04:24:04 -0800241 static_cast<GrPathRange*>(ctx->findAndRefCachedResource(key)));
cdalton855d83f2014-09-18 13:51:53 -0700242 if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) {
243 glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke));
bsalomon24db3b12015-01-23 04:24:04 -0800244 ctx->addResourceToCache(key, glyphs);
cdalton855d83f2014-09-18 13:51:53 -0700245 }
246
247 return glyphs.detach();
248}
249
joshualitt25d9c152015-02-18 12:29:52 -0800250void GrStencilAndCoverTextContext::init(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800251 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800252 const GrPaint& paint,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700253 const SkPaint& skPaint,
cdaltonb2808cd2014-07-25 14:13:57 -0700254 size_t textByteLength,
joshualitt5531d512014-12-17 15:50:11 -0800255 RenderMode renderMode,
joshualitt6e8cd962015-03-20 10:30:14 -0700256 const SkMatrix& viewMatrix,
257 const SkIRect& regionClipBounds) {
258 GrTextContext::init(rt, clip, paint, skPaint, regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700259
joshualitt5531d512014-12-17 15:50:11 -0800260 fContextInitialMatrix = viewMatrix;
261 fViewMatrix = viewMatrix;
joshualitt290c09b2014-12-19 13:45:20 -0800262 fLocalMatrix = SkMatrix::I();
cdaltonb2808cd2014-07-25 14:13:57 -0700263
cdalton855d83f2014-09-18 13:51:53 -0700264 const bool otherBackendsWillDrawAsPaths =
cdaltonb2808cd2014-07-25 14:13:57 -0700265 SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700266
cdalton20b373c2014-12-01 08:38:55 -0800267 fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths &&
cdalton855d83f2014-09-18 13:51:53 -0700268 kMaxAccuracy_RenderMode == renderMode &&
269 SkToBool(fContextInitialMatrix.getType() &
270 (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700271
cdalton20b373c2014-12-01 08:38:55 -0800272 if (fUsingDeviceSpaceGlyphs) {
cdalton855d83f2014-09-18 13:51:53 -0700273 // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms.
274 SkASSERT(!fContextInitialMatrix.hasPerspective());
cdaltonb2808cd2014-07-25 14:13:57 -0700275
cdalton20b373c2014-12-01 08:38:55 -0800276 // The whole shape (including stroke) will be baked into the glyph outlines. Make
277 // NVPR just fill the baked shapes.
278 fStroke = SkStrokeRec(SkStrokeRec::kFill_InitStyle);
279
cdalton855d83f2014-09-18 13:51:53 -0700280 fTextRatio = fTextInverseRatio = 1.0f;
281
282 // Glyphs loaded by GPU path rendering have an inverted y-direction.
283 SkMatrix m;
284 m.setScale(1, -1);
joshualitt5531d512014-12-17 15:50:11 -0800285 fViewMatrix = m;
cdalton855d83f2014-09-18 13:51:53 -0700286
287 // Post-flip the initial matrix so we're left with just the flip after
288 // the paint preConcats the inverse.
289 m = fContextInitialMatrix;
290 m.postScale(1, -1);
joshualitt290c09b2014-12-19 13:45:20 -0800291 if (!m.invert(&fLocalMatrix)) {
292 SkDebugf("Not invertible!\n");
293 return;
294 }
cdalton855d83f2014-09-18 13:51:53 -0700295
cdaltone05162d2014-12-01 08:57:33 -0800296 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix,
297 true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700298 fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800299 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700300 } else {
cdalton855d83f2014-09-18 13:51:53 -0700301 // Don't bake strokes into the glyph outlines. We will stroke the glyphs
302 // using the GPU instead. This is the fast path.
cdalton20b373c2014-12-01 08:38:55 -0800303 fStroke = SkStrokeRec(fSkPaint);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700304 fSkPaint.setStyle(SkPaint::kFill_Style);
cdalton855d83f2014-09-18 13:51:53 -0700305
cdalton20b373c2014-12-01 08:38:55 -0800306 if (fStroke.isHairlineStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700307 // Approximate hairline stroke.
308 SkScalar strokeWidth = SK_Scalar1 /
309 (SkVector::Make(fContextInitialMatrix.getScaleX(),
310 fContextInitialMatrix.getSkewY()).length());
cdalton20b373c2014-12-01 08:38:55 -0800311 fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700312
313 } else if (fSkPaint.isFakeBoldText() &&
314#ifdef SK_USE_FREETYPE_EMBOLDEN
315 kMaxPerformance_RenderMode == renderMode &&
316#endif
cdalton20b373c2014-12-01 08:38:55 -0800317 SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700318
319 // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke.
320 SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(),
321 kStdFakeBoldInterpKeys,
322 kStdFakeBoldInterpValues,
323 kStdFakeBoldInterpLength);
324 SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale);
cdalton20b373c2014-12-01 08:38:55 -0800325 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
326 true /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700327
328 fSkPaint.setFakeBoldText(false);
329 }
330
331 bool canUseRawPaths;
332
333 if (otherBackendsWillDrawAsPaths || kMaxPerformance_RenderMode == renderMode) {
334 // We can draw the glyphs from canonically sized paths.
335 fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
336 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize();
337
338 // Compensate for the glyphs being scaled by fTextRatio.
cdalton20b373c2014-12-01 08:38:55 -0800339 if (!fStroke.isFillStyle()) {
340 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
341 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
cdalton855d83f2014-09-18 13:51:53 -0700342 }
343
344 fSkPaint.setLinearText(true);
345 fSkPaint.setLCDRenderText(false);
346 fSkPaint.setAutohinted(false);
347 fSkPaint.setHinting(SkPaint::kNo_Hinting);
348 fSkPaint.setSubpixelText(true);
349 fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
350
351 canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() &&
352 0 == fSkPaint.getTextSkewX() &&
353 !fSkPaint.isFakeBoldText() &&
354 !fSkPaint.isVerticalText();
355 } else {
356 fTextRatio = fTextInverseRatio = 1.0f;
357 canUseRawPaths = false;
358 }
359
360 SkMatrix textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700361 // Glyphs loaded by GPU path rendering have an inverted y-direction.
cdalton38e13ad2014-11-07 06:02:15 -0800362 textMatrix.setScale(fTextRatio, -fTextRatio);
joshualitt5531d512014-12-17 15:50:11 -0800363 fViewMatrix.preConcat(textMatrix);
joshualitt290c09b2014-12-19 13:45:20 -0800364 fLocalMatrix = textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700365
cdaltone05162d2014-12-01 08:57:33 -0800366 fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700367 fGlyphs = canUseRawPaths ?
cdalton20b373c2014-12-01 08:38:55 -0800368 get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) :
cdalton855d83f2014-09-18 13:51:53 -0700369 get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800370 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700371 }
cdalton855d83f2014-09-18 13:51:53 -0700372
egdaniel8dd688b2015-01-22 10:16:09 -0800373 fStateRestore.set(&fPipelineBuilder);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700374
joshualitt44701df2015-02-23 14:44:57 -0800375 fPipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700376
377 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
378 kZero_StencilOp,
379 kZero_StencilOp,
380 kNotEqual_StencilFunc,
381 0xffff,
382 0x0000,
383 0xffff);
384
egdaniel8dd688b2015-01-22 10:16:09 -0800385 *fPipelineBuilder.stencil() = kStencilPass;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700386
cdalton20b373c2014-12-01 08:38:55 -0800387 SkASSERT(0 == fQueuedGlyphCount);
388 SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700389}
390
joshualitt5531d512014-12-17 15:50:11 -0800391bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) {
cdalton20b373c2014-12-01 08:38:55 -0800392 // The current view matrix is flipped because GPU path rendering glyphs have an
393 // inverted y-direction. Unflip the view matrix for the fallback context. If using
394 // device-space glyphs, we'll also need to restore the original view matrix since
395 // we moved that transfomation into our local glyph cache for this scenario. Also
396 // track the inverse operation so the caller can unmap the paint and glyph positions.
397 if (fUsingDeviceSpaceGlyphs) {
joshualitt5531d512014-12-17 15:50:11 -0800398 fViewMatrix = fContextInitialMatrix;
cdalton20b373c2014-12-01 08:38:55 -0800399 if (!fContextInitialMatrix.invert(inverse)) {
400 return false;
401 }
402 inverse->preScale(1, -1);
403 } else {
404 inverse->setScale(1, -1);
405 const SkMatrix& unflip = *inverse; // unflip is equal to its own inverse.
joshualitt5531d512014-12-17 15:50:11 -0800406 fViewMatrix.preConcat(unflip);
cdalton20b373c2014-12-01 08:38:55 -0800407 }
408 return true;
409}
410
411inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) {
412 if (fQueuedGlyphCount >= fFallbackGlyphsIdx) {
413 SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx);
cdaltonb2808cd2014-07-25 14:13:57 -0700414 this->flush();
415 }
416
cdalton20b373c2014-12-01 08:38:55 -0800417 // Stick the glyphs we can't draw at the end of the buffer, growing backwards.
418 int index = (SkMask::kARGB32_Format == glyph.fMaskFormat) ?
419 --fFallbackGlyphsIdx : fQueuedGlyphCount++;
cdaltonb85a0aa2014-07-21 15:32:44 -0700420
cdalton20b373c2014-12-01 08:38:55 -0800421 fGlyphIndices[index] = glyph.getGlyphID();
422 fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y());
423}
424
425static const SkScalar* get_xy_scalar_array(const SkPoint* pointArray) {
426 GR_STATIC_ASSERT(2 * sizeof(SkScalar) == sizeof(SkPoint));
427 GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
428
429 return &pointArray[0].fX;
cdaltonb85a0aa2014-07-21 15:32:44 -0700430}
431
432void GrStencilAndCoverTextContext::flush() {
cdalton20b373c2014-12-01 08:38:55 -0800433 if (fQueuedGlyphCount > 0) {
joshualitt8059eb92014-12-29 15:10:07 -0800434 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(),
435 fViewMatrix,
436 fLocalMatrix));
egdaniel8dd688b2015-01-22 10:16:09 -0800437 fDrawTarget->drawPaths(&fPipelineBuilder, pp, fGlyphs,
cdalton20b373c2014-12-01 08:38:55 -0800438 fGlyphIndices, GrPathRange::kU16_PathIndexType,
439 get_xy_scalar_array(fGlyphPositions),
440 GrPathRendering::kTranslate_PathTransformType,
441 fQueuedGlyphCount, GrPathRendering::kWinding_FillType);
442
443 fQueuedGlyphCount = 0;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700444 }
445
cdalton20b373c2014-12-01 08:38:55 -0800446 if (fFallbackGlyphsIdx < kGlyphBufferSize) {
447 int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx;
cdaltonb85a0aa2014-07-21 15:32:44 -0700448
cdalton20b373c2014-12-01 08:38:55 -0800449 GrPaint paintFallback(fPaint);
450
451 SkPaint skPaintFallback(fSkPaint);
452 if (!fUsingDeviceSpaceGlyphs) {
453 fStroke.applyToPaint(&skPaintFallback);
454 }
455 skPaintFallback.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for.
456 skPaintFallback.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
457
cdalton20b373c2014-12-01 08:38:55 -0800458 SkMatrix inverse;
joshualitt5531d512014-12-17 15:50:11 -0800459 if (this->mapToFallbackContext(&inverse)) {
cdalton20b373c2014-12-01 08:38:55 -0800460 inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount);
461 }
462
joshualitt570d2f82015-02-25 13:19:48 -0800463 fFallbackTextContext->drawPosText(fRenderTarget, fClip, paintFallback, skPaintFallback,
joshualitt25d9c152015-02-18 12:29:52 -0800464 fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx],
cdalton20b373c2014-12-01 08:38:55 -0800465 2 * fallbackGlyphCount,
466 get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]),
joshualitt6e8cd962015-03-20 10:30:14 -0700467 2, SkPoint::Make(0, 0), fRegionClipBounds);
cdalton20b373c2014-12-01 08:38:55 -0800468
469 fFallbackGlyphsIdx = kGlyphBufferSize;
470 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700471}
472
473void GrStencilAndCoverTextContext::finish() {
cdaltonb85a0aa2014-07-21 15:32:44 -0700474 this->flush();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700475
cdalton855d83f2014-09-18 13:51:53 -0700476 fGlyphs->unref();
cdaltonb85a0aa2014-07-21 15:32:44 -0700477 fGlyphs = NULL;
cdalton855d83f2014-09-18 13:51:53 -0700478
479 SkGlyphCache::AttachCache(fGlyphCache);
cdaltonb85a0aa2014-07-21 15:32:44 -0700480 fGlyphCache = NULL;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700481
egdaniel8dd688b2015-01-22 10:16:09 -0800482 fPipelineBuilder.stencil()->setDisabled();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700483 fStateRestore.set(NULL);
joshualitt5531d512014-12-17 15:50:11 -0800484 fViewMatrix = fContextInitialMatrix;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700485 GrTextContext::finish();
486}
487