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" |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 11 | #include "GrGpuGL.h" |
| 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, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 19 | 0xFF, // conic |
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); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 26 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 27 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 28 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 29 | SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 30 | return gTable[verb]; |
| 31 | } |
| 32 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 33 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 34 | inline int num_pts(SkPath::Verb verb) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 35 | static const int gTable[] = { |
| 36 | 1, // move |
| 37 | 1, // line |
| 38 | 2, // quad |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 39 | 2, // conic |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 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); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 46 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 47 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 49 | SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 50 | return gTable[verb]; |
| 51 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 52 | #endif |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 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); |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 64 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 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); |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 77 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 80 | } |
| 81 | |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 82 | static const bool kIsWrapped = false; // The constructor creates the GL path object. |
| 83 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 84 | void GrGLPath::InitPathObject(GrGpuGL* gpu, |
bungeman | af13c7c | 2014-08-06 11:14:31 -0400 | [diff] [blame] | 85 | GrGLuint pathID, |
| 86 | const SkPath& skPath, |
| 87 | const SkStrokeRec& stroke) { |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 88 | if (!skPath.isEmpty()) { |
| 89 | SkSTArray<16, GrGLubyte, true> pathCommands; |
| 90 | SkSTArray<16, SkPoint, true> pathPoints; |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 91 | |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 92 | int verbCnt = skPath.countVerbs(); |
| 93 | int pointCnt = skPath.countPoints(); |
| 94 | pathCommands.resize_back(verbCnt); |
| 95 | pathPoints.resize_back(pointCnt); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 96 | |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 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); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 100 | |
cdalton | fa3a41f | 2014-08-29 12:18:36 -0700 | [diff] [blame] | 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 | GR_GL_CALL(gpu->glInterface(), |
| 110 | PathCommands(pathID, verbCnt, &pathCommands[0], |
| 111 | 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0])); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 112 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 114 | if (stroke.needToApply()) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 115 | SkASSERT(!stroke.isHairlineStyle()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 116 | GR_GL_CALL(gpu->glInterface(), |
| 117 | PathParameterf(pathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth()))); |
| 118 | GR_GL_CALL(gpu->glInterface(), |
| 119 | PathParameterf(pathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter()))); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 120 | GrGLenum join = join_to_gl_join(stroke.getJoin()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 121 | GR_GL_CALL(gpu->glInterface(), |
| 122 | PathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join)); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 123 | GrGLenum cap = cap_to_gl_cap(stroke.getCap()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 124 | GR_GL_CALL(gpu->glInterface(), |
| 125 | PathParameteri(pathID, GR_GL_PATH_INITIAL_END_CAP, cap)); |
| 126 | GR_GL_CALL(gpu->glInterface(), |
| 127 | PathParameteri(pathID, GR_GL_PATH_TERMINAL_END_CAP, cap)); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 130 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 131 | GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke) |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 132 | : INHERITED(gpu, kIsWrapped, path, stroke), |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 133 | fPathID(gpu->glPathRendering()->genPaths(1)) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 134 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 135 | InitPathObject(gpu, fPathID, fSkPath, stroke); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 136 | |
| 137 | if (stroke.needToApply()) { |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 138 | // FIXME: try to account for stroking, without rasterizing the stroke. |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 139 | fBounds.outset(stroke.getWidth(), stroke.getWidth()); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 140 | } |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 141 | this->registerWithCache(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | GrGLPath::~GrGLPath() { |
| 145 | this->release(); |
| 146 | } |
| 147 | |
| 148 | void GrGLPath::onRelease() { |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 149 | if (0 != fPathID && !this->isWrapped()) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 150 | static_cast<GrGpuGL*>(this->getGpu())->glPathRendering()->deletePaths(fPathID, 1); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 151 | fPathID = 0; |
| 152 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 153 | |
| 154 | INHERITED::onRelease(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void GrGLPath::onAbandon() { |
| 158 | fPathID = 0; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 159 | |
| 160 | INHERITED::onAbandon(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 161 | } |