Refactor VirtualLightRefBase & JNI

Change-Id: I8e244e7109e59d5be96871b23bb9b1201c7f9eaa
This commit is contained in:
John Reck
2014-05-09 15:26:59 -07:00
parent 0557476eb0
commit 9fa4071c47
13 changed files with 127 additions and 95 deletions

View File

@ -21,6 +21,8 @@ import android.graphics.CanvasProperty;
import android.graphics.Paint;
import android.util.SparseIntArray;
import com.android.internal.util.VirtualRefBasePtr;
import java.lang.ref.WeakReference;
/**
@ -70,28 +72,32 @@ public final class RenderNodeAnimator {
public static final int DELTA_TYPE_DELTA = 1;
private RenderNode mTarget;
private long mNativePtr;
private VirtualRefBasePtr mNativePtr;
public int mapViewPropertyToRenderProperty(int viewProperty) {
return sViewPropertyAnimatorMap.get(viewProperty);
}
public RenderNodeAnimator(int property, int deltaType, float deltaValue) {
mNativePtr = nCreateAnimator(new WeakReference<RenderNodeAnimator>(this),
property, deltaType, deltaValue);
init(nCreateAnimator(new WeakReference<RenderNodeAnimator>(this),
property, deltaType, deltaValue));
}
public RenderNodeAnimator(CanvasProperty<Float> property, int deltaType, float deltaValue) {
mNativePtr = nCreateCanvasPropertyFloatAnimator(
init(nCreateCanvasPropertyFloatAnimator(
new WeakReference<RenderNodeAnimator>(this),
property.getNativeContainer(), deltaType, deltaValue);
property.getNativeContainer(), deltaType, deltaValue));
}
public RenderNodeAnimator(CanvasProperty<Paint> property, int paintField,
int deltaType, float deltaValue) {
mNativePtr = nCreateCanvasPropertyPaintAnimator(
init(nCreateCanvasPropertyPaintAnimator(
new WeakReference<RenderNodeAnimator>(this),
property.getNativeContainer(), paintField, deltaType, deltaValue);
property.getNativeContainer(), paintField, deltaType, deltaValue));
}
private void init(long ptr) {
mNativePtr = new VirtualRefBasePtr(ptr);
}
public void start(View target) {
@ -115,11 +121,11 @@ public final class RenderNodeAnimator {
}
public void setDuration(int duration) {
nSetDuration(mNativePtr, duration);
nSetDuration(mNativePtr.get(), duration);
}
long getNativeAnimator() {
return mNativePtr;
return mNativePtr.get();
}
private void onFinished() {
@ -134,16 +140,6 @@ public final class RenderNodeAnimator {
}
}
@Override
protected void finalize() throws Throwable {
try {
nUnref(mNativePtr);
mNativePtr = 0;
} finally {
super.finalize();
}
}
private static native long nCreateAnimator(WeakReference<RenderNodeAnimator> weakThis,
int property, int deltaValueType, float deltaValue);
private static native long nCreateCanvasPropertyFloatAnimator(WeakReference<RenderNodeAnimator> weakThis,
@ -151,5 +147,4 @@ public final class RenderNodeAnimator {
private static native long nCreateCanvasPropertyPaintAnimator(WeakReference<RenderNodeAnimator> weakThis,
long canvasProperty, int paintField, int deltaValueType, float deltaValue);
private static native void nSetDuration(long nativePtr, int duration);
private static native void nUnref(long nativePtr);
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2014 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.
*/
package com.android.internal.util;
/**
* Helper class that contains a strong reference to a VirtualRefBase native
* object. This will incStrong in the ctor, and decStrong in the finalizer
*/
public final class VirtualRefBasePtr {
private long mNativePtr;
public VirtualRefBasePtr(long ptr) {
mNativePtr = ptr;
nIncStrong(mNativePtr);
}
public long get() {
return mNativePtr;
}
@Override
protected void finalize() throws Throwable {
try {
nDecStrong(mNativePtr);
mNativePtr = 0;
} finally {
super.finalize();
}
}
private static native void nIncStrong(long ptr);
private static native void nDecStrong(long ptr);
}

View File

@ -155,7 +155,8 @@ LOCAL_SRC_FILES:= \
android_content_res_Configuration.cpp \
android_animation_PropertyValuesHolder.cpp \
com_android_internal_net_NetworkStatsFactory.cpp \
com_android_internal_os_Zygote.cpp
com_android_internal_os_Zygote.cpp \
com_android_internal_util_VirtualRefBasePtr.cpp
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE) \

View File

@ -181,6 +181,7 @@ extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
extern int register_com_android_internal_os_Zygote(JNIEnv *env);
extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env);
static AndroidRuntime* gCurRuntime = NULL;
@ -1262,6 +1263,7 @@ static const RegJNIRec gRegJNI[] = {
REG_JNI(register_android_os_MemoryFile),
REG_JNI(register_com_android_internal_os_ZygoteInit),
REG_JNI(register_com_android_internal_os_Zygote),
REG_JNI(register_com_android_internal_util_VirtualRefBasePtr),
REG_JNI(register_android_hardware_Camera),
REG_JNI(register_android_hardware_camera2_CameraMetadata),
REG_JNI(register_android_hardware_SensorManager),

View File

@ -18,7 +18,7 @@
#include "GraphicsJNI.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/VirtualLightRefBase.h>
#include <utils/RefBase.h>
#include <CanvasProperty.h>
namespace android {
@ -27,22 +27,13 @@ using namespace uirenderer;
#ifdef USE_OPENGL_RENDERER
static jlong incRef(VirtualLightRefBase* ptr) {
ptr->incStrong(0);
return reinterpret_cast<jlong>(ptr);
}
static jlong createFloat(JNIEnv* env, jobject clazz, jfloat initialValue) {
return incRef(new CanvasPropertyPrimitive(initialValue));
return reinterpret_cast<jlong>(new CanvasPropertyPrimitive(initialValue));
}
static jlong createPaint(JNIEnv* env, jobject clazz, jlong paintPtr) {
const SkPaint* paint = reinterpret_cast<const SkPaint*>(paintPtr);
return incRef(new CanvasPropertyPaint(*paint));
}
static void unref(JNIEnv* env, jobject clazz, jlong containerPtr) {
reinterpret_cast<VirtualLightRefBase*>(containerPtr)->decStrong(0);
return reinterpret_cast<jlong>(new CanvasPropertyPaint(*paint));
}
#endif
@ -57,7 +48,6 @@ static JNINativeMethod gMethods[] = {
#ifdef USE_OPENGL_RENDERER
{ "nCreateFloat", "(F)J", (void*) createFloat },
{ "nCreatePaint", "(J)J", (void*) createPaint },
{ "nUnref", "(J)V", (void*) unref },
#endif
};

View File

@ -101,7 +101,6 @@ static jlong createAnimator(JNIEnv* env, jobject clazz, jobject weakThis,
RenderPropertyAnimator::DeltaValueType deltaType = toDeltaType(deltaTypeRaw);
BaseAnimator* animator = new RenderPropertyAnimator(property, deltaType, deltaValue);
animator->incStrong(0);
animator->setListener(new AnimationListenerBridge(env, weakThis));
return reinterpret_cast<jlong>( animator );
}
@ -111,7 +110,6 @@ static jlong createCanvasPropertyFloatAnimator(JNIEnv* env, jobject clazz,
RenderPropertyAnimator::DeltaValueType deltaType = toDeltaType(deltaTypeRaw);
CanvasPropertyPrimitive* canvasProperty = reinterpret_cast<CanvasPropertyPrimitive*>(canvasPropertyPtr);
BaseAnimator* animator = new CanvasPropertyPrimitiveAnimator(canvasProperty, deltaType, deltaValue);
animator->incStrong(0);
animator->setListener(new AnimationListenerBridge(env, weakThis));
return reinterpret_cast<jlong>( animator );
}
@ -124,7 +122,6 @@ static jlong createCanvasPropertyPaintAnimator(JNIEnv* env, jobject clazz,
CanvasPropertyPaintAnimator::PaintField paintField = toPaintField(paintFieldRaw);
BaseAnimator* animator = new CanvasPropertyPaintAnimator(
canvasProperty, paintField, deltaType, deltaValue);
animator->incStrong(0);
animator->setListener(new AnimationListenerBridge(env, weakThis));
return reinterpret_cast<jlong>( animator );
}
@ -135,11 +132,6 @@ static void setDuration(JNIEnv* env, jobject clazz, jlong animatorPtr, jint dura
animator->setDuration(duration);
}
static void unref(JNIEnv* env, jobject clazz, jlong objPtr) {
VirtualLightRefBase* obj = reinterpret_cast<VirtualLightRefBase*>(objPtr);
obj->decStrong(0);
}
#endif
// ----------------------------------------------------------------------------
@ -154,7 +146,6 @@ static JNINativeMethod gMethods[] = {
{ "nCreateCanvasPropertyFloatAnimator", "(Ljava/lang/ref/WeakReference;JIF)J", (void*) createCanvasPropertyFloatAnimator },
{ "nCreateCanvasPropertyPaintAnimator", "(Ljava/lang/ref/WeakReference;JIIF)J", (void*) createCanvasPropertyPaintAnimator },
{ "nSetDuration", "(JI)V", (void*) setDuration },
{ "nUnref", "(J)V", (void*) unref },
#endif
};

View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2014 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 "jni.h"
#include <nativehelper/JNIHelp.h>
#include <android_runtime/AndroidRuntime.h>
namespace android {
static void incStrong(JNIEnv* env, jobject clazz, jlong objPtr) {
VirtualLightRefBase* obj = reinterpret_cast<VirtualLightRefBase*>(objPtr);
obj->incStrong(0);
}
static void decStrong(JNIEnv* env, jobject clazz, jlong objPtr) {
VirtualLightRefBase* obj = reinterpret_cast<VirtualLightRefBase*>(objPtr);
obj->decStrong(0);
}
// ----------------------------------------------------------------------------
// JNI Glue
// ----------------------------------------------------------------------------
const char* const kClassPathName = "com/android/internal/util/VirtualRefBasePtr";
static JNINativeMethod gMethods[] = {
{ "nIncStrong", "(J)V", (void*) incStrong },
{ "nDecStrong", "(J)V", (void*) decStrong },
};
int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv* env) {
return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
}
} // namespace android

View File

@ -16,12 +16,15 @@
package android.graphics;
import com.android.internal.util.VirtualRefBasePtr;
/**
* TODO: Make public?
* @hide
*/
public final class CanvasProperty<T> {
private long mNativeContainer;
private VirtualRefBasePtr mProperty;
public static CanvasProperty<Float> createFloat(float initialValue) {
return new CanvasProperty<Float>(nCreateFloat(initialValue));
@ -32,25 +35,14 @@ public final class CanvasProperty<T> {
}
private CanvasProperty(long nativeContainer) {
mNativeContainer = nativeContainer;
mProperty = new VirtualRefBasePtr(nativeContainer);
}
/** @hide */
public long getNativeContainer() {
return mNativeContainer;
}
@Override
protected void finalize() throws Throwable {
try {
nUnref(mNativeContainer);
mNativeContainer = 0;
} finally {
super.finalize();
}
return mProperty.get();
}
private static native long nCreateFloat(float initialValue);
private static native long nCreatePaint(long initialValuePaintPtr);
private static native void nUnref(long ptr);
}

View File

@ -17,13 +17,13 @@
#define ANIMATOR_H
#include <cutils/compiler.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
#include "CanvasProperty.h"
#include "Interpolator.h"
#include "TreeInfo.h"
#include "utils/Macros.h"
#include "utils/VirtualLightRefBase.h"
namespace android {
namespace uirenderer {

View File

@ -16,8 +16,9 @@
#ifndef CANVASPROPERTY_H
#define CANVASPROPERTY_H
#include <utils/RefBase.h>
#include "utils/Macros.h"
#include "utils/VirtualLightRefBase.h"
#include <SkPaint.h>

View File

@ -41,7 +41,6 @@
#include "Matrix.h"
#include "DeferredDisplayList.h"
#include "RenderProperties.h"
#include "utils/VirtualLightRefBase.h"
class SkBitmap;
class SkPaint;

View File

@ -45,7 +45,6 @@
#include "DisplayList.h"
#include "RenderProperties.h"
#include "TreeInfo.h"
#include "utils/VirtualLightRefBase.h"
class SkBitmap;
class SkPaint;

View File

@ -1,34 +0,0 @@
/*
* Copyright (C) 2014 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.
*/
#ifndef VIRTUALLIGHTREFBASE_H
#define VIRTUALLIGHTREFBASE_H
#include <utils/RefBase.h>
namespace android {
namespace uirenderer {
// This is a wrapper around LightRefBase that simply enforces a virtual
// destructor to eliminate the template requirement of LightRefBase
class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
public:
virtual ~VirtualLightRefBase() {}
};
} /* namespace uirenderer */
} /* namespace android */
#endif /* VIRTUALLIGHTREFBASE_H */