Visit Intel® Developer Zone for Android
The Android Native Development Kit (NDK) is a companion tool to the Android SDK and allows you to implement parts of your app using native-code languages such as C and C++.
You can download the NDK toolkit: http://developer.android.com/tools/sdk/ndk/index.html
NDK for X86 Instruction Set Architecture
Android is an open-source operating system developed by Google. Currently, Android can be run on three families of instruction set architectures: ARM, x86, and MIPS. X86 denotes a family of instruction set architectures based on the Intel 8086 CPU, introduced in 1978. Let’s describe the differences between X86 (also called Intel® architecture or IA) and the other chipsets that Android runs on from an application perspective.
Android applications can be classified into two types:
- Dalvik applications that include Java* code and use the official Android SDK API only and necessary resource files, such as .xml and .png, compiled into an APK file.
- Android NDK applications that include Java code and resource files as well as C/C++ source code and sometimes assembly code. All native code is compiled into a dynamic linked library (.so file) and then called by Java in the main program using a JNI mechanism.
Android Game Engine
The game engine is a key module for game applications. There are several engines that run in Android, including 2D and 3D engines which are open source and commercial engines. Thus, it is difficult to migrate and develop Android-based games to run on the IA platform. Cocos2d-x and Unity 3D are the most popular game engines for Android platforms.
Cocos2d-x is based on Cocos2d-iPhone and consists of expanding supported platforms, with multiple programming languages that share the same API structure. Since its introduction in July 2010, cocos2d-x has been downloaded over 500 million times. Giants in the mobile game industry such as Zynga, Glu, GREE, DeNA, Konami, TinyCo, Gamevil, HandyGames, Renren Games, 4399, HappyElements, SDO, and Kingsoft are using cocos2d-x.
Unity 3D is a cross-platform game engine with a built-in IDE developed by Unity Technologies. It is used to develop video games for web plugins, desktop platforms, consoles, and mobile devices, and is utilized by over one million developers. It grew from an OS X supported game development tool in 2005 to a multi-platform game engine. The latest update, Unity 4.1, was released March 2013. It currently supports development for iOS, Android, Windows, Blackberry 10, OS X, Linux, web browsers, Flash*, PlayStation 3, Xbox 360, Windows Phone, and Wii.
Developing Android NDK-based games on IA platforms
Before we talk game development, we should talk about the Android platform in general. As you know, games come in many different styles. Different styles of games need different design principles. At the start of your project, you usually decide the genre of your game. Unless you come up with something completely new and previously unseen, chances are high that your game idea fits into one of the broad genres currently popular. Most genres have established game mechanic standards (e.g., control schemes, specific goals, etc.). Deviating from these standards can make a game a great hit, as gamers always long for something new. Some of the common genres are:
- Arcade & Action
- Brain & Puzzle
- Cards & Casino
- Live Wallpaper
- Sports Games
The process for developing general Android games is similar to any other Android application. First, download the Android SDK and NDK from Google’s web site and install them properly.
I assume you have done all the installation and preparation work. Using Cocos2d-x game engine as the example, let’s see how to create a game for Intel architecture.
Download the latest stable version of Cocos2D-x from the web site: http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download
Execute the batch
Execute the batch from Windows Explorer. When it asks you for the project location, set it to something like com.yourproject.something, and choose the project name and target ID. This will create a folder with the project name inside the cocos2dx installation folder. You should see the execution of a script, without any error, something like this:
Set Environment Variables of NDK_ROOT
Add the following environment variable at the end of the home\<yourname>\.bash_profile file (in this case:c:\cygwin\home\user\.bash_profile):
restart cygwin,input cd $NDK_ROOT, and you should see this screen:
Execute the build_native.sh file
The default configuration is ARM; we need to change it to compile for x86. Open the file \helloworld\proj.android \build_native.sh, find ndk-build command, and add the APP_ABI=x86 parameter to the end of the command. Run it in Cygwin and you will see:
Import project to Eclipse
Now go to Eclipse, create a new project -> Import from existing project.
Build and Run
At this step, Eclipse will have some problems:
The import org.cocos2dx.lib cannot be resolved HelloWorld.java
/HelloWorld/src/com/young40/test line 26 Java Problem Cocos2dxActivity cannot be resolved to a type HelloWorld.java
/HelloWorld/src/com/young40/test line 30 Java Problem Cocos2dxActivity cannot be resolved to a type HelloWorld.java
/HelloWorld/src/com/young40/test line 33 Java Problem
You must import the following library into Eclipse as a project:
Go to Project -> Build, and then Run As -> Android Application:
Then a game framework for the cocos2dx game engine will be built. You can add game logic, audio, picture, etc. resources to this project to make a full game.
Optimize Android NDK-based games on IA platforms
Intel® System Studio is a suite of tool for profiling and optimizing applications on Android platforms. Of course, we can use it for optimizing games. Intel System Studio includes:
- Intel® C++ Compiler
- Intel® Graphics Performance Analyzers
- Intel® VTune™ Amplifier
- (Intel® JTAG Debugger)
Here we won’t explain the details about each tool. Instead, we will walk through an example that shows how Intel tools work.
First, let’s take an application, called Bounding Ball, that we will run on an Intel® Atom™ Z2460 (code name Medfield) processor. The game has more than 800 balls that move at random speed and collide with each other without any regularity. We can see the performance is bad by measuring the FPS, which is only 6 without any optimization.
We can use Intel® Graphics Performance Analyzers (Intel® GPA) to locate which module is the bottleneck and find out if it is CPU bound or GPU bound.
The Intel GPA screen shot below shows a chart that describes the details of this application via the GPA on Android platform . From this, you can see that the CPU consumed 52.5% of the resources. That is a rather high ratio for one application. Meanwhile ISP Load, TA Load, TSP Load and USSE Total Load running inside GPU are all less than 10%, which means that the GPU load is normal. Thus we can conclude the bottleneck is in CPU module. To further analyze the CPU bottleneck issue, we need to profile the code using the VTune™ analyzer.
Here, we don’t describe how to use the VTune analyzer, we just explain the results we obtained when we ran it. The hotspots are the sin and cos functions inside libm.so. So the question is: why does the application spend so much time and CPU cycles to run these two functions?
By checking the application source code, we find that these two hotspot functions are called when every ball is rendered by OpenGL ES*. As geometry of all the balls is the same, only the size is different. We can duplicate balls using the OpenGL function
glScale so that hotspot function can be decreased greatly.
After code optimization, performance is improved 80%; the FPS is 14. Further, we can compile the application with Intel C/C++ Compiler to get better performance on Intel architecture platforms. The Intel C/C++ Compiler has many flags for performance optimization on IA platforms. Here we just introduce some of them.
Supplemental Streaming SIMD Extensions 3 (SSSE3 or SSE3S) is a SIMD instruction set created by Intel and is the fourth iteration of the SSE technology.
Interprocedural Optimization flag will reduce function call overhead, eliminate dead code, and reorder constant propagation and procedure.
Profile-Guided Optimizations flag will analyze leaves many questions open for the optimizer like:
- How often is x > y
- What is the size of count
- Which code is touched and how often
In addition, the Intel C/C++ Compiler can also enhance applications as follows:
- More accurate branch prediction
- Basic block movement to improve instruction cache behavior
- Better decision of functions to inline (help IPO)
- Better optimization of function ordering
- Optimization of switch-statements
- Better vectorization decisions
Using different compilers and different compiling parameters, an app can get different performance. Here is a performance comparison of two compilers GCC and ICC. Same application Bounding Ball is running on android phone based on Intel Medfield. Blue part is performance of GCC and red part is that of ICC. Baseline is to compile without any parameters. The second part of chart is to compile with arch=atom. The third part is to recompile with all parameters mentioned above. Finally, you can see the performance of app compiled by ICC is 60% higher than GCC.
We’ve given you a quick introduction of Android game development and optimization in IA platforms. Game engines are the core part of all game development. If they run well on IA platforms, then the games will run well too. We took the popular game engine, cocos2dx, as an example to demonstrate how to develop on the IA platform. Intel also offers many tools for developers to optimize their game applications on Android platforms. Using Intel System Studio we showed the steps of how to optimize a demo application.
About the Author
Tao Peng is an application engineer in Intel Software and Service Group, and focus on mobile application enabling, including Android applications development and optimization for x86 devices, Web HTML5 application development.
Other Related Articles
To learn more about Intel tools for the Android developer, visit Intel® Developer Zone for Android.