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) |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 28 | : INHERITED(context, surfaceProps) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 29 | } |
| 30 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 31 | GrStencilAndCoverTextContext* |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 32 | GrStencilAndCoverTextContext::Create(GrContext* context, const SkSurfaceProps& surfaceProps) { |
| 33 | GrStencilAndCoverTextContext* textContext = |
| 34 | new GrStencilAndCoverTextContext(context, surfaceProps); |
| 35 | textContext->fFallbackTextContext = GrAtlasTextContext::Create(context, surfaceProps); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 36 | |
| 37 | return textContext; |
| 38 | } |
| 39 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 40 | GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() { |
| 41 | } |
| 42 | |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 43 | bool GrStencilAndCoverTextContext::canDraw(const GrRenderTarget* rt, |
| 44 | const GrClip& clip, |
| 45 | const GrPaint& paint, |
| 46 | const SkPaint& skPaint, |
| 47 | const SkMatrix& viewMatrix) { |
| 48 | if (skPaint.getRasterizer()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 49 | return false; |
| 50 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 51 | if (skPaint.getMaskFilter()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 52 | return false; |
| 53 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 54 | if (SkPathEffect* pe = skPaint.getPathEffect()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 58 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 59 | // No hairlines. They would require new paths with customized strokes for every new draw matrix. |
| 60 | return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth(); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 61 | } |
| 62 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 63 | void GrStencilAndCoverTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 64 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 65 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 66 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 67 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 68 | const char text[], |
| 69 | size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 70 | SkScalar x, SkScalar y, |
| 71 | const SkIRect& regionClipBounds) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 72 | TextRun run(skPaint); |
| 73 | run.setText(text, byteLength, x, y, fContext, &fSurfaceProps); |
| 74 | run.draw(dc, rt, clip, paint, viewMatrix, regionClipBounds, fFallbackTextContext, skPaint); |
| 75 | } |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 76 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 77 | void GrStencilAndCoverTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt, |
| 78 | const GrClip& clip, |
| 79 | const GrPaint& paint, |
| 80 | const SkPaint& skPaint, |
| 81 | const SkMatrix& viewMatrix, |
| 82 | const char text[], |
| 83 | size_t byteLength, |
| 84 | const SkScalar pos[], |
| 85 | int scalarsPerPosition, |
| 86 | const SkPoint& offset, |
| 87 | const SkIRect& regionClipBounds) { |
| 88 | TextRun run(skPaint); |
| 89 | run.setPosText(text, byteLength, pos, scalarsPerPosition, offset, fContext, &fSurfaceProps); |
| 90 | run.draw(dc, rt, clip, paint, viewMatrix, regionClipBounds, fFallbackTextContext, skPaint); |
| 91 | } |
| 92 | |
| 93 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 94 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 95 | class GrStencilAndCoverTextContext::FallbackBlobBuilder { |
| 96 | public: |
| 97 | FallbackBlobBuilder() : fBuffIdx(0) {} |
| 98 | |
| 99 | bool isInitialized() const { return SkToBool(fBuilder); } |
| 100 | |
| 101 | void init(const SkPaint& font, SkScalar textRatio); |
| 102 | |
| 103 | void appendGlyph(uint16_t glyphId, const SkPoint& pos); |
| 104 | |
| 105 | const SkTextBlob* buildIfInitialized(); |
| 106 | |
| 107 | private: |
| 108 | enum { kWriteBufferSize = 1024 }; |
| 109 | |
| 110 | void flush(); |
| 111 | |
| 112 | SkAutoTDelete<SkTextBlobBuilder> fBuilder; |
| 113 | SkPaint fFont; |
| 114 | int fBuffIdx; |
| 115 | uint16_t fGlyphIds[kWriteBufferSize]; |
| 116 | SkPoint fPositions[kWriteBufferSize]; |
| 117 | }; |
| 118 | |
| 119 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 120 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 121 | GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke) |
| 122 | : fStroke(fontAndStroke), |
| 123 | fFont(fontAndStroke) { |
| 124 | SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported. |
| 125 | |
| 126 | // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path |
| 127 | // rendering API for stroking). |
| 128 | fFont.setStyle(SkPaint::kFill_Style); |
| 129 | |
| 130 | if (fFont.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
| 131 | // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke. |
| 132 | SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(), |
| 133 | kStdFakeBoldInterpKeys, |
| 134 | kStdFakeBoldInterpValues, |
| 135 | kStdFakeBoldInterpLength); |
| 136 | SkScalar extra = SkScalarMul(fFont.getTextSize(), fakeBoldScale); |
| 137 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 138 | true /*strokeAndFill*/); |
| 139 | |
| 140 | fFont.setFakeBoldText(false); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 141 | } |
| 142 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 143 | if (!fFont.getPathEffect() && !fStroke.isDashed()) { |
| 144 | // We can draw the glyphs from canonically sized paths. |
| 145 | fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 146 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 147 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 148 | // Compensate for the glyphs being scaled by fTextRatio. |
| 149 | if (!fStroke.isFillStyle()) { |
| 150 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 151 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
| 152 | } |
| 153 | |
| 154 | fFont.setLinearText(true); |
| 155 | fFont.setLCDRenderText(false); |
| 156 | fFont.setAutohinted(false); |
| 157 | fFont.setHinting(SkPaint::kNo_Hinting); |
| 158 | fFont.setSubpixelText(true); |
| 159 | fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 160 | |
| 161 | fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() && |
| 162 | 0 == fFont.getTextSkewX() && |
| 163 | !fFont.isFakeBoldText() && |
| 164 | !fFont.isVerticalText(); |
| 165 | } else { |
| 166 | fTextRatio = fTextInverseRatio = 1.0f; |
| 167 | fUsingRawGlyphPaths = false; |
| 168 | } |
| 169 | |
| 170 | // When drawing from canonically sized paths, the actual local coords are fTextRatio * coords. |
| 171 | fLocalMatrix.setScale(fTextRatio, fTextRatio); |
| 172 | } |
| 173 | |
| 174 | GrStencilAndCoverTextContext::TextRun::~TextRun() { |
| 175 | } |
| 176 | |
| 177 | void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t byteLength, |
| 178 | SkScalar x, SkScalar y, GrContext* ctx, |
| 179 | const SkSurfaceProps* surfaceProps) { |
| 180 | SkASSERT(byteLength == 0 || text != nullptr); |
| 181 | |
| 182 | SkAutoGlyphCacheNoGamma autoGlyphCache(fFont, surfaceProps, nullptr); |
| 183 | SkGlyphCache* glyphCache = autoGlyphCache.getCache(); |
| 184 | |
| 185 | fDraw.reset(GrPathRangeDraw::Create(this->createGlyphs(ctx, glyphCache), |
| 186 | GrPathRendering::kTranslate_PathTransformType, |
| 187 | fFont.countText(text, byteLength))); |
| 188 | |
| 189 | SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 190 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 191 | const char* stop = text + byteLength; |
| 192 | |
| 193 | // Measure first if needed. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 194 | if (fFont.getTextAlign() != SkPaint::kLeft_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 195 | SkFixed stopX = 0; |
| 196 | SkFixed stopY = 0; |
| 197 | |
| 198 | const char* textPtr = text; |
| 199 | while (textPtr < stop) { |
| 200 | // We don't need x, y here, since all subpixel variants will have the |
| 201 | // same advance. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 202 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr, 0, 0); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 203 | |
| 204 | stopX += glyph.fAdvanceX; |
| 205 | stopY += glyph.fAdvanceY; |
| 206 | } |
| 207 | SkASSERT(textPtr == stop); |
| 208 | |
| 209 | SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio; |
| 210 | SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio; |
| 211 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 212 | if (fFont.getTextAlign() == SkPaint::kCenter_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 213 | alignX = SkScalarHalf(alignX); |
| 214 | alignY = SkScalarHalf(alignY); |
| 215 | } |
| 216 | |
| 217 | x -= alignX; |
| 218 | y -= alignY; |
| 219 | } |
| 220 | |
| 221 | SkAutoKern autokern; |
| 222 | |
| 223 | SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio); |
| 224 | |
| 225 | SkFixed fx = SkScalarToFixed(x); |
| 226 | SkFixed fy = SkScalarToFixed(y); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 227 | FallbackBlobBuilder fallback; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 228 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 229 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 230 | fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 231 | if (glyph.fWidth) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 232 | this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)), |
| 233 | &fallback); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 234 | } |
| 235 | |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 236 | fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio); |
| 237 | fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 238 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 239 | |
| 240 | fFallbackTextBlob.reset(fallback.buildIfInitialized()); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 241 | } |
| 242 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 243 | void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength, |
| 244 | const SkScalar pos[], int scalarsPerPosition, |
| 245 | const SkPoint& offset, GrContext* ctx, |
| 246 | const SkSurfaceProps* surfaceProps) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 247 | SkASSERT(byteLength == 0 || text != nullptr); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 248 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 249 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 250 | SkAutoGlyphCacheNoGamma autoGlyphCache(fFont, surfaceProps, nullptr); |
| 251 | SkGlyphCache* glyphCache = autoGlyphCache.getCache(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 252 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 253 | fDraw.reset(GrPathRangeDraw::Create(this->createGlyphs(ctx, glyphCache), |
| 254 | GrPathRendering::kTranslate_PathTransformType, |
| 255 | fFont.countText(text, byteLength))); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 256 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 257 | SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 258 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 259 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 260 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 261 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 262 | SkTextAlignProc alignProc(fFont.getTextAlign()); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 263 | FallbackBlobBuilder fallback; |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 264 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 265 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 266 | if (glyph.fWidth) { |
| 267 | SkPoint tmsLoc; |
| 268 | tmsProc(pos, &tmsLoc); |
| 269 | SkPoint loc; |
| 270 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 271 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 272 | this->appendGlyph(glyph, loc, &fallback); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 273 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 274 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 275 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 276 | |
| 277 | fFallbackTextBlob.reset(fallback.buildIfInitialized()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 278 | } |
| 279 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 280 | GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx, |
| 281 | SkGlyphCache* glyphCache) { |
| 282 | SkTypeface* typeface = fUsingRawGlyphPaths ? fFont.getTypeface() |
| 283 | : glyphCache->getScalerContext()->getTypeface(); |
| 284 | const SkDescriptor* desc = fUsingRawGlyphPaths ? nullptr : &glyphCache->getDescriptor(); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 285 | |
| 286 | static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 287 | int strokeDataCount = fStroke.computeUniqueKeyFragmentData32Cnt(); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 288 | GrUniqueKey glyphKey; |
| 289 | GrUniqueKey::Builder builder(&glyphKey, kPathGlyphDomain, 2 + strokeDataCount); |
| 290 | reinterpret_cast<uint32_t&>(builder[0]) = desc ? desc->getChecksum() : 0; |
| 291 | reinterpret_cast<uint32_t&>(builder[1]) = typeface ? typeface->uniqueID() : 0; |
| 292 | if (strokeDataCount > 0) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 293 | fStroke.asUniqueKeyFragment(&builder[2]); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 294 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 295 | builder.finish(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 296 | |
| 297 | SkAutoTUnref<GrPathRange> glyphs( |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 298 | static_cast<GrPathRange*>( |
| 299 | ctx->resourceProvider()->findAndRefResourceByUniqueKey(glyphKey))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 300 | if (nullptr == glyphs) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 301 | glyphs.reset(ctx->resourceProvider()->createGlyphs(typeface, desc, fStroke)); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 302 | ctx->resourceProvider()->assignUniqueKeyToResource(glyphKey, glyphs); |
| 303 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 304 | SkASSERT(nullptr == desc || glyphs->isEqualTo(*desc)); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return glyphs.detach(); |
| 308 | } |
| 309 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 310 | inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph, |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 311 | const SkPoint& pos, |
| 312 | FallbackBlobBuilder* fallback) { |
| 313 | // Stick the glyphs we can't draw into the fallback text blob. |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 314 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 315 | if (!fallback->isInitialized()) { |
| 316 | fallback->init(fFont, fTextRatio); |
| 317 | } |
| 318 | fallback->appendGlyph(glyph.getGlyphID(), pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 319 | } else { |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 320 | float translate[] = { fTextInverseRatio * pos.x(), fTextInverseRatio * pos.y() }; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 321 | fDraw->append(glyph.getGlyphID(), translate); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 322 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 323 | } |
| 324 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 325 | void GrStencilAndCoverTextContext::TextRun::draw(GrDrawContext* dc, |
| 326 | GrRenderTarget* rt, |
| 327 | const GrClip& clip, |
| 328 | const GrPaint& paint, |
| 329 | const SkMatrix& viewMatrix, |
| 330 | const SkIRect& regionClipBounds, |
| 331 | GrTextContext* fallbackTextContext, |
| 332 | const SkPaint& originalSkPaint) const { |
| 333 | SkASSERT(fDraw); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 334 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 335 | if (fDraw->count()) { |
| 336 | GrPipelineBuilder pipelineBuilder(paint, rt, clip); |
| 337 | SkASSERT(rt->isStencilBufferMultisampled() || !paint.isAntiAlias()); |
| 338 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, paint.isAntiAlias()); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 339 | |
| 340 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 341 | kZero_StencilOp, |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 342 | kKeep_StencilOp, |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 343 | kNotEqual_StencilFunc, |
| 344 | 0xffff, |
| 345 | 0x0000, |
| 346 | 0xffff); |
| 347 | |
| 348 | *pipelineBuilder.stencil() = kStencilPass; |
| 349 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 350 | SkMatrix drawMatrix(viewMatrix); |
| 351 | drawMatrix.preScale(fTextRatio, fTextRatio); |
| 352 | |
| 353 | dc->drawPathsFromRange(&pipelineBuilder, drawMatrix, fLocalMatrix, paint.getColor(), fDraw, |
| 354 | GrPathRendering::kWinding_FillType); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 355 | } |
| 356 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 357 | if (fFallbackTextBlob) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 358 | SkPaint fallbackSkPaint(originalSkPaint); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 359 | fStroke.applyToPaint(&fallbackSkPaint); |
| 360 | if (!fStroke.isFillStyle()) { |
| 361 | fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 362 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 363 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 364 | fallbackTextContext->drawTextBlob(dc, rt, clip, fallbackSkPaint, viewMatrix, |
| 365 | fFallbackTextBlob, 0, 0, nullptr, regionClipBounds); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 366 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 367 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame^] | 368 | |
| 369 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 370 | |
| 371 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font, |
| 372 | SkScalar textRatio) { |
| 373 | SkASSERT(!this->isInitialized()); |
| 374 | fBuilder.reset(new SkTextBlobBuilder); |
| 375 | fFont = font; |
| 376 | fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align. |
| 377 | fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 378 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs |
| 379 | // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 380 | fFont.setSubpixelText(false); |
| 381 | fFont.setTextSize(fFont.getTextSize() * textRatio); |
| 382 | fBuffIdx = 0; |
| 383 | } |
| 384 | |
| 385 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId, |
| 386 | const SkPoint& pos) { |
| 387 | SkASSERT(this->isInitialized()); |
| 388 | if (fBuffIdx >= kWriteBufferSize) { |
| 389 | this->flush(); |
| 390 | } |
| 391 | fGlyphIds[fBuffIdx] = glyphId; |
| 392 | fPositions[fBuffIdx] = pos; |
| 393 | fBuffIdx++; |
| 394 | } |
| 395 | |
| 396 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() { |
| 397 | SkASSERT(this->isInitialized()); |
| 398 | SkASSERT(fBuffIdx <= kWriteBufferSize); |
| 399 | if (!fBuffIdx) { |
| 400 | return; |
| 401 | } |
| 402 | // This will automatically merge with previous runs since we use the same font. |
| 403 | const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx); |
| 404 | memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t)); |
| 405 | memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar)); |
| 406 | fBuffIdx = 0; |
| 407 | } |
| 408 | |
| 409 | const SkTextBlob* GrStencilAndCoverTextContext::FallbackBlobBuilder::buildIfInitialized() { |
| 410 | if (!this->isInitialized()) { |
| 411 | return nullptr; |
| 412 | } |
| 413 | this->flush(); |
| 414 | return fBuilder->build(); |
| 415 | } |