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/ops/GrSimpleMeshDrawOpHelper.h"
23#include "src/gpu/text/GrAtlasManager.h"
24#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
25
26///////////////////////////////////////////////////////////////////////////////////////////////////
27
28std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
29 GrPaint&& paint,
30 GrMaskFormat maskFormat,
31 int glyphCount,
32 bool needsTransform) {
33 GrOpMemoryPool* pool = context->priv().opMemoryPool();
34
35 std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
36
37 switch (maskFormat) {
38 case kA8_GrMaskFormat:
39 op->fMaskType = kGrayscaleCoverageMask_MaskType;
40 break;
41 case kA565_GrMaskFormat:
42 op->fMaskType = kLCDCoverageMask_MaskType;
43 break;
44 case kARGB_GrMaskFormat:
45 op->fMaskType = kColorBitmapMask_MaskType;
46 break;
47 }
48 op->fNumGlyphs = glyphCount;
49 op->fGeoCount = 1;
50 op->fLuminanceColor = 0;
51 op->fNeedsGlyphTransform = needsTransform;
52 return op;
53 }
54
55std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
56 GrRecordingContext* context,
57 GrPaint&& paint,
58 int glyphCount,
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->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable;
75 op->fLuminanceColor = luminanceColor;
76 op->fNumGlyphs = glyphCount;
77 op->fGeoCount = 1;
78 return op;
79 }
80
81static const int kDistanceAdjustLumShift = 5;
82
83void GrAtlasTextOp::init() {
84 const Geometry& geo = fGeoData[0];
85 if (this->usesDistanceFields()) {
86 bool isLCD = this->isLCD();
87
88 const SkMatrix& drawMatrix = geo.fDrawMatrix;
89
90 fDFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
91 fDFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
92 fDFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
93 fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
94 fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
95 ? kAliased_DistanceFieldEffectFlag
96 : 0;
97
98 if (isLCD) {
99 fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
100 fDFGPFlags |=
101 (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
102 }
103
104 fNeedsGlyphTransform = true;
105 }
106
107 SkRect bounds;
108 geo.fBlob->computeSubRunBounds(
109 &bounds, *geo.fSubRunPtr, geo.fDrawMatrix, geo.fDrawOrigin, fNeedsGlyphTransform);
110 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
111 // we treat this as a set of non-AA rects rendered with a texture.
112 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
113}
114
115void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
116 fProcessors.visitProxies(func);
117}
118
119#ifdef SK_DEBUG
120SkString GrAtlasTextOp::dumpInfo() const {
121 SkString str;
122
123 for (int i = 0; i < fGeoCount; ++i) {
124 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
125 i,
126 fGeoData[i].fColor.toBytes_RGBA(),
127 fGeoData[i].fDrawOrigin.x(),
128 fGeoData[i].fDrawOrigin.y());
129 }
130
131 str += fProcessors.dumpProcessors();
132 str += INHERITED::dumpInfo();
133 return str;
134}
135#endif
136
137GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
138 return FixedFunctionFlags::kNone;
139}
140
141GrProcessorSet::Analysis GrAtlasTextOp::finalize(
142 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
143 GrClampType clampType) {
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(
167 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
168 clampType, &fGeoData[0].fColor);
169 fUsesLocalCoords = analysis.usesLocalCoords();
170 return analysis;
171}
172
173static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
174 size_t vertexStride, int glyphCount) {
175 for (int i = 0; i < glyphCount; ++i) {
176 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
177 const SkPoint* blobPositionRB =
178 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
179
180 // positions for bitmap glyphs are pixel boundary aligned
181 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
182 SkScalarRoundToInt(blobPositionLT->fY),
183 SkScalarRoundToInt(blobPositionRB->fX),
184 SkScalarRoundToInt(blobPositionRB->fY));
185 if (clipRect.contains(positionRect)) {
186 memcpy(currVertex, blobVertices, 4 * vertexStride);
187 currVertex += 4 * vertexStride;
188 } else {
189 // Pull out some more data that we'll need.
190 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
191 // and it avoids a lot of conditionals.
192 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
193 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
194 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
195 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
196 coordOffset);
197 // Pull out the texel coordinates and texture index bits
198 uint16_t coordsRectL = blobCoordsLT[0];
199 uint16_t coordsRectT = blobCoordsLT[1];
200 uint16_t coordsRectR = blobCoordsRB[0];
201 uint16_t coordsRectB = blobCoordsRB[1];
202 int index0, index1;
203 std::tie(coordsRectL, coordsRectT, index0) =
204 GrDrawOpAtlas::UnpackIndexFromTexCoords(coordsRectL, coordsRectT);
205 std::tie(coordsRectR, coordsRectB, index1) =
206 GrDrawOpAtlas::UnpackIndexFromTexCoords(coordsRectR, coordsRectB);
207 SkASSERT(index0 == index1);
208
209 int positionRectWidth = positionRect.width();
210 int positionRectHeight = positionRect.height();
211 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
212 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
213
214 // Clip position and texCoords to the clipRect
215 unsigned int delta;
216 delta = std::min(std::max(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
217 coordsRectL += delta;
218 positionRect.fLeft += delta;
219
220 delta = std::min(std::max(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
221 coordsRectT += delta;
222 positionRect.fTop += delta;
223
224 delta = std::min(std::max(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
225 coordsRectR -= delta;
226 positionRect.fRight -= delta;
227
228 delta = std::min(std::max(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
229 coordsRectB -= delta;
230 positionRect.fBottom -= delta;
231
232 // Repack texel coordinates and index
233 std::tie(coordsRectL, coordsRectT) =
234 GrDrawOpAtlas::PackIndexInTexCoords(coordsRectL, coordsRectT, index0);
235 std::tie(coordsRectR, coordsRectB) =
236 GrDrawOpAtlas::PackIndexInTexCoords(coordsRectR, coordsRectB, index1);
237
238 // Set new positions and coords
239 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
240 currPosition->fX = positionRect.fLeft;
241 currPosition->fY = positionRect.fTop;
242 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
243 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
244 currCoords[0] = coordsRectL;
245 currCoords[1] = coordsRectT;
246 currVertex += vertexStride;
247
248 currPosition = reinterpret_cast<SkPoint*>(currVertex);
249 currPosition->fX = positionRect.fLeft;
250 currPosition->fY = positionRect.fBottom;
251 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
252 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
253 currCoords[0] = coordsRectL;
254 currCoords[1] = coordsRectB;
255 currVertex += vertexStride;
256
257 currPosition = reinterpret_cast<SkPoint*>(currVertex);
258 currPosition->fX = positionRect.fRight;
259 currPosition->fY = positionRect.fTop;
260 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
261 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
262 currCoords[0] = coordsRectR;
263 currCoords[1] = coordsRectT;
264 currVertex += vertexStride;
265
266 currPosition = reinterpret_cast<SkPoint*>(currVertex);
267 currPosition->fX = positionRect.fRight;
268 currPosition->fY = positionRect.fBottom;
269 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
270 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
271 currCoords[0] = coordsRectR;
272 currCoords[1] = coordsRectB;
273 currVertex += vertexStride;
274 }
275
276 blobVertices += 4 * vertexStride;
277 }
278}
279
280void GrAtlasTextOp::onPrepareDraws(Target* target) {
281 auto resourceProvider = target->resourceProvider();
282
283 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
284 // TODO actually only invert if we don't have RGBA
285 SkMatrix localMatrix;
286 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
287 return;
288 }
289
290 GrAtlasManager* atlasManager = target->atlasManager();
291
292 GrMaskFormat maskFormat = this->maskFormat();
293
294 unsigned int numActiveViews;
295 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
296 if (!views) {
297 SkDebugf("Could not allocate backing texture for atlas\n");
298 return;
299 }
300 SkASSERT(views[0].proxy());
301
302 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
303 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
304 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
305
306 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
307 for (unsigned i = 0; i < numActiveViews; ++i) {
308 primProcProxies[i] = views[i].proxy();
309 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
310 // don't get added during the visitProxies call. Thus we add them here.
311 target->sampledProxyArray()->push_back(views[i].proxy());
312 }
313
314 FlushInfo flushInfo;
315 flushInfo.fPrimProcProxies = primProcProxies;
316
317 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
318 if (this->usesDistanceFields()) {
319 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
320 *target->caps().shaderCaps(),
321 views, numActiveViews);
322 } else {
323 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
324 : GrSamplerState::Filter::kNearest;
325 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
326 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
327 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
328 }
329
330 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
331
332 // Ensure we don't request an insanely large contiguous vertex allocation.
333 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
334 const int quadSize = vertexStride * kVerticesPerGlyph;
335 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
336
337 // Where the quad buffer begins and ends relative to totalGlyphsRegened.
338 int quadBufferBegin = 0;
339 int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer);
340
341 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
342 void* vertices = target->makeVertexSpace(
343 vertexStride,
344 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
345 &flushInfo.fVertexBuffer,
346 &flushInfo.fVertexOffset);
347 if (!vertices || !flushInfo.fVertexBuffer) {
348 SkDebugf("Could not allocate vertices\n");
349 return;
350 }
351
352 // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and
353 // quad buffer indices are calculated from this.
354 int totalGlyphsRegened = 0;
355 for (int i = 0; i < fGeoCount; i++) {
356 const Geometry& args = fGeoData[i];
357 auto subRun = args.fSubRunPtr;
358 SkASSERT((int)subRun->vertexStride() == vertexStride);
359
360 subRun->prepareGrGlyphs(target->strikeCache());
361 subRun->updateVerticesColorIfNeeded(args.fColor.toBytes_RGBA());
362 subRun->translateVerticesIfNeeded(args.fDrawMatrix, args.fDrawOrigin);
363
364 // TODO4F: Preserve float colors
365 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
366 target->deferredUploadTarget(), atlasManager);
367
368 // Where the subRun begins and ends relative to totalGlyphsRegened.
369 int subRunBegin = totalGlyphsRegened;
370 int subRunEnd = subRunBegin + subRun->fGlyphs.count();
371
372 // Draw all the glyphs in the subRun.
373 while (totalGlyphsRegened < subRunEnd) {
374 // drawBegin and drawEnd are indices for the subRun on the
375 // interval [0, subRun->fGlyphs.size()).
376 int drawBegin = totalGlyphsRegened - subRunBegin;
377 // drawEnd is either the end of the subRun or the end of the current quad buffer.
378 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
379 auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd);
380
381 // There was a problem allocating the glyph in the atlas. Bail.
382 if (!ok) {
383 return;
384 }
385
386 // Update all the vertices for glyphsRegenerate glyphs.
387 if (glyphsRegenerated > 0) {
388 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
389 int subRunIndex = totalGlyphsRegened - subRunBegin;
390 auto regeneratedQuadBuffer =
391 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
392 if (args.fClipRect.isEmpty()) {
393 memcpy(regeneratedQuadBuffer,
394 subRun->quadStart(subRunIndex),
395 glyphsRegenerated * quadSize);
396 } else {
397 SkASSERT(!vmPerspective);
398 clip_quads(args.fClipRect,
399 regeneratedQuadBuffer,
400 subRun->quadStart(subRunIndex),
401 vertexStride,
402 glyphsRegenerated);
403 }
404 if (fNeedsGlyphTransform && !args.fDrawMatrix.isIdentity()) {
405 // We always do the distance field view matrix transformation after copying
406 // rather than during blob vertex generation time in the blob as handling
407 // successive arbitrary transformations would be complicated and accumulate
408 // error.
409 if (args.fDrawMatrix.hasPerspective()) {
410 auto* pos = reinterpret_cast<SkPoint3*>(regeneratedQuadBuffer);
411 SkMatrixPriv::MapHomogeneousPointsWithStride(
412 args.fDrawMatrix, pos,
413 vertexStride, pos,
414 vertexStride,
415 glyphsRegenerated * kVerticesPerGlyph);
416 } else {
417 auto* pos = reinterpret_cast<SkPoint*>(regeneratedQuadBuffer);
418 SkMatrixPriv::MapPointsWithStride(args.fDrawMatrix, pos, vertexStride,
419 glyphsRegenerated * kVerticesPerGlyph);
420 }
421 }
422 }
423
424 totalGlyphsRegened += glyphsRegenerated;
425 flushInfo.fGlyphsToFlush += glyphsRegenerated;
426
427 // regenerate() has stopped part way through a SubRun. This means that either the atlas
428 // or the quad buffer is full or both. There is a case were the flow through
429 // the loop is strange. If we run out of quad buffer space at the same time the
430 // SubRun ends, then this is not triggered which is the right result for the last
431 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
432 // will process no glyphs, and return to this point where the quad buffer will be
433 // expanded.
434 if (totalGlyphsRegened != subRunEnd) {
435 // Flush if not all glyphs drawn because either the quad buffer is full or the
436 // atlas is out of space.
437 this->createDrawForGeneratedGlyphs(target, &flushInfo);
438 if (totalGlyphsRegened == quadBufferEnd) {
439 // Quad buffer is full. Get more buffer.
440 quadBufferBegin = totalGlyphsRegened;
441 int quadBufferSize =
442 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
443 quadBufferEnd = quadBufferBegin + quadBufferSize;
444
445 vertices = target->makeVertexSpace(
446 vertexStride,
447 kVerticesPerGlyph * quadBufferSize,
448 &flushInfo.fVertexBuffer,
449 &flushInfo.fVertexOffset);
450 if (!vertices || !flushInfo.fVertexBuffer) {
451 SkDebugf("Could not allocate vertices\n");
452 return;
453 }
454 }
455 }
456 }
457 } // for all geometries
458 this->createDrawForGeneratedGlyphs(target, &flushInfo);
459}
460
461void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
462 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
463 std::move(fProcessors),
464 GrPipeline::InputFlags::kNone);
465
466 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
467}
468
469void GrAtlasTextOp::createDrawForGeneratedGlyphs(
470 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
471 if (!flushInfo->fGlyphsToFlush) {
472 return;
473 }
474
475 auto atlasManager = target->atlasManager();
476
477 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
478 GrMaskFormat maskFormat = this->maskFormat();
479
480 unsigned int numActiveViews;
481 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
482 SkASSERT(views);
483 // Something has gone terribly wrong, bail
484 if (!views || 0 == numActiveViews) {
485 return;
486 }
487 if (gp->numTextureSamplers() != (int) numActiveViews) {
488 // During preparation the number of atlas pages has increased.
489 // Update the proxies used in the GP to match.
490 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
491 flushInfo->fPrimProcProxies[i] = views[i].proxy();
492 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
493 // proxies don't get added during the visitProxies call. Thus we add them here.
494 target->sampledProxyArray()->push_back(views[i].proxy());
495 // These will get unreffed when the previously recorded draws destruct.
496 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
497 flushInfo->fPrimProcProxies[i]->ref();
498 }
499 }
500 if (this->usesDistanceFields()) {
501 if (this->isLCD()) {
502 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
503 views, numActiveViews, GrSamplerState::Filter::kBilerp);
504 } else {
505 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
506 views, numActiveViews, GrSamplerState::Filter::kBilerp);
507 }
508 } else {
509 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
510 : GrSamplerState::Filter::kNearest;
511 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
512 }
513 }
514 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
515 GrSimpleMesh* mesh = target->allocMesh();
516 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
517 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
518 flushInfo->fVertexOffset);
519 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
520 GrPrimitiveType::kTriangles);
521 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
522 flushInfo->fGlyphsToFlush = 0;
523 ++flushInfo->fNumDraws;
524}
525
526GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
527 const GrCaps& caps) {
528 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
529 if (fProcessors != that->fProcessors) {
530 return CombineResult::kCannotCombine;
531 }
532
533 if (fMaskType != that->fMaskType) {
534 return CombineResult::kCannotCombine;
535 }
536
537 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
538 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
539
540 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
541 return CombineResult::kCannotCombine;
542 }
543
544 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
545 return CombineResult::kCannotCombine;
546 }
547
548 if (fNeedsGlyphTransform &&
549 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
550 return CombineResult::kCannotCombine;
551 }
552
553 if (this->usesDistanceFields()) {
554 if (fDFGPFlags != that->fDFGPFlags) {
555 return CombineResult::kCannotCombine;
556 }
557
558 if (fLuminanceColor != that->fLuminanceColor) {
559 return CombineResult::kCannotCombine;
560 }
561 } else {
562 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
563 return CombineResult::kCannotCombine;
564 }
565 }
566
567 fNumGlyphs += that->numGlyphs();
568
569 // Reallocate space for geo data if necessary and then import that geo's data.
570 int newGeoCount = that->fGeoCount + fGeoCount;
571
572 // We reallocate at a rate of 1.5x to try to get better total memory usage
573 if (newGeoCount > fGeoDataAllocSize) {
574 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
575 while (newAllocSize < newGeoCount) {
576 newAllocSize += newAllocSize / 2;
577 }
578 fGeoData.realloc(newAllocSize);
579 fGeoDataAllocSize = newAllocSize;
580 }
581
582 // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
583 // it doesn't try to unref them.
584 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
585#ifdef SK_DEBUG
586 for (int i = 0; i < that->fGeoCount; ++i) {
587 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
588 }
589#endif
590 that->fGeoCount = 0;
591 fGeoCount = newGeoCount;
592
593 return CombineResult::kMerged;
594}
595
596// TODO trying to figure out why lcd is so whack
597// (see comments in GrTextContext::ComputeCanonicalColor)
598GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
599 const GrShaderCaps& caps,
600 const GrSurfaceProxyView* views,
601 unsigned int numActiveViews) const {
602 bool isLCD = this->isLCD();
603
604 SkMatrix localMatrix = SkMatrix::I();
605 if (this->usesLocalCoords()) {
606 // If this fails we'll just use I().
607 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
608 (void)result;
609 }
610
611 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
612
613 // see if we need to create a new effect
614 if (isLCD) {
615 float redCorrection = dfAdjustTable->getAdjustment(
616 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
617 fUseGammaCorrectDistanceTable);
618 float greenCorrection = dfAdjustTable->getAdjustment(
619 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
620 fUseGammaCorrectDistanceTable);
621 float blueCorrection = dfAdjustTable->getAdjustment(
622 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
623 fUseGammaCorrectDistanceTable);
624 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
625 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
626 redCorrection, greenCorrection, blueCorrection);
627 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
628 GrSamplerState::Filter::kBilerp, widthAdjust,
629 fDFGPFlags, localMatrix);
630 } else {
631#ifdef SK_GAMMA_APPLY_TO_A8
632 float correction = 0;
633 if (kAliasedDistanceField_MaskType != fMaskType) {
634 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
635 fLuminanceColor);
636 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
637 fUseGammaCorrectDistanceTable);
638 }
639 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
640 GrSamplerState::Filter::kBilerp, correction,
641 fDFGPFlags, localMatrix);
642#else
643 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
644 GrSamplerState::Filter::kBilerp,
645 fDFGPFlags, localMatrix);
646#endif
647 }
648}
649
650