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