blob: c228a16539e973f8de089ce6b66e236444111d7b [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
joshualitt60ce86d2015-11-23 13:08:22 -080034///////////////////////////////////////////////////////////////////////////////////////////////////
35
Herb Derby3c873af2020-04-29 15:56:07 -040036GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
Herb Derby268e48b2020-07-16 12:56:58 -040037 bool needsTransform,
38 int glyphCount,
39 SkRect deviceRect,
Herb Derby6b748e42020-12-02 17:44:54 -050040 Geometry* geo,
Herb Derby1d17e492020-07-21 11:45:04 -040041 GrPaint&& paint)
Michael Ludwigefc89d22020-11-05 11:43:10 -050042 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050043 , fProcessors(std::move(paint))
44 , fNumGlyphs(glyphCount)
45 , fDFGPFlags(0)
46 , fMaskType(static_cast<uint32_t>(maskType))
47 , fUsesLocalCoords(false)
48 , fNeedsGlyphTransform(needsTransform)
Herb Derby6b748e42020-12-02 17:44:54 -050049 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
50 , fUseGammaCorrectDistanceTable(false)
51 , fHead{geo}
52 , fTail{&fHead->fNext} {
Herb Derby268e48b2020-07-16 12:56:58 -040053 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
54 // we treat this as a set of non-AA rects rendered with a texture.
55 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
56}
57
58GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
59 bool needsTransform,
60 int glyphCount,
61 SkRect deviceRect,
Herb Derby3c873af2020-04-29 15:56:07 -040062 SkColor luminanceColor,
63 bool useGammaCorrectDistanceTable,
Herb Derby268e48b2020-07-16 12:56:58 -040064 uint32_t DFGPFlags,
Herb Derby6b748e42020-12-02 17:44:54 -050065 Geometry* geo,
Herb Derby1d17e492020-07-21 11:45:04 -040066 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040067 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050068 , fProcessors(std::move(paint))
69 , fNumGlyphs(glyphCount)
70 , fDFGPFlags(DFGPFlags)
71 , fMaskType(static_cast<uint32_t>(maskType))
72 , fUsesLocalCoords(false)
73 , fNeedsGlyphTransform(needsTransform)
Herb Derby6b748e42020-12-02 17:44:54 -050074 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
Michael Ludwigefc89d22020-11-05 11:43:10 -050075 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
Herb Derby6b748e42020-12-02 17:44:54 -050076 , fLuminanceColor(luminanceColor)
77 , fHead{geo}
78 , fTail{&fHead->fNext} {
Herb Derby3c873af2020-04-29 15:56:07 -040079 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
80 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -040081 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -040082}
83
Herb Derby6b748e42020-12-02 17:44:54 -050084auto GrAtlasTextOp::Geometry::Make(GrRecordingContext* rc,
85 const GrAtlasSubRun& subRun,
86 const SkMatrix& drawMatrix,
87 SkPoint drawOrigin,
88 SkIRect clipRect,
89 GrTextBlob* blob,
90 const SkPMColor4f& color) -> Geometry* {
91 auto arena = rc->priv().recordTimeAllocator();
92 return arena->make<Geometry>(subRun,
93 drawMatrix,
94 drawOrigin,
95 clipRect,
96 SkRef(blob),
97 color);
98}
99
100
101
Herb Derby64391c42020-05-16 14:32:15 -0400102void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby40894182020-12-02 11:39:48 -0500103 SkMatrix positionMatrix = fDrawMatrix;
104 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
105 fSubRun.fillVertexData(
106 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -0400107}
108
Chris Dalton1706cbf2019-05-21 19:35:29 -0600109void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500110 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500111}
112
John Stiles8d9bf642020-08-12 15:07:45 -0400113#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400114SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800115 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -0500116 int i = 0;
Herb Derby6b748e42020-12-02 17:44:54 -0500117 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500118 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -0500119 i++,
Herb Derby6b748e42020-12-02 17:44:54 -0500120 geom->fColor.toBytes_RGBA(),
121 geom->fDrawOrigin.x(),
122 geom->fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800123 }
124
Brian Salomon44acb5b2017-07-18 19:59:24 -0400125 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800126 return str;
127}
Brian Osman9a390ac2018-11-12 09:47:48 -0500128#endif
joshualitta751c972015-11-20 13:37:32 -0800129
Brian Salomon44acb5b2017-07-18 19:59:24 -0400130GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
131 return FixedFunctionFlags::kNone;
132}
133
Chris Dalton6ce447a2019-06-23 18:07:38 -0600134GrProcessorSet::Analysis GrAtlasTextOp::finalize(
135 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
136 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400137 GrProcessorAnalysisCoverage coverage;
138 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500139 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400140 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800141 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500142 // finalize() is called before any merging is done, so at this point there's at most one
143 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Herb Derby6b748e42020-12-02 17:44:54 -0500144 color.setToConstant(fHead->fColor);
joshualitta751c972015-11-20 13:37:32 -0800145 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500146
Michael Ludwigefc89d22020-11-05 11:43:10 -0500147 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500148 case MaskType::kGrayscaleCoverage:
149 case MaskType::kAliasedDistanceField:
150 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400151 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800152 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500153 case MaskType::kLCDCoverage:
154 case MaskType::kLCDDistanceField:
155 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400156 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800157 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500158 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400159 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400160 break;
joshualitta751c972015-11-20 13:37:32 -0800161 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500162
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700163 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600164 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Herb Derby6b748e42020-12-02 17:44:54 -0500165 clampType, &fHead->fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500166 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
167 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
168 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400169 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700170 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800171}
172
Brian Salomon91326c32017-08-09 16:02:19 -0400173void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500174 auto resourceProvider = target->resourceProvider();
175
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500176 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
177 // processor analysis will not require local coords and the GPs will skip local coords when
178 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
179 // all geometries to have same draw matrix.
180 SkMatrix localMatrix = SkMatrix::I();
Herb Derby6b748e42020-12-02 17:44:54 -0500181 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800182 return;
183 }
184
Robert Phillips5a66efb2018-03-07 15:13:18 -0500185 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000186
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400187 GrMaskFormat maskFormat = this->maskFormat();
188
Greg Daniel9715b6c2019-12-10 15:03:10 -0500189 unsigned int numActiveViews;
190 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
191 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800192 SkDebugf("Could not allocate backing texture for atlas\n");
193 return;
194 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500195 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800196
Brian Salomon7eae3e02018-08-07 14:02:38 +0000197 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500198 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
199 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000200
Chris Dalton304e14d2020-03-17 14:29:06 -0600201 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500202 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600203 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400204 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
205 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500206 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000207 }
Brian Salomon49348902018-06-26 09:12:38 -0400208
209 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600210 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400211 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400212
joshualittd9d30f72015-12-08 10:47:55 -0800213 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500214 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
215 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500216 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800217 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400218 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500219 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500220 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
221 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500222 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Herb Derby6b748e42020-12-02 17:44:54 -0500223 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
Michael Ludwig64596c52020-11-05 12:39:13 -0500224 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800225 }
226
Herb Derbyfd894ff2020-07-15 13:23:33 -0400227 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800228
Brian Salomon43cbd722020-01-03 22:09:12 -0500229 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500230 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
231 const int quadSize = vertexStride * kVerticesPerGlyph;
232 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
233
Herb Derbyfd894ff2020-07-15 13:23:33 -0400234 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500235 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400236 int quadCursor;
237 int quadEnd;
238 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500239
Herb Derbyfd894ff2020-07-15 13:23:33 -0400240 auto resetVertexBuffer = [&] {
241 quadCursor = 0;
242 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800243
Herb Derbyfd894ff2020-07-15 13:23:33 -0400244 vertices = (char*)target->makeVertexSpace(
245 vertexStride,
246 kVerticesPerGlyph * quadEnd,
247 &flushInfo.fVertexBuffer,
248 &flushInfo.fVertexOffset);
249
250 if (!vertices || !flushInfo.fVertexBuffer) {
251 SkDebugf("Could not allocate vertices\n");
252 return false;
253 }
254 return true;
255 };
256
257 resetVertexBuffer();
258
Herb Derby6b748e42020-12-02 17:44:54 -0500259 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
260 const GrAtlasSubRun& subRun = geo->fSubRun;
261 SkASSERT((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500262
Herb Derby43ad7912020-07-20 16:14:19 -0400263 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400264 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
265 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
266 // of the glyphs to fill the vertex buffer.
267 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400268 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500269 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400270 if (!ok) {
271 return;
272 }
Herb Derby23f29762020-01-10 16:26:14 -0500273
Herb Derby6b748e42020-12-02 17:44:54 -0500274 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500275
Herb Derbyfd894ff2020-07-15 13:23:33 -0400276 subRunCursor += glyphsRegenerated;
277 quadCursor += glyphsRegenerated;
278 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500279 flushInfo.fGlyphsToFlush += glyphsRegenerated;
280
Herb Derbyfd894ff2020-07-15 13:23:33 -0400281 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
282 // Flush if not all the glyphs are drawn because either the quad buffer is full or
283 // the atlas is out of space.
Herb Derby89acfe72021-01-28 10:57:40 -0500284 if (subRunCursor < subRunEnd) {
285 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
286 }
Herb Derby4513cdd2020-01-31 13:28:49 -0500287 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400288 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
289 // If the vertex buffer is full and there are still glyphs to draw then
290 // get a new buffer.
291 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500292 return;
293 }
294 }
295 }
296 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400297 }
joshualitta751c972015-11-20 13:37:32 -0800298}
299
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700300void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500301 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
302 std::move(fProcessors),
303 GrPipeline::InputFlags::kNone);
304
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600305 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
306 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700307}
308
Herb Derby4513cdd2020-01-31 13:28:49 -0500309void GrAtlasTextOp::createDrawForGeneratedGlyphs(
310 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500311 if (!flushInfo->fGlyphsToFlush) {
312 return;
313 }
314
Robert Phillips5a66efb2018-03-07 15:13:18 -0500315 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500316
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500317 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400318 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500319
Greg Daniel9715b6c2019-12-10 15:03:10 -0500320 unsigned int numActiveViews;
321 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
322 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500323 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500324 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500325 return;
326 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500327 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400328 // During preparation the number of atlas pages has increased.
329 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500330 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600331 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400332 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
333 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500334 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500335 // These will get unreffed when the previously recorded draws destruct.
336 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600337 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500338 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000339 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400340 if (this->usesDistanceFields()) {
341 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500342 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400343 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400344 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500345 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400346 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400347 }
348 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400349 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500350 : GrSamplerState::Filter::kNearest;
351 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400352 }
353 }
Brian Salomondbf70722019-02-07 11:31:24 -0500354 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600355 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600356 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
357 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
358 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600359 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600360 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800361 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
362 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500363 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800364}
365
Herb Derbye25c3002020-10-27 15:57:27 -0400366GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500367 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500368
369 if (fDFGPFlags != that->fDFGPFlags ||
370 fMaskType != that->fMaskType ||
371 fUsesLocalCoords != that->fUsesLocalCoords ||
372 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
373 fHasPerspective != that->fHasPerspective ||
374 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
375 // All flags must match for an op to be combined
376 return CombineResult::kCannotCombine;
377 }
378
Brian Salomon44acb5b2017-07-18 19:59:24 -0400379 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000380 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400381 }
382
Michael Ludwigefc89d22020-11-05 11:43:10 -0500383 if (fUsesLocalCoords) {
384 // If the fragment processors use local coordinates, the GPs compute them using the inverse
385 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Herb Derby6b748e42020-12-02 17:44:54 -0500386 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
387 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500388 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
389 return CombineResult::kCannotCombine;
390 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400391 }
392
Brian Salomon5c6ac642017-12-19 11:09:32 -0500393 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500394 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400395 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000396 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800397 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500398 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500399 if (this->maskType() == MaskType::kColorBitmap &&
Herb Derby6b748e42020-12-02 17:44:54 -0500400 fHead->fColor != that->fHead->fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500401 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000402 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500403 }
joshualitta751c972015-11-20 13:37:32 -0800404 }
405
Michael Ludwigefc89d22020-11-05 11:43:10 -0500406 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800407
Michael Ludwig64596c52020-11-05 12:39:13 -0500408 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
Herb Derby6b748e42020-12-02 17:44:54 -0500409 this->addGeometry(that->fHead);
410 that->fHead = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000411 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800412}
413
joshualitta751c972015-11-20 13:37:32 -0800414// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500415GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
416 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500417 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500418 const GrSurfaceProxyView* views,
419 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500420 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400421 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
422
joshualitta751c972015-11-20 13:37:32 -0800423 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500424 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400425 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400426 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500427 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400428 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400429 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500430 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400431 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400432 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500433 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800434 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500435 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
436 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500437 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400438 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500439 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800440 } else {
joshualitta751c972015-11-20 13:37:32 -0800441#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400442 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500443 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400444 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
445 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400446 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
447 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400448 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500449 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400450 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500451 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800452#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500453 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400454 GrSamplerState::Filter::kLinear, fDFGPFlags,
455 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800456#endif
457 }
joshualitta751c972015-11-20 13:37:32 -0800458}
joshualittddd22d82016-02-16 06:47:52 -0800459
Herb Derby4598fa12020-06-10 14:54:22 -0400460#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400461
Brian Salomoneebe7352020-12-09 16:37:04 -0500462GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc,
Herb Derbyc76d4092020-10-07 16:46:15 -0400463 const SkPaint& skPaint,
464 const SkFont& font,
465 const SkMatrixProvider& mtxProvider,
466 const char* text,
467 int x,
468 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400469 size_t textLen = (int)strlen(text);
470
471 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
472
473 auto drawOrigin = SkPoint::Make(x, y);
474 SkGlyphRunBuilder builder;
475 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
476
477 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400478 if (glyphRunList.empty()) {
479 return nullptr;
480 }
Herb Derby4598fa12020-06-10 14:54:22 -0400481
Brian Salomon70fe17e2020-11-30 14:33:58 -0500482 auto rContext = rtc->recordingContext();
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400483 GrSDFTOptions SDFOptions = rContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400484
Herb Derby4598fa12020-06-10 14:54:22 -0400485 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500486 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby281583a2020-11-19 11:40:07 -0500487 painter->processGlyphRun(
488 *glyphRunList.begin(),
489 drawMatrix, glyphRunList.origin(),
490 glyphRunList.paint(),
491 rtc->surfaceProps(),
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400492 rContext->priv().caps()->shaderCaps()->supportsDistanceFieldText(),
Herb Derby4598fa12020-06-10 14:54:22 -0400493 SDFOptions, blob.get());
Herb Derby55f795e2021-02-05 13:45:05 -0500494 if (blob->subRunList().isEmpty()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400495 return nullptr;
496 }
Herb Derby4598fa12020-06-10 14:54:22 -0400497
Herb Derby55f795e2021-02-05 13:45:05 -0500498 GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -0500499 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400500 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400501 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400502 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400503}
504
505GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
Brian Salomoneebe7352020-12-09 16:37:04 -0500506 // Setup dummy SkPaint / GrPaint / GrSurfaceDrawContext
507 auto rtc = GrSurfaceDrawContext::Make(
Herb Derby4598fa12020-06-10 14:54:22 -0400508 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
509
510 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
511
512 SkPaint skPaint;
513 skPaint.setColor(random->nextU());
514
515 SkFont font;
516 if (random->nextBool()) {
517 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
518 } else {
519 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
520 }
521 font.setSubpixel(random->nextBool());
522
523 const char* text = "The quick brown fox jumps over the lazy dog.";
524
525 // create some random x/y offsets, including negative offsets
526 static const int kMaxTrans = 1024;
527 int xPos = (random->nextU() % 2) * 2 - 1;
528 int yPos = (random->nextU() % 2) * 2 - 1;
529 int xInt = (random->nextU() % kMaxTrans) * xPos;
530 int yInt = (random->nextU() % kMaxTrans) * yPos;
531
532 return GrAtlasTextOp::CreateOpTestingOnly(
533 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
534}
535
536#endif