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