blob: b07a546b9a7c4f9dac9707e570b9373176c375b4 [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"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070011#include "GrDrawTarget.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070012#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,
robertphillips2334fb62015-06-17 05:43:33 -070025 GrDrawContext* drawContext,
robertphillipsfcf78292015-06-19 11:49:52 -070026 const SkSurfaceProps& surfaceProps)
27 : GrTextContext(context, drawContext, surfaceProps)
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*
robertphillips2334fb62015-06-17 05:43:33 -070034GrStencilAndCoverTextContext::Create(GrContext* context, GrDrawContext* drawContext,
robertphillipsfcf78292015-06-19 11:49:52 -070035 const SkSurfaceProps& surfaceProps) {
jvanverth8c27a182014-10-14 08:45:50 -070036 GrStencilAndCoverTextContext* textContext = SkNEW_ARGS(GrStencilAndCoverTextContext,
robertphillipsfcf78292015-06-19 11:49:52 -070037 (context, drawContext, surfaceProps));
38 textContext->fFallbackTextContext = GrAtlasTextContext::Create(context, drawContext, surfaceProps);
jvanverth8c27a182014-10-14 08:45:50 -070039
40 return textContext;
41}
42
kkinnunenc6cb56f2014-06-24 00:12:27 -070043GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
44}
45
cdaltone68f7362015-03-25 14:02:37 -070046bool GrStencilAndCoverTextContext::canDraw(const GrRenderTarget* rt,
47 const GrClip& clip,
48 const GrPaint& paint,
49 const SkPaint& skPaint,
50 const SkMatrix& viewMatrix) {
51 if (skPaint.getRasterizer()) {
jvanverth0fedb192014-10-08 09:07:27 -070052 return false;
53 }
cdaltone68f7362015-03-25 14:02:37 -070054 if (skPaint.getMaskFilter()) {
jvanverth0fedb192014-10-08 09:07:27 -070055 return false;
56 }
kkinnunen50b58e62015-05-18 23:02:07 -070057 if (SkPathEffect* pe = skPaint.getPathEffect()) {
58 if (pe->asADash(NULL) != SkPathEffect::kDash_DashType) {
59 return false;
60 }
jvanverth0fedb192014-10-08 09:07:27 -070061 }
62
63 // No hairlines unless we can map the 1 px width to the object space.
cdaltone68f7362015-03-25 14:02:37 -070064 if (skPaint.getStyle() == SkPaint::kStroke_Style
65 && skPaint.getStrokeWidth() == 0
joshualitt5531d512014-12-17 15:50:11 -080066 && viewMatrix.hasPerspective()) {
jvanverth0fedb192014-10-08 09:07:27 -070067 return false;
68 }
69
70 // No color bitmap fonts.
71 SkScalerContext::Rec rec;
robertphillipsfcf78292015-06-19 11:49:52 -070072 SkScalerContext::MakeRec(skPaint, &fSurfaceProps, NULL, &rec);
jvanverth0fedb192014-10-08 09:07:27 -070073 return rec.getFormat() != SkMask::kARGB32_Format;
74}
75
robertphillips2334fb62015-06-17 05:43:33 -070076void GrStencilAndCoverTextContext::onDrawText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -080077 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -080078 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -080079 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -080080 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -080081 const char text[],
82 size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070083 SkScalar x, SkScalar y,
84 const SkIRect& regionClipBounds) {
jvanverthaab626c2014-10-16 08:04:39 -070085 SkASSERT(byteLength == 0 || text != NULL);
86
87 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
88 return;
89 }
90
91 // This is the slow path, mainly used by Skia unit tests. The other
92 // backends (8888, gpu, ...) use device-space dependent glyph caches. In
93 // order to match the glyph positions that the other code paths produce, we
94 // must also use device-space dependent glyph cache. This has the
95 // side-effect that the glyph shape outline will be in device-space,
96 // too. This in turn has the side-effect that NVPR can not stroke the paths,
97 // as the stroke in NVPR is defined in object-space.
98 // NOTE: here we have following coincidence that works at the moment:
99 // - When using the device-space glyphs, the transforms we pass to NVPR
100 // instanced drawing are the global transforms, and the view transform is
101 // identity. NVPR can not use non-affine transforms in the instanced
102 // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it
103 // will turn off the use of device-space glyphs when perspective transforms
104 // are in use.
105
joshualitt6e8cd962015-03-20 10:30:14 -0700106 this->init(rt, clip, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix,
107 regionClipBounds);
jvanverthaab626c2014-10-16 08:04:39 -0700108
109 // Transform our starting point.
cdalton20b373c2014-12-01 08:38:55 -0800110 if (fUsingDeviceSpaceGlyphs) {
jvanverthaab626c2014-10-16 08:04:39 -0700111 SkPoint loc;
112 fContextInitialMatrix.mapXY(x, y, &loc);
113 x = loc.fX;
114 y = loc.fY;
115 }
116
117 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
118
jvanverthaab626c2014-10-16 08:04:39 -0700119 const char* stop = text + byteLength;
120
121 // Measure first if needed.
122 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
123 SkFixed stopX = 0;
124 SkFixed stopY = 0;
125
126 const char* textPtr = text;
127 while (textPtr < stop) {
128 // We don't need x, y here, since all subpixel variants will have the
129 // same advance.
130 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0);
131
132 stopX += glyph.fAdvanceX;
133 stopY += glyph.fAdvanceY;
134 }
135 SkASSERT(textPtr == stop);
136
137 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
138 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
139
140 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
141 alignX = SkScalarHalf(alignX);
142 alignY = SkScalarHalf(alignY);
143 }
144
145 x -= alignX;
146 y -= alignY;
147 }
148
149 SkAutoKern autokern;
150
151 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
152
153 SkFixed fx = SkScalarToFixed(x);
154 SkFixed fy = SkScalarToFixed(y);
155 while (text < stop) {
156 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
bungemand709ea82015-03-17 07:23:39 -0700157 fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700158 if (glyph.fWidth) {
robertphillips2334fb62015-06-17 05:43:33 -0700159 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)));
jvanverthaab626c2014-10-16 08:04:39 -0700160 }
161
bungemand709ea82015-03-17 07:23:39 -0700162 fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio);
163 fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700164 }
165
robertphillips2334fb62015-06-17 05:43:33 -0700166 this->finish();
jvanverthaab626c2014-10-16 08:04:39 -0700167}
168
robertphillips2334fb62015-06-17 05:43:33 -0700169void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800170 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800171 const GrPaint& paint,
cdalton20b373c2014-12-01 08:38:55 -0800172 const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800173 const SkMatrix& viewMatrix,
cdalton20b373c2014-12-01 08:38:55 -0800174 const char text[],
175 size_t byteLength,
176 const SkScalar pos[],
177 int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -0700178 const SkPoint& offset,
179 const SkIRect& regionClipBounds) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700180 SkASSERT(byteLength == 0 || text != NULL);
181 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
182
183 // nothing to draw
184 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
185 return;
186 }
187
188 // This is the fast path. Here we do not bake in the device-transform to
189 // the glyph outline or the advances. This is because we do not need to
190 // position the glyphs at all, since the caller has done the positioning.
191 // The positioning is based on SkPaint::measureText of individual
192 // glyphs. That already uses glyph cache without device transforms. Device
193 // transform is not part of SkPaint::measureText API, and thus we use the
194 // same glyphs as what were measured.
kkinnunenc6cb56f2014-06-24 00:12:27 -0700195
joshualitt6e8cd962015-03-20 10:30:14 -0700196 this->init(rt, clip, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix,
197 regionClipBounds);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700198
199 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
200
kkinnunenc6cb56f2014-06-24 00:12:27 -0700201 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700202
cdalton38e13ad2014-11-07 06:02:15 -0800203 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
bungeman79738cc2015-03-11 14:05:29 -0700204 SkTextAlignProc alignProc(fSkPaint.getTextAlign());
cdalton38e13ad2014-11-07 06:02:15 -0800205 while (text < stop) {
206 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
207 if (glyph.fWidth) {
208 SkPoint tmsLoc;
209 tmsProc(pos, &tmsLoc);
210 SkPoint loc;
211 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700212
robertphillips2334fb62015-06-17 05:43:33 -0700213 this->appendGlyph(glyph, loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700214 }
cdalton38e13ad2014-11-07 06:02:15 -0800215 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700216 }
217
robertphillips2334fb62015-06-17 05:43:33 -0700218 this->finish();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700219}
220
cdalton855d83f2014-09-18 13:51:53 -0700221static GrPathRange* get_gr_glyphs(GrContext* ctx,
222 const SkTypeface* typeface,
223 const SkDescriptor* desc,
kkinnunen50b58e62015-05-18 23:02:07 -0700224 const GrStrokeInfo& stroke) {
225
226 static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain();
227 int strokeDataCount = stroke.computeUniqueKeyFragmentData32Cnt();
228 GrUniqueKey glyphKey;
229 GrUniqueKey::Builder builder(&glyphKey, kPathGlyphDomain, 2 + strokeDataCount);
230 reinterpret_cast<uint32_t&>(builder[0]) = desc ? desc->getChecksum() : 0;
231 reinterpret_cast<uint32_t&>(builder[1]) = typeface ? typeface->uniqueID() : 0;
232 if (strokeDataCount > 0) {
233 stroke.asUniqueKeyFragment(&builder[2]);
234 }
bsalomon24db3b12015-01-23 04:24:04 -0800235 builder.finish();
cdalton855d83f2014-09-18 13:51:53 -0700236
237 SkAutoTUnref<GrPathRange> glyphs(
kkinnunen50b58e62015-05-18 23:02:07 -0700238 static_cast<GrPathRange*>(
239 ctx->resourceProvider()->findAndRefResourceByUniqueKey(glyphKey)));
240 if (NULL == glyphs) {
bsalomon706f08f2015-05-22 07:35:58 -0700241 glyphs.reset(ctx->resourceProvider()->createGlyphs(typeface, desc, stroke));
kkinnunen50b58e62015-05-18 23:02:07 -0700242 ctx->resourceProvider()->assignUniqueKeyToResource(glyphKey, glyphs);
243 } else {
244 SkASSERT(NULL == desc || glyphs->isEqualTo(*desc));
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) {
joshualitt9df46592015-07-09 10:55:28 -0700258 fClip = clip;
259
260 fRenderTarget.reset(SkRef(rt));
261
262 fRegionClipBounds = regionClipBounds;
263 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
264
265 fPaint = paint;
266 fSkPaint = skPaint;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700267
joshualitt5531d512014-12-17 15:50:11 -0800268 fContextInitialMatrix = viewMatrix;
269 fViewMatrix = viewMatrix;
joshualitt290c09b2014-12-19 13:45:20 -0800270 fLocalMatrix = SkMatrix::I();
cdaltonb2808cd2014-07-25 14:13:57 -0700271
cdalton855d83f2014-09-18 13:51:53 -0700272 const bool otherBackendsWillDrawAsPaths =
cdaltonb2808cd2014-07-25 14:13:57 -0700273 SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700274
cdalton20b373c2014-12-01 08:38:55 -0800275 fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths &&
cdalton855d83f2014-09-18 13:51:53 -0700276 kMaxAccuracy_RenderMode == renderMode &&
277 SkToBool(fContextInitialMatrix.getType() &
278 (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700279
cdalton20b373c2014-12-01 08:38:55 -0800280 if (fUsingDeviceSpaceGlyphs) {
cdalton855d83f2014-09-18 13:51:53 -0700281 // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms.
282 SkASSERT(!fContextInitialMatrix.hasPerspective());
cdaltonb2808cd2014-07-25 14:13:57 -0700283
cdalton20b373c2014-12-01 08:38:55 -0800284 // The whole shape (including stroke) will be baked into the glyph outlines. Make
285 // NVPR just fill the baked shapes.
kkinnunen50b58e62015-05-18 23:02:07 -0700286 fStroke = GrStrokeInfo(SkStrokeRec::kFill_InitStyle);
cdalton20b373c2014-12-01 08:38:55 -0800287
cdalton855d83f2014-09-18 13:51:53 -0700288 fTextRatio = fTextInverseRatio = 1.0f;
289
290 // Glyphs loaded by GPU path rendering have an inverted y-direction.
291 SkMatrix m;
292 m.setScale(1, -1);
joshualitt5531d512014-12-17 15:50:11 -0800293 fViewMatrix = m;
cdalton855d83f2014-09-18 13:51:53 -0700294
295 // Post-flip the initial matrix so we're left with just the flip after
296 // the paint preConcats the inverse.
297 m = fContextInitialMatrix;
298 m.postScale(1, -1);
joshualitt290c09b2014-12-19 13:45:20 -0800299 if (!m.invert(&fLocalMatrix)) {
300 SkDebugf("Not invertible!\n");
301 return;
302 }
cdalton855d83f2014-09-18 13:51:53 -0700303
robertphillipsfcf78292015-06-19 11:49:52 -0700304 fGlyphCache = fSkPaint.detachCache(&fSurfaceProps, &fContextInitialMatrix,
cdaltone05162d2014-12-01 08:57:33 -0800305 true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700306 fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800307 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700308 } else {
cdalton855d83f2014-09-18 13:51:53 -0700309 // Don't bake strokes into the glyph outlines. We will stroke the glyphs
310 // using the GPU instead. This is the fast path.
kkinnunen50b58e62015-05-18 23:02:07 -0700311 fStroke = GrStrokeInfo(fSkPaint);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700312 fSkPaint.setStyle(SkPaint::kFill_Style);
cdalton855d83f2014-09-18 13:51:53 -0700313
cdalton20b373c2014-12-01 08:38:55 -0800314 if (fStroke.isHairlineStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700315 // Approximate hairline stroke.
316 SkScalar strokeWidth = SK_Scalar1 /
317 (SkVector::Make(fContextInitialMatrix.getScaleX(),
318 fContextInitialMatrix.getSkewY()).length());
cdalton20b373c2014-12-01 08:38:55 -0800319 fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700320
321 } else if (fSkPaint.isFakeBoldText() &&
322#ifdef SK_USE_FREETYPE_EMBOLDEN
323 kMaxPerformance_RenderMode == renderMode &&
324#endif
cdalton20b373c2014-12-01 08:38:55 -0800325 SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
cdalton855d83f2014-09-18 13:51:53 -0700326
327 // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke.
328 SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(),
329 kStdFakeBoldInterpKeys,
330 kStdFakeBoldInterpValues,
331 kStdFakeBoldInterpLength);
332 SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale);
cdalton20b373c2014-12-01 08:38:55 -0800333 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
334 true /*strokeAndFill*/);
cdalton855d83f2014-09-18 13:51:53 -0700335
336 fSkPaint.setFakeBoldText(false);
337 }
338
339 bool canUseRawPaths;
kkinnunen50b58e62015-05-18 23:02:07 -0700340 if (!fStroke.isDashed() && (otherBackendsWillDrawAsPaths ||
341 kMaxPerformance_RenderMode == renderMode)) {
cdalton855d83f2014-09-18 13:51:53 -0700342 // We can draw the glyphs from canonically sized paths.
343 fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
344 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize();
345
346 // Compensate for the glyphs being scaled by fTextRatio.
cdalton20b373c2014-12-01 08:38:55 -0800347 if (!fStroke.isFillStyle()) {
348 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
349 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
cdalton855d83f2014-09-18 13:51:53 -0700350 }
351
352 fSkPaint.setLinearText(true);
353 fSkPaint.setLCDRenderText(false);
354 fSkPaint.setAutohinted(false);
355 fSkPaint.setHinting(SkPaint::kNo_Hinting);
356 fSkPaint.setSubpixelText(true);
357 fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
358
359 canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() &&
360 0 == fSkPaint.getTextSkewX() &&
361 !fSkPaint.isFakeBoldText() &&
362 !fSkPaint.isVerticalText();
363 } else {
364 fTextRatio = fTextInverseRatio = 1.0f;
365 canUseRawPaths = false;
366 }
367
368 SkMatrix textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700369 // Glyphs loaded by GPU path rendering have an inverted y-direction.
cdalton38e13ad2014-11-07 06:02:15 -0800370 textMatrix.setScale(fTextRatio, -fTextRatio);
joshualitt5531d512014-12-17 15:50:11 -0800371 fViewMatrix.preConcat(textMatrix);
joshualitt290c09b2014-12-19 13:45:20 -0800372 fLocalMatrix = textMatrix;
cdalton855d83f2014-09-18 13:51:53 -0700373
robertphillipsfcf78292015-06-19 11:49:52 -0700374 fGlyphCache = fSkPaint.detachCache(&fSurfaceProps, NULL, true /*ignoreGamma*/);
cdalton855d83f2014-09-18 13:51:53 -0700375 fGlyphs = canUseRawPaths ?
cdalton20b373c2014-12-01 08:38:55 -0800376 get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) :
cdalton855d83f2014-09-18 13:51:53 -0700377 get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(),
cdalton20b373c2014-12-01 08:38:55 -0800378 &fGlyphCache->getDescriptor(), fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700379 }
cdalton855d83f2014-09-18 13:51:53 -0700380
egdaniel8dd688b2015-01-22 10:16:09 -0800381 fStateRestore.set(&fPipelineBuilder);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700382
joshualitt44701df2015-02-23 14:44:57 -0800383 fPipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip);
vbuzinovdded6962015-06-12 08:59:45 -0700384 SkASSERT(fRenderTarget->isStencilBufferMultisampled() || !fPaint.isAntiAlias());
385 fPipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, fPaint.isAntiAlias());
kkinnunenc6cb56f2014-06-24 00:12:27 -0700386
387 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
388 kZero_StencilOp,
389 kZero_StencilOp,
390 kNotEqual_StencilFunc,
391 0xffff,
392 0x0000,
393 0xffff);
394
egdaniel8dd688b2015-01-22 10:16:09 -0800395 *fPipelineBuilder.stencil() = kStencilPass;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700396
cdalton20b373c2014-12-01 08:38:55 -0800397 SkASSERT(0 == fQueuedGlyphCount);
398 SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700399}
400
joshualitt5531d512014-12-17 15:50:11 -0800401bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) {
cdalton20b373c2014-12-01 08:38:55 -0800402 // The current view matrix is flipped because GPU path rendering glyphs have an
403 // inverted y-direction. Unflip the view matrix for the fallback context. If using
404 // device-space glyphs, we'll also need to restore the original view matrix since
405 // we moved that transfomation into our local glyph cache for this scenario. Also
406 // track the inverse operation so the caller can unmap the paint and glyph positions.
407 if (fUsingDeviceSpaceGlyphs) {
joshualitt5531d512014-12-17 15:50:11 -0800408 fViewMatrix = fContextInitialMatrix;
cdalton20b373c2014-12-01 08:38:55 -0800409 if (!fContextInitialMatrix.invert(inverse)) {
410 return false;
411 }
412 inverse->preScale(1, -1);
413 } else {
414 inverse->setScale(1, -1);
415 const SkMatrix& unflip = *inverse; // unflip is equal to its own inverse.
joshualitt5531d512014-12-17 15:50:11 -0800416 fViewMatrix.preConcat(unflip);
cdalton20b373c2014-12-01 08:38:55 -0800417 }
418 return true;
419}
420
robertphillips2334fb62015-06-17 05:43:33 -0700421inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) {
cdalton20b373c2014-12-01 08:38:55 -0800422 if (fQueuedGlyphCount >= fFallbackGlyphsIdx) {
423 SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx);
robertphillips2334fb62015-06-17 05:43:33 -0700424 this->flush();
cdaltonb2808cd2014-07-25 14:13:57 -0700425 }
426
cdalton20b373c2014-12-01 08:38:55 -0800427 // Stick the glyphs we can't draw at the end of the buffer, growing backwards.
428 int index = (SkMask::kARGB32_Format == glyph.fMaskFormat) ?
429 --fFallbackGlyphsIdx : fQueuedGlyphCount++;
cdaltonb85a0aa2014-07-21 15:32:44 -0700430
cdalton20b373c2014-12-01 08:38:55 -0800431 fGlyphIndices[index] = glyph.getGlyphID();
432 fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y());
433}
434
435static const SkScalar* get_xy_scalar_array(const SkPoint* pointArray) {
436 GR_STATIC_ASSERT(2 * sizeof(SkScalar) == sizeof(SkPoint));
437 GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
438
439 return &pointArray[0].fX;
cdaltonb85a0aa2014-07-21 15:32:44 -0700440}
441
robertphillips2334fb62015-06-17 05:43:33 -0700442void GrStencilAndCoverTextContext::flush() {
cdalton20b373c2014-12-01 08:38:55 -0800443 if (fQueuedGlyphCount > 0) {
joshualitt8059eb92014-12-29 15:10:07 -0800444 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(),
445 fViewMatrix,
446 fLocalMatrix));
robertphillipsea461502015-05-26 11:38:03 -0700447
robertphillips2334fb62015-06-17 05:43:33 -0700448 fDrawContext->drawPaths(&fPipelineBuilder, pp, fGlyphs,
449 fGlyphIndices, GrPathRange::kU16_PathIndexType,
450 get_xy_scalar_array(fGlyphPositions),
451 GrPathRendering::kTranslate_PathTransformType,
452 fQueuedGlyphCount, GrPathRendering::kWinding_FillType);
cdalton20b373c2014-12-01 08:38:55 -0800453
454 fQueuedGlyphCount = 0;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700455 }
456
cdalton20b373c2014-12-01 08:38:55 -0800457 if (fFallbackGlyphsIdx < kGlyphBufferSize) {
458 int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx;
cdaltonb85a0aa2014-07-21 15:32:44 -0700459
cdalton20b373c2014-12-01 08:38:55 -0800460 GrPaint paintFallback(fPaint);
461
462 SkPaint skPaintFallback(fSkPaint);
463 if (!fUsingDeviceSpaceGlyphs) {
464 fStroke.applyToPaint(&skPaintFallback);
465 }
466 skPaintFallback.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for.
467 skPaintFallback.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
468
cdalton20b373c2014-12-01 08:38:55 -0800469 SkMatrix inverse;
joshualitt5531d512014-12-17 15:50:11 -0800470 if (this->mapToFallbackContext(&inverse)) {
cdalton20b373c2014-12-01 08:38:55 -0800471 inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount);
472 }
473
joshualitt570d2f82015-02-25 13:19:48 -0800474 fFallbackTextContext->drawPosText(fRenderTarget, fClip, paintFallback, skPaintFallback,
joshualitt25d9c152015-02-18 12:29:52 -0800475 fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx],
cdalton20b373c2014-12-01 08:38:55 -0800476 2 * fallbackGlyphCount,
477 get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]),
joshualitt6e8cd962015-03-20 10:30:14 -0700478 2, SkPoint::Make(0, 0), fRegionClipBounds);
cdalton20b373c2014-12-01 08:38:55 -0800479
480 fFallbackGlyphsIdx = kGlyphBufferSize;
481 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700482}
483
robertphillips2334fb62015-06-17 05:43:33 -0700484void GrStencilAndCoverTextContext::finish() {
485 this->flush();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700486
cdalton855d83f2014-09-18 13:51:53 -0700487 fGlyphs->unref();
cdaltonb85a0aa2014-07-21 15:32:44 -0700488 fGlyphs = NULL;
cdalton855d83f2014-09-18 13:51:53 -0700489
490 SkGlyphCache::AttachCache(fGlyphCache);
cdaltonb85a0aa2014-07-21 15:32:44 -0700491 fGlyphCache = NULL;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700492
egdaniel8dd688b2015-01-22 10:16:09 -0800493 fPipelineBuilder.stencil()->setDisabled();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700494 fStateRestore.set(NULL);
joshualitt5531d512014-12-17 15:50:11 -0800495 fViewMatrix = fContextInitialMatrix;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700496}
497