7.5.1 Introduction to Harware Acceleration in Android
The android 2D rendering pipeline supports hardware acceleration. Now you might be wondering what is this pipelining. Well pipelining is actually an implementation technique. Multiple instructions are executed. So how is this useful in android? Well, the answer is we have multiple drawing operations which are performed over a view. With pipelining this multiple executions are possible. Now because of multiple operations, multiple resources are engaged to accomplish these operations. Thus application will create a bottleneck effect on usage of RAM.
By default if we use android API level 14 or API level above 14 then this is automatically enabled. Hardware acceleration is not a very good friend of 2D operations. Although it doesn’t pose any problems but it may affect custom views or some standard view’s performance. But android is smart enough to tackle this sort of problem. We can explicitly enable or disable hardware acceleration. Hardware acceleration can be controlled at following levels:
- Application Level: In application tag <application/>, hardware acceleration can be enabled. At application level the acceleration ability is enabled for entire application.
- Activity level: At activity level, it might be enable or disabled. In other words, if acceleration is turned on globally at application level but you don’t want an activity in particular to be accelerated then it is possible to do that. <activity /> tag is used for this purpose. Don’t worry I will such an example manifest file.
- Window Level: At window level, we can enable or disable hardware acceleration at window level. This has to be coded inside the activity.
- View Level: We can control the hardware acceleration at view level in the runtime i.e. each view can be customized as far as hardware acceleration is concerned. Again it is coded inside activity at view level. View object is used to enable or disable the feature by using flags. We will have a sample activity for demonstration.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.tution.BitmapExample" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:hardwareAccelerated="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android.tution.BitmapExample.MainActivity" android:hardwareAccelerated="false" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Figure - sample manifest file (Application level and Activity level)
This is a sample activity which will provide a demonstration for window and activity level. So whenever you want to do this explicitly you can do it. So refer to the following lines of code:
package com.android.tution.sample; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.WindowManager; public class MainActivity extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = null; // setContentView(new view(this)); setContentView(view); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Figure - Sample activity
7.5.2 Android Drawing Models
Android has two drawing models and they are:
- Software-based model.
- Hardware-based model.
In software model, whenever application is updated it calls the invalidate() method which spread out the message about the redrawing of view in the view hierarchy. But the two major concerns about this model are:
- Drawing model may hide bugs in the application.
- For every pass of draw extensive coding is involved.
In case of hardware acceleration, although the basic logic is same but working strategy is different. The drawing commands are not immediately executed. They are recorded in a display list. Only those views are recorded and updated which are reported by invalidate() method as dirty views. So it is a sort of drawing optimization.
7.5.3 Influential Factors
There are many factors which influence the usage criteria of software or hardware based rendering at view level. Among them the following are the most competent factors:
- Visual effects: Hardware or software layer type is used. Additionally a paint method is applied for visual treatments to a view.
- Compatibility: software layer type is used. The view forcefully rendered in software. It is a very competent option when the hardware pipelining causes trouble in rendering.
- Performance: Hardware layer is used to render view into hardware texture.
While using Android hardware acceleration, following points should be kept in mind:
- Number of views in application should be adequate. They should be minimum or optimal in number.
- Overdrawing should be avoided.
- Render objects should not be created in draw methods.
- Shapes should not be modified often.
- Bitmaps modification should be least or avoided.
- Alpha should be used with care.
Next section would deal with multimedia framework. Hope this tutorial was useful for you. See you in the next section. Till then keep practicing. Happy App Developing!!!