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 "GrContext.h"
12 #include "GrContextPriv.h"
13 #include "GrMemoryPool.h"
14 #include "GrOpFlushState.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(GrContext * context,GrPaint && paint,GrMaskFormat maskFormat,int glyphCount,bool needsTransform)27 std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrContext* context,
28 GrPaint&& paint,
29 GrMaskFormat maskFormat,
30 int glyphCount,
31 bool needsTransform) {
32 GrOpMemoryPool* pool = context->contextPriv().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(GrContext * 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 GrContext* 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->contextPriv().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)143 GrProcessorSet::Analysis GrAtlasTextOp::finalize(const GrCaps& caps, const GrAppliedClip* clip) {
144 GrProcessorAnalysisCoverage coverage;
145 GrProcessorAnalysisColor color;
146 if (kColorBitmapMask_MaskType == fMaskType) {
147 color.setToUnknown();
148 } else {
149 color.setToConstant(this->color());
150 }
151 switch (fMaskType) {
152 case kGrayscaleCoverageMask_MaskType:
153 case kAliasedDistanceField_MaskType:
154 case kGrayscaleDistanceField_MaskType:
155 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
156 break;
157 case kLCDCoverageMask_MaskType:
158 case kLCDDistanceField_MaskType:
159 case kLCDBGRDistanceField_MaskType:
160 coverage = GrProcessorAnalysisCoverage::kLCD;
161 break;
162 case kColorBitmapMask_MaskType:
163 coverage = GrProcessorAnalysisCoverage::kNone;
164 break;
165 }
166 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, &fGeoData[0].fColor);
167 fUsesLocalCoords = analysis.usesLocalCoords();
168 return analysis;
169 }
170
clip_quads(const SkIRect & clipRect,char * currVertex,const char * blobVertices,size_t vertexStride,int glyphCount)171 static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
172 size_t vertexStride, int glyphCount) {
173 for (int i = 0; i < glyphCount; ++i) {
174 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
175 const SkPoint* blobPositionRB =
176 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
177
178 // positions for bitmap glyphs are pixel boundary aligned
179 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
180 SkScalarRoundToInt(blobPositionLT->fY),
181 SkScalarRoundToInt(blobPositionRB->fX),
182 SkScalarRoundToInt(blobPositionRB->fY));
183 if (clipRect.contains(positionRect)) {
184 memcpy(currVertex, blobVertices, 4 * vertexStride);
185 currVertex += 4 * vertexStride;
186 } else {
187 // Pull out some more data that we'll need.
188 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
189 // and it avoids a lot of conditionals.
190 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
191 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
192 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
193 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
194 coordOffset);
195 // Pull out the texel coordinates and texture index bits
196 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
197 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
198 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
199 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
200 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
201 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
202
203 int positionRectWidth = positionRect.width();
204 int positionRectHeight = positionRect.height();
205 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
206 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
207
208 // Clip position and texCoords to the clipRect
209 unsigned int delta;
210 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
211 coordsRectL += delta;
212 positionRect.fLeft += delta;
213
214 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
215 coordsRectT += delta;
216 positionRect.fTop += delta;
217
218 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
219 coordsRectR -= delta;
220 positionRect.fRight -= delta;
221
222 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
223 coordsRectB -= delta;
224 positionRect.fBottom -= delta;
225
226 // Repack texel coordinates and index
227 coordsRectL = coordsRectL << 1 | pageIndexX;
228 coordsRectT = coordsRectT << 1 | pageIndexY;
229 coordsRectR = coordsRectR << 1 | pageIndexX;
230 coordsRectB = coordsRectB << 1 | pageIndexY;
231
232 // Set new positions and coords
233 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
234 currPosition->fX = positionRect.fLeft;
235 currPosition->fY = positionRect.fTop;
236 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
237 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
238 currCoords[0] = coordsRectL;
239 currCoords[1] = coordsRectT;
240 currVertex += vertexStride;
241
242 currPosition = reinterpret_cast<SkPoint*>(currVertex);
243 currPosition->fX = positionRect.fLeft;
244 currPosition->fY = positionRect.fBottom;
245 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
246 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
247 currCoords[0] = coordsRectL;
248 currCoords[1] = coordsRectB;
249 currVertex += vertexStride;
250
251 currPosition = reinterpret_cast<SkPoint*>(currVertex);
252 currPosition->fX = positionRect.fRight;
253 currPosition->fY = positionRect.fTop;
254 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
255 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
256 currCoords[0] = coordsRectR;
257 currCoords[1] = coordsRectT;
258 currVertex += vertexStride;
259
260 currPosition = reinterpret_cast<SkPoint*>(currVertex);
261 currPosition->fX = positionRect.fRight;
262 currPosition->fY = positionRect.fBottom;
263 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
264 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
265 currCoords[0] = coordsRectR;
266 currCoords[1] = coordsRectB;
267 currVertex += vertexStride;
268 }
269
270 blobVertices += 4 * vertexStride;
271 }
272 }
273
onPrepareDraws(Target * target)274 void GrAtlasTextOp::onPrepareDraws(Target* target) {
275 auto resourceProvider = target->resourceProvider();
276
277 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
278 // TODO actually only invert if we don't have RGBA
279 SkMatrix localMatrix;
280 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
281 return;
282 }
283
284 GrAtlasManager* atlasManager = target->atlasManager();
285 GrStrikeCache* glyphCache = target->glyphCache();
286
287 GrMaskFormat maskFormat = this->maskFormat();
288
289 unsigned int numActiveProxies;
290 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
291 if (!proxies) {
292 SkDebugf("Could not allocate backing texture for atlas\n");
293 return;
294 }
295 SkASSERT(proxies[0]);
296
297 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
298 GR_STATIC_ASSERT(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
299 GR_STATIC_ASSERT(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
300
301 static const uint32_t kPipelineFlags = 0;
302 auto pipe = target->makePipeline(kPipelineFlags, std::move(fProcessors),
303 target->detachAppliedClip(), kMaxTextures);
304 for (unsigned i = 0; i < numActiveProxies; ++i) {
305 pipe.fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
306 }
307
308 FlushInfo flushInfo;
309 flushInfo.fPipeline = pipe.fPipeline;
310 flushInfo.fFixedDynamicState = pipe.fFixedDynamicState;
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
flush(GrMeshDrawOp::Target * target,FlushInfo * flushInfo) const392 void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
393 if (!flushInfo->fGlyphsToFlush) {
394 return;
395 }
396
397 auto atlasManager = target->atlasManager();
398
399 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
400 GrMaskFormat maskFormat = this->maskFormat();
401
402 unsigned int numActiveProxies;
403 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
404 SkASSERT(proxies);
405 if (gp->numTextureSamplers() != (int) numActiveProxies) {
406 // During preparation the number of atlas pages has increased.
407 // Update the proxies used in the GP to match.
408 for (unsigned i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
409 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
410 }
411 if (this->usesDistanceFields()) {
412 if (this->isLCD()) {
413 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
414 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
415 } else {
416 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
417 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
418 }
419 } else {
420 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
421 : GrSamplerState::ClampNearest();
422 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
423 samplerState);
424 }
425 }
426 int maxGlyphsPerDraw =
427 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
428 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
429 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, kVerticesPerGlyph,
430 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
431 mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
432 target->draw(flushInfo->fGeometryProcessor, flushInfo->fPipeline, flushInfo->fFixedDynamicState,
433 mesh);
434 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
435 flushInfo->fGlyphsToFlush = 0;
436 }
437
onCombineIfPossible(GrOp * t,const GrCaps & caps)438 GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
439 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
440 if (fProcessors != that->fProcessors) {
441 return CombineResult::kCannotCombine;
442 }
443
444 if (fMaskType != that->fMaskType) {
445 return CombineResult::kCannotCombine;
446 }
447
448 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
449 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
450
451 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
452 return CombineResult::kCannotCombine;
453 }
454
455 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
456 return CombineResult::kCannotCombine;
457 }
458
459 if (fNeedsGlyphTransform &&
460 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
461 return CombineResult::kCannotCombine;
462 }
463
464 if (this->usesDistanceFields()) {
465 if (fDFGPFlags != that->fDFGPFlags) {
466 return CombineResult::kCannotCombine;
467 }
468
469 if (fLuminanceColor != that->fLuminanceColor) {
470 return CombineResult::kCannotCombine;
471 }
472 } else {
473 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
474 return CombineResult::kCannotCombine;
475 }
476 }
477
478 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
479 // We use the largest possible vertex size for this
480 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
481 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
482 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
483 return CombineResult::kCannotCombine;
484 }
485
486 fNumGlyphs += that->numGlyphs();
487
488 // Reallocate space for geo data if necessary and then import that geo's data.
489 int newGeoCount = that->fGeoCount + fGeoCount;
490
491 // We reallocate at a rate of 1.5x to try to get better total memory usage
492 if (newGeoCount > fGeoDataAllocSize) {
493 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
494 while (newAllocSize < newGeoCount) {
495 newAllocSize += newAllocSize / 2;
496 }
497 fGeoData.realloc(newAllocSize);
498 fGeoDataAllocSize = newAllocSize;
499 }
500
501 // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
502 // it doesn't try to unref them.
503 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
504 #ifdef SK_DEBUG
505 for (int i = 0; i < that->fGeoCount; ++i) {
506 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
507 }
508 #endif
509 that->fGeoCount = 0;
510 fGeoCount = newGeoCount;
511
512 return CombineResult::kMerged;
513 }
514
515 // TODO trying to figure out why lcd is so whack
516 // (see comments in GrTextContext::ComputeCanonicalColor)
setupDfProcessor(const GrShaderCaps & caps,const sk_sp<GrTextureProxy> * proxies,unsigned int numActiveProxies) const517 sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const GrShaderCaps& caps,
518 const sk_sp<GrTextureProxy>* proxies,
519 unsigned int numActiveProxies) const {
520 bool isLCD = this->isLCD();
521
522 SkMatrix localMatrix = SkMatrix::I();
523 if (this->usesLocalCoords()) {
524 // If this fails we'll just use I().
525 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
526 (void)result;
527 }
528
529 // see if we need to create a new effect
530 if (isLCD) {
531 float redCorrection = fDistanceAdjustTable->getAdjustment(
532 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
533 fUseGammaCorrectDistanceTable);
534 float greenCorrection = fDistanceAdjustTable->getAdjustment(
535 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
536 fUseGammaCorrectDistanceTable);
537 float blueCorrection = fDistanceAdjustTable->getAdjustment(
538 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
539 fUseGammaCorrectDistanceTable);
540 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
541 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
542 redCorrection, greenCorrection, blueCorrection);
543 return GrDistanceFieldLCDTextGeoProc::Make(caps, proxies, numActiveProxies,
544 GrSamplerState::ClampBilerp(), widthAdjust,
545 fDFGPFlags, localMatrix);
546 } else {
547 #ifdef SK_GAMMA_APPLY_TO_A8
548 float correction = 0;
549 if (kAliasedDistanceField_MaskType != fMaskType) {
550 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
551 fLuminanceColor);
552 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
553 fUseGammaCorrectDistanceTable);
554 }
555 return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
556 GrSamplerState::ClampBilerp(),
557 correction, fDFGPFlags, localMatrix);
558 #else
559 return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
560 GrSamplerState::ClampBilerp(),
561 fDFGPFlags, localMatrix);
562 #endif
563 }
564 }
565
566