×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Asha 311 issue with Graphics.copyArea?

    Hello,

    I'm developing a simple game for J2ME. In game loop thread it uses an offscreen bitmap (Image) for the scrolling background, always first moving the current contents of the bitmap a pixel or a few to the left, then drawing newly visible parts to the right side of the bitmap. In the end of the loop, everything is drawn to the screen via Graphics obtained by GameCanvas.getGraphics() - the background bitmap is drawn first, and then other stuff on top of that.

    This works fine on all the emulators that I have, Asha 310, Nokia 808 and Nokia N81. However, I sent the game to a friend who uses Asha 311, and he complained it crashes the whole device so badly that the battery needs to be removed. I checked on on RDA, and was able to narrow the issue to a copyArea call done to the Graphics object of the background bitmap. The following midlet reproduces the issue after running for a while, even without having a Drawable on screen:

    Code:
    (removed)
    Am I doing something wrong, is copyArea not supposed to be used in a loop like this?

    It's pretty easy to change my code so that instead of scrolling the background bitmap, I'll just draw each new vertical line to the right side of the line drawn last time (jumping back to the left side when running out of space) and then draw the background bitmap to the screen in two parts. This is probably more efficient too. However, would be interesting to know if I'm not supposed to do what I'm doing in the code snippet above, and if anyone else has encountered this issue. According to download stats 311 is one of the more popular S40 devices currently.

    Edit: Changed sleep time from 50 ms to 10 ms, to make the crashing more consistent.
    Edit: Removed code, see later in this thread for the updated version.
    Last edited by TongMuan; 2013-07-28 at 15:08.

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

    Re: Asha 311 issue with Graphics.copyArea?

    Without real Java ME experience, I would assume it is valid use of the API - especially as it works well in general. You may have tapped into some device bug, you may even consider reporting it (http://developer.nokia.com/bugs/)
    Have you seen an Error or an Exception on the way? I wonder what copyArea does internally. An over-simplified implementation may use Image.getRGB+Graphics.drawRGB. For example as it needs a buffer, it is prone to GC glitches if there are any. Bringing down the device is an other level of problems however.

  3. #3
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    603

    Re: Asha 311 issue with Graphics.copyArea?

    @TongMuan: i took your code and run it on my Nokia Asha 311 for around 5 min and nothing crashed.
    From my Java ME experience I also dont see any reason this code would cause a crash, Note that there a difference between the real screen graphics and other graphics object so maybe your problem in your game is with the real screen graphics.

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

    Re: Asha 311 issue with Graphics.copyArea?

    It may be worth asking here which firmware version you use. I remember someone mentioned a recent (2-3 weeks ago?) firmware update of the 311.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: Asha 311 issue with Graphics.copyArea?

    Sorry, should have tested the crashing midlet example code on another 311 before posting, it appears to somewhat depend on device or device state (crashing seems to happen faster if the device has just been rebooted, for example). Yesterday it was quickly and consistently crashing the RDA device I tested with, today the same package didn't crash another device at all, at least not withing around a minute that I waited. However, after changing the sleep time to 10 ms, it now crashed on two different devices every time (always within 30 seconds, usually less than 10), I tried on both three times:

    Nokia Asha 311
    355477058348419
    +358504871623
    SW 05.92

    Nokia Asha 311
    355477058350936
    +358504871644
    SW 05.92

    I will edit the code in the original message, and change the sleep time from 50ms to 10ms, in case someone else wants to try this as well.

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,105

    Re: Asha 311 issue with Graphics.copyArea?

    This should not crash anything. However, if you use while(true)/sleep in Java, you are doing something wrong. Although, this is just a rule of dump, let us start from here. Please, try to explain your (thread/game) logic again, as I did not got it, yet.

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: Asha 311 issue with Graphics.copyArea?

    Thanks for the reply.

    The CrashMidlet posted is code to just to reproduce the crash as simply as possible, it's not my game code. In the actual game loop, there's of course an exit condition, instead of while(true). Also, doing some sleeping in each run of the loop in a non-ui game thread is recommended in many sources, to make sure the UI thread can run (in case the J2ME implementation's threading is not implemented in a pre-emptive way).

    Not sure how my actual game loop is relevant, as the issue is clearly in Image's Graphics' copyArea function (because this is the only thing CrashMidlet's thread does, and the midlet crashes every time on Asha 311), but my game loop in a thread in a GameCanvas subclass goes as follows:

    while(stop thread not requested)
    1. Scroll and update the background bitmap, as described in the original post
    2. Move objects
    3. Check hits of objects against background and each other, update their status
    4. Draw the new situation to GameCanvas' Graphics
    5. Flush graphics
    6. Sleep

    Pointer and key events are handled in the main UI thread.

  8. #8
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,105

    Re: Asha 311 issue with Graphics.copyArea?

    Quote Originally Posted by TongMuan View Post
    doing some sleeping in each run of the loop in a non-ui game thread is recommended in many sources, to make sure the UI thread can run
    Bad examples stay bad examples. First of all you yield and not sleep. Second, you do not sleep but create semaphores to block. In Java world you have to find a way to block without doing things repeatedly. In mobile world this kills batteries. You code is executed very, very often and might trash the threading model (because it contains a bug as well).

    Your original code is relevant because I thought I might find a solution for you, to circumvent this crashing code.

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    603

    Re: Asha 311 issue with Graphics.copyArea?

    traud: i actually dont see any problem with his example code that tries to demonstrate a bug he thinks occur on his full game (though the bug doesnt occur when i tested his example code as i've written below)
    I'm an 8+ year veteran in java mobile world and an expert game developer and i can tell you this is the way things are done in games with a endless game loop with sleep (and not yield), also sleep in term of battery consumpion is the same as wait() block in a semaphore/mutex because in both cases the thread isnt active.
    TongMuan: as i've said below i havent found the "bug" in my test on an actual 311 device with the same firmware as yours. i suggest you try to better pin-point the problem.

  10. #10
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

    Re: Asha 311 issue with Graphics.copyArea?

    For the fun part, TongMuan could test the code on the problematic device(s) with having sleep, having yield, having both and perhaps having none. As they are different methods with different semantics, they may also have different side effects on resource management, or on a bug somewhere.
    For the yield vs. sleep part: it is obvious that in "normal" threaded code you use nothing or yield, as you are probably doing some calculations which you want to finish as soon as possible. However an endless game loop will not end anyway, so here you can really play with sleep, which caps the frame rate and lightens the battery load a little.

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: Asha 311 issue with Graphics.copyArea?

    Thanks everybody for all the comments and suggestions!

    Like I said, the original CrashMidlet (in the original post, now removed) with 10 milliseconds sleep was crashing consistenly on several RDA 311 devices. I tried again today and it still did it.

    Just to double check one more time, I sent the same midlet to the friend who experienced the crashing in the original game on his 311. The CrashMidlet didn't crash!!! A total WTF moment . After a few tries of starting, exiting and waiting, the midlet finally did crash the device, but it doesn't seem consistent like on RDA.

    The crashability somehow appears to depend on the phone state, amount of background processes running, device-wide free memory or something. Below is a bit extended code, which has exactly the same image area copying, but within a GameCanvas subclass, along with a frame counter displayed to see when it crashes. Not sure if it's the added activity in the thread, the extra drawing to the screen or extra memory use caused by GameCanvas that makes it crash consistently, but now it does also crash almost immediately every time also on the non-RDA 311.

    Tested it on two devices.

    My friends phone:
    Nokia 311
    V 07.36
    16-05-13
    RM-714

    1st try: crashed after: 548 frames (battery removed afterwards, restart)
    2nd try: crashed after: 186 frames (battery removed afterwards, restart)
    3rd try: crashed after: 19 frames

    RDA
    Nokia 311
    V 05.92
    10-10-12
    RM-714

    1st try: crashed after: 62 frames (Rebooted phone from the menu)
    2nd try: crashed after: 3 frames (Rebooted phone from the menu)
    3rd try: crashed after: 34 frames

    The code:

    Code:
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.game.GameCanvas;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    public class CrashMidlet extends MIDlet {
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    	}
    
    	protected void pauseApp() {
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    		MyCanvas canvas = new MyCanvas();
    		Display.getDisplay(this).setCurrent(canvas);
    		new Thread(canvas).start();
    	}
    	
    	MyCanvas mCanvas;
    	
    	class MyCanvas extends GameCanvas implements Runnable {
    		protected MyCanvas() {
    			super(false);
    		}
    
    		public void run() {
    			Image img = Image.createImage(320, 240);
    			Graphics imgGraphics = img.getGraphics();
    			Graphics myGraphics = getGraphics();
    			int counter = 0;
    			
    			while(true) {
    					imgGraphics.copyArea(1, 0, 319, 240, 0, 0, Graphics.TOP|Graphics.LEFT);
    					
    					myGraphics.setColor(0);
    					myGraphics.fillRect(0, 0, getWidth(), getHeight());
    					myGraphics.setColor(0xffffff);
    					myGraphics.drawString("" + counter , getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.LEFT);
    					
    					flushGraphics();
    					counter++;
    				
    				try {
    					Thread.sleep(10);
    				}
    				catch(Throwable t) {}		
    			}	
    		}
    	};
    }
    I think I'll file an official bug report for this, as wizard_hu_ suggested already in the beginning of the thread, with the above code attached. Probably there won't be a fix for 311 anymore, but at least it'll get to some known issues list, and the bug might not go to the future devices with the same HW/SW (if any). I'll leave testing the yield/sleep stuff to someone else, as I already have a workaround in place (not using copyArea anymore), and I also don't have a 311.

    Btw, about sleep vs yield, I remember someone somewhere recommending sleep(0) instead of yield(). I think the explanation was that, on some devices, yielding would not allow lower priority threads run, but sleeping did.

    shai.i: Sorry for wasting your time on trying the non-crashing CrashMidlet, not only once but twice.

    Edit: Attached CrashMidlet.jar
    CrashMidlet.jar

  12. #12
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    603

    Re: Asha 311 issue with Graphics.copyArea?

    @TongMuan, i can now confirm that your new code does indeed crash the phone to the point of battery needed to be pulled (which is just very bad)
    I hope you file the bug for it to be solved.

  13. #13
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,105

    Re: Asha 311 issue with Graphics.copyArea?

    Quote Originally Posted by TongMuan View Post
    about sleep vs yield
    Nobody said ‘sleep versus yield’. You do yield/wait plus events. Sleep is for stuff when you do not have an event, which is very, very unlikely in Java world.

    Anyway, where did you file the bug?

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: Asha 311 issue with Graphics.copyArea?

    Quote Originally Posted by traud View Post
    Nobody said ‘sleep versus yield’.
    Actually, wizard_hu_ did (although the words were in different order and he used vs. as an abbreviation for versus) - I was replying to him:

    Quote Originally Posted by wizard_hu_ View Post
    For the yield vs. sleep part: it is obvious that in "normal" threaded code you use nothing or yield, as you are probably doing some calculations which you want to finish as soon as possible. However an endless game loop will not end anyway, so here you can really play with sleep, which caps the frame rate and lightens the battery load a little.


    Quote Originally Posted by traud View Post
    Anyway, where did you file the bug?
    Did it just now, in the Developer Nokia Bugs database:
    http://developer.nokia.com/bugs/show_bug.cgi?id=1302

  15. #15
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,105

    Re: Asha 311 issue with Graphics.copyArea?

    Replace nobody with traud, if you like to. Anyway, submitting your bug to ‘our’ bug database was useless because the database is unmonitored since end of 2012. You have to create a support ticket to get noticed.

Similar Threads

  1. LWUIT TextField issue (Asha FullTouch )
    By bogus40 in forum Mobile Java UI and User Experience
    Replies: 6
    Last Post: 2014-03-21, 08:22
  2. Nokia Asha 501 Emulator - jar size issue
    By Giorg189 in forum Mobile Java Tools & SDKs
    Replies: 4
    Last Post: 2013-07-16, 18:13
  3. 6600 graphics issue
    By ralabate in forum General Development Questions
    Replies: 0
    Last Post: 2004-07-22, 21:18
  4. Graphics.copyArea() vs Image.getRGB() / Graphics.drawRGB() ?
    By akokchai in forum Mobile Java General
    Replies: 1
    Last Post: 2004-06-23, 04:47

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •