OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MultisampledFBO.java
Go to the documentation of this file.
1 /* Copyright (c) 2011-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 package com.arm.malideveloper.openglessdk.multisampledfbo;
22 
23 import javax.microedition.khronos.egl.EGLConfig;
24 import javax.microedition.khronos.opengles.GL10;
25 
26 import android.content.Context;
27 import android.opengl.GLSurfaceView;
28 import android.os.Bundle;
29 import android.util.Log;
30 import android.view.GestureDetector;
31 import android.view.KeyEvent;
32 import android.view.MotionEvent;
33 import android.view.ScaleGestureDetector;
34 import android.view.Window;
35 import android.view.WindowManager;
36 
37 import com.arm.malideveloper.openglessdk.*;
38 
39 class MultisampledFBOView extends MaliSamplesView implements GestureDetector.OnGestureListener, ScaleGestureDetector.OnScaleGestureListener
40 {
41  private static final String TAG = MultisampledFBOView.class.getSimpleName();
42  private boolean initOk = false;
43 
44  private GestureDetector gestureDetector;
45  private ScaleGestureDetector scaleGestureDetector;
46 
47  private float scaleFactor = 1.0f;
48 
49  public MultisampledFBOView(Context context)
50  {
51  /* Ensure we ask for a GLES3 context instead of a GLES2 context. */
52  super(context, GlesVersion.GLES3);
53 
54  gestureDetector = new GestureDetector(context, this);
55  scaleGestureDetector = new ScaleGestureDetector(context, this);
56  }
57 
58  @Override protected void setRendererCallback()
59  {
60  setRenderer(new Renderer());
61  }
62 
63  @Override protected void destroyContextCallback()
64  {
65  MultisampledFBO.uninit();
66  }
67 
68  @Override public boolean onTouchEvent(MotionEvent ev)
69  {
70  this.gestureDetector.onTouchEvent(ev);
71  this.scaleGestureDetector.onTouchEvent(ev);
72  return true;
73  }
74 
75  protected class Renderer implements GLSurfaceView.Renderer
76  {
77  @Override public void onDrawFrame(GL10 gl)
78  {
79  if (initOk) MultisampledFBO.step();
80  }
81 
82  @Override public void onSurfaceChanged(GL10 gl, int width, int height)
83  {
84  initOk = MultisampledFBO.init(width, height);
85  if (initOk)
86  Log.d(TAG, "Initialization complete.");
87  else
88  Log.e(TAG, "Initialization FAILED!");
89  }
90 
91  @Override public void onSurfaceCreated(GL10 gl, EGLConfig config)
92  {
93  }
94  }
95 
96  @Override public boolean onDown(MotionEvent e)
97  {
98  return true;
99  }
100 
101  @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
102  {
103  return true;
104  }
105 
106  @Override public void onLongPress(MotionEvent e)
107  {
108  MultisampledFBO.switchColor();
109  }
110 
111  @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
112  {
113  if (e2.getAction() == MotionEvent.ACTION_MOVE)
114  MultisampledFBO.setDragRotation(distanceX, distanceY);
115  return true;
116  }
117 
118  @Override public void onShowPress(MotionEvent e)
119  {
120  }
121 
122  @Override public boolean onSingleTapUp(MotionEvent e)
123  {
124  MultisampledFBO.toggleAnim();
125  return true;
126  }
127 
128  @Override public boolean onScale(ScaleGestureDetector scaleGestureDetector)
129  {
130  scaleFactor *= scaleGestureDetector.getScaleFactor();
131  scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 2.0f));
132  MultisampledFBO.setScaleFactor(scaleFactor);
133  return true;
134  }
135 
136  @Override public boolean onScaleBegin(ScaleGestureDetector arg0)
137  {
138  return true;
139  }
140 
141  @Override public void onScaleEnd(ScaleGestureDetector arg0)
142  {
143  }
144 }
145 
147 {
148  boolean volUpInLongPress = false;
149  boolean volDownInLongPress = false;
150 
151  MultisampledFBOView mView;
152 
153  public static native boolean init(int width, int height);
154  public static native void step();
155  public static native void toggleAnim();
156  public static native void switchColor();
157  public static native void switchSamples();
158  public static native void switchTextureFormat();
159  public static native void switchTextureSize();
160  public static native void toggleTextureFiltering();
161  public static native void setScaleFactor(float scaleFactor);
162  public static native void setDragRotation(float rotationX, float rotationY);
163  public static native void uninit();
164 
165  @Override protected void onCreate(Bundle savedInstanceState)
166  {
167  super.onCreate(savedInstanceState);
168 
169  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
170  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
171  WindowManager.LayoutParams.FLAG_FULLSCREEN);
172 
173  mView = new MultisampledFBOView(getApplication());
174  setContentView(mView);
175  }
176 
177  @Override protected void onPause()
178  {
179  super.onPause();
180  mView.onPause();
181  }
182 
183  @Override protected void onResume()
184  {
185  super.onResume();
186  mView.onResume();
187  }
188 
189  @Override protected void onDestroy()
190  {
191  super.onDestroy();
192  }
193 
194  @Override public boolean dispatchKeyEvent(KeyEvent event)
195  {
196  int action = event.getAction();
197  int keyCode = event.getKeyCode();
198 
199  switch (keyCode)
200  {
201  case KeyEvent.KEYCODE_VOLUME_UP:
202  if (action == KeyEvent.ACTION_DOWN && event.isLongPress())
203  {
204  volUpInLongPress = true;
205 
206  /* We must ensure these calls occur on the UI thread. */
207  mView.queueEvent(new Runnable()
208  {
209  @Override
210  public void run()
211  {
213  }
214  });
215  }
216 
217  if (action == KeyEvent.ACTION_UP)
218  {
219  if (!volUpInLongPress)
220  {
221  mView.queueEvent(new Runnable()
222  {
223  @Override
224  public void run()
225  {
226  switchSamples();
227  }
228  });
229  }
230  else
231  {
232  volUpInLongPress = false;
233  }
234  }
235  return true;
236 
237  case KeyEvent.KEYCODE_VOLUME_DOWN:
238  if (action == KeyEvent.ACTION_DOWN && event.isLongPress())
239  {
240  volDownInLongPress = true;
241  mView.queueEvent(new Runnable()
242  {
243  @Override
244  public void run()
245  {
247  }
248  });
249  }
250  if (action == KeyEvent.ACTION_UP)
251  {
252  if(!volDownInLongPress)
253  {
254  mView.queueEvent(new Runnable()
255  {
256  @Override
257  public void run()
258  {
260  }
261  });
262  }
263  else
264  {
265  volDownInLongPress = false;
266  }
267  }
268  return true;
269 
270  default:
271  return super.dispatchKeyEvent(event);
272  }
273  }
274 
275  static
276  {
277  // Load the NDK library for this example, built with ndk-build
278  System.loadLibrary("Native");
279  }
280 }
static native void setDragRotation(float rotationX, float rotationY)
Matrix rotationY
static native boolean init(int width, int height)
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
Matrix rotationX
GLint GLsizei width
Definition: gl2ext.h:179