blob: 467ff3c559beacdbdf8ef1e1aba433b74225fd10 [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 Derby40894182020-12-02 11:39:48 -050083 SkMatrix positionMatrix = fDrawMatrix;
84 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
85 fSubRun.fillVertexData(
86 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -040087}
88
Chris Dalton1706cbf2019-05-21 19:35:29 -060089void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050090 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050091}
92
John Stiles8d9bf642020-08-12 15:07:45 -040093#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -040094SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080095 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -050096 int i = 0;
97 for (const auto& g : fGeometries.items()) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050098 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -050099 i++,
100 g.fColor.toBytes_RGBA(),
101 g.fDrawOrigin.x(),
102 g.fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800103 }
104
Brian Salomon44acb5b2017-07-18 19:59:24 -0400105 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800106 return str;
107}
Brian Osman9a390ac2018-11-12 09:47:48 -0500108#endif
joshualitta751c972015-11-20 13:37:32 -0800109
Brian Salomon44acb5b2017-07-18 19:59:24 -0400110GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
111 return FixedFunctionFlags::kNone;
112}
113
Chris Dalton6ce447a2019-06-23 18:07:38 -0600114GrProcessorSet::Analysis GrAtlasTextOp::finalize(
115 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
116 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400117 GrProcessorAnalysisCoverage coverage;
118 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500119 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400120 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800121 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500122 // finalize() is called before any merging is done, so at this point there's at most one
123 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Michael Ludwig64596c52020-11-05 12:39:13 -0500124 color.setToConstant(fGeometries.front().fColor);
joshualitta751c972015-11-20 13:37:32 -0800125 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500126
Michael Ludwigefc89d22020-11-05 11:43:10 -0500127 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500128 case MaskType::kGrayscaleCoverage:
129 case MaskType::kAliasedDistanceField:
130 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400131 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800132 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500133 case MaskType::kLCDCoverage:
134 case MaskType::kLCDDistanceField:
135 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400136 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800137 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500138 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400139 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400140 break;
joshualitta751c972015-11-20 13:37:32 -0800141 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500142
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700143 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600144 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Michael Ludwig64596c52020-11-05 12:39:13 -0500145 clampType, &fGeometries.front().fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500146 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
147 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
148 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400149 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700150 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800151}
152
Brian Salomon91326c32017-08-09 16:02:19 -0400153void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500154 auto resourceProvider = target->resourceProvider();
155
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500156 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
157 // processor analysis will not require local coords and the GPs will skip local coords when
158 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
159 // all geometries to have same draw matrix.
160 SkMatrix localMatrix = SkMatrix::I();
Michael Ludwig64596c52020-11-05 12:39:13 -0500161 if (fUsesLocalCoords && !fGeometries.front().fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800162 return;
163 }
164
Robert Phillips5a66efb2018-03-07 15:13:18 -0500165 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000166
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400167 GrMaskFormat maskFormat = this->maskFormat();
168
Greg Daniel9715b6c2019-12-10 15:03:10 -0500169 unsigned int numActiveViews;
170 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
171 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800172 SkDebugf("Could not allocate backing texture for atlas\n");
173 return;
174 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500175 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800176
Brian Salomon7eae3e02018-08-07 14:02:38 +0000177 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500178 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
179 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000180
Chris Dalton304e14d2020-03-17 14:29:06 -0600181 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500182 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600183 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400184 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
185 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500186 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000187 }
Brian Salomon49348902018-06-26 09:12:38 -0400188
189 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600190 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400191 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400192
joshualittd9d30f72015-12-08 10:47:55 -0800193 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500194 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
195 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500196 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800197 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400198 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500199 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500200 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
201 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500202 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Michael Ludwig64596c52020-11-05 12:39:13 -0500203 target->allocator(), *target->caps().shaderCaps(), fGeometries.front().fColor,
204 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800205 }
206
Herb Derbyfd894ff2020-07-15 13:23:33 -0400207 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800208
Brian Salomon43cbd722020-01-03 22:09:12 -0500209 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500210 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
211 const int quadSize = vertexStride * kVerticesPerGlyph;
212 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
213
Herb Derbyfd894ff2020-07-15 13:23:33 -0400214 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500215 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400216 int quadCursor;
217 int quadEnd;
218 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500219
Herb Derbyfd894ff2020-07-15 13:23:33 -0400220 auto resetVertexBuffer = [&] {
221 quadCursor = 0;
222 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800223
Herb Derbyfd894ff2020-07-15 13:23:33 -0400224 vertices = (char*)target->makeVertexSpace(
225 vertexStride,
226 kVerticesPerGlyph * quadEnd,
227 &flushInfo.fVertexBuffer,
228 &flushInfo.fVertexOffset);
229
230 if (!vertices || !flushInfo.fVertexBuffer) {
231 SkDebugf("Could not allocate vertices\n");
232 return false;
233 }
234 return true;
235 };
236
237 resetVertexBuffer();
238
Michael Ludwig64596c52020-11-05 12:39:13 -0500239 for (const Geometry& geo : fGeometries.items()) {
Herb Derby43ad7912020-07-20 16:14:19 -0400240 const GrAtlasSubRun& subRun = geo.fSubRun;
Herb Derby46d1e9f2020-12-01 14:12:04 -0500241 SkASSERT((int) subRun.vertexStride(geo.fDrawMatrix) == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500242
Herb Derby43ad7912020-07-20 16:14:19 -0400243 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400244 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
245 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
246 // of the glyphs to fill the vertex buffer.
247 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400248 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500249 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400250 if (!ok) {
251 return;
252 }
Herb Derby23f29762020-01-10 16:26:14 -0500253
Herb Derbyfd894ff2020-07-15 13:23:33 -0400254 geo.fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500255
Herb Derbyfd894ff2020-07-15 13:23:33 -0400256 subRunCursor += glyphsRegenerated;
257 quadCursor += glyphsRegenerated;
258 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500259 flushInfo.fGlyphsToFlush += glyphsRegenerated;
260
Herb Derbyfd894ff2020-07-15 13:23:33 -0400261 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
262 // Flush if not all the glyphs are drawn because either the quad buffer is full or
263 // the atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500264 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400265 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
266 // If the vertex buffer is full and there are still glyphs to draw then
267 // get a new buffer.
268 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500269 return;
270 }
271 }
272 }
273 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400274 }
joshualitta751c972015-11-20 13:37:32 -0800275}
276
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700277void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500278 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
279 std::move(fProcessors),
280 GrPipeline::InputFlags::kNone);
281
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600282 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
283 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700284}
285
Herb Derby4513cdd2020-01-31 13:28:49 -0500286void GrAtlasTextOp::createDrawForGeneratedGlyphs(
287 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500288 if (!flushInfo->fGlyphsToFlush) {
289 return;
290 }
291
Robert Phillips5a66efb2018-03-07 15:13:18 -0500292 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500293
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500294 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400295 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500296
Greg Daniel9715b6c2019-12-10 15:03:10 -0500297 unsigned int numActiveViews;
298 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
299 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500300 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500301 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500302 return;
303 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500304 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400305 // During preparation the number of atlas pages has increased.
306 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500307 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600308 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400309 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
310 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500311 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500312 // These will get unreffed when the previously recorded draws destruct.
313 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600314 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500315 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000316 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400317 if (this->usesDistanceFields()) {
318 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500319 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400320 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400321 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500322 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400323 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400324 }
325 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400326 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500327 : GrSamplerState::Filter::kNearest;
328 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400329 }
330 }
Brian Salomondbf70722019-02-07 11:31:24 -0500331 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600332 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600333 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
334 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
335 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600336 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600337 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800338 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
339 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500340 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800341}
342
Herb Derbye25c3002020-10-27 15:57:27 -0400343GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500344 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500345
346 if (fDFGPFlags != that->fDFGPFlags ||
347 fMaskType != that->fMaskType ||
348 fUsesLocalCoords != that->fUsesLocalCoords ||
349 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
350 fHasPerspective != that->fHasPerspective ||
351 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
352 // All flags must match for an op to be combined
353 return CombineResult::kCannotCombine;
354 }
355
Brian Salomon44acb5b2017-07-18 19:59:24 -0400356 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000357 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400358 }
359
Michael Ludwigefc89d22020-11-05 11:43:10 -0500360 if (fUsesLocalCoords) {
361 // If the fragment processors use local coordinates, the GPs compute them using the inverse
362 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Michael Ludwig64596c52020-11-05 12:39:13 -0500363 const SkMatrix& thisFirstMatrix = fGeometries.front().fDrawMatrix;
364 const SkMatrix& thatFirstMatrix = that->fGeometries.front().fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500365 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
366 return CombineResult::kCannotCombine;
367 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400368 }
369
Brian Salomon5c6ac642017-12-19 11:09:32 -0500370 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500371 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400372 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000373 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800374 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500375 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500376 if (this->maskType() == MaskType::kColorBitmap &&
Michael Ludwig64596c52020-11-05 12:39:13 -0500377 fGeometries.front().fColor != that->fGeometries.front().fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500378 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000379 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500380 }
joshualitta751c972015-11-20 13:37:32 -0800381 }
382
Michael Ludwigefc89d22020-11-05 11:43:10 -0500383 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800384
Michael Ludwig64596c52020-11-05 12:39:13 -0500385 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
386 fGeometries.concat(std::move(that->fGeometries));
Brian Salomon7eae3e02018-08-07 14:02:38 +0000387 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800388}
389
joshualitta751c972015-11-20 13:37:32 -0800390// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500391GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
392 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500393 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500394 const GrSurfaceProxyView* views,
395 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500396 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400397 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
398
joshualitta751c972015-11-20 13:37:32 -0800399 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500400 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400401 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400402 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500403 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400404 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400405 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500406 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400407 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400408 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500409 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800410 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500411 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
412 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500413 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400414 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500415 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800416 } else {
joshualitta751c972015-11-20 13:37:32 -0800417#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400418 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500419 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400420 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
421 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400422 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
423 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400424 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500425 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400426 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500427 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800428#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500429 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400430 GrSamplerState::Filter::kLinear, fDFGPFlags,
431 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800432#endif
433 }
joshualitta751c972015-11-20 13:37:32 -0800434}
joshualittddd22d82016-02-16 06:47:52 -0800435
Herb Derby4598fa12020-06-10 14:54:22 -0400436#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400437
438GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
439 const SkPaint& skPaint,
440 const SkFont& font,
441 const SkMatrixProvider& mtxProvider,
442 const char* text,
443 int x,
444 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400445 size_t textLen = (int)strlen(text);
446
447 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
448
449 auto drawOrigin = SkPoint::Make(x, y);
450 SkGlyphRunBuilder builder;
451 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
452
453 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400454 if (glyphRunList.empty()) {
455 return nullptr;
456 }
Herb Derby4598fa12020-06-10 14:54:22 -0400457
Brian Salomon70fe17e2020-11-30 14:33:58 -0500458 auto rContext = rtc->recordingContext();
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400459 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);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500462 SkGlyphRunListPainter* painter = rtc->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 Derbyd90024d2020-11-20 10:21:32 -0500474 GrAtlasSubRun* subRun = blob->subRunList().head()->testingOnly_atlasSubRun();
475 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400476 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400477 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400478 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400479}
480
481GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
482 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
483 auto rtc = GrRenderTargetContext::Make(
484 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
485
486 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
487
488 SkPaint skPaint;
489 skPaint.setColor(random->nextU());
490
491 SkFont font;
492 if (random->nextBool()) {
493 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
494 } else {
495 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
496 }
497 font.setSubpixel(random->nextBool());
498
499 const char* text = "The quick brown fox jumps over the lazy dog.";
500
501 // create some random x/y offsets, including negative offsets
502 static const int kMaxTrans = 1024;
503 int xPos = (random->nextU() % 2) * 2 - 1;
504 int yPos = (random->nextU() % 2) * 2 - 1;
505 int xInt = (random->nextU() % kMaxTrans) * xPos;
506 int yInt = (random->nextU() % kMaxTrans) * yPos;
507
508 return GrAtlasTextOp::CreateOpTestingOnly(
509 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
510}
511
512#endif