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" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 14 | #include "SkAutoKern.h" |
| 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 | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame^] | 65 | void GrStencilAndCoverTextContext::onDrawText(const GrPaint& paint, |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 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 | |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 91 | this->init(paint, skPaint, byteLength, kMaxAccuracy_RenderMode, SkPoint::Make(0, 0)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 92 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 93 | // Transform our starting point. |
| 94 | if (fNeedsDeviceSpaceGlyphs) { |
| 95 | SkPoint loc; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 96 | fContextInitialMatrix.mapXY(x, y, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 97 | x = loc.fX; |
| 98 | y = loc.fY; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 102 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 103 | fTransformType = GrPathRendering::kTranslate_PathTransformType; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 104 | |
| 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. |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 116 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 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) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 142 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 143 | fx += SkFixedMul_portable(autokern.adjust(glyph), fixedSizeRatio); |
| 144 | if (glyph.fWidth) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 145 | this->appendGlyph(glyph.getGlyphID(), SkFixedToScalar(fx), SkFixedToScalar(fy)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -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, |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 156 | const SkPaint& skPaint, |
| 157 | const char text[], |
| 158 | size_t byteLength, |
| 159 | const SkScalar pos[], |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 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 | |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 178 | this->init(paint, skPaint, byteLength, kMaxPerformance_RenderMode, offset); |
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 | |
| 184 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 185 | if (1 == scalarsPerPosition) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 186 | fTransformType = GrPathRendering::kTranslateX_PathTransformType; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 187 | while (text < stop) { |
| 188 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
| 189 | if (glyph.fWidth) { |
| 190 | this->appendGlyph(glyph.getGlyphID(), *pos); |
| 191 | } |
| 192 | pos++; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 193 | } |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 194 | } else { |
| 195 | SkASSERT(2 == scalarsPerPosition); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 196 | fTransformType = GrPathRendering::kTranslate_PathTransformType; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 197 | while (text < stop) { |
| 198 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
| 199 | if (glyph.fWidth) { |
| 200 | this->appendGlyph(glyph.getGlyphID(), pos[0], pos[1]); |
| 201 | } |
| 202 | pos += 2; |
| 203 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 204 | } |
| 205 | } else { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 206 | fTransformType = GrPathRendering::kTranslate_PathTransformType; |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 207 | SkTextMapStateProc tmsProc(SkMatrix::I(), SkPoint::Make(0, 0), scalarsPerPosition); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 208 | SkTextAlignProcScalar alignProc(fSkPaint.getTextAlign()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 209 | while (text < stop) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 210 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 211 | if (glyph.fWidth) { |
| 212 | SkPoint tmsLoc; |
| 213 | tmsProc(pos, &tmsLoc); |
| 214 | SkPoint loc; |
| 215 | alignProc(tmsLoc, glyph, &loc); |
| 216 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 217 | this->appendGlyph(glyph.getGlyphID(), loc.x(), loc.y()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 218 | } |
| 219 | pos += scalarsPerPosition; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | this->finish(); |
| 224 | } |
| 225 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 226 | static GrPathRange* get_gr_glyphs(GrContext* ctx, |
| 227 | const SkTypeface* typeface, |
| 228 | const SkDescriptor* desc, |
| 229 | const SkStrokeRec& stroke) { |
| 230 | static const GrCacheID::Domain gGlyphsDomain = GrCacheID::GenerateDomain(); |
| 231 | |
| 232 | GrCacheID::Key key; |
| 233 | uint64_t* keyData = key.fData64; |
| 234 | keyData[0] = desc ? desc->getChecksum() : 0; |
| 235 | keyData[0] = (keyData[0] << 32) | (typeface ? typeface->uniqueID() : 0); |
| 236 | keyData[1] = GrPath::ComputeStrokeKey(stroke); |
| 237 | GrResourceKey resourceKey = GrResourceKey(GrCacheID(gGlyphsDomain, key), |
| 238 | GrPathRange::resourceType(), 0); |
| 239 | |
| 240 | SkAutoTUnref<GrPathRange> glyphs( |
| 241 | static_cast<GrPathRange*>(ctx->findAndRefCachedResource(resourceKey))); |
| 242 | if (NULL == glyphs || (NULL != desc && !glyphs->isEqualTo(*desc))) { |
| 243 | glyphs.reset(ctx->getGpu()->pathRendering()->createGlyphs(typeface, desc, stroke)); |
| 244 | ctx->addResourceToCache(resourceKey, glyphs); |
| 245 | } |
| 246 | |
| 247 | return glyphs.detach(); |
| 248 | } |
| 249 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 250 | void GrStencilAndCoverTextContext::init(const GrPaint& paint, |
| 251 | const SkPaint& skPaint, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 252 | size_t textByteLength, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 253 | RenderMode renderMode, |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 254 | const SkPoint& textTranslate) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 255 | GrTextContext::init(paint, skPaint); |
| 256 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 257 | fContextInitialMatrix = fContext->getMatrix(); |
| 258 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 259 | const bool otherBackendsWillDrawAsPaths = |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 260 | SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 261 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 262 | fNeedsDeviceSpaceGlyphs = !otherBackendsWillDrawAsPaths && |
| 263 | kMaxAccuracy_RenderMode == renderMode && |
| 264 | SkToBool(fContextInitialMatrix.getType() & |
| 265 | (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 266 | |
| 267 | if (fNeedsDeviceSpaceGlyphs) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 268 | // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms. |
| 269 | SkASSERT(!fContextInitialMatrix.hasPerspective()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 270 | SkASSERT(textTranslate.isZero()); // TODO: Handle textTranslate in device-space usecase. |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 271 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 272 | fTextRatio = fTextInverseRatio = 1.0f; |
| 273 | |
| 274 | // Glyphs loaded by GPU path rendering have an inverted y-direction. |
| 275 | SkMatrix m; |
| 276 | m.setScale(1, -1); |
| 277 | fContext->setMatrix(m); |
| 278 | |
| 279 | // Post-flip the initial matrix so we're left with just the flip after |
| 280 | // the paint preConcats the inverse. |
| 281 | m = fContextInitialMatrix; |
| 282 | m.postScale(1, -1); |
| 283 | fPaint.localCoordChangeInverse(m); |
| 284 | |
| 285 | // The whole shape (including stroke) will be baked into the glyph outlines. Make |
| 286 | // NVPR just fill the baked shapes. |
| 287 | fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, &fContextInitialMatrix, false); |
| 288 | fGlyphs = get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
| 289 | &fGlyphCache->getDescriptor(), |
| 290 | SkStrokeRec(SkStrokeRec::kFill_InitStyle)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 291 | } else { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 292 | // Don't bake strokes into the glyph outlines. We will stroke the glyphs |
| 293 | // using the GPU instead. This is the fast path. |
| 294 | SkStrokeRec gpuStroke = SkStrokeRec(fSkPaint); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 295 | fSkPaint.setStyle(SkPaint::kFill_Style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 296 | |
| 297 | if (gpuStroke.isHairlineStyle()) { |
| 298 | // Approximate hairline stroke. |
| 299 | SkScalar strokeWidth = SK_Scalar1 / |
| 300 | (SkVector::Make(fContextInitialMatrix.getScaleX(), |
| 301 | fContextInitialMatrix.getSkewY()).length()); |
| 302 | gpuStroke.setStrokeStyle(strokeWidth, false /*strokeAndFill*/); |
| 303 | |
| 304 | } else if (fSkPaint.isFakeBoldText() && |
| 305 | #ifdef SK_USE_FREETYPE_EMBOLDEN |
| 306 | kMaxPerformance_RenderMode == renderMode && |
| 307 | #endif |
| 308 | SkStrokeRec::kStroke_Style != gpuStroke.getStyle()) { |
| 309 | |
| 310 | // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke. |
| 311 | SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(), |
| 312 | kStdFakeBoldInterpKeys, |
| 313 | kStdFakeBoldInterpValues, |
| 314 | kStdFakeBoldInterpLength); |
| 315 | SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale); |
| 316 | gpuStroke.setStrokeStyle(gpuStroke.needToApply() ? gpuStroke.getWidth() + extra : extra, |
| 317 | true /*strokeAndFill*/); |
| 318 | |
| 319 | fSkPaint.setFakeBoldText(false); |
| 320 | } |
| 321 | |
| 322 | bool canUseRawPaths; |
| 323 | |
| 324 | if (otherBackendsWillDrawAsPaths || kMaxPerformance_RenderMode == renderMode) { |
| 325 | // We can draw the glyphs from canonically sized paths. |
| 326 | fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 327 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize(); |
| 328 | |
| 329 | // Compensate for the glyphs being scaled by fTextRatio. |
| 330 | if (!gpuStroke.isFillStyle()) { |
| 331 | gpuStroke.setStrokeStyle(gpuStroke.getWidth() / fTextRatio, |
| 332 | SkStrokeRec::kStrokeAndFill_Style == gpuStroke.getStyle()); |
| 333 | } |
| 334 | |
| 335 | fSkPaint.setLinearText(true); |
| 336 | fSkPaint.setLCDRenderText(false); |
| 337 | fSkPaint.setAutohinted(false); |
| 338 | fSkPaint.setHinting(SkPaint::kNo_Hinting); |
| 339 | fSkPaint.setSubpixelText(true); |
| 340 | fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 341 | |
| 342 | canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() && |
| 343 | 0 == fSkPaint.getTextSkewX() && |
| 344 | !fSkPaint.isFakeBoldText() && |
| 345 | !fSkPaint.isVerticalText(); |
| 346 | } else { |
| 347 | fTextRatio = fTextInverseRatio = 1.0f; |
| 348 | canUseRawPaths = false; |
| 349 | } |
| 350 | |
| 351 | SkMatrix textMatrix; |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 352 | textMatrix.setTranslate(textTranslate.x(), textTranslate.y()); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 353 | // Glyphs loaded by GPU path rendering have an inverted y-direction. |
| 354 | textMatrix.preScale(fTextRatio, -fTextRatio); |
| 355 | fPaint.localCoordChange(textMatrix); |
| 356 | fContext->concatMatrix(textMatrix); |
| 357 | |
| 358 | fGlyphCache = fSkPaint.detachCache(&fDeviceProperties, NULL, false); |
| 359 | fGlyphs = canUseRawPaths ? |
| 360 | get_gr_glyphs(fContext, fSkPaint.getTypeface(), NULL, gpuStroke) : |
| 361 | get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
| 362 | &fGlyphCache->getDescriptor(), gpuStroke); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 363 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 364 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 365 | fStateRestore.set(fDrawTarget->drawState()); |
| 366 | |
| 367 | fDrawTarget->drawState()->setFromPaint(fPaint, fContext->getMatrix(), |
| 368 | fContext->getRenderTarget()); |
| 369 | |
| 370 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 371 | kZero_StencilOp, |
| 372 | kZero_StencilOp, |
| 373 | kNotEqual_StencilFunc, |
| 374 | 0xffff, |
| 375 | 0x0000, |
| 376 | 0xffff); |
| 377 | |
| 378 | *fDrawTarget->drawState()->stencil() = kStencilPass; |
| 379 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 380 | SkASSERT(0 == fPendingGlyphCount); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 381 | } |
| 382 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 383 | inline void GrStencilAndCoverTextContext::appendGlyph(uint16_t glyphID, float x) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 384 | SkASSERT(GrPathRendering::kTranslateX_PathTransformType == fTransformType); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 385 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 386 | if (fPendingGlyphCount >= kGlyphBufferSize) { |
| 387 | this->flush(); |
| 388 | } |
| 389 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 390 | fIndexBuffer[fPendingGlyphCount] = glyphID; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 391 | fTransformBuffer[fPendingGlyphCount] = fTextInverseRatio * x; |
| 392 | |
| 393 | ++fPendingGlyphCount; |
| 394 | } |
| 395 | |
| 396 | inline void GrStencilAndCoverTextContext::appendGlyph(uint16_t glyphID, float x, float y) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 397 | SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 398 | |
| 399 | if (fPendingGlyphCount >= kGlyphBufferSize) { |
| 400 | this->flush(); |
| 401 | } |
| 402 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 403 | fIndexBuffer[fPendingGlyphCount] = glyphID; |
| 404 | fTransformBuffer[2 * fPendingGlyphCount] = fTextInverseRatio * x; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 405 | fTransformBuffer[2 * fPendingGlyphCount + 1] = -fTextInverseRatio * y; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 406 | |
| 407 | ++fPendingGlyphCount; |
| 408 | } |
| 409 | |
| 410 | void GrStencilAndCoverTextContext::flush() { |
| 411 | if (0 == fPendingGlyphCount) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 415 | fDrawTarget->drawPaths(fGlyphs, fIndexBuffer, fPendingGlyphCount, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 416 | fTransformBuffer, fTransformType, SkPath::kWinding_FillType); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 417 | |
| 418 | fPendingGlyphCount = 0; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void GrStencilAndCoverTextContext::finish() { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 422 | this->flush(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 423 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 424 | fGlyphs->unref(); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 425 | fGlyphs = NULL; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 426 | |
| 427 | SkGlyphCache::AttachCache(fGlyphCache); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 428 | fGlyphCache = NULL; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 429 | |
| 430 | fDrawTarget->drawState()->stencil()->setDisabled(); |
| 431 | fStateRestore.set(NULL); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 432 | fContext->setMatrix(fContextInitialMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 433 | GrTextContext::finish(); |
| 434 | } |
| 435 | |