blob: 7bf7df0741e91f12731c1c270232f27bb95b93b1 [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,
reed@google.com277c3f82013-05-31 15:17:50 +000023 0xFF, // conic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000024 GR_GL_CUBIC_CURVE_TO,
25 GR_GL_CLOSE_PATH,
26 };
27 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
28 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
29 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000030 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
31 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000032
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000033 SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000034 return gTable[verb];
35}
36
humper@google.com0e515772013-01-07 19:54:40 +000037#ifdef SK_DEBUG
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000038inline int num_pts(const SkPath::Verb verb) {
39 static const int gTable[] = {
40 1, // move
41 1, // line
42 2, // quad
reed@google.com277c3f82013-05-31 15:17:50 +000043 2, // conic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000044 3, // cubic
45 0, // close
46 };
47 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
48 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
49 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000050 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
51 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000052
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053 SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000054 return gTable[verb];
55}
humper@google.com0e515772013-01-07 19:54:40 +000056#endif
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000057}
58
bsalomon@google.com72830222013-01-23 20:25:22 +000059static const bool kIsWrapped = false; // The constructor creates the GL path object.
60
61GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path) : INHERITED(gpu, kIsWrapped) {
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
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000065 SkASSERT(!path.isEmpty());
66
67 GL_CALL_RET(fPathID, GenPaths(1));
68
69 SkSTArray<16, GrGLubyte, true> pathCommands;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000070 SkSTArray<16, SkPoint, true> pathPoints;
71
72 int verbCnt = path.countVerbs();
73 int pointCnt = path.countPoints();
74 pathCommands.resize_back(verbCnt);
75 pathPoints.resize_back(pointCnt);
76
77 // TODO: Direct access to path points since we could pass them on directly.
78 path.getPoints(&pathPoints[0], pointCnt);
79 path.getVerbs(&pathCommands[0], verbCnt);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000081 GR_DEBUGCODE(int numPts = 0);
82 for (int i = 0; i < verbCnt; ++i) {
83 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
84 pathCommands[i] = verb_to_gl_path_cmd(v);
85 GR_DEBUGCODE(numPts += num_pts(v));
86 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000087 SkASSERT(pathPoints.count() == numPts);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000089 GL_CALL(PathCommands(fPathID,
90 verbCnt, &pathCommands[0],
91 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000092 fBounds = path.getBounds();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000093}
94
95GrGLPath::~GrGLPath() {
96 this->release();
97}
98
99void GrGLPath::onRelease() {
bsalomon@google.com72830222013-01-23 20:25:22 +0000100 if (0 != fPathID && !this->isWrapped()) {
bsalomon@google.com21320a12012-07-09 14:30:26 +0000101 GL_CALL(DeletePaths(fPathID, 1));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000102 fPathID = 0;
103 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000104
105 INHERITED::onRelease();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000106}
107
108void GrGLPath::onAbandon() {
109 fPathID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000110
111 INHERITED::onAbandon();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000112}