Debian Codegraph @

src/gpu/ganesh/ops/AtlasTextOp.cpp

raw ◀ ▶
Source reconstructed at patch state 2 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
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/ganesh/ops/AtlasTextOp.h"
9
10 #include "include/core/SkRRect.h"
11 #include "include/core/SkSamplingOptions.h"
12 #include "include/core/SkSurfaceProps.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkTArray.h"
16 #include "src/base/SkArenaAlloc.h"
17 #include "src/core/SkMatrixPriv.h"
18 #include "src/core/SkPaintPriv.h"
19 #include "src/core/SkTraceEvent.h"
20 #include "src/gpu/ganesh/GrBufferAllocPool.h"
21 #include "src/gpu/ganesh/GrCaps.h"
22 #include "src/gpu/ganesh/GrClip.h"
23 #include "src/gpu/ganesh/GrColorSpaceXform.h"
24 #include "src/gpu/ganesh/GrFragmentProcessor.h"
25 #include "src/gpu/ganesh/GrGeometryProcessor.h"
26 #include "src/gpu/ganesh/GrMeshDrawTarget.h"
27 #include "src/gpu/ganesh/GrOpFlushState.h"
28 #include "src/gpu/ganesh/GrPaint.h"
29 #include "src/gpu/ganesh/GrPipeline.h"
30 #include "src/gpu/ganesh/GrProcessorAnalysis.h"
31 #include "src/gpu/ganesh/GrResourceProvider.h"
32 #include "src/gpu/ganesh/GrSamplerState.h"
33 #include "src/gpu/ganesh/GrSimpleMesh.h"
34 #include "src/gpu/ganesh/GrSurfaceProxy.h"
35 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
36 #include "src/gpu/ganesh/GrUserStencilSettings.h"
37 #include "src/gpu/ganesh/SkGr.h"
38 #include "src/gpu/ganesh/SurfaceDrawContext.h"
39 #include "src/gpu/ganesh/effects/GrBitmapTextGeoProc.h"
40 #include "src/gpu/ganesh/effects/GrDistanceFieldGeoProc.h"
41 #include "src/gpu/ganesh/ops/GrDrawOp.h"
42 #include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
43 #include "src/gpu/ganesh/text/GrAtlasManager.h"
44 #include "src/text/gpu/DistanceFieldAdjustTable.h"
45 #include "src/text/gpu/GlyphVector.h"
46 #include "src/text/gpu/SubRunContainer.h"
47
48 #if defined(SK_GAMMA_APPLY_TO_A8)
49 #include "include/private/base/SkCPUTypes.h"
50 #include "src/core/SkMaskGamma.h"
51 #endif
52
53 #include <algorithm>
54 #include <functional>
55 #include <new>
56 #include <tuple>
57 #include <utility>
58
59 struct GrShaderCaps;
60
61 using MaskFormat = skgpu::MaskFormat;
62
63 namespace skgpu::ganesh {
64
65 inline static constexpr int kVerticesPerGlyph = 4;
66 inline static constexpr int kIndicesPerGlyph = 6;
67
68 // If we have thread local, then cache memory for a single AtlasTextOp.
69 static thread_local void* gCache = nullptr;
70 void* AtlasTextOp::operator new(size_t s) {
71 if (gCache != nullptr) {
72 return std::exchange(gCache, nullptr);
73 }
74
75 return ::operator new(s);
76 }
77
78 void AtlasTextOp::operator delete(void* bytes) noexcept {
79 if (gCache == nullptr) {
80 gCache = bytes;
81 return;
82 }
83 ::operator delete(bytes);
84 }
85
86 void AtlasTextOp::ClearCache() {
87 ::operator delete(gCache);
88 gCache = nullptr;
89 }
90
91 namespace {
92 SkPMColor4f calculate_colors(skgpu::ganesh::SurfaceDrawContext* sdc,
93 const SkPaint& paint,
94 const SkMatrix& matrix,
95 MaskFormat maskFormat,
96 GrPaint* grPaint) {
97 if (maskFormat == MaskFormat::kARGB) {
98 SkPaintToGrPaintReplaceShader(sdc, paint, matrix, nullptr, grPaint);
99 float a = grPaint->getColor4f().fA;
100 return {a, a, a, a};
101 }
102 SkPaintToGrPaint(sdc, paint, matrix, grPaint);
103 return grPaint->getColor4f();
104 }
105
106 SkMatrix position_matrix(const SkMatrix& drawMatrix, SkPoint drawOrigin) {
107 SkMatrix position_matrix = drawMatrix;
108 return position_matrix.preTranslate(drawOrigin.x(), drawOrigin.y());
109 }
110
111 AtlasTextOp::MaskType op_mask_type(MaskFormat maskFormat) {
112 switch (maskFormat) {
113 case MaskFormat::kA8: return AtlasTextOp::MaskType::kGrayscaleCoverage;
114 case MaskFormat::kA565: return AtlasTextOp::MaskType::kLCDCoverage;
115 case MaskFormat::kARGB: return AtlasTextOp::MaskType::kColorBitmap;
116 }
117 SkUNREACHABLE;
118 }
119
120 enum ClipMethod {
121 kClippedOut,
122 kUnclipped,
123 kGPUClipped,
124 kGeometryClipped
125 };
126
127 std::tuple<ClipMethod, SkIRect>
128 calculate_clip(const GrClip* clip, SkRect deviceBounds, SkRect glyphBounds) {
129 if (clip == nullptr && !deviceBounds.intersects(glyphBounds)) {
130 return {kClippedOut, SkIRect::MakeEmpty()};
131 } else if (clip != nullptr) {
132 switch (auto result = clip->preApply(glyphBounds, GrAA::kNo); result.fEffect) {
133 case GrClip::Effect::kClippedOut:
134 return {kClippedOut, SkIRect::MakeEmpty()};
135 case GrClip::Effect::kUnclipped:
136 return {kUnclipped, SkIRect::MakeEmpty()};
137 case GrClip::Effect::kClipped: {
138 if (result.fIsRRect && result.fRRect.isRect()) {
139 SkRect r = result.fRRect.rect();
140 if (result.fAA == GrAA::kNo || GrClip::IsPixelAligned(r)) {
141 SkIRect clipRect = SkIRect::MakeEmpty();
142 // Clip geometrically during onPrepare using clipRect.
143 r.round(&clipRect);
144 if (clipRect.contains(glyphBounds)) {
145 // If fully within the clip, signal no clipping using the empty rect.
146 return {kUnclipped, SkIRect::MakeEmpty()};
147 }
148 // Use the clipRect to clip the geometry.
149 return {kGeometryClipped, clipRect};
150 }
151 // Partial pixel clipped at this point. Have the GPU handle it.
152 }
153 }
154 break;
155 }
156 }
157 return {kGPUClipped, SkIRect::MakeEmpty()};
158 }
159
160 #if !defined(SK_DISABLE_SDF_TEXT)
161 static std::tuple<AtlasTextOp::MaskType, uint32_t, bool> calculate_sdf_parameters(
162 const skgpu::ganesh::SurfaceDrawContext& sdc,
163 const SkMatrix& drawMatrix,
164 bool useLCDText,
165 bool isAntiAliased) {
166 const GrColorInfo& colorInfo = sdc.colorInfo();
167 const SkSurfaceProps& props = sdc.surfaceProps();
168 using MT = AtlasTextOp::MaskType;
169 bool isLCD = useLCDText && props.pixelGeometry() != kUnknown_SkPixelGeometry;
170 MT maskType = !isAntiAliased ? MT::kAliasedDistanceField
171 : isLCD ? MT::kLCDDistanceField
172 : MT::kGrayscaleDistanceField;
173
174 bool useGammaCorrectDistanceTable = colorInfo.isLinearlyBlended();
175 uint32_t DFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
176 DFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
177 DFGPFlags |= useGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
178 DFGPFlags |= MT::kAliasedDistanceField == maskType ? kAliased_DistanceFieldEffectFlag : 0;
179 DFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
180
181 if (isLCD) {
182 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
183 bool isVertical = SkPixelGeometryIsV(props.pixelGeometry());
184 DFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
185 DFGPFlags |= isBGR ? kBGR_DistanceFieldEffectFlag : 0;
186 DFGPFlags |= isVertical ? kPortrait_DistanceFieldEffectFlag : 0;
187 }
188 return {maskType, DFGPFlags, useGammaCorrectDistanceTable};
189 }
190 #endif
191 } // anonymous namespace
192
193 std::tuple<const GrClip*, GrOp::Owner> AtlasTextOp::Make(SurfaceDrawContext* sdc,
194 const sktext::gpu::AtlasSubRun* subrun,
195 const GrClip* clip,
196 const SkMatrix& viewMatrix,
197 SkPoint drawOrigin,
198 const SkPaint& paint,
199 sk_sp<SkRefCnt>&& subRunStorage) {
200 SkASSERT(subrun->glyphCount() != 0);
201 const SkMatrix& positionMatrix = position_matrix(viewMatrix, drawOrigin);
202
203 auto [needsTransform, subRunDeviceBounds] =
204 subrun->deviceRectAndNeedsTransform(positionMatrix);
205 if (subRunDeviceBounds.isEmpty()) {
206 return {nullptr, nullptr};
207 }
208
209 SkIRect geometricClipRect = SkIRect::MakeEmpty();
210 if (!needsTransform) {
211 // We can clip geometrically using clipRect and ignore clip when an axis-aligned
212 // rectangular non-AA clip is used. If clipRect is empty, and clip is nullptr, then
213 // there is no clipping needed.
214 const SkRect deviceBounds = SkRect::MakeWH(sdc->width(), sdc->height());
215 auto [clipMethod, clipRect] = calculate_clip(clip, deviceBounds, subRunDeviceBounds);
216
217 switch (clipMethod) {
218 case kClippedOut:
219 // Returning nullptr as op means skip this op.
220 return {nullptr, nullptr};
221 case kUnclipped:
222 case kGeometryClipped:
223 // GPU clip is not needed.
224 clip = nullptr;
225 break;
226 case kGPUClipped:
227 // Use the GPU clip; clipRect is ignored.
228 break;
229 }
230 geometricClipRect = clipRect;
231
232 if (!geometricClipRect.isEmpty()) { SkASSERT(clip == nullptr); }
233 }
234
235 GrPaint grPaint;
236 const SkPMColor4f drawingColor = calculate_colors(sdc,
237 paint,
238 viewMatrix,
239 subrun->maskFormat(),
240 &grPaint);
241
242 auto geometry = AtlasTextOp::Geometry::Make(*subrun,
243 viewMatrix,
244 drawOrigin,
245 geometricClipRect,
246 std::move(subRunStorage),
247 drawingColor,
248 sdc->arenaAlloc());
249
250 GrRecordingContext* const rContext = sdc->recordingContext();
251
252 GrOp::Owner op;
253 #if !defined(SK_DISABLE_SDF_TEXT)
254 auto glyphParams = subrun->glyphParams();
255 if (glyphParams.isSDF) {
256 auto [maskType, DFGPFlags, useGammaCorrectDistanceTable] =
257 calculate_sdf_parameters(*sdc, viewMatrix, glyphParams.isLCD, glyphParams.isAA);
258 op = GrOp::Make<AtlasTextOp>(rContext,
259 maskType,
260 true,
261 subrun->glyphCount(),
262 subRunDeviceBounds,
263 SkPaintPriv::ComputeLuminanceColor(paint),
264 useGammaCorrectDistanceTable,
265 DFGPFlags,
266 geometry,
267 std::move(grPaint));
268 } else
269 #endif
270 {
271 op = GrOp::Make<AtlasTextOp>(rContext,
272 op_mask_type(subrun->maskFormat()),
273 needsTransform,
274 subrun->glyphCount(),
275 subRunDeviceBounds,
276 geometry,
277 sdc->colorInfo(),
278 std::move(grPaint));
279 }
280
281 return {clip, std::move(op)};
282 }
283
284 AtlasTextOp::AtlasTextOp(MaskType maskType,
285 bool needsTransform,
286 int glyphCount,
287 SkRect deviceRect,
288 Geometry* geo,
289 const GrColorInfo& dstColorInfo,
290 GrPaint&& paint)
291 : INHERITED{ClassID()}
292 , fProcessors(std::move(paint))
293 , fNumGlyphs(glyphCount)
294 , fDFGPFlags(0)
295 , fMaskType(static_cast<uint32_t>(maskType))
296 , fUsesLocalCoords(false)
297 , fNeedsGlyphTransform(needsTransform)
298 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
299 , fUseGammaCorrectDistanceTable(false)
300 , fHead{geo}
301 , fTail{&fHead->fNext} {
302 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
303 // we treat this as a set of non-AA rects rendered with a texture.
304 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
305 if (maskType == MaskType::kColorBitmap) {
306 // We assume that color emoji use the sRGB colorspace
307 fColorSpaceXform = dstColorInfo.refColorSpaceXformFromSRGB();
308 }
309 }
310
311 AtlasTextOp::AtlasTextOp(MaskType maskType,
312 bool needsTransform,
313 int glyphCount,
314 SkRect deviceRect,
315 SkColor luminanceColor,
316 bool useGammaCorrectDistanceTable,
317 uint32_t DFGPFlags,
318 Geometry* geo,
319 GrPaint&& paint)
320 : INHERITED{ClassID()}
321 , fProcessors(std::move(paint))
322 , fNumGlyphs(glyphCount)
323 , fDFGPFlags(DFGPFlags)
324 , fMaskType(static_cast<uint32_t>(maskType))
325 , fUsesLocalCoords(false)
326 , fNeedsGlyphTransform(needsTransform)
327 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
328 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
329 , fLuminanceColor(luminanceColor)
330 , fHead{geo}
331 , fTail{&fHead->fNext} {
332 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
333 // we treat this as a set of non-AA rects rendered with a texture.
334 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
335 }
336
337 auto AtlasTextOp::Geometry::Make(const sktext::gpu::AtlasSubRun& subRun,
338 const SkMatrix& drawMatrix,
339 SkPoint drawOrigin,
340 SkIRect clipRect,
341 sk_sp<SkRefCnt>&& supportData,
342 const SkPMColor4f& color,
343 SkArenaAlloc* alloc) -> Geometry* {
344 // Bypass the automatic dtor behavior in SkArenaAlloc. I'm leaving this up to the Op to run
345 // all geometry dtors for now.
346 void* geo = alloc->makeBytesAlignedTo(sizeof(Geometry), alignof(Geometry));
347 return new(geo) Geometry{subRun,
348 drawMatrix,
349 drawOrigin,
350 clipRect,
351 std::move(supportData),
352 color};
353 }
354
355 void AtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
356 fSubRun.fillVertexData(
357 dst, offset, count, fColor, fDrawMatrix, fDrawOrigin, fClipRect);
358 }
359
360 void AtlasTextOp::visitProxies(const GrVisitProxyFunc& func) const {
361 fProcessors.visitProxies(func);
362 }
363
364 #if defined(GPU_TEST_UTILS)
365 SkString AtlasTextOp::onDumpInfo() const {
366 SkString str;
367 int i = 0;
368 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
369 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
370 i++,
371 geom->fColor.toBytes_RGBA(),
372 geom->fDrawOrigin.x(),
373 geom->fDrawOrigin.y());
374 }
375
376 str += fProcessors.dumpProcessors();
377 return str;
378 }
379 #endif
380
381 GrDrawOp::FixedFunctionFlags AtlasTextOp::fixedFunctionFlags() const {
382 return FixedFunctionFlags::kNone;
383 }
384
385 GrProcessorSet::Analysis AtlasTextOp::finalize(const GrCaps& caps,
386 const GrAppliedClip* clip,
387 GrClampType clampType) {
388 GrProcessorAnalysisCoverage coverage;
389 GrProcessorAnalysisColor color;
390 if (this->maskType() == MaskType::kColorBitmap) {
391 color.setToUnknown();
392 } else {
393 // finalize() is called before any merging is done, so at this point there's at most one
394 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
395 color.setToConstant(fHead->fColor);
396 }
397
398 switch (this->maskType()) {
399 case MaskType::kGrayscaleCoverage:
400 #if !defined(SK_DISABLE_SDF_TEXT)
401 case MaskType::kAliasedDistanceField:
402 case MaskType::kGrayscaleDistanceField:
403 #endif
404 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
405 break;
406 case MaskType::kLCDCoverage:
407 #if !defined(SK_DISABLE_SDF_TEXT)
408 case MaskType::kLCDDistanceField:
409 #endif
410 coverage = GrProcessorAnalysisCoverage::kLCD;
411 break;
412 case MaskType::kColorBitmap:
413 coverage = GrProcessorAnalysisCoverage::kNone;
414 break;
415 }
416
417 auto analysis = fProcessors.finalize(color, coverage, clip, &GrUserStencilSettings::kUnused,
418 caps, clampType, &fHead->fColor);
419 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
420 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
421 // before the op is merged, so during combineIfPossible, metadata is effectively const.
422 fUsesLocalCoords = analysis.usesLocalCoords();
423 return analysis;
424 }
425
426 void AtlasTextOp::onPrepareDraws(GrMeshDrawTarget* target) {
427 auto resourceProvider = target->resourceProvider();
428
429 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
430 // processor analysis will not require local coords and the GPs will skip local coords when
431 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
432 // all geometries to have same draw matrix.
433 SkMatrix localMatrix = SkMatrix::I();
434 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
435 return;
436 }
437
438 GrAtlasManager* atlasManager = target->atlasManager();
439
440 MaskFormat maskFormat = this->maskFormat();
441
442 unsigned int numActiveViews;
443 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
444 if (!views) {
445 SkDebugf("Could not allocate backing texture for atlas\n");
446 return;
447 }
448 SkASSERT(views[0].proxy());
449
450 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
451 #if !defined(SK_DISABLE_SDF_TEXT)
452 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
453 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
454 #endif
455
456 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
457 for (unsigned i = 0; i < numActiveViews; ++i) {
458 primProcProxies[i] = views[i].proxy();
459 // This op does not know its atlas proxies when it is added to a OpsTasks, so the proxies
460 // don't get added during the visitProxies call. Thus we add them here.
461 target->sampledProxyArray()->push_back(views[i].proxy());
462 }
463
464 FlushInfo flushInfo;
465 flushInfo.fPrimProcProxies = primProcProxies;
466 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
467
468 #if !defined(SK_DISABLE_SDF_TEXT)
469 if (this->usesDistanceFields()) {
470 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
471 *target->caps().shaderCaps(),
472 localMatrix, views, numActiveViews);
473 } else
474 #endif
475 {
476 // Only use linear padding if the glyphs were also padded for it. If we somehow get a direct
477 // subrun with a corrupted transform, we should still use nearest neighbor since it was
478 // packed tightly.
479 const bool hasGlyphPadding = fHead->fSubRun.glyphSrcPadding() > 0;
480 auto filter = fNeedsGlyphTransform && hasGlyphPadding ? GrSamplerState::Filter::kLinear
481 : GrSamplerState::Filter::kNearest;
482 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
483 // color, so we can use the first's without worry.
484 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
485 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
486 /*wideColor=*/false, fColorSpaceXform, views, numActiveViews, filter,
487 maskFormat, localMatrix, fHasPerspective);
488 }
489
490 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
491
492 // Ensure we don't request an insanely large contiguous vertex allocation.
493 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
494 const int quadSize = vertexStride * kVerticesPerGlyph;
495 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
496
497 int allGlyphsCursor = 0;
498 const int allGlyphsEnd = fNumGlyphs;
499 int quadCursor;
500 int quadEnd;
501 char* vertices;
502
503 auto resetVertexBuffer = [&] {
504 quadCursor = 0;
505 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
506
507 vertices = (char*)target->makeVertexSpace(
508 vertexStride,
509 kVerticesPerGlyph * quadEnd,
510 &flushInfo.fVertexBuffer,
511 &flushInfo.fVertexOffset);
512
513 if (!vertices || !flushInfo.fVertexBuffer) {
514 SkDebugf("Could not allocate vertices\n");
515 return false;
516 }
517 return true;
518 };
519
520 if (!resetVertexBuffer()) {
521 return;
522 }
523
524 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
525 const sktext::gpu::AtlasSubRun& subRun = geo->fSubRun;
526
527 int strideCheck = SkToInt(subRun.vertexStride(geo->fDrawMatrix));
528 if (strideCheck != vertexStride) {
529 // We (unexpectedly) have buffers of different sizes between CPU and GPU. Bail out.
530 SKIA_LOG_D(
531 "Warning: stride mismatch detected (subrun stride: %d vertex buffer stride: "
532 "%d). Aborting draw.\n",
533 strideCheck,
534 vertexStride);
535 return;
536 }
537
538 const int subRunEnd = subRun.glyphCount();
539 auto regenerateDelegate = [&](sktext::gpu::GlyphVector* glyphs,
540 int begin,
541 int end,
542 skgpu::MaskFormat maskFormat,
543 int padding) {
544 return glyphs->regenerateAtlasForGanesh(begin, end, maskFormat, padding, target);
545 };
546 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
547 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
548 // of the glyphs to fill the vertex buffer.
549 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
550 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd,
551 regenerateDelegate);
552 // There was a problem allocating the glyph in the atlas. Bail.
553 if (!ok) {
554 return;
555 }
556
557 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
558
559 subRunCursor += glyphsRegenerated;
560 quadCursor += glyphsRegenerated;
561 allGlyphsCursor += glyphsRegenerated;
562 flushInfo.fGlyphsToFlush += glyphsRegenerated;
563
564 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
565 // Flush if not all the glyphs are drawn because either the quad buffer is full or
566 // the atlas is out of space.
567 if (subRunCursor < subRunEnd) {
568 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
569 }
570 this->createDrawForGeneratedGlyphs(target, &flushInfo);
571 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
572 // If the vertex buffer is full and there are still glyphs to draw then
573 // get a new buffer.
574 if(!resetVertexBuffer()) {
575 return;
576 }
577 }
578 }
579 }
580 }
581 }
582
583 void AtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
584 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
585 std::move(fProcessors),
586 GrPipeline::InputFlags::kNone);
587
588 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
589 &GrUserStencilSettings::kUnused);
590 }
591
592 void AtlasTextOp::createDrawForGeneratedGlyphs(GrMeshDrawTarget* target,
593 FlushInfo* flushInfo) const {
594 if (!flushInfo->fGlyphsToFlush) {
595 return;
596 }
597
598 auto atlasManager = target->atlasManager();
599
600 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
601 MaskFormat maskFormat = this->maskFormat();
602
603 unsigned int numActiveViews;
604 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
605 SkASSERT(views);
606 // Something has gone terribly wrong, bail
607 if (!views || 0 == numActiveViews) {
608 return;
609 }
610 if (gp->numTextureSamplers() != (int) numActiveViews) {
611 // During preparation the number of atlas pages has increased.
612 // Update the proxies used in the GP to match.
613 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
614 flushInfo->fPrimProcProxies[i] = views[i].proxy();
615 // This op does not know its atlas proxies when it is added to a OpsTasks, so the
616 // proxies don't get added during the visitProxies call. Thus we add them here.
617 target->sampledProxyArray()->push_back(views[i].proxy());
618 // These will get unreffed when the previously recorded draws destruct.
619 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
620 flushInfo->fPrimProcProxies[i]->ref();
621 }
622 }
623 #if !defined(SK_DISABLE_SDF_TEXT)
624 if (this->usesDistanceFields()) {
625 if (this->isLCD()) {
626 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
627 views, numActiveViews, GrSamplerState::Filter::kLinear);
628 } else {
629 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
630 views, numActiveViews, GrSamplerState::Filter::kLinear);
631 }
632 } else
633 #endif
634 {
635 const bool hasGlyphPadding = fHead->fSubRun.glyphSrcPadding() > 0;
636 auto filter = fNeedsGlyphTransform && hasGlyphPadding
637 ? GrSamplerState::Filter::kLinear : GrSamplerState::Filter::kNearest;
638 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
639 }
640 }
641 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
642 GrSimpleMesh* mesh = target->allocMesh();
643 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
644 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
645 flushInfo->fVertexOffset);
646 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
647 GrPrimitiveType::kTriangles);
648 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
649 flushInfo->fGlyphsToFlush = 0;
650 ++flushInfo->fNumDraws;
651 }
652
653 GrOp::CombineResult AtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
654 auto that = t->cast<AtlasTextOp>();
655
656 if (fDFGPFlags != that->fDFGPFlags ||
657 fMaskType != that->fMaskType ||
658 fUsesLocalCoords != that->fUsesLocalCoords ||
659 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
660 fHasPerspective != that->fHasPerspective ||
661 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
662 // All flags must match for an op to be combined
663 return CombineResult::kCannotCombine;
664 }
665
666 // We use the same filter for every Geometry that is combined, but the filter choice only looks
667 // at the head's src padding, so we can only combine if we are consistent with that.
668 if (fHead->fSubRun.glyphSrcPadding() != that->fHead->fSubRun.glyphSrcPadding()) {
669 return CombineResult::kCannotCombine;
670 }
671
672 if (fProcessors != that->fProcessors) {
673 return CombineResult::kCannotCombine;
674 }
675
676 if (fUsesLocalCoords) {
677 // If the fragment processors use local coordinates, the GPs compute them using the inverse
678 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
679 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
680 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
681 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
682 return CombineResult::kCannotCombine;
683 }
684 }
685
686 #if !defined(SK_DISABLE_SDF_TEXT)
687 if (this->usesDistanceFields()) {
688 SkASSERT(that->usesDistanceFields());
689 if (fLuminanceColor != that->fLuminanceColor) {
690 return CombineResult::kCannotCombine;
691 }
692 } else
693 #endif
694 {
695 if (this->maskType() == MaskType::kColorBitmap &&
696 fHead->fColor != that->fHead->fColor) {
697 // This ensures all merged bitmap color text ops have a constant color
698 return CombineResult::kCannotCombine;
699 }
700 }
701
702 fNumGlyphs += that->fNumGlyphs;
703
704 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
705 this->addGeometry(that->fHead);
706 that->fHead = nullptr;
707 return CombineResult::kMerged;
708 }
709
710 #if !defined(SK_DISABLE_SDF_TEXT)
711 GrGeometryProcessor* AtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
712 const GrShaderCaps& caps,
713 const SkMatrix& localMatrix,
714 const GrSurfaceProxyView* views,
715 unsigned int numActiveViews) const {
716 auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
717
718 // see if we need to create a new effect
719 if (this->isLCD()) {
720 float redCorrection = dfAdjustTable->getAdjustment(SkColorGetR(fLuminanceColor),
721 fUseGammaCorrectDistanceTable);
722 float greenCorrection = dfAdjustTable->getAdjustment(SkColorGetG(fLuminanceColor),
723 fUseGammaCorrectDistanceTable);
724 float blueCorrection = dfAdjustTable->getAdjustment(SkColorGetB(fLuminanceColor),
725 fUseGammaCorrectDistanceTable);
726 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
727 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
728 redCorrection, greenCorrection, blueCorrection);
729 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
730 GrSamplerState::Filter::kLinear, widthAdjust,
731 fDFGPFlags, localMatrix);
732 } else {
733 #if defined(SK_GAMMA_APPLY_TO_A8)
734 float correction = 0;
735 if (this->maskType() != MaskType::kAliasedDistanceField) {
736 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, fLuminanceColor);
737 correction = dfAdjustTable->getAdjustment(lum, fUseGammaCorrectDistanceTable);
738 }
739 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
740 GrSamplerState::Filter::kLinear, correction,
741 fDFGPFlags, localMatrix);
742 #else
743 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
744 GrSamplerState::Filter::kLinear, fDFGPFlags,
745 localMatrix);
746 #endif
747 }
748 }
749 #endif // !defined(SK_DISABLE_SDF_TEXT)
750
751 } // namespace skgpu::ganesh
752
753