Android renderscript, more info' and an example application

Source: Internet
Author: User

Here's a few more snippets of information I 've managed to uncover on android renderscript:

  • Compiled on the device
  • Uses acc compiler
  • No required tural support issues.
  • No external libraries
  • You cannot # include
  • No allocation allowed
  • Safe predictable

I admit some of these statements don't give too much away, but there's not too much else to go on at the moment. it does seem to point to a language that is compiled and is C like, but maybe with not all the power of C as allocations are not allowed.

Trying to shed some more light on things I had another look in the source code and found a few simple example Android applications usingrenderscript One of these simple example applications was called Fountain, it seemed to be one of the simplest applications so I thought it wocould be a good place to start.

The Android Fountain renderscript example

What does this application do? Well I'm not totally sure because I haven't run it myself, and to be honest there are not allowed comments in the code, so it really is a case of read the code and work it out. my best guess is that it produces a fountain like animation that randomly animates points on a screen generally moving up and outwards in a fountain like way. these animations start when a users touches the screen and originate from that point. this is my best guess at the moment from what I can make out in the code example.

OK so what does the Code look like ?. First lets have a look at the files and how they are layed out, the structure is:

  • Android. mk
  • AndroidManifest. xml
  • Res
    • Drawable
      • Gadgets_clock_mp3.png
    • Raw
      • Fountain. c
  • Src
    • Com
      • Android
        • Fountain
        • Fountain. java
        • FountainRS. java
        • FountainView. java

Most of what we see here is a standard looking Android application. we have the basic android files such as the AndroidManifest file. then we have our src directory that contains our android application source files, also we have the res directory, not unsual as it contains the drawable and raw directories, but as you can see the raw directory contains one very interesting and rather unusual file, the fountain. c file. this, it seems, is where therenderscript code resides, and the name does indeed suggest that it is C code file. so lets have a look at what is contained in this file:

// Fountain test script#pragma version(1)int newPart = 0;int main(int launchID) {    int ct;    int count = Control->count;    int rate = Control->rate;    float height = getHeight();    struct point_s * p = (struct point_s *)point;    if (rate) {        float rMax = ((float)rate) * 0.005f;        int x = Control->x;        int y = Control->y;        char r = Control->r * 255.f;        char g = Control->g * 255.f;        char b = Control->b * 255.f;        struct point_s * np = &p[newPart];        while (rate--) {            vec2Rand((float *)np, rMax);            np->x = x;            np->y = y;            np->r = r;            np->g = g;            np->b = b;            np->a = 0xf0;            newPart++;            np++;            if (newPart >= count) {                newPart = 0;                np = &p[newPart];            }        }    }    for (ct=0; ct < count; ct++) {        float dy = p->dy + 0.15f;        float posy = p->y + dy;        if ((posy > height) && (dy > 0)) {            dy *= -0.3f;        }        p->dy = dy;        p->x += p->dx;        p->y = posy;        p++;    }    uploadToBufferObject(NAMED_PartBuffer);    drawSimpleMesh(NAMED_PartMesh);    return 1;}

 

Yes, it is very C like. we have structs, pointers and chars. starting at the top of the file. we have a control structure or class that gives use a rate and a count as well as X, Y and R, G, B values. where does this control structure get instantiated? I'll come back to this. another structure that is also used in this code is point_s. this structure has a X and Y coordinates, R, G, B values, which are likely to be red, blue green, and an "A" value which is the Alpha value. without more information I cannot be sure exactly what is happening in this code, but I think that generally an array of points is given and then array of new points is generated, to allow some kind of animation.

Looking at the src code directory we have three. java files. fountain. java, FountainView. java and FountainRS. java. fountain. java is just the basic Android activity class that has an onCreate method that sets the contentView to an instance of FountainView. the Code for the FountainView. java file looks like this:

/* * 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 com.android.fountain;import java.io.Writer;import java.util.ArrayList;import java.util.concurrent.Semaphore;import android.renderscript.RSSurfaceView;import android.renderscript.RenderScript;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.Log;import android.view.Surface;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.KeyEvent;import android.view.MotionEvent;public class FountainView extends RSSurfaceView {    public FountainView(Context context) {        super(context);        //setFocusable(true);    }    private RenderScript mRS;    private FountainRS mRender;    private void destroyRS() {        if(mRS != null) {            mRS = null;            destroyRenderScript();        }        java.lang.System.gc();    }    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {        super.surfaceChanged(holder, format, w, h);        destroyRS();        mRS = createRenderScript(false, true);        mRender = new FountainRS();        mRender.init(mRS, getResources(), w, h);    }    public void surfaceDestroyed(SurfaceHolder holder) {        // Surface will be destroyed when we return        destroyRS();    }    @Override    public boolean onTouchEvent(MotionEvent ev)    {        int act = ev.getAction();        if (act == ev.ACTION_UP) {            mRender.newTouchPosition(0, 0, 0);            return false;        }        float rate = (ev.getPressure() * 50.f);        rate *= rate;        if(rate > 2000.f) {            rate = 2000.f;        }        mRender.newTouchPosition((int)ev.getX(), (int)ev.getY(), (int)rate);        return true;    }}

The FountainView class is an Android View. as you can see from the code, FountainView extends a new type of Android view RSSufraceView. it also has references to a new RenderScript class and our defined FountainRS class. when creating a new surface in the surfacedChanged method a new RenderScript class and a new FountainRS class are created. we also call the init method on the FountainRS class and pass in several arguments, including a reference to the RenderScript object. so lets have a look at the FountainRS. java file:

/* * 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 com.android.fountain;import android.content.res.Resources;import android.renderscript.*;import android.util.Log;public class FountainRS {    public static final int PART_COUNT = 20000;    static class SomeData {        public int x;        public int y;        public int rate;        public int count;        public float r;        public float g;        public float b;    }    public FountainRS() {    }    public void init(RenderScript rs, Resources res, int width, int height) {        mRS = rs;        mRes = res;        initRS();    }    public void newTouchPosition(int x, int y, int rate) {        if (mSD.rate == 0) {            mSD.r = ((x & 0x1) != 0) ? 0.f : 1.f;            mSD.g = ((x & 0x2) != 0) ? 0.f : 1.f;            mSD.b = ((x & 0x4) != 0) ? 0.f : 1.f;            if ((mSD.r + mSD.g + mSD.b) < 0.9f) {                mSD.r = 0.8f;                mSD.g = 0.5f;                mSD.b = 1.f;            }        }        mSD.rate = rate;        mSD.x = x;        mSD.y = y;        mIntAlloc.data(mSD);    }    /////////////////////////////////////////    private Resources mRes;    private RenderScript mRS;    private Allocation mIntAlloc;    private SimpleMesh mSM;    private SomeData mSD;    private Type mSDType;    private void initRS() {        mSD = new SomeData();        mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData");        mIntAlloc = Allocation.createTyped(mRS, mSDType);        mSD.count = PART_COUNT;        mIntAlloc.data(mSD);        Element.Builder eb = new Element.Builder(mRS);        eb.addFloat(Element.DataKind.USER, "dx");        eb.addFloat(Element.DataKind.USER, "dy");        eb.addFloatXY("");        eb.addUNorm8RGBA("");        Element primElement = eb.create();        SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS);        int vtxSlot = smb.addVertexType(primElement, PART_COUNT);        smb.setPrimitive(Primitive.POINT);        mSM = smb.create();        mSM.setName("PartMesh");        Allocation partAlloc = mSM.createVertexAllocation(vtxSlot);        partAlloc.setName("PartBuffer");        mSM.bindVertexAllocation(partAlloc, 0);        // All setup of named objects should be done by this point        // because we are about to compile the script.        ScriptC.Builder sb = new ScriptC.Builder(mRS);        sb.setScript(mRes, R.raw.fountain);        sb.setRoot(true);        sb.setType(mSDType, "Control", 0);        sb.setType(mSM.getVertexType(0), "point", 1);        Script script = sb.create();        script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);        script.bindAllocation(mIntAlloc, 0);        script.bindAllocation(partAlloc, 1);        mRS.contextBindRootScript(script);    }}

I'm not going to try and go through every detail of this code, but it seems that the interesting areas are in the initRS function. here we have element builders, Simple Mesh builders and last but not least a script builder. here we get a script builder instance, set the script to the fountain. c file. set up some types such as Control and point (remember, these were used in the fountain. c file) and then create the script.

So there we have it, this is a quick peek into how Renderscript might be used. there are still a lot of unanswered questions yet, and there is still a lot more to learn about how renderscript will, and can be use, but I hope these few code snippets will at least give people a starting point. as usual, if anyone else out there has any interesting insights or comments I 'd be really interested to hear them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.