Example #1
0
 private boolean createSurface() {
   EGL10 egl = (EGL10) EGLContext.getEGL();
   mEglSurface = egl.eglCreateWindowSurface(mEglDisplay, mEglConfig, getHolder(), null);
   if (mEglSurface == EGL10.EGL_NO_SURFACE) {
     return false;
   }
   return true;
 }
Example #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);
  }
  public EGLSurface eglCreateWindowSurface(
      EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list) {
    begin("eglCreateWindowSurface");
    arg("display", display);
    arg("config", config);
    arg("native_window", native_window);
    arg("attrib_list", attrib_list);
    end();

    EGLSurface result = mEgl10.eglCreateWindowSurface(display, config, native_window, attrib_list);
    returns(result);
    checkError();
    return result;
  }
Example #4
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();
    }
Example #5
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;
  }
Example #6
0
  public static boolean createEGLSurface() {
    if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
      EGL10 egl = (EGL10) EGLContext.getEGL();
      if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) createEGLContext();

      if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
        Log.v("SDL", "Creating new EGL Surface");
        SDLActivity.mEGLSurface =
            egl.eglCreateWindowSurface(
                SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
        if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
          Log.e("SDL", "Couldn't create surface");
          return false;
        }
      } else Log.v("SDL", "EGL Surface remains valid");

      if (egl.eglGetCurrentContext() != SDLActivity.mEGLContext) {
        if (!egl.eglMakeCurrent(
            SDLActivity.mEGLDisplay,
            SDLActivity.mEGLSurface,
            SDLActivity.mEGLSurface,
            SDLActivity.mEGLContext)) {
          Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");
          // TODO: Notify the user via a message that the old context could not be restored, and
          // that textures need to be manually restored.
          createEGLContext();
          if (!egl.eglMakeCurrent(
              SDLActivity.mEGLDisplay,
              SDLActivity.mEGLSurface,
              SDLActivity.mEGLSurface,
              SDLActivity.mEGLContext)) {
            Log.e("SDL", "Failed making EGL Context current");
            return false;
          }
        } else Log.v("SDL", "EGL Context made current");
      } else Log.v("SDL", "EGL Context remains current");

      return true;
    } else {
      Log.e(
          "SDL",
          "Surface creation failed, display = "
              + SDLActivity.mEGLDisplay
              + ", config = "
              + SDLActivity.mEGLConfig);
      return false;
    }
  }
Example #7
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;
 }
 public EGLSurface createWindowSurface(
     EGL10 egl, EGLDisplay display, EGLConfig config, Object nativeWindow) {
   // this is a bit of a hack to work around Droid init problems - if you don't have this, it'll
   // get hung up on orientation changes
   EGLSurface eglSurface = null;
   while (eglSurface == null) {
     try {
       eglSurface = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
     } catch (Throwable t) {
     } finally {
       if (eglSurface == null) {
         try {
           Thread.sleep(10);
         } catch (InterruptedException t) {
         }
       }
     }
   }
   return eglSurface;
 }
Example #9
0
 private synchronized boolean AttemptPreallocateEGLSurfaceForCompositor() {
   if (mEGLSurfaceForCompositor == null) {
     initEGL();
     try {
       mEGLSurfaceForCompositor =
           sEGL.eglCreateWindowSurface(sEGLDisplay, sEGLConfig, mView.getNativeWindow(), null);
       // In failure cases, eglCreateWindowSurface should return EGL_NO_SURFACE.
       // We currently normalize this to null, and compare to null in all our checks.
       if (mEGLSurfaceForCompositor == EGL10.EGL_NO_SURFACE) {
         mEGLSurfaceForCompositor = null;
       }
     } catch (Exception e) {
       Log.e(LOGTAG, "eglCreateWindowSurface threw", e);
     }
   }
   if (mEGLSurfaceForCompositor == null) {
     Log.w(LOGTAG, "eglCreateWindowSurface returned no surface!");
   }
   return mEGLSurfaceForCompositor != null;
 }
Example #10
0
    private boolean createSurface(Surface surface) {
      mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, surface, null);

      if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) {
        int error = sEgl.eglGetError();
        if (error == EGL_BAD_NATIVE_WINDOW) {
          Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
          return false;
        }
        throw new RuntimeException(
            "createWindowSurface failed " + GLUtils.getEGLErrorString(error));
      }

      if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
        throw new IllegalStateException(
            "eglMakeCurrent failed " + GLUtils.getEGLErrorString(sEgl.eglGetError()));
      }

      enableDirtyRegions();

      return true;
    }
        @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);
        }
Example #12
0
 /**
  * Creates a surface with the given config bound to the area given to the surface holder.
  *
  * @param egl The EGL instance.
  * @param config The EGL context configuration.
  * @param display The display which EGL has a connection to.
  * @param holder The area in the screen which the EGL surface will be bound to.
  * @return The created surface.
  */
 public static EGLSurface createSurface(
     EGL10 egl, EGLConfig config, EGLDisplay display, SurfaceHolder holder) {
   return egl.eglCreateWindowSurface(display, config, holder, null);
 }
Example #13
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);
  }
Example #14
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;
  }