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 | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 14 | #include "GrBatchFontCache.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 15 | #include "GrGeometryProcessor.h" |
| 16 | #include "SkDescriptor.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 17 | #include "GrMemoryPool.h" |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 18 | #include "SkMaskFilter.h" |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 19 | #include "SkTextBlob.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 20 | #include "SkTInternalLList.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 21 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 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 | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 31 | static GrAtlasTextContext* Create(GrContext*, SkGpuDevice*, const SkDeviceProperties&, |
| 32 | bool enableDistanceFields); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 33 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 34 | private: |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 35 | GrAtlasTextContext(GrContext*, SkGpuDevice*, const SkDeviceProperties&, |
| 36 | bool enableDistanceFields); |
| 37 | ~GrAtlasTextContext() override {} |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 38 | |
| 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 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 54 | /* |
| 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. |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 63 | */ |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 64 | // TODO Pack these bytes |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 65 | struct BitmapTextBlob : public SkRefCnt { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 66 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(BitmapTextBlob); |
| 67 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 68 | /* |
| 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 | */ |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 91 | struct Run { |
joshualitt | c3c5990 | 2015-04-14 14:33:37 -0700 | [diff] [blame] | 92 | Run() |
| 93 | : fColor(GrColor_ILLEGAL) |
| 94 | , fInitialized(false) |
| 95 | , fDrawAsPaths(false) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 96 | fVertexBounds.setLargestInverted(); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame^] | 97 | // To ensure we always have one subrun, we push back a fresh run here |
| 98 | fSubRunInfo.push_back(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 99 | } |
| 100 | struct SubRunInfo { |
| 101 | SubRunInfo() |
| 102 | : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGeneration) |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 103 | , fVertexStartIndex(0) |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 104 | , fVertexEndIndex(0) |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 105 | , fGlyphStartIndex(0) |
| 106 | , fGlyphEndIndex(0) |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 107 | , fDrawAsDistanceFields(false) {} |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 108 | // Distance field text cannot draw coloremoji, and so has to fall back. However, |
| 109 | // though the distance field text and the coloremoji may share the same run, they |
| 110 | // will have different descriptors. If fOverrideDescriptor is non-NULL, then it |
| 111 | // will be used in place of the run's descriptor to regen texture coords |
| 112 | // TODO we could have a descriptor cache, it would reduce the size of these blobs |
| 113 | // significantly, and then the subrun could just have a refed pointer to the |
| 114 | // correct descriptor. |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 115 | GrBatchAtlas::BulkUseTokenUpdater fBulkUseToken; |
| 116 | uint64_t fAtlasGeneration; |
| 117 | size_t fVertexStartIndex; |
| 118 | size_t fVertexEndIndex; |
| 119 | uint32_t fGlyphStartIndex; |
| 120 | uint32_t fGlyphEndIndex; |
| 121 | SkScalar fTextRatio; // df property |
| 122 | GrMaskFormat fMaskFormat; |
| 123 | bool fDrawAsDistanceFields; // df property |
| 124 | bool fUseLCDText; // df property |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 125 | }; |
joshualitt | c3c5990 | 2015-04-14 14:33:37 -0700 | [diff] [blame] | 126 | |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame^] | 127 | SubRunInfo& push_back() { |
| 128 | // Forward glyph / vertex information to seed the new sub run |
| 129 | SubRunInfo& prevSubRun = fSubRunInfo.back(); |
| 130 | SubRunInfo& newSubRun = fSubRunInfo.push_back(); |
| 131 | newSubRun.fGlyphStartIndex = prevSubRun.fGlyphEndIndex; |
| 132 | newSubRun.fGlyphEndIndex = prevSubRun.fGlyphEndIndex; |
joshualitt | c3c5990 | 2015-04-14 14:33:37 -0700 | [diff] [blame] | 133 | |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame^] | 134 | newSubRun.fVertexStartIndex = prevSubRun.fVertexEndIndex; |
| 135 | newSubRun.fVertexEndIndex = prevSubRun.fVertexEndIndex; |
| 136 | return newSubRun; |
| 137 | } |
| 138 | static const int kMinSubRuns = 1; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 139 | SkAutoTUnref<GrBatchTextStrike> fStrike; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 140 | SkAutoTUnref<SkTypeface> fTypeface; |
| 141 | SkRect fVertexBounds; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame^] | 142 | SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 143 | SkAutoDescriptor fDescriptor; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame^] | 144 | SkAutoTDelete<SkAutoDescriptor> fOverrideDescriptor; // df properties |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 145 | GrColor fColor; |
| 146 | bool fInitialized; |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 147 | bool fDrawAsPaths; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | struct BigGlyph { |
| 151 | BigGlyph(const SkPath& path, int vx, int vy) : fPath(path), fVx(vx), fVy(vy) {} |
| 152 | SkPath fPath; |
| 153 | int fVx; |
| 154 | int fVy; |
| 155 | }; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 156 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 157 | struct Key { |
| 158 | Key() { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 159 | sk_bzero(this, sizeof(Key)); |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 160 | } |
| 161 | uint32_t fUniqueID; |
| 162 | SkPaint::Style fStyle; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 163 | // Color may affect the gamma of the mask we generate, but in a fairly limited way. |
| 164 | // Each color is assigned to on of a fixed number of buckets based on its |
| 165 | // luminance. For each luminance bucket there is a "canonical color" that |
| 166 | // represents the bucket. This functionality is currently only supported for A8 |
| 167 | SkColor fCanonicalColor; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 168 | bool fHasBlur; |
| 169 | |
| 170 | bool operator==(const Key& other) const { |
| 171 | return 0 == memcmp(this, &other, sizeof(Key)); |
| 172 | } |
| 173 | }; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 174 | |
| 175 | struct StrokeInfo { |
| 176 | SkScalar fFrameWidth; |
| 177 | SkScalar fMiterLimit; |
| 178 | SkPaint::Join fJoin; |
| 179 | }; |
| 180 | |
| 181 | enum TextType { |
| 182 | kHasDistanceField_TextType = 0x1, |
| 183 | kHasBitmap_TextType = 0x2, |
| 184 | }; |
| 185 | |
| 186 | // all glyph / vertex offsets are into these pools. |
| 187 | unsigned char* fVertices; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 188 | GrGlyph** fGlyphs; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 189 | Run* fRuns; |
| 190 | GrMemoryPool* fPool; |
| 191 | SkMaskFilter::BlurRec fBlurRec; |
| 192 | StrokeInfo fStrokeInfo; |
| 193 | SkTArray<BigGlyph> fBigGlyphs; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 194 | Key fKey; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 195 | SkMatrix fViewMatrix; |
| 196 | #ifdef SK_DEBUG |
| 197 | mutable SkScalar fTotalXError; |
| 198 | mutable SkScalar fTotalYError; |
| 199 | #endif |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 200 | SkColor fPaintColor; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 201 | SkScalar fX; |
| 202 | SkScalar fY; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 203 | |
| 204 | // We can reuse distance field text, but only if the new viewmatrix would not result in |
| 205 | // a mip change. Because there can be multiple runs in a blob, we track the overall |
| 206 | // maximum minimum scale, and minimum maximum scale, we can support before we need to regen |
| 207 | SkScalar fMaxMinScale; |
| 208 | SkScalar fMinMaxScale; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 209 | int fRunCount; |
| 210 | uint8_t fTextType; |
| 211 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 212 | BitmapTextBlob() |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 213 | : fMaxMinScale(-SK_ScalarMax) |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 214 | , fMinMaxScale(SK_ScalarMax) |
| 215 | , fTextType(0) {} |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 216 | |
joshualitt | 12c20e4 | 2015-04-22 11:24:20 -0700 | [diff] [blame] | 217 | ~BitmapTextBlob() override { |
| 218 | for (int i = 0; i < fRunCount; i++) { |
| 219 | fRuns[i].~Run(); |
| 220 | } |
| 221 | } |
| 222 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 223 | static const Key& GetKey(const BitmapTextBlob& blob) { |
| 224 | return blob.fKey; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 225 | } |
| 226 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 227 | static uint32_t Hash(const Key& key) { |
| 228 | return SkChecksum::Murmur3(&key, sizeof(Key)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 229 | } |
| 230 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 231 | void operator delete(void* p) { |
| 232 | BitmapTextBlob* blob = reinterpret_cast<BitmapTextBlob*>(p); |
| 233 | blob->fPool->release(p); |
| 234 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 235 | void* operator new(size_t) { |
| 236 | SkFAIL("All blobs are created by placement new."); |
| 237 | return sk_malloc_throw(0); |
| 238 | } |
| 239 | |
| 240 | void* operator new(size_t, void* p) { return p; } |
| 241 | void operator delete(void* target, void* placement) { |
| 242 | ::operator delete(target, placement); |
| 243 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 244 | |
| 245 | bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); } |
| 246 | bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); } |
| 247 | void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; } |
| 248 | void setHasBitmap() { fTextType |= kHasBitmap_TextType; } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | typedef BitmapTextBlob::Run Run; |
| 252 | typedef Run::SubRunInfo PerSubRunInfo; |
| 253 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 254 | inline bool canDrawAsDistanceFields(const SkPaint&, const SkMatrix& viewMatrix); |
| 255 | BitmapTextBlob* setupDFBlob(int glyphCount, const SkPaint& origPaint, |
| 256 | const SkMatrix& viewMatrix, SkGlyphCache** cache, |
| 257 | SkPaint* dfPaint, SkScalar* textRatio); |
| 258 | void bmpAppendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left, int top, |
| 259 | GrColor color, GrFontScaler*, const SkIRect& clipRect); |
| 260 | bool dfAppendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, SkScalar sx, SkScalar sy, |
| 261 | GrColor color, GrFontScaler*, const SkIRect& clipRect, SkScalar textRatio, |
| 262 | const SkMatrix& viewMatrix); |
| 263 | inline void appendGlyphPath(BitmapTextBlob* blob, GrGlyph* glyph, |
| 264 | GrFontScaler* scaler, int x, int y); |
| 265 | inline void appendGlyphCommon(BitmapTextBlob*, Run*, Run::SubRunInfo*, |
| 266 | const SkRect& positions, GrColor color, |
| 267 | size_t vertexStride, bool useVertexColor, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 268 | GrGlyph*); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 269 | |
| 270 | inline void flushRunAsPaths(const SkTextBlob::RunIterator&, const SkPaint&, SkDrawFilter*, |
| 271 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, |
| 272 | SkScalar y); |
| 273 | inline void flushRun(GrDrawTarget*, GrPipelineBuilder*, BitmapTextBlob*, int run, GrColor, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 274 | SkScalar transX, SkScalar transY, const SkPaint&); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 275 | inline void flushBigGlyphs(BitmapTextBlob* cacheBlob, GrRenderTarget* rt, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 276 | const GrPaint& grPaint, const GrClip& clip, |
| 277 | SkScalar transX, SkScalar transY); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 278 | |
| 279 | // We have to flush SkTextBlobs differently from drawText / drawPosText |
| 280 | void flush(GrDrawTarget*, const SkTextBlob*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&, |
| 281 | const GrPaint&, SkDrawFilter*, const GrClip&, const SkMatrix& viewMatrix, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 282 | const SkIRect& clipBounds, SkScalar x, SkScalar y, SkScalar transX, SkScalar transY); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 283 | void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 284 | const GrPaint&, const GrClip&); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 285 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 286 | // A helper for drawing BitmapText in a run of distance fields |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 287 | inline void fallbackDrawPosText(BitmapTextBlob*, int runIndex, |
| 288 | GrRenderTarget*, const GrClip&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 289 | const GrPaint&, |
| 290 | const SkPaint&, const SkMatrix& viewMatrix, |
| 291 | const SkTDArray<char>& fallbackTxt, |
| 292 | const SkTDArray<SkScalar>& fallbackPos, |
| 293 | int scalarsPerPosition, |
| 294 | const SkPoint& offset, |
| 295 | const SkIRect& clipRect); |
| 296 | |
| 297 | void internalDrawBMPText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 298 | GrColor color, const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 299 | const char text[], size_t byteLength, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 300 | SkScalar x, SkScalar y, const SkIRect& clipRect); |
| 301 | void internalDrawBMPPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
| 302 | GrColor color, const SkMatrix& viewMatrix, |
| 303 | const char text[], size_t byteLength, |
| 304 | const SkScalar pos[], int scalarsPerPosition, |
| 305 | const SkPoint& offset, const SkIRect& clipRect); |
| 306 | |
| 307 | void internalDrawDFText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
| 308 | GrColor color, const SkMatrix& viewMatrix, |
| 309 | const char text[], size_t byteLength, |
| 310 | SkScalar x, SkScalar y, const SkIRect& clipRect, |
| 311 | SkScalar textRatio, |
| 312 | SkTDArray<char>* fallbackTxt, |
| 313 | SkTDArray<SkScalar>* fallbackPos, |
| 314 | SkPoint* offset, const SkPaint& origPaint); |
| 315 | void internalDrawDFPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
| 316 | GrColor color, const SkMatrix& viewMatrix, |
| 317 | const char text[], size_t byteLength, |
| 318 | const SkScalar pos[], int scalarsPerPosition, |
| 319 | const SkPoint& offset, const SkIRect& clipRect, |
| 320 | SkScalar textRatio, |
| 321 | SkTDArray<char>* fallbackTxt, |
| 322 | SkTDArray<SkScalar>* fallbackPos); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 323 | |
| 324 | // 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] | 325 | inline static GrColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 326 | inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix* viewMatrix, bool noGamma); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 327 | static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY, |
| 328 | const BitmapTextBlob&, const SkPaint&, |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 329 | const SkMaskFilter::BlurRec&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 330 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 331 | void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, GrColor, |
| 332 | const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 333 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 334 | SkDrawFilter* drawFilter, const SkIRect& clipRect, GrRenderTarget*, |
| 335 | const GrClip&, const GrPaint&); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 336 | inline static bool HasLCD(const SkTextBlob*); |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 337 | inline void initDistanceFieldPaint(BitmapTextBlob*, SkPaint*, SkScalar* textRatio, |
| 338 | const SkMatrix&); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 339 | |
| 340 | // Distance field text needs this table to compute a value for use in the fragment shader. |
| 341 | // Because the GrAtlasTextContext can go out of scope before the final flush, this needs to be |
| 342 | // refcnted and malloced |
| 343 | struct DistanceAdjustTable : public SkNVRefCnt<DistanceAdjustTable> { |
| 344 | DistanceAdjustTable(float gamma) { this->buildDistanceAdjustTable(gamma); } |
| 345 | ~DistanceAdjustTable() { SkDELETE_ARRAY(fTable); } |
| 346 | |
| 347 | void buildDistanceAdjustTable(float gamma); |
| 348 | |
| 349 | SkScalar& operator[] (int i) { |
| 350 | return fTable[i]; |
| 351 | } |
| 352 | |
| 353 | const SkScalar& operator[] (int i) const { |
| 354 | return fTable[i]; |
| 355 | } |
| 356 | |
| 357 | SkScalar* fTable; |
| 358 | }; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 359 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 360 | GrBatchTextStrike* fCurrStrike; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 361 | GrTextBlobCache* fCache; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 362 | bool fEnableDFRendering; |
| 363 | SkAutoTUnref<DistanceAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 364 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 365 | friend class GrTextBlobCache; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 366 | friend class BitmapTextBatch; |
| 367 | |
| 368 | typedef GrTextContext INHERITED; |
| 369 | }; |
| 370 | |
| 371 | #endif |