• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2012 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #include "GrGLPath.h"
10 #include "GrGLPathRendering.h"
11 #include "GrGLGpu.h"
12 
13 namespace {
verb_to_gl_path_cmd(SkPath::Verb verb)14 inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) {
15     static const GrGLubyte gTable[] = {
16         GR_GL_MOVE_TO,
17         GR_GL_LINE_TO,
18         GR_GL_QUADRATIC_CURVE_TO,
19         GR_GL_CONIC_CURVE_TO,
20         GR_GL_CUBIC_CURVE_TO,
21         GR_GL_CLOSE_PATH,
22     };
23     GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
24     GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
25     GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
26     GR_STATIC_ASSERT(3 == SkPath::kConic_Verb);
27     GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
28     GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
29 
30     SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
31     return gTable[verb];
32 }
33 
34 #ifdef SK_DEBUG
num_coords(SkPath::Verb verb)35 inline int num_coords(SkPath::Verb verb) {
36     static const int gTable[] = {
37         2, // move
38         2, // line
39         4, // quad
40         5, // conic
41         6, // cubic
42         0, // close
43     };
44     GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
45     GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
46     GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
47     GR_STATIC_ASSERT(3 == SkPath::kConic_Verb);
48     GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
49     GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
50 
51     SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
52     return gTable[verb];
53 }
54 #endif
55 
join_to_gl_join(SkPaint::Join join)56 inline GrGLenum join_to_gl_join(SkPaint::Join join) {
57     static GrGLenum gSkJoinsToGrGLJoins[] = {
58         GR_GL_MITER_REVERT,
59         GR_GL_ROUND,
60         GR_GL_BEVEL
61     };
62     return gSkJoinsToGrGLJoins[join];
63     GR_STATIC_ASSERT(0 == SkPaint::kMiter_Join);
64     GR_STATIC_ASSERT(1 == SkPaint::kRound_Join);
65     GR_STATIC_ASSERT(2 == SkPaint::kBevel_Join);
66     GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount);
67 }
68 
cap_to_gl_cap(SkPaint::Cap cap)69 inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) {
70     static GrGLenum gSkCapsToGrGLCaps[] = {
71         GR_GL_FLAT,
72         GR_GL_ROUND,
73         GR_GL_SQUARE
74     };
75     return gSkCapsToGrGLCaps[cap];
76     GR_STATIC_ASSERT(0 == SkPaint::kButt_Cap);
77     GR_STATIC_ASSERT(1 == SkPaint::kRound_Cap);
78     GR_STATIC_ASSERT(2 == SkPaint::kSquare_Cap);
79     GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount);
80 }
81 
points_to_coords(const SkPoint points[],size_t first_point,size_t amount,GrGLfloat coords[])82 inline void points_to_coords(const SkPoint points[], size_t first_point, size_t amount,
83                              GrGLfloat coords[]) {
84     for (size_t i = 0;  i < amount; ++i) {
85         coords[i * 2] =  SkScalarToFloat(points[first_point + i].fX);
86         coords[i * 2 + 1] = SkScalarToFloat(points[first_point + i].fY);
87     }
88 }
89 }
90 
InitPathObject(GrGLGpu * gpu,GrGLuint pathID,const SkPath & skPath,const SkStrokeRec & stroke)91 void GrGLPath::InitPathObject(GrGLGpu* gpu,
92                               GrGLuint pathID,
93                               const SkPath& skPath,
94                               const SkStrokeRec& stroke) {
95     if (!skPath.isEmpty()) {
96         int verbCnt = skPath.countVerbs();
97         int pointCnt = skPath.countPoints();
98         int minCoordCnt = pointCnt * 2;
99 
100         SkSTArray<16, GrGLubyte, true> pathCommands(verbCnt);
101         SkSTArray<16, GrGLfloat, true> pathCoords(minCoordCnt);
102 
103         SkDEBUGCODE(int numCoords = 0);
104 
105         if ((skPath.getSegmentMasks() & SkPath::kConic_SegmentMask) == 0) {
106             // This branch does type punning, converting SkPoint* to GrGLfloat*.
107             SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(GrGLfloat) * 2, sk_point_not_two_floats);
108             // This branch does not convert with SkScalarToFloat.
109 #ifndef SK_SCALAR_IS_FLOAT
110 #error Need SK_SCALAR_IS_FLOAT.
111 #endif
112             pathCommands.resize_back(verbCnt);
113             pathCoords.resize_back(minCoordCnt);
114             skPath.getPoints(reinterpret_cast<SkPoint*>(&pathCoords[0]), pointCnt);
115             skPath.getVerbs(&pathCommands[0], verbCnt);
116             for (int i = 0; i < verbCnt; ++i) {
117                 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
118                 pathCommands[i] = verb_to_gl_path_cmd(v);
119                 SkDEBUGCODE(numCoords += num_coords(v));
120             }
121         } else {
122             SkPoint points[4];
123             SkPath::RawIter iter(skPath);
124             SkPath::Verb verb;
125             while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
126                 pathCommands.push_back(verb_to_gl_path_cmd(verb));
127                 GrGLfloat coords[6];
128                 int coordsForVerb;
129                 switch (verb) {
130                     case SkPath::kMove_Verb:
131                         points_to_coords(points, 0, 1, coords);
132                         coordsForVerb = 2;
133                         break;
134                     case SkPath::kLine_Verb:
135                         points_to_coords(points, 1, 1, coords);
136                         coordsForVerb = 2;
137                         break;
138                     case SkPath::kConic_Verb:
139                         points_to_coords(points, 1, 2, coords);
140                         coords[4] = SkScalarToFloat(iter.conicWeight());
141                         coordsForVerb = 5;
142                         break;
143                     case SkPath::kQuad_Verb:
144                         points_to_coords(points, 1, 2, coords);
145                         coordsForVerb = 4;
146                         break;
147                     case SkPath::kCubic_Verb:
148                         points_to_coords(points, 1, 3, coords);
149                         coordsForVerb = 6;
150                         break;
151                     case SkPath::kClose_Verb:
152                         continue;
153                     default:
154                         SkASSERT(false);  // Not reached.
155                         continue;
156                 }
157                 SkDEBUGCODE(numCoords += num_coords(verb));
158                 pathCoords.push_back_n(coordsForVerb, coords);
159             }
160         }
161 
162         SkASSERT(verbCnt == pathCommands.count());
163         SkASSERT(numCoords == pathCoords.count());
164 
165         GR_GL_CALL(gpu->glInterface(), PathCommands(pathID, pathCommands.count(), &pathCommands[0],
166                    pathCoords.count(), GR_GL_FLOAT, &pathCoords[0]));
167     } else {
168         GR_GL_CALL(gpu->glInterface(), PathCommands(pathID, 0, NULL, 0, GR_GL_FLOAT, NULL));
169     }
170 
171     if (stroke.needToApply()) {
172         SkASSERT(!stroke.isHairlineStyle());
173         GR_GL_CALL(gpu->glInterface(),
174             PathParameterf(pathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth())));
175         GR_GL_CALL(gpu->glInterface(),
176             PathParameterf(pathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter())));
177         GrGLenum join = join_to_gl_join(stroke.getJoin());
178         GR_GL_CALL(gpu->glInterface(), PathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join));
179         GrGLenum cap = cap_to_gl_cap(stroke.getCap());
180         GR_GL_CALL(gpu->glInterface(), PathParameteri(pathID, GR_GL_PATH_END_CAPS, cap));
181     }
182 }
183 
GrGLPath(GrGLGpu * gpu,const SkPath & path,const SkStrokeRec & stroke)184 GrGLPath::GrGLPath(GrGLGpu* gpu, const SkPath& path, const SkStrokeRec& stroke)
185     : INHERITED(gpu, path, stroke),
186       fPathID(gpu->glPathRendering()->genPaths(1)) {
187 
188     InitPathObject(gpu, fPathID, fSkPath, stroke);
189 
190     if (stroke.needToApply()) {
191         // FIXME: try to account for stroking, without rasterizing the stroke.
192         fBounds.outset(stroke.getWidth(), stroke.getWidth());
193     }
194     this->registerWithCache();
195 }
196 
onRelease()197 void GrGLPath::onRelease() {
198     if (0 != fPathID && !this->isWrapped()) {
199         static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fPathID, 1);
200         fPathID = 0;
201     }
202 
203     INHERITED::onRelease();
204 }
205 
onAbandon()206 void GrGLPath::onAbandon() {
207     fPathID = 0;
208 
209     INHERITED::onAbandon();
210 }
211