Project

General

Profile

Download (3.79 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / plainmonalisa / RenderHandler.java @ 862fcd79

1
package org.distorted.examples.plainmonalisa;
2

    
3
import android.os.Handler;
4
import android.os.Message;
5
import android.util.Log;
6

    
7
import java.lang.ref.WeakReference;
8

    
9
///////////////////////////////////////////////////////////////////////////////////////////////////
10

    
11
/**
12
 * Handler for RenderThread.  Used for messages sent from the UI thread to the render thread.
13
 * <p>
14
 * The object is created on the render thread, and the various "send" methods are called
15
 * from the UI thread.
16
 */
17
public class RenderHandler extends Handler
18
  {
19
  private static final String TAG = "RenderHandler";
20

    
21
  private static final int MSG_SURFACE_CREATED = 0;
22
  private static final int MSG_SURFACE_CHANGED = 1;
23
  private static final int MSG_DO_FRAME = 2;
24
  private static final int MSG_SHUTDOWN = 4;
25

    
26
  // This shouldn't need to be a weak ref, since we'll go away when the Looper quits,
27
  // but no real harm in it.
28
  private WeakReference<RenderThread> mWeakRenderThread;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
  /**
32
   * Call from render thread.
33
   */
34
  public RenderHandler(RenderThread rt)
35
      {
36
      mWeakRenderThread = new WeakReference<>(rt);
37
      }
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
  /**
41
   * Sends the "surface created" message.
42
   * <p>
43
   * Call from UI thread.
44
   */
45
  public void sendSurfaceCreated()
46
    {
47
    sendMessage(obtainMessage(RenderHandler.MSG_SURFACE_CREATED));
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
  /**
52
   * Sends the "surface changed" message, forwarding what we got from the SurfaceHolder.
53
   * <p>
54
   * Call from UI thread.
55
   */
56
  public void sendSurfaceChanged(@SuppressWarnings("unused") int format, int width, int height)
57
    {
58
    sendMessage(obtainMessage(RenderHandler.MSG_SURFACE_CHANGED, width, height));
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
  /**
63
   * Sends the "do frame" message, forwarding the Choreographer event.
64
   * <p>
65
   * Call from UI thread.
66
   */
67
  public void sendDoFrame(long frameTimeNanos)
68
    {
69
    sendMessage(obtainMessage(RenderHandler.MSG_DO_FRAME, (int) (frameTimeNanos >> 32), (int) frameTimeNanos));
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
  /**
74
   * Sends the "shutdown" message, which tells the render thread to halt.
75
   * <p>
76
   * Call from UI thread.
77
   */
78
  public void sendShutdown()
79
      {
80
      sendMessage(obtainMessage(RenderHandler.MSG_SHUTDOWN));
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
  @Override  // runs on RenderThread
85
  public void handleMessage(Message msg)
86
    {
87
    int what = msg.what;
88
    //Log.d(TAG, "RenderHandler [" + this + "]: what=" + what);
89
    RenderThread renderThread = mWeakRenderThread.get();
90

    
91
    if (renderThread == null)
92
      {
93
      Log.w(TAG, "RenderHandler.handleMessage: weak ref is null");
94
      return;
95
      }
96

    
97
    switch (what)
98
      {
99
      case MSG_SURFACE_CREATED: renderThread.surfaceCreated();
100
                                break;
101
      case MSG_SURFACE_CHANGED: renderThread.surfaceChanged(msg.arg1, msg.arg2);
102
                                break;
103
      case MSG_DO_FRAME:        long timestamp = (((long) msg.arg1) << 32) | (((long) msg.arg2) & 0xffffffffL);
104
                                renderThread.doFrame(timestamp);
105
                                break;
106
      case MSG_SHUTDOWN:        renderThread.shutdown();
107
                                break;
108
      default:                  throw new RuntimeException("unknown message " + what);
109
      }
110
    }
111
  }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
(4-4/5)