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 | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 13 | #include "GrAtlasTextBlob.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 14 | #include "GrGeometryProcessor.h" |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 15 | #include "SkTextBlob.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 16 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 17 | #ifdef GR_TEST_UTILS |
| 18 | #include "GrBatchTest.h" |
| 19 | #endif |
| 20 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame^] | 21 | class GrDrawBatch; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 22 | class GrDrawContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 23 | class GrDrawTarget; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 24 | class GrPipelineBuilder; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 25 | class GrTextBlobCache; |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 26 | class SkGlyph; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * This class implements GrTextContext using standard bitmap fonts, and can also process textblobs. |
| 30 | * TODO replace GrBitmapTextContext |
| 31 | */ |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 32 | class GrAtlasTextContext : public GrTextContext { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 33 | public: |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 34 | static GrAtlasTextContext* Create(GrContext*, GrDrawContext*, const SkSurfaceProps&); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 35 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 36 | private: |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 37 | GrAtlasTextContext(GrContext*, GrDrawContext*, const SkSurfaceProps&); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 38 | ~GrAtlasTextContext() override {} |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 39 | |
| 40 | bool canDraw(const GrRenderTarget*, const GrClip&, const GrPaint&, |
| 41 | const SkPaint&, const SkMatrix& viewMatrix) override; |
| 42 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 43 | void onDrawText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 44 | const SkMatrix& viewMatrix, const char text[], size_t byteLength, |
| 45 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) override; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 46 | void onDrawPosText(GrRenderTarget*, const GrClip&, const GrPaint&, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 47 | const SkPaint&, const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 48 | const char text[], size_t byteLength, |
| 49 | const SkScalar pos[], int scalarsPerPosition, |
| 50 | const SkPoint& offset, const SkIRect& regionClipBounds) override; |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 51 | void drawTextBlob(GrRenderTarget*, const GrClip&, const SkPaint&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 52 | const SkMatrix& viewMatrix, const SkTextBlob*, SkScalar x, SkScalar y, |
| 53 | SkDrawFilter*, const SkIRect& clipBounds) override; |
| 54 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 55 | typedef GrAtlasTextBlob::Run Run; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 56 | typedef Run::SubRunInfo PerSubRunInfo; |
| 57 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 58 | inline bool canDrawAsDistanceFields(const SkPaint&, const SkMatrix& viewMatrix); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 59 | GrAtlasTextBlob* setupDFBlob(int glyphCount, const SkPaint& origPaint, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 60 | const SkMatrix& viewMatrix, SkGlyphCache** cache, |
| 61 | SkPaint* dfPaint, SkScalar* textRatio); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 62 | void bmpAppendGlyph(GrAtlasTextBlob*, int runIndex, const SkGlyph&, int left, int top, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 63 | GrColor color, GrFontScaler*, const SkIRect& clipRect); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 64 | bool dfAppendGlyph(GrAtlasTextBlob*, int runIndex, const SkGlyph&, SkScalar sx, SkScalar sy, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 65 | GrColor color, GrFontScaler*, const SkIRect& clipRect, SkScalar textRatio, |
| 66 | const SkMatrix& viewMatrix); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 67 | inline void appendGlyphPath(GrAtlasTextBlob*, GrGlyph*, GrFontScaler*, const SkGlyph&, |
| 68 | SkScalar x, SkScalar y); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 69 | inline void appendGlyphCommon(GrAtlasTextBlob*, Run*, Run::SubRunInfo*, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 70 | const SkRect& positions, GrColor color, |
| 71 | size_t vertexStride, bool useVertexColor, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 72 | GrGlyph*); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 73 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 74 | inline void flushRunAsPaths(GrRenderTarget*, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 75 | const SkTextBlob::RunIterator&, const GrClip& clip, |
| 76 | const SkPaint&, SkDrawFilter*, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 77 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, |
| 78 | SkScalar y); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame^] | 79 | inline GrDrawBatch* createBatch(GrAtlasTextBlob*, const PerSubRunInfo&, |
| 80 | int glyphCount, int run, int subRun, |
| 81 | GrColor, SkScalar transX, SkScalar transY, |
| 82 | const SkPaint&); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 83 | inline void flushRun(GrPipelineBuilder*, GrAtlasTextBlob*, int run, GrColor, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 84 | SkScalar transX, SkScalar transY, const SkPaint&); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 85 | inline void flushBigGlyphs(GrAtlasTextBlob* cacheBlob, GrRenderTarget*, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 86 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 87 | SkScalar transX, SkScalar transY, const SkIRect& clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 88 | |
| 89 | // We have to flush SkTextBlobs differently from drawText / drawPosText |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 90 | void flush(const SkTextBlob*, GrAtlasTextBlob*, GrRenderTarget*, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 91 | const SkPaint&, const GrPaint&, SkDrawFilter*, const GrClip&, |
| 92 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, SkScalar y, |
| 93 | SkScalar transX, SkScalar transY); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 94 | void flush(GrAtlasTextBlob*, GrRenderTarget*, const SkPaint&, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 95 | const GrPaint&, const GrClip&, const SkIRect& clipBounds); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 96 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 97 | // A helper for drawing BitmapText in a run of distance fields |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 98 | inline void fallbackDrawPosText(GrAtlasTextBlob*, int runIndex, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 99 | GrRenderTarget*, const GrClip&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 100 | const GrPaint&, |
| 101 | const SkPaint&, const SkMatrix& viewMatrix, |
| 102 | const SkTDArray<char>& fallbackTxt, |
| 103 | const SkTDArray<SkScalar>& fallbackPos, |
| 104 | int scalarsPerPosition, |
| 105 | const SkPoint& offset, |
| 106 | const SkIRect& clipRect); |
| 107 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 108 | void internalDrawBMPText(GrAtlasTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 109 | GrColor color, const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 110 | const char text[], size_t byteLength, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 111 | SkScalar x, SkScalar y, const SkIRect& clipRect); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 112 | void internalDrawBMPPosText(GrAtlasTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 113 | GrColor color, const SkMatrix& viewMatrix, |
| 114 | const char text[], size_t byteLength, |
| 115 | const SkScalar pos[], int scalarsPerPosition, |
| 116 | const SkPoint& offset, const SkIRect& clipRect); |
| 117 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 118 | void internalDrawDFText(GrAtlasTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 119 | GrColor color, const SkMatrix& viewMatrix, |
| 120 | const char text[], size_t byteLength, |
| 121 | SkScalar x, SkScalar y, const SkIRect& clipRect, |
| 122 | SkScalar textRatio, |
| 123 | SkTDArray<char>* fallbackTxt, |
| 124 | SkTDArray<SkScalar>* fallbackPos, |
| 125 | SkPoint* offset, const SkPaint& origPaint); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 126 | void internalDrawDFPosText(GrAtlasTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 127 | GrColor color, const SkMatrix& viewMatrix, |
| 128 | const char text[], size_t byteLength, |
| 129 | const SkScalar pos[], int scalarsPerPosition, |
| 130 | const SkPoint& offset, const SkIRect& clipRect, |
| 131 | SkScalar textRatio, |
| 132 | SkTDArray<char>* fallbackTxt, |
| 133 | SkTDArray<SkScalar>* fallbackPos); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 134 | |
| 135 | // 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] | 136 | inline static GrColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 137 | inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix* viewMatrix, bool noGamma); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 138 | static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 139 | const GrAtlasTextBlob&, const SkPaint&, |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 140 | const SkMaskFilter::BlurRec&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 141 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 142 | void regenerateTextBlob(GrAtlasTextBlob* bmp, const SkPaint& skPaint, GrColor, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 143 | const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 144 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 145 | SkDrawFilter* drawFilter, const SkIRect& clipRect, GrRenderTarget*, |
| 146 | const GrClip&, const GrPaint&); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 147 | inline static bool HasLCD(const SkTextBlob*); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 148 | inline void initDistanceFieldPaint(GrAtlasTextBlob*, SkPaint*, SkScalar* textRatio, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 149 | const SkMatrix&); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 150 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 151 | // Test methods |
| 152 | // TODO this is really ugly. It'd be much nicer if positioning could be moved to batch |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 153 | inline GrAtlasTextBlob* createDrawTextBlob(GrRenderTarget*, const GrClip&, const GrPaint&, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 154 | const SkPaint&, const SkMatrix& viewMatrix, |
| 155 | const char text[], size_t byteLength, |
| 156 | SkScalar x, SkScalar y, |
| 157 | const SkIRect& regionClipBounds); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 158 | inline GrAtlasTextBlob* createDrawPosTextBlob(GrRenderTarget*, const GrClip&, const GrPaint&, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 159 | const SkPaint&, const SkMatrix& viewMatrix, |
| 160 | const char text[], size_t byteLength, |
| 161 | const SkScalar pos[], int scalarsPerPosition, |
| 162 | const SkPoint& offset, |
| 163 | const SkIRect& regionClipBounds); |
| 164 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 165 | // Distance field text needs this table to compute a value for use in the fragment shader. |
| 166 | // Because the GrAtlasTextContext can go out of scope before the final flush, this needs to be |
| 167 | // refcnted and malloced |
| 168 | struct DistanceAdjustTable : public SkNVRefCnt<DistanceAdjustTable> { |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 169 | DistanceAdjustTable() { this->buildDistanceAdjustTable(); } |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 170 | ~DistanceAdjustTable() { SkDELETE_ARRAY(fTable); } |
| 171 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 172 | const SkScalar& operator[] (int i) const { |
| 173 | return fTable[i]; |
| 174 | } |
| 175 | |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 176 | private: |
| 177 | void buildDistanceAdjustTable(); |
| 178 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 179 | SkScalar* fTable; |
| 180 | }; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 181 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 182 | GrBatchTextStrike* fCurrStrike; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 183 | GrTextBlobCache* fCache; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 184 | SkAutoTUnref<DistanceAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 185 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 186 | friend class GrTextBlobCache; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 187 | friend class TextBatch; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 188 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 189 | #ifdef GR_TEST_UTILS |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame^] | 190 | DRAW_BATCH_TEST_FRIEND(TextBlobBatch); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 191 | #endif |
| 192 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 193 | typedef GrTextContext INHERITED; |
| 194 | }; |
| 195 | |
| 196 | #endif |