Android Open Source - android-plotter Multisample Config Chooser






From Project

Back to project page android-plotter.

License

The source code is released under:

Apache License

If you think the Android project android-plotter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.solovyev.android.plotter;
/*w ww .  j  a v a 2 s .  com*/
import android.opengl.GLSurfaceView;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;

/**
 * OpenGL configuration chooser which tries to choose a config with anti-aliasing (multisampling).
 * In case if such configuration is not supported by device default configuration is used.
 */
public final class MultisampleConfigChooser implements GLSurfaceView.EGLConfigChooser {

  private static final int EGL_COVERAGE_BUFFERS_NV = 0x30E0;
  private static final int EGL_COVERAGE_SAMPLES_NV = 0x30E1;

  private static final int RED = 5;
  private static final int GREEN = 6;
  private static final int BLUE = 5;
  private static final int ALPHA = 0;
  private static final int DEPTH = 16;

  @Override
  public EGLConfig chooseConfig(@Nonnull EGL10 gl, @Nonnull EGLDisplay display) {
    // try to find a normal multisample configuration first.
    EGLConfig config = ConfigData.create(EGL10.EGL_RED_SIZE, RED,
        EGL10.EGL_GREEN_SIZE, GREEN,
        EGL10.EGL_BLUE_SIZE, BLUE,
        EGL10.EGL_ALPHA_SIZE, ALPHA,
        EGL10.EGL_DEPTH_SIZE, DEPTH,
        EGL10.EGL_RENDERABLE_TYPE, 4 /* EGL_OPENGL_ES2_BIT */,
        EGL10.EGL_SAMPLE_BUFFERS, 1 /* true */,
        EGL10.EGL_SAMPLES, 2,
        EGL10.EGL_NONE).tryConfig(gl, display);
    if (config != null) {
      return config;
    }

    // no normal multisampling config was found. Try to create a
    // coverage multisampling configuration, for the nVidia Tegra2.
    // See the EGL_NV_coverage_sample documentation.
    config = ConfigData.create(EGL10.EGL_RED_SIZE, RED,
        EGL10.EGL_GREEN_SIZE, GREEN,
        EGL10.EGL_BLUE_SIZE, BLUE,
        EGL10.EGL_ALPHA_SIZE, ALPHA,
        EGL10.EGL_DEPTH_SIZE, DEPTH,
        EGL10.EGL_RENDERABLE_TYPE, 4 /* EGL_OPENGL_ES2_BIT */,
        EGL_COVERAGE_BUFFERS_NV, 1 /* true */,
        EGL_COVERAGE_SAMPLES_NV, 2,  // always 5 in practice on tegra 2
        EGL10.EGL_NONE).tryConfig(gl, display);
    if (config != null) {
      return config;
    }

    // fallback to simple configuration
    config = ConfigData.create(
        EGL10.EGL_RED_SIZE, RED,
        EGL10.EGL_GREEN_SIZE, GREEN,
        EGL10.EGL_BLUE_SIZE, BLUE,
        EGL10.EGL_ALPHA_SIZE, ALPHA,
        EGL10.EGL_DEPTH_SIZE, DEPTH,
        EGL10.EGL_NONE).tryConfig(gl, display);
    if (config != null) {
      return config;
    }

    throw new IllegalArgumentException("No supported configuration found");
  }

  private static int findConfigAttrib(@Nonnull EGL10 gl, @Nonnull EGLDisplay display, @Nonnull EGLConfig config, int attribute, int defaultValue, int[] tmp) {
    if (gl.eglGetConfigAttrib(display, config, attribute, tmp)) {
      return tmp[0];
    }
    return defaultValue;
  }

  private static final class ConfigData {
    final int[] spec;

    private ConfigData(int[] spec) {
      this.spec = spec;
    }

    @Nonnull
    private static ConfigData create(int... spec) {
      return new ConfigData(spec);
    }

    @Nullable
    private EGLConfig tryConfig(@Nonnull EGL10 gl, @Nonnull EGLDisplay display) {
      final int[] tmp = new int[1];

      if (!gl.eglChooseConfig(display, spec, null, 0, tmp)) {
        return null;
      }
      final int count = tmp[0];
      if (count > 0) {
        // get all matching configurations
        final EGLConfig[] configs = new EGLConfig[count];
        if (!gl.eglChooseConfig(display, spec, configs, count, tmp)) {
          return null;
        }

        return findConfig(gl, display, configs, tmp);
      }

      return null;
    }

    @Nullable
    private EGLConfig findConfig(@Nonnull EGL10 gl, @Nonnull EGLDisplay display, @Nonnull EGLConfig[] configs, int[] tmp) {
      // sometimes eglChooseConfig returns configurations with not requested
      // options: even though we asked for rgb565 configurations, rgb888
      // configurations are considered to be "better" and returned first.
      // We need to explicitly filter data returned by eglChooseConfig
      // adn choose the right configuration.
      for (final EGLConfig config : configs) {
        if (config != null && isDesiredConfig(gl, display, tmp, config)) {
          return config;
        }
      }

      return null;
    }

    private boolean isDesiredConfig(@Nonnull EGL10 gl, @Nonnull EGLDisplay display, @Nonnull int[] tmp, @Nonnull EGLConfig config) {
      for (int i = 0; i + 1 < spec.length; i += 2) {
        final int attribute = spec[i];
        final int desiredValue = spec[i + 1];
        final int actualValue = findConfigAttrib(gl, display, config, attribute, 0, tmp);
        if (attribute == EGL10.EGL_DEPTH_SIZE) {
          if (actualValue < desiredValue) {
            return false;
          }
        } else {
          if (desiredValue != actualValue) {
            return false;
          }
        }
      }

      return true;
    }
  }
}




Java Source Code List

com.android.texample.GLText.java
com.android.texample.SpriteBatch.java
com.android.texample.TexampleRenderer.java
com.android.texample.TextureRegion.java
com.android.texample.Vertices.java
org.solovyev.android.plotter.Angle.java
org.solovyev.android.plotter.AxisStyle.java
org.solovyev.android.plotter.Check.java
org.solovyev.android.plotter.Color.java
org.solovyev.android.plotter.DefaultPlotter.java
org.solovyev.android.plotter.Dimensions.java
org.solovyev.android.plotter.Frustum.java
org.solovyev.android.plotter.Function0.java
org.solovyev.android.plotter.Function1.java
org.solovyev.android.plotter.Function2.java
org.solovyev.android.plotter.Function.java
org.solovyev.android.plotter.LineStyle.java
org.solovyev.android.plotter.MeshConfig.java
org.solovyev.android.plotter.MultisampleConfigChooser.java
org.solovyev.android.plotter.PinchZoomTracker.java
org.solovyev.android.plotter.PlotData.java
org.solovyev.android.plotter.PlotFunction.java
org.solovyev.android.plotter.PlotRenderer.java
org.solovyev.android.plotter.PlotView.java
org.solovyev.android.plotter.Plot.java
org.solovyev.android.plotter.Plotter.java
org.solovyev.android.plotter.PlottingView.java
org.solovyev.android.plotter.Spf.java
org.solovyev.android.plotter.SuperFunction.java
org.solovyev.android.plotter.TouchHandler.java
org.solovyev.android.plotter.ZoomLevels.java
org.solovyev.android.plotter.Zoomer.java
org.solovyev.android.plotter.app.MainActivity.java
org.solovyev.android.plotter.app.PlotterApplication.java
org.solovyev.android.plotter.meshes.Arrays.java
org.solovyev.android.plotter.meshes.AxisGrid.java
org.solovyev.android.plotter.meshes.Axis.java
org.solovyev.android.plotter.meshes.BaseCube.java
org.solovyev.android.plotter.meshes.BaseCurve.java
org.solovyev.android.plotter.meshes.BaseMesh.java
org.solovyev.android.plotter.meshes.BaseSurface.java
org.solovyev.android.plotter.meshes.DimensionsAwareSwapper.java
org.solovyev.android.plotter.meshes.DimensionsAware.java
org.solovyev.android.plotter.meshes.DoubleBufferGroup.java
org.solovyev.android.plotter.meshes.DoubleBufferMesh.java
org.solovyev.android.plotter.meshes.FunctionGraph2d.java
org.solovyev.android.plotter.meshes.FunctionGraph3d.java
org.solovyev.android.plotter.meshes.FunctionGraphSwapper.java
org.solovyev.android.plotter.meshes.FunctionGraph.java
org.solovyev.android.plotter.meshes.Graph.java
org.solovyev.android.plotter.meshes.Group.java
org.solovyev.android.plotter.meshes.IndicesOrder.java
org.solovyev.android.plotter.meshes.ListGroup.java
org.solovyev.android.plotter.meshes.ListPool.java
org.solovyev.android.plotter.meshes.Mesh.java
org.solovyev.android.plotter.meshes.Meshes.java
org.solovyev.android.plotter.meshes.Pool.java
org.solovyev.android.plotter.meshes.Scene.java
org.solovyev.android.plotter.meshes.SolidCube.java
org.solovyev.android.plotter.meshes.SurfaceInitializer.java
org.solovyev.android.plotter.meshes.WireFrameCube.java