blob: f39e7a1bb0d9cf922ee261b5600cbae65a2dc517 [file] [log] [blame]
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +00001/*
2 * Copyright 2013 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 "GrBitmapTextContext.h"
9#include "GrAtlas.h"
joshualitt5478d422014-11-14 16:00:38 -080010#include "GrDefaultGeoProcFactory.h"
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000011#include "GrDrawTarget.h"
jvanverth787cdf92014-12-04 10:46:50 -080012#include "GrFontCache.h"
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000013#include "GrFontScaler.h"
14#include "GrIndexBuffer.h"
egdanield58a0ba2014-06-11 10:30:05 -070015#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrTexturePriv.h"
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000017
jvanverthaab626c2014-10-16 08:04:39 -070018#include "SkAutoKern.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "SkColorPriv.h"
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000020#include "SkDraw.h"
kkinnunencb9a2c82014-06-12 23:06:28 -070021#include "SkDrawProcs.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000022#include "SkGlyphCache.h"
23#include "SkGpuDevice.h"
24#include "SkGr.h"
bsalomonafbf2d62014-09-30 12:18:44 -070025#include "SkPath.h"
26#include "SkRTConf.h"
27#include "SkStrokeRec.h"
kkinnunencb9a2c82014-06-12 23:06:28 -070028#include "SkTextMapStateProc.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000029
egdaniel309e3462014-12-09 10:35:58 -080030#include "effects/GrBitmapTextGeoProc.h"
joshualitt5478d422014-11-14 16:00:38 -080031#include "effects/GrSimpleTextureEffect.h"
32
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000033SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
34 "Dump the contents of the font cache before every purge.");
35
bsalomon594069f2014-06-06 06:16:34 -070036namespace {
egdanieled3af662014-10-31 06:55:45 -070037static const size_t kLCDTextVASize = 2 * sizeof(SkPoint);
38
39// position + local coord
egdanieled3af662014-10-31 06:55:45 -070040static const size_t kColorTextVASize = 2 * sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -070041
egdanieled3af662014-10-31 06:55:45 -070042static const size_t kGrayTextVASize = 2 * sizeof(SkPoint) + sizeof(GrColor);
egdaniel7b3d5ee2014-08-28 05:41:14 -070043
jvanverth73f10532014-10-23 11:57:12 -070044static const int kVerticesPerGlyph = 4;
45static const int kIndicesPerGlyph = 6;
bsalomon594069f2014-06-06 06:16:34 -070046};
47
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000048GrBitmapTextContext::GrBitmapTextContext(GrContext* context,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000049 const SkDeviceProperties& properties)
Mike Klein6a25bd02014-08-29 10:03:59 -040050 : GrTextContext(context, properties) {
51 fStrike = NULL;
52
53 fCurrTexture = NULL;
Mike Klein6a25bd02014-08-29 10:03:59 -040054 fEffectTextureUniqueID = SK_InvalidUniqueID;
55
56 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070057 fCurrVertex = 0;
58 fAllocVertexCount = 0;
59 fTotalVertexCount = 0;
Mike Klein6a25bd02014-08-29 10:03:59 -040060
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000061 fVertexBounds.setLargestInverted();
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000062}
63
jvanverth8c27a182014-10-14 08:45:50 -070064GrBitmapTextContext* GrBitmapTextContext::Create(GrContext* context,
65 const SkDeviceProperties& props) {
66 return SkNEW_ARGS(GrBitmapTextContext, (context, props));
67}
68
joshualitt5531d512014-12-17 15:50:11 -080069bool GrBitmapTextContext::canDraw(const SkPaint& paint, const SkMatrix& viewMatrix) {
70 return !SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000071}
72
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000073inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
74 GrTextContext::init(paint, skPaint);
75
76 fStrike = NULL;
77
Mike Klein6a25bd02014-08-29 10:03:59 -040078 fCurrTexture = NULL;
jvanverth63b9dc82014-08-28 10:39:40 -070079 fCurrVertex = 0;
Mike Klein6a25bd02014-08-29 10:03:59 -040080
81 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070082 fAllocVertexCount = 0;
83 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000084}
85
jvanverthaab626c2014-10-16 08:04:39 -070086void GrBitmapTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -080087 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -080088 const char text[], size_t byteLength,
89 SkScalar x, SkScalar y) {
jvanverthaab626c2014-10-16 08:04:39 -070090 SkASSERT(byteLength == 0 || text != NULL);
91
92 // nothing to draw
93 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) {
94 return;
95 }
96
97 this->init(paint, skPaint);
98
99 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
100
joshualitt5531d512014-12-17 15:50:11 -0800101 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &viewMatrix);
jvanverthaab626c2014-10-16 08:04:39 -0700102 SkGlyphCache* cache = autoCache.getCache();
103 GrFontScaler* fontScaler = GetGrFontScaler(cache);
104
105 // transform our starting point
106 {
107 SkPoint loc;
joshualitt5531d512014-12-17 15:50:11 -0800108 viewMatrix.mapXY(x, y, &loc);
jvanverthaab626c2014-10-16 08:04:39 -0700109 x = loc.fX;
110 y = loc.fY;
111 }
112
113 // need to measure first
jvanverth73f10532014-10-23 11:57:12 -0700114 int numGlyphs;
jvanverthaab626c2014-10-16 08:04:39 -0700115 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
jvanverth73f10532014-10-23 11:57:12 -0700116 SkVector stopVector;
117 numGlyphs = MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector);
jvanverthaab626c2014-10-16 08:04:39 -0700118
jvanverth73f10532014-10-23 11:57:12 -0700119 SkScalar stopX = stopVector.fX;
120 SkScalar stopY = stopVector.fY;
jvanverthaab626c2014-10-16 08:04:39 -0700121
122 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
123 stopX = SkScalarHalf(stopX);
124 stopY = SkScalarHalf(stopY);
125 }
126 x -= stopX;
127 y -= stopY;
jvanverth73f10532014-10-23 11:57:12 -0700128 } else {
129 numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
jvanverthaab626c2014-10-16 08:04:39 -0700130 }
jvanverth73f10532014-10-23 11:57:12 -0700131 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
jvanverthaab626c2014-10-16 08:04:39 -0700132
133 const char* stop = text + byteLength;
134
135 SkAutoKern autokern;
136
137 SkFixed fxMask = ~0;
138 SkFixed fyMask = ~0;
139 SkFixed halfSampleX, halfSampleY;
140 if (cache->isSubpixel()) {
141 halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits);
joshualitt5531d512014-12-17 15:50:11 -0800142 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
jvanverthaab626c2014-10-16 08:04:39 -0700143 if (kX_SkAxisAlignment == baseline) {
144 fyMask = 0;
145 halfSampleY = SK_FixedHalf;
146 } else if (kY_SkAxisAlignment == baseline) {
147 fxMask = 0;
148 halfSampleX = SK_FixedHalf;
149 }
150 } else {
151 halfSampleX = halfSampleY = SK_FixedHalf;
152 }
153
154 SkFixed fx = SkScalarToFixed(x) + halfSampleX;
155 SkFixed fy = SkScalarToFixed(y) + halfSampleY;
156
joshualitt73483d92014-12-23 07:58:02 -0800157 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix, but for
158 // performance reasons we just invert here instead
159 if (!viewMatrix.invert(&fLocalMatrix)) {
joshualitt787bb482015-01-02 10:54:26 -0800160 SkDebugf("Cannot invert viewmatrix\n");
161 return;
joshualitt5531d512014-12-17 15:50:11 -0800162 }
jvanverthaab626c2014-10-16 08:04:39 -0700163
164 while (text < stop) {
165 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
166
167 fx += autokern.adjust(glyph);
168
169 if (glyph.fWidth) {
170 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
171 glyph.getSubXFixed(),
172 glyph.getSubYFixed()),
173 SkFixedFloorToFixed(fx),
174 SkFixedFloorToFixed(fy),
175 fontScaler);
176 }
177
178 fx += glyph.fAdvanceX;
179 fy += glyph.fAdvanceY;
180 }
181
182 this->finish();
183}
184
jvanverth8c27a182014-10-14 08:45:50 -0700185void GrBitmapTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800186 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800187 const char text[], size_t byteLength,
188 const SkScalar pos[], int scalarsPerPosition,
189 const SkPoint& offset) {
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000190 SkASSERT(byteLength == 0 || text != NULL);
191 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
192
193 // nothing to draw
194 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
195 return;
196 }
197
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000198 this->init(paint, skPaint);
199
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000200 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
201
joshualitt5531d512014-12-17 15:50:11 -0800202 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &viewMatrix);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000203 SkGlyphCache* cache = autoCache.getCache();
204 GrFontScaler* fontScaler = GetGrFontScaler(cache);
205
joshualitt73483d92014-12-23 07:58:02 -0800206 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix, but for
207 // performance reasons we just invert here instead
208 if (!viewMatrix.invert(&fLocalMatrix)) {
joshualitt787bb482015-01-02 10:54:26 -0800209 SkDebugf("Cannot invert viewmatrix\n");
210 return;
joshualitt5531d512014-12-17 15:50:11 -0800211 }
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000212
jvanverth73f10532014-10-23 11:57:12 -0700213 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
214 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
215
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000216 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -0700217 SkTextAlignProc alignProc(fSkPaint.getTextAlign());
joshualitt73483d92014-12-23 07:58:02 -0800218 SkTextMapStateProc tmsProc(viewMatrix, offset, scalarsPerPosition);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000219 SkFixed halfSampleX = 0, halfSampleY = 0;
220
221 if (cache->isSubpixel()) {
222 // maybe we should skip the rounding if linearText is set
joshualitt73483d92014-12-23 07:58:02 -0800223 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000224
225 SkFixed fxMask = ~0;
226 SkFixed fyMask = ~0;
227 if (kX_SkAxisAlignment == baseline) {
228 fyMask = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000229 halfSampleY = SK_FixedHalf;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000230 } else if (kY_SkAxisAlignment == baseline) {
231 fxMask = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000232 halfSampleX = SK_FixedHalf;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000233 }
234
235 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
236 while (text < stop) {
kkinnunencb9a2c82014-06-12 23:06:28 -0700237 SkPoint tmsLoc;
238 tmsProc(pos, &tmsLoc);
239 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + halfSampleX;
240 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + halfSampleY;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000241
242 const SkGlyph& glyph = glyphCacheProc(cache, &text,
243 fx & fxMask, fy & fyMask);
244
245 if (glyph.fWidth) {
jvanverth0fedb192014-10-08 09:07:27 -0700246 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
247 glyph.getSubXFixed(),
248 glyph.getSubYFixed()),
249 SkFixedFloorToFixed(fx),
250 SkFixedFloorToFixed(fy),
251 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000252 }
253 pos += scalarsPerPosition;
254 }
255 } else {
256 while (text < stop) {
257 const char* currentText = text;
258 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
259
260 if (metricGlyph.fWidth) {
261 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
262 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
kkinnunencb9a2c82014-06-12 23:06:28 -0700263 SkPoint tmsLoc;
264 tmsProc(pos, &tmsLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000265 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -0700266 alignProc(tmsLoc, metricGlyph, &fixedLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000267
268 SkFixed fx = fixedLoc.fX + halfSampleX;
269 SkFixed fy = fixedLoc.fY + halfSampleY;
270
271 // have to call again, now that we've been "aligned"
272 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
273 fx & fxMask, fy & fyMask);
274 // the assumption is that the metrics haven't changed
275 SkASSERT(prevAdvX == glyph.fAdvanceX);
276 SkASSERT(prevAdvY == glyph.fAdvanceY);
277 SkASSERT(glyph.fWidth);
278
jvanverth0fedb192014-10-08 09:07:27 -0700279 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
280 glyph.getSubXFixed(),
281 glyph.getSubYFixed()),
282 SkFixedFloorToFixed(fx),
283 SkFixedFloorToFixed(fy),
284 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000285 }
286 pos += scalarsPerPosition;
287 }
288 }
289 } else { // not subpixel
290
291 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
292 while (text < stop) {
293 // the last 2 parameters are ignored
294 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
295
296 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -0700297 SkPoint tmsLoc;
298 tmsProc(pos, &tmsLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000299
kkinnunencb9a2c82014-06-12 23:06:28 -0700300 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf; //halfSampleX;
301 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf; //halfSampleY;
jvanverth0fedb192014-10-08 09:07:27 -0700302 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
303 glyph.getSubXFixed(),
304 glyph.getSubYFixed()),
305 SkFixedFloorToFixed(fx),
306 SkFixedFloorToFixed(fy),
307 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000308 }
309 pos += scalarsPerPosition;
310 }
311 } else {
312 while (text < stop) {
313 // the last 2 parameters are ignored
314 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
315
316 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -0700317 SkPoint tmsLoc;
318 tmsProc(pos, &tmsLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000319
320 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -0700321 alignProc(tmsLoc, glyph, &fixedLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000322
323 SkFixed fx = fixedLoc.fX + SK_FixedHalf; //halfSampleX;
324 SkFixed fy = fixedLoc.fY + SK_FixedHalf; //halfSampleY;
jvanverth0fedb192014-10-08 09:07:27 -0700325 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
326 glyph.getSubXFixed(),
327 glyph.getSubYFixed()),
328 SkFixedFloorToFixed(fx),
329 SkFixedFloorToFixed(fy),
330 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000331 }
332 pos += scalarsPerPosition;
333 }
334 }
335 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000336
337 this->finish();
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000338}
339
joshualitt9853cce2014-11-17 14:22:48 -0800340static size_t get_vertex_stride(GrMaskFormat maskFormat) {
341 switch (maskFormat) {
342 case kA8_GrMaskFormat:
343 return kGrayTextVASize;
344 case kARGB_GrMaskFormat:
345 return kColorTextVASize;
346 default:
347 return kLCDTextVASize;
348 }
349}
350
joshualitt9853cce2014-11-17 14:22:48 -0800351static void* alloc_vertices(GrDrawTarget* drawTarget,
352 int numVertices,
353 GrMaskFormat maskFormat) {
jvanverth73f10532014-10-23 11:57:12 -0700354 if (numVertices <= 0) {
355 return NULL;
356 }
357
358 // set up attributes
jvanverth73f10532014-10-23 11:57:12 -0700359 void* vertices = NULL;
360 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800361 get_vertex_stride(maskFormat),
jvanverth73f10532014-10-23 11:57:12 -0700362 0,
363 &vertices,
364 NULL);
365 GrAlwaysAssert(success);
366 return vertices;
367}
368
jvanverth787cdf92014-12-04 10:46:50 -0800369inline bool GrBitmapTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
370 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
371 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
372 return true;
373 }
374
375 // try to clear out an unused plot before we flush
376 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
377 fStrike->addGlyphToAtlas(glyph, scaler)) {
378 return true;
379 }
380
381 if (c_DumpFontCache) {
382#ifdef SK_DEVELOPER
383 fContext->getFontCache()->dump();
384#endif
385 }
386
387 // before we purge the cache, we must flush any accumulated draws
388 this->flush();
389 fContext->flush();
390
391 // we should have an unused plot now
392 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
393 fStrike->addGlyphToAtlas(glyph, scaler)) {
394 return true;
395 }
396
397 // we should never get here
398 SkASSERT(false);
399 }
400
401 return false;
402}
403
jvanverth0fedb192014-10-08 09:07:27 -0700404void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed,
405 SkFixed vx, SkFixed vy,
406 GrFontScaler* scaler) {
Mike Klein6a25bd02014-08-29 10:03:59 -0400407 if (NULL == fDrawTarget) {
408 return;
409 }
410
411 if (NULL == fStrike) {
412 fStrike = fContext->getFontCache()->getStrike(scaler, false);
413 }
414
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000415 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
416 if (NULL == glyph || glyph->fBounds.isEmpty()) {
417 return;
418 }
419
420 vx += SkIntToFixed(glyph->fBounds.fLeft);
421 vy += SkIntToFixed(glyph->fBounds.fTop);
422
423 // keep them as ints until we've done the clip-test
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000424 SkFixed width = glyph->fBounds.width();
425 SkFixed height = glyph->fBounds.height();
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000426
427 // check if we clipped out
428 if (true || NULL == glyph->fPlot) {
429 int x = vx >> 16;
430 int y = vy >> 16;
431 if (fClipRect.quickReject(x, y, x + width, y + height)) {
432// SkCLZ(3); // so we can set a break-point in the debugger
433 return;
434 }
435 }
436
joshualittc2625822014-12-18 16:40:54 -0800437 // If the glyph is too large we fall back to paths
jvanverth787cdf92014-12-04 10:46:50 -0800438 if (NULL == glyph->fPlot && !uploadGlyph(glyph, scaler)) {
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000439 if (NULL == glyph->fPath) {
440 SkPath* path = SkNEW(SkPath);
441 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
442 // flag the glyph as being dead?
443 delete path;
444 return;
445 }
446 glyph->fPath = path;
447 }
448
bsalomonec87dc62014-10-14 10:52:00 -0700449 // flush any accumulated draws before drawing this glyph as a path.
450 this->flush();
451
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000452 SkMatrix translate;
453 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)),
454 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop)));
joshualittc2625822014-12-18 16:40:54 -0800455 SkPath tmpPath(*glyph->fPath);
456 tmpPath.transform(translate);
egdanield58a0ba2014-06-11 10:30:05 -0700457 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
joshualittc2625822014-12-18 16:40:54 -0800458 fContext->drawPath(fPaint, SkMatrix::I(), tmpPath, strokeInfo);
jvanverth73f10532014-10-23 11:57:12 -0700459
460 // remove this glyph from the vertices we need to allocate
461 fTotalVertexCount -= kVerticesPerGlyph;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000462 return;
463 }
464
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000465 SkASSERT(glyph->fPlot);
466 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
467 glyph->fPlot->setDrawToken(drawToken);
468
469 // now promote them to fixed (TODO: Rethink using fixed pt).
470 width = SkIntToFixed(width);
471 height = SkIntToFixed(height);
472
jvanverth294c3262014-10-10 11:36:12 -0700473 // the current texture/maskformat must match what the glyph needs
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000474 GrTexture* texture = glyph->fPlot->texture();
475 SkASSERT(texture);
476
jvanverth73f10532014-10-23 11:57:12 -0700477 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fAllocVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700478 this->flush();
Mike Klein6a25bd02014-08-29 10:03:59 -0400479 fCurrTexture = texture;
480 fCurrTexture->ref();
jvanverth294c3262014-10-10 11:36:12 -0700481 fCurrMaskFormat = glyph->fMaskFormat;
Mike Klein6a25bd02014-08-29 10:03:59 -0400482 }
483
Mike Klein6a25bd02014-08-29 10:03:59 -0400484 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700485 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
486 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
egdanieled3af662014-10-31 06:55:45 -0700487 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, fCurrMaskFormat);
Mike Klein6a25bd02014-08-29 10:03:59 -0400488 }
489
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000490 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
491 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000492
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000493 SkRect r;
494 r.fLeft = SkFixedToFloat(vx);
495 r.fTop = SkFixedToFloat(vy);
496 r.fRight = SkFixedToFloat(vx + width);
497 r.fBottom = SkFixedToFloat(vy + height);
498
reed10d03272014-10-01 09:24:12 -0700499 fVertexBounds.joinNonEmptyArg(r);
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000500
joshualitt9853cce2014-11-17 14:22:48 -0800501 size_t vertSize = get_vertex_stride(fCurrMaskFormat);
bsalomon594069f2014-06-06 06:16:34 -0700502
503 SkPoint* positions = reinterpret_cast<SkPoint*>(
504 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
505 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
506
507 // The texture coords are last in both the with and without color vertex layouts.
508 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
509 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
bsalomonafbf2d62014-09-30 12:18:44 -0700510 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
511 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
512 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + width)),
513 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + height)),
bsalomon594069f2014-06-06 06:16:34 -0700514 vertSize);
egdanieled3af662014-10-31 06:55:45 -0700515 if (kA8_GrMaskFormat == fCurrMaskFormat) {
bsalomon594069f2014-06-06 06:16:34 -0700516 // color comes after position.
517 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
518 for (int i = 0; i < 4; ++i) {
519 *colors = fPaint.getColor();
520 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
521 }
522 }
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000523 fCurrVertex += 4;
524}
jvanverth0fedb192014-10-08 09:07:27 -0700525
jvanverth0fedb192014-10-08 09:07:27 -0700526void GrBitmapTextContext::flush() {
527 if (NULL == fDrawTarget) {
528 return;
529 }
530
jvanverth0fedb192014-10-08 09:07:27 -0700531 if (fCurrVertex > 0) {
egdaniel8dd688b2015-01-22 10:16:09 -0800532 GrPipelineBuilder pipelineBuilder;
533 pipelineBuilder.setFromPaint(fPaint, fContext->getRenderTarget());
joshualitt780b11e2014-11-18 09:40:40 -0800534
jvanverth0fedb192014-10-08 09:07:27 -0700535 // setup our sampler state for our text texture/atlas
536 SkASSERT(SkIsAlign4(fCurrVertex));
537 SkASSERT(fCurrTexture);
jvanverth0fedb192014-10-08 09:07:27 -0700538
jvanverth0fedb192014-10-08 09:07:27 -0700539 SkASSERT(fStrike);
joshualitt2e3b3e32014-12-09 13:31:14 -0800540 GrColor color = fPaint.getColor();
jvanverth294c3262014-10-10 11:36:12 -0700541 switch (fCurrMaskFormat) {
jvanverth0fedb192014-10-08 09:07:27 -0700542 // Color bitmap text
joshualitt2e3b3e32014-12-09 13:31:14 -0800543 case kARGB_GrMaskFormat: {
544 int a = fSkPaint.getAlpha();
545 color = SkColorSetARGB(a, a, a, a);
jvanverth0fedb192014-10-08 09:07:27 -0700546 break;
joshualitt2e3b3e32014-12-09 13:31:14 -0800547 }
jvanverth0fedb192014-10-08 09:07:27 -0700548 // LCD text
jvanverth0fedb192014-10-08 09:07:27 -0700549 case kA565_GrMaskFormat: {
egdaniel378092f2014-12-03 10:40:13 -0800550 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
551 // processor if the xp can support it. For now we will simply assume that if
552 // fUseLCDText is true, then we have a known color output.
egdaniel8dd688b2015-01-22 10:16:09 -0800553 const GrXPFactory* xpFactory = pipelineBuilder.getXPFactory();
554 if (!xpFactory->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700555 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700556 }
jvanverth0fedb192014-10-08 09:07:27 -0700557 break;
558 }
559 // Grayscale/BW text
560 case kA8_GrMaskFormat:
jvanverth0fedb192014-10-08 09:07:27 -0700561 break;
562 default:
jvanverth294c3262014-10-10 11:36:12 -0700563 SkFAIL("Unexpected mask format.");
jvanverth0fedb192014-10-08 09:07:27 -0700564 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800565
566 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt2e3b3e32014-12-09 13:31:14 -0800567 if (kARGB_GrMaskFormat == fCurrMaskFormat) {
568 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
569 if (textureUniqueID != fEffectTextureUniqueID ||
joshualitt56995b52014-12-11 15:44:02 -0800570 fCachedGeometryProcessor->color() != color) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800571 uint32_t flags = GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt8059eb92014-12-29 15:10:07 -0800572 fCachedGeometryProcessor.reset(GrDefaultGeoProcFactory::Create(flags, color));
joshualitt2e3b3e32014-12-09 13:31:14 -0800573 fCachedTextureProcessor.reset(GrSimpleTextureEffect::Create(fCurrTexture,
574 SkMatrix::I(),
575 params));
joshualitt8fc6c2d2014-12-22 15:27:05 -0800576 fEffectTextureUniqueID = textureUniqueID;
joshualitt2e3b3e32014-12-09 13:31:14 -0800577 }
egdaniel8dd688b2015-01-22 10:16:09 -0800578 pipelineBuilder.addColorProcessor(fCachedTextureProcessor.get());
joshualitt2e3b3e32014-12-09 13:31:14 -0800579 } else {
580 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
581 if (textureUniqueID != fEffectTextureUniqueID ||
joshualitt8fc6c2d2014-12-22 15:27:05 -0800582 fCachedGeometryProcessor->color() != color ||
583 !fCachedGeometryProcessor->localMatrix().cheapEqualTo(fLocalMatrix)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800584 bool hasColor = kA8_GrMaskFormat == fCurrMaskFormat;
joshualitt56995b52014-12-11 15:44:02 -0800585 bool opaqueVertexColors = GrColorIsOpaque(fPaint.getColor());
joshualitt2e3b3e32014-12-09 13:31:14 -0800586 fCachedGeometryProcessor.reset(GrBitmapTextGeoProc::Create(color,
joshualitt56995b52014-12-11 15:44:02 -0800587 fCurrTexture,
588 params,
589 hasColor,
joshualitt8fc6c2d2014-12-22 15:27:05 -0800590 opaqueVertexColors,
591 fLocalMatrix));
joshualitt2e3b3e32014-12-09 13:31:14 -0800592 fEffectTextureUniqueID = textureUniqueID;
593 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800594 }
595
jvanverth73f10532014-10-23 11:57:12 -0700596 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700597 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
egdaniel8dd688b2015-01-22 10:16:09 -0800598 fDrawTarget->drawIndexedInstances(&pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800599 fCachedGeometryProcessor.get(),
joshualitt9853cce2014-11-17 14:22:48 -0800600 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700601 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800602 kVerticesPerGlyph,
603 kIndicesPerGlyph,
604 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700605
606 fDrawTarget->resetVertexSource();
607 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700608 fAllocVertexCount = 0;
609 // reset to be those that are left
610 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700611 fCurrVertex = 0;
612 fVertexBounds.setLargestInverted();
613 SkSafeSetNull(fCurrTexture);
614 }
615}
616
617inline void GrBitmapTextContext::finish() {
618 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700619 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700620
621 GrTextContext::finish();
622}
623