From 7eecbf2085bcfe475daf68090a1a4f47fbb6ec72 Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Tue, 23 Feb 2010 15:27:51 -0800 Subject: Begin implementation of generated java files from RS files. --- libs/rs/RenderScript.h | 3 +- libs/rs/rsg_ScriptJavaClass.cpp | 254 ++++++++++++++++++++++++++++++++++++++++ libs/rs/rsg_generator.c | 2 +- 3 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 libs/rs/rsg_ScriptJavaClass.cpp (limited to 'libs/rs') diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h index 02667d8..0c95e7d 100644 --- a/libs/rs/RenderScript.h +++ b/libs/rs/RenderScript.h @@ -200,8 +200,9 @@ enum RsPrimitive { RS_PRIMITIVE_TRIANGLE_FAN }; - +#ifndef NO_RS_FUNCS #include "rsgApiFuncDecl.h" +#endif #ifdef __cplusplus }; diff --git a/libs/rs/rsg_ScriptJavaClass.cpp b/libs/rs/rsg_ScriptJavaClass.cpp new file mode 100644 index 0000000..cee9f52 --- /dev/null +++ b/libs/rs/rsg_ScriptJavaClass.cpp @@ -0,0 +1,254 @@ +#define NO_RS_FUNCS 1 + +#include "stdio.h" +#include "RenderScript.h" +#include + +struct Element; + +struct ElementField { + const char *name; + Element *e; + ElementField(const char *n, Element *_e) { + name = n; + e = _e; + } + ElementField() { + name = NULL; + e = NULL; + } +}; + +struct Element { + ElementField *fields; + size_t fieldCount; + const char *name; + bool generated; + + RsDataType compType; + uint32_t compVectorSize; + + Element() { + fields = NULL; + fieldCount = 0; + name = NULL; + generated = false; + compType = RS_TYPE_ELEMENT; + compVectorSize = 0; + } + + Element(uint32_t _fieldCount, const char *_name) { + fields = new ElementField[_fieldCount]; + fieldCount = _fieldCount; + name = _name; + generated = false; + compType = RS_TYPE_ELEMENT; + compVectorSize = 0; + } + + Element(RsDataType t, uint32_t s) { + fields = NULL; + fieldCount = 0; + name = NULL; + generated = false; + compType = t; + compVectorSize = s; + } + +}; + + +static void genHeader(FILE *f, const char *packageName) +{ + fprintf(f, "package %s;\n", packageName); + fprintf(f, "\n"); + fprintf(f, "import android.renderscript.*;\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); +} + +static const char * RSTypeToJava(RsDataType dt) +{ + switch(dt) { + //case RS_TYPE_FLOAT_16: return "float"; + case RS_TYPE_FLOAT_32: return "float"; + //case RS_TYPE_FLOAT_64: return "double"; + + case RS_TYPE_SIGNED_8: return "byte"; + case RS_TYPE_SIGNED_16: return "short"; + case RS_TYPE_SIGNED_32: return "int"; + //case RS_TYPE_SIGNED_64: return "long"; + + case RS_TYPE_UNSIGNED_8: return "short"; + case RS_TYPE_UNSIGNED_16: return "int"; + case RS_TYPE_UNSIGNED_32: return "long"; + //case RS_TYPE_UNSIGNED_64: return NULL; + + //case RS_TYPE_ELEMENT: return "android.renderscript.Element"; + //case RS_TYPE_TYPE: return "android.renderscript.Type"; + //case RS_TYPE_ALLOCATION: return "android.renderscript.Allocation"; + //case RS_TYPE_SAMPLER: return "android.renderscript.Sampler"; + //case RS_TYPE_SCRIPT: return "android.renderscript.Script"; + //case RS_TYPE_MESH: return "android.renderscript.Mesh"; + //case RS_TYPE_PROGRAM_FRAGMENT: return "android.renderscript.ProgramFragment"; + //case RS_TYPE_PROGRAM_VERTEX: return "android.renderscript.ProgramVertex"; + //case RS_TYPE_PROGRAM_RASTER: return "android.renderscript.ProgramRaster"; + //case RS_TYPE_PROGRAM_STORE: return "android.renderscript.ProgramStore"; + default: return NULL; + } + return NULL; +} + +static const char * RSTypeToString(RsDataType dt) +{ + switch(dt) { + case RS_TYPE_FLOAT_16: return "F16"; + case RS_TYPE_FLOAT_32: return "F32"; + case RS_TYPE_FLOAT_64: return "F64"; + + case RS_TYPE_SIGNED_8: return "I8"; + case RS_TYPE_SIGNED_16: return "I16"; + case RS_TYPE_SIGNED_32: return "I32"; + case RS_TYPE_SIGNED_64: return "I64"; + + case RS_TYPE_UNSIGNED_8: return "U8"; + case RS_TYPE_UNSIGNED_16: return "U16"; + case RS_TYPE_UNSIGNED_32: return "U32"; + case RS_TYPE_UNSIGNED_64: return "U64"; + + //case RS_TYPE_ELEMENT: return "android.renderscript.Element"; + //case RS_TYPE_TYPE: return "android.renderscript.Type"; + //case RS_TYPE_ALLOCATION: return "android.renderscript.Allocation"; + //case RS_TYPE_SAMPLER: return "android.renderscript.Sampler"; + //case RS_TYPE_SCRIPT: return "android.renderscript.Script"; + //case RS_TYPE_MESH: return "android.renderscript.Mesh"; + //case RS_TYPE_PROGRAM_FRAGMENT: return "android.renderscript.ProgramFragment"; + //case RS_TYPE_PROGRAM_VERTEX: return "android.renderscript.ProgramVertex"; + //case RS_TYPE_PROGRAM_RASTER: return "android.renderscript.ProgramRaster"; + //case RS_TYPE_PROGRAM_STORE: return "android.renderscript.ProgramStore"; + default: return NULL; + } + return NULL; +} + +bool rsGenerateElementClass(const Element *e, const char *packageName, FILE *f) +{ + genHeader(f, packageName); + + fprintf(f, "class Element_%s {\n", e->name); + + for (size_t ct=0; ct < e->fieldCount; ct++) { + const char *ts = RSTypeToJava(e->fields[ct].e->compType); + if (ts == NULL) { + return false; + } + fprintf(f, " public %s %s;\n", ts, e->fields[ct].name); + } + + fprintf(f, "\n"); + fprintf(f, " static Element getElement(RenderScript rs) {\n"); + fprintf(f, " Element.Builder eb = new Element.Builder(rs);\n"); + for (size_t ct=0; ct < e->fieldCount; ct++) { + const char *ts = RSTypeToString(e->fields[ct].e->compType); + fprintf(f, " eb.add(Element.USER_%s(rs), \"%s\");\n", ts, e->fields[ct].name); + } + fprintf(f, " return eb.create();\n"); + fprintf(f, " }\n"); + + fprintf(f, " static Allocation createAllocation(RenderScript rs) {\n"); + fprintf(f, " Element e = getElement(rs);\n"); + fprintf(f, " Allocation a = Allocation.createSized(rs, e, 1);\n"); + fprintf(f, " return a;\n"); + fprintf(f, " }\n"); + + + fprintf(f, " void copyToAllocation(Allocation a) {\n"); + fprintf(f, " mIOBuffer.reset();\n"); + for (size_t ct=0; ct < e->fieldCount; ct++) { + const char *ts = RSTypeToString(e->fields[ct].e->compType); + fprintf(f, " mIOBuffer.add%s(%s);\n", ts, e->fields[ct].name); + } + fprintf(f, " a.data(mIOBuffer.getData());\n"); + fprintf(f, " }\n"); + + + + fprintf(f, " private FieldPacker mIOBuffer[];\n"); + fprintf(f, " public Element_%s() {\n", e->name); + fprintf(f, " mIOBuffer = new FieldPacker(%i);\n", 100/*element->getSizeBytes()*/); + fprintf(f, " }\n"); + + + fprintf(f, "}\n"); + + return true; +} + +bool rsGenerateElementClassFile(Element *e, const char *packageName) +{ + char buf[1024]; + sprintf(buf, "Element_%s.java", e->name); + printf("Creating file %s \n", buf); + FILE *f = fopen(buf, "w"); + bool ret = rsGenerateElementClass(e, packageName, f); + fclose(f); + return ret; +} + + + + +/* +bool rsGenerateScriptClass(const ScriptC *script, const char *packageName, FILE *f) +{ + genHeader(f, packageName); + + fprintf(f, "class ScriptC_%s {\n", script->getName()); + + + + ObjectBaseRef mTypes[MAX_SCRIPT_BANKS]; + String8 mSlotNames[MAX_SCRIPT_BANKS]; + bool mSlotWritable[MAX_SCRIPT_BANKS]; + + +} +*/ + + + +int main(int argc, const char *argv) +{ + Element *u8 = new Element(RS_TYPE_UNSIGNED_8, 1); + Element *i32 = new Element(RS_TYPE_SIGNED_32, 1); + Element *f32 = new Element(RS_TYPE_FLOAT_32, 1); + + Element *e_Pixel = new Element(4, "Pixel"); + e_Pixel->fields[0].e = u8; + e_Pixel->fields[0].name = "a"; + e_Pixel->fields[1].e = u8; + e_Pixel->fields[1].name = "b"; + e_Pixel->fields[2].e = u8; + e_Pixel->fields[2].name = "g"; + e_Pixel->fields[3].e = u8; + e_Pixel->fields[3].name = "r"; + + Element *e_Params = new Element(5, "Params"); + e_Params->fields[0].e = i32; + e_Params->fields[0].name = "inHeight"; + e_Params->fields[1].e = i32; + e_Params->fields[1].name = "inWidth"; + e_Params->fields[2].e = i32; + e_Params->fields[2].name = "outHeight"; + e_Params->fields[3].e = i32; + e_Params->fields[3].name = "outWidth"; + e_Params->fields[4].e = f32; + e_Params->fields[4].name = "threshold"; + + + printf("1\n"); + rsGenerateElementClassFile(e_Pixel, "android"); + rsGenerateElementClassFile(e_Params, "android"); + +} + diff --git a/libs/rs/rsg_generator.c b/libs/rs/rsg_generator.c index 74ba248..f4e8c68 100644 --- a/libs/rs/rsg_generator.c +++ b/libs/rs/rsg_generator.c @@ -5,7 +5,7 @@ void printFileHeader(FILE *f) { fprintf(f, "/*\n"); - fprintf(f, " * Copyright (C) 2009 The Android Open Source Project\n"); + fprintf(f, " * Copyright (C) 2010 The Android Open Source Project\n"); fprintf(f, " *\n"); fprintf(f, " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"); fprintf(f, " * you may not use this file except in compliance with the License.\n"); -- cgit v1.1