blob: 514c8c738c99a045c50e67961d3d2fce4a67fd73 [file] [log] [blame]
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001
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 "GrGpuGL.h"
11
12#define GPUGL static_cast<GrGpuGL*>(this->getGpu())
13
14#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
15#define GL_CALL_RET(R, X) GR_GL_CALL_RET(GPUGL->glInterface(), R, X)
16
17namespace {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000018inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000019 static const GrGLubyte gTable[] = {
20 GR_GL_MOVE_TO,
21 GR_GL_LINE_TO,
22 GR_GL_QUADRATIC_CURVE_TO,
reed@google.com277c3f82013-05-31 15:17:50 +000023 0xFF, // conic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000024 GR_GL_CUBIC_CURVE_TO,
25 GR_GL_CLOSE_PATH,
26 };
27 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
28 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
29 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000030 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
31 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000032
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000033 SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000034 return gTable[verb];
35}
36
humper@google.com0e515772013-01-07 19:54:40 +000037#ifdef SK_DEBUG
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000038inline int num_pts(SkPath::Verb verb) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000039 static const int gTable[] = {
40 1, // move
41 1, // line
42 2, // quad
reed@google.com277c3f82013-05-31 15:17:50 +000043 2, // conic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000044 3, // cubic
45 0, // close
46 };
47 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
48 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
49 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000050 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
51 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000052
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053 SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000054 return gTable[verb];
55}
humper@google.com0e515772013-01-07 19:54:40 +000056#endif
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000057
58inline GrGLenum join_to_gl_join(SkPaint::Join join) {
59 static GrGLenum gSkJoinsToGrGLJoins[] = {
60 GR_GL_MITER_REVERT,
61 GR_GL_ROUND,
62 GR_GL_BEVEL
63 };
64 return gSkJoinsToGrGLJoins[join];
65 GR_STATIC_ASSERT(0 == SkPaint::kMiter_Join);
66 GR_STATIC_ASSERT(1 == SkPaint::kRound_Join);
67 GR_STATIC_ASSERT(2 == SkPaint::kBevel_Join);
68 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount);
69}
70
71inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) {
72 static GrGLenum gSkCapsToGrGLCaps[] = {
73 GR_GL_FLAT,
74 GR_GL_ROUND,
75 GR_GL_SQUARE
76 };
77 return gSkCapsToGrGLCaps[cap];
78 GR_STATIC_ASSERT(0 == SkPaint::kButt_Cap);
79 GR_STATIC_ASSERT(1 == SkPaint::kRound_Cap);
80 GR_STATIC_ASSERT(2 == SkPaint::kSquare_Cap);
81 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount);
82}
83
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000084}
85
bsalomon@google.com72830222013-01-23 20:25:22 +000086static const bool kIsWrapped = false; // The constructor creates the GL path object.
87
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000088GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke)
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000089 : INHERITED(gpu, kIsWrapped, path, stroke) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000090 SkASSERT(!path.isEmpty());
91
92 GL_CALL_RET(fPathID, GenPaths(1));
93
94 SkSTArray<16, GrGLubyte, true> pathCommands;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000095 SkSTArray<16, SkPoint, true> pathPoints;
96
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000097 int verbCnt = fSkPath.countVerbs();
98 int pointCnt = fSkPath.countPoints();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000099 pathCommands.resize_back(verbCnt);
100 pathPoints.resize_back(pointCnt);
101
102 // TODO: Direct access to path points since we could pass them on directly.
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +0000103 fSkPath.getPoints(&pathPoints[0], pointCnt);
104 fSkPath.getVerbs(&pathCommands[0], verbCnt);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000106 SkDEBUGCODE(int numPts = 0);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000107 for (int i = 0; i < verbCnt; ++i) {
108 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
109 pathCommands[i] = verb_to_gl_path_cmd(v);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000110 SkDEBUGCODE(numPts += num_pts(v));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000111 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000112 SkASSERT(pathPoints.count() == numPts);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000113
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000114 GL_CALL(PathCommands(fPathID,
115 verbCnt, &pathCommands[0],
116 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000117
118 if (stroke.needToApply()) {
119 GL_CALL(PathParameterf(fPathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth())));
120 GL_CALL(PathParameterf(fPathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter())));
121 GrGLenum join = join_to_gl_join(stroke.getJoin());
122 GL_CALL(PathParameteri(fPathID, GR_GL_PATH_JOIN_STYLE, join));
123 GrGLenum cap = cap_to_gl_cap(stroke.getCap());
124 GL_CALL(PathParameteri(fPathID, GR_GL_PATH_INITIAL_END_CAP, cap));
125 GL_CALL(PathParameteri(fPathID, GR_GL_PATH_TERMINAL_END_CAP, cap));
126
127 // FIXME: try to account for stroking, without rasterizing the stroke.
128 fBounds.outset(SkScalarToFloat(stroke.getWidth()), SkScalarToFloat(stroke.getWidth()));
129 }
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000130}
131
132GrGLPath::~GrGLPath() {
133 this->release();
134}
135
136void GrGLPath::onRelease() {
bsalomon@google.com72830222013-01-23 20:25:22 +0000137 if (0 != fPathID && !this->isWrapped()) {
bsalomon@google.com21320a12012-07-09 14:30:26 +0000138 GL_CALL(DeletePaths(fPathID, 1));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000139 fPathID = 0;
140 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000141
142 INHERITED::onRelease();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000143}
144
145void GrGLPath::onAbandon() {
146 fPathID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000147
148 INHERITED::onAbandon();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000149}