joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 13 | #include "GrBatchAtlas.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 14 | #include "GrGeometryProcessor.h" |
| 15 | #include "SkDescriptor.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 16 | #include "GrMemoryPool.h" |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 17 | #include "SkMaskFilter.h" |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 18 | #include "SkTextBlob.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 19 | #include "SkTInternalLList.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 20 | |
| 21 | class GrBatchTextStrike; |
| 22 | class GrPipelineBuilder; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 23 | class GrTextBlobCache; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * This class implements GrTextContext using standard bitmap fonts, and can also process textblobs. |
| 27 | * TODO replace GrBitmapTextContext |
| 28 | */ |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 29 | class GrAtlasTextContext : public GrTextContext { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 30 | public: |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 31 | static GrAtlasTextContext* Create(GrContext*, SkGpuDevice*, const SkDeviceProperties&); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 32 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 33 | private: |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 34 | GrAtlasTextContext(GrContext*, SkGpuDevice*, const SkDeviceProperties&); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 35 | |
| 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 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 51 | /* |
| 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. |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 60 | */ |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 61 | // TODO Pack these bytes |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 62 | struct BitmapTextBlob : public SkRefCnt { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 63 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(BitmapTextBlob); |
| 64 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 65 | /* |
| 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 | */ |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 88 | struct Run { |
joshualitt | c3c5990 | 2015-04-14 14:33:37 -0700 | [diff] [blame^] | 89 | Run() |
| 90 | : fColor(GrColor_ILLEGAL) |
| 91 | , fInitialized(false) |
| 92 | , fDrawAsPaths(false) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 93 | fVertexBounds.setLargestInverted(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 94 | } |
| 95 | struct SubRunInfo { |
| 96 | SubRunInfo() |
| 97 | : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGeneration) |
| 98 | , fGlyphStartIndex(0) |
| 99 | , fGlyphEndIndex(0) |
| 100 | , fVertexStartIndex(0) |
| 101 | , fVertexEndIndex(0) {} |
| 102 | GrMaskFormat fMaskFormat; |
| 103 | uint64_t fAtlasGeneration; |
| 104 | uint32_t fGlyphStartIndex; |
| 105 | uint32_t fGlyphEndIndex; |
| 106 | size_t fVertexStartIndex; |
| 107 | size_t fVertexEndIndex; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 108 | GrBatchAtlas::BulkUseTokenUpdater fBulkUseToken; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 109 | }; |
joshualitt | c3c5990 | 2015-04-14 14:33:37 -0700 | [diff] [blame^] | 110 | |
| 111 | class SubRunInfoArray { |
| 112 | public: |
| 113 | SubRunInfoArray() |
| 114 | : fSubRunCount(0) |
| 115 | , fSubRunAllocation(kMinSubRuns) { |
| 116 | fPtr = reinterpret_cast<SubRunInfo*>(fSubRunStorage.get()); |
| 117 | this->push_back(); |
| 118 | } |
| 119 | |
| 120 | int count() const { return fSubRunCount; } |
| 121 | SubRunInfo& back() { return fPtr[fSubRunCount - 1]; } |
| 122 | SubRunInfo& push_back() { |
| 123 | if (fSubRunCount >= fSubRunAllocation) { |
| 124 | fSubRunAllocation = fSubRunAllocation << 1; |
| 125 | fSubRunStorage.realloc(fSubRunAllocation * sizeof(SubRunInfo)); |
| 126 | fPtr = reinterpret_cast<SubRunInfo*>(fSubRunStorage.get()); |
| 127 | } |
| 128 | SkNEW_PLACEMENT(&fPtr[fSubRunCount], SubRunInfo); |
| 129 | return fPtr[fSubRunCount++]; |
| 130 | } |
| 131 | SubRunInfo& operator[](int index) { |
| 132 | return fPtr[index]; |
| 133 | } |
| 134 | const SubRunInfo& operator[](int index) const { |
| 135 | return fPtr[index]; |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | static const int kMinSubRuns = 1; |
| 140 | static const int kMinSubRunStorage = kMinSubRuns * sizeof(SubRunInfo); |
| 141 | SkAutoSTMalloc<kMinSubRunStorage, unsigned char> fSubRunStorage; |
| 142 | int fSubRunCount; |
| 143 | int fSubRunAllocation; |
| 144 | SubRunInfo* fPtr; |
| 145 | }; |
| 146 | SubRunInfoArray fSubRunInfo; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 147 | SkAutoDescriptor fDescriptor; |
| 148 | SkAutoTUnref<SkTypeface> fTypeface; |
| 149 | SkRect fVertexBounds; |
| 150 | GrColor fColor; |
| 151 | bool fInitialized; |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 152 | bool fDrawAsPaths; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | struct BigGlyph { |
| 156 | BigGlyph(const SkPath& path, int vx, int vy) : fPath(path), fVx(vx), fVy(vy) {} |
| 157 | SkPath fPath; |
| 158 | int fVx; |
| 159 | int fVy; |
| 160 | }; |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 161 | #ifdef SK_DEBUG |
| 162 | mutable SkScalar fTotalXError; |
| 163 | mutable SkScalar fTotalYError; |
| 164 | #endif |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 165 | SkColor fPaintColor; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 166 | SkTArray<BigGlyph> fBigGlyphs; |
| 167 | SkMatrix fViewMatrix; |
| 168 | SkScalar fX; |
| 169 | SkScalar fY; |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 170 | int fRunCount; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 171 | SkMaskFilter::BlurRec fBlurRec; |
| 172 | struct StrokeInfo { |
| 173 | SkScalar fFrameWidth; |
| 174 | SkScalar fMiterLimit; |
| 175 | SkPaint::Join fJoin; |
| 176 | }; |
| 177 | StrokeInfo fStrokeInfo; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 178 | GrMemoryPool* fPool; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 179 | |
| 180 | // all glyph / vertex offsets are into these pools. |
| 181 | unsigned char* fVertices; |
| 182 | GrGlyph::PackedID* fGlyphIDs; |
| 183 | Run* fRuns; |
| 184 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 185 | struct Key { |
| 186 | Key() { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 187 | sk_bzero(this, sizeof(Key)); |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 188 | } |
| 189 | uint32_t fUniqueID; |
| 190 | SkPaint::Style fStyle; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 191 | // Color may affect the gamma of the mask we generate, but in a fairly limited way. |
| 192 | // Each color is assigned to on of a fixed number of buckets based on its |
| 193 | // luminance. For each luminance bucket there is a "canonical color" that |
| 194 | // represents the bucket. This functionality is currently only supported for A8 |
| 195 | SkColor fCanonicalColor; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 196 | bool fHasBlur; |
| 197 | |
| 198 | bool operator==(const Key& other) const { |
| 199 | return 0 == memcmp(this, &other, sizeof(Key)); |
| 200 | } |
| 201 | }; |
| 202 | Key fKey; |
| 203 | |
| 204 | static const Key& GetKey(const BitmapTextBlob& blob) { |
| 205 | return blob.fKey; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 206 | } |
| 207 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 208 | static uint32_t Hash(const Key& key) { |
| 209 | return SkChecksum::Murmur3(&key, sizeof(Key)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 210 | } |
| 211 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 212 | void operator delete(void* p) { |
| 213 | BitmapTextBlob* blob = reinterpret_cast<BitmapTextBlob*>(p); |
| 214 | blob->fPool->release(p); |
| 215 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 216 | void* operator new(size_t) { |
| 217 | SkFAIL("All blobs are created by placement new."); |
| 218 | return sk_malloc_throw(0); |
| 219 | } |
| 220 | |
| 221 | void* operator new(size_t, void* p) { return p; } |
| 222 | void operator delete(void* target, void* placement) { |
| 223 | ::operator delete(target, placement); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | typedef BitmapTextBlob::Run Run; |
| 228 | typedef Run::SubRunInfo PerSubRunInfo; |
| 229 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 230 | void appendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left, int top, |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 231 | GrColor color, GrFontScaler*, const SkIRect& clipRect); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 232 | |
| 233 | inline void flushRunAsPaths(const SkTextBlob::RunIterator&, const SkPaint&, SkDrawFilter*, |
| 234 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, |
| 235 | SkScalar y); |
| 236 | inline void flushRun(GrDrawTarget*, GrPipelineBuilder*, BitmapTextBlob*, int run, GrColor, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 237 | uint8_t paintAlpha, SkScalar transX, SkScalar transY); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 238 | inline void flushBigGlyphs(BitmapTextBlob* cacheBlob, GrRenderTarget* rt, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 239 | const GrPaint& grPaint, const GrClip& clip, |
| 240 | SkScalar transX, SkScalar transY); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 241 | |
| 242 | // We have to flush SkTextBlobs differently from drawText / drawPosText |
| 243 | void flush(GrDrawTarget*, const SkTextBlob*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&, |
| 244 | const GrPaint&, SkDrawFilter*, const GrClip&, const SkMatrix& viewMatrix, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 245 | const SkIRect& clipBounds, SkScalar x, SkScalar y, SkScalar transX, SkScalar transY); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 246 | void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&, |
| 247 | const GrPaint&, const GrClip&, const SkMatrix& viewMatrix); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 248 | |
| 249 | void internalDrawText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 250 | GrColor color, const SkMatrix& viewMatrix, |
| 251 | const char text[], size_t byteLength, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 252 | SkScalar x, SkScalar y, const SkIRect& clipRect); |
| 253 | void internalDrawPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 254 | GrColor color, const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 255 | const char text[], size_t byteLength, |
| 256 | const SkScalar pos[], int scalarsPerPosition, |
| 257 | const SkPoint& offset, const SkIRect& clipRect); |
| 258 | |
| 259 | // sets up the descriptor on the blob and returns a detached cache. Client must attach |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 260 | inline static GrColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 261 | inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix& viewMatrix); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 262 | static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY, |
| 263 | const BitmapTextBlob&, const SkPaint&, |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 264 | const SkMaskFilter::BlurRec&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 265 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 266 | void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, GrColor, |
| 267 | const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 268 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 269 | SkDrawFilter* drawFilter, const SkIRect& clipRect); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 270 | inline static bool HasLCD(const SkTextBlob*); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 271 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 272 | GrBatchTextStrike* fCurrStrike; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 273 | GrTextBlobCache* fCache; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 274 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 275 | friend class GrTextBlobCache; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 276 | friend class BitmapTextBatch; |
| 277 | |
| 278 | typedef GrTextContext INHERITED; |
| 279 | }; |
| 280 | |
| 281 | #endif |