├── .classpath ├── .gitignore ├── .project ├── AndroidManifest.xml ├── README ├── build.xml ├── local.properties ├── proguard.cfg ├── project.properties ├── res ├── drawable-hdpi │ ├── c10_muse_tr.jpg │ ├── c11_muse_sb.jpg │ ├── c12_incubus_sc.jpg │ ├── c13_korn_ftl.jpg │ ├── c14_pt_pt.jpg │ ├── c15_ra_eg.jpg │ ├── c1_ff_wl.jpg │ ├── c2_ff_gh.jpg │ ├── c3_sublime.jpg │ ├── c4_dm_dr.jpg │ ├── c5_pf_dsm.jpg │ ├── c6_rhcp_c.jpg │ ├── c7_rhcp_iwy.jpg │ ├── c8_rhcp_sa.jpg │ ├── c9_patrice_one.jpg │ ├── icon.png │ ├── inovex_free.png │ ├── inovex_logo.jpg │ ├── inovex_logo200.jpg │ └── sunset.jpg ├── drawable-ldpi │ └── icon.png ├── drawable-mdpi │ └── icon.png ├── layout │ ├── chooser_activity.xml │ ├── mandelbrot_activity.xml │ └── sepia_activity.xml ├── raw │ ├── mandelbrot.bc │ ├── mono.bc │ └── rsgraphicsdemo.bc └── values │ └── strings.xml └── src └── de └── inovex └── mobi └── rsdemos ├── ChooserActivity.java ├── Mandelbrot$CalcMandel.class ├── Mandelbrot.class ├── MandelbrotActivity.java ├── RSCarouselDemoActivity.java ├── RSCarouselDemoScript.java ├── RSCarouselDemoView.java ├── RSGraphicsDemoActivity.java ├── RSGraphicsDemoScript.java ├── RSGraphicsDemoView.java ├── SepiaActivity.java ├── carousel.rs ├── mandelbrot.rs ├── rsgraphicsdemo.rs └── sepia.rs /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin/* 2 | gen/* 3 | .DS_Store 4 | res/raw/*.bc 5 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | RenderScriptPlayground 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Source code of the Android RenderScript Demos which I showed and used for my presentation at the Mobile Tech Con 2011. 2 | 3 | Some samples are based on code from daoki2 http://code.google.com/p/renderscript-examples/ which is mentioned at the top of the source code files. 4 | 5 | Compile and run on an Android 3.x tablet. This will NOT work on the emulator as it doesn't support RenderScript at all. 6 | 7 | Check the slides of the presentation here: 8 | 9 | http://www.slideshare.net/dhelleberg/renderscript-in-android-3x 10 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 29 | 30 | 31 | 40 | 41 | 42 | 43 | 47 | 48 | 49 | 51 | 63 | 64 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked in Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | 7 | # location of the SDK. This is only used by Ant 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | sdk.dir=/Users/dhelleberg/dev/android-sdk-mac_x86 11 | -------------------------------------------------------------------------------- /proguard.cfg: -------------------------------------------------------------------------------- 1 | -optimizationpasses 5 2 | -dontusemixedcaseclassnames 3 | -dontskipnonpubliclibraryclasses 4 | -dontpreverify 5 | -verbose 6 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 7 | 8 | -keep public class * extends android.app.Activity 9 | -keep public class * extends android.app.Application 10 | -keep public class * extends android.app.Service 11 | -keep public class * extends android.content.BroadcastReceiver 12 | -keep public class * extends android.content.ContentProvider 13 | -keep public class * extends android.app.backup.BackupAgentHelper 14 | -keep public class * extends android.preference.Preference 15 | -keep public class com.android.vending.licensing.ILicensingService 16 | 17 | -keepclasseswithmembernames class * { 18 | native ; 19 | } 20 | 21 | -keepclasseswithmembers class * { 22 | public (android.content.Context, android.util.AttributeSet); 23 | } 24 | 25 | -keepclasseswithmembers class * { 26 | public (android.content.Context, android.util.AttributeSet, int); 27 | } 28 | 29 | -keepclassmembers class * extends android.app.Activity { 30 | public void *(android.view.View); 31 | } 32 | 33 | -keepclassmembers enum * { 34 | public static **[] values(); 35 | public static ** valueOf(java.lang.String); 36 | } 37 | 38 | -keep class * implements android.os.Parcelable { 39 | public static final android.os.Parcelable$Creator *; 40 | } 41 | -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system use, 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | 10 | # Project target. 11 | target=android-13 12 | -------------------------------------------------------------------------------- /res/drawable-hdpi/c10_muse_tr.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c10_muse_tr.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c11_muse_sb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c11_muse_sb.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c12_incubus_sc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c12_incubus_sc.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c13_korn_ftl.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c13_korn_ftl.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c14_pt_pt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c14_pt_pt.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c15_ra_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c15_ra_eg.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c1_ff_wl.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c1_ff_wl.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c2_ff_gh.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c2_ff_gh.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c3_sublime.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c3_sublime.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c4_dm_dr.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c4_dm_dr.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c5_pf_dsm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c5_pf_dsm.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c6_rhcp_c.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c6_rhcp_c.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c7_rhcp_iwy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c7_rhcp_iwy.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c8_rhcp_sa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c8_rhcp_sa.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/c9_patrice_one.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/c9_patrice_one.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/icon.png -------------------------------------------------------------------------------- /res/drawable-hdpi/inovex_free.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/inovex_free.png -------------------------------------------------------------------------------- /res/drawable-hdpi/inovex_logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/inovex_logo.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/inovex_logo200.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/inovex_logo200.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/sunset.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-hdpi/sunset.jpg -------------------------------------------------------------------------------- /res/drawable-ldpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-ldpi/icon.png -------------------------------------------------------------------------------- /res/drawable-mdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/drawable-mdpi/icon.png -------------------------------------------------------------------------------- /res/layout/chooser_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 11 | 14 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /res/layout/mandelbrot_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 7 | 9 | 12 | 15 | 16 | 18 | 21 | 24 | 25 | 27 | 30 | 33 | 34 | 35 | 37 | 38 | -------------------------------------------------------------------------------- /res/layout/sepia_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 7 | 11 | 15 | 19 | 23 | 27 | 31 | 32 | 34 | 35 | -------------------------------------------------------------------------------- /res/raw/mandelbrot.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/raw/mandelbrot.bc -------------------------------------------------------------------------------- /res/raw/mono.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/raw/mono.bc -------------------------------------------------------------------------------- /res/raw/rsgraphicsdemo.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/res/raw/rsgraphicsdemo.bc -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello World, RenderScriptPlaygroundActivity! 4 | RenderScriptPlayground 5 | 6 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/ChooserActivity.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | 4 | import android.app.Activity; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.view.View; 9 | import android.view.View.OnClickListener; 10 | import android.widget.Button; 11 | 12 | public class ChooserActivity extends Activity { 13 | 14 | private Context mContext = this; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.chooser_activity); 20 | 21 | Button brs = (Button) findViewById(R.id.chooser_button1); 22 | brs.setOnClickListener(new OnClickListener() { 23 | 24 | @Override 25 | public void onClick(View v) { 26 | startActivity(new Intent(mContext, MandelbrotActivity.class)); 27 | } 28 | }); 29 | 30 | Button br = (Button) findViewById(R.id.chooser_button2); 31 | br.setOnClickListener(new OnClickListener() { 32 | 33 | @Override 34 | public void onClick(View v) { 35 | startActivity(new Intent(mContext, SepiaActivity.class)); 36 | } 37 | }); 38 | Button brgra = (Button) findViewById(R.id.chooser_button_simple_graphics); 39 | brgra.setOnClickListener(new OnClickListener() { 40 | 41 | @Override 42 | public void onClick(View v) { 43 | startActivity(new Intent(mContext, RSGraphicsDemoActivity.class)); 44 | } 45 | }); 46 | Button bcd = (Button) findViewById(R.id.chooser_button_carousel_Demo); 47 | bcd.setOnClickListener(new OnClickListener() { 48 | 49 | @Override 50 | public void onClick(View v) { 51 | startActivity(new Intent(mContext, RSCarouselDemoActivity.class)); 52 | } 53 | }); 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/Mandelbrot$CalcMandel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/src/de/inovex/mobi/rsdemos/Mandelbrot$CalcMandel.class -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/Mandelbrot.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhelleberg/RenderScriptDemos/70307a646cb8f4192d292ff737c22b2dc5aed16c/src/de/inovex/mobi/rsdemos/Mandelbrot.class -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/MandelbrotActivity.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | import android.app.Activity; 6 | import android.content.Context; 7 | import android.graphics.Bitmap; 8 | import android.graphics.Color; 9 | import android.os.AsyncTask; 10 | import android.os.Bundle; 11 | import android.renderscript.Allocation; 12 | import android.renderscript.Element; 13 | import android.renderscript.RenderScript; 14 | import android.util.Log; 15 | import android.view.View; 16 | import android.view.View.OnClickListener; 17 | import android.widget.Button; 18 | import android.widget.ImageView; 19 | import android.widget.TextView; 20 | 21 | /** 22 | * Calculates a Mandelbrot algorithm based bitmap using renderscript, java single threaded and java multithreaded 23 | * For demo purposes only 24 | * @author dhelleberg 25 | * 26 | */ 27 | 28 | public class MandelbrotActivity extends Activity { 29 | private RenderScript mRS; 30 | private ScriptC_mandelbrot mScript; 31 | private Context mContext = this; 32 | 33 | static int BAILOUT = 16; 34 | static int MAX_ITERATIONS = 1300; 35 | 36 | static final int SIZE_QUAD = 600; 37 | static final int SIZE_QUAD_HALF = SIZE_QUAD/2; 38 | 39 | //don't do this in production 40 | static int[] out = new int[SIZE_QUAD*SIZE_QUAD]; 41 | 42 | 43 | /** Called when the activity is first created. */ 44 | @Override 45 | public void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.mandelbrot_activity); 48 | Button brs = (Button) findViewById(R.id.buttonRS); 49 | brs.setOnClickListener(new OnClickListener() { 50 | 51 | @Override 52 | public void onClick(View v) { 53 | new RSMandelBrotTask().execute(SIZE_QUAD*SIZE_QUAD); 54 | } 55 | }); 56 | 57 | Button bja = (Button) findViewById(R.id.buttonJava); 58 | bja.setOnClickListener(new OnClickListener() { 59 | @Override 60 | public void onClick(View v) { 61 | new JavaMandelBrotTask().execute(SIZE_QUAD * SIZE_QUAD); 62 | } 63 | }); 64 | 65 | 66 | Button bj2 = (Button) findViewById(R.id.buttonJavaThreads); 67 | bj2.setOnClickListener(new OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | new JavaThreadedMandelBrotTask().execute(SIZE_QUAD*SIZE_QUAD); 71 | } 72 | }); 73 | } 74 | 75 | 76 | 77 | private int iterate(double x, double y) 78 | { 79 | double cr = y-0.5f; 80 | double ci = x; 81 | double zi = 0.0f; 82 | double zr = 0.0f; 83 | int i = 0; 84 | while (true) { 85 | i++; 86 | double temp = zr * zi; 87 | double zr2 = zr * zr; 88 | double zi2 = zi * zi; 89 | zr = zr2 - zi2 + cr; 90 | zi = temp + temp + ci; 91 | if (zi2 + zr2 > BAILOUT) 92 | { 93 | return i; 94 | } 95 | if (i > MAX_ITERATIONS) 96 | { 97 | return 0; 98 | } 99 | } 100 | } 101 | 102 | private class JavaMandelBrotTask extends AsyncTask 103 | { 104 | long duration = 0; 105 | 106 | @Override 107 | protected Bitmap doInBackground(Integer... params) { 108 | final int count = params[0]; 109 | final Bitmap b = Bitmap.createBitmap(SIZE_QUAD, SIZE_QUAD, Bitmap.Config.ARGB_8888); 110 | 111 | long start = System.currentTimeMillis(); 112 | for(int c = 0; c < count; c++) 113 | { 114 | double myy = ((int)(-SIZE_QUAD_HALF+(c / SIZE_QUAD))) / (double)SIZE_QUAD_HALF; 115 | double myx = (-SIZE_QUAD_HALF+(c % SIZE_QUAD)) / (double)SIZE_QUAD_HALF; 116 | int val = iterate(myx , myy); 117 | int by = (c / SIZE_QUAD); 118 | int bx = (c % SIZE_QUAD); 119 | b.setPixel(bx, by, Color.rgb((int)(val*1.8),(int) (val*1.0), (int)(val*1.5))); 120 | 121 | } 122 | long end = System.currentTimeMillis(); 123 | duration = end-start; 124 | Log.v("!!","Java Elapsed " + duration); 125 | 126 | return b; 127 | } 128 | @Override 129 | protected void onPostExecute(Bitmap result) { 130 | ImageView iv = (ImageView) findViewById(R.id.imageView1); 131 | iv.setImageBitmap(result); 132 | 133 | TextView tv = (TextView) findViewById(R.id.tv_java); 134 | tv.setText(duration+" ms"); 135 | } 136 | 137 | } 138 | 139 | private class JavaThreadedMandelBrotTask extends AsyncTask 140 | { 141 | long time = 0; 142 | 143 | @Override 144 | protected Bitmap doInBackground(Integer... params) { 145 | final int count = params[0]; 146 | Thread t1; 147 | Thread t2; 148 | 149 | final CountDownLatch doneSignal = new CountDownLatch(2); 150 | 151 | 152 | final int[]res = new int[params[0]]; 153 | final Bitmap b = Bitmap.createBitmap(SIZE_QUAD, SIZE_QUAD, Bitmap.Config.ARGB_8888); 154 | 155 | t1 = new Thread(new Runnable() { 156 | 157 | @Override 158 | public void run() { 159 | for(int c = 0; c < (count/2)+25000; c++) 160 | { 161 | double myy = ((int)(-SIZE_QUAD_HALF+(c / SIZE_QUAD))) / (double)SIZE_QUAD_HALF; 162 | double myx = (-SIZE_QUAD_HALF+(c % SIZE_QUAD)) / (double)SIZE_QUAD_HALF; 163 | int val = iterate(myx , myy); 164 | res[c] = val; 165 | } 166 | Log.v("T1", "done"); 167 | doneSignal.countDown(); 168 | } 169 | }); 170 | 171 | t2 = new Thread(new Runnable() { 172 | 173 | @Override 174 | public void run() { 175 | for(int c = (count/2)+25000; c < count; c++) 176 | { 177 | double myy = ((int)(-SIZE_QUAD_HALF+(c / SIZE_QUAD))) / (double)SIZE_QUAD_HALF; 178 | double myx = (-SIZE_QUAD_HALF+(c % SIZE_QUAD)) / (double)SIZE_QUAD_HALF; 179 | int val = iterate(myx , myy); 180 | res[c] = val; 181 | } 182 | Log.v("T2", "done"); 183 | doneSignal.countDown(); 184 | } 185 | }); 186 | 187 | long start = System.currentTimeMillis(); 188 | 189 | t1.start(); 190 | t2.start(); 191 | try { 192 | doneSignal.await(); 193 | } catch (InterruptedException e) { 194 | e.printStackTrace(); 195 | } 196 | for (int i = 0; i < res.length; i++) { 197 | int val = res[i]; 198 | int by = (i / SIZE_QUAD); 199 | int bx = (i % SIZE_QUAD); 200 | b.setPixel(bx, by, Color.rgb((int)(val*1.8),(int) (val*1.0), (int)(val*1.5))); 201 | } 202 | long end = System.currentTimeMillis(); 203 | 204 | long diff = end-start; 205 | 206 | Log.v("!!","Java Elapsed " + diff); 207 | time = diff; 208 | return b; 209 | } 210 | 211 | @Override 212 | protected void onPostExecute(Bitmap result) { 213 | ImageView iv = (ImageView) findViewById(R.id.imageView1); 214 | iv.setImageBitmap(result); 215 | 216 | TextView tv = (TextView) findViewById(R.id.tv_javathreads); 217 | tv.setText(time+" ms"); 218 | } 219 | } 220 | 221 | 222 | private class RSMandelBrotTask extends AsyncTask 223 | { 224 | 225 | long duration = 0; 226 | @Override 227 | protected Bitmap doInBackground(Integer... params) { 228 | 229 | mRS = RenderScript.create(mContext); 230 | mScript = new ScriptC_mandelbrot(mRS, getResources(), R.raw.mandelbrot); 231 | 232 | final int size = params[0]; 233 | 234 | Allocation alloc_in = Allocation.createSized(mRS, Element.BOOLEAN(mRS), size); 235 | 236 | Bitmap b = Bitmap.createBitmap(SIZE_QUAD,SIZE_QUAD,Bitmap.Config.ARGB_8888); 237 | Allocation alloc_out = Allocation.createFromBitmap(mRS, b); 238 | 239 | mScript.set_gIn(alloc_in); 240 | mScript.set_gOut(alloc_out); 241 | mScript.set_gScript(mScript); 242 | 243 | long start = System.currentTimeMillis(); 244 | mScript.invoke_mandelbrot(); 245 | 246 | alloc_out.copyTo(b); 247 | 248 | long end = System.currentTimeMillis(); 249 | duration = end-start; 250 | Log.v("Jupp:", duration+" ms"); 251 | return b; 252 | } 253 | 254 | @Override 255 | protected void onPostExecute(Bitmap result) { 256 | ImageView iv = (ImageView) findViewById(R.id.imageView1); 257 | iv.setImageBitmap(result); 258 | 259 | TextView tv = (TextView) findViewById(R.id.tv_rs); 260 | tv.setText(duration+" ms"); 261 | 262 | } 263 | 264 | } 265 | } -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/RSCarouselDemoActivity.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | /** 4 | * This code is based on the work of daoki2 5 | * CarouselActivity.java 6 | * Copyright (c) 2011 daoki2 7 | * http://code.google.com/p/renderscript-examples/ 8 | * 9 | */ 10 | 11 | import android.app.Activity; 12 | import android.os.Bundle; 13 | 14 | public class RSCarouselDemoActivity extends Activity { 15 | 16 | RSCarouselDemoView mCView = null; 17 | 18 | @Override 19 | public void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | mCView = new RSCarouselDemoView(this); 22 | setContentView(mCView); 23 | } 24 | 25 | /** 26 | * Missing onDestroy, cleanups, etc. Don't use this in production! 27 | */ 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/RSCarouselDemoScript.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | /** 4 | * This code is based on the work of daoki2 5 | * CarouselRS.java 6 | * Copyright (c) 2011 daoki2 7 | * http://code.google.com/p/renderscript-examples/ 8 | */ 9 | 10 | 11 | import android.content.res.Resources; 12 | import android.graphics.Bitmap; 13 | import android.graphics.BitmapFactory; 14 | import android.renderscript.Allocation; 15 | import android.renderscript.Matrix4f; 16 | import android.renderscript.ProgramFragment; 17 | import android.renderscript.ProgramFragmentFixedFunction; 18 | import android.renderscript.ProgramRaster; 19 | import android.renderscript.ProgramStore; 20 | import android.renderscript.ProgramStore.BlendDstFunc; 21 | import android.renderscript.ProgramStore.BlendSrcFunc; 22 | import android.renderscript.ProgramVertex; 23 | import android.renderscript.ProgramVertexFixedFunction; 24 | import android.renderscript.RenderScript; 25 | import android.renderscript.RenderScriptGL; 26 | import android.renderscript.Sampler; 27 | 28 | 29 | public class RSCarouselDemoScript { 30 | 31 | int mWidth; 32 | int mHeight; 33 | 34 | public void init(RenderScriptGL rs, Resources res) { 35 | mRS = rs; 36 | mWidth = mRS.getWidth(); 37 | mHeight = mRS.getHeight(); 38 | mRes = res; 39 | mOptionsARGB.inScaled = false; 40 | mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888; 41 | initRS(); 42 | } 43 | 44 | public void surfaceChanged() { 45 | mWidth = mRS.getWidth(); 46 | mHeight = mRS.getHeight(); 47 | Matrix4f proj = new Matrix4f(); 48 | proj.loadOrthoWindow(mWidth, mHeight); 49 | mPVA.setProjection(proj); 50 | } 51 | 52 | private Resources mRes; 53 | private RenderScriptGL mRS; 54 | private Sampler mLinearClamp; 55 | private ProgramStore mProgStoreBlendNone; 56 | private ProgramFragment mProgFragmentTexture; 57 | private ProgramVertex mProgVertex; 58 | private ProgramVertexFixedFunction.Constants mPVA; 59 | private ProgramRaster mCullNone; 60 | private ScriptC_carousel mScript; 61 | 62 | private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options(); 63 | 64 | int px, py; 65 | 66 | public void onActionDown(int x, int y) { 67 | px = x; 68 | py = y; 69 | mScript.set_gPx(x); 70 | mScript.set_gPy(y); 71 | } 72 | 73 | public void onActionMove(int x, int y) { 74 | mScript.set_gVx((float)(px - x)/5); 75 | mScript.set_gVy((float)(py - y)/5); 76 | px = x; 77 | py = y; 78 | } 79 | 80 | public void onActionUp(int x, int y) { 81 | //mScript.set_gVx(0); 82 | //mScript.set_gVy(0); 83 | //px = 0; 84 | //py = 0; 85 | } 86 | 87 | public void onFling(float vx, float vy) { 88 | mScript.set_gVx(vx); 89 | mScript.set_gVy(vy); 90 | } 91 | 92 | ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) { 93 | ProgramStore.Builder builder = new ProgramStore.Builder(rs); 94 | builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); 95 | builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE); 96 | builder.setDitherEnabled(false); 97 | builder.setDepthMaskEnabled(false); 98 | return builder.create(); 99 | } 100 | 101 | private void initProgramStore() { 102 | // Use stock the stock program store object 103 | mProgStoreBlendNone = ProgramStore.BLEND_NONE_DEPTH_NONE(mRS); 104 | mScript.set_gProgStoreBlendNone(mProgStoreBlendNone); 105 | } 106 | 107 | private void initProgramFragment() { 108 | ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS); 109 | texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE, 110 | ProgramFragmentFixedFunction.Builder.Format.RGBA, 0); 111 | mProgFragmentTexture = texBuilder.create(); 112 | mProgFragmentTexture.bindSampler(mLinearClamp, 0); 113 | 114 | mScript.set_gProgFragmentTexture(mProgFragmentTexture); 115 | } 116 | 117 | private void initProgramVertex() { 118 | ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS); 119 | mProgVertex = pvb.create(); 120 | 121 | mPVA = new ProgramVertexFixedFunction.Constants(mRS); 122 | ((ProgramVertexFixedFunction)mProgVertex).bindConstants(mPVA); 123 | Matrix4f proj = new Matrix4f(); 124 | proj.loadOrthoWindow(mWidth, mHeight); 125 | mPVA.setProjection(proj); 126 | 127 | mScript.set_gProgVertex(mProgVertex); 128 | } 129 | 130 | private void loadImages() { 131 | 132 | mScript.set_gTex_00(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c1_ff_wl, 133 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 134 | Allocation.USAGE_GRAPHICS_TEXTURE)); 135 | 136 | mScript.set_gTex_01(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c2_ff_gh, 137 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 138 | Allocation.USAGE_GRAPHICS_TEXTURE)); 139 | 140 | mScript.set_gTex_02(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c3_sublime, 141 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 142 | Allocation.USAGE_GRAPHICS_TEXTURE)); 143 | 144 | mScript.set_gTex_03(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c4_dm_dr, 145 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 146 | Allocation.USAGE_GRAPHICS_TEXTURE)); 147 | 148 | mScript.set_gTex_04(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c5_pf_dsm, 149 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 150 | Allocation.USAGE_GRAPHICS_TEXTURE)); 151 | 152 | mScript.set_gTex_05(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c6_rhcp_c, 153 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 154 | Allocation.USAGE_GRAPHICS_TEXTURE)); 155 | 156 | mScript.set_gTex_06(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c7_rhcp_iwy, 157 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 158 | Allocation.USAGE_GRAPHICS_TEXTURE)); 159 | 160 | mScript.set_gTex_07(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c8_rhcp_sa, 161 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 162 | Allocation.USAGE_GRAPHICS_TEXTURE)); 163 | 164 | mScript.set_gTex_08(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c9_patrice_one, 165 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 166 | Allocation.USAGE_GRAPHICS_TEXTURE)); 167 | 168 | mScript.set_gTex_09(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c10_muse_tr, 169 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 170 | Allocation.USAGE_GRAPHICS_TEXTURE)); 171 | 172 | mScript.set_gTex_10(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c11_muse_sb, 173 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 174 | Allocation.USAGE_GRAPHICS_TEXTURE)); 175 | 176 | mScript.set_gTex_11(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c12_incubus_sc, 177 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 178 | Allocation.USAGE_GRAPHICS_TEXTURE)); 179 | 180 | mScript.set_gTex_12(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c13_korn_ftl, 181 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 182 | Allocation.USAGE_GRAPHICS_TEXTURE)); 183 | 184 | mScript.set_gTex_13(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c14_pt_pt, 185 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 186 | Allocation.USAGE_GRAPHICS_TEXTURE)); 187 | 188 | mScript.set_gTex_14(Allocation.createFromBitmapResource(mRS, mRes, R.drawable.c15_ra_eg, 189 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 190 | Allocation.USAGE_GRAPHICS_TEXTURE)); 191 | 192 | 193 | ScriptField_Bitmaps bitmap = new ScriptField_Bitmaps(mRS, 15); 194 | mScript.bind_bitmap(bitmap); 195 | } 196 | 197 | private void initSamplers() { 198 | mLinearClamp = Sampler.CLAMP_LINEAR(mRS); 199 | mScript.set_gLinearClamp(mLinearClamp); 200 | } 201 | 202 | private void initProgramRaster() { 203 | mCullNone = ProgramRaster.CULL_NONE(mRS); 204 | mScript.set_gCullNone(mCullNone); 205 | } 206 | 207 | private void initRS() { 208 | mScript = new ScriptC_carousel(mRS, mRes, R.raw.carousel); 209 | 210 | initSamplers(); 211 | initProgramStore(); 212 | initProgramFragment(); 213 | initProgramVertex(); 214 | loadImages(); 215 | initProgramRaster(); 216 | 217 | mRS.bindRootScript(mScript); 218 | } 219 | } 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/RSCarouselDemoView.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | 4 | import android.content.Context; 5 | import android.renderscript.RSSurfaceView; 6 | import android.renderscript.RenderScriptGL; 7 | import android.view.GestureDetector; 8 | import android.view.MotionEvent; 9 | import android.view.SurfaceHolder; 10 | 11 | /** 12 | * this code is based on the work of daoki2 13 | * CarouselView.java 14 | * Copyright (c) 2011 daoki2 15 | * http://code.google.com/p/renderscript-examples/ 16 | * 17 | */ 18 | 19 | public class RSCarouselDemoView extends RSSurfaceView implements GestureDetector.OnGestureListener { 20 | private RenderScriptGL mRS; 21 | private RSCarouselDemoScript mRender; 22 | 23 | 24 | public RSCarouselDemoView(Context context) { 25 | super(context); 26 | ensureRenderScript(); 27 | } 28 | 29 | 30 | private void ensureRenderScript() { 31 | if (mRS == null) { 32 | RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig(); 33 | sc.setDepth(16, 24); 34 | mRS = createRenderScriptGL(sc); 35 | mRender = new RSCarouselDemoScript(); 36 | mRender.init(mRS, getResources()); 37 | } 38 | } 39 | 40 | @Override 41 | protected void onAttachedToWindow() { 42 | super.onAttachedToWindow(); 43 | ensureRenderScript(); 44 | } 45 | 46 | @Override 47 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 48 | super.surfaceChanged(holder, format, w, h); 49 | mRender.surfaceChanged(); 50 | } 51 | 52 | @Override 53 | protected void onDetachedFromWindow() { 54 | mRender = null; 55 | if (mRS != null) { 56 | mRS = null; 57 | destroyRenderScriptGL(); 58 | } 59 | } 60 | 61 | @Override 62 | public boolean onTouchEvent(MotionEvent ev) { 63 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { 64 | mRender.onActionDown((int)ev.getX(), (int)ev.getY()); 65 | return true; 66 | } 67 | 68 | if (ev.getAction() == MotionEvent.ACTION_MOVE) { 69 | mRender.onActionMove((int)ev.getX(), (int)ev.getY()); 70 | return true; 71 | } 72 | 73 | if (ev.getAction() == MotionEvent.ACTION_UP) { 74 | mRender.onActionUp((int)ev.getX(), (int)ev.getY()); 75 | return true; 76 | } 77 | 78 | return false; 79 | } 80 | 81 | @Override 82 | public boolean onDown(MotionEvent e) { 83 | //addInfo("Down"); 84 | return false; 85 | } 86 | 87 | public boolean onFling(MotionEvent e0,MotionEvent e1, 88 | float velocityX,float velocityY) { 89 | //addInfo("Fling("+velocityX+","+velocityY+")"); 90 | mRender.onFling(velocityX, velocityY); 91 | return false; 92 | } 93 | 94 | public void onLongPress(MotionEvent e) { 95 | //addInfo("LongPress"); 96 | } 97 | 98 | public boolean onScroll(MotionEvent e0,MotionEvent e1, 99 | float distanceX,float distanceY) { 100 | //addInfo("Scroll("+distanceX+","+distanceY+")"); 101 | return false; 102 | } 103 | 104 | public void onShowPress(MotionEvent e) { 105 | //addInfo("ShowPress"); 106 | } 107 | 108 | public boolean onSingleTapUp(MotionEvent e) { 109 | //addInfo("SigngleTapUp"); 110 | return false; 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/RSGraphicsDemoActivity.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | 6 | public class RSGraphicsDemoActivity extends Activity { 7 | 8 | private RSGraphicsDemoView mView; 9 | 10 | @Override 11 | public void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | 14 | mView = new RSGraphicsDemoView(this); 15 | setContentView(mView); 16 | } 17 | 18 | /** 19 | * Missing onDestroy, cleanups, etc. Don't use this in production! 20 | */ 21 | 22 | } -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/RSGraphicsDemoScript.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | 4 | import android.content.res.Resources; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.renderscript.Allocation; 8 | import android.renderscript.Mesh; 9 | import android.renderscript.ProgramFragmentFixedFunction; 10 | import android.renderscript.ProgramStore; 11 | import android.renderscript.RenderScriptGL; 12 | 13 | public class RSGraphicsDemoScript { 14 | 15 | private Resources mRes; 16 | private RenderScriptGL mRSGL; 17 | private ScriptC_rsgraphicsdemo mScript; 18 | private ProgramFragmentFixedFunction mProgramFragment; 19 | 20 | 21 | 22 | // This provides us with the renderscript context and resources that 23 | // allow us to create the script that does rendering 24 | public void init(RenderScriptGL rsGL, Resources res) { 25 | mRSGL = rsGL; 26 | mRes = res; 27 | 28 | //get instance of the script wrapper class 29 | mScript = new ScriptC_rsgraphicsdemo(mRSGL, mRes, R.raw.rsgraphicsdemo); 30 | 31 | //load bitmap for texturing 32 | Bitmap logo = BitmapFactory.decodeResource( res, R.drawable.inovex_free); 33 | float logo_with = logo.getWidth(); 34 | float logo_height = logo.getHeight(); 35 | 36 | //create allocation 37 | Allocation mLogoAlloc = Allocation.createFromBitmap(mRSGL, logo); 38 | 39 | //Build mesh and set texture coords 40 | Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRSGL, 2, Mesh.TriangleMeshBuilder.TEXTURE_0); 41 | tmb.setTexture(0f, 0f); 42 | tmb.addVertex(0f, 0f); 43 | tmb.setTexture(1, 0); 44 | tmb.addVertex(logo_with, 0f); 45 | tmb.setTexture(1, 1); 46 | tmb.addVertex(logo_with, logo_height); 47 | tmb.setTexture(0, 1); 48 | tmb.addVertex(0, logo_height); 49 | //build triangles 50 | tmb.addTriangle(0, 3, 1); 51 | tmb.addTriangle(1, 3, 2); 52 | Mesh logoMesh = tmb.create(true); 53 | mScript.set_gLogoMesh(logoMesh); 54 | 55 | ProgramFragmentFixedFunction.Builder textureBuilder = new ProgramFragmentFixedFunction.Builder(mRSGL); 56 | textureBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.DECAL, 57 | ProgramFragmentFixedFunction.Builder.Format.RGBA, 0); 58 | mProgramFragment = textureBuilder.create(); 59 | mProgramFragment.bindTexture(mLogoAlloc, 0); 60 | mScript.set_gProgFragmentTexture(mProgramFragment); 61 | 62 | mRSGL.bindProgramStore(ProgramStore.BLEND_ALPHA_DEPTH_NONE(mRSGL)); 63 | 64 | mRSGL.bindRootScript(mScript); 65 | 66 | 67 | } 68 | 69 | public void onActionDown(int x, int y) { 70 | mScript.set_gTouchX(x); 71 | mScript.set_gTouchY(y); 72 | } 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/RSGraphicsDemoView.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | import android.content.Context; 4 | import android.renderscript.RSSurfaceView; 5 | import android.renderscript.RenderScriptGL; 6 | import android.view.MotionEvent; 7 | 8 | public class RSGraphicsDemoView extends RSSurfaceView{ 9 | 10 | // Renderscipt context 11 | private RenderScriptGL mRSGL; 12 | private RSGraphicsDemoScript mRenderScript; 13 | 14 | public RSGraphicsDemoView(Context context) { 15 | super(context); 16 | initRenderScript(); 17 | } 18 | 19 | @Override 20 | protected void onAttachedToWindow() { 21 | super.onAttachedToWindow(); 22 | initRenderScript(); 23 | } 24 | 25 | @Override 26 | public boolean onTouchEvent(MotionEvent ev) { 27 | // Pass touch events from the system to the rendering script 28 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { 29 | mRenderScript.onActionDown((int)ev.getX(), (int)ev.getY()); 30 | return true; 31 | } 32 | return false; 33 | } 34 | 35 | private void initRenderScript() { 36 | if (mRSGL == null) { 37 | // Initialize renderscript with desired surface characteristics. 38 | // In this case, just use the defaults 39 | RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig(); 40 | mRSGL = createRenderScriptGL(sc); 41 | // Create an instance of the script that does the rendering 42 | mRenderScript = new RSGraphicsDemoScript(); 43 | mRenderScript.init(mRSGL, getResources()); 44 | } 45 | } 46 | 47 | @Override 48 | protected void onDetachedFromWindow() { 49 | // Handle the system event and clean up 50 | //mRender = null; 51 | if (mRSGL != null) { 52 | mRSGL = null; 53 | destroyRenderScriptGL(); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/SepiaActivity.java: -------------------------------------------------------------------------------- 1 | package de.inovex.mobi.rsdemos; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | import android.app.Activity; 6 | import android.content.Context; 7 | import android.graphics.Bitmap; 8 | import android.graphics.BitmapFactory; 9 | import android.os.AsyncTask; 10 | import android.os.Bundle; 11 | import android.renderscript.Allocation; 12 | import android.renderscript.RenderScript; 13 | import android.util.Log; 14 | import android.view.View; 15 | import android.view.View.OnClickListener; 16 | import android.widget.Button; 17 | import android.widget.ImageView; 18 | import android.widget.TextView; 19 | 20 | public class SepiaActivity extends Activity { 21 | 22 | Context mContext = this; 23 | 24 | 25 | /** Called when the activity is first created. */ 26 | @Override 27 | public void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.sepia_activity); 30 | 31 | final BitmapFactory.Options options = new BitmapFactory.Options(); 32 | options.inPreferredConfig = Bitmap.Config.ARGB_8888; 33 | final Bitmap input = BitmapFactory.decodeResource(getResources(), R.drawable.sunset, options); 34 | 35 | Button brs = (Button) findViewById(R.id.sepia_buttonRS); 36 | brs.setOnClickListener(new OnClickListener() { 37 | 38 | @Override 39 | public void onClick(View v) { 40 | new RSSepiaTask().execute(input); 41 | } 42 | }); 43 | 44 | Button bja = (Button) findViewById(R.id.sepia_buttonJava); 45 | bja.setOnClickListener(new OnClickListener() { 46 | 47 | @Override 48 | public void onClick(View v) { 49 | new JavaSepiaTask().execute(input); 50 | 51 | } 52 | }); 53 | 54 | 55 | Button bj2 = (Button) findViewById(R.id.sepia_buttonJavaThreads); 56 | bj2.setOnClickListener(new OnClickListener() { 57 | 58 | @Override 59 | public void onClick(View v) { 60 | new JavaThreadedSepiaTask().execute(input); 61 | 62 | } 63 | 64 | }); 65 | 66 | } 67 | 68 | private class RSSepiaTask extends AsyncTask 69 | { 70 | 71 | private RenderScript mRS; 72 | long duration; 73 | 74 | @Override 75 | protected Bitmap doInBackground(Bitmap... params) { 76 | 77 | 78 | mRS = RenderScript.create(mContext); 79 | 80 | final Bitmap mBitmapIn = params[0]; 81 | 82 | Bitmap mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), mBitmapIn.getHeight(), 83 | mBitmapIn.getConfig()); 84 | 85 | Allocation mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn, 86 | Allocation.MipmapControl.MIPMAP_NONE, 87 | Allocation.USAGE_SCRIPT); 88 | 89 | Allocation mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType()); 90 | 91 | ScriptC_sepia mScript = new ScriptC_sepia(mRS, getResources(), R.raw.sepia); 92 | 93 | mScript.set_gIn(mInAllocation); 94 | mScript.set_gOut(mOutAllocation); 95 | mScript.set_gScript(mScript); 96 | 97 | long start = System.currentTimeMillis(); 98 | 99 | mScript.invoke_sepiaFilter(); 100 | mOutAllocation.copyTo(mBitmapOut); 101 | 102 | duration = System.currentTimeMillis()-start; 103 | 104 | return mBitmapOut; 105 | } 106 | 107 | @Override 108 | protected void onPostExecute(Bitmap result) { 109 | ImageView iv = (ImageView) findViewById(R.id.sepia_imageView1); 110 | iv.setImageBitmap(result); 111 | 112 | TextView tv = (TextView) findViewById(R.id.sepia_tv_rs); 113 | tv.setText(duration+" ms"); 114 | 115 | } 116 | } 117 | 118 | private class JavaThreadedSepiaTask extends AsyncTask 119 | { 120 | 121 | private long duration; 122 | 123 | @Override 124 | protected Bitmap doInBackground(Bitmap... params) { 125 | Thread t1; 126 | Thread t2; 127 | 128 | final CountDownLatch doneSignal = new CountDownLatch(2); 129 | 130 | final float gSepiaDepth = 20/255.0f; 131 | final float gSepiaIntensity = 50/255.0f; 132 | 133 | 134 | final Bitmap bitmap_in = params[0]; 135 | int count = bitmap_in.getWidth() * bitmap_in.getHeight(); 136 | final int[]pixels = new int[count]; 137 | bitmap_in.getPixels(pixels, 0, bitmap_in.getWidth(), 0, 0, bitmap_in.getWidth(), bitmap_in.getHeight()); 138 | 139 | 140 | t1 = new Thread(new Runnable() { 141 | 142 | @Override 143 | public void run() { 144 | 145 | for (int i = 0; i < pixels.length/2; i++) { 146 | int pixel = pixels[i]; 147 | float r = ((pixel >> 16) & 0xff) / 255.0f; 148 | float g = ((pixel >> 8) & 0xff) / 255.0f; 149 | float b = ((pixel >> 0) & 0xff) / 255.0f; 150 | float gray = ( r * 0.299f + g * 0.587f + b * 0.114f); 151 | 152 | r = gray + (gSepiaDepth*2); 153 | g = gray + gSepiaDepth; 154 | b = gray - gSepiaIntensity; 155 | 156 | if(r > 1.0) r = 1.0f; 157 | if(g > 1.0) g = 1.0f; 158 | if(b > 1.0) b = 1.0f; 159 | if(b < 0.0) b = 0.0f; 160 | 161 | 162 | pixels[i] = 0xff000000 | ( (int)(r*255.0f) << 16) | ((int)(g*255.0f) << 8) | ((int)(b*255.0f) << 0); 163 | } 164 | Log.v("T1", "done"); 165 | doneSignal.countDown(); 166 | } 167 | }); 168 | 169 | t2 = new Thread(new Runnable() { 170 | 171 | @Override 172 | public void run() { 173 | 174 | for (int i = pixels.length/2; i < pixels.length; i++) { 175 | int pixel = pixels[i]; 176 | float r = ((pixel >> 16) & 0xff) / 255.0f; 177 | float g = ((pixel >> 8) & 0xff) / 255.0f; 178 | float b = ((pixel >> 0) & 0xff) / 255.0f; 179 | float gray = ( r * 0.299f + g * 0.587f + b * 0.114f); 180 | 181 | r = gray + (gSepiaDepth*2); 182 | g = gray + gSepiaDepth; 183 | b = gray - gSepiaIntensity; 184 | 185 | if(r > 1.0) r = 1.0f; 186 | if(g > 1.0) g = 1.0f; 187 | if(b > 1.0) b = 1.0f; 188 | if(b < 0.0) b = 0.0f; 189 | 190 | 191 | pixels[i] = 0xff000000 | ( (int)(r*255.0f) << 16) | ((int)(g*255.0f) << 8) | ((int)(b*255.0f) << 0); 192 | } 193 | Log.v("T2", "done"); 194 | doneSignal.countDown(); 195 | } 196 | }); 197 | 198 | long start = System.currentTimeMillis(); 199 | 200 | t1.start(); 201 | t2.start(); 202 | try { 203 | doneSignal.await(); 204 | } catch (InterruptedException e) { 205 | e.printStackTrace(); 206 | } 207 | bitmap_in.setPixels(pixels, 0, bitmap_in.getWidth(), 0, 0, bitmap_in.getWidth(), bitmap_in.getHeight()); 208 | duration = System.currentTimeMillis()-start; 209 | 210 | return bitmap_in; 211 | } 212 | @Override 213 | protected void onPostExecute(Bitmap result) { 214 | ImageView iv = (ImageView) findViewById(R.id.sepia_imageView1); 215 | iv.setImageBitmap(result); 216 | 217 | TextView tv = (TextView) findViewById(R.id.sepia_tv_javathreads); 218 | tv.setText(duration+" ms"); 219 | 220 | } 221 | 222 | 223 | } 224 | 225 | private class JavaSepiaTask extends AsyncTask 226 | { 227 | long duration; 228 | 229 | @Override 230 | protected Bitmap doInBackground(Bitmap... params) { 231 | 232 | final float gSepiaDepth = 20/255.0f; 233 | final float gSepiaIntensity = 50/255.0f; 234 | 235 | 236 | final Bitmap bitmap_in = params[0]; 237 | int count = bitmap_in.getWidth() * bitmap_in.getHeight(); 238 | int[]pixels = new int[count]; 239 | bitmap_in.getPixels(pixels, 0, bitmap_in.getWidth(), 0, 0, bitmap_in.getWidth(), bitmap_in.getHeight()); 240 | 241 | long start = System.currentTimeMillis(); 242 | for (int i = 0; i < pixels.length; i++) { 243 | int pixel = pixels[i]; 244 | float r = ((pixel >> 16) & 0xff) / 255.0f; 245 | float g = ((pixel >> 8) & 0xff) / 255.0f; 246 | float b = ((pixel >> 0) & 0xff) / 255.0f; 247 | float gray = ( r * 0.299f + g * 0.587f + b * 0.114f); 248 | 249 | r = gray + (gSepiaDepth*2); 250 | g = gray + gSepiaDepth; 251 | b = gray - gSepiaIntensity; 252 | 253 | if(r > 1.0) r = 1.0f; 254 | if(g > 1.0) g = 1.0f; 255 | if(b > 1.0) b = 1.0f; 256 | if(b < 0.0) b = 0.0f; 257 | 258 | 259 | pixels[i] = 0xff000000 | ( (int)(r*255.0f) << 16) | ((int)(g*255.0f) << 8) | ((int)(b*255.0f) << 0); 260 | } 261 | bitmap_in.setPixels(pixels, 0, bitmap_in.getWidth(), 0, 0, bitmap_in.getWidth(), bitmap_in.getHeight()); 262 | duration = System.currentTimeMillis()-start; 263 | 264 | return bitmap_in; 265 | } 266 | 267 | @Override 268 | protected void onPostExecute(Bitmap result) { 269 | ImageView iv = (ImageView) findViewById(R.id.sepia_imageView1); 270 | iv.setImageBitmap(result); 271 | 272 | TextView tv = (TextView) findViewById(R.id.sepia_tv_java); 273 | tv.setText(duration+" ms"); 274 | 275 | } 276 | } 277 | } 278 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/carousel.rs: -------------------------------------------------------------------------------- 1 | /** 2 | * This code is based on the work of daoki2 3 | * http://code.google.com/p/renderscript-examples/ 4 | * carousel.rs 5 | * Copyright (c) 2011 daoki2 6 | */ 7 | 8 | #pragma version(1) 9 | #pragma rs java_package_name(de.inovex.mobi.rsdemos) 10 | 11 | #include "rs_graphics.rsh" 12 | 13 | #define NUM_ITEMS 15 14 | #define RADIUS 1800 15 | 16 | rs_program_vertex gProgVertex; 17 | rs_program_fragment gProgFragmentTexture; 18 | rs_program_store gProgStoreBlendNone; 19 | rs_program_raster gCullNone; 20 | rs_sampler gLinearClamp; 21 | 22 | rs_allocation gTex_00, gTex_01, gTex_02, gTex_03, gTex_04; 23 | rs_allocation gTex_05, gTex_06, gTex_07, gTex_08, gTex_09, gTex_10, gTex_11, gTex_12, gTex_13, gTex_14; 24 | 25 | typedef struct __attribute__((packed, aligned(4))) Bitmaps { 26 | rs_allocation data; 27 | } Bitmaps_t; 28 | Bitmaps_t *bitmap; 29 | 30 | float gPx = 0; 31 | float gPy = 0; 32 | float gVx = 0; 33 | float gVy = 0; 34 | 35 | //float gDt = 0; 36 | 37 | static float vertices[60]; 38 | static float len; 39 | static int rot = 360/NUM_ITEMS/2; // Default angle 40 | static int initialized = 0; 41 | 42 | void init() { 43 | } 44 | 45 | static void displayCarousel() { 46 | // Default vertex shader 47 | rsgBindProgramVertex(gProgVertex); 48 | 49 | // Setup the projection matrix 50 | rs_matrix4x4 proj; 51 | float aspect = (float)rsgGetWidth() / (float)rsgGetHeight(); 52 | rsMatrixLoadPerspective(&proj, 30.0f, aspect, 0.1f, 2500.0f); 53 | rsgProgramVertexLoadProjectionMatrix(&proj); 54 | 55 | // Fragment shader with texture 56 | rsgBindProgramStore(gProgStoreBlendNone); 57 | rsgBindProgramFragment(gProgFragmentTexture); 58 | rsgBindProgramRaster(gCullNone); 59 | rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp); 60 | 61 | // Reduce the rotation speed 62 | if (gVx != 0) { 63 | rot = rot + gVx; 64 | gVx = gVx * 0.995f; 65 | if ( gVx < 0.00001f) { 66 | gVx = 0; 67 | } 68 | } 69 | 70 | // Load vertex matrix as model matrix 71 | rs_matrix4x4 matrix; 72 | rsMatrixLoadTranslate(&matrix, 0.0f, 0.0f, -400.0f); // camera position 73 | rsMatrixRotate(&matrix, rot, 0.0f, 1.0f, 0.0f); // camera rotation 74 | rsgProgramVertexLoadModelMatrix(&matrix); 75 | 76 | // Draw the rectangles 77 | Bitmaps_t *b = bitmap; 78 | for (int i = 0; i < 15; i++) { 79 | rsgBindTexture(gProgFragmentTexture, 0, b->data); 80 | rsgDrawQuadTexCoords( 81 | vertices[i*3], 82 | -(len/2), 83 | vertices[i*3+2], 84 | 0,1, 85 | vertices[i*3], 86 | len/2, 87 | vertices[i*3+2], 88 | 0,0, 89 | vertices[i == 14 ? 0 : (i+1)*3], 90 | len/2, 91 | vertices[i == 14 ? 0 + 2 : (i+1)*3 + 2], 92 | 1,0, 93 | vertices[i == 14 ? 0 : (i+1)*3], 94 | -(len/2), 95 | vertices[i == 14 ? 0 + 2 : (i+1)*3 + 2], 96 | 1,1 97 | ); 98 | b++; 99 | } 100 | } 101 | 102 | static void initBitmaps() { 103 | // Set the bitmap address to the structure 104 | Bitmaps_t *b = bitmap; 105 | b->data = gTex_00; b++; 106 | b->data = gTex_01; b++; 107 | b->data = gTex_02; b++; 108 | b->data = gTex_03; b++; 109 | b->data = gTex_04; b++; 110 | b->data = gTex_05; b++; 111 | b->data = gTex_06; b++; 112 | b->data = gTex_07; b++; 113 | b->data = gTex_08; b++; 114 | b->data = gTex_09; b++; 115 | b->data = gTex_10; b++; 116 | b->data = gTex_11; b++; 117 | b->data = gTex_12; b++; 118 | b->data = gTex_13; b++; 119 | b->data = gTex_14; b++; 120 | 121 | // Calculate the length of the polygon 122 | len = RADIUS * 2 * sin(M_PI/NUM_ITEMS); 123 | 124 | // Calculate the vertices of rectangles 125 | float angle; 126 | for (int i = 0; i < NUM_ITEMS; i++) { 127 | angle = i * 360 / NUM_ITEMS; 128 | vertices[i*3] = sin(angle * M_PI / 180) * RADIUS; 129 | vertices[i*3 + 1] = 0; 130 | vertices[i*3 + 2] = -cos(angle * M_PI / 180) * RADIUS; 131 | } 132 | } 133 | 134 | int root() { 135 | if (initialized == 0) { 136 | initBitmaps(); 137 | initialized = 1; 138 | } 139 | 140 | //gDt = rsGetDt(); 141 | rsgClearColor(0.0f, 0.0f, 0.3333f, 0.0f); 142 | rsgClearDepth(1.0f); 143 | 144 | displayCarousel(); 145 | 146 | return 10; 147 | } 148 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/mandelbrot.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 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 | #pragma version(1) 18 | #pragma rs java_package_name(de.inovex.mobi.rsdemos) 19 | 20 | #define BAILOUT 16 21 | #define MAX_ITERATIONS 1300 22 | 23 | #define SIZE 600 24 | #define HALF_SIZE 300 25 | 26 | rs_allocation gIn; 27 | rs_allocation gOut; 28 | rs_script gScript; 29 | 30 | uint32_t counter = 0; 31 | 32 | 33 | static int calculateDistance(double x, double y) 34 | { 35 | int c = 0; 36 | 37 | double cr = y - 0.5; 38 | double ci = x; 39 | double zi = 0.0; 40 | double zr = 0.0; 41 | int i = 0; 42 | 43 | while(1) { 44 | c++; 45 | i ++; 46 | double temp = zr * zi; 47 | double zr2 = zr * zr; 48 | double zi2 = zi * zi; 49 | zr = zr2 - zi2 + cr; 50 | zi = temp + temp + ci; 51 | if (zi2 + zr2 > BAILOUT) 52 | { 53 | //rsDebug("return for",x,y,c,i); 54 | return i; 55 | } 56 | if (i > MAX_ITERATIONS) 57 | { 58 | //rsDebug("return for",x,y,c,0); 59 | return 0; 60 | } 61 | } 62 | 63 | } 64 | 65 | const static float3 gColorMult = {1.8f, 1.0f, 1.5f}; 66 | 67 | 68 | void root(const void *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) { 69 | counter++; 70 | 71 | int64_t mod = x % 600L; 72 | 73 | double mandel_y = ((int) (-HALF_SIZE + (x / SIZE)) ) / (float)HALF_SIZE; 74 | double mandel_x = (-HALF_SIZE + mod) / (float)HALF_SIZE; 75 | int val = calculateDistance(mandel_x, mandel_y); 76 | float valf = val/255.0; 77 | float3 val3 = {valf, valf, valf}; 78 | float3 col = val3 * gColorMult; 79 | *v_out = rsPackColorTo8888(col); 80 | 81 | } 82 | 83 | void mandelbrot() { 84 | rsForEach(gScript, gIn, gOut, 0); 85 | rsDebug("counter",counter); 86 | } 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/rsgraphicsdemo.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2011 The Android Open Source Project 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma version(1) 16 | 17 | // Tell which java package name the reflected files should belong to 18 | #pragma rs java_package_name(de.inovex.mobi.rsdemos) 19 | 20 | // Built-in header with graphics API's 21 | #include "rs_graphics.rsh" 22 | 23 | // gTouchX and gTouchY are variables that will be reflected for use 24 | // by the java API. We can use them to notify the script of touch events. 25 | int gTouchX; 26 | int gTouchY; 27 | 28 | 29 | // simple mesh to display the logo 30 | rs_mesh gLogoMesh; 31 | 32 | // fragment shader for the texture 33 | rs_program_fragment gProgFragmentTexture; 34 | 35 | 36 | 37 | // This is invoked automatically when the script is created 38 | void init() { 39 | gTouchX = 50.0f; 40 | gTouchY = 50.0f; 41 | } 42 | 43 | int root() { 44 | 45 | // Clear the background color 46 | rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f); 47 | 48 | //texture shader 49 | rsgBindProgramFragment(gProgFragmentTexture); 50 | 51 | // Load vertex matrix as model matrix 52 | rs_matrix4x4 matrix; 53 | rsMatrixLoadIdentity(&matrix); 54 | rsMatrixLoadTranslate(&matrix,gTouchX, gTouchY, 0.0f); // mesh position 55 | 56 | rsgProgramVertexLoadModelMatrix(&matrix); 57 | 58 | 59 | rsgDrawMesh(gLogoMesh); 60 | 61 | // Return value tells RS roughly how often to redraw 62 | // in this case 20 ms 63 | return 20; 64 | } 65 | -------------------------------------------------------------------------------- /src/de/inovex/mobi/rsdemos/sepia.rs: -------------------------------------------------------------------------------- 1 | 2 | #pragma version(1) 3 | #pragma rs java_package_name(de.inovex.mobi.rsdemos) 4 | 5 | // set from the java SDK level 6 | rs_allocation gIn; 7 | rs_allocation gOut; 8 | 9 | rs_script gScript; 10 | 11 | //magic sepia parameters 12 | const float gSepiaDepth = 20/255.0f; 13 | const float gSepiaIntensity = 50/255.0f; 14 | 15 | //magic grayscale factor 16 | const static float3 gMonoMult = {0.299f, 0.587f, 0.114f}; 17 | 18 | 19 | void sepiaFilter() { 20 | rsForEach(gScript, gIn, gOut, 0); // for each element of the input allocation, 21 | // call root() method on gScript 22 | } 23 | 24 | 25 | void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) { 26 | 27 | float4 f4 = rsUnpackColor8888(*v_in); // extract RGBA values, see rs_core.rsh 28 | 29 | float3 mono = dot(f4.rgb, gMonoMult); // dot product of the vector, see rs_cl.rsh 30 | 31 | mono.x = mono.x + (gSepiaDepth*2); // sepia calculations 32 | mono.y = mono.y + gSepiaDepth; 33 | mono.z = mono.z - gSepiaIntensity; 34 | 35 | if(mono.x > 1.0) mono.x = 1.0f; // clipping check 36 | if(mono.y > 1.0) mono.y = 1.0f; 37 | if(mono.z > 1.0) mono.z = 1.0f; 38 | if(mono.z < 0.0) mono.z = 0.0f; 39 | 40 | *v_out = rsPackColorTo8888(mono); // pack color back to uchar4 41 | 42 | } 43 | 44 | 45 | 46 | 47 | --------------------------------------------------------------------------------