Debian Codegraph @

src/gpu/ganesh/ops/AtlasTextOp.cpp

raw ◀ ▶
Source reconstructed at patch state 1 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 SkASSERTF((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride,
527 "subRun stride: %d vertex buffer stride: %d\n",
528 (int)subRun.vertexStride(geo->fDrawMatrix), vertexStride);
529
530 const int subRunEnd = subRun.glyphCount();
531 auto regenerateDelegate = [&](sktext::gpu::GlyphVector* glyphs,
532 int begin,
533 int end,
534 skgpu::MaskFormat maskFormat,
535 int padding) {
536 return glyphs->regenerateAtlasForGanesh(begin, end, maskFormat, padding, target);
537 };
538 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
539 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
540 // of the glyphs to fill the vertex buffer.
541 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
542 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd,
543 regenerateDelegate);
544 // There was a problem allocating the glyph in the atlas. Bail.
545 if (!ok) {
546 return;
547 }
548
549 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
550
551 subRunCursor += glyphsRegenerated;
552 quadCursor += glyphsRegenerated;
553 allGlyphsCursor += glyphsRegenerated;
554 flushInfo.fGlyphsToFlush += glyphsRegenerated;
555
556 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
557 // Flush if not all the glyphs are drawn because either the quad buffer is full or
558 // the atlas is out of space.
559 if (subRunCursor < subRunEnd) {
560 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
561 }
562 this->createDrawForGeneratedGlyphs(target, &flushInfo);
563 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
564 // If the vertex buffer is full and there are still glyphs to draw then
565 // get a new buffer.
566 if(!resetVertexBuffer()) {
567 return;
568 }
569 }
570 }
571 }
572 }
573 }
574
575 void AtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
576 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
577 std::move(fProcessors),
578 GrPipeline::InputFlags::kNone);
579
580 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
581 &GrUserStencilSettings::kUnused);
582 }
583
584 void AtlasTextOp::createDrawForGeneratedGlyphs(GrMeshDrawTarget* target,
585 FlushInfo* flushInfo) const {
586 if (!flushInfo->fGlyphsToFlush) {
587 return;
588 }
589
590 auto atlasManager = target->atlasManager();
591
592 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
593 MaskFormat maskFormat = this->maskFormat();
594
595 unsigned int numActiveViews;
596 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
597 SkASSERT(views);
598 // Something has gone terribly wrong, bail
599 if (!views || 0 == numActiveViews) {
600 return;
601 }
602 if (gp->numTextureSamplers() != (int) numActiveViews) {
603 // During preparation the number of atlas pages has increased.
604 // Update the proxies used in the GP to match.
605 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
606 flushInfo->fPrimProcProxies[i] = views[i].proxy();
607 // This op does not know its atlas proxies when it is added to a OpsTasks, so the
608 // proxies don't get added during the visitProxies call. Thus we add them here.
609 target->sampledProxyArray()->push_back(views[i].proxy());
610 // These will get unreffed when the previously recorded draws destruct.
611 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
612 flushInfo->fPrimProcProxies[i]->ref();
613 }
614 }
615 #if !defined(SK_DISABLE_SDF_TEXT)
616 if (this->usesDistanceFields()) {
617 if (this->isLCD()) {
618 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
619 views, numActiveViews, GrSamplerState::Filter::kLinear);
620 } else {
621 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
622 views, numActiveViews, GrSamplerState::Filter::kLinear);
623 }
624 } else
625 #endif
626 {
627 const bool hasGlyphPadding = fHead->fSubRun.glyphSrcPadding() > 0;
628 auto filter = fNeedsGlyphTransform && hasGlyphPadding
629 ? GrSamplerState::Filter::kLinear : GrSamplerState::Filter::kNearest;
630 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
631 }
632 }
633 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
634 GrSimpleMesh* mesh = target->allocMesh();
635 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
636 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
637 flushInfo->fVertexOffset);
638 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
639 GrPrimitiveType::kTriangles);
640 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
641 flushInfo->fGlyphsToFlush = 0;
642 ++flushInfo->fNumDraws;
643 }
644
645 GrOp::CombineResult AtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
646 auto that = t->cast<AtlasTextOp>();
647
648 if (fDFGPFlags != that->fDFGPFlags ||
649 fMaskType != that->fMaskType ||
650 fUsesLocalCoords != that->fUsesLocalCoords ||
651 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
652 fHasPerspective != that->fHasPerspective ||
653 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
654 // All flags must match for an op to be combined
655 return CombineResult::kCannotCombine;
656 }
657
658 // We use the same filter for every Geometry that is combined, but the filter choice only looks
659 // at the head's src padding, so we can only combine if we are consistent with that.
660 if (fHead->fSubRun.glyphSrcPadding() != that->fHead->fSubRun.glyphSrcPadding()) {
661 return CombineResult::kCannotCombine;
662 }
663
664 if (fProcessors != that->fProcessors) {
665 return CombineResult::kCannotCombine;
666 }
667
668 if (fUsesLocalCoords) {
669 // If the fragment processors use local coordinates, the GPs compute them using the inverse
670 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
671 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
672 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
673 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
674 return CombineResult::kCannotCombine;
675 }
676 }
677
678 #if !defined(SK_DISABLE_SDF_TEXT)
679 if (this->usesDistanceFields()) {
680 SkASSERT(that->usesDistanceFields());
681 if (fLuminanceColor != that->fLuminanceColor) {
682 return CombineResult::kCannotCombine;
683 }
684 } else
685 #endif
686 {
687 if (this->maskType() == MaskType::kColorBitmap &&
688 fHead->fColor != that->fHead->fColor) {
689 // This ensures all merged bitmap color text ops have a constant color
690 return CombineResult::kCannotCombine;
691 }
692 }
693
694 fNumGlyphs += that->fNumGlyphs;
695
696 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
697 this->addGeometry(that->fHead);
698 that->fHead = nullptr;
699 return CombineResult::kMerged;
700 }
701
702 #if !defined(SK_DISABLE_SDF_TEXT)
703 GrGeometryProcessor* AtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
704 const GrShaderCaps& caps,
705 const SkMatrix& localMatrix,
706 const GrSurfaceProxyView* views,
707 unsigned int numActiveViews) const {
708 auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
709
710 // see if we need to create a new effect
711 if (this->isLCD()) {
712 float redCorrection = dfAdjustTable->getAdjustment(SkColorGetR(fLuminanceColor),
713 fUseGammaCorrectDistanceTable);
714 float greenCorrection = dfAdjustTable->getAdjustment(SkColorGetG(fLuminanceColor),
715 fUseGammaCorrectDistanceTable);
716 float blueCorrection = dfAdjustTable->getAdjustment(SkColorGetB(fLuminanceColor),
717 fUseGammaCorrectDistanceTable);
718 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
719 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
720 redCorrection, greenCorrection, blueCorrection);
721 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
722 GrSamplerState::Filter::kLinear, widthAdjust,
723 fDFGPFlags, localMatrix);
724 } else {
725 #if defined(SK_GAMMA_APPLY_TO_A8)
726 float correction = 0;
727 if (this->maskType() != MaskType::kAliasedDistanceField) {
728 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, fLuminanceColor);
729 correction = dfAdjustTable->getAdjustment(lum, fUseGammaCorrectDistanceTable);
730 }
731 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
732 GrSamplerState::Filter::kLinear, correction,
733 fDFGPFlags, localMatrix);
734 #else
735 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
736 GrSamplerState::Filter::kLinear, fDFGPFlags,
737 localMatrix);
738 #endif
739 }
740 }
741 #endif // !defined(SK_DISABLE_SDF_TEXT)
742
743 } // namespace skgpu::ganesh
744
745