blob: 654643d3399e33308011be09b474a3d78e39c933 [file] [log] [blame]
joshualitta751c972015-11-20 13:37:32 -08001/*
2 * Copyright 2015 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 "GrAtlasTextBatch.h"
9
joshualitta751c972015-11-20 13:37:32 -080010#include "GrBatchFlushState.h"
joshualitta751c972015-11-20 13:37:32 -080011#include "GrResourceProvider.h"
12
joshualitta751c972015-11-20 13:37:32 -080013#include "SkGlyphCache.h"
14
15#include "effects/GrBitmapTextGeoProc.h"
16#include "effects/GrDistanceFieldGeoProc.h"
joshualitte8042922015-12-11 06:11:21 -080017#include "text/GrBatchFontCache.h"
joshualitta751c972015-11-20 13:37:32 -080018
joshualitt60ce86d2015-11-23 13:08:22 -080019///////////////////////////////////////////////////////////////////////////////////////////////////
20
joshualitta751c972015-11-20 13:37:32 -080021static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
22 unsigned r = SkColorGetR(c);
23 unsigned g = SkColorGetG(c);
24 unsigned b = SkColorGetB(c);
25 return GrColorPackRGBA(r, g, b, 0xff);
26}
27
28static const int kDistanceAdjustLumShift = 5;
29
30SkString GrAtlasTextBatch::dumpInfo() const {
31 SkString str;
32
33 for (int i = 0; i < fGeoCount; ++i) {
34 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
35 i,
36 fGeoData[i].fColor,
joshualitt8e0ef292016-02-19 14:13:03 -080037 fGeoData[i].fX,
38 fGeoData[i].fY,
joshualittddd22d82016-02-16 06:47:52 -080039 fGeoData[i].fBlob->runCount());
joshualitta751c972015-11-20 13:37:32 -080040 }
41
42 str.append(INHERITED::dumpInfo());
43 return str;
44}
45
ethannicholasff210322015-11-24 12:10:10 -080046void GrAtlasTextBatch::computePipelineOptimizations(GrInitInvariantOutput* color,
47 GrInitInvariantOutput* coverage,
48 GrBatchToXPOverrides* overrides) const {
joshualitta751c972015-11-20 13:37:32 -080049 if (kColorBitmapMask_MaskType == fMaskType) {
ethannicholasff210322015-11-24 12:10:10 -080050 color->setUnknownFourComponents();
joshualitta751c972015-11-20 13:37:32 -080051 } else {
ethannicholasff210322015-11-24 12:10:10 -080052 color->setKnownFourComponents(fBatch.fColor);
joshualitta751c972015-11-20 13:37:32 -080053 }
joshualitta751c972015-11-20 13:37:32 -080054 switch (fMaskType) {
55 case kGrayscaleDistanceField_MaskType:
56 case kGrayscaleCoverageMask_MaskType:
ethannicholasff210322015-11-24 12:10:10 -080057 coverage->setUnknownSingleComponent();
joshualitta751c972015-11-20 13:37:32 -080058 break;
59 case kLCDCoverageMask_MaskType:
60 case kLCDDistanceField_MaskType:
ethannicholasff210322015-11-24 12:10:10 -080061 coverage->setUnknownOpaqueFourComponents();
62 coverage->setUsingLCDCoverage();
joshualitta751c972015-11-20 13:37:32 -080063 break;
64 case kColorBitmapMask_MaskType:
ethannicholasff210322015-11-24 12:10:10 -080065 coverage->setKnownSingleComponent(0xff);
joshualitta751c972015-11-20 13:37:32 -080066 }
67}
68
ethannicholasff210322015-11-24 12:10:10 -080069void GrAtlasTextBatch::initBatchTracker(const GrXPOverridesForBatch& overrides) {
joshualitta751c972015-11-20 13:37:32 -080070 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -080071 if (!overrides.readsColor()) {
joshualitta751c972015-11-20 13:37:32 -080072 fGeoData[0].fColor = GrColor_ILLEGAL;
73 }
ethannicholasff210322015-11-24 12:10:10 -080074 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitta751c972015-11-20 13:37:32 -080075
76 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -080077 fBatch.fColorIgnored = !overrides.readsColor();
joshualitta751c972015-11-20 13:37:32 -080078 fBatch.fColor = fGeoData[0].fColor;
ethannicholasff210322015-11-24 12:10:10 -080079 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
80 fBatch.fCoverageIgnored = !overrides.readsCoverage();
joshualitta751c972015-11-20 13:37:32 -080081}
82
joshualitt144c3c82015-11-30 12:30:13 -080083void GrAtlasTextBatch::onPrepareDraws(Target* target) const {
joshualitta751c972015-11-20 13:37:32 -080084 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
85 // TODO actually only invert if we don't have RGBA
86 SkMatrix localMatrix;
87 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
88 SkDebugf("Cannot invert viewmatrix\n");
89 return;
90 }
91
92 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
93 if (!texture) {
94 SkDebugf("Could not allocate backing texture for atlas\n");
95 return;
96 }
97
joshualitta751c972015-11-20 13:37:32 -080098 GrMaskFormat maskFormat = this->maskFormat();
joshualitta751c972015-11-20 13:37:32 -080099
100 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualittd9d30f72015-12-08 10:47:55 -0800101 if (this->usesDistanceFields()) {
joshualitta751c972015-11-20 13:37:32 -0800102 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
103 texture));
104 } else {
105 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
106 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
107 texture,
108 params,
109 maskFormat,
110 localMatrix,
111 this->usesLocalCoords()));
112 }
113
114 FlushInfo flushInfo;
115 flushInfo.fGlyphsToFlush = 0;
116 size_t vertexStride = gp->getVertexStride();
joshualittf528e0d2015-12-09 06:42:52 -0800117 SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat));
joshualitta751c972015-11-20 13:37:32 -0800118
egdaniel0e1853c2016-03-17 11:35:45 -0700119 target->initDraw(gp);
joshualitta751c972015-11-20 13:37:32 -0800120
121 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700122 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800123
124 void* vertices = target->makeVertexSpace(vertexStride,
125 glyphCount * kVerticesPerGlyph,
126 &vertexBuffer,
127 &flushInfo.fVertexOffset);
128 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
129 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
130 if (!vertices || !flushInfo.fVertexBuffer) {
131 SkDebugf("Could not allocate vertices\n");
132 return;
133 }
134
135 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
136
137 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
138 // in a row
139 const SkDescriptor* desc = nullptr;
140 SkGlyphCache* cache = nullptr;
141 GrFontScaler* scaler = nullptr;
142 SkTypeface* typeface = nullptr;
143
joshualittddd22d82016-02-16 06:47:52 -0800144 GrBlobRegenHelper helper(this, target, &flushInfo, gp);
145
joshualitta751c972015-11-20 13:37:32 -0800146 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800147 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800148 Blob* blob = args.fBlob;
joshualittddd22d82016-02-16 06:47:52 -0800149 size_t byteCount;
150 void* blobVertices;
joshualitt8e0ef292016-02-19 14:13:03 -0800151 int subRunGlyphCount;
joshualittddd22d82016-02-16 06:47:52 -0800152 blob->regenInBatch(target, fFontCache, &helper, args.fRun, args.fSubRun, &cache,
joshualitt8e0ef292016-02-19 14:13:03 -0800153 &typeface, &scaler, &desc, vertexStride, args.fViewMatrix, args.fX,
154 args.fY, args.fColor, &blobVertices, &byteCount, &subRunGlyphCount);
joshualitta751c972015-11-20 13:37:32 -0800155
156 // now copy all vertices
joshualittddd22d82016-02-16 06:47:52 -0800157 memcpy(currVertex, blobVertices, byteCount);
joshualitta751c972015-11-20 13:37:32 -0800158
joshualitt7481e752016-01-22 06:08:48 -0800159#ifdef SK_DEBUG
160 // bounds sanity check
161 SkRect rect;
162 rect.setLargestInverted();
joshualittddd22d82016-02-16 06:47:52 -0800163 SkPoint* vertex = (SkPoint*) ((char*)blobVertices);
joshualitt8e0ef292016-02-19 14:13:03 -0800164 rect.growToInclude(vertex, vertexStride, kVerticesPerGlyph * subRunGlyphCount);
joshualitt7481e752016-01-22 06:08:48 -0800165
166 if (this->usesDistanceFields()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800167 args.fViewMatrix.mapRect(&rect);
joshualitt7481e752016-01-22 06:08:48 -0800168 }
169 SkASSERT(fBounds.contains(rect));
170#endif
171
joshualitta751c972015-11-20 13:37:32 -0800172 currVertex += byteCount;
173 }
joshualitt60ce86d2015-11-23 13:08:22 -0800174
joshualitta751c972015-11-20 13:37:32 -0800175 // Make sure to attach the last cache if applicable
176 if (cache) {
177 SkGlyphCache::AttachCache(cache);
178 }
179 this->flush(target, &flushInfo);
180}
181
joshualitt144c3c82015-11-30 12:30:13 -0800182void GrAtlasTextBatch::flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const {
egdaniel0e1853c2016-03-17 11:35:45 -0700183 GrMesh mesh;
cdalton397536c2016-03-25 12:15:03 -0700184 int maxGlyphsPerDraw =
185 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
egdaniel0e1853c2016-03-17 11:35:45 -0700186 mesh.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
187 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
188 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
189 maxGlyphsPerDraw);
190 target->draw(mesh);
joshualitta751c972015-11-20 13:37:32 -0800191 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
192 flushInfo->fGlyphsToFlush = 0;
193}
194
195bool GrAtlasTextBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
196 GrAtlasTextBatch* that = t->cast<GrAtlasTextBatch>();
197 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
198 that->bounds(), caps)) {
199 return false;
200 }
201
202 if (fMaskType != that->fMaskType) {
203 return false;
204 }
205
206 if (!this->usesDistanceFields()) {
joshualittd9d30f72015-12-08 10:47:55 -0800207 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
joshualitta751c972015-11-20 13:37:32 -0800208 return false;
209 }
210 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
211 return false;
212 }
213 } else {
214 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
215 return false;
216 }
217
218 if (fFilteredColor != that->fFilteredColor) {
219 return false;
220 }
221
222 if (fUseBGR != that->fUseBGR) {
223 return false;
224 }
joshualitta751c972015-11-20 13:37:32 -0800225 }
226
227 fBatch.fNumGlyphs += that->numGlyphs();
228
229 // Reallocate space for geo data if necessary and then import that's geo data.
230 int newGeoCount = that->fGeoCount + fGeoCount;
231 // We assume (and here enforce) that the allocation size is the smallest power of two that
232 // is greater than or equal to the number of geometries (and at least
233 // kMinGeometryAllocated).
234 int newAllocSize = GrNextPow2(newGeoCount);
235 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
236
237 if (newGeoCount > currAllocSize) {
238 fGeoData.realloc(newAllocSize);
239 }
240
241 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
242 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
243 // it doesn't try to unref them.
244#ifdef SK_DEBUG
245 for (int i = 0; i < that->fGeoCount; ++i) {
246 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
247 }
248#endif
249 that->fGeoCount = 0;
250 fGeoCount = newGeoCount;
251
252 this->joinBounds(that->bounds());
253 return true;
254}
255
256// TODO just use class params
257// TODO trying to figure out why lcd is so whack
258GrGeometryProcessor* GrAtlasTextBatch::setupDfProcessor(const SkMatrix& viewMatrix,
259 SkColor filteredColor,
joshualitt144c3c82015-11-30 12:30:13 -0800260 GrColor color, GrTexture* texture) const {
joshualitta751c972015-11-20 13:37:32 -0800261 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
262 bool isLCD = this->isLCD();
263 // set up any flags
264 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
jvanverthcf371bb2016-03-10 11:10:43 -0800265 flags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
joshualitta751c972015-11-20 13:37:32 -0800266
267 // see if we need to create a new effect
268 if (isLCD) {
269 flags |= kUseLCD_DistanceFieldEffectFlag;
joshualitta751c972015-11-20 13:37:32 -0800270 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
joshualitta751c972015-11-20 13:37:32 -0800271
272 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
273
274 float redCorrection =
275 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
276 float greenCorrection =
277 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
278 float blueCorrection =
279 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
280 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
281 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
282 greenCorrection,
283 blueCorrection);
284
285 return GrDistanceFieldLCDTextGeoProc::Create(color,
286 viewMatrix,
287 texture,
288 params,
289 widthAdjust,
290 flags,
291 this->usesLocalCoords());
292 } else {
joshualitta751c972015-11-20 13:37:32 -0800293#ifdef SK_GAMMA_APPLY_TO_A8
294 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
295 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
296 return GrDistanceFieldA8TextGeoProc::Create(color,
297 viewMatrix,
298 texture,
299 params,
300 correction,
301 flags,
302 this->usesLocalCoords());
303#else
304 return GrDistanceFieldA8TextGeoProc::Create(color,
305 viewMatrix,
306 texture,
307 params,
308 flags,
309 this->usesLocalCoords());
310#endif
311 }
312
313}
joshualittddd22d82016-02-16 06:47:52 -0800314
315void GrBlobRegenHelper::flush() {
316 fBatch->flush(fTarget, fFlushInfo);
egdaniel0e1853c2016-03-17 11:35:45 -0700317 fTarget->initDraw(fGP);
joshualittddd22d82016-02-16 06:47:52 -0800318}