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, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 68 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 69 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 70 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 71 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 72 | const char text[], |
| 73 | size_t byteLength, |
| 74 | SkScalar x, SkScalar y) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 75 | SkASSERT(byteLength == 0 || text != NULL); |
| 76 | |
| 77 | if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // This is the slow path, mainly used by Skia unit tests. The other |
| 82 | // backends (8888, gpu, ...) use device-space dependent glyph caches. In |
| 83 | // order to match the glyph positions that the other code paths produce, we |
| 84 | // must also use device-space dependent glyph cache. This has the |
| 85 | // side-effect that the glyph shape outline will be in device-space, |
| 86 | // too. This in turn has the side-effect that NVPR can not stroke the paths, |
| 87 | // as the stroke in NVPR is defined in object-space. |
| 88 | // NOTE: here we have following coincidence that works at the moment: |
| 89 | // - When using the device-space glyphs, the transforms we pass to NVPR |
| 90 | // instanced drawing are the global transforms, and the view transform is |
| 91 | // identity. NVPR can not use non-affine transforms in the instanced |
| 92 | // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it |
| 93 | // will turn off the use of device-space glyphs when perspective transforms |
| 94 | // are in use. |
| 95 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 96 | this->init(rt, clip, paint, skPaint, byteLength, kMaxAccuracy_RenderMode, viewMatrix); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 97 | |
| 98 | // Transform our starting point. |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 99 | if (fUsingDeviceSpaceGlyphs) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 100 | SkPoint loc; |
| 101 | fContextInitialMatrix.mapXY(x, y, &loc); |
| 102 | x = loc.fX; |
| 103 | y = loc.fY; |
| 104 | } |
| 105 | |
| 106 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 107 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 108 | const char* stop = text + byteLength; |
| 109 | |
| 110 | // Measure first if needed. |
| 111 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
| 112 | SkFixed stopX = 0; |
| 113 | SkFixed stopY = 0; |
| 114 | |
| 115 | const char* textPtr = text; |
| 116 | while (textPtr < stop) { |
| 117 | // We don't need x, y here, since all subpixel variants will have the |
| 118 | // same advance. |
| 119 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0); |
| 120 | |
| 121 | stopX += glyph.fAdvanceX; |
| 122 | stopY += glyph.fAdvanceY; |
| 123 | } |
| 124 | SkASSERT(textPtr == stop); |
| 125 | |
| 126 | SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio; |
| 127 | SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio; |
| 128 | |
| 129 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 130 | alignX = SkScalarHalf(alignX); |
| 131 | alignY = SkScalarHalf(alignY); |
| 132 | } |
| 133 | |
| 134 | x -= alignX; |
| 135 | y -= alignY; |
| 136 | } |
| 137 | |
| 138 | SkAutoKern autokern; |
| 139 | |
| 140 | SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio); |
| 141 | |
| 142 | SkFixed fx = SkScalarToFixed(x); |
| 143 | SkFixed fy = SkScalarToFixed(y); |
| 144 | while (text < stop) { |
| 145 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 146 | fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 147 | if (glyph.fWidth) { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 148 | this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy))); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 149 | } |
| 150 | |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 151 | fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio); |
| 152 | fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | this->finish(); |
| 156 | } |
| 157 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 158 | void GrStencilAndCoverTextContext::onDrawPosText(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 159 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 160 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 161 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 162 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 163 | const char text[], |
| 164 | size_t byteLength, |
| 165 | const SkScalar pos[], |
| 166 | int scalarsPerPosition, |
| 167 | const SkPoint& offset) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 168 | SkASSERT(byteLength == 0 || text != NULL); |
| 169 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 170 | |
| 171 | // nothing to draw |
| 172 | if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // This is the fast path. Here we do not bake in the device-transform to |
| 177 | // the glyph outline or the advances. This is because we do not need to |
| 178 | // position the glyphs at all, since the caller has done the positioning. |
| 179 | // The positioning is based on SkPaint::measureText of individual |
| 180 | // glyphs. That already uses glyph cache without device transforms. Device |
| 181 | // transform is not part of SkPaint::measureText API, and thus we use the |
| 182 | // same glyphs as what were measured. |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 183 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 184 | this->init(rt, clip, paint, skPaint, byteLength, kMaxPerformance_RenderMode, viewMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 185 | |
| 186 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 187 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 188 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 189 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 190 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 191 | SkTextAlignProc alignProc(fSkPaint.getTextAlign()); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 192 | while (text < stop) { |
| 193 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
| 194 | if (glyph.fWidth) { |
| 195 | SkPoint tmsLoc; |
| 196 | tmsProc(pos, &tmsLoc); |
| 197 | SkPoint loc; |
| 198 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 199 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 200 | this->appendGlyph(glyph, loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 201 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 202 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | this->finish(); |
| 206 | } |
| 207 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 208 | static GrPathRange* get_gr_glyphs(GrContext* ctx, |
| 209 | const SkTypeface* typeface, |
| 210 | const SkDescriptor* desc, |
| 211 | const SkStrokeRec& stroke) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 212 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 213 | GrUniqueKey key; |
| 214 | GrUniqueKey::Builder builder(&key, kDomain, 4); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 215 | struct GlyphKey { |
| 216 | uint32_t fChecksum; |
| 217 | uint32_t fTypeface; |
| 218 | uint64_t fStroke; |
| 219 | }; |
| 220 | GlyphKey* glyphKey = reinterpret_cast<GlyphKey*>(&builder[0]); |
| 221 | glyphKey->fChecksum = desc ? desc->getChecksum() : 0; |
| 222 | glyphKey->fTypeface = typeface ? typeface->uniqueID() : 0; |
| 223 | glyphKey->fStroke = GrPath::ComputeStrokeKey(stroke); |
| 224 | builder.finish(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 225 | |
| 226 | SkAutoTUnref<GrPathRange> glyphs( |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 227 | static_cast<GrPathRange*>(ctx->findAndRefCachedResource(key))); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 228 | if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) { |
| 229 | glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke)); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 230 | ctx->addResourceToCache(key, glyphs); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return glyphs.detach(); |
| 234 | } |
| 235 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 236 | void GrStencilAndCoverTextContext::init(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 237 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 238 | const GrPaint& paint, |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 239 | const SkPaint& skPaint, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 240 | size_t textByteLength, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 241 | RenderMode renderMode, |
| 242 | const SkMatrix& viewMatrix) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 243 | GrTextContext::init(rt, clip, paint, skPaint); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 244 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 245 | fContextInitialMatrix = viewMatrix; |
| 246 | fViewMatrix = viewMatrix; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 247 | fLocalMatrix = SkMatrix::I(); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 248 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 249 | const bool otherBackendsWillDrawAsPaths = |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 250 | SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 251 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 252 | fUsingDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths && |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 253 | kMaxAccuracy_RenderMode == renderMode && |
| 254 | SkToBool(fContextInitialMatrix.getType() & |
| 255 | (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 256 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 257 | if (fUsingDeviceSpaceGlyphs) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 258 | // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms. |
| 259 | SkASSERT(!fContextInitialMatrix.hasPerspective()); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 260 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 261 | // The whole shape (including stroke) will be baked into the glyph outlines. Make |
| 262 | // NVPR just fill the baked shapes. |
| 263 | fStroke = SkStrokeRec(SkStrokeRec::kFill_InitStyle); |
| 264 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 265 | fTextRatio = fTextInverseRatio = 1.0f; |
| 266 | |
| 267 | // Glyphs loaded by GPU path rendering have an inverted y-direction. |
| 268 | SkMatrix m; |
| 269 | m.setScale(1, -1); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 270 | fViewMatrix = m; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 271 | |
| 272 | // Post-flip the initial matrix so we're left with just the flip after |
| 273 | // the paint preConcats the inverse. |
| 274 | m = fContextInitialMatrix; |
| 275 | m.postScale(1, -1); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 276 | if (!m.invert(&fLocalMatrix)) { |
| 277 | SkDebugf("Not invertible!\n"); |
| 278 | return; |
| 279 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 280 | |
cdalton | e05162d | 2014-12-01 08:57:33 -0800 | [diff] [blame] | 281 | fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix, |
| 282 | true /*ignoreGamma*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 283 | fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 284 | &fGlyphCache->getDescriptor(), fStroke); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 285 | } else { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 286 | // Don't bake strokes into the glyph outlines. We will stroke the glyphs |
| 287 | // using the GPU instead. This is the fast path. |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 288 | fStroke = SkStrokeRec(fSkPaint); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 289 | fSkPaint.setStyle(SkPaint::kFill_Style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 290 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 291 | if (fStroke.isHairlineStyle()) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 292 | // Approximate hairline stroke. |
| 293 | SkScalar strokeWidth = SK_Scalar1 / |
| 294 | (SkVector::Make(fContextInitialMatrix.getScaleX(), |
| 295 | fContextInitialMatrix.getSkewY()).length()); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 296 | fStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 297 | |
| 298 | } else if (fSkPaint.isFakeBoldText() && |
| 299 | #ifdef SK_USE_FREETYPE_EMBOLDEN |
| 300 | kMaxPerformance_RenderMode == renderMode && |
| 301 | #endif |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 302 | SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 303 | |
| 304 | // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke. |
| 305 | SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(), |
| 306 | kStdFakeBoldInterpKeys, |
| 307 | kStdFakeBoldInterpValues, |
| 308 | kStdFakeBoldInterpLength); |
| 309 | SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 310 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 311 | true /*strokeAndFill*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 312 | |
| 313 | fSkPaint.setFakeBoldText(false); |
| 314 | } |
| 315 | |
| 316 | bool canUseRawPaths; |
| 317 | |
| 318 | if (otherBackendsWillDrawAsPaths || kMaxPerformance_RenderMode == renderMode) { |
| 319 | // We can draw the glyphs from canonically sized paths. |
| 320 | fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 321 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize(); |
| 322 | |
| 323 | // Compensate for the glyphs being scaled by fTextRatio. |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 324 | if (!fStroke.isFillStyle()) { |
| 325 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 326 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | fSkPaint.setLinearText(true); |
| 330 | fSkPaint.setLCDRenderText(false); |
| 331 | fSkPaint.setAutohinted(false); |
| 332 | fSkPaint.setHinting(SkPaint::kNo_Hinting); |
| 333 | fSkPaint.setSubpixelText(true); |
| 334 | fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 335 | |
| 336 | canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() && |
| 337 | 0 == fSkPaint.getTextSkewX() && |
| 338 | !fSkPaint.isFakeBoldText() && |
| 339 | !fSkPaint.isVerticalText(); |
| 340 | } else { |
| 341 | fTextRatio = fTextInverseRatio = 1.0f; |
| 342 | canUseRawPaths = false; |
| 343 | } |
| 344 | |
| 345 | SkMatrix textMatrix; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 346 | // Glyphs loaded by GPU path rendering have an inverted y-direction. |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 347 | textMatrix.setScale(fTextRatio, -fTextRatio); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 348 | fViewMatrix.preConcat(textMatrix); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 349 | fLocalMatrix = textMatrix; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 350 | |
cdalton | e05162d | 2014-12-01 08:57:33 -0800 | [diff] [blame] | 351 | fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, true /*ignoreGamma*/); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 352 | fGlyphs = canUseRawPaths ? |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 353 | get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, fStroke) : |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 354 | get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 355 | &fGlyphCache->getDescriptor(), fStroke); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 356 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 357 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 358 | fStateRestore.set(&fPipelineBuilder); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 359 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 360 | fPipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 361 | |
| 362 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 363 | kZero_StencilOp, |
| 364 | kZero_StencilOp, |
| 365 | kNotEqual_StencilFunc, |
| 366 | 0xffff, |
| 367 | 0x0000, |
| 368 | 0xffff); |
| 369 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 370 | *fPipelineBuilder.stencil() = kStencilPass; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 371 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 372 | SkASSERT(0 == fQueuedGlyphCount); |
| 373 | SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 374 | } |
| 375 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 376 | bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 377 | // The current view matrix is flipped because GPU path rendering glyphs have an |
| 378 | // inverted y-direction. Unflip the view matrix for the fallback context. If using |
| 379 | // device-space glyphs, we'll also need to restore the original view matrix since |
| 380 | // we moved that transfomation into our local glyph cache for this scenario. Also |
| 381 | // track the inverse operation so the caller can unmap the paint and glyph positions. |
| 382 | if (fUsingDeviceSpaceGlyphs) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 383 | fViewMatrix = fContextInitialMatrix; |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 384 | if (!fContextInitialMatrix.invert(inverse)) { |
| 385 | return false; |
| 386 | } |
| 387 | inverse->preScale(1, -1); |
| 388 | } else { |
| 389 | inverse->setScale(1, -1); |
| 390 | const SkMatrix& unflip = *inverse; // unflip is equal to its own inverse. |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 391 | fViewMatrix.preConcat(unflip); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) { |
| 397 | if (fQueuedGlyphCount >= fFallbackGlyphsIdx) { |
| 398 | SkASSERT(fQueuedGlyphCount == fFallbackGlyphsIdx); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 399 | this->flush(); |
| 400 | } |
| 401 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 402 | // Stick the glyphs we can't draw at the end of the buffer, growing backwards. |
| 403 | int index = (SkMask::kARGB32_Format == glyph.fMaskFormat) ? |
| 404 | --fFallbackGlyphsIdx : fQueuedGlyphCount++; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 405 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 406 | fGlyphIndices[index] = glyph.getGlyphID(); |
| 407 | fGlyphPositions[index].set(fTextInverseRatio * pos.x(), -fTextInverseRatio * pos.y()); |
| 408 | } |
| 409 | |
| 410 | static const SkScalar* get_xy_scalar_array(const SkPoint* pointArray) { |
| 411 | GR_STATIC_ASSERT(2 * sizeof(SkScalar) == sizeof(SkPoint)); |
| 412 | GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX)); |
| 413 | |
| 414 | return &pointArray[0].fX; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void GrStencilAndCoverTextContext::flush() { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 418 | if (fQueuedGlyphCount > 0) { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 419 | SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(fPaint.getColor(), |
| 420 | fViewMatrix, |
| 421 | fLocalMatrix)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 422 | fDrawTarget->drawPaths(&fPipelineBuilder, pp, fGlyphs, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 423 | fGlyphIndices, GrPathRange::kU16_PathIndexType, |
| 424 | get_xy_scalar_array(fGlyphPositions), |
| 425 | GrPathRendering::kTranslate_PathTransformType, |
| 426 | fQueuedGlyphCount, GrPathRendering::kWinding_FillType); |
| 427 | |
| 428 | fQueuedGlyphCount = 0; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 429 | } |
| 430 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 431 | if (fFallbackGlyphsIdx < kGlyphBufferSize) { |
| 432 | int fallbackGlyphCount = kGlyphBufferSize - fFallbackGlyphsIdx; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 433 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 434 | GrPaint paintFallback(fPaint); |
| 435 | |
| 436 | SkPaint skPaintFallback(fSkPaint); |
| 437 | if (!fUsingDeviceSpaceGlyphs) { |
| 438 | fStroke.applyToPaint(&skPaintFallback); |
| 439 | } |
| 440 | skPaintFallback.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for. |
| 441 | skPaintFallback.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 442 | |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 443 | SkMatrix inverse; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 444 | if (this->mapToFallbackContext(&inverse)) { |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 445 | inverse.mapPoints(&fGlyphPositions[fFallbackGlyphsIdx], fallbackGlyphCount); |
| 446 | } |
| 447 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 448 | fFallbackTextContext->drawPosText(fRenderTarget, fClip, paintFallback, skPaintFallback, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 449 | fViewMatrix, (char*)&fGlyphIndices[fFallbackGlyphsIdx], |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 450 | 2 * fallbackGlyphCount, |
| 451 | get_xy_scalar_array(&fGlyphPositions[fFallbackGlyphsIdx]), |
| 452 | 2, SkPoint::Make(0, 0)); |
| 453 | |
| 454 | fFallbackGlyphsIdx = kGlyphBufferSize; |
| 455 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void GrStencilAndCoverTextContext::finish() { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 459 | this->flush(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 460 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 461 | fGlyphs->unref(); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 462 | fGlyphs = NULL; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 463 | |
| 464 | SkGlyphCache::AttachCache(fGlyphCache); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 465 | fGlyphCache = NULL; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 466 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 467 | fPipelineBuilder.stencil()->setDisabled(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 468 | fStateRestore.set(NULL); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 469 | fViewMatrix = fContextInitialMatrix; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 470 | GrTextContext::finish(); |
| 471 | } |
| 472 | |