blob: b52a19e0f04aa933f038e6c71bbac8ee0162800b [file] [log] [blame]
joshualitta751c972015-11-20 13:37:32 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrAtlasTextOp.h"
Robert Phillipse4fda6c2018-02-21 12:10:41 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint3.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkMathPriv.h"
13#include "src/core/SkMatrixPriv.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040014#include "src/core/SkMatrixProvider.h"
Herb Derbyfd894ff2020-07-15 13:23:33 -040015#include "src/core/SkSpan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkStrikeCache.h"
17#include "src/gpu/GrCaps.h"
18#include "src/gpu/GrMemoryPool.h"
19#include "src/gpu/GrOpFlushState.h"
20#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050022#include "src/gpu/GrSurfaceDrawContext.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040023#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/effects/GrBitmapTextGeoProc.h"
25#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050026#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040028#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080029
Herb Derbya08bde62020-06-12 15:46:06 -040030#if GR_TEST_UTILS
31#include "src/gpu/GrDrawOpTest.h"
32#endif
33
Herb Derby4b1fb7c2021-02-26 12:18:26 -050034#if !defined(GR_OP_ALLOCATE_USE_POOL) && defined(GR_HAS_THREAD_LOCAL)
35static thread_local std::unique_ptr<char> gGrAtlasTextOpCache = nullptr;
36void* GrAtlasTextOp::operator new(size_t s) {
37 if (gGrAtlasTextOpCache != nullptr) {
38 return gGrAtlasTextOpCache.release();
39 } else {
40 return ::operator new(s);
41 }
42}
43
44void GrAtlasTextOp::operator delete(void* b) noexcept {
45 if (gGrAtlasTextOpCache == nullptr) {
46 gGrAtlasTextOpCache.reset(static_cast<char*>(b));
47 } else {
48 ::operator delete(b);
49 }
50}
51#endif
joshualitt60ce86d2015-11-23 13:08:22 -080052
Herb Derby3c873af2020-04-29 15:56:07 -040053GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
Herb Derby268e48b2020-07-16 12:56:58 -040054 bool needsTransform,
55 int glyphCount,
56 SkRect deviceRect,
Herb Derby6b748e42020-12-02 17:44:54 -050057 Geometry* geo,
Herb Derby1d17e492020-07-21 11:45:04 -040058 GrPaint&& paint)
Michael Ludwigefc89d22020-11-05 11:43:10 -050059 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050060 , fProcessors(std::move(paint))
61 , fNumGlyphs(glyphCount)
62 , fDFGPFlags(0)
63 , fMaskType(static_cast<uint32_t>(maskType))
64 , fUsesLocalCoords(false)
65 , fNeedsGlyphTransform(needsTransform)
Herb Derby6b748e42020-12-02 17:44:54 -050066 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
67 , fUseGammaCorrectDistanceTable(false)
68 , fHead{geo}
69 , fTail{&fHead->fNext} {
Herb Derby268e48b2020-07-16 12:56:58 -040070 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
71 // we treat this as a set of non-AA rects rendered with a texture.
72 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
73}
74
75GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
76 bool needsTransform,
77 int glyphCount,
78 SkRect deviceRect,
Herb Derby3c873af2020-04-29 15:56:07 -040079 SkColor luminanceColor,
80 bool useGammaCorrectDistanceTable,
Herb Derby268e48b2020-07-16 12:56:58 -040081 uint32_t DFGPFlags,
Herb Derby6b748e42020-12-02 17:44:54 -050082 Geometry* geo,
Herb Derby1d17e492020-07-21 11:45:04 -040083 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040084 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050085 , fProcessors(std::move(paint))
86 , fNumGlyphs(glyphCount)
87 , fDFGPFlags(DFGPFlags)
88 , fMaskType(static_cast<uint32_t>(maskType))
89 , fUsesLocalCoords(false)
90 , fNeedsGlyphTransform(needsTransform)
Herb Derby6b748e42020-12-02 17:44:54 -050091 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
Michael Ludwigefc89d22020-11-05 11:43:10 -050092 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
Herb Derby6b748e42020-12-02 17:44:54 -050093 , fLuminanceColor(luminanceColor)
94 , fHead{geo}
95 , fTail{&fHead->fNext} {
Herb Derby3c873af2020-04-29 15:56:07 -040096 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
97 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -040098 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -040099}
100
Herb Derby6b748e42020-12-02 17:44:54 -0500101auto GrAtlasTextOp::Geometry::Make(GrRecordingContext* rc,
102 const GrAtlasSubRun& subRun,
103 const SkMatrix& drawMatrix,
104 SkPoint drawOrigin,
105 SkIRect clipRect,
Herb Derby4f78f232021-02-18 10:42:35 -0500106 sk_sp<GrTextBlob> blob,
Herb Derby6b748e42020-12-02 17:44:54 -0500107 const SkPMColor4f& color) -> Geometry* {
108 auto arena = rc->priv().recordTimeAllocator();
Herb Derby4f78f232021-02-18 10:42:35 -0500109 // Bypass the automatic dtor behavior in SkArenaAlloc. I'm leaving this up to the Op to run
110 // all geometry dtors for now.
111 void* geo = arena->makeBytesAlignedTo(sizeof(Geometry), alignof(Geometry));
112 return new (geo) Geometry{subRun,
113 drawMatrix,
114 drawOrigin,
115 clipRect,
116 std::move(blob),
117 color};
Herb Derby6b748e42020-12-02 17:44:54 -0500118}
119
120
121
Herb Derby64391c42020-05-16 14:32:15 -0400122void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby40894182020-12-02 11:39:48 -0500123 SkMatrix positionMatrix = fDrawMatrix;
124 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
125 fSubRun.fillVertexData(
126 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -0400127}
128
Chris Dalton1706cbf2019-05-21 19:35:29 -0600129void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500130 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500131}
132
John Stiles8d9bf642020-08-12 15:07:45 -0400133#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400134SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800135 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -0500136 int i = 0;
Herb Derby6b748e42020-12-02 17:44:54 -0500137 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500138 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -0500139 i++,
Herb Derby6b748e42020-12-02 17:44:54 -0500140 geom->fColor.toBytes_RGBA(),
141 geom->fDrawOrigin.x(),
142 geom->fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800143 }
144
Brian Salomon44acb5b2017-07-18 19:59:24 -0400145 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800146 return str;
147}
Brian Osman9a390ac2018-11-12 09:47:48 -0500148#endif
joshualitta751c972015-11-20 13:37:32 -0800149
Brian Salomon44acb5b2017-07-18 19:59:24 -0400150GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
151 return FixedFunctionFlags::kNone;
152}
153
Chris Dalton6ce447a2019-06-23 18:07:38 -0600154GrProcessorSet::Analysis GrAtlasTextOp::finalize(
155 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
156 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400157 GrProcessorAnalysisCoverage coverage;
158 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500159 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400160 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800161 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500162 // finalize() is called before any merging is done, so at this point there's at most one
163 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Herb Derby6b748e42020-12-02 17:44:54 -0500164 color.setToConstant(fHead->fColor);
joshualitta751c972015-11-20 13:37:32 -0800165 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500166
Michael Ludwigefc89d22020-11-05 11:43:10 -0500167 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500168 case MaskType::kGrayscaleCoverage:
169 case MaskType::kAliasedDistanceField:
170 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400171 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800172 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500173 case MaskType::kLCDCoverage:
174 case MaskType::kLCDDistanceField:
175 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400176 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800177 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500178 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400179 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400180 break;
joshualitta751c972015-11-20 13:37:32 -0800181 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500182
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700183 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600184 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Herb Derby6b748e42020-12-02 17:44:54 -0500185 clampType, &fHead->fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500186 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
187 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
188 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400189 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700190 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800191}
192
Brian Salomon91326c32017-08-09 16:02:19 -0400193void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500194 auto resourceProvider = target->resourceProvider();
195
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500196 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
197 // processor analysis will not require local coords and the GPs will skip local coords when
198 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
199 // all geometries to have same draw matrix.
200 SkMatrix localMatrix = SkMatrix::I();
Herb Derby6b748e42020-12-02 17:44:54 -0500201 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800202 return;
203 }
204
Robert Phillips5a66efb2018-03-07 15:13:18 -0500205 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000206
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400207 GrMaskFormat maskFormat = this->maskFormat();
208
Greg Daniel9715b6c2019-12-10 15:03:10 -0500209 unsigned int numActiveViews;
210 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
211 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800212 SkDebugf("Could not allocate backing texture for atlas\n");
213 return;
214 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500215 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800216
Brian Salomon7eae3e02018-08-07 14:02:38 +0000217 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500218 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
219 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000220
Chris Dalton304e14d2020-03-17 14:29:06 -0600221 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500222 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600223 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400224 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
225 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500226 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000227 }
Brian Salomon49348902018-06-26 09:12:38 -0400228
229 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600230 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400231 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400232
joshualittd9d30f72015-12-08 10:47:55 -0800233 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500234 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
235 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500236 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800237 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400238 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500239 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500240 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
241 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500242 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Herb Derby6b748e42020-12-02 17:44:54 -0500243 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
Michael Ludwig64596c52020-11-05 12:39:13 -0500244 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800245 }
246
Herb Derbyfd894ff2020-07-15 13:23:33 -0400247 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800248
Brian Salomon43cbd722020-01-03 22:09:12 -0500249 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500250 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
251 const int quadSize = vertexStride * kVerticesPerGlyph;
252 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
253
Herb Derbyfd894ff2020-07-15 13:23:33 -0400254 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500255 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400256 int quadCursor;
257 int quadEnd;
258 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500259
Herb Derbyfd894ff2020-07-15 13:23:33 -0400260 auto resetVertexBuffer = [&] {
261 quadCursor = 0;
262 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800263
Herb Derbyfd894ff2020-07-15 13:23:33 -0400264 vertices = (char*)target->makeVertexSpace(
265 vertexStride,
266 kVerticesPerGlyph * quadEnd,
267 &flushInfo.fVertexBuffer,
268 &flushInfo.fVertexOffset);
269
270 if (!vertices || !flushInfo.fVertexBuffer) {
271 SkDebugf("Could not allocate vertices\n");
272 return false;
273 }
274 return true;
275 };
276
277 resetVertexBuffer();
278
Herb Derby6b748e42020-12-02 17:44:54 -0500279 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
280 const GrAtlasSubRun& subRun = geo->fSubRun;
281 SkASSERT((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500282
Herb Derby43ad7912020-07-20 16:14:19 -0400283 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400284 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
285 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
286 // of the glyphs to fill the vertex buffer.
287 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400288 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500289 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400290 if (!ok) {
291 return;
292 }
Herb Derby23f29762020-01-10 16:26:14 -0500293
Herb Derby6b748e42020-12-02 17:44:54 -0500294 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500295
Herb Derbyfd894ff2020-07-15 13:23:33 -0400296 subRunCursor += glyphsRegenerated;
297 quadCursor += glyphsRegenerated;
298 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500299 flushInfo.fGlyphsToFlush += glyphsRegenerated;
300
Herb Derbyfd894ff2020-07-15 13:23:33 -0400301 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
302 // Flush if not all the glyphs are drawn because either the quad buffer is full or
303 // the atlas is out of space.
Herb Derby89acfe72021-01-28 10:57:40 -0500304 if (subRunCursor < subRunEnd) {
305 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
306 }
Herb Derby4513cdd2020-01-31 13:28:49 -0500307 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400308 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
309 // If the vertex buffer is full and there are still glyphs to draw then
310 // get a new buffer.
311 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500312 return;
313 }
314 }
315 }
316 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400317 }
joshualitta751c972015-11-20 13:37:32 -0800318}
319
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700320void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500321 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
322 std::move(fProcessors),
323 GrPipeline::InputFlags::kNone);
324
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600325 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
326 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700327}
328
Herb Derby4513cdd2020-01-31 13:28:49 -0500329void GrAtlasTextOp::createDrawForGeneratedGlyphs(
330 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500331 if (!flushInfo->fGlyphsToFlush) {
332 return;
333 }
334
Robert Phillips5a66efb2018-03-07 15:13:18 -0500335 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500336
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500337 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400338 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500339
Greg Daniel9715b6c2019-12-10 15:03:10 -0500340 unsigned int numActiveViews;
341 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
342 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500343 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500344 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500345 return;
346 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500347 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400348 // During preparation the number of atlas pages has increased.
349 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500350 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600351 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400352 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
353 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500354 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500355 // These will get unreffed when the previously recorded draws destruct.
356 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600357 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500358 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000359 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400360 if (this->usesDistanceFields()) {
361 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500362 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400363 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400364 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500365 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400366 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400367 }
368 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400369 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500370 : GrSamplerState::Filter::kNearest;
371 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400372 }
373 }
Brian Salomondbf70722019-02-07 11:31:24 -0500374 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600375 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600376 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
377 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
378 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600379 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600380 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800381 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
382 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500383 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800384}
385
Herb Derbye25c3002020-10-27 15:57:27 -0400386GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500387 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500388
389 if (fDFGPFlags != that->fDFGPFlags ||
390 fMaskType != that->fMaskType ||
391 fUsesLocalCoords != that->fUsesLocalCoords ||
392 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
393 fHasPerspective != that->fHasPerspective ||
394 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
395 // All flags must match for an op to be combined
396 return CombineResult::kCannotCombine;
397 }
398
Brian Salomon44acb5b2017-07-18 19:59:24 -0400399 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000400 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400401 }
402
Michael Ludwigefc89d22020-11-05 11:43:10 -0500403 if (fUsesLocalCoords) {
404 // If the fragment processors use local coordinates, the GPs compute them using the inverse
405 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Herb Derby6b748e42020-12-02 17:44:54 -0500406 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
407 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500408 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
409 return CombineResult::kCannotCombine;
410 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400411 }
412
Brian Salomon5c6ac642017-12-19 11:09:32 -0500413 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500414 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400415 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000416 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800417 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500418 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500419 if (this->maskType() == MaskType::kColorBitmap &&
Herb Derby6b748e42020-12-02 17:44:54 -0500420 fHead->fColor != that->fHead->fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500421 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000422 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500423 }
joshualitta751c972015-11-20 13:37:32 -0800424 }
425
Michael Ludwigefc89d22020-11-05 11:43:10 -0500426 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800427
Michael Ludwig64596c52020-11-05 12:39:13 -0500428 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
Herb Derby6b748e42020-12-02 17:44:54 -0500429 this->addGeometry(that->fHead);
430 that->fHead = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000431 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800432}
433
joshualitta751c972015-11-20 13:37:32 -0800434// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500435GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
436 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500437 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500438 const GrSurfaceProxyView* views,
439 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500440 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400441 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
442
joshualitta751c972015-11-20 13:37:32 -0800443 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500444 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400445 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400446 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500447 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400448 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400449 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500450 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400451 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400452 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500453 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800454 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500455 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
456 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500457 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400458 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500459 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800460 } else {
joshualitta751c972015-11-20 13:37:32 -0800461#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400462 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500463 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400464 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
465 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400466 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
467 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400468 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500469 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400470 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500471 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800472#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500473 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400474 GrSamplerState::Filter::kLinear, fDFGPFlags,
475 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800476#endif
477 }
joshualitta751c972015-11-20 13:37:32 -0800478}
joshualittddd22d82016-02-16 06:47:52 -0800479
Herb Derby4598fa12020-06-10 14:54:22 -0400480#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400481
Brian Salomoneebe7352020-12-09 16:37:04 -0500482GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc,
Herb Derbyc76d4092020-10-07 16:46:15 -0400483 const SkPaint& skPaint,
484 const SkFont& font,
485 const SkMatrixProvider& mtxProvider,
486 const char* text,
487 int x,
488 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400489 size_t textLen = (int)strlen(text);
490
Herb Derby861ef8f2021-02-26 16:49:06 -0500491 SkMatrix drawMatrix(mtxProvider.localToDevice());
492 drawMatrix.preTranslate(x, y);
Herb Derby4598fa12020-06-10 14:54:22 -0400493 auto drawOrigin = SkPoint::Make(x, y);
494 SkGlyphRunBuilder builder;
495 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
496
497 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400498 if (glyphRunList.empty()) {
499 return nullptr;
500 }
Herb Derby4598fa12020-06-10 14:54:22 -0400501
Brian Salomon70fe17e2020-11-30 14:33:58 -0500502 auto rContext = rtc->recordingContext();
Mike Reed9edff532021-03-05 11:15:07 +0000503 GrSDFTOptions SDFOptions =
504 rContext->priv().getSDFTOptions(rtc->surfaceProps().isUseDeviceIndependentFonts());
Herb Derby4598fa12020-06-10 14:54:22 -0400505
Herb Derby4598fa12020-06-10 14:54:22 -0400506 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500507 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby281583a2020-11-19 11:40:07 -0500508 painter->processGlyphRun(
509 *glyphRunList.begin(),
Herb Derby861ef8f2021-02-26 16:49:06 -0500510 drawMatrix,
Herb Derby281583a2020-11-19 11:40:07 -0500511 glyphRunList.paint(),
Mike Reed9edff532021-03-05 11:15:07 +0000512 SDFOptions,
Herb Derby9ad09822021-03-01 14:19:36 -0500513 blob.get());
Herb Derby55f795e2021-02-05 13:45:05 -0500514 if (blob->subRunList().isEmpty()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400515 return nullptr;
516 }
Herb Derby4598fa12020-06-10 14:54:22 -0400517
Herb Derby55f795e2021-02-05 13:45:05 -0500518 GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -0500519 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400520 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400521 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400522 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400523}
524
525GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
Brian Salomoneebe7352020-12-09 16:37:04 -0500526 // Setup dummy SkPaint / GrPaint / GrSurfaceDrawContext
527 auto rtc = GrSurfaceDrawContext::Make(
Herb Derby4598fa12020-06-10 14:54:22 -0400528 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
529
530 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
531
532 SkPaint skPaint;
533 skPaint.setColor(random->nextU());
534
535 SkFont font;
536 if (random->nextBool()) {
537 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
538 } else {
539 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
540 }
541 font.setSubpixel(random->nextBool());
542
543 const char* text = "The quick brown fox jumps over the lazy dog.";
544
545 // create some random x/y offsets, including negative offsets
546 static const int kMaxTrans = 1024;
547 int xPos = (random->nextU() % 2) * 2 - 1;
548 int yPos = (random->nextU() % 2) * 2 - 1;
549 int xInt = (random->nextU() % kMaxTrans) * xPos;
550 int yInt = (random->nextU() % kMaxTrans) * yPos;
551
552 return GrAtlasTextOp::CreateOpTestingOnly(
553 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
554}
555
556#endif