blob: f5f66b8f8a8365b428895d857160d7b48d6cbf87 [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"
cdaltonc7103a12014-08-11 14:05:05 -070010#include "GrGLPathRendering.h"
jvanverth39edf762014-12-22 11:44:19 -080011#include "GrGLGpu.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000012
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000013namespace {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000014inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000015 static const GrGLubyte gTable[] = {
16 GR_GL_MOVE_TO,
17 GR_GL_LINE_TO,
18 GR_GL_QUADRATIC_CURVE_TO,
kkinnunene097be52014-11-19 22:49:03 -080019 GR_GL_CONIC_CURVE_TO,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000020 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);
kkinnunene097be52014-11-19 22:49:03 -080026 GR_STATIC_ASSERT(3 == SkPath::kConic_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000027 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
28 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000029
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000030 SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000031 return gTable[verb];
32}
33
humper@google.com0e515772013-01-07 19:54:40 +000034#ifdef SK_DEBUG
kkinnunene097be52014-11-19 22:49:03 -080035inline int num_coords(SkPath::Verb verb) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000036 static const int gTable[] = {
kkinnunene097be52014-11-19 22:49:03 -080037 2, // move
38 2, // line
39 4, // quad
40 5, // conic
41 6, // cubic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000042 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);
kkinnunene097be52014-11-19 22:49:03 -080047 GR_STATIC_ASSERT(3 == SkPath::kConic_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000048 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
49 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000050
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000051 SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000052 return gTable[verb];
53}
humper@google.com0e515772013-01-07 19:54:40 +000054#endif
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000055
56inline 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);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000066 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000067}
68
69inline 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);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000079 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000080}
81
kkinnunene097be52014-11-19 22:49:03 -080082inline 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}
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000089}
90
bsalomon@google.com72830222013-01-23 20:25:22 +000091static const bool kIsWrapped = false; // The constructor creates the GL path object.
92
bsalomon861e1032014-12-16 07:33:49 -080093void GrGLPath::InitPathObject(GrGLGpu* gpu,
bungemanaf13c7c2014-08-06 11:14:31 -040094 GrGLuint pathID,
95 const SkPath& skPath,
96 const SkStrokeRec& stroke) {
cdaltonfa3a41f2014-08-29 12:18:36 -070097 if (!skPath.isEmpty()) {
cdaltonfa3a41f2014-08-29 12:18:36 -070098 int verbCnt = skPath.countVerbs();
99 int pointCnt = skPath.countPoints();
kkinnunene097be52014-11-19 22:49:03 -0800100 int minCoordCnt = pointCnt * 2;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000101
kkinnunene097be52014-11-19 22:49:03 -0800102 SkSTArray<16, GrGLubyte, true> pathCommands(verbCnt);
103 SkSTArray<16, GrGLfloat, true> pathCoords(minCoordCnt);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104
kkinnunene097be52014-11-19 22:49:03 -0800105 SkDEBUGCODE(int numCoords = 0);
106
107 if ((skPath.getSegmentMasks() & SkPath::kConic_SegmentMask) == 0) {
108 // This branch does type punning, converting SkPoint* to GrGLfloat*.
109 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(GrGLfloat) * 2, sk_point_not_two_floats);
110 // This branch does not convert with SkScalarToFloat.
111#ifndef SK_SCALAR_IS_FLOAT
112#error Need SK_SCALAR_IS_FLOAT.
113#endif
114 pathCommands.resize_back(verbCnt);
115 pathCoords.resize_back(minCoordCnt);
116 skPath.getPoints(reinterpret_cast<SkPoint*>(&pathCoords[0]), pointCnt);
117 skPath.getVerbs(&pathCommands[0], verbCnt);
118 for (int i = 0; i < verbCnt; ++i) {
119 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
120 pathCommands[i] = verb_to_gl_path_cmd(v);
121 SkDEBUGCODE(numCoords += num_coords(v));
122 }
123 } else {
124 SkPoint points[4];
125 SkPath::RawIter iter(skPath);
126 SkPath::Verb verb;
127 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
128 pathCommands.push_back(verb_to_gl_path_cmd(verb));
129 GrGLfloat coords[6];
130 int coordsForVerb;
131 switch (verb) {
132 case SkPath::kMove_Verb:
133 points_to_coords(points, 0, 1, coords);
134 coordsForVerb = 2;
135 break;
136 case SkPath::kLine_Verb:
137 points_to_coords(points, 1, 1, coords);
138 coordsForVerb = 2;
139 break;
140 case SkPath::kConic_Verb:
141 points_to_coords(points, 1, 2, coords);
142 coords[4] = SkScalarToFloat(iter.conicWeight());
143 coordsForVerb = 5;
144 break;
145 case SkPath::kQuad_Verb:
146 points_to_coords(points, 1, 2, coords);
147 coordsForVerb = 4;
148 break;
149 case SkPath::kCubic_Verb:
150 points_to_coords(points, 1, 3, coords);
151 coordsForVerb = 6;
152 break;
153 case SkPath::kClose_Verb:
154 continue;
155 default:
156 SkASSERT(false); // Not reached.
157 continue;
158 }
159 SkDEBUGCODE(numCoords += num_coords(verb));
160 pathCoords.push_back_n(coordsForVerb, coords);
161 }
cdaltonfa3a41f2014-08-29 12:18:36 -0700162 }
cdaltonfa3a41f2014-08-29 12:18:36 -0700163
kkinnunene097be52014-11-19 22:49:03 -0800164 SkASSERT(verbCnt == pathCommands.count());
165 SkASSERT(numCoords == pathCoords.count());
166
167 GR_GL_CALL(gpu->glInterface(), PathCommands(pathID, pathCommands.count(), &pathCommands[0],
168 pathCoords.count(), GR_GL_FLOAT, &pathCoords[0]));
cdalton855d83f2014-09-18 13:51:53 -0700169 } else {
170 GR_GL_CALL(gpu->glInterface(), PathCommands(pathID, 0, NULL, 0, GR_GL_FLOAT, NULL));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000171 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000172
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000173 if (stroke.needToApply()) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700174 SkASSERT(!stroke.isHairlineStyle());
kkinnunen5b653572014-08-20 04:13:27 -0700175 GR_GL_CALL(gpu->glInterface(),
176 PathParameterf(pathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth())));
177 GR_GL_CALL(gpu->glInterface(),
178 PathParameterf(pathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter())));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000179 GrGLenum join = join_to_gl_join(stroke.getJoin());
kkinnunen542ecbb2014-11-18 05:13:48 -0800180 GR_GL_CALL(gpu->glInterface(), PathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000181 GrGLenum cap = cap_to_gl_cap(stroke.getCap());
kkinnunen542ecbb2014-11-18 05:13:48 -0800182 GR_GL_CALL(gpu->glInterface(), PathParameteri(pathID, GR_GL_PATH_END_CAPS, cap));
cdaltonb85a0aa2014-07-21 15:32:44 -0700183 }
184}
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000185
bsalomon861e1032014-12-16 07:33:49 -0800186GrGLPath::GrGLPath(GrGLGpu* gpu, const SkPath& path, const SkStrokeRec& stroke)
cdaltonc7103a12014-08-11 14:05:05 -0700187 : INHERITED(gpu, kIsWrapped, path, stroke),
kkinnunenccdaa042014-08-20 01:36:23 -0700188 fPathID(gpu->glPathRendering()->genPaths(1)) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700189
cdaltonc7103a12014-08-11 14:05:05 -0700190 InitPathObject(gpu, fPathID, fSkPath, stroke);
cdaltonb85a0aa2014-07-21 15:32:44 -0700191
192 if (stroke.needToApply()) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000193 // FIXME: try to account for stroking, without rasterizing the stroke.
cdaltonb85a0aa2014-07-21 15:32:44 -0700194 fBounds.outset(stroke.getWidth(), stroke.getWidth());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000195 }
bsalomon16961262014-08-26 14:01:07 -0700196 this->registerWithCache();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000197}
198
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000199void GrGLPath::onRelease() {
bsalomon@google.com72830222013-01-23 20:25:22 +0000200 if (0 != fPathID && !this->isWrapped()) {
bsalomon861e1032014-12-16 07:33:49 -0800201 static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fPathID, 1);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000202 fPathID = 0;
203 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000204
205 INHERITED::onRelease();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000206}
207
208void GrGLPath::onAbandon() {
209 fPathID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000210
211 INHERITED::onAbandon();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000212}