OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrTextContext.h" | 8 #include "GrTextContext.h" |
9 #include "GrBlurUtils.h" | 9 #include "GrBlurUtils.h" |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
11 #include "GrDrawContext.h" | 11 #include "GrDrawContext.h" |
12 #include "GrFontScaler.h" | 12 #include "GrFontScaler.h" |
13 | 13 |
14 #include "SkAutoKern.h" | 14 #include "SkAutoKern.h" |
15 #include "SkDrawFilter.h" | 15 #include "SkDrawFilter.h" |
16 #include "SkDrawProcs.h" | 16 #include "SkDrawProcs.h" |
17 #include "SkGlyphCache.h" | 17 #include "SkGlyphCache.h" |
18 #include "SkGpuDevice.h" | 18 #include "SkGpuDevice.h" |
19 #include "SkTextBlob.h" | 19 #include "SkTextBlob.h" |
20 #include "SkTextMapStateProc.h" | 20 #include "SkTextMapStateProc.h" |
21 #include "SkTextToPathIter.h" | 21 #include "SkTextToPathIter.h" |
22 | 22 |
23 GrTextContext::GrTextContext(GrContext* context, GrDrawContext* drawContext, | 23 GrTextContext::GrTextContext(GrContext* context, const SkSurfaceProps& surfacePr
ops) |
24 const SkSurfaceProps& surfaceProps) | |
25 : fFallbackTextContext(nullptr) | 24 : fFallbackTextContext(nullptr) |
26 , fContext(context) | 25 , fContext(context) |
27 , fSurfaceProps(surfaceProps) | 26 , fSurfaceProps(surfaceProps) { |
28 , fDrawContext(drawContext) { | |
29 } | 27 } |
30 | 28 |
31 GrTextContext::~GrTextContext() { delete fFallbackTextContext; } | 29 GrTextContext::~GrTextContext() { delete fFallbackTextContext; } |
32 | 30 |
33 void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai
nt& paint, | 31 void GrTextContext::drawText(GrDrawContext* dc, GrRenderTarget* rt, |
| 32 const GrClip& clip, const GrPaint& paint, |
34 const SkPaint& skPaint, const SkMatrix& viewMatrix, | 33 const SkPaint& skPaint, const SkMatrix& viewMatrix, |
35 const char text[], size_t byteLength, | 34 const char text[], size_t byteLength, |
36 SkScalar x, SkScalar y, const SkIRect& clipBounds)
{ | 35 SkScalar x, SkScalar y, const SkIRect& clipBounds)
{ |
37 if (fContext->abandoned() || !fDrawContext) { | 36 if (fContext->abandoned()) { |
38 return; | 37 return; |
39 } | 38 } |
40 | 39 |
41 GrTextContext* textContext = this; | 40 GrTextContext* textContext = this; |
42 do { | 41 do { |
43 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { | 42 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
44 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, | 43 textContext->onDrawText(dc, rt, clip, paint, skPaint, viewMatrix, |
45 text, byteLength, x, y, clipBounds); | 44 text, byteLength, x, y, clipBounds); |
46 return; | 45 return; |
47 } | 46 } |
48 textContext = textContext->fFallbackTextContext; | 47 textContext = textContext->fFallbackTextContext; |
49 } while (textContext); | 48 } while (textContext); |
50 | 49 |
51 // fall back to drawing as a path | 50 // fall back to drawing as a path |
52 this->drawTextAsPath(rt, clip, skPaint, viewMatrix, | 51 this->drawTextAsPath(dc, rt, clip, skPaint, viewMatrix, |
53 text, byteLength, x, y, clipBounds); | 52 text, byteLength, x, y, clipBounds); |
54 } | 53 } |
55 | 54 |
56 void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr
Paint& paint, | 55 void GrTextContext::drawPosText(GrDrawContext* dc, GrRenderTarget* rt, |
| 56 const GrClip& clip, const GrPaint& paint, |
57 const SkPaint& skPaint, const SkMatrix& viewMatr
ix, | 57 const SkPaint& skPaint, const SkMatrix& viewMatr
ix, |
58 const char text[], size_t byteLength, | 58 const char text[], size_t byteLength, |
59 const SkScalar pos[], int scalarsPerPosition, | 59 const SkScalar pos[], int scalarsPerPosition, |
60 const SkPoint& offset, const SkIRect& clipBounds
) { | 60 const SkPoint& offset, const SkIRect& clipBounds
) { |
61 if (fContext->abandoned() || !fDrawContext) { | 61 if (fContext->abandoned()) { |
62 return; | 62 return; |
63 } | 63 } |
64 | 64 |
65 GrTextContext* textContext = this; | 65 GrTextContext* textContext = this; |
66 do { | 66 do { |
67 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { | 67 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
68 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, | 68 textContext->onDrawPosText(dc, rt, clip, paint, skPaint, viewMatrix, |
69 text, byteLength, pos, | 69 text, byteLength, pos, |
70 scalarsPerPosition, offset, clipBounds); | 70 scalarsPerPosition, offset, clipBounds); |
71 return; | 71 return; |
72 } | 72 } |
73 textContext = textContext->fFallbackTextContext; | 73 textContext = textContext->fFallbackTextContext; |
74 } while (textContext); | 74 } while (textContext); |
75 | 75 |
76 // fall back to drawing as a path | 76 // fall back to drawing as a path |
77 this->drawPosTextAsPath(rt, clip, skPaint, viewMatrix, text, byteLength, pos
, | 77 this->drawPosTextAsPath(dc, rt, clip, skPaint, viewMatrix, text, byteLength,
pos, |
78 scalarsPerPosition, offset, clipBounds); | 78 scalarsPerPosition, offset, clipBounds); |
79 } | 79 } |
80 | 80 |
81 bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) { | 81 bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) { |
82 if (paint.getShader() || | 82 if (paint.getShader() || |
83 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) || | 83 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) || |
84 paint.getMaskFilter() || | 84 paint.getMaskFilter() || |
85 paint.getRasterizer() || | 85 paint.getRasterizer() || |
86 paint.getColorFilter() || | 86 paint.getColorFilter() || |
87 paint.getPathEffect() || | 87 paint.getPathEffect() || |
(...skipping 13 matching lines...) Expand all Loading... |
101 } | 101 } |
102 | 102 |
103 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisabl
eLCD(paint)) { | 103 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisabl
eLCD(paint)) { |
104 flags &= ~SkPaint::kLCDRenderText_Flag; | 104 flags &= ~SkPaint::kLCDRenderText_Flag; |
105 flags |= SkPaint::kGenA8FromLCD_Flag; | 105 flags |= SkPaint::kGenA8FromLCD_Flag; |
106 } | 106 } |
107 | 107 |
108 return flags; | 108 return flags; |
109 } | 109 } |
110 | 110 |
111 void GrTextContext::drawTextBlob(GrRenderTarget* rt, | 111 void GrTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt, |
112 const GrClip& clip, const SkPaint& skPaint, | 112 const GrClip& clip, const SkPaint& skPaint, |
113 const SkMatrix& viewMatrix, const SkTextBlob* b
lob, | 113 const SkMatrix& viewMatrix, const SkTextBlob* b
lob, |
114 SkScalar x, SkScalar y, | 114 SkScalar x, SkScalar y, |
115 SkDrawFilter* drawFilter, const SkIRect& clipBo
unds) { | 115 SkDrawFilter* drawFilter, const SkIRect& clipBo
unds) { |
116 SkPaint runPaint = skPaint; | 116 SkPaint runPaint = skPaint; |
117 | 117 |
118 SkTextBlob::RunIterator it(blob); | 118 SkTextBlob::RunIterator it(blob); |
119 for (;!it.done(); it.next()) { | 119 for (;!it.done(); it.next()) { |
120 size_t textLen = it.glyphCount() * sizeof(uint16_t); | 120 size_t textLen = it.glyphCount() * sizeof(uint16_t); |
121 const SkPoint& offset = it.offset(); | 121 const SkPoint& offset = it.offset(); |
122 // applyFontToPaint() always overwrites the exact same attributes, | 122 // applyFontToPaint() always overwrites the exact same attributes, |
123 // so it is safe to not re-seed the paint for this reason. | 123 // so it is safe to not re-seed the paint for this reason. |
124 it.applyFontToPaint(&runPaint); | 124 it.applyFontToPaint(&runPaint); |
125 | 125 |
126 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Typ
e)) { | 126 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Typ
e)) { |
127 // A false return from filter() means we should abort the current dr
aw. | 127 // A false return from filter() means we should abort the current dr
aw. |
128 runPaint = skPaint; | 128 runPaint = skPaint; |
129 continue; | 129 continue; |
130 } | 130 } |
131 | 131 |
132 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); | 132 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
133 | 133 |
134 GrPaint grPaint; | 134 GrPaint grPaint; |
135 if (!SkPaint2GrPaint(fContext, rt, runPaint, viewMatrix, true, &grPaint)
) { | 135 if (!SkPaint2GrPaint(fContext, rt, runPaint, viewMatrix, true, &grPaint)
) { |
136 return; | 136 return; |
137 } | 137 } |
138 | 138 |
139 switch (it.positioning()) { | 139 switch (it.positioning()) { |
140 case SkTextBlob::kDefault_Positioning: | 140 case SkTextBlob::kDefault_Positioning: |
141 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char
*)it.glyphs(), | 141 this->drawText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const c
har *)it.glyphs(), |
142 textLen, x + offset.x(), y + offset.y(), clipBounds); | 142 textLen, x + offset.x(), y + offset.y(), clipBounds); |
143 break; | 143 break; |
144 case SkTextBlob::kHorizontal_Positioning: | 144 case SkTextBlob::kHorizontal_Positioning: |
145 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const ch
ar*)it.glyphs(), | 145 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (cons
t char*)it.glyphs(), |
146 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.
y()), clipBounds); | 146 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.
y()), clipBounds); |
147 break; | 147 break; |
148 case SkTextBlob::kFull_Positioning: | 148 case SkTextBlob::kFull_Positioning: |
149 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const ch
ar*)it.glyphs(), | 149 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (cons
t char*)it.glyphs(), |
150 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBou
nds); | 150 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBou
nds); |
151 break; | 151 break; |
152 default: | 152 default: |
153 SkFAIL("unhandled positioning mode"); | 153 SkFAIL("unhandled positioning mode"); |
154 } | 154 } |
155 | 155 |
156 if (drawFilter) { | 156 if (drawFilter) { |
157 // A draw filter may change the paint arbitrarily, so we must re-see
d in this case. | 157 // A draw filter may change the paint arbitrarily, so we must re-see
d in this case. |
158 runPaint = skPaint; | 158 runPaint = skPaint; |
159 } | 159 } |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 void GrTextContext::drawTextAsPath(GrRenderTarget* rt, | 163 void GrTextContext::drawTextAsPath(GrDrawContext* dc, GrRenderTarget* rt, |
164 const GrClip& clip, | 164 const GrClip& clip, |
165 const SkPaint& skPaint, const SkMatrix& viewM
atrix, | 165 const SkPaint& skPaint, const SkMatrix& viewM
atrix, |
166 const char text[], size_t byteLength, SkScala
r x, SkScalar y, | 166 const char text[], size_t byteLength, SkScala
r x, SkScalar y, |
167 const SkIRect& clipBounds) { | 167 const SkIRect& clipBounds) { |
168 SkTextToPathIter iter(text, byteLength, skPaint, true); | 168 SkTextToPathIter iter(text, byteLength, skPaint, true); |
169 | 169 |
170 SkMatrix matrix; | 170 SkMatrix matrix; |
171 matrix.setScale(iter.getPathScale(), iter.getPathScale()); | 171 matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
172 matrix.postTranslate(x, y); | 172 matrix.postTranslate(x, y); |
173 | 173 |
174 const SkPath* iterPath; | 174 const SkPath* iterPath; |
175 SkScalar xpos, prevXPos = 0; | 175 SkScalar xpos, prevXPos = 0; |
176 | 176 |
177 while (iter.next(&iterPath, &xpos)) { | 177 while (iter.next(&iterPath, &xpos)) { |
178 matrix.postTranslate(xpos - prevXPos, 0); | 178 matrix.postTranslate(xpos - prevXPos, 0); |
179 if (iterPath) { | 179 if (iterPath) { |
180 const SkPaint& pnt = iter.getPaint(); | 180 const SkPaint& pnt = iter.getPaint(); |
181 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt, clip
, *iterPath, | 181 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, *iterPat
h, |
182 pnt, viewMatrix, &matrix, clipBo
unds, false); | 182 pnt, viewMatrix, &matrix, clipBo
unds, false); |
183 } | 183 } |
184 prevXPos = xpos; | 184 prevXPos = xpos; |
185 } | 185 } |
186 } | 186 } |
187 | 187 |
188 void GrTextContext::drawPosTextAsPath(GrRenderTarget* rt, | 188 void GrTextContext::drawPosTextAsPath(GrDrawContext* dc, GrRenderTarget* rt, |
189 const GrClip& clip, | 189 const GrClip& clip, |
190 const SkPaint& origPaint, const SkMatrix&
viewMatrix, | 190 const SkPaint& origPaint, const SkMatrix&
viewMatrix, |
191 const char text[], size_t byteLength, | 191 const char text[], size_t byteLength, |
192 const SkScalar pos[], int scalarsPerPositi
on, | 192 const SkScalar pos[], int scalarsPerPositi
on, |
193 const SkPoint& offset, const SkIRect& clip
Bounds) { | 193 const SkPoint& offset, const SkIRect& clip
Bounds) { |
194 // setup our std paint, in hopes of getting hits in the cache | 194 // setup our std paint, in hopes of getting hits in the cache |
195 SkPaint paint(origPaint); | 195 SkPaint paint(origPaint); |
196 SkScalar matrixScale = paint.setupForAsPaths(); | 196 SkScalar matrixScale = paint.setupForAsPaths(); |
197 | 197 |
198 SkMatrix matrix; | 198 SkMatrix matrix; |
(...skipping 20 matching lines...) Expand all Loading... |
219 if (glyph.fWidth) { | 219 if (glyph.fWidth) { |
220 const SkPath* path = cache->findPath(glyph); | 220 const SkPath* path = cache->findPath(glyph); |
221 if (path) { | 221 if (path) { |
222 SkPoint tmsLoc; | 222 SkPoint tmsLoc; |
223 tmsProc(pos, &tmsLoc); | 223 tmsProc(pos, &tmsLoc); |
224 SkPoint loc; | 224 SkPoint loc; |
225 alignProc(tmsLoc, glyph, &loc); | 225 alignProc(tmsLoc, glyph, &loc); |
226 | 226 |
227 matrix[SkMatrix::kMTransX] = loc.fX; | 227 matrix[SkMatrix::kMTransX] = loc.fX; |
228 matrix[SkMatrix::kMTransY] = loc.fY; | 228 matrix[SkMatrix::kMTransY] = loc.fY; |
229 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt,
clip, *path, paint, | 229 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, *pat
h, paint, |
230 viewMatrix, &matrix, clipBou
nds, false); | 230 viewMatrix, &matrix, clipBou
nds, false); |
231 } | 231 } |
232 } | 232 } |
233 pos += scalarsPerPosition; | 233 pos += scalarsPerPosition; |
234 } | 234 } |
235 } | 235 } |
236 | 236 |
237 // *** change to output positions? | 237 // *** change to output positions? |
238 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr
oc, | 238 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr
oc, |
239 const char text[], size_t byteLength, SkVector*
stopVector) { | 239 const char text[], size_t byteLength, SkVector*
stopVector) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { | 271 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
272 scaler = (GrFontScaler*)auxData; | 272 scaler = (GrFontScaler*)auxData; |
273 } | 273 } |
274 if (nullptr == scaler) { | 274 if (nullptr == scaler) { |
275 scaler = new GrFontScaler(cache); | 275 scaler = new GrFontScaler(cache); |
276 cache->setAuxProc(GlyphCacheAuxProc, scaler); | 276 cache->setAuxProc(GlyphCacheAuxProc, scaler); |
277 } | 277 } |
278 | 278 |
279 return scaler; | 279 return scaler; |
280 } | 280 } |
OLD | NEW |