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