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