blob: 732a514c4fb2ef38983d69059afa68328ffb0b2b [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
joshualitt5531d512014-12-17 15:50:11 -0800157 if (!fPaint.localCoordChangeInverse(viewMatrix)) {
158 SkDebugf("Cannot invert viewmatrix\n");
159 }
jvanverthaab626c2014-10-16 08:04:39 -0700160
161 while (text < stop) {
162 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
163
164 fx += autokern.adjust(glyph);
165
166 if (glyph.fWidth) {
167 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
168 glyph.getSubXFixed(),
169 glyph.getSubYFixed()),
170 SkFixedFloorToFixed(fx),
171 SkFixedFloorToFixed(fy),
172 fontScaler);
173 }
174
175 fx += glyph.fAdvanceX;
176 fy += glyph.fAdvanceY;
177 }
178
179 this->finish();
180}
181
jvanverth8c27a182014-10-14 08:45:50 -0700182void GrBitmapTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800183 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800184 const char text[], size_t byteLength,
185 const SkScalar pos[], int scalarsPerPosition,
186 const SkPoint& offset) {
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000187 SkASSERT(byteLength == 0 || text != NULL);
188 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
189
190 // nothing to draw
191 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) {
192 return;
193 }
194
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000195 this->init(paint, skPaint);
196
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000197 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
198
joshualitt5531d512014-12-17 15:50:11 -0800199 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &viewMatrix);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000200 SkGlyphCache* cache = autoCache.getCache();
201 GrFontScaler* fontScaler = GetGrFontScaler(cache);
202
203 // store original matrix before we reset, so we can use it to transform positions
joshualitt5531d512014-12-17 15:50:11 -0800204 SkMatrix ctm = viewMatrix;
205 if (!fPaint.localCoordChangeInverse(viewMatrix)) {
206 SkDebugf("Cannot invert viewmatrix\n");
207 }
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000208
jvanverth73f10532014-10-23 11:57:12 -0700209 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
210 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
211
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000212 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -0700213 SkTextAlignProc alignProc(fSkPaint.getTextAlign());
fmalita05c4a432014-09-29 06:29:53 -0700214 SkTextMapStateProc tmsProc(ctm, offset, scalarsPerPosition);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000215 SkFixed halfSampleX = 0, halfSampleY = 0;
216
217 if (cache->isSubpixel()) {
218 // maybe we should skip the rounding if linearText is set
219 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm);
220
221 SkFixed fxMask = ~0;
222 SkFixed fyMask = ~0;
223 if (kX_SkAxisAlignment == baseline) {
224 fyMask = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000225 halfSampleY = SK_FixedHalf;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000226 } else if (kY_SkAxisAlignment == baseline) {
227 fxMask = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000228 halfSampleX = SK_FixedHalf;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000229 }
230
231 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
232 while (text < stop) {
kkinnunencb9a2c82014-06-12 23:06:28 -0700233 SkPoint tmsLoc;
234 tmsProc(pos, &tmsLoc);
235 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + halfSampleX;
236 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + halfSampleY;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000237
238 const SkGlyph& glyph = glyphCacheProc(cache, &text,
239 fx & fxMask, fy & fyMask);
240
241 if (glyph.fWidth) {
jvanverth0fedb192014-10-08 09:07:27 -0700242 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
243 glyph.getSubXFixed(),
244 glyph.getSubYFixed()),
245 SkFixedFloorToFixed(fx),
246 SkFixedFloorToFixed(fy),
247 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000248 }
249 pos += scalarsPerPosition;
250 }
251 } else {
252 while (text < stop) {
253 const char* currentText = text;
254 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
255
256 if (metricGlyph.fWidth) {
257 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
258 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
kkinnunencb9a2c82014-06-12 23:06:28 -0700259 SkPoint tmsLoc;
260 tmsProc(pos, &tmsLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000261 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -0700262 alignProc(tmsLoc, metricGlyph, &fixedLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000263
264 SkFixed fx = fixedLoc.fX + halfSampleX;
265 SkFixed fy = fixedLoc.fY + halfSampleY;
266
267 // have to call again, now that we've been "aligned"
268 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
269 fx & fxMask, fy & fyMask);
270 // the assumption is that the metrics haven't changed
271 SkASSERT(prevAdvX == glyph.fAdvanceX);
272 SkASSERT(prevAdvY == glyph.fAdvanceY);
273 SkASSERT(glyph.fWidth);
274
jvanverth0fedb192014-10-08 09:07:27 -0700275 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
276 glyph.getSubXFixed(),
277 glyph.getSubYFixed()),
278 SkFixedFloorToFixed(fx),
279 SkFixedFloorToFixed(fy),
280 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000281 }
282 pos += scalarsPerPosition;
283 }
284 }
285 } else { // not subpixel
286
287 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
288 while (text < stop) {
289 // the last 2 parameters are ignored
290 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
291
292 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -0700293 SkPoint tmsLoc;
294 tmsProc(pos, &tmsLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000295
kkinnunencb9a2c82014-06-12 23:06:28 -0700296 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf; //halfSampleX;
297 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf; //halfSampleY;
jvanverth0fedb192014-10-08 09:07:27 -0700298 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
299 glyph.getSubXFixed(),
300 glyph.getSubYFixed()),
301 SkFixedFloorToFixed(fx),
302 SkFixedFloorToFixed(fy),
303 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000304 }
305 pos += scalarsPerPosition;
306 }
307 } else {
308 while (text < stop) {
309 // the last 2 parameters are ignored
310 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
311
312 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -0700313 SkPoint tmsLoc;
314 tmsProc(pos, &tmsLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000315
316 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -0700317 alignProc(tmsLoc, glyph, &fixedLoc);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000318
319 SkFixed fx = fixedLoc.fX + SK_FixedHalf; //halfSampleX;
320 SkFixed fy = fixedLoc.fY + SK_FixedHalf; //halfSampleY;
jvanverth0fedb192014-10-08 09:07:27 -0700321 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
322 glyph.getSubXFixed(),
323 glyph.getSubYFixed()),
324 SkFixedFloorToFixed(fx),
325 SkFixedFloorToFixed(fy),
326 fontScaler);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000327 }
328 pos += scalarsPerPosition;
329 }
330 }
331 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000332
333 this->finish();
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000334}
335
joshualitt9853cce2014-11-17 14:22:48 -0800336static size_t get_vertex_stride(GrMaskFormat maskFormat) {
337 switch (maskFormat) {
338 case kA8_GrMaskFormat:
339 return kGrayTextVASize;
340 case kARGB_GrMaskFormat:
341 return kColorTextVASize;
342 default:
343 return kLCDTextVASize;
344 }
345}
346
joshualitt9853cce2014-11-17 14:22:48 -0800347static void* alloc_vertices(GrDrawTarget* drawTarget,
348 int numVertices,
349 GrMaskFormat maskFormat) {
jvanverth73f10532014-10-23 11:57:12 -0700350 if (numVertices <= 0) {
351 return NULL;
352 }
353
354 // set up attributes
jvanverth73f10532014-10-23 11:57:12 -0700355 void* vertices = NULL;
356 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800357 get_vertex_stride(maskFormat),
jvanverth73f10532014-10-23 11:57:12 -0700358 0,
359 &vertices,
360 NULL);
361 GrAlwaysAssert(success);
362 return vertices;
363}
364
jvanverth787cdf92014-12-04 10:46:50 -0800365inline bool GrBitmapTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
366 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
367 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
368 return true;
369 }
370
371 // try to clear out an unused plot before we flush
372 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
373 fStrike->addGlyphToAtlas(glyph, scaler)) {
374 return true;
375 }
376
377 if (c_DumpFontCache) {
378#ifdef SK_DEVELOPER
379 fContext->getFontCache()->dump();
380#endif
381 }
382
383 // before we purge the cache, we must flush any accumulated draws
384 this->flush();
385 fContext->flush();
386
387 // we should have an unused plot now
388 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
389 fStrike->addGlyphToAtlas(glyph, scaler)) {
390 return true;
391 }
392
393 // we should never get here
394 SkASSERT(false);
395 }
396
397 return false;
398}
399
jvanverth0fedb192014-10-08 09:07:27 -0700400void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed,
401 SkFixed vx, SkFixed vy,
402 GrFontScaler* scaler) {
Mike Klein6a25bd02014-08-29 10:03:59 -0400403 if (NULL == fDrawTarget) {
404 return;
405 }
406
407 if (NULL == fStrike) {
408 fStrike = fContext->getFontCache()->getStrike(scaler, false);
409 }
410
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000411 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
412 if (NULL == glyph || glyph->fBounds.isEmpty()) {
413 return;
414 }
415
416 vx += SkIntToFixed(glyph->fBounds.fLeft);
417 vy += SkIntToFixed(glyph->fBounds.fTop);
418
419 // keep them as ints until we've done the clip-test
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000420 SkFixed width = glyph->fBounds.width();
421 SkFixed height = glyph->fBounds.height();
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000422
423 // check if we clipped out
424 if (true || NULL == glyph->fPlot) {
425 int x = vx >> 16;
426 int y = vy >> 16;
427 if (fClipRect.quickReject(x, y, x + width, y + height)) {
428// SkCLZ(3); // so we can set a break-point in the debugger
429 return;
430 }
431 }
432
joshualittc2625822014-12-18 16:40:54 -0800433 // If the glyph is too large we fall back to paths
jvanverth787cdf92014-12-04 10:46:50 -0800434 if (NULL == glyph->fPlot && !uploadGlyph(glyph, scaler)) {
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000435 if (NULL == glyph->fPath) {
436 SkPath* path = SkNEW(SkPath);
437 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
438 // flag the glyph as being dead?
439 delete path;
440 return;
441 }
442 glyph->fPath = path;
443 }
444
bsalomonec87dc62014-10-14 10:52:00 -0700445 // flush any accumulated draws before drawing this glyph as a path.
446 this->flush();
447
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000448 SkMatrix translate;
449 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)),
450 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop)));
joshualittc2625822014-12-18 16:40:54 -0800451 SkPath tmpPath(*glyph->fPath);
452 tmpPath.transform(translate);
egdanield58a0ba2014-06-11 10:30:05 -0700453 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
joshualittc2625822014-12-18 16:40:54 -0800454 fContext->drawPath(fPaint, SkMatrix::I(), tmpPath, strokeInfo);
jvanverth73f10532014-10-23 11:57:12 -0700455
456 // remove this glyph from the vertices we need to allocate
457 fTotalVertexCount -= kVerticesPerGlyph;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000458 return;
459 }
460
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000461 SkASSERT(glyph->fPlot);
462 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
463 glyph->fPlot->setDrawToken(drawToken);
464
465 // now promote them to fixed (TODO: Rethink using fixed pt).
466 width = SkIntToFixed(width);
467 height = SkIntToFixed(height);
468
jvanverth294c3262014-10-10 11:36:12 -0700469 // the current texture/maskformat must match what the glyph needs
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000470 GrTexture* texture = glyph->fPlot->texture();
471 SkASSERT(texture);
472
jvanverth73f10532014-10-23 11:57:12 -0700473 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fAllocVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700474 this->flush();
Mike Klein6a25bd02014-08-29 10:03:59 -0400475 fCurrTexture = texture;
476 fCurrTexture->ref();
jvanverth294c3262014-10-10 11:36:12 -0700477 fCurrMaskFormat = glyph->fMaskFormat;
Mike Klein6a25bd02014-08-29 10:03:59 -0400478 }
479
Mike Klein6a25bd02014-08-29 10:03:59 -0400480 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700481 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
482 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
egdanieled3af662014-10-31 06:55:45 -0700483 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, fCurrMaskFormat);
Mike Klein6a25bd02014-08-29 10:03:59 -0400484 }
485
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000486 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
487 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000488
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000489 SkRect r;
490 r.fLeft = SkFixedToFloat(vx);
491 r.fTop = SkFixedToFloat(vy);
492 r.fRight = SkFixedToFloat(vx + width);
493 r.fBottom = SkFixedToFloat(vy + height);
494
reed10d03272014-10-01 09:24:12 -0700495 fVertexBounds.joinNonEmptyArg(r);
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000496
joshualitt9853cce2014-11-17 14:22:48 -0800497 size_t vertSize = get_vertex_stride(fCurrMaskFormat);
bsalomon594069f2014-06-06 06:16:34 -0700498
499 SkPoint* positions = reinterpret_cast<SkPoint*>(
500 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
501 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
502
503 // The texture coords are last in both the with and without color vertex layouts.
504 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
505 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
bsalomonafbf2d62014-09-30 12:18:44 -0700506 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
507 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
508 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + width)),
509 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + height)),
bsalomon594069f2014-06-06 06:16:34 -0700510 vertSize);
egdanieled3af662014-10-31 06:55:45 -0700511 if (kA8_GrMaskFormat == fCurrMaskFormat) {
bsalomon594069f2014-06-06 06:16:34 -0700512 // color comes after position.
513 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
514 for (int i = 0; i < 4; ++i) {
515 *colors = fPaint.getColor();
516 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
517 }
518 }
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +0000519 fCurrVertex += 4;
520}
jvanverth0fedb192014-10-08 09:07:27 -0700521
jvanverth0fedb192014-10-08 09:07:27 -0700522void GrBitmapTextContext::flush() {
523 if (NULL == fDrawTarget) {
524 return;
525 }
526
jvanverth0fedb192014-10-08 09:07:27 -0700527 if (fCurrVertex > 0) {
joshualitt780b11e2014-11-18 09:40:40 -0800528 GrDrawState drawState;
529 drawState.setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget());
530
jvanverth0fedb192014-10-08 09:07:27 -0700531 // setup our sampler state for our text texture/atlas
532 SkASSERT(SkIsAlign4(fCurrVertex));
533 SkASSERT(fCurrTexture);
jvanverth0fedb192014-10-08 09:07:27 -0700534
jvanverth0fedb192014-10-08 09:07:27 -0700535 SkASSERT(fStrike);
joshualitt2e3b3e32014-12-09 13:31:14 -0800536 GrColor color = fPaint.getColor();
jvanverth294c3262014-10-10 11:36:12 -0700537 switch (fCurrMaskFormat) {
jvanverth0fedb192014-10-08 09:07:27 -0700538 // Color bitmap text
joshualitt2e3b3e32014-12-09 13:31:14 -0800539 case kARGB_GrMaskFormat: {
540 int a = fSkPaint.getAlpha();
541 color = SkColorSetARGB(a, a, a, a);
jvanverth0fedb192014-10-08 09:07:27 -0700542 break;
joshualitt2e3b3e32014-12-09 13:31:14 -0800543 }
jvanverth0fedb192014-10-08 09:07:27 -0700544 // LCD text
jvanverth0fedb192014-10-08 09:07:27 -0700545 case kA565_GrMaskFormat: {
egdaniel378092f2014-12-03 10:40:13 -0800546 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
547 // processor if the xp can support it. For now we will simply assume that if
548 // fUseLCDText is true, then we have a known color output.
549 if (!drawState.getXPFactory()->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700550 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700551 }
jvanverth0fedb192014-10-08 09:07:27 -0700552 break;
553 }
554 // Grayscale/BW text
555 case kA8_GrMaskFormat:
jvanverth0fedb192014-10-08 09:07:27 -0700556 break;
557 default:
jvanverth294c3262014-10-10 11:36:12 -0700558 SkFAIL("Unexpected mask format.");
jvanverth0fedb192014-10-08 09:07:27 -0700559 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800560
561 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNone_FilterMode);
562 // TODO cache these GPs
563 if (kARGB_GrMaskFormat == fCurrMaskFormat) {
564 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
565 if (textureUniqueID != fEffectTextureUniqueID ||
joshualitt56995b52014-12-11 15:44:02 -0800566 fCachedGeometryProcessor->color() != color) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800567 uint32_t flags = GrDefaultGeoProcFactory::kLocalCoord_GPType;
568 fCachedGeometryProcessor.reset(GrDefaultGeoProcFactory::Create(color, flags));
569 fCachedTextureProcessor.reset(GrSimpleTextureEffect::Create(fCurrTexture,
570 SkMatrix::I(),
571 params));
572 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800573 drawState.addColorProcessor(fCachedTextureProcessor.get());
574 } else {
575 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
576 if (textureUniqueID != fEffectTextureUniqueID ||
joshualitt56995b52014-12-11 15:44:02 -0800577 fCachedGeometryProcessor->color() != color) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800578 bool hasColor = kA8_GrMaskFormat == fCurrMaskFormat;
joshualitt56995b52014-12-11 15:44:02 -0800579 bool opaqueVertexColors = GrColorIsOpaque(fPaint.getColor());
joshualitt2e3b3e32014-12-09 13:31:14 -0800580 fCachedGeometryProcessor.reset(GrBitmapTextGeoProc::Create(color,
joshualitt56995b52014-12-11 15:44:02 -0800581 fCurrTexture,
582 params,
583 hasColor,
584 opaqueVertexColors));
joshualitt2e3b3e32014-12-09 13:31:14 -0800585 fEffectTextureUniqueID = textureUniqueID;
586 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800587 }
588
jvanverth73f10532014-10-23 11:57:12 -0700589 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700590 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800591 fDrawTarget->drawIndexedInstances(&drawState,
joshualitt56995b52014-12-11 15:44:02 -0800592 fCachedGeometryProcessor.get(),
joshualitt9853cce2014-11-17 14:22:48 -0800593 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700594 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800595 kVerticesPerGlyph,
596 kIndicesPerGlyph,
597 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700598
599 fDrawTarget->resetVertexSource();
600 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700601 fAllocVertexCount = 0;
602 // reset to be those that are left
603 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700604 fCurrVertex = 0;
605 fVertexBounds.setLargestInverted();
606 SkSafeSetNull(fCurrTexture);
607 }
608}
609
610inline void GrBitmapTextContext::finish() {
611 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700612 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700613
614 GrTextContext::finish();
615}
616