Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aae74ad6 authored by Alex Sakhartchouk's avatar Alex Sakhartchouk
Browse files

Creating the jni and java layer to integrate a3d

Change-Id: I438359633bae59bf9188cd2c4664a92ca16c5f37
parent e407a608
Loading
Loading
Loading
Loading
+214 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 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 android.renderscript;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.Resources;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.TypedValue;

/**
 * @hide
 *
 **/
public class FileA3D extends BaseObj {

    public enum ClassID {

        UNKNOWN,
        MESH,
        SIMPLE_MESH,
        TYPE,
        ELEMENT,
        ALLOCATION,
        PROGRAM_VERTEX,
        PROGRAM_RASTER,
        PROGRAM_FRAGMENT,
        PROGRAM_STORE,
        SAMPLER,
        ANIMATION,
        LIGHT,
        ADAPTER_1D,
        ADAPTER_2D,
        SCRIPT_C;

        public static ClassID toClassID(int intID) {
            return ClassID.values()[intID];
        }
    }

    // Read only class with index entries
    public class IndexEntry {
        RenderScript mRS;
        int mIndex;
        int mID;
        String mName;
        ClassID mClassID;
        BaseObj mLoadedObj;

        public String getName() {
            return mName;
        }

        public ClassID getClassID() {
            return mClassID;
        }

        public BaseObj getObject() {
            if(mLoadedObj != null) {
                return mLoadedObj;
            }

            if(mClassID == ClassID.UNKNOWN) {
                return null;
            }

            int objectID = mRS.nFileA3DGetEntryByIndex(mID, mIndex);
            if(objectID == 0) {
                return null;
            }

            switch (mClassID) {
            case MESH:
                mLoadedObj = null;
                break;
            case SIMPLE_MESH:
                mLoadedObj = new SimpleMesh(objectID, mRS);
                break;
            case TYPE:
                mLoadedObj = new Type(objectID, mRS);
                break;
            case ELEMENT:
                mLoadedObj = null;
                break;
            case ALLOCATION:
                mLoadedObj = null;
                break;
            case PROGRAM_VERTEX:
                mLoadedObj = new ProgramVertex(objectID, mRS);
                break;
            case PROGRAM_RASTER:
                break;
            case PROGRAM_FRAGMENT:
                break;
            case PROGRAM_STORE:
                break;
            case SAMPLER:
                break;
            case ANIMATION:
                break;
            case LIGHT:
                break;
            case ADAPTER_1D:
                break;
            case ADAPTER_2D:
                break;
            case SCRIPT_C:
                break;
            }

            return mLoadedObj;
        }

        IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) {
            mRS = rs;
            mIndex = index;
            mID = id;
            mName = name;
            mClassID = classID;
            mLoadedObj = null;
        }
    }

    IndexEntry[] mFileEntries;

    FileA3D(int id, RenderScript rs) {
        super(rs);
        mID = id;
    }

    private void initEntries() {
        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID);
        if(numFileEntries <= 0) {
            return;
        }

        mFileEntries = new IndexEntry[numFileEntries];
        int[] ids = new int[numFileEntries];
        String[] names = new String[numFileEntries];

        mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names);

        for(int i = 0; i < numFileEntries; i ++) {
            mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i]));
        }
    }

    public int getNumIndexEntries() {
        if(mFileEntries == null) {
            return 0;
        }
        return mFileEntries.length;
    }

    public IndexEntry getIndexEntry(int index) {
        if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) {
            return null;
        }
        return mFileEntries[index];
    }

    static public FileA3D createFromResource(RenderScript rs, Resources res, int id)
        throws IllegalArgumentException {

        rs.validate();
        InputStream is = null;
        try {
            final TypedValue value = new TypedValue();
            is = res.openRawResource(id, value);

            int asset = ((AssetManager.AssetInputStream) is).getAssetInt();

            int fileId = rs.nFileA3DCreateFromAssetStream(asset);

            if(fileId == 0) {
                throw new IllegalStateException("Load failed.");
            }
            FileA3D fa3d = new FileA3D(fileId, rs);
            fa3d.initEntries();
            return fa3d;

        } catch (Exception e) {
            // Ignore
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }

        return null;
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -117,6 +117,11 @@ public class RenderScript {
    native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o);
    native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o);

    native int  nFileA3DCreateFromAssetStream(int assetStream);
    native int  nFileA3DGetNumIndexEntries(int fileA3D);
    native void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names);
    native int  nFileA3DGetEntryByIndex(int fileA3D, int index);

    native void nAdapter1DBindAllocation(int ad, int alloc);
    native void nAdapter1DSetConstraint(int ad, int dim, int value);
    native void nAdapter1DData(int ad, int[] d);
+58 −0
Original line number Diff line number Diff line
@@ -700,6 +700,59 @@ nAllocationSubReadFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _t
    free(bufAlloc);
}

// -----------------------------------

static int
nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jint native_asset)
{
    LOGV("______nFileA3D %u", (uint32_t) native_asset);
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));

    Asset* asset = reinterpret_cast<Asset*>(native_asset);

    jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
    return id;
}

static int
nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jint fileA3D)
{
    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));

    int32_t numEntries = 0;
    rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
    LOGV("______nFileA3D NumEntries %u", (uint32_t) numEntries);
    return numEntries;
}

static void
nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
{
    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));

    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));

    rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);

    for(jint i = 0; i < numEntries; i ++) {
        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
    }

    free(fileEntries);
}

static int
nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jint fileA3D, jint index)
{
    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));

    jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
    return id;
}

// -----------------------------------

@@ -1442,6 +1495,11 @@ static JNINativeMethod methods[] = {
{"nSimpleMeshBindVertex",          "(III)V",                               (void*)nSimpleMeshBindVertex },
{"nSimpleMeshBindIndex",           "(II)V",                                (void*)nSimpleMeshBindIndex },

{"nFileA3DCreateFromAssetStream", "(I)I",                                 (void*)nFileA3DCreateFromAssetStream },
{"nFileA3DGetNumIndexEntries",     "(I)I",                                 (void*)nFileA3DGetNumIndexEntries },
{"nFileA3DGetIndexEntries",        "(II[I[Ljava/lang/String;)V",          (void*)nFileA3DGetIndexEntries },
{"nFileA3DGetEntryByIndex",        "(II)I",                                (void*)nFileA3DGetEntryByIndex },

};

static int registerFuncs(JNIEnv *_env)
+25 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ typedef void * RsScript;
typedef void * RsSimpleMesh;
typedef void * RsType;
typedef void * RsLight;
typedef void * RsObjectBase;

typedef void * RsProgram;
typedef void * RsProgramVertex;
@@ -229,6 +230,30 @@ enum RsAnimationEdge {
    RS_ANIMATION_EDGE_CYLE_RELATIVE
};

enum RsA3DClassID {
    RS_A3D_CLASS_ID_UNKNOWN,
    RS_A3D_CLASS_ID_MESH,
    RS_A3D_CLASS_ID_SIMPLE_MESH,
    RS_A3D_CLASS_ID_TYPE,
    RS_A3D_CLASS_ID_ELEMENT,
    RS_A3D_CLASS_ID_ALLOCATION,
    RS_A3D_CLASS_ID_PROGRAM_VERTEX,
    RS_A3D_CLASS_ID_PROGRAM_RASTER,
    RS_A3D_CLASS_ID_PROGRAM_FRAGMENT,
    RS_A3D_CLASS_ID_PROGRAM_STORE,
    RS_A3D_CLASS_ID_SAMPLER,
    RS_A3D_CLASS_ID_ANIMATION,
    RS_A3D_CLASS_ID_LIGHT,
    RS_A3D_CLASS_ID_ADAPTER_1D,
    RS_A3D_CLASS_ID_ADAPTER_2D,
    RS_A3D_CLASS_ID_SCRIPT_C
};

typedef struct {
    RsA3DClassID classID;
    const char* objectName;
} RsFileIndexEntry;

#ifndef NO_RS_FUNCS
#include "rsgApiFuncDecl.h"
#endif
+27 −0
Original line number Diff line number Diff line
#
# Copyright (C) 2008 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.
#

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)
#LOCAL_STATIC_JAVA_LIBRARIES := android.renderscript

LOCAL_PACKAGE_NAME := ModelViewer

include $(BUILD_PACKAGE)
Loading