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