blob: 205c6d976a9e079defe601208d96f3c71de694e4 [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
joshualitt27004b72016-02-11 12:00:33 -080040GrStencilAndCoverTextContext::GrStencilAndCoverTextContext()
41 : fFallbackTextContext(nullptr)
joshualitte55750e2016-02-10 12:52:21 -080042 , fCacheSize(0) {
kkinnunenc6cb56f2014-06-24 00:12:27 -070043}
44
joshualitt6e8cd962015-03-20 10:30:14 -070045GrStencilAndCoverTextContext*
joshualitt27004b72016-02-11 12:00:33 -080046GrStencilAndCoverTextContext::Create() {
47 GrStencilAndCoverTextContext* textContext = new GrStencilAndCoverTextContext();
48 textContext->fFallbackTextContext = GrAtlasTextContext::Create();
jvanverth8c27a182014-10-14 08:45:50 -070049
50 return textContext;
51}
52
kkinnunenc6cb56f2014-06-24 00:12:27 -070053GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() {
joshualitte55750e2016-02-10 12:52:21 -080054 delete fFallbackTextContext;
cdaltoncdd79072015-10-05 15:37:35 -070055 fBlobIdCache.foreach(delete_hash_map_entry<uint32_t, TextBlob*>);
56 fBlobKeyCache.foreach(delete_hash_table_entry<TextBlob*>);
kkinnunenc6cb56f2014-06-24 00:12:27 -070057}
58
cdaltoncdd79072015-10-05 15:37:35 -070059bool GrStencilAndCoverTextContext::internalCanDraw(const SkPaint& skPaint) {
cdaltone68f7362015-03-25 14:02:37 -070060 if (skPaint.getRasterizer()) {
jvanverth0fedb192014-10-08 09:07:27 -070061 return false;
62 }
cdaltone68f7362015-03-25 14:02:37 -070063 if (skPaint.getMaskFilter()) {
jvanverth0fedb192014-10-08 09:07:27 -070064 return false;
65 }
kkinnunen50b58e62015-05-18 23:02:07 -070066 if (SkPathEffect* pe = skPaint.getPathEffect()) {
halcanary96fcdcc2015-08-27 07:41:13 -070067 if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) {
kkinnunen50b58e62015-05-18 23:02:07 -070068 return false;
69 }
jvanverth0fedb192014-10-08 09:07:27 -070070 }
cdalton7d5c9502015-10-03 13:28:35 -070071 // No hairlines. They would require new paths with customized strokes for every new draw matrix.
72 return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth();
jvanverth0fedb192014-10-08 09:07:27 -070073}
74
joshualitt27004b72016-02-11 12:00:33 -080075void GrStencilAndCoverTextContext::drawText(GrContext* context, GrDrawContext* dc,
joshualitte55750e2016-02-10 12:52:21 -080076 const GrClip& clip, const GrPaint& paint,
77 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt2c89bc12016-02-11 05:42:30 -080078 const SkSurfaceProps& props,
joshualitte55750e2016-02-10 12:52:21 -080079 const char text[], size_t byteLength,
80 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
joshualitt27004b72016-02-11 12:00:33 -080081 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -080082 return;
83 } else if (this->canDraw(skPaint, viewMatrix)) {
84 TextRun run(skPaint);
85 GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), clip);
86 run.setText(text, byteLength, x, y);
joshualitt27004b72016-02-11 12:00:33 -080087 run.draw(context, dc, &pipelineBuilder, paint.getColor(), viewMatrix, props, 0, 0,
joshualitt2c89bc12016-02-11 05:42:30 -080088 clipBounds, fFallbackTextContext, skPaint);
joshualitte55750e2016-02-10 12:52:21 -080089 return;
joshualitt27004b72016-02-11 12:00:33 -080090 } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix, props,
91 *context->caps()->shaderCaps())) {
92 fFallbackTextContext->drawText(context, dc, clip, paint, skPaint, viewMatrix, props, text,
joshualitte55750e2016-02-10 12:52:21 -080093 byteLength, x, y, clipBounds);
94 return;
95 }
96
97 // fall back to drawing as a path
joshualitt27004b72016-02-11 12:00:33 -080098 GrTextUtils::DrawTextAsPath(context, dc, clip, skPaint, viewMatrix, text, byteLength, x, y,
joshualitte55750e2016-02-10 12:52:21 -080099 clipBounds);
cdalton3bd909a2015-10-05 14:57:20 -0700100}
jvanverthaab626c2014-10-16 08:04:39 -0700101
joshualitt27004b72016-02-11 12:00:33 -0800102void GrStencilAndCoverTextContext::drawPosText(GrContext* context, GrDrawContext* dc,
joshualitte55750e2016-02-10 12:52:21 -0800103 const GrClip& clip,
104 const GrPaint& paint,
105 const SkPaint& skPaint,
106 const SkMatrix& viewMatrix,
joshualitt2c89bc12016-02-11 05:42:30 -0800107 const SkSurfaceProps& props,
joshualitte55750e2016-02-10 12:52:21 -0800108 const char text[],
109 size_t byteLength,
110 const SkScalar pos[],
111 int scalarsPerPosition,
112 const SkPoint& offset,
113 const SkIRect& clipBounds) {
joshualitt27004b72016-02-11 12:00:33 -0800114 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800115 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);
joshualitt27004b72016-02-11 12:00:33 -0800120 run.draw(context, dc, &pipelineBuilder, paint.getColor(), viewMatrix, props, 0, 0,
joshualitt2c89bc12016-02-11 05:42:30 -0800121 clipBounds, fFallbackTextContext, skPaint);
joshualitte55750e2016-02-10 12:52:21 -0800122 return;
joshualitt27004b72016-02-11 12:00:33 -0800123 } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix, props,
124 *context->caps()->shaderCaps())) {
125 fFallbackTextContext->drawPosText(context, dc, clip, paint, skPaint, viewMatrix, props,
joshualitte55750e2016-02-10 12:52:21 -0800126 text, byteLength, pos,
127 scalarsPerPosition, offset, clipBounds);
128 return;
129 }
130
131 // fall back to drawing as a path
joshualitt27004b72016-02-11 12:00:33 -0800132 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, skPaint, viewMatrix, text,
joshualitte55750e2016-02-10 12:52:21 -0800133 byteLength, pos, scalarsPerPosition, offset, clipBounds);
134}
135
joshualitt27004b72016-02-11 12:00:33 -0800136void GrStencilAndCoverTextContext::uncachedDrawTextBlob(GrContext* context,
137 GrDrawContext* dc,
joshualitte55750e2016-02-10 12:52:21 -0800138 const GrClip& clip, const SkPaint& skPaint,
139 const SkMatrix& viewMatrix,
joshualitt2c89bc12016-02-11 05:42:30 -0800140 const SkSurfaceProps& props,
joshualitte55750e2016-02-10 12:52:21 -0800141 const SkTextBlob* blob,
142 SkScalar x, SkScalar y,
143 SkDrawFilter* drawFilter,
144 const SkIRect& clipBounds) {
145 SkPaint runPaint = skPaint;
146
147 SkTextBlobRunIterator it(blob);
148 for (;!it.done(); it.next()) {
149 size_t textLen = it.glyphCount() * sizeof(uint16_t);
150 const SkPoint& offset = it.offset();
151
152 // applyFontToPaint() always overwrites the exact same attributes,
153 // so it is safe to not re-seed the paint for this reason.
154 it.applyFontToPaint(&runPaint);
155
156 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
157 // A false return from filter() means we should abort the current draw.
158 runPaint = skPaint;
159 continue;
160 }
161
joshualitt8e84a1e2016-02-16 11:09:25 -0800162 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitte55750e2016-02-10 12:52:21 -0800163
164 GrPaint grPaint;
joshualitt27004b72016-02-11 12:00:33 -0800165 if (!SkPaintToGrPaint(context, runPaint, viewMatrix, &grPaint)) {
joshualitte55750e2016-02-10 12:52:21 -0800166 return;
167 }
168
169 switch (it.positioning()) {
170 case SkTextBlob::kDefault_Positioning:
joshualitt27004b72016-02-11 12:00:33 -0800171 this->drawText(context, dc, clip, grPaint, runPaint, viewMatrix, props,
joshualitt2c89bc12016-02-11 05:42:30 -0800172 (const char *)it.glyphs(),
joshualitte55750e2016-02-10 12:52:21 -0800173 textLen, x + offset.x(), y + offset.y(), clipBounds);
174 break;
175 case SkTextBlob::kHorizontal_Positioning:
joshualitt27004b72016-02-11 12:00:33 -0800176 this->drawPosText(context, dc, clip, grPaint, runPaint, viewMatrix, props,
joshualitt2c89bc12016-02-11 05:42:30 -0800177 (const char*)it.glyphs(),
joshualitte55750e2016-02-10 12:52:21 -0800178 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
179 clipBounds);
180 break;
181 case SkTextBlob::kFull_Positioning:
joshualitt27004b72016-02-11 12:00:33 -0800182 this->drawPosText(context, dc, clip, grPaint, runPaint, viewMatrix, props,
joshualitt2c89bc12016-02-11 05:42:30 -0800183 (const char*)it.glyphs(),
joshualitte55750e2016-02-10 12:52:21 -0800184 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
185 break;
186 }
187
188 if (drawFilter) {
189 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
190 runPaint = skPaint;
191 }
192 }
cdaltoncdd79072015-10-05 15:37:35 -0700193}
194
joshualitt27004b72016-02-11 12:00:33 -0800195void GrStencilAndCoverTextContext::drawTextBlob(GrContext* context, GrDrawContext* dc,
cdaltoncdd79072015-10-05 15:37:35 -0700196 const GrClip& clip, const SkPaint& skPaint,
197 const SkMatrix& viewMatrix,
joshualitt2c89bc12016-02-11 05:42:30 -0800198 const SkSurfaceProps& props,
cdaltoncdd79072015-10-05 15:37:35 -0700199 const SkTextBlob* skBlob, SkScalar x, SkScalar y,
200 SkDrawFilter* drawFilter,
201 const SkIRect& clipBounds) {
joshualitt27004b72016-02-11 12:00:33 -0800202 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800203 return;
204 }
205
cdaltoncdd79072015-10-05 15:37:35 -0700206 if (!this->internalCanDraw(skPaint)) {
joshualitt27004b72016-02-11 12:00:33 -0800207 fFallbackTextContext->drawTextBlob(context, dc, clip, skPaint, viewMatrix, props, skBlob,
208 x, y, drawFilter, clipBounds);
cdaltoncdd79072015-10-05 15:37:35 -0700209 return;
210 }
211
212 if (drawFilter || skPaint.getPathEffect()) {
213 // This draw can't be cached.
joshualitt27004b72016-02-11 12:00:33 -0800214 this->uncachedDrawTextBlob(context, dc, clip, skPaint, viewMatrix, props, skBlob, x, y,
215 drawFilter, clipBounds);
cdaltoncdd79072015-10-05 15:37:35 -0700216 return;
217 }
218
219 GrPaint paint;
joshualitt27004b72016-02-11 12:00:33 -0800220 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &paint)) {
cdaltoncdd79072015-10-05 15:37:35 -0700221 return;
222 }
223
224 const TextBlob& blob = this->findOrCreateTextBlob(skBlob, skPaint);
robertphillips433625e2015-12-04 06:58:16 -0800225 GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), clip);
cdaltoncdd79072015-10-05 15:37:35 -0700226
227 TextBlob::Iter iter(blob);
228 for (TextRun* run = iter.get(); run; run = iter.next()) {
joshualitt27004b72016-02-11 12:00:33 -0800229 run->draw(context, dc, &pipelineBuilder, paint.getColor(), viewMatrix, props, x, y,
joshualitt2c89bc12016-02-11 05:42:30 -0800230 clipBounds, fFallbackTextContext, skPaint);
cdalton8585dd22015-10-08 08:04:09 -0700231 run->releaseGlyphCache();
cdaltoncdd79072015-10-05 15:37:35 -0700232 }
233}
234
235const GrStencilAndCoverTextContext::TextBlob&
236GrStencilAndCoverTextContext::findOrCreateTextBlob(const SkTextBlob* skBlob,
237 const SkPaint& skPaint) {
238 // The font-related parameters are baked into the text blob and will override this skPaint, so
239 // the only remaining properties that can affect a TextBlob are the ones related to stroke.
240 if (SkPaint::kFill_Style == skPaint.getStyle()) { // Fast path.
241 if (TextBlob** found = fBlobIdCache.find(skBlob->uniqueID())) {
242 fLRUList.remove(*found);
243 fLRUList.addToTail(*found);
244 return **found;
245 }
cdalton8585dd22015-10-08 08:04:09 -0700246 TextBlob* blob = new TextBlob(skBlob->uniqueID(), skBlob, skPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700247 this->purgeToFit(*blob);
248 fBlobIdCache.set(skBlob->uniqueID(), blob);
249 fLRUList.addToTail(blob);
250 fCacheSize += blob->cpuMemorySize();
251 return *blob;
252 } else {
253 GrStrokeInfo stroke(skPaint);
254 SkSTArray<4, uint32_t, true> key;
255 key.reset(1 + stroke.computeUniqueKeyFragmentData32Cnt());
256 key[0] = skBlob->uniqueID();
257 stroke.asUniqueKeyFragment(&key[1]);
258 if (TextBlob** found = fBlobKeyCache.find(key)) {
259 fLRUList.remove(*found);
260 fLRUList.addToTail(*found);
261 return **found;
262 }
cdalton8585dd22015-10-08 08:04:09 -0700263 TextBlob* blob = new TextBlob(key, skBlob, skPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700264 this->purgeToFit(*blob);
265 fBlobKeyCache.set(blob);
266 fLRUList.addToTail(blob);
267 fCacheSize += blob->cpuMemorySize();
268 return *blob;
269 }
270}
271
272void GrStencilAndCoverTextContext::purgeToFit(const TextBlob& blob) {
cdalton8585dd22015-10-08 08:04:09 -0700273 static const size_t maxCacheSize = 4 * 1024 * 1024; // Allow up to 4 MB for caching text blobs.
cdaltoncdd79072015-10-05 15:37:35 -0700274
cdalton8585dd22015-10-08 08:04:09 -0700275 size_t maxSizeForNewBlob = maxCacheSize - blob.cpuMemorySize();
cdaltoncdd79072015-10-05 15:37:35 -0700276 while (fCacheSize && fCacheSize > maxSizeForNewBlob) {
277 TextBlob* lru = fLRUList.head();
278 if (1 == lru->key().count()) {
279 // 1-length keys are unterstood to be the blob id.
280 fBlobIdCache.remove(lru->key()[0]);
281 } else {
282 fBlobKeyCache.remove(lru->key());
283 }
284 fLRUList.remove(lru);
285 fCacheSize -= lru->cpuMemorySize();
286 delete lru;
287 }
288}
289
290////////////////////////////////////////////////////////////////////////////////////////////////////
291
cdalton8585dd22015-10-08 08:04:09 -0700292void GrStencilAndCoverTextContext::TextBlob::init(const SkTextBlob* skBlob,
293 const SkPaint& skPaint) {
cdaltoncdd79072015-10-05 15:37:35 -0700294 fCpuMemorySize = sizeof(TextBlob);
295 SkPaint runPaint(skPaint);
halcanary33779752015-10-27 14:01:05 -0700296 for (SkTextBlobRunIterator iter(skBlob); !iter.done(); iter.next()) {
cdaltoncdd79072015-10-05 15:37:35 -0700297 iter.applyFontToPaint(&runPaint); // No need to re-seed the paint.
bsalomon5aaef1f2015-11-18 14:11:08 -0800298 TextRun* run = this->addToTail(runPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700299
300 const char* text = reinterpret_cast<const char*>(iter.glyphs());
301 size_t byteLength = sizeof(uint16_t) * iter.glyphCount();
302 const SkPoint& runOffset = iter.offset();
303
304 switch (iter.positioning()) {
305 case SkTextBlob::kDefault_Positioning:
cdalton8585dd22015-10-08 08:04:09 -0700306 run->setText(text, byteLength, runOffset.fX, runOffset.fY);
cdaltoncdd79072015-10-05 15:37:35 -0700307 break;
308 case SkTextBlob::kHorizontal_Positioning:
cdalton8585dd22015-10-08 08:04:09 -0700309 run->setPosText(text, byteLength, iter.pos(), 1, SkPoint::Make(0, runOffset.fY));
cdaltoncdd79072015-10-05 15:37:35 -0700310 break;
311 case SkTextBlob::kFull_Positioning:
cdalton8585dd22015-10-08 08:04:09 -0700312 run->setPosText(text, byteLength, iter.pos(), 2, SkPoint::Make(0, 0));
cdaltoncdd79072015-10-05 15:37:35 -0700313 break;
314 }
315
cdalton8585dd22015-10-08 08:04:09 -0700316 fCpuMemorySize += run->computeSizeInCache();
cdaltoncdd79072015-10-05 15:37:35 -0700317 }
cdalton3bd909a2015-10-05 14:57:20 -0700318}
319
320////////////////////////////////////////////////////////////////////////////////////////////////////
321
cdalton02015e52015-10-05 15:28:20 -0700322class GrStencilAndCoverTextContext::FallbackBlobBuilder {
323public:
cdaltoncdd46822015-12-08 10:48:31 -0800324 FallbackBlobBuilder() : fBuffIdx(0), fCount(0) {}
cdalton02015e52015-10-05 15:28:20 -0700325
326 bool isInitialized() const { return SkToBool(fBuilder); }
327
328 void init(const SkPaint& font, SkScalar textRatio);
329
330 void appendGlyph(uint16_t glyphId, const SkPoint& pos);
331
cdaltoncdd46822015-12-08 10:48:31 -0800332 const SkTextBlob* buildIfNeeded(int* count);
cdalton02015e52015-10-05 15:28:20 -0700333
334private:
335 enum { kWriteBufferSize = 1024 };
336
337 void flush();
338
339 SkAutoTDelete<SkTextBlobBuilder> fBuilder;
340 SkPaint fFont;
341 int fBuffIdx;
cdaltoncdd46822015-12-08 10:48:31 -0800342 int fCount;
cdalton02015e52015-10-05 15:28:20 -0700343 uint16_t fGlyphIds[kWriteBufferSize];
344 SkPoint fPositions[kWriteBufferSize];
345};
346
347////////////////////////////////////////////////////////////////////////////////////////////////////
348
cdalton3bd909a2015-10-05 14:57:20 -0700349GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke)
350 : fStroke(fontAndStroke),
cdaltoncdd79072015-10-05 15:37:35 -0700351 fFont(fontAndStroke),
cdalton8585dd22015-10-08 08:04:09 -0700352 fTotalGlyphCount(0),
cdaltoncdd46822015-12-08 10:48:31 -0800353 fFallbackGlyphCount(0),
cdalton8585dd22015-10-08 08:04:09 -0700354 fDetachedGlyphCache(nullptr),
355 fLastDrawnGlyphsID(SK_InvalidUniqueID) {
cdalton3bd909a2015-10-05 14:57:20 -0700356 SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported.
357
358 // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path
359 // rendering API for stroking).
360 fFont.setStyle(SkPaint::kFill_Style);
361
362 if (fFont.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) {
363 // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke.
364 SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(),
365 kStdFakeBoldInterpKeys,
366 kStdFakeBoldInterpValues,
367 kStdFakeBoldInterpLength);
368 SkScalar extra = SkScalarMul(fFont.getTextSize(), fakeBoldScale);
369 fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra,
370 true /*strokeAndFill*/);
371
372 fFont.setFakeBoldText(false);
jvanverthaab626c2014-10-16 08:04:39 -0700373 }
374
cdalton3bd909a2015-10-05 14:57:20 -0700375 if (!fFont.getPathEffect() && !fStroke.isDashed()) {
376 // We can draw the glyphs from canonically sized paths.
377 fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths;
378 fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize();
jvanverthaab626c2014-10-16 08:04:39 -0700379
cdalton3bd909a2015-10-05 14:57:20 -0700380 // Compensate for the glyphs being scaled by fTextRatio.
381 if (!fStroke.isFillStyle()) {
382 fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio,
383 SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle());
384 }
385
386 fFont.setLinearText(true);
387 fFont.setLCDRenderText(false);
388 fFont.setAutohinted(false);
389 fFont.setHinting(SkPaint::kNo_Hinting);
390 fFont.setSubpixelText(true);
391 fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
392
393 fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() &&
394 0 == fFont.getTextSkewX() &&
395 !fFont.isFakeBoldText() &&
396 !fFont.isVerticalText();
397 } else {
398 fTextRatio = fTextInverseRatio = 1.0f;
399 fUsingRawGlyphPaths = false;
400 }
401
cdalton8585dd22015-10-08 08:04:09 -0700402 // Generate the key that will be used to cache the GPU glyph path objects.
403 if (fUsingRawGlyphPaths && fStroke.isFillStyle()) {
404 static const GrUniqueKey::Domain kRawFillPathGlyphDomain = GrUniqueKey::GenerateDomain();
405
406 const SkTypeface* typeface = fFont.getTypeface();
407 GrUniqueKey::Builder builder(&fGlyphPathsKey, kRawFillPathGlyphDomain, 1);
408 reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0;
409 } else {
410 static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain();
411
412 int strokeDataCount = fStroke.computeUniqueKeyFragmentData32Cnt();
413 if (fUsingRawGlyphPaths) {
414 const SkTypeface* typeface = fFont.getTypeface();
415 GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, 2 + strokeDataCount);
416 reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0;
417 reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount;
418 fStroke.asUniqueKeyFragment(&builder[2]);
419 } else {
420 SkGlyphCache* glyphCache = this->getGlyphCache();
421 const SkTypeface* typeface = glyphCache->getScalerContext()->getTypeface();
422 const SkDescriptor* desc = &glyphCache->getDescriptor();
423 int descDataCount = (desc->getLength() + 3) / 4;
424 GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain,
425 2 + strokeDataCount + descDataCount);
426 reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0;
427 reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount | (descDataCount << 16);
428 fStroke.asUniqueKeyFragment(&builder[2]);
429 memcpy(&builder[2 + strokeDataCount], desc, desc->getLength());
430 }
431 }
cdalton3bd909a2015-10-05 14:57:20 -0700432}
433
434GrStencilAndCoverTextContext::TextRun::~TextRun() {
cdalton8585dd22015-10-08 08:04:09 -0700435 this->releaseGlyphCache();
cdalton3bd909a2015-10-05 14:57:20 -0700436}
437
438void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t byteLength,
cdalton8585dd22015-10-08 08:04:09 -0700439 SkScalar x, SkScalar y) {
cdalton3bd909a2015-10-05 14:57:20 -0700440 SkASSERT(byteLength == 0 || text != nullptr);
441
cdalton8585dd22015-10-08 08:04:09 -0700442 SkGlyphCache* glyphCache = this->getGlyphCache();
cdalton3bd909a2015-10-05 14:57:20 -0700443 SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc();
jvanverthaab626c2014-10-16 08:04:39 -0700444
cdaltoncdd46822015-12-08 10:48:31 -0800445 fTotalGlyphCount = fFont.countText(text, byteLength);
446 fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType,
447 fTotalGlyphCount));
cdalton8585dd22015-10-08 08:04:09 -0700448
jvanverthaab626c2014-10-16 08:04:39 -0700449 const char* stop = text + byteLength;
450
451 // Measure first if needed.
cdalton3bd909a2015-10-05 14:57:20 -0700452 if (fFont.getTextAlign() != SkPaint::kLeft_Align) {
jvanverthaab626c2014-10-16 08:04:39 -0700453 SkFixed stopX = 0;
454 SkFixed stopY = 0;
455
456 const char* textPtr = text;
457 while (textPtr < stop) {
458 // We don't need x, y here, since all subpixel variants will have the
459 // same advance.
cdalton3bd909a2015-10-05 14:57:20 -0700460 const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700461
462 stopX += glyph.fAdvanceX;
463 stopY += glyph.fAdvanceY;
464 }
465 SkASSERT(textPtr == stop);
466
467 SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio;
468 SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio;
469
cdalton3bd909a2015-10-05 14:57:20 -0700470 if (fFont.getTextAlign() == SkPaint::kCenter_Align) {
jvanverthaab626c2014-10-16 08:04:39 -0700471 alignX = SkScalarHalf(alignX);
472 alignY = SkScalarHalf(alignY);
473 }
474
475 x -= alignX;
476 y -= alignY;
477 }
478
479 SkAutoKern autokern;
480
481 SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio);
482
483 SkFixed fx = SkScalarToFixed(x);
484 SkFixed fy = SkScalarToFixed(y);
cdalton02015e52015-10-05 15:28:20 -0700485 FallbackBlobBuilder fallback;
jvanverthaab626c2014-10-16 08:04:39 -0700486 while (text < stop) {
cdalton3bd909a2015-10-05 14:57:20 -0700487 const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0);
bungemand709ea82015-03-17 07:23:39 -0700488 fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700489 if (glyph.fWidth) {
cdalton02015e52015-10-05 15:28:20 -0700490 this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)),
491 &fallback);
jvanverthaab626c2014-10-16 08:04:39 -0700492 }
493
bungemand709ea82015-03-17 07:23:39 -0700494 fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio);
495 fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio);
jvanverthaab626c2014-10-16 08:04:39 -0700496 }
cdalton02015e52015-10-05 15:28:20 -0700497
cdaltoncdd46822015-12-08 10:48:31 -0800498 fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount));
jvanverthaab626c2014-10-16 08:04:39 -0700499}
500
cdalton3bd909a2015-10-05 14:57:20 -0700501void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength,
502 const SkScalar pos[], int scalarsPerPosition,
cdalton8585dd22015-10-08 08:04:09 -0700503 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700504 SkASSERT(byteLength == 0 || text != nullptr);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700505 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
506
cdalton8585dd22015-10-08 08:04:09 -0700507 SkGlyphCache* glyphCache = this->getGlyphCache();
cdalton3bd909a2015-10-05 14:57:20 -0700508 SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700509
cdaltoncdd46822015-12-08 10:48:31 -0800510 fTotalGlyphCount = fFont.countText(text, byteLength);
511 fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType,
512 fTotalGlyphCount));
cdalton8585dd22015-10-08 08:04:09 -0700513
kkinnunenc6cb56f2014-06-24 00:12:27 -0700514 const char* stop = text + byteLength;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700515
cdalton38e13ad2014-11-07 06:02:15 -0800516 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
cdalton3bd909a2015-10-05 14:57:20 -0700517 SkTextAlignProc alignProc(fFont.getTextAlign());
cdalton02015e52015-10-05 15:28:20 -0700518 FallbackBlobBuilder fallback;
cdalton38e13ad2014-11-07 06:02:15 -0800519 while (text < stop) {
cdalton3bd909a2015-10-05 14:57:20 -0700520 const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0);
cdalton38e13ad2014-11-07 06:02:15 -0800521 if (glyph.fWidth) {
522 SkPoint tmsLoc;
523 tmsProc(pos, &tmsLoc);
524 SkPoint loc;
525 alignProc(tmsLoc, glyph, &loc);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700526
cdalton02015e52015-10-05 15:28:20 -0700527 this->appendGlyph(glyph, loc, &fallback);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700528 }
cdalton38e13ad2014-11-07 06:02:15 -0800529 pos += scalarsPerPosition;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700530 }
cdalton02015e52015-10-05 15:28:20 -0700531
cdaltoncdd46822015-12-08 10:48:31 -0800532 fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount));
kkinnunenc6cb56f2014-06-24 00:12:27 -0700533}
534
cdalton8585dd22015-10-08 08:04:09 -0700535GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx) const {
536 GrPathRange* glyphs = static_cast<GrPathRange*>(
537 ctx->resourceProvider()->findAndRefResourceByUniqueKey(fGlyphPathsKey));
halcanary96fcdcc2015-08-27 07:41:13 -0700538 if (nullptr == glyphs) {
cdalton8585dd22015-10-08 08:04:09 -0700539 if (fUsingRawGlyphPaths) {
540 glyphs = ctx->resourceProvider()->createGlyphs(fFont.getTypeface(), nullptr, fStroke);
541 } else {
542 SkGlyphCache* cache = this->getGlyphCache();
543 glyphs = ctx->resourceProvider()->createGlyphs(cache->getScalerContext()->getTypeface(),
544 &cache->getDescriptor(),
545 fStroke);
546 }
547 ctx->resourceProvider()->assignUniqueKeyToResource(fGlyphPathsKey, glyphs);
cdalton855d83f2014-09-18 13:51:53 -0700548 }
cdalton8585dd22015-10-08 08:04:09 -0700549 return glyphs;
cdalton855d83f2014-09-18 13:51:53 -0700550}
551
cdalton3bd909a2015-10-05 14:57:20 -0700552inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph,
cdalton02015e52015-10-05 15:28:20 -0700553 const SkPoint& pos,
554 FallbackBlobBuilder* fallback) {
555 // Stick the glyphs we can't draw into the fallback text blob.
bsalomon1fcc01c2015-09-09 09:48:06 -0700556 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
cdalton02015e52015-10-05 15:28:20 -0700557 if (!fallback->isInitialized()) {
558 fallback->init(fFont, fTextRatio);
559 }
560 fallback->appendGlyph(glyph.getGlyphID(), pos);
bsalomon1fcc01c2015-09-09 09:48:06 -0700561 } else {
cdaltoncdd46822015-12-08 10:48:31 -0800562 fInstanceData->append(glyph.getGlyphID(), fTextInverseRatio * pos.x(),
563 fTextInverseRatio * pos.y());
cdaltonb2808cd2014-07-25 14:13:57 -0700564 }
cdalton20b373c2014-12-01 08:38:55 -0800565}
566
cdalton8585dd22015-10-08 08:04:09 -0700567void GrStencilAndCoverTextContext::TextRun::draw(GrContext* ctx,
568 GrDrawContext* dc,
cdaltoncdd79072015-10-05 15:37:35 -0700569 GrPipelineBuilder* pipelineBuilder,
570 GrColor color,
cdalton3bd909a2015-10-05 14:57:20 -0700571 const SkMatrix& viewMatrix,
joshualitt2c89bc12016-02-11 05:42:30 -0800572 const SkSurfaceProps& props,
cdaltoncdd79072015-10-05 15:37:35 -0700573 SkScalar x, SkScalar y,
574 const SkIRect& clipBounds,
joshualitt8e84a1e2016-02-16 11:09:25 -0800575 GrAtlasTextContext* fallbackTextContext,
cdalton3bd909a2015-10-05 14:57:20 -0700576 const SkPaint& originalSkPaint) const {
cdaltoncdd46822015-12-08 10:48:31 -0800577 SkASSERT(fInstanceData);
robertphillips433625e2015-12-04 06:58:16 -0800578 SkASSERT(dc->accessRenderTarget()->isStencilBufferMultisampled() || !fFont.isAntiAlias());
robertphillipsea461502015-05-26 11:38:03 -0700579
cdaltoncdd46822015-12-08 10:48:31 -0800580 if (fInstanceData->count()) {
cdaltoncdd79072015-10-05 15:37:35 -0700581 pipelineBuilder->setState(GrPipelineBuilder::kHWAntialias_Flag, fFont.isAntiAlias());
joshualitt7b670db2015-07-09 13:25:02 -0700582
583 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
584 kZero_StencilOp,
vbuzinovc5d58f02015-08-21 05:24:24 -0700585 kKeep_StencilOp,
joshualitt7b670db2015-07-09 13:25:02 -0700586 kNotEqual_StencilFunc,
587 0xffff,
588 0x0000,
589 0xffff);
590
cdaltoncdd79072015-10-05 15:37:35 -0700591 *pipelineBuilder->stencil() = kStencilPass;
joshualitt7b670db2015-07-09 13:25:02 -0700592
cdalton8585dd22015-10-08 08:04:09 -0700593 SkAutoTUnref<GrPathRange> glyphs(this->createGlyphs(ctx));
594 if (fLastDrawnGlyphsID != glyphs->getUniqueID()) {
595 // Either this is the first draw or the glyphs object was purged since last draw.
cdaltoncdd46822015-12-08 10:48:31 -0800596 glyphs->loadPathsIfNeeded(fInstanceData->indices(), fInstanceData->count());
cdalton8585dd22015-10-08 08:04:09 -0700597 fLastDrawnGlyphsID = glyphs->getUniqueID();
598 }
599
bsalomonbf074552015-11-23 14:25:19 -0800600 // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy
601 // the entire dst. Realistically this is a moot point, because any context that supports
602 // NV_path_rendering will also support NV_blend_equation_advanced.
603 // For clipping we'll just skip any optimizations based on the bounds. This does, however,
604 // hurt batching.
605 SkRect bounds = SkRect::MakeIWH(pipelineBuilder->getRenderTarget()->width(),
606 pipelineBuilder->getRenderTarget()->height());
607
cdalton8ff8d242015-12-08 10:20:32 -0800608 SkAutoTUnref<GrDrawPathBatchBase> batch(
cdaltoncdd46822015-12-08 10:48:31 -0800609 GrDrawPathRangeBatch::Create(viewMatrix, fTextRatio, fTextInverseRatio * x,
610 fTextInverseRatio * y, color,
611 GrPathRendering::kWinding_FillType, glyphs, fInstanceData,
cdalton8ff8d242015-12-08 10:20:32 -0800612 bounds));
613
614 dc->drawPathBatch(*pipelineBuilder, batch);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700615 }
616
cdalton02015e52015-10-05 15:28:20 -0700617 if (fFallbackTextBlob) {
cdalton3bd909a2015-10-05 14:57:20 -0700618 SkPaint fallbackSkPaint(originalSkPaint);
cdalton7d5c9502015-10-03 13:28:35 -0700619 fStroke.applyToPaint(&fallbackSkPaint);
620 if (!fStroke.isFillStyle()) {
621 fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio);
cdalton20b373c2014-12-01 08:38:55 -0800622 }
cdalton20b373c2014-12-01 08:38:55 -0800623
joshualitt27004b72016-02-11 12:00:33 -0800624 fallbackTextContext->drawTextBlob(ctx, dc, pipelineBuilder->clip(), fallbackSkPaint,
625 viewMatrix, props, fFallbackTextBlob, x, y, nullptr,
626 clipBounds);
cdalton20b373c2014-12-01 08:38:55 -0800627 }
kkinnunenc6cb56f2014-06-24 00:12:27 -0700628}
cdalton02015e52015-10-05 15:28:20 -0700629
cdalton8585dd22015-10-08 08:04:09 -0700630SkGlyphCache* GrStencilAndCoverTextContext::TextRun::getGlyphCache() const {
631 if (!fDetachedGlyphCache) {
632 fDetachedGlyphCache = fFont.detachCache(nullptr, nullptr, true /*ignoreGamma*/);
633 }
634 return fDetachedGlyphCache;
635}
636
637
638void GrStencilAndCoverTextContext::TextRun::releaseGlyphCache() const {
639 if (fDetachedGlyphCache) {
640 SkGlyphCache::AttachCache(fDetachedGlyphCache);
641 fDetachedGlyphCache = nullptr;
642 }
643}
644
645size_t GrStencilAndCoverTextContext::TextRun::computeSizeInCache() const {
cdaltoncdd46822015-12-08 10:48:31 -0800646 size_t size = sizeof(TextRun) + fGlyphPathsKey.size();
647 // The instance data always reserves enough space for every glyph.
648 size += (fTotalGlyphCount + fFallbackGlyphCount) * (sizeof(uint16_t) + 2 * sizeof(float));
649 if (fInstanceData) {
650 size += sizeof(InstanceData);
cdaltoncdd79072015-10-05 15:37:35 -0700651 }
652 if (fFallbackTextBlob) {
653 size += sizeof(SkTextBlob);
654 }
655 return size;
656}
657
cdalton02015e52015-10-05 15:28:20 -0700658////////////////////////////////////////////////////////////////////////////////////////////////////
659
660void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font,
661 SkScalar textRatio) {
662 SkASSERT(!this->isInitialized());
663 fBuilder.reset(new SkTextBlobBuilder);
664 fFont = font;
665 fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align.
666 fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
667 // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs
668 // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved.
669 fFont.setSubpixelText(false);
670 fFont.setTextSize(fFont.getTextSize() * textRatio);
671 fBuffIdx = 0;
672}
673
674void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId,
675 const SkPoint& pos) {
676 SkASSERT(this->isInitialized());
677 if (fBuffIdx >= kWriteBufferSize) {
678 this->flush();
679 }
680 fGlyphIds[fBuffIdx] = glyphId;
681 fPositions[fBuffIdx] = pos;
682 fBuffIdx++;
cdaltoncdd46822015-12-08 10:48:31 -0800683 fCount++;
cdalton02015e52015-10-05 15:28:20 -0700684}
685
686void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() {
687 SkASSERT(this->isInitialized());
688 SkASSERT(fBuffIdx <= kWriteBufferSize);
689 if (!fBuffIdx) {
690 return;
691 }
692 // This will automatically merge with previous runs since we use the same font.
693 const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx);
694 memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t));
695 memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar));
696 fBuffIdx = 0;
697}
698
cdaltoncdd46822015-12-08 10:48:31 -0800699const SkTextBlob* GrStencilAndCoverTextContext::FallbackBlobBuilder::buildIfNeeded(int *count) {
700 *count = fCount;
701 if (fCount) {
702 this->flush();
703 return fBuilder->build();
cdalton02015e52015-10-05 15:28:20 -0700704 }
cdaltoncdd46822015-12-08 10:48:31 -0800705 return nullptr;
cdalton02015e52015-10-05 15:28:20 -0700706}