blob: 68dc6fae90cda4dfa84517ffad61fe3c5a406a11 [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"
Robert Phillips7c525e62018-06-12 10:11:12 -04009
Brian Salomoncbcb0a12017-11-19 13:20:13 -050010#include "GrClip.h"
Brian Salomona0ba7142017-11-20 13:17:43 -050011#include "GrContextPriv.h"
12#include "GrDrawingManager.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040013#include "GrMemoryPool.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050014#include "SkAtlasTextContext.h"
15#include "SkAtlasTextFont.h"
16#include "SkAtlasTextRenderer.h"
Herb Derby8378dfb2018-08-30 14:50:04 -040017#include "SkGlyphRunPainter.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050018#include "SkGr.h"
19#include "SkInternalAtlasTextContext.h"
20#include "ops/GrAtlasTextOp.h"
Herb Derby26cbe512018-05-24 14:39:01 -040021#include "text/GrTextContext.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050022
Brian Salomon778a2c92017-11-27 12:18:04 -050023static constexpr int kMaxBatchLookBack = 10;
24
Brian Salomoncbcb0a12017-11-19 13:20:13 -050025SkAtlasTextTarget::SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height,
26 void* handle)
Brian Salomonb5086962017-12-13 10:59:33 -050027 : 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}
Brian Salomoncbcb0a12017-11-19 13:20:13 -050036
37SkAtlasTextTarget::~SkAtlasTextTarget() { fContext->renderer()->targetDeleted(fHandle); }
38
Brian Salomonb5086962017-12-13 10:59:33 -050039int SkAtlasTextTarget::save() {
40 const auto& currCTM = this->ctm();
41 *static_cast<SkMatrix*>(fMatrixStack.push_back()) = currCTM;
42 return fSaveCnt++;
43}
44
45void SkAtlasTextTarget::restore() {
46 if (fSaveCnt) {
47 fMatrixStack.pop_back();
48 fSaveCnt--;
49 }
50}
51
52void SkAtlasTextTarget::restoreToCount(int count) {
53 while (fSaveCnt > count) {
54 this->restore();
55 }
56}
57
58void SkAtlasTextTarget::translate(SkScalar dx, SkScalar dy) {
59 this->accessCTM()->preTranslate(dx, dy);
60}
61
62void SkAtlasTextTarget::scale(SkScalar sx, SkScalar sy) { this->accessCTM()->preScale(sx, sy); }
63
64void SkAtlasTextTarget::rotate(SkScalar degrees) { this->accessCTM()->preRotate(degrees); }
65
66void SkAtlasTextTarget::rotate(SkScalar degrees, SkScalar px, SkScalar py) {
67 this->accessCTM()->preRotate(degrees, px, py);
68}
69
70void SkAtlasTextTarget::skew(SkScalar sx, SkScalar sy) { this->accessCTM()->preSkew(sx, sy); }
71
72void SkAtlasTextTarget::concat(const SkMatrix& matrix) { this->accessCTM()->preConcat(matrix); }
73
Brian Salomoncbcb0a12017-11-19 13:20:13 -050074//////////////////////////////////////////////////////////////////////////////
75
76static const GrColorSpaceInfo kColorSpaceInfo(nullptr, kRGBA_8888_GrPixelConfig);
Herb Derby74c6ed32018-07-28 18:07:54 -040077static const SkSurfaceProps kProps(
78 SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry);
Brian Salomoncbcb0a12017-11-19 13:20:13 -050079
80//////////////////////////////////////////////////////////////////////////////
81
Herb Derbyc1b482c2018-08-09 15:02:27 -040082class SkInternalAtlasTextTarget : public GrTextTarget, public SkAtlasTextTarget {
Brian Salomoncbcb0a12017-11-19 13:20:13 -050083public:
Robert Phillips7c525e62018-06-12 10:11:12 -040084 SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context,
85 int width, int height,
Brian Salomoncbcb0a12017-11-19 13:20:13 -050086 void* handle)
Herb Derbyc1b482c2018-08-09 15:02:27 -040087 : GrTextTarget(width, height, kColorSpaceInfo)
Herb Derby74c6ed32018-07-28 18:07:54 -040088 , SkAtlasTextTarget(std::move(context), width, height, handle)
Herb Derby65956872018-08-21 16:55:04 -040089 , fGlyphPainter(kProps, kColorSpaceInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050090 fOpMemoryPool = fContext->internal().grContext()->priv().refOpMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040091 }
92
93 ~SkInternalAtlasTextTarget() override {
94 this->deleteOps();
Robert Phillips7c525e62018-06-12 10:11:12 -040095 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -050096
Herb Derbyc1b482c2018-08-09 15:02:27 -040097 /** GrTextTarget overrides */
Brian Salomoncbcb0a12017-11-19 13:20:13 -050098
99 void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) override;
100
Robert Phillips46a13382018-08-23 13:53:01 -0400101 void drawShape(const GrClip&, const SkPaint&, const SkMatrix& viewMatrix,
102 const GrShape&) override {
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500103 SkDebugf("Path glyph??");
104 }
105
106 void makeGrPaint(GrMaskFormat, const SkPaint& skPaint, const SkMatrix&,
107 GrPaint* grPaint) override {
Brian Osmancb3d0872018-10-16 15:19:28 -0400108 grPaint->setColor4f(skPaint.getColor4f().premul());
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500109 }
110
Robert Phillips7c525e62018-06-12 10:11:12 -0400111 GrContext* getContext() override {
112 return this->context()->internal().grContext();
113 }
114
Herb Derby65956872018-08-21 16:55:04 -0400115 SkGlyphRunListPainter* glyphPainter() override {
116 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400117 }
118
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500119 /** SkAtlasTextTarget overrides */
120
Brian Salomona0ba7142017-11-20 13:17:43 -0500121 void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color,
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500122 const SkAtlasTextFont&) override;
123 void flush() override;
124
125private:
Robert Phillipsc994a932018-06-19 13:09:54 -0400126 void deleteOps();
127
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500128 uint32_t fColor;
129 using SkAtlasTextTarget::fWidth;
130 using SkAtlasTextTarget::fHeight;
Brian Salomon778a2c92017-11-27 12:18:04 -0500131 SkTArray<std::unique_ptr<GrAtlasTextOp>, true> fOps;
Robert Phillipsc994a932018-06-19 13:09:54 -0400132 sk_sp<GrOpMemoryPool> fOpMemoryPool;
Herb Derby65956872018-08-21 16:55:04 -0400133 SkGlyphRunListPainter fGlyphPainter;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500134};
135
136//////////////////////////////////////////////////////////////////////////////
137
138std::unique_ptr<SkAtlasTextTarget> SkAtlasTextTarget::Make(sk_sp<SkAtlasTextContext> context,
139 int width, int height, void* handle) {
140 return std::unique_ptr<SkAtlasTextTarget>(
141 new SkInternalAtlasTextTarget(std::move(context), width, height, handle));
142}
143
144//////////////////////////////////////////////////////////////////////////////
145
Brian Salomona0ba7142017-11-20 13:17:43 -0500146void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[],
147 int glyphCnt, uint32_t color,
148 const SkAtlasTextFont& font) {
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500149 SkPaint paint;
150 paint.setAntiAlias(true);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500151
152 // The atlas text context does munging of the paint color. We store the client's color here
Brian Salomon778a2c92017-11-27 12:18:04 -0500153 // and then overwrite the generated op's color when addDrawOp() is called.
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500154 fColor = color;
155
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500156 SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry);
157 auto* grContext = this->context()->internal().grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500158 auto atlasTextContext = grContext->priv().drawingManager()->getTextContext();
Herb Derby13449ce2018-07-27 16:23:27 -0400159 SkGlyphRunBuilder builder;
Mike Reed191e64b2019-01-02 15:35:29 -0500160 builder.drawGlyphsWithPositions(paint, font.makeFont(),
161 SkSpan<const SkGlyphID>{glyphs, SkTo<size_t>(glyphCnt)},
Herb Derby95e17602018-12-06 17:11:43 -0500162 positions);
Herb Derby13449ce2018-07-27 16:23:27 -0400163 auto glyphRunList = builder.useGlyphRunList();
164 if (!glyphRunList.empty()) {
165 atlasTextContext->drawGlyphRunList(grContext, this, GrNoClip(), this->ctm(), props,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400166 glyphRunList);
Herb Derby13449ce2018-07-27 16:23:27 -0400167 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500168}
169
170void SkInternalAtlasTextTarget::addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) {
171 SkASSERT(clip.quickContains(SkRect::MakeIWH(fWidth, fHeight)));
172 // The SkAtlasTextRenderer currently only handles grayscale SDF glyphs.
173 if (op->maskType() != GrAtlasTextOp::kGrayscaleDistanceField_MaskType) {
174 return;
175 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500176 const GrCaps& caps = *this->context()->internal().grContext()->priv().caps();
Brian Salomon778a2c92017-11-27 12:18:04 -0500177 op->finalizeForTextTarget(fColor, caps);
178 int n = SkTMin(kMaxBatchLookBack, fOps.count());
179 for (int i = 0; i < n; ++i) {
180 GrAtlasTextOp* other = fOps.fromBack(i).get();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000181 if (other->combineIfPossible(op.get(), caps) == GrOp::CombineResult::kMerged) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400182 fOpMemoryPool->release(std::move(op));
Brian Salomon778a2c92017-11-27 12:18:04 -0500183 return;
184 }
185 if (GrRectsOverlap(op->bounds(), other->bounds())) {
186 break;
187 }
188 }
Brian Salomon778a2c92017-11-27 12:18:04 -0500189 fOps.emplace_back(std::move(op));
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500190}
191
Robert Phillipsc994a932018-06-19 13:09:54 -0400192void SkInternalAtlasTextTarget::deleteOps() {
193 for (int i = 0; i < fOps.count(); ++i) {
194 if (fOps[i]) {
195 fOpMemoryPool->release(std::move(fOps[i]));
196 }
197 }
198 fOps.reset();
199}
200
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500201void SkInternalAtlasTextTarget::flush() {
202 for (int i = 0; i < fOps.count(); ++i) {
Brian Salomon778a2c92017-11-27 12:18:04 -0500203 fOps[i]->executeForTextTarget(this);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500204 }
205 this->context()->internal().flush();
Robert Phillipsc994a932018-06-19 13:09:54 -0400206 this->deleteOps();
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500207}
208
Brian Salomon778a2c92017-11-27 12:18:04 -0500209void GrAtlasTextOp::finalizeForTextTarget(uint32_t color, const GrCaps& caps) {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400210 // TODO4F: Odd handling of client colors among AtlasTextTarget and AtlasTextRenderer
Brian Osmancf860852018-10-31 14:04:39 -0400211 SkPMColor4f color4f = SkPMColor4f::FromBytes_RGBA(color);
Brian Salomon778a2c92017-11-27 12:18:04 -0500212 for (int i = 0; i < fGeoCount; ++i) {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400213 fGeoData[i].fColor = color4f;
Brian Salomon778a2c92017-11-27 12:18:04 -0500214 }
Brian Osman532b3f92018-07-11 10:02:07 -0400215 this->finalize(caps, nullptr /* applied clip */);
Brian Salomon778a2c92017-11-27 12:18:04 -0500216}
217
218void GrAtlasTextOp::executeForTextTarget(SkAtlasTextTarget* target) {
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500219 FlushInfo flushInfo;
Herb Derby7956b592018-03-22 16:10:30 -0400220 SkExclusiveStrikePtr autoGlyphCache;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500221 auto& context = target->context()->internal();
Robert Phillips9da87e02019-02-04 13:26:26 -0500222 auto glyphCache = context.grContext()->priv().getGlyphCache();
223 auto atlasManager = context.grContext()->priv().getAtlasManager();
224 auto resourceProvider = context.grContext()->priv().resourceProvider();
Robert Phillips4bc70112018-03-01 10:24:02 -0500225
Robert Phillips5a66efb2018-03-07 15:13:18 -0500226 unsigned int numProxies;
227 if (!atlasManager->getProxies(kA8_GrMaskFormat, &numProxies)) {
228 return;
229 }
230
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500231 for (int i = 0; i < fGeoCount; ++i) {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400232 // TODO4F: Preserve float colors
Herb Derby86240592018-05-24 16:12:31 -0400233 GrTextBlob::VertexRegenerator regenerator(
Robert Phillips4bc70112018-03-01 10:24:02 -0500234 resourceProvider, fGeoData[i].fBlob, fGeoData[i].fRun, fGeoData[i].fSubRun,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400235 fGeoData[i].fViewMatrix, fGeoData[i].fX, fGeoData[i].fY,
Brian Osmancf860852018-10-31 14:04:39 -0400236 fGeoData[i].fColor.toBytes_RGBA(), &context, glyphCache, atlasManager,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400237 &autoGlyphCache);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500238 bool done = false;
239 while (!done) {
Herb Derby86240592018-05-24 16:12:31 -0400240 GrTextBlob::VertexRegenerator::Result result;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500241 if (!regenerator.regenerate(&result)) {
242 break;
243 }
244 done = result.fFinished;
245
Brian Salomonb5086962017-12-13 10:59:33 -0500246 context.recordDraw(result.fFirstVertex, result.fGlyphsRegenerated,
247 fGeoData[i].fViewMatrix, target->handle());
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500248 if (!result.fFinished) {
249 // Make space in the atlas so we can continue generating vertices.
250 context.flush();
251 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500252 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500253 }
254}