3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "GrGLPathRendering.h"
14 inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) {
15 static const GrGLubyte gTable[] = {
18 GR_GL_QUADRATIC_CURVE_TO,
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);
29 SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
34 inline int num_pts(SkPath::Verb verb) {
35 static const int gTable[] = {
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);
49 SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
54 inline GrGLenum join_to_gl_join(SkPaint::Join join) {
55 static GrGLenum gSkJoinsToGrGLJoins[] = {
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);
67 inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) {
68 static GrGLenum gSkCapsToGrGLCaps[] = {
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);
82 static const bool kIsWrapped = false; // The constructor creates the GL path object.
84 void GrGLPath::InitPathObject(GrGpuGL* gpu,
87 const SkStrokeRec& stroke) {
88 GrGLPathRendering* pr = gpu->pathRendering();
89 SkSTArray<16, GrGLubyte, true> pathCommands;
90 SkSTArray<16, SkPoint, true> pathPoints;
92 int verbCnt = skPath.countVerbs();
93 int pointCnt = skPath.countPoints();
94 pathCommands.resize_back(verbCnt);
95 pathPoints.resize_back(pointCnt);
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);
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));
107 SkASSERT(pathPoints.count() == numPts);
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);
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());
127 InitPathObject(gpu, fPathID, fSkPath, stroke);
129 if (stroke.needToApply()) {
130 // FIXME: try to account for stroking, without rasterizing the stroke.
131 fBounds.outset(stroke.getWidth(), stroke.getWidth());
135 GrGLPath::~GrGLPath() {
139 void GrGLPath::onRelease() {
140 if (0 != fPathID && !this->isWrapped()) {
141 static_cast<GrGpuGL*>(this->getGpu())->pathRendering()->deletePaths(fPathID, 1);
145 INHERITED::onRelease();
148 void GrGLPath::onAbandon() {
151 INHERITED::onAbandon();