【问题标题】:How can I create an EGL context from a native function when the NativeWindowType is created in a Java SurfaceView?在 Java SurfaceView 中创建 NativeWindowType 时,如何从本机函数创建 EGL 上下文?
【发布时间】:2012-05-25 17:36:37
【问题描述】:

我正在尝试创建 EGL 上下文以在本机函数调用中使用 OpenglES 绘制所有内容。问题是我需要访问 NativeWindowType 实例,但我只能找到一个函数来创建一个(无论如何我不知道如何链接)。但是,即使我创建了一个,我也怀疑这是错误的,因为我真正需要的是由我调用此本机函数的 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;
}

感谢您的帮助

【问题讨论】:

  • 不要使用 android_createDisplaySurface()。它不是公共 API,仅适用于支持旧帧缓冲区 HAL 的设备(最近的 Nexus 设备都不支持它)。

标签: android opengl-es android-ndk


【解决方案1】:

表面不能支持请求的 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); 要在原生活动中执行等效操作,您必须执行以下操作。

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,
};

希望这会有所帮助。我已经看到了这个问题,我只是想通了。

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 2014-11-14
    • 1970-01-01
    • 2012-07-13
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多