예제 #1
0
    GL createEglSurface(Surface surface) throws Surface.OutOfResourcesException {
      // Check preconditions.
      if (sEgl == null) {
        throw new RuntimeException("egl not initialized");
      }
      if (sEglDisplay == null) {
        throw new RuntimeException("eglDisplay not initialized");
      }
      if (sEglConfig == null) {
        throw new RuntimeException("eglConfig not initialized");
      }
      if (Thread.currentThread() != mEglThread) {
        throw new IllegalStateException(
            "HardwareRenderer cannot be used " + "from multiple threads");
      }

      // In case we need to destroy an existing surface
      destroySurface();

      // Create an EGL surface we can render into.
      if (!createSurface(surface)) {
        return null;
      }

      initCaches();

      return mEglContext.getGL();
    }
예제 #2
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);
  }
예제 #3
0
    public void initgl() {
      cleanupgl();

      // Create new surface. Must succeed.
      surface = egl.eglCreateWindowSurface(display, config, getHolder(), null);
      if (null == surface) {
        throw new RuntimeException("eglCreateWindowSurface");
      }

      // Bind the rendering context to the surface.
      if (!egl.eglMakeCurrent(display, surface, surface, ctxt)) {
        throw new RuntimeException("eglMakeCurrent");
      }
      gl = (GL10) ctxt.getGL();
    }
예제 #4
0
  private void createSurface() {
    hasSurface = false;

    if (surface != null) {
      egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
      egl.eglDestroySurface(display, surface);
    }

    surface = egl.eglCreateWindowSurface(display, config, surfaceHolder, null);
    egl.eglMakeCurrent(display, surface, surface, eglContext);

    gl = eglContext.getGL();

    hasSurface = true;
  }
  /*
   * React to the creation of a new surface by creating and returning an OpenGL interface that renders to that
   * surface.
   */
  public GL createSurface(SurfaceHolder holder) {
    /*
     * The window size has changed, so we need to create a new surface.
     */
    if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {

      /*
       * Unbind and destroy the old EGL surface, if there is one.
       */
      mEgl.eglMakeCurrent(
          mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
      mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
    }

    /*
     * Create an EGL surface we can render into.
     */
    mEglSurface =
        mEGLWindowSurfaceFactory.createWindowSurface(mEgl, mEglDisplay, mEglConfig, holder);

    if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
      throw new RuntimeException("createWindowSurface failed");
    }

    /*
     * Before we can issue GL commands, we need to make sure the context is current and bound to a surface.
     */
    if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
      throw new RuntimeException("eglMakeCurrent failed.");
    }

    GL gl = mEglContext.getGL();
    if (mGLWrapper != null) {
      gl = mGLWrapper.wrap(gl);
    }

    /*
     * if ((mDebugFlags & (DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS))!= 0) { int configFlags = 0; Writer log =
     * null; if ((mDebugFlags & DEBUG_CHECK_GL_ERROR) != 0) { configFlags |= GLDebugHelper.CONFIG_CHECK_GL_ERROR; }
     * if ((mDebugFlags & DEBUG_LOG_GL_CALLS) != 0) { log = new LogWriter(); } gl = GLDebugHelper.wrap(gl,
     * configFlags, log); }
     */
    return gl;
  }
예제 #6
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);
  }