Paug renderscript-mars-2013
-
Upload
paris-android-user-group -
Category
Documents
-
view
623 -
download
0
Transcript of Paug renderscript-mars-2013
![Page 1: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/1.jpg)
RenderscriptGDG Paris Android User GroupMars 2013 - par Sylvain Galand
![Page 2: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/2.jpg)
Renderscript
![Page 3: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/3.jpg)
Objectif : Embellir Android !
![Page 4: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/4.jpg)
Outil pour supporter la diversité
● "Write once, run anywhere"
● Quelque soit le GPU (extensions OpenGL)
● Introduction en interne en 2.x
![Page 5: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/5.jpg)
Evolution : Calcul
● Execution optimale partout
● Important pour le calcul
● Publique depuis 3.x
![Page 6: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/6.jpg)
Et Maintenant ?
CALCULSEULEMENT !
![Page 7: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/7.jpg)
Dépréciation "Render" (Android 4.1)
Deprecation Notice:
Earlier versions of Renderscript included an experimental graphics engine component. This component is now deprecated as of Android 4.1 [...] If you have apps that render graphics with Renderscript, we highly recommend you convert your code to another Android graphics rendering option.
![Page 8: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/8.jpg)
Pourquoi ?
● Les développeurs préfèrent OpenGL
● OpenGL plus utilisé en interne aussi
![Page 9: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/9.jpg)
Renderscript, c'est ...
● Un outil pour les calculs
● Ne remplace ni SDK, ni NDK.
● Problématiques spécifiques ○ calculs○ performances
![Page 10: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/10.jpg)
Les 3 piliers de Renderscript
● Portabilité○ Android 3.0+○ ≈45%
● Performances○ CPU (architectures, coeurs, instructions)○ GPU
● Simplicité d'utilisation○ Génération de "glue code"
![Page 11: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/11.jpg)
MAIS QU'EST CE QUE C'EST ?
● C99 : le langage
● API de calcul
● Outils SDK pour la génération de code
![Page 12: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/12.jpg)
Au coeur de RS
APK Appareil
code.rs bytecodeRenderscript
binaireRS
GPU
CPU #1
CPU #2
![Page 13: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/13.jpg)
script.rs ScriptC_script.java /res/raw/script.bc
typedef struct Struct ScriptField_Struct.java
Struct_t* my_struct; .bind_my_struct(...);
int entier; .set_entier(int i);.get_entier();
void fonction(); .invoke_fonction();
Code généré
![Page 14: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/14.jpg)
Utilisation de Renderscript
![Page 15: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/15.jpg)
Par exemple, un exemple :
● Application d'un filtre sur un Bitmap
● Calcul matriciel
● 1 milliard de $
● Améliorons les temps de calcul
![Page 16: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/16.jpg)
filter.rs - Pragmas - 1/4
#pragma version(1)
#pragma rs java_package_name(com.genymobile.owf)
rs_matrix3x3 filter;
![Page 17: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/17.jpg)
filter.rs - init() - 2/4
void init() {rsMatrixLoadIdentity(&filter);
}
![Page 18: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/18.jpg)
filter.rs - root() - 3/4
void root(const uchar4 *in, uchar4 *out) {
float3 pixel =convert_float4(in[0]).rgb; pixel = rsMatrixMultiply(&filter,pixel);pixel = clamp(pixel, 0.f, 255.f);out->a = in->a;out->rgb = convert_uchar3(pixel);
}
![Page 19: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/19.jpg)
filter.rs - Complet - 4/4#pragma version(1)#pragma rs java_package_name(com.genymobile.owf)rs_matrix3x3 filter;
void init() {rsMatrixLoadIdentity(&filter);
}
void root(const uchar4 *in, uchar4 *out) {float3 pixel = convert_float4(in[0]).rgb; pixel = rsMatrixMultiply(&filter, pixel);pixel = clamp(pixel, 0.f, 255.f);out->a = in->a;out->rgb = convert_uchar3(pixel);
}
![Page 20: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/20.jpg)
Filter.java - Création - 1/7
// Création du scriptmRS = RenderScript.create(mContext);mScript = new ScriptC_filter(mRS,
mContext.getResources(),R.raw.filter);
![Page 21: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/21.jpg)
Filter.java - Allocations - 2/7
// Allocation de la mémoire// contenant le bitmapmInAllocation =
Allocation.createFromBitmap(mRS,inputBitmap,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
![Page 22: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/22.jpg)
Filter.java - Allocations - 3/7
// Allocation de la mémoire pour// récupérer l'image généréemOutAllocation = Allocation.createTyped(
mRS,mInAllocation.getType());
![Page 23: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/23.jpg)
Filter.java - Paramètre - 4/7
// Mise en place des paramètresMatrix3f sepiaMatrix =
new Matrix3f(mMatrix);
mScript.set_filter(sepiaMatrix);
![Page 24: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/24.jpg)
Filter.java - Appel - 5/7
// Appel du script (rsForEach)mScript.forEach_root(
mInAllocation,mOutAllocation);
![Page 25: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/25.jpg)
Filter.java - Résultat - 6/7
// Copie du résultat dans// le bitmap de sortiemOutAllocation.copyTo(outputBitmap);
![Page 26: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/26.jpg)
Filter.java - Complet - 7/7 // Création du scriptmRS = RenderScript.create(mContext);mScript = new ScriptC_filter(mRS, mContext.getResources(), R.raw.filter);
// Allocation de la mémoire contenant le bitmapmInAllocation = Allocation.createFromBitmap(mRS, inputBitmap,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
// Allocation de la mémoire pour récupérer l'image généréemOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());
// Mise en place des paramètresMatrix3f sepiaMatrix = new Matrix3f(mMatrix);mScript.set_filter(sepiaMatrix);
// Appel du script (rsForEach)mScript.forEach_root(mInAllocation, mOutAllocation);
// Copie du résultat dans le bitmap de sortiemOutAllocation.copyTo(outputBitmap);
![Page 27: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/27.jpg)
Démo
![Page 28: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/28.jpg)
QRCode!
![Page 29: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/29.jpg)
Résultats
Appareil (matériel) Java RS GainNexus S(Exynos 1 GHz Cortex A8) 960 ms 280 ms x 3.4
Galaxy Nexus(OMAP 4460 1,2 GHz dual-core) 360 ms 80 ms x 4.5
Samsung Galaxy S2(1.2 GHz dual-core ARM Cortex-A9) 340 ms 48 ms x 7.0
Samsung Galaxy S3(1.4 GHz quad-core ARM Cortex-A9) 325 ms 49 ms x 6.6
Motorola Xoom(Tegra 2: 1GHz dual-core) 210 ms 26 ms x 8.0
Nexus 7(Tegra 3: 1.3 GHz quad-core) 180 ms 22 ms x 8.2
Intel AZ210 - Intel Orange San Diego(Intel Atom 1.6 Ghz Z2460 with HT - x86) 293 ms 59 ms x 4.9
![Page 30: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/30.jpg)
RS en Bref
● Solution pour les calculs
● Intégration simplifiée & portabilitée
● à vous de jouer !
![Page 31: Paug renderscript-mars-2013](https://reader035.fdocuments.net/reader035/viewer/2022062706/557b8e6cd8b42a32618b4e76/html5/thumbnails/31.jpg)
MERCI !
@sylvaingaland
http://slvn.fr/+
Code :http://github.com/sgaland