blob: e86860485440b1ca20be01cfb03a316c9ad09fda [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"
Herb Derby4598fa12020-06-10 14:54:22 -040021#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrResourceProvider.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 Derby1d17e492020-07-21 11:45:04 -040040 const Geometry& geo,
41 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)
49 , fHasPerspective(needsTransform && geo.fDrawMatrix.hasPerspective())
50 , fUseGammaCorrectDistanceTable(false) {
Michael Ludwig64596c52020-11-05 12:39:13 -050051 fGeometries.push_back(geo);
Herb Derby268e48b2020-07-16 12:56:58 -040052 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
53 // we treat this as a set of non-AA rects rendered with a texture.
54 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
55}
56
57GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
58 bool needsTransform,
59 int glyphCount,
60 SkRect deviceRect,
Herb Derby3c873af2020-04-29 15:56:07 -040061 SkColor luminanceColor,
62 bool useGammaCorrectDistanceTable,
Herb Derby268e48b2020-07-16 12:56:58 -040063 uint32_t DFGPFlags,
Herb Derby1d17e492020-07-21 11:45:04 -040064 const Geometry& geo,
65 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040066 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050067 , fProcessors(std::move(paint))
68 , fNumGlyphs(glyphCount)
69 , fDFGPFlags(DFGPFlags)
70 , fMaskType(static_cast<uint32_t>(maskType))
71 , fUsesLocalCoords(false)
72 , fNeedsGlyphTransform(needsTransform)
73 , fHasPerspective(needsTransform && geo.fDrawMatrix.hasPerspective())
74 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
75 , fLuminanceColor(luminanceColor) {
Michael Ludwig64596c52020-11-05 12:39:13 -050076 fGeometries.push_back(geo);
Herb Derby3c873af2020-04-29 15:56:07 -040077 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
78 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -040079 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -040080}
81
Herb Derby64391c42020-05-16 14:32:15 -040082void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby43ad7912020-07-20 16:14:19 -040083 fSubRun.fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
84 fDrawMatrix, fDrawOrigin, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -040085}
86
Chris Dalton1706cbf2019-05-21 19:35:29 -060087void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050088 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050089}
90
John Stiles8d9bf642020-08-12 15:07:45 -040091#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -040092SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080093 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -050094 int i = 0;
95 for (const auto& g : fGeometries.items()) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050096 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -050097 i++,
98 g.fColor.toBytes_RGBA(),
99 g.fDrawOrigin.x(),
100 g.fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800101 }
102
Brian Salomon44acb5b2017-07-18 19:59:24 -0400103 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800104 return str;
105}
Brian Osman9a390ac2018-11-12 09:47:48 -0500106#endif
joshualitta751c972015-11-20 13:37:32 -0800107
Brian Salomon44acb5b2017-07-18 19:59:24 -0400108GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
109 return FixedFunctionFlags::kNone;
110}
111
Chris Dalton6ce447a2019-06-23 18:07:38 -0600112GrProcessorSet::Analysis GrAtlasTextOp::finalize(
113 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
114 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400115 GrProcessorAnalysisCoverage coverage;
116 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500117 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400118 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800119 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500120 // finalize() is called before any merging is done, so at this point there's at most one
121 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Michael Ludwig64596c52020-11-05 12:39:13 -0500122 color.setToConstant(fGeometries.front().fColor);
joshualitta751c972015-11-20 13:37:32 -0800123 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500124
Michael Ludwigefc89d22020-11-05 11:43:10 -0500125 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500126 case MaskType::kGrayscaleCoverage:
127 case MaskType::kAliasedDistanceField:
128 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400129 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800130 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500131 case MaskType::kLCDCoverage:
132 case MaskType::kLCDDistanceField:
133 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400134 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800135 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500136 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400137 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400138 break;
joshualitta751c972015-11-20 13:37:32 -0800139 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500140
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700141 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600142 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Michael Ludwig64596c52020-11-05 12:39:13 -0500143 clampType, &fGeometries.front().fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500144 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
145 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
146 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400147 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700148 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800149}
150
Brian Salomon91326c32017-08-09 16:02:19 -0400151void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500152 auto resourceProvider = target->resourceProvider();
153
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500154 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
155 // processor analysis will not require local coords and the GPs will skip local coords when
156 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
157 // all geometries to have same draw matrix.
158 SkMatrix localMatrix = SkMatrix::I();
Michael Ludwig64596c52020-11-05 12:39:13 -0500159 if (fUsesLocalCoords && !fGeometries.front().fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800160 return;
161 }
162
Robert Phillips5a66efb2018-03-07 15:13:18 -0500163 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000164
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400165 GrMaskFormat maskFormat = this->maskFormat();
166
Greg Daniel9715b6c2019-12-10 15:03:10 -0500167 unsigned int numActiveViews;
168 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
169 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800170 SkDebugf("Could not allocate backing texture for atlas\n");
171 return;
172 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500173 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800174
Brian Salomon7eae3e02018-08-07 14:02:38 +0000175 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500176 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
177 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000178
Chris Dalton304e14d2020-03-17 14:29:06 -0600179 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500180 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600181 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400182 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
183 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500184 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000185 }
Brian Salomon49348902018-06-26 09:12:38 -0400186
187 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600188 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400189 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400190
joshualittd9d30f72015-12-08 10:47:55 -0800191 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500192 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
193 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500194 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800195 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400196 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500197 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500198 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
199 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500200 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Michael Ludwig64596c52020-11-05 12:39:13 -0500201 target->allocator(), *target->caps().shaderCaps(), fGeometries.front().fColor,
202 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800203 }
204
Herb Derbyfd894ff2020-07-15 13:23:33 -0400205 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800206
Brian Salomon43cbd722020-01-03 22:09:12 -0500207 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500208 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
209 const int quadSize = vertexStride * kVerticesPerGlyph;
210 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
211
Herb Derbyfd894ff2020-07-15 13:23:33 -0400212 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500213 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400214 int quadCursor;
215 int quadEnd;
216 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500217
Herb Derbyfd894ff2020-07-15 13:23:33 -0400218 auto resetVertexBuffer = [&] {
219 quadCursor = 0;
220 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800221
Herb Derbyfd894ff2020-07-15 13:23:33 -0400222 vertices = (char*)target->makeVertexSpace(
223 vertexStride,
224 kVerticesPerGlyph * quadEnd,
225 &flushInfo.fVertexBuffer,
226 &flushInfo.fVertexOffset);
227
228 if (!vertices || !flushInfo.fVertexBuffer) {
229 SkDebugf("Could not allocate vertices\n");
230 return false;
231 }
232 return true;
233 };
234
235 resetVertexBuffer();
236
Michael Ludwig64596c52020-11-05 12:39:13 -0500237 for (const Geometry& geo : fGeometries.items()) {
Herb Derby43ad7912020-07-20 16:14:19 -0400238 const GrAtlasSubRun& subRun = geo.fSubRun;
239 SkASSERT((int)subRun.vertexStride() == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500240
Herb Derby43ad7912020-07-20 16:14:19 -0400241 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400242 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
243 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
244 // of the glyphs to fill the vertex buffer.
245 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400246 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500247 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400248 if (!ok) {
249 return;
250 }
Herb Derby23f29762020-01-10 16:26:14 -0500251
Herb Derbyfd894ff2020-07-15 13:23:33 -0400252 geo.fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500253
Herb Derbyfd894ff2020-07-15 13:23:33 -0400254 subRunCursor += glyphsRegenerated;
255 quadCursor += glyphsRegenerated;
256 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500257 flushInfo.fGlyphsToFlush += glyphsRegenerated;
258
Herb Derbyfd894ff2020-07-15 13:23:33 -0400259 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
260 // Flush if not all the glyphs are drawn because either the quad buffer is full or
261 // the atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500262 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400263 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
264 // If the vertex buffer is full and there are still glyphs to draw then
265 // get a new buffer.
266 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500267 return;
268 }
269 }
270 }
271 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400272 }
joshualitta751c972015-11-20 13:37:32 -0800273}
274
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700275void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500276 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
277 std::move(fProcessors),
278 GrPipeline::InputFlags::kNone);
279
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600280 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
281 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700282}
283
Herb Derby4513cdd2020-01-31 13:28:49 -0500284void GrAtlasTextOp::createDrawForGeneratedGlyphs(
285 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500286 if (!flushInfo->fGlyphsToFlush) {
287 return;
288 }
289
Robert Phillips5a66efb2018-03-07 15:13:18 -0500290 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500291
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500292 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400293 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500294
Greg Daniel9715b6c2019-12-10 15:03:10 -0500295 unsigned int numActiveViews;
296 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
297 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500298 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500299 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500300 return;
301 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500302 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400303 // During preparation the number of atlas pages has increased.
304 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500305 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600306 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400307 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
308 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500309 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500310 // These will get unreffed when the previously recorded draws destruct.
311 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600312 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500313 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000314 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400315 if (this->usesDistanceFields()) {
316 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500317 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400318 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400319 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500320 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400321 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400322 }
323 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400324 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500325 : GrSamplerState::Filter::kNearest;
326 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400327 }
328 }
Brian Salomondbf70722019-02-07 11:31:24 -0500329 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600330 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600331 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
332 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
333 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600334 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600335 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800336 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
337 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500338 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800339}
340
Herb Derbye25c3002020-10-27 15:57:27 -0400341GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500342 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500343
344 if (fDFGPFlags != that->fDFGPFlags ||
345 fMaskType != that->fMaskType ||
346 fUsesLocalCoords != that->fUsesLocalCoords ||
347 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
348 fHasPerspective != that->fHasPerspective ||
349 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
350 // All flags must match for an op to be combined
351 return CombineResult::kCannotCombine;
352 }
353
Brian Salomon44acb5b2017-07-18 19:59:24 -0400354 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000355 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400356 }
357
Michael Ludwigefc89d22020-11-05 11:43:10 -0500358 if (fUsesLocalCoords) {
359 // If the fragment processors use local coordinates, the GPs compute them using the inverse
360 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Michael Ludwig64596c52020-11-05 12:39:13 -0500361 const SkMatrix& thisFirstMatrix = fGeometries.front().fDrawMatrix;
362 const SkMatrix& thatFirstMatrix = that->fGeometries.front().fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500363 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
364 return CombineResult::kCannotCombine;
365 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400366 }
367
Brian Salomon5c6ac642017-12-19 11:09:32 -0500368 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500369 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400370 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000371 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800372 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500373 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500374 if (this->maskType() == MaskType::kColorBitmap &&
Michael Ludwig64596c52020-11-05 12:39:13 -0500375 fGeometries.front().fColor != that->fGeometries.front().fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500376 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000377 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500378 }
joshualitta751c972015-11-20 13:37:32 -0800379 }
380
Michael Ludwigefc89d22020-11-05 11:43:10 -0500381 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800382
Michael Ludwig64596c52020-11-05 12:39:13 -0500383 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
384 fGeometries.concat(std::move(that->fGeometries));
Brian Salomon7eae3e02018-08-07 14:02:38 +0000385 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800386}
387
joshualitta751c972015-11-20 13:37:32 -0800388// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500389GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
390 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500391 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500392 const GrSurfaceProxyView* views,
393 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500394 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400395 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
396
joshualitta751c972015-11-20 13:37:32 -0800397 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500398 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400399 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400400 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500401 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400402 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400403 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500404 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400405 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400406 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500407 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800408 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500409 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
410 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500411 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400412 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500413 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800414 } else {
joshualitta751c972015-11-20 13:37:32 -0800415#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400416 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500417 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400418 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
419 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400420 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
421 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400422 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500423 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400424 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500425 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800426#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500427 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400428 GrSamplerState::Filter::kLinear, fDFGPFlags,
429 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800430#endif
431 }
joshualitta751c972015-11-20 13:37:32 -0800432}
joshualittddd22d82016-02-16 06:47:52 -0800433
Herb Derby4598fa12020-06-10 14:54:22 -0400434#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400435
436GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
437 const SkPaint& skPaint,
438 const SkFont& font,
439 const SkMatrixProvider& mtxProvider,
440 const char* text,
441 int x,
442 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400443 size_t textLen = (int)strlen(text);
444
445 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
446
447 auto drawOrigin = SkPoint::Make(x, y);
448 SkGlyphRunBuilder builder;
449 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
450
451 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400452 if (glyphRunList.empty()) {
453 return nullptr;
454 }
Herb Derby4598fa12020-06-10 14:54:22 -0400455
Brian Salomon70fe17e2020-11-30 14:33:58 -0500456 auto rContext = rtc->recordingContext();
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400457 GrSDFTOptions SDFOptions = rContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400458
Herb Derby4598fa12020-06-10 14:54:22 -0400459 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500460 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby281583a2020-11-19 11:40:07 -0500461 painter->processGlyphRun(
462 *glyphRunList.begin(),
463 drawMatrix, glyphRunList.origin(),
464 glyphRunList.paint(),
465 rtc->surfaceProps(),
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400466 rContext->priv().caps()->shaderCaps()->supportsDistanceFieldText(),
Herb Derby4598fa12020-06-10 14:54:22 -0400467 SDFOptions, blob.get());
Herb Derby342273c2020-07-13 10:22:32 -0400468 if (!blob->subRunList().head()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400469 return nullptr;
470 }
Herb Derby4598fa12020-06-10 14:54:22 -0400471
Herb Derbyd90024d2020-11-20 10:21:32 -0500472 GrAtlasSubRun* subRun = blob->subRunList().head()->testingOnly_atlasSubRun();
473 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400474 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400475 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400476 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400477}
478
479GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
480 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
481 auto rtc = GrRenderTargetContext::Make(
482 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
483
484 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
485
486 SkPaint skPaint;
487 skPaint.setColor(random->nextU());
488
489 SkFont font;
490 if (random->nextBool()) {
491 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
492 } else {
493 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
494 }
495 font.setSubpixel(random->nextBool());
496
497 const char* text = "The quick brown fox jumps over the lazy dog.";
498
499 // create some random x/y offsets, including negative offsets
500 static const int kMaxTrans = 1024;
501 int xPos = (random->nextU() % 2) * 2 - 1;
502 int yPos = (random->nextU() % 2) * 2 - 1;
503 int xInt = (random->nextU() % kMaxTrans) * xPos;
504 int yInt = (random->nextU() % kMaxTrans) * yPos;
505
506 return GrAtlasTextOp::CreateOpTestingOnly(
507 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
508}
509
510#endif