blob: d80c7a45d7f1f6c0146181ca954ce8979b26994e [file] [log] [blame]
Brian Salomoncbcb0a12017-11-19 13:20:13 -05001/*
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"
Brian Salomona0ba7142017-11-20 13:17:43 -050010#include "GrContextPriv.h"
11#include "GrDrawingManager.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050012#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
Brian Salomon778a2c92017-11-27 12:18:04 -050020static constexpr int kMaxBatchLookBack = 10;
21
Brian Salomoncbcb0a12017-11-19 13:20:13 -050022SkAtlasTextTarget::SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height,
23 void* handle)
Brian Salomonb5086962017-12-13 10:59:33 -050024 : 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}
Brian Salomoncbcb0a12017-11-19 13:20:13 -050033
34SkAtlasTextTarget::~SkAtlasTextTarget() { fContext->renderer()->targetDeleted(fHandle); }
35
Brian Salomonb5086962017-12-13 10:59:33 -050036int SkAtlasTextTarget::save() {
37 const auto& currCTM = this->ctm();
38 *static_cast<SkMatrix*>(fMatrixStack.push_back()) = currCTM;
39 return fSaveCnt++;
40}
41
42void SkAtlasTextTarget::restore() {
43 if (fSaveCnt) {
44 fMatrixStack.pop_back();
45 fSaveCnt--;
46 }
47}
48
49void SkAtlasTextTarget::restoreToCount(int count) {
50 while (fSaveCnt > count) {
51 this->restore();
52 }
53}
54
55void SkAtlasTextTarget::translate(SkScalar dx, SkScalar dy) {
56 this->accessCTM()->preTranslate(dx, dy);
57}
58
59void SkAtlasTextTarget::scale(SkScalar sx, SkScalar sy) { this->accessCTM()->preScale(sx, sy); }
60
61void SkAtlasTextTarget::rotate(SkScalar degrees) { this->accessCTM()->preRotate(degrees); }
62
63void SkAtlasTextTarget::rotate(SkScalar degrees, SkScalar px, SkScalar py) {
64 this->accessCTM()->preRotate(degrees, px, py);
65}
66
67void SkAtlasTextTarget::skew(SkScalar sx, SkScalar sy) { this->accessCTM()->preSkew(sx, sy); }
68
69void SkAtlasTextTarget::concat(const SkMatrix& matrix) { this->accessCTM()->preConcat(matrix); }
70
Brian Salomoncbcb0a12017-11-19 13:20:13 -050071//////////////////////////////////////////////////////////////////////////////
72
73static const GrColorSpaceInfo kColorSpaceInfo(nullptr, kRGBA_8888_GrPixelConfig);
74
75//////////////////////////////////////////////////////////////////////////////
76
77class SkInternalAtlasTextTarget : public GrTextUtils::Target, public SkAtlasTextTarget {
78public:
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
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
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
Brian Salomona0ba7142017-11-20 13:17:43 -0500100 void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500101 const SkAtlasTextFont&) override;
102 void flush() override;
103
104private:
105 uint32_t fColor;
106 using SkAtlasTextTarget::fWidth;
107 using SkAtlasTextTarget::fHeight;
Brian Salomon778a2c92017-11-27 12:18:04 -0500108 SkTArray<std::unique_ptr<GrAtlasTextOp>, true> fOps;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500109};
110
111//////////////////////////////////////////////////////////////////////////////
112
113std::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
Brian Salomona0ba7142017-11-20 13:17:43 -0500121void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[],
122 int glyphCnt, uint32_t color,
123 const SkAtlasTextFont& font) {
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500124 SkPaint paint;
125 paint.setAntiAlias(true);
126 paint.setTypeface(font.refTypeface());
127 paint.setTextSize(font.size());
128 paint.setStyle(SkPaint::kFill_Style);
Brian Salomon0c1c2b32017-11-20 13:13:01 -0500129 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500130
131 // The atlas text context does munging of the paint color. We store the client's color here
Brian Salomon778a2c92017-11-27 12:18:04 -0500132 // and then overwrite the generated op's color when addDrawOp() is called.
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500133 fColor = color;
134
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500135 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();
Brian Salomon0c1c2b32017-11-20 13:13:01 -0500139 size_t byteLength = sizeof(SkGlyphID) * glyphCnt;
Brian Salomona0ba7142017-11-20 13:17:43 -0500140 const SkScalar* pos = &positions->fX;
Brian Salomonb5086962017-12-13 10:59:33 -0500141 atlasTextContext->drawPosText(grContext, this, GrNoClip(), paint, this->ctm(), props,
Brian Salomona0ba7142017-11-20 13:17:43 -0500142 (const char*)glyphs, byteLength, pos, 2, {0, 0}, bounds);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500143}
144
145void 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 }
Brian Salomon778a2c92017-11-27 12:18:04 -0500151 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 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500163 op->visitProxies([](GrSurfaceProxy*) {});
Brian Salomon778a2c92017-11-27 12:18:04 -0500164 fOps.emplace_back(std::move(op));
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500165}
166
167void SkInternalAtlasTextTarget::flush() {
168 for (int i = 0; i < fOps.count(); ++i) {
Brian Salomon778a2c92017-11-27 12:18:04 -0500169 fOps[i]->executeForTextTarget(this);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500170 }
171 this->context()->internal().flush();
172 fOps.reset();
173}
174
Brian Salomon778a2c92017-11-27 12:18:04 -0500175void 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
182void GrAtlasTextOp::executeForTextTarget(SkAtlasTextTarget* target) {
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500183 FlushInfo flushInfo;
184 SkAutoGlyphCache glyphCache;
185 auto& context = target->context()->internal();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500186 auto* atlasGlyphCache = context.grContext()->contextPriv().getAtlasGlyphCache();
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500187 for (int i = 0; i < fGeoCount; ++i) {
188 GrAtlasTextBlob::VertexRegenerator regenerator(
189 fGeoData[i].fBlob, fGeoData[i].fRun, fGeoData[i].fSubRun, fGeoData[i].fViewMatrix,
Brian Salomon778a2c92017-11-27 12:18:04 -0500190 fGeoData[i].fX, fGeoData[i].fY, fGeoData[i].fColor, &context, atlasGlyphCache,
191 &glyphCache);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500192 GrAtlasTextBlob::VertexRegenerator::Result result;
193 do {
194 result = regenerator.regenerate();
Brian Salomonb5086962017-12-13 10:59:33 -0500195 context.recordDraw(result.fFirstVertex, result.fGlyphsRegenerated,
196 fGeoData[i].fViewMatrix, target->handle());
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500197 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}