You may use egl to create Opengl context in nearly all situations.
Edit (putting a small c example egl + GLES2):
#include <stdio.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
static const EGLint context_attribute_list[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
int main(int argc, char** argv)
{
EGLDisplay dpy;
EGLContext ctx;
EGLConfig config;
EGLSurface surface;
EGLint major, minor, n;
ANativeWindow *window;
GLint width, height;
dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (dpy == EGL_NO_DISPLAY) {
fprintf(stderr, "eglGetDisplay() failed\n");
return -1;
}
eglInitialize(dpy, &major, &minor);
printf("GL Version: %d.%d\n", major, minor);
if (!eglChooseConfig(dpy, attribs, &config, 1, &n))
{
printf("eglChooseConfig failed\n");
printf("Error code: %x\n", eglGetError());
}
/* create an EGL rendering context */
ctx = eglCreateContext(dpy, config, EGL_NO_CONTEXT, context_attribute_list);
if (ctx == EGL_NO_CONTEXT) {
printf("Error: eglCreateContext failed: %d\n", eglGetError());
return -1;
}
window = android_createDisplaySurface();
if (window == EGL_NO_SURFACE) {
printf("Error: android_createDisplaySurface failed: %x\n", eglGetError());
return -1;
}
surface = eglCreateWindowSurface(dpy, config, window, NULL);
if (surface == EGL_NO_SURFACE) {
printf("Error: eglCreateWindowSurface failed: %d\n", eglGetError());
return -1;
}
if (!eglQuerySurface(dpy, surface, EGL_WIDTH, &width) ||
!eglQuerySurface(dpy, surface, EGL_HEIGHT, &height)) {
printf("Error: eglQuerySurface failed: %d\n", eglGetError());
return -1;
}
printf("PBuffer: %dx%d\n", width, height);
/* connect the context to the surface */
if (!eglMakeCurrent(dpy, surface, surface, ctx)) {
printf("Error: eglMakeCurrent() failed: %d\n", eglGetError());
return -1;
}
glViewport(0, 0, width, height);
/* clear the color buffer */
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
eglSwapBuffers(dpy, surface);
usleep(1000000);
return 0;
}
compiled with:
arm-linux-androideabi-gcc hello.c -o hello -lEGL -lGLESv2 -lui
On android
setEGLContextClientVersion(2);
seems to enable GLES20. (but i've not tested myself)
note: On linux, thru cairo api, I had offscreen rendering with FBO, (egl context)