blob: d425b713e89d91e1f27ee9b57b2573e79f6c327c [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"
22#include "src/gpu/text/GrAtlasManager.h"
23#include "src/gpu/text/GrStrikeCache.h"
joshualitta751c972015-11-20 13:37:32 -080024
joshualitt60ce86d2015-11-23 13:08:22 -080025///////////////////////////////////////////////////////////////////////////////////////////////////
26
Robert Phillipsb97da532019-02-12 15:24:12 -050027std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040028 GrPaint&& paint,
29 GrMaskFormat maskFormat,
30 int glyphCount,
31 bool needsTransform) {
Robert Phillips9da87e02019-02-04 13:26:26 -050032 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040033
34 std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
Robert Phillips7c525e62018-06-12 10:11:12 -040035
36 switch (maskFormat) {
37 case kA8_GrMaskFormat:
38 op->fMaskType = kGrayscaleCoverageMask_MaskType;
39 break;
40 case kA565_GrMaskFormat:
41 op->fMaskType = kLCDCoverageMask_MaskType;
42 break;
43 case kARGB_GrMaskFormat:
44 op->fMaskType = kColorBitmapMask_MaskType;
45 break;
46 }
47 op->fNumGlyphs = glyphCount;
48 op->fGeoCount = 1;
49 op->fLuminanceColor = 0;
50 op->fNeedsGlyphTransform = needsTransform;
51 return op;
52 }
53
54std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
Robert Phillipsb97da532019-02-12 15:24:12 -050055 GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040056 GrPaint&& paint,
57 int glyphCount,
58 const GrDistanceFieldAdjustTable* distanceAdjustTable,
59 bool useGammaCorrectDistanceTable,
60 SkColor luminanceColor,
61 const SkSurfaceProps& props,
62 bool isAntiAliased,
63 bool useLCD) {
Robert Phillips9da87e02019-02-04 13:26:26 -050064 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040065
66 std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
Robert Phillips7c525e62018-06-12 10:11:12 -040067
68 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
69 bool isLCD = useLCD && SkPixelGeometryIsH(props.pixelGeometry());
70 op->fMaskType = !isAntiAliased ? kAliasedDistanceField_MaskType
71 : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
72 : kLCDDistanceField_MaskType)
73 : kGrayscaleDistanceField_MaskType;
74 op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
75 op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable;
76 op->fLuminanceColor = luminanceColor;
77 op->fNumGlyphs = glyphCount;
78 op->fGeoCount = 1;
79 return op;
80 }
81
joshualitta751c972015-11-20 13:37:32 -080082static const int kDistanceAdjustLumShift = 5;
83
Brian Salomon5c6ac642017-12-19 11:09:32 -050084void GrAtlasTextOp::init() {
85 const Geometry& geo = fGeoData[0];
Brian Salomon5c6ac642017-12-19 11:09:32 -050086 if (this->usesDistanceFields()) {
87 bool isLCD = this->isLCD();
88
89 const SkMatrix& viewMatrix = geo.fViewMatrix;
90
91 fDFGPFlags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
92 fDFGPFlags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
93 fDFGPFlags |= viewMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
94 fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
95 fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
96 ? kAliased_DistanceFieldEffectFlag
97 : 0;
98
99 if (isLCD) {
100 fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
101 fDFGPFlags |=
102 (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
103 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400104
105 fNeedsGlyphTransform = true;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500106 }
Jim Van Verth70276912018-06-01 13:46:46 -0400107
108 SkRect bounds;
Herb Derby660c2ff2019-11-14 18:22:41 -0500109 geo.fBlob->computeSubRunBounds(&bounds, *geo.fSubRunPtr, geo.fViewMatrix, geo.fX, geo.fY,
Jim Van Verth70276912018-06-01 13:46:46 -0400110 fNeedsGlyphTransform);
111 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
112 // we treat this as a set of non-AA rects rendered with a texture.
Greg Daniel5faf4742019-10-01 15:14:44 -0400113 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114}
115
Chris Dalton1706cbf2019-05-21 19:35:29 -0600116void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500117 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500118}
119
Brian Osman9a390ac2018-11-12 09:47:48 -0500120#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -0500121SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800122 SkString str;
123
124 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500125 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800126 i,
Brian Osmancf860852018-10-31 14:04:39 -0400127 fGeoData[i].fColor.toBytes_RGBA(),
joshualitt8e0ef292016-02-19 14:13:03 -0800128 fGeoData[i].fX,
Herb Derby1b8dcd12019-11-15 15:21:15 -0500129 fGeoData[i].fY);
joshualitta751c972015-11-20 13:37:32 -0800130 }
131
Brian Salomon44acb5b2017-07-18 19:59:24 -0400132 str += fProcessors.dumpProcessors();
133 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800134 return str;
135}
Brian Osman9a390ac2018-11-12 09:47:48 -0500136#endif
joshualitta751c972015-11-20 13:37:32 -0800137
Brian Salomon44acb5b2017-07-18 19:59:24 -0400138GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
139 return FixedFunctionFlags::kNone;
140}
141
Chris Dalton6ce447a2019-06-23 18:07:38 -0600142GrProcessorSet::Analysis GrAtlasTextOp::finalize(
143 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
144 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400145 GrProcessorAnalysisCoverage coverage;
146 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800147 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400148 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800149 } else {
Brian Osman09068252018-01-03 09:57:29 -0500150 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800151 }
joshualitta751c972015-11-20 13:37:32 -0800152 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800153 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400154 case kAliasedDistanceField_MaskType:
155 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400156 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800157 break;
158 case kLCDCoverageMask_MaskType:
159 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400160 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400161 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800162 break;
163 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400164 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400165 break;
joshualitta751c972015-11-20 13:37:32 -0800166 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700167 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600168 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
169 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400170 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700171 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800172}
173
Brian Salomon18923f92017-11-06 16:26:02 -0500174static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400175 size_t vertexStride, int glyphCount) {
176 for (int i = 0; i < glyphCount; ++i) {
Brian Salomon18923f92017-11-06 16:26:02 -0500177 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
178 const SkPoint* blobPositionRB =
179 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400180
Jim Van Verth328a33f2017-10-20 12:14:22 -0400181 // positions for bitmap glyphs are pixel boundary aligned
Brian Osman7ca90d22017-11-10 15:31:27 -0500182 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
183 SkScalarRoundToInt(blobPositionLT->fY),
184 SkScalarRoundToInt(blobPositionRB->fX),
185 SkScalarRoundToInt(blobPositionRB->fY));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400186 if (clipRect.contains(positionRect)) {
187 memcpy(currVertex, blobVertices, 4 * vertexStride);
188 currVertex += 4 * vertexStride;
189 } else {
190 // Pull out some more data that we'll need.
191 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
192 // and it avoids a lot of conditionals.
Brian Salomon18923f92017-11-06 16:26:02 -0500193 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
Jim Van Verth328a33f2017-10-20 12:14:22 -0400194 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
Brian Salomon18923f92017-11-06 16:26:02 -0500195 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
196 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
197 coordOffset);
Jim Van Verth328a33f2017-10-20 12:14:22 -0400198 // Pull out the texel coordinates and texture index bits
199 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
200 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
201 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
202 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
203 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
204 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400205
Jim Van Verth328a33f2017-10-20 12:14:22 -0400206 int positionRectWidth = positionRect.width();
207 int positionRectHeight = positionRect.height();
208 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
209 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400210
211 // Clip position and texCoords to the clipRect
Jim Van Verth328a33f2017-10-20 12:14:22 -0400212 unsigned int delta;
213 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
214 coordsRectL += delta;
215 positionRect.fLeft += delta;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400216
Jim Van Verth328a33f2017-10-20 12:14:22 -0400217 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
218 coordsRectT += delta;
219 positionRect.fTop += delta;
220
221 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
222 coordsRectR -= delta;
223 positionRect.fRight -= delta;
224
225 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
226 coordsRectB -= delta;
227 positionRect.fBottom -= delta;
228
229 // Repack texel coordinates and index
230 coordsRectL = coordsRectL << 1 | pageIndexX;
231 coordsRectT = coordsRectT << 1 | pageIndexY;
232 coordsRectR = coordsRectR << 1 | pageIndexX;
233 coordsRectB = coordsRectB << 1 | pageIndexY;
234
235 // Set new positions and coords
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400236 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
237 currPosition->fX = positionRect.fLeft;
238 currPosition->fY = positionRect.fTop;
239 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400240 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
241 currCoords[0] = coordsRectL;
242 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400243 currVertex += vertexStride;
244
245 currPosition = reinterpret_cast<SkPoint*>(currVertex);
246 currPosition->fX = positionRect.fLeft;
247 currPosition->fY = positionRect.fBottom;
248 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400249 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
250 currCoords[0] = coordsRectL;
251 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400252 currVertex += vertexStride;
253
254 currPosition = reinterpret_cast<SkPoint*>(currVertex);
255 currPosition->fX = positionRect.fRight;
256 currPosition->fY = positionRect.fTop;
257 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400258 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
259 currCoords[0] = coordsRectR;
260 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400261 currVertex += vertexStride;
262
263 currPosition = reinterpret_cast<SkPoint*>(currVertex);
264 currPosition->fX = positionRect.fRight;
265 currPosition->fY = positionRect.fBottom;
266 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400267 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
268 currCoords[0] = coordsRectR;
269 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400270 currVertex += vertexStride;
271 }
272
273 blobVertices += 4 * vertexStride;
274 }
275}
276
Brian Salomon91326c32017-08-09 16:02:19 -0400277void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500278 auto resourceProvider = target->resourceProvider();
279
joshualitta751c972015-11-20 13:37:32 -0800280 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
281 // TODO actually only invert if we don't have RGBA
282 SkMatrix localMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500283 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800284 return;
285 }
286
Robert Phillips5a66efb2018-03-07 15:13:18 -0500287 GrAtlasManager* atlasManager = target->atlasManager();
Herb Derby081e6f32019-01-16 13:46:02 -0500288 GrStrikeCache* glyphCache = target->glyphCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500289
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400290 GrMaskFormat maskFormat = this->maskFormat();
291
Jim Van Verthcbeae032018-05-16 14:54:41 -0400292 unsigned int numActiveProxies;
293 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
294 if (!proxies) {
joshualitta751c972015-11-20 13:37:32 -0800295 SkDebugf("Could not allocate backing texture for atlas\n");
296 return;
297 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400298 SkASSERT(proxies[0]);
joshualitta751c972015-11-20 13:37:32 -0800299
Brian Salomon7eae3e02018-08-07 14:02:38 +0000300 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomonb0047b52019-12-05 19:52:25 +0000301 GR_STATIC_ASSERT(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
302 GR_STATIC_ASSERT(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000303
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700304 auto fixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000305 for (unsigned i = 0; i < numActiveProxies; ++i) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700306 fixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
Greg Danielb20d7e52019-09-03 13:54:39 -0400307 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
308 // don't get added during the visitProxies call. Thus we add them here.
309 target->sampledProxyArray()->push_back(proxies[i].get());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000310 }
Brian Salomon49348902018-06-26 09:12:38 -0400311
312 FlushInfo flushInfo;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700313 flushInfo.fFixedDynamicState = fixedDynamicState;
Brian Salomon49348902018-06-26 09:12:38 -0400314
Jim Van Verthb515ae72018-05-23 16:44:55 -0400315 bool vmPerspective = fGeoData[0].fViewMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800316 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500317 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
318 *target->caps().shaderCaps(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400319 proxies, numActiveProxies);
joshualitta751c972015-11-20 13:37:32 -0800320 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400321 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
322 : GrSamplerState::ClampNearest();
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500323 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(target->allocator(),
Brian Osmanc906d252018-12-04 11:17:46 -0500324 *target->caps().shaderCaps(), this->color(), false, proxies, numActiveProxies,
325 samplerState, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800326 }
327
joshualitta751c972015-11-20 13:37:32 -0800328 flushInfo.fGlyphsToFlush = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500329 size_t vertexStride = flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800330
joshualitta751c972015-11-20 13:37:32 -0800331 int glyphCount = this->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800332
Brian Salomon12d22642019-01-29 14:38:50 -0500333 void* vertices = target->makeVertexSpace(vertexStride, glyphCount * kVerticesPerGlyph,
334 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset);
Robert Phillipsee08d522019-10-28 16:34:44 -0400335 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
joshualitta751c972015-11-20 13:37:32 -0800336 if (!vertices || !flushInfo.fVertexBuffer) {
337 SkDebugf("Could not allocate vertices\n");
338 return;
339 }
340
Brian Salomon18923f92017-11-06 16:26:02 -0500341 char* currVertex = reinterpret_cast<char*>(vertices);
joshualitta751c972015-11-20 13:37:32 -0800342
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400343 // each of these is a SubRun
joshualitta751c972015-11-20 13:37:32 -0800344 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800345 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800346 Blob* blob = args.fBlob;
Brian Osman1be2b7c2018-10-29 16:07:15 -0400347 // TODO4F: Preserve float colors
Herb Derby86240592018-05-24 16:12:31 -0400348 GrTextBlob::VertexRegenerator regenerator(
Herb Derby660c2ff2019-11-14 18:22:41 -0500349 resourceProvider, blob, args.fSubRunPtr, args.fViewMatrix, args.fX, args.fY,
Brian Osmancf860852018-10-31 14:04:39 -0400350 args.fColor.toBytes_RGBA(), target->deferredUploadTarget(), glyphCache,
Herb Derbycb443a52019-11-11 18:01:36 -0500351 atlasManager);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500352 bool done = false;
353 while (!done) {
Herb Derby86240592018-05-24 16:12:31 -0400354 GrTextBlob::VertexRegenerator::Result result;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500355 if (!regenerator.regenerate(&result)) {
356 break;
357 }
358 done = result.fFinished;
359
Brian Salomon18923f92017-11-06 16:26:02 -0500360 // Copy regenerated vertices from the blob to our vertex buffer.
361 size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
362 if (args.fClipRect.isEmpty()) {
363 memcpy(currVertex, result.fFirstVertex, vertexBytes);
364 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400365 SkASSERT(!vmPerspective);
Brian Salomon18923f92017-11-06 16:26:02 -0500366 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
367 result.fGlyphsRegenerated);
368 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400369 if (fNeedsGlyphTransform && !args.fViewMatrix.isIdentity()) {
Brian Salomon5c6ac642017-12-19 11:09:32 -0500370 // We always do the distance field view matrix transformation after copying rather
371 // than during blob vertex generation time in the blob as handling successive
372 // arbitrary transformations would be complicated and accumulate error.
373 if (args.fViewMatrix.hasPerspective()) {
374 auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
Brian Salomon7c2192b2018-01-08 09:47:57 -0500375 SkMatrixPriv::MapHomogeneousPointsWithStride(
376 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
377 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500378 } else {
379 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500380 SkMatrixPriv::MapPointsWithStride(
381 args.fViewMatrix, pos, vertexStride,
382 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500383 }
384 }
Brian Salomon18923f92017-11-06 16:26:02 -0500385 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
386 if (!result.fFinished) {
387 this->flush(target, &flushInfo);
388 }
389 currVertex += vertexBytes;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500390 }
joshualitta751c972015-11-20 13:37:32 -0800391 }
joshualitta751c972015-11-20 13:37:32 -0800392 this->flush(target, &flushInfo);
393}
394
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700395void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700396 flushState->executeDrawsAndUploadsForMeshDrawOp(
Chris Daltonbaa1b352019-04-03 12:03:00 -0600397 this, chainBounds, std::move(fProcessors), GrPipeline::InputFlags::kNone);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700398}
399
Brian Salomone5b399e2017-07-19 13:50:54 -0400400void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500401 if (!flushInfo->fGlyphsToFlush) {
402 return;
403 }
404
Robert Phillips5a66efb2018-03-07 15:13:18 -0500405 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500406
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500407 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400408 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500409
Jim Van Verthcbeae032018-05-16 14:54:41 -0400410 unsigned int numActiveProxies;
411 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
412 SkASSERT(proxies);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500413 // Something has gone terribly wrong, bail
414 if (!proxies || 0 == numActiveProxies) {
415 return;
416 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400417 if (gp->numTextureSamplers() != (int) numActiveProxies) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400418 // During preparation the number of atlas pages has increased.
419 // Update the proxies used in the GP to match.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000420 for (unsigned i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
421 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
Greg Danielb20d7e52019-09-03 13:54:39 -0400422 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
423 // proxies don't get added during the visitProxies call. Thus we add them here.
424 target->sampledProxyArray()->push_back(proxies[i].get());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000425 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400426 if (this->usesDistanceFields()) {
427 if (this->isLCD()) {
428 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400429 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400430 } else {
431 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400432 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400433 }
434 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400435 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
436 : GrSamplerState::ClampNearest();
Jim Van Verthcbeae032018-05-16 14:54:41 -0400437 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
Jim Van Verthcf838c72018-03-05 14:40:36 -0500438 samplerState);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400439 }
440 }
Brian Salomondbf70722019-02-07 11:31:24 -0500441 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000442 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -0500443 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, kVerticesPerGlyph,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000444 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
Brian Salomon12d22642019-01-29 14:38:50 -0500445 mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
Robert Phillipscea290f2019-11-06 11:21:03 -0500446 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState,
447 nullptr, GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800448 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
449 flushInfo->fGlyphsToFlush = 0;
450}
451
Brian Salomon7eae3e02018-08-07 14:02:38 +0000452GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500453 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400454 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000455 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400456 }
457
joshualitta751c972015-11-20 13:37:32 -0800458 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000459 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800460 }
461
Brian Salomon5c6ac642017-12-19 11:09:32 -0500462 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
463 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
464
465 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000466 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500467 }
468
Jim Van Verthb515ae72018-05-23 16:44:55 -0400469 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000470 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400471 }
472
473 if (fNeedsGlyphTransform &&
474 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000475 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400476 }
477
Brian Salomon5c6ac642017-12-19 11:09:32 -0500478 if (this->usesDistanceFields()) {
479 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000480 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800481 }
482
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400483 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000484 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800485 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500486 } else {
487 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000488 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500489 }
joshualitta751c972015-11-20 13:37:32 -0800490 }
491
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400492 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
493 // We use the largest possible vertex size for this
494 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
Jim Van Verth56c37142017-10-31 14:44:25 -0400495 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400496 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000497 return CombineResult::kCannotCombine;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400498 }
499
Brian Salomon344ec422016-12-15 10:58:41 -0500500 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800501
Jim Van Verth56c37142017-10-31 14:44:25 -0400502 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800503 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800504
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400505 // We reallocate at a rate of 1.5x to try to get better total memory usage
506 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400507 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400508 while (newAllocSize < newGeoCount) {
509 newAllocSize += newAllocSize / 2;
510 }
joshualitta751c972015-11-20 13:37:32 -0800511 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400512 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800513 }
514
Brian Salomon344ec422016-12-15 10:58:41 -0500515 // 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 -0800516 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500517 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800518#ifdef SK_DEBUG
519 for (int i = 0; i < that->fGeoCount; ++i) {
520 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
521 }
522#endif
523 that->fGeoCount = 0;
524 fGeoCount = newGeoCount;
525
Brian Salomon7eae3e02018-08-07 14:02:38 +0000526 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800527}
528
joshualitta751c972015-11-20 13:37:32 -0800529// TODO trying to figure out why lcd is so whack
Herb Derby26cbe512018-05-24 14:39:01 -0400530// (see comments in GrTextContext::ComputeCanonicalColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500531GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
532 const GrShaderCaps& caps,
533 const sk_sp<GrTextureProxy>* proxies,
534 unsigned int numActiveProxies) const {
joshualitta751c972015-11-20 13:37:32 -0800535 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500536
537 SkMatrix localMatrix = SkMatrix::I();
538 if (this->usesLocalCoords()) {
539 // If this fails we'll just use I().
540 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
541 (void)result;
542 }
joshualitta751c972015-11-20 13:37:32 -0800543
544 // see if we need to create a new effect
545 if (isLCD) {
brianosman0586f5c2016-04-12 12:48:21 -0700546 float redCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400547 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500548 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700549 float greenCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400550 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500551 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700552 float blueCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400553 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500554 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800555 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500556 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
557 redCorrection, greenCorrection, blueCorrection);
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500558 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500559 GrSamplerState::ClampBilerp(), widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500560 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800561 } else {
joshualitta751c972015-11-20 13:37:32 -0800562#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400563 float correction = 0;
564 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400565 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
566 fLuminanceColor);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400567 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
568 fUseGammaCorrectDistanceTable);
569 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500570 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500571 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500572 correction, fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800573#else
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500574 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500575 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500576 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800577#endif
578 }
joshualitta751c972015-11-20 13:37:32 -0800579}
joshualittddd22d82016-02-16 06:47:52 -0800580