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, |
| 23 | GR_GL_CUBIC_CURVE_TO, |
| 24 | GR_GL_CLOSE_PATH, |
| 25 | }; |
| 26 | GR_STATIC_ASSERT(0 == SkPath::kMove_Verb); |
| 27 | GR_STATIC_ASSERT(1 == SkPath::kLine_Verb); |
| 28 | GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb); |
| 29 | GR_STATIC_ASSERT(3 == SkPath::kCubic_Verb); |
| 30 | GR_STATIC_ASSERT(4 == SkPath::kClose_Verb); |
| 31 | |
bsalomon@google.com | 548a433 | 2012-07-11 19:45:22 +0000 | [diff] [blame^] | 32 | GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 33 | return gTable[verb]; |
| 34 | } |
| 35 | |
| 36 | inline int num_pts(const SkPath::Verb verb) { |
| 37 | static const int gTable[] = { |
| 38 | 1, // move |
| 39 | 1, // line |
| 40 | 2, // quad |
| 41 | 3, // cubic |
| 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); |
| 47 | GR_STATIC_ASSERT(3 == SkPath::kCubic_Verb); |
| 48 | GR_STATIC_ASSERT(4 == SkPath::kClose_Verb); |
| 49 | |
bsalomon@google.com | 548a433 | 2012-07-11 19:45:22 +0000 | [diff] [blame^] | 50 | GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 51 | return gTable[verb]; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path) : INHERITED(gpu) { |
| 56 | GL_CALL_RET(fPathID, GenPaths(1)); |
| 57 | SkPath::Iter iter(path, true); |
| 58 | |
| 59 | SkSTArray<16, GrGLubyte, true> pathCommands; |
bsalomon@google.com | b51c633 | 2012-06-11 15:46:05 +0000 | [diff] [blame] | 60 | #ifndef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | d1e533f | 2012-06-28 18:56:22 +0000 | [diff] [blame] | 61 | GrCrash("Assumes scalar is float."); |
bsalomon@google.com | b51c633 | 2012-06-11 15:46:05 +0000 | [diff] [blame] | 62 | #endif |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 63 | SkSTArray<16, SkPoint, true> pathPoints; |
| 64 | |
| 65 | int verbCnt = path.countVerbs(); |
| 66 | int pointCnt = path.countPoints(); |
| 67 | pathCommands.resize_back(verbCnt); |
| 68 | pathPoints.resize_back(pointCnt); |
| 69 | |
| 70 | // TODO: Direct access to path points since we could pass them on directly. |
| 71 | path.getPoints(&pathPoints[0], pointCnt); |
| 72 | path.getVerbs(&pathCommands[0], verbCnt); |
| 73 | |
| 74 | GR_DEBUGCODE(int numPts = 0); |
| 75 | for (int i = 0; i < verbCnt; ++i) { |
| 76 | SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]); |
| 77 | pathCommands[i] = verb_to_gl_path_cmd(v); |
| 78 | GR_DEBUGCODE(numPts += num_pts(v)); |
| 79 | } |
| 80 | GrAssert(pathPoints.count() == numPts); |
| 81 | |
| 82 | GL_CALL(PathCommands(fPathID, |
| 83 | verbCnt, &pathCommands[0], |
| 84 | 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0])); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 85 | fBounds = path.getBounds(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | GrGLPath::~GrGLPath() { |
| 89 | this->release(); |
| 90 | } |
| 91 | |
| 92 | void GrGLPath::onRelease() { |
| 93 | if (0 != fPathID) { |
bsalomon@google.com | 21320a1 | 2012-07-09 14:30:26 +0000 | [diff] [blame] | 94 | GL_CALL(DeletePaths(fPathID, 1)); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 95 | fPathID = 0; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void GrGLPath::onAbandon() { |
| 100 | fPathID = 0; |
| 101 | } |
| 102 | |