blob: 5f96fc22d80074b90af0c242620ee073934c9aa4 [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"
Herb Derby7a1d9422020-07-06 14:18:01 -040022#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrResourceProvider.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040024#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/effects/GrBitmapTextGeoProc.h"
26#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050027#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040029#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080030
Herb Derbya08bde62020-06-12 15:46:06 -040031#if GR_TEST_UTILS
32#include "src/gpu/GrDrawOpTest.h"
33#endif
34
joshualitt60ce86d2015-11-23 13:08:22 -080035///////////////////////////////////////////////////////////////////////////////////////////////////
36
Herb Derby3c873af2020-04-29 15:56:07 -040037GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
Herb Derby268e48b2020-07-16 12:56:58 -040038 bool needsTransform,
39 int glyphCount,
40 SkRect deviceRect,
Herb Derby1d17e492020-07-21 11:45:04 -040041 const Geometry& geo,
42 GrPaint&& paint)
Michael Ludwigefc89d22020-11-05 11:43:10 -050043 : INHERITED{ClassID()}
44 , fGeoCount(1)
45 , fGeoDataAllocSize(kMinGeometryAllocated)
46 , fProcessors(std::move(paint))
47 , fNumGlyphs(glyphCount)
48 , fDFGPFlags(0)
49 , fMaskType(static_cast<uint32_t>(maskType))
50 , fUsesLocalCoords(false)
51 , fNeedsGlyphTransform(needsTransform)
52 , fHasPerspective(needsTransform && geo.fDrawMatrix.hasPerspective())
53 , fUseGammaCorrectDistanceTable(false) {
Herb Derby1d17e492020-07-21 11:45:04 -040054 new (&fGeoData[0]) Geometry{geo};
Herb Derby268e48b2020-07-16 12:56:58 -040055 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
56 // we treat this as a set of non-AA rects rendered with a texture.
57 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
58}
59
60GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
61 bool needsTransform,
62 int glyphCount,
63 SkRect deviceRect,
Herb Derby3c873af2020-04-29 15:56:07 -040064 SkColor luminanceColor,
65 bool useGammaCorrectDistanceTable,
Herb Derby268e48b2020-07-16 12:56:58 -040066 uint32_t DFGPFlags,
Herb Derby1d17e492020-07-21 11:45:04 -040067 const Geometry& geo,
68 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040069 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050070 , fGeoCount(1)
71 , fGeoDataAllocSize(kMinGeometryAllocated)
72 , fProcessors(std::move(paint))
73 , fNumGlyphs(glyphCount)
74 , fDFGPFlags(DFGPFlags)
75 , fMaskType(static_cast<uint32_t>(maskType))
76 , fUsesLocalCoords(false)
77 , fNeedsGlyphTransform(needsTransform)
78 , fHasPerspective(needsTransform && geo.fDrawMatrix.hasPerspective())
79 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
80 , fLuminanceColor(luminanceColor) {
Herb Derby1d17e492020-07-21 11:45:04 -040081 new (&fGeoData[0]) Geometry{geo};
Herb Derby3c873af2020-04-29 15:56:07 -040082 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
83 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -040084 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -040085}
86
Herb Derby64391c42020-05-16 14:32:15 -040087void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby43ad7912020-07-20 16:14:19 -040088 fSubRun.fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
89 fDrawMatrix, fDrawOrigin, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -040090}
91
Chris Dalton1706cbf2019-05-21 19:35:29 -060092void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050093 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050094}
95
John Stiles8d9bf642020-08-12 15:07:45 -040096#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -040097SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080098 SkString str;
99
100 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500101 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800102 i,
Brian Osmancf860852018-10-31 14:04:39 -0400103 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500104 fGeoData[i].fDrawOrigin.x(),
105 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800106 }
107
Brian Salomon44acb5b2017-07-18 19:59:24 -0400108 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800109 return str;
110}
Brian Osman9a390ac2018-11-12 09:47:48 -0500111#endif
joshualitta751c972015-11-20 13:37:32 -0800112
Brian Salomon44acb5b2017-07-18 19:59:24 -0400113GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
114 return FixedFunctionFlags::kNone;
115}
116
Chris Dalton6ce447a2019-06-23 18:07:38 -0600117GrProcessorSet::Analysis GrAtlasTextOp::finalize(
118 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
119 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400120 GrProcessorAnalysisCoverage coverage;
121 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500122 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400123 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800124 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500125 // finalize() is called before any merging is done, so at this point there's at most one
126 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Michael Ludwigefc89d22020-11-05 11:43:10 -0500127 color.setToConstant(fGeoData[0].fColor);
joshualitta751c972015-11-20 13:37:32 -0800128 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500129
Michael Ludwigefc89d22020-11-05 11:43:10 -0500130 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500131 case MaskType::kGrayscaleCoverage:
132 case MaskType::kAliasedDistanceField:
133 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400134 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800135 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500136 case MaskType::kLCDCoverage:
137 case MaskType::kLCDDistanceField:
138 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400139 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800140 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500141 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400142 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400143 break;
joshualitta751c972015-11-20 13:37:32 -0800144 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500145
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700146 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600147 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
148 clampType, &fGeoData[0].fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500149 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
150 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
151 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400152 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700153 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800154}
155
Brian Salomon91326c32017-08-09 16:02:19 -0400156void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500157 auto resourceProvider = target->resourceProvider();
158
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500159 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
160 // processor analysis will not require local coords and the GPs will skip local coords when
161 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
162 // all geometries to have same draw matrix.
163 SkMatrix localMatrix = SkMatrix::I();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500164 if (fUsesLocalCoords && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800165 return;
166 }
167
Robert Phillips5a66efb2018-03-07 15:13:18 -0500168 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000169
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400170 GrMaskFormat maskFormat = this->maskFormat();
171
Greg Daniel9715b6c2019-12-10 15:03:10 -0500172 unsigned int numActiveViews;
173 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
174 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800175 SkDebugf("Could not allocate backing texture for atlas\n");
176 return;
177 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500178 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800179
Brian Salomon7eae3e02018-08-07 14:02:38 +0000180 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500181 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
182 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000183
Chris Dalton304e14d2020-03-17 14:29:06 -0600184 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500185 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600186 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400187 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
188 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500189 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000190 }
Brian Salomon49348902018-06-26 09:12:38 -0400191
192 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600193 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400194 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400195
joshualittd9d30f72015-12-08 10:47:55 -0800196 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500197 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
198 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500199 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800200 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400201 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500202 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500203 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
204 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500205 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Michael Ludwigefc89d22020-11-05 11:43:10 -0500206 target->allocator(), *target->caps().shaderCaps(), fGeoData[0].fColor, false, views,
207 numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800208 }
209
Herb Derbyfd894ff2020-07-15 13:23:33 -0400210 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800211
Brian Salomon43cbd722020-01-03 22:09:12 -0500212 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500213 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
214 const int quadSize = vertexStride * kVerticesPerGlyph;
215 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
216
Herb Derbyfd894ff2020-07-15 13:23:33 -0400217 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500218 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400219 int quadCursor;
220 int quadEnd;
221 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500222
Herb Derbyfd894ff2020-07-15 13:23:33 -0400223 auto resetVertexBuffer = [&] {
224 quadCursor = 0;
225 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800226
Herb Derbyfd894ff2020-07-15 13:23:33 -0400227 vertices = (char*)target->makeVertexSpace(
228 vertexStride,
229 kVerticesPerGlyph * quadEnd,
230 &flushInfo.fVertexBuffer,
231 &flushInfo.fVertexOffset);
232
233 if (!vertices || !flushInfo.fVertexBuffer) {
234 SkDebugf("Could not allocate vertices\n");
235 return false;
236 }
237 return true;
238 };
239
240 resetVertexBuffer();
241
Herb Derbya80ce1a2020-05-26 12:58:59 -0400242 for (const Geometry& geo : SkSpan(fGeoData.get(), fGeoCount)) {
Herb Derby43ad7912020-07-20 16:14:19 -0400243 const GrAtlasSubRun& subRun = geo.fSubRun;
244 SkASSERT((int)subRun.vertexStride() == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500245
Herb Derby43ad7912020-07-20 16:14:19 -0400246 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400247 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
248 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
249 // of the glyphs to fill the vertex buffer.
250 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400251 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500252 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400253 if (!ok) {
254 return;
255 }
Herb Derby23f29762020-01-10 16:26:14 -0500256
Herb Derbyfd894ff2020-07-15 13:23:33 -0400257 geo.fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500258
Herb Derbyfd894ff2020-07-15 13:23:33 -0400259 subRunCursor += glyphsRegenerated;
260 quadCursor += glyphsRegenerated;
261 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500262 flushInfo.fGlyphsToFlush += glyphsRegenerated;
263
Herb Derbyfd894ff2020-07-15 13:23:33 -0400264 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
265 // Flush if not all the glyphs are drawn because either the quad buffer is full or
266 // the atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500267 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400268 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
269 // If the vertex buffer is full and there are still glyphs to draw then
270 // get a new buffer.
271 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500272 return;
273 }
274 }
275 }
276 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400277 }
joshualitta751c972015-11-20 13:37:32 -0800278}
279
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700280void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500281 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
282 std::move(fProcessors),
283 GrPipeline::InputFlags::kNone);
284
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600285 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
286 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700287}
288
Herb Derby4513cdd2020-01-31 13:28:49 -0500289void GrAtlasTextOp::createDrawForGeneratedGlyphs(
290 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500291 if (!flushInfo->fGlyphsToFlush) {
292 return;
293 }
294
Robert Phillips5a66efb2018-03-07 15:13:18 -0500295 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500296
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500297 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400298 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500299
Greg Daniel9715b6c2019-12-10 15:03:10 -0500300 unsigned int numActiveViews;
301 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
302 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500303 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500304 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500305 return;
306 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500307 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400308 // During preparation the number of atlas pages has increased.
309 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500310 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600311 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400312 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
313 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500314 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500315 // These will get unreffed when the previously recorded draws destruct.
316 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600317 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500318 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000319 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400320 if (this->usesDistanceFields()) {
321 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500322 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400323 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400324 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500325 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400326 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400327 }
328 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400329 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500330 : GrSamplerState::Filter::kNearest;
331 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400332 }
333 }
Brian Salomondbf70722019-02-07 11:31:24 -0500334 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600335 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600336 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
337 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
338 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600339 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600340 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800341 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
342 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500343 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800344}
345
Herb Derbye25c3002020-10-27 15:57:27 -0400346GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500347 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500348
349 if (fDFGPFlags != that->fDFGPFlags ||
350 fMaskType != that->fMaskType ||
351 fUsesLocalCoords != that->fUsesLocalCoords ||
352 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
353 fHasPerspective != that->fHasPerspective ||
354 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
355 // All flags must match for an op to be combined
356 return CombineResult::kCannotCombine;
357 }
358
Brian Salomon44acb5b2017-07-18 19:59:24 -0400359 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000360 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400361 }
362
Michael Ludwigefc89d22020-11-05 11:43:10 -0500363 if (fUsesLocalCoords) {
364 // If the fragment processors use local coordinates, the GPs compute them using the inverse
365 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
366 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
367 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
368 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
369 return CombineResult::kCannotCombine;
370 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400371 }
372
Brian Salomon5c6ac642017-12-19 11:09:32 -0500373 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500374 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400375 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000376 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800377 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500378 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500379 if (this->maskType() == MaskType::kColorBitmap &&
380 fGeoData[0].fColor != that->fGeoData[0].fColor) {
381 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000382 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500383 }
joshualitta751c972015-11-20 13:37:32 -0800384 }
385
Michael Ludwigefc89d22020-11-05 11:43:10 -0500386 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800387
Jim Van Verth56c37142017-10-31 14:44:25 -0400388 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800389 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800390
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400391 // We reallocate at a rate of 1.5x to try to get better total memory usage
392 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400393 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400394 while (newAllocSize < newGeoCount) {
395 newAllocSize += newAllocSize / 2;
396 }
joshualitta751c972015-11-20 13:37:32 -0800397 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400398 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800399 }
400
Brian Salomon344ec422016-12-15 10:58:41 -0500401 // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
joshualitta751c972015-11-20 13:37:32 -0800402 // it doesn't try to unref them.
Herb Derby1d17e492020-07-21 11:45:04 -0400403 for (int i = 0; i < that->fGeoCount; i++) {
404 new (&fGeoData[fGeoCount + i]) Geometry{that->fGeoData[i]};
joshualitta751c972015-11-20 13:37:32 -0800405 }
Herb Derby1d17e492020-07-21 11:45:04 -0400406
joshualitta751c972015-11-20 13:37:32 -0800407 that->fGeoCount = 0;
408 fGeoCount = newGeoCount;
409
Brian Salomon7eae3e02018-08-07 14:02:38 +0000410 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800411}
412
Herb Derby3c873af2020-04-29 15:56:07 -0400413
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
462GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
463 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
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400482
483 auto rContext = rtc->priv().recordingContext();
484 GrSDFTOptions SDFOptions = rContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400485
Herb Derby4598fa12020-06-10 14:54:22 -0400486 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby411e7aa2020-07-09 16:02:08 -0400487 SkGlyphRunListPainter* painter = rtc->priv().testingOnly_glyphRunPainter();
Herb Derby4598fa12020-06-10 14:54:22 -0400488 painter->processGlyphRunList(
Herb Derbyd5764642020-07-08 09:57:11 -0400489 glyphRunList, drawMatrix, rtc->surfaceProps(),
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400490 rContext->priv().caps()->shaderCaps()->supportsDistanceFieldText(),
Herb Derby4598fa12020-06-10 14:54:22 -0400491 SDFOptions, blob.get());
Herb Derby342273c2020-07-13 10:22:32 -0400492 if (!blob->subRunList().head()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400493 return nullptr;
494 }
Herb Derby4598fa12020-06-10 14:54:22 -0400495
Herb Derby252a3c02020-07-14 12:15:34 -0400496 GrAtlasSubRun* subRun = static_cast<GrAtlasSubRun*>(blob->subRunList().head());
Herb Derbyc76d4092020-10-07 16:46:15 -0400497 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400498 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400499 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400500}
501
502GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
503 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
504 auto rtc = GrRenderTargetContext::Make(
505 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
506
507 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
508
509 SkPaint skPaint;
510 skPaint.setColor(random->nextU());
511
512 SkFont font;
513 if (random->nextBool()) {
514 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
515 } else {
516 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
517 }
518 font.setSubpixel(random->nextBool());
519
520 const char* text = "The quick brown fox jumps over the lazy dog.";
521
522 // create some random x/y offsets, including negative offsets
523 static const int kMaxTrans = 1024;
524 int xPos = (random->nextU() % 2) * 2 - 1;
525 int yPos = (random->nextU() % 2) * 2 - 1;
526 int xInt = (random->nextU() % kMaxTrans) * xPos;
527 int yInt = (random->nextU() % kMaxTrans) * yPos;
528
529 return GrAtlasTextOp::CreateOpTestingOnly(
530 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
531}
532
533#endif