B4A Library [Lib] Accelerated surface

Discussion in 'Additional libraries, classes and official updates' started by Informatix, Mar 19, 2013.

Similar threads

Share My Creation Blur Effect Demo
B4A Example MidiSystem Midi Monitor
B4A Question Accelerated surface camera
B4A Tutorial GUI AVD Manager / Emulator
B4A Tutorial Camera2 - Still images and videos
  1. Informatix

    Informatix Expert Licensed User

    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).


    You can use it to make games:


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


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


    The archive includes four benchmarks:


    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.

    Attached Files:

    Last edited: Dec 11, 2015
  2. Informatix

    Informatix Expert Licensed User

    Version 0.94

    New version:
    I added the ability to import the Matrix, Camera, Paint and Path objects from another library (if they are not wrapped).
    I added the functions using these imports: DrawBitmapWithPaint, DrawCircleWithPaint, DrawPath2, DrawTextOnPath2, etc.
    lemonisdead likes this.
  3. sorex

    sorex Expert Licensed User

    not sure if you need a donation just to answer this question? ;)

    but "you get the speed of OpenGL for 2D without the complexity."

    does this mean it's still using GL but with easier command?

    or is it GL less and working on < 3.0 but still faster than the normal bitmap/canvas methods ?
  4. Informatix

    Informatix Expert Licensed User

    With Android 3 or newer version, the drawing surface is a OpenGL2 surface. So behind a method like DrawBitmap, you have OpenGL2 methods. With Android 2, the Canvas is a normal Canvas, not hardware accelerated. That allows to have the same code with all Android versions. However, that doesn't mean the code is slow on a Gingerbread device. On my main phone, I can display about 75 smileys at 60FPS.
    Last edited: Mar 23, 2013
  5. sorex

    sorex Expert Licensed User

    ok, that's clever to go for one piece of code for both OGL supporting and none supporting devices.
  6. Informatix

    Informatix Expert Licensed User

    New version

    New version 0.95:
    I added the support of textures (new class: Texture, and new functions: DrawRectWithTexture, DrawPathWithTexture, etc.);
    I added a timer making direct calls to the Draw event handler (StartRegularDraw, StopRegularDraw, ElapsedTimeSinceLastDraw);
    I added several examples.
  7. airblaster

    airblaster Active Member Licensed User

    Hi Informatix,

    since you are writing this could be a good replace for image views:
    Would it have advantages to use accelerated surface instead of image views in your ULV library?
  8. Informatix

    Informatix Expert Licensed User

    Yes and no. If your ImageViews display static images loaded with LoadBitmap, then you can replace them, but if you use LoadImageAsync, you cannot because this function expects an ImageView as the first parameter. A trick would be to create a dummy ImageView that you pass to LoadImageAsync with a custom location. In the CustomLoadCallback function, you load the image in the AS view, put it in the cache with the Feed...WithBitmap functions and return null so nothing is displayed in the ImageView. This way you benefit from the asynchronous operation and from the cache, but it's cumbersome!
  9. Informatix

    Informatix Expert Licensed User

    New version

    New version 0.96:
    - I use now a hardware layer (it solves issues with some devices);
    - I added the Update event and the StartRegularUpdateAndDraw function;
    - I added three functions: AlterColors, DrawArcWithTexture, and DrawTextWithTexture;
    - I added a game: Space Enemies.

    For animations, the Draw event is sufficient, but for games, it is better to have a separate Update event to compute moves and detect collisions. If you use the new function "StartRegularUpdateAndDraw", a timer is going to trigger regularly the Update event, then the Draw event.
  10. Informatix

    Informatix Expert Licensed User

    New version

    Version 0.961:
    - I fixed a bug with Honeycomb and ICS in Space Enemies (no text displayed);
    - I scaled the score text in Space Enemies;
    - I optimized a little bit the AlterColors function.
  11. jmbm

    jmbm New Member Licensed User

    Hi Informatix,

    I'm trying your great lib.
    I think there is a bug in the Lib. When you try to draw a text and you have used before a Texture, the text is draw textured.
    As reference, you can see it in the water sample, with the FPS text count. There is no color in the text, you see the background texture.

    Excuse my English.

    Thanks and regards.
  12. Informatix

    Informatix Expert Licensed User

    Yes I confirm, but it's not a bug in the lib, it's a bug in the OS. It's fixed in Jelly Bean 4.2.
  13. sterlingy

    sterlingy Active Member Licensed User

    I'm looking into if your lib will fix some issues for me. In my app, I use DrawBitmapRotated, and that seems to be one of the methods that you didn't mimic. However, I could use your MatrixSetRotate, but it doesn't look like it rotates around the Bitmap center point.

    Is there a solution with your lib?


  14. Informatix

    Informatix Expert Licensed User

    You are right, I voluntarily omitted the DrawBitmapRotated function because rotating a bitmap with a matrix is faster and allows to change the pivot point (it can be anywhere, so you have different rotation effects at your disposal).

    The pivot point, at start, is always located in the top left corner. So if you want to rotate your bitmap, you have to:
    - move the pivot point to the center of your bitmap:
    AC.MatrixSetTranslate(-myBmp.width/2, -myBmp.height/2)
    - do the rotation:
    - go back to the top left corner (and draw the bitmap to its final location on screen):
    AC.DrawBitmapWithMatrixAt(myBmp.bmp, myBmp.width/2 + destX, myBmp.height/2 + destY, True)
  15. Informatix

    Informatix Expert Licensed User

    All texts should be separated from the other drawings for two reasons:
    - the bug you noticed and a couple of another bugs (all fixed in Jelly Bean 4.2);
    - they don't need to be refreshed 60 times per second.

    In the Space Enemies example, I use a second layer for the title and I animate it only when needed. I disabled the hardware acceleration in this Accelerated Surface to avoid a bug with TextOnPath in Honeycomb and ICS (and I don't really need the HW acceleration to animate two words, even on a slow device).

    I didn't isolate the score in its own AcceleratedSurface, but maybe I should have. It's not necessary to redraw this score every 16ms. Only when it changes.
  16. padvou

    padvou Active Member Licensed User

    If I want to draw a shape, which consists of n dots (example: 100 dots)?
    each dot has a fixed distance from a center spot and also each dot has a know x and y coordinate value. All distances are in mm
    How would you do that?
  17. Informatix

    Informatix Expert Licensed User

    It depends on what you want to achieve. Is it a closed shape? Is it a static polygon (not moving, not changing)? The most obvious solution is to use a Path (with a DrawPath function), but it's slow.

    You have to use a scale. For example: 1 mm = 0.1%x.
  18. sterlingy

    sterlingy Active Member Licensed User


    I did as you suggested. I've been trying to compare AC against GameView. I did see a speed improvement from about 27FPS with GV to 30FPS with AC.

    BTW, I really like the library, so I donated, even though I may not use it for my current project. I'm sure I will use it in something.

    I have a feeling there are some ways to get better performance. I'm making a tile based game. Each Tile is 240x240px and on my Nexus 7, it can sometimes need to process 30 tiles per tick. That's to rotate and draw them to a canvas. This is just for the background.

    I would like to keep all the sprites in GV, unless you think it would be faster to keep everything in AC, or maybe even keep everything in GV.

    What do you think?


  19. Informatix

    Informatix Expert Licensed User

    Thank you for your donation. I will send you a few things later this day. I'm currently making two new examples for this lib. One of them uses tiles. Maybe that will answer to your question.
  20. padvou

    padvou Active Member Licensed User

    It's a closed shape, like an ellipse.
    Drawpath? I'll look into it..
    Thank you for your reply!
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice