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