WritePixel - VERY slow on Android ..

Monkey Targets Forums/Android/WritePixel - VERY slow on Android ..

necky(Posted 2013) [#1]
Hi!
I was wondering if anyone can shed some light on why WritePixel is so slow on the Android.

Here's my test code below. It grabs a 256x256 square in the top left had corner and then draws it back to the screen. At the moment this is running extremely slowly. I'm running on a Samsung Tablet (the latest model) so I was expecting this to be far quicker than it is.

It's (hopefully) something I've done wrong here :)

Thanks
Mike

Strict

Import mojo

Const WIDTH:Int=256
Const HEIGHT:Int=256

Function Main:Int()
New Game()
Return 0
End

Class Game Extends App

Field Xpos:Float
Field Ypos:Float
Field OldXpos:Float
Field OldYpos:Float
Field Screen:Image
Field ScreenPixels:Int[]
Field image:Image

Method OnCreate:Int()
ScreenPixels = New Int[WIDTH*HEIGHT]
SetUpdateRate(60)
Screen = CreateImage(WIDTH,HEIGHT)
Xpos = MouseX()
Ypos = MouseY()
OldXpos = MouseX()
OldYpos = MouseY()
image=LoadImage( "King_Head.png",1,Image.MidHandle )
Return 0
End

Method OnUpdate:Int()
OldXpos = Xpos
OldYpos = Ypos
Xpos = MouseX()
Ypos = MouseY()
Return 0
End

Method OnRender:Int()
'Cls (0,0,0)
DrawImage (Screen,0,0)
'DrawLine OldXpos,OldYpos,Xpos,Ypos
DrawImage (image,MouseX(),MouseY())
ReadPixels ( ScreenPixels, 0, 0, WIDTH,HEIGHT )
Screen.WritePixels(ScreenPixels, 0, 0, WIDTH,HEIGHT)
Return 0
End
End


Gerry Quinn(Posted 2013) [#2]
I don't think you should normally be doing this every frame - I would see WritePixels as more for building composite images that you will then use normally in subsequent frames. You are pushing pixels around between graphic memory and ordinary RAM which may well be slow.

That's just my guess.


necky(Posted 2013) [#3]
Hi Gerry,
Thanks for the advice :) I was wondering if there is another way I could tackle what I'm trying to achieve? What I'd like to do is draw to a screen and for it not to clear, like this example does above but far quicker. Can anyone suggest anything?
All the best
Mike


ziggy(Posted 2013) [#4]
Possible optimizations:
1.- Avoid the whole read/write pixels when there are not changes on screen. Don't do it in a per-frame basis!
2.- Just write/read into the "screen" Image the portions of the device canvas that have been modified, and only when something has been modified.


ElectricBoogaloo(Posted 2013) [#5]
I've found that Android's actually pretty slow at most "Create Image" stuff. Not only is WritePixels slow, but there's a noticable delay between me loading levels in one of my current projects. The only thing happening is a loading of different textures.
Sure it's only a delay of about half a second, but when you compare it to iOS, GLFW and HTML targets, it's odd that Android would be noticably slower at doing it.

I'm currently working under the assumption that it's something to do with how Android's handling it's image memory, since drawing rate seems fairly decent. Something to do with accessing texture memory, or something like that.
Where possible, avoid doing it too often. It's odd to have to cater for quirks like that, but IMO it's all part of the wonderful puzzle that is Coding!


ElectricBoogaloo(Posted 2015) [#6]
Has anybody managed to find a way to speed this up, yet?
The bottleneck is almost certainly down to Android moving the image from system memory to gfx memory.
It'd be nice if I could do fancy artsy stuff in realtime.


Danilo(Posted 2015) [#7]
- Plotting Pixels in an Image
Danilo wrote:
Yes, changing the magic line 118 in mojo.android.java from Bind() to Bind2() made the FPS jump from 3 to 58 FPS! :D



Danilo(Posted 2015) [#8]
@ElectricBoogaloo:
Did it help to speed-up WritePixels stuff? What's your experience?


ElectricBoogaloo(Posted 2015) [#9]
Sorry, got sidetracked by something entirely different.
Will get back to this when I attempt my next project. (About a week or so, I think.. )

Current project : Invisible Munky 2


ElectricBoogaloo(Posted 2015) [#10]
.... Eventually...!

Yes, thank you. Switching to Bind2 has DRASTICALLY improved image loading and editing speeds.
I'm sure there's a reason it's not the default, and that that reason will bite me in the ass.
For now, I am un-ass-bitten, and happy with the result.

.. Now.. How to find "memory available"... Hmm....