B4A Library [Lib] Accelerated surface

This library provides a dedicated drawing surface embedded inside a view, which benefits from the hardware acceleration. With it, you get the speed of OpenGL for 2D without the complexity.
It includes many Canvas methods (with anti-aliasing, matrix and camera) and a few useful methods for Bitmaps and Drawables (AlterColors, Crop, LoadNinePatch, ReduceColors, SetDensity, etc.). You can import the Matrix, Camera, Paint and Path objects from another library (if they are not wrapped).

surface.png

imageviews.jpg


You can use it to make games:

princess_tiles.png

space_enemies.png


It includes a tool (TextFactory) to do nice titles (that you can export to a bitmap):

textfactory.jpg


It supports Porter-Duff modes, color filters and texture blending (the processing time is very fast):

pdmodes.jpg


The archive includes four benchmarks:

Perf.png


Because of my lack of (free) time, don't expect answers from me about this library if you're not one of my donors.

Download the latest version (1.12)
To convert a project from v0.9x to v1.x, read this.

Hints & Tips

This library does not work with Android versions < 2 (Eclair and Froyo may exhibit performance problems, so I recommend only Gingerbread or a newer version for animations with a high FPS).
The hardware acceleration is not enabled with Android versions < 3.
 

Attachments

  • Java source - AcceleratedSurface.zip
    21.3 KB · Views: 665
Last edited:

Informatix

Expert
Licensed User
Longtime User
informatix can you help me to make a game pad on your game exemple?
princestale?
i realy like this game i want to make many changes, but i dont know to get a simple movimente

press button1 = hero go to right
press button2 = hero go to left
etc

can you make a simple pls?
The Space Enemies demo uses two buttons (one for the right, one for the left) to move the ship.
 

JordiCP

Expert
Licensed User
Longtime User
Hi Informatix,

I am starting to develop a kind of renderer for some simple graphic objects. For each object, and for convenience, I have the bitmap data in an array, and then draw it onto the final bitmap with the java call.

B4X:
canvas.drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)

This final bitmap is set as the background of a normal View. In my scene, there are some of this objects. I create them sequentially and then draw them using the previous call.

When measuring timings, I see that a great portion is lost in this call, so I wonder if it would help to have hardware acceleration and use AcceleratedSurface.


  • Is it possible (by means of reflection or whatever) with your library, in the OnDraw sub, to call drawBitmap method passing an array as an argument instead of a bitmap? I mean, using the previous java method. That would save me a lot of headaches


Thanks for your time.
 

Informatix

Expert
Licensed User
Longtime User
My library will draw your bitmaps a lot faster than the Canvas library, and even faster than any other library except the ones using directly OpenGL like libGDX.
Unfortunately, it has no function to draw an array, so you'll have to create your bitmap before entering the Draw event (with the ABExtDrawing library for example). Do you have a strong reason to use an array?
 

JordiCP

Expert
Licensed User
Longtime User
Will try it with bitmaps, I'm getting about 25fps with this demo and even if I won't get 60, would like more. I am converting some utils I had from a previous app in order to do a game based on spheres

The only reason is that for my intermediate operations I use allocated arrays and just wanted to "jump" over that step (creating a bitmap) if possible.

Will try with mutableBitmaps and working directly on the bitmap buffers instead of the arrays.


Just another thing

With normal canvas, there is a huge time difference between drawing a bitmap without scaling and doing it with scaling. Does it also happen with hardware accelerated canvas?


Thanks for your support, will let you know if I succeed with it!
 

JordiCP

Expert
Licensed User
Longtime User
I see, but anyway it will be faster using hardware accelerated canvas (let's hope so :))

Thanks again
 

JordiCP

Expert
Licensed User
Longtime User
Just to report, I managed to work with bitmaps instead of arrays and now I can get the speed benefits of your lib

As a curiosity, regarding some comment I read about Android canvas XferModes not always working correctly in all cases, i found (and was turning mad about it o_O) that if I have a transparent pixel but with any of its color components different than zero, for instance ARGB(0,x,y,z), it is drawn anyway depending on the canvas method you use. But if X,y,z are all zero, it is drawn (at least in the cases I tested) correctly.

The only method that, without knowing previously, seems to work ok for me in such situations (not clearing totally the pixels that I want to be transparent) is calling the canvas method with arrays

Even more strange, even if I don't draw them with any kind of canvas but simply assign the bitmap as the background of a Imageview, the system composes it badly


Anyway, having learnt this and thanks to your lib, now I can go on optimizing my code. Thanks a lot.
 

Douglas Farias

Expert
Licensed User
Longtime User
@Informatix
only one question.
why when i run your tile princess example this run 2x

make a world tiles and put a hero on the world 2 times

the game start normal and i move to tile 99 for exemple

if i press backkey now my hero go to tile 1 the game start again o_O

i think is like same when u run 2 activitys

u press 1x back key close the current activity but have another behind this current *-*
 

Informatix

Expert
Licensed User
Longtime User
@Informatix
only one question.
why when i run your tile princess example this run 2x

make a world tiles and put a hero on the world 2 times

the game start normal and i move to tile 99 for exemple

if i press backkey now my hero go to tile 1 the game start again o_O

i think is like same when u run 2 activitys

u press 1x back key close the current activity but have another behind this current *-*
There's no reason for such a behavior and never noticed it on any device or emulator. What did you get in the Logs window? And what's your device?
 

Douglas Farias

Expert
Licensed User
Longtime User
moto g when i go to home i try make log but its strange because dont make any log

only start the main 2 times *-*

u can play normal colect gems give to princess but if you press back key your hero come to tile 0 again.
 

Informatix

Expert
Licensed User
Longtime User
moto g when i go to home i try make log but its strange because dont make any log

only start the main 2 times *-*

u can play normal colect gems give to princess but if you press back key your hero come to tile 0 again.
You should have at least these lines in your log:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
...
How do you start the demo? From the B4A IDE or from the device by clicking on the icon?
 

Douglas Farias

Expert
Licensed User
Longtime User
with the two, i go check the logs on the home and send u thx man
 

Himred

Member
Licensed User
Longtime User
Hello,

Great lib, nice work !

Is it possible to translate/rotate an object (not a bitmap) ?
There is no AC.DrawObjectAtWithMatrix function, I only found AC.DrawObjectAt

Regards,

Himred
 

johndb

Active Member
Licensed User
Longtime User
I'm currently using ABExtDrawing as the engine for vector drawing (i.e circles, lines. ...) and it works ok. I'm considering using this library if there is a significant improvement in speed and if it is capable of creating custom line styles in a similar manner as using the ABextDrawing's ABPaint's function where you can define dash intervals by passing them in an array as per below:
  • SetDashPathEffect (EffectNum1Or2 As Int, intervals() As Float, phase As Float)
    After setting this effect you can use DoPathEffectSingle if one effect is set or DoPathEffectSum, DoPathEffectCompose if both effects are set
It would appear reading the documentation that with this library you can only set the line type to one of the predefined styles and custom line styles as per above is not possible. Is there a way to define custom line styles in this library?

Thanks,

John
 

Informatix

Expert
Licensed User
Longtime User
Is there a way to define custom line styles in this library?
Yes, here's the help text of the function (and you can combine effects with AddToPathEffect):
* Sets a patheffect. This only affects drawing with a style set to STROKE or FILL_AND_STROKE.
* @param Effect One of the PATHEFFECT constants.
* @param Value Value of the effect (for dash effects, it defines the dash size and interval).
Look at the demo; it shows custom styles.
 

johndb

Active Member
Licensed User
Longtime User
Yes, here's the help text of the function (and you can combine effects with AddToPathEffect):
* Sets a patheffect. This only affects drawing with a style set to STROKE or FILL_AND_STROKE.
* @param Effect One of the PATHEFFECT constants.
* @param Value Value of the effect (for dash effects, it defines the dash size and interval).
Look at the demo; it shows custom styles.

Thank you but I played with the examples and I can only display your predefined line types. How would I create a line type such as "DASH DASH dot dot dot"? I'm most likely missing something?
 

Informatix

Expert
Licensed User
Longtime User
Thank you but I played with the examples and I can only display your predefined line types. How would I create a line type such as "DASH DASH dot dot dot"? I'm most likely missing something?
Did you try to combine the effects ? E.g.:
B4X:
ObjRect(2).SetPathEffect(ObjRect(2).PATHEFFECT_DASH_REGULAR, 2dip)
ObjRect(2).AddToPathEffect(ObjRect(2).PATHEFFECT_DASH_SEGMENTED, 10dip, False)
 

johndb

Active Member
Licensed User
Longtime User
Did you try to combine the effects ? E.g.:
B4X:
ObjRect(2).SetPathEffect(ObjRect(2).PATHEFFECT_DASH_REGULAR, 2dip)
ObjRect(2).AddToPathEffect(ObjRect(2).PATHEFFECT_DASH_SEGMENTED, 10dip, False)
Great, it worked! Thank you very much!
 
Top