`
yelinsen05
  • 浏览: 497860 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

<转>Android renderscript, more info' and an example application

阅读更多

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

    * Compiled on the device
    * Uses acc compiler
    * No architectural 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 would 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 many 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
          o drawable
                + gadgets_clock_mp3.png
          o raw
                + fountain.c
    * src
          o 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:

view sourceprint?
01	// Fountain test script
02	#pragma version(1)
03	 
04	int newPart = 0;
05	 
06	int main(int launchID) {
07	    int ct;
08	    int count = Control->count;
09	    int rate = Control->rate;
10	    float height = getHeight();
11	    struct point_s * p = (struct point_s *)point;
12	 
13	    if (rate) {
14	        float rMax = ((float)rate) * 0.005f;
15	        int x = Control->x;
16	        int y = Control->y;
17	        char r = Control->r * 255.f;
18	        char g = Control->g * 255.f;
19	        char b = Control->b * 255.f;
20	        struct point_s * np = &p[newPart];
21	 
22	        while (rate--) {
23	            vec2Rand((float *)np, rMax);
24	            np->x = x;
25	            np->y = y;
26	            np->r = r;
27	            np->g = g;
28	            np->b = b;
29	            np->a = 0xf0;
30	            newPart++;
31	            np++;
32	            if (newPart >= count) {
33	                newPart = 0;
34	                np = &p[newPart];
35	            }
36	        }
37	    }
38	 
39	    for (ct=0; ct < count; ct++) {
40	        float dy = p->dy + 0.15f;
41	        float posy = p->y + dy;
42	        if ((posy > height) && (dy > 0)) {
43	            dy *= -0.3f;
44	        }
45	        p->dy = dy;
46	        p->x += p->dx;
47	        p->y = posy;
48	        p++;
49	    }
50	 
51	    uploadToBufferObject(NAMED_PartBuffer);
52	    drawSimpleMesh(NAMED_PartMesh);
53	    return 1;
54	}

 

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:
view sourceprint?
01	/*
02	 * Copyright (C) 2008 The Android Open Source Project
03	 *
04	 * Licensed under the Apache License, Version 2.0 (the "License");
05	 * you may not use this file except in compliance with the License.
06	 * You may obtain a copy of the License at
07	 *
08	 *      http://www.apache.org/licenses/LICENSE-2.0
09	 *
10	 * Unless required by applicable law or agreed to in writing, software
11	 * distributed under the License is distributed on an "AS IS" BASIS,
12	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13	 * See the License for the specific language governing permissions and
14	 * limitations under the License.
15	 */
16	 
17	package com.android.fountain;
18	 
19	import java.io.Writer;
20	import java.util.ArrayList;
21	import java.util.concurrent.Semaphore;
22	 
23	import android.renderscript.RSSurfaceView;
24	import android.renderscript.RenderScript;
25	 
26	import android.content.Context;
27	import android.content.res.Resources;
28	import android.graphics.Bitmap;
29	import android.graphics.drawable.BitmapDrawable;
30	import android.graphics.drawable.Drawable;
31	import android.os.Handler;
32	import android.os.Message;
33	import android.util.AttributeSet;
34	import android.util.Log;
35	import android.view.Surface;
36	import android.view.SurfaceHolder;
37	import android.view.SurfaceView;
38	import android.view.KeyEvent;
39	import android.view.MotionEvent;
40	 
41	public class FountainView extends RSSurfaceView {
42	 
43	    public FountainView(Context context) {
44	        super(context);
45	        //setFocusable(true);
46	    }
47	 
48	    private RenderScript mRS;
49	    private FountainRS mRender;
50	 
51	    private void destroyRS() {
52	        if(mRS != null) {
53	            mRS = null;
54	            destroyRenderScript();
55	        }
56	        java.lang.System.gc();
57	    }
58	 
59	    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
60	        super.surfaceChanged(holder, format, w, h);
61	        destroyRS();
62	        mRS = createRenderScript(false, true);
63	        mRender = new FountainRS();
64	        mRender.init(mRS, getResources(), w, h);
65	    }
66	 
67	    public void surfaceDestroyed(SurfaceHolder holder) {
68	        // Surface will be destroyed when we return
69	        destroyRS();
70	    }
71	 
72	 
73	 
74	    @Override
75	    public boolean onTouchEvent(MotionEvent ev)
76	    {
77	        int act = ev.getAction();
78	        if (act == ev.ACTION_UP) {
79	            mRender.newTouchPosition(0, 0, 0);
80	            return false;
81	        }
82	        float rate = (ev.getPressure() * 50.f);
83	        rate *= rate;
84	        if(rate > 2000.f) {
85	            rate = 2000.f;
86	        }
87	        mRender.newTouchPosition((int)ev.getX(), (int)ev.getY(), (int)rate);
88	        return true;
89	    }
90	}


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:
view sourceprint?
001	/*
002	 * Copyright (C) 2008 The Android Open Source Project
003	 *
004	 * Licensed under the Apache License, Version 2.0 (the "License");
005	 * you may not use this file except in compliance with the License.
006	 * You may obtain a copy of the License at
007	 *
008	 *      http://www.apache.org/licenses/LICENSE-2.0
009	 *
010	 * Unless required by applicable law or agreed to in writing, software
011	 * distributed under the License is distributed on an "AS IS" BASIS,
012	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013	 * See the License for the specific language governing permissions and
014	 * limitations under the License.
015	 */
016	 
017	package com.android.fountain;
018	 
019	import android.content.res.Resources;
020	import android.renderscript.*;
021	import android.util.Log;
022	 
023	 
024	public class FountainRS {
025	    public static final int PART_COUNT = 20000;
026	 
027	    static class SomeData {
028	        public int x;
029	        public int y;
030	        public int rate;
031	        public int count;
032	        public float r;
033	        public float g;
034	        public float b;
035	    }
036	 
037	    public FountainRS() {
038	    }
039	 
040	    public void init(RenderScript rs, Resources res, int width, int height) {
041	        mRS = rs;
042	        mRes = res;
043	        initRS();
044	    }
045	 
046	    public void newTouchPosition(int x, int y, int rate) {
047	        if (mSD.rate == 0) {
048	            mSD.r = ((x & 0x1) != 0) ? 0.f : 1.f;
049	            mSD.g = ((x & 0x2) != 0) ? 0.f : 1.f;
050	            mSD.b = ((x & 0x4) != 0) ? 0.f : 1.f;
051	            if ((mSD.r + mSD.g + mSD.b) < 0.9f) {
052	                mSD.r = 0.8f;
053	                mSD.g = 0.5f;
054	                mSD.b = 1.f;
055	            }
056	        }
057	        mSD.rate = rate;
058	        mSD.x = x;
059	        mSD.y = y;
060	        mIntAlloc.data(mSD);
061	    }
062	 
063	 
064	    /////////////////////////////////////////
065	 
066	    private Resources mRes;
067	 
068	    private RenderScript mRS;
069	    private Allocation mIntAlloc;
070	    private SimpleMesh mSM;
071	    private SomeData mSD;
072	    private Type mSDType;
073	 
074	    private void initRS() {
075	        mSD = new SomeData();
076	        mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData");
077	        mIntAlloc = Allocation.createTyped(mRS, mSDType);
078	        mSD.count = PART_COUNT;
079	        mIntAlloc.data(mSD);
080	 
081	        Element.Builder eb = new Element.Builder(mRS);
082	        eb.addFloat(Element.DataKind.USER, "dx");
083	        eb.addFloat(Element.DataKind.USER, "dy");
084	        eb.addFloatXY("");
085	        eb.addUNorm8RGBA("");
086	        Element primElement = eb.create();
087	 
088	 
089	        SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS);
090	        int vtxSlot = smb.addVertexType(primElement, PART_COUNT);
091	        smb.setPrimitive(Primitive.POINT);
092	        mSM = smb.create();
093	        mSM.setName("PartMesh");
094	 
095	        Allocation partAlloc = mSM.createVertexAllocation(vtxSlot);
096	        partAlloc.setName("PartBuffer");
097	        mSM.bindVertexAllocation(partAlloc, 0);
098	 
099	        // All setup of named objects should be done by this point
100	        // because we are about to compile the script.
101	        ScriptC.Builder sb = new ScriptC.Builder(mRS);
102	        sb.setScript(mRes, R.raw.fountain);
103	        sb.setRoot(true);
104	        sb.setType(mSDType, "Control", 0);
105	        sb.setType(mSM.getVertexType(0), "point", 1);
106	        Script script = sb.create();
107	        script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
108	 
109	        script.bindAllocation(mIntAlloc, 0);
110	        script.bindAllocation(partAlloc, 1);
111	        mRS.contextBindRootScript(script);
112	    }
113	 
114	}


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.
分享到:
评论

相关推荐

    Android代码-利用 Renderscript 实现的一个简单的模糊效果封装组件

    Includes library and small example project. Gradle compile 'com.qiushui:blurredview:0.8.1' Enable RenderScript support mode: defaultConfig { renderscriptTargetApi 19 renderscriptSupportModeEnabled ...

    Android RenderScript高斯模糊

    Android RenderScript 是一个Android平台上的计算框架,它提供了一种高效的方法来执行计算密集型任务,特别是图形处理和计算机视觉应用。在Android中,RenderScript主要用于图像处理,如高斯模糊,因为它可以在GPU或...

    RenderScript案例学习代码

    分别为:RenderScript绘制字符串、RenderScript绘制颜色矩形、RenderScript绘制纹理矩形、RenderScript混合、RenderScript采样、RenderScript旋转的圆环(剪裁方式)、RenderScript中的光照、RenderScript中片元着色器...

    Android-RenderScript-Tutorials:我编写的Android RenderScript教程随附的源代码-android source code

    Android RenderScript教程 作者:Ian G. Clifton链接: : 概述 该存储库包含我编写的RenderScript教程的源代码。 源代码与教程文章可能会有一些差异(例如,为了使所有代码适合一个存储库而进行了一些小的改进和...

    YUVRenderScript:史上最强YUV转换RenderScript,支持转换为RGBA、BGRA,同时支持旋转和翻转。 YUV数据格式支持NV21、YV12、YUV420_888,对应Android Camera1API和Camera2 API的YUV处理

    这段时间在做一些Android Camera数据处理相关的工作,涉及到YUV转RGBA。 由于对性能要求极高,我采用了多种解决方案。 起初,我尝试了OpenCV中的方法,终于找到了性能。 不是很理想。 最后使用了libyuv。 该库的性能...

    renderscript-toolkit-release.aar

    实现高斯模糊的工具包,github开源项目https://github.com/android/renderscript-intrinsics-replacement-toolkit打包生成。

    renderscript Google官方源码

    RenderScript是Google推出的一种并行计算框架,主要应用于Android系统,用于提高应用程序的性能,特别是在图形处理、图像处理和计算密集型任务上。这个“renderscript Google官方源码”很可能是RenderScript的底层...

    RenderScript简单使用

    RenderScript是Android操作系统中的一种并行计算框架,用于提高设备上的计算性能,特别是在处理图形、图像和计算密集型任务时。在本示例中,我们将深入探讨如何使用RenderScript实现照片取反色这一功能。 首先,...

    renderscript代码示例

    RenderScript是Android系统中一种并行计算框架,用于提高设备上的计算性能,特别是在图像处理、图形渲染等领域的应用。这个压缩包提供了两个重要的文件夹,`BasicRenderScript`和`RenderScriptIntrinsic`,分别包含...

    renderScript

    renderScript_fina ,max批量渲染工具,可以一键渲染多个文件

    Android利用RenderScript实现毛玻璃模糊效果示例

    RenderScript is a framework for running computationally intensive tasks at high performance on Android. RenderScript is primarily oriented for use with data-parallel computation, although serial ...

    Camera 使用renderscript高效转化YUV到RGB

    Camera 使用renderscript高效转化YUV到RGB,我查了很多代码资料,关于renderscript的使用方法都没太看懂,特别是foreach的使用,以及camera照片需要90度旋转的问题都没有很好的解决。自己摸索了两天终于试了出来,...

    android studio build-tools 25.0.2

    Android Studio 是Google官方推出的Android应用开发集成环境,它包含了丰富的工具和功能,使得开发者能够高效地构建、测试和调试Android应用程序。在Android Studio中,`build-tools` 是一个至关重要的组件,它是一...

    RenderScriptPlayground:将RenderScript与Gradle和Android Studio结合使用的示例。 适用于Android API> = 8

    RenderScriptPlayground是一个示例性Android应用程序,显示了如何将RenderScript与Android Studio和Gradle一起使用。 应用程序正在使用RenderScript支持库,因此它应可与Android Froyo(2.2)一起使用。 入门 1.克隆...

    Android shade

    通过XML定义图形时,可以使用`&lt;vector&gt;`标签的`&lt;path&gt;`子元素的`android:shadowColor`、`android:shadowDx`、`android:shadowDy`和`android:shadowRadius`属性。 8. **RenderScript** RenderScript是一种低级别的...

    Android.Recipes.A.Problem-Solution.Approach.5th

    Discover more than 100 down-to-earth code recipes, covering a wide range of useful topics using complete and real-world working code examples. This book is updated to include the Android N SDK (7.0), ...

    android图像处理高级技术

    &lt;item name="android:textColor"&gt;#FF0000&lt;/item&gt; &lt;item name="android:textSize"&gt;20sp&lt;/item&gt; &lt;/style&gt; &lt;!-- 应用自定义风格到主题中 --&gt; &lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt;...

Global site tag (gtag.js) - Google Analytics