blob: 8bfda875e8ac7d6250fe1c0fe07820e82a140ac7 [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"
11#include "include/private/GrRecordingContext.h"
12#include "src/core/SkMathPriv.h"
13#include "src/core/SkMatrixPriv.h"
14#include "src/core/SkStrikeCache.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrMemoryPool.h"
17#include "src/gpu/GrOpFlushState.h"
18#include "src/gpu/GrRecordingContextPriv.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/effects/GrBitmapTextGeoProc.h"
21#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050022#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040024#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080025
joshualitt60ce86d2015-11-23 13:08:22 -080026///////////////////////////////////////////////////////////////////////////////////////////////////
27
Herb Derby3c873af2020-04-29 15:56:07 -040028GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
29 GrPaint&& paint,
30 GrTextBlob::SubRun* subrun,
31 const SkMatrix& drawMatrix,
32 SkPoint drawOrigin,
33 const SkIRect& clipRect,
34 const SkPMColor4f& filteredColor,
35 SkColor luminanceColor,
36 bool useGammaCorrectDistanceTable,
37 uint32_t DFGPFlags)
38 : INHERITED(ClassID())
39 , fMaskType{maskType}
40 , fNeedsGlyphTransform{subrun->needsTransform()}
41 , fLuminanceColor{luminanceColor}
42 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
43 , fDFGPFlags{DFGPFlags}
44 , fGeoDataAllocSize{kMinGeometryAllocated}
45 , fProcessors{std::move(paint)}
Herb Derbyd5cbc1e2020-05-16 13:45:55 -040046 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040047 GrAtlasTextOp::Geometry& geometry = fGeoData[0];
48
49 // Unref handled in ~GrAtlasTextOp().
50 geometry.fBlob = SkRef(subrun->fBlob);
51 geometry.fSubRunPtr = subrun;
52 geometry.fDrawMatrix = drawMatrix;
53 geometry.fDrawOrigin = drawOrigin;
54 geometry.fClipRect = clipRect;
55 geometry.fColor = subrun->maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE
56 : filteredColor;
57 fGeoCount = 1;
58
59 SkRect bounds = subrun->deviceRect(drawMatrix, drawOrigin);
60 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
61 // we treat this as a set of non-AA rects rendered with a texture.
62 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
63}
64
Herb Derby64391c42020-05-16 14:32:15 -040065// Entry point just for the SkAtlasTextTarget
66std::unique_ptr<GrTextBlob::Mask3DVertex[][4]> GrAtlasTextOp::Geometry::textTargetCreateVertexData(
67 int offset, int count) const {
68 std::unique_ptr<GrTextBlob::Mask3DVertex[][4]> data{new GrTextBlob::Mask3DVertex[count][4]};
69
70 fSubRunPtr->fillTextTargetVertexData(data.get(), offset, count, fColor.toBytes_RGBA(),
71 fDrawOrigin);
72
73 return data;
74}
75
76void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
77 fSubRunPtr->fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
78 fDrawMatrix, fDrawOrigin, fClipRect);
79}
80
Robert Phillipsb97da532019-02-12 15:24:12 -050081std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040082 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -040083 GrTextBlob::SubRun* subrun,
84 const SkMatrix& drawMatrix,
85 SkPoint drawOrigin,
86 const SkIRect& clipRect,
87 const SkPMColor4f& filteredColor) {
88 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040089
Herb Derby3c873af2020-04-29 15:56:07 -040090 MaskType maskType = [&]() {
91 switch (subrun->maskFormat()) {
92 case kA8_GrMaskFormat: return kGrayscaleCoverageMask_MaskType;
93 case kA565_GrMaskFormat: return kLCDCoverageMask_MaskType;
94 case kARGB_GrMaskFormat: return kColorBitmapMask_MaskType;
95 // Needed to placate some compilers.
96 default: return kGrayscaleCoverageMask_MaskType;
Robert Phillips7c525e62018-06-12 10:11:12 -040097 }
Herb Derby3c873af2020-04-29 15:56:07 -040098 }();
99
100 return pool->allocate<GrAtlasTextOp>(maskType,
101 std::move(paint),
102 subrun,
103 drawMatrix,
104 drawOrigin,
105 clipRect,
106 filteredColor,
107 0,
108 false,
109 0);
110}
Robert Phillips7c525e62018-06-12 10:11:12 -0400111
112std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
Robert Phillipsb97da532019-02-12 15:24:12 -0500113 GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400114 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -0400115 GrTextBlob::SubRun* subrun,
116 const SkMatrix& drawMatrix,
117 SkPoint drawOrigin,
118 const SkIRect& clipRect,
119 const SkPMColor4f& filteredColor,
Robert Phillips7c525e62018-06-12 10:11:12 -0400120 bool useGammaCorrectDistanceTable,
121 SkColor luminanceColor,
Herb Derby3c873af2020-04-29 15:56:07 -0400122 const SkSurfaceProps& props) {
123 GrOpMemoryPool* pool = context->priv().opMemoryPool();
124 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
125 bool isLCD = subrun->hasUseLCDText() && SkPixelGeometryIsH(props.pixelGeometry());
126 MaskType maskType = !subrun->isAntiAliased() ? kAliasedDistanceField_MaskType
127 : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
128 : kLCDDistanceField_MaskType)
129 : kGrayscaleDistanceField_MaskType;
Robert Phillipsc994a932018-06-19 13:09:54 -0400130
Herb Derby3c873af2020-04-29 15:56:07 -0400131 uint32_t DFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
132 DFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
133 DFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
134 DFGPFlags |= useGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
135 DFGPFlags |= kAliasedDistanceField_MaskType == maskType ? kAliased_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400136
Herb Derby3c873af2020-04-29 15:56:07 -0400137 if (isLCD) {
138 DFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
139 DFGPFlags |= kLCDBGRDistanceField_MaskType == maskType ? kBGR_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400140 }
141
Herb Derby3c873af2020-04-29 15:56:07 -0400142 return pool->allocate<GrAtlasTextOp>(maskType,
143 std::move(paint),
144 subrun,
145 drawMatrix,
146 drawOrigin,
147 clipRect,
148 filteredColor,
149 luminanceColor,
150 useGammaCorrectDistanceTable,
151 DFGPFlags);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500152}
153
Chris Dalton1706cbf2019-05-21 19:35:29 -0600154void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500155 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500156}
157
Brian Osman9a390ac2018-11-12 09:47:48 -0500158#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -0500159SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800160 SkString str;
161
162 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500163 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800164 i,
Brian Osmancf860852018-10-31 14:04:39 -0400165 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500166 fGeoData[i].fDrawOrigin.x(),
167 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800168 }
169
Brian Salomon44acb5b2017-07-18 19:59:24 -0400170 str += fProcessors.dumpProcessors();
171 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800172 return str;
173}
Brian Osman9a390ac2018-11-12 09:47:48 -0500174#endif
joshualitta751c972015-11-20 13:37:32 -0800175
Brian Salomon44acb5b2017-07-18 19:59:24 -0400176GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
177 return FixedFunctionFlags::kNone;
178}
179
Chris Dalton6ce447a2019-06-23 18:07:38 -0600180GrProcessorSet::Analysis GrAtlasTextOp::finalize(
181 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
182 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400183 GrProcessorAnalysisCoverage coverage;
184 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800185 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400186 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800187 } else {
Brian Osman09068252018-01-03 09:57:29 -0500188 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800189 }
joshualitta751c972015-11-20 13:37:32 -0800190 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800191 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400192 case kAliasedDistanceField_MaskType:
193 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400194 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800195 break;
196 case kLCDCoverageMask_MaskType:
197 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400198 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400199 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800200 break;
201 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400202 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400203 break;
joshualitta751c972015-11-20 13:37:32 -0800204 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700205 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600206 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
207 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400208 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700209 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800210}
211
Brian Salomon91326c32017-08-09 16:02:19 -0400212void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500213 auto resourceProvider = target->resourceProvider();
214
joshualitta751c972015-11-20 13:37:32 -0800215 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
216 // TODO actually only invert if we don't have RGBA
217 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500218 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800219 return;
220 }
221
Robert Phillips5a66efb2018-03-07 15:13:18 -0500222 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000223
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400224 GrMaskFormat maskFormat = this->maskFormat();
225
Greg Daniel9715b6c2019-12-10 15:03:10 -0500226 unsigned int numActiveViews;
227 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
228 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800229 SkDebugf("Could not allocate backing texture for atlas\n");
230 return;
231 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500232 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800233
Brian Salomon7eae3e02018-08-07 14:02:38 +0000234 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500235 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
236 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000237
Chris Dalton304e14d2020-03-17 14:29:06 -0600238 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500239 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600240 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400241 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
242 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500243 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000244 }
Brian Salomon49348902018-06-26 09:12:38 -0400245
246 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600247 flushInfo.fPrimProcProxies = primProcProxies;
Brian Salomon49348902018-06-26 09:12:38 -0400248
Herb Derby1c5be7b2019-12-13 12:03:06 -0500249 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800250 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500251 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
252 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500253 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800254 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500255 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
256 : GrSamplerState::Filter::kNearest;
257 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
258 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
259 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800260 }
261
Herb Derby23f29762020-01-10 16:26:14 -0500262 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800263
Brian Salomon43cbd722020-01-03 22:09:12 -0500264 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500265 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
266 const int quadSize = vertexStride * kVerticesPerGlyph;
267 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
268
269 // Where the quad buffer begins and ends relative to totalGlyphsRegened.
270 int quadBufferBegin = 0;
271 int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer);
272
Robert Phillipsee08d522019-10-28 16:34:44 -0400273 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Herb Derby23f29762020-01-10 16:26:14 -0500274 void* vertices = target->makeVertexSpace(
275 vertexStride,
276 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
277 &flushInfo.fVertexBuffer,
278 &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800279 if (!vertices || !flushInfo.fVertexBuffer) {
280 SkDebugf("Could not allocate vertices\n");
281 return;
282 }
283
Herb Derby23f29762020-01-10 16:26:14 -0500284 // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and
285 // quad buffer indices are calculated from this.
286 int totalGlyphsRegened = 0;
joshualitta751c972015-11-20 13:37:32 -0800287 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800288 const Geometry& args = fGeoData[i];
Herb Derby23f29762020-01-10 16:26:14 -0500289 auto subRun = args.fSubRunPtr;
290 SkASSERT((int)subRun->vertexStride() == vertexStride);
291
Robert Phillips207d24b2020-04-09 10:23:42 -0400292 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500293
Brian Osman1be2b7c2018-10-29 16:07:15 -0400294 // TODO4F: Preserve float colors
Robert Phillips297a2192020-04-08 15:26:54 -0400295 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
296 target->deferredUploadTarget(), atlasManager);
Herb Derby23f29762020-01-10 16:26:14 -0500297
298 // Where the subRun begins and ends relative to totalGlyphsRegened.
299 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400300 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500301
302 // Draw all the glyphs in the subRun.
303 while (totalGlyphsRegened < subRunEnd) {
304 // drawBegin and drawEnd are indices for the subRun on the
305 // interval [0, subRun->fGlyphs.size()).
306 int drawBegin = totalGlyphsRegened - subRunBegin;
307 // drawEnd is either the end of the subRun or the end of the current quad buffer.
308 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
309 auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd);
310
311 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400312 if (!ok) {
313 return;
314 }
Herb Derby23f29762020-01-10 16:26:14 -0500315
316 // Update all the vertices for glyphsRegenerate glyphs.
317 if (glyphsRegenerated > 0) {
318 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500319 auto regeneratedQuadBuffer =
320 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400321 int subRunIndex = totalGlyphsRegened - subRunBegin;
322 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500323 }
Herb Derby23f29762020-01-10 16:26:14 -0500324
325 totalGlyphsRegened += glyphsRegenerated;
326 flushInfo.fGlyphsToFlush += glyphsRegenerated;
327
328 // regenerate() has stopped part way through a SubRun. This means that either the atlas
329 // or the quad buffer is full or both. There is a case were the flow through
330 // the loop is strange. If we run out of quad buffer space at the same time the
331 // SubRun ends, then this is not triggered which is the right result for the last
332 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
333 // will process no glyphs, and return to this point where the quad buffer will be
334 // expanded.
335 if (totalGlyphsRegened != subRunEnd) {
336 // Flush if not all glyphs drawn because either the quad buffer is full or the
337 // atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500338 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500339 if (totalGlyphsRegened == quadBufferEnd) {
340 // Quad buffer is full. Get more buffer.
341 quadBufferBegin = totalGlyphsRegened;
342 int quadBufferSize =
343 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
344 quadBufferEnd = quadBufferBegin + quadBufferSize;
345
346 vertices = target->makeVertexSpace(
347 vertexStride,
348 kVerticesPerGlyph * quadBufferSize,
349 &flushInfo.fVertexBuffer,
350 &flushInfo.fVertexOffset);
351 if (!vertices || !flushInfo.fVertexBuffer) {
352 SkDebugf("Could not allocate vertices\n");
353 return;
354 }
355 }
356 }
357 }
Herb Derby5f6f8512020-01-10 12:50:35 -0500358 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500359 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800360}
361
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700362void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500363 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
364 std::move(fProcessors),
365 GrPipeline::InputFlags::kNone);
366
367 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700368}
369
Herb Derby4513cdd2020-01-31 13:28:49 -0500370void GrAtlasTextOp::createDrawForGeneratedGlyphs(
371 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500372 if (!flushInfo->fGlyphsToFlush) {
373 return;
374 }
375
Robert Phillips5a66efb2018-03-07 15:13:18 -0500376 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500377
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500378 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400379 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500380
Greg Daniel9715b6c2019-12-10 15:03:10 -0500381 unsigned int numActiveViews;
382 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
383 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500384 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500385 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500386 return;
387 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500388 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400389 // During preparation the number of atlas pages has increased.
390 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500391 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600392 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400393 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
394 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500395 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500396 // These will get unreffed when the previously recorded draws destruct.
397 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600398 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500399 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000400 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400401 if (this->usesDistanceFields()) {
402 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500403 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500404 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400405 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500406 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500407 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400408 }
409 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500410 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
411 : GrSamplerState::Filter::kNearest;
412 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400413 }
414 }
Brian Salomondbf70722019-02-07 11:31:24 -0500415 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600416 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600417 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
418 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
419 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600420 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600421 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800422 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
423 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500424 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800425}
426
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500427GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
428 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500429 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400430 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000431 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400432 }
433
joshualitta751c972015-11-20 13:37:32 -0800434 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000435 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800436 }
437
Herb Derby1c5be7b2019-12-13 12:03:06 -0500438 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
439 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500440
Mike Reed2c383152019-12-18 16:47:47 -0500441 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000442 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500443 }
444
Jim Van Verthb515ae72018-05-23 16:44:55 -0400445 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000446 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400447 }
448
449 if (fNeedsGlyphTransform &&
450 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000451 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400452 }
453
Brian Salomon5c6ac642017-12-19 11:09:32 -0500454 if (this->usesDistanceFields()) {
455 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000456 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800457 }
458
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400459 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000460 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800461 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500462 } else {
463 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000464 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500465 }
joshualitta751c972015-11-20 13:37:32 -0800466 }
467
Brian Salomon344ec422016-12-15 10:58:41 -0500468 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800469
Jim Van Verth56c37142017-10-31 14:44:25 -0400470 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800471 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800472
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400473 // We reallocate at a rate of 1.5x to try to get better total memory usage
474 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400475 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400476 while (newAllocSize < newGeoCount) {
477 newAllocSize += newAllocSize / 2;
478 }
joshualitta751c972015-11-20 13:37:32 -0800479 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400480 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800481 }
482
Brian Salomon344ec422016-12-15 10:58:41 -0500483 // 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 -0800484 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500485 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800486#ifdef SK_DEBUG
487 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500488 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800489 }
490#endif
491 that->fGeoCount = 0;
492 fGeoCount = newGeoCount;
493
Brian Salomon7eae3e02018-08-07 14:02:38 +0000494 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800495}
496
Herb Derby3c873af2020-04-29 15:56:07 -0400497static const int kDistanceAdjustLumShift = 5;
498
joshualitta751c972015-11-20 13:37:32 -0800499// TODO trying to figure out why lcd is so whack
Herb Derby26cbe512018-05-24 14:39:01 -0400500// (see comments in GrTextContext::ComputeCanonicalColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500501GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
502 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500503 const GrSurfaceProxyView* views,
504 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800505 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500506
507 SkMatrix localMatrix = SkMatrix::I();
508 if (this->usesLocalCoords()) {
509 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500510 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500511 (void)result;
512 }
joshualitta751c972015-11-20 13:37:32 -0800513
Robert Phillips841c9a52020-03-27 12:41:31 -0400514 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
515
joshualitta751c972015-11-20 13:37:32 -0800516 // see if we need to create a new effect
517 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400518 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400519 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500520 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400521 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400522 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500523 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400524 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400525 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500526 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800527 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500528 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
529 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500530 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500531 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500532 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800533 } else {
joshualitta751c972015-11-20 13:37:32 -0800534#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400535 float correction = 0;
536 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400537 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
538 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400539 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
540 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400541 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500542 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500543 GrSamplerState::Filter::kBilerp, correction,
544 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800545#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500546 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500547 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500548 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800549#endif
550 }
joshualitta751c972015-11-20 13:37:32 -0800551}
joshualittddd22d82016-02-16 06:47:52 -0800552