GL30 adapter (#652)

This commit is contained in:
Gustl22
2019-02-03 21:10:11 +01:00
committed by Emux
parent f6f00c2521
commit 45cf4057d1
11 changed files with 191 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
/*
* Copyright 2012 Hannes Janetzek
* Copyright 2016-2018 devemux86
* Copyright 2018 Gustl22
* Copyright 2018-2019 Gustl22
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@@ -31,7 +31,9 @@ import android.view.WindowManager;
import org.oscim.android.canvas.AndroidGraphics;
import org.oscim.android.gl.AndroidGL;
import org.oscim.android.gl.AndroidGL30;
import org.oscim.android.gl.GlConfigChooser;
import org.oscim.android.gl.GlContextFactory;
import org.oscim.android.input.AndroidMotionEvent;
import org.oscim.android.input.GestureHandler;
import org.oscim.backend.CanvasAdapter;
@@ -59,6 +61,11 @@ public class MapView extends GLSurfaceView {
static final Logger log = LoggerFactory.getLogger(MapView.class);
/**
* Target OpenGL ES version, if not available fall back to OpenGL ES 2.0
*/
public static int targetGLESVersion = 3;
private static void init() {
System.loadLibrary("vtm-jni");
}
@@ -90,7 +97,6 @@ public class MapView extends GLSurfaceView {
/* Setup android backend */
AndroidGraphics.init();
AndroidAssets.init(context);
GLAdapter.init(new AndroidGL());
DateTimeAdapter.init(new DateTime());
DisplayMetrics metrics = getResources().getDisplayMetrics();
@@ -116,8 +122,9 @@ public class MapView extends GLSurfaceView {
mMap = new AndroidMap(this);
/* Initialize Renderer */
//setEGLContextClientVersion(targetGLESVersion);
setEGLContextFactory(new GlContextFactory());
setEGLConfigChooser(new GlConfigChooser());
setEGLContextClientVersion(2);
if (GLAdapter.debug)
setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR
@@ -291,6 +298,15 @@ public class MapView extends GLSurfaceView {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Check OpenGL ES version
String versionStr = gl.glGetString(GL10.GL_VERSION);
int versionIndex = "OpenGL ES ".length();
float version = Float.parseFloat(versionStr.substring(versionIndex, versionIndex + 3));
if (version >= 3)
GLAdapter.init(new AndroidGL30());
else
GLAdapter.init(new AndroidGL());
super.onSurfaceCreated();
}

View File

@@ -20,7 +20,7 @@ public class GlConfigChooser implements GLSurfaceView.EGLConfigChooser {
EGL10.EGL_ALPHA_SIZE, 8,
EGL10.EGL_DEPTH_SIZE, 16,
// Requires that setEGLContextClientVersion(2) is called on the view.
EGL10.EGL_RENDERABLE_TYPE, 4 /* EGL_OPENGL_ES2_BIT */,
EGL10.EGL_RENDERABLE_TYPE, 4 /*EGL14.EGL_OPENGL_ES2_BIT*/ /*0x40 /*EGLExt.EGL_OPENGL_ES3_BIT_KHR*/,
EGL10.EGL_STENCIL_SIZE, 8,
EGL10.EGL_NONE};
@@ -38,7 +38,7 @@ public class GlConfigChooser implements GLSurfaceView.EGLConfigChooser {
EGL10.EGL_BLUE_SIZE, 8,
EGL10.EGL_ALPHA_SIZE, 8,
EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_RENDERABLE_TYPE, 4 /* EGL_OPENGL_ES2_BIT */,
EGL10.EGL_RENDERABLE_TYPE, 4 /*EGL14.EGL_OPENGL_ES2_BIT*/ /*0x40 /*EGLExt.EGL_OPENGL_ES3_BIT_KHR*/,
EGL10.EGL_STENCIL_SIZE, 8,
EGL10.EGL_NONE};

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2019 Gustl22
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.oscim.android.gl;
import android.opengl.GLSurfaceView;
import org.oscim.android.MapView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
/**
* See https://github.com/libgdx/libgdx/blob/master/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/surfaceview/GLSurfaceView20.java
*/
public class GlContextFactory implements GLSurfaceView.EGLContextFactory {
private static final Logger log = LoggerFactory.getLogger(GlContextFactory.class);
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
@Override
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
log.info("creating OpenGL ES " + MapView.targetGLESVersion + ".0 context");
checkEglError("Before eglCreateContext " + MapView.targetGLESVersion, egl);
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, MapView.targetGLESVersion, EGL10.EGL_NONE};
EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
boolean success = checkEglError("After eglCreateContext " + MapView.targetGLESVersion, egl);
if ((!success || context == null) && MapView.targetGLESVersion > 2) {
log.warn("Falling back to GLES 2");
MapView.targetGLESVersion = 2;
return createContext(egl, display, eglConfig);
}
log.info("Returning a GLES " + MapView.targetGLESVersion + " context");
return context;
}
@Override
public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
egl.eglDestroyContext(display, context);
}
private static boolean checkEglError(String prompt, EGL10 egl) {
int error;
boolean result = true;
while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
result = false;
log.error(String.format("%s: EGL error: 0x%x", prompt, error));
}
return result;
}
}