blob: ca6b99ea336312d51282339c547b82575ae12f64 [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
halcanary9d524f22016-03-29 09:03:52 -070046void GrAtlasTextBatch::computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080047 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
bsalomon342bfc22016-04-01 06:06:20 -0700100 FlushInfo flushInfo;
joshualittd9d30f72015-12-08 10:47:55 -0800101 if (this->usesDistanceFields()) {
bsalomon342bfc22016-04-01 06:06:20 -0700102 flushInfo.fGeometryProcessor.reset(
103 this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(), texture));
joshualitta751c972015-11-20 13:37:32 -0800104 } else {
105 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
bsalomon342bfc22016-04-01 06:06:20 -0700106 flushInfo.fGeometryProcessor.reset(
107 GrBitmapTextGeoProc::Create(this->color(),
108 texture,
109 params,
110 maskFormat,
111 localMatrix,
112 this->usesLocalCoords()));
joshualitta751c972015-11-20 13:37:32 -0800113 }
114
joshualitta751c972015-11-20 13:37:32 -0800115 flushInfo.fGlyphsToFlush = 0;
bsalomon342bfc22016-04-01 06:06:20 -0700116 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
joshualittf528e0d2015-12-09 06:42:52 -0800117 SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat));
joshualitta751c972015-11-20 13:37:32 -0800118
joshualitta751c972015-11-20 13:37:32 -0800119 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700120 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800121
122 void* vertices = target->makeVertexSpace(vertexStride,
123 glyphCount * kVerticesPerGlyph,
124 &vertexBuffer,
125 &flushInfo.fVertexOffset);
126 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
127 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
128 if (!vertices || !flushInfo.fVertexBuffer) {
129 SkDebugf("Could not allocate vertices\n");
130 return;
131 }
132
133 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
134
135 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
136 // in a row
137 const SkDescriptor* desc = nullptr;
138 SkGlyphCache* cache = nullptr;
139 GrFontScaler* scaler = nullptr;
140 SkTypeface* typeface = nullptr;
141
bsalomon342bfc22016-04-01 06:06:20 -0700142 GrBlobRegenHelper helper(this, target, &flushInfo);
joshualittddd22d82016-02-16 06:47:52 -0800143
joshualitta751c972015-11-20 13:37:32 -0800144 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800145 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800146 Blob* blob = args.fBlob;
joshualittddd22d82016-02-16 06:47:52 -0800147 size_t byteCount;
148 void* blobVertices;
joshualitt8e0ef292016-02-19 14:13:03 -0800149 int subRunGlyphCount;
joshualittddd22d82016-02-16 06:47:52 -0800150 blob->regenInBatch(target, fFontCache, &helper, args.fRun, args.fSubRun, &cache,
joshualitt8e0ef292016-02-19 14:13:03 -0800151 &typeface, &scaler, &desc, vertexStride, args.fViewMatrix, args.fX,
152 args.fY, args.fColor, &blobVertices, &byteCount, &subRunGlyphCount);
joshualitta751c972015-11-20 13:37:32 -0800153
154 // now copy all vertices
joshualittddd22d82016-02-16 06:47:52 -0800155 memcpy(currVertex, blobVertices, byteCount);
joshualitta751c972015-11-20 13:37:32 -0800156
joshualitt7481e752016-01-22 06:08:48 -0800157#ifdef SK_DEBUG
158 // bounds sanity check
159 SkRect rect;
160 rect.setLargestInverted();
joshualittddd22d82016-02-16 06:47:52 -0800161 SkPoint* vertex = (SkPoint*) ((char*)blobVertices);
joshualitt8e0ef292016-02-19 14:13:03 -0800162 rect.growToInclude(vertex, vertexStride, kVerticesPerGlyph * subRunGlyphCount);
joshualitt7481e752016-01-22 06:08:48 -0800163
164 if (this->usesDistanceFields()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800165 args.fViewMatrix.mapRect(&rect);
joshualitt7481e752016-01-22 06:08:48 -0800166 }
167 SkASSERT(fBounds.contains(rect));
168#endif
169
joshualitta751c972015-11-20 13:37:32 -0800170 currVertex += byteCount;
171 }
joshualitt60ce86d2015-11-23 13:08:22 -0800172
joshualitta751c972015-11-20 13:37:32 -0800173 // Make sure to attach the last cache if applicable
174 if (cache) {
175 SkGlyphCache::AttachCache(cache);
176 }
177 this->flush(target, &flushInfo);
178}
179
joshualitt144c3c82015-11-30 12:30:13 -0800180void GrAtlasTextBatch::flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const {
egdaniel0e1853c2016-03-17 11:35:45 -0700181 GrMesh mesh;
cdalton397536c2016-03-25 12:15:03 -0700182 int maxGlyphsPerDraw =
183 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
egdaniel0e1853c2016-03-17 11:35:45 -0700184 mesh.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
185 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
186 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
187 maxGlyphsPerDraw);
bsalomon342bfc22016-04-01 06:06:20 -0700188 target->draw(flushInfo->fGeometryProcessor, mesh);
joshualitta751c972015-11-20 13:37:32 -0800189 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
190 flushInfo->fGlyphsToFlush = 0;
191}
192
193bool GrAtlasTextBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
194 GrAtlasTextBatch* that = t->cast<GrAtlasTextBatch>();
195 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
196 that->bounds(), caps)) {
197 return false;
198 }
199
200 if (fMaskType != that->fMaskType) {
201 return false;
202 }
203
204 if (!this->usesDistanceFields()) {
joshualittd9d30f72015-12-08 10:47:55 -0800205 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
joshualitta751c972015-11-20 13:37:32 -0800206 return false;
207 }
208 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
209 return false;
210 }
211 } else {
212 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
213 return false;
214 }
215
216 if (fFilteredColor != that->fFilteredColor) {
217 return false;
218 }
219
220 if (fUseBGR != that->fUseBGR) {
221 return false;
222 }
joshualitta751c972015-11-20 13:37:32 -0800223 }
224
225 fBatch.fNumGlyphs += that->numGlyphs();
226
227 // Reallocate space for geo data if necessary and then import that's geo data.
228 int newGeoCount = that->fGeoCount + fGeoCount;
229 // We assume (and here enforce) that the allocation size is the smallest power of two that
230 // is greater than or equal to the number of geometries (and at least
231 // kMinGeometryAllocated).
232 int newAllocSize = GrNextPow2(newGeoCount);
233 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
234
235 if (newGeoCount > currAllocSize) {
236 fGeoData.realloc(newAllocSize);
237 }
238
239 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
240 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
241 // it doesn't try to unref them.
242#ifdef SK_DEBUG
243 for (int i = 0; i < that->fGeoCount; ++i) {
244 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
245 }
246#endif
247 that->fGeoCount = 0;
248 fGeoCount = newGeoCount;
249
250 this->joinBounds(that->bounds());
251 return true;
252}
253
254// TODO just use class params
255// TODO trying to figure out why lcd is so whack
256GrGeometryProcessor* GrAtlasTextBatch::setupDfProcessor(const SkMatrix& viewMatrix,
257 SkColor filteredColor,
joshualitt144c3c82015-11-30 12:30:13 -0800258 GrColor color, GrTexture* texture) const {
joshualitta751c972015-11-20 13:37:32 -0800259 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
260 bool isLCD = this->isLCD();
261 // set up any flags
262 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
jvanverthcf371bb2016-03-10 11:10:43 -0800263 flags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
joshualitta751c972015-11-20 13:37:32 -0800264
265 // see if we need to create a new effect
266 if (isLCD) {
267 flags |= kUseLCD_DistanceFieldEffectFlag;
joshualitta751c972015-11-20 13:37:32 -0800268 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
joshualitta751c972015-11-20 13:37:32 -0800269
270 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
271
272 float redCorrection =
273 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
274 float greenCorrection =
275 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
276 float blueCorrection =
277 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
278 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
279 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
280 greenCorrection,
281 blueCorrection);
282
283 return GrDistanceFieldLCDTextGeoProc::Create(color,
284 viewMatrix,
285 texture,
286 params,
287 widthAdjust,
288 flags,
289 this->usesLocalCoords());
290 } else {
joshualitta751c972015-11-20 13:37:32 -0800291#ifdef SK_GAMMA_APPLY_TO_A8
292 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
293 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
294 return GrDistanceFieldA8TextGeoProc::Create(color,
295 viewMatrix,
296 texture,
297 params,
298 correction,
299 flags,
300 this->usesLocalCoords());
301#else
302 return GrDistanceFieldA8TextGeoProc::Create(color,
303 viewMatrix,
304 texture,
305 params,
306 flags,
307 this->usesLocalCoords());
308#endif
309 }
310
311}
joshualittddd22d82016-02-16 06:47:52 -0800312
313void GrBlobRegenHelper::flush() {
314 fBatch->flush(fTarget, fFlushInfo);
joshualittddd22d82016-02-16 06:47:52 -0800315}