Skip to content

Commit

Permalink
Update to pathway 0.5.0
Browse files Browse the repository at this point in the history
- Added offset to next() to use large arrays
  • Loading branch information
romainguy committed Aug 2, 2022
1 parent 4be84e4 commit db67186
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 32 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
}
dependencies {
implementation 'dev.romainguy:pathway:0.4.0'
implementation 'dev.romainguy:pathway:0.5.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=dev.romainguy
VERSION_NAME=0.4.0
VERSION_NAME=0.5.0

POM_DESCRIPTION=Android library to extend the functionality of the graphics Path API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,37 @@ class PathIteratorTest {

val iterator1 = path.iterator(PathIterator.ConicEvaluation.AsConic)
val iterator2 = path.iterator(PathIterator.ConicEvaluation.AsConic)
val iterator3 = path.iterator(PathIterator.ConicEvaluation.AsConic)

val points = FloatArray(8)
val points2 = FloatArray(16)

while (iterator1.hasNext() || iterator2.hasNext()) {
while (iterator1.hasNext() || iterator2.hasNext() || iterator3.hasNext()) {
val segment = iterator1.next()
val type = iterator2.next(points)
val type2 = iterator3.next(points2, 8)

assertEquals(type, segment.type)
assertEquals(type2, segment.type)

when (type) {
PathSegment.Type.Move -> {
assertPointsEquals(points, 0, segment.points[0])
assertPointsEquals(points2, 4, segment.points[0])
}
PathSegment.Type.Line -> {
assertPointsEquals(points, 0, segment.points[0])
assertPointsEquals(points, 1, segment.points[1])
assertPointsEquals(points2, 4, segment.points[0])
assertPointsEquals(points2, 5, segment.points[1])
}
PathSegment.Type.Quadratic -> {
assertPointsEquals(points, 0, segment.points[0])
assertPointsEquals(points, 1, segment.points[1])
assertPointsEquals(points, 2, segment.points[2])
assertPointsEquals(points2, 4, segment.points[0])
assertPointsEquals(points2, 5, segment.points[1])
assertPointsEquals(points2, 6, segment.points[2])
}
PathSegment.Type.Conic -> {
assertPointsEquals(points, 0, segment.points[0])
Expand All @@ -198,12 +208,24 @@ class PathIteratorTest {
// We store the weight twice
assertEquals(points[6], segment.weight)
assertEquals(points[7], segment.weight)

assertPointsEquals(points2, 4, segment.points[0])
assertPointsEquals(points2, 5, segment.points[1])
assertPointsEquals(points2, 6, segment.points[2])
// We store the weight twice
assertEquals(points2[14], segment.weight)
assertEquals(points2[15], segment.weight)
}
PathSegment.Type.Cubic -> {
assertPointsEquals(points, 0, segment.points[0])
assertPointsEquals(points, 1, segment.points[1])
assertPointsEquals(points, 2, segment.points[2])
assertPointsEquals(points, 3, segment.points[3])

assertPointsEquals(points2, 4, segment.points[0])
assertPointsEquals(points2, 5, segment.points[1])
assertPointsEquals(points2, 6, segment.points[2])
assertPointsEquals(points2, 7, segment.points[3])
}
PathSegment.Type.Close -> { }
PathSegment.Type.Done -> { }
Expand Down
14 changes: 8 additions & 6 deletions pathway/src/main/cpp/pathway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct {
uint32_t sApiLevel = 0;
std::once_flag sApiLevelOnceFlag;

static int api_level() {
static uint32_t api_level() {
std::call_once(sApiLevelOnceFlag, []() {
char sdkVersion[PROP_VALUE_MAX];
__system_property_get("ro.build.version.sdk", sdkVersion);
Expand All @@ -59,7 +59,7 @@ static jlong createPathIterator(JNIEnv* env, jobject,
int count;
PathIterator::VerbDirection direction;

const int apiLevel = api_level();
const uint32_t apiLevel = api_level();
if (apiLevel >= 30) {
auto* ref = reinterpret_cast<PathRef30*>(path->pathRef);
points = ref->points;
Expand Down Expand Up @@ -104,13 +104,15 @@ static jboolean pathIteratorHasNext(JNIEnv*, jobject, jlong pathIterator_) {
return reinterpret_cast<PathIterator*>(pathIterator_)->hasNext();
}

static jint pathIteratorNext(JNIEnv* env, jobject, jlong pathIterator_, jfloatArray points_) {
static jint pathIteratorNext(
JNIEnv* env, jobject, jlong pathIterator_, jfloatArray points_, jint offset_) {
auto pathIterator = reinterpret_cast<PathIterator*>(pathIterator_);
Point pointsData[4];
Verb verb = pathIterator->next(pointsData);

if (verb != Verb::Done && verb != Verb::Close) {
jfloat* points = env->GetFloatArrayElements(points_, nullptr);
auto* pointsArray = static_cast<jfloat*>(env->GetPrimitiveArrayCritical(points_, nullptr));
jfloat* points = pointsArray + offset_;
switch (verb) {
case Verb::Cubic:
case Verb::Conic: // to copy the weight
Expand All @@ -133,7 +135,7 @@ static jint pathIteratorNext(JNIEnv* env, jobject, jlong pathIterator_, jfloatAr
break;
#pragma clang diagnostic pop
}
env->ReleaseFloatArrayElements(points_, points, 0);
env->ReleasePrimitiveArrayCritical(points_, pointsArray, 0);
}

return static_cast<jint>(verb);
Expand Down Expand Up @@ -177,7 +179,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
},
{
(char*) "internalPathIteratorNext",
(char*) "(J[F)I",
(char*) "(J[FI)I",
reinterpret_cast<void*>(pathIteratorNext)
},
{
Expand Down
18 changes: 12 additions & 6 deletions pathway/src/main/java/dev/romainguy/graphics/path/Paths.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ class PathIterator(

@JvmStatic
@Suppress("KotlinJniMissingFunction")
external fun internalPathIteratorNext(internalPathIterator: Long, points: FloatArray): Int
external fun internalPathIteratorNext(
internalPathIterator: Long,
points: FloatArray,
offset: Int
): Int

@JvmStatic
@Suppress("KotlinJniMissingFunction")
Expand Down Expand Up @@ -228,11 +232,13 @@ class PathIterator(
* - [Done][PathSegment.Type.Done]: 0 pair
* This method does not allocate any memory.
*
* @param points *Must* be a [FloatArray] of size 8, throws an [IllegalStateException] otherwise.
* @param points A [FloatArray] large enough to hold 8 floats starting at [offset],
* throws an [IllegalStateException] otherwise.
* @param offset Offset in [points] where to store the result
*/
fun next(points: FloatArray): PathSegment.Type {
check(points.size >= 8) { "The points array must contain at least 8 floats" }
val typeValue = internalPathIteratorNext(internalPathIterator, points)
fun next(points: FloatArray, offset: Int = 0): PathSegment.Type {
check(points.size - offset >= 8) { "The points array must contain at least 8 floats" }
val typeValue = internalPathIteratorNext(internalPathIterator, points, offset)
return pathSegmentTypes[typeValue]
}

Expand All @@ -242,7 +248,7 @@ class PathIterator(
* [next] method.
*/
override fun next(): PathSegment {
val typeValue = internalPathIteratorNext(internalPathIterator, pointsData)
val typeValue = internalPathIteratorNext(internalPathIterator, pointsData, 0)
val type = pathSegmentTypes[typeValue]

if (type == PathSegment.Type.Done) return DoneSegment
Expand Down

0 comments on commit db67186

Please sign in to comment.