blob: 0867f2eb91f50c434226cd60b9cac4e9bdf3225a [file] [log] [blame]
kkinnunenc6cb56f2014-06-24 00:12:27 -07001/*
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"
kkinnunenc6cb56f2014-06-24 00:12:27 -070010#include "GrGpu.h"
11#include "GrPath.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070012#include "GrPathRange.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070013#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
bsalomonc44be0e2014-07-25 07:32:33 -070021class GrStencilAndCoverTextContext::GlyphPathRange : public GrGpuObject {
cdaltonb85a0aa2014-07-21 15:32:44 -070022 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
25public:
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
bsalomonc44be0e2014-07-25 07:32:33 -070078 // GrGpuObject overrides
cdaltonb85a0aa2014-07-21 15:32:44 -070079 virtual size_t gpuMemorySize() const SK_OVERRIDE { return fPathRange->gpuMemorySize(); }
cdaltonb85a0aa2014-07-21 15:32:44 -070080
81private:
82 GlyphPathRange(GrContext* context, const SkDescriptor& desc, const SkStrokeRec& stroke)
bsalomonc44be0e2014-07-25 07:32:33 -070083 : INHERITED(context->getGpu(), false)
84 , fDesc(desc.copy())
cdaltonb85a0aa2014-07-21 15:32:44 -070085 // 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() {
bsalomonc44be0e2014-07-25 07:32:33 -070093 this->release();
cdaltonb85a0aa2014-07-21 15:32:44 -070094 SkDescriptor::Free(fDesc);
95 }
96
bsalomonc44be0e2014-07-25 07:32:33 -070097 virtual void onRelease() SK_OVERRIDE {
98 INHERITED::onRelease();
99 fPathRange.reset(NULL);
100 }
101
102 virtual void onAbandon() SK_OVERRIDE {
103 INHERITED::onAbandon();
104 fPathRange->abandon();
105 fPathRange.reset(NULL);
106 }
107
108
cdaltonb85a0aa2014-07-21 15:32:44 -0700109 static const int kMaxGroupCount = (kMaxGlyphCount + (kGlyphGroupSize - 1)) / kGlyphGroupSize;
110 SkDescriptor* const fDesc;
111 uint8_t fLoadedGlyphs[(kMaxGroupCount + 7) >> 3]; // One bit per glyph group
112 SkAutoTUnref<GrPathRange> fPathRange;
113
bsalomonc44be0e2014-07-25 07:32:33 -0700114 typedef GrGpuObject INHERITED;
cdaltonb85a0aa2014-07-21 15:32:44 -0700115};
116
kkinnunenc6cb56f2014-06-24 00:12:27 -0700117
118GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(
119 GrContext* context, const SkDeviceProperties& properties)
120 : GrTextContext(context, properties)
cdaltonb85a0aa2014-07-21 15:32:44 -0700121 , fStroke(SkStrokeRec::kFill_InitStyle)
122 , fPendingGlyphCount(0) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700123}
124
125GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
126}
127
128void GrStencilAndCoverTextContext::drawText(const GrPaint& paint,
129 const SkPaint& skPaint,
130 const char text[],
131 size_t byteLength,
132 SkScalar x, SkScalar y) {
133 SkASSERT(byteLength == 0 || text != NULL);
134
135 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
136 return;
137 }
138
139 // This is the slow path, mainly used by Skia unit tests. The other
140 // backends (8888, gpu, ...) use device-space dependent glyph caches. In
141 // order to match the glyph positions that the other code paths produce, we
142 // must also use device-space dependent glyph cache. This has the
143 // side-effect that the glyph shape outline will be in device-space,
144 // too. This in turn has the side-effect that NVPR can not stroke the paths,
145 // as the stroke in NVPR is defined in object-space.
146 // NOTE: here we have following coincidence that works at the moment:
147 // - When using the device-space glyphs, the transforms we pass to NVPR
148 // instanced drawing are the global transforms, and the view transform is
149 // identity. NVPR can not use non-affine transforms in the instanced
150 // drawing. This is taken care of by SkDraw::ShouldDrawTextAsPaths since it
151 // will turn off the use of device-space glyphs when perspective transforms
152 // are in use.
153
154 fGlyphTransform = fContext->getMatrix();
155
156 this->init(paint, skPaint, byteLength);
157
158 SkMatrix* glyphCacheTransform = NULL;
159 // Transform our starting point.
160 if (fNeedsDeviceSpaceGlyphs) {
161 SkPoint loc;
162 fGlyphTransform.mapXY(x, y, &loc);
163 x = loc.fX;
164 y = loc.fY;
165 glyphCacheTransform = &fGlyphTransform;
166 }
167
168 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
169 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, glyphCacheTransform);
cdaltonb85a0aa2014-07-21 15:32:44 -0700170 fGlyphCache = autoCache.getCache();
171 fGlyphs = GlyphPathRange::Create(fContext, fGlyphCache, fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700172
173 const char* stop = text + byteLength;
174
175 // Measure first if needed.
176 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
177 SkFixed stopX = 0;
178 SkFixed stopY = 0;
179
180 const char* textPtr = text;
181 while (textPtr < stop) {
182 // We don't need x, y here, since all subpixel variants will have the
183 // same advance.
cdaltonb85a0aa2014-07-21 15:32:44 -0700184 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &textPtr, 0, 0);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700185
186 stopX += glyph.fAdvanceX;
187 stopY += glyph.fAdvanceY;
188 }
189 SkASSERT(textPtr == stop);
190
191 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
192 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
193
194 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
195 alignX = SkScalarHalf(alignX);
196 alignY = SkScalarHalf(alignY);
197 }
198
199 x -= alignX;
200 y -= alignY;
201 }
202
203 SkAutoKern autokern;
204
205 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
206
207 SkFixed fx = SkScalarToFixed(x);
208 SkFixed fy = SkScalarToFixed(y);
209 while (text < stop) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700210 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700211 fx += SkFixedMul_portable(autokern.adjust(glyph), fixedSizeRatio);
212 if (glyph.fWidth) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700213 this->appendGlyph(glyph.getGlyphID(), SkFixedToScalar(fx), SkFixedToScalar(fy));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700214 }
215
216 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedSizeRatio);
217 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedSizeRatio);
218 }
219
220 this->finish();
221}
222
223void GrStencilAndCoverTextContext::drawPosText(const GrPaint& paint,
224 const SkPaint& skPaint,
225 const char text[],
226 size_t byteLength,
227 const SkScalar pos[],
228 SkScalar constY,
229 int scalarsPerPosition) {
230 SkASSERT(byteLength == 0 || text != NULL);
231 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
232
233 // nothing to draw
234 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
235 return;
236 }
237
238 // This is the fast path. Here we do not bake in the device-transform to
239 // the glyph outline or the advances. This is because we do not need to
240 // position the glyphs at all, since the caller has done the positioning.
241 // The positioning is based on SkPaint::measureText of individual
242 // glyphs. That already uses glyph cache without device transforms. Device
243 // transform is not part of SkPaint::measureText API, and thus we use the
244 // same glyphs as what were measured.
245 fGlyphTransform.reset();
246
247 this->init(paint, skPaint, byteLength);
248
249 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
250
251 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL);
cdaltonb85a0aa2014-07-21 15:32:44 -0700252 fGlyphCache = autoCache.getCache();
253 fGlyphs = GlyphPathRange::Create(fContext, fGlyphCache, fStroke);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700254
255 const char* stop = text + byteLength;
256 SkTextAlignProcScalar alignProc(fSkPaint.getTextAlign());
257 SkTextMapStateProc tmsProc(SkMatrix::I(), constY, scalarsPerPosition);
258
259 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
260 while (text < stop) {
261 SkPoint loc;
262 tmsProc(pos, &loc);
cdaltonb85a0aa2014-07-21 15:32:44 -0700263 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700264 if (glyph.fWidth) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700265 this->appendGlyph(glyph.getGlyphID(), loc.x(), loc.y());
kkinnunenc6cb56f2014-06-24 00:12:27 -0700266 }
267 pos += scalarsPerPosition;
268 }
269 } else {
270 while (text < stop) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700271 const SkGlyph& glyph = glyphCacheProc(fGlyphCache, &text, 0, 0);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700272 if (glyph.fWidth) {
273 SkPoint tmsLoc;
274 tmsProc(pos, &tmsLoc);
275 SkPoint loc;
276 alignProc(tmsLoc, glyph, &loc);
277
cdaltonb85a0aa2014-07-21 15:32:44 -0700278 this->appendGlyph(glyph.getGlyphID(), loc.x(), loc.y());
kkinnunenc6cb56f2014-06-24 00:12:27 -0700279 }
280 pos += scalarsPerPosition;
281 }
282 }
283
284 this->finish();
285}
286
287bool GrStencilAndCoverTextContext::canDraw(const SkPaint& paint) {
288 if (paint.getRasterizer()) {
289 return false;
290 }
291 if (paint.getMaskFilter()) {
292 return false;
293 }
294 if (paint.getPathEffect()) {
295 return false;
296 }
297
298 // No hairlines unless we can map the 1 px width to the object space.
299 if (paint.getStyle() == SkPaint::kStroke_Style
300 && paint.getStrokeWidth() == 0
301 && fContext->getMatrix().hasPerspective()) {
302 return false;
303 }
304
305 // No color bitmap fonts.
306 SkScalerContext::Rec rec;
307 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
308 return rec.getFormat() != SkMask::kARGB32_Format;
309}
310
311void GrStencilAndCoverTextContext::init(const GrPaint& paint,
312 const SkPaint& skPaint,
313 size_t textByteLength) {
314 GrTextContext::init(paint, skPaint);
315
316 bool otherBackendsWillDrawAsPaths =
317 SkDraw::ShouldDrawTextAsPaths(skPaint, fContext->getMatrix());
318
319 if (otherBackendsWillDrawAsPaths) {
320 // This is to reproduce SkDraw::drawText_asPaths glyph positions.
321 fSkPaint.setLinearText(true);
322 fTextRatio = fSkPaint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
323 fSkPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
324 if (fSkPaint.getStyle() != SkPaint::kFill_Style) {
325 // Compensate the glyphs being scaled up by fTextRatio by scaling the
326 // stroke down.
327 fSkPaint.setStrokeWidth(fSkPaint.getStrokeWidth() / fTextRatio);
328 }
329 fNeedsDeviceSpaceGlyphs = false;
330 } else {
331 fTextRatio = 1.0f;
332 fNeedsDeviceSpaceGlyphs = (fGlyphTransform.getType() &
333 (SkMatrix::kScale_Mask | SkMatrix::kAffine_Mask)) != 0;
334 // SkDraw::ShouldDrawTextAsPaths takes care of perspective transforms.
335 SkASSERT(!fGlyphTransform.hasPerspective());
336 if (fNeedsDeviceSpaceGlyphs) {
337 fPaint.localCoordChangeInverse(fGlyphTransform);
338 fContext->setIdentityMatrix();
339 }
340 }
341
342 fStroke = SkStrokeRec(fSkPaint);
343
344 if (fNeedsDeviceSpaceGlyphs) {
345 // The whole shape is baked into the glyph. Make NVPR just fill the
346 // baked shape.
347 fStroke.setStrokeStyle(-1, false);
348 } else {
349 if (fSkPaint.getStrokeWidth() == 0.0f) {
350 if (fSkPaint.getStyle() == SkPaint::kStrokeAndFill_Style) {
351 fStroke.setStrokeStyle(-1, false);
352 } else if (fSkPaint.getStyle() == SkPaint::kStroke_Style) {
353 // Approximate hairline stroke.
354 const SkMatrix& ctm = fContext->getMatrix();
355 SkScalar strokeWidth = SK_Scalar1 /
356 (fTextRatio * SkVector::Make(ctm.getScaleX(), ctm.getSkewY()).length());
357 fStroke.setStrokeStyle(strokeWidth, false);
358 }
359 }
360
361 // Make glyph cache produce paths geometry for fill. We will stroke them
362 // by passing fStroke to drawPath. This is the fast path.
363 fSkPaint.setStyle(SkPaint::kFill_Style);
364 }
365 fStateRestore.set(fDrawTarget->drawState());
366
367 fDrawTarget->drawState()->setFromPaint(fPaint, fContext->getMatrix(),
368 fContext->getRenderTarget());
369
370 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
371 kZero_StencilOp,
372 kZero_StencilOp,
373 kNotEqual_StencilFunc,
374 0xffff,
375 0x0000,
376 0xffff);
377
378 *fDrawTarget->drawState()->stencil() = kStencilPass;
379
cdaltonb85a0aa2014-07-21 15:32:44 -0700380 SkASSERT(0 == fPendingGlyphCount);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700381}
382
cdaltonb85a0aa2014-07-21 15:32:44 -0700383inline void GrStencilAndCoverTextContext::appendGlyph(uint16_t glyphID, float x, float y) {
384 if (fPendingGlyphCount >= kGlyphBufferSize) {
385 this->flush();
386 }
387
388 fGlyphs->preloadGlyph(glyphID, fGlyphCache);
389
390 fIndexBuffer[fPendingGlyphCount] = glyphID;
391 fTransformBuffer[6 * fPendingGlyphCount + 0] = fTextRatio;
392 fTransformBuffer[6 * fPendingGlyphCount + 1] = 0;
393 fTransformBuffer[6 * fPendingGlyphCount + 2] = x;
394 fTransformBuffer[6 * fPendingGlyphCount + 3] = 0;
395 fTransformBuffer[6 * fPendingGlyphCount + 4] = fTextRatio;
396 fTransformBuffer[6 * fPendingGlyphCount + 5] = y;
397
398 ++fPendingGlyphCount;
399}
400
401void GrStencilAndCoverTextContext::flush() {
402 if (0 == fPendingGlyphCount) {
kkinnunenc6cb56f2014-06-24 00:12:27 -0700403 return;
404 }
405
cdaltonb85a0aa2014-07-21 15:32:44 -0700406 fDrawTarget->drawPaths(fGlyphs->pathRange(), fIndexBuffer, fPendingGlyphCount,
407 fTransformBuffer, GrDrawTarget::kAffine_PathTransformType,
408 SkPath::kWinding_FillType);
409
410 fPendingGlyphCount = 0;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700411}
412
413void GrStencilAndCoverTextContext::finish() {
cdaltonb85a0aa2014-07-21 15:32:44 -0700414 this->flush();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700415
cdaltonb85a0aa2014-07-21 15:32:44 -0700416 SkSafeUnref(fGlyphs);
417 fGlyphs = NULL;
418 fGlyphCache = NULL;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700419
420 fDrawTarget->drawState()->stencil()->setDisabled();
421 fStateRestore.set(NULL);
422 if (fNeedsDeviceSpaceGlyphs) {
423 fContext->setMatrix(fGlyphTransform);
424 }
425 GrTextContext::finish();
426}
427