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" |
| 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 | |
| 17 | namespace { |
| 18 | inline GrGLubyte verb_to_gl_path_cmd(const SkPath::Verb verb) { |
| 19 | static const GrGLubyte gTable[] = { |
| 20 | GR_GL_MOVE_TO, |
| 21 | GR_GL_LINE_TO, |
| 22 | GR_GL_QUADRATIC_CURVE_TO, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame^] | 23 | 0xFF, // conic |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 24 | 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.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame^] | 30 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 31 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 548a433 | 2012-07-11 19:45:22 +0000 | [diff] [blame] | 33 | GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 34 | return gTable[verb]; |
| 35 | } |
| 36 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 37 | #ifdef SK_DEBUG |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 38 | inline int num_pts(const SkPath::Verb verb) { |
| 39 | static const int gTable[] = { |
| 40 | 1, // move |
| 41 | 1, // line |
| 42 | 2, // quad |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame^] | 43 | 2, // conic |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 44 | 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.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame^] | 50 | GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); |
| 51 | GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 52 | |
bsalomon@google.com | 548a433 | 2012-07-11 19:45:22 +0000 | [diff] [blame] | 53 | GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 54 | return gTable[verb]; |
| 55 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 56 | #endif |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 57 | } |
| 58 | |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 59 | static const bool kIsWrapped = false; // The constructor creates the GL path object. |
| 60 | |
| 61 | GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path) : INHERITED(gpu, kIsWrapped) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 62 | GL_CALL_RET(fPathID, GenPaths(1)); |
| 63 | SkPath::Iter iter(path, true); |
| 64 | |
| 65 | SkSTArray<16, GrGLubyte, true> pathCommands; |
bsalomon@google.com | b51c633 | 2012-06-11 15:46:05 +0000 | [diff] [blame] | 66 | #ifndef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | d1e533f | 2012-06-28 18:56:22 +0000 | [diff] [blame] | 67 | GrCrash("Assumes scalar is float."); |
bsalomon@google.com | b51c633 | 2012-06-11 15:46:05 +0000 | [diff] [blame] | 68 | #endif |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 69 | SkSTArray<16, SkPoint, true> pathPoints; |
| 70 | |
| 71 | int verbCnt = path.countVerbs(); |
| 72 | int pointCnt = path.countPoints(); |
| 73 | pathCommands.resize_back(verbCnt); |
| 74 | pathPoints.resize_back(pointCnt); |
| 75 | |
| 76 | // TODO: Direct access to path points since we could pass them on directly. |
| 77 | path.getPoints(&pathPoints[0], pointCnt); |
| 78 | path.getVerbs(&pathCommands[0], verbCnt); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 79 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 80 | GR_DEBUGCODE(int numPts = 0); |
| 81 | for (int i = 0; i < verbCnt; ++i) { |
| 82 | SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]); |
| 83 | pathCommands[i] = verb_to_gl_path_cmd(v); |
| 84 | GR_DEBUGCODE(numPts += num_pts(v)); |
| 85 | } |
| 86 | GrAssert(pathPoints.count() == numPts); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 87 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 88 | GL_CALL(PathCommands(fPathID, |
| 89 | verbCnt, &pathCommands[0], |
| 90 | 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0])); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 91 | fBounds = path.getBounds(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | GrGLPath::~GrGLPath() { |
| 95 | this->release(); |
| 96 | } |
| 97 | |
| 98 | void GrGLPath::onRelease() { |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 99 | if (0 != fPathID && !this->isWrapped()) { |
bsalomon@google.com | 21320a1 | 2012-07-09 14:30:26 +0000 | [diff] [blame] | 100 | GL_CALL(DeletePaths(fPathID, 1)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 101 | fPathID = 0; |
| 102 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 103 | |
| 104 | INHERITED::onRelease(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void GrGLPath::onAbandon() { |
| 108 | fPathID = 0; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 109 | |
| 110 | INHERITED::onAbandon(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 111 | } |