Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Unified Diff: src/utils/SkPatchUtils.cpp

Issue 424663006: SkCanvas interface for drawing a patch. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Moved SkPatchUtils.h to src/utils, moved partition size and approx_arc_length to SkPatchUtils.cpp Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/utils/SkPatchUtils.h ('K') | « src/utils/SkPatchUtils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/utils/SkPatchUtils.cpp
diff --git a/src/utils/SkPatchUtils.cpp b/src/utils/SkPatchUtils.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..24428beb74f9e6ac6037ad765ae590aacf87b793
--- /dev/null
+++ b/src/utils/SkPatchUtils.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkPatchUtils.h"
+
+#include "SkGeometry.h"
+
+// size in pixels of each partition per axis, adjust this knob
+static const int kPartitionSize = 30;
+
+/**
+ * Calculate the approximate arc length given a bezier curve's control points.
+ */
+static SkScalar approx_arc_length(SkPoint* points, int count) {
+ if (count < 2) {
+ return 0;
+ }
+ SkScalar arcLength = 0;
+ for (int i = 0; i < count - 1; i++) {
+ arcLength += SkPoint::Distance(points[i], points[i + 1]);
+ }
+ return arcLength;
+}
+
+SkIPoint SkPatchUtils::GetLevelOfDetail(const SkPatch& patch, const SkMatrix* matrix) {
+
+ SkPoint mapPts[12];
+ matrix->mapPoints(mapPts, patch.getControlPoints(), 12);
+
+ // Approximate length of each cubic.
+ SkPoint pts[4];
+ patch.getTopPoints(pts);
+ matrix->mapPoints(pts, 4);
+ int topLength = approx_arc_length(pts, 4);
+
+ patch.getBottomPoints(pts);
+ matrix->mapPoints(pts, 4);
+ int bottomLength = approx_arc_length(pts, 4);
+
+ patch.getLeftPoints(pts);
+ matrix->mapPoints(pts, 4);
+ int leftLength = approx_arc_length(pts, 4);
+
+ patch.getRightPoints(pts);
+ matrix->mapPoints(pts, 4);
+ int rightLength = approx_arc_length(pts, 4);
+
+ // Level of detail per axis, based on the larger side between top and bottom or left and right
+ int lodX = SkMaxScalar(topLength, bottomLength) / kPartitionSize;
+ int lodY = SkMaxScalar(leftLength, rightLength) / kPartitionSize;
+
+ return SkIPoint::Make(SkMax32(4, lodX), SkMax32(4, lodY));
+}
« src/utils/SkPatchUtils.h ('K') | « src/utils/SkPatchUtils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698