blob: cf2ba6bcc2cb947152e7a31fa2b075275fbf2a41 [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:
joshualitt9bd2daf2015-04-17 09:30:06 -070031 static GrAtlasTextContext* Create(GrContext*, SkGpuDevice*, const SkDeviceProperties&,
32 bool enableDistanceFields);
joshualitt1d89e8d2015-04-01 12:40:54 -070033
joshualitt1d89e8d2015-04-01 12:40:54 -070034private:
joshualitt9bd2daf2015-04-17 09:30:06 -070035 GrAtlasTextContext(GrContext*, SkGpuDevice*, const SkDeviceProperties&,
36 bool enableDistanceFields);
37 ~GrAtlasTextContext() override {}
joshualitt1d89e8d2015-04-01 12:40:54 -070038
39 bool canDraw(const GrRenderTarget*, const GrClip&, const GrPaint&,
40 const SkPaint&, const SkMatrix& viewMatrix) override;
41
42 void onDrawText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
43 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
44 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) override;
45 void onDrawPosText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
46 const SkMatrix& viewMatrix,
47 const char text[], size_t byteLength,
48 const SkScalar pos[], int scalarsPerPosition,
49 const SkPoint& offset, const SkIRect& regionClipBounds) override;
50 void drawTextBlob(GrRenderTarget*, const GrClip&, const SkPaint&,
51 const SkMatrix& viewMatrix, const SkTextBlob*, SkScalar x, SkScalar y,
52 SkDrawFilter*, const SkIRect& clipBounds) override;
53
joshualitt1d89e8d2015-04-01 12:40:54 -070054 /*
55 * A BitmapTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
56 * on the GPU. These are initially created with valid positions and colors, but invalid
57 * texture coordinates. The BitmapTextBlob itself has a few Blob-wide properties, and also
58 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
59 * reordered.
60 *
61 * The only thing(aside from a memcopy) required to flush a BitmapTextBlob is to ensure that
62 * the GrAtlas will not evict anything the Blob needs.
joshualitt1d89e8d2015-04-01 12:40:54 -070063 */
joshualitt9e36c1a2015-04-14 12:17:27 -070064 // TODO Pack these bytes
joshualitt1d89e8d2015-04-01 12:40:54 -070065 struct BitmapTextBlob : public SkRefCnt {
joshualittb7133be2015-04-08 09:08:31 -070066 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BitmapTextBlob);
67
joshualitt9a27e632015-04-06 10:53:36 -070068 /*
69 * Each Run inside of the blob can have its texture coordinates regenerated if required.
70 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
71 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
72 * this at a more fine grained level, but its not clear if this is worth it, as evictions
73 * should be fairly rare.
74 *
75 * One additional point, each run can contain glyphs with any of the three mask formats.
76 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
77 * a new subrun each time the mask format changes in a run. In theory, a run can have as
78 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
79 * practice, the vast majority of runs have only a single subrun.
80 *
81 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
82 * handle, we have a bit to mark the run as flusahable via rendering as paths. It is worth
83 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
84 * can flush in this manner, so we always allocate vertices for the run, regardless of
85 * whether or not it is too large. The benefit of this strategy is that we can always reuse
86 * a blob allocation regardless of viewmatrix changes. We could store positions for these
87 * glyphs. However, its not clear if this is a win because we'd still have to either go the
88 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
89 * would greatly increase the memory of these cached items.
90 */
joshualitt1d89e8d2015-04-01 12:40:54 -070091 struct Run {
joshualittc3c59902015-04-14 14:33:37 -070092 Run()
93 : fColor(GrColor_ILLEGAL)
94 , fInitialized(false)
95 , fDrawAsPaths(false) {
joshualitt1d89e8d2015-04-01 12:40:54 -070096 fVertexBounds.setLargestInverted();
joshualitt1d89e8d2015-04-01 12:40:54 -070097 }
98 struct SubRunInfo {
99 SubRunInfo()
100 : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGeneration)
101 , fGlyphStartIndex(0)
102 , fGlyphEndIndex(0)
103 , fVertexStartIndex(0)
joshualitt9bd2daf2015-04-17 09:30:06 -0700104 , fVertexEndIndex(0)
105 , fDrawAsDistanceFields(false) {}
joshualitt1d89e8d2015-04-01 12:40:54 -0700106 GrMaskFormat fMaskFormat;
107 uint64_t fAtlasGeneration;
108 uint32_t fGlyphStartIndex;
109 uint32_t fGlyphEndIndex;
110 size_t fVertexStartIndex;
111 size_t fVertexEndIndex;
joshualittb4c507e2015-04-08 08:07:59 -0700112 GrBatchAtlas::BulkUseTokenUpdater fBulkUseToken;
joshualitt9bd2daf2015-04-17 09:30:06 -0700113
114 // distance field properties
115 bool fDrawAsDistanceFields;
116 bool fUseLCDText;
joshualittfec19e12015-04-17 10:32:32 -0700117 // Distance field text cannot draw coloremoji, and so has to fall back. However,
118 // though the distance field text and the coloremoji may share the same run, they
119 // will have different descriptors. If fOverrideDescriptor is non-NULL, then it
120 // will be used in place of the run's descriptor to regen texture coords
121 // TODO we could have a descriptor cache, it would reduce the size of these blobs
122 // significantly, and then the subrun could just have a refed pointer to the
123 // correct descriptor.
124 SkAutoTDelete<SkAutoDescriptor> fOverrideDescriptor;
joshualitt1d89e8d2015-04-01 12:40:54 -0700125 };
joshualittc3c59902015-04-14 14:33:37 -0700126
127 class SubRunInfoArray {
128 public:
129 SubRunInfoArray()
130 : fSubRunCount(0)
131 , fSubRunAllocation(kMinSubRuns) {
joshualittfec19e12015-04-17 10:32:32 -0700132 SK_COMPILE_ASSERT(kMinSubRuns > 0, insufficient_subrun_allocation);
133 // We always seed with one here, so we can assume a valid subrun during
134 // push_back
joshualittc3c59902015-04-14 14:33:37 -0700135 fPtr = reinterpret_cast<SubRunInfo*>(fSubRunStorage.get());
joshualittfec19e12015-04-17 10:32:32 -0700136 SkNEW_PLACEMENT(&fPtr[fSubRunCount++], SubRunInfo);
137 }
138 ~SubRunInfoArray() {
139 for (int i = 0; i < fSubRunCount; i++) {
140 fPtr[i].~SubRunInfo();
141 }
joshualittc3c59902015-04-14 14:33:37 -0700142 }
143
144 int count() const { return fSubRunCount; }
145 SubRunInfo& back() { return fPtr[fSubRunCount - 1]; }
146 SubRunInfo& push_back() {
147 if (fSubRunCount >= fSubRunAllocation) {
148 fSubRunAllocation = fSubRunAllocation << 1;
149 fSubRunStorage.realloc(fSubRunAllocation * sizeof(SubRunInfo));
150 fPtr = reinterpret_cast<SubRunInfo*>(fSubRunStorage.get());
151 }
152 SkNEW_PLACEMENT(&fPtr[fSubRunCount], SubRunInfo);
joshualittfec19e12015-04-17 10:32:32 -0700153
154 // Forward glyph / vertex information to seed the new sub run
155 SubRunInfo& newSubRun = fPtr[fSubRunCount];
156 SubRunInfo& prevSubRun = fPtr[fSubRunCount - 1];
157 newSubRun.fGlyphStartIndex = prevSubRun.fGlyphEndIndex;
158 newSubRun.fGlyphEndIndex = prevSubRun.fGlyphEndIndex;
159
160 newSubRun.fVertexStartIndex = prevSubRun.fVertexEndIndex;
161 newSubRun.fVertexEndIndex = prevSubRun.fVertexEndIndex;
joshualittc3c59902015-04-14 14:33:37 -0700162 return fPtr[fSubRunCount++];
163 }
164 SubRunInfo& operator[](int index) {
165 return fPtr[index];
166 }
167 const SubRunInfo& operator[](int index) const {
168 return fPtr[index];
169 }
170
171 private:
172 static const int kMinSubRuns = 1;
173 static const int kMinSubRunStorage = kMinSubRuns * sizeof(SubRunInfo);
174 SkAutoSTMalloc<kMinSubRunStorage, unsigned char> fSubRunStorage;
175 int fSubRunCount;
176 int fSubRunAllocation;
177 SubRunInfo* fPtr;
178 };
179 SubRunInfoArray fSubRunInfo;
joshualitt1d89e8d2015-04-01 12:40:54 -0700180 SkAutoDescriptor fDescriptor;
181 SkAutoTUnref<SkTypeface> fTypeface;
182 SkRect fVertexBounds;
183 GrColor fColor;
184 bool fInitialized;
joshualitt9a27e632015-04-06 10:53:36 -0700185 bool fDrawAsPaths;
joshualitt1d89e8d2015-04-01 12:40:54 -0700186 };
187
188 struct BigGlyph {
189 BigGlyph(const SkPath& path, int vx, int vy) : fPath(path), fVx(vx), fVy(vy) {}
190 SkPath fPath;
191 int fVx;
192 int fVy;
193 };
joshualitt2a0e9f32015-04-13 06:12:21 -0700194#ifdef SK_DEBUG
195 mutable SkScalar fTotalXError;
196 mutable SkScalar fTotalYError;
197#endif
joshualitt9e36c1a2015-04-14 12:17:27 -0700198 SkColor fPaintColor;
joshualitt1d89e8d2015-04-01 12:40:54 -0700199 SkTArray<BigGlyph> fBigGlyphs;
200 SkMatrix fViewMatrix;
201 SkScalar fX;
202 SkScalar fY;
joshualitt9a27e632015-04-06 10:53:36 -0700203 int fRunCount;
joshualitt53b5f442015-04-13 06:33:59 -0700204 SkMaskFilter::BlurRec fBlurRec;
205 struct StrokeInfo {
206 SkScalar fFrameWidth;
207 SkScalar fMiterLimit;
208 SkPaint::Join fJoin;
209 };
210 StrokeInfo fStrokeInfo;
joshualittb7133be2015-04-08 09:08:31 -0700211 GrMemoryPool* fPool;
joshualitt1d89e8d2015-04-01 12:40:54 -0700212
213 // all glyph / vertex offsets are into these pools.
214 unsigned char* fVertices;
215 GrGlyph::PackedID* fGlyphIDs;
216 Run* fRuns;
217
joshualitt53b5f442015-04-13 06:33:59 -0700218 struct Key {
219 Key() {
joshualitt9e36c1a2015-04-14 12:17:27 -0700220 sk_bzero(this, sizeof(Key));
joshualitt53b5f442015-04-13 06:33:59 -0700221 }
222 uint32_t fUniqueID;
223 SkPaint::Style fStyle;
joshualitt9e36c1a2015-04-14 12:17:27 -0700224 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
225 // Each color is assigned to on of a fixed number of buckets based on its
226 // luminance. For each luminance bucket there is a "canonical color" that
227 // represents the bucket. This functionality is currently only supported for A8
228 SkColor fCanonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700229 bool fHasBlur;
230
231 bool operator==(const Key& other) const {
232 return 0 == memcmp(this, &other, sizeof(Key));
233 }
234 };
235 Key fKey;
236
237 static const Key& GetKey(const BitmapTextBlob& blob) {
238 return blob.fKey;
joshualittb7133be2015-04-08 09:08:31 -0700239 }
240
joshualitt53b5f442015-04-13 06:33:59 -0700241 static uint32_t Hash(const Key& key) {
242 return SkChecksum::Murmur3(&key, sizeof(Key));
joshualitt1d89e8d2015-04-01 12:40:54 -0700243 }
244
joshualittb7133be2015-04-08 09:08:31 -0700245 void operator delete(void* p) {
246 BitmapTextBlob* blob = reinterpret_cast<BitmapTextBlob*>(p);
247 blob->fPool->release(p);
248 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700249 void* operator new(size_t) {
250 SkFAIL("All blobs are created by placement new.");
251 return sk_malloc_throw(0);
252 }
253
254 void* operator new(size_t, void* p) { return p; }
255 void operator delete(void* target, void* placement) {
256 ::operator delete(target, placement);
257 }
258 };
259
260 typedef BitmapTextBlob::Run Run;
261 typedef Run::SubRunInfo PerSubRunInfo;
262
joshualitt9bd2daf2015-04-17 09:30:06 -0700263 inline bool canDrawAsDistanceFields(const SkPaint&, const SkMatrix& viewMatrix);
264 BitmapTextBlob* setupDFBlob(int glyphCount, const SkPaint& origPaint,
265 const SkMatrix& viewMatrix, SkGlyphCache** cache,
266 SkPaint* dfPaint, SkScalar* textRatio);
267 void bmpAppendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left, int top,
268 GrColor color, GrFontScaler*, const SkIRect& clipRect);
269 bool dfAppendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, SkScalar sx, SkScalar sy,
270 GrColor color, GrFontScaler*, const SkIRect& clipRect, SkScalar textRatio,
271 const SkMatrix& viewMatrix);
272 inline void appendGlyphPath(BitmapTextBlob* blob, GrGlyph* glyph,
273 GrFontScaler* scaler, int x, int y);
274 inline void appendGlyphCommon(BitmapTextBlob*, Run*, Run::SubRunInfo*,
275 const SkRect& positions, GrColor color,
276 size_t vertexStride, bool useVertexColor,
277 GrGlyph::PackedID);
joshualitt9a27e632015-04-06 10:53:36 -0700278
279 inline void flushRunAsPaths(const SkTextBlob::RunIterator&, const SkPaint&, SkDrawFilter*,
280 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
281 SkScalar y);
282 inline void flushRun(GrDrawTarget*, GrPipelineBuilder*, BitmapTextBlob*, int run, GrColor,
joshualitt9bd2daf2015-04-17 09:30:06 -0700283 SkScalar transX, SkScalar transY, const SkPaint&);
joshualitt9a27e632015-04-06 10:53:36 -0700284 inline void flushBigGlyphs(BitmapTextBlob* cacheBlob, GrRenderTarget* rt,
joshualitt2a0e9f32015-04-13 06:12:21 -0700285 const GrPaint& grPaint, const GrClip& clip,
286 SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700287
288 // We have to flush SkTextBlobs differently from drawText / drawPosText
289 void flush(GrDrawTarget*, const SkTextBlob*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&,
290 const GrPaint&, SkDrawFilter*, const GrClip&, const SkMatrix& viewMatrix,
joshualitt2a0e9f32015-04-13 06:12:21 -0700291 const SkIRect& clipBounds, SkScalar x, SkScalar y, SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700292 void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&,
joshualitt9bd2daf2015-04-17 09:30:06 -0700293 const GrPaint&, const GrClip&);
joshualitt1d89e8d2015-04-01 12:40:54 -0700294
joshualitt9bd2daf2015-04-17 09:30:06 -0700295 // A helper for drawing BitmapText in a run of distance fields
joshualittfec19e12015-04-17 10:32:32 -0700296 inline void fallbackDrawPosText(BitmapTextBlob*, GrRenderTarget*, const GrClip&,
joshualitt9bd2daf2015-04-17 09:30:06 -0700297 const GrPaint&,
298 const SkPaint&, const SkMatrix& viewMatrix,
299 const SkTDArray<char>& fallbackTxt,
300 const SkTDArray<SkScalar>& fallbackPos,
301 int scalarsPerPosition,
302 const SkPoint& offset,
303 const SkIRect& clipRect);
304
305 void internalDrawBMPText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
joshualitt9e36c1a2015-04-14 12:17:27 -0700306 GrColor color, const SkMatrix& viewMatrix,
joshualitt1d89e8d2015-04-01 12:40:54 -0700307 const char text[], size_t byteLength,
joshualitt9bd2daf2015-04-17 09:30:06 -0700308 SkScalar x, SkScalar y, const SkIRect& clipRect);
309 void internalDrawBMPPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
310 GrColor color, const SkMatrix& viewMatrix,
311 const char text[], size_t byteLength,
312 const SkScalar pos[], int scalarsPerPosition,
313 const SkPoint& offset, const SkIRect& clipRect);
314
315 void internalDrawDFText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
316 GrColor color, const SkMatrix& viewMatrix,
317 const char text[], size_t byteLength,
318 SkScalar x, SkScalar y, const SkIRect& clipRect,
319 SkScalar textRatio,
320 SkTDArray<char>* fallbackTxt,
321 SkTDArray<SkScalar>* fallbackPos,
322 SkPoint* offset, const SkPaint& origPaint);
323 void internalDrawDFPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
324 GrColor color, const SkMatrix& viewMatrix,
325 const char text[], size_t byteLength,
326 const SkScalar pos[], int scalarsPerPosition,
327 const SkPoint& offset, const SkIRect& clipRect,
328 SkScalar textRatio,
329 SkTDArray<char>* fallbackTxt,
330 SkTDArray<SkScalar>* fallbackPos);
joshualitt1d89e8d2015-04-01 12:40:54 -0700331
332 // sets up the descriptor on the blob and returns a detached cache. Client must attach
joshualitt9e36c1a2015-04-14 12:17:27 -0700333 inline static GrColor ComputeCanonicalColor(const SkPaint&, bool lcd);
joshualitt9bd2daf2015-04-17 09:30:06 -0700334 inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix* viewMatrix, bool noGamma);
joshualitt2a0e9f32015-04-13 06:12:21 -0700335 static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
336 const BitmapTextBlob&, const SkPaint&,
joshualitt53b5f442015-04-13 06:33:59 -0700337 const SkMaskFilter::BlurRec&,
joshualitt1d89e8d2015-04-01 12:40:54 -0700338 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
joshualitt9e36c1a2015-04-14 12:17:27 -0700339 void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, GrColor,
340 const SkMatrix& viewMatrix,
joshualitt1d89e8d2015-04-01 12:40:54 -0700341 const SkTextBlob* blob, SkScalar x, SkScalar y,
342 SkDrawFilter* drawFilter, const SkIRect& clipRect);
joshualitt9e36c1a2015-04-14 12:17:27 -0700343 inline static bool HasLCD(const SkTextBlob*);
joshualitt9bd2daf2015-04-17 09:30:06 -0700344 inline void initDistanceFieldPaint(SkPaint*, SkScalar* textRatio, const SkMatrix&);
345
346 // Distance field text needs this table to compute a value for use in the fragment shader.
347 // Because the GrAtlasTextContext can go out of scope before the final flush, this needs to be
348 // refcnted and malloced
349 struct DistanceAdjustTable : public SkNVRefCnt<DistanceAdjustTable> {
350 DistanceAdjustTable(float gamma) { this->buildDistanceAdjustTable(gamma); }
351 ~DistanceAdjustTable() { SkDELETE_ARRAY(fTable); }
352
353 void buildDistanceAdjustTable(float gamma);
354
355 SkScalar& operator[] (int i) {
356 return fTable[i];
357 }
358
359 const SkScalar& operator[] (int i) const {
360 return fTable[i];
361 }
362
363 SkScalar* fTable;
364 };
joshualitt1d89e8d2015-04-01 12:40:54 -0700365
joshualitt1d89e8d2015-04-01 12:40:54 -0700366 GrBatchTextStrike* fCurrStrike;
joshualittb7133be2015-04-08 09:08:31 -0700367 GrTextBlobCache* fCache;
joshualitt9bd2daf2015-04-17 09:30:06 -0700368 bool fEnableDFRendering;
369 SkAutoTUnref<DistanceAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700370
joshualittb7133be2015-04-08 09:08:31 -0700371 friend class GrTextBlobCache;
joshualitt1d89e8d2015-04-01 12:40:54 -0700372 friend class BitmapTextBatch;
373
374 typedef GrTextContext INHERITED;
375};
376
377#endif