bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 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" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 10 | #include "GrGLPathRendering.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 11 | #include "GrGLGpu.h" |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 13 | namespace { |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 14 | inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 15 | static const GrGLubyte gTable[] = { |
| 16 | GR_GL_MOVE_TO, |
| 17 | GR_GL_LINE_TO, |
| 18 | GR_GL_QUADRATIC_CURVE_TO, |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 19 | GR_GL_CONIC_CURVE_TO, |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 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); |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 26 | GR_STATIC_ASSERT(3 == SkPath::kConic_Verb); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 27 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 28 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 30 | SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 31 | return gTable[verb]; |
| 32 | } |
| 33 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 34 | #ifdef SK_DEBUG |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 35 | inline int num_coords(SkPath::Verb verb) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 36 | static const int gTable[] = { |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 37 | 2, // move |
| 38 | 2, // line |
| 39 | 4, // quad |
| 40 | 5, // conic |
| 41 | 6, // cubic |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 42 | 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); |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 47 | GR_STATIC_ASSERT(3 == SkPath::kConic_Verb); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 48 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 49 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 50 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 51 | SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 52 | return gTable[verb]; |
| 53 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 54 | #endif |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 55 | |
| 56 | inline 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.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 66 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | inline 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.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 79 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 80 | } |
| 81 | |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 82 | inline 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.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 89 | } |
| 90 | |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 91 | static const bool kIsWrapped = false; // The constructor creates the GL path object. |
| 92 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 93 | void GrGLPath::InitPathObject(GrGLGpu* gpu, |
bungeman | af13c7c | 2014-08-06 11:14:31 -0400 | [diff] [blame] | 94 | GrGLuint pathID, |
| 95 | const SkPath& skPath, |
| 96 | const SkStrokeRec& stroke) { |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 97 | if (!skPath.isEmpty()) { |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 98 | int verbCnt = skPath.countVerbs(); |
| 99 | int pointCnt = skPath.countPoints(); |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 100 | int minCoordCnt = pointCnt * 2; |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 101 | |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 102 | SkSTArray<16, GrGLubyte, true> pathCommands(verbCnt); |
| 103 | SkSTArray<16, GrGLfloat, true> pathCoords(minCoordCnt); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 104 | |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 105 | 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 | } |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 162 | } |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 163 | |
kkinnunen | e097be5 | 2014-11-19 22:49:03 -0800 | [diff] [blame] | 164 | 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])); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 169 | } else { |
| 170 | GR_GL_CALL(gpu->glInterface(), PathCommands(pathID, 0, NULL, 0, GR_GL_FLOAT, NULL)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 171 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 172 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 173 | if (stroke.needToApply()) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 174 | SkASSERT(!stroke.isHairlineStyle()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 175 | 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.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 179 | GrGLenum join = join_to_gl_join(stroke.getJoin()); |
kkinnunen | 542ecbb | 2014-11-18 05:13:48 -0800 | [diff] [blame] | 180 | GR_GL_CALL(gpu->glInterface(), PathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join)); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 181 | GrGLenum cap = cap_to_gl_cap(stroke.getCap()); |
kkinnunen | 542ecbb | 2014-11-18 05:13:48 -0800 | [diff] [blame] | 182 | GR_GL_CALL(gpu->glInterface(), PathParameteri(pathID, GR_GL_PATH_END_CAPS, cap)); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 185 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 186 | GrGLPath::GrGLPath(GrGLGpu* gpu, const SkPath& path, const SkStrokeRec& stroke) |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 187 | : INHERITED(gpu, kIsWrapped, path, stroke), |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 188 | fPathID(gpu->glPathRendering()->genPaths(1)) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 189 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 190 | InitPathObject(gpu, fPathID, fSkPath, stroke); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 191 | |
| 192 | if (stroke.needToApply()) { |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 193 | // FIXME: try to account for stroking, without rasterizing the stroke. |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 194 | fBounds.outset(stroke.getWidth(), stroke.getWidth()); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 195 | } |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 196 | this->registerWithCache(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 197 | } |
| 198 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 199 | void GrGLPath::onRelease() { |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 200 | if (0 != fPathID && !this->isWrapped()) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 201 | static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fPathID, 1); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 202 | fPathID = 0; |
| 203 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 204 | |
| 205 | INHERITED::onRelease(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void GrGLPath::onAbandon() { |
| 209 | fPathID = 0; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 210 | |
| 211 | INHERITED::onAbandon(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 212 | } |