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" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 15 | #include "SkTextBlobRunIterator.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. |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 30 | */ |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 31 | class GrAtlasTextContext : public GrTextContext { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 32 | public: |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 33 | static GrAtlasTextContext* Create(GrContext*, const SkSurfaceProps&); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 34 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 35 | private: |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 36 | GrAtlasTextContext(GrContext*, const SkSurfaceProps&); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 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 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 42 | void onDrawText(GrDrawContext*, GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 43 | const SkMatrix& viewMatrix, const char text[], size_t byteLength, |
| 44 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) override; |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 45 | void onDrawPosText(GrDrawContext*, GrRenderTarget*, const GrClip&, const GrPaint&, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 46 | const SkPaint&, const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 47 | const char text[], size_t byteLength, |
| 48 | const SkScalar pos[], int scalarsPerPosition, |
| 49 | const SkPoint& offset, const SkIRect& regionClipBounds) override; |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 50 | void drawTextBlob(GrDrawContext*, GrRenderTarget*, const GrClip&, const SkPaint&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 51 | const SkMatrix& viewMatrix, const SkTextBlob*, SkScalar x, SkScalar y, |
| 52 | SkDrawFilter*, const SkIRect& clipBounds) override; |
| 53 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 54 | typedef GrAtlasTextBlob::Run Run; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 55 | typedef Run::SubRunInfo PerSubRunInfo; |
| 56 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 57 | inline bool canDrawAsDistanceFields(const SkPaint&, const SkMatrix& viewMatrix); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 58 | GrAtlasTextBlob* setupDFBlob(int glyphCount, const SkPaint& origPaint, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 59 | const SkMatrix& viewMatrix, SkPaint* dfPaint, |
| 60 | SkScalar* textRatio); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 61 | void bmpAppendGlyph(GrAtlasTextBlob*, int runIndex, const SkGlyph&, int left, int top, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 62 | GrColor color, GrFontScaler*, const SkIRect& clipRect); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 63 | bool dfAppendGlyph(GrAtlasTextBlob*, int runIndex, const SkGlyph&, SkScalar sx, SkScalar sy, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 64 | GrColor color, GrFontScaler*, const SkIRect& clipRect, SkScalar textRatio, |
| 65 | const SkMatrix& viewMatrix); |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 66 | inline void appendGlyphPath(GrAtlasTextBlob*, GrGlyph*, GrFontScaler*, const SkGlyph&, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 67 | SkScalar x, SkScalar y, SkScalar scale = 1.0f, |
| 68 | bool applyVM = false); |
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 | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 74 | inline void flushRunAsPaths(GrDrawContext*, |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 75 | const SkTextBlobRunIterator&, const GrClip& clip, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 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&); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 83 | inline void flushRun(GrDrawContext*, GrPipelineBuilder*, GrAtlasTextBlob*, int run, GrColor, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 84 | SkScalar transX, SkScalar transY, const SkPaint&); |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 85 | inline void flushBigGlyphs(GrAtlasTextBlob* cacheBlob, GrDrawContext*, |
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 |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 90 | void flush(const SkTextBlob*, GrAtlasTextBlob*, GrDrawContext*, 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); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 94 | void flush(GrAtlasTextBlob*, GrDrawContext*, 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, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 99 | GrRenderTarget*, const GrClip&, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 100 | const SkPaint&, const SkMatrix& viewMatrix, |
| 101 | const SkTDArray<char>& fallbackTxt, |
| 102 | const SkTDArray<SkScalar>& fallbackPos, |
| 103 | int scalarsPerPosition, |
| 104 | const SkPoint& offset, |
| 105 | const SkIRect& clipRect); |
| 106 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 107 | void internalDrawBMPText(GrAtlasTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 108 | GrColor color, const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 109 | const char text[], size_t byteLength, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 110 | SkScalar x, SkScalar y, const SkIRect& clipRect); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 111 | void internalDrawBMPPosText(GrAtlasTextBlob*, int runIndex, SkGlyphCache*, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 112 | GrColor color, const SkMatrix& viewMatrix, |
| 113 | const char text[], size_t byteLength, |
| 114 | const SkScalar pos[], int scalarsPerPosition, |
| 115 | const SkPoint& offset, const SkIRect& clipRect); |
| 116 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 117 | void internalDrawDFText(GrAtlasTextBlob*, int runIndex, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 118 | GrColor color, const SkMatrix& viewMatrix, |
| 119 | const char text[], size_t byteLength, |
| 120 | SkScalar x, SkScalar y, const SkIRect& clipRect, |
| 121 | SkScalar textRatio, |
| 122 | SkTDArray<char>* fallbackTxt, |
| 123 | SkTDArray<SkScalar>* fallbackPos, |
| 124 | SkPoint* offset, const SkPaint& origPaint); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 125 | void internalDrawDFPosText(GrAtlasTextBlob*, int runIndex, const SkPaint&, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 126 | GrColor color, const SkMatrix& viewMatrix, |
| 127 | const char text[], size_t byteLength, |
| 128 | const SkScalar pos[], int scalarsPerPosition, |
| 129 | const SkPoint& offset, const SkIRect& clipRect, |
| 130 | SkScalar textRatio, |
| 131 | SkTDArray<char>* fallbackTxt, |
| 132 | SkTDArray<SkScalar>* fallbackPos); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 133 | |
| 134 | // 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] | 135 | inline static GrColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 136 | inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix* viewMatrix, bool noGamma); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 137 | static inline bool MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 138 | const GrAtlasTextBlob&, const SkPaint&, GrColor, |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 139 | const SkMaskFilter::BlurRec&, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 140 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 141 | void regenerateTextBlob(GrAtlasTextBlob* bmp, const SkPaint& skPaint, GrColor, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 142 | const SkMatrix& viewMatrix, |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 143 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 144 | SkDrawFilter* drawFilter, const SkIRect& clipRect, GrRenderTarget*, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 145 | const GrClip&); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 146 | inline static bool HasLCD(const SkTextBlob*); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 147 | inline void initDistanceFieldPaint(GrAtlasTextBlob*, SkPaint*, SkScalar* textRatio, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 148 | const SkMatrix&); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 149 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 150 | // Test methods |
| 151 | // 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] | 152 | inline GrAtlasTextBlob* createDrawTextBlob(GrRenderTarget*, const GrClip&, const GrPaint&, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 153 | const SkPaint&, const SkMatrix& viewMatrix, |
| 154 | const char text[], size_t byteLength, |
| 155 | SkScalar x, SkScalar y, |
| 156 | const SkIRect& regionClipBounds); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 157 | inline GrAtlasTextBlob* createDrawPosTextBlob(GrRenderTarget*, const GrClip&, const GrPaint&, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 158 | const SkPaint&, const SkMatrix& viewMatrix, |
| 159 | const char text[], size_t byteLength, |
| 160 | const SkScalar pos[], int scalarsPerPosition, |
| 161 | const SkPoint& offset, |
| 162 | const SkIRect& regionClipBounds); |
| 163 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 164 | // Distance field text needs this table to compute a value for use in the fragment shader. |
| 165 | // Because the GrAtlasTextContext can go out of scope before the final flush, this needs to be |
| 166 | // refcnted and malloced |
| 167 | struct DistanceAdjustTable : public SkNVRefCnt<DistanceAdjustTable> { |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 168 | DistanceAdjustTable() { this->buildDistanceAdjustTable(); } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 169 | ~DistanceAdjustTable() { delete[] fTable; } |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 170 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 171 | const SkScalar& operator[] (int i) const { |
| 172 | return fTable[i]; |
| 173 | } |
| 174 | |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 175 | private: |
| 176 | void buildDistanceAdjustTable(); |
| 177 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 178 | SkScalar* fTable; |
| 179 | }; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 180 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 181 | GrBatchTextStrike* fCurrStrike; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 182 | GrTextBlobCache* fCache; |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 183 | SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 184 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 185 | friend class GrTextBlobCache; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 186 | friend class GrAtlasTextBatch; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 187 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 188 | #ifdef GR_TEST_UTILS |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 189 | DRAW_BATCH_TEST_FRIEND(TextBlobBatch); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 190 | #endif |
| 191 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 192 | typedef GrTextContext INHERITED; |
| 193 | }; |
| 194 | |
| 195 | #endif |