Android_Gaming_on_Tegra.ppt - NVIDIA Developer

59
Android gaming on Tegra: Android gaming on Tegra: The future of gaming is now, he future of gaming is now, and it’s on the move! and it’s on the move! Lars M. Bishop Senior Engineer, NVIDIA Tegra Developer Technologies

Transcript of Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Page 1: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Android gaming on Tegra: Android gaming on Tegra: The future of gaming is now, The future of gaming is now,

and it’s on the move!and it’s on the move!Lars M. Bishop

Senior Engineer, NVIDIATegra Developer Technologies

Page 2: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Agenda

Top development challenges on AndroidGetting startedDown the development road

“Pure” native game development on AndroidProgramming for:

PerformancePowerOptimal interaction

Mobile “console” gaming

Page 3: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Top Developer Challenges (Getting Started)

Setting up the development environmentLearning the basics of Android app architectureNative C/C++, Java, or both?

Page 4: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Top Developer Challenges (Core Dev / QA)

Android Lifecycle and orientation handlingJava-only Android features and native codeMulti-core performanceFeature/performance tuning across screen sizesInput, sensors, and game controller support

Page 5: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Setting up a Development Environment

NVIDIA’s “TADP” !Tegra Android Development PackSimple installer that downloads/installs:

Android SDK, ADTAndroid NDKNVIDIA Android samples/examples packEclipse IDE

Supports Windows, OSX and Linux

http://developer.nvidia.com/tegra-android-development-pack

Page 6: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Learning the Basics of Android Development

NEW! NVIDIA Android Native Samples PackStandalone download – soon to be in TADPNative and Java codeExplains/demos lifecycle, renderingGame controllers and sensors

NVIDIA Developer resourcesLots of docsYears of Android development presentations

http://developer.nvidia.com/tegra-resources The reference, of course:

http://developer.android.com/index.html

Page 7: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Android App Development

Android apps are JavaGame dev may not need to write any JavaBut it is always there

Can call native-compiled C/C++95% of the code may be native

Java

Native

Application Process

Application “Activity” Class

Native Dynamic Library

JNI

Page 8: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Java to Native and back: JNI

Java Native Interface allows crossoverC/C++ code calling up to JavaJava calling down to native functionsJava/C Type conversionJava-level keywords (“native” method)C/C++ functions and types

Java

Native

Application Process

Application “Activity” Class

Native Dynamic Library

JNI

Page 9: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

“Pure” Native Gaming

The NDK has always allowed “primarily C/C++” games“Pure Native” game support is newer

Doesn’t mean “no Java”, just that the developer isn’t writing anyCan run on the vast majority of active Android devices

Gingerbread (2.3.x)Honeycomb (3.x)Ice Cream Sandwich (4.x)

We’ll focus C/C++ development, but…Some features are still Java-only APIsOr require native extensionsWe’ll explain several methods of accessing these features

Page 10: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

GB, HC and ICS Benefits

GB (2.3.x), SDK9: NativeActivityNative:

EGLLifecycleInputSoundPacked resource loading

HC (3.1), SDK12Game controller support (officially Java-only)

ICS (4.x), SDK14OpenMAX AL video / camera / support in native code

Page 11: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

“Min SDK” and Optional Features

Some features are architecturalI.e. require the app to have them in their min specE.g. NativeActivity

Most can be runtime-selectedLower min-spec for the appMore functionality on newer-OS HWE.g. game controllers

Page 12: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

NativeActivity

Built into the OS image and SDK“portal” between Java-level Android app behavior and native

Exposed as native code callback functionsNeeds/has additional native support APIs:

AssetManagerLooperNativeWindowInputSensorManagerSoundEGL

Page 13: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

NativeActivity: What Does it Do?

Android Lifecycle callbacks -> Native callbacksInput/sensor events -> Native looperNative window instanceAssetManager instanceConvenience properties:

SDK level IDInternal and external storage paths

JNI handles

Page 14: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

NativeActivity: What Doesn’t it Do?

NativeActivity does not:Provide the classic “event loop” modelSingle-thread/synchronize input and other callbacksSupport game controller setupSet up your GLES rendering surfaces/contexts for youAutomatically manage lifecycle (more later…)

Page 15: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Native_app_glue: Filling in the Gaps

Google/NDK-provided source codeNativeActivity’s callback -> queued eventsProvides a classic “event loop” structure

Query a Looper for events, handleas desiredAnd give the app a “main”!

Native_app_glue runs the app’s main function in a secondary thread

Handy, but important to remember…

Page 16: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Native_app_glue: Take Ownership!

native_app_glue is NOT platform codeApache-licensed sample code

Can branch it for each appReview the code and understand itModify/extend itDon’t just WAR issues / missing features

NVIDIA does this…

Page 17: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Nv_native_app_glue

NVIDIA’s version adds:Missing events for NativeActivity callbacksState-tracking convenience functionsHooks to make additional app calls in the main Java thread(s)

Made it easier for us to create lifecycle-friendly appsSimple enum to query the current lifecycle state and focus

Made it easier to call up to JavaSince the main loop is in a native_app_glue-spawned threadAnd JNI calls need to be made from Java threads

Page 18: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Native Apps and Lifecycle: No Silver Bullet

NativeActivity forwards lifecycle eventsto native codeDoes NOT magically handle app lifecycleReview lifecycle docs/samples from Google and NVIDIA:

NVIDIA lifecycle samplesGoogle/NVIDIA lifecycle guidelines docs

http://developer.nvidia.com/category/zone/mobile-development http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle

onCreate()

onStart() onRestart()

onResume()

onPause()

onStop()

onDestroy()

onWindowFocusChanged(T)

onWindowFocusChanged(F)

Page 19: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Quitting your app when you want to

Don’t just “return from main”Java Activity class chooses when to shut downUse NativeActivity’s ANativeActivity_finish

Just a “request”Wait for the shutdown callbacks

onPause()

onStop()

onDestroy()

Page 20: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

NOT Quitting your app when you don’t want to

Take care with button inputDefault BACK == “Close Current Activity”Most 3D games use one ActivitySo, BACK == instant quit!

Implement the UI stack internallyHandle/eat the Android BACK buttonUse it to signal your engine to pause game, step back in UIWhen YOU want to exit, call finish yourself

Page 21: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Functionality not in NativeActivity / NDK

NDK APIs << Android Java APIsTop game developer features that are still Java-only:

In App Billing / PaymentAndroid UIGame controllersDownload ManagerMost camera functionality

Page 22: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Adding Java to NativeActivity

NativeActivity makes it possible to create a game without JavaDoesn’t exclude JavaJava can be focused where neededSome real benefits to adding some Java

NativeCode can invoke Java-only APIsJNI call-upSubclassing NativeActivity

Each method has its place

Page 23: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Querying Functions Directly

For simple function calls, etc in Java, e.g.Static functions / fieldsInstance member functions / fields in Activity / NativeActivity

There may be no need to add Java codeJNI allows query / invocation of

Class object instancesInterfaces

This is done via string class and member name queriesBe careful – check return values to avoid exceptions

Java

C/C++

JNI

Page 24: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Query Example: Android Device Name

Querying a static member of an Android classCaveat Emptor: No error-checking

jclass k = (*env)->FindClass(env, "android/os/Build"); jfieldID DEVICE_ID = (*env)->GetStaticFieldID(env, k, "DEVICE", "Ljava/lang/String;"); jstring DEVICE = (*env)->GetStaticObjectField(env, k, DEVICE_ID); jbyte* str = (*env)->GetStringUTFChars(env, DEVICE, NULL); if (str) LOGI("Android Device Name: %s", str); (*env)->ReleaseStringUTFChars(env, DEVICE, str);

Full app code should check for exception, e.g. usingExceptionOccurredExceptionClear (call before the next JNI function to avoid JVM crash)

Page 25: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Subclassing NativeActivity

Subclassing NativeAcivity is powerfulLeverage all of NativeActivity, add only the code you need

Subclasses of NativeActivity canDeclare and call native functions in your codeImplement/use Java-only APIs/classesDeclare/show Android UI elements/dialogs

AndroidManifest.xml must be changed!Reference your subclass name instead of NativeActivityRemove the tag android:hasCode="false"!

Page 26: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Subclassing Example: In-app Billing

In-App Billing / Payment classes are 100% JavaMultiple componentsClass-to-class interactionsRequires new Java classes for purchasing behaviorPurchase UI can remain in native code

Plan Java features in Java firstThen see if it makes sense to transform to JNI

Page 27: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Example Java: Launch the Browser

public void launchURL(String urlString){ Uri uri = Uri.parse(urlString); Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri); startActivity(launchBrowser); }

public void launchURL2(String urlString){ startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlString))); }

OR (Java haxxor-style):

Page 28: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Example JNI: Launch the Browservoid launchURL(const char* urlString){ jstring urlText = env->NewStringUTF(urlString);

jclass uriClass = env->FindClass("android/net/Uri"); jmethodID uriParse = env->GetStaticMethodID(uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;"); jobject uri = env->CallStaticObjectMethod(uriClass, uriParse, urlText);

jclass intentClass = env->FindClass("android/content/Intent"); jfieldID ACTION_VIEW_id = env->GetStaticFieldID(intentClass, "ACTION_VIEW", "Ljava/lang/String;"); jobject ACTION_VIEW = env->GetStaticObjectField(intentClass, ACTION_VIEW_id);

jmethodID newIntent = env->GetMethodID(intentClass, "<init>", "(Ljava/lang/String;Landroid/net/Uri;)V"); jobject intent = env->AllocObject(intentClass); env->CallVoidMethod(intent, newIntent, ACTION_VIEW, uri);

jclass activityClass = env->FindClass("android/app/Activity"); jmethodID startActivity = env->GetMethodID(activityClass, "startActivity", "(Landroid/content/Intent;)V"); env->CallVoidMethod(thiz, startActivity, intent);}

Page 29: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Be Ready for Change

New Android Oses sometimes change existing behaviorsICS and the ActionBar

HC removed HW home/menu buttonAdded on-screen ActionBar

HC was tablet-only; ActionBar had minimal screen area effectICS supports phones, on-screen ActionBar is bigger

Some apps need to be changed:Most games use their own UI/buttonsUse non-ActionBar themeTheme.Holo.NoActionBar, Theme.DeviceDefault.NoActionBar

http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html

Page 30: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Programming for Perf (Speed AND Power)

Optimizing a modern game is about bothPerformance

GPU: screen size, 3D vision, effects, etcCPU: Multi-core utilization, NEON utilization

PowerSleeping when reasonableAvoiding busy-loopsManaging peripherals

Page 31: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Ready, Fire! Aim… (Profiling)

Profile first!Don’t waste your timeSeems obvious, but we all do it…

Use the tools available:NVIDIA PerfHUD ESPERFOprofile

All of these are available fromhttp://developer.nvidia.com/tegra-resources

Page 32: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Coming Soon…

Tegra ProfilerMaximize multi-core CPU utilizationQuickly identify CPU hot spotsIdentify thread contention issues

Identify call chain “hot spots”Visualize function cost over time

Visualize CPU utilization over time

Capture CPU core utilization, L1/L2 cache counters

Page 33: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Programming for Multi-core

Tegra3 has four CPU coresKeep them busy when you can!

Create at least 4 threadsEspecially if your algorithms are memory-intensiveThis maximizes memory utilization

Create more threads if your algorithm is arithmetically heavy

Page 34: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Programming for NEON

Use NEONBe smart about it

Longer blocks of NEON are betterARMVFP/NEON register transfers hurt peak performanceTransfer:Work ratio is key

GCC can generate NEONVia intrinsics and/or -ftree-vectorizeConsider coding your own!

Use APP_ABI := armeabi-v7a

Page 35: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Programming for Power

Power matters!Multi-threaded can help with powerDon’t

Spin (busyloop)Render more (frequently) than needed

Screen is king for powerDon’t lock it on when not needed

Page 36: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Programming for Interaction

Input and SensorsTouch: ubiquitousAccelerometer / Gyros: extremely popularCompass: available, not heavily usedCameras: new, up-and-coming

Top-level tipsTest, test, test

On as many handsets/tablets as you canUse the lowest rate for a sensor that you can get away withDisable sensors when you can

Page 37: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Accelerometer

“Tilt” and “Tap” controlsAbsolute reference: gravityHigh rate, low noise

Global “up” axis ambiguityCalibration may still be neededSensorEvents are not relative to current screen orientation

Relative to device “ROTATION_0”App must translate to current orientation

9.8 m/s2

Page 38: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Orientation and Sensors

Orientation affects even fixed-orientation games!“Default device orientations” differ

Tablet == LandscapePhone == Portrait

Games often lock to landscape orientationTablet: SensorEvent space == rendering spacePhone: SensorEvent 90° off of rendering space!

Assuming landscape == sensor space?Tablets might workPhones will not

http://developer.nvidia.com/tegra-resources

+X

+Y +Z

-X

-Z -Y

+X

+Y +Z

-X

-Z -Y

Page 39: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Rate Gyros

Measure rotation change In three axes“change”, not “absolute”App must integrate Rate X Time ~= Value

No world-space “ground truth”Suffer from “drift” over time3-axis, unlike accelerometer’s 2.

Gyro error can be noisy and/or correlatedBest in conjunction with absolute sensor(s)I.e. sensor fusion

Page 40: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Sensor Fusion

Using multiple sensors to:Reduce noiseCorrect for driftFull-axis result from partial-axis sensors

Android’s SensorManager can merge compass + accelerometer into a full-axis result

SensorManager.getRotationMatrix Applications can implement the other forms themselves

E.g. compass and/or accelerometer to fix gyro drift

Page 41: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Console Gaming on Tegra

Consumer Tegra tablets/phones can support Console-style gaming today!Involves several Tegra features:

HDMI-outGame controller support3D Vision

Each of these involves some amount of application work

Page 42: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Game Controller Support

HC 3.1 added (Java) game controller supportandroid.view.InputDeviceandroid.view.InputDevice.MotionEventandroid.view.InputDevice.MotionRange

SupportsDevice discoveryButtonsDigital axesAnalog axes

NVIDIA provides detailed documentation on game controllers

Page 43: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Game Controller Tips

Unlike a console, there is no single, standard controller!Axis-mappings varyInclude a game control panel for mapping controllers

Default mappings alone will NOT sufficeSave/pre-fill settings based on device ID

Create/ship “profiles” by device IDAlways allow the user to customize

Make the controller code optionalLowers min-spec.

Page 44: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Game Controller Tricks/Surprises

Your game simply MUST have a mapping control panelButtons == KeyEvents

Except when they aren’t…DPAD could be 4 buttons

Or 2 axesUp/Down upside down?Can’t assume “classic” ABXY layout

DPad or Cross Pad

Analog Sticks

The Middle Section

Buttons

The Shoulder Buttons

Page 45: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

3D Vision

Stereo 3D works on Tegra!Tegra apps can be automatically rendered in 3D stereoApps can detect whether 3D Vision-based stereo is active

Adjusting effects to counter the cost of doubled renderingModifying effects that break the illusion of stereo

3D Vision is automaticBut there are some recommendations…

Page 46: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

3D Vision App Recommendations

Draw UI at 0 depthTell NVIDIA about your engine updates

So 3D Vision profile is always up to date.Post-processing frag shaders should be position-independent

e.g. gl_FragCoord should not be used in stereo mode.Test the separation during gameplay

3D Vision System UI Control PanelRendering should work with any separationTest fullscreen effects at low and high separation

Detect 3D vision activity and scale rendering

Page 47: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Screen Resolution and Feature Tuning

Console-style gaming:720P or 1080PPossibly double-rendered

Keep performance in mindRender high-res, scale back pixel effects Advanced pixel effects, render lower-res

Tune your game your waySimilar to what console developers do today, 1080P-vs-720PConsider resolution and/orrendering effects “sliders”

Page 48: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Summary

Use the resources available:http://developer.nvidia.com !http://developer.android.comhttp://android-developers.blogspot.com

Support and test Android lifecycleEarly and often

Consider “console mode”Controllers3D Vision

Page 49: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Shadowgun by Madfinger Games

Page 50: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Questions?

Page 51: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Backup

Page 52: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Threading in Java or Native

Threads can be launched from Java or Native

Java

Native

JNI

Java Function

Native Function

Java Function

Native PThread

Native Function

Native Function

Call from and return to Java Call from and return to Java

Spawn native thread

Java Thread

Page 53: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Integrating Android UI

NativeActivity-subclass apps can use Android UIDialogs are the easiest case

No layouts to overrideNote: showing a dialog will cause the window to lose focus!

Be ready to handle the lifecycle Other UI is possible

But consider leaving NativeActivity behind…

Page 54: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Compass

One axis: headingReally a “magnetic field” sensor“North” can move around a lot indoorsCompass + accelerometer ~= all-axis global orientationBut rarely used in action games

Page 55: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Cameras as Input Devices

Mobile devices have 2-3 cameras1 user-facing (front)1-2 world-facing (back)

Camera-based input:Gesture controlsAmbient lighting captureAugmented Reality

Tegra supports this via OpenMAX AL

Page 56: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

OpenMAX AL

C/C++ media APIsApplication-levelStandard, extensibleShips in the NDK

>=API14 (Ice Cream Sandwich)Fixed sources, sinks and processorsVideo playback sources, camera(s), etc

Page 57: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Accessing the Camera(s)

OMXAL supports camera sourcesNVIDIA extensions for

Advanced camera parametersOMXAL OpenGLES interopApplication data tapsProtected/premium content support

Page 58: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

EGLStream

A cross-API “image pipeline”NVOMX AL includes Video data EGLStream sinkNative video/camera texture

High-performanceLow-latencyMinimizes copies, roundtrips

Perfect “live camera feed” in AR

Page 59: Android_Gaming_on_Tegra.ppt - NVIDIA Developer

Allows OMXAL video buffers application memoryVisible to the app as callbacks

CPU memory buffersYUV 420 format

Data tap and EGLStream stream resolutions are independentCPU camera tracking is normally low-res

OMXAL Data Taps

Camera/Video Source Data Sink(s)

Data Tap (Extension)

MediaObject