PathIterator uses VMRuntime.newNonMovableArray and VMRuntime.addressOf to create a region of memory for iterating over path segments. However, VMRuntime.newNonMovableArray and VMRuntime.addressOf are libcore-only and not supported on the host JVM. This causes problems when Compose animations run on host runtimes such as LayoutLib, Ravenwood, and Robolectric. Add a variant method PathIterator.nNextHost that passes the float array (not just the address) to a regular JNI method. An alternative to this would be to convert `nNext` to a `FastNative`, but that would slow it down on Android. Another alternative would be to require LayoutLib/Ravenwood/Robolectric to use some DirectByteBuffer hacks to simulate a non-movable address, but that would require each framework to delegate/fake/shadow parts of PathIterator. This approach solves it in one fell swoop. Flag: NONE host-only change Bug: 354621198 Bug: 357624564 Test: cts PathIteratorTest Change-Id: Idfd2b6e41b499efffbbf5cbef343183981edd7b2
96 lines
3.7 KiB
C++
96 lines
3.7 KiB
C++
/* libs/android_runtime/android/graphics/PathMeasure.cpp
|
|
**
|
|
** Copyright 2007, The Android Open Source Project
|
|
**
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
** you may not use this file except in compliance with the License.
|
|
** You may obtain a copy of the License at
|
|
**
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
**
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
** See the License for the specific language governing permissions and
|
|
** limitations under the License.
|
|
*/
|
|
|
|
#include <log/log.h>
|
|
|
|
#include "GraphicsJNI.h"
|
|
#include "SkPath.h"
|
|
#include "SkPoint.h"
|
|
#include "graphics_jni_helpers.h"
|
|
|
|
namespace android {
|
|
|
|
class SkPathIteratorGlue {
|
|
public:
|
|
static void finalizer(SkPath::RawIter* obj) { delete obj; }
|
|
|
|
static jlong getFinalizer(JNIEnv* env, jclass clazz) {
|
|
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalizer));
|
|
}
|
|
|
|
static jlong create(JNIEnv* env, jobject clazz, jlong pathHandle) {
|
|
const SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
|
|
return reinterpret_cast<jlong>(new SkPath::RawIter(*path));
|
|
}
|
|
|
|
// A variant of 'next' (below) that is compatible with the host JVM.
|
|
static jint nextHost(JNIEnv* env, jclass clazz, jlong iteratorHandle, jfloatArray pointsArray) {
|
|
jfloat* points = env->GetFloatArrayElements(pointsArray, 0);
|
|
#ifdef __ANDROID__
|
|
jint result = next(iteratorHandle, reinterpret_cast<jlong>(points));
|
|
#else
|
|
jint result = next(env, clazz, iteratorHandle, reinterpret_cast<jlong>(points));
|
|
#endif
|
|
env->ReleaseFloatArrayElements(pointsArray, points, 0);
|
|
return result;
|
|
}
|
|
|
|
// ---------------- @CriticalNative -------------------------
|
|
|
|
static jint peek(CRITICAL_JNI_PARAMS_COMMA jlong iteratorHandle) {
|
|
SkPath::RawIter* iterator = reinterpret_cast<SkPath::RawIter*>(iteratorHandle);
|
|
return iterator->peek();
|
|
}
|
|
|
|
static jint next(CRITICAL_JNI_PARAMS_COMMA jlong iteratorHandle, jlong pointsArray) {
|
|
static_assert(SkPath::kMove_Verb == 0, "SkPath::Verb unexpected index");
|
|
static_assert(SkPath::kLine_Verb == 1, "SkPath::Verb unexpected index");
|
|
static_assert(SkPath::kQuad_Verb == 2, "SkPath::Verb unexpected index");
|
|
static_assert(SkPath::kConic_Verb == 3, "SkPath::Verb unexpected index");
|
|
static_assert(SkPath::kCubic_Verb == 4, "SkPath::Verb unexpected index");
|
|
static_assert(SkPath::kClose_Verb == 5, "SkPath::Verb unexpected index");
|
|
static_assert(SkPath::kDone_Verb == 6, "SkPath::Verb unexpected index");
|
|
|
|
SkPath::RawIter* iterator = reinterpret_cast<SkPath::RawIter*>(iteratorHandle);
|
|
float* points = reinterpret_cast<float*>(pointsArray);
|
|
SkPath::Verb verb =
|
|
static_cast<SkPath::Verb>(iterator->next(reinterpret_cast<SkPoint*>(points)));
|
|
if (verb == SkPath::kConic_Verb) {
|
|
float weight = iterator->conicWeight();
|
|
points[6] = weight;
|
|
}
|
|
return static_cast<int>(verb);
|
|
}
|
|
};
|
|
|
|
static const JNINativeMethod methods[] = {
|
|
{"nCreate", "(J)J", (void*)SkPathIteratorGlue::create},
|
|
{"nGetFinalizer", "()J", (void*)SkPathIteratorGlue::getFinalizer},
|
|
|
|
// ------- @CriticalNative below here ------------------
|
|
|
|
{"nPeek", "(J)I", (void*)SkPathIteratorGlue::peek},
|
|
{"nNext", "(JJ)I", (void*)SkPathIteratorGlue::next},
|
|
{"nNextHost", "(J[F)I", (void*)SkPathIteratorGlue::nextHost},
|
|
};
|
|
|
|
int register_android_graphics_PathIterator(JNIEnv* env) {
|
|
return RegisterMethodsOrDie(env, "android/graphics/PathIterator", methods, NELEM(methods));
|
|
}
|
|
|
|
} // namespace android
|