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" |
| 9 | #include "GrDrawTarget.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 10 | #include "GrGpu.h" |
| 11 | #include "GrPath.h" |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 12 | #include "GrPathRange.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 13 | #include "SkAutoKern.h" |
| 14 | #include "SkDraw.h" |
| 15 | #include "SkDrawProcs.h" |
| 16 | #include "SkGlyphCache.h" |
| 17 | #include "SkGpuDevice.h" |
| 18 | #include "SkPath.h" |
| 19 | #include "SkTextMapStateProc.h" |
| 20 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 21 | class GrStencilAndCoverTextContext::GlyphPathRange : public GrGpuResource { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 22 | static const int kMaxGlyphCount = 1 << 16; // Glyph IDs are uint16_t's |
| 23 | static const int kGlyphGroupSize = 16; // Glyphs get tracked in groups of 16 |
| 24 | |
| 25 | public: |
| 26 | static GlyphPathRange* Create(GrContext* context, |
| 27 | SkGlyphCache* cache, |
| 28 | const SkStrokeRec& stroke) { |
| 29 | static const GrCacheID::Domain gGlyphPathRangeDomain = GrCacheID::GenerateDomain(); |
| 30 | |
| 31 | GrCacheID::Key key; |
| 32 | key.fData32[0] = cache->getDescriptor().getChecksum(); |
| 33 | key.fData32[1] = cache->getScalerContext()->getTypeface()->uniqueID(); |
| 34 | key.fData64[1] = GrPath::ComputeStrokeKey(stroke); |
| 35 | |
| 36 | GrResourceKey resourceKey(GrCacheID(gGlyphPathRangeDomain, key), |
| 37 | GrPathRange::resourceType(), 0); |
| 38 | SkAutoTUnref<GlyphPathRange> glyphs( |
| 39 | static_cast<GlyphPathRange*>(context->findAndRefCachedResource(resourceKey))); |
| 40 | |
| 41 | if (NULL == glyphs || |
| 42 | !glyphs->fDesc->equals(cache->getDescriptor() /*checksum collision*/)) { |
| 43 | glyphs.reset(SkNEW_ARGS(GlyphPathRange, (context, cache->getDescriptor(), stroke))); |
| 44 | context->addResourceToCache(resourceKey, glyphs); |
| 45 | } |
| 46 | |
| 47 | return glyphs.detach(); |
| 48 | } |
| 49 | |
| 50 | const GrPathRange* pathRange() const { return fPathRange.get(); } |
| 51 | |
| 52 | void preloadGlyph(uint16_t glyphID, SkGlyphCache* cache) { |
| 53 | const uint16_t groupIndex = glyphID / kGlyphGroupSize; |
| 54 | const uint16_t groupByte = groupIndex >> 3; |
| 55 | const uint8_t groupBit = 1 << (groupIndex & 7); |
| 56 | |
| 57 | const bool hasGlyph = 0 != (fLoadedGlyphs[groupByte] & groupBit); |
| 58 | if (hasGlyph) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // We track which glyphs are loaded in groups of kGlyphGroupSize. To |
| 63 | // mark a glyph loaded we need to load the entire group. |
| 64 | const uint16_t groupFirstID = groupIndex * kGlyphGroupSize; |
| 65 | const uint16_t groupLastID = groupFirstID + kGlyphGroupSize - 1; |
| 66 | SkPath skPath; |
| 67 | for (int id = groupFirstID; id <= groupLastID; ++id) { |
| 68 | const SkGlyph& skGlyph = cache->getGlyphIDMetrics(id); |
| 69 | if (const SkPath* skPath = cache->findPath(skGlyph)) { |
| 70 | fPathRange->initAt(id, *skPath); |
| 71 | } // GrGpu::drawPaths will silently ignore undefined paths. |
| 72 | } |
| 73 | |
| 74 | fLoadedGlyphs[groupByte] |= groupBit; |
| 75 | this->didChangeGpuMemorySize(); |
| 76 | } |
| 77 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 78 | // GrGpuResource overrides |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 79 | virtual size_t gpuMemorySize() const SK_OVERRIDE { return fPathRange->gpuMemorySize(); } |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | GlyphPathRange(GrContext* context, const SkDescriptor& desc, const SkStrokeRec& stroke) |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 83 | : INHERITED(context->getGpu(), false) |
| 84 | , fDesc(desc.copy()) |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 85 | // We reserve a range of kMaxGlyphCount paths because of fallbacks fonts. We |
| 86 | // can't know exactly how many glyphs we might need without preloading every |
| 87 | // fallback, which we don't want to do at this point. |
| 88 | , fPathRange(context->getGpu()->createPathRange(kMaxGlyphCount, stroke)) { |
| 89 | memset(fLoadedGlyphs, 0, sizeof(fLoadedGlyphs)); |
| 90 | } |
| 91 | |
| 92 | ~GlyphPathRange() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 93 | this->release(); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 94 | SkDescriptor::Free(fDesc); |
| 95 | } |
| 96 | |
| 97 | static const int kMaxGroupCount = (kMaxGlyphCount + (kGlyphGroupSize - 1)) / kGlyphGroupSize; |
| 98 | SkDescriptor* const fDesc; |
| 99 | uint8_t fLoadedGlyphs[(kMaxGroupCount + 7) >> 3]; // One bit per glyph group |
| 100 | SkAutoTUnref<GrPathRange> fPathRange; |
| 101 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 102 | typedef GrGpuResource INHERITED; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 105 | |
| 106 | GrStencilAndCoverTextContext::GrStencilAndCoverTextContext( |
| 107 | GrContext* context, const SkDeviceProperties& properties) |
| 108 | : GrTextContext(context, properties) |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 109 | , fStroke(SkStrokeRec::kFill_InitStyle) |
| 110 | , fPendingGlyphCount(0) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() { |
| 114 | } |
| 115 | |
| 116 | void GrStencilAndCoverTextContext::drawText(const GrPaint& paint, |
| 117 | const SkPaint& skPaint, |
| 118 | const char text[], |
| 119 | size_t byteLength, |
| 120 | SkScalar x, SkScalar y) { |
| 121 | SkASSERT(byteLength == 0 || text != NULL); |
| 122 | |
| 123 | if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | // This is the slow path, mainly used by Skia unit tests. The other |
| 128 | // backends (8888, gpu, ...) use device-space dependent glyph caches. In |
| 129 | // order to match the glyph positions that the other code paths produce, we |
| 130 | // must also use device-space dependent glyph cache. This has the |
| 131 | // side-effect that the glyph shape outline will be in device-space, |
| 132 | // too. This in turn has the side-effect that NVPR can not stroke the paths, |
| 133 | // as the stroke in NVPR is defined in object-space. |
| 134 | // NOTE: here we have following coincidence that works at the moment: |
| 135 | // - When using the device-space glyphs, the transforms we pass to NVPR |
| 136 | // instanced drawing are the global transforms, and the view transform is |
| 137 | // identity. NVPR can not use non-affine transforms in the instanced |
| 138 | // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it |
| 139 | // will turn off the use of device-space glyphs when perspective transforms |
| 140 | // are in use. |
| 141 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 142 | this->init(paint, skPaint, byteLength, kUseIfNeeded_DeviceSpaceGlyphsBehavior); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 143 | |
| 144 | SkMatrix* glyphCacheTransform = NULL; |
| 145 | // Transform our starting point. |
| 146 | if (fNeedsDeviceSpaceGlyphs) { |
| 147 | SkPoint loc; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 148 | fContextInitialMatrix.mapXY(x, y, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 149 | x = loc.fX; |
| 150 | y = loc.fY; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 151 | glyphCacheTransform = &fContextInitialMatrix; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 155 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, glyphCacheTransform); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 156 | fGlyphCache = autoCache.getCache(); |
| 157 | fGlyphs = GlyphPathRange::Create(fContext, fGlyphCache, fStroke); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 158 | fTransformType = GrDrawTarget::kTranslate_PathTransformType; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 159 | |
| 160 | const char* stop = text + byteLength; |
| 161 | |
| 162 | // Measure first if needed. |
| 163 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
| 164 | SkFixed stopX = 0; |
| 165 | SkFixed stopY = 0; |
| 166 | |
| 167 | const char* textPtr = text; |
| 168 | while (textPtr < stop) { |
| 169 | // We don't need x, y here, since all subpixel variants will have the |
| 170 | // same advance. |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 171 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 172 | |
| 173 | stopX += glyph.fAdvanceX; |
| 174 | stopY += glyph.fAdvanceY; |
| 175 | } |
| 176 | SkASSERT(textPtr == stop); |
| 177 | |
| 178 | SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio; |
| 179 | SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio; |
| 180 | |
| 181 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 182 | alignX = SkScalarHalf(alignX); |
| 183 | alignY = SkScalarHalf(alignY); |
| 184 | } |
| 185 | |
| 186 | x -= alignX; |
| 187 | y -= alignY; |
| 188 | } |
| 189 | |
| 190 | SkAutoKern autokern; |
| 191 | |
| 192 | SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio); |
| 193 | |
| 194 | SkFixed fx = SkScalarToFixed(x); |
| 195 | SkFixed fy = SkScalarToFixed(y); |
| 196 | while (text < stop) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 197 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 198 | fx += SkFixedMul_portable(autokern.adjust(glyph), fixedSizeRatio); |
| 199 | if (glyph.fWidth) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 200 | this->appendGlyph(glyph.getGlyphID(), SkFixedToScalar(fx), SkFixedToScalar(fy)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | fx += SkFixedMul_portable(glyph.fAdvanceX, fixedSizeRatio); |
| 204 | fy += SkFixedMul_portable(glyph.fAdvanceY, fixedSizeRatio); |
| 205 | } |
| 206 | |
| 207 | this->finish(); |
| 208 | } |
| 209 | |
| 210 | void GrStencilAndCoverTextContext::drawPosText(const GrPaint& paint, |
| 211 | const SkPaint& skPaint, |
| 212 | const char text[], |
| 213 | size_t byteLength, |
| 214 | const SkScalar pos[], |
| 215 | SkScalar constY, |
| 216 | int scalarsPerPosition) { |
| 217 | SkASSERT(byteLength == 0 || text != NULL); |
| 218 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 219 | |
| 220 | // nothing to draw |
| 221 | if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // This is the fast path. Here we do not bake in the device-transform to |
| 226 | // the glyph outline or the advances. This is because we do not need to |
| 227 | // position the glyphs at all, since the caller has done the positioning. |
| 228 | // The positioning is based on SkPaint::measureText of individual |
| 229 | // glyphs. That already uses glyph cache without device transforms. Device |
| 230 | // transform is not part of SkPaint::measureText API, and thus we use the |
| 231 | // same glyphs as what were measured. |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 232 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 233 | const float textTranslateY = (1 == scalarsPerPosition ? constY : 0); |
| 234 | this->init(paint, skPaint, byteLength, kDoNotUse_DeviceSpaceGlyphsBehavior, textTranslateY); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 235 | |
| 236 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 237 | |
| 238 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 239 | fGlyphCache = autoCache.getCache(); |
| 240 | fGlyphs = GlyphPathRange::Create(fContext, fGlyphCache, fStroke); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 241 | |
| 242 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 243 | |
| 244 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 245 | if (1 == scalarsPerPosition) { |
| 246 | fTransformType = GrDrawTarget::kTranslateX_PathTransformType; |
| 247 | while (text < stop) { |
| 248 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
| 249 | if (glyph.fWidth) { |
| 250 | this->appendGlyph(glyph.getGlyphID(), *pos); |
| 251 | } |
| 252 | pos++; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 253 | } |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 254 | } else { |
| 255 | SkASSERT(2 == scalarsPerPosition); |
| 256 | fTransformType = GrDrawTarget::kTranslate_PathTransformType; |
| 257 | while (text < stop) { |
| 258 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
| 259 | if (glyph.fWidth) { |
| 260 | this->appendGlyph(glyph.getGlyphID(), pos[0], pos[1]); |
| 261 | } |
| 262 | pos += 2; |
| 263 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 264 | } |
| 265 | } else { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 266 | fTransformType = GrDrawTarget::kTranslate_PathTransformType; |
| 267 | SkTextMapStateProc tmsProc(SkMatrix::I(), 0, scalarsPerPosition); |
| 268 | SkTextAlignProcScalar alignProc(fSkPaint.getTextAlign()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 269 | while (text < stop) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 270 | const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 271 | if (glyph.fWidth) { |
| 272 | SkPoint tmsLoc; |
| 273 | tmsProc(pos, &tmsLoc); |
| 274 | SkPoint loc; |
| 275 | alignProc(tmsLoc, glyph, &loc); |
| 276 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 277 | this->appendGlyph(glyph.getGlyphID(), loc.x(), loc.y()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 278 | } |
| 279 | pos += scalarsPerPosition; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | this->finish(); |
| 284 | } |
| 285 | |
| 286 | bool GrStencilAndCoverTextContext::canDraw(const SkPaint& paint) { |
| 287 | if (paint.getRasterizer()) { |
| 288 | return false; |
| 289 | } |
| 290 | if (paint.getMaskFilter()) { |
| 291 | return false; |
| 292 | } |
| 293 | if (paint.getPathEffect()) { |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | // No hairlines unless we can map the 1 px width to the object space. |
| 298 | if (paint.getStyle() == SkPaint::kStroke_Style |
| 299 | && paint.getStrokeWidth() == 0 |
| 300 | && fContext->getMatrix().hasPerspective()) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | // No color bitmap fonts. |
| 305 | SkScalerContext::Rec rec; |
| 306 | SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec); |
| 307 | return rec.getFormat() != SkMask::kARGB32_Format; |
| 308 | } |
| 309 | |
| 310 | void GrStencilAndCoverTextContext::init(const GrPaint& paint, |
| 311 | const SkPaint& skPaint, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 312 | size_t textByteLength, |
| 313 | DeviceSpaceGlyphsBehavior deviceSpaceGlyphsBehavior, |
| 314 | SkScalar textTranslateY) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 315 | GrTextContext::init(paint, skPaint); |
| 316 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 317 | fContextInitialMatrix = fContext->getMatrix(); |
| 318 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 319 | bool otherBackendsWillDrawAsPaths = |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 320 | SkDraw::ShouldDrawTextAsPaths(skPaint, fContextInitialMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 321 | |
| 322 | if (otherBackendsWillDrawAsPaths) { |
| 323 | // This is to reproduce SkDraw::drawText_asPaths glyph positions. |
| 324 | fSkPaint.setLinearText(true); |
| 325 | fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 326 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fSkPaint.getTextSize(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 327 | fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 328 | if (fSkPaint.getStyle() != SkPaint::kFill_Style) { |
| 329 | // Compensate the glyphs being scaled up by fTextRatio by scaling the |
| 330 | // stroke down. |
| 331 | fSkPaint.setStrokeWidth(fSkPaint.getStrokeWidth() / fTextRatio); |
| 332 | } |
| 333 | fNeedsDeviceSpaceGlyphs = false; |
| 334 | } else { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 335 | fTextRatio = fTextInverseRatio = 1.0f; |
| 336 | fNeedsDeviceSpaceGlyphs = |
| 337 | kUseIfNeeded_DeviceSpaceGlyphsBehavior == deviceSpaceGlyphsBehavior && |
| 338 | (fContextInitialMatrix.getType() & |
| 339 | (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask)) != 0; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 340 | // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms. |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 341 | SkASSERT(!fContextInitialMatrix.hasPerspective()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | fStroke = SkStrokeRec(fSkPaint); |
| 345 | |
| 346 | if (fNeedsDeviceSpaceGlyphs) { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 347 | SkASSERT(1.0f == fTextRatio); |
| 348 | SkASSERT(0.0f == textTranslateY); |
| 349 | fPaint.localCoordChangeInverse(fContextInitialMatrix); |
| 350 | fContext->setIdentityMatrix(); |
| 351 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 352 | // The whole shape is baked into the glyph. Make NVPR just fill the |
| 353 | // baked shape. |
| 354 | fStroke.setStrokeStyle(-1, false); |
| 355 | } else { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 356 | if (1.0f != fTextRatio || 0.0f != textTranslateY) { |
| 357 | SkMatrix textMatrix; |
| 358 | textMatrix.setTranslate(0, textTranslateY); |
| 359 | textMatrix.preScale(fTextRatio, fTextRatio); |
| 360 | fPaint.localCoordChange(textMatrix); |
| 361 | fContext->concatMatrix(textMatrix); |
| 362 | } |
| 363 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 364 | if (fSkPaint.getStrokeWidth() == 0.0f) { |
| 365 | if (fSkPaint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 366 | fStroke.setStrokeStyle(-1, false); |
| 367 | } else if (fSkPaint.getStyle() == SkPaint::kStroke_Style) { |
| 368 | // Approximate hairline stroke. |
| 369 | const SkMatrix& ctm = fContext->getMatrix(); |
| 370 | SkScalar strokeWidth = SK_Scalar1 / |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 371 | (SkVector::Make(ctm.getScaleX(), ctm.getSkewY()).length()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 372 | fStroke.setStrokeStyle(strokeWidth, false); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Make glyph cache produce paths geometry for fill. We will stroke them |
| 377 | // by passing fStroke to drawPath. This is the fast path. |
| 378 | fSkPaint.setStyle(SkPaint::kFill_Style); |
| 379 | } |
| 380 | fStateRestore.set(fDrawTarget->drawState()); |
| 381 | |
| 382 | fDrawTarget->drawState()->setFromPaint(fPaint, fContext->getMatrix(), |
| 383 | fContext->getRenderTarget()); |
| 384 | |
| 385 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 386 | kZero_StencilOp, |
| 387 | kZero_StencilOp, |
| 388 | kNotEqual_StencilFunc, |
| 389 | 0xffff, |
| 390 | 0x0000, |
| 391 | 0xffff); |
| 392 | |
| 393 | *fDrawTarget->drawState()->stencil() = kStencilPass; |
| 394 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 395 | SkASSERT(0 == fPendingGlyphCount); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 396 | } |
| 397 | |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 398 | inline void GrStencilAndCoverTextContext::appendGlyph(uint16_t glyphID, float x) { |
| 399 | SkASSERT(GrDrawTarget::kTranslateX_PathTransformType == fTransformType); |
| 400 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 401 | if (fPendingGlyphCount >= kGlyphBufferSize) { |
| 402 | this->flush(); |
| 403 | } |
| 404 | |
| 405 | fGlyphs->preloadGlyph(glyphID, fGlyphCache); |
| 406 | |
| 407 | fIndexBuffer[fPendingGlyphCount] = glyphID; |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 408 | fTransformBuffer[fPendingGlyphCount] = fTextInverseRatio * x; |
| 409 | |
| 410 | ++fPendingGlyphCount; |
| 411 | } |
| 412 | |
| 413 | inline void GrStencilAndCoverTextContext::appendGlyph(uint16_t glyphID, float x, float y) { |
| 414 | SkASSERT(GrDrawTarget::kTranslate_PathTransformType == fTransformType); |
| 415 | |
| 416 | if (fPendingGlyphCount >= kGlyphBufferSize) { |
| 417 | this->flush(); |
| 418 | } |
| 419 | |
| 420 | fGlyphs->preloadGlyph(glyphID, fGlyphCache); |
| 421 | |
| 422 | fIndexBuffer[fPendingGlyphCount] = glyphID; |
| 423 | fTransformBuffer[2 * fPendingGlyphCount] = fTextInverseRatio * x; |
| 424 | fTransformBuffer[2 * fPendingGlyphCount + 1] = fTextInverseRatio * y; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 425 | |
| 426 | ++fPendingGlyphCount; |
| 427 | } |
| 428 | |
| 429 | void GrStencilAndCoverTextContext::flush() { |
| 430 | if (0 == fPendingGlyphCount) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 434 | fDrawTarget->drawPaths(fGlyphs->pathRange(), fIndexBuffer, fPendingGlyphCount, |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 435 | fTransformBuffer, fTransformType, SkPath::kWinding_FillType); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 436 | |
| 437 | fPendingGlyphCount = 0; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void GrStencilAndCoverTextContext::finish() { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 441 | this->flush(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 442 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 443 | SkSafeUnref(fGlyphs); |
| 444 | fGlyphs = NULL; |
| 445 | fGlyphCache = NULL; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 446 | |
| 447 | fDrawTarget->drawState()->stencil()->setDisabled(); |
| 448 | fStateRestore.set(NULL); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 449 | fContext->setMatrix(fContextInitialMatrix); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 450 | GrTextContext::finish(); |
| 451 | } |
| 452 | |