blob: a6cf804c2f44196898f8d6e1a1c4ccf79c659e43 [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"
cdaltonc7103a12014-08-11 14:05:05 -070010#include "GrGLPathRendering.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGpuGL.h"
12
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000013namespace {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000014inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000015 static const GrGLubyte gTable[] = {
16 GR_GL_MOVE_TO,
17 GR_GL_LINE_TO,
18 GR_GL_QUADRATIC_CURVE_TO,
reed@google.com277c3f82013-05-31 15:17:50 +000019 0xFF, // conic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000020 GR_GL_CUBIC_CURVE_TO,
21 GR_GL_CLOSE_PATH,
22 };
23 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
24 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
25 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000026 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
27 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000028
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000029 SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000030 return gTable[verb];
31}
32
humper@google.com0e515772013-01-07 19:54:40 +000033#ifdef SK_DEBUG
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000034inline int num_pts(SkPath::Verb verb) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000035 static const int gTable[] = {
36 1, // move
37 1, // line
38 2, // quad
reed@google.com277c3f82013-05-31 15:17:50 +000039 2, // conic
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000040 3, // cubic
41 0, // close
42 };
43 GR_STATIC_ASSERT(0 == SkPath::kMove_Verb);
44 GR_STATIC_ASSERT(1 == SkPath::kLine_Verb);
45 GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb);
reed@google.com277c3f82013-05-31 15:17:50 +000046 GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb);
47 GR_STATIC_ASSERT(5 == SkPath::kClose_Verb);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000048
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000049 SkASSERT(verb >= 0 && (size_t)verb < SK_ARRAY_COUNT(gTable));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000050 return gTable[verb];
51}
humper@google.com0e515772013-01-07 19:54:40 +000052#endif
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000053
54inline GrGLenum join_to_gl_join(SkPaint::Join join) {
55 static GrGLenum gSkJoinsToGrGLJoins[] = {
56 GR_GL_MITER_REVERT,
57 GR_GL_ROUND,
58 GR_GL_BEVEL
59 };
60 return gSkJoinsToGrGLJoins[join];
61 GR_STATIC_ASSERT(0 == SkPaint::kMiter_Join);
62 GR_STATIC_ASSERT(1 == SkPaint::kRound_Join);
63 GR_STATIC_ASSERT(2 == SkPaint::kBevel_Join);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000064 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000065}
66
67inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) {
68 static GrGLenum gSkCapsToGrGLCaps[] = {
69 GR_GL_FLAT,
70 GR_GL_ROUND,
71 GR_GL_SQUARE
72 };
73 return gSkCapsToGrGLCaps[cap];
74 GR_STATIC_ASSERT(0 == SkPaint::kButt_Cap);
75 GR_STATIC_ASSERT(1 == SkPaint::kRound_Cap);
76 GR_STATIC_ASSERT(2 == SkPaint::kSquare_Cap);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000077 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000078}
79
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000080}
81
bsalomon@google.com72830222013-01-23 20:25:22 +000082static const bool kIsWrapped = false; // The constructor creates the GL path object.
83
cdaltonc7103a12014-08-11 14:05:05 -070084void GrGLPath::InitPathObject(GrGpuGL* gpu,
bungemanaf13c7c2014-08-06 11:14:31 -040085 GrGLuint pathID,
86 const SkPath& skPath,
87 const SkStrokeRec& stroke) {
cdaltonfa3a41f2014-08-29 12:18:36 -070088 if (!skPath.isEmpty()) {
89 SkSTArray<16, GrGLubyte, true> pathCommands;
90 SkSTArray<16, SkPoint, true> pathPoints;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000091
cdaltonfa3a41f2014-08-29 12:18:36 -070092 int verbCnt = skPath.countVerbs();
93 int pointCnt = skPath.countPoints();
94 pathCommands.resize_back(verbCnt);
95 pathPoints.resize_back(pointCnt);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000096
cdaltonfa3a41f2014-08-29 12:18:36 -070097 // TODO: Direct access to path points since we could pass them on directly.
98 skPath.getPoints(&pathPoints[0], pointCnt);
99 skPath.getVerbs(&pathCommands[0], verbCnt);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100
cdaltonfa3a41f2014-08-29 12:18:36 -0700101 SkDEBUGCODE(int numPts = 0);
102 for (int i = 0; i < verbCnt; ++i) {
103 SkPath::Verb v = static_cast<SkPath::Verb>(pathCommands[i]);
104 pathCommands[i] = verb_to_gl_path_cmd(v);
105 SkDEBUGCODE(numPts += num_pts(v));
106 }
107 SkASSERT(pathPoints.count() == numPts);
108
109 GR_GL_CALL(gpu->glInterface(),
110 PathCommands(pathID, verbCnt, &pathCommands[0],
111 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0]));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000112 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000113
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000114 if (stroke.needToApply()) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700115 SkASSERT(!stroke.isHairlineStyle());
kkinnunen5b653572014-08-20 04:13:27 -0700116 GR_GL_CALL(gpu->glInterface(),
117 PathParameterf(pathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth())));
118 GR_GL_CALL(gpu->glInterface(),
119 PathParameterf(pathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter())));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000120 GrGLenum join = join_to_gl_join(stroke.getJoin());
kkinnunen5b653572014-08-20 04:13:27 -0700121 GR_GL_CALL(gpu->glInterface(),
122 PathParameteri(pathID, GR_GL_PATH_JOIN_STYLE, join));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000123 GrGLenum cap = cap_to_gl_cap(stroke.getCap());
kkinnunen5b653572014-08-20 04:13:27 -0700124 GR_GL_CALL(gpu->glInterface(),
125 PathParameteri(pathID, GR_GL_PATH_INITIAL_END_CAP, cap));
126 GR_GL_CALL(gpu->glInterface(),
127 PathParameteri(pathID, GR_GL_PATH_TERMINAL_END_CAP, cap));
cdaltonb85a0aa2014-07-21 15:32:44 -0700128 }
129}
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000130
cdaltonb85a0aa2014-07-21 15:32:44 -0700131GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke)
cdaltonc7103a12014-08-11 14:05:05 -0700132 : INHERITED(gpu, kIsWrapped, path, stroke),
kkinnunenccdaa042014-08-20 01:36:23 -0700133 fPathID(gpu->glPathRendering()->genPaths(1)) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700134
cdaltonc7103a12014-08-11 14:05:05 -0700135 InitPathObject(gpu, fPathID, fSkPath, stroke);
cdaltonb85a0aa2014-07-21 15:32:44 -0700136
137 if (stroke.needToApply()) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000138 // FIXME: try to account for stroking, without rasterizing the stroke.
cdaltonb85a0aa2014-07-21 15:32:44 -0700139 fBounds.outset(stroke.getWidth(), stroke.getWidth());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000140 }
bsalomon16961262014-08-26 14:01:07 -0700141 this->registerWithCache();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000142}
143
144GrGLPath::~GrGLPath() {
145 this->release();
146}
147
148void GrGLPath::onRelease() {
bsalomon@google.com72830222013-01-23 20:25:22 +0000149 if (0 != fPathID && !this->isWrapped()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700150 static_cast<GrGpuGL*>(this->getGpu())->glPathRendering()->deletePaths(fPathID, 1);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000151 fPathID = 0;
152 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000153
154 INHERITED::onRelease();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000155}
156
157void GrGLPath::onAbandon() {
158 fPathID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000159
160 INHERITED::onAbandon();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000161}