Skip to content

Creating a custom renderer #49

Closed

Description

@processing-bugs

Issue by kiwistrongis
Thursday Mar 13, 2014 at 00:42 GMT
Originally opened as processing/processing-android-archive#64


Moved from processing/processing#2390
Hi there, just working on something that will resolve the problems SMT was having with the latest version of processing - a custom renderer. I have it working fully on the desktop on Windows and Linux ( can't test on mac ). I ran into a problem on Android however.

This works on the desktop:

void setup(){
    size( 800, 600, Renderer.class.getName());
}
void draw(){
    background( 30);
    fill( 200, 100, 100, 200);
    noStroke();
    rect( 10, 10, 100, 100);
}
/*
public int sketchWidth() {
    return 800;
}
public int sketchHeight() {
    return 600;
}
public String sketchRenderer() {
    return Renderer.class.getName();
}
*/
public static class Renderer extends PGraphics3D {
    public Renderer(){
        super();}
}

However this fails on android:

void setup(){
    //size( 800, 600, Renderer.class.getName());
}
void draw(){
    background( 30);
    fill( 200, 100, 100, 200);
    noStroke();
    rect( 10, 10, 100, 100);
}

public int sketchWidth() {
    return 800;
}
public int sketchHeight() {
    return 600;
}
public String sketchRenderer() {
    return Renderer.class.getName();
}

public static class Renderer extends PGraphics3D {
    public Renderer(){
        super();}
}

With the following logcat output:

D/AndroidRuntime(17635): 
D/AndroidRuntime(17635): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime(17635): CheckJNI is OFF
D/AndroidRuntime(17635): Calling main entry com.android.commands.am.Am
D/AndroidRuntime(17635): Shutting down VM
D/AndroidRuntime(17649): 
D/AndroidRuntime(17649): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime(17649): CheckJNI is OFF
D/AndroidRuntime(17649): Calling main entry com.android.commands.pm.Pm
D/AndroidRuntime(17649): Shutting down VM
I/AndroidRuntime(17649): NOTE: attach of thread 'Binder Thread #3' failed
D/AndroidRuntime(17713): 
D/AndroidRuntime(17713): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime(17713): CheckJNI is OFF
D/AndroidRuntime(17713): Calling main entry com.android.commands.am.Am
D/AndroidRuntime(17713): Shutting down VM
I/AndroidRuntime(17713): NOTE: attach of thread 'Binder Thread #3' failed
D/AndroidRuntime(17741): Shutting down VM
E/AndroidRuntime(17741): FATAL EXCEPTION: main
E/AndroidRuntime(17741): java.lang.RuntimeException: Unable to start activity ComponentInfo{processing.test.asdf/processing.test.asdf.asdf}: java.lang.NullPointerException
E/AndroidRuntime(17741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
E/AndroidRuntime(17741):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
E/AndroidRuntime(17741):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
E/AndroidRuntime(17741):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
E/AndroidRuntime(17741):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17741):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(17741):    at android.app.ActivityThread.main(ActivityThread.java:4447)
E/AndroidRuntime(17741):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(17741):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(17741):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(17741):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(17741):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(17741): Caused by: java.lang.NullPointerException
E/AndroidRuntime(17741):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3342)
E/AndroidRuntime(17741):    at android.view.ViewGroup.addView(ViewGroup.java:3214)
E/AndroidRuntime(17741):    at android.view.ViewGroup.addView(ViewGroup.java:3184)
E/AndroidRuntime(17741):    at processing.core.PApplet.onCreate(Unknown Source)
E/AndroidRuntime(17741):    at android.app.Activity.performCreate(Activity.java:4466)
E/AndroidRuntime(17741):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
E/AndroidRuntime(17741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
E/AndroidRuntime(17741):    ... 11 more

Help of any kind would be nice. Advice as to where to look and the like would be great.

Activity

  1. processing-bugs commented on Jul 18, 2014

    @processing-bugs
    Author

    Comment by Calsign
    Thursday Mar 13, 2014 at 00:57 GMT


    Take a look around line 470 of processing.core.PApplet:

    if (sketchRenderer().equals(JAVA2D)) {
      surfaceView = new SketchSurfaceView(this, sw, sh);
    } else if (sketchRenderer().equals(P2D) || sketchRenderer().equals(P3D)) {
      surfaceView = new SketchSurfaceViewGL(this, sw, sh, sketchRenderer().equals(P3D));
    }
    

    Then, a little bit later:

    window.setContentView(surfaceView);
    

    There is simply no code to accommodate for a custom renderer. I don't know what steps would be necessary to get around this... but this should be enough to get you going in the right direction.

  2. processing-bugs commented on Jul 18, 2014

    @processing-bugs
    Author

    Comment by kiwistrongis
    Thursday Mar 13, 2014 at 01:20 GMT


    :S Thanks, I had a feeling this was the case, but didn't know where to look. I'll take a look to see if it can be worked around tomorrow, but I'm not optimistic :(.

  3. self-assigned this
    on Aug 5, 2014
  4. kiwistrongis commented on Aug 8, 2014

    @kiwistrongis
    Contributor

    Hey @codeanticode, I saw you assigned yourself this. I've actually been recently tasked to try to get SMT to work on android. This issue is kinda my first step. Are you working on it? I don't want to step on your toes.

  5. codeanticode commented on Aug 8, 2014

    @codeanticode
    Contributor

    Hi @kiwistrongis, I assigned this issue to myself simply because I was involved in the discussion from the beginning and could give you a hand in figuring out what is needed to add custom renderer support in Android. I'm not planning to implement it myself, so you your pull requests are welcome!

    Anyways, as Calsign pointed out a while ago, the onCreate method only checks if the renderer is either JAVA2D or P2D/P3D, so you need to add some handling code in there. The other thing you need is to provide a subclass of SurfaceView that actually creates the appropriate renderer. If you take a look, the SurfaceView for JAVA2D essentially looks like:

      public class SketchSurfaceView extends SurfaceView
      implements SurfaceHolder.Callback {
        PGraphicsAndroid2D g2;
        SurfaceHolder surfaceHolder;
    
        public SketchSurfaceView(Context context, int wide, int high) {
          super(context);
    
          surfaceHolder = getHolder();
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    
          g2 = new PGraphicsAndroid2D();
          // Set semi-arbitrary size; will be set properly when surfaceChanged() called
          g2.setSize(wide, high);
          g2.setParent(PApplet.this);
          g2.setPrimary(true);
          g = g2;  // assign the g object for the PApplet
    
          setFocusable(true);
          setFocusableInTouchMode(true);
          requestFocus();
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          surfaceChanged = true;
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
          surfaceWindowFocusChanged(hasFocus);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
          return surfaceTouchEvent(event);
        }
    
        @Override
        public boolean onKeyDown(int code, android.view.KeyEvent event) {
          return surfaceKeyDown(code, event);
        }
    
        @Override
        public boolean onKeyUp(int code, android.view.KeyEvent event) {
          return surfaceKeyUp(code, event);
        }
      }

    so it you were to implement your own inside your renderer's library package, should probably be something like

      public class CustomSurfaceView extends SurfaceView
      implements SurfaceHolder.Callback {
        PApplet parent;
        PGraphicsCustom pg;
        SurfaceHolder surfaceHolder;
    
        public CustomSurfaceView(PApplet parent, Context context, int wide, int high) {
          super(context);
    
          surfaceHolder = getHolder();
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    
          pg = new PGraphicsCustom();
          pg.setSize(wide, high);
          pg.setParent(parent);
          pg.setPrimary(true);
          parent.g = pg;  // assign the g object for the PApplet
    
          setFocusable(true);
          setFocusableInTouchMode(true);
          requestFocus();
        }
        // The rest should be the same...
      }

    Hope this helps!

  6. kiwistrongis commented on Aug 8, 2014

    @kiwistrongis
    Contributor

    Cool. So conveniently enough our renderer just extends PGraphics3D and just adds an unusual feature we needed. I was planning on just adapting SurfaceViewGL to work on anything that extends PGraphicsOpenGL.

  7. kiwistrongis commented on Aug 9, 2014

    @kiwistrongis
    Contributor

    So here's what I've got so far: kiwistrongis@95a4c3d
    I was almost done until I decided to rebase onto the lastest master :(. Now I'm getting these errors when I try to run my test sketch:

    BUILD FAILED
    /tmp/android7645066765005575148sketch/build.xml:15: The following error occurred while executing this line:
    /tmp/android7645066765005575148sketch/build.xml:28: Class not found: org.eclipse.jdt.core.JDTCompilerAdapter
    

    I'm going to manually rebase ( or cherry-pick? ) back onto the old version so I can keep working for now, but any idea where the problem is?

  8. kiwistrongis commented on Aug 9, 2014

    @kiwistrongis
    Contributor

    Yup I'm stuck on that now... I figured out the other problem I was having (it was with my test custom renderer), and I can't keep testing until I work around it.

  9. kiwistrongis commented on Aug 9, 2014

    @kiwistrongis
    Contributor

    Ooooookay. So I rebased onto c44b09e cause 899a08a looked suspect, and I can once again compile processing-android and test my sketches. Aaaand its working :). I'll try to figure out exactly which commit caused the above problems later.

    So, this commit: kiwistrongis@d701ecf should add support for custom renderers that extend PGraphicsOpenGL, given that they have the empty constructor. I've been testing with this sketch: kiwistrongis/processing-shell/test_android/test_android.pde.

    Now with SMT I've got some problems with some classes not being loaded, which is weird, but w/e, the custom renderers thing is working.

  10. codeanticode commented on Aug 9, 2014

    @codeanticode
    Contributor

    Hi @kiwistrongis, excellent work! Once you feel your changes are ready please submit a pull request. The commits from @imilka represent ongoing GSOC work aiming to properly support a number of missing features (signed package export, device and sdk selection, etc) which we are still debugging and testing.

  11. kiwistrongis commented on Aug 9, 2014

    @kiwistrongis
    Contributor

    Alrighty. I think I'll comment what I've done, then submit the pull request.

  12. kiwistrongis commented on Aug 11, 2014

    @kiwistrongis
    Contributor

    Submitted: #61
    Let me know if you want me to change the code style to fit in better, or if you want me to walk forward through the commits and solve the other problem I ran into.

  13. kiwistrongis commented on Aug 14, 2014

    @kiwistrongis
    Contributor

    I just found out that I made that silly semicolon mistake in my last commit... sorry :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions