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" |
| 10 | #include "GrGpuGL.h" |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 11 | #include "SkStrokeRec.h" |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 12 | |
| 13 | #define GPUGL static_cast<GrGpuGL*>(this->getGpu()) |
| 14 | |
| 15 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
| 16 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(GPUGL->glInterface(), R, X) |
| 17 | |
| 18 | namespace { |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 19 | inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 20 | static const GrGLubyte gTable[] = { |
| 21 | GR_GL_MOVE_TO, |
| 22 | GR_GL_LINE_TO, |
| 23 | GR_GL_QUADRATIC_CURVE_TO, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 24 | 0xFF, // conic |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 25 | GR_GL_CUBIC_CURVE_TO, |
| 26 | GR_GL_CLOSE_PATH, |
| 27 | }; |
| 28 | GR_STATIC_ASSERT(0 == SkPath::kMove_Verb); |
| 29 | GR_STATIC_ASSERT(1 == SkPath::kLine_Verb); |
| 30 | GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 31 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 32 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 33 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 34 | SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 35 | return gTable[verb]; |
| 36 | } |
| 37 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 38 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 39 | inline int num_pts(SkPath::Verb verb) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 40 | static const int gTable[] = { |
| 41 | 1, // move |
| 42 | 1, // line |
| 43 | 2, // quad |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 44 | 2, // conic |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 45 | 3, // cubic |
| 46 | 0, // close |
| 47 | }; |
| 48 | GR_STATIC_ASSERT(0 == SkPath::kMove_Verb); |
| 49 | GR_STATIC_ASSERT(1 == SkPath::kLine_Verb); |
| 50 | GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 51 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 52 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 53 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 54 | SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 55 | return gTable[verb]; |
| 56 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 57 | #endif |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 58 | |
| 59 | inline GrGLenum join_to_gl_join(SkPaint::Join join) { |
| 60 | static GrGLenum gSkJoinsToGrGLJoins[] = { |
| 61 | GR_GL_MITER_REVERT, |
| 62 | GR_GL_ROUND, |
| 63 | GR_GL_BEVEL |
| 64 | }; |
| 65 | return gSkJoinsToGrGLJoins[join]; |
| 66 | GR_STATIC_ASSERT(0 == SkPaint::kMiter_Join); |
| 67 | GR_STATIC_ASSERT(1 == SkPaint::kRound_Join); |
| 68 | GR_STATIC_ASSERT(2 == SkPaint::kBevel_Join); |
| 69 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount); |
| 70 | } |
| 71 | |
| 72 | inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) { |
| 73 | static GrGLenum gSkCapsToGrGLCaps[] = { |
| 74 | GR_GL_FLAT, |
| 75 | GR_GL_ROUND, |
| 76 | GR_GL_SQUARE |
| 77 | }; |
| 78 | return gSkCapsToGrGLCaps[cap]; |
| 79 | GR_STATIC_ASSERT(0 == SkPaint::kButt_Cap); |
| 80 | GR_STATIC_ASSERT(1 == SkPaint::kRound_Cap); |
| 81 | GR_STATIC_ASSERT(2 == SkPaint::kSquare_Cap); |
| 82 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount); |
| 83 | } |
| 84 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 87 | static const bool kIsWrapped = false; // The constructor creates the GL path object. |
| 88 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 89 | GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke) |
| 90 | : INHERITED(gpu, kIsWrapped, stroke) { |
bsalomon@google.com | b51c633 | 2012-06-11 15:46:05 +0000 | [diff] [blame] | 91 | #ifndef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | d1e533f | 2012-06-28 18:56:22 +0000 | [diff] [blame] | 92 | GrCrash("Assumes scalar is float."); |
bsalomon@google.com | b51c633 | 2012-06-11 15:46:05 +0000 | [diff] [blame] | 93 | #endif |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 94 | SkASSERT(!path.isEmpty()); |
| 95 | |
| 96 | GL_CALL_RET(fPathID, GenPaths(1)); |
| 97 | |
| 98 | SkSTArray<16, GrGLubyte, true> pathCommands; |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 99 | SkSTArray<16, SkPoint, true> pathPoints; |
| 100 | |
| 101 | int verbCnt = path.countVerbs(); |
| 102 | int pointCnt = path.countPoints(); |
| 103 | pathCommands.resize_back(verbCnt); |
| 104 | pathPoints.resize_back(pointCnt); |
| 105 | |
| 106 | // TODO: Direct access to path points since we could pass them on directly. |
| 107 | path.getPoints(&pathPoints[0], pointCnt); |
| 108 | path.getVerbs(&pathCommands[0], verbCnt); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 109 | |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 110 | SkDEBUGCODE(int numPts = 0); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 111 | for (int i = 0; i < verbCnt; ++i) { |
| 112 | SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]); |
| 113 | pathCommands[i] = verb_to_gl_path_cmd(v); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 114 | SkDEBUGCODE(numPts += num_pts(v)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 115 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 116 | SkASSERT(pathPoints.count() == numPts); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 118 | GL_CALL(PathCommands(fPathID, |
| 119 | verbCnt, &pathCommands[0], |
| 120 | 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0])); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 121 | fBounds = path.getBounds(); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 122 | |
| 123 | if (stroke.needToApply()) { |
| 124 | GL_CALL(PathParameterf(fPathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth()))); |
| 125 | GL_CALL(PathParameterf(fPathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter()))); |
| 126 | GrGLenum join = join_to_gl_join(stroke.getJoin()); |
| 127 | GL_CALL(PathParameteri(fPathID, GR_GL_PATH_JOIN_STYLE, join)); |
| 128 | GrGLenum cap = cap_to_gl_cap(stroke.getCap()); |
| 129 | GL_CALL(PathParameteri(fPathID, GR_GL_PATH_INITIAL_END_CAP, cap)); |
| 130 | GL_CALL(PathParameteri(fPathID, GR_GL_PATH_TERMINAL_END_CAP, cap)); |
| 131 | |
| 132 | // FIXME: try to account for stroking, without rasterizing the stroke. |
| 133 | fBounds.outset(SkScalarToFloat(stroke.getWidth()), SkScalarToFloat(stroke.getWidth())); |
| 134 | } |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | GrGLPath::~GrGLPath() { |
| 138 | this->release(); |
| 139 | } |
| 140 | |
| 141 | void GrGLPath::onRelease() { |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 142 | if (0 != fPathID && !this->isWrapped()) { |
bsalomon@google.com | 21320a1 | 2012-07-09 14:30:26 +0000 | [diff] [blame] | 143 | GL_CALL(DeletePaths(fPathID, 1)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 144 | fPathID = 0; |
| 145 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 146 | |
| 147 | INHERITED::onRelease(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void GrGLPath::onAbandon() { |
| 151 | fPathID = 0; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 152 | |
| 153 | INHERITED::onAbandon(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 154 | } |