Bitmap rendering and Surfaceflinger

603 views
Skip to first unread message

max

unread,
Jun 9, 2010, 11:41:09 AM6/9/10
to android-ndk
Hi android-ndk,

I am writing an app in which I have to render a number of bitmaps very
quickly. I've tried several different methods (most recently drawing
to a canvas, and creating an OpenGL texture then rendering a textured
quad), but these were too slow. I don't need to do any processing on
the bitmap at all, just push it to the screen, so I believe it
possible for the device to render faster if I can figure out how to
make it do so. I've been reading about SurfaceFlinger, and it seems
to be the API that I want to use. However, from this mailing list and
some blog posts I am unsure whether it is possible to use this API
through the NDK in a release app. libsurfaceflinger isn't one of the
NDK's supported API's, so I am wondering if either it's impossible to
use SurfaceFlinger, or if using it will break my app on certain phones
or for certain updates.

Thanks,
Max

Dianne Hackborn

unread,
Jun 9, 2010, 6:47:54 PM6/9/10
to andro...@googlegroups.com
Sorry, SurfaceFlinger is not an API available to applications.  It changes every release.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Onur Cinar

unread,
Jun 9, 2010, 7:44:08 PM6/9/10
to android-ndk
Hi Max,

Yes, for painting bitmaps on the screen OpenGL doesn't perform too
well, so it is better to use the SurfaceView for doing the drawing. In
addition to that, the colorspace of the bitmap also plays an important
role. Based on my tests, RGB565 performed the best.

You can draw to the SurfaceView either through the Java (public API),
or either through the native space (private API).

The Java based drawing is very easy to accomplish, but if you would
like to do the native drawing, please do the following:

- Get the Android source code for (platform/frameworks/base.git) from
android.git.kernel.org.

- Implement the SurfaceHolder.Callback as usual in your Java
application.

- As soon as you have the Surface ready, pass it to the native code
through JNI.

- On the native code, do the following to get the native interface:

jclass surfaceClass = env->FindClass("android/view/Surface");
jfieldID fieldId = env->GetFieldID(surfaceClass, "mSurface", "I");

sp<Surface> surface = env->GetIntField(surface, fieldId);

- At this stage we have the Surface instance, so in order to draw your
bitmap, you can do the following:

SurfaceInfo info;

surface->lock(&info, true);

memcpy(info.bits, bitmapBytes, bitmapSize);

surface->unlockAndPost();

If you are planing to do the drawing on a native thread, please make
sure to attach it to the JVM also.

Again, this is not a part of the public API.

Regards,

-onur





---
www.zdo.com

David Turner

unread,
Jun 9, 2010, 7:51:26 PM6/9/10
to andro...@googlegroups.com
Of course, there is no guarantee that the method described here by Onur will work on future releases of the
platform, since it accesses internal / private framework methods. Prepare for it to break eventually.

Note that if you target froyo, there is a native API (<android/bitmap.h>) in NDK r4 to access the content
of an android.graphics.Bitmap object directly (and a sample that demonstrates this)

Onur Cinar

unread,
Jun 10, 2010, 9:02:45 PM6/10/10
to android-ndk

I'm agree with David. This week I'm experimenting with Android 2.2 on
Nexus One devices, and I noticed that the Surface code is now located
in libsurfaceflinger_client library. So if you decide to use any of
these Private libraries, you will need to maintain different versions
of your application since there is no guarantee that it will continue
working on new releases.

Regards,

-onur



---
www.zdo.com

Pradeep

unread,
Jun 17, 2010, 3:26:54 AM6/17/10
to android-ndk
Hi David,

I have a Video Player like application and had observed OpenGL not
giving good performance on SnapDragon based devices so we had to move
to SurfaceView.

I would like to try the approach mentioned by you. I understand that
it might not work on 2.2 but does it work on other platforms 1.5,
1,6 , 2.0 ?

If it does then I can have two solutions, one for 2.2 using <android/
bitmap.h> and for others the one mentioned by you.

Regards,
Pradeep


On Jun 10, 4:51 am, David Turner <di...@android.com> wrote:
> Of course, there is no guarantee that the method described here by Onur will
> work on future releases of the
> platform, since it accesses internal / private framework methods. Prepare
> for it to break eventually.
>
> Note that if you target froyo, there is a native API (<android/bitmap.h>) in
> NDK r4 to access the content
> of an android.graphics.Bitmap object directly (and a sample that
> demonstrates this)
>
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .

Angus Lees

unread,
Jun 17, 2010, 3:35:27 AM6/17/10
to andro...@googlegroups.com
android/bitmap.h *only* works on 2.2 (and newer).  You will have to do something else for 1.5-2.1.

 - Gus


To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages