blob: 62991f4be161ff9d5d126d54dae179ed68d993e2 [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
Brian Salomon09e019e2016-12-15 10:20:35 -05008#include "GrAtlasTextOp.h"
Robert Phillipse4fda6c2018-02-21 12:10:41 -05009
Robert Phillips296b1cc2017-03-15 10:42:12 -040010#include "GrContext.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
joshualitta751c972015-11-20 13:37:32 -080012#include "GrResourceProvider.h"
joshualitta751c972015-11-20 13:37:32 -080013#include "SkGlyphCache.h"
halcanary4dbbd042016-06-07 17:21:10 -070014#include "SkMathPriv.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050015#include "SkMatrixPriv.h"
16#include "SkPoint3.h"
joshualitta751c972015-11-20 13:37:32 -080017#include "effects/GrBitmapTextGeoProc.h"
18#include "effects/GrDistanceFieldGeoProc.h"
Robert Phillips5c56af12018-02-28 16:37:34 +000019#include "text/GrAtlasGlyphCache.h"
joshualitta751c972015-11-20 13:37:32 -080020
joshualitt60ce86d2015-11-23 13:08:22 -080021///////////////////////////////////////////////////////////////////////////////////////////////////
22
joshualitta751c972015-11-20 13:37:32 -080023static const int kDistanceAdjustLumShift = 5;
24
Brian Salomon5c6ac642017-12-19 11:09:32 -050025void GrAtlasTextOp::init() {
26 const Geometry& geo = fGeoData[0];
Brian Salomon5c6ac642017-12-19 11:09:32 -050027 SkRect bounds;
28 geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, geo.fY);
29 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
30 // we treat this as a set of non-AA rects rendered with a texture.
31 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
32 if (this->usesDistanceFields()) {
33 bool isLCD = this->isLCD();
34
35 const SkMatrix& viewMatrix = geo.fViewMatrix;
36
37 fDFGPFlags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
38 fDFGPFlags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
39 fDFGPFlags |= viewMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
40 fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
41 fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
42 ? kAliased_DistanceFieldEffectFlag
43 : 0;
44
45 if (isLCD) {
46 fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
47 fDFGPFlags |=
48 (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
49 }
50 }
51}
52
Robert Phillipse4fda6c2018-02-21 12:10:41 -050053void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
54 fProcessors.visitProxies(func);
55
56 unsigned int numProxies;
Robert Phillips5c56af12018-02-28 16:37:34 +000057 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(this->maskFormat(),
58 &numProxies);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050059 for (unsigned int i = 0; i < numProxies; ++i) {
60 if (proxies[i]) {
61 func(proxies[i].get());
62 }
63 }
64}
65
Brian Salomon344ec422016-12-15 10:58:41 -050066SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080067 SkString str;
68
69 for (int i = 0; i < fGeoCount; ++i) {
70 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
71 i,
72 fGeoData[i].fColor,
joshualitt8e0ef292016-02-19 14:13:03 -080073 fGeoData[i].fX,
74 fGeoData[i].fY,
joshualittddd22d82016-02-16 06:47:52 -080075 fGeoData[i].fBlob->runCount());
joshualitta751c972015-11-20 13:37:32 -080076 }
77
Brian Salomon44acb5b2017-07-18 19:59:24 -040078 str += fProcessors.dumpProcessors();
79 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -080080 return str;
81}
82
Brian Salomon44acb5b2017-07-18 19:59:24 -040083GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
84 return FixedFunctionFlags::kNone;
85}
86
87GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040088 const GrAppliedClip* clip,
89 GrPixelConfigIsClamped dstIsClamped) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040090 GrProcessorAnalysisCoverage coverage;
91 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -080092 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040093 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -080094 } else {
Brian Osman09068252018-01-03 09:57:29 -050095 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -080096 }
joshualitta751c972015-11-20 13:37:32 -080097 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -080098 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -040099 case kAliasedDistanceField_MaskType:
100 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400101 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800102 break;
103 case kLCDCoverageMask_MaskType:
104 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400105 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400106 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800107 break;
108 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400109 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400110 break;
joshualitta751c972015-11-20 13:37:32 -0800111 }
Brian Osman09068252018-01-03 09:57:29 -0500112 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, dstIsClamped,
113 &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400114 fUsesLocalCoords = analysis.usesLocalCoords();
115 fCanCombineOnTouchOrOverlap =
116 !analysis.requiresDstTexture() &&
117 !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps));
118 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitta751c972015-11-20 13:37:32 -0800119}
120
Brian Salomon18923f92017-11-06 16:26:02 -0500121static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400122 size_t vertexStride, int glyphCount) {
123 for (int i = 0; i < glyphCount; ++i) {
Brian Salomon18923f92017-11-06 16:26:02 -0500124 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
125 const SkPoint* blobPositionRB =
126 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400127
Jim Van Verth328a33f2017-10-20 12:14:22 -0400128 // positions for bitmap glyphs are pixel boundary aligned
Brian Osman7ca90d22017-11-10 15:31:27 -0500129 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
130 SkScalarRoundToInt(blobPositionLT->fY),
131 SkScalarRoundToInt(blobPositionRB->fX),
132 SkScalarRoundToInt(blobPositionRB->fY));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400133 if (clipRect.contains(positionRect)) {
134 memcpy(currVertex, blobVertices, 4 * vertexStride);
135 currVertex += 4 * vertexStride;
136 } else {
137 // Pull out some more data that we'll need.
138 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
139 // and it avoids a lot of conditionals.
Brian Salomon18923f92017-11-06 16:26:02 -0500140 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
Jim Van Verth328a33f2017-10-20 12:14:22 -0400141 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
Brian Salomon18923f92017-11-06 16:26:02 -0500142 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
143 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
144 coordOffset);
Jim Van Verth328a33f2017-10-20 12:14:22 -0400145 // Pull out the texel coordinates and texture index bits
146 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
147 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
148 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
149 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
150 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
151 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400152
Jim Van Verth328a33f2017-10-20 12:14:22 -0400153 int positionRectWidth = positionRect.width();
154 int positionRectHeight = positionRect.height();
155 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
156 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400157
158 // Clip position and texCoords to the clipRect
Jim Van Verth328a33f2017-10-20 12:14:22 -0400159 unsigned int delta;
160 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
161 coordsRectL += delta;
162 positionRect.fLeft += delta;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400163
Jim Van Verth328a33f2017-10-20 12:14:22 -0400164 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
165 coordsRectT += delta;
166 positionRect.fTop += delta;
167
168 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
169 coordsRectR -= delta;
170 positionRect.fRight -= delta;
171
172 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
173 coordsRectB -= delta;
174 positionRect.fBottom -= delta;
175
176 // Repack texel coordinates and index
177 coordsRectL = coordsRectL << 1 | pageIndexX;
178 coordsRectT = coordsRectT << 1 | pageIndexY;
179 coordsRectR = coordsRectR << 1 | pageIndexX;
180 coordsRectB = coordsRectB << 1 | pageIndexY;
181
182 // Set new positions and coords
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400183 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
184 currPosition->fX = positionRect.fLeft;
185 currPosition->fY = positionRect.fTop;
186 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400187 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
188 currCoords[0] = coordsRectL;
189 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400190 currVertex += vertexStride;
191
192 currPosition = reinterpret_cast<SkPoint*>(currVertex);
193 currPosition->fX = positionRect.fLeft;
194 currPosition->fY = positionRect.fBottom;
195 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400196 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
197 currCoords[0] = coordsRectL;
198 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400199 currVertex += vertexStride;
200
201 currPosition = reinterpret_cast<SkPoint*>(currVertex);
202 currPosition->fX = positionRect.fRight;
203 currPosition->fY = positionRect.fTop;
204 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400205 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
206 currCoords[0] = coordsRectR;
207 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400208 currVertex += vertexStride;
209
210 currPosition = reinterpret_cast<SkPoint*>(currVertex);
211 currPosition->fX = positionRect.fRight;
212 currPosition->fY = positionRect.fBottom;
213 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400214 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
215 currCoords[0] = coordsRectR;
216 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400217 currVertex += vertexStride;
218 }
219
220 blobVertices += 4 * vertexStride;
221 }
222}
223
Brian Salomon91326c32017-08-09 16:02:19 -0400224void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500225 auto resourceProvider = target->resourceProvider();
226
joshualitta751c972015-11-20 13:37:32 -0800227 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
228 // TODO actually only invert if we don't have RGBA
229 SkMatrix localMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500230 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800231 SkDebugf("Cannot invert viewmatrix\n");
232 return;
233 }
234
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400235 GrMaskFormat maskFormat = this->maskFormat();
236
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500237 unsigned int atlasPageCount;
Robert Phillips5c56af12018-02-28 16:37:34 +0000238 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(maskFormat, &atlasPageCount);
Robert Phillips4bc70112018-03-01 10:24:02 -0500239 if (!proxies[0]) {
joshualitta751c972015-11-20 13:37:32 -0800240 SkDebugf("Could not allocate backing texture for atlas\n");
241 return;
242 }
243
bsalomon342bfc22016-04-01 06:06:20 -0700244 FlushInfo flushInfo;
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400245 flushInfo.fPipeline =
246 target->makePipeline(fSRGBFlags, std::move(fProcessors), target->detachAppliedClip());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500247 SkDEBUGCODE(bool dfPerspective = false);
joshualittd9d30f72015-12-08 10:47:55 -0800248 if (this->usesDistanceFields()) {
Robert Phillips5c56af12018-02-28 16:37:34 +0000249 flushInfo.fGeometryProcessor = this->setupDfProcessor();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500250 SkDEBUGCODE(dfPerspective = fGeoData[0].fViewMatrix.hasPerspective());
joshualitta751c972015-11-20 13:37:32 -0800251 } else {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400252 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Robert Phillips4bc70112018-03-01 10:24:02 -0500253 this->color(), proxies, atlasPageCount, GrSamplerState::ClampNearest(), maskFormat,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400254 localMatrix, this->usesLocalCoords());
joshualitta751c972015-11-20 13:37:32 -0800255 }
256
joshualitta751c972015-11-20 13:37:32 -0800257 flushInfo.fGlyphsToFlush = 0;
bsalomon342bfc22016-04-01 06:06:20 -0700258 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500259 SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat, dfPerspective));
joshualitta751c972015-11-20 13:37:32 -0800260
joshualitta751c972015-11-20 13:37:32 -0800261 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700262 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800263
Brian Salomon344ec422016-12-15 10:58:41 -0500264 void* vertices = target->makeVertexSpace(
265 vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800266 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400267 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
joshualitta751c972015-11-20 13:37:32 -0800268 if (!vertices || !flushInfo.fVertexBuffer) {
269 SkDebugf("Could not allocate vertices\n");
270 return;
271 }
272
Brian Salomon18923f92017-11-06 16:26:02 -0500273 char* currVertex = reinterpret_cast<char*>(vertices);
joshualitta751c972015-11-20 13:37:32 -0800274
Robert Phillips5c56af12018-02-28 16:37:34 +0000275 SkAutoGlyphCache glyphCache;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400276 // each of these is a SubRun
joshualitta751c972015-11-20 13:37:32 -0800277 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800278 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800279 Blob* blob = args.fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500280 GrAtlasTextBlob::VertexRegenerator regenerator(
Robert Phillips4bc70112018-03-01 10:24:02 -0500281 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
282 args.fColor, target->deferredUploadTarget(), fFontCache, &glyphCache);
Brian Salomon18923f92017-11-06 16:26:02 -0500283 GrAtlasTextBlob::VertexRegenerator::Result result;
284 do {
285 result = regenerator.regenerate();
286 // Copy regenerated vertices from the blob to our vertex buffer.
287 size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
288 if (args.fClipRect.isEmpty()) {
289 memcpy(currVertex, result.fFirstVertex, vertexBytes);
290 } else {
Brian Salomon5c6ac642017-12-19 11:09:32 -0500291 SkASSERT(!dfPerspective);
Brian Salomon18923f92017-11-06 16:26:02 -0500292 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
293 result.fGlyphsRegenerated);
294 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500295 if (this->usesDistanceFields() && !args.fViewMatrix.isIdentity()) {
296 // We always do the distance field view matrix transformation after copying rather
297 // than during blob vertex generation time in the blob as handling successive
298 // arbitrary transformations would be complicated and accumulate error.
299 if (args.fViewMatrix.hasPerspective()) {
300 auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
Brian Salomon7c2192b2018-01-08 09:47:57 -0500301 SkMatrixPriv::MapHomogeneousPointsWithStride(
302 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
303 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500304 } else {
305 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500306 SkMatrixPriv::MapPointsWithStride(
307 args.fViewMatrix, pos, vertexStride,
308 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500309 }
310 }
Brian Salomon18923f92017-11-06 16:26:02 -0500311 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
312 if (!result.fFinished) {
313 this->flush(target, &flushInfo);
314 }
315 currVertex += vertexBytes;
316 } while (!result.fFinished);
joshualitta751c972015-11-20 13:37:32 -0800317 }
joshualitta751c972015-11-20 13:37:32 -0800318 this->flush(target, &flushInfo);
319}
320
Brian Salomone5b399e2017-07-19 13:50:54 -0400321void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400322 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
323 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500324
325 unsigned int numProxies;
Robert Phillips5c56af12018-02-28 16:37:34 +0000326 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(maskFormat, &numProxies);
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500327 if (gp->numTextureSamplers() != (int) numProxies) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400328 // During preparation the number of atlas pages has increased.
329 // Update the proxies used in the GP to match.
330 if (this->usesDistanceFields()) {
331 if (this->isLCD()) {
332 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500333 proxies, numProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400334 } else {
335 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500336 proxies, numProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400337 }
338 } else {
339 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500340 proxies, numProxies, GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400341 }
342 }
343
Chris Dalton3809bab2017-06-13 10:55:06 -0600344 GrMesh mesh(GrPrimitiveType::kTriangles);
cdalton397536c2016-03-25 12:15:03 -0700345 int maxGlyphsPerDraw =
Brian Salomon344ec422016-12-15 10:58:41 -0500346 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Dalton114a3c02017-05-26 15:17:19 -0600347 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph,
Chris Daltonbca46e22017-05-15 11:03:26 -0600348 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
Chris Dalton114a3c02017-05-26 15:17:19 -0600349 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400350 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
joshualitta751c972015-11-20 13:37:32 -0800351 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
352 flushInfo->fGlyphsToFlush = 0;
353}
354
Brian Salomon344ec422016-12-15 10:58:41 -0500355bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
356 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400357 if (fProcessors != that->fProcessors) {
358 return false;
359 }
360
361 if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
joshualitta751c972015-11-20 13:37:32 -0800362 return false;
363 }
364
365 if (fMaskType != that->fMaskType) {
366 return false;
367 }
368
Brian Salomon5c6ac642017-12-19 11:09:32 -0500369 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
370 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
371
372 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
373 return false;
374 }
375
376 if (this->usesDistanceFields()) {
377 if (fDFGPFlags != that->fDFGPFlags) {
joshualitta751c972015-11-20 13:37:32 -0800378 return false;
379 }
380
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400381 if (fLuminanceColor != that->fLuminanceColor) {
joshualitta751c972015-11-20 13:37:32 -0800382 return false;
383 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500384 } else {
385 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
386 return false;
387 }
joshualitta751c972015-11-20 13:37:32 -0800388 }
389
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400390 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
391 // We use the largest possible vertex size for this
392 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
Jim Van Verth56c37142017-10-31 14:44:25 -0400393 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400394 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
395 return false;
396 }
397
Brian Salomon344ec422016-12-15 10:58:41 -0500398 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800399
Jim Van Verth56c37142017-10-31 14:44:25 -0400400 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800401 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800402
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400403 // We reallocate at a rate of 1.5x to try to get better total memory usage
404 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400405 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400406 while (newAllocSize < newGeoCount) {
407 newAllocSize += newAllocSize / 2;
408 }
joshualitta751c972015-11-20 13:37:32 -0800409 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400410 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800411 }
412
Brian Salomon344ec422016-12-15 10:58:41 -0500413 // 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 -0800414 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500415 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800416#ifdef SK_DEBUG
417 for (int i = 0; i < that->fGeoCount; ++i) {
418 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
419 }
420#endif
421 that->fGeoCount = 0;
422 fGeoCount = newGeoCount;
423
bsalomon88cf17d2016-07-08 06:40:56 -0700424 this->joinBounds(*that);
joshualitta751c972015-11-20 13:37:32 -0800425 return true;
426}
427
joshualitta751c972015-11-20 13:37:32 -0800428// TODO trying to figure out why lcd is so whack
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400429// (see comments in GrAtlasTextContext::ComputeCanonicalColor)
Robert Phillips5c56af12018-02-28 16:37:34 +0000430sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor() const {
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500431 unsigned int numProxies;
Robert Phillips4bc70112018-03-01 10:24:02 -0500432 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(this->maskFormat(), &numProxies);
joshualitta751c972015-11-20 13:37:32 -0800433 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500434
435 SkMatrix localMatrix = SkMatrix::I();
436 if (this->usesLocalCoords()) {
437 // If this fails we'll just use I().
438 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
439 (void)result;
440 }
joshualitta751c972015-11-20 13:37:32 -0800441
442 // see if we need to create a new effect
443 if (isLCD) {
brianosman0586f5c2016-04-12 12:48:21 -0700444 float redCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400445 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500446 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700447 float greenCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400448 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500449 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700450 float blueCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400451 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500452 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800453 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500454 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
455 redCorrection, greenCorrection, blueCorrection);
Robert Phillips4bc70112018-03-01 10:24:02 -0500456 return GrDistanceFieldLCDTextGeoProc::Make(proxies, numProxies,
457 GrSamplerState::ClampBilerp(), widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500458 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800459 } else {
joshualitta751c972015-11-20 13:37:32 -0800460#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400461 float correction = 0;
462 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400463 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
464 fLuminanceColor);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400465 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
466 fUseGammaCorrectDistanceTable);
467 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500468 return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies,
469 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500470 correction, fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800471#else
Robert Phillips4bc70112018-03-01 10:24:02 -0500472 return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies,
473 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500474 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800475#endif
476 }
joshualitta751c972015-11-20 13:37:32 -0800477}
joshualittddd22d82016-02-16 06:47:52 -0800478