michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrGLPath.h" michael@0: #include "GrGpuGL.h" michael@0: michael@0: #define GPUGL static_cast(this->getGpu()) michael@0: michael@0: #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) michael@0: #define GL_CALL_RET(R, X) GR_GL_CALL_RET(GPUGL->glInterface(), R, X) michael@0: michael@0: namespace { michael@0: inline GrGLubyte verb_to_gl_path_cmd(SkPath::Verb verb) { michael@0: static const GrGLubyte gTable[] = { michael@0: GR_GL_MOVE_TO, michael@0: GR_GL_LINE_TO, michael@0: GR_GL_QUADRATIC_CURVE_TO, michael@0: 0xFF, // conic michael@0: GR_GL_CUBIC_CURVE_TO, michael@0: GR_GL_CLOSE_PATH, michael@0: }; michael@0: GR_STATIC_ASSERT(0 == SkPath::kMove_Verb); michael@0: GR_STATIC_ASSERT(1 == SkPath::kLine_Verb); michael@0: GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb); michael@0: GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); michael@0: GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); michael@0: michael@0: SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); michael@0: return gTable[verb]; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: inline int num_pts(SkPath::Verb verb) { michael@0: static const int gTable[] = { michael@0: 1, // move michael@0: 1, // line michael@0: 2, // quad michael@0: 2, // conic michael@0: 3, // cubic michael@0: 0, // close michael@0: }; michael@0: GR_STATIC_ASSERT(0 == SkPath::kMove_Verb); michael@0: GR_STATIC_ASSERT(1 == SkPath::kLine_Verb); michael@0: GR_STATIC_ASSERT(2 == SkPath::kQuad_Verb); michael@0: GR_STATIC_ASSERT(4 == SkPath::kCubic_Verb); michael@0: GR_STATIC_ASSERT(5 == SkPath::kClose_Verb); michael@0: michael@0: SkASSERT(verb >= 0 && (size_t)verb < GR_ARRAY_COUNT(gTable)); michael@0: return gTable[verb]; michael@0: } michael@0: #endif michael@0: michael@0: inline GrGLenum join_to_gl_join(SkPaint::Join join) { michael@0: static GrGLenum gSkJoinsToGrGLJoins[] = { michael@0: GR_GL_MITER_REVERT, michael@0: GR_GL_ROUND, michael@0: GR_GL_BEVEL michael@0: }; michael@0: return gSkJoinsToGrGLJoins[join]; michael@0: GR_STATIC_ASSERT(0 == SkPaint::kMiter_Join); michael@0: GR_STATIC_ASSERT(1 == SkPaint::kRound_Join); michael@0: GR_STATIC_ASSERT(2 == SkPaint::kBevel_Join); michael@0: GR_STATIC_ASSERT(GR_ARRAY_COUNT(gSkJoinsToGrGLJoins) == SkPaint::kJoinCount); michael@0: } michael@0: michael@0: inline GrGLenum cap_to_gl_cap(SkPaint::Cap cap) { michael@0: static GrGLenum gSkCapsToGrGLCaps[] = { michael@0: GR_GL_FLAT, michael@0: GR_GL_ROUND, michael@0: GR_GL_SQUARE michael@0: }; michael@0: return gSkCapsToGrGLCaps[cap]; michael@0: GR_STATIC_ASSERT(0 == SkPaint::kButt_Cap); michael@0: GR_STATIC_ASSERT(1 == SkPaint::kRound_Cap); michael@0: GR_STATIC_ASSERT(2 == SkPaint::kSquare_Cap); michael@0: GR_STATIC_ASSERT(GR_ARRAY_COUNT(gSkCapsToGrGLCaps) == SkPaint::kCapCount); michael@0: } michael@0: michael@0: } michael@0: michael@0: static const bool kIsWrapped = false; // The constructor creates the GL path object. michael@0: michael@0: GrGLPath::GrGLPath(GrGpuGL* gpu, const SkPath& path, const SkStrokeRec& stroke) michael@0: : INHERITED(gpu, kIsWrapped, path, stroke) { michael@0: SkASSERT(!path.isEmpty()); michael@0: michael@0: GL_CALL_RET(fPathID, GenPaths(1)); michael@0: michael@0: SkSTArray<16, GrGLubyte, true> pathCommands; michael@0: SkSTArray<16, SkPoint, true> pathPoints; michael@0: michael@0: int verbCnt = fSkPath.countVerbs(); michael@0: int pointCnt = fSkPath.countPoints(); michael@0: pathCommands.resize_back(verbCnt); michael@0: pathPoints.resize_back(pointCnt); michael@0: michael@0: // TODO: Direct access to path points since we could pass them on directly. michael@0: fSkPath.getPoints(&pathPoints[0], pointCnt); michael@0: fSkPath.getVerbs(&pathCommands[0], verbCnt); michael@0: michael@0: SkDEBUGCODE(int numPts = 0); michael@0: for (int i = 0; i < verbCnt; ++i) { michael@0: SkPath::Verb v = static_cast(pathCommands[i]); michael@0: pathCommands[i] = verb_to_gl_path_cmd(v); michael@0: SkDEBUGCODE(numPts += num_pts(v)); michael@0: } michael@0: SkASSERT(pathPoints.count() == numPts); michael@0: michael@0: GL_CALL(PathCommands(fPathID, michael@0: verbCnt, &pathCommands[0], michael@0: 2 * pointCnt, GR_GL_FLOAT, &pathPoints[0])); michael@0: michael@0: if (stroke.needToApply()) { michael@0: GL_CALL(PathParameterf(fPathID, GR_GL_PATH_STROKE_WIDTH, SkScalarToFloat(stroke.getWidth()))); michael@0: GL_CALL(PathParameterf(fPathID, GR_GL_PATH_MITER_LIMIT, SkScalarToFloat(stroke.getMiter()))); michael@0: GrGLenum join = join_to_gl_join(stroke.getJoin()); michael@0: GL_CALL(PathParameteri(fPathID, GR_GL_PATH_JOIN_STYLE, join)); michael@0: GrGLenum cap = cap_to_gl_cap(stroke.getCap()); michael@0: GL_CALL(PathParameteri(fPathID, GR_GL_PATH_INITIAL_END_CAP, cap)); michael@0: GL_CALL(PathParameteri(fPathID, GR_GL_PATH_TERMINAL_END_CAP, cap)); michael@0: michael@0: // FIXME: try to account for stroking, without rasterizing the stroke. michael@0: fBounds.outset(SkScalarToFloat(stroke.getWidth()), SkScalarToFloat(stroke.getWidth())); michael@0: } michael@0: } michael@0: michael@0: GrGLPath::~GrGLPath() { michael@0: this->release(); michael@0: } michael@0: michael@0: void GrGLPath::onRelease() { michael@0: if (0 != fPathID && !this->isWrapped()) { michael@0: GL_CALL(DeletePaths(fPathID, 1)); michael@0: fPathID = 0; michael@0: } michael@0: michael@0: INHERITED::onRelease(); michael@0: } michael@0: michael@0: void GrGLPath::onAbandon() { michael@0: fPathID = 0; michael@0: michael@0: INHERITED::onAbandon(); michael@0: }