blob: 2fceaa32097618e5a0a2a86b3d87b089f82c54fb [file] [log] [blame]
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001
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
17namespace {
18inline 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
32 GrAssert(verb >= 0 && verb < GR_ARRAY_COUNT(gTable));
33 return gTable[verb];
34}
35
36inline 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
50 GrAssert(verb >= 0 && verb < GR_ARRAY_COUNT(gTable));
51 return gTable[verb];
52}
53}
54
55GrGLPath::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;
60 GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT); // assuming SkPoint is floats
61 SkSTArray<16, SkPoint, true> pathPoints;
62
63 int verbCnt = path.countVerbs();
64 int pointCnt = path.countPoints();
65 pathCommands.resize_back(verbCnt);
66 pathPoints.resize_back(pointCnt);
67
68 // TODO: Direct access to path points since we could pass them on directly.
69 path.getPoints(&pathPoints[0], pointCnt);
70 path.getVerbs(&pathCommands[0], verbCnt);
71
72 GR_DEBUGCODE(int numPts = 0);
73 for (int i = 0; i < verbCnt; ++i) {
74 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
75 pathCommands[i] = verb_to_gl_path_cmd(v);
76 GR_DEBUGCODE(numPts += num_pts(v));
77 }
78 GrAssert(pathPoints.count() == numPts);
79
80 GL_CALL(PathCommands(fPathID,
81 verbCnt, &pathCommands[0],
82 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]));
83 GrRect bounds = path.getBounds();
84 bounds.roundOut(&fBounds);
85}
86
87GrGLPath::~GrGLPath() {
88 this->release();
89}
90
91void GrGLPath::onRelease() {
92 if (0 != fPathID) {
93 GL_CALL(DeletePaths(1, fPathID));
94 fPathID = 0;
95 }
96}
97
98void GrGLPath::onAbandon() {
99 fPathID = 0;
100}
101