31
Renderscript GDG Paris Android User Group Mars 2013 - par Sylvain Galand

Paug renderscript-mars-2013

Embed Size (px)

Citation preview

Page 1: Paug renderscript-mars-2013

RenderscriptGDG Paris Android User GroupMars 2013 - par Sylvain Galand

Page 2: Paug renderscript-mars-2013

Renderscript

Page 3: Paug renderscript-mars-2013

Objectif : Embellir Android !

Page 4: Paug renderscript-mars-2013

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

Evolution : Calcul

● Execution optimale partout

● Important pour le calcul

● Publique depuis 3.x

Page 6: Paug renderscript-mars-2013

Et Maintenant ?

CALCULSEULEMENT !

Page 7: Paug renderscript-mars-2013

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

Pourquoi ?

● Les développeurs préfèrent OpenGL

● OpenGL plus utilisé en interne aussi

Page 9: Paug renderscript-mars-2013

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

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

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

Au coeur de RS

APK Appareil

code.rs bytecodeRenderscript

binaireRS

GPU

CPU #1

CPU #2

Page 13: Paug renderscript-mars-2013

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

Utilisation de Renderscript

Page 15: Paug renderscript-mars-2013

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

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

filter.rs - init() - 2/4

void init() {rsMatrixLoadIdentity(&filter);

}

Page 18: Paug renderscript-mars-2013

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

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

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

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

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

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

Filter.java - Appel - 5/7

// Appel du script (rsForEach)mScript.forEach_root(

mInAllocation,mOutAllocation);

Page 25: Paug renderscript-mars-2013

Filter.java - Résultat - 6/7

// Copie du résultat dans// le bitmap de sortiemOutAllocation.copyTo(outputBitmap);

Page 26: Paug renderscript-mars-2013

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

Démo

Page 28: Paug renderscript-mars-2013

QRCode!

Page 29: Paug renderscript-mars-2013

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

RS en Bref

● Solution pour les calculs

● Intégration simplifiée & portabilitée

● à vous de jouer !

Page 31: Paug renderscript-mars-2013

MERCI !

@sylvaingaland

http://slvn.fr/+

Code :http://github.com/sgaland