Saturday, October 3, 2009

Using SKIA in Android native code

I was in a tearing hurry to get started on Android's MeadiaPlayer like application. I searched all over the net and I could not find any then I set out, with a MacBook (not Pro ), to figure it out myself.

It took me a while, a week, to figure out SKIA usage. First I started with mediaplayer and got lost in the code and reached a dead end in mediaplayer.cpp, then I set out on WebKit. 


I could find everything I wanted in WebView.cpp. 
Here are the changes I did in SimpleJNI sample to get native rendering. 


First include the SKIA header files in SimpleJNI/jni/native.cpp


#include "GraphicsJNI.h"
#include "SkBlurMaskFilter.h"
#include "SkCanvas.h"
#include "SkCornerPathEffect.h"
#include "SkDumpCanvas.h"
#include "SkPath.h"
#include "SkPicture.h"
#include "SkPixelXorXfermode.h"
#include "SkPaint.h"
#include "SkRect.h"
#include "SkTime.h"


Then the entry into methods[] and draw() method definition 


static JNINativeMethod methods[] = {
{"draw", "(Landroid/graphics/Canvas;Landroid/graphics/Paint;)V",(void*)draw},
};


static jint
draw(JNIEnv *env, jobject thiz, jobject canv, jobject pa) {

SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, canv);
SkPaint* paint = GraphicsJNI::getNativePaint(env,pa);

SkPath path;
SkRect rect;

paint->setStyle(SkPaint::kFill_Style);
paint->setFlags(SkPaint::kAntiAlias_Flag);
paint->setARGB(128, 0, 255, 0);
rect.set(SkIntToScalar(100),SkIntToScalar(75),SkIntToScalar(200),SkIntToScalar(150));
path.addRoundRect(rect, SkIntToScalar(10), SkIntToScalar(10));
canvas->gt;drawPath(path, *paint);

return 1;
}


And here is the java client SimpleJNI.java, I removed “add()” related code and added draw method that takes canvas and paint


@Override
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
Native.draw(canvas,p);
}


finally declare draw method in Native class.


class Native {
static {
// The runtime will add "lib" on the front and ".o" on the end of
// the name supplied to loadLibrary.
System.loadLibrary("simplejni");
}
static native int add(int a, int b);
static native int draw(Canvas can, Paint p);
}

It's not over yet, need to add libraries and include paths to jni/Android.mk file


# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libutils \
libandroid_runtime \
libutils \
libskiagl \
libcorecg \
libsgl
# No static libraries.
LOCAL_STATIC_LIBRARIES :=
# Also need the JNI headers.
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE) \
external/skia/include/core \
external/skia/include/effects \
external/skia/include/images \
external/skia/include/utils \
frameworks/base/core/jni/android/graphics
#


now, save all and hit "make SimpleJNI" in the Android Root and should create SimpleJNI APK.


Some observations ..


I used Android 1.6 source code that supports Native code.


I have reused GraphicsJNI to convert Java objects, Canvas and Paint to native Skcanvas objects.


SimpleJNI does not build if I issue just “make” and “make
APP=SimpleJNI", I added an entry development/sdk-atree. and it started to work, Not sure why?


I always got “Permission denied” in Debug.startMethodTracing  opening /sdcard/SimpleJNI.trace.. (I used correct -sdcard ./imgcd option)


"adb install -r" "-r" option does not overwrite the older version of the application, I need to manually remove


Thanks to my team members, Mahalakshmi (script) and Sham (who followed my idea and provided mk file entries), here are some scripts, first to launch the emulator (I used MacBook, run from Android ROOT  the  path)


./out/host/darwin-x86/bin/emulator -kernel ./prebuilt/android-arm/kernel/kernel-qemu -ramdisk ./out/target/product/generic/ramdisk.img -data ./out/target/product/generic/userdata.img -system ./out/target/product/generic/system.img -init-data ./out/target/product/generic/userdata.img -sysdir ./out/target/product/generic/ -skindir ./development/emulator/skins/HVGA -skin layout -shell


To install SimpleJNI
>adb install -r ./out/target/product/generic/system/app/SimpleJNI.apk
To unistall SimpleJNI
>adb uninstall example.android.simplejni



I just scratched the surface of Android..hope to do more. Guys, leave your comments or any queries on hacks, let me see, if I could hack in.



Saturday, April 5, 2008

After all, today's Numerator is tomorrow's Denominator..

It was roller coaster ride for the stocks in Asia esp. India, India was the worst performer in Asia region. Indian IT stocks already battered by the rising Rupee and the recession fears in the USA, IT was the worst performer of all the sectors When things were looking up a little then came the inflation data and the stocks were back to square one, worst is that the capital goods sector declined the most, putting even the IT stocks into shame. And my portfolio hit a negative territory dragged down by the IT and capital goods.

Well, what’s this background got do with the title? Look at any performance analysis, it’s just the growth over the pervious quarter, half or at best previous year. Analysts do not even want to recollect beyond this duration. A litter of cooking oil used to cost Rs 10 /- 20 years ago but now it costs 120/- over 1200 % increase but does anyone remember, NO, what an economist would say is the cooking oil has risen 5 % over the same period last year.. same is the case with performance of any company. The company might have grown CAGR of 40% in the last couple of years but will it grow CAGR of atleast 15% in this year? if the answer is NO, then sell.
From the analyst’s point of view, “Performance” is all about growth over the previous year, that is, statistically, current year’s data over previous year’s. If the growth of current year is exceptionally good then one can expect, next year’s to go down or at best, after a couple of years because the denominator keeps growing and the numerator in absolute terms may be same but in percentage terms it would be lesser. In other words the growth in absolute terms should grow more to keep up with the same percentage growth. This does not happen practically, for example, though IT industry had grown over 40% CAGR it cannot sustain further, if it have to it should grow over 10 billion USD in absolute terms which practically impossible given the infrastructure and human resource constraints, In sum, the denominator has grown so much that even same absolute growth is going to only show the performance in poor light.
The opposite is what matters to me, in times of slower growth, the rate scenario that today’s smaller Numerator will be tomorrow’s denominator and the analyst can see a bigger growth. And 3 -4 quarters of slower growth rate would be 3-4 quarters of higher growth next year. This is what keeping me invested with the hope that analysts will stick to the same analysis.

Saturday, March 15, 2008

greenNow - I got a brilliant (crazy?) idea



Why not install Vertical Axis Wind Turbines (VAWT) along the motorway sides as shown in the picture? When a vehicle passes by the VAWT, the wind energy generated will drive the VAWT, the electricity generated can be used to light up the street lights! This has to be made mandatory installation!

This is a simple idea when implemented would result in huge benefit by harnessing energy.

Imagine there is one million of kilometers of road and install VAWT every 50 meters, we have 20 million units generating 1 kw per hour, it adds up to 20 million kilowatt per hour production..simple idea huge saving!!

update: Feb 07, 2010, Someone had implemented this idea! feels great

http://www.engadget.com/2010/02/06/eco-shocker-turbine-light-concept-uses-wind-to-light-highways/

Friday, March 14, 2008

Friday, July 20, 2007

Kalam - Modern Day MAHATHMA

Well, should I go on write about?