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