| 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, const SkDeviceProperties& prope
rties) | 23 GrTextContext::GrTextContext(GrContext* context, GrDrawContext* drawContext, |
| 24 const SkDeviceProperties& properties) |
| 24 : fFallbackTextContext(NULL) | 25 : fFallbackTextContext(NULL) |
| 25 , fContext(context) | 26 , fContext(context) |
| 26 , fDeviceProperties(properties) { | 27 , fDeviceProperties(properties) |
| 28 , fDrawContext(drawContext) { |
| 27 } | 29 } |
| 28 | 30 |
| 29 GrTextContext::~GrTextContext() { | 31 GrTextContext::~GrTextContext() { |
| 30 SkDELETE(fFallbackTextContext); | 32 SkDELETE(fFallbackTextContext); |
| 31 } | 33 } |
| 32 | 34 |
| 33 void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint&
grPaint, | 35 void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint&
grPaint, |
| 34 const SkPaint& skPaint, const SkIRect& regionClipBounds
) { | 36 const SkPaint& skPaint, const SkIRect& regionClipBounds
) { |
| 35 fClip = clip; | 37 fClip = clip; |
| 36 | 38 |
| 37 fRenderTarget.reset(SkRef(rt)); | 39 fRenderTarget.reset(SkRef(rt)); |
| 38 | 40 |
| 39 fRegionClipBounds = regionClipBounds; | 41 fRegionClipBounds = regionClipBounds; |
| 40 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(),
&fClipRect); | 42 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(),
&fClipRect); |
| 41 | 43 |
| 42 fPaint = grPaint; | 44 fPaint = grPaint; |
| 43 fSkPaint = skPaint; | 45 fSkPaint = skPaint; |
| 44 } | 46 } |
| 45 | 47 |
| 46 void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai
nt& paint, | 48 void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai
nt& paint, |
| 47 const SkPaint& skPaint, const SkMatrix& viewMatrix, | 49 const SkPaint& skPaint, const SkMatrix& viewMatrix, |
| 48 const char text[], size_t byteLength, | 50 const char text[], size_t byteLength, |
| 49 SkScalar x, SkScalar y, const SkIRect& clipBounds)
{ | 51 SkScalar x, SkScalar y, const SkIRect& clipBounds)
{ |
| 50 if (fContext->abandoned()) { | 52 if (fContext->abandoned() || !fDrawContext) { |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 GrDrawContext* drawContext = fContext->drawContext(); | |
| 55 if (!drawContext) { | |
| 56 return; | 53 return; |
| 57 } | 54 } |
| 58 | 55 |
| 59 GrTextContext* textContext = this; | 56 GrTextContext* textContext = this; |
| 60 do { | 57 do { |
| 61 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { | 58 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
| 62 textContext->onDrawText(drawContext, rt, clip, paint, skPaint, viewM
atrix, | 59 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, |
| 63 text, byteLength, x, y, clipBounds); | 60 text, byteLength, x, y, clipBounds); |
| 64 return; | 61 return; |
| 65 } | 62 } |
| 66 textContext = textContext->fFallbackTextContext; | 63 textContext = textContext->fFallbackTextContext; |
| 67 } while (textContext); | 64 } while (textContext); |
| 68 | 65 |
| 69 // fall back to drawing as a path | 66 // fall back to drawing as a path |
| 70 this->drawTextAsPath(drawContext, rt, clip, skPaint, viewMatrix, | 67 this->drawTextAsPath(rt, clip, skPaint, viewMatrix, |
| 71 text, byteLength, x, y, clipBounds); | 68 text, byteLength, x, y, clipBounds); |
| 72 } | 69 } |
| 73 | 70 |
| 74 void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr
Paint& paint, | 71 void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr
Paint& paint, |
| 75 const SkPaint& skPaint, const SkMatrix& viewMatr
ix, | 72 const SkPaint& skPaint, const SkMatrix& viewMatr
ix, |
| 76 const char text[], size_t byteLength, | 73 const char text[], size_t byteLength, |
| 77 const SkScalar pos[], int scalarsPerPosition, | 74 const SkScalar pos[], int scalarsPerPosition, |
| 78 const SkPoint& offset, const SkIRect& clipBounds
) { | 75 const SkPoint& offset, const SkIRect& clipBounds
) { |
| 79 if (fContext->abandoned()) { | 76 if (fContext->abandoned() || !fDrawContext) { |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 GrDrawContext* drawContext = fContext->drawContext(); | |
| 84 if (!drawContext) { | |
| 85 return; | 77 return; |
| 86 } | 78 } |
| 87 | 79 |
| 88 GrTextContext* textContext = this; | 80 GrTextContext* textContext = this; |
| 89 do { | 81 do { |
| 90 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { | 82 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
| 91 textContext->onDrawPosText(drawContext, rt, clip, paint, skPaint, vi
ewMatrix, | 83 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, |
| 92 text, byteLength, pos, | 84 text, byteLength, pos, |
| 93 scalarsPerPosition, offset, clipBounds); | 85 scalarsPerPosition, offset, clipBounds); |
| 94 return; | 86 return; |
| 95 } | 87 } |
| 96 textContext = textContext->fFallbackTextContext; | 88 textContext = textContext->fFallbackTextContext; |
| 97 } while (textContext); | 89 } while (textContext); |
| 98 | 90 |
| 99 // fall back to drawing as a path | 91 // fall back to drawing as a path |
| 100 this->drawPosTextAsPath(drawContext, rt, clip, skPaint, viewMatrix, text, by
teLength, pos, | 92 this->drawPosTextAsPath(rt, clip, skPaint, viewMatrix, text, byteLength, pos
, |
| 101 scalarsPerPosition, offset, clipBounds); | 93 scalarsPerPosition, offset, clipBounds); |
| 102 } | 94 } |
| 103 | 95 |
| 104 bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) { | 96 bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) { |
| 105 if (paint.getShader() || | 97 if (paint.getShader() || |
| 106 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) || | 98 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) || |
| 107 paint.getMaskFilter() || | 99 paint.getMaskFilter() || |
| 108 paint.getRasterizer() || | 100 paint.getRasterizer() || |
| 109 paint.getColorFilter() || | 101 paint.getColorFilter() || |
| 110 paint.getPathEffect() || | 102 paint.getPathEffect() || |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 SkFAIL("unhandled positioning mode"); | 168 SkFAIL("unhandled positioning mode"); |
| 177 } | 169 } |
| 178 | 170 |
| 179 if (drawFilter) { | 171 if (drawFilter) { |
| 180 // A draw filter may change the paint arbitrarily, so we must re-see
d in this case. | 172 // A draw filter may change the paint arbitrarily, so we must re-see
d in this case. |
| 181 runPaint = skPaint; | 173 runPaint = skPaint; |
| 182 } | 174 } |
| 183 } | 175 } |
| 184 } | 176 } |
| 185 | 177 |
| 186 void GrTextContext::drawTextAsPath(GrDrawContext* drawContext, GrRenderTarget* r
t, | 178 void GrTextContext::drawTextAsPath(GrRenderTarget* rt, |
| 187 const GrClip& clip, | 179 const GrClip& clip, |
| 188 const SkPaint& skPaint, const SkMatrix& viewM
atrix, | 180 const SkPaint& skPaint, const SkMatrix& viewM
atrix, |
| 189 const char text[], size_t byteLength, SkScala
r x, SkScalar y, | 181 const char text[], size_t byteLength, SkScala
r x, SkScalar y, |
| 190 const SkIRect& clipBounds) { | 182 const SkIRect& clipBounds) { |
| 191 SkTextToPathIter iter(text, byteLength, skPaint, true); | 183 SkTextToPathIter iter(text, byteLength, skPaint, true); |
| 192 | 184 |
| 193 SkMatrix matrix; | 185 SkMatrix matrix; |
| 194 matrix.setScale(iter.getPathScale(), iter.getPathScale()); | 186 matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
| 195 matrix.postTranslate(x, y); | 187 matrix.postTranslate(x, y); |
| 196 | 188 |
| 197 const SkPath* iterPath; | 189 const SkPath* iterPath; |
| 198 SkScalar xpos, prevXPos = 0; | 190 SkScalar xpos, prevXPos = 0; |
| 199 | 191 |
| 200 while (iter.next(&iterPath, &xpos)) { | 192 while (iter.next(&iterPath, &xpos)) { |
| 201 matrix.postTranslate(xpos - prevXPos, 0); | 193 matrix.postTranslate(xpos - prevXPos, 0); |
| 202 if (iterPath) { | 194 if (iterPath) { |
| 203 const SkPaint& pnt = iter.getPaint(); | 195 const SkPaint& pnt = iter.getPaint(); |
| 204 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip,
*iterPath, | 196 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt, clip
, *iterPath, |
| 205 pnt, viewMatrix, &matrix, clipBo
unds, false); | 197 pnt, viewMatrix, &matrix, clipBo
unds, false); |
| 206 } | 198 } |
| 207 prevXPos = xpos; | 199 prevXPos = xpos; |
| 208 } | 200 } |
| 209 } | 201 } |
| 210 | 202 |
| 211 void GrTextContext::drawPosTextAsPath(GrDrawContext* drawContext, GrRenderTarget
* rt, | 203 void GrTextContext::drawPosTextAsPath(GrRenderTarget* rt, |
| 212 const GrClip& clip, | 204 const GrClip& clip, |
| 213 const SkPaint& origPaint, const SkMatrix&
viewMatrix, | 205 const SkPaint& origPaint, const SkMatrix&
viewMatrix, |
| 214 const char text[], size_t byteLength, | 206 const char text[], size_t byteLength, |
| 215 const SkScalar pos[], int scalarsPerPositi
on, | 207 const SkScalar pos[], int scalarsPerPositi
on, |
| 216 const SkPoint& offset, const SkIRect& clip
Bounds) { | 208 const SkPoint& offset, const SkIRect& clip
Bounds) { |
| 217 // setup our std paint, in hopes of getting hits in the cache | 209 // setup our std paint, in hopes of getting hits in the cache |
| 218 SkPaint paint(origPaint); | 210 SkPaint paint(origPaint); |
| 219 SkScalar matrixScale = paint.setupForAsPaths(); | 211 SkScalar matrixScale = paint.setupForAsPaths(); |
| 220 | 212 |
| 221 SkMatrix matrix; | 213 SkMatrix matrix; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 242 if (glyph.fWidth) { | 234 if (glyph.fWidth) { |
| 243 const SkPath* path = cache->findPath(glyph); | 235 const SkPath* path = cache->findPath(glyph); |
| 244 if (path) { | 236 if (path) { |
| 245 SkPoint tmsLoc; | 237 SkPoint tmsLoc; |
| 246 tmsProc(pos, &tmsLoc); | 238 tmsProc(pos, &tmsLoc); |
| 247 SkPoint loc; | 239 SkPoint loc; |
| 248 alignProc(tmsLoc, glyph, &loc); | 240 alignProc(tmsLoc, glyph, &loc); |
| 249 | 241 |
| 250 matrix[SkMatrix::kMTransX] = loc.fX; | 242 matrix[SkMatrix::kMTransX] = loc.fX; |
| 251 matrix[SkMatrix::kMTransY] = loc.fY; | 243 matrix[SkMatrix::kMTransY] = loc.fY; |
| 252 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, c
lip, *path, paint, | 244 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt,
clip, *path, paint, |
| 253 viewMatrix, &matrix, clipBou
nds, false); | 245 viewMatrix, &matrix, clipBou
nds, false); |
| 254 } | 246 } |
| 255 } | 247 } |
| 256 pos += scalarsPerPosition; | 248 pos += scalarsPerPosition; |
| 257 } | 249 } |
| 258 } | 250 } |
| 259 | 251 |
| 260 //*** change to output positions? | 252 //*** change to output positions? |
| 261 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr
oc, | 253 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr
oc, |
| 262 const char text[], size_t byteLength, SkVector*
stopVector) { | 254 const char text[], size_t byteLength, SkVector*
stopVector) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { | 286 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 295 scaler = (GrFontScaler*)auxData; | 287 scaler = (GrFontScaler*)auxData; |
| 296 } | 288 } |
| 297 if (NULL == scaler) { | 289 if (NULL == scaler) { |
| 298 scaler = SkNEW_ARGS(GrFontScaler, (cache)); | 290 scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
| 299 cache->setAuxProc(GlyphCacheAuxProc, scaler); | 291 cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 300 } | 292 } |
| 301 | 293 |
| 302 return scaler; | 294 return scaler; |
| 303 } | 295 } |
| OLD | NEW |