blob: 116826a91a71548e190f59adb4a75c9a179f475a [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()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050044 , fProcessors(std::move(paint))
45 , fNumGlyphs(glyphCount)
46 , fDFGPFlags(0)
47 , fMaskType(static_cast<uint32_t>(maskType))
48 , fUsesLocalCoords(false)
49 , fNeedsGlyphTransform(needsTransform)
50 , fHasPerspective(needsTransform && geo.fDrawMatrix.hasPerspective())
51 , fUseGammaCorrectDistanceTable(false) {
Michael Ludwig64596c52020-11-05 12:39:13 -050052 fGeometries.push_back(geo);
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 Derby1d17e492020-07-21 11:45:04 -040065 const Geometry& geo,
66 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)
74 , fHasPerspective(needsTransform && geo.fDrawMatrix.hasPerspective())
75 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
76 , fLuminanceColor(luminanceColor) {
Michael Ludwig64596c52020-11-05 12:39:13 -050077 fGeometries.push_back(geo);
Herb Derby3c873af2020-04-29 15:56:07 -040078 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
79 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -040080 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -040081}
82
Herb Derby64391c42020-05-16 14:32:15 -040083void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby43ad7912020-07-20 16:14:19 -040084 fSubRun.fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
85 fDrawMatrix, fDrawOrigin, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -040086}
87
Chris Dalton1706cbf2019-05-21 19:35:29 -060088void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050089 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050090}
91
John Stiles8d9bf642020-08-12 15:07:45 -040092#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -040093SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080094 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -050095 int i = 0;
96 for (const auto& g : fGeometries.items()) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050097 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -050098 i++,
99 g.fColor.toBytes_RGBA(),
100 g.fDrawOrigin.x(),
101 g.fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800102 }
103
Brian Salomon44acb5b2017-07-18 19:59:24 -0400104 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800105 return str;
106}
Brian Osman9a390ac2018-11-12 09:47:48 -0500107#endif
joshualitta751c972015-11-20 13:37:32 -0800108
Brian Salomon44acb5b2017-07-18 19:59:24 -0400109GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
110 return FixedFunctionFlags::kNone;
111}
112
Chris Dalton6ce447a2019-06-23 18:07:38 -0600113GrProcessorSet::Analysis GrAtlasTextOp::finalize(
114 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
115 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400116 GrProcessorAnalysisCoverage coverage;
117 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500118 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400119 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800120 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500121 // finalize() is called before any merging is done, so at this point there's at most one
122 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Michael Ludwig64596c52020-11-05 12:39:13 -0500123 color.setToConstant(fGeometries.front().fColor);
joshualitta751c972015-11-20 13:37:32 -0800124 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500125
Michael Ludwigefc89d22020-11-05 11:43:10 -0500126 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500127 case MaskType::kGrayscaleCoverage:
128 case MaskType::kAliasedDistanceField:
129 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400130 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800131 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500132 case MaskType::kLCDCoverage:
133 case MaskType::kLCDDistanceField:
134 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400135 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800136 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500137 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400138 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400139 break;
joshualitta751c972015-11-20 13:37:32 -0800140 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500141
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700142 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600143 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Michael Ludwig64596c52020-11-05 12:39:13 -0500144 clampType, &fGeometries.front().fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500145 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
146 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
147 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400148 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700149 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800150}
151
Brian Salomon91326c32017-08-09 16:02:19 -0400152void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500153 auto resourceProvider = target->resourceProvider();
154
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500155 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
156 // processor analysis will not require local coords and the GPs will skip local coords when
157 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
158 // all geometries to have same draw matrix.
159 SkMatrix localMatrix = SkMatrix::I();
Michael Ludwig64596c52020-11-05 12:39:13 -0500160 if (fUsesLocalCoords && !fGeometries.front().fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800161 return;
162 }
163
Robert Phillips5a66efb2018-03-07 15:13:18 -0500164 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000165
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400166 GrMaskFormat maskFormat = this->maskFormat();
167
Greg Daniel9715b6c2019-12-10 15:03:10 -0500168 unsigned int numActiveViews;
169 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
170 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800171 SkDebugf("Could not allocate backing texture for atlas\n");
172 return;
173 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500174 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800175
Brian Salomon7eae3e02018-08-07 14:02:38 +0000176 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500177 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
178 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000179
Chris Dalton304e14d2020-03-17 14:29:06 -0600180 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500181 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600182 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400183 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
184 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500185 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000186 }
Brian Salomon49348902018-06-26 09:12:38 -0400187
188 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600189 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400190 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400191
joshualittd9d30f72015-12-08 10:47:55 -0800192 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500193 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
194 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500195 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800196 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400197 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500198 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500199 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
200 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500201 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Michael Ludwig64596c52020-11-05 12:39:13 -0500202 target->allocator(), *target->caps().shaderCaps(), fGeometries.front().fColor,
203 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800204 }
205
Herb Derbyfd894ff2020-07-15 13:23:33 -0400206 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800207
Brian Salomon43cbd722020-01-03 22:09:12 -0500208 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500209 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
210 const int quadSize = vertexStride * kVerticesPerGlyph;
211 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
212
Herb Derbyfd894ff2020-07-15 13:23:33 -0400213 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500214 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400215 int quadCursor;
216 int quadEnd;
217 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500218
Herb Derbyfd894ff2020-07-15 13:23:33 -0400219 auto resetVertexBuffer = [&] {
220 quadCursor = 0;
221 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800222
Herb Derbyfd894ff2020-07-15 13:23:33 -0400223 vertices = (char*)target->makeVertexSpace(
224 vertexStride,
225 kVerticesPerGlyph * quadEnd,
226 &flushInfo.fVertexBuffer,
227 &flushInfo.fVertexOffset);
228
229 if (!vertices || !flushInfo.fVertexBuffer) {
230 SkDebugf("Could not allocate vertices\n");
231 return false;
232 }
233 return true;
234 };
235
236 resetVertexBuffer();
237
Michael Ludwig64596c52020-11-05 12:39:13 -0500238 for (const Geometry& geo : fGeometries.items()) {
Herb Derby43ad7912020-07-20 16:14:19 -0400239 const GrAtlasSubRun& subRun = geo.fSubRun;
240 SkASSERT((int)subRun.vertexStride() == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500241
Herb Derby43ad7912020-07-20 16:14:19 -0400242 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400243 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
244 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
245 // of the glyphs to fill the vertex buffer.
246 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400247 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500248 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400249 if (!ok) {
250 return;
251 }
Herb Derby23f29762020-01-10 16:26:14 -0500252
Herb Derbyfd894ff2020-07-15 13:23:33 -0400253 geo.fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500254
Herb Derbyfd894ff2020-07-15 13:23:33 -0400255 subRunCursor += glyphsRegenerated;
256 quadCursor += glyphsRegenerated;
257 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500258 flushInfo.fGlyphsToFlush += glyphsRegenerated;
259
Herb Derbyfd894ff2020-07-15 13:23:33 -0400260 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
261 // Flush if not all the glyphs are drawn because either the quad buffer is full or
262 // the atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500263 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400264 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
265 // If the vertex buffer is full and there are still glyphs to draw then
266 // get a new buffer.
267 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500268 return;
269 }
270 }
271 }
272 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400273 }
joshualitta751c972015-11-20 13:37:32 -0800274}
275
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700276void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500277 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
278 std::move(fProcessors),
279 GrPipeline::InputFlags::kNone);
280
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600281 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
282 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700283}
284
Herb Derby4513cdd2020-01-31 13:28:49 -0500285void GrAtlasTextOp::createDrawForGeneratedGlyphs(
286 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500287 if (!flushInfo->fGlyphsToFlush) {
288 return;
289 }
290
Robert Phillips5a66efb2018-03-07 15:13:18 -0500291 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500292
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500293 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400294 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500295
Greg Daniel9715b6c2019-12-10 15:03:10 -0500296 unsigned int numActiveViews;
297 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
298 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500299 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500300 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500301 return;
302 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500303 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400304 // During preparation the number of atlas pages has increased.
305 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500306 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600307 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400308 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
309 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500310 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500311 // These will get unreffed when the previously recorded draws destruct.
312 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600313 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500314 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000315 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400316 if (this->usesDistanceFields()) {
317 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500318 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400319 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400320 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500321 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400322 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400323 }
324 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400325 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500326 : GrSamplerState::Filter::kNearest;
327 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400328 }
329 }
Brian Salomondbf70722019-02-07 11:31:24 -0500330 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600331 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600332 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
333 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
334 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600335 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600336 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800337 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
338 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500339 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800340}
341
Herb Derbye25c3002020-10-27 15:57:27 -0400342GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500343 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500344
345 if (fDFGPFlags != that->fDFGPFlags ||
346 fMaskType != that->fMaskType ||
347 fUsesLocalCoords != that->fUsesLocalCoords ||
348 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
349 fHasPerspective != that->fHasPerspective ||
350 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
351 // All flags must match for an op to be combined
352 return CombineResult::kCannotCombine;
353 }
354
Brian Salomon44acb5b2017-07-18 19:59:24 -0400355 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000356 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400357 }
358
Michael Ludwigefc89d22020-11-05 11:43:10 -0500359 if (fUsesLocalCoords) {
360 // If the fragment processors use local coordinates, the GPs compute them using the inverse
361 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Michael Ludwig64596c52020-11-05 12:39:13 -0500362 const SkMatrix& thisFirstMatrix = fGeometries.front().fDrawMatrix;
363 const SkMatrix& thatFirstMatrix = that->fGeometries.front().fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500364 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
365 return CombineResult::kCannotCombine;
366 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400367 }
368
Brian Salomon5c6ac642017-12-19 11:09:32 -0500369 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500370 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400371 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000372 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800373 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500374 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500375 if (this->maskType() == MaskType::kColorBitmap &&
Michael Ludwig64596c52020-11-05 12:39:13 -0500376 fGeometries.front().fColor != that->fGeometries.front().fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500377 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000378 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500379 }
joshualitta751c972015-11-20 13:37:32 -0800380 }
381
Michael Ludwigefc89d22020-11-05 11:43:10 -0500382 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800383
Michael Ludwig64596c52020-11-05 12:39:13 -0500384 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
385 fGeometries.concat(std::move(that->fGeometries));
Brian Salomon7eae3e02018-08-07 14:02:38 +0000386 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800387}
388
joshualitta751c972015-11-20 13:37:32 -0800389// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500390GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
391 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500392 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500393 const GrSurfaceProxyView* views,
394 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500395 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400396 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
397
joshualitta751c972015-11-20 13:37:32 -0800398 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500399 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400400 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400401 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500402 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400403 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400404 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500405 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400406 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400407 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500408 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800409 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500410 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
411 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500412 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400413 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500414 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800415 } else {
joshualitta751c972015-11-20 13:37:32 -0800416#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400417 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500418 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400419 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
420 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400421 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
422 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400423 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500424 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400425 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500426 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800427#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500428 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400429 GrSamplerState::Filter::kLinear, fDFGPFlags,
430 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800431#endif
432 }
joshualitta751c972015-11-20 13:37:32 -0800433}
joshualittddd22d82016-02-16 06:47:52 -0800434
Herb Derby4598fa12020-06-10 14:54:22 -0400435#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400436
437GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
438 const SkPaint& skPaint,
439 const SkFont& font,
440 const SkMatrixProvider& mtxProvider,
441 const char* text,
442 int x,
443 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400444 size_t textLen = (int)strlen(text);
445
446 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
447
448 auto drawOrigin = SkPoint::Make(x, y);
449 SkGlyphRunBuilder builder;
450 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
451
452 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400453 if (glyphRunList.empty()) {
454 return nullptr;
455 }
Herb Derby4598fa12020-06-10 14:54:22 -0400456
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400457
458 auto rContext = rtc->priv().recordingContext();
459 GrSDFTOptions SDFOptions = rContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400460
Herb Derby4598fa12020-06-10 14:54:22 -0400461 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby411e7aa2020-07-09 16:02:08 -0400462 SkGlyphRunListPainter* painter = rtc->priv().testingOnly_glyphRunPainter();
Herb Derby281583a2020-11-19 11:40:07 -0500463 painter->processGlyphRun(
464 *glyphRunList.begin(),
465 drawMatrix, glyphRunList.origin(),
466 glyphRunList.paint(),
467 rtc->surfaceProps(),
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400468 rContext->priv().caps()->shaderCaps()->supportsDistanceFieldText(),
Herb Derby4598fa12020-06-10 14:54:22 -0400469 SDFOptions, blob.get());
Herb Derby342273c2020-07-13 10:22:32 -0400470 if (!blob->subRunList().head()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400471 return nullptr;
472 }
Herb Derby4598fa12020-06-10 14:54:22 -0400473
Herb Derby252a3c02020-07-14 12:15:34 -0400474 GrAtlasSubRun* subRun = static_cast<GrAtlasSubRun*>(blob->subRunList().head());
Herb Derbyc76d4092020-10-07 16:46:15 -0400475 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400476 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400477 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400478}
479
480GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
481 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
482 auto rtc = GrRenderTargetContext::Make(
483 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
484
485 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
486
487 SkPaint skPaint;
488 skPaint.setColor(random->nextU());
489
490 SkFont font;
491 if (random->nextBool()) {
492 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
493 } else {
494 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
495 }
496 font.setSubpixel(random->nextBool());
497
498 const char* text = "The quick brown fox jumps over the lazy dog.";
499
500 // create some random x/y offsets, including negative offsets
501 static const int kMaxTrans = 1024;
502 int xPos = (random->nextU() % 2) * 2 - 1;
503 int yPos = (random->nextU() % 2) * 2 - 1;
504 int xInt = (random->nextU() % kMaxTrans) * xPos;
505 int yInt = (random->nextU() % kMaxTrans) * yPos;
506
507 return GrAtlasTextOp::CreateOpTestingOnly(
508 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
509}
510
511#endif