Skip to content

Commit

Permalink
Shrink library size and preserve JNI symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed Dec 13, 2023
1 parent d9d5985 commit f54957b
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 57 deletions.
1 change: 1 addition & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.15.0'
implementation 'dev.romainguy:pathway:0.16.0'
}
```

Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ buildscript {
}

dependencies {
classpath "com.android.tools.build:gradle:8.0.2"
classpath "com.android.tools.build:gradle:8.2.0"
classpath "com.vanniktech:gradle-maven-publish-plugin:0.25.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.21"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:1.8.20"
}

Expand All @@ -29,5 +29,6 @@ buildscript {
"-fdata-sections",
"-Wl,--gc-sections",
"-Wl,-Bsymbolic-functions",
"-nostdlib++"
]
}
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.15.0
VERSION_NAME=0.16.0

SONATYPE_HOST=S01
RELEASE_SIGNING_ENABLED=true
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jul 12 13:05:07 PDT 2023
#Tue Dec 12 17:22:13 PST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 2 additions & 0 deletions pathway/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ android {
}
}

consumerProguardFiles "proguard-rules.pro"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
3 changes: 3 additions & 0 deletions pathway/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-keepclasseswithmembernames,includedescriptorclasses class dev.romainguy.graphics.path.** {
native <methods>;
}
14 changes: 2 additions & 12 deletions pathway/src/main/cpp/Conic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

using namespace filament::math;

constexpr int kMaxConicToQuadCount = 5;

constexpr bool isFinite(const Point points[], int count) noexcept {
return isFinite(&points[0].x, count << 1);
}
Expand All @@ -53,17 +51,9 @@ const Point* ConicConverter::toQuadratics(
Conic conic(points[0], points[1], points[2], weight);

int count = conic.computeQuadraticCount(tolerance);
mQuadraticCount = 1 << count;

int newSize = 1 + 2 * mQuadraticCount;
if (newSize > mStorage.size()) {
mStorage.resize(newSize);
}

Point* data = mStorage.data();
mQuadraticCount = conic.splitIntoQuadratics(data, count);
mQuadraticCount = conic.splitIntoQuadratics(mStorage, count);

return data;
return mStorage;
}

int Conic::computeQuadraticCount(float tolerance) const noexcept {
Expand Down
9 changes: 4 additions & 5 deletions pathway/src/main/cpp/Conic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

#include "Path.h"

#include <vector>

constexpr int kDefaultQuadraticCount = 8;
constexpr int kMaxConicToQuadCount = 5;
constexpr int kMaxQuadraticCount = 1 << kMaxConicToQuadCount;

class ConicConverter {
public:
Expand All @@ -32,12 +31,12 @@ class ConicConverter {
int quadraticCount() const noexcept { return mQuadraticCount; }

const Point* quadratics() const noexcept {
return mQuadraticCount > 0 ? mStorage.data() : nullptr;
return mQuadraticCount > 0 ? mStorage : nullptr;
}

private:
int mQuadraticCount = 0;
std::vector<Point> mStorage{1 + 2 * kDefaultQuadraticCount};
Point mStorage[1 + 2 * kMaxQuadraticCount];
};

struct Conic {
Expand Down
10 changes: 8 additions & 2 deletions pathway/src/main/cpp/pathway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include <android/api-level.h>

#include <cstdlib>
#include <new>

#define JNI_CLASS_NAME "dev/romainguy/graphics/path/Paths"

struct {
Expand Down Expand Up @@ -77,14 +80,17 @@ static jlong createPathIterator(JNIEnv* env, jclass,
direction = PathIterator::VerbDirection::Backward;
}

return jlong(new PathIterator(
PathIterator* iterator = static_cast<PathIterator*>(malloc(sizeof(PathIterator)));
return jlong(new(iterator) PathIterator(
points, verbs, conicWeights, count, direction,
PathIterator::ConicEvaluation(conicEvaluation_), tolerance_
));
}

static void destroyPathIterator(JNIEnv*, jclass, jlong pathIterator_) {
delete reinterpret_cast<PathIterator*>(pathIterator_);
PathIterator* iterator = reinterpret_cast<PathIterator*>(pathIterator_);
iterator->~PathIterator();
free(iterator);
}

static jboolean pathIteratorHasNext(JNIEnv*, jclass, jlong pathIterator_) {
Expand Down
25 changes: 7 additions & 18 deletions pathway/src/main/java/dev/romainguy/graphics/path/Paths.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ class PathSegment internal constructor(val type: Type, val points: Array<PointF>

if (type != other.type) return false
if (!points.contentEquals(other.points)) return false
if (weight != other.weight) return false

return true
return weight == other.weight
}

override fun hashCode(): Int {
Expand All @@ -130,11 +128,6 @@ val DoneSegment = PathSegment(PathSegment.Type.Done, emptyArray(), 0.0f)
*/
val CloseSegment = PathSegment(PathSegment.Type.Close, emptyArray(), 0.0f)

/**
* Cache of [PathSegment.Type] values to avoid internal allocation on each use.
*/
private val pathSegmentTypes = PathSegment.Type.values()

/**
* Creates a new [PathIterator] for this [path][android.graphics.Path] that evaluates
* conics as quadratics. To preserve conics, use [Path.iterator].
Expand Down Expand Up @@ -211,7 +204,7 @@ class PathIterator(
* Returns the type of the current segment in the iteration, or [Done][PathSegment.Type.Done]
* if the iteration is finished.
*/
fun peek() = pathSegmentTypes[internalPathIteratorPeek(internalPathIterator)]
fun peek() = PathSegment.Type.entries[internalPathIteratorPeek(internalPathIterator)]

/**
* Returns the [type][PathSegment.Type] of the next [path segment][PathSegment] in the iteration
Expand All @@ -236,7 +229,7 @@ class PathIterator(
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]
return PathSegment.Type.entries[typeValue]
}

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

if (type == PathSegment.Type.Done) return DoneSegment
if (type == PathSegment.Type.Close) return CloseSegment
Expand All @@ -255,12 +248,14 @@ class PathIterator(
PathSegment.Type.Move -> {
arrayOf(PointF(pointsData[0], pointsData[1]))
}

PathSegment.Type.Line -> {
arrayOf(
PointF(pointsData[0], pointsData[1]),
PointF(pointsData[2], pointsData[3])
)
}

PathSegment.Type.Quadratic,
PathSegment.Type.Conic -> {
arrayOf(
Expand All @@ -269,6 +264,7 @@ class PathIterator(
PointF(pointsData[4], pointsData[5])
)
}

PathSegment.Type.Cubic -> {
arrayOf(
PointF(pointsData[0], pointsData[1]),
Expand All @@ -291,29 +287,22 @@ class PathIterator(
}
}

@Suppress("KotlinJniMissingFunction")
private external fun createInternalPathIterator(
path: Path, conicEvaluation: Int, tolerance: Float
): Long

@Suppress("KotlinJniMissingFunction")
private external fun destroyInternalPathIterator(internalPathIterator: Long)

@Suppress("KotlinJniMissingFunction")
private external fun internalPathIteratorHasNext(internalPathIterator: Long): Boolean

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

@Suppress("KotlinJniMissingFunction")
private external fun internalPathIteratorPeek(internalPathIterator: Long): Int

@Suppress("KotlinJniMissingFunction")
private external fun internalPathIteratorRawSize(internalPathIterator: Long): Int

@Suppress("KotlinJniMissingFunction")
private external fun internalPathIteratorSize(internalPathIterator: Long): Int

0 comments on commit f54957b

Please sign in to comment.