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 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 61 | // No hairlines. They would require new paths with customized strokes for every new draw matrix. |
| 62 | return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth(); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 63 | } |
| 64 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 65 | void GrStencilAndCoverTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 66 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 67 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 68 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 69 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 70 | const char text[], |
| 71 | size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 72 | SkScalar x, SkScalar y, |
| 73 | const SkIRect& regionClipBounds) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | SkASSERT(byteLength == 0 || text != nullptr); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 75 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | if (text == nullptr || byteLength == 0 /*|| fRC->isEmpty()*/) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 80 | this->init(rt, clip, paint, skPaint, byteLength, viewMatrix, regionClipBounds); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 81 | |
| 82 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 83 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 84 | const char* stop = text + byteLength; |
| 85 | |
| 86 | // Measure first if needed. |
| 87 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
| 88 | SkFixed stopX = 0; |
| 89 | SkFixed stopY = 0; |
| 90 | |
| 91 | const char* textPtr = text; |
| 92 | while (textPtr < stop) { |
| 93 | // We don't need x, y here, since all subpixel variants will have the |
| 94 | // same advance. |
| 95 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0); |
| 96 | |
| 97 | stopX += glyph.fAdvanceX; |
| 98 | stopY += glyph.fAdvanceY; |
| 99 | } |
| 100 | SkASSERT(textPtr == stop); |
| 101 | |
| 102 | SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio; |
| 103 | SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio; |
| 104 | |
| 105 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 106 | alignX = SkScalarHalf(alignX); |
| 107 | alignY = SkScalarHalf(alignY); |
| 108 | } |
| 109 | |
| 110 | x -= alignX; |
| 111 | y -= alignY; |
| 112 | } |
| 113 | |
| 114 | SkAutoKern autokern; |
| 115 | |
| 116 | SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio); |
| 117 | |
| 118 | SkFixed fx = SkScalarToFixed(x); |
| 119 | SkFixed fy = SkScalarToFixed(y); |
| 120 | while (text < stop) { |
| 121 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 122 | fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 123 | if (glyph.fWidth) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 124 | this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy))); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 125 | } |
| 126 | |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 127 | fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio); |
| 128 | fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 129 | } |
| 130 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 131 | this->finish(dc); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 132 | } |
| 133 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 134 | void GrStencilAndCoverTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 135 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 136 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 137 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 138 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 139 | const char text[], |
| 140 | size_t byteLength, |
| 141 | const SkScalar pos[], |
| 142 | int scalarsPerPosition, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 143 | const SkPoint& offset, |
| 144 | const SkIRect& regionClipBounds) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | SkASSERT(byteLength == 0 || text != nullptr); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 146 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 147 | |
| 148 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | if (text == nullptr || byteLength == 0/* || fRC->isEmpty()*/) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 153 | this->init(rt, clip, paint, skPaint, byteLength, viewMatrix, regionClipBounds); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 154 | |
| 155 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 156 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 157 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 158 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 159 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 160 | SkTextAlignProc alignProc(fSkPaint.getTextAlign()); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 161 | while (text < stop) { |
| 162 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
| 163 | if (glyph.fWidth) { |
| 164 | SkPoint tmsLoc; |
| 165 | tmsProc(pos, &tmsLoc); |
| 166 | SkPoint loc; |
| 167 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 168 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 169 | this->appendGlyph(glyph, loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 170 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 171 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 172 | } |
| 173 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 174 | this->finish(dc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 175 | } |
| 176 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 177 | static GrPathRange* get_gr_glyphs(GrContext* ctx, |
| 178 | const SkTypeface* typeface, |
| 179 | const SkDescriptor* desc, |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 180 | const GrStrokeInfo& stroke) { |
| 181 | |
| 182 | static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 183 | int strokeDataCount = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 184 | GrUniqueKey glyphKey; |
| 185 | GrUniqueKey::Builder builder(&glyphKey, kPathGlyphDomain, 2 + strokeDataCount); |
| 186 | reinterpret_cast<uint32_t&>(builder[0]) = desc ? desc->getChecksum() : 0; |
| 187 | reinterpret_cast<uint32_t&>(builder[1]) = typeface ? typeface->uniqueID() : 0; |
| 188 | if (strokeDataCount > 0) { |
| 189 | stroke.asUniqueKeyFragment(&builder[2]); |
| 190 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 191 | builder.finish(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 192 | |
| 193 | SkAutoTUnref<GrPathRange> glyphs( |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 194 | static_cast<GrPathRange*>( |
| 195 | ctx->resourceProvider()->findAndRefResourceByUniqueKey(glyphKey))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | if (nullptr == glyphs) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 197 | glyphs.reset(ctx->resourceProvider()->createGlyphs(typeface, desc, stroke)); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 198 | ctx->resourceProvider()->assignUniqueKeyToResource(glyphKey, glyphs); |
| 199 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 200 | SkASSERT(nullptr == desc || glyphs->isEqualTo(*desc)); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return glyphs.detach(); |
| 204 | } |
| 205 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 206 | void GrStencilAndCoverTextContext::init(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 207 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 208 | const GrPaint& paint, |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 209 | const SkPaint& skPaint, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 210 | size_t textByteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 211 | const SkMatrix& viewMatrix, |
| 212 | const SkIRect& regionClipBounds) { |
joshualitt | 9df4659 | 2015-07-09 10:55:28 -0700 | [diff] [blame] | 213 | fClip = clip; |
| 214 | |
| 215 | fRenderTarget.reset(SkRef(rt)); |
| 216 | |
| 217 | fRegionClipBounds = regionClipBounds; |
| 218 | fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect); |
| 219 | |
| 220 | fPaint = paint; |
| 221 | fSkPaint = skPaint; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 222 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 223 | // Don't bake strokes into the glyph outlines. We will stroke the glyphs using the GPU instead. |
| 224 | fStroke = GrStrokeInfo(fSkPaint); |
| 225 | fSkPaint.setStyle(SkPaint::kFill_Style); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 226 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 227 | SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported. |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 228 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 229 | if (fSkPaint.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
| 230 | // Instead of baking fake bold into the glyph outlines, do it with the GPU stroke. |
| 231 | SkScalar fakeBoldScale = SkScalarInterpFunc(fSkPaint.getTextSize(), |
| 232 | kStdFakeBoldInterpKeys, |
| 233 | kStdFakeBoldInterpValues, |
| 234 | kStdFakeBoldInterpLength); |
| 235 | SkScalar extra = SkScalarMul(fSkPaint.getTextSize(), fakeBoldScale); |
| 236 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 237 | true /*strokeAndFill*/); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 238 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 239 | fSkPaint.setFakeBoldText(false); |
| 240 | } |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 241 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 242 | bool canUseRawPaths; |
| 243 | if (!fStroke.isDashed()) { |
| 244 | // We can draw the glyphs from canonically sized paths. |
| 245 | fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 246 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize(); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 247 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 248 | // Compensate for the glyphs being scaled by fTextRatio. |
| 249 | if (!fStroke.isFillStyle()) { |
| 250 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 251 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
| 252 | } |
| 253 | |
| 254 | fSkPaint.setLinearText(true); |
| 255 | fSkPaint.setLCDRenderText(false); |
| 256 | fSkPaint.setAutohinted(false); |
| 257 | fSkPaint.setHinting(SkPaint::kNo_Hinting); |
| 258 | fSkPaint.setSubpixelText(true); |
| 259 | fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 260 | |
| 261 | canUseRawPaths = SK_Scalar1 == fSkPaint.getTextScaleX() && |
| 262 | 0 == fSkPaint.getTextSkewX() && |
| 263 | !fSkPaint.isFakeBoldText() && |
| 264 | !fSkPaint.isVerticalText(); |
| 265 | } else { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 266 | fTextRatio = fTextInverseRatio = 1.0f; |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 267 | canUseRawPaths = false; |
| 268 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 269 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 270 | fViewMatrix = viewMatrix; |
| 271 | fViewMatrix.preScale(fTextRatio, fTextRatio); |
| 272 | fLocalMatrix.setScale(fTextRatio, fTextRatio); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 273 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 274 | fGlyphCache = fSkPaint.detachCache(&fSurfaceProps, nullptr, true /*ignoreGamma*/); |
| 275 | fGlyphs = canUseRawPaths ? |
| 276 | get_gr_glyphs(fContext, fSkPaint.getTypeface(), nullptr, fStroke) : |
| 277 | get_gr_glyphs(fContext, fGlyphCache->getScalerContext()->getTypeface(), |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 278 | &fGlyphCache->getDescriptor(), fStroke); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 279 | } |
| 280 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 281 | inline void GrStencilAndCoverTextContext::appendGlyph(const SkGlyph& glyph, const SkPoint& pos) { |
| 282 | // Stick the glyphs we can't draw into the fallback arrays. |
| 283 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
| 284 | fFallbackIndices.push_back(glyph.getGlyphID()); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 285 | fFallbackPositions.push_back(pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 286 | } else { |
| 287 | // TODO: infer the reserve count from the text length. |
| 288 | if (!fDraw) { |
| 289 | fDraw = GrPathRangeDraw::Create(fGlyphs, |
| 290 | GrPathRendering::kTranslate_PathTransformType, |
| 291 | 64); |
| 292 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 293 | float translate[] = { fTextInverseRatio * pos.x(), fTextInverseRatio * pos.y() }; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 294 | fDraw->append(glyph.getGlyphID(), translate); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 295 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 296 | } |
| 297 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 298 | void GrStencilAndCoverTextContext::flush(GrDrawContext* dc) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 299 | if (fDraw) { |
| 300 | SkASSERT(fDraw->count()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 301 | |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 302 | // We should only be flushing about once every run. However, if this impacts performance |
| 303 | // we could move the creation of the GrPipelineBuilder earlier. |
| 304 | GrPipelineBuilder pipelineBuilder(fPaint, fRenderTarget, fClip); |
| 305 | SkASSERT(fRenderTarget->isStencilBufferMultisampled() || !fPaint.isAntiAlias()); |
| 306 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, fPaint.isAntiAlias()); |
| 307 | |
| 308 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 309 | kZero_StencilOp, |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 310 | kKeep_StencilOp, |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 311 | kNotEqual_StencilFunc, |
| 312 | 0xffff, |
| 313 | 0x0000, |
| 314 | 0xffff); |
| 315 | |
| 316 | *pipelineBuilder.stencil() = kStencilPass; |
| 317 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 318 | dc->drawPathsFromRange(&pipelineBuilder, fViewMatrix, fLocalMatrix, fPaint.getColor(), |
| 319 | fDraw, GrPathRendering::kWinding_FillType); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 320 | fDraw->unref(); |
| 321 | fDraw = nullptr; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 322 | } |
| 323 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 324 | if (fFallbackIndices.count()) { |
| 325 | SkASSERT(fFallbackPositions.count() == fFallbackIndices.count()); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 326 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 327 | SkPaint fallbackSkPaint(fSkPaint); |
| 328 | fStroke.applyToPaint(&fallbackSkPaint); |
| 329 | if (!fStroke.isFillStyle()) { |
| 330 | fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 331 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 332 | fallbackSkPaint.setTextAlign(SkPaint::kLeft_Align); // Align has already been accounted for. |
| 333 | fallbackSkPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 334 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color |
| 335 | // glyphs show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 336 | fallbackSkPaint.setSubpixelText(false); |
| 337 | fallbackSkPaint.setTextSize(fSkPaint.getTextSize() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 338 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 339 | SkMatrix fallbackMatrix(fViewMatrix); |
| 340 | fallbackMatrix.preScale(fTextInverseRatio, fTextInverseRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 341 | |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 342 | fFallbackTextContext->drawPosText(dc, fRenderTarget, fClip, fPaint, fallbackSkPaint, |
| 343 | fallbackMatrix, (char*)fFallbackIndices.begin(), |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 344 | sizeof(uint16_t) * fFallbackIndices.count(), |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 345 | fFallbackPositions[0].asScalars(), 2, SkPoint::Make(0, 0), |
| 346 | fRegionClipBounds); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 347 | fFallbackIndices.reset(); |
| 348 | fFallbackPositions.reset(); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 349 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 350 | } |
| 351 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 352 | void GrStencilAndCoverTextContext::finish(GrDrawContext* dc) { |
| 353 | this->flush(dc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 354 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 355 | SkASSERT(!fDraw); |
| 356 | SkASSERT(!fFallbackIndices.count()); |
| 357 | SkASSERT(!fFallbackPositions.count()); |
| 358 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 359 | fGlyphs->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 360 | fGlyphs = nullptr; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 361 | |
| 362 | SkGlyphCache::AttachCache(fGlyphCache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 363 | fGlyphCache = nullptr; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 364 | } |