1 /*
2 * Copyright 2017 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
8 #include "include/atlastext/SkAtlasTextTarget.h"
9
10 #include "include/atlastext/SkAtlasTextContext.h"
11 #include "include/atlastext/SkAtlasTextFont.h"
12 #include "include/atlastext/SkAtlasTextRenderer.h"
13 #include "src/atlastext/SkInternalAtlasTextContext.h"
14 #include "src/core/SkGlyphRunPainter.h"
15 #include "src/gpu/GrClip.h"
16 #include "src/gpu/GrContextPriv.h"
17 #include "src/gpu/GrDrawingManager.h"
18 #include "src/gpu/GrMemoryPool.h"
19 #include "src/gpu/SkGr.h"
20 #include "src/gpu/ops/GrAtlasTextOp.h"
21 #include "src/gpu/text/GrTextContext.h"
22
23 static constexpr int kMaxBatchLookBack = 10;
24
SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context,int width,int height,void * handle)25 SkAtlasTextTarget::SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height,
26 void* handle)
27 : fHandle(handle)
28 , fContext(std::move(context))
29 , fWidth(width)
30 , fHeight(height)
31 , fMatrixStack(sizeof(SkMatrix), 4)
32 , fSaveCnt(0) {
33 fMatrixStack.push_back();
34 this->accessCTM()->reset();
35 }
36
~SkAtlasTextTarget()37 SkAtlasTextTarget::~SkAtlasTextTarget() { fContext->renderer()->targetDeleted(fHandle); }
38
save()39 int SkAtlasTextTarget::save() {
40 const auto& currCTM = this->ctm();
41 *static_cast<SkMatrix*>(fMatrixStack.push_back()) = currCTM;
42 return fSaveCnt++;
43 }
44
restore()45 void SkAtlasTextTarget::restore() {
46 if (fSaveCnt) {
47 fMatrixStack.pop_back();
48 fSaveCnt--;
49 }
50 }
51
restoreToCount(int count)52 void SkAtlasTextTarget::restoreToCount(int count) {
53 while (fSaveCnt > count) {
54 this->restore();
55 }
56 }
57
translate(SkScalar dx,SkScalar dy)58 void SkAtlasTextTarget::translate(SkScalar dx, SkScalar dy) {
59 this->accessCTM()->preTranslate(dx, dy);
60 }
61
scale(SkScalar sx,SkScalar sy)62 void SkAtlasTextTarget::scale(SkScalar sx, SkScalar sy) { this->accessCTM()->preScale(sx, sy); }
63
rotate(SkScalar degrees)64 void SkAtlasTextTarget::rotate(SkScalar degrees) { this->accessCTM()->preRotate(degrees); }
65
rotate(SkScalar degrees,SkScalar px,SkScalar py)66 void SkAtlasTextTarget::rotate(SkScalar degrees, SkScalar px, SkScalar py) {
67 this->accessCTM()->preRotate(degrees, px, py);
68 }
69
skew(SkScalar sx,SkScalar sy)70 void SkAtlasTextTarget::skew(SkScalar sx, SkScalar sy) { this->accessCTM()->preSkew(sx, sy); }
71
concat(const SkMatrix & matrix)72 void SkAtlasTextTarget::concat(const SkMatrix& matrix) { this->accessCTM()->preConcat(matrix); }
73
74 //////////////////////////////////////////////////////////////////////////////
75
76 static const GrColorSpaceInfo kColorSpaceInfo(GrColorType::kRGBA_8888, kPremul_SkAlphaType,
77 nullptr);
78 static const SkSurfaceProps kProps(
79 SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry);
80
81 //////////////////////////////////////////////////////////////////////////////
82
83 class SkInternalAtlasTextTarget : public GrTextTarget, public SkAtlasTextTarget {
84 public:
SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context,int width,int height,void * handle)85 SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context,
86 int width, int height,
87 void* handle)
88 : GrTextTarget(width, height, kColorSpaceInfo)
89 , SkAtlasTextTarget(std::move(context), width, height, handle)
90 , fGlyphPainter(kProps, kColorSpaceInfo) {
91 fOpMemoryPool = fContext->internal().grContext()->priv().refOpMemoryPool();
92 }
93
~SkInternalAtlasTextTarget()94 ~SkInternalAtlasTextTarget() override {
95 this->deleteOps();
96 }
97
98 /** GrTextTarget overrides */
99
100 void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) override;
101
drawShape(const GrClip &,const SkPaint &,const SkMatrix & viewMatrix,const GrShape &)102 void drawShape(const GrClip&, const SkPaint&, const SkMatrix& viewMatrix,
103 const GrShape&) override {
104 SkDebugf("Path glyph??");
105 }
106
makeGrPaint(GrMaskFormat,const SkPaint & skPaint,const SkMatrix &,GrPaint * grPaint)107 void makeGrPaint(GrMaskFormat, const SkPaint& skPaint, const SkMatrix&,
108 GrPaint* grPaint) override {
109 grPaint->setColor4f(skPaint.getColor4f().premul());
110 }
111
getContext()112 GrContext* getContext() override {
113 return this->context()->internal().grContext();
114 }
115
glyphPainter()116 SkGlyphRunListPainter* glyphPainter() override {
117 return &fGlyphPainter;
118 }
119
120 /** SkAtlasTextTarget overrides */
121
122 void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
123 const SkAtlasTextFont&) override;
124 void flush() override;
125
126 private:
127 void deleteOps();
128
129 uint32_t fColor;
130 using SkAtlasTextTarget::fWidth;
131 using SkAtlasTextTarget::fHeight;
132 SkTArray<std::unique_ptr<GrAtlasTextOp>, true> fOps;
133 sk_sp<GrOpMemoryPool> fOpMemoryPool;
134 SkGlyphRunListPainter fGlyphPainter;
135 };
136
137 //////////////////////////////////////////////////////////////////////////////
138
Make(sk_sp<SkAtlasTextContext> context,int width,int height,void * handle)139 std::unique_ptr<SkAtlasTextTarget> SkAtlasTextTarget::Make(sk_sp<SkAtlasTextContext> context,
140 int width, int height, void* handle) {
141 return std::unique_ptr<SkAtlasTextTarget>(
142 new SkInternalAtlasTextTarget(std::move(context), width, height, handle));
143 }
144
145 //////////////////////////////////////////////////////////////////////////////
146
drawText(const SkGlyphID glyphs[],const SkPoint positions[],int glyphCnt,uint32_t color,const SkAtlasTextFont & font)147 void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[],
148 int glyphCnt, uint32_t color,
149 const SkAtlasTextFont& font) {
150 SkPaint paint;
151 paint.setAntiAlias(true);
152
153 // The atlas text context does munging of the paint color. We store the client's color here
154 // and then overwrite the generated op's color when addDrawOp() is called.
155 fColor = color;
156
157 SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry);
158 auto grContext = this->context()->internal().grContext();
159 auto atlasTextContext = grContext->priv().drawingManager()->getTextContext();
160 SkGlyphRunBuilder builder;
161 builder.drawGlyphsWithPositions(paint, font.makeFont(),
162 SkSpan<const SkGlyphID>{glyphs, SkTo<size_t>(glyphCnt)},
163 positions);
164 auto glyphRunList = builder.useGlyphRunList();
165 if (!glyphRunList.empty()) {
166 atlasTextContext->drawGlyphRunList(grContext, this, GrNoClip(), this->ctm(), props,
167 glyphRunList);
168 }
169 }
170
addDrawOp(const GrClip & clip,std::unique_ptr<GrAtlasTextOp> op)171 void SkInternalAtlasTextTarget::addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) {
172 SkASSERT(clip.quickContains(SkRect::MakeIWH(fWidth, fHeight)));
173 // The SkAtlasTextRenderer currently only handles grayscale SDF glyphs.
174 if (op->maskType() != GrAtlasTextOp::kGrayscaleDistanceField_MaskType) {
175 return;
176 }
177 const GrCaps& caps = *this->context()->internal().grContext()->priv().caps();
178 op->finalizeForTextTarget(fColor, caps);
179 int n = SkTMin(kMaxBatchLookBack, fOps.count());
180 for (int i = 0; i < n; ++i) {
181 GrAtlasTextOp* other = fOps.fromBack(i).get();
182 if (other->combineIfPossible(op.get(), caps) == GrOp::CombineResult::kMerged) {
183 fOpMemoryPool->release(std::move(op));
184 return;
185 }
186 if (GrRectsOverlap(op->bounds(), other->bounds())) {
187 break;
188 }
189 }
190 fOps.emplace_back(std::move(op));
191 }
192
deleteOps()193 void SkInternalAtlasTextTarget::deleteOps() {
194 for (int i = 0; i < fOps.count(); ++i) {
195 if (fOps[i]) {
196 fOpMemoryPool->release(std::move(fOps[i]));
197 }
198 }
199 fOps.reset();
200 }
201
flush()202 void SkInternalAtlasTextTarget::flush() {
203 for (int i = 0; i < fOps.count(); ++i) {
204 fOps[i]->executeForTextTarget(this);
205 }
206 this->context()->internal().flush();
207 this->deleteOps();
208 }
209
finalizeForTextTarget(uint32_t color,const GrCaps & caps)210 void GrAtlasTextOp::finalizeForTextTarget(uint32_t color, const GrCaps& caps) {
211 // TODO4F: Odd handling of client colors among AtlasTextTarget and AtlasTextRenderer
212 SkPMColor4f color4f = SkPMColor4f::FromBytes_RGBA(color);
213 for (int i = 0; i < fGeoCount; ++i) {
214 fGeoData[i].fColor = color4f;
215 }
216 // Atlas text doesn't use MSAA, so no need to handle mixed samples.
217 // Also, no need to support normalized F16 with manual clamp?
218 this->finalize(caps, nullptr /* applied clip */, false /* mixed samples */, GrClampType::kAuto);
219 }
220
executeForTextTarget(SkAtlasTextTarget * target)221 void GrAtlasTextOp::executeForTextTarget(SkAtlasTextTarget* target) {
222 FlushInfo flushInfo;
223 SkExclusiveStrikePtr autoGlyphCache;
224 auto& context = target->context()->internal();
225 auto glyphCache = context.grContext()->priv().getGrStrikeCache();
226 auto atlasManager = context.grContext()->priv().getAtlasManager();
227 auto resourceProvider = context.grContext()->priv().resourceProvider();
228
229 unsigned int numProxies;
230 if (!atlasManager->getProxies(kA8_GrMaskFormat, &numProxies)) {
231 return;
232 }
233
234 for (int i = 0; i < fGeoCount; ++i) {
235 // TODO4F: Preserve float colors
236 GrTextBlob::VertexRegenerator regenerator(
237 resourceProvider, fGeoData[i].fBlob, fGeoData[i].fRun, fGeoData[i].fSubRun,
238 fGeoData[i].fViewMatrix, fGeoData[i].fX, fGeoData[i].fY,
239 fGeoData[i].fColor.toBytes_RGBA(), &context, glyphCache, atlasManager,
240 &autoGlyphCache);
241 bool done = false;
242 while (!done) {
243 GrTextBlob::VertexRegenerator::Result result;
244 if (!regenerator.regenerate(&result)) {
245 break;
246 }
247 done = result.fFinished;
248
249 context.recordDraw(result.fFirstVertex, result.fGlyphsRegenerated,
250 fGeoData[i].fViewMatrix, target->handle());
251 if (!result.fFinished) {
252 // Make space in the atlas so we can continue generating vertices.
253 context.flush();
254 }
255 }
256 }
257 }
258