Debian Codegraph @

src/gpu/ganesh/ops/AtlasTextOp.cpp

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