예제 #1
0
  private static void initEGL() {
    if (sEGL != null) {
      return;
    }

    sEGL = (EGL10) EGLContext.getEGL();

    sEGLDisplay = sEGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    if (sEGLDisplay == EGL10.EGL_NO_DISPLAY) {
      Log.w(LOGTAG, "can't get EGL display!");
      return;
    }

    // while calling eglInitialize here should not be necessary as it was already called
    // by the EGLPreloadingThread, it really doesn't cost much to call it again here,
    // and makes this code easier to think about: EGLPreloadingThread is only a
    // preloading optimization, not something we rely on for anything else.
    //
    // Also note that while calling eglInitialize isn't necessary on Android 4.x
    // (at least Android's HardwareRenderer does it for us already), it is necessary
    // on Android 2.x.
    int[] returnedVersion = new int[2];
    if (!sEGL.eglInitialize(sEGLDisplay, returnedVersion)) {
      Log.w(LOGTAG, "eglInitialize failed");
      return;
    }

    sEGLConfig = chooseConfig();
  }
예제 #2
0
  // EGL functions
  public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
    try {
      if (SDLActivity.mEGLDisplay == null) {
        Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);

        EGL10 egl = (EGL10) EGLContext.getEGL();

        EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

        int[] version = new int[2];
        egl.eglInitialize(dpy, version);

        EGLConfig[] configs = new EGLConfig[1];
        int[] num_config = new int[1];
        if (!egl.eglChooseConfig(dpy, attribs, configs, 1, num_config) || num_config[0] == 0) {
          Log.e("SDL", "No EGL config available");
          return false;
        }
        EGLConfig config = configs[0];

        SDLActivity.mEGLDisplay = dpy;
        SDLActivity.mEGLConfig = config;
        SDLActivity.mGLMajor = majorVersion;
        SDLActivity.mGLMinor = minorVersion;
      }
      return SDLActivity.createEGLSurface();

    } catch (Exception e) {
      Log.v("SDL", e + "");
      for (StackTraceElement s : e.getStackTrace()) {
        Log.v("SDL", s.toString());
      }
      return false;
    }
  }
예제 #3
0
  private boolean initGLES() {

    EGL10 egl = (EGL10) EGLContext.getEGL();

    mEglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
      Log.e(TAG, "Fail to get Display");
      return false;
    }

    int[] version = new int[2];
    if (!egl.eglInitialize(mEglDisplay, version)) {
      Log.e(TAG, "Fail to eglInitialize");
      return false;
    }

    int[] configSpec = {EGL10.EGL_NONE};
    EGLConfig[] configs = new EGLConfig[1];

    int[] numConfigs = new int[1];
    if (!egl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, numConfigs)) {
      Log.e(TAG, "Fail to Choose Config");
      return false;
    }
    mEglConfig = configs[0];

    mEglContext = egl.eglCreateContext(mEglDisplay, mEglConfig, EGL10.EGL_NO_CONTEXT, null);
    if (mEglContext == EGL10.EGL_NO_CONTEXT) {
      Log.e(TAG, "Fail to Create OpenGL Context");
      return false;
    }
    return true;
  }
  protected boolean checkGL20() {
    EGL10 egl = (EGL10) EGLContext.getEGL();
    EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    int[] version = new int[2];
    egl.eglInitialize(display, version);

    int EGL_OPENGL_ES2_BIT = 4;
    int[] configAttribs = {
      EGL10.EGL_RED_SIZE,
      4,
      EGL10.EGL_GREEN_SIZE,
      4,
      EGL10.EGL_BLUE_SIZE,
      4,
      EGL10.EGL_RENDERABLE_TYPE,
      EGL_OPENGL_ES2_BIT,
      EGL10.EGL_NONE
    };

    EGLConfig[] configs = new EGLConfig[10];
    int[] num_config = new int[1];
    egl.eglChooseConfig(display, configAttribs, configs, 10, num_config);
    egl.eglTerminate(display);
    return num_config[0] > 0;
  }
예제 #5
0
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
      Log.d(TAG, "surfaceCreated");
      // Lots of stunningly tedious EGL setup. All we want is a 565 surface.
      egl = (EGL10) EGLContext.getEGL();
      display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
      int[] version = new int[2];
      if (!egl.eglInitialize(display, version)) {
        throw new RuntimeException("eglInitialize failed");
      }
      /*
      int[] attrib_list = new int[] {
      		EGL10.EGL_RED_SIZE, 5,
      		EGL10.EGL_GREEN_SIZE, 6,
      		EGL10.EGL_BLUE_SIZE, 5,
      		EGL10.EGL_ALPHA_SIZE, 0,
      		EGL10.EGL_DEPTH_SIZE, 0,
      		EGL10.EGL_NONE
      };
      egl.eglGetConfigs(display, configs, config_size, num_config)
      EGLConfig[] configs = new EGLConfig[1];
      int[] numConfigs = new int[] {1};
      egl.eglChooseConfig(display, attrib_list, configs, configs.length, numConfigs);
      if (0 == numConfigs[0]) {
      	throw new RuntimeException("No matching EGL config");
      }
      config = configs[0];
       */

      config = getEglConfig565(egl, display);

      ctxt = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);
    }
 private void destroySurfaceForTimeWarp() {
   if (null != mMainSurface) {
     Log.v(TAG, "destroying mMainSurface: 0x%x", mMainSurface.hashCode());
     final EGL10 egl = (EGL10) EGLContext.getEGL();
     final EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
     egl.eglDestroySurface(display, mMainSurface);
     mMainSurface = null;
   }
 }
  public EGLDisplay eglGetDisplay(Object native_display) {
    begin("eglGetDisplay");
    arg("native_display", native_display);
    end();

    EGLDisplay result = mEgl10.eglGetDisplay(native_display);
    returns(result);

    checkError();
    return result;
  }
예제 #8
0
  /** Some initialization of the OpenGL stuff (that I don't understand...) */
  protected void init() {
    // Much of this code is from GLSurfaceView in the Google API Demos.
    // I encourage those interested to look there for documentation.
    egl = (EGL10) EGLContext.getEGL();
    dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    int[] version = new int[2];
    egl.eglInitialize(dpy, version);

    int[] configSpec = {
      EGL10.EGL_RED_SIZE, 5,
      EGL10.EGL_GREEN_SIZE, 6,
      EGL10.EGL_BLUE_SIZE, 5,
      EGL10.EGL_DEPTH_SIZE, 8,
      EGL10.EGL_NONE
    };

    EGLConfig[] configs = new EGLConfig[1];
    int[] num_config = new int[1];
    egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
    EGLConfig config = configs[0];

    eglContext = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);

    surface = egl.eglCreateWindowSurface(dpy, config, sHolder, null);
    egl.eglMakeCurrent(dpy, surface, surface, eglContext);

    gl = (GL10) eglContext.getGL();

    // Load the buffer and stuff
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glClearDepthf(1.0f);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glEnable(GL10.GL_TEXTURE_2D);

    // Resize... whatever?
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glViewport(0, 0, width, height);
    GLU.gluPerspective(gl, 45.0f, ((float) width) / height, 1f, 100f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 5.5f, 0, 0, 0, 0, 1, 0);
    gl.glNormal3f(0, 0, 1);
  }
  /** Initialize EGL for a given configuration spec. */
  @SuppressWarnings("StatementWithEmptyBody")
  public void start() {
    // Log.d("EglHelper" + instanceId, "start()");
    if (mEgl == null) {
      // Log.d("EglHelper" + instanceId, "getting new EGL");
      /*
       * Get an EGL instance
       */
      mEgl = (EGL10) EGLContext.getEGL();
    } else {
      // Log.d("EglHelper" + instanceId, "reusing EGL");
    }

    if (mEglDisplay == null) {
      // Log.d("EglHelper" + instanceId, "getting new display");
      /*
       * Get to the default display.
       */
      mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    } else {
      // Log.d("EglHelper" + instanceId, "reusing display");
    }

    if (mEglConfig == null) {
      // Log.d("EglHelper" + instanceId, "getting new config");
      /*
       * We can now initialize EGL for that display
       */
      int[] version = new int[2];
      mEgl.eglInitialize(mEglDisplay, version);
      mEglConfig = mEGLConfigChooser.chooseConfig(mEgl, mEglDisplay);
    } else {
      // Log.d("EglHelper" + instanceId, "reusing config");
    }

    if (mEglContext == null) {
      // Log.d("EglHelper" + instanceId, "creating new context");
      /*
       * Create an OpenGL ES context. This must be done only once, an OpenGL context is a somewhat heavy object.
       */
      mEglContext = mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
      if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
        throw new RuntimeException("createContext failed");
      }
    } else {
      // Log.d("EglHelper" + instanceId, "reusing context");
    }

    mEglSurface = null;
  }
예제 #10
0
    void initializeEgl() {
      synchronized (sEglLock) {
        if (sEgl == null && sEglConfig == null) {
          sEgl = (EGL10) EGLContext.getEGL();

          // Get to the default display.
          sEglDisplay = sEgl.eglGetDisplay(EGL_DEFAULT_DISPLAY);

          if (sEglDisplay == EGL_NO_DISPLAY) {
            throw new RuntimeException(
                "eglGetDisplay failed " + GLUtils.getEGLErrorString(sEgl.eglGetError()));
          }

          // We can now initialize EGL for that display
          int[] version = new int[2];
          if (!sEgl.eglInitialize(sEglDisplay, version)) {
            throw new RuntimeException(
                "eglInitialize failed " + GLUtils.getEGLErrorString(sEgl.eglGetError()));
          }

          checkEglErrorsForced();

          sEglConfig = chooseEglConfig();
          if (sEglConfig == null) {
            // We tried to use EGL_SWAP_BEHAVIOR_PRESERVED_BIT, try again without
            if (sDirtyRegions) {
              sDirtyRegions = false;
              sEglConfig = chooseEglConfig();
              if (sEglConfig == null) {
                throw new RuntimeException("eglConfig not initialized");
              }
            } else {
              throw new RuntimeException("eglConfig not initialized");
            }
          }
        }
      }

      ManagedEGLContext managedContext = sEglContextStorage.get();
      mEglContext = managedContext != null ? managedContext.getContext() : null;
      mEglThread = Thread.currentThread();

      if (mEglContext == null) {
        mEglContext = createContext(sEgl, sEglDisplay, sEglConfig);
        sEglContextStorage.set(createManagedContext(mEglContext));
      }
    }
예제 #11
0
  public void initialize(int[] configSpec, Renderer rendererInterface) {
    renderer = rendererInterface;

    egl = (EGL10) EGLContext.getEGL();

    display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    egl.eglInitialize(display, null);

    EGLConfig[] configs = new EGLConfig[1];
    int[] num_config = new int[1];
    egl.eglChooseConfig(display, configSpec, configs, 1, num_config);
    config = configs[0];

    eglContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);

    hasSurface = false;
    surface = null;
    createSurface();
  }
예제 #12
0
  private static void checkEGLConfigChooserSupport() throws DeviceNotSupportedException {
    /* Get an EGL instance. */
    final EGL10 egl = (EGL10) EGLContext.getEGL();

    /* Get to the default display. */
    final EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    /* We can now initialize EGL for that display. */
    final int[] version = new int[2];
    egl.eglInitialize(eglDisplay, version);

    final ConfigChooser configChooser =
        new ConfigChooser(
            false); // TODO Doesn't correlate to possible multisampling request in EngineOptions...

    try {
      configChooser.chooseConfig(egl, eglDisplay);
    } catch (final IllegalArgumentException e) {
      throw new DeviceNotSupportedException(DeviceNotSupportedCause.EGLCONFIG_NOT_FOUND, e);
    }
  }
예제 #13
0
 private void logConfig(EGLConfig config) {
   EGL10 egl = (EGL10) EGLContext.getEGL();
   EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
   int r = getAttrib(egl, display, config, EGL10.EGL_RED_SIZE, 0);
   int g = getAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0);
   int b = getAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0);
   int a = getAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0);
   int d = getAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0);
   int s = getAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0);
   int samples =
       Math.max(
           getAttrib(egl, display, config, EGL10.EGL_SAMPLES, 0),
           getAttrib(egl, display, config, GdxEglConfigChooser.EGL_COVERAGE_SAMPLES_NV, 0));
   boolean coverageSample =
       getAttrib(egl, display, config, GdxEglConfigChooser.EGL_COVERAGE_SAMPLES_NV, 0) != 0;
   Gdx.app.log("AndroidGraphics", "framebuffer: (" + r + ", " + g + ", " + b + ", " + a + ")");
   Gdx.app.log("AndroidGraphics", "depthbuffer: (" + d + ")");
   Gdx.app.log("AndroidGraphics", "stencilbuffer: (" + s + ")");
   Gdx.app.log("AndroidGraphics", "samples: (" + samples + ")");
   Gdx.app.log("AndroidGraphics", "coverage sampling: (" + coverageSample + ")");
   bufferFormat = new BufferFormat(r, g, b, a, d, s, samples, coverageSample);
 }
  private void logConfig(EGLConfig config) {
    EGL10 egl = (EGL10) EGLContext.getEGL();
    EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    int r = getAttrib(egl, display, config, EGL10.EGL_RED_SIZE, 0);
    int g = getAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0);
    int b = getAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0);
    int a = getAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0);
    int d = getAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0);
    int s = getAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0);
    int samples =
        Math.max(
            getAttrib(egl, display, config, EGL10.EGL_SAMPLES, 0),
            getAttrib(egl, display, config, GdxEglConfigChooser.EGL_COVERAGE_SAMPLES_NV, 0));
    boolean coverageSample =
        getAttrib(egl, display, config, GdxEglConfigChooser.EGL_COVERAGE_SAMPLES_NV, 0) != 0;

    // print configuration just one time (on some devices gl context is recreated every time when
    // device is locked / unlocked - every time when screen turns on and off)
    if (!configLogged) {

      if (gl != null) {
        Gdx.app.log("AndroidGraphics", "OGL renderer: " + gl.glGetString(GL10.GL_RENDERER));
        Gdx.app.log("AndroidGraphics", "OGL vendor: " + gl.glGetString(GL10.GL_VENDOR));
        Gdx.app.log("AndroidGraphics", "OGL version: " + gl.glGetString(GL10.GL_VERSION));
        Gdx.app.log("AndroidGraphics", "OGL extensions: " + gl.glGetString(GL10.GL_EXTENSIONS));
        configLogged = true;
      }

      Gdx.app.log("AndroidGraphics", "framebuffer: (" + r + ", " + g + ", " + b + ", " + a + ")");
      Gdx.app.log("AndroidGraphics", "depthbuffer: (" + d + ")");
      Gdx.app.log("AndroidGraphics", "stencilbuffer: (" + s + ")");
      Gdx.app.log("AndroidGraphics", "samples: (" + samples + ")");
      Gdx.app.log("AndroidGraphics", "coverage sampling: (" + coverageSample + ")");
    }

    bufferFormat = new BufferFormat(r, g, b, a, d, s, samples, coverageSample);
  }
예제 #15
0
  /** @return OpenGL ES major version 1, 2, or 3 or some non-positive number for error */
  private static int getDetectedVersion() {
    /*
     * Get all the device configurations and check the EGL_RENDERABLE_TYPE attribute
     * to determine the highest ES version supported by any config. The
     * EGL_KHR_create_context extension is required to check for ES3 support; if the
     * extension is not present this test will fail to detect ES3 support. This
     * effectively makes the extension mandatory for ES3-capable devices.
     */

    EGL10 egl = (EGL10) EGLContext.getEGL();
    EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    int[] numConfigs = new int[1];

    if (egl.eglInitialize(display, null)) {
      try {
        boolean checkES3 =
            hasExtension(
                egl.eglQueryString(display, EGL10.EGL_EXTENSIONS), "EGL_KHR_create_context");
        if (egl.eglGetConfigs(display, null, 0, numConfigs)) {
          EGLConfig[] configs = new EGLConfig[numConfigs[0]];
          if (egl.eglGetConfigs(display, configs, numConfigs[0], numConfigs)) {
            int highestEsVersion = 0;
            int[] value = new int[1];
            for (int i = 0; i < numConfigs[0]; i++) {
              if (egl.eglGetConfigAttrib(display, configs[i], EGL10.EGL_RENDERABLE_TYPE, value)) {
                if (checkES3 && ((value[0] & EGL_OPENGL_ES3_BIT_KHR) == EGL_OPENGL_ES3_BIT_KHR)) {
                  if (highestEsVersion < 3) highestEsVersion = 3;
                } else if ((value[0] & EGL_OPENGL_ES2_BIT) == EGL_OPENGL_ES2_BIT) {
                  if (highestEsVersion < 2) highestEsVersion = 2;
                } else if ((value[0] & EGL_OPENGL_ES_BIT) == EGL_OPENGL_ES_BIT) {
                  if (highestEsVersion < 1) highestEsVersion = 1;
                }
              } else {
                Log.w(
                    TAG,
                    "Getting config attribute with "
                        + "EGL10#eglGetConfigAttrib failed "
                        + "("
                        + i
                        + "/"
                        + numConfigs[0]
                        + "): "
                        + egl.eglGetError());
              }
            }
            return highestEsVersion;
          } else {
            Log.e(TAG, "Getting configs with EGL10#eglGetConfigs failed: " + egl.eglGetError());
            return -1;
          }
        } else {
          Log.e(
              TAG,
              "Getting number of configs with EGL10#eglGetConfigs failed: " + egl.eglGetError());
          return -2;
        }
      } finally {
        egl.eglTerminate(display);
      }
    } else {
      Log.e(TAG, "Couldn't initialize EGL.");
      return -3;
    }
  }
예제 #16
0
  public MainView(Context context, Activity inActivity) {
    super(context);

    int eglVersion = 1;

    // See if version 2 is supported?
    if (false || false) {
      EGL10 egl = (EGL10) EGLContext.getEGL();
      EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
      int[] version = new int[2];
      egl.eglInitialize(display, version);
      EGLConfig[] v2_configs = new EGLConfig[1];
      int[] num_config = new int[1];
      int[] attrs = {EGL10.EGL_RENDERABLE_TYPE, 4 /*EGL_OPENGL_ES2_BIT*/, EGL10.EGL_NONE};
      egl.eglChooseConfig(display, attrs, v2_configs, 1, num_config);
      // Log.v("EGL","v2 configs : " + num_config[0]);
      if (num_config[0] == 1) {
        eglVersion = 2;
        setEGLContextClientVersion(2);
      }
    }

    final int renderType = eglVersion == 1 ? 0x01 : 0x04;

    setEGLConfigChooser(
        new EGLConfigChooser() {
          public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            int depth = 0;
            int stencil = 0;

            EGLConfig[] configs = new EGLConfig[1];
            int[] num_config = new int[1];

            // Try as specified - aa
            if (0 > 1) {
              int[] attrs = {
                EGL10.EGL_DEPTH_SIZE,
                depth,
                EGL10.EGL_STENCIL_SIZE,
                stencil,
                EGL10.EGL_SAMPLE_BUFFERS,
                1 /* true */,
                EGL10.EGL_SAMPLES,
                0,
                EGL10.EGL_RENDERABLE_TYPE,
                renderType,
                EGL10.EGL_NONE
              };

              egl.eglChooseConfig(display, attrs, configs, 1, num_config);
              Log.v("EGL", "Match AA=0, depth + stencil : " + num_config[0]);

              if (num_config[0] == 1) return configs[0];

              // Try with just 2 specified - aa
              if (0 > 2) {
                int[] attrs_aa2 = {
                  EGL10.EGL_DEPTH_SIZE,
                  depth,
                  EGL10.EGL_STENCIL_SIZE,
                  stencil,
                  EGL10.EGL_SAMPLE_BUFFERS,
                  1 /* true */,
                  EGL10.EGL_SAMPLES,
                  2,
                  EGL10.EGL_RENDERABLE_TYPE,
                  renderType,
                  EGL10.EGL_NONE
                };

                egl.eglChooseConfig(display, attrs_aa2, configs, 1, num_config);
                Log.v("EGL", "Match AA=2, depth + stencil : " + num_config[0]);

                if (num_config[0] == 1) return configs[0];
              }

              // No normal multisampling config was found. Try to create a
              // converage multisampling configuration, for the nVidia Tegra2.
              // See the EGL_NV_coverage_sample documentation.

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

              int[] attrs_aanv = {
                EGL10.EGL_DEPTH_SIZE,
                depth,
                EGL10.EGL_STENCIL_SIZE,
                stencil,
                EGL_COVERAGE_BUFFERS_NV,
                1 /* true */,
                EGL_COVERAGE_SAMPLES_NV,
                2, // always 5 in practice on tegra 2
                EGL10.EGL_RENDERABLE_TYPE,
                renderType,
                EGL10.EGL_NONE
              };

              egl.eglChooseConfig(display, attrs_aanv, configs, 1, num_config);
              Log.v("EGL", "Match AANV, depth + stencil : " + num_config[0]);

              if (num_config[0] == 1) return configs[0];
            }

            // Try just specifying just depth and stencil
            int[] attrs1 = {
              EGL10.EGL_DEPTH_SIZE,
              depth,
              EGL10.EGL_STENCIL_SIZE,
              stencil,
              EGL10.EGL_RENDERABLE_TYPE,
              renderType,
              EGL10.EGL_NONE
            };

            egl.eglChooseConfig(display, attrs1, configs, 1, num_config);
            Log.v("EGL", "Matched depth + stencil : " + num_config[0]);
            if (num_config[0] == 1) return configs[0];

            // Just give me whatever you've got
            int[] attrs2 = {EGL10.EGL_NONE};
            egl.eglChooseConfig(display, attrs2, configs, 1, num_config);
            if (num_config[0] == 1) return configs[0];
            Log.v("EGL", "Matched any : " + num_config[0]);

            return null;
          }
        });
    mActivity = inActivity;
    mRefreshView = this;
    setFocusable(true);
    setFocusableInTouchMode(true);
    setRenderer(new Renderer(this));
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
  }
        @Override
        public void onSurfaceChanged(final GL10 gl, final int width, final int height) {
          Log.i(TAG, "onSurfaceChanged; %d x %d", width, height);

          if (null != mMainSurface) {
            Log.v(TAG, "short-circuiting onSurfaceChanged");
            return;
          }

          final EGL10 egl = (EGL10) EGLContext.getEGL();
          final EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
          final EGLContext context = egl.eglGetCurrentContext();

          int numAttribs = 0;
          final int[] configAttribs = new int[16];
          Arrays.fill(configAttribs, EGL10.EGL_NONE);

          Log.v(TAG, "--- window surface configuration ---");
          final VrAppSettings appSettings = mActivity.getAppSettings();
          if (appSettings.useSrgbFramebuffer) {
            final int EGL_GL_COLORSPACE_KHR = 0x309D;
            final int EGL_GL_COLORSPACE_SRGB_KHR = 0x3089;

            configAttribs[numAttribs++] = EGL_GL_COLORSPACE_KHR;
            configAttribs[numAttribs++] = EGL_GL_COLORSPACE_SRGB_KHR;
          }
          Log.v(TAG, "--- srgb framebuffer: %b", appSettings.useSrgbFramebuffer);

          if (appSettings.useProtectedFramebuffer) {
            final int EGL_PROTECTED_CONTENT_EXT = 0x32c0;

            configAttribs[numAttribs++] = EGL_PROTECTED_CONTENT_EXT;
            configAttribs[numAttribs++] = EGL14.EGL_TRUE;
          }
          Log.v(TAG, "--- protected framebuffer: %b", appSettings.useProtectedFramebuffer);

          configAttribs[numAttribs++] = EGL10.EGL_NONE;
          Log.v(TAG, "------------------------------------");

          // this is the display surface timewarp will hijack
          mMainSurface =
              egl.eglCreateWindowSurface(display, mConfig, mSurfaceView.getHolder(), configAttribs);
          Log.v(TAG, "mMainSurface: 0x%x", mMainSurface.hashCode());

          if (mMainSurface == EGL10.EGL_NO_SURFACE) {
            throw new IllegalStateException(
                "eglCreateWindowSurface() failed: 0x" + Integer.toHexString(egl.eglGetError()));
          }
          if (!egl.eglMakeCurrent(display, mMainSurface, mMainSurface, context)) {
            throw new IllegalStateException(
                "eglMakeCurrent() failed: 0x " + Integer.toHexString(egl.eglGetError()));
          }
          nativeOnSurfaceChanged(mPtr);

          // necessary to explicitly make the pbuffer current for the rendering thread;
          // TimeWarp took over the window surface
          if (!egl.eglMakeCurrent(display, mPixelBuffer, mPixelBuffer, context)) {
            throw new IllegalStateException(
                "Failed to make context current ; egl error 0x"
                    + Integer.toHexString(egl.eglGetError()));
          }

          startChoreographerThreadIfNotStarted();
          mViewManager.onSurfaceChanged(width, height);
        }
예제 #18
0
  // EGL functions
  public boolean initEGL(int majorVersion, int minorVersion) {
    Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);

    try {
      EGL10 egl = (EGL10) EGLContext.getEGL();

      EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

      int[] version = new int[2];
      egl.eglInitialize(dpy, version);

      int EGL_OPENGL_ES_BIT = 1;
      int EGL_OPENGL_ES2_BIT = 4;
      int renderableType = 0;
      if (majorVersion == 2) {
        renderableType = EGL_OPENGL_ES2_BIT;
      } else if (majorVersion == 1) {
        renderableType = EGL_OPENGL_ES_BIT;
      }
      int[] configSpec = {
        // EGL10.EGL_DEPTH_SIZE,   16,
        EGL10.EGL_RENDERABLE_TYPE, renderableType, EGL10.EGL_NONE
      };
      EGLConfig[] configs = new EGLConfig[1];
      int[] num_config = new int[1];
      if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
        Log.e("SDL", "No EGL config available");
        return false;
      }
      EGLConfig config = configs[0];

      EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
      if (ctx == EGL10.EGL_NO_CONTEXT) {
        Log.e("SDL", "Couldn't create context");
        return false;
      }

      EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
      if (surface == EGL10.EGL_NO_SURFACE) {
        Log.e("SDL", "Couldn't create surface");
        return false;
      }

      if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) {
        Log.e("SDL", "Couldn't make context current");
        return false;
      }

      mEGLContext = ctx;
      mEGLDisplay = dpy;
      mEGLSurface = surface;

    } catch (Exception e) {
      Log.v("SDL", e + "");
      for (StackTraceElement s : e.getStackTrace()) {
        Log.v("SDL", s.toString());
      }
    }

    return true;
  }
예제 #19
0
  /**
   * <code>createView</code> creates the GLSurfaceView that the renderer will draw to.
   *
   * <p>The result GLSurfaceView will receive input events and forward them to the Application. Any
   * rendering will be done into the GLSurfaceView. Only one GLSurfaceView can be created at this
   * time. The given configType specifies how to determine the display configuration.
   *
   * @param configType ConfigType.FASTEST (Default) | ConfigType.LEGACY | ConfigType.BEST
   * @param eglConfigVerboseLogging if true show all found configs
   * @return GLSurfaceView The newly created view
   */
  public GLSurfaceView createView(ConfigType configType, boolean eglConfigVerboseLogging) {
    // if simulated joysticks are used, init the window to update the activity used to
    // get the window orientation
    if (androidSensorJoyInput != null && androidSensorJoyInput instanceof AndroidSensorJoyInput) {
      ((AndroidSensorJoyInput) androidSensorJoyInput).initWindow();
    }

    // Start to set up the view
    view = new AndroidGLSurfaceView(JmeAndroidSystem.getActivity());
    if (androidInput == null) {
      androidInput = new AndroidInput(view);
    } else {
      androidInput.setView(view);
    }
    if (configType == ConfigType.LEGACY) {
      // Hardcoded egl setup
      clientOpenGLESVersion = 2;
      view.setEGLContextClientVersion(2);
      // RGB565, Depth16
      view.setEGLConfigChooser(5, 6, 5, 0, 16, 0);
      logger.info("ConfigType.LEGACY using RGB565");
    } else {
      EGL10 egl = (EGL10) EGLContext.getEGL();
      EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

      int[] version = new int[2];
      if (egl.eglInitialize(display, version) == true) {
        logger.log(
            Level.INFO, "Display EGL Version: {0}.{1}", new Object[] {version[0], version[1]});
      }

      try {
        // Create a config chooser
        AndroidConfigChooser configChooser = new AndroidConfigChooser(configType);
        // Init chooser
        if (!configChooser.findConfig(egl, display)) {
          listener.handleError("Unable to find suitable EGL config", null);
          return null;
        }

        clientOpenGLESVersion = configChooser.getClientOpenGLESVersion();
        if (clientOpenGLESVersion < 2) {
          listener.handleError("OpenGL ES 2.0 is not supported on this device", null);
          return null;
        }

        // Requesting client version from GLSurfaceView which is extended by
        // AndroidInput.
        view.setEGLContextClientVersion(clientOpenGLESVersion);
        view.setEGLConfigChooser(configChooser);
        view.getHolder().setFormat(configChooser.getPixelFormat());
      } finally {
        if (display != null) {
          egl.eglTerminate(display);
        }
      }
    }

    view.setFocusableInTouchMode(true);
    view.setFocusable(true);
    view.getHolder().setType(SurfaceHolder.SURFACE_TYPE_GPU);
    if (configType == ConfigType.BEST_TRANSLUCENT) {
      // This is important to allow the GL surface to have a translucent background
      view.setZOrderOnTop(true);
    }
    view.setRenderer(this);

    return view;
  }
예제 #20
0
  // EGL functions
  public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
    try {
      EGL10 egl = (EGL10) EGLContext.getEGL();

      if (SDLActivity.mEGLDisplay == null) {
        SDLActivity.mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        int[] version = new int[2];
        egl.eglInitialize(SDLActivity.mEGLDisplay, version);
      }

      if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
        // No current GL context exists, we will create a new one.
        Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
        EGLConfig[] configs = new EGLConfig[128];
        int[] num_config = new int[1];
        if (!egl.eglChooseConfig(SDLActivity.mEGLDisplay, attribs, configs, 1, num_config)
            || num_config[0] == 0) {
          Log.e("SDL", "No EGL config available");
          return false;
        }
        EGLConfig config = null;
        int bestdiff = -1, bitdiff;
        int[] value = new int[1];

        // eglChooseConfig returns a number of configurations that match or exceed the requested
        // attribs.
        // From those, we select the one that matches our requirements more closely
        Log.v("SDL", "Got " + num_config[0] + " valid modes from egl");
        for (int i = 0; i < num_config[0]; i++) {
          bitdiff = 0;
          // Go through some of the attributes and compute the bit difference between what we want
          // and what we get.
          for (int j = 0; ; j += 2) {
            if (attribs[j] == EGL10.EGL_NONE) break;

            if (attribs[j + 1] != EGL10.EGL_DONT_CARE
                && (attribs[j] == EGL10.EGL_RED_SIZE
                    || attribs[j] == EGL10.EGL_GREEN_SIZE
                    || attribs[j] == EGL10.EGL_BLUE_SIZE
                    || attribs[j] == EGL10.EGL_ALPHA_SIZE
                    || attribs[j] == EGL10.EGL_DEPTH_SIZE
                    || attribs[j] == EGL10.EGL_STENCIL_SIZE)) {
              egl.eglGetConfigAttrib(SDLActivity.mEGLDisplay, configs[i], attribs[j], value);
              bitdiff += value[0] - attribs[j + 1]; // value is always >= attrib
            }
          }

          if (bitdiff < bestdiff || bestdiff == -1) {
            config = configs[i];
            bestdiff = bitdiff;
          }

          if (bitdiff == 0) break; // we found an exact match!
        }

        Log.d("SDL", "Selected mode with a total bit difference of " + bestdiff);

        SDLActivity.mEGLConfig = config;
        SDLActivity.mGLMajor = majorVersion;
        SDLActivity.mGLMinor = minorVersion;
      }

      return SDLActivity.createEGLSurface();

    } catch (Exception e) {
      Log.v("SDL", e + "");
      for (StackTraceElement s : e.getStackTrace()) {
        Log.v("SDL", s.toString());
      }
      return false;
    }
  }
예제 #21
0
  public void run() {
    // Much of this code is from GLSurfaceView in the Google API Demos.
    // I encourage those interested to look there for documentation.
    EGL10 egl = (EGL10) EGLContext.getEGL();
    EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    int[] version = new int[2];
    egl.eglInitialize(dpy, version);

    int[] configSpec = {
      EGL10.EGL_RED_SIZE, 5,
      EGL10.EGL_GREEN_SIZE, 6,
      EGL10.EGL_BLUE_SIZE, 5,
      EGL10.EGL_DEPTH_SIZE, 16,
      EGL10.EGL_NONE
    };

    EGLConfig[] configs = new EGLConfig[1];
    int[] num_config = new int[1];
    egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
    EGLConfig config = configs[0];

    EGLContext context = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);

    EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, sHolder, null);
    egl.eglMakeCurrent(dpy, surface, surface, context);

    GL10 gl = (GL10) context.getGL();

    init(gl);

    int delta = -1;
    if (fps > 0) {
      delta = 1000 / fps;
    }
    long time = System.currentTimeMillis();

    running = true;
    while (running) {
      int w, h;
      synchronized (this) {
        w = width;
        h = height;
      }
      if (System.currentTimeMillis() - time < delta) {
        try {
          Thread.sleep(System.currentTimeMillis() - time);
        } catch (InterruptedException ex) {
        }
      }
      drawFrame(gl, w, h);
      egl.eglSwapBuffers(dpy, surface);

      if (egl.eglGetError() == EGL11.EGL_CONTEXT_LOST) {
        Context c = getContext();
        if (c instanceof Activity) {
          ((Activity) c).finish();
        }
      }
      time = System.currentTimeMillis();
    }
    egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
    egl.eglDestroySurface(dpy, surface);
    egl.eglDestroyContext(dpy, context);
    egl.eglTerminate(dpy);
  }