下面列出了怎么用javax.microedition.khronos.egl.EGLDisplay的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display) {
// 要求されている仕様から使用可能な構成の数を抽出します。
final int[] num_config = new int[1];
if (!egl.eglChooseConfig(display, configSpec, null, 0, num_config)) {
throw new IllegalArgumentException("eglChooseConfig failed");
}
final int config_size = num_config[0];
if (config_size <= 0) {
throw new IllegalArgumentException("No configs match configSpec");
}
// 実際の構成を抽出します。
final EGLConfig[] configs = new EGLConfig[config_size];
if (!egl.eglChooseConfig(display, configSpec, configs, config_size, num_config)) {
throw new IllegalArgumentException("eglChooseConfig#2 failed");
}
final EGLConfig config = chooseConfig(egl, display, configs);
if (config == null) {
throw new IllegalArgumentException("No config chosen");
}
return config;
}
private EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display, final EGLConfig[] configs) {
for (final EGLConfig config : configs) {
final int d = findConfigAttrib(egl, display, config, EGL_DEPTH_SIZE, 0);
final int s = findConfigAttrib(egl, display, config, EGL_STENCIL_SIZE, 0);
if ((d >= depthSize) && (s >= stencilSize)) {
final int r = findConfigAttrib(egl, display, config, EGL_RED_SIZE, 0);
final int g = findConfigAttrib(egl, display, config, EGL_GREEN_SIZE, 0);
final int b = findConfigAttrib(egl, display, config, EGL_BLUE_SIZE, 0);
final int a = findConfigAttrib(egl, display, config, EGL_ALPHA_SIZE, 0);
if ((r == redSize) && (g == greenSize) && (b == blueSize) && (a == alphaSize)) {
return config;
}
}
}
return null;
}
private EGLConfig getEglConfig(EGLDisplay eglDisplay, int[] configAttributes) {
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
if (!egl.eglChooseConfig(eglDisplay, configAttributes, configs, configs.length, numConfigs)) {
throw new RuntimeException(
"eglChooseConfig failed: 0x" + Integer.toHexString(egl.eglGetError()));
}
if (numConfigs[0] <= 0) {
throw new RuntimeException("Unable to find any matching EGL config");
}
final EGLConfig eglConfig = configs[0];
if (eglConfig == null) {
throw new RuntimeException("eglChooseConfig returned null");
}
return eglConfig;
}
private EGLContext createEglContext(
EGLContext sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfig) {
if (sharedContext != null && sharedContext == EGL10.EGL_NO_CONTEXT) {
throw new RuntimeException("Invalid sharedContext");
}
int[] contextAttributes = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
EGLContext rootContext = sharedContext == null ? EGL10.EGL_NO_CONTEXT : sharedContext;
final EGLContext eglContext;
synchronized (EglBase.lock) {
eglContext = egl.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes);
}
if (eglContext == EGL10.EGL_NO_CONTEXT) {
throw new RuntimeException(
"Failed to create EGL context: 0x" + Integer.toHexString(egl.eglGetError()));
}
return eglContext;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public android.opengl.EGLSurface createWindowSurface(android.opengl.EGLDisplay display, android.opengl.EGLConfig config, Object nativeWindow) {
int[] surfaceAttribs = {
EGL14.EGL_NONE
};
android.opengl.EGLSurface result = null;
try {
result = EGL14.eglCreateWindowSurface(display, config, nativeWindow, surfaceAttribs, 0);
} catch (IllegalArgumentException e) {
// This exception indicates that the surface flinger surface
// is not valid. This can happen if the surface flinger surface has
// been torn down, but the application has not yet been
// notified via SurfaceHolder.Callback.surfaceDestroyed.
// In theory the application should be notified first,
// but in practice sometimes it is not. See b/4588890
Log.e("DefaultWindow", "eglCreateWindowSurface", e);
}
return result;
}
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
int[] num_config = new int[1];
if (!egl.eglChooseConfig(display, mConfigSpec, null, 0,
num_config)) {
throw new IllegalArgumentException("eglChooseConfig failed");
}
int numConfigs = num_config[0];
if (numConfigs <= 0) {
throw new IllegalArgumentException(
"No configs match configSpec");
}
EGLConfig[] configs = new EGLConfig[numConfigs];
if (!egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs,
num_config)) {
throw new IllegalArgumentException("eglChooseConfig#2 failed");
}
EGLConfig config = chooseConfig(egl, display, configs);
if (config == null) {
throw new IllegalArgumentException("No config chosen");
}
return config;
}
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
EGLConfig[] configs) {
for (EGLConfig config : configs) {
int d = findConfigAttrib(egl, display, config,
EGL10.EGL_DEPTH_SIZE, 0);
int s = findConfigAttrib(egl, display, config,
EGL10.EGL_STENCIL_SIZE, 0);
if ((d >= mDepthSize) && (s >= mStencilSize)) {
int r = findConfigAttrib(egl, display, config,
EGL10.EGL_RED_SIZE, 0);
int g = findConfigAttrib(egl, display, config,
EGL10.EGL_GREEN_SIZE, 0);
int b = findConfigAttrib(egl, display, config,
EGL10.EGL_BLUE_SIZE, 0);
int a = findConfigAttrib(egl, display, config,
EGL10.EGL_ALPHA_SIZE, 0);
if ((r == mRedSize) && (g == mGreenSize)
&& (b == mBlueSize) && (a == mAlphaSize)) {
return config;
}
}
}
return null;
}
@Override
public EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display) {
final int[] num_config = new int[1];
if (!egl.eglChooseConfig(display, configSpec, null, 0, num_config)) {
throw new IllegalArgumentException("eglChooseConfig failed");
}
final int config_size = num_config[0];
if (config_size <= 0) {
throw new IllegalArgumentException("No configs match configSpec");
}
final EGLConfig[] configs = new EGLConfig[config_size];
if (!egl.eglChooseConfig(display, configSpec, configs, config_size, num_config)) {
throw new IllegalArgumentException("eglChooseConfig#2 failed");
}
final EGLConfig config = chooseConfig(egl, display, configs);
if (config == null) {
throw new IllegalArgumentException("No config chosen");
}
return config;
}
private EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display, final EGLConfig[] configs) {
for (final EGLConfig config : configs) {
final int d = findConfigAttrib(egl, display, config, EGL_DEPTH_SIZE, 0);
final int s = findConfigAttrib(egl, display, config, EGL_STENCIL_SIZE, 0);
if ((d >= depthSize) && (s >= stencilSize)) {
final int r = findConfigAttrib(egl, display, config, EGL_RED_SIZE, 0);
final int g = findConfigAttrib(egl, display, config, EGL_GREEN_SIZE, 0);
final int b = findConfigAttrib(egl, display, config, EGL_BLUE_SIZE, 0);
final int a = findConfigAttrib(egl, display, config, EGL_ALPHA_SIZE, 0);
if ((r == redSize) && (g == greenSize) && (b == blueSize) && (a == alphaSize)) {
return config;
}
}
}
return null;
}
private static EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
{
int[] numConfig = new int[1];
if (!egl.eglChooseConfig(display, CONFIG_SPEC, null, 0, numConfig))
{
throw new IllegalArgumentException("eglChooseConfig failed");
}
int numConfigs = numConfig[0];
if (numConfigs <= 0)
{
throw new IllegalArgumentException("No configs match configSpec");
}
EGLConfig[] configs = new EGLConfig[numConfigs];
if (!egl.eglChooseConfig(
display, CONFIG_SPEC, configs, numConfigs, numConfig))
{
throw new IllegalArgumentException("eglChooseConfig#2 failed");
}
return configs[0];
}
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
EGLConfig config, Object nativeWindow) {
EGLSurface result = null;
try {
result = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
} catch (IllegalArgumentException e) {
// This exception indicates that the surface flinger surface
// is not valid. This can happen if the surface flinger surface has
// been torn down, but the application has not yet been
// notified via SurfaceHolder.Callback.surfaceDestroyed.
// In theory the application should be notified first,
// but in practice sometimes it is not. See b/4588890
Log.e(TAG, "eglCreateWindowSurface", e);
}
return result;
}
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
EGLConfig[] configs) {
for (EGLConfig config : configs) {
int d = findConfigAttrib(egl, display, config,
EGL10.EGL_DEPTH_SIZE, 0);
int s = findConfigAttrib(egl, display, config,
EGL10.EGL_STENCIL_SIZE, 0);
if ((d >= mDepthSize) && (s >= mStencilSize)) {
int r = findConfigAttrib(egl, display, config,
EGL10.EGL_RED_SIZE, 0);
int g = findConfigAttrib(egl, display, config,
EGL10.EGL_GREEN_SIZE, 0);
int b = findConfigAttrib(egl, display, config,
EGL10.EGL_BLUE_SIZE, 0);
int a = findConfigAttrib(egl, display, config,
EGL10.EGL_ALPHA_SIZE, 0);
if ((r == mRedSize) && (g == mGreenSize)
&& (b == mBlueSize) && (a == mAlphaSize)) {
return config;
}
}
}
return null;
}
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
EGLConfig config, Object nativeWindow) {
EGLSurface result = null;
try {
result = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
} catch (IllegalArgumentException e) {
// This exception indicates that the surface flinger surface
// is not valid. This can happen if the surface flinger surface has
// been torn down, but the application has not yet been
// notified via SurfaceHolder.Callback.surfaceDestroyed.
// In theory the application should be notified first,
// but in practice sometimes it is not. See b/4588890
Log.e(TAG, "eglCreateWindowSurface", e);
}
return result;
}
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
EGLConfig[] configs) {
for (EGLConfig config : configs) {
int d = findConfigAttrib(egl, display, config,
EGL10.EGL_DEPTH_SIZE, 0);
int s = findConfigAttrib(egl, display, config,
EGL10.EGL_STENCIL_SIZE, 0);
if ((d >= mDepthSize) && (s >= mStencilSize)) {
int r = findConfigAttrib(egl, display, config,
EGL10.EGL_RED_SIZE, 0);
int g = findConfigAttrib(egl, display, config,
EGL10.EGL_GREEN_SIZE, 0);
int b = findConfigAttrib(egl, display, config,
EGL10.EGL_BLUE_SIZE, 0);
int a = findConfigAttrib(egl, display, config,
EGL10.EGL_ALPHA_SIZE, 0);
if ((r == mRedSize) && (g == mGreenSize)
&& (b == mBlueSize) && (a == mAlphaSize)) {
return config;
}
}
}
return null;
}
@Override
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
EGLConfig config, Object nativeWindow) {
int[] surfaceAttribs = {
EGL10.EGL_NONE
};
EGLSurface result = null;
try {
result = egl.eglCreateWindowSurface(display, config, nativeWindow, surfaceAttribs);
} catch (IllegalArgumentException e) {
// This exception indicates that the surface flinger surface
// is not valid. This can happen if the surface flinger surface has
// been torn down, but the application has not yet been
// notified via SurfaceHolder.Callback.surfaceDestroyed.
// In theory the application should be notified first,
// but in practice sometimes it is not. See b/4588890
Log.e("DefaultWindow", "eglCreateWindowSurface", e);
}
return result;
}
@Override
public EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display) {
// 要求されている仕様から使用可能な構成の数を抽出します。
final int[] num_config = new int[1];
if (!egl.eglChooseConfig(display, configSpec, null, 0, num_config)) {
throw new IllegalArgumentException("eglChooseConfig failed");
}
final int config_size = num_config[0];
if (config_size <= 0) {
throw new IllegalArgumentException("No configs match configSpec");
}
// 実際の構成を抽出します。
final EGLConfig[] configs = new EGLConfig[config_size];
if (!egl.eglChooseConfig(display, configSpec, configs, config_size, num_config)) {
throw new IllegalArgumentException("eglChooseConfig#2 failed");
}
final EGLConfig config = chooseConfig(egl, display, configs);
if (config == null) {
throw new IllegalArgumentException("No config chosen");
}
return config;
}
private EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display, final EGLConfig[] configs) {
for (final EGLConfig config : configs) {
final int d = findConfigAttrib(egl, display, config, EGL_DEPTH_SIZE, 0);
final int s = findConfigAttrib(egl, display, config, EGL_STENCIL_SIZE, 0);
if ((d >= depthSize) && (s >= stencilSize)) {
final int r = findConfigAttrib(egl, display, config, EGL_RED_SIZE, 0);
final int g = findConfigAttrib(egl, display, config, EGL_GREEN_SIZE, 0);
final int b = findConfigAttrib(egl, display, config, EGL_BLUE_SIZE, 0);
final int a = findConfigAttrib(egl, display, config, EGL_ALPHA_SIZE, 0);
if ((r == redSize) && (g == greenSize) && (b == blueSize) && (a == alphaSize)) {
return config;
}
}
}
return null;
}
public boolean eglChooseConfig(EGLDisplay display, int[] attrib_list,
EGLConfig[] configs, int config_size, int[] num_config) {
begin("eglChooseConfig");
arg("display", display);
arg("attrib_list", attrib_list);
arg("config_size", config_size);
end();
boolean result = mEgl10.eglChooseConfig(display, attrib_list, configs,
config_size, num_config);
arg("configs", configs);
arg("num_config", num_config);
returns(result);
checkError();
return result;
}
public static int getMaxTextureSize() {
final int MAX_ALLOWED_TEXTURE_SIZE = 2048;
EGL10 egl = (EGL10) EGLContext.getEGL();
EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
egl.eglInitialize(display, version);
int[] totalConfigurations = new int[1];
egl.eglGetConfigs(display, null, 0, totalConfigurations);
EGLConfig[] configurationsList = new EGLConfig[totalConfigurations[0]];
egl.eglGetConfigs(display, configurationsList, totalConfigurations[0], totalConfigurations);
int[] textureSize = new int[1];
int maximumTextureSize = 0;
for (int i = 0; i < totalConfigurations[0]; i++) {
egl.eglGetConfigAttrib(display, configurationsList[i], EGL10.EGL_MAX_PBUFFER_WIDTH, textureSize);
if (maximumTextureSize < textureSize[0])
maximumTextureSize = textureSize[0];
}
egl.eglTerminate(display);
return Math.min(maximumTextureSize, MAX_ALLOWED_TEXTURE_SIZE);
}
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
EGLConfig config, Object nativeWindow) {
EGLSurface result = null;
d = display;
c = config;
try {
mEGLPreviewSurface = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
} catch (IllegalArgumentException e) {
Log.e(TAG, "eglCreateWindowSurface (native)", e);
}
// this return will trigger Renderer
result = mEGLPreviewSurface;
return result;
}
public VIASurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
setEGLContextClientVersion(2);
mRenderer = new AdavanceRenderer(context);
if(true) {
setEGLConfigChooser(new EGLConfigChooser() {
@Override
public EGLConfig chooseConfig(EGL10 egl10, EGLDisplay eglDisplay) {
final int[] attribList = {EGL14.EGL_RED_SIZE, 8, //
EGL14.EGL_GREEN_SIZE, 8, //
EGL14.EGL_BLUE_SIZE, 8, //
EGL14.EGL_ALPHA_SIZE, 8, //
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, //
EGL14.EGL_NONE, 0, //
EGL14.EGL_NONE};
attribList[attribList.length - 3] = EGL_RECORDABLE_ANDROID;
attribList[attribList.length - 2] = 1;
int[] configNum = new int[1];
EGLConfig config = null;
egl10.eglChooseConfig(eglDisplay, attribList, null, 0, configNum);
int num = configNum[0];
if (num != 0) {
EGLConfig[] configs = new EGLConfig[num];
egl10.eglChooseConfig(eglDisplay, attribList, configs, num, configNum);
config = configs[0];
}
return config;
}
});
}
setEGLContextFactory(mRenderer.mContextFactory);
setEGLWindowSurfaceFactory(mRenderer.mWindowSurfaceFactory);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
@Override
public EGLContext createContext(final EGL10 egl, final EGLDisplay display, final EGLConfig config) {
final int[] attrib_list;
if (EGLContextClientVersion != 0) {
attrib_list = new int[]{EGL_CONTEXT_CLIENT_VERSION, EGLContextClientVersion, EGL_NONE};
} else {
attrib_list = null;
}
return egl.eglCreateContext(display, config, EGL_NO_CONTEXT, attrib_list);
}
@Override
public void destroyContext(final EGL10 egl, final EGLDisplay display, final EGLContext context) {
if (!egl.eglDestroyContext(display, context)) {
Log.e(TAG, "display:" + display + " context: " + context);
throw new RuntimeException("eglDestroyContext" + egl.eglGetError());
}
}
private int findConfigAttrib(final EGL10 egl, final EGLDisplay display, final EGLConfig config, final int attribute, final int defaultValue) {
final int[] value = new int[1];
if (egl.eglGetConfigAttrib(display, config, attribute, value)) {
return value[0];
}
return defaultValue;
}
private EGLDisplay getEglDisplay() {
EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
throw new RuntimeException(
"Unable to get EGL10 display: 0x" + Integer.toHexString(egl.eglGetError()));
}
int[] version = new int[2];
if (!egl.eglInitialize(eglDisplay, version)) {
throw new RuntimeException(
"Unable to initialize EGL10: 0x" + Integer.toHexString(egl.eglGetError()));
}
return eglDisplay;
}
public boolean eglMakeCurrent(EGLDisplay display, EGLSurface draw,
EGLSurface read, EGLContext context) {
begin("eglMakeCurrent");
arg("display", display);
arg("draw", draw);
arg("read", read);
arg("context", context);
end();
boolean result = mEgl10.eglMakeCurrent(display, draw, read, context);
returns(result);
checkError();
return result;
}
private EGLDisplay getEglDisplay() {
EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
throw new RuntimeException(
"Unable to get EGL10 display: 0x" + Integer.toHexString(egl.eglGetError()));
}
int[] version = new int[2];
if (!egl.eglInitialize(eglDisplay, version)) {
throw new RuntimeException(
"Unable to initialize EGL10: 0x" + Integer.toHexString(egl.eglGetError()));
}
return eglDisplay;
}
public String eglQueryString(EGLDisplay display, int name) {
begin("eglQueryString");
arg("display", display);
arg("name", name);
end();
String result = mEgl10.eglQueryString(display, name);
returns(result);
checkError();
return result;
}
public boolean eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
begin("eglSwapBuffers");
arg("display", display);
arg("surface", surface);
end();
boolean result = mEgl10.eglSwapBuffers(display, surface);
returns(result);
checkError();
return result;
}
public boolean eglInitialize(EGLDisplay display, int[] major_minor) {
begin("eglInitialize");
arg("display", display);
end();
boolean result = mEgl10.eglInitialize(display, major_minor);
returns(result);
arg("major_minor", major_minor);
checkError();
return result;
}