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