blob: 55ce686fd57e2b8ac3abc411f182e4bb0c0aafe1 [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,
38 GrPaint&& paint,
Herb Derby252a3c02020-07-14 12:15:34 -040039 GrAtlasSubRun* subrun,
Herb Derby3c873af2020-04-29 15:56:07 -040040 const SkMatrix& drawMatrix,
41 SkPoint drawOrigin,
42 const SkIRect& clipRect,
43 const SkPMColor4f& filteredColor,
44 SkColor luminanceColor,
45 bool useGammaCorrectDistanceTable,
46 uint32_t DFGPFlags)
47 : INHERITED(ClassID())
48 , fMaskType{maskType}
49 , fNeedsGlyphTransform{subrun->needsTransform()}
50 , fLuminanceColor{luminanceColor}
51 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
52 , fDFGPFlags{DFGPFlags}
53 , fGeoDataAllocSize{kMinGeometryAllocated}
54 , fProcessors{std::move(paint)}
Herb Derbyd5cbc1e2020-05-16 13:45:55 -040055 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040056 GrAtlasTextOp::Geometry& geometry = fGeoData[0];
57
58 // Unref handled in ~GrAtlasTextOp().
59 geometry.fBlob = SkRef(subrun->fBlob);
60 geometry.fSubRunPtr = subrun;
61 geometry.fDrawMatrix = drawMatrix;
62 geometry.fDrawOrigin = drawOrigin;
63 geometry.fClipRect = clipRect;
64 geometry.fColor = subrun->maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE
65 : filteredColor;
66 fGeoCount = 1;
67
68 SkRect bounds = subrun->deviceRect(drawMatrix, drawOrigin);
69 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
70 // we treat this as a set of non-AA rects rendered with a texture.
71 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
72}
73
Herb Derby64391c42020-05-16 14:32:15 -040074void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
75 fSubRunPtr->fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
76 fDrawMatrix, fDrawOrigin, fClipRect);
77}
78
Chris Dalton1706cbf2019-05-21 19:35:29 -060079void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050080 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050081}
82
Brian Osman9a390ac2018-11-12 09:47:48 -050083#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -050084SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080085 SkString str;
86
87 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050088 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -080089 i,
Brian Osmancf860852018-10-31 14:04:39 -040090 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -050091 fGeoData[i].fDrawOrigin.x(),
92 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -080093 }
94
Brian Salomon44acb5b2017-07-18 19:59:24 -040095 str += fProcessors.dumpProcessors();
96 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -080097 return str;
98}
Brian Osman9a390ac2018-11-12 09:47:48 -050099#endif
joshualitta751c972015-11-20 13:37:32 -0800100
Brian Salomon44acb5b2017-07-18 19:59:24 -0400101GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
102 return FixedFunctionFlags::kNone;
103}
104
Chris Dalton6ce447a2019-06-23 18:07:38 -0600105GrProcessorSet::Analysis GrAtlasTextOp::finalize(
106 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
107 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400108 GrProcessorAnalysisCoverage coverage;
109 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800110 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400111 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800112 } else {
Brian Osman09068252018-01-03 09:57:29 -0500113 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800114 }
joshualitta751c972015-11-20 13:37:32 -0800115 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800116 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400117 case kAliasedDistanceField_MaskType:
118 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400119 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800120 break;
121 case kLCDCoverageMask_MaskType:
122 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400123 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400124 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800125 break;
126 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400127 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400128 break;
joshualitta751c972015-11-20 13:37:32 -0800129 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700130 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600131 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
132 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400133 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700134 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800135}
136
Brian Salomon91326c32017-08-09 16:02:19 -0400137void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500138 auto resourceProvider = target->resourceProvider();
139
joshualitta751c972015-11-20 13:37:32 -0800140 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
141 // TODO actually only invert if we don't have RGBA
142 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500143 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800144 return;
145 }
146
Robert Phillips5a66efb2018-03-07 15:13:18 -0500147 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000148
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400149 GrMaskFormat maskFormat = this->maskFormat();
150
Greg Daniel9715b6c2019-12-10 15:03:10 -0500151 unsigned int numActiveViews;
152 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
153 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800154 SkDebugf("Could not allocate backing texture for atlas\n");
155 return;
156 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500157 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800158
Brian Salomon7eae3e02018-08-07 14:02:38 +0000159 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500160 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
161 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000162
Chris Dalton304e14d2020-03-17 14:29:06 -0600163 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500164 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600165 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400166 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
167 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500168 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000169 }
Brian Salomon49348902018-06-26 09:12:38 -0400170
171 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600172 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400173 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400174
Herb Derby1c5be7b2019-12-13 12:03:06 -0500175 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800176 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500177 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
178 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500179 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800180 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400181 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500182 : GrSamplerState::Filter::kNearest;
183 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
184 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
185 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800186 }
187
Herb Derbyfd894ff2020-07-15 13:23:33 -0400188 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800189
Brian Salomon43cbd722020-01-03 22:09:12 -0500190 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500191 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
192 const int quadSize = vertexStride * kVerticesPerGlyph;
193 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
194
Herb Derbyfd894ff2020-07-15 13:23:33 -0400195 int allGlyphsCursor = 0;
196 const int allGlyphsEnd = this->numGlyphs();
197 int quadCursor;
198 int quadEnd;
199 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500200
Herb Derbyfd894ff2020-07-15 13:23:33 -0400201 auto resetVertexBuffer = [&] {
202 quadCursor = 0;
203 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800204
Herb Derbyfd894ff2020-07-15 13:23:33 -0400205 vertices = (char*)target->makeVertexSpace(
206 vertexStride,
207 kVerticesPerGlyph * quadEnd,
208 &flushInfo.fVertexBuffer,
209 &flushInfo.fVertexOffset);
210
211 if (!vertices || !flushInfo.fVertexBuffer) {
212 SkDebugf("Could not allocate vertices\n");
213 return false;
214 }
215 return true;
216 };
217
218 resetVertexBuffer();
219
220 for (const Geometry& geo : SkMakeSpan(fGeoData.get(), fGeoCount)) {
221 GrAtlasSubRun* subRun = geo.fSubRunPtr;
Herb Derby23f29762020-01-10 16:26:14 -0500222 SkASSERT((int)subRun->vertexStride() == vertexStride);
Robert Phillips207d24b2020-04-09 10:23:42 -0400223 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500224
Herb Derbyfd894ff2020-07-15 13:23:33 -0400225 const int subRunEnd = subRun->glyphCount();
226 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
227 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
228 // of the glyphs to fill the vertex buffer.
229 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
230 auto[ok, glyphsRegenerated] = subRun->regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500231 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400232 if (!ok) {
233 return;
234 }
Herb Derby23f29762020-01-10 16:26:14 -0500235
Herb Derbyfd894ff2020-07-15 13:23:33 -0400236 geo.fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500237
Herb Derbyfd894ff2020-07-15 13:23:33 -0400238 subRunCursor += glyphsRegenerated;
239 quadCursor += glyphsRegenerated;
240 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500241 flushInfo.fGlyphsToFlush += glyphsRegenerated;
242
Herb Derbyfd894ff2020-07-15 13:23:33 -0400243 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
244 // Flush if not all the glyphs are drawn because either the quad buffer is full or
245 // the atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500246 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400247 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
248 // If the vertex buffer is full and there are still glyphs to draw then
249 // get a new buffer.
250 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500251 return;
252 }
253 }
254 }
255 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400256 }
joshualitta751c972015-11-20 13:37:32 -0800257}
258
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700259void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500260 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
261 std::move(fProcessors),
262 GrPipeline::InputFlags::kNone);
263
264 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700265}
266
Herb Derby4513cdd2020-01-31 13:28:49 -0500267void GrAtlasTextOp::createDrawForGeneratedGlyphs(
268 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500269 if (!flushInfo->fGlyphsToFlush) {
270 return;
271 }
272
Robert Phillips5a66efb2018-03-07 15:13:18 -0500273 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500274
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500275 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400276 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500277
Greg Daniel9715b6c2019-12-10 15:03:10 -0500278 unsigned int numActiveViews;
279 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
280 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500281 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500282 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500283 return;
284 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500285 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400286 // During preparation the number of atlas pages has increased.
287 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500288 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600289 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400290 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
291 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500292 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500293 // These will get unreffed when the previously recorded draws destruct.
294 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600295 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500296 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000297 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400298 if (this->usesDistanceFields()) {
299 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500300 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400301 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400302 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500303 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400304 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400305 }
306 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400307 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500308 : GrSamplerState::Filter::kNearest;
309 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400310 }
311 }
Brian Salomondbf70722019-02-07 11:31:24 -0500312 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600313 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600314 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
315 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
316 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600317 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600318 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800319 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
320 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500321 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800322}
323
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500324GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
325 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500326 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400327 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000328 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400329 }
330
joshualitta751c972015-11-20 13:37:32 -0800331 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000332 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800333 }
334
Herb Derby1c5be7b2019-12-13 12:03:06 -0500335 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
336 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500337
Mike Reed2c383152019-12-18 16:47:47 -0500338 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000339 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500340 }
341
Jim Van Verthb515ae72018-05-23 16:44:55 -0400342 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000343 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400344 }
345
346 if (fNeedsGlyphTransform &&
347 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000348 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400349 }
350
Brian Salomon5c6ac642017-12-19 11:09:32 -0500351 if (this->usesDistanceFields()) {
352 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000353 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800354 }
355
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400356 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000357 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800358 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500359 } else {
360 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000361 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500362 }
joshualitta751c972015-11-20 13:37:32 -0800363 }
364
Brian Salomon344ec422016-12-15 10:58:41 -0500365 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800366
Jim Van Verth56c37142017-10-31 14:44:25 -0400367 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800368 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800369
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400370 // We reallocate at a rate of 1.5x to try to get better total memory usage
371 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400372 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400373 while (newAllocSize < newGeoCount) {
374 newAllocSize += newAllocSize / 2;
375 }
joshualitta751c972015-11-20 13:37:32 -0800376 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400377 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800378 }
379
Brian Salomon344ec422016-12-15 10:58:41 -0500380 // 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 -0800381 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500382 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800383#ifdef SK_DEBUG
384 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500385 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800386 }
387#endif
388 that->fGeoCount = 0;
389 fGeoCount = newGeoCount;
390
Brian Salomon7eae3e02018-08-07 14:02:38 +0000391 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800392}
393
Herb Derby3c873af2020-04-29 15:56:07 -0400394static const int kDistanceAdjustLumShift = 5;
395
joshualitta751c972015-11-20 13:37:32 -0800396// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500397GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
398 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500399 const GrSurfaceProxyView* views,
400 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800401 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500402
403 SkMatrix localMatrix = SkMatrix::I();
404 if (this->usesLocalCoords()) {
405 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500406 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500407 (void)result;
408 }
joshualitta751c972015-11-20 13:37:32 -0800409
Robert Phillips841c9a52020-03-27 12:41:31 -0400410 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
411
joshualitta751c972015-11-20 13:37:32 -0800412 // see if we need to create a new effect
413 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400414 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400415 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500416 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400417 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400418 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500419 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400420 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400421 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500422 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800423 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500424 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
425 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500426 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400427 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500428 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800429 } else {
joshualitta751c972015-11-20 13:37:32 -0800430#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400431 float correction = 0;
432 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400433 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
434 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400435 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
436 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400437 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500438 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400439 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500440 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800441#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500442 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400443 GrSamplerState::Filter::kLinear, fDFGPFlags,
444 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800445#endif
446 }
joshualitta751c972015-11-20 13:37:32 -0800447}
joshualittddd22d82016-02-16 06:47:52 -0800448
Herb Derby4598fa12020-06-10 14:54:22 -0400449#if GR_TEST_UTILS
450std::unique_ptr<GrDrawOp> GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
451 const SkPaint& skPaint,
452 const SkFont& font,
453 const SkMatrixProvider& mtxProvider,
454 const char* text,
455 int x,
456 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400457 size_t textLen = (int)strlen(text);
458
459 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
460
461 auto drawOrigin = SkPoint::Make(x, y);
462 SkGlyphRunBuilder builder;
463 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
464
465 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400466 if (glyphRunList.empty()) {
467 return nullptr;
468 }
Herb Derby4598fa12020-06-10 14:54:22 -0400469
Herb Derby411e7aa2020-07-09 16:02:08 -0400470 const GrRecordingContextPriv& contextPriv = rtc->priv().getContext()->priv();
471 GrSDFTOptions SDFOptions = contextPriv.SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400472
Herb Derby4598fa12020-06-10 14:54:22 -0400473 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby411e7aa2020-07-09 16:02:08 -0400474 SkGlyphRunListPainter* painter = rtc->priv().testingOnly_glyphRunPainter();
Herb Derby4598fa12020-06-10 14:54:22 -0400475 painter->processGlyphRunList(
Herb Derbyd5764642020-07-08 09:57:11 -0400476 glyphRunList, drawMatrix, rtc->surfaceProps(),
Herb Derby4598fa12020-06-10 14:54:22 -0400477 contextPriv.caps()->shaderCaps()->supportsDistanceFieldText(),
478 SDFOptions, blob.get());
Herb Derby342273c2020-07-13 10:22:32 -0400479 if (!blob->subRunList().head()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400480 return nullptr;
481 }
Herb Derby4598fa12020-06-10 14:54:22 -0400482
Herb Derby252a3c02020-07-14 12:15:34 -0400483 GrAtlasSubRun* subRun = static_cast<GrAtlasSubRun*>(blob->subRunList().head());
Herb Derby411e7aa2020-07-09 16:02:08 -0400484 std::unique_ptr<GrDrawOp> op;
Herb Derby252a3c02020-07-14 12:15:34 -0400485 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400486 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400487}
488
489GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
490 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
491 auto rtc = GrRenderTargetContext::Make(
492 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
493
494 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
495
496 SkPaint skPaint;
497 skPaint.setColor(random->nextU());
498
499 SkFont font;
500 if (random->nextBool()) {
501 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
502 } else {
503 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
504 }
505 font.setSubpixel(random->nextBool());
506
507 const char* text = "The quick brown fox jumps over the lazy dog.";
508
509 // create some random x/y offsets, including negative offsets
510 static const int kMaxTrans = 1024;
511 int xPos = (random->nextU() % 2) * 2 - 1;
512 int yPos = (random->nextU() % 2) * 2 - 1;
513 int xInt = (random->nextU() % kMaxTrans) * xPos;
514 int yInt = (random->nextU() % kMaxTrans) * yPos;
515
516 return GrAtlasTextOp::CreateOpTestingOnly(
517 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
518}
519
520#endif
521
522