blob: c999e1f0afb34642eea402b2f8864c6f790e4957 [file] [log] [blame]
joshualitt1d89e8d2015-04-01 12:40:54 -07001/*
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
8#ifndef GrAtlasTextContext_DEFINED
9#define GrAtlasTextContext_DEFINED
10
11#include "GrTextContext.h"
12
joshualittb4c507e2015-04-08 08:07:59 -070013#include "GrBatchAtlas.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070014#include "GrGeometryProcessor.h"
15#include "SkDescriptor.h"
joshualittb7133be2015-04-08 09:08:31 -070016#include "GrMemoryPool.h"
joshualitt53b5f442015-04-13 06:33:59 -070017#include "SkMaskFilter.h"
joshualitt9a27e632015-04-06 10:53:36 -070018#include "SkTextBlob.h"
joshualittb7133be2015-04-08 09:08:31 -070019#include "SkTInternalLList.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070020
21class GrBatchTextStrike;
22class GrPipelineBuilder;
joshualittb7133be2015-04-08 09:08:31 -070023class GrTextBlobCache;
joshualitt1d89e8d2015-04-01 12:40:54 -070024
25/*
26 * This class implements GrTextContext using standard bitmap fonts, and can also process textblobs.
27 * TODO replace GrBitmapTextContext
28 */
joshualittdbd35932015-04-02 09:19:04 -070029class GrAtlasTextContext : public GrTextContext {
joshualitt1d89e8d2015-04-01 12:40:54 -070030public:
joshualittdbd35932015-04-02 09:19:04 -070031 static GrAtlasTextContext* Create(GrContext*, SkGpuDevice*, const SkDeviceProperties&);
joshualitt1d89e8d2015-04-01 12:40:54 -070032
joshualitt1d89e8d2015-04-01 12:40:54 -070033private:
joshualittdbd35932015-04-02 09:19:04 -070034 GrAtlasTextContext(GrContext*, SkGpuDevice*, const SkDeviceProperties&);
joshualitt1d89e8d2015-04-01 12:40:54 -070035
36 bool canDraw(const GrRenderTarget*, const GrClip&, const GrPaint&,
37 const SkPaint&, const SkMatrix& viewMatrix) override;
38
39 void onDrawText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
40 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
41 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) override;
42 void onDrawPosText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
43 const SkMatrix& viewMatrix,
44 const char text[], size_t byteLength,
45 const SkScalar pos[], int scalarsPerPosition,
46 const SkPoint& offset, const SkIRect& regionClipBounds) override;
47 void drawTextBlob(GrRenderTarget*, const GrClip&, const SkPaint&,
48 const SkMatrix& viewMatrix, const SkTextBlob*, SkScalar x, SkScalar y,
49 SkDrawFilter*, const SkIRect& clipBounds) override;
50
joshualitt1d89e8d2015-04-01 12:40:54 -070051 /*
52 * A BitmapTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
53 * on the GPU. These are initially created with valid positions and colors, but invalid
54 * texture coordinates. The BitmapTextBlob itself has a few Blob-wide properties, and also
55 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
56 * reordered.
57 *
58 * The only thing(aside from a memcopy) required to flush a BitmapTextBlob is to ensure that
59 * the GrAtlas will not evict anything the Blob needs.
60 * TODO this is currently a bug
61 */
62 struct BitmapTextBlob : public SkRefCnt {
joshualittb7133be2015-04-08 09:08:31 -070063 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BitmapTextBlob);
64
joshualitt9a27e632015-04-06 10:53:36 -070065 /*
66 * Each Run inside of the blob can have its texture coordinates regenerated if required.
67 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
68 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
69 * this at a more fine grained level, but its not clear if this is worth it, as evictions
70 * should be fairly rare.
71 *
72 * One additional point, each run can contain glyphs with any of the three mask formats.
73 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
74 * a new subrun each time the mask format changes in a run. In theory, a run can have as
75 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
76 * practice, the vast majority of runs have only a single subrun.
77 *
78 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
79 * handle, we have a bit to mark the run as flusahable via rendering as paths. It is worth
80 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
81 * can flush in this manner, so we always allocate vertices for the run, regardless of
82 * whether or not it is too large. The benefit of this strategy is that we can always reuse
83 * a blob allocation regardless of viewmatrix changes. We could store positions for these
84 * glyphs. However, its not clear if this is a win because we'd still have to either go the
85 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
86 * would greatly increase the memory of these cached items.
87 */
joshualitt1d89e8d2015-04-01 12:40:54 -070088 struct Run {
joshualitt9a27e632015-04-06 10:53:36 -070089 Run() : fColor(GrColor_ILLEGAL), fInitialized(false), fDrawAsPaths(false) {
joshualitt1d89e8d2015-04-01 12:40:54 -070090 fVertexBounds.setLargestInverted();
91 // We insert the first subrun to gurantee a run always has atleast one subrun.
92 // We do this to simplify things when we 'hand off' data from one subrun to the
93 // next
94 fSubRunInfo.push_back();
95 }
96 struct SubRunInfo {
97 SubRunInfo()
98 : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGeneration)
99 , fGlyphStartIndex(0)
100 , fGlyphEndIndex(0)
101 , fVertexStartIndex(0)
102 , fVertexEndIndex(0) {}
103 GrMaskFormat fMaskFormat;
104 uint64_t fAtlasGeneration;
105 uint32_t fGlyphStartIndex;
106 uint32_t fGlyphEndIndex;
107 size_t fVertexStartIndex;
108 size_t fVertexEndIndex;
joshualittb4c507e2015-04-08 08:07:59 -0700109 GrBatchAtlas::BulkUseTokenUpdater fBulkUseToken;
joshualitt1d89e8d2015-04-01 12:40:54 -0700110 };
111 SkSTArray<1, SubRunInfo, true> fSubRunInfo;
112 SkAutoDescriptor fDescriptor;
113 SkAutoTUnref<SkTypeface> fTypeface;
114 SkRect fVertexBounds;
115 GrColor fColor;
116 bool fInitialized;
joshualitt9a27e632015-04-06 10:53:36 -0700117 bool fDrawAsPaths;
joshualitt1d89e8d2015-04-01 12:40:54 -0700118 };
119
120 struct BigGlyph {
121 BigGlyph(const SkPath& path, int vx, int vy) : fPath(path), fVx(vx), fVy(vy) {}
122 SkPath fPath;
123 int fVx;
124 int fVy;
125 };
joshualitt2a0e9f32015-04-13 06:12:21 -0700126#ifdef SK_DEBUG
127 mutable SkScalar fTotalXError;
128 mutable SkScalar fTotalYError;
129#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700130 SkTArray<BigGlyph> fBigGlyphs;
joshualitt2a0e9f32015-04-13 06:12:21 -0700131 GrColor fColor; // the original color on the paint
joshualitt1d89e8d2015-04-01 12:40:54 -0700132 SkMatrix fViewMatrix;
133 SkScalar fX;
134 SkScalar fY;
joshualitt9a27e632015-04-06 10:53:36 -0700135 int fRunCount;
joshualitt53b5f442015-04-13 06:33:59 -0700136 SkMaskFilter::BlurRec fBlurRec;
137 struct StrokeInfo {
138 SkScalar fFrameWidth;
139 SkScalar fMiterLimit;
140 SkPaint::Join fJoin;
141 };
142 StrokeInfo fStrokeInfo;
joshualittb7133be2015-04-08 09:08:31 -0700143 GrMemoryPool* fPool;
joshualitt1d89e8d2015-04-01 12:40:54 -0700144
145 // all glyph / vertex offsets are into these pools.
146 unsigned char* fVertices;
147 GrGlyph::PackedID* fGlyphIDs;
148 Run* fRuns;
149
joshualitt53b5f442015-04-13 06:33:59 -0700150 struct Key {
151 Key() {
152 memset(this, 0, sizeof(Key));
153 }
154 uint32_t fUniqueID;
155 SkPaint::Style fStyle;
156 bool fHasBlur;
157
158 bool operator==(const Key& other) const {
159 return 0 == memcmp(this, &other, sizeof(Key));
160 }
161 };
162 Key fKey;
163
164 static const Key& GetKey(const BitmapTextBlob& blob) {
165 return blob.fKey;
joshualittb7133be2015-04-08 09:08:31 -0700166 }
167
joshualitt53b5f442015-04-13 06:33:59 -0700168 static uint32_t Hash(const Key& key) {
169 return SkChecksum::Murmur3(&key, sizeof(Key));
joshualitt1d89e8d2015-04-01 12:40:54 -0700170 }
171
joshualittb7133be2015-04-08 09:08:31 -0700172 void operator delete(void* p) {
173 BitmapTextBlob* blob = reinterpret_cast<BitmapTextBlob*>(p);
174 blob->fPool->release(p);
175 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700176 void* operator new(size_t) {
177 SkFAIL("All blobs are created by placement new.");
178 return sk_malloc_throw(0);
179 }
180
181 void* operator new(size_t, void* p) { return p; }
182 void operator delete(void* target, void* placement) {
183 ::operator delete(target, placement);
184 }
185 };
186
187 typedef BitmapTextBlob::Run Run;
188 typedef Run::SubRunInfo PerSubRunInfo;
189
joshualitt1d89e8d2015-04-01 12:40:54 -0700190 void appendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left, int top,
joshualitteef5b3e2015-04-03 08:07:26 -0700191 GrColor color, GrFontScaler*, const SkIRect& clipRect);
joshualitt9a27e632015-04-06 10:53:36 -0700192
193 inline void flushRunAsPaths(const SkTextBlob::RunIterator&, const SkPaint&, SkDrawFilter*,
194 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
195 SkScalar y);
196 inline void flushRun(GrDrawTarget*, GrPipelineBuilder*, BitmapTextBlob*, int run, GrColor,
joshualitt2a0e9f32015-04-13 06:12:21 -0700197 uint8_t paintAlpha, SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700198 inline void flushBigGlyphs(BitmapTextBlob* cacheBlob, GrRenderTarget* rt,
joshualitt2a0e9f32015-04-13 06:12:21 -0700199 const GrPaint& grPaint, const GrClip& clip,
200 SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700201
202 // We have to flush SkTextBlobs differently from drawText / drawPosText
203 void flush(GrDrawTarget*, const SkTextBlob*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&,
204 const GrPaint&, SkDrawFilter*, const GrClip&, const SkMatrix& viewMatrix,
joshualitt2a0e9f32015-04-13 06:12:21 -0700205 const SkIRect& clipBounds, SkScalar x, SkScalar y, SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700206 void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&,
207 const GrPaint&, const GrClip&, const SkMatrix& viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700208
209 void internalDrawText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
210 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
211 SkScalar x, SkScalar y, const SkIRect& clipRect);
212 void internalDrawPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
213 const SkMatrix& viewMatrix,
214 const char text[], size_t byteLength,
215 const SkScalar pos[], int scalarsPerPosition,
216 const SkPoint& offset, const SkIRect& clipRect);
217
218 // sets up the descriptor on the blob and returns a detached cache. Client must attach
219 inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix& viewMatrix);
joshualitt2a0e9f32015-04-13 06:12:21 -0700220 static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
221 const BitmapTextBlob&, const SkPaint&,
joshualitt53b5f442015-04-13 06:33:59 -0700222 const SkMaskFilter::BlurRec&,
joshualitt1d89e8d2015-04-01 12:40:54 -0700223 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
224 void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, const SkMatrix& viewMatrix,
225 const SkTextBlob* blob, SkScalar x, SkScalar y,
226 SkDrawFilter* drawFilter, const SkIRect& clipRect);
227
joshualitt1d89e8d2015-04-01 12:40:54 -0700228 GrBatchTextStrike* fCurrStrike;
joshualittb7133be2015-04-08 09:08:31 -0700229 GrTextBlobCache* fCache;
joshualitt1d89e8d2015-04-01 12:40:54 -0700230
joshualittb7133be2015-04-08 09:08:31 -0700231 friend class GrTextBlobCache;
joshualitt1d89e8d2015-04-01 12:40:54 -0700232 friend class BitmapTextBatch;
233
234 typedef GrTextContext INHERITED;
235};
236
237#endif