kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 1 | /* |
| 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" |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 9 | #include "GrBitmapTextContext.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 10 | #include "GrDrawTarget.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 11 | #include "GrGpu.h" |
| 12 | #include "GrPath.h" |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 13 | #include "GrPathRange.h" |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 14 | #include "SkAutoKern.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 15 | #include "SkDraw.h" |
| 16 | #include "SkDrawProcs.h" |
| 17 | #include "SkGlyphCache.h" |
| 18 | #include "SkGpuDevice.h" |
| 19 | #include "SkPath.h" |
| 20 | #include "SkTextMapStateProc.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 21 | #include "SkTextFormatParams.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 22 | |
| 23 | GrStencilAndCoverTextContext::GrStencilAndCoverTextContext( |
| 24 | GrContext* context, const SkDeviceProperties& properties) |
| 25 | : GrTextContext(context, properties) |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 26 | , fStroke(SkStrokeRec::kFill_InitStyle) |
| 27 | , fQueuedGlyphCount(0) |
| 28 | , fFallbackGlyphsIdx(kGlyphBufferSize) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 29 | } |
| 30 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 31 | GrStencilAndCoverTextContext* 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 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 40 | GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() { |
| 41 | } |
| 42 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 43 | bool GrStencilAndCoverTextContext::canDraw(const SkPaint& paint, const SkMatrix& viewMatrix) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 44 | 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 |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 57 | && viewMatrix.hasPerspective()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 58 | 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 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 67 | void GrStencilAndCoverTextContext::onDrawText(GrRenderTarget* rt, |
| 68 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 69 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 70 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 71 | const char text[], |
| 72 | size_t byteLength, |
| 73 | SkScalar x, SkScalar y) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 74 | 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 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 95 | this->init(rt, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 96 | |
| 97 | // Transform our starting point. |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 98 | if (fUsingDeviceSpaceGlyphs) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 99 | SkPoint loc; |
| 100 | fContextInitialMatrix.mapXY(x, y, &loc); |
| 101 | x = loc.fX; |
| 102 | y = loc.fY; |
| 103 | } |
| 104 | |
| 105 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 106 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 107 | 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) { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 147 | this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy))); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | fx += SkFixedMul_portable(glyph.fAdvanceX, fixedSizeRatio); |
| 151 | fy += SkFixedMul_portable(glyph.fAdvanceY, fixedSizeRatio); |
| 152 | } |
| 153 | |
| 154 | this->finish(); |
| 155 | } |
| 156 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 157 | void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt, |
| 158 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 159 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 160 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 161 | const char text[], |
| 162 | size_t byteLength, |
| 163 | const SkScalar pos[], |
| 164 | int scalarsPerPosition, |
| 165 | const SkPoint& offset) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 166 | 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. |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 181 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 182 | this->init(rt, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 183 | |
| 184 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 185 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 186 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 187 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 188 | 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); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 197 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 198 | this->appendGlyph(glyph, loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 199 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 200 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | this->finish(); |
| 204 | } |
| 205 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 206 | static GrPathRange* get_gr_glyphs(GrContext* ctx, |
| 207 | const SkTypeface* typeface, |
| 208 | const SkDescriptor* desc, |
| 209 | const SkStrokeRec& stroke) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame^] | 210 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 211 | GrUniqueKey key; |
| 212 | GrUniqueKey::Builder builder(&key, kDomain, 4); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 213 | 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(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 223 | |
| 224 | SkAutoTUnref<GrPathRange> glyphs( |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 225 | static_cast<GrPathRange*>(ctx->findAndRefCachedResource(key))); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 226 | if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) { |
| 227 | glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke)); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 228 | ctx->addResourceToCache(key, glyphs); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return glyphs.detach(); |
| 232 | } |
| 233 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 234 | void GrStencilAndCoverTextContext::init(GrRenderTarget* rt, |
| 235 | const GrPaint& paint, |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 236 | const SkPaint& skPaint, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 237 | size_t textByteLength, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 238 | RenderMode renderMode, |
| 239 | const SkMatrix& viewMatrix) { |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 240 | GrTextContext::init(rt, paint, skPaint); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 241 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 242 | fContextInitialMatrix = viewMatrix; |
| 243 | fViewMatrix = viewMatrix; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 244 | fLocalMatrix = SkMatrix::I(); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 245 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 246 | const bool otherBackendsWillDrawAsPaths = |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 247 | SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 248 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 249 | fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths && |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 250 | kMaxAccuracy_RenderMode == renderMode && |
| 251 | SkToBool(fContextInitialMatrix.getType() & |
| 252 | (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 253 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 254 | if (fUsingDeviceSpaceGlyphs) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 255 | // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms. |
| 256 | SkASSERT(!fContextInitialMatrix.hasPerspective()); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 257 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 258 | // 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 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 262 | 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); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 267 | fViewMatrix = m; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 268 | |
| 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); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 273 | if (!m.invert(&fLocalMatrix)) { |
| 274 | SkDebugf("Not invertible!\n"); |
| 275 | return; |
| 276 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 277 | |
cdalton | e05162d | 2014-12-01 08:57:33 -0800 | [diff] [blame] | 278 | fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix, |
| 279 | true /*ignoreGamma*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 280 | fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 281 | &fGlyphCache->getDescriptor(), fStroke); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 282 | } else { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 283 | // Don't bake strokes into the glyph outlines. We will stroke the glyphs |
| 284 | // using the GPU instead. This is the fast path. |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 285 | fStroke = SkStrokeRec(fSkPaint); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 286 | fSkPaint.setStyle(SkPaint::kFill_Style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 287 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 288 | if (fStroke.isHairlineStyle()) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 289 | // Approximate hairline stroke. |
| 290 | SkScalar strokeWidth = SK_Scalar1 / |
| 291 | (SkVector::Make(fContextInitialMatrix.getScaleX(), |
| 292 | fContextInitialMatrix.getSkewY()).length()); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 293 | fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 294 | |
| 295 | } else if (fSkPaint.isFakeBoldText() && |
| 296 | #ifdef SK_USE_FREETYPE_EMBOLDEN |
| 297 | kMaxPerformance_RenderMode == renderMode && |
| 298 | #endif |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 299 | SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 300 | |
| 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); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 307 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 308 | true /*strokeAndFill*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 309 | |
| 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. |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 321 | if (!fStroke.isFillStyle()) { |
| 322 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 323 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 324 | } |
| 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; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 343 | // Glyphs loaded by GPU path rendering have an inverted y-direction. |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 344 | textMatrix.setScale(fTextRatio, -fTextRatio); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 345 | fViewMatrix.preConcat(textMatrix); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 346 | fLocalMatrix = textMatrix; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 347 | |
cdalton | e05162d | 2014-12-01 08:57:33 -0800 | [diff] [blame] | 348 | fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, true /*ignoreGamma*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 349 | fGlyphs = canUseRawPaths ? |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 350 | get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) : |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 351 | get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 352 | &fGlyphCache->getDescriptor(), fStroke); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 353 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 354 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 355 | fStateRestore.set(&fPipelineBuilder); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 356 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 357 | fPipelineBuilder.setFromPaint(fPaint, fRenderTarget); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 358 | |
| 359 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 360 | kZero_StencilOp, |
| 361 | kZero_StencilOp, |
| 362 | kNotEqual_StencilFunc, |
| 363 | 0xffff, |
| 364 | 0x0000, |
| 365 | 0xffff); |
| 366 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 367 | *fPipelineBuilder.stencil() = kStencilPass; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 368 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 369 | SkASSERT(0 == fQueuedGlyphCount); |
| 370 | SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 371 | } |
| 372 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 373 | bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 374 | // 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) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 380 | fViewMatrix = fContextInitialMatrix; |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 381 | 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. |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 388 | fViewMatrix.preConcat(unflip); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 389 | } |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) { |
| 394 | if (fQueuedGlyphCount >= fFallbackGlyphsIdx) { |
| 395 | SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 396 | this->flush(); |
| 397 | } |
| 398 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 399 | // 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++; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 402 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 403 | fGlyphIndices[index] = glyph.getGlyphID(); |
| 404 | fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y()); |
| 405 | } |
| 406 | |
| 407 | static 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; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void GrStencilAndCoverTextContext::flush() { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 415 | if (fQueuedGlyphCount > 0) { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 416 | SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(), |
| 417 | fViewMatrix, |
| 418 | fLocalMatrix)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 419 | fDrawTarget->drawPaths(&fPipelineBuilder, pp, fGlyphs, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 420 | fGlyphIndices, GrPathRange::kU16_PathIndexType, |
| 421 | get_xy_scalar_array(fGlyphPositions), |
| 422 | GrPathRendering::kTranslate_PathTransformType, |
| 423 | fQueuedGlyphCount, GrPathRendering::kWinding_FillType); |
| 424 | |
| 425 | fQueuedGlyphCount = 0; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 426 | } |
| 427 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 428 | if (fFallbackGlyphsIdx < kGlyphBufferSize) { |
| 429 | int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 430 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 431 | 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 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 440 | SkMatrix inverse; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 441 | if (this->mapToFallbackContext(&inverse)) { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 442 | inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount); |
| 443 | } |
| 444 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 445 | fFallbackTextContext->drawPosText(fRenderTarget, paintFallback, skPaintFallback, |
| 446 | fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx], |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 447 | 2 * fallbackGlyphCount, |
| 448 | get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]), |
| 449 | 2, SkPoint::Make(0, 0)); |
| 450 | |
| 451 | fFallbackGlyphsIdx = kGlyphBufferSize; |
| 452 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void GrStencilAndCoverTextContext::finish() { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 456 | this->flush(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 457 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 458 | fGlyphs->unref(); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 459 | fGlyphs = NULL; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 460 | |
| 461 | SkGlyphCache::AttachCache(fGlyphCache); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 462 | fGlyphCache = NULL; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 463 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 464 | fPipelineBuilder.stencil()->setDisabled(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 465 | fStateRestore.set(NULL); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 466 | fViewMatrix = fContextInitialMatrix; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 467 | GrTextContext::finish(); |
| 468 | } |
| 469 | |