blob: 07627367ccf0e290c86e62e81767c0fd00951e15 [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 Salomon344ec422016-12-15 10:58:41 -05008#ifndef GrAtlasTextOp_DEFINED
9#define GrAtlasTextOp_DEFINED
joshualitta751c972015-11-20 13:37:32 -080010
Brian Salomon89527432016-12-16 09:52:16 -050011#include "ops/GrMeshDrawOp.h"
joshualitte8042922015-12-11 06:11:21 -080012#include "text/GrAtlasTextContext.h"
13#include "text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080014
Brian Salomon44acb5b2017-07-18 19:59:24 -040015class GrAtlasTextOp final : public GrMeshDrawOp {
joshualitta751c972015-11-20 13:37:32 -080016public:
Brian Salomon25a88092016-12-01 09:36:50 -050017 DEFINE_OP_CLASS_ID
joshualitta751c972015-11-20 13:37:32 -080018
Brian Salomonf8334782017-01-03 09:42:58 -050019 ~GrAtlasTextOp() override {
20 for (int i = 0; i < fGeoCount; i++) {
21 fGeoData[i].fBlob->unref();
22 }
23 }
24
joshualitt3660d532015-12-07 11:32:50 -080025 static const int kVerticesPerGlyph = GrAtlasTextBlob::kVerticesPerGlyph;
joshualitta751c972015-11-20 13:37:32 -080026 static const int kIndicesPerGlyph = 6;
27
joshualitta751c972015-11-20 13:37:32 -080028 typedef GrAtlasTextBlob Blob;
joshualitta751c972015-11-20 13:37:32 -080029 struct Geometry {
joshualitt8e0ef292016-02-19 14:13:03 -080030 SkMatrix fViewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -040031 SkIRect fClipRect;
32 Blob* fBlob;
joshualitt8e0ef292016-02-19 14:13:03 -080033 SkScalar fX;
34 SkScalar fY;
Jim Van Verth58c3cce2017-10-19 15:50:24 -040035 int fRun;
36 int fSubRun;
37 GrColor fColor;
joshualitta751c972015-11-20 13:37:32 -080038 };
39
Brian Salomon44acb5b2017-07-18 19:59:24 -040040 static std::unique_ptr<GrAtlasTextOp> MakeBitmap(GrPaint&& paint, GrMaskFormat maskFormat,
41 int glyphCount, GrAtlasGlyphCache* fontCache) {
42 std::unique_ptr<GrAtlasTextOp> op(new GrAtlasTextOp(std::move(paint)));
joshualitta751c972015-11-20 13:37:32 -080043
Brian Salomon344ec422016-12-15 10:58:41 -050044 op->fFontCache = fontCache;
joshualitta751c972015-11-20 13:37:32 -080045 switch (maskFormat) {
46 case kA8_GrMaskFormat:
Brian Salomon344ec422016-12-15 10:58:41 -050047 op->fMaskType = kGrayscaleCoverageMask_MaskType;
joshualitta751c972015-11-20 13:37:32 -080048 break;
49 case kA565_GrMaskFormat:
Brian Salomon344ec422016-12-15 10:58:41 -050050 op->fMaskType = kLCDCoverageMask_MaskType;
joshualitta751c972015-11-20 13:37:32 -080051 break;
52 case kARGB_GrMaskFormat:
Brian Salomon344ec422016-12-15 10:58:41 -050053 op->fMaskType = kColorBitmapMask_MaskType;
joshualitta751c972015-11-20 13:37:32 -080054 break;
55 }
Brian Salomon344ec422016-12-15 10:58:41 -050056 op->fNumGlyphs = glyphCount;
57 op->fGeoCount = 1;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -040058 op->fLuminanceColor = 0;
Brian Salomon344ec422016-12-15 10:58:41 -050059 op->fFontCache = fontCache;
Brian Salomon344ec422016-12-15 10:58:41 -050060 return op;
joshualitta751c972015-11-20 13:37:32 -080061 }
62
Brian Salomonf8334782017-01-03 09:42:58 -050063 static std::unique_ptr<GrAtlasTextOp> MakeDistanceField(
Brian Salomon44acb5b2017-07-18 19:59:24 -040064 GrPaint&& paint, int glyphCount, GrAtlasGlyphCache* fontCache,
Brian Salomon344ec422016-12-15 10:58:41 -050065 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Jim Van Verth90e89b32017-07-06 16:36:55 -040066 bool useGammaCorrectDistanceTable, SkColor luminanceColor, bool isLCD, bool useBGR,
67 bool isAntiAliased) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040068 std::unique_ptr<GrAtlasTextOp> op(new GrAtlasTextOp(std::move(paint)));
joshualitta751c972015-11-20 13:37:32 -080069
Brian Salomon344ec422016-12-15 10:58:41 -050070 op->fFontCache = fontCache;
Jim Van Verth90e89b32017-07-06 16:36:55 -040071 op->fMaskType = !isAntiAliased ? kAliasedDistanceField_MaskType
72 : isLCD ? (useBGR ? kLCDBGRDistanceField_MaskType
73 : kLCDDistanceField_MaskType)
74 : kGrayscaleDistanceField_MaskType;
Brian Salomon344ec422016-12-15 10:58:41 -050075 op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
76 op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -040077 op->fLuminanceColor = luminanceColor;
Brian Salomon344ec422016-12-15 10:58:41 -050078 op->fNumGlyphs = glyphCount;
79 op->fGeoCount = 1;
80 return op;
joshualitta751c972015-11-20 13:37:32 -080081 }
82
Brian Salomon344ec422016-12-15 10:58:41 -050083 // To avoid even the initial copy of the struct, we have a getter for the first item which
84 // is used to seed the op with its initial geometry. After seeding, the client should call
85 // init() so the op can initialize itself
joshualitta751c972015-11-20 13:37:32 -080086 Geometry& geometry() { return fGeoData[0]; }
87
88 void init() {
89 const Geometry& geo = fGeoData[0];
Brian Salomon344ec422016-12-15 10:58:41 -050090 fColor = geo.fColor;
bsalomon88cf17d2016-07-08 06:40:56 -070091 SkRect bounds;
92 geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX,
joshualitt8e0ef292016-02-19 14:13:03 -080093 geo.fY);
bsalomon88cf17d2016-07-08 06:40:56 -070094 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
95 // we treat this as a set of non-AA rects rendered with a texture.
96 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
joshualitta751c972015-11-20 13:37:32 -080097 }
98
Brian Salomon344ec422016-12-15 10:58:41 -050099 const char* name() const override { return "AtlasTextOp"; }
joshualitta751c972015-11-20 13:37:32 -0800100
Robert Phillipsf1748f52017-09-14 14:11:24 -0400101 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400102 fProcessors.visitProxies(func);
103
104 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(this->maskFormat());
105 for (int i = 0; i < kMaxTextures; ++i) {
106 if (proxies[i]) {
107 func(proxies[i].get());
108 }
109 }
110 }
111
joshualitta751c972015-11-20 13:37:32 -0800112 SkString dumpInfo() const override;
113
Brian Salomon44acb5b2017-07-18 19:59:24 -0400114 FixedFunctionFlags fixedFunctionFlags() const override;
115
Brian Osman9a725dd2017-09-20 09:53:22 -0400116 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
117 GrPixelConfigIsClamped dstIsClamped) override;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400118
joshualitta751c972015-11-20 13:37:32 -0800119private:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400120 GrAtlasTextOp(GrPaint&& paint)
121 : INHERITED(ClassID())
122 , fColor(paint.getColor())
123 , fSRGBFlags(GrPipeline::SRGBFlagsFromPaint(paint))
124 , fProcessors(std::move(paint)) {}
joshualitta751c972015-11-20 13:37:32 -0800125
126 struct FlushInfo {
Brian Salomon344ec422016-12-15 10:58:41 -0500127 sk_sp<const GrBuffer> fVertexBuffer;
128 sk_sp<const GrBuffer> fIndexBuffer;
Hal Canary144caf52016-11-07 17:57:18 -0500129 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400130 const GrPipeline* fPipeline;
Brian Salomon344ec422016-12-15 10:58:41 -0500131 int fGlyphsToFlush;
132 int fVertexOffset;
joshualitta751c972015-11-20 13:37:32 -0800133 };
134
Brian Salomon91326c32017-08-09 16:02:19 -0400135 void onPrepareDraws(Target*) override;
joshualitta751c972015-11-20 13:37:32 -0800136
joshualitta751c972015-11-20 13:37:32 -0800137 GrMaskFormat maskFormat() const {
138 switch (fMaskType) {
139 case kLCDCoverageMask_MaskType:
140 return kA565_GrMaskFormat;
141 case kColorBitmapMask_MaskType:
142 return kARGB_GrMaskFormat;
143 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400144 case kAliasedDistanceField_MaskType:
joshualitta751c972015-11-20 13:37:32 -0800145 case kGrayscaleDistanceField_MaskType:
146 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400147 case kLCDBGRDistanceField_MaskType:
joshualitta751c972015-11-20 13:37:32 -0800148 return kA8_GrMaskFormat;
149 }
Brian Salomon344ec422016-12-15 10:58:41 -0500150 return kA8_GrMaskFormat; // suppress warning
joshualitta751c972015-11-20 13:37:32 -0800151 }
152
153 bool usesDistanceFields() const {
Jim Van Verth90e89b32017-07-06 16:36:55 -0400154 return kAliasedDistanceField_MaskType == fMaskType ||
155 kGrayscaleDistanceField_MaskType == fMaskType ||
156 kLCDDistanceField_MaskType == fMaskType ||
157 kLCDBGRDistanceField_MaskType == fMaskType;
joshualitta751c972015-11-20 13:37:32 -0800158 }
159
160 bool isLCD() const {
Jim Van Verth90e89b32017-07-06 16:36:55 -0400161 return kLCDCoverageMask_MaskType == fMaskType ||
162 kLCDDistanceField_MaskType == fMaskType ||
163 kLCDBGRDistanceField_MaskType == fMaskType;
joshualitta751c972015-11-20 13:37:32 -0800164 }
165
Brian Salomone5b399e2017-07-19 13:50:54 -0400166 inline void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const;
joshualitta751c972015-11-20 13:37:32 -0800167
Brian Salomon344ec422016-12-15 10:58:41 -0500168 GrColor color() const { return fColor; }
joshualitt8e0ef292016-02-19 14:13:03 -0800169 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
Brian Salomon344ec422016-12-15 10:58:41 -0500170 bool usesLocalCoords() const { return fUsesLocalCoords; }
171 int numGlyphs() const { return fNumGlyphs; }
joshualitta751c972015-11-20 13:37:32 -0800172
Brian Salomon25a88092016-12-01 09:36:50 -0500173 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
joshualitta751c972015-11-20 13:37:32 -0800174
Jim Van Vertha950b632017-09-12 11:54:11 -0400175 static constexpr auto kMaxTextures = 4;
Ben Wagner63fd7602017-10-09 15:45:33 -0400176
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400177 sk_sp<GrGeometryProcessor> setupDfProcessor() const;
joshualitta751c972015-11-20 13:37:32 -0800178
joshualitta751c972015-11-20 13:37:32 -0800179 // The minimum number of Geometry we will try to allocate.
180 enum { kMinGeometryAllocated = 4 };
joshualitta751c972015-11-20 13:37:32 -0800181
182 enum MaskType {
183 kGrayscaleCoverageMask_MaskType,
184 kLCDCoverageMask_MaskType,
185 kColorBitmapMask_MaskType,
Jim Van Verth90e89b32017-07-06 16:36:55 -0400186 kAliasedDistanceField_MaskType,
joshualitta751c972015-11-20 13:37:32 -0800187 kGrayscaleDistanceField_MaskType,
188 kLCDDistanceField_MaskType,
Jim Van Verth90e89b32017-07-06 16:36:55 -0400189 kLCDBGRDistanceField_MaskType,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400190 };
joshualitta751c972015-11-20 13:37:32 -0800191
Brian Salomon44acb5b2017-07-18 19:59:24 -0400192 SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
193 GrColor fColor;
194 uint32_t fSRGBFlags;
195 GrProcessorSet fProcessors;
196 bool fUsesLocalCoords;
197 bool fCanCombineOnTouchOrOverlap;
198 int fGeoCount;
199 int fNumGlyphs;
200 MaskType fMaskType;
Brian Salomonf856fd12016-12-16 14:24:34 -0500201 GrAtlasGlyphCache* fFontCache;
joshualitta751c972015-11-20 13:37:32 -0800202 // Distance field properties
Hal Canary144caf52016-11-07 17:57:18 -0500203 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400204 SkColor fLuminanceColor;
brianosmanb461d342016-04-13 13:10:14 -0700205 bool fUseGammaCorrectDistanceTable;
joshualitta751c972015-11-20 13:37:32 -0800206
Brian Salomon344ec422016-12-15 10:58:41 -0500207 friend class GrBlobRegenHelper; // Needs to trigger flushes
joshualittddd22d82016-02-16 06:47:52 -0800208
Brian Salomon44acb5b2017-07-18 19:59:24 -0400209 typedef GrMeshDrawOp INHERITED;
joshualitta751c972015-11-20 13:37:32 -0800210};
211
joshualittddd22d82016-02-16 06:47:52 -0800212/*
213 * A simple helper class to abstract the interface GrAtlasTextBlob needs to regenerate itself.
214 * It'd be nicer if this was nested, but we need to forward declare it in GrAtlasTextBlob.h
215 */
216class GrBlobRegenHelper {
217public:
Brian Salomone5b399e2017-07-19 13:50:54 -0400218 GrBlobRegenHelper(const GrAtlasTextOp* op, GrMeshDrawOp::Target* target,
Brian Salomon344ec422016-12-15 10:58:41 -0500219 GrAtlasTextOp::FlushInfo* flushInfo)
220 : fOp(op), fTarget(target), fFlushInfo(flushInfo) {}
joshualittddd22d82016-02-16 06:47:52 -0800221
222 void flush();
223
Brian Salomon344ec422016-12-15 10:58:41 -0500224 void incGlyphCount(int glyphCount = 1) { fFlushInfo->fGlyphsToFlush += glyphCount; }
joshualittddd22d82016-02-16 06:47:52 -0800225
226private:
Brian Salomon344ec422016-12-15 10:58:41 -0500227 const GrAtlasTextOp* fOp;
Brian Salomone5b399e2017-07-19 13:50:54 -0400228 GrMeshDrawOp::Target* fTarget;
Brian Salomon344ec422016-12-15 10:58:41 -0500229 GrAtlasTextOp::FlushInfo* fFlushInfo;
joshualittddd22d82016-02-16 06:47:52 -0800230};
231
joshualitta751c972015-11-20 13:37:32 -0800232#endif