我正在尝试创建 EGL 上下文以在 native 函数调用中使用 OpenglES 绘制所有内容。问题是我需要访问 NativeWindowType 实例,但我只能找到一个函数来创建一个(无论如何我找不到如何链接)。但是,即使我创建了一个,我怀疑那是错误的,因为我真正需要的是由我从中调用此 native 函数的 SurfaceView 实例创建的。
代码如下:
static int egl_init() {
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
EGLint w, h, dummy, format;
EGLint egl_major_version, egl_minor_version;
EGLint numConfigs;
EGLConfig egl_config;
EGLSurface egl_surface;
EGLContext egl_context;
EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
// v--------- This is where I should get the display window
NativeWindowType display_window;
display_window = android_createDisplaySurface();
eglInitialize(egl_display, &egl_major_version, &egl_minor_version);
printf("GL Version: %d.%d\n", egl_major_version, egl_minor_version);
if (!eglChooseConfig(egl_display, attribs, &egl_config, 1, &numConfigs))
{
printf("eglChooseConfig failed\n");
if (egl_context == 0) printf("Error code: %x\n", eglGetError());
}
eglGetConfigAttrib(egl_display, egl_config, EGL_NATIVE_VISUAL_ID, &format);
// v---------- This requires that I link libandroid, it is found in android/native_window.h
ANativeWindow_setBuffersGeometry(display_window, 0, 0, format);
egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, NULL);
if (egl_context == 0) LOGE("Error code: %x\n", eglGetError());
egl_surface = eglCreateWindowSurface(egl_display, egl_config, display_window, NULL);
if (egl_surface == 0) LOGE("Error code: %x\n", eglGetError());
if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE) {
LOGE("Unable to eglMakeCurrent");
return -1;
}
return 0;
}
谢谢你的帮助
最佳答案
表面不支持请求的egl配置(红绿蓝至少8位)。
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
某些手机默认将所有表面缓冲区设置为 RGB_565。
在 Java 中,要获得更详细的颜色或 alpha,您可以使用 getWindow 和 setFormat()。像这样: getWindow().setFormat(PixelFormat.TRANSLUCENT); 要在 native Activity 中执行等效操作,您必须执行类似以下操作。
ANativeWindow_setBuffersGeometry(display_window, 0, 0, 1);
在android/native_window.h中定义
/*
* Pixel formats that a window can use.
*/
enum {
WINDOW_FORMAT_RGBA_8888 = 1,
WINDOW_FORMAT_RGBX_8888 = 2,
WINDOW_FORMAT_RGB_565 = 4,
};
希望这对您有所帮助。我已经看到了这个问题,我刚刚弄明白了。
关于android - 当在 Java SurfaceView 中创建 NativeWindowType 时,如何从 native 函数创建 EGL 上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758975/