Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLPath.cpp
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 "GrGpuGL.h"
12
13 namespace {
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         0xFF, // conic
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(4 == SkPath::kCubic_Verb);
27     GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
28
29     SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
30     return gTable[verb];
31 }
32
33 #ifdef SK_DEBUG
34 inline int num_pts(SkPath::Verb verb) {
35     static const int gTable[] = {
36         1, // move
37         1, // line
38         2, // quad
39         2, // conic
40         3, // cubic
41         0, // close
42     };
43     GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
44     GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
45     GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
46     GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
47     GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
48
49     SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
50     return gTable[verb];
51 }
52 #endif
53
54 inline GrGLenum join_to_gl_join(SkPaint::Join join) {
55     static GrGLenum gSkJoinsToGrGLJoins[] = {
56         GR_GL_MITER_REVERT,
57         GR_GL_ROUND,
58         GR_GL_BEVEL
59     };
60     return gSkJoinsToGrGLJoins[join];
61     GR_STATIC_ASSERT(0 == SkPaint::kMiter_Join);
62     GR_STATIC_ASSERT(1 == SkPaint::kRound_Join);
63     GR_STATIC_ASSERT(2 == SkPaint::kBevel_Join);
64     GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount);
65 }
66
67 inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) {
68     static GrGLenum gSkCapsToGrGLCaps[] = {
69         GR_GL_FLAT,
70         GR_GL_ROUND,
71         GR_GL_SQUARE
72     };
73     return gSkCapsToGrGLCaps[cap];
74     GR_STATIC_ASSERT(0 == SkPaint::kButt_Cap);
75     GR_STATIC_ASSERT(1 == SkPaint::kRound_Cap);
76     GR_STATIC_ASSERT(2 == SkPaint::kSquare_Cap);
77     GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount);
78 }
79
80 }
81
82 static const bool kIsWrapped = false; // The constructor creates the GL path object.
83
84 void GrGLPath::InitPathObject(GrGpuGL* gpu,
85                               GrGLuint pathID,
86                               const SkPath& skPath,
87                               const SkStrokeRec& stroke) {
88     if (!skPath.isEmpty()) {
89         SkSTArray<16, GrGLubyte, true> pathCommands;
90         SkSTArray<16, SkPoint, true> pathPoints;
91
92         int verbCnt = skPath.countVerbs();
93         int pointCnt = skPath.countPoints();
94         pathCommands.resize_back(verbCnt);
95         pathPoints.resize_back(pointCnt);
96
97         // TODO: Direct access to path points since we could pass them on directly.
98         skPath.getPoints(&pathPoints[0], pointCnt);
99         skPath.getVerbs(&pathCommands[0], verbCnt);
100
101         SkDEBUGCODE(int numPts = 0);
102         for (int i = 0; i < verbCnt; ++i) {
103             SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
104             pathCommands[i] = verb_to_gl_path_cmd(v);
105             SkDEBUGCODE(numPts += num_pts(v));
106         }
107         SkASSERT(pathPoints.count() == numPts);
108
109         GR_GL_CALL(gpu->glInterface(),
110                    PathCommands(pathID, verbCnt, &pathCommands[0],
111                                 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]));
112     } else {
113         GR_GL_CALL(gpu->glInterface(), PathCommands(pathID, 0, NULL, 0, GR_GL_FLOAT, NULL));
114     }
115
116     if (stroke.needToApply()) {
117         SkASSERT(!stroke.isHairlineStyle());
118         GR_GL_CALL(gpu->glInterface(),
119             PathParameterf(pathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth())));
120         GR_GL_CALL(gpu->glInterface(),
121             PathParameterf(pathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter())));
122         GrGLenum join = join_to_gl_join(stroke.getJoin());
123         GR_GL_CALL(gpu->glInterface(),
124             PathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join));
125         GrGLenum cap = cap_to_gl_cap(stroke.getCap());
126         GR_GL_CALL(gpu->glInterface(),
127             PathParameteri(pathID, GR_GL_PATH_INITIAL_END_CAP, cap));
128         GR_GL_CALL(gpu->glInterface(),
129             PathParameteri(pathID, GR_GL_PATH_TERMINAL_END_CAP, cap));
130     }
131 }
132
133 GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke)
134     : INHERITED(gpu, kIsWrapped, path, stroke),
135       fPathID(gpu->glPathRendering()->genPaths(1)) {
136
137     InitPathObject(gpu, fPathID, fSkPath, stroke);
138
139     if (stroke.needToApply()) {
140         // FIXME: try to account for stroking, without rasterizing the stroke.
141         fBounds.outset(stroke.getWidth(), stroke.getWidth());
142     }
143     this->registerWithCache();
144 }
145
146 GrGLPath::~GrGLPath() {
147     this->release();
148 }
149
150 void GrGLPath::onRelease() {
151     if (0 != fPathID && !this->isWrapped()) {
152         static_cast<GrGpuGL*>(this->getGpu())->glPathRendering()->deletePaths(fPathID, 1);
153         fPathID = 0;
154     }
155
156     INHERITED::onRelease();
157 }
158
159 void GrGLPath::onAbandon() {
160     fPathID = 0;
161
162     INHERITED::onAbandon();
163 }