blob: 64d5a559451ddf0dd36eee1b036e78674e46b83e [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
bsalomon@google.com548a4332012-07-11 19:45:22 +000032 GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000033 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
bsalomon@google.com548a4332012-07-11 19:45:22 +000050 GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000051 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
bsalomon@google.comd1e533f2012-06-28 18:56:22 +000061 GrCrash("Assumes scalar is float.");
bsalomon@google.comb51c6332012-06-11 15:46:05 +000062#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]));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000085 fBounds = path.getBounds();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000086}
87
88GrGLPath::~GrGLPath() {
89 this->release();
90}
91
92void GrGLPath::onRelease() {
93 if (0 != fPathID) {
bsalomon@google.com21320a12012-07-09 14:30:26 +000094 GL_CALL(DeletePaths(fPathID, 1));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000095 fPathID = 0;
96 }
97}
98
99void GrGLPath::onAbandon() {
100 fPathID = 0;
101}
102