blob: cfd0c45c702d9e39b1c0ed62e98a1b2f9aecc8d0 [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"
Robert Phillipsc994a932018-06-19 13:09:54 -040011#include "GrContextPriv.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040012#include "GrMemoryPool.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050013#include "GrOpFlushState.h"
joshualitta751c972015-11-20 13:37:32 -080014#include "GrResourceProvider.h"
halcanary4dbbd042016-06-07 17:21:10 -070015#include "SkMathPriv.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050016#include "SkMatrixPriv.h"
17#include "SkPoint3.h"
Herb Derbydce19a72018-04-18 16:02:17 -040018#include "SkStrikeCache.h"
joshualitta751c972015-11-20 13:37:32 -080019#include "effects/GrBitmapTextGeoProc.h"
20#include "effects/GrDistanceFieldGeoProc.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050021#include "text/GrAtlasManager.h"
22#include "text/GrGlyphCache.h"
joshualitta751c972015-11-20 13:37:32 -080023
joshualitt60ce86d2015-11-23 13:08:22 -080024///////////////////////////////////////////////////////////////////////////////////////////////////
25
Robert Phillips7c525e62018-06-12 10:11:12 -040026std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrContext* context,
27 GrPaint&& paint,
28 GrMaskFormat maskFormat,
29 int glyphCount,
30 bool needsTransform) {
Robert Phillipsc994a932018-06-19 13:09:54 -040031 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
32
33 std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
Robert Phillips7c525e62018-06-12 10:11:12 -040034
35 switch (maskFormat) {
36 case kA8_GrMaskFormat:
37 op->fMaskType = kGrayscaleCoverageMask_MaskType;
38 break;
39 case kA565_GrMaskFormat:
40 op->fMaskType = kLCDCoverageMask_MaskType;
41 break;
42 case kARGB_GrMaskFormat:
43 op->fMaskType = kColorBitmapMask_MaskType;
44 break;
45 }
46 op->fNumGlyphs = glyphCount;
47 op->fGeoCount = 1;
48 op->fLuminanceColor = 0;
49 op->fNeedsGlyphTransform = needsTransform;
50 return op;
51 }
52
53std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
54 GrContext* context,
55 GrPaint&& paint,
56 int glyphCount,
57 const GrDistanceFieldAdjustTable* distanceAdjustTable,
58 bool useGammaCorrectDistanceTable,
59 SkColor luminanceColor,
60 const SkSurfaceProps& props,
61 bool isAntiAliased,
62 bool useLCD) {
Robert Phillipsc994a932018-06-19 13:09:54 -040063 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
64
65 std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint));
Robert Phillips7c525e62018-06-12 10:11:12 -040066
67 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
68 bool isLCD = useLCD && SkPixelGeometryIsH(props.pixelGeometry());
69 op->fMaskType = !isAntiAliased ? kAliasedDistanceField_MaskType
70 : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
71 : kLCDDistanceField_MaskType)
72 : kGrayscaleDistanceField_MaskType;
73 op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
74 op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable;
75 op->fLuminanceColor = luminanceColor;
76 op->fNumGlyphs = glyphCount;
77 op->fGeoCount = 1;
78 return op;
79 }
80
joshualitta751c972015-11-20 13:37:32 -080081static const int kDistanceAdjustLumShift = 5;
82
Brian Salomon5c6ac642017-12-19 11:09:32 -050083void GrAtlasTextOp::init() {
84 const Geometry& geo = fGeoData[0];
Brian Salomon5c6ac642017-12-19 11:09:32 -050085 if (this->usesDistanceFields()) {
86 bool isLCD = this->isLCD();
87
88 const SkMatrix& viewMatrix = geo.fViewMatrix;
89
90 fDFGPFlags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
91 fDFGPFlags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
92 fDFGPFlags |= viewMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
93 fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
94 fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
95 ? kAliased_DistanceFieldEffectFlag
96 : 0;
97
98 if (isLCD) {
99 fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
100 fDFGPFlags |=
101 (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
102 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400103
104 fNeedsGlyphTransform = true;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500105 }
Jim Van Verth70276912018-06-01 13:46:46 -0400106
107 SkRect bounds;
108 geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, geo.fY,
109 fNeedsGlyphTransform);
110 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
111 // we treat this as a set of non-AA rects rendered with a texture.
112 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500113}
114
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500115void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
116 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500117}
118
Brian Salomon344ec422016-12-15 10:58:41 -0500119SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800120 SkString str;
121
122 for (int i = 0; i < fGeoCount; ++i) {
123 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
124 i,
125 fGeoData[i].fColor,
joshualitt8e0ef292016-02-19 14:13:03 -0800126 fGeoData[i].fX,
127 fGeoData[i].fY,
joshualittddd22d82016-02-16 06:47:52 -0800128 fGeoData[i].fBlob->runCount());
joshualitta751c972015-11-20 13:37:32 -0800129 }
130
Brian Salomon44acb5b2017-07-18 19:59:24 -0400131 str += fProcessors.dumpProcessors();
132 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800133 return str;
134}
135
Brian Salomon44acb5b2017-07-18 19:59:24 -0400136GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
137 return FixedFunctionFlags::kNone;
138}
139
140GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps,
Brian Osman532b3f92018-07-11 10:02:07 -0400141 const GrAppliedClip* clip) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400142 GrProcessorAnalysisCoverage coverage;
143 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800144 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400145 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800146 } else {
Brian Osman09068252018-01-03 09:57:29 -0500147 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800148 }
joshualitta751c972015-11-20 13:37:32 -0800149 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800150 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400151 case kAliasedDistanceField_MaskType:
152 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400153 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800154 break;
155 case kLCDCoverageMask_MaskType:
156 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400157 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400158 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800159 break;
160 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400161 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400162 break;
joshualitta751c972015-11-20 13:37:32 -0800163 }
Brian Osman532b3f92018-07-11 10:02:07 -0400164 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400165 fUsesLocalCoords = analysis.usesLocalCoords();
166 fCanCombineOnTouchOrOverlap =
167 !analysis.requiresDstTexture() &&
168 !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps));
169 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitta751c972015-11-20 13:37:32 -0800170}
171
Brian Salomon18923f92017-11-06 16:26:02 -0500172static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400173 size_t vertexStride, int glyphCount) {
174 for (int i = 0; i < glyphCount; ++i) {
Brian Salomon18923f92017-11-06 16:26:02 -0500175 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
176 const SkPoint* blobPositionRB =
177 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400178
Jim Van Verth328a33f2017-10-20 12:14:22 -0400179 // positions for bitmap glyphs are pixel boundary aligned
Brian Osman7ca90d22017-11-10 15:31:27 -0500180 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
181 SkScalarRoundToInt(blobPositionLT->fY),
182 SkScalarRoundToInt(blobPositionRB->fX),
183 SkScalarRoundToInt(blobPositionRB->fY));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400184 if (clipRect.contains(positionRect)) {
185 memcpy(currVertex, blobVertices, 4 * vertexStride);
186 currVertex += 4 * vertexStride;
187 } else {
188 // Pull out some more data that we'll need.
189 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
190 // and it avoids a lot of conditionals.
Brian Salomon18923f92017-11-06 16:26:02 -0500191 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
Jim Van Verth328a33f2017-10-20 12:14:22 -0400192 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
Brian Salomon18923f92017-11-06 16:26:02 -0500193 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
194 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
195 coordOffset);
Jim Van Verth328a33f2017-10-20 12:14:22 -0400196 // Pull out the texel coordinates and texture index bits
197 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
198 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
199 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
200 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
201 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
202 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400203
Jim Van Verth328a33f2017-10-20 12:14:22 -0400204 int positionRectWidth = positionRect.width();
205 int positionRectHeight = positionRect.height();
206 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
207 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400208
209 // Clip position and texCoords to the clipRect
Jim Van Verth328a33f2017-10-20 12:14:22 -0400210 unsigned int delta;
211 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
212 coordsRectL += delta;
213 positionRect.fLeft += delta;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400214
Jim Van Verth328a33f2017-10-20 12:14:22 -0400215 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
216 coordsRectT += delta;
217 positionRect.fTop += delta;
218
219 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
220 coordsRectR -= delta;
221 positionRect.fRight -= delta;
222
223 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
224 coordsRectB -= delta;
225 positionRect.fBottom -= delta;
226
227 // Repack texel coordinates and index
228 coordsRectL = coordsRectL << 1 | pageIndexX;
229 coordsRectT = coordsRectT << 1 | pageIndexY;
230 coordsRectR = coordsRectR << 1 | pageIndexX;
231 coordsRectB = coordsRectB << 1 | pageIndexY;
232
233 // Set new positions and coords
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400234 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
235 currPosition->fX = positionRect.fLeft;
236 currPosition->fY = positionRect.fTop;
237 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400238 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
239 currCoords[0] = coordsRectL;
240 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400241 currVertex += vertexStride;
242
243 currPosition = reinterpret_cast<SkPoint*>(currVertex);
244 currPosition->fX = positionRect.fLeft;
245 currPosition->fY = positionRect.fBottom;
246 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400247 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
248 currCoords[0] = coordsRectL;
249 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400250 currVertex += vertexStride;
251
252 currPosition = reinterpret_cast<SkPoint*>(currVertex);
253 currPosition->fX = positionRect.fRight;
254 currPosition->fY = positionRect.fTop;
255 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400256 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
257 currCoords[0] = coordsRectR;
258 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400259 currVertex += vertexStride;
260
261 currPosition = reinterpret_cast<SkPoint*>(currVertex);
262 currPosition->fX = positionRect.fRight;
263 currPosition->fY = positionRect.fBottom;
264 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400265 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
266 currCoords[0] = coordsRectR;
267 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400268 currVertex += vertexStride;
269 }
270
271 blobVertices += 4 * vertexStride;
272 }
273}
274
Brian Salomon91326c32017-08-09 16:02:19 -0400275void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500276 auto resourceProvider = target->resourceProvider();
277
joshualitta751c972015-11-20 13:37:32 -0800278 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
279 // TODO actually only invert if we don't have RGBA
280 SkMatrix localMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500281 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800282 return;
283 }
284
Robert Phillips5a66efb2018-03-07 15:13:18 -0500285 GrAtlasManager* atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500286 GrGlyphCache* glyphCache = target->glyphCache();
287
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400288 GrMaskFormat maskFormat = this->maskFormat();
289
Jim Van Verthcbeae032018-05-16 14:54:41 -0400290 unsigned int numActiveProxies;
291 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
292 if (!proxies) {
joshualitta751c972015-11-20 13:37:32 -0800293 SkDebugf("Could not allocate backing texture for atlas\n");
294 return;
295 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400296 SkASSERT(proxies[0]);
joshualitta751c972015-11-20 13:37:32 -0800297
Brian Osman9aa30c62018-07-02 15:21:46 -0400298 static const uint32_t kPipelineFlags = 0;
299 auto pipe = target->makePipeline(kPipelineFlags, std::move(fProcessors),
300 target->detachAppliedClip());
Brian Salomon49348902018-06-26 09:12:38 -0400301
302 FlushInfo flushInfo;
303 flushInfo.fPipeline = pipe.fPipeline;
304 flushInfo.fFixedDynamicState = pipe.fFixedDynamicState;
305
Jim Van Verthb515ae72018-05-23 16:44:55 -0400306 bool vmPerspective = fGeoData[0].fViewMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800307 if (this->usesDistanceFields()) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400308 flushInfo.fGeometryProcessor = this->setupDfProcessor(proxies, numActiveProxies);
joshualitta751c972015-11-20 13:37:32 -0800309 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400310 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
311 : GrSamplerState::ClampNearest();
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400312 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400313 this->color(), proxies, numActiveProxies, samplerState, maskFormat,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400314 localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800315 }
316
joshualitta751c972015-11-20 13:37:32 -0800317 flushInfo.fGlyphsToFlush = 0;
Brian Salomon92be2f72018-06-19 14:33:47 -0400318 size_t vertexStride = GrTextBlob::GetVertexStride(maskFormat, vmPerspective);
319 SkASSERT(vertexStride == flushInfo.fGeometryProcessor->debugOnly_vertexStride());
joshualitta751c972015-11-20 13:37:32 -0800320
joshualitta751c972015-11-20 13:37:32 -0800321 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700322 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800323
Brian Salomon344ec422016-12-15 10:58:41 -0500324 void* vertices = target->makeVertexSpace(
325 vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800326 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400327 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
joshualitta751c972015-11-20 13:37:32 -0800328 if (!vertices || !flushInfo.fVertexBuffer) {
329 SkDebugf("Could not allocate vertices\n");
330 return;
331 }
332
Brian Salomon18923f92017-11-06 16:26:02 -0500333 char* currVertex = reinterpret_cast<char*>(vertices);
joshualitta751c972015-11-20 13:37:32 -0800334
Herb Derby7956b592018-03-22 16:10:30 -0400335 SkExclusiveStrikePtr autoGlyphCache;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400336 // each of these is a SubRun
joshualitta751c972015-11-20 13:37:32 -0800337 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800338 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800339 Blob* blob = args.fBlob;
Herb Derby86240592018-05-24 16:12:31 -0400340 GrTextBlob::VertexRegenerator regenerator(
Robert Phillips4bc70112018-03-01 10:24:02 -0500341 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500342 args.fColor, target->deferredUploadTarget(), glyphCache, atlasManager,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500343 &autoGlyphCache);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500344 bool done = false;
345 while (!done) {
Herb Derby86240592018-05-24 16:12:31 -0400346 GrTextBlob::VertexRegenerator::Result result;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500347 if (!regenerator.regenerate(&result)) {
348 break;
349 }
350 done = result.fFinished;
351
Brian Salomon18923f92017-11-06 16:26:02 -0500352 // Copy regenerated vertices from the blob to our vertex buffer.
353 size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
354 if (args.fClipRect.isEmpty()) {
355 memcpy(currVertex, result.fFirstVertex, vertexBytes);
356 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400357 SkASSERT(!vmPerspective);
Brian Salomon18923f92017-11-06 16:26:02 -0500358 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
359 result.fGlyphsRegenerated);
360 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400361 if (fNeedsGlyphTransform && !args.fViewMatrix.isIdentity()) {
Brian Salomon5c6ac642017-12-19 11:09:32 -0500362 // We always do the distance field view matrix transformation after copying rather
363 // than during blob vertex generation time in the blob as handling successive
364 // arbitrary transformations would be complicated and accumulate error.
365 if (args.fViewMatrix.hasPerspective()) {
366 auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
Brian Salomon7c2192b2018-01-08 09:47:57 -0500367 SkMatrixPriv::MapHomogeneousPointsWithStride(
368 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
369 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500370 } else {
371 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500372 SkMatrixPriv::MapPointsWithStride(
373 args.fViewMatrix, pos, vertexStride,
374 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500375 }
376 }
Brian Salomon18923f92017-11-06 16:26:02 -0500377 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
378 if (!result.fFinished) {
379 this->flush(target, &flushInfo);
380 }
381 currVertex += vertexBytes;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500382 }
joshualitta751c972015-11-20 13:37:32 -0800383 }
joshualitta751c972015-11-20 13:37:32 -0800384 this->flush(target, &flushInfo);
385}
386
Brian Salomone5b399e2017-07-19 13:50:54 -0400387void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500388 if (!flushInfo->fGlyphsToFlush) {
389 return;
390 }
391
Robert Phillips5a66efb2018-03-07 15:13:18 -0500392 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500393
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400394 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
395 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500396
Jim Van Verthcbeae032018-05-16 14:54:41 -0400397 unsigned int numActiveProxies;
398 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
399 SkASSERT(proxies);
400 if (gp->numTextureSamplers() != (int) numActiveProxies) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400401 // During preparation the number of atlas pages has increased.
402 // Update the proxies used in the GP to match.
403 if (this->usesDistanceFields()) {
404 if (this->isLCD()) {
405 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400406 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400407 } else {
408 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400409 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400410 }
411 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400412 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
413 : GrSamplerState::ClampNearest();
Jim Van Verthcbeae032018-05-16 14:54:41 -0400414 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
Jim Van Verthcf838c72018-03-05 14:40:36 -0500415 samplerState);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400416 }
417 }
418
Chris Dalton3809bab2017-06-13 10:55:06 -0600419 GrMesh mesh(GrPrimitiveType::kTriangles);
cdalton397536c2016-03-25 12:15:03 -0700420 int maxGlyphsPerDraw =
Brian Salomon344ec422016-12-15 10:58:41 -0500421 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Dalton114a3c02017-05-26 15:17:19 -0600422 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph,
Chris Daltonbca46e22017-05-15 11:03:26 -0600423 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
Chris Dalton114a3c02017-05-26 15:17:19 -0600424 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomon49348902018-06-26 09:12:38 -0400425 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline,
426 flushInfo->fFixedDynamicState, mesh);
joshualitta751c972015-11-20 13:37:32 -0800427 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
428 flushInfo->fGlyphsToFlush = 0;
429}
430
Brian Salomon344ec422016-12-15 10:58:41 -0500431bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
432 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400433 if (fProcessors != that->fProcessors) {
434 return false;
435 }
436
437 if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
joshualitta751c972015-11-20 13:37:32 -0800438 return false;
439 }
440
441 if (fMaskType != that->fMaskType) {
442 return false;
443 }
444
Brian Salomon5c6ac642017-12-19 11:09:32 -0500445 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
446 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
447
448 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
449 return false;
450 }
451
Jim Van Verthb515ae72018-05-23 16:44:55 -0400452 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
453 return false;
454 }
455
456 if (fNeedsGlyphTransform &&
457 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
458 return false;
459 }
460
Brian Salomon5c6ac642017-12-19 11:09:32 -0500461 if (this->usesDistanceFields()) {
462 if (fDFGPFlags != that->fDFGPFlags) {
joshualitta751c972015-11-20 13:37:32 -0800463 return false;
464 }
465
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400466 if (fLuminanceColor != that->fLuminanceColor) {
joshualitta751c972015-11-20 13:37:32 -0800467 return false;
468 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500469 } else {
470 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
471 return false;
472 }
Jim Van Verthcf838c72018-03-05 14:40:36 -0500473
joshualitta751c972015-11-20 13:37:32 -0800474 }
475
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400476 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
477 // We use the largest possible vertex size for this
478 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
Jim Van Verth56c37142017-10-31 14:44:25 -0400479 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400480 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
481 return false;
482 }
483
Brian Salomon344ec422016-12-15 10:58:41 -0500484 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800485
Jim Van Verth56c37142017-10-31 14:44:25 -0400486 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800487 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800488
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400489 // We reallocate at a rate of 1.5x to try to get better total memory usage
490 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400491 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400492 while (newAllocSize < newGeoCount) {
493 newAllocSize += newAllocSize / 2;
494 }
joshualitta751c972015-11-20 13:37:32 -0800495 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400496 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800497 }
498
Brian Salomon344ec422016-12-15 10:58:41 -0500499 // 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 -0800500 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500501 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800502#ifdef SK_DEBUG
503 for (int i = 0; i < that->fGeoCount; ++i) {
504 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
505 }
506#endif
507 that->fGeoCount = 0;
508 fGeoCount = newGeoCount;
509
bsalomon88cf17d2016-07-08 06:40:56 -0700510 this->joinBounds(*that);
joshualitta751c972015-11-20 13:37:32 -0800511 return true;
512}
513
joshualitta751c972015-11-20 13:37:32 -0800514// TODO trying to figure out why lcd is so whack
Herb Derby26cbe512018-05-24 14:39:01 -0400515// (see comments in GrTextContext::ComputeCanonicalColor)
Jim Van Verthcbeae032018-05-16 14:54:41 -0400516sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const sk_sp<GrTextureProxy>* proxies,
517 unsigned int numActiveProxies) const {
joshualitta751c972015-11-20 13:37:32 -0800518 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500519
520 SkMatrix localMatrix = SkMatrix::I();
521 if (this->usesLocalCoords()) {
522 // If this fails we'll just use I().
523 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
524 (void)result;
525 }
joshualitta751c972015-11-20 13:37:32 -0800526
527 // see if we need to create a new effect
528 if (isLCD) {
brianosman0586f5c2016-04-12 12:48:21 -0700529 float redCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400530 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500531 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700532 float greenCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400533 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500534 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700535 float blueCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400536 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500537 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800538 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500539 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
540 redCorrection, greenCorrection, blueCorrection);
Jim Van Verthcbeae032018-05-16 14:54:41 -0400541 return GrDistanceFieldLCDTextGeoProc::Make(proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500542 GrSamplerState::ClampBilerp(), widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500543 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800544 } else {
joshualitta751c972015-11-20 13:37:32 -0800545#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400546 float correction = 0;
547 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400548 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
549 fLuminanceColor);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400550 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
551 fUseGammaCorrectDistanceTable);
552 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400553 return GrDistanceFieldA8TextGeoProc::Make(proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500554 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500555 correction, fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800556#else
Jim Van Verthcbeae032018-05-16 14:54:41 -0400557 return GrDistanceFieldA8TextGeoProc::Make(proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500558 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500559 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800560#endif
561 }
joshualitta751c972015-11-20 13:37:32 -0800562}
joshualittddd22d82016-02-16 06:47:52 -0800563