OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TutorialView.java
Go to the documentation of this file.
1 /* Copyright (c) 2013-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 /* [Includes] */
22 package com.arm.malideveloper.openglessdk.graphicssetup;
23 
24 import android.content.Context;
25 import android.opengl.GLSurfaceView;
26 import javax.microedition.khronos.egl.EGL10;
27 import javax.microedition.khronos.egl.EGLConfig;
28 import javax.microedition.khronos.egl.EGLContext;
29 import javax.microedition.khronos.egl.EGLDisplay;
30 import javax.microedition.khronos.opengles.GL10;
31 /* [Includes] */
32 
33 /* [Member variables] */
34 class TutorialView extends GLSurfaceView
35 {
36  protected int redSize = 8;
37  protected int greenSize = 8;
38  protected int blueSize = 8 ;
39  protected int alphaSize = 8;
40  protected int depthSize = 16;
41  protected int sampleSize = 4;
42  protected int stencilSize = 0;
43  protected int[] value = new int [1];
44 /* [Member variables] */
45  /* [Constructor] */
46  public TutorialView(Context context)
47  {
48  super(context);
49 
50  setEGLContextFactory(new ContextFactory());
51 
52  setEGLConfigChooser(new ConfigChooser());
53 
54  setRenderer(new Renderer());
55  }
56  /* [Constructor] */
57 
58  /* [ContextFactory] */
59  private static class ContextFactory implements GLSurfaceView.EGLContextFactory
60  {
61  public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig)
62  {
63  final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
64 
65  int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
66 
67  EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
68 
69  return context;
70  }
71 
72  public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context)
73  {
74  egl.eglDestroyContext(display, context);
75  }
76  }
77  /* [ContextFactory] */
78  /* [ConfigChooser] */
79  protected class ConfigChooser implements GLSurfaceView.EGLConfigChooser
80  {
81  public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
82  {
83  final int EGL_OPENGL_ES2_BIT = 4;
84  int[] configAttributes =
85  {
86  EGL10.EGL_RED_SIZE, redSize,
87  EGL10.EGL_GREEN_SIZE, greenSize,
88  EGL10.EGL_BLUE_SIZE, blueSize,
89  EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
90  EGL10.EGL_SAMPLES, sampleSize,
91  EGL10.EGL_DEPTH_SIZE, depthSize,
92  EGL10.EGL_STENCIL_SIZE, stencilSize,
93  EGL10.EGL_NONE
94  };
95 
96  int[] num_config = new int[1];
97  egl.eglChooseConfig(display, configAttributes, null, 0, num_config);
98 
99  int numConfigs = num_config[0];
100 
101  EGLConfig[] configs = new EGLConfig[numConfigs];
102  egl.eglChooseConfig(display, configAttributes, configs, numConfigs, num_config);
103 
104  return selectConfig(egl, display, configs);
105  }
106  /* [ConfigChooser] */
107  /* [selectConfig] */
108  public EGLConfig selectConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs)
109  {
110  for(EGLConfig config : configs)
111  {
112  int d = getConfigAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0);
113  int s = getConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0);
114  int r = getConfigAttrib(egl, display, config, EGL10.EGL_RED_SIZE,0);
115  int g = getConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0);
116  int b = getConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0);
117  int a = getConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0);
118 
119  if (r == redSize && g == greenSize && b == blueSize && a == alphaSize && d >= depthSize && s >= stencilSize)
120  return config;
121  }
122 
123  return null;
124  }
125  /* [selectConfig] */
126 
127  /* [getConfigAttrib] */
128  private int getConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config,
129  int attribute, int defaultValue)
130  {
131  if (egl.eglGetConfigAttrib(display, config, attribute, value))
132  return value[0];
133 
134  return defaultValue;
135  }
136  /* [getConfigAttrib] */
137  }
138 
139  /* [Renderer] */
140  private static class Renderer implements GLSurfaceView.Renderer
141  {
142  public void onDrawFrame(GL10 gl)
143  {
144  NativeLibrary.step();
145  }
146 
147  public void onSurfaceChanged(GL10 gl, int width, int height)
148  {
149  NativeLibrary.init(width,height);
150  }
151 
152  public void onSurfaceCreated(GL10 gl, EGLConfig config)
153  {
154 
155  }
156  }
157  /* [Renderer] */
158 }
GLboolean GLboolean GLboolean GLboolean a
Definition: gl2ext.h:306
GLboolean r
Definition: gl2ext.h:306
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
EGLConfig selectConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs)
GLint value
Definition: gl2ext.h:558
int getConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue)
EGLint configAttributes[]
Definition: ThreadSync.h:160
GLboolean GLboolean g
Definition: gl2ext.h:306
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306
GLint GLsizei width
Definition: gl2ext.h:179
EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig)
void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context)