blob: bdf69861a4a8fa97e831a3513d043793906b734d [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 }
bsalomon33b6b8e2016-04-13 08:14:22 -0700167 // Allow for small numerical error in the bounds.
168 SkRect bounds = fBounds;
169 bounds.outset(0.001f, 0.001f);
170 SkASSERT(bounds.contains(rect));
joshualitt7481e752016-01-22 06:08:48 -0800171#endif
172
joshualitta751c972015-11-20 13:37:32 -0800173 currVertex += byteCount;
174 }
joshualitt60ce86d2015-11-23 13:08:22 -0800175
joshualitta751c972015-11-20 13:37:32 -0800176 // Make sure to attach the last cache if applicable
177 if (cache) {
178 SkGlyphCache::AttachCache(cache);
179 }
180 this->flush(target, &flushInfo);
181}
182
joshualitt144c3c82015-11-30 12:30:13 -0800183void GrAtlasTextBatch::flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const {
egdaniel0e1853c2016-03-17 11:35:45 -0700184 GrMesh mesh;
cdalton397536c2016-03-25 12:15:03 -0700185 int maxGlyphsPerDraw =
186 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
egdaniel0e1853c2016-03-17 11:35:45 -0700187 mesh.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
188 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
189 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
190 maxGlyphsPerDraw);
bsalomon342bfc22016-04-01 06:06:20 -0700191 target->draw(flushInfo->fGeometryProcessor, mesh);
joshualitta751c972015-11-20 13:37:32 -0800192 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
193 flushInfo->fGlyphsToFlush = 0;
194}
195
196bool GrAtlasTextBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
197 GrAtlasTextBatch* that = t->cast<GrAtlasTextBatch>();
198 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
199 that->bounds(), caps)) {
200 return false;
201 }
202
203 if (fMaskType != that->fMaskType) {
204 return false;
205 }
206
207 if (!this->usesDistanceFields()) {
joshualittd9d30f72015-12-08 10:47:55 -0800208 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
joshualitta751c972015-11-20 13:37:32 -0800209 return false;
210 }
211 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
212 return false;
213 }
214 } else {
215 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
216 return false;
217 }
218
219 if (fFilteredColor != that->fFilteredColor) {
220 return false;
221 }
222
223 if (fUseBGR != that->fUseBGR) {
224 return false;
225 }
joshualitta751c972015-11-20 13:37:32 -0800226 }
227
228 fBatch.fNumGlyphs += that->numGlyphs();
229
230 // Reallocate space for geo data if necessary and then import that's geo data.
231 int newGeoCount = that->fGeoCount + fGeoCount;
232 // We assume (and here enforce) that the allocation size is the smallest power of two that
233 // is greater than or equal to the number of geometries (and at least
234 // kMinGeometryAllocated).
235 int newAllocSize = GrNextPow2(newGeoCount);
236 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
237
238 if (newGeoCount > currAllocSize) {
239 fGeoData.realloc(newAllocSize);
240 }
241
242 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
243 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
244 // it doesn't try to unref them.
245#ifdef SK_DEBUG
246 for (int i = 0; i < that->fGeoCount; ++i) {
247 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
248 }
249#endif
250 that->fGeoCount = 0;
251 fGeoCount = newGeoCount;
252
253 this->joinBounds(that->bounds());
254 return true;
255}
256
257// TODO just use class params
258// TODO trying to figure out why lcd is so whack
259GrGeometryProcessor* GrAtlasTextBatch::setupDfProcessor(const SkMatrix& viewMatrix,
260 SkColor filteredColor,
joshualitt144c3c82015-11-30 12:30:13 -0800261 GrColor color, GrTexture* texture) const {
joshualitta751c972015-11-20 13:37:32 -0800262 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
263 bool isLCD = this->isLCD();
264 // set up any flags
265 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
jvanverthcf371bb2016-03-10 11:10:43 -0800266 flags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
brianosmanb461d342016-04-13 13:10:14 -0700267 flags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
joshualitta751c972015-11-20 13:37:32 -0800268
269 // see if we need to create a new effect
270 if (isLCD) {
271 flags |= kUseLCD_DistanceFieldEffectFlag;
joshualitta751c972015-11-20 13:37:32 -0800272 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
joshualitta751c972015-11-20 13:37:32 -0800273
274 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
275
brianosman0586f5c2016-04-12 12:48:21 -0700276 float redCorrection = fDistanceAdjustTable->getAdjustment(
brianosmanb461d342016-04-13 13:10:14 -0700277 GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift,
278 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700279 float greenCorrection = fDistanceAdjustTable->getAdjustment(
brianosmanb461d342016-04-13 13:10:14 -0700280 GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift,
281 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700282 float blueCorrection = fDistanceAdjustTable->getAdjustment(
brianosmanb461d342016-04-13 13:10:14 -0700283 GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift,
284 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800285 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
286 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
287 greenCorrection,
288 blueCorrection);
289
290 return GrDistanceFieldLCDTextGeoProc::Create(color,
291 viewMatrix,
292 texture,
293 params,
294 widthAdjust,
295 flags,
296 this->usesLocalCoords());
297 } else {
joshualitta751c972015-11-20 13:37:32 -0800298#ifdef SK_GAMMA_APPLY_TO_A8
299 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
brianosman0586f5c2016-04-12 12:48:21 -0700300 float correction = fDistanceAdjustTable->getAdjustment(
brianosmanb461d342016-04-13 13:10:14 -0700301 lum >> kDistanceAdjustLumShift, fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800302 return GrDistanceFieldA8TextGeoProc::Create(color,
303 viewMatrix,
304 texture,
305 params,
306 correction,
307 flags,
308 this->usesLocalCoords());
309#else
310 return GrDistanceFieldA8TextGeoProc::Create(color,
311 viewMatrix,
312 texture,
313 params,
314 flags,
315 this->usesLocalCoords());
316#endif
317 }
318
319}
joshualittddd22d82016-02-16 06:47:52 -0800320
321void GrBlobRegenHelper::flush() {
322 fBatch->flush(fTarget, fFlushInfo);
joshualittddd22d82016-02-16 06:47:52 -0800323}