Project

General

Profile

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

examples / src / main / java / org / distorted / examples / plainmonalisa / PlainMonaLisaSurfaceView.java @ 107e4b72

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.plainmonalisa;
21

    
22
import android.content.Context;
23
import android.view.Choreographer;
24
import android.view.SurfaceView;
25
import android.view.SurfaceHolder;
26
import android.util.Log;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Choreographer.FrameCallback
31
  {
32
  private static final String TAG = "MonaLisaSurface";
33
  private RenderThread mRenderThread;
34
  private static volatile boolean mPaused = true;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
  public PlainMonaLisaSurfaceView(Context context)
39
    {
40
    super(context);
41
    getHolder().addCallback(this);
42
    }
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
  @Override
47
  public void surfaceCreated(SurfaceHolder holder)
48
    {
49
    Log.d(TAG, "surfaceCreated holder=" + holder);
50

    
51
    mRenderThread = new RenderThread(holder, this);
52
    mRenderThread.setName("GL render");
53
    mRenderThread.start();
54
    mRenderThread.waitUntilReady();
55

    
56
    RenderHandler rh = mRenderThread.getHandler();
57

    
58
    if (rh != null)
59
      {
60
      rh.sendSurfaceCreated();
61
      }
62

    
63
    Choreographer.getInstance().postFrameCallback(this);
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  @Override
69
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
70
    {
71
    Log.d(TAG, "surfaceChanged fmt=" + format + " size=" + width + "x" + height +" holder=" + holder);
72

    
73
    RenderHandler rh = mRenderThread.getHandler();
74

    
75
    if (rh != null)
76
      {
77
      rh.sendSurfaceChanged(format, width, height);
78
      }
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  @Override
84
  public void surfaceDestroyed(SurfaceHolder holder)
85
    {
86
    Log.d(TAG, "surfaceDestroyed holder=" + holder);
87

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

    
93
    RenderHandler rh = mRenderThread.getHandler();
94

    
95
    if (rh != null)
96
      {
97
      rh.sendShutdown();
98

    
99
      try
100
        {
101
        mRenderThread.join();
102
        }
103
      catch (InterruptedException ie)
104
        {
105
        throw new RuntimeException("join was interrupted", ie);
106
        }
107
      }
108
    mRenderThread = null;
109

    
110
    Log.d(TAG, "surfaceDestroyed complete");
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public void onPause()
116
    {
117
    mPaused = true;
118
    RenderThread.setResourcesCreated();
119

    
120
    Log.d(TAG, "onPause unhooking choreographer");
121
    Choreographer.getInstance().removeFrameCallback(this);
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void onResume()
127
    {
128
    mPaused = false;
129

    
130
    if (mRenderThread != null)
131
      {
132
      Log.d(TAG, "onResume re-hooking choreographer");
133
      Choreographer.getInstance().postFrameCallback(this);
134
      }
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public static boolean isPaused()
140
    {
141
    return mPaused;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  @Override
147
  public void doFrame(long frameTimeNanos)
148
    {
149
    RenderHandler rh = mRenderThread.getHandler();
150

    
151
    if (rh != null)
152
      {
153
      Choreographer.getInstance().postFrameCallback(this);
154
      rh.sendDoFrame(frameTimeNanos);
155
      }
156
    }
157
  }
158

    
159

    
(3-3/5)