• 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 #ifdef SK_ENABLE_SMALL_PAGE
201             uint16_t coordsRectL = blobCoordsLT[0] >> 2;
202             uint16_t coordsRectT = blobCoordsLT[1] >> 2;
203             uint16_t coordsRectR = blobCoordsRB[0] >> 2;
204             uint16_t coordsRectB = blobCoordsRB[1] >> 2;
205             uint16_t pageIndexX = blobCoordsLT[0] & 0x3;
206             uint16_t pageIndexY = blobCoordsLT[1] & 0x3;
207 #else
208             uint16_t coordsRectL = blobCoordsLT[0] >> 1;
209             uint16_t coordsRectT = blobCoordsLT[1] >> 1;
210             uint16_t coordsRectR = blobCoordsRB[0] >> 1;
211             uint16_t coordsRectB = blobCoordsRB[1] >> 1;
212             uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
213             uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
214 #endif
215 
216             int positionRectWidth = positionRect.width();
217             int positionRectHeight = positionRect.height();
218             SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
219             SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
220 
221             // Clip position and texCoords to the clipRect
222             unsigned int delta;
223             delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
224             coordsRectL += delta;
225             positionRect.fLeft += delta;
226 
227             delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
228             coordsRectT += delta;
229             positionRect.fTop += delta;
230 
231             delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
232             coordsRectR -= delta;
233             positionRect.fRight -= delta;
234 
235             delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
236             coordsRectB -= delta;
237             positionRect.fBottom -= delta;
238 
239             // Repack texel coordinates and index
240 #ifdef SK_ENABLE_SMALL_PAGE
241             coordsRectL = coordsRectL << 2 | pageIndexX;
242             coordsRectT = coordsRectT << 2 | pageIndexY;
243             coordsRectR = coordsRectR << 2 | pageIndexX;
244             coordsRectB = coordsRectB << 2 | pageIndexY;
245 #else
246             coordsRectL = coordsRectL << 1 | pageIndexX;
247             coordsRectT = coordsRectT << 1 | pageIndexY;
248             coordsRectR = coordsRectR << 1 | pageIndexX;
249             coordsRectB = coordsRectB << 1 | pageIndexY;
250 #endif
251 
252             // Set new positions and coords
253             SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
254             currPosition->fX = positionRect.fLeft;
255             currPosition->fY = positionRect.fTop;
256             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
257             uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
258             currCoords[0] = coordsRectL;
259             currCoords[1] = coordsRectT;
260             currVertex += vertexStride;
261 
262             currPosition = reinterpret_cast<SkPoint*>(currVertex);
263             currPosition->fX = positionRect.fLeft;
264             currPosition->fY = positionRect.fBottom;
265             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
266             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
267             currCoords[0] = coordsRectL;
268             currCoords[1] = coordsRectB;
269             currVertex += vertexStride;
270 
271             currPosition = reinterpret_cast<SkPoint*>(currVertex);
272             currPosition->fX = positionRect.fRight;
273             currPosition->fY = positionRect.fTop;
274             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
275             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
276             currCoords[0] = coordsRectR;
277             currCoords[1] = coordsRectT;
278             currVertex += vertexStride;
279 
280             currPosition = reinterpret_cast<SkPoint*>(currVertex);
281             currPosition->fX = positionRect.fRight;
282             currPosition->fY = positionRect.fBottom;
283             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
284             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
285             currCoords[0] = coordsRectR;
286             currCoords[1] = coordsRectB;
287             currVertex += vertexStride;
288         }
289 
290         blobVertices += 4 * vertexStride;
291     }
292 }
293 
onPrepareDraws(Target * target)294 void GrAtlasTextOp::onPrepareDraws(Target* target) {
295     auto resourceProvider = target->resourceProvider();
296 
297     // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
298     // TODO actually only invert if we don't have RGBA
299     SkMatrix localMatrix;
300     if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
301         return;
302     }
303 
304     GrAtlasManager* atlasManager = target->atlasManager();
305     GrStrikeCache* glyphCache = target->glyphCache();
306 
307     GrMaskFormat maskFormat = this->maskFormat();
308 
309     unsigned int numActiveProxies;
310     const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
311     if (!proxies) {
312         SkDebugf("Could not allocate backing texture for atlas\n");
313         return;
314     }
315     SkASSERT(proxies[0]);
316 
317     static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
318     GR_STATIC_ASSERT(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
319     GR_STATIC_ASSERT(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
320 
321     auto fixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
322     for (unsigned i = 0; i < numActiveProxies; ++i) {
323         fixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
324     }
325 
326     FlushInfo flushInfo;
327     flushInfo.fFixedDynamicState = fixedDynamicState;
328 
329     bool vmPerspective = fGeoData[0].fViewMatrix.hasPerspective();
330     if (this->usesDistanceFields()) {
331         flushInfo.fGeometryProcessor = this->setupDfProcessor(*target->caps().shaderCaps(),
332                                                               proxies, numActiveProxies);
333     } else {
334         GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
335                                                            : GrSamplerState::ClampNearest();
336         flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
337             *target->caps().shaderCaps(), this->color(), false, proxies, numActiveProxies,
338             samplerState, maskFormat, localMatrix, vmPerspective);
339     }
340 
341     flushInfo.fGlyphsToFlush = 0;
342     size_t vertexStride = flushInfo.fGeometryProcessor->vertexStride();
343 
344     int glyphCount = this->numGlyphs();
345 
346     void* vertices = target->makeVertexSpace(vertexStride, glyphCount * kVerticesPerGlyph,
347                                              &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset);
348     flushInfo.fIndexBuffer = resourceProvider->refQuadIndexBuffer();
349     if (!vertices || !flushInfo.fVertexBuffer) {
350         SkDebugf("Could not allocate vertices\n");
351         return;
352     }
353 
354     char* currVertex = reinterpret_cast<char*>(vertices);
355 
356     SkExclusiveStrikePtr autoGlyphCache;
357     // each of these is a SubRun
358     for (int i = 0; i < fGeoCount; i++) {
359         const Geometry& args = fGeoData[i];
360         Blob* blob = args.fBlob;
361         // TODO4F: Preserve float colors
362         GrTextBlob::VertexRegenerator regenerator(
363                 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
364                 args.fColor.toBytes_RGBA(), target->deferredUploadTarget(), glyphCache,
365                 atlasManager, &autoGlyphCache);
366         bool done = false;
367         while (!done) {
368             GrTextBlob::VertexRegenerator::Result result;
369             if (!regenerator.regenerate(&result)) {
370                 break;
371             }
372             done = result.fFinished;
373 
374             // Copy regenerated vertices from the blob to our vertex buffer.
375             size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
376             if (args.fClipRect.isEmpty()) {
377                 memcpy(currVertex, result.fFirstVertex, vertexBytes);
378             } else {
379                 SkASSERT(!vmPerspective);
380                 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
381                            result.fGlyphsRegenerated);
382             }
383             if (fNeedsGlyphTransform && !args.fViewMatrix.isIdentity()) {
384                 // We always do the distance field view matrix transformation after copying rather
385                 // than during blob vertex generation time in the blob as handling successive
386                 // arbitrary transformations would be complicated and accumulate error.
387                 if (args.fViewMatrix.hasPerspective()) {
388                     auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
389                     SkMatrixPriv::MapHomogeneousPointsWithStride(
390                             args.fViewMatrix, pos, vertexStride, pos, vertexStride,
391                             result.fGlyphsRegenerated * kVerticesPerGlyph);
392                 } else {
393                     auto* pos = reinterpret_cast<SkPoint*>(currVertex);
394                     SkMatrixPriv::MapPointsWithStride(
395                             args.fViewMatrix, pos, vertexStride,
396                             result.fGlyphsRegenerated * kVerticesPerGlyph);
397                 }
398             }
399             flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
400             if (!result.fFinished) {
401                 this->flush(target, &flushInfo);
402             }
403             currVertex += vertexBytes;
404         }
405     }
406     this->flush(target, &flushInfo);
407 }
408 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)409 void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
410     flushState->executeDrawsAndUploadsForMeshDrawOp(
411             this, chainBounds, std::move(fProcessors), GrPipeline::InputFlags::kNone);
412 }
413 
flush(GrMeshDrawOp::Target * target,FlushInfo * flushInfo) const414 void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
415     if (!flushInfo->fGlyphsToFlush) {
416         return;
417     }
418 
419     auto atlasManager = target->atlasManager();
420 
421     GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
422     GrMaskFormat maskFormat = this->maskFormat();
423 
424     unsigned int numActiveProxies;
425     const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
426     SkASSERT(proxies);
427     if (gp->numTextureSamplers() != (int) numActiveProxies) {
428         // During preparation the number of atlas pages has increased.
429         // Update the proxies used in the GP to match.
430         for (unsigned i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
431             flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
432         }
433         if (this->usesDistanceFields()) {
434             if (this->isLCD()) {
435                 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
436                     proxies, numActiveProxies, GrSamplerState::ClampBilerp());
437             } else {
438                 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
439                     proxies, numActiveProxies, GrSamplerState::ClampBilerp());
440             }
441         } else {
442             GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
443                                                                : GrSamplerState::ClampNearest();
444             reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
445                                                                       samplerState);
446         }
447     }
448     int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
449     GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
450     mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, kVerticesPerGlyph,
451                               flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
452     mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
453     target->recordDraw(
454             flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState, nullptr);
455     flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
456     flushInfo->fGlyphsToFlush = 0;
457 }
458 
onCombineIfPossible(GrOp * t,const GrCaps & caps)459 GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
460     GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
461     if (fProcessors != that->fProcessors) {
462         return CombineResult::kCannotCombine;
463     }
464 
465     if (fMaskType != that->fMaskType) {
466         return CombineResult::kCannotCombine;
467     }
468 
469     const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
470     const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
471 
472     if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
473         return CombineResult::kCannotCombine;
474     }
475 
476     if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
477         return CombineResult::kCannotCombine;
478     }
479 
480     if (fNeedsGlyphTransform &&
481         (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
482         return CombineResult::kCannotCombine;
483     }
484 
485     if (this->usesDistanceFields()) {
486         if (fDFGPFlags != that->fDFGPFlags) {
487             return CombineResult::kCannotCombine;
488         }
489 
490         if (fLuminanceColor != that->fLuminanceColor) {
491             return CombineResult::kCannotCombine;
492         }
493     } else {
494         if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
495             return CombineResult::kCannotCombine;
496         }
497     }
498 
499     // Keep the batch vertex buffer size below 32K so we don't have to create a special one
500     // We use the largest possible vertex size for this
501     static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
502     static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
503     if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
504         return CombineResult::kCannotCombine;
505     }
506 
507     fNumGlyphs += that->numGlyphs();
508 
509     // Reallocate space for geo data if necessary and then import that geo's data.
510     int newGeoCount = that->fGeoCount + fGeoCount;
511 
512     // We reallocate at a rate of 1.5x to try to get better total memory usage
513     if (newGeoCount > fGeoDataAllocSize) {
514         int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
515         while (newAllocSize < newGeoCount) {
516             newAllocSize += newAllocSize / 2;
517         }
518         fGeoData.realloc(newAllocSize);
519         fGeoDataAllocSize = newAllocSize;
520     }
521 
522     // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
523     // it doesn't try to unref them.
524     memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
525 #ifdef SK_DEBUG
526     for (int i = 0; i < that->fGeoCount; ++i) {
527         that->fGeoData.get()[i].fBlob = (Blob*)0x1;
528     }
529 #endif
530     that->fGeoCount = 0;
531     fGeoCount = newGeoCount;
532 
533     return CombineResult::kMerged;
534 }
535 
536 // TODO trying to figure out why lcd is so whack
537 // (see comments in GrTextContext::ComputeCanonicalColor)
setupDfProcessor(const GrShaderCaps & caps,const sk_sp<GrTextureProxy> * proxies,unsigned int numActiveProxies) const538 sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const GrShaderCaps& caps,
539                                                            const sk_sp<GrTextureProxy>* proxies,
540                                                            unsigned int numActiveProxies) const {
541     bool isLCD = this->isLCD();
542 
543     SkMatrix localMatrix = SkMatrix::I();
544     if (this->usesLocalCoords()) {
545         // If this fails we'll just use I().
546         bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
547         (void)result;
548     }
549 
550     // see if we need to create a new effect
551     if (isLCD) {
552         float redCorrection = fDistanceAdjustTable->getAdjustment(
553                 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
554                 fUseGammaCorrectDistanceTable);
555         float greenCorrection = fDistanceAdjustTable->getAdjustment(
556                 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
557                 fUseGammaCorrectDistanceTable);
558         float blueCorrection = fDistanceAdjustTable->getAdjustment(
559                 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
560                 fUseGammaCorrectDistanceTable);
561         GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
562                 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
563                         redCorrection, greenCorrection, blueCorrection);
564         return GrDistanceFieldLCDTextGeoProc::Make(caps, proxies, numActiveProxies,
565                                                    GrSamplerState::ClampBilerp(), widthAdjust,
566                                                    fDFGPFlags, localMatrix);
567     } else {
568 #ifdef SK_GAMMA_APPLY_TO_A8
569         float correction = 0;
570         if (kAliasedDistanceField_MaskType != fMaskType) {
571             U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
572                                                                 fLuminanceColor);
573             correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
574                                                              fUseGammaCorrectDistanceTable);
575         }
576         return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
577                                                   GrSamplerState::ClampBilerp(),
578                                                   correction, fDFGPFlags, localMatrix);
579 #else
580         return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
581                                                   GrSamplerState::ClampBilerp(),
582                                                   fDFGPFlags, localMatrix);
583 #endif
584     }
585 }
586 
587