blob: b616c290bc2554c98b3ffc1ad16890abc4d433f0 [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
Brian Osman4a3f5c82018-09-18 16:16:38 -040010#include "GrCaps.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -040011#include "GrContext.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040012#include "GrContextPriv.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040013#include "GrMemoryPool.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
joshualitta751c972015-11-20 13:37:32 -080015#include "GrResourceProvider.h"
halcanary4dbbd042016-06-07 17:21:10 -070016#include "SkMathPriv.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050017#include "SkMatrixPriv.h"
18#include "SkPoint3.h"
Herb Derbydce19a72018-04-18 16:02:17 -040019#include "SkStrikeCache.h"
joshualitta751c972015-11-20 13:37:32 -080020#include "effects/GrBitmapTextGeoProc.h"
21#include "effects/GrDistanceFieldGeoProc.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050022#include "text/GrAtlasManager.h"
23#include "text/GrGlyphCache.h"
joshualitta751c972015-11-20 13:37:32 -080024
joshualitt60ce86d2015-11-23 13:08:22 -080025///////////////////////////////////////////////////////////////////////////////////////////////////
26
Robert Phillips7c525e62018-06-12 10:11:12 -040027std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrContext* context,
28 GrPaint&& paint,
29 GrMaskFormat maskFormat,
30 int glyphCount,
31 bool needsTransform) {
Robert Phillipsc994a932018-06-19 13:09:54 -040032 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
33
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(
55 GrContext* context,
56 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 Phillipsc994a932018-06-19 13:09:54 -040064 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
65
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;
109 geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, geo.fY,
110 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.
113 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114}
115
Brian Salomon7d94bb52018-10-12 14:37:19 -0400116void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func, VisitorType) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500117 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500118}
119
Brian Salomon344ec422016-12-15 10:58:41 -0500120SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800121 SkString str;
122
123 for (int i = 0; i < fGeoCount; ++i) {
124 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
125 i,
126 fGeoData[i].fColor,
joshualitt8e0ef292016-02-19 14:13:03 -0800127 fGeoData[i].fX,
128 fGeoData[i].fY,
joshualittddd22d82016-02-16 06:47:52 -0800129 fGeoData[i].fBlob->runCount());
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}
136
Brian Salomon44acb5b2017-07-18 19:59:24 -0400137GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
138 return FixedFunctionFlags::kNone;
139}
140
141GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps,
Brian Osman532b3f92018-07-11 10:02:07 -0400142 const GrAppliedClip* clip) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400143 GrProcessorAnalysisCoverage coverage;
144 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800145 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400146 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800147 } else {
Brian Osman09068252018-01-03 09:57:29 -0500148 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800149 }
joshualitta751c972015-11-20 13:37:32 -0800150 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800151 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400152 case kAliasedDistanceField_MaskType:
153 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400154 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800155 break;
156 case kLCDCoverageMask_MaskType:
157 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400158 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400159 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800160 break;
161 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400162 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400163 break;
joshualitta751c972015-11-20 13:37:32 -0800164 }
Brian Osman532b3f92018-07-11 10:02:07 -0400165 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400166 fUsesLocalCoords = analysis.usesLocalCoords();
167 fCanCombineOnTouchOrOverlap =
168 !analysis.requiresDstTexture() &&
169 !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps));
170 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitta751c972015-11-20 13:37:32 -0800171}
172
Brian Salomon18923f92017-11-06 16:26:02 -0500173static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400174 size_t vertexStride, int glyphCount) {
175 for (int i = 0; i < glyphCount; ++i) {
Brian Salomon18923f92017-11-06 16:26:02 -0500176 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
177 const SkPoint* blobPositionRB =
178 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400179
Jim Van Verth328a33f2017-10-20 12:14:22 -0400180 // positions for bitmap glyphs are pixel boundary aligned
Brian Osman7ca90d22017-11-10 15:31:27 -0500181 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
182 SkScalarRoundToInt(blobPositionLT->fY),
183 SkScalarRoundToInt(blobPositionRB->fX),
184 SkScalarRoundToInt(blobPositionRB->fY));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400185 if (clipRect.contains(positionRect)) {
186 memcpy(currVertex, blobVertices, 4 * vertexStride);
187 currVertex += 4 * vertexStride;
188 } else {
189 // Pull out some more data that we'll need.
190 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
191 // and it avoids a lot of conditionals.
Brian Salomon18923f92017-11-06 16:26:02 -0500192 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
Jim Van Verth328a33f2017-10-20 12:14:22 -0400193 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
Brian Salomon18923f92017-11-06 16:26:02 -0500194 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
195 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
196 coordOffset);
Jim Van Verth328a33f2017-10-20 12:14:22 -0400197 // Pull out the texel coordinates and texture index bits
198 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
199 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
200 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
201 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
202 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
203 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400204
Jim Van Verth328a33f2017-10-20 12:14:22 -0400205 int positionRectWidth = positionRect.width();
206 int positionRectHeight = positionRect.height();
207 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
208 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400209
210 // Clip position and texCoords to the clipRect
Jim Van Verth328a33f2017-10-20 12:14:22 -0400211 unsigned int delta;
212 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
213 coordsRectL += delta;
214 positionRect.fLeft += delta;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400215
Jim Van Verth328a33f2017-10-20 12:14:22 -0400216 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
217 coordsRectT += delta;
218 positionRect.fTop += delta;
219
220 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
221 coordsRectR -= delta;
222 positionRect.fRight -= delta;
223
224 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
225 coordsRectB -= delta;
226 positionRect.fBottom -= delta;
227
228 // Repack texel coordinates and index
229 coordsRectL = coordsRectL << 1 | pageIndexX;
230 coordsRectT = coordsRectT << 1 | pageIndexY;
231 coordsRectR = coordsRectR << 1 | pageIndexX;
232 coordsRectB = coordsRectB << 1 | pageIndexY;
233
234 // Set new positions and coords
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400235 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
236 currPosition->fX = positionRect.fLeft;
237 currPosition->fY = positionRect.fTop;
238 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400239 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
240 currCoords[0] = coordsRectL;
241 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400242 currVertex += vertexStride;
243
244 currPosition = reinterpret_cast<SkPoint*>(currVertex);
245 currPosition->fX = positionRect.fLeft;
246 currPosition->fY = positionRect.fBottom;
247 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400248 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
249 currCoords[0] = coordsRectL;
250 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400251 currVertex += vertexStride;
252
253 currPosition = reinterpret_cast<SkPoint*>(currVertex);
254 currPosition->fX = positionRect.fRight;
255 currPosition->fY = positionRect.fTop;
256 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400257 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
258 currCoords[0] = coordsRectR;
259 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400260 currVertex += vertexStride;
261
262 currPosition = reinterpret_cast<SkPoint*>(currVertex);
263 currPosition->fX = positionRect.fRight;
264 currPosition->fY = positionRect.fBottom;
265 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400266 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
267 currCoords[0] = coordsRectR;
268 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400269 currVertex += vertexStride;
270 }
271
272 blobVertices += 4 * vertexStride;
273 }
274}
275
Brian Salomon91326c32017-08-09 16:02:19 -0400276void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500277 auto resourceProvider = target->resourceProvider();
278
joshualitta751c972015-11-20 13:37:32 -0800279 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
280 // TODO actually only invert if we don't have RGBA
281 SkMatrix localMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500282 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800283 return;
284 }
285
Robert Phillips5a66efb2018-03-07 15:13:18 -0500286 GrAtlasManager* atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500287 GrGlyphCache* glyphCache = target->glyphCache();
288
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400289 GrMaskFormat maskFormat = this->maskFormat();
290
Jim Van Verthcbeae032018-05-16 14:54:41 -0400291 unsigned int numActiveProxies;
292 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
293 if (!proxies) {
joshualitta751c972015-11-20 13:37:32 -0800294 SkDebugf("Could not allocate backing texture for atlas\n");
295 return;
296 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400297 SkASSERT(proxies[0]);
joshualitta751c972015-11-20 13:37:32 -0800298
Brian Salomon7eae3e02018-08-07 14:02:38 +0000299 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
300 GR_STATIC_ASSERT(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
301 GR_STATIC_ASSERT(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
302
Brian Osman9aa30c62018-07-02 15:21:46 -0400303 static const uint32_t kPipelineFlags = 0;
304 auto pipe = target->makePipeline(kPipelineFlags, std::move(fProcessors),
Brian Salomon7eae3e02018-08-07 14:02:38 +0000305 target->detachAppliedClip(), kMaxTextures);
306 for (unsigned i = 0; i < numActiveProxies; ++i) {
307 pipe.fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
308 }
Brian Salomon49348902018-06-26 09:12:38 -0400309
310 FlushInfo flushInfo;
311 flushInfo.fPipeline = pipe.fPipeline;
312 flushInfo.fFixedDynamicState = pipe.fFixedDynamicState;
313
Jim Van Verthb515ae72018-05-23 16:44:55 -0400314 bool vmPerspective = fGeoData[0].fViewMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800315 if (this->usesDistanceFields()) {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400316 flushInfo.fGeometryProcessor = this->setupDfProcessor(*target->caps().shaderCaps(),
317 proxies, numActiveProxies);
joshualitta751c972015-11-20 13:37:32 -0800318 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400319 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
320 : GrSamplerState::ClampNearest();
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400321 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Brian Osman4a3f5c82018-09-18 16:16:38 -0400322 *target->caps().shaderCaps(), this->color(), proxies, numActiveProxies, samplerState,
323 maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800324 }
325
joshualitta751c972015-11-20 13:37:32 -0800326 flushInfo.fGlyphsToFlush = 0;
Brian Salomon92be2f72018-06-19 14:33:47 -0400327 size_t vertexStride = GrTextBlob::GetVertexStride(maskFormat, vmPerspective);
328 SkASSERT(vertexStride == flushInfo.fGeometryProcessor->debugOnly_vertexStride());
joshualitta751c972015-11-20 13:37:32 -0800329
joshualitta751c972015-11-20 13:37:32 -0800330 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700331 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800332
Brian Salomon344ec422016-12-15 10:58:41 -0500333 void* vertices = target->makeVertexSpace(
334 vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800335 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400336 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
joshualitta751c972015-11-20 13:37:32 -0800337 if (!vertices || !flushInfo.fVertexBuffer) {
338 SkDebugf("Could not allocate vertices\n");
339 return;
340 }
341
Brian Salomon18923f92017-11-06 16:26:02 -0500342 char* currVertex = reinterpret_cast<char*>(vertices);
joshualitta751c972015-11-20 13:37:32 -0800343
Herb Derby7956b592018-03-22 16:10:30 -0400344 SkExclusiveStrikePtr autoGlyphCache;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400345 // each of these is a SubRun
joshualitta751c972015-11-20 13:37:32 -0800346 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800347 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800348 Blob* blob = args.fBlob;
Herb Derby86240592018-05-24 16:12:31 -0400349 GrTextBlob::VertexRegenerator regenerator(
Robert Phillips4bc70112018-03-01 10:24:02 -0500350 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500351 args.fColor, target->deferredUploadTarget(), glyphCache, atlasManager,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500352 &autoGlyphCache);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500353 bool done = false;
354 while (!done) {
Herb Derby86240592018-05-24 16:12:31 -0400355 GrTextBlob::VertexRegenerator::Result result;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500356 if (!regenerator.regenerate(&result)) {
357 break;
358 }
359 done = result.fFinished;
360
Brian Salomon18923f92017-11-06 16:26:02 -0500361 // Copy regenerated vertices from the blob to our vertex buffer.
362 size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
363 if (args.fClipRect.isEmpty()) {
364 memcpy(currVertex, result.fFirstVertex, vertexBytes);
365 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400366 SkASSERT(!vmPerspective);
Brian Salomon18923f92017-11-06 16:26:02 -0500367 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
368 result.fGlyphsRegenerated);
369 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400370 if (fNeedsGlyphTransform && !args.fViewMatrix.isIdentity()) {
Brian Salomon5c6ac642017-12-19 11:09:32 -0500371 // We always do the distance field view matrix transformation after copying rather
372 // than during blob vertex generation time in the blob as handling successive
373 // arbitrary transformations would be complicated and accumulate error.
374 if (args.fViewMatrix.hasPerspective()) {
375 auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
Brian Salomon7c2192b2018-01-08 09:47:57 -0500376 SkMatrixPriv::MapHomogeneousPointsWithStride(
377 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
378 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500379 } else {
380 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500381 SkMatrixPriv::MapPointsWithStride(
382 args.fViewMatrix, pos, vertexStride,
383 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500384 }
385 }
Brian Salomon18923f92017-11-06 16:26:02 -0500386 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
387 if (!result.fFinished) {
388 this->flush(target, &flushInfo);
389 }
390 currVertex += vertexBytes;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500391 }
joshualitta751c972015-11-20 13:37:32 -0800392 }
joshualitta751c972015-11-20 13:37:32 -0800393 this->flush(target, &flushInfo);
394}
395
Brian Salomone5b399e2017-07-19 13:50:54 -0400396void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500397 if (!flushInfo->fGlyphsToFlush) {
398 return;
399 }
400
Robert Phillips5a66efb2018-03-07 15:13:18 -0500401 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500402
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400403 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
404 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500405
Jim Van Verthcbeae032018-05-16 14:54:41 -0400406 unsigned int numActiveProxies;
407 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(maskFormat, &numActiveProxies);
408 SkASSERT(proxies);
409 if (gp->numTextureSamplers() != (int) numActiveProxies) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400410 // During preparation the number of atlas pages has increased.
411 // Update the proxies used in the GP to match.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000412 for (unsigned i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
413 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
414 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400415 if (this->usesDistanceFields()) {
416 if (this->isLCD()) {
417 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400418 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400419 } else {
420 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400421 proxies, numActiveProxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400422 }
423 } else {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400424 GrSamplerState samplerState = fNeedsGlyphTransform ? GrSamplerState::ClampBilerp()
425 : GrSamplerState::ClampNearest();
Jim Van Verthcbeae032018-05-16 14:54:41 -0400426 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(proxies, numActiveProxies,
Jim Van Verthcf838c72018-03-05 14:40:36 -0500427 samplerState);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400428 }
429 }
cdalton397536c2016-03-25 12:15:03 -0700430 int maxGlyphsPerDraw =
Brian Salomon344ec422016-12-15 10:58:41 -0500431 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000432 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
433 mesh->setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph,
434 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
435 mesh->setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
436 target->draw(flushInfo->fGeometryProcessor, flushInfo->fPipeline, flushInfo->fFixedDynamicState,
437 mesh);
joshualitta751c972015-11-20 13:37:32 -0800438 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
439 flushInfo->fGlyphsToFlush = 0;
440}
441
Brian Salomon7eae3e02018-08-07 14:02:38 +0000442GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500443 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400444 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000445 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400446 }
447
448 if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000449 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800450 }
451
452 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000453 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800454 }
455
Brian Salomon5c6ac642017-12-19 11:09:32 -0500456 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
457 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
458
459 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000460 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500461 }
462
Jim Van Verthb515ae72018-05-23 16:44:55 -0400463 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000464 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400465 }
466
467 if (fNeedsGlyphTransform &&
468 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000469 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400470 }
471
Brian Salomon5c6ac642017-12-19 11:09:32 -0500472 if (this->usesDistanceFields()) {
473 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000474 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800475 }
476
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400477 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000478 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800479 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500480 } else {
481 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000482 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500483 }
joshualitta751c972015-11-20 13:37:32 -0800484 }
485
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400486 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
487 // We use the largest possible vertex size for this
488 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
Jim Van Verth56c37142017-10-31 14:44:25 -0400489 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400490 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000491 return CombineResult::kCannotCombine;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400492 }
493
Brian Salomon344ec422016-12-15 10:58:41 -0500494 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800495
Jim Van Verth56c37142017-10-31 14:44:25 -0400496 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800497 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800498
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400499 // We reallocate at a rate of 1.5x to try to get better total memory usage
500 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400501 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400502 while (newAllocSize < newGeoCount) {
503 newAllocSize += newAllocSize / 2;
504 }
joshualitta751c972015-11-20 13:37:32 -0800505 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400506 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800507 }
508
Brian Salomon344ec422016-12-15 10:58:41 -0500509 // 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 -0800510 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500511 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800512#ifdef SK_DEBUG
513 for (int i = 0; i < that->fGeoCount; ++i) {
514 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
515 }
516#endif
517 that->fGeoCount = 0;
518 fGeoCount = newGeoCount;
519
bsalomon88cf17d2016-07-08 06:40:56 -0700520 this->joinBounds(*that);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000521 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800522}
523
joshualitta751c972015-11-20 13:37:32 -0800524// TODO trying to figure out why lcd is so whack
Herb Derby26cbe512018-05-24 14:39:01 -0400525// (see comments in GrTextContext::ComputeCanonicalColor)
Brian Osman4a3f5c82018-09-18 16:16:38 -0400526sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const GrShaderCaps& caps,
527 const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400528 unsigned int numActiveProxies) const {
joshualitta751c972015-11-20 13:37:32 -0800529 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500530
531 SkMatrix localMatrix = SkMatrix::I();
532 if (this->usesLocalCoords()) {
533 // If this fails we'll just use I().
534 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
535 (void)result;
536 }
joshualitta751c972015-11-20 13:37:32 -0800537
538 // see if we need to create a new effect
539 if (isLCD) {
brianosman0586f5c2016-04-12 12:48:21 -0700540 float redCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400541 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500542 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700543 float greenCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400544 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500545 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700546 float blueCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400547 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500548 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800549 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500550 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
551 redCorrection, greenCorrection, blueCorrection);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400552 return GrDistanceFieldLCDTextGeoProc::Make(caps, proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500553 GrSamplerState::ClampBilerp(), widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500554 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800555 } else {
joshualitta751c972015-11-20 13:37:32 -0800556#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400557 float correction = 0;
558 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400559 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
560 fLuminanceColor);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400561 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
562 fUseGammaCorrectDistanceTable);
563 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400564 return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500565 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500566 correction, fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800567#else
Brian Osman4a3f5c82018-09-18 16:16:38 -0400568 return GrDistanceFieldA8TextGeoProc::Make(caps, proxies, numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500569 GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500570 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800571#endif
572 }
joshualitta751c972015-11-20 13:37:32 -0800573}
joshualittddd22d82016-02-16 06:47:52 -0800574