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