blob: 2fbaec8f326b196c26567bea94a7dafc31790891 [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"
joshualitt1d89e8d2015-04-01 12:40:54 -07009#include "GrAtlasTextContext.h"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070011#include "GrDrawTarget.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070012#include "GrPath.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070013#include "GrPathRange.h"
bsalomond309e7a2015-04-30 14:18:54 -070014#include "GrResourceProvider.h"
joshualitte55750e2016-02-10 12:52:21 -080015#include "GrTextUtils.h"
jvanverthaab626c2014-10-16 08:04:39 -070016#include "SkAutoKern.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070017#include "SkDraw.h"
18#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
20#include "SkGpuDevice.h"
cdaltoncdd79072015-10-05 15:37:35 -070021#include "SkGrPriv.h"
joshualitte55750e2016-02-10 12:52:21 -080022#include "SkDrawFilter.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070023#include "SkPath.h"
halcanary33779752015-10-27 14:01:05 -070024#include "SkTextBlobRunIterator.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070025#include "SkTextMapStateProc.h"
cdalton855d83f2014-09-18 13:51:53 -070026#include "SkTextFormatParams.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070027
bsalomon1fcc01c2015-09-09 09:48:06 -070028#include "batches/GrDrawPathBatch.h"
29
cdaltoncdd79072015-10-05 15:37:35 -070030template<typename Key, typename Val> static void delete_hash_map_entry(const Key&, Val* val) {
31 SkASSERT(*val);
32 delete *val;
33}
34
35template<typename T> static void delete_hash_table_entry(T* val) {
36 SkASSERT(*val);
37 delete *val;
38}
39
joshualitt6e8cd962015-03-20 10:30:14 -070040GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -070041 const SkSurfaceProps& surfaceProps)
joshualitte55750e2016-02-10 12:52:21 -080042 : INHERITED(context, surfaceProps)
43 , fFallbackTextContext(nullptr)
44 , fCacheSize(0) {
kkinnunenc6cb56f2014-06-24 00:12:27 -070045}
46
joshualitt6e8cd962015-03-20 10:30:14 -070047GrStencilAndCoverTextContext*
robertphillipsf6703fa2015-09-01 05:36:47 -070048GrStencilAndCoverTextContext::Create(GrContext* context, const SkSurfaceProps& surfaceProps) {
49 GrStencilAndCoverTextContext* textContext =
50 new GrStencilAndCoverTextContext(context, surfaceProps);
51 textContext->fFallbackTextContext = GrAtlasTextContext::Create(context, surfaceProps);
jvanverth8c27a182014-10-14 08:45:50 -070052
53 return textContext;
54}
55
kkinnunenc6cb56f2014-06-24 00:12:27 -070056GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
joshualitte55750e2016-02-10 12:52:21 -080057 delete fFallbackTextContext;
cdaltoncdd79072015-10-05 15:37:35 -070058 fBlobIdCache.foreach(delete_hash_map_entry<uint32_t, TextBlob*>);
59 fBlobKeyCache.foreach(delete_hash_table_entry<TextBlob*>);
kkinnunenc6cb56f2014-06-24 00:12:27 -070060}
61
cdaltoncdd79072015-10-05 15:37:35 -070062bool GrStencilAndCoverTextContext::internalCanDraw(const SkPaint& skPaint) {
cdaltone68f7362015-03-25 14:02:37 -070063 if (skPaint.getRasterizer()) {
jvanverth0fedb192014-10-08 09:07:27 -070064 return false;
65 }
cdaltone68f7362015-03-25 14:02:37 -070066 if (skPaint.getMaskFilter()) {
jvanverth0fedb192014-10-08 09:07:27 -070067 return false;
68 }
kkinnunen50b58e62015-05-18 23:02:07 -070069 if (SkPathEffect* pe = skPaint.getPathEffect()) {
halcanary96fcdcc2015-08-27 07:41:13 -070070 if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) {
kkinnunen50b58e62015-05-18 23:02:07 -070071 return false;
72 }
jvanverth0fedb192014-10-08 09:07:27 -070073 }
cdalton7d5c9502015-10-03 13:28:35 -070074 // No hairlines. They would require new paths with customized strokes for every new draw matrix.
75 return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth();
jvanverth0fedb192014-10-08 09:07:27 -070076}
77
joshualitte55750e2016-02-10 12:52:21 -080078void GrStencilAndCoverTextContext::drawText(GrDrawContext* dc,
79 const GrClip& clip, const GrPaint& paint,
80 const SkPaint& skPaint, const SkMatrix& viewMatrix,
81 const char text[], size_t byteLength,
82 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
83 if (fContext->abandoned()) {
84 return;
85 } else if (this->canDraw(skPaint, viewMatrix)) {
86 TextRun run(skPaint);
87 GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), clip);
88 run.setText(text, byteLength, x, y);
89 run.draw(fContext, dc, &pipelineBuilder, paint.getColor(), viewMatrix, 0, 0, clipBounds,
90 fFallbackTextContext, skPaint);
91 return;
92 } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix)) {
93 fFallbackTextContext->drawText(dc, clip, paint, skPaint, viewMatrix, text,
94 byteLength, x, y, clipBounds);
95 return;
96 }
97
98 // fall back to drawing as a path
99 GrTextUtils::DrawTextAsPath(fContext, dc, clip, skPaint, viewMatrix, text, byteLength, x, y,
100 clipBounds);
cdalton3bd909a2015-10-05 14:57:20 -0700101}
jvanverthaab626c2014-10-16 08:04:39 -0700102
joshualitte55750e2016-02-10 12:52:21 -0800103void GrStencilAndCoverTextContext::drawPosText(GrDrawContext* dc,
104 const GrClip& clip,
105 const GrPaint& paint,
106 const SkPaint& skPaint,
107 const SkMatrix& viewMatrix,
108 const char text[],
109 size_t byteLength,
110 const SkScalar pos[],
111 int scalarsPerPosition,
112 const SkPoint& offset,
113 const SkIRect& clipBounds) {
114 if (fContext->abandoned()) {
115 return;
116 } else if (this->canDraw(skPaint, viewMatrix)) {
117 TextRun run(skPaint);
118 GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), clip);
119 run.setPosText(text, byteLength, pos, scalarsPerPosition, offset);
120 run.draw(fContext, dc, &pipelineBuilder, paint.getColor(), viewMatrix, 0, 0, clipBounds,
121 fFallbackTextContext, skPaint);
122 return;
123 } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix)) {
124 fFallbackTextContext->drawPosText(dc, clip, paint, skPaint, viewMatrix,
125 text, byteLength, pos,
126 scalarsPerPosition, offset, clipBounds);
127 return;
128 }
129
130 // fall back to drawing as a path
131 GrTextUtils::DrawPosTextAsPath(fContext, dc, fSurfaceProps, clip, skPaint, viewMatrix, text,
132 byteLength, pos, scalarsPerPosition, offset, clipBounds);
133}
134
135void GrStencilAndCoverTextContext::uncachedDrawTextBlob(GrDrawContext* dc,
136 const GrClip& clip, const SkPaint& skPaint,
137 const SkMatrix& viewMatrix,
138 const SkTextBlob* blob,
139 SkScalar x, SkScalar y,
140 SkDrawFilter* drawFilter,
141 const SkIRect& clipBounds) {
142 SkPaint runPaint = skPaint;
143
144 SkTextBlobRunIterator it(blob);
145 for (;!it.done(); it.next()) {
146 size_t textLen = it.glyphCount() * sizeof(uint16_t);
147 const SkPoint& offset = it.offset();
148
149 // applyFontToPaint() always overwrites the exact same attributes,
150 // so it is safe to not re-seed the paint for this reason.
151 it.applyFontToPaint(&runPaint);
152
153 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
154 // A false return from filter() means we should abort the current draw.
155 runPaint = skPaint;
156 continue;
157 }
158
159 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
160
161 GrPaint grPaint;
162 if (!SkPaintToGrPaint(fContext, runPaint, viewMatrix, &grPaint)) {
163 return;
164 }
165
166 switch (it.positioning()) {
167 case SkTextBlob::kDefault_Positioning:
168 this->drawText(dc, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
169 textLen, x + offset.x(), y + offset.y(), clipBounds);
170 break;
171 case SkTextBlob::kHorizontal_Positioning:
172 this->drawPosText(dc, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
173 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
174 clipBounds);
175 break;
176 case SkTextBlob::kFull_Positioning:
177 this->drawPosText(dc, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
178 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
179 break;
180 }
181
182 if (drawFilter) {
183 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
184 runPaint = skPaint;
185 }
186 }
cdaltoncdd79072015-10-05 15:37:35 -0700187}
188
robertphillips433625e2015-12-04 06:58:16 -0800189void GrStencilAndCoverTextContext::drawTextBlob(GrDrawContext* dc,
cdaltoncdd79072015-10-05 15:37:35 -0700190 const GrClip& clip, const SkPaint& skPaint,
191 const SkMatrix& viewMatrix,
192 const SkTextBlob* skBlob, SkScalar x, SkScalar y,
193 SkDrawFilter* drawFilter,
194 const SkIRect& clipBounds) {
joshualitte55750e2016-02-10 12:52:21 -0800195 if (fContext->abandoned()) {
196 return;
197 }
198
cdaltoncdd79072015-10-05 15:37:35 -0700199 if (!this->internalCanDraw(skPaint)) {
robertphillips433625e2015-12-04 06:58:16 -0800200 fFallbackTextContext->drawTextBlob(dc, clip, skPaint, viewMatrix, skBlob, x, y,
cdaltoncdd79072015-10-05 15:37:35 -0700201 drawFilter, clipBounds);
202 return;
203 }
204
205 if (drawFilter || skPaint.getPathEffect()) {
206 // This draw can't be cached.
joshualitte55750e2016-02-10 12:52:21 -0800207 this->uncachedDrawTextBlob(dc, clip, skPaint, viewMatrix, skBlob, x, y, drawFilter,
208 clipBounds);
cdaltoncdd79072015-10-05 15:37:35 -0700209 return;
210 }
211
212 GrPaint paint;
213 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &paint)) {
214 return;
215 }
216
217 const TextBlob& blob = this->findOrCreateTextBlob(skBlob, skPaint);
robertphillips433625e2015-12-04 06:58:16 -0800218 GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), clip);
cdaltoncdd79072015-10-05 15:37:35 -0700219
220 TextBlob::Iter iter(blob);
221 for (TextRun* run = iter.get(); run; run = iter.next()) {
cdalton8585dd22015-10-08 08:04:09 -0700222 run->draw(fContext, dc, &pipelineBuilder, paint.getColor(), viewMatrix, x, y, clipBounds,
cdaltoncdd79072015-10-05 15:37:35 -0700223 fFallbackTextContext, skPaint);
cdalton8585dd22015-10-08 08:04:09 -0700224 run->releaseGlyphCache();
cdaltoncdd79072015-10-05 15:37:35 -0700225 }
226}
227
228const GrStencilAndCoverTextContext::TextBlob&
229GrStencilAndCoverTextContext::findOrCreateTextBlob(const SkTextBlob* skBlob,
230 const SkPaint& skPaint) {
231 // The font-related parameters are baked into the text blob and will override this skPaint, so
232 // the only remaining properties that can affect a TextBlob are the ones related to stroke.
233 if (SkPaint::kFill_Style == skPaint.getStyle()) { // Fast path.
234 if (TextBlob** found = fBlobIdCache.find(skBlob->uniqueID())) {
235 fLRUList.remove(*found);
236 fLRUList.addToTail(*found);
237 return **found;
238 }
cdalton8585dd22015-10-08 08:04:09 -0700239 TextBlob* blob = new TextBlob(skBlob->uniqueID(), skBlob, skPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700240 this->purgeToFit(*blob);
241 fBlobIdCache.set(skBlob->uniqueID(), blob);
242 fLRUList.addToTail(blob);
243 fCacheSize += blob->cpuMemorySize();
244 return *blob;
245 } else {
246 GrStrokeInfo stroke(skPaint);
247 SkSTArray<4, uint32_t, true> key;
248 key.reset(1 + stroke.computeUniqueKeyFragmentData32Cnt());
249 key[0] = skBlob->uniqueID();
250 stroke.asUniqueKeyFragment(&key[1]);
251 if (TextBlob** found = fBlobKeyCache.find(key)) {
252 fLRUList.remove(*found);
253 fLRUList.addToTail(*found);
254 return **found;
255 }
cdalton8585dd22015-10-08 08:04:09 -0700256 TextBlob* blob = new TextBlob(key, skBlob, skPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700257 this->purgeToFit(*blob);
258 fBlobKeyCache.set(blob);
259 fLRUList.addToTail(blob);
260 fCacheSize += blob->cpuMemorySize();
261 return *blob;
262 }
263}
264
265void GrStencilAndCoverTextContext::purgeToFit(const TextBlob& blob) {
cdalton8585dd22015-10-08 08:04:09 -0700266 static const size_t maxCacheSize = 4 * 1024 * 1024; // Allow up to 4 MB for caching text blobs.
cdaltoncdd79072015-10-05 15:37:35 -0700267
cdalton8585dd22015-10-08 08:04:09 -0700268 size_t maxSizeForNewBlob = maxCacheSize - blob.cpuMemorySize();
cdaltoncdd79072015-10-05 15:37:35 -0700269 while (fCacheSize && fCacheSize > maxSizeForNewBlob) {
270 TextBlob* lru = fLRUList.head();
271 if (1 == lru->key().count()) {
272 // 1-length keys are unterstood to be the blob id.
273 fBlobIdCache.remove(lru->key()[0]);
274 } else {
275 fBlobKeyCache.remove(lru->key());
276 }
277 fLRUList.remove(lru);
278 fCacheSize -= lru->cpuMemorySize();
279 delete lru;
280 }
281}
282
283////////////////////////////////////////////////////////////////////////////////////////////////////
284
cdalton8585dd22015-10-08 08:04:09 -0700285void GrStencilAndCoverTextContext::TextBlob::init(const SkTextBlob* skBlob,
286 const SkPaint& skPaint) {
cdaltoncdd79072015-10-05 15:37:35 -0700287 fCpuMemorySize = sizeof(TextBlob);
288 SkPaint runPaint(skPaint);
halcanary33779752015-10-27 14:01:05 -0700289 for (SkTextBlobRunIterator iter(skBlob); !iter.done(); iter.next()) {
cdaltoncdd79072015-10-05 15:37:35 -0700290 iter.applyFontToPaint(&runPaint); // No need to re-seed the paint.
bsalomon5aaef1f2015-11-18 14:11:08 -0800291 TextRun* run = this->addToTail(runPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700292
293 const char* text = reinterpret_cast<const char*>(iter.glyphs());
294 size_t byteLength = sizeof(uint16_t) * iter.glyphCount();
295 const SkPoint& runOffset = iter.offset();
296
297 switch (iter.positioning()) {
298 case SkTextBlob::kDefault_Positioning:
cdalton8585dd22015-10-08 08:04:09 -0700299 run->setText(text, byteLength, runOffset.fX, runOffset.fY);
cdaltoncdd79072015-10-05 15:37:35 -0700300 break;
301 case SkTextBlob::kHorizontal_Positioning:
cdalton8585dd22015-10-08 08:04:09 -0700302 run->setPosText(text, byteLength, iter.pos(), 1, SkPoint::Make(0, runOffset.fY));
cdaltoncdd79072015-10-05 15:37:35 -0700303 break;
304 case SkTextBlob::kFull_Positioning:
cdalton8585dd22015-10-08 08:04:09 -0700305 run->setPosText(text, byteLength, iter.pos(), 2, SkPoint::Make(0, 0));
cdaltoncdd79072015-10-05 15:37:35 -0700306 break;
307 }
308
cdalton8585dd22015-10-08 08:04:09 -0700309 fCpuMemorySize += run->computeSizeInCache();
cdaltoncdd79072015-10-05 15:37:35 -0700310 }
cdalton3bd909a2015-10-05 14:57:20 -0700311}
312
313////////////////////////////////////////////////////////////////////////////////////////////////////
314
cdalton02015e52015-10-05 15:28:20 -0700315class GrStencilAndCoverTextContext::FallbackBlobBuilder {
316public:
cdaltoncdd46822015-12-08 10:48:31 -0800317 FallbackBlobBuilder() : fBuffIdx(0), fCount(0) {}
cdalton02015e52015-10-05 15:28:20 -0700318
319 bool isInitialized() const { return SkToBool(fBuilder); }
320
321 void init(const SkPaint& font, SkScalar textRatio);
322
323 void appendGlyph(uint16_t glyphId, const SkPoint& pos);
324
cdaltoncdd46822015-12-08 10:48:31 -0800325 const SkTextBlob* buildIfNeeded(int* count);
cdalton02015e52015-10-05 15:28:20 -0700326
327private:
328 enum { kWriteBufferSize = 1024 };
329
330 void flush();
331
332 SkAutoTDelete<SkTextBlobBuilder> fBuilder;
333 SkPaint fFont;
334 int fBuffIdx;
cdaltoncdd46822015-12-08 10:48:31 -0800335 int fCount;
cdalton02015e52015-10-05 15:28:20 -0700336 uint16_t fGlyphIds[kWriteBufferSize];
337 SkPoint fPositions[kWriteBufferSize];
338};
339
340////////////////////////////////////////////////////////////////////////////////////////////////////
341
cdalton3bd909a2015-10-05 14:57:20 -0700342GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke)
343 : fStroke(fontAndStroke),
cdaltoncdd79072015-10-05 15:37:35 -0700344 fFont(fontAndStroke),
cdalton8585dd22015-10-08 08:04:09 -0700345 fTotalGlyphCount(0),
cdaltoncdd46822015-12-08 10:48:31 -0800346 fFallbackGlyphCount(0),
cdalton8585dd22015-10-08 08:04:09 -0700347 fDetachedGlyphCache(nullptr),
348 fLastDrawnGlyphsID(SK_InvalidUniqueID) {
cdalton3bd909a2015-10-05 14:57:20 -0700349 SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported.
350
351 // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path
352 // rendering API for stroking).
353 fFont.setStyle(SkPaint::kFill_Style);
354
355 if (fFont.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
356 // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke.
357 SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(),
358 kStdFakeBoldInterpKeys,
359 kStdFakeBoldInterpValues,
360 kStdFakeBoldInterpLength);
361 SkScalar extra = SkScalarMul(fFont.getTextSize(), fakeBoldScale);
362 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
363 true /*strokeAndFill*/);
364
365 fFont.setFakeBoldText(false);
jvanverthaab626c2014-10-16 08:04:39 -0700366 }
367
cdalton3bd909a2015-10-05 14:57:20 -0700368 if (!fFont.getPathEffect() && !fStroke.isDashed()) {
369 // We can draw the glyphs from canonically sized paths.
370 fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
371 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize();
jvanverthaab626c2014-10-16 08:04:39 -0700372
cdalton3bd909a2015-10-05 14:57:20 -0700373 // Compensate for the glyphs being scaled by fTextRatio.
374 if (!fStroke.isFillStyle()) {
375 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
376 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
377 }
378
379 fFont.setLinearText(true);
380 fFont.setLCDRenderText(false);
381 fFont.setAutohinted(false);
382 fFont.setHinting(SkPaint::kNo_Hinting);
383 fFont.setSubpixelText(true);
384 fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
385
386 fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() &&
387 0 == fFont.getTextSkewX() &&
388 !fFont.isFakeBoldText() &&
389 !fFont.isVerticalText();
390 } else {
391 fTextRatio = fTextInverseRatio = 1.0f;
392 fUsingRawGlyphPaths = false;
393 }
394
cdalton8585dd22015-10-08 08:04:09 -0700395 // Generate the key that will be used to cache the GPU glyph path objects.
396 if (fUsingRawGlyphPaths && fStroke.isFillStyle()) {
397 static const GrUniqueKey::Domain kRawFillPathGlyphDomain = GrUniqueKey::GenerateDomain();
398
399 const SkTypeface* typeface = fFont.getTypeface();
400 GrUniqueKey::Builder builder(&fGlyphPathsKey, kRawFillPathGlyphDomain, 1);
401 reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0;
402 } else {
403 static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain();
404
405 int strokeDataCount = fStroke.computeUniqueKeyFragmentData32Cnt();
406 if (fUsingRawGlyphPaths) {
407 const SkTypeface* typeface = fFont.getTypeface();
408 GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, 2 + strokeDataCount);
409 reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0;
410 reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount;
411 fStroke.asUniqueKeyFragment(&builder[2]);
412 } else {
413 SkGlyphCache* glyphCache = this->getGlyphCache();
414 const SkTypeface* typeface = glyphCache->getScalerContext()->getTypeface();
415 const SkDescriptor* desc = &glyphCache->getDescriptor();
416 int descDataCount = (desc->getLength() + 3) / 4;
417 GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain,
418 2 + strokeDataCount + descDataCount);
419 reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0;
420 reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount | (descDataCount << 16);
421 fStroke.asUniqueKeyFragment(&builder[2]);
422 memcpy(&builder[2 + strokeDataCount], desc, desc->getLength());
423 }
424 }
cdalton3bd909a2015-10-05 14:57:20 -0700425}
426
427GrStencilAndCoverTextContext::TextRun::~TextRun() {
cdalton8585dd22015-10-08 08:04:09 -0700428 this->releaseGlyphCache();
cdalton3bd909a2015-10-05 14:57:20 -0700429}
430
431void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t byteLength,
cdalton8585dd22015-10-08 08:04:09 -0700432 SkScalar x, SkScalar y) {
cdalton3bd909a2015-10-05 14:57:20 -0700433 SkASSERT(byteLength == 0 || text != nullptr);
434
cdalton8585dd22015-10-08 08:04:09 -0700435 SkGlyphCache* glyphCache = this->getGlyphCache();
cdalton3bd909a2015-10-05 14:57:20 -0700436 SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc();
jvanverthaab626c2014-10-16 08:04:39 -0700437
cdaltoncdd46822015-12-08 10:48:31 -0800438 fTotalGlyphCount = fFont.countText(text, byteLength);
439 fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType,
440 fTotalGlyphCount));
cdalton8585dd22015-10-08 08:04:09 -0700441
jvanverthaab626c2014-10-16 08:04:39 -0700442 const char* stop = text + byteLength;
443
444 // Measure first if needed.
cdalton3bd909a2015-10-05 14:57:20 -0700445 if (fFont.getTextAlign() != SkPaint::kLeft_Align) {
jvanverthaab626c2014-10-16 08:04:39 -0700446 SkFixed stopX = 0;
447 SkFixed stopY = 0;
448
449 const char* textPtr = text;
450 while (textPtr < stop) {
451 // We don't need x, y here, since all subpixel variants will have the
452 // same advance.
cdalton3bd909a2015-10-05 14:57:20 -0700453 const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700454
455 stopX += glyph.fAdvanceX;
456 stopY += glyph.fAdvanceY;
457 }
458 SkASSERT(textPtr == stop);
459
460 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
461 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
462
cdalton3bd909a2015-10-05 14:57:20 -0700463 if (fFont.getTextAlign() == SkPaint::kCenter_Align) {
jvanverthaab626c2014-10-16 08:04:39 -0700464 alignX = SkScalarHalf(alignX);
465 alignY = SkScalarHalf(alignY);
466 }
467
468 x -= alignX;
469 y -= alignY;
470 }
471
472 SkAutoKern autokern;
473
474 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
475
476 SkFixed fx = SkScalarToFixed(x);
477 SkFixed fy = SkScalarToFixed(y);
cdalton02015e52015-10-05 15:28:20 -0700478 FallbackBlobBuilder fallback;
jvanverthaab626c2014-10-16 08:04:39 -0700479 while (text < stop) {
cdalton3bd909a2015-10-05 14:57:20 -0700480 const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0);
bungemand709ea82015-03-17 07:23:39 -0700481 fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700482 if (glyph.fWidth) {
cdalton02015e52015-10-05 15:28:20 -0700483 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)),
484 &fallback);
jvanverthaab626c2014-10-16 08:04:39 -0700485 }
486
bungemand709ea82015-03-17 07:23:39 -0700487 fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio);
488 fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700489 }
cdalton02015e52015-10-05 15:28:20 -0700490
cdaltoncdd46822015-12-08 10:48:31 -0800491 fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount));
jvanverthaab626c2014-10-16 08:04:39 -0700492}
493
cdalton3bd909a2015-10-05 14:57:20 -0700494void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength,
495 const SkScalar pos[], int scalarsPerPosition,
cdalton8585dd22015-10-08 08:04:09 -0700496 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700497 SkASSERT(byteLength == 0 || text != nullptr);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700498 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
499
cdalton8585dd22015-10-08 08:04:09 -0700500 SkGlyphCache* glyphCache = this->getGlyphCache();
cdalton3bd909a2015-10-05 14:57:20 -0700501 SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700502
cdaltoncdd46822015-12-08 10:48:31 -0800503 fTotalGlyphCount = fFont.countText(text, byteLength);
504 fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType,
505 fTotalGlyphCount));
cdalton8585dd22015-10-08 08:04:09 -0700506
kkinnunenc6cb56f2014-06-24 00:12:27 -0700507 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700508
cdalton38e13ad2014-11-07 06:02:15 -0800509 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
cdalton3bd909a2015-10-05 14:57:20 -0700510 SkTextAlignProc alignProc(fFont.getTextAlign());
cdalton02015e52015-10-05 15:28:20 -0700511 FallbackBlobBuilder fallback;
cdalton38e13ad2014-11-07 06:02:15 -0800512 while (text < stop) {
cdalton3bd909a2015-10-05 14:57:20 -0700513 const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0);
cdalton38e13ad2014-11-07 06:02:15 -0800514 if (glyph.fWidth) {
515 SkPoint tmsLoc;
516 tmsProc(pos, &tmsLoc);
517 SkPoint loc;
518 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700519
cdalton02015e52015-10-05 15:28:20 -0700520 this->appendGlyph(glyph, loc, &fallback);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700521 }
cdalton38e13ad2014-11-07 06:02:15 -0800522 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700523 }
cdalton02015e52015-10-05 15:28:20 -0700524
cdaltoncdd46822015-12-08 10:48:31 -0800525 fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700526}
527
cdalton8585dd22015-10-08 08:04:09 -0700528GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx) const {
529 GrPathRange* glyphs = static_cast<GrPathRange*>(
530 ctx->resourceProvider()->findAndRefResourceByUniqueKey(fGlyphPathsKey));
halcanary96fcdcc2015-08-27 07:41:13 -0700531 if (nullptr == glyphs) {
cdalton8585dd22015-10-08 08:04:09 -0700532 if (fUsingRawGlyphPaths) {
533 glyphs = ctx->resourceProvider()->createGlyphs(fFont.getTypeface(), nullptr, fStroke);
534 } else {
535 SkGlyphCache* cache = this->getGlyphCache();
536 glyphs = ctx->resourceProvider()->createGlyphs(cache->getScalerContext()->getTypeface(),
537 &cache->getDescriptor(),
538 fStroke);
539 }
540 ctx->resourceProvider()->assignUniqueKeyToResource(fGlyphPathsKey, glyphs);
cdalton855d83f2014-09-18 13:51:53 -0700541 }
cdalton8585dd22015-10-08 08:04:09 -0700542 return glyphs;
cdalton855d83f2014-09-18 13:51:53 -0700543}
544
cdalton3bd909a2015-10-05 14:57:20 -0700545inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph,
cdalton02015e52015-10-05 15:28:20 -0700546 const SkPoint& pos,
547 FallbackBlobBuilder* fallback) {
548 // Stick the glyphs we can't draw into the fallback text blob.
bsalomon1fcc01c2015-09-09 09:48:06 -0700549 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
cdalton02015e52015-10-05 15:28:20 -0700550 if (!fallback->isInitialized()) {
551 fallback->init(fFont, fTextRatio);
552 }
553 fallback->appendGlyph(glyph.getGlyphID(), pos);
bsalomon1fcc01c2015-09-09 09:48:06 -0700554 } else {
cdaltoncdd46822015-12-08 10:48:31 -0800555 fInstanceData->append(glyph.getGlyphID(), fTextInverseRatio * pos.x(),
556 fTextInverseRatio * pos.y());
cdaltonb2808cd2014-07-25 14:13:57 -0700557 }
cdalton20b373c2014-12-01 08:38:55 -0800558}
559
cdalton8585dd22015-10-08 08:04:09 -0700560void GrStencilAndCoverTextContext::TextRun::draw(GrContext* ctx,
561 GrDrawContext* dc,
cdaltoncdd79072015-10-05 15:37:35 -0700562 GrPipelineBuilder* pipelineBuilder,
563 GrColor color,
cdalton3bd909a2015-10-05 14:57:20 -0700564 const SkMatrix& viewMatrix,
cdaltoncdd79072015-10-05 15:37:35 -0700565 SkScalar x, SkScalar y,
566 const SkIRect& clipBounds,
cdalton3bd909a2015-10-05 14:57:20 -0700567 GrTextContext* fallbackTextContext,
568 const SkPaint& originalSkPaint) const {
cdaltoncdd46822015-12-08 10:48:31 -0800569 SkASSERT(fInstanceData);
robertphillips433625e2015-12-04 06:58:16 -0800570 SkASSERT(dc->accessRenderTarget()->isStencilBufferMultisampled() || !fFont.isAntiAlias());
robertphillipsea461502015-05-26 11:38:03 -0700571
cdaltoncdd46822015-12-08 10:48:31 -0800572 if (fInstanceData->count()) {
cdaltoncdd79072015-10-05 15:37:35 -0700573 pipelineBuilder->setState(GrPipelineBuilder::kHWAntialias_Flag, fFont.isAntiAlias());
joshualitt7b670db2015-07-09 13:25:02 -0700574
575 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
576 kZero_StencilOp,
vbuzinovc5d58f02015-08-21 05:24:24 -0700577 kKeep_StencilOp,
joshualitt7b670db2015-07-09 13:25:02 -0700578 kNotEqual_StencilFunc,
579 0xffff,
580 0x0000,
581 0xffff);
582
cdaltoncdd79072015-10-05 15:37:35 -0700583 *pipelineBuilder->stencil() = kStencilPass;
joshualitt7b670db2015-07-09 13:25:02 -0700584
cdalton8585dd22015-10-08 08:04:09 -0700585 SkAutoTUnref<GrPathRange> glyphs(this->createGlyphs(ctx));
586 if (fLastDrawnGlyphsID != glyphs->getUniqueID()) {
587 // Either this is the first draw or the glyphs object was purged since last draw.
cdaltoncdd46822015-12-08 10:48:31 -0800588 glyphs->loadPathsIfNeeded(fInstanceData->indices(), fInstanceData->count());
cdalton8585dd22015-10-08 08:04:09 -0700589 fLastDrawnGlyphsID = glyphs->getUniqueID();
590 }
591
bsalomonbf074552015-11-23 14:25:19 -0800592 // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy
593 // the entire dst. Realistically this is a moot point, because any context that supports
594 // NV_path_rendering will also support NV_blend_equation_advanced.
595 // For clipping we'll just skip any optimizations based on the bounds. This does, however,
596 // hurt batching.
597 SkRect bounds = SkRect::MakeIWH(pipelineBuilder->getRenderTarget()->width(),
598 pipelineBuilder->getRenderTarget()->height());
599
cdalton8ff8d242015-12-08 10:20:32 -0800600 SkAutoTUnref<GrDrawPathBatchBase> batch(
cdaltoncdd46822015-12-08 10:48:31 -0800601 GrDrawPathRangeBatch::Create(viewMatrix, fTextRatio, fTextInverseRatio * x,
602 fTextInverseRatio * y, color,
603 GrPathRendering::kWinding_FillType, glyphs, fInstanceData,
cdalton8ff8d242015-12-08 10:20:32 -0800604 bounds));
605
606 dc->drawPathBatch(*pipelineBuilder, batch);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700607 }
608
cdalton02015e52015-10-05 15:28:20 -0700609 if (fFallbackTextBlob) {
cdalton3bd909a2015-10-05 14:57:20 -0700610 SkPaint fallbackSkPaint(originalSkPaint);
cdalton7d5c9502015-10-03 13:28:35 -0700611 fStroke.applyToPaint(&fallbackSkPaint);
612 if (!fStroke.isFillStyle()) {
613 fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio);
cdalton20b373c2014-12-01 08:38:55 -0800614 }
cdalton20b373c2014-12-01 08:38:55 -0800615
robertphillips433625e2015-12-04 06:58:16 -0800616 fallbackTextContext->drawTextBlob(dc, pipelineBuilder->clip(), fallbackSkPaint, viewMatrix,
cdaltoncdd79072015-10-05 15:37:35 -0700617 fFallbackTextBlob, x, y, nullptr, clipBounds);
cdalton20b373c2014-12-01 08:38:55 -0800618 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700619}
cdalton02015e52015-10-05 15:28:20 -0700620
cdalton8585dd22015-10-08 08:04:09 -0700621SkGlyphCache* GrStencilAndCoverTextContext::TextRun::getGlyphCache() const {
622 if (!fDetachedGlyphCache) {
623 fDetachedGlyphCache = fFont.detachCache(nullptr, nullptr, true /*ignoreGamma*/);
624 }
625 return fDetachedGlyphCache;
626}
627
628
629void GrStencilAndCoverTextContext::TextRun::releaseGlyphCache() const {
630 if (fDetachedGlyphCache) {
631 SkGlyphCache::AttachCache(fDetachedGlyphCache);
632 fDetachedGlyphCache = nullptr;
633 }
634}
635
636size_t GrStencilAndCoverTextContext::TextRun::computeSizeInCache() const {
cdaltoncdd46822015-12-08 10:48:31 -0800637 size_t size = sizeof(TextRun) + fGlyphPathsKey.size();
638 // The instance data always reserves enough space for every glyph.
639 size += (fTotalGlyphCount + fFallbackGlyphCount) * (sizeof(uint16_t) + 2 * sizeof(float));
640 if (fInstanceData) {
641 size += sizeof(InstanceData);
cdaltoncdd79072015-10-05 15:37:35 -0700642 }
643 if (fFallbackTextBlob) {
644 size += sizeof(SkTextBlob);
645 }
646 return size;
647}
648
cdalton02015e52015-10-05 15:28:20 -0700649////////////////////////////////////////////////////////////////////////////////////////////////////
650
651void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font,
652 SkScalar textRatio) {
653 SkASSERT(!this->isInitialized());
654 fBuilder.reset(new SkTextBlobBuilder);
655 fFont = font;
656 fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align.
657 fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
658 // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs
659 // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved.
660 fFont.setSubpixelText(false);
661 fFont.setTextSize(fFont.getTextSize() * textRatio);
662 fBuffIdx = 0;
663}
664
665void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId,
666 const SkPoint& pos) {
667 SkASSERT(this->isInitialized());
668 if (fBuffIdx >= kWriteBufferSize) {
669 this->flush();
670 }
671 fGlyphIds[fBuffIdx] = glyphId;
672 fPositions[fBuffIdx] = pos;
673 fBuffIdx++;
cdaltoncdd46822015-12-08 10:48:31 -0800674 fCount++;
cdalton02015e52015-10-05 15:28:20 -0700675}
676
677void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() {
678 SkASSERT(this->isInitialized());
679 SkASSERT(fBuffIdx <= kWriteBufferSize);
680 if (!fBuffIdx) {
681 return;
682 }
683 // This will automatically merge with previous runs since we use the same font.
684 const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx);
685 memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t));
686 memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar));
687 fBuffIdx = 0;
688}
689
cdaltoncdd46822015-12-08 10:48:31 -0800690const SkTextBlob* GrStencilAndCoverTextContext::FallbackBlobBuilder::buildIfNeeded(int *count) {
691 *count = fCount;
692 if (fCount) {
693 this->flush();
694 return fBuilder->build();
cdalton02015e52015-10-05 15:28:20 -0700695 }
cdaltoncdd46822015-12-08 10:48:31 -0800696 return nullptr;
cdalton02015e52015-10-05 15:28:20 -0700697}