blob: e09d073ee2fad4cb332e1ca547c675139ff1dfec [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;
bsalomon@google.comb51c6332012-06-11 15:46:05 +000060#ifndef SK_SCALAR_IS_FLOAT
61 GrCrash("Expected scalar is float.");
62#endif
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000063 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]));
85 GrRect bounds = path.getBounds();
86 bounds.roundOut(&fBounds);
87}
88
89GrGLPath::~GrGLPath() {
90 this->release();
91}
92
93void GrGLPath::onRelease() {
94 if (0 != fPathID) {
95 GL_CALL(DeletePaths(1, fPathID));
96 fPathID = 0;
97 }
98}
99
100void GrGLPath::onAbandon() {
101 fPathID = 0;
102}
103