Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: src/gpu/GrTextContext.cpp

Issue 1015173002: Let text contexts fall back directly to paths (Closed) Base URL: https://skia.googlesource.com/skia.git@text-blob-to-context
Patch Set: tidying Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "GrDrawTarget.h"
11 #include "GrFontScaler.h" 11 #include "GrFontScaler.h"
12 12
13 #include "SkAutoKern.h" 13 #include "SkAutoKern.h"
14 #include "SkDrawProcs.h"
14 #include "SkGlyphCache.h" 15 #include "SkGlyphCache.h"
16 #include "SkGpuDevice.h"
17 #include "SkTextMapStateProc.h"
18 #include "SkTextToPathIter.h"
15 19
16 GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& prope rties) : 20 GrTextContext::GrTextContext(GrContext* context, SkGpuDevice* gpuDevice,
17 fFallbackTextContext(NULL), 21 const SkDeviceProperties& properties)
18 fContext(context), fDeviceProperties(properties), fD rawTarget(NULL) { 22 : fFallbackTextContext(NULL)
23 , fContext(context)
24 , fGpuDevice(gpuDevice)
25 , fDeviceProperties(properties)
26 , fDrawTarget(NULL) {
19 } 27 }
20 28
21 GrTextContext::~GrTextContext() { 29 GrTextContext::~GrTextContext() {
22 SkDELETE(fFallbackTextContext); 30 SkDELETE(fFallbackTextContext);
23 } 31 }
24 32
25 void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint, 33 void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
26 const SkPaint& skPaint) { 34 const SkPaint& skPaint) {
27 fClip = clip; 35 fClip = clip;
28 36
29 fRenderTarget.reset(SkRef(rt)); 37 fRenderTarget.reset(SkRef(rt));
30 38
31 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect); 39 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
32 40
33 fDrawTarget = fContext->getTextTarget(); 41 fDrawTarget = fContext->getTextTarget();
34 42
35 fPaint = grPaint; 43 fPaint = grPaint;
36 fSkPaint = skPaint; 44 fSkPaint = skPaint;
37 } 45 }
38 46
39 bool GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai nt& paint, 47 void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPai nt& paint,
40 const SkPaint& skPaint, const SkMatrix& viewMatrix, 48 const SkPaint& skPaint, const SkMatrix& viewMatrix,
41 const char text[], size_t byteLength, 49 const char text[], size_t byteLength,
42 SkScalar x, SkScalar y) { 50 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
43 if (!fContext->getTextTarget()) { 51 if (!fContext->getTextTarget()) {
44 return false; 52 return;
45 } 53 }
46 54
47 GrTextContext* textContext = this; 55 GrTextContext* textContext = this;
48 do { 56 do {
49 if (textContext->canDraw(skPaint, viewMatrix)) { 57 if (textContext->canDraw(skPaint, viewMatrix)) {
50 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y); 58 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y);
51 return true; 59 return;
52 } 60 }
53 textContext = textContext->fFallbackTextContext; 61 textContext = textContext->fFallbackTextContext;
54 } while (textContext); 62 } while (textContext);
55 63
56 return false; 64 // fall back to drawing as a path
65 this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds );
57 } 66 }
58 67
59 bool GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr Paint& paint, 68 void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const Gr Paint& paint,
60 const SkPaint& skPaint, const SkMatrix& viewMatr ix, 69 const SkPaint& skPaint, const SkMatrix& viewMatr ix,
61 const char text[], size_t byteLength, 70 const char text[], size_t byteLength,
62 const SkScalar pos[], int scalarsPerPosition, 71 const SkScalar pos[], int scalarsPerPosition,
63 const SkPoint& offset) { 72 const SkPoint& offset, const SkIRect& clipBounds ) {
64 if (!fContext->getTextTarget()) { 73 if (!fContext->getTextTarget()) {
65 return false; 74 return;
66 } 75 }
67 76
68 GrTextContext* textContext = this; 77 GrTextContext* textContext = this;
69 do { 78 do {
70 if (textContext->canDraw(skPaint, viewMatrix)) { 79 if (textContext->canDraw(skPaint, viewMatrix)) {
71 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, tex t, byteLength, pos, 80 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, tex t, byteLength, pos,
72 scalarsPerPosition, offset); 81 scalarsPerPosition, offset);
73 return true; 82 return;
74 } 83 }
75 textContext = textContext->fFallbackTextContext; 84 textContext = textContext->fFallbackTextContext;
76 } while (textContext); 85 } while (textContext);
77 86
78 return false; 87 // fall back to drawing as a path
88 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsP erPosition, offset,
89 clipBounds);
79 } 90 }
80 91
92 void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewM atrix,
93 const char text[], size_t byteLength, SkScala r x, SkScalar y,
94 const SkIRect& clipBounds) {
95 SkTextToPathIter iter(text, byteLength, skPaint, true);
96
97 SkMatrix matrix;
98 matrix.setScale(iter.getPathScale(), iter.getPathScale());
99 matrix.postTranslate(x, y);
100
101 const SkPath* iterPath;
102 SkScalar xpos, prevXPos = 0;
103
104 while (iter.next(&iterPath, &xpos)) {
105 matrix.postTranslate(xpos - prevXPos, 0);
106 if (iterPath) {
107 const SkPaint& pnt = iter.getPaint();
108 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, cl ipBounds, false);
109 }
110 prevXPos = xpos;
111 }
112 }
113
114 void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
115 const char text[], size_t byteLength,
116 const SkScalar pos[], int scalarsPerPositi on,
117 const SkPoint& offset, const SkIRect& clip Bounds) {
118 // setup our std paint, in hopes of getting hits in the cache
119 SkPaint paint(origPaint);
120 SkScalar matrixScale = paint.setupForAsPaths();
121
122 SkMatrix matrix;
123 matrix.setScale(matrixScale, matrixScale);
124
125 // Temporarily jam in kFill, so we only ever ask for the raw outline from th e cache.
126 paint.setStyle(SkPaint::kFill_Style);
127 paint.setPathEffect(NULL);
128
129 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
130 SkAutoGlyphCache autoCache(paint, NULL, NULL);
131 SkGlyphCache* cache = autoCache.getCache();
132
133 const char* stop = text + byteLength;
134 SkTextAlignProc alignProc(paint.getTextAlign());
135 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
136
137 // Now restore the original settings, so we "draw" with whatever style/strok ing.
138 paint.setStyle(origPaint.getStyle());
139 paint.setPathEffect(origPaint.getPathEffect());
140
141 while (text < stop) {
142 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
143 if (glyph.fWidth) {
144 const SkPath* path = cache->findPath(glyph);
145 if (path) {
146 SkPoint tmsLoc;
147 tmsProc(pos, &tmsLoc);
148 SkPoint loc;
149 alignProc(tmsLoc, glyph, &loc);
150
151 matrix[SkMatrix::kMTransX] = loc.fX;
152 matrix[SkMatrix::kMTransY] = loc.fY;
153 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
154 }
155 }
156 pos += scalarsPerPosition;
157 }
158 }
81 159
82 //*** change to output positions? 160 //*** change to output positions?
83 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr oc, 161 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCachePr oc,
84 const char text[], size_t byteLength, SkVector* stopVector) { 162 const char text[], size_t byteLength, SkVector* stopVector) {
85 SkFixed x = 0, y = 0; 163 SkFixed x = 0, y = 0;
86 const char* stop = text + byteLength; 164 const char* stop = text + byteLength;
87 165
88 SkAutoKern autokern; 166 SkAutoKern autokern;
89 167
90 int numGlyphs = 0; 168 int numGlyphs = 0;
(...skipping 25 matching lines...) Expand all
116 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { 194 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
117 scaler = (GrFontScaler*)auxData; 195 scaler = (GrFontScaler*)auxData;
118 } 196 }
119 if (NULL == scaler) { 197 if (NULL == scaler) {
120 scaler = SkNEW_ARGS(GrFontScaler, (cache)); 198 scaler = SkNEW_ARGS(GrFontScaler, (cache));
121 cache->setAuxProc(GlyphCacheAuxProc, scaler); 199 cache->setAuxProc(GlyphCacheAuxProc, scaler);
122 } 200 }
123 201
124 return scaler; 202 return scaler;
125 } 203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698