• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "src/gpu/ops/GrAtlasTextOp.h"
9 
10 #include "include/core/SkPoint3.h"
11 #include "include/private/GrRecordingContext.h"
12 #include "src/core/SkMathPriv.h"
13 #include "src/core/SkMatrixPriv.h"
14 #include "src/core/SkStrikeCache.h"
15 #include "src/gpu/GrCaps.h"
16 #include "src/gpu/GrMemoryPool.h"
17 #include "src/gpu/GrOpFlushState.h"
18 #include "src/gpu/GrRecordingContextPriv.h"
19 #include "src/gpu/GrResourceProvider.h"
20 #include "src/gpu/effects/GrBitmapTextGeoProc.h"
21 #include "src/gpu/effects/GrDistanceFieldGeoProc.h"
22 #include "src/gpu/text/GrAtlasManager.h"
23 #include "src/gpu/text/GrStrikeCache.h"
24 
25 ///////////////////////////////////////////////////////////////////////////////////////////////////
26 
MakeBitmap(GrRecordingContext * context,GrPaint && paint,GrMaskFormat maskFormat,int glyphCount,bool needsTransform)27 std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
28                                                          GrPaint&& paint,
29                                                          GrMaskFormat maskFormat,
30                                                          int glyphCount,
31                                                          bool needsTransform) {
32         GrOpMemoryPool* pool = context->priv().opMemoryPool();
33 
34         std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
35 
36         switch (maskFormat) {
37             case kA8_GrMaskFormat:
38                 op->fMaskType = kGrayscaleCoverageMask_MaskType;
39                 break;
40             case kA565_GrMaskFormat:
41                 op->fMaskType = kLCDCoverageMask_MaskType;
42                 break;
43             case kARGB_GrMaskFormat:
44                 op->fMaskType = kColorBitmapMask_MaskType;
45                 break;
46         }
47         op->fNumGlyphs = glyphCount;
48         op->fGeoCount = 1;
49         op->fLuminanceColor = 0;
50         op->fNeedsGlyphTransform = needsTransform;
51         return op;
52     }
53 
MakeDistanceField(GrRecordingContext * context,GrPaint && paint,int glyphCount,const GrDistanceFieldAdjustTable * distanceAdjustTable,bool useGammaCorrectDistanceTable,SkColor luminanceColor,const SkSurfaceProps & props,bool isAntiAliased,bool useLCD)54 std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
55                                             GrRecordingContext* context,
56                                             GrPaint&& paint,
57                                             int glyphCount,
58                                             const GrDistanceFieldAdjustTable* distanceAdjustTable,
59                                             bool useGammaCorrectDistanceTable,
60                                             SkColor luminanceColor,
61                                             const SkSurfaceProps& props,
62                                             bool isAntiAliased,
63                                             bool useLCD) {
64         GrOpMemoryPool* pool = context->priv().opMemoryPool();
65 
66         std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
67 
68         bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
69         bool isLCD = useLCD && SkPixelGeometryIsH(props.pixelGeometry());
70         op->fMaskType = !isAntiAliased ? kAliasedDistanceField_MaskType
71                                        : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
72                                                         : kLCDDistanceField_MaskType)
73                                                : kGrayscaleDistanceField_MaskType;
74         op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
75         op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable;
76         op->fLuminanceColor = luminanceColor;
77         op->fNumGlyphs = glyphCount;
78         op->fGeoCount = 1;
79         return op;
80     }
81 
82 static const int kDistanceAdjustLumShift = 5;
83 
init()84 void GrAtlasTextOp::init() {
85     const Geometry& geo = fGeoData[0];
86     if (this->usesDistanceFields()) {
87         bool isLCD = this->isLCD();
88 
89         const SkMatrix& viewMatrix = geo.fViewMatrix;
90 
91         fDFGPFlags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
92         fDFGPFlags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
93         fDFGPFlags |= viewMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
94         fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
95         fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
96                               ? kAliased_DistanceFieldEffectFlag
97                               : 0;
98 
99         if (isLCD) {
100             fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
101             fDFGPFlags |=
102                     (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
103         }
104 
105         fNeedsGlyphTransform = true;
106     }
107 
108     SkRect bounds;
109     geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, geo.fY,
110                                    fNeedsGlyphTransform);
111     // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
112     // we treat this as a set of non-AA rects rendered with a texture.
113     this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
114 }
115 
visitProxies(const VisitProxyFunc & func) const116 void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
117     fProcessors.visitProxies(func);
118 }
119 
120 #ifdef SK_DEBUG
dumpInfo() const121 SkString GrAtlasTextOp::dumpInfo() const {
122     SkString str;
123 
124     for (int i = 0; i < fGeoCount; ++i) {
125         str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
126                     i,
127                     fGeoData[i].fColor.toBytes_RGBA(),
128                     fGeoData[i].fX,
129                     fGeoData[i].fY,
130                     fGeoData[i].fBlob->runCountLimit());
131     }
132 
133     str += fProcessors.dumpProcessors();
134     str += INHERITED::dumpInfo();
135     return str;
136 }
137 #endif
138 
fixedFunctionFlags() const139 GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
140     return FixedFunctionFlags::kNone;
141 }
142 
finalize(const GrCaps & caps,const GrAppliedClip * clip,bool hasMixedSampledCoverage,GrClampType clampType)143 GrProcessorSet::Analysis GrAtlasTextOp::finalize(
144         const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
145         GrClampType clampType) {
146     GrProcessorAnalysisCoverage coverage;
147     GrProcessorAnalysisColor color;
148     if (kColorBitmapMask_MaskType == fMaskType) {
149         color.setToUnknown();
150     } else {
151         color.setToConstant(this->color());
152     }
153     switch (fMaskType) {
154         case kGrayscaleCoverageMask_MaskType:
155         case kAliasedDistanceField_MaskType:
156         case kGrayscaleDistanceField_MaskType:
157             coverage = GrProcessorAnalysisCoverage::kSingleChannel;
158             break;
159         case kLCDCoverageMask_MaskType:
160         case kLCDDistanceField_MaskType:
161         case kLCDBGRDistanceField_MaskType:
162             coverage = GrProcessorAnalysisCoverage::kLCD;
163             break;
164         case kColorBitmapMask_MaskType:
165             coverage = GrProcessorAnalysisCoverage::kNone;
166             break;
167     }
168     auto analysis = fProcessors.finalize(
169             color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
170             clampType, &fGeoData[0].fColor);
171     fUsesLocalCoords = analysis.usesLocalCoords();
172     return analysis;
173 }
174 
clip_quads(const SkIRect & clipRect,char * currVertex,const char * blobVertices,size_t vertexStride,int glyphCount)175 static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
176                        size_t vertexStride, int glyphCount) {
177     for (int i = 0; i < glyphCount; ++i) {
178         const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
179         const SkPoint* blobPositionRB =
180                 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
181 
182         // positions for bitmap glyphs are pixel boundary aligned
183         SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
184                                                  SkScalarRoundToInt(blobPositionLT->fY),
185                                                  SkScalarRoundToInt(blobPositionRB->fX),
186                                                  SkScalarRoundToInt(blobPositionRB->fY));
187         if (clipRect.contains(positionRect)) {
188             memcpy(currVertex, blobVertices, 4 * vertexStride);
189             currVertex += 4 * vertexStride;
190         } else {
191             // Pull out some more data that we'll need.
192             // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
193             // and it avoids a lot of conditionals.
194             auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
195             size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
196             auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
197             auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
198                                                                    coordOffset);
199             // Pull out the texel coordinates and texture index bits
200             uint16_t coordsRectL = blobCoordsLT[0] >> 1;
201             uint16_t coordsRectT = blobCoordsLT[1] >> 1;
202             uint16_t coordsRectR = blobCoordsRB[0] >> 1;
203             uint16_t coordsRectB = blobCoordsRB[1] >> 1;
204             uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
205             uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
206 
207             int positionRectWidth = positionRect.width();
208             int positionRectHeight = positionRect.height();
209             SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
210             SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
211 
212             // Clip position and texCoords to the clipRect
213             unsigned int delta;
214             delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
215             coordsRectL += delta;
216             positionRect.fLeft += delta;
217 
218             delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
219             coordsRectT += delta;
220             positionRect.fTop += delta;
221 
222             delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
223             coordsRectR -= delta;
224             positionRect.fRight -= delta;
225 
226             delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
227             coordsRectB -= delta;
228             positionRect.fBottom -= delta;
229 
230             // Repack texel coordinates and index
231             coordsRectL = coordsRectL << 1 | pageIndexX;
232             coordsRectT = coordsRectT << 1 | pageIndexY;
233             coordsRectR = coordsRectR << 1 | pageIndexX;
234             coordsRectB = coordsRectB << 1 | pageIndexY;
235 
236             // Set new positions and coords
237             SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
238             currPosition->fX = positionRect.fLeft;
239             currPosition->fY = positionRect.fTop;
240             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
241             uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
242             currCoords[0] = coordsRectL;
243             currCoords[1] = coordsRectT;
244             currVertex += vertexStride;
245 
246             currPosition = reinterpret_cast<SkPoint*>(currVertex);
247             currPosition->fX = positionRect.fLeft;
248             currPosition->fY = positionRect.fBottom;
249             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
250             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
251             currCoords[0] = coordsRectL;
252             currCoords[1] = coordsRectB;
253             currVertex += vertexStride;
254 
255             currPosition = reinterpret_cast<SkPoint*>(currVertex);
256             currPosition->fX = positionRect.fRight;
257             currPosition->fY = positionRect.fTop;
258             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
259             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
260             currCoords[0] = coordsRectR;
261             currCoords[1] = coordsRectT;
262             currVertex += vertexStride;
263 
264             currPosition = reinterpret_cast<SkPoint*>(currVertex);
265             currPosition->fX = positionRect.fRight;
266             currPosition->fY = positionRect.fBottom;
267             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
268             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
269             currCoords[0] = coordsRectR;
270             currCoords[1] = coordsRectB;
271             currVertex += vertexStride;
272         }
273 
274         blobVertices += 4 * vertexStride;
275     }
276 }
277 
onPrepareDraws(Target * target)278 void GrAtlasTextOp::onPrepareDraws(Target* target) {
279     auto resourceProvider = target->resourceProvider();
280 
281     // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
282     // TODO actually only invert if we don't have RGBA
283     SkMatrix localMatrix;
284     if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
285         return;
286     }
287 
288     GrAtlasManager* atlasManager = target->atlasManager();
289     GrStrikeCache* glyphCache = target->glyphCache();
290 
291     GrMaskFormat maskFormat = this->maskFormat();
292 
293     unsigned int numActiveProxies;
294     const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
295     if (!proxies) {
296         SkDebugf("Could not allocate backing texture for atlas\n");
297         return;
298     }
299     SkASSERT(proxies[0]);
300 
301     static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
302     GR_STATIC_ASSERT(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
303     GR_STATIC_ASSERT(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
304 
305     auto fixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
306     for (unsigned i = 0; i < numActiveProxies; ++i) {
307         fixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
308     }
309 
310     FlushInfo flushInfo;
311     flushInfo.fFixedDynamicState = fixedDynamicState;
312 
313     bool vmPerspective = fGeoData[0].fViewMatrix.hasPerspective();
314     if (this->usesDistanceFields()) {
315         flushInfo.fGeometryProcessor = this->setupDfProcessor(*target->caps().shaderCaps(),
316                                                               proxies, numActiveProxies);
317     } else {
318         GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
319                                                            : GrSamplerState::ClampNearest();
320         flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
321             *target->caps().shaderCaps(), this->color(), false, proxies, numActiveProxies,
322             samplerState, maskFormat, localMatrix, vmPerspective);
323     }
324 
325     flushInfo.fGlyphsToFlush = 0;
326     size_t vertexStride = flushInfo.fGeometryProcessor->vertexStride();
327 
328     int glyphCount = this->numGlyphs();
329 
330     void* vertices = target->makeVertexSpace(vertexStride, glyphCount * kVerticesPerGlyph,
331                                              &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset);
332     flushInfo.fIndexBuffer = resourceProvider->refQuadIndexBuffer();
333     if (!vertices || !flushInfo.fVertexBuffer) {
334         SkDebugf("Could not allocate vertices\n");
335         return;
336     }
337 
338     char* currVertex = reinterpret_cast<char*>(vertices);
339 
340     SkExclusiveStrikePtr autoGlyphCache;
341     // each of these is a SubRun
342     for (int i = 0; i < fGeoCount; i++) {
343         const Geometry& args = fGeoData[i];
344         Blob* blob = args.fBlob;
345         // TODO4F: Preserve float colors
346         GrTextBlob::VertexRegenerator regenerator(
347                 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
348                 args.fColor.toBytes_RGBA(), target->deferredUploadTarget(), glyphCache,
349                 atlasManager, &autoGlyphCache);
350         bool done = false;
351         while (!done) {
352             GrTextBlob::VertexRegenerator::Result result;
353             if (!regenerator.regenerate(&result)) {
354                 break;
355             }
356             done = result.fFinished;
357 
358             // Copy regenerated vertices from the blob to our vertex buffer.
359             size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
360             if (args.fClipRect.isEmpty()) {
361                 memcpy(currVertex, result.fFirstVertex, vertexBytes);
362             } else {
363                 SkASSERT(!vmPerspective);
364                 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
365                            result.fGlyphsRegenerated);
366             }
367             if (fNeedsGlyphTransform && !args.fViewMatrix.isIdentity()) {
368                 // We always do the distance field view matrix transformation after copying rather
369                 // than during blob vertex generation time in the blob as handling successive
370                 // arbitrary transformations would be complicated and accumulate error.
371                 if (args.fViewMatrix.hasPerspective()) {
372                     auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
373                     SkMatrixPriv::MapHomogeneousPointsWithStride(
374                             args.fViewMatrix, pos, vertexStride, pos, vertexStride,
375                             result.fGlyphsRegenerated * kVerticesPerGlyph);
376                 } else {
377                     auto* pos = reinterpret_cast<SkPoint*>(currVertex);
378                     SkMatrixPriv::MapPointsWithStride(
379                             args.fViewMatrix, pos, vertexStride,
380                             result.fGlyphsRegenerated * kVerticesPerGlyph);
381                 }
382             }
383             flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
384             if (!result.fFinished) {
385                 this->flush(target, &flushInfo);
386             }
387             currVertex += vertexBytes;
388         }
389     }
390     this->flush(target, &flushInfo);
391 }
392 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)393 void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
394     flushState->executeDrawsAndUploadsForMeshDrawOp(
395             this, chainBounds, std::move(fProcessors), GrPipeline::InputFlags::kNone);
396 }
397 
flush(GrMeshDrawOp::Target * target,FlushInfo * flushInfo) const398 void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
399     if (!flushInfo->fGlyphsToFlush) {
400         return;
401     }
402 
403     auto atlasManager = target->atlasManager();
404 
405     GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
406     GrMaskFormat maskFormat = this->maskFormat();
407 
408     unsigned int numActiveProxies;
409     const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
410     SkASSERT(proxies);
411     if (gp->numTextureSamplers() != (int) numActiveProxies) {
412         // During preparation the number of atlas pages has increased.
413         // Update the proxies used in the GP to match.
414         for (unsigned i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
415             flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
416         }
417         if (this->usesDistanceFields()) {
418             if (this->isLCD()) {
419                 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
420                     proxies, numActiveProxies, GrSamplerState::ClampBilerp());
421             } else {
422                 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
423                     proxies, numActiveProxies, GrSamplerState::ClampBilerp());
424             }
425         } else {
426             GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
427                                                                : GrSamplerState::ClampNearest();
428             reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
429                                                                       samplerState);
430         }
431     }
432     int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
433     GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
434     mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, kVerticesPerGlyph,
435                               flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
436     mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
437     target->recordDraw(
438             flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState, nullptr);
439     flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
440     flushInfo->fGlyphsToFlush = 0;
441 }
442 
onCombineIfPossible(GrOp * t,const GrCaps & caps)443 GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
444     GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
445     if (fProcessors != that->fProcessors) {
446         return CombineResult::kCannotCombine;
447     }
448 
449     if (fMaskType != that->fMaskType) {
450         return CombineResult::kCannotCombine;
451     }
452 
453     const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
454     const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
455 
456     if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
457         return CombineResult::kCannotCombine;
458     }
459 
460     if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
461         return CombineResult::kCannotCombine;
462     }
463 
464     if (fNeedsGlyphTransform &&
465         (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
466         return CombineResult::kCannotCombine;
467     }
468 
469     if (this->usesDistanceFields()) {
470         if (fDFGPFlags != that->fDFGPFlags) {
471             return CombineResult::kCannotCombine;
472         }
473 
474         if (fLuminanceColor != that->fLuminanceColor) {
475             return CombineResult::kCannotCombine;
476         }
477     } else {
478         if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
479             return CombineResult::kCannotCombine;
480         }
481     }
482 
483     // Keep the batch vertex buffer size below 32K so we don't have to create a special one
484     // We use the largest possible vertex size for this
485     static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
486     static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
487     if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
488         return CombineResult::kCannotCombine;
489     }
490 
491     fNumGlyphs += that->numGlyphs();
492 
493     // Reallocate space for geo data if necessary and then import that geo's data.
494     int newGeoCount = that->fGeoCount + fGeoCount;
495 
496     // We reallocate at a rate of 1.5x to try to get better total memory usage
497     if (newGeoCount > fGeoDataAllocSize) {
498         int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
499         while (newAllocSize < newGeoCount) {
500             newAllocSize += newAllocSize / 2;
501         }
502         fGeoData.realloc(newAllocSize);
503         fGeoDataAllocSize = newAllocSize;
504     }
505 
506     // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
507     // it doesn't try to unref them.
508     memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
509 #ifdef SK_DEBUG
510     for (int i = 0; i < that->fGeoCount; ++i) {
511         that->fGeoData.get()[i].fBlob = (Blob*)0x1;
512     }
513 #endif
514     that->fGeoCount = 0;
515     fGeoCount = newGeoCount;
516 
517     return CombineResult::kMerged;
518 }
519 
520 // TODO trying to figure out why lcd is so whack
521 // (see comments in GrTextContext::ComputeCanonicalColor)
setupDfProcessor(const GrShaderCaps & caps,const sk_sp<GrTextureProxy> * proxies,unsigned int numActiveProxies) const522 sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const GrShaderCaps& caps,
523                                                            const sk_sp<GrTextureProxy>* proxies,
524                                                            unsigned int numActiveProxies) const {
525     bool isLCD = this->isLCD();
526 
527     SkMatrix localMatrix = SkMatrix::I();
528     if (this->usesLocalCoords()) {
529         // If this fails we'll just use I().
530         bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
531         (void)result;
532     }
533 
534     // see if we need to create a new effect
535     if (isLCD) {
536         float redCorrection = fDistanceAdjustTable->getAdjustment(
537                 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
538                 fUseGammaCorrectDistanceTable);
539         float greenCorrection = fDistanceAdjustTable->getAdjustment(
540                 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
541                 fUseGammaCorrectDistanceTable);
542         float blueCorrection = fDistanceAdjustTable->getAdjustment(
543                 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
544                 fUseGammaCorrectDistanceTable);
545         GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
546                 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
547                         redCorrection, greenCorrection, blueCorrection);
548         return GrDistanceFieldLCDTextGeoProc::Make(caps, proxies, numActiveProxies,
549                                                    GrSamplerState::ClampBilerp(), widthAdjust,
550                                                    fDFGPFlags, localMatrix);
551     } else {
552 #ifdef SK_GAMMA_APPLY_TO_A8
553         float correction = 0;
554         if (kAliasedDistanceField_MaskType != fMaskType) {
555             U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
556                                                                 fLuminanceColor);
557             correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
558                                                              fUseGammaCorrectDistanceTable);
559         }
560         return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
561                                                   GrSamplerState::ClampBilerp(),
562                                                   correction, fDFGPFlags, localMatrix);
563 #else
564         return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
565                                                   GrSamplerState::ClampBilerp(),
566                                                   fDFGPFlags, localMatrix);
567 #endif
568     }
569 }
570 
571