Project

General

Profile

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

examples / src / main / java / org / distorted / examples / plainmonalisa / PlainMonaLisaSurfaceView.java @ 95593730

1
package org.distorted.examples.plainmonalisa;
2

    
3
import android.content.Context;
4
import android.view.Choreographer;
5
import android.view.SurfaceView;
6
import android.view.SurfaceHolder;
7
import android.util.Log;
8

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

    
11
class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Choreographer.FrameCallback
12
  {
13
  private static final String TAG = "MonaLisaSurface";
14
  private RenderThread mRenderThread;
15

    
16
///////////////////////////////////////////////////////////////////////////////////////////////////
17

    
18
  public PlainMonaLisaSurfaceView(Context context)
19
    {
20
    super(context);
21
    getHolder().addCallback(this);
22
    }
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
  public void surfaceCreated(SurfaceHolder holder)
27
    {
28
    Log.d(TAG, "surfaceCreated holder=" + holder);
29

    
30
    mRenderThread = new RenderThread(holder, this);
31
    mRenderThread.setName("GL render");
32
    mRenderThread.start();
33
    mRenderThread.waitUntilReady();
34

    
35
    RenderHandler rh = mRenderThread.getHandler();
36

    
37
    if (rh != null)
38
      {
39
      rh.sendSurfaceCreated();
40
      }
41

    
42
    Choreographer.getInstance().postFrameCallback(this);
43
    }
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
48
    {
49
    Log.d(TAG, "surfaceChanged fmt=" + format + " size=" + width + "x" + height +" holder=" + holder);
50

    
51
    RenderHandler rh = mRenderThread.getHandler();
52

    
53
    if (rh != null)
54
      {
55
      rh.sendSurfaceChanged(format, width, height);
56
      }
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public void surfaceDestroyed(SurfaceHolder holder)
62
    {
63
    Log.d(TAG, "surfaceDestroyed holder=" + holder);
64

    
65
    // We need to wait for the render thread to shut down before continuing because we
66
    // don't want the Surface to disappear out from under it mid-render.  The frame
67
    // notifications will have been stopped back in onPause(), but there might have
68
    // been one in progress.
69

    
70
    RenderHandler rh = mRenderThread.getHandler();
71

    
72
    if (rh != null)
73
      {
74
      rh.sendShutdown();
75

    
76
      try
77
        {
78
        mRenderThread.join();
79
        }
80
      catch (InterruptedException ie)
81
        {
82
        throw new RuntimeException("join was interrupted", ie);
83
        }
84
      }
85
    mRenderThread = null;
86

    
87
    Log.d(TAG, "surfaceDestroyed complete");
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  public void onPause()
93
    {
94
    Log.d(TAG, "onPause unhooking choreographer");
95
    Choreographer.getInstance().removeFrameCallback(this);
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public void onResume()
101
    {
102
    if (mRenderThread != null)
103
      {
104
      Log.d(TAG, "onResume re-hooking choreographer");
105
      Choreographer.getInstance().postFrameCallback(this);
106
      }
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  @Override
112
  public void doFrame(long frameTimeNanos)
113
    {
114
    RenderHandler rh = mRenderThread.getHandler();
115

    
116
    if (rh != null)
117
      {
118
      Choreographer.getInstance().postFrameCallback(this);
119
      rh.sendDoFrame(frameTimeNanos);
120
      }
121
    }
122
  }
123

    
124

    
(3-3/5)