OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MaliSamplesView.java
Go to the documentation of this file.
1 /*
2  * This is a modified version of the GL2JNIView.java file distributed by The
3  * Android Open Source Project
4 
5  * Copyright (C) 2008-2009 The Android Open Source Project
6  * Copyright (C) 2012 ARM Limited
7 
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11 
12  * http://www.apache.org/licenses/LICENSE-2.0
13 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.arm.malideveloper.openglessdk;
22 
23 import android.content.Context;
24 import android.opengl.GLSurfaceView;
25 import android.util.Log;
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 
31 public abstract class MaliSamplesView extends GLSurfaceView
32 {
33  public static enum GlesVersion {
34  GLES2(2), GLES3(3);
35 
36  private int value;
37 
38  private GlesVersion(int v){
39  value = v;
40  }
41 
42  public int getValue(){
43  return value;
44  }
45  }
46 
47  protected static String LOG_TAG = "MaliSamplesView";
48  abstract protected void setRendererCallback();
49  abstract protected void destroyContextCallback();
50 
51  protected int redSize = 5;
52  protected int greenSize = 6;
53  protected int blueSize = 5;
54  protected int alphaSize = 0;
55  protected int numberOfSamples = 4;
56  protected int depthSize = 16;
57 
59 
60  public MaliSamplesView(Context context)
61  {
62  this(context,GlesVersion.GLES2);
63  }
64 
65  public MaliSamplesView(Context context, GlesVersion version)
66  {
67  super(context);
68 
69  /* Initialize GLES version member */
70  glesVersion = version;
71 
72  /* Initialize this view */
73  setEGLContextFactory(new TheEGLContextFactory());
74  setEGLConfigChooser(new TheConfigChooser());
75  /* Allow the derived classes to set the renderer */
77  }
78 
79  /*
80  * Return the default value for depth size,
81  * can be overridden by a subclass
82  */
83  protected int getDepthSize()
84  {
85  return depthSize;
86  }
87 
88  /*
89  * Return the default value for number or samples,
90  * can be overridden by a subclass
91  */
92  protected int getNumberOfSamples()
93  {
94  return numberOfSamples;
95  }
96 
97  /*
98  * An implementation of "GLSurfaceView.EGLContextFactory"
99  * for customizing the eglCreateContext and
100  * eglDestroyContext calls.
101  */
102  protected class TheEGLContextFactory implements GLSurfaceView.EGLContextFactory
103  {
104 
105  public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config)
106  {
107  /* From the Khronos definitions */
108  final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
109  int error;
110 
111  while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS)
112  {
113  Log.e(LOG_TAG, String.format("Before TheEGLContextFactory.createContext(): EGL error: 0x%x", error));
114  }
115 
116  /* Use the value of glesVersion */
117  int[] attribs = {EGL_CONTEXT_CLIENT_VERSION, glesVersion.getValue(), EGL10.EGL_NONE };
118 
119  EGLContext context = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, attribs);
120 
121  while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS)
122  {
123  Log.e(LOG_TAG, String.format("After TheEGLContextFactory.createContext(): EGL error: 0x%x", error));
124  }
125 
126  return context;
127  }
128 
129  public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context)
130  {
131  /* Allow the derived classes to destroy their resources*/
133  egl.eglDestroyContext(display, context);
134  }
135  }
136 
137  /*
138  * An implementation of "GLSurfaceView.EGLConfigChooser"
139  * for choosing an EGLConfig configuration from a list of
140  * potential configurations.
141  */
142  protected class TheConfigChooser implements GLSurfaceView.EGLConfigChooser
143  {
144  protected int getConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue)
145  {
146  int[] ret = new int[1];
147 
148  if (egl.eglGetConfigAttrib(display, config, attribute, ret))
149  {
150  return ret[0];
151  }
152 
153  return defaultValue;
154  }
155 
156  public EGLConfig SelectAnEGLConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs)
157  {
158 
159  for(EGLConfig cfg : configs)
160  {
161  int r = getConfigAttrib(egl, display, cfg, EGL10.EGL_RED_SIZE, 0);
162  int g = getConfigAttrib(egl, display, cfg, EGL10.EGL_GREEN_SIZE, 0);
163  int b = getConfigAttrib(egl, display, cfg, EGL10.EGL_BLUE_SIZE, 0);
164  int a = getConfigAttrib(egl, display, cfg, EGL10.EGL_ALPHA_SIZE, 0);
165 
166  if (r == redSize && g == greenSize && b == blueSize && a == alphaSize)
167  {
168  return cfg;
169  }
170  }
171 
172  return null;
173  }
174 
175  public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
176  {
177  /* From the Khronos definitions */
178  final int EGL_OPENGL_ES2_BIT = 4;
179 
180  int[] attribs =
181  {
182  EGL10.EGL_RED_SIZE, redSize,
183  EGL10.EGL_GREEN_SIZE, greenSize,
184  EGL10.EGL_BLUE_SIZE, blueSize,
185  EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
186  EGL10.EGL_SAMPLES, getNumberOfSamples(),
187  EGL10.EGL_DEPTH_SIZE, getDepthSize(),
188  EGL10.EGL_NONE
189  };
190  int[] num_config = new int[1];
191  egl.eglChooseConfig(display, attribs, null, 0, num_config);
192 
193  int numConfigs = num_config[0];
194 
195  if (numConfigs <= 0)
196  {
197  Log.e(LOG_TAG, "No EGL configs were found.");
198  return null;
199  }
200 
201  EGLConfig[] configs = new EGLConfig[numConfigs];
202  egl.eglChooseConfig(display, attribs, configs, numConfigs, num_config);
203 
204  return SelectAnEGLConfig(egl, display, configs);
205  }
206  }
207 }
const GLfloat * v
Definition: gl2ext.h:2231
GLboolean GLboolean GLboolean GLboolean a
Definition: gl2ext.h:306
GLboolean r
Definition: gl2ext.h:306
MaliSamplesView(Context context, GlesVersion version)
GLint value
Definition: gl2ext.h:558
EGLConfig SelectAnEGLConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs)
void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context)
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 config)
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306