blob: 1de8713774373b6c013a30dc5cf1875ad5271a04 [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
humper@google.com0e515772013-01-07 19:54:40 +000036#ifdef SK_DEBUG
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000037inline int num_pts(const SkPath::Verb verb) {
38 static const int gTable[] = {
39 1, // move
40 1, // line
41 2, // quad
42 3, // cubic
43 0, // close
44 };
45 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
46 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
47 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
48 GR_STATIC_ASSERT(3 == SkPath::kCubic_Verb);
49 GR_STATIC_ASSERT(4 == SkPath::kClose_Verb);
50
bsalomon@google.com548a4332012-07-11 19:45:22 +000051 GrAssert(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000052 return gTable[verb];
53}
humper@google.com0e515772013-01-07 19:54:40 +000054#endif
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000055}
56
57GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path) : INHERITED(gpu) {
58 GL_CALL_RET(fPathID, GenPaths(1));
59 SkPath::Iter iter(path, true);
60
61 SkSTArray<16, GrGLubyte, true> pathCommands;
bsalomon@google.comb51c6332012-06-11 15:46:05 +000062#ifndef SK_SCALAR_IS_FLOAT
bsalomon@google.comd1e533f2012-06-28 18:56:22 +000063 GrCrash("Assumes scalar is float.");
bsalomon@google.comb51c6332012-06-11 15:46:05 +000064#endif
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000065 SkSTArray<16, SkPoint, true> pathPoints;
66
67 int verbCnt = path.countVerbs();
68 int pointCnt = path.countPoints();
69 pathCommands.resize_back(verbCnt);
70 pathPoints.resize_back(pointCnt);
71
72 // TODO: Direct access to path points since we could pass them on directly.
73 path.getPoints(&pathPoints[0], pointCnt);
74 path.getVerbs(&pathCommands[0], verbCnt);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000076 GR_DEBUGCODE(int numPts = 0);
77 for (int i = 0; i < verbCnt; ++i) {
78 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
79 pathCommands[i] = verb_to_gl_path_cmd(v);
80 GR_DEBUGCODE(numPts += num_pts(v));
81 }
82 GrAssert(pathPoints.count() == numPts);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000084 GL_CALL(PathCommands(fPathID,
85 verbCnt, &pathCommands[0],
86 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000087 fBounds = path.getBounds();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000088}
89
90GrGLPath::~GrGLPath() {
91 this->release();
92}
93
94void GrGLPath::onRelease() {
95 if (0 != fPathID) {
bsalomon@google.com21320a12012-07-09 14:30:26 +000096 GL_CALL(DeletePaths(fPathID, 1));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000097 fPathID = 0;
98 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000099
100 INHERITED::onRelease();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000101}
102
103void GrGLPath::onAbandon() {
104 fPathID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000105
106 INHERITED::onAbandon();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000107}
108