blob: 7859c04e23cb5e6bf530d640e996488245d6e859 [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"
joshualitt9a27e632015-04-06 10:53:36 -070017#include "SkTextBlob.h"
joshualittb7133be2015-04-08 09:08:31 -070018#include "SkTInternalLList.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070019
20class GrBatchTextStrike;
21class GrPipelineBuilder;
joshualittb7133be2015-04-08 09:08:31 -070022class GrTextBlobCache;
joshualitt1d89e8d2015-04-01 12:40:54 -070023
24/*
25 * This class implements GrTextContext using standard bitmap fonts, and can also process textblobs.
26 * TODO replace GrBitmapTextContext
27 */
joshualittdbd35932015-04-02 09:19:04 -070028class GrAtlasTextContext : public GrTextContext {
joshualitt1d89e8d2015-04-01 12:40:54 -070029public:
joshualittdbd35932015-04-02 09:19:04 -070030 static GrAtlasTextContext* Create(GrContext*, SkGpuDevice*, const SkDeviceProperties&);
joshualitt1d89e8d2015-04-01 12:40:54 -070031
joshualitt1d89e8d2015-04-01 12:40:54 -070032private:
joshualittdbd35932015-04-02 09:19:04 -070033 GrAtlasTextContext(GrContext*, SkGpuDevice*, const SkDeviceProperties&);
joshualitt1d89e8d2015-04-01 12:40:54 -070034
35 bool canDraw(const GrRenderTarget*, const GrClip&, const GrPaint&,
36 const SkPaint&, const SkMatrix& viewMatrix) override;
37
38 void onDrawText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
39 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
40 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) override;
41 void onDrawPosText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
42 const SkMatrix& viewMatrix,
43 const char text[], size_t byteLength,
44 const SkScalar pos[], int scalarsPerPosition,
45 const SkPoint& offset, const SkIRect& regionClipBounds) override;
46 void drawTextBlob(GrRenderTarget*, const GrClip&, const SkPaint&,
47 const SkMatrix& viewMatrix, const SkTextBlob*, SkScalar x, SkScalar y,
48 SkDrawFilter*, const SkIRect& clipBounds) override;
49
joshualitt1d89e8d2015-04-01 12:40:54 -070050 /*
51 * A BitmapTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
52 * on the GPU. These are initially created with valid positions and colors, but invalid
53 * texture coordinates. The BitmapTextBlob itself has a few Blob-wide properties, and also
54 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
55 * reordered.
56 *
57 * The only thing(aside from a memcopy) required to flush a BitmapTextBlob is to ensure that
58 * the GrAtlas will not evict anything the Blob needs.
59 * TODO this is currently a bug
60 */
61 struct BitmapTextBlob : public SkRefCnt {
joshualittb7133be2015-04-08 09:08:31 -070062 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BitmapTextBlob);
63
joshualitt9a27e632015-04-06 10:53:36 -070064 /*
65 * Each Run inside of the blob can have its texture coordinates regenerated if required.
66 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
67 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
68 * this at a more fine grained level, but its not clear if this is worth it, as evictions
69 * should be fairly rare.
70 *
71 * One additional point, each run can contain glyphs with any of the three mask formats.
72 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
73 * a new subrun each time the mask format changes in a run. In theory, a run can have as
74 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
75 * practice, the vast majority of runs have only a single subrun.
76 *
77 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
78 * handle, we have a bit to mark the run as flusahable via rendering as paths. It is worth
79 * pointing. It would be a bit expensive to figure out ahead of time whether or not a run
80 * can flush in this manner, so we always allocate vertices for the run, regardless of
81 * whether or not it is too large. The benefit of this strategy is that we can always reuse
82 * a blob allocation regardless of viewmatrix changes. We could store positions for these
83 * glyphs. However, its not clear if this is a win because we'd still have to either go the
84 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
85 * would greatly increase the memory of these cached items.
86 */
joshualitt1d89e8d2015-04-01 12:40:54 -070087
88 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;
135 SkPaint::Style fStyle;
joshualitt9a27e632015-04-06 10:53:36 -0700136 int fRunCount;
joshualittb7133be2015-04-08 09:08:31 -0700137 uint32_t fUniqueID;
138 GrMemoryPool* fPool;
joshualitt1d89e8d2015-04-01 12:40:54 -0700139
140 // all glyph / vertex offsets are into these pools.
141 unsigned char* fVertices;
142 GrGlyph::PackedID* fGlyphIDs;
143 Run* fRuns;
144
joshualittb7133be2015-04-08 09:08:31 -0700145 static const uint32_t& GetKey(const BitmapTextBlob& blob) {
146 return blob.fUniqueID;
147 }
148
joshualitt1d89e8d2015-04-01 12:40:54 -0700149 static uint32_t Hash(const uint32_t& key) {
150 return SkChecksum::Mix(key);
151 }
152
joshualittb7133be2015-04-08 09:08:31 -0700153 void operator delete(void* p) {
154 BitmapTextBlob* blob = reinterpret_cast<BitmapTextBlob*>(p);
155 blob->fPool->release(p);
156 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700157 void* operator new(size_t) {
158 SkFAIL("All blobs are created by placement new.");
159 return sk_malloc_throw(0);
160 }
161
162 void* operator new(size_t, void* p) { return p; }
163 void operator delete(void* target, void* placement) {
164 ::operator delete(target, placement);
165 }
166 };
167
168 typedef BitmapTextBlob::Run Run;
169 typedef Run::SubRunInfo PerSubRunInfo;
170
joshualitt1d89e8d2015-04-01 12:40:54 -0700171 void appendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left, int top,
joshualitteef5b3e2015-04-03 08:07:26 -0700172 GrColor color, GrFontScaler*, const SkIRect& clipRect);
joshualitt9a27e632015-04-06 10:53:36 -0700173
174 inline void flushRunAsPaths(const SkTextBlob::RunIterator&, const SkPaint&, SkDrawFilter*,
175 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
176 SkScalar y);
177 inline void flushRun(GrDrawTarget*, GrPipelineBuilder*, BitmapTextBlob*, int run, GrColor,
joshualitt2a0e9f32015-04-13 06:12:21 -0700178 uint8_t paintAlpha, SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700179 inline void flushBigGlyphs(BitmapTextBlob* cacheBlob, GrRenderTarget* rt,
joshualitt2a0e9f32015-04-13 06:12:21 -0700180 const GrPaint& grPaint, const GrClip& clip,
181 SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700182
183 // We have to flush SkTextBlobs differently from drawText / drawPosText
184 void flush(GrDrawTarget*, const SkTextBlob*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&,
185 const GrPaint&, SkDrawFilter*, const GrClip&, const SkMatrix& viewMatrix,
joshualitt2a0e9f32015-04-13 06:12:21 -0700186 const SkIRect& clipBounds, SkScalar x, SkScalar y, SkScalar transX, SkScalar transY);
joshualitt9a27e632015-04-06 10:53:36 -0700187 void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&,
188 const GrPaint&, const GrClip&, const SkMatrix& viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700189
190 void internalDrawText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
191 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
192 SkScalar x, SkScalar y, const SkIRect& clipRect);
193 void internalDrawPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&,
194 const SkMatrix& viewMatrix,
195 const char text[], size_t byteLength,
196 const SkScalar pos[], int scalarsPerPosition,
197 const SkPoint& offset, const SkIRect& clipRect);
198
199 // sets up the descriptor on the blob and returns a detached cache. Client must attach
200 inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix& viewMatrix);
joshualitt2a0e9f32015-04-13 06:12:21 -0700201 static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
202 const BitmapTextBlob&, const SkPaint&,
joshualitt1d89e8d2015-04-01 12:40:54 -0700203 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
204 void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, const SkMatrix& viewMatrix,
205 const SkTextBlob* blob, SkScalar x, SkScalar y,
206 SkDrawFilter* drawFilter, const SkIRect& clipRect);
207
joshualitt1d89e8d2015-04-01 12:40:54 -0700208 GrBatchTextStrike* fCurrStrike;
joshualittb7133be2015-04-08 09:08:31 -0700209 GrTextBlobCache* fCache;
joshualitt1d89e8d2015-04-01 12:40:54 -0700210
joshualittb7133be2015-04-08 09:08:31 -0700211 friend class GrTextBlobCache;
joshualitt1d89e8d2015-04-01 12:40:54 -0700212 friend class BitmapTextBatch;
213
214 typedef GrTextContext INHERITED;
215};
216
217#endif