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