Upstream version 9.38.198.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     GrGLPathRendering* pr = gpu->pathRendering();
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     pr->pathCommands(pathID, verbCnt, &pathCommands[0], 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]);
110     if (stroke.needToApply()) {
111         SkASSERT(!stroke.isHairlineStyle());
112         pr->pathParameterf(pathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth()));
113         pr->pathParameterf(pathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter()));
114         GrGLenum join = join_to_gl_join(stroke.getJoin());
115         pr->pathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join);
116         GrGLenum cap = cap_to_gl_cap(stroke.getCap());
117         pr->pathParameteri(pathID, GR_GL_PATH_INITIAL_END_CAP, cap);
118         pr->pathParameteri(pathID, GR_GL_PATH_TERMINAL_END_CAP, cap);
119     }
120 }
121
122 GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke)
123     : INHERITED(gpu, kIsWrapped, path, stroke),
124       fPathID(gpu->pathRendering()->genPaths(1)) {
125     SkASSERT(!path.isEmpty());
126
127     InitPathObject(gpu, fPathID, fSkPath, stroke);
128
129     if (stroke.needToApply()) {
130         // FIXME: try to account for stroking, without rasterizing the stroke.
131         fBounds.outset(stroke.getWidth(), stroke.getWidth());
132     }
133 }
134
135 GrGLPath::~GrGLPath() {
136     this->release();
137 }
138
139 void GrGLPath::onRelease() {
140     if (0 != fPathID && !this->isWrapped()) {
141         static_cast<GrGpuGL*>(this->getGpu())->pathRendering()->deletePaths(fPathID, 1);
142         fPathID = 0;
143     }
144
145     INHERITED::onRelease();
146 }
147
148 void GrGLPath::onAbandon() {
149     fPathID = 0;
150
151     INHERITED::onAbandon();
152 }