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 "GrContext.h" | 9 #include "GrContext.h" |
10 #include "GrDrawTarget.h" | 10 #include "GrDrawContext.h" |
11 #include "GrFontScaler.h" | 11 #include "GrFontScaler.h" |
12 | 12 |
13 #include "SkAutoKern.h" | 13 #include "SkAutoKern.h" |
14 #include "SkDrawFilter.h" | 14 #include "SkDrawFilter.h" |
15 #include "SkDrawProcs.h" | 15 #include "SkDrawProcs.h" |
16 #include "SkGlyphCache.h" | 16 #include "SkGlyphCache.h" |
17 #include "SkGpuDevice.h" | 17 #include "SkGpuDevice.h" |
18 #include "SkTextBlob.h" | 18 #include "SkTextBlob.h" |
19 #include "SkTextMapStateProc.h" | 19 #include "SkTextMapStateProc.h" |
20 #include "SkTextToPathIter.h" | 20 #include "SkTextToPathIter.h" |
21 | 21 |
22 GrTextContext::GrTextContext(GrContext* context, SkGpuDevice* gpuDevice, | 22 GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& prope
rties) |
23 const SkDeviceProperties& properties) | |
24 : fFallbackTextContext(NULL) | 23 : fFallbackTextContext(NULL) |
25 , fContext(context) | 24 , fContext(context) |
26 , fGpuDevice(gpuDevice) | |
27 , fDeviceProperties(properties) { | 25 , fDeviceProperties(properties) { |
28 } | 26 } |
29 | 27 |
30 GrTextContext::~GrTextContext() { | 28 GrTextContext::~GrTextContext() { |
31 SkDELETE(fFallbackTextContext); | 29 SkDELETE(fFallbackTextContext); |
32 } | 30 } |
33 | 31 |
34 void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint&
grPaint, | 32 void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint&
grPaint, |
35 const SkPaint& skPaint, const SkIRect& regionClipBounds
) { | 33 const SkPaint& skPaint, const SkIRect& regionClipBounds
) { |
36 fClip = clip; | 34 fClip = clip; |
37 | 35 |
38 fRenderTarget.reset(SkRef(rt)); | 36 fRenderTarget.reset(SkRef(rt)); |
39 | 37 |
40 fRegionClipBounds = regionClipBounds; | 38 fRegionClipBounds = regionClipBounds; |
41 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(),
&fClipRect); | 39 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(),
&fClipRect); |
42 | 40 |
43 fPaint = grPaint; | 41 fPaint = grPaint; |
44 fSkPaint = skPaint; | 42 fSkPaint = skPaint; |
45 } | 43 } |
46 | 44 |
47 void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai
nt& paint, | 45 void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai
nt& paint, |
48 const SkPaint& skPaint, const SkMatrix& viewMatrix, | 46 const SkPaint& skPaint, const SkMatrix& viewMatrix, |
49 const char text[], size_t byteLength, | 47 const char text[], size_t byteLength, |
50 SkScalar x, SkScalar y, const SkIRect& clipBounds)
{ | 48 SkScalar x, SkScalar y, const SkIRect& clipBounds)
{ |
51 if (fContext->abandoned()) { | 49 if (fContext->abandoned()) { |
52 return; | 50 return; |
53 } | 51 } |
54 | 52 |
| 53 GrDrawContext* drawContext = fContext->drawContext(); |
| 54 if (!drawContext) { |
| 55 return; |
| 56 } |
| 57 |
55 GrTextContext* textContext = this; | 58 GrTextContext* textContext = this; |
56 do { | 59 do { |
57 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { | 60 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
58 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text,
byteLength, x, y, | 61 textContext->onDrawText(drawContext, rt, clip, paint, skPaint, viewM
atrix, |
59 clipBounds); | 62 text, byteLength, x, y, clipBounds); |
60 return; | 63 return; |
61 } | 64 } |
62 textContext = textContext->fFallbackTextContext; | 65 textContext = textContext->fFallbackTextContext; |
63 } while (textContext); | 66 } while (textContext); |
64 | 67 |
65 // fall back to drawing as a path | 68 // fall back to drawing as a path |
66 SkASSERT(fGpuDevice); | 69 this->drawTextAsPath(drawContext, rt, clip, skPaint, viewMatrix, |
67 this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds
); | 70 text, byteLength, x, y, clipBounds); |
68 } | 71 } |
69 | 72 |
70 void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr
Paint& paint, | 73 void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr
Paint& paint, |
71 const SkPaint& skPaint, const SkMatrix& viewMatr
ix, | 74 const SkPaint& skPaint, const SkMatrix& viewMatr
ix, |
72 const char text[], size_t byteLength, | 75 const char text[], size_t byteLength, |
73 const SkScalar pos[], int scalarsPerPosition, | 76 const SkScalar pos[], int scalarsPerPosition, |
74 const SkPoint& offset, const SkIRect& clipBounds
) { | 77 const SkPoint& offset, const SkIRect& clipBounds
) { |
75 if (fContext->abandoned()) { | 78 if (fContext->abandoned()) { |
76 return; | 79 return; |
77 } | 80 } |
78 | 81 |
| 82 GrDrawContext* drawContext = fContext->drawContext(); |
| 83 if (!drawContext) { |
| 84 return; |
| 85 } |
| 86 |
79 GrTextContext* textContext = this; | 87 GrTextContext* textContext = this; |
80 do { | 88 do { |
81 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { | 89 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
82 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, tex
t, byteLength, pos, | 90 textContext->onDrawPosText(drawContext, rt, clip, paint, skPaint, vi
ewMatrix, |
| 91 text, byteLength, pos, |
83 scalarsPerPosition, offset, clipBounds); | 92 scalarsPerPosition, offset, clipBounds); |
84 return; | 93 return; |
85 } | 94 } |
86 textContext = textContext->fFallbackTextContext; | 95 textContext = textContext->fFallbackTextContext; |
87 } while (textContext); | 96 } while (textContext); |
88 | 97 |
89 // fall back to drawing as a path | 98 // fall back to drawing as a path |
90 SkASSERT(fGpuDevice); | 99 this->drawPosTextAsPath(drawContext, rt, clip, skPaint, viewMatrix, text, by
teLength, pos, |
91 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsP
erPosition, offset, | 100 scalarsPerPosition, offset, clipBounds); |
92 clipBounds); | |
93 } | 101 } |
94 | 102 |
95 void GrTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, const S
kPaint& skPaint, | 103 void GrTextContext::drawTextBlob(SkGpuDevice* gpuDevice, GrRenderTarget* rt, |
| 104 const GrClip& clip, const SkPaint& skPaint, |
96 const SkMatrix& viewMatrix, const SkTextBlob* b
lob, | 105 const SkMatrix& viewMatrix, const SkTextBlob* b
lob, |
97 SkScalar x, SkScalar y, | 106 SkScalar x, SkScalar y, |
98 SkDrawFilter* drawFilter, const SkIRect& clipBo
unds) { | 107 SkDrawFilter* drawFilter, const SkIRect& clipBo
unds) { |
99 SkPaint runPaint = skPaint; | 108 SkPaint runPaint = skPaint; |
100 | 109 |
101 SkTextBlob::RunIterator it(blob); | 110 SkTextBlob::RunIterator it(blob); |
102 for (;!it.done(); it.next()) { | 111 for (;!it.done(); it.next()) { |
103 size_t textLen = it.glyphCount() * sizeof(uint16_t); | 112 size_t textLen = it.glyphCount() * sizeof(uint16_t); |
104 const SkPoint& offset = it.offset(); | 113 const SkPoint& offset = it.offset(); |
105 // applyFontToPaint() always overwrites the exact same attributes, | 114 // applyFontToPaint() always overwrites the exact same attributes, |
106 // so it is safe to not re-seed the paint for this reason. | 115 // so it is safe to not re-seed the paint for this reason. |
107 it.applyFontToPaint(&runPaint); | 116 it.applyFontToPaint(&runPaint); |
108 | 117 |
109 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Typ
e)) { | 118 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Typ
e)) { |
110 // A false return from filter() means we should abort the current dr
aw. | 119 // A false return from filter() means we should abort the current dr
aw. |
111 runPaint = skPaint; | 120 runPaint = skPaint; |
112 continue; | 121 continue; |
113 } | 122 } |
114 | 123 |
115 runPaint.setFlags(fGpuDevice->filterTextFlags(runPaint)); | 124 runPaint.setFlags(gpuDevice->filterTextFlags(runPaint)); |
116 | 125 |
117 GrPaint grPaint; | 126 GrPaint grPaint; |
118 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true
, &grPaint)) { | 127 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true
, &grPaint)) { |
119 return; | 128 return; |
120 } | 129 } |
121 | 130 |
122 switch (it.positioning()) { | 131 switch (it.positioning()) { |
123 case SkTextBlob::kDefault_Positioning: | 132 case SkTextBlob::kDefault_Positioning: |
124 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char
*)it.glyphs(), | 133 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char
*)it.glyphs(), |
125 textLen, x + offset.x(), y + offset.y(), clipBounds); | 134 textLen, x + offset.x(), y + offset.y(), clipBounds); |
(...skipping 10 matching lines...) Expand all Loading... |
136 SkFAIL("unhandled positioning mode"); | 145 SkFAIL("unhandled positioning mode"); |
137 } | 146 } |
138 | 147 |
139 if (drawFilter) { | 148 if (drawFilter) { |
140 // A draw filter may change the paint arbitrarily, so we must re-see
d in this case. | 149 // A draw filter may change the paint arbitrarily, so we must re-see
d in this case. |
141 runPaint = skPaint; | 150 runPaint = skPaint; |
142 } | 151 } |
143 } | 152 } |
144 } | 153 } |
145 | 154 |
146 void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewM
atrix, | 155 void GrTextContext::drawTextAsPath(GrDrawContext* drawContext, GrRenderTarget* r
t, |
| 156 const GrClip& clip, |
| 157 const SkPaint& skPaint, const SkMatrix& viewM
atrix, |
147 const char text[], size_t byteLength, SkScala
r x, SkScalar y, | 158 const char text[], size_t byteLength, SkScala
r x, SkScalar y, |
148 const SkIRect& clipBounds) { | 159 const SkIRect& clipBounds) { |
149 SkTextToPathIter iter(text, byteLength, skPaint, true); | 160 SkTextToPathIter iter(text, byteLength, skPaint, true); |
150 | 161 |
151 SkMatrix matrix; | 162 SkMatrix matrix; |
152 matrix.setScale(iter.getPathScale(), iter.getPathScale()); | 163 matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
153 matrix.postTranslate(x, y); | 164 matrix.postTranslate(x, y); |
154 | 165 |
155 const SkPath* iterPath; | 166 const SkPath* iterPath; |
156 SkScalar xpos, prevXPos = 0; | 167 SkScalar xpos, prevXPos = 0; |
157 | 168 |
158 while (iter.next(&iterPath, &xpos)) { | 169 while (iter.next(&iterPath, &xpos)) { |
159 matrix.postTranslate(xpos - prevXPos, 0); | 170 matrix.postTranslate(xpos - prevXPos, 0); |
160 if (iterPath) { | 171 if (iterPath) { |
161 const SkPaint& pnt = iter.getPaint(); | 172 const SkPaint& pnt = iter.getPaint(); |
162 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, cl
ipBounds, false); | 173 drawContext->drawPathFull(fContext, rt, clip, *iterPath, |
| 174 pnt, viewMatrix, &matrix, clipBounds, fals
e); |
163 } | 175 } |
164 prevXPos = xpos; | 176 prevXPos = xpos; |
165 } | 177 } |
166 } | 178 } |
167 | 179 |
168 void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix&
viewMatrix, | 180 void GrTextContext::drawPosTextAsPath(GrDrawContext* drawContext, GrRenderTarget
* rt, |
| 181 const GrClip& clip, |
| 182 const SkPaint& origPaint, const SkMatrix&
viewMatrix, |
169 const char text[], size_t byteLength, | 183 const char text[], size_t byteLength, |
170 const SkScalar pos[], int scalarsPerPositi
on, | 184 const SkScalar pos[], int scalarsPerPositi
on, |
171 const SkPoint& offset, const SkIRect& clip
Bounds) { | 185 const SkPoint& offset, const SkIRect& clip
Bounds) { |
172 // setup our std paint, in hopes of getting hits in the cache | 186 // setup our std paint, in hopes of getting hits in the cache |
173 SkPaint paint(origPaint); | 187 SkPaint paint(origPaint); |
174 SkScalar matrixScale = paint.setupForAsPaths(); | 188 SkScalar matrixScale = paint.setupForAsPaths(); |
175 | 189 |
176 SkMatrix matrix; | 190 SkMatrix matrix; |
177 matrix.setScale(matrixScale, matrixScale); | 191 matrix.setScale(matrixScale, matrixScale); |
178 | 192 |
(...skipping 18 matching lines...) Expand all Loading... |
197 if (glyph.fWidth) { | 211 if (glyph.fWidth) { |
198 const SkPath* path = cache->findPath(glyph); | 212 const SkPath* path = cache->findPath(glyph); |
199 if (path) { | 213 if (path) { |
200 SkPoint tmsLoc; | 214 SkPoint tmsLoc; |
201 tmsProc(pos, &tmsLoc); | 215 tmsProc(pos, &tmsLoc); |
202 SkPoint loc; | 216 SkPoint loc; |
203 alignProc(tmsLoc, glyph, &loc); | 217 alignProc(tmsLoc, glyph, &loc); |
204 | 218 |
205 matrix[SkMatrix::kMTransX] = loc.fX; | 219 matrix[SkMatrix::kMTransX] = loc.fX; |
206 matrix[SkMatrix::kMTransY] = loc.fY; | 220 matrix[SkMatrix::kMTransY] = loc.fY; |
207 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix,
clipBounds, false); | 221 drawContext->drawPathFull(fContext, rt, clip, *path, paint, |
| 222 viewMatrix, &matrix, clipBounds, false
); |
208 } | 223 } |
209 } | 224 } |
210 pos += scalarsPerPosition; | 225 pos += scalarsPerPosition; |
211 } | 226 } |
212 } | 227 } |
213 | 228 |
214 //*** change to output positions? | 229 //*** change to output positions? |
215 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr
oc, | 230 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr
oc, |
216 const char text[], size_t byteLength, SkVector*
stopVector) { | 231 const char text[], size_t byteLength, SkVector*
stopVector) { |
217 SkFixed x = 0, y = 0; | 232 SkFixed x = 0, y = 0; |
(...skipping 30 matching lines...) Expand all Loading... |
248 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { | 263 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
249 scaler = (GrFontScaler*)auxData; | 264 scaler = (GrFontScaler*)auxData; |
250 } | 265 } |
251 if (NULL == scaler) { | 266 if (NULL == scaler) { |
252 scaler = SkNEW_ARGS(GrFontScaler, (cache)); | 267 scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
253 cache->setAuxProc(GlyphCacheAuxProc, scaler); | 268 cache->setAuxProc(GlyphCacheAuxProc, scaler); |
254 } | 269 } |
255 | 270 |
256 return scaler; | 271 return scaler; |
257 } | 272 } |
OLD | NEW |