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 "SkAtlasTextTarget.h"
9 #include "GrClip.h"
10 #include "GrContextPriv.h"
11 #include "GrDrawingManager.h"
12 #include "SkAtlasTextContext.h"
13 #include "SkAtlasTextFont.h"
14 #include "SkAtlasTextRenderer.h"
15 #include "SkGr.h"
16 #include "SkInternalAtlasTextContext.h"
17 #include "ops/GrAtlasTextOp.h"
18 #include "text/GrAtlasTextContext.h"
19 
20 static constexpr int kMaxBatchLookBack = 10;
21 
SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context,int width,int height,void * handle)22 SkAtlasTextTarget::SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height,
23                                      void* handle)
24         : fHandle(handle)
25         , fContext(std::move(context))
26         , fWidth(width)
27         , fHeight(height)
28         , fMatrixStack(sizeof(SkMatrix), 4)
29         , fSaveCnt(0) {
30     fMatrixStack.push_back();
31     this->accessCTM()->reset();
32 }
33 
~SkAtlasTextTarget()34 SkAtlasTextTarget::~SkAtlasTextTarget() { fContext->renderer()->targetDeleted(fHandle); }
35 
save()36 int SkAtlasTextTarget::save() {
37     const auto& currCTM = this->ctm();
38     *static_cast<SkMatrix*>(fMatrixStack.push_back()) = currCTM;
39     return fSaveCnt++;
40 }
41 
restore()42 void SkAtlasTextTarget::restore() {
43     if (fSaveCnt) {
44         fMatrixStack.pop_back();
45         fSaveCnt--;
46     }
47 }
48 
restoreToCount(int count)49 void SkAtlasTextTarget::restoreToCount(int count) {
50     while (fSaveCnt > count) {
51         this->restore();
52     }
53 }
54 
translate(SkScalar dx,SkScalar dy)55 void SkAtlasTextTarget::translate(SkScalar dx, SkScalar dy) {
56     this->accessCTM()->preTranslate(dx, dy);
57 }
58 
scale(SkScalar sx,SkScalar sy)59 void SkAtlasTextTarget::scale(SkScalar sx, SkScalar sy) { this->accessCTM()->preScale(sx, sy); }
60 
rotate(SkScalar degrees)61 void SkAtlasTextTarget::rotate(SkScalar degrees) { this->accessCTM()->preRotate(degrees); }
62 
rotate(SkScalar degrees,SkScalar px,SkScalar py)63 void SkAtlasTextTarget::rotate(SkScalar degrees, SkScalar px, SkScalar py) {
64     this->accessCTM()->preRotate(degrees, px, py);
65 }
66 
skew(SkScalar sx,SkScalar sy)67 void SkAtlasTextTarget::skew(SkScalar sx, SkScalar sy) { this->accessCTM()->preSkew(sx, sy); }
68 
concat(const SkMatrix & matrix)69 void SkAtlasTextTarget::concat(const SkMatrix& matrix) { this->accessCTM()->preConcat(matrix); }
70 
71 //////////////////////////////////////////////////////////////////////////////
72 
73 static const GrColorSpaceInfo kColorSpaceInfo(nullptr, kRGBA_8888_GrPixelConfig);
74 
75 //////////////////////////////////////////////////////////////////////////////
76 
77 class SkInternalAtlasTextTarget : public GrTextUtils::Target, public SkAtlasTextTarget {
78 public:
SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context,int width,int height,void * handle)79     SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height,
80                               void* handle)
81             : GrTextUtils::Target(width, height, kColorSpaceInfo)
82             , SkAtlasTextTarget(std::move(context), width, height, handle) {}
83 
84     /** GrTextUtils::Target overrides */
85 
86     void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) override;
87 
drawPath(const GrClip &,const SkPath &,const SkPaint &,const SkMatrix & viewMatrix,const SkMatrix * pathMatrix,const SkIRect & clipBounds)88     void drawPath(const GrClip&, const SkPath&, const SkPaint&, const SkMatrix& viewMatrix,
89                   const SkMatrix* pathMatrix, const SkIRect& clipBounds) override {
90         SkDebugf("Path glyph??");
91     }
92 
makeGrPaint(GrMaskFormat,const SkPaint & skPaint,const SkMatrix &,GrPaint * grPaint)93     void makeGrPaint(GrMaskFormat, const SkPaint& skPaint, const SkMatrix&,
94                      GrPaint* grPaint) override {
95         grPaint->setColor4f(SkColorToPremulGrColor4fLegacy(skPaint.getColor()));
96     }
97 
98     /** SkAtlasTextTarget overrides */
99 
100     void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
101                   const SkAtlasTextFont&) override;
102     void flush() override;
103 
104 private:
105     uint32_t fColor;
106     using SkAtlasTextTarget::fWidth;
107     using SkAtlasTextTarget::fHeight;
108     SkTArray<std::unique_ptr<GrAtlasTextOp>, true> fOps;
109 };
110 
111 //////////////////////////////////////////////////////////////////////////////
112 
Make(sk_sp<SkAtlasTextContext> context,int width,int height,void * handle)113 std::unique_ptr<SkAtlasTextTarget> SkAtlasTextTarget::Make(sk_sp<SkAtlasTextContext> context,
114                                                            int width, int height, void* handle) {
115     return std::unique_ptr<SkAtlasTextTarget>(
116             new SkInternalAtlasTextTarget(std::move(context), width, height, handle));
117 }
118 
119 //////////////////////////////////////////////////////////////////////////////
120 
drawText(const SkGlyphID glyphs[],const SkPoint positions[],int glyphCnt,uint32_t color,const SkAtlasTextFont & font)121 void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[],
122                                          int glyphCnt, uint32_t color,
123                                          const SkAtlasTextFont& font) {
124     SkPaint paint;
125     paint.setAntiAlias(true);
126     paint.setTypeface(font.refTypeface());
127     paint.setTextSize(font.size());
128     paint.setStyle(SkPaint::kFill_Style);
129     paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
130 
131     // The atlas text context does munging of the paint color. We store the client's color here
132     // and then overwrite the generated op's color when addDrawOp() is called.
133     fColor = color;
134 
135     SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry);
136     auto* grContext = this->context()->internal().grContext();
137     auto bounds = SkIRect::MakeWH(fWidth, fHeight);
138     auto atlasTextContext = grContext->contextPriv().drawingManager()->getAtlasTextContext();
139     size_t byteLength = sizeof(SkGlyphID) * glyphCnt;
140     const SkScalar* pos = &positions->fX;
141     atlasTextContext->drawPosText(grContext, this, GrNoClip(), paint, this->ctm(), props,
142                                   (const char*)glyphs, byteLength, pos, 2, {0, 0}, bounds);
143 }
144 
addDrawOp(const GrClip & clip,std::unique_ptr<GrAtlasTextOp> op)145 void SkInternalAtlasTextTarget::addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) {
146     SkASSERT(clip.quickContains(SkRect::MakeIWH(fWidth, fHeight)));
147     // The SkAtlasTextRenderer currently only handles grayscale SDF glyphs.
148     if (op->maskType() != GrAtlasTextOp::kGrayscaleDistanceField_MaskType) {
149         return;
150     }
151     const GrCaps& caps = *this->context()->internal().grContext()->caps();
152     op->finalizeForTextTarget(fColor, caps);
153     int n = SkTMin(kMaxBatchLookBack, fOps.count());
154     for (int i = 0; i < n; ++i) {
155         GrAtlasTextOp* other = fOps.fromBack(i).get();
156         if (other->combineIfPossible(op.get(), caps)) {
157             return;
158         }
159         if (GrRectsOverlap(op->bounds(), other->bounds())) {
160             break;
161         }
162     }
163     op->visitProxies([](GrSurfaceProxy*) {});
164     fOps.emplace_back(std::move(op));
165 }
166 
flush()167 void SkInternalAtlasTextTarget::flush() {
168     for (int i = 0; i < fOps.count(); ++i) {
169         fOps[i]->executeForTextTarget(this);
170     }
171     this->context()->internal().flush();
172     fOps.reset();
173 }
174 
finalizeForTextTarget(uint32_t color,const GrCaps & caps)175 void GrAtlasTextOp::finalizeForTextTarget(uint32_t color, const GrCaps& caps) {
176     for (int i = 0; i < fGeoCount; ++i) {
177         fGeoData[i].fColor = color;
178     }
179     this->finalize(caps, nullptr /* applied clip */, GrPixelConfigIsClamped::kNo);
180 }
181 
executeForTextTarget(SkAtlasTextTarget * target)182 void GrAtlasTextOp::executeForTextTarget(SkAtlasTextTarget* target) {
183     FlushInfo flushInfo;
184     SkAutoGlyphCache glyphCache;
185     auto& context = target->context()->internal();
186     auto* atlasGlyphCache = context.grContext()->contextPriv().getAtlasGlyphCache();
187     for (int i = 0; i < fGeoCount; ++i) {
188         GrAtlasTextBlob::VertexRegenerator regenerator(
189                 fGeoData[i].fBlob, fGeoData[i].fRun, fGeoData[i].fSubRun, fGeoData[i].fViewMatrix,
190                 fGeoData[i].fX, fGeoData[i].fY, fGeoData[i].fColor, &context, atlasGlyphCache,
191                 &glyphCache);
192         GrAtlasTextBlob::VertexRegenerator::Result result;
193         do {
194             result = regenerator.regenerate();
195             context.recordDraw(result.fFirstVertex, result.fGlyphsRegenerated,
196                                fGeoData[i].fViewMatrix, target->handle());
197             if (!result.fFinished) {
198                 // Make space in the atlas so we can continue generating vertices.
199                 context.flush();
200             }
201         } while (!result.fFinished);
202     }
203 }
204