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 "GrAtlasTextOp.h"
9 
10 #include "GrCaps.h"
11 #include "GrMemoryPool.h"
12 #include "GrOpFlushState.h"
13 #include "GrRecordingContext.h"
14 #include "GrRecordingContextPriv.h"
15 #include "GrResourceProvider.h"
16 #include "SkMathPriv.h"
17 #include "SkMatrixPriv.h"
18 #include "SkPoint3.h"
19 #include "SkStrikeCache.h"
20 #include "effects/GrBitmapTextGeoProc.h"
21 #include "effects/GrDistanceFieldGeoProc.h"
22 #include "text/GrAtlasManager.h"
23 #include "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,VisitorType) const116 void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func, VisitorType) 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,GrFSAAType fsaaType,GrClampType clampType)143 GrProcessorSet::Analysis GrAtlasTextOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
144                                                  GrFSAAType fsaaType, GrClampType clampType) {
145     GrProcessorAnalysisCoverage coverage;
146     GrProcessorAnalysisColor color;
147     if (kColorBitmapMask_MaskType == fMaskType) {
148         color.setToUnknown();
149     } else {
150         color.setToConstant(this->color());
151     }
152     switch (fMaskType) {
153         case kGrayscaleCoverageMask_MaskType:
154         case kAliasedDistanceField_MaskType:
155         case kGrayscaleDistanceField_MaskType:
156             coverage = GrProcessorAnalysisCoverage::kSingleChannel;
157             break;
158         case kLCDCoverageMask_MaskType:
159         case kLCDDistanceField_MaskType:
160         case kLCDBGRDistanceField_MaskType:
161             coverage = GrProcessorAnalysisCoverage::kLCD;
162             break;
163         case kColorBitmapMask_MaskType:
164             coverage = GrProcessorAnalysisCoverage::kNone;
165             break;
166     }
167     auto analysis = fProcessors.finalize(
168             color, coverage, clip, &GrUserStencilSettings::kUnused, fsaaType, caps, clampType,
169             &fGeoData[0].fColor);
170     fUsesLocalCoords = analysis.usesLocalCoords();
171     return analysis;
172 }
173 
clip_quads(const SkIRect & clipRect,char * currVertex,const char * blobVertices,size_t vertexStride,int glyphCount)174 static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
175                        size_t vertexStride, int glyphCount) {
176     for (int i = 0; i < glyphCount; ++i) {
177         const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
178         const SkPoint* blobPositionRB =
179                 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
180 
181         // positions for bitmap glyphs are pixel boundary aligned
182         SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
183                                                  SkScalarRoundToInt(blobPositionLT->fY),
184                                                  SkScalarRoundToInt(blobPositionRB->fX),
185                                                  SkScalarRoundToInt(blobPositionRB->fY));
186         if (clipRect.contains(positionRect)) {
187             memcpy(currVertex, blobVertices, 4 * vertexStride);
188             currVertex += 4 * vertexStride;
189         } else {
190             // Pull out some more data that we'll need.
191             // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
192             // and it avoids a lot of conditionals.
193             auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
194             size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
195             auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
196             auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
197                                                                    coordOffset);
198             // Pull out the texel coordinates and texture index bits
199             uint16_t coordsRectL = blobCoordsLT[0] >> 1;
200             uint16_t coordsRectT = blobCoordsLT[1] >> 1;
201             uint16_t coordsRectR = blobCoordsRB[0] >> 1;
202             uint16_t coordsRectB = blobCoordsRB[1] >> 1;
203             uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
204             uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
205 
206             int positionRectWidth = positionRect.width();
207             int positionRectHeight = positionRect.height();
208             SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
209             SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
210 
211             // Clip position and texCoords to the clipRect
212             unsigned int delta;
213             delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
214             coordsRectL += delta;
215             positionRect.fLeft += delta;
216 
217             delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
218             coordsRectT += delta;
219             positionRect.fTop += delta;
220 
221             delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
222             coordsRectR -= delta;
223             positionRect.fRight -= delta;
224 
225             delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
226             coordsRectB -= delta;
227             positionRect.fBottom -= delta;
228 
229             // Repack texel coordinates and index
230             coordsRectL = coordsRectL << 1 | pageIndexX;
231             coordsRectT = coordsRectT << 1 | pageIndexY;
232             coordsRectR = coordsRectR << 1 | pageIndexX;
233             coordsRectB = coordsRectB << 1 | pageIndexY;
234 
235             // Set new positions and coords
236             SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
237             currPosition->fX = positionRect.fLeft;
238             currPosition->fY = positionRect.fTop;
239             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
240             uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
241             currCoords[0] = coordsRectL;
242             currCoords[1] = coordsRectT;
243             currVertex += vertexStride;
244 
245             currPosition = reinterpret_cast<SkPoint*>(currVertex);
246             currPosition->fX = positionRect.fLeft;
247             currPosition->fY = positionRect.fBottom;
248             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
249             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
250             currCoords[0] = coordsRectL;
251             currCoords[1] = coordsRectB;
252             currVertex += vertexStride;
253 
254             currPosition = reinterpret_cast<SkPoint*>(currVertex);
255             currPosition->fX = positionRect.fRight;
256             currPosition->fY = positionRect.fTop;
257             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
258             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
259             currCoords[0] = coordsRectR;
260             currCoords[1] = coordsRectT;
261             currVertex += vertexStride;
262 
263             currPosition = reinterpret_cast<SkPoint*>(currVertex);
264             currPosition->fX = positionRect.fRight;
265             currPosition->fY = positionRect.fBottom;
266             *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
267             currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
268             currCoords[0] = coordsRectR;
269             currCoords[1] = coordsRectB;
270             currVertex += vertexStride;
271         }
272 
273         blobVertices += 4 * vertexStride;
274     }
275 }
276 
onPrepareDraws(Target * target)277 void GrAtlasTextOp::onPrepareDraws(Target* target) {
278     auto resourceProvider = target->resourceProvider();
279 
280     // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
281     // TODO actually only invert if we don't have RGBA
282     SkMatrix localMatrix;
283     if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
284         return;
285     }
286 
287     GrAtlasManager* atlasManager = target->atlasManager();
288     GrStrikeCache* glyphCache = target->glyphCache();
289 
290     GrMaskFormat maskFormat = this->maskFormat();
291 
292     unsigned int numActiveProxies;
293     const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
294     if (!proxies) {
295         SkDebugf("Could not allocate backing texture for atlas\n");
296         return;
297     }
298     SkASSERT(proxies[0]);
299 
300     static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
301     GR_STATIC_ASSERT(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
302     GR_STATIC_ASSERT(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
303 
304     auto fixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
305     for (unsigned i = 0; i < numActiveProxies; ++i) {
306         fixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
307     }
308 
309     FlushInfo flushInfo;
310     flushInfo.fFixedDynamicState = fixedDynamicState;
311 
312     bool vmPerspective = fGeoData[0].fViewMatrix.hasPerspective();
313     if (this->usesDistanceFields()) {
314         flushInfo.fGeometryProcessor = this->setupDfProcessor(*target->caps().shaderCaps(),
315                                                               proxies, numActiveProxies);
316     } else {
317         GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
318                                                            : GrSamplerState::ClampNearest();
319         flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
320             *target->caps().shaderCaps(), this->color(), false, proxies, numActiveProxies,
321             samplerState, maskFormat, localMatrix, vmPerspective);
322     }
323 
324     flushInfo.fGlyphsToFlush = 0;
325     size_t vertexStride = flushInfo.fGeometryProcessor->vertexStride();
326 
327     int glyphCount = this->numGlyphs();
328 
329     void* vertices = target->makeVertexSpace(vertexStride, glyphCount * kVerticesPerGlyph,
330                                              &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset);
331     flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
332     if (!vertices || !flushInfo.fVertexBuffer) {
333         SkDebugf("Could not allocate vertices\n");
334         return;
335     }
336 
337     char* currVertex = reinterpret_cast<char*>(vertices);
338 
339     SkExclusiveStrikePtr autoGlyphCache;
340     // each of these is a SubRun
341     for (int i = 0; i < fGeoCount; i++) {
342         const Geometry& args = fGeoData[i];
343         Blob* blob = args.fBlob;
344         // TODO4F: Preserve float colors
345         GrTextBlob::VertexRegenerator regenerator(
346                 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
347                 args.fColor.toBytes_RGBA(), target->deferredUploadTarget(), glyphCache,
348                 atlasManager, &autoGlyphCache);
349         bool done = false;
350         while (!done) {
351             GrTextBlob::VertexRegenerator::Result result;
352             if (!regenerator.regenerate(&result)) {
353                 break;
354             }
355             done = result.fFinished;
356 
357             // Copy regenerated vertices from the blob to our vertex buffer.
358             size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
359             if (args.fClipRect.isEmpty()) {
360                 memcpy(currVertex, result.fFirstVertex, vertexBytes);
361             } else {
362                 SkASSERT(!vmPerspective);
363                 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
364                            result.fGlyphsRegenerated);
365             }
366             if (fNeedsGlyphTransform && !args.fViewMatrix.isIdentity()) {
367                 // We always do the distance field view matrix transformation after copying rather
368                 // than during blob vertex generation time in the blob as handling successive
369                 // arbitrary transformations would be complicated and accumulate error.
370                 if (args.fViewMatrix.hasPerspective()) {
371                     auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
372                     SkMatrixPriv::MapHomogeneousPointsWithStride(
373                             args.fViewMatrix, pos, vertexStride, pos, vertexStride,
374                             result.fGlyphsRegenerated * kVerticesPerGlyph);
375                 } else {
376                     auto* pos = reinterpret_cast<SkPoint*>(currVertex);
377                     SkMatrixPriv::MapPointsWithStride(
378                             args.fViewMatrix, pos, vertexStride,
379                             result.fGlyphsRegenerated * kVerticesPerGlyph);
380                 }
381             }
382             flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
383             if (!result.fFinished) {
384                 this->flush(target, &flushInfo);
385             }
386             currVertex += vertexBytes;
387         }
388     }
389     this->flush(target, &flushInfo);
390 }
391 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)392 void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
393     static const uint32_t kPipelineFlags = 0;
394     flushState->executeDrawsAndUploadsForMeshDrawOp(
395             this, chainBounds, std::move(fProcessors), kPipelineFlags);
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