void android_main(struct android_app* app) { char * argv[] = {"cvandroid"}; const EGLint attr[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_NONE }; EGLint ncfg; EGLint fmt; EGLint w; EGLint h; EGLConfig cfg; EGLDisplay dpy; EGLSurface surf; EGLContext ctx; app_dummy(); app->userData = 0; app->onAppCmd = handle; app->onInputEvent = input; dbg("wait\n"); while (!app->userData) pollEvent(app); g_app = app; cvInject(CVE_INIT, 1, (intptr_t)argv); dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (!eglInitialize(dpy, 0, 0)) err("initialize display"); eglChooseConfig(dpy, attr, &cfg, 1, &ncfg); eglGetConfigAttrib(dpy, cfg, EGL_NATIVE_VISUAL_ID, &fmt); ANativeWindow_setBuffersGeometry(app->window, 0, 0, fmt); surf = eglCreateWindowSurface(dpy, cfg, app->window, 0); ctx = eglCreateContext(dpy, cfg, 0, 0); eglMakeCurrent(dpy, surf, surf, ctx); eglQuerySurface(dpy, surf, EGL_WIDTH, &w); eglQuerySurface(dpy, surf, EGL_HEIGHT, &h); if (!eglSwapInterval(dpy, 1)) err("setting swap interval"); cvInject(CVE_RESIZE, w, h); cvInject(CVE_GLINIT, 0, 0); app->userData = 0; while (!app->userData && !app->destroyRequested) { cvInject(CVE_UPDATE, 0, 0); if (!eglSwapBuffers(dpy, surf)) err("swapbuffers failed"); pollEvent(app); } if (app->destroyRequested) cvInject(CVE_CLOSE, 0, 0); dbg("terminated"); cvInject(CVE_GLTERM, 0, 0); if (!eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) err("clearing current"); if (!eglDestroyContext(dpy, ctx)) err("destroying context"); if (!eglDestroySurface(dpy, surf)) err("destroying surface"); if (!eglTerminate(dpy)) err("terminating"); }
int main(int argc, char *argv[]) { static unsigned int gs_screen_wdt = 0; static unsigned int gs_screen_hgt = 0; static void *gs_native_window = 0; NXPL_NativeWindowInfo win_info; NEXUS_ClientAuthenticationSettings authSettings; NEXUS_Error err; printf ("simple_client_init(\"xre\", &authSettings)\n"); simple_client_init("xre", &authSettings); err = NEXUS_Platform_AuthenticatedJoin(&authSettings); printf("NEXUS_Platform_AuthenticatedJoin(&authSettings) : %d\n", NEXUS_Platform_AuthenticatedJoin(&authSettings)); if (err) { exit(EXIT_FAILURE); } NXPL_PlatformHandle nxpl_handle = 0; NXPL_RegisterNexusDisplayPlatform (&nxpl_handle, EGL_DEFAULT_DISPLAY ); gs_screen_wdt = 1280; gs_screen_hgt = 720; win_info.x = 0; win_info.y = 0; win_info.width = gs_screen_wdt; win_info.height = gs_screen_hgt; win_info.stretch = true; gs_native_window = NXPL_CreateNativeWindow(&win_info); printf ("NXPL_CreateNativeWindow(&win_info) : %d\n", gs_native_window); int maj, min; EGLContext ctx; EGLSurface pbuffer, pwindow; EGLConfig* configs; EGLBoolean b; const EGLint pbufAttribs[] = { EGL_WIDTH, 500, EGL_HEIGHT, 500, EGL_NONE }; const EGLint pwinAttribs[] = { EGL_WIDTH, 500, EGL_HEIGHT, 500, EGL_NONE }; if ( eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE ) { printf("failed to bind api %x\n", eglGetError()); exit (EXIT_FAILURE); } EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(d); if (!eglInitialize(d, &maj, &min)) { printf("demo: eglInitialize failed\n"); exit(1); } printf("EGL version = %d.%d\n", maj, min); printf("EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR)); configs=PrintConfigs(d); ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL); if (ctx == EGL_NO_CONTEXT) { printf("failed to create context\n"); return 0; } printf("eglCreateWindowSurface : configs[0] %p\n", configs[0]); /* pbuffer = eglCreatePbufferSurface(d, configs[0], pbufAttribs); if (pbuffer == EGL_NO_SURFACE) { printf("failed to create pbuffer\n"); return 0; } */ int i =0; for (i=0; i<=0x1c; ++i) { pbuffer = eglCreatePbufferSurface(d, configs[i], pbufAttribs); // pwindow = eglCreateWindowSurface(d, i, gs_native_window, pwinAttribs); // if (pwindow == EGL_NO_SURFACE) { if (pbuffer == EGL_NO_SURFACE) { printf("failed to create pwindow for config[%x]\n", i); // return 0; } else { printf("created pwindow for config[%x]\n", i); // eglDestroySurface(d, pwindow); eglDestroySurface(d, pbuffer); } } // b = eglMakeCurrent(d, pbuffer, pbuffer, ctx); b = eglMakeCurrent(d, pwindow, pwindow, ctx); if (!b) { printf("make current failed\n"); return 0; } b = eglMakeCurrent(d, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); // eglDestroySurface(d, pbuffer); // eglDestroySurface(d, pwindow); eglDestroyContext(d, ctx); free(configs); eglTerminate(d); return 0; }
static bool gfx_ctx_set_video_mode( unsigned width, unsigned height, bool fullscreen) { if (g_inited) return false; int ret = 0; struct drm_fb *fb = NULL; struct sigaction sa = {{0}}; sa.sa_handler = sighandler; sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); #define EGL_ATTRIBS_BASE \ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \ EGL_RED_SIZE, 1, \ EGL_GREEN_SIZE, 1, \ EGL_BLUE_SIZE, 1, \ EGL_ALPHA_SIZE, 0, \ EGL_DEPTH_SIZE, 0 static const EGLint egl_attribs_gl[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE, }; static const EGLint egl_attribs_gles[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE, }; static const EGLint egl_attribs_vg[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT, EGL_NONE, }; // GLES 2.0. Don't use for any other API. static const EGLint gles_context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint *attrib_ptr; switch (g_api) { case GFX_CTX_OPENGL_API: attrib_ptr = egl_attribs_gl; break; case GFX_CTX_OPENGL_ES_API: attrib_ptr = egl_attribs_gles; break; case GFX_CTX_OPENVG_API: attrib_ptr = egl_attribs_vg; break; default: attrib_ptr = NULL; } g_egl_dpy = eglGetDisplay((EGLNativeDisplayType)g_gbm_dev); if (!g_egl_dpy) { RARCH_ERR("[KMS/EGL]: Couldn't get EGL display.\n"); goto error; } EGLint major, minor; if (!eglInitialize(g_egl_dpy, &major, &minor)) goto error; EGLint n; if (!eglChooseConfig(g_egl_dpy, attrib_ptr, &g_config, 1, &n) || n != 1) goto error; g_egl_ctx = eglCreateContext(g_egl_dpy, g_config, EGL_NO_CONTEXT, (g_api == GFX_CTX_OPENGL_ES_API) ? gles_context_attribs : NULL); if (!g_egl_ctx) goto error; g_egl_surf = eglCreateWindowSurface(g_egl_dpy, g_config, (EGLNativeWindowType)g_gbm_surface, NULL); if (!g_egl_surf) goto error; if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx)) goto error; glClear(GL_COLOR_BUFFER_BIT); eglSwapBuffers(g_egl_dpy, g_egl_surf); g_bo = gbm_surface_lock_front_buffer(g_gbm_surface); fb = drm_fb_get_from_bo(g_bo); ret = drmModeSetCrtc(g_drm_fd, g_crtc_id, fb->fb_id, 0, 0, &g_connector_id, 1, g_drm_mode); if (ret < 0) goto error; g_inited = true; return true; error: gfx_ctx_destroy(); return false; }
SkDevice* GaneshContext::getDeviceForBaseTile(const TileRenderInfo& renderInfo) { // Ganesh should be the only code in the rendering thread that is using GL // and setting the EGLContext. If this is not the case then we need to // reset the Ganesh context to prevent rendering issues. bool contextNeedsReset = false; if (eglGetCurrentContext() != m_surfaceContext) { XLOG("Warning: EGLContext has Changed! %p, %p", m_surfaceContext, eglGetCurrentContext()); contextNeedsReset = true; } EGLDisplay display; if (!m_surfaceContext) { if(eglGetCurrentContext() != EGL_NO_CONTEXT) { XLOG("ERROR: should not have a context yet"); } display = eglGetDisplay(EGL_DEFAULT_DISPLAY); GLUtils::checkEglError("eglGetDisplay"); EGLint majorVersion; EGLint minorVersion; EGLBoolean returnValue = eglInitialize(display, &majorVersion, &minorVersion); GLUtils::checkEglError("eglInitialize", returnValue); EGLint numConfigs; static const EGLint configAttribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_STENCIL_SIZE, 8, EGL_NONE }; eglChooseConfig(display, configAttribs, &m_surfaceConfig, 1, &numConfigs); GLUtils::checkEglError("eglChooseConfig"); static const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; m_surfaceContext = eglCreateContext(display, m_surfaceConfig, NULL, contextAttribs); GLUtils::checkEglError("eglCreateContext"); } else { display = eglGetCurrentDisplay(); GLUtils::checkEglError("eglGetCurrentDisplay"); } TransferQueue* tileQueue = TilesManager::instance()->transferQueue(); if (tileQueue->m_eglSurface == EGL_NO_SURFACE) { const float tileWidth = renderInfo.tileSize.width(); const float tileHeight = renderInfo.tileSize.height(); ANativeWindow* anw = tileQueue->m_ANW.get(); int result = ANativeWindow_setBuffersGeometry(anw, (int)tileWidth, (int)tileHeight, WINDOW_FORMAT_RGBA_8888); renderInfo.textureInfo->m_width = tileWidth; renderInfo.textureInfo->m_height = tileHeight; tileQueue->m_eglSurface = eglCreateWindowSurface(display, m_surfaceConfig, anw, NULL); GLUtils::checkEglError("eglCreateWindowSurface"); XLOG("eglCreateWindowSurface"); } EGLBoolean returnValue = eglMakeCurrent(display, tileQueue->m_eglSurface, tileQueue->m_eglSurface, m_surfaceContext); GLUtils::checkEglError("eglMakeCurrent", returnValue); XLOG("eglMakeCurrent"); if (!m_baseTileDeviceSurface) { GrPlatformRenderTargetDesc renderTargetDesc; renderTargetDesc.fWidth = TilesManager::tileWidth(); renderTargetDesc.fHeight = TilesManager::tileHeight(); renderTargetDesc.fConfig = kRGBA_8888_PM_GrPixelConfig; renderTargetDesc.fSampleCnt = 0; renderTargetDesc.fStencilBits = 8; renderTargetDesc.fRenderTargetHandle = 0; GrContext* grContext = getGrContext(); GrRenderTarget* renderTarget = grContext->createPlatformRenderTarget(renderTargetDesc); m_baseTileDeviceSurface = new SkGpuDevice(grContext, renderTarget); renderTarget->unref(); XLOG("generated device %p", m_baseTileDeviceSurface); } GLUtils::checkGlError("getDeviceForBaseTile"); // We must reset the Ganesh context only after we are sure we have // re-established our EGLContext as the current context. if (m_baseTileDeviceSurface && contextNeedsReset) getGrContext()->resetContext(); return m_baseTileDeviceSurface; }
bool FBgles1Glue::init(int /*argc*/, char *** /*argv*/) { GNASH_REPORT_FUNCTION; EGLint majorVersion, minorVersion; EGLint numOfConfigs = 0; EGLint result; const EGLint main_attrib_list[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BUFFER_SIZE, 32, EGL_DEPTH_SIZE, 16, EGL_NONE }; _display = eglGetDisplay((NativeDisplayType)0); if (_display == EGL_NO_DISPLAY) { return false; } log_trace(_("EGL: getDisplay ok")); result = eglInitialize(_display, &majorVersion, &minorVersion); if (result == EGL_FALSE) { return false; } log_trace(_("EGL: initialize ok")); result = eglChooseConfig(_display, main_attrib_list, &_config, 1, &numOfConfigs); if (result == EGL_FALSE || numOfConfigs != 1) { return false; } log_trace(_("EGL: config ok")); _surface = eglCreateWindowSurface(_display, _config, (NativeWindowType)0, NULL); if (eglGetError () != EGL_SUCCESS) { return false; } log_trace(_("EGL: surface ok")); _context = eglCreateContext(_display, _config, NULL, NULL); if (eglGetError () != EGL_SUCCESS) { return false; } log_trace(_("EGL: context ok")); eglMakeCurrent(_display, _surface, _surface, _context); if (eglGetError () != EGL_SUCCESS) { return false; } log_trace(_("EGL: current ok")); const EGLint pbuffer_config_list[] = { EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_BUFFER_SIZE, 32, EGL_DEPTH_SIZE, 0, EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE, EGL_CONFIG_CAVEAT, EGL_NONE, EGL_NONE }; result = eglChooseConfig(_display, pbuffer_config_list, &_pbuffer_config, 1, &numOfConfigs); if (result == EGL_FALSE || numOfConfigs == 0) { return false; } // log_trace("EGL: pbuffer config ok"); const EGLint pbuffer_attrib_list[] = { EGL_WIDTH, EGL_MAX_PBUFFER_WIDTH, EGL_HEIGHT, EGL_MAX_PBUFFER_HEIGHT, EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA, EGL_TEXTURE_TARGET, EGL_TEXTURE_2D, EGL_MIPMAP_TEXTURE, EGL_FALSE, EGL_NONE }; _pbuffer = eglCreatePbufferSurface(_display, _pbuffer_config, pbuffer_attrib_list); if (eglGetError () != EGL_SUCCESS) { return false; } log_trace("EGL: pbuffer surface ok"); return true; }
//============================================================================== void initGL(int argc, char **argv){ // Start OpenGL ES bcm_host_init(); // Clear application state int32_t success = 0; EGLBoolean result; EGLint num_config; static EGL_DISPMANX_WINDOW_T nativeviewport; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; uint32_t screen_width; uint32_t screen_height; static const EGLint attribute_list[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_DEPTH_SIZE, 16, EGL_NONE }; static const EGLint context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLConfig config; // get an EGL display connection display = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(display!=EGL_NO_DISPLAY); check(); // initialize the EGL display connection result = eglInitialize(display, NULL, NULL); assert(EGL_FALSE != result); check(); // get an appropriate EGL frame buffer configuration result = eglChooseConfig(display, attribute_list, &config, 1, &num_config); assert(EGL_FALSE != result); check(); // get an appropriate EGL frame buffer configuration result = eglBindAPI(EGL_OPENGL_ES_API); assert(EGL_FALSE != result); check(); // create an EGL rendering context context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes); assert(context!=EGL_NO_CONTEXT); check(); // create an EGL viewport surface success = graphics_get_display_size(0 /* LCD */, &screen_width, &screen_height); assert( success >= 0 ); // Initially the viewport is for all the screen viewport.x = 0; viewport.y = 0; viewport.z = screen_width; viewport.w = screen_height; dst_rect.x = viewport.x; dst_rect.y = viewport.y; dst_rect.width = viewport.z; dst_rect.height = viewport.w; src_rect.x = 0; src_rect.y = 0; src_rect.width = viewport.z << 16; src_rect.height = viewport.w << 16; dispman_display = vc_dispmanx_display_open( 0 /* LCD */); dispman_update = vc_dispmanx_update_start( 0 ); dispman_element = vc_dispmanx_element_add( dispman_update, dispman_display, 0/*layer*/, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/); nativeviewport.element = dispman_element; nativeviewport.width = viewport.z; nativeviewport.height = viewport.w; vc_dispmanx_update_submit_sync( dispman_update ); check(); surface = eglCreateWindowSurface( display, config, &nativeviewport, NULL ); assert(surface != EGL_NO_SURFACE); check(); // connect the context to the surface result = eglMakeCurrent(display, surface, surface, context); assert(EGL_FALSE != result); check(); // Set background color and clear buffers // glClearColor(0.15f, 0.25f, 0.35f, 1.0f); // glClear( GL_COLOR_BUFFER_BIT ); setWindowSize(viewport.z,viewport.w); mouse.x = viewport.z*0.5; mouse.y = viewport.w*0.5; check(); initMouse(); ///printf("OpenGL Initialize at %i,%i,%i,%i\n",viewport.x,viewport.y,viewport.z,viewport.w); }
int egl_create(EGLDisplay *_display, EGLSurface *_surface, int *_w, int *_h) { EGLBoolean res; EGLConfig config = { 0 }; EGLint context_attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLint config_attrs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint major, minor; EGLContext context; EGLSurface surface; EGLint w, h; EGLDisplay display; EGLNativeWindowType window; unsigned width, height, format; struct CNativeWindow *cnw; display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (display == EGL_NO_DISPLAY) return -1; if (!(res = eglInitialize(display, &major, &minor))) return -1; fprintf(stderr, "egl version: %d.%d\n", major, minor); if ((cnw = cnw_create()) == 0) return -1; cnw_info(cnw, &width, &height, &format); window = (EGLNativeWindowType) cnw; if ((res = select_config_for_window(display, config_attrs, format, &config))) goto fail; surface = eglCreateWindowSurface(display, config, window, NULL); if (surface == EGL_NO_SURFACE) goto fail; context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attrs); if (context == EGL_NO_CONTEXT) goto fail; if (!(res = eglMakeCurrent(display, surface, surface, context))) goto fail; eglQuerySurface(display, surface, EGL_WIDTH, &w); eglQuerySurface(display, surface, EGL_HEIGHT, &h); fprintf(stderr, "window: %d x %d\n", w, h); *_display = display; *_surface = surface; *_w = w; *_h = h; _cnw = cnw; return 0; fail: cnw_destroy(cnw); return -1; }
int main(int argc, char *argv[]) { EGLDisplay display; EGLint egl_major, egl_minor; EGLConfig config; EGLint num_config; EGLContext context; EGLSurface surface; GLuint vertex_shader; GLuint fragment_shader; GLuint program; GLint ret; GLint width, height; const char *vertex_shader_source = "uniform mat4 modelviewMatrix;\n" "uniform mat4 modelviewprojectionMatrix;\n" "uniform mat3 normalMatrix;\n" "\n" "attribute vec4 in_position; \n" "attribute vec3 in_normal; \n" "attribute vec4 in_color; \n" "\n" "vec4 lightSource = vec4(2.0, 2.0, 20.0, 0.0);\n" " \n" "varying vec4 vVaryingColor; \n" " \n" "void main() \n" "{ \n" " gl_Position = modelviewprojectionMatrix * in_position;\n" " vec3 vEyeNormal = normalMatrix * in_normal;\n" " vec4 vPosition4 = modelviewMatrix * in_position;\n" " vec3 vPosition3 = vPosition4.xyz / vPosition4.w;\n" " vec3 vLightDir = normalize(lightSource.xyz - vPosition3);\n" " float diff = max(0.0, dot(vEyeNormal, vLightDir));\n" " vVaryingColor = vec4(diff * in_color.rgb, 1.0);\n" "} \n"; const char *fragment_shader_source = "precision mediump float; \n" " \n" "varying vec4 vVaryingColor; \n" " \n" "void main() \n" "{ \n" " gl_FragColor = vVaryingColor; \n" "} \n"; GLfloat vVertices[] = { // front -1.0f, -1.0f, +1.0f, // point blue +1.0f, -1.0f, +1.0f, // point magenta -1.0f, +1.0f, +1.0f, // point cyan +1.0f, +1.0f, +1.0f, // point white // back +1.0f, -1.0f, -1.0f, // point red -1.0f, -1.0f, -1.0f, // point black +1.0f, +1.0f, -1.0f, // point yellow -1.0f, +1.0f, -1.0f, // point green // right +1.0f, -1.0f, +1.0f, // point magenta +1.0f, -1.0f, -1.0f, // point red +1.0f, +1.0f, +1.0f, // point white +1.0f, +1.0f, -1.0f, // point yellow // left -1.0f, -1.0f, -1.0f, // point black -1.0f, -1.0f, +1.0f, // point blue -1.0f, +1.0f, -1.0f, // point green -1.0f, +1.0f, +1.0f, // point cyan // top -1.0f, +1.0f, +1.0f, // point cyan +1.0f, +1.0f, +1.0f, // point white -1.0f, +1.0f, -1.0f, // point green +1.0f, +1.0f, -1.0f, // point yellow // bottom -1.0f, -1.0f, -1.0f, // point black +1.0f, -1.0f, -1.0f, // point red -1.0f, -1.0f, +1.0f, // point blue +1.0f, -1.0f, +1.0f // point magenta }; GLfloat vColors[] = { // front 0.0f, 0.0f, 1.0f, // blue 1.0f, 0.0f, 1.0f, // magenta 0.0f, 1.0f, 1.0f, // cyan 1.0f, 1.0f, 1.0f, // white // back 1.0f, 0.0f, 0.0f, // red 0.0f, 0.0f, 0.0f, // black 1.0f, 1.0f, 0.0f, // yellow 0.0f, 1.0f, 0.0f, // green // right 1.0f, 0.0f, 1.0f, // magenta 1.0f, 0.0f, 0.0f, // red 1.0f, 1.0f, 1.0f, // white 1.0f, 1.0f, 0.0f, // yellow // left 0.0f, 0.0f, 0.0f, // black 0.0f, 0.0f, 1.0f, // blue 0.0f, 1.0f, 0.0f, // green 0.0f, 1.0f, 1.0f, // cyan // top 0.0f, 1.0f, 1.0f, // cyan 1.0f, 1.0f, 1.0f, // white 0.0f, 1.0f, 0.0f, // green 1.0f, 1.0f, 0.0f, // yellow // bottom 0.0f, 0.0f, 0.0f, // black 1.0f, 0.0f, 0.0f, // red 0.0f, 0.0f, 1.0f, // blue 1.0f, 0.0f, 1.0f // magenta }; GLfloat vNormals[] = { // front +0.0f, +0.0f, +1.0f, // forward +0.0f, +0.0f, +1.0f, // forward +0.0f, +0.0f, +1.0f, // forward +0.0f, +0.0f, +1.0f, // forward // back +0.0f, +0.0f, -1.0f, // backbard +0.0f, +0.0f, -1.0f, // backbard +0.0f, +0.0f, -1.0f, // backbard +0.0f, +0.0f, -1.0f, // backbard // right +1.0f, +0.0f, +0.0f, // right +1.0f, +0.0f, +0.0f, // right +1.0f, +0.0f, +0.0f, // right +1.0f, +0.0f, +0.0f, // right // left -1.0f, +0.0f, +0.0f, // left -1.0f, +0.0f, +0.0f, // left -1.0f, +0.0f, +0.0f, // left -1.0f, +0.0f, +0.0f, // left // top +0.0f, +1.0f, +0.0f, // up +0.0f, +1.0f, +0.0f, // up +0.0f, +1.0f, +0.0f, // up +0.0f, +1.0f, +0.0f, // up // bottom +0.0f, -1.0f, +0.0f, // down +0.0f, -1.0f, +0.0f, // down +0.0f, -1.0f, +0.0f, // down +0.0f, -1.0f, +0.0f // down }; display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (display == EGL_NO_DISPLAY) { printf("Error: No display found!\n"); return -1; } if (!eglInitialize(display, &egl_major, &egl_minor)) { printf("Error: eglInitialise failed!\n"); return -1; } printf("Using display %p with EGL version %d.%d\n", display, egl_major, egl_minor); printf("EGL Version \"%s\"\n", eglQueryString(display, EGL_VERSION)); printf("EGL Vendor \"%s\"\n", eglQueryString(display, EGL_VENDOR)); printf("EGL Extensions \"%s\"\n", eglQueryString(display, EGL_EXTENSIONS)); /* get an appropriate EGL frame buffer configuration */ eglChooseConfig(display, config_attribute_list, &config, 1, &num_config); /* create an EGL rendering context */ context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribute_list); if (context == EGL_NO_CONTEXT) { printf("Error: eglCreateContext failed: %d\n", eglGetError()); return -1; } surface = eglCreatePbufferSurface(display, config, pbuffer_attribute_list); if (surface == EGL_NO_SURFACE) { printf("Error: eglCreatePbufferSurface failed: %d (%s)\n", eglGetError(), eglStrError(eglGetError())); return -1; } if (!eglQuerySurface(display, surface, EGL_WIDTH, &width) || !eglQuerySurface(display, surface, EGL_HEIGHT, &height)) { printf("Error: eglQuerySurface failed: %d (%s)\n", eglGetError(), eglStrError(eglGetError())); return -1; } printf("PBuffer: %dx%d\n", width, height); /* connect the context to the surface */ if (!eglMakeCurrent(display, surface, surface, context)) { printf("Error: eglMakeCurrent() failed: %d (%s)\n", eglGetError(), eglStrError(eglGetError())); return -1; } vertex_shader = glCreateShader(GL_VERTEX_SHADER); if (!vertex_shader) { printf("Error: glCreateShader(GL_VERTEX_SHADER) failed: %d (%s)\n", eglGetError(), eglStrError(eglGetError())); return -1; } glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL); glCompileShader(vertex_shader); glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &ret); if (!ret) { char *log; printf("Error: vertex shader compilation failed!:\n"); glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &ret); if (ret > 1) { log = malloc(ret); glGetShaderInfoLog(vertex_shader, ret, NULL, log); printf("%s", log); } return -1; } else printf("Vertex shader compilation succeeded!\n"); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); if (!fragment_shader) { printf("Error: glCreateShader(GL_FRAGMENT_SHADER) failed: %d (%s)\n", eglGetError(), eglStrError(eglGetError())); return -1; } glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL); glCompileShader(fragment_shader); glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &ret); if (!ret) { char *log; printf("Error: fragment shader compilation failed!:\n"); glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &ret); if (ret > 1) { log = malloc(ret); glGetShaderInfoLog(fragment_shader, ret, NULL, log); printf("%s", log); } return -1; } else printf("Fragment shader compilation succeeded!\n"); program = glCreateProgram(); if (!program) { printf("Error: failed to create program!\n"); return -1; } glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glBindAttribLocation(program, 0, "in_position"); glBindAttribLocation(program, 1, "in_normal"); glBindAttribLocation(program, 2, "in_color"); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &ret); if (!ret) { char *log; printf("Error: program linking failed!:\n"); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &ret); if (ret > 1) { log = malloc(ret); glGetProgramInfoLog(program, ret, NULL, log); printf("%s", log); } return -1; } else printf("program linking succeeded!\n"); glUseProgram(program); glViewport(0, 0, width, height); /* clear the color buffer */ glClearColor(0.5, 0.5, 0.5, 1.0); glClear(GL_COLOR_BUFFER_BIT); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, vNormals); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, vColors); glEnableVertexAttribArray(2); ESMatrix modelview; esMatrixLoadIdentity(&modelview); esTranslate(&modelview, 0.0f, 0.0f, -8.0f); esRotate(&modelview, 45.0f, 1.0f, 0.0f, 0.0f); esRotate(&modelview, 45.0f, 0.0f, 1.0f, 0.0f); esRotate(&modelview, 10.0f, 0.0f, 0.0f, 1.0f); GLfloat aspect = (GLfloat)(height) / (GLfloat)(width); ESMatrix projection; esMatrixLoadIdentity(&projection); esFrustum(&projection, -2.8f, +2.8f, -2.8f * aspect, +2.8f * aspect, 6.0f, 10.0f); ESMatrix modelviewprojection; esMatrixLoadIdentity(&modelviewprojection); esMatrixMultiply(&modelviewprojection, &modelview, &projection); float normal[9]; normal[0] = modelview.m[0][0]; normal[1] = modelview.m[0][1]; normal[2] = modelview.m[0][2]; normal[3] = modelview.m[1][0]; normal[4] = modelview.m[1][1]; normal[5] = modelview.m[1][2]; normal[6] = modelview.m[2][0]; normal[7] = modelview.m[2][1]; normal[8] = modelview.m[2][2]; GLint modelviewmatrix_handle = glGetUniformLocation(program, "modelviewMatrix"); GLint modelviewprojectionmatrix_handle = glGetUniformLocation(program, "modelviewprojectionMatrix"); GLint normalmatrix_handle = glGetUniformLocation(program, "normalMatrix"); glUniformMatrix4fv(modelviewmatrix_handle, 1, GL_FALSE, &modelview.m[0][0]); glUniformMatrix4fv(modelviewprojectionmatrix_handle, 1, GL_FALSE, &modelviewprojection.m[0][0]); glUniformMatrix3fv(normalmatrix_handle, 1, GL_FALSE, normal); glEnable(GL_CULL_FACE); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 4, 4); glDrawArrays(GL_TRIANGLE_STRIP, 8, 4); glDrawArrays(GL_TRIANGLE_STRIP, 12, 4); glDrawArrays(GL_TRIANGLE_STRIP, 16, 4); glDrawArrays(GL_TRIANGLE_STRIP, 20, 4); glFlush(); usleep(1000000); fflush(stdout); return 0; }
/*********************************************************** * Name: init_ogl * * Arguments: * CUBE_STATE_T *state - holds OGLES model info * * Description: Sets the display, OpenGL|ES context and screen stuff * * Returns: void * ***********************************************************/ static void init_ogl(CUBE_STATE_T *state) { int32_t success = 0; EGLBoolean result; EGLint num_config; static EGL_DISPMANX_WINDOW_T nativewindow; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; static const EGLint attribute_list[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 16, //EGL_SAMPLES, 4, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; EGLConfig config; // get an EGL display connection state->display = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(state->display!=EGL_NO_DISPLAY); // initialize the EGL display connection result = eglInitialize(state->display, NULL, NULL); assert(EGL_FALSE != result); // get an appropriate EGL frame buffer configuration // this uses a BRCM extension that gets the closest match, rather than standard which returns anything that matches result = eglSaneChooseConfigBRCM(state->display, attribute_list, &config, 1, &num_config); assert(EGL_FALSE != result); // create an EGL rendering context state->context = eglCreateContext(state->display, config, EGL_NO_CONTEXT, NULL); assert(state->context!=EGL_NO_CONTEXT); // create an EGL window surface success = graphics_get_display_size(0 /* LCD */, &state->screen_width, &state->screen_height); assert( success >= 0 ); dst_rect.x = 0; dst_rect.y = 0; dst_rect.width = state->screen_width; dst_rect.height = state->screen_height; src_rect.x = 0; src_rect.y = 0; src_rect.width = state->screen_width << 16; src_rect.height = state->screen_height << 16; dispman_display = vc_dispmanx_display_open( 0 /* LCD */); dispman_update = vc_dispmanx_update_start( 0 ); dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display, 0/*layer*/, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/); nativewindow.element = dispman_element; nativewindow.width = state->screen_width; nativewindow.height = state->screen_height; vc_dispmanx_update_submit_sync( dispman_update ); state->surface = eglCreateWindowSurface( state->display, config, &nativewindow, NULL ); assert(state->surface != EGL_NO_SURFACE); // connect the context to the surface result = eglMakeCurrent(state->display, state->surface, state->surface, state->context); assert(EGL_FALSE != result); // Set background color and clear buffers glClearColor(0.15f, 0.25f, 0.35f, 1.0f); // Enable back face culling. glEnable(GL_CULL_FACE); glMatrixMode(GL_MODELVIEW); }
bool CWinEGLPlatformGeneric::InitializeDisplay() { if (m_display != EGL_NO_DISPLAY && m_config != NULL) return true; EGLBoolean eglStatus; EGLint configCount; EGLConfig* configList = NULL; m_display = eglGetDisplay(m_nativeDisplay); if (m_display == EGL_NO_DISPLAY) { CLog::Log(LOGERROR, "EGL failed to obtain display"); return false; } if (!eglInitialize(m_display, 0, 0)) { CLog::Log(LOGERROR, "EGL failed to initialize"); return false; } EGLint configAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, 0, EGL_SAMPLE_BUFFERS, 0, EGL_SAMPLES, 0, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; // Find out how many configurations suit our needs eglStatus = eglChooseConfig(m_display, configAttrs, NULL, 0, &configCount); if (!eglStatus || !configCount) { CLog::Log(LOGERROR, "EGL failed to return any matching configurations: %d", eglStatus); return false; } // Allocate room for the list of matching configurations configList = (EGLConfig*)malloc(configCount * sizeof(EGLConfig)); if (!configList) { CLog::Log(LOGERROR, "kdMalloc failure obtaining configuration list"); return false; } // Obtain the configuration list from EGL eglStatus = eglChooseConfig(m_display, configAttrs, configList, configCount, &configCount); if (!eglStatus || !configCount) { CLog::Log(LOGERROR, "EGL failed to populate configuration list: %d", eglStatus); return false; } // Select an EGL configuration that matches the native window m_config = configList[0]; if (m_surface != EGL_NO_SURFACE) ReleaseSurface(); free(configList); return true; }
bool createSurface() //unsigned int display_width, unsigned int display_height) { LOG(LogInfo) << "Starting SDL..."; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO) != 0) { LOG(LogError) << "Error initializing SDL!\n " << SDL_GetError() << "\n" << "Are you in the 'video', 'audio', and 'input' groups? Is X closed? Is your firmware up to date? Are you using at least the 192/64 memory split?"; return false; } sdlScreen = SDL_SetVideoMode(1, 1, 0, SDL_SWSURFACE); if(sdlScreen == NULL) { LOG(LogError) << "Error creating SDL window for input!"; return false; } LOG(LogInfo) << "Creating surface..."; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if(display == EGL_NO_DISPLAY) { LOG(LogError) << "Error getting display!"; return false; } bool result = eglInitialize(display, NULL, NULL); if(result == EGL_FALSE) { LOG(LogError) << "Error initializing display!"; return false; } result = eglBindAPI(EGL_OPENGL_ES_API); if(result == EGL_FALSE) { LOG(LogError) << "Error binding API!"; return false; } static const EGLint config_attributes[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; GLint numConfigs; result = eglChooseConfig(display, config_attributes, &config, 1, &numConfigs); if(result == EGL_FALSE) { LOG(LogError) << "Error choosing config!"; return false; } context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL); if(context == EGL_NO_CONTEXT) { LOG(LogError) << "Error getting context!\n " << eglGetError(); return false; } if(!display_width || !display_height) { bool success = graphics_get_display_size(0, &display_width, &display_height); //0 = LCD if(success < 0) { LOG(LogError) << "Error getting display size!"; return false; } } LOG(LogInfo) << "Resolution: " << display_width << "x" << display_height << "..."; dst_rect.x = 0; dst_rect.y = 0; dst_rect.width = display_width; dst_rect.height = display_height; src_rect.x = 0; src_rect.y = 0; src_rect.width = display_width << 16; src_rect.height = display_height << 16; dispman_display = vc_dispmanx_display_open(0); //0 = LCD dispman_update = vc_dispmanx_update_start(0); dispman_element = vc_dispmanx_element_add(dispman_update, dispman_display, 0 /*layer*/, &dst_rect, 0 /*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0 /*clamp*/, DISPMANX_NO_ROTATE /*transform*/); nativewindow.element = dispman_element; nativewindow.width = display_width; nativewindow.height = display_height; vc_dispmanx_update_submit_sync(dispman_update); surface = eglCreateWindowSurface(display, config, &nativewindow, NULL); if(surface == EGL_NO_SURFACE) { LOG(LogError) << "Error creating window surface!"; return false; } result = eglMakeCurrent(display, surface, surface, context); if(result == EGL_FALSE) { LOG(LogError) << "Error with eglMakeCurrent!"; return false; } LOG(LogInfo) << "Created surface successfully!"; return true; }
static DFBResult InitGL( Test *test ) { EGLint major, minor, nconfigs; EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1, EGL_BLUE_SIZE, 1, EGL_ALPHA_SIZE, 1, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint context_attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; EGLint surface_attrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; EGLNativeDisplayType disp = EGL_DEFAULT_DISPLAY; #define EGL_CHECK(cmd) \ /*fprintf(stderr, "CALLING %s...\n", #cmd);*/ \ if (cmd) { \ fprintf(stderr, "!!! %s failed\n", #cmd); \ goto quit; \ } // get display EGL_CHECK((display = eglGetDisplay(disp)) == EGL_NO_DISPLAY) // init EGL_CHECK(!eglInitialize(display, &major, &minor)) // get configs EGL_CHECK(!eglGetConfigs(display, configs, 2, &nconfigs)) // choose config EGL_CHECK(!eglChooseConfig(display, attribs, configs, 2, &nconfigs)) // create a surface EGL_CHECK((surface = eglCreateWindowSurface(display, configs[0], test->primary, surface_attrs)) == EGL_NO_SURFACE) EGL_CHECK(eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) // create context EGL_CHECK((context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, context_attrs)) == EGL_NO_CONTEXT) EGL_CHECK(eglMakeCurrent(display, surface, surface, context) != EGL_TRUE) eglSwapInterval( display, 1 ); /* Setup the viewport */ glViewport( 0, 0, (GLint) test->size.w, (GLint) test->size.h ); return DFB_OK; quit: return DFB_FAILURE; }
static gboolean gst_gl_context_egl_create_context (GstGLContext * context, GstGLAPI gl_api, GstGLContext * other_context, GError ** error) { GstGLContextEGL *egl; GstGLWindow *window = NULL; EGLNativeWindowType window_handle = (EGLNativeWindowType) 0; gint i = 0; EGLint context_attrib[5]; EGLint majorVersion; EGLint minorVersion; const gchar *egl_exts; gboolean need_surface = TRUE; guintptr external_gl_context = 0; GstGLDisplay *display; egl = GST_GL_CONTEXT_EGL (context); window = gst_gl_context_get_window (context); GST_DEBUG_OBJECT (context, "Creating EGL context"); if (other_context) { if (gst_gl_context_get_gl_platform (other_context) != GST_GL_PLATFORM_EGL) { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_CONFIG, "Cannot share context with non-EGL context"); goto failure; } external_gl_context = gst_gl_context_get_gl_context (other_context); } if ((gl_api & (GST_GL_API_OPENGL | GST_GL_API_GLES2)) == GST_GL_API_NONE) { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_API, "EGL supports opengl or gles2"); goto failure; } display = gst_gl_context_get_display (context); if (display->type == GST_GL_DISPLAY_TYPE_EGL) { egl->egl_display = (EGLDisplay) gst_gl_display_get_handle (display); } else { guintptr native_display = gst_gl_display_get_handle (display); if (!native_display) { GstGLWindow *window = NULL; GST_WARNING ("Failed to get a global display handle, falling back to " "per-window display handles. Context sharing may not work"); if (other_context) window = gst_gl_context_get_window (other_context); if (!window) window = gst_gl_context_get_window (context); if (window) { native_display = gst_gl_window_get_display (window); gst_object_unref (window); } } egl->egl_display = eglGetDisplay ((EGLNativeDisplayType) native_display); } gst_object_unref (display); if (eglInitialize (egl->egl_display, &majorVersion, &minorVersion)) { GST_INFO ("egl initialized, version: %d.%d", majorVersion, minorVersion); } else { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_RESOURCE_UNAVAILABLE, "Failed to initialize egl: %s", gst_gl_context_egl_get_error_string ()); goto failure; } if (gl_api & GST_GL_API_OPENGL) { /* egl + opengl only available with EGL 1.4+ */ if (majorVersion == 1 && minorVersion <= 3) { if ((gl_api & ~GST_GL_API_OPENGL) == GST_GL_API_NONE) { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_OLD_LIBS, "EGL version (%i.%i) too old for OpenGL support, (needed at least 1.4)", majorVersion, minorVersion); goto failure; } else { GST_WARNING ("EGL version (%i.%i) too old for OpenGL support, (needed at least 1.4)", majorVersion, minorVersion); if (gl_api & GST_GL_API_GLES2) { goto try_gles2; } else { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_CONFIG, "Failed to choose a suitable OpenGL API"); goto failure; } } } if (!eglBindAPI (EGL_OPENGL_API)) { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED, "Failed to bind OpenGL API: %s", gst_gl_context_egl_get_error_string ()); goto failure; } GST_INFO ("Using OpenGL"); egl->gl_api = GST_GL_API_OPENGL; } else if (gl_api & GST_GL_API_GLES2) { try_gles2: if (!eglBindAPI (EGL_OPENGL_ES_API)) { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED, "Failed to bind OpenGL|ES API: %s", gst_gl_context_egl_get_error_string ()); goto failure; } GST_INFO ("Using OpenGL|ES 2.0"); egl->gl_api = GST_GL_API_GLES2; } if (!gst_gl_context_egl_choose_config (egl, other_context, error)) { g_assert (error == NULL || *error != NULL); goto failure; } egl_exts = eglQueryString (egl->egl_display, EGL_EXTENSIONS); GST_DEBUG ("about to create gl context"); if (egl->gl_api & GST_GL_API_GLES2) { context_attrib[i++] = EGL_CONTEXT_CLIENT_VERSION; context_attrib[i++] = 2; } #if !defined(GST_DISABLE_GST_DEBUG) && defined(EGL_KHR_create_context) if (gst_gl_check_extension ("EGL_KHR_create_context", egl_exts)) { context_attrib[i++] = EGL_CONTEXT_FLAGS_KHR; context_attrib[i++] = EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR; } #endif context_attrib[i++] = EGL_NONE; egl->egl_context = eglCreateContext (egl->egl_display, egl->egl_config, (EGLContext) external_gl_context, context_attrib); #ifdef EGL_KHR_create_context if (egl->egl_context == EGL_NO_CONTEXT && egl->gl_api & GST_GL_API_GLES2 && eglGetError () != EGL_SUCCESS) { /* try without EGL_CONTEXT_FLAGS flags as it was added to * EGL_KHR_create_context for gles contexts */ int i; for (i = 0; i < G_N_ELEMENTS (context_attrib); i++) { if (context_attrib[i] == EGL_CONTEXT_FLAGS_KHR || context_attrib[i] == EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR) context_attrib[i] = EGL_NONE; } egl->egl_context = eglCreateContext (egl->egl_display, egl->egl_config, (EGLContext) external_gl_context, context_attrib); } #endif if (egl->egl_context != EGL_NO_CONTEXT) { GST_INFO ("gl context created: %" G_GUINTPTR_FORMAT, (guintptr) egl->egl_context); } else { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_CREATE_CONTEXT, "Failed to create a OpenGL context: %s", gst_gl_context_egl_get_error_string ()); goto failure; } /* FIXME do we want a window vfunc ? */ #if GST_GL_HAVE_WINDOW_X11 if (GST_GL_IS_WINDOW_X11 (context->window)) { gst_gl_window_x11_create_window ((GstGLWindowX11 *) context->window); } #endif if (other_context == NULL) { #if GST_GL_HAVE_WINDOW_WAYLAND if (GST_GL_IS_WINDOW_WAYLAND_EGL (context->window)) { gst_gl_window_wayland_egl_create_window ((GstGLWindowWaylandEGL *) context->window); } #endif #if GST_GL_HAVE_WINDOW_WIN32 if (GST_GL_IS_WINDOW_WIN32 (context->window)) { gst_gl_window_win32_create_window ((GstGLWindowWin32 *) context->window); } #endif #if GST_GL_HAVE_WINDOW_DISPMANX if (GST_GL_IS_WINDOW_DISPMANX_EGL (context->window)) { gst_gl_window_dispmanx_egl_create_window ((GstGLWindowDispmanxEGL *) context->window); } #endif } if (window) window_handle = (EGLNativeWindowType) gst_gl_window_get_window_handle (window); if (window_handle) { GST_DEBUG ("Creating EGLSurface from window_handle %p", (void *) window_handle); egl->egl_surface = eglCreateWindowSurface (egl->egl_display, egl->egl_config, window_handle, NULL); /* Store window handle for later comparision */ egl->window_handle = window_handle; } else if (!gst_gl_check_extension ("EGL_KHR_surfaceless_context", egl_exts)) { EGLint surface_attrib[7]; gint j = 0; GST_DEBUG ("Surfaceless context, creating PBufferSurface"); /* FIXME: Width/height doesn't seem to matter but we can't leave them * at 0, otherwise X11 complains about BadValue */ surface_attrib[j++] = EGL_WIDTH; surface_attrib[j++] = 1; surface_attrib[j++] = EGL_HEIGHT; surface_attrib[j++] = 1; surface_attrib[j++] = EGL_LARGEST_PBUFFER; surface_attrib[j++] = EGL_TRUE; surface_attrib[j++] = EGL_NONE; egl->egl_surface = eglCreatePbufferSurface (egl->egl_display, egl->egl_config, surface_attrib); } else { GST_DEBUG ("No surface/handle !"); egl->egl_surface = EGL_NO_SURFACE; need_surface = FALSE; } if (need_surface) { if (egl->egl_surface != EGL_NO_SURFACE) { GST_INFO ("surface created"); } else { g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED, "Failed to create window surface: %s", gst_gl_context_egl_get_error_string ()); goto failure; } } /* EGLImage functions */ if (GST_GL_CHECK_GL_VERSION (majorVersion, minorVersion, 1, 5)) { egl->eglCreateImage = gst_gl_context_get_proc_address (context, "eglCreateImage"); egl->eglDestroyImage = gst_gl_context_get_proc_address (context, "eglDestroyImage"); } else if (gst_gl_check_extension ("EGL_KHR_image_base", egl_exts)) { egl->eglCreateImage = gst_gl_context_get_proc_address (context, "eglCreateImageKHR"); egl->eglDestroyImage = gst_gl_context_get_proc_address (context, "eglDestroyImageKHR"); } if (egl->eglCreateImage == NULL || egl->eglDestroyImage == NULL) { egl->eglCreateImage = NULL; egl->eglDestroyImage = NULL; } if (window) gst_object_unref (window); return TRUE; failure: if (window) gst_object_unref (window); return FALSE; }
static bool gfx_ctx_set_video_mode(void *data, unsigned width, unsigned height, bool fullscreen) { if (g_inited) return false; int i; int ret = 0; struct drm_fb *fb = NULL; struct sigaction sa = {{0}}; sa.sa_handler = sighandler; sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); #define EGL_ATTRIBS_BASE \ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \ EGL_RED_SIZE, 1, \ EGL_GREEN_SIZE, 1, \ EGL_BLUE_SIZE, 1, \ EGL_ALPHA_SIZE, 0, \ EGL_DEPTH_SIZE, 0 static const EGLint egl_attribs_gl[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE, }; static const EGLint egl_attribs_gles[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE, }; #ifdef EGL_KHR_create_context static const EGLint egl_attribs_gles3[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, EGL_NONE, }; #endif static const EGLint egl_attribs_vg[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT, EGL_NONE, }; const EGLint *attrib_ptr; switch (g_api) { case GFX_CTX_OPENGL_API: attrib_ptr = egl_attribs_gl; break; case GFX_CTX_OPENGL_ES_API: #ifdef EGL_KHR_create_context if (g_major >= 3) attrib_ptr = egl_attribs_gles3; else #endif attrib_ptr = egl_attribs_gles; break; case GFX_CTX_OPENVG_API: attrib_ptr = egl_attribs_vg; break; default: attrib_ptr = NULL; } // Find desired video mode, and use that. // If not fullscreen, we get desired windowed size, which is not appropriate. if ((width == 0 && height == 0) || !fullscreen) g_drm_mode = &g_connector->modes[0]; else { // Try to match g_settings.video.refresh_rate as closely as possible. // Lower resolutions tend to have multiple supported refresh rates as well. float minimum_fps_diff = 0.0f; // Find best match. for (i = 0; i < g_connector->count_modes; i++) { if (width != g_connector->modes[i].hdisplay || height != g_connector->modes[i].vdisplay) continue; if (!g_drm_mode) { g_drm_mode = &g_connector->modes[i]; minimum_fps_diff = g_drm_mode->vrefresh - g_settings.video.refresh_rate; } else { float diff = g_connector->modes[i].vrefresh - g_settings.video.refresh_rate; if (diff < minimum_fps_diff) { g_drm_mode = &g_connector->modes[i]; minimum_fps_diff = diff; } } } } if (!g_drm_mode) { RARCH_ERR("[KMS/EGL]: Did not find suitable video mode for %u x %u.\n", width, height); goto error; } g_fb_width = g_drm_mode->hdisplay; g_fb_height = g_drm_mode->vdisplay; // Create GBM surface. g_gbm_surface = gbm_surface_create(g_gbm_dev, g_fb_width, g_fb_height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); if (!g_gbm_surface) { RARCH_ERR("[KMS/EGL]: Couldn't create GBM surface.\n"); goto error; } g_egl_dpy = eglGetDisplay((EGLNativeDisplayType)g_gbm_dev); if (!g_egl_dpy) { RARCH_ERR("[KMS/EGL]: Couldn't get EGL display.\n"); goto error; } EGLint major, minor; if (!eglInitialize(g_egl_dpy, &major, &minor)) goto error; EGLint n; if (!eglChooseConfig(g_egl_dpy, attrib_ptr, &g_config, 1, &n) || n != 1) goto error; EGLint egl_attribs[16]; EGLint *attr = egl_attribs; attr = egl_fill_attribs(attr); g_egl_ctx = eglCreateContext(g_egl_dpy, g_config, EGL_NO_CONTEXT, attr != egl_attribs ? egl_attribs : NULL); if (g_egl_ctx == EGL_NO_CONTEXT) goto error; if (g_use_hw_ctx) { g_egl_hw_ctx = eglCreateContext(g_egl_dpy, g_config, g_egl_ctx, attr != egl_attribs ? egl_attribs : NULL); RARCH_LOG("[KMS/EGL]: Created shared context: %p.\n", (void*)g_egl_hw_ctx); if (g_egl_hw_ctx == EGL_NO_CONTEXT) goto error; } g_egl_surf = eglCreateWindowSurface(g_egl_dpy, g_config, (EGLNativeWindowType)g_gbm_surface, NULL); if (!g_egl_surf) goto error; if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx)) goto error; glClear(GL_COLOR_BUFFER_BIT); eglSwapBuffers(g_egl_dpy, g_egl_surf); g_bo = gbm_surface_lock_front_buffer(g_gbm_surface); fb = drm_fb_get_from_bo(g_bo); ret = drmModeSetCrtc(g_drm_fd, g_crtc_id, fb->fb_id, 0, 0, &g_connector_id, 1, g_drm_mode); if (ret < 0) goto error; g_inited = true; return true; error: gfx_ctx_destroy(data); return false; }
int ogl_init_window(int x, int y) { int use_x,use_y,use_bpp; Uint32 use_flags; #ifdef OGLES SDL_SysWMinfo info; Window x11Window = 0; Display* x11Display = 0; EGLint ver_maj, ver_min; EGLint configAttribs[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_DEPTH_SIZE, 16, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT, EGL_NONE, EGL_NONE }; // explicitely request an OpenGL ES 1.x context EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE, EGL_NONE }; // explicitely request a doublebuffering window EGLint winAttribs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE, EGL_NONE }; int iConfigs; #endif // OGLES if (gl_initialized) ogl_smash_texture_list_internal();//if we are or were fullscreen, changing vid mode will invalidate current textures SDL_WM_SetCaption(DESCENT_VERSION, "Descent II"); SDL_WM_SetIcon( SDL_LoadBMP( "d2x-rebirth.bmp" ), NULL ); use_x=x; use_y=y; use_bpp=GameArg.DbgBpp; use_flags=sdl_video_flags; if (sdl_no_modeswitch) { const SDL_VideoInfo *vinfo=SDL_GetVideoInfo(); if (vinfo) { use_x=vinfo->current_w; use_y=vinfo->current_h; use_bpp=vinfo->vfmt->BitsPerPixel; use_flags=SDL_SWSURFACE | SDL_ANYFORMAT; } else { con_printf(CON_URGENT, "Could not query video info\n"); } } if (!SDL_SetVideoMode(use_x, use_y, use_bpp, use_flags)) { #ifdef RPI con_printf(CON_URGENT, "Could not set %dx%dx%d opengl video mode: %s\n (Ignored for RPI)", x, y, GameArg.DbgBpp, SDL_GetError()); #else Error("Could not set %dx%dx%d opengl video mode: %s\n", x, y, GameArg.DbgBpp, SDL_GetError()); #endif } #ifdef OGLES #ifndef RPI // NOTE: on the RPi, the EGL stuff is not connected to the X11 window, // so there is no need to destroy and recreate this ogles_destroy(); #endif SDL_VERSION(&info.version); if (SDL_GetWMInfo(&info) > 0) { if (info.subsystem == SDL_SYSWM_X11) { x11Display = info.info.x11.display; x11Window = info.info.x11.window; con_printf (CON_DEBUG, "Display: %p, Window: %i ===\n", (void*)x11Display, (int)x11Window); } } if (eglDisplay == EGL_NO_DISPLAY) { #ifdef RPI eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY); #else eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display); #endif if (eglDisplay == EGL_NO_DISPLAY) { con_printf(CON_URGENT, "EGL: Error querying EGL Display\n"); } if (!eglInitialize(eglDisplay, &ver_maj, &ver_min)) { con_printf(CON_URGENT, "EGL: Error initializing EGL\n"); } else { con_printf(CON_DEBUG, "EGL: Initialized, version: major %i minor %i\n", ver_maj, ver_min); } } #ifdef RPI if (rpi_setup_element(x,y,sdl_video_flags,1)) { Error("RPi: Could not set up a %dx%d element\n", x, y); } #endif if (eglSurface == EGL_NO_SURFACE) { if (!eglChooseConfig(eglDisplay, configAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1)) { con_printf(CON_URGENT, "EGL: Error choosing config\n"); } else { con_printf(CON_DEBUG, "EGL: config chosen\n"); } #ifdef RPI eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)&nativewindow, winAttribs); #else eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (NativeWindowType)x11Window, winAttribs); #endif if ((!TestEGLError("eglCreateWindowSurface")) || eglSurface == EGL_NO_SURFACE) { con_printf(CON_URGENT, "EGL: Error creating window surface\n"); } else { con_printf(CON_DEBUG, "EGL: Created window surface\n"); } } if (eglContext == EGL_NO_CONTEXT) { eglContext = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, contextAttribs); if ((!TestEGLError("eglCreateContext")) || eglContext == EGL_NO_CONTEXT) { con_printf(CON_URGENT, "EGL: Error creating context\n"); } else { con_printf(CON_DEBUG, "EGL: Created context\n"); } } eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); if (!TestEGLError("eglMakeCurrent")) { con_printf(CON_URGENT, "EGL: Error making current\n"); } else { con_printf(CON_DEBUG, "EGL: made context current\n"); } #endif linedotscale = ((x/640<y/480?x/640:y/480)<1?1:(x/640<y/480?x/640:y/480)); gl_initialized=1; return 0; }
SDL_GLContext PSP_GL_CreateContext(_THIS, SDL_Window * window) { SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata; EGLint attribs[32]; EGLDisplay display; EGLContext context; EGLSurface surface; EGLConfig config; EGLint num_configs; int i; /* EGL init taken from glutCreateWindow() in PSPGL's glut.c. */ EGLCHK(display = eglGetDisplay(0)); EGLCHK(eglInitialize(display, NULL, NULL)); wdata->uses_gles = SDL_TRUE; window->flags |= SDL_WINDOW_FULLSCREEN; /* Setup the config based on SDL's current values. */ i = 0; attribs[i++] = EGL_RED_SIZE; attribs[i++] = _this->gl_config.red_size; attribs[i++] = EGL_GREEN_SIZE; attribs[i++] = _this->gl_config.green_size; attribs[i++] = EGL_BLUE_SIZE; attribs[i++] = _this->gl_config.blue_size; attribs[i++] = EGL_DEPTH_SIZE; attribs[i++] = _this->gl_config.depth_size; if (_this->gl_config.alpha_size) { attribs[i++] = EGL_ALPHA_SIZE; attribs[i++] = _this->gl_config.alpha_size; } if (_this->gl_config.stencil_size) { attribs[i++] = EGL_STENCIL_SIZE; attribs[i++] = _this->gl_config.stencil_size; } attribs[i++] = EGL_NONE; EGLCHK(eglChooseConfig(display, attribs, &config, 1, &num_configs)); if (num_configs == 0) { SDL_SetError("No valid EGL configs for requested mode"); return 0; } EGLCHK(eglGetConfigAttrib(display, config, EGL_WIDTH, &width)); EGLCHK(eglGetConfigAttrib(display, config, EGL_HEIGHT, &height)); EGLCHK(context = eglCreateContext(display, config, NULL, NULL)); EGLCHK(surface = eglCreateWindowSurface(display, config, 0, NULL)); EGLCHK(eglMakeCurrent(display, surface, surface, context)); _this->gl_data->display = display; _this->gl_data->context = context; _this->gl_data->surface = surface; return context; }
int main(int argc, char** argv) { gf_3d_target_t target; gf_display_t gf_disp; EGLConfig config; EGLContext econtext; EGLint num_config; gf_dev_info_t info; gf_layer_info_t linfo; gf_display_info_t disp_info; GLuint width, height; GLuint it; /* initialize the graphics device */ if (gf_dev_attach(&gfdev, NULL, &info)!=GF_ERR_OK) { perror("gf_dev_attach()"); return -1; } /* Setup the layer we will use */ if (gf_display_attach(&gf_disp, gfdev, 0, &disp_info)!=GF_ERR_OK) { fprintf(stderr, "gf_display_attach() failed\n"); return -1; } width=disp_info.xres; height=disp_info.yres; layer_idx=disp_info.main_layer_index; /* get an EGL display connection */ display=eglGetDisplay(gfdev); if (display==EGL_NO_DISPLAY) { fprintf(stderr, "eglGetDisplay() failed\n"); return -1; } if (gf_layer_attach(&layer, gf_disp, layer_idx, 0)!=GF_ERR_OK) { fprintf(stderr, "gf_layer_attach() failed\n"); return -1; } /* initialize the EGL display connection */ if (eglInitialize(display, NULL, NULL)!=EGL_TRUE) { fprintf(stderr, "eglInitialize: error 0x%x\n", eglGetError()); return -1; } for (it=0;; it++) { /* Walk through all possible pixel formats for this layer */ if (gf_layer_query(layer, it, &linfo)==-1) { fprintf(stderr, "Couldn't find a compatible frame " "buffer configuration on layer %d\n", layer_idx); return -1; } /* * We want the color buffer format to match the layer format, * so request the layer format through EGL_NATIVE_VISUAL_ID. */ attribute_list[1]=linfo.format; /* Look for a compatible EGL frame buffer configuration */ if (eglChooseConfig(display, attribute_list, &config, 1, &num_config)==EGL_TRUE) { if (num_config>0) { break; } } } /* create a 3D rendering target */ if (gf_3d_target_create(&target, layer, NULL, 0, width, height, linfo.format)!=GF_ERR_OK) { fprintf(stderr, "Unable to create rendering target\n"); return -1; } gf_layer_set_src_viewport(layer, 0, 0, width-1, height-1); gf_layer_set_dst_viewport(layer, 0, 0, width-1, height-1); gf_layer_enable(layer); /* * The layer settings haven't taken effect yet since we haven't * called gf_layer_update() yet. This is exactly what we want, * since we haven't supplied a valid surface to display yet. * Later, the OpenGL ES library calls will call gf_layer_update() * internally, when displaying the rendered 3D content. */ /* create an EGL rendering context */ econtext=eglCreateContext(display, config, EGL_NO_CONTEXT, NULL); if (econtext==EGL_NO_CONTEXT) { fprintf(stderr, "Create context failed: 0x%x\n", eglGetError()); return -1; } /* create an EGL window surface */ surface=eglCreateWindowSurface(display, config, target, NULL); if (surface==EGL_NO_SURFACE) { fprintf(stderr, "Create surface failed: 0x%x\n", eglGetError()); return -1; } /* connect the context to the surface */ if (eglMakeCurrent(display, surface, surface, econtext)==EGL_FALSE) { fprintf(stderr, "Make current failed: 0x%x\n", eglGetError()); return -1; } init_scene(width, height); do { render_scene(); glFinish(); eglWaitGL(); eglSwapBuffers(display, surface); } while(1); return 0; }
bool CCEGLView::initGL() { EGLint eglConfigCount; EGLConfig config; // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Get the EGL display and initialize. m_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); return false; } if (eglInitialize(m_eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); return false; } if (eglChooseConfig(m_eglDisplay, eglConfigAttrs, &config, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); return false; } m_eglContext = eglCreateContext(m_eglDisplay, config, EGL_NO_CONTEXT, eglContextAttrs); if (m_eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); return false; } m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, NULL, eglSurfaceAttrs); if (m_eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); return false; } if (eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); return false; } // FIXME: Get the actual canvas size somehow. EGLint width; EGLint height; if ((m_eglDisplay == EGL_NO_DISPLAY) || (m_eglSurface == EGL_NO_SURFACE) ) return EXIT_FAILURE; eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width); eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height); m_obScreenSize.width = width; m_obScreenSize.height = height; glViewport(0, 0, width, height); // Default the frame size to be the whole canvas. In general we want to be // setting the size of the viewport by adjusting the canvas size (so // there's no weird letterboxing). setFrameSize(width, height); return true; }
void gp2x_set_video_mode(struct osd_bitmap *bitmap, int bpp,int width,int height) { int ret; uint32_t display_width, display_height; uint32_t display_x=0, display_y=0; float display_ratio,game_ratio; VC_RECT_T dst_rect; VC_RECT_T src_rect; surface_width = width; surface_height = height; gp2x_screen8=(unsigned char *) calloc(1, width*height); gp2x_screen15=(unsigned short *) calloc(1, width*height*2); // get an EGL display connection display = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(display != EGL_NO_DISPLAY); // initialize the EGL display connection EGLBoolean result = eglInitialize(display, NULL, NULL); assert(EGL_FALSE != result); // get an appropriate EGL frame buffer configuration EGLint num_config; EGLConfig config; static const EGLint attribute_list[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; result = eglChooseConfig(display, attribute_list, &config, 1, &num_config); assert(EGL_FALSE != result); result = eglBindAPI(EGL_OPENGL_ES_API); assert(EGL_FALSE != result); // create an EGL rendering context static const EGLint context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes); assert(context != EGL_NO_CONTEXT); // create an EGL window surface int32_t success = graphics_get_display_size(0, &display_width, &display_height); assert(success >= 0); display_adj_width = display_width - (options.display_border * 2); display_adj_height = display_height - (options.display_border * 2); if (options.display_smooth_stretch) { //We use the dispmanx scaler to smooth stretch the display //so GLES2 doesn't have to handle the performance intensive postprocessing uint32_t sx, sy; // Work out the position and size on the display display_ratio = (float)display_width/(float)display_height; game_ratio = (float)width/(float)height*options.pixel_aspect_ratio; display_x = sx = display_adj_width; display_y = sy = display_adj_height; float ratio_diff = game_ratio / display_ratio; if (ratio_diff < 1.0) ratio_diff = 1.0 / ratio_diff; // we only adjust for game ratio if more then % diff if (ratio_diff > options.ratio_threshold) { if (game_ratio>display_ratio) sy = (float)display_adj_width/(float)game_ratio; else sx = (float)display_adj_height*(float)game_ratio; } // Centre bitmap on screen display_x = (display_x - sx) / 2; display_y = (display_y - sy) / 2; vc_dispmanx_rect_set( &dst_rect, display_x + options.display_border, display_y + options.display_border, sx, sy); } else vc_dispmanx_rect_set( &dst_rect, options.display_border, options.display_border, display_adj_width, display_adj_height); if (options.display_smooth_stretch) vc_dispmanx_rect_set( &src_rect, 0, 0, width << 16, height << 16); else vc_dispmanx_rect_set( &src_rect, 0, 0, display_adj_width << 16, display_adj_height << 16); dispman_display = vc_dispmanx_display_open(0); dispman_update = vc_dispmanx_update_start(0); dispman_element = vc_dispmanx_element_add(dispman_update, dispman_display, 10, &dst_rect, 0, &src_rect, DISPMANX_PROTECTION_NONE, NULL, NULL, DISPMANX_NO_ROTATE); //Black background surface dimensions vc_dispmanx_rect_set( &dst_rect, 0, 0, display_width, display_height ); vc_dispmanx_rect_set( &src_rect, 0, 0, 128 << 16, 128 << 16); //Create a blank background for the whole screen, make sure width is divisible by 32! uint32_t crap; resource_bg = vc_dispmanx_resource_create(VC_IMAGE_RGB565, 128, 128, &crap); dispman_element_bg = vc_dispmanx_element_add( dispman_update, dispman_display, 9, &dst_rect, resource_bg, &src_rect, DISPMANX_PROTECTION_NONE, 0, 0, (DISPMANX_TRANSFORM_T) 0 ); nativewindow.element = dispman_element; if (options.display_smooth_stretch) { nativewindow.width = width; nativewindow.height = height; } else { nativewindow.width = display_adj_width; nativewindow.height = display_adj_height; } vc_dispmanx_update_submit_sync(dispman_update); surface = eglCreateWindowSurface(display, config, &nativewindow, NULL); assert(surface != EGL_NO_SURFACE); // connect the context to the surface result = eglMakeCurrent(display, surface, surface, context); assert(EGL_FALSE != result); //Smooth stretch the display size for GLES2 is the size of the bitmap //otherwise let GLES2 upscale (NEAR) to the size of the display if (options.display_smooth_stretch) gles2_create(width, height, width, height, bitmap->depth); else gles2_create(display_adj_width, display_adj_height, width, height, bitmap->depth); }
static bool gfx_ctx_init(void) { const EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_NONE }; EGLint num_config; EGLint egl_version_major, egl_version_minor; int format = SCREEN_FORMAT_RGBX8888; EGLint context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; int usage; usage = SCREEN_USAGE_OPENGL_ES2 | SCREEN_USAGE_ROTATION; RARCH_LOG("Initializing context\n"); if ((g_egl_dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY) { RARCH_ERR("eglGetDisplay failed.\n"); goto error; } if (!eglInitialize(g_egl_dpy, &egl_version_major, &egl_version_minor)) { RARCH_ERR("eglInitialize failed.\n"); goto error; } if (!eglBindAPI(EGL_OPENGL_ES_API)) { RARCH_ERR("eglBindAPI failed.\n"); goto error; } RARCH_LOG("[BLACKBERRY QNX/EGL]: EGL version: %d.%d\n", egl_version_major, egl_version_minor); if (!eglChooseConfig(g_egl_dpy, attribs, &egl_config, 1, &num_config)) { RARCH_ERR("eglChooseConfig failed.\n"); goto error; } if ((g_egl_ctx = eglCreateContext(g_egl_dpy, egl_config, 0, context_attributes)) == EGL_NO_CONTEXT) { RARCH_ERR("eglCreateContext failed.\n"); goto error; } if (screen_create_window(&screen_win, screen_ctx)) { RARCH_ERR("screen_create_window failed:.\n"); goto error; } if (screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_FORMAT, &format)) { RARCH_ERR("screen_set_window_property_iv [SCREEN_PROPERTY_FORMAT] failed.\n"); goto error; } if (screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_USAGE, &usage)) { RARCH_ERR("screen_set_window_property_iv [SCREEN_PROPERTY_USAGE] failed.\n"); goto error; } if (screen_get_window_property_pv(screen_win, SCREEN_PROPERTY_DISPLAY, (void **)&screen_disp)) { RARCH_ERR("screen_get_window_property_pv [SCREEN_PROPERTY_DISPLAY] failed.\n"); goto error; } int screen_resolution[2]; if (screen_get_display_property_iv(screen_disp, SCREEN_PROPERTY_SIZE, screen_resolution)) { RARCH_ERR("screen_get_window_property_iv [SCREEN_PROPERTY_SIZE] failed.\n"); goto error; } int angle = atoi(getenv("ORIENTATION")); screen_display_mode_t screen_mode; if (screen_get_display_property_pv(screen_disp, SCREEN_PROPERTY_MODE, (void**)&screen_mode)) { RARCH_ERR("screen_get_display_property_pv [SCREEN_PROPERTY_MODE] failed.\n"); goto error; } int size[2]; if (screen_get_window_property_iv(screen_win, SCREEN_PROPERTY_BUFFER_SIZE, size)) { RARCH_ERR("screen_get_window_property_iv [SCREEN_PROPERTY_BUFFER_SIZE] failed.\n"); goto error; } int buffer_size[2] = {size[0], size[1]}; if ((angle == 0) || (angle == 180)) { if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] > size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else if ((angle == 90) || (angle == 270)){ if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) || ((screen_mode.width < screen_mode.height && size[0] < size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else { RARCH_ERR("Navigator returned an unexpected orientation angle.\n"); goto error; } if (screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_BUFFER_SIZE, buffer_size)) { RARCH_ERR("screen_set_window_property_iv [SCREEN_PROPERTY_BUFFER_SIZE] failed.\n"); goto error; } if (screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_ROTATION, &angle)) { RARCH_ERR("screen_set_window_property_iv [SCREEN_PROPERTY_ROTATION] failed.\n"); goto error; } if (screen_create_window_buffers(screen_win, WINDOW_BUFFERS)) { RARCH_ERR("screen_create_window_buffers failed.\n"); goto error; } if (!(g_egl_surf = eglCreateWindowSurface(g_egl_dpy, egl_config, screen_win, 0))) { RARCH_ERR("eglCreateWindowSurface failed.\n"); goto error; } if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx)) { RARCH_ERR("eglMakeCurrent failed.\n"); goto error; } return true; error: RARCH_ERR("EGL error: %d.\n", eglGetError()); gfx_ctx_destroy(); return false; }
Outbuf * eng_window_new(Evas_Engine_Info_Wayland *einfo, int w, int h, Render_Engine_Swap_Mode swap_mode) { Outbuf *gw; int context_attrs[3]; int config_attrs[40]; int major_version, minor_version; int num_config, n = 0; const GLubyte *vendor, *renderer, *version; Eina_Bool blacklist = EINA_FALSE; /* try to allocate space for our window */ if (!(gw = calloc(1, sizeof(Outbuf)))) return NULL; win_count++; gw->info = einfo; gw->w = w; gw->h = h; gw->swap_mode = swap_mode; gw->disp = einfo->info.wl_display; if (display && (display != gw->disp)) context = EGL_NO_CONTEXT; display = gw->disp; gw->surface = einfo->info.wl_surface; gw->depth = einfo->info.depth; gw->alpha = einfo->info.destination_alpha; gw->rot = einfo->info.rotation; context_attrs[0] = EGL_CONTEXT_CLIENT_VERSION; context_attrs[1] = 2; context_attrs[2] = EGL_NONE; config_attrs[n++] = EGL_SURFACE_TYPE; config_attrs[n++] = EGL_WINDOW_BIT; config_attrs[n++] = EGL_RENDERABLE_TYPE; config_attrs[n++] = EGL_OPENGL_ES2_BIT; # if 0 /* FIXME: n900 - omap3 sgx libs break here */ config_attrs[n++] = EGL_RED_SIZE; config_attrs[n++] = 1; config_attrs[n++] = EGL_GREEN_SIZE; config_attrs[n++] = 1; config_attrs[n++] = EGL_BLUE_SIZE; config_attrs[n++] = 1; /* FIXME: end n900 breakage */ # endif config_attrs[n++] = EGL_ALPHA_SIZE; config_attrs[n++] = gw->alpha; config_attrs[n++] = EGL_DEPTH_SIZE; config_attrs[n++] = 0; config_attrs[n++] = EGL_STENCIL_SIZE; config_attrs[n++] = 0; config_attrs[n++] = EGL_NONE; /* FIXME: Remove this line as soon as eglGetDisplay() autodetection * gets fixed. Currently it is incorrectly detecting wl_display and * returning _EGL_PLATFORM_X11 instead of _EGL_PLATFORM_WAYLAND. * * See ticket #1972 for more info. */ setenv("EGL_PLATFORM", "wayland", 1); gw->egl_disp = eglGetDisplay((EGLNativeDisplayType)gw->disp); if (!gw->egl_disp) { ERR("eglGetDisplay() fail. code=%#x", eglGetError()); eng_window_free(gw); return NULL; } if (!eglInitialize(gw->egl_disp, &major_version, &minor_version)) { ERR("eglInitialize() fail. code=%#x", eglGetError()); eng_window_free(gw); return NULL; } if (!eglBindAPI(EGL_OPENGL_ES_API)) { ERR("eglBindAPI() fail. code=%#x", eglGetError()); eng_window_free(gw); return NULL; } num_config = 0; if (!eglChooseConfig(gw->egl_disp, config_attrs, &gw->egl_config, 1, &num_config) || (num_config != 1)) { ERR("eglChooseConfig() fail. code=%#x", eglGetError()); eng_window_free(gw); return NULL; } if ((gw->rot == 0) || (gw->rot == 180)) gw->win = wl_egl_window_create(gw->surface, gw->w, gw->h); else if ((gw->rot == 90) || (gw->rot == 270)) gw->win = wl_egl_window_create(gw->surface, gw->h, gw->w); gw->egl_surface = eglCreateWindowSurface(gw->egl_disp, gw->egl_config, (EGLNativeWindowType)gw->win, NULL); if (gw->egl_surface == EGL_NO_SURFACE) { ERR("eglCreateWindowSurface() fail for %p. code=%#x", gw->win, eglGetError()); eng_window_free(gw); return NULL; } gw->egl_context = eglCreateContext(gw->egl_disp, gw->egl_config, context, context_attrs); if (gw->egl_context == EGL_NO_CONTEXT) { ERR("eglCreateContext() fail. code=%#x", eglGetError()); eng_window_free(gw); return NULL; } if (context == EGL_NO_CONTEXT) context = gw->egl_context; if (eglMakeCurrent(gw->egl_disp, gw->egl_surface, gw->egl_surface, gw->egl_context) == EGL_FALSE) { ERR("eglMakeCurrent() fail. code=%#x", eglGetError()); eng_window_free(gw); return NULL; } vendor = glGetString(GL_VENDOR); renderer = glGetString(GL_RENDERER); version = glGetString(GL_VERSION); if (!vendor) vendor = (unsigned char *)"-UNKNOWN-"; if (!renderer) renderer = (unsigned char *)"-UNKNOWN-"; if (!version) version = (unsigned char *)"-UNKNOWN-"; if (getenv("EVAS_GL_INFO")) { fprintf(stderr, "vendor: %s\n", vendor); fprintf(stderr, "renderer: %s\n", renderer); fprintf(stderr, "version: %s\n", version); } if (strstr((const char *)vendor, "Mesa Project")) { if (strstr((const char *)renderer, "Software Rasterizer")) blacklist = EINA_TRUE; } if (strstr((const char *)renderer, "softpipe")) blacklist = EINA_TRUE; if (strstr((const char *)renderer, "llvmpipe")) blacklist = EINA_TRUE; if ((blacklist) && (!getenv("EVAS_GL_NO_BLACKLIST"))) { ERR("OpenGL Driver blacklisted:"); ERR("Vendor: %s", (const char *)vendor); ERR("Renderer: %s", (const char *)renderer); ERR("Version: %s", (const char *)version); eng_window_free(gw); return NULL; } if (!(gw->gl_context = glsym_evas_gl_common_context_new())) { eng_window_free(gw); return NULL; } gw->gl_context->egldisp = gw->egl_disp; gw->gl_context->eglctxt = gw->egl_context; eng_window_use(gw); glsym_evas_gl_common_context_resize(gw->gl_context, w, h, gw->rot); gw->surf = EINA_TRUE; return gw; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { int i; // Windows variables HWND hWnd=0; HDC hDC=0; // EGL variables EGLDisplay eglDisplay=0; EGLConfig eglConfig=0; EGLSurface eglSurface=0; EGLContext eglContext=0; NativeWindowType eglWindow=0; EGLint pi32ConfigAttribs[128]; unsigned int nWidth; unsigned int nHeight; ATOM registerClass; RECT sRect; EGLint iMajorVersion, iMinorVersion; int iConfigs; /* Step 0 - Create a NativeWindowType that we can use for OpenGL ES output */ // Register the windows class WNDCLASS sWC; sWC.style=CS_HREDRAW | CS_VREDRAW; sWC.lpfnWndProc=WndProc; sWC.cbClsExtra=0; sWC.cbWndExtra=0; sWC.hInstance=hInstance; sWC.hIcon=0; sWC.hCursor=0; sWC.lpszMenuName=0; sWC.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); sWC.lpszClassName=WINDOW_CLASS; nWidth=WINDOW_WIDTH; nHeight=WINDOW_HEIGHT; registerClass=RegisterClass(&sWC); if (!registerClass) { MessageBox(0, _T("Failed to register the window class"), _T("Error"), MB_OK | MB_ICONEXCLAMATION); } // Create the eglWindow SetRect(&sRect, 0, 0, nWidth, nHeight); AdjustWindowRectEx(&sRect, WS_CAPTION | WS_SYSMENU, 0, 0); hWnd=CreateWindow(WINDOW_CLASS, _T("GLU ES Sphere test"), WS_VISIBLE | WS_SYSMENU, 0, 0, nWidth, nHeight, NULL, NULL, hInstance, NULL); eglWindow=hWnd; // Get the associated device context hDC=GetDC(hWnd); if (!hDC) { MessageBox(0, _T("Failed to create the device context"), _T("Error"), MB_OK|MB_ICONEXCLAMATION); goto cleanup; } /* Step 1 - Get the default display. EGL uses the concept of a "display" which in most environments corresponds to a single physical screen. Since we usually want to draw to the main screen or only have a single screen to begin with, we let EGL pick the default display. Querying other displays is platform specific. */ eglDisplay=eglGetDisplay((NativeDisplayType)hDC); if(eglDisplay==EGL_NO_DISPLAY) { eglDisplay=eglGetDisplay((NativeDisplayType)EGL_DEFAULT_DISPLAY); } /* Step 2 - Initialize EGL. EGL has to be initialized with the display obtained in the previous step. We cannot use other EGL functions except eglGetDisplay and eglGetError before eglInitialize has been called. If we're not interested in the EGL version number we can just pass NULL for the second and third parameters. */ if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion)) { MessageBox(0, _T("eglInitialize() failed."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); goto cleanup; } /* Step 3 - Specify the required configuration attributes. An EGL "configuration" describes the pixel format and type of surfaces that can be used for drawing. For now we just want to use a 16 bit RGB surface that is a Window surface, i.e. it will be visible on screen. The list has to contain key/value pairs, terminated with EGL_NONE. */ i=0; pi32ConfigAttribs[i++]=EGL_RED_SIZE; pi32ConfigAttribs[i++]=5; pi32ConfigAttribs[i++]=EGL_GREEN_SIZE; pi32ConfigAttribs[i++]=6; pi32ConfigAttribs[i++]=EGL_BLUE_SIZE; pi32ConfigAttribs[i++]=5; pi32ConfigAttribs[i++]=EGL_ALPHA_SIZE; pi32ConfigAttribs[i++]=0; pi32ConfigAttribs[i++]=EGL_DEPTH_SIZE; pi32ConfigAttribs[i++]=16; pi32ConfigAttribs[i++]=EGL_SURFACE_TYPE; pi32ConfigAttribs[i++]=EGL_WINDOW_BIT; pi32ConfigAttribs[i++]=EGL_NONE; /* Step 4 - Find a config that matches all requirements. eglChooseConfig provides a list of all available configurations that meet or exceed the requirements given as the second argument. In most cases we just want the first config that meets all criteria, so we can limit the number of configs returned to 1. */ if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1)) { MessageBox(0, _T("eglChooseConfig() failed."), _T("Error"), MB_OK|MB_ICONEXCLAMATION); goto cleanup; } /* Step 5 - Create a surface to draw to. Use the config picked in the previous step and the native window handle when available to create a window surface. A window surface is one that will be visible on screen inside the native display (or fullscreen if there is no windowing system). Pixmaps and pbuffers are surfaces which only exist in off-screen memory. */ eglSurface=eglCreateWindowSurface(eglDisplay, eglConfig, eglWindow, NULL); if (eglSurface==EGL_NO_SURFACE) { eglGetError(); // Clear error eglSurface=eglCreateWindowSurface(eglDisplay, eglConfig, NULL, NULL); } if (!TestEGLError(hWnd, "eglCreateWindowSurface")) { goto cleanup; } /* Step 6 - Create a context. EGL has to create a context for OpenGL ES. Our OpenGL ES resources like textures will only be valid inside this context (or shared contexts) */ eglContext=eglCreateContext(eglDisplay, eglConfig, NULL, NULL); if (!TestEGLError(hWnd, "eglCreateContext")) { goto cleanup; } /* Step 7 - Bind the context to the current thread and use our window surface for drawing and reading. Contexts are bound to a thread. This means you don't have to worry about other threads and processes interfering with your OpenGL ES application. We need to specify a surface that will be the target of all subsequent drawing operations, and one that will be the source of read operations. They can be the same surface. */ eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); if (!TestEGLError(hWnd, "eglMakeCurrent")) { goto cleanup; } /* Initialize scene */ init_scene(WINDOW_WIDTH, WINDOW_HEIGHT); /* Render stuff */ do { MSG msg; render_scene(); glFinish(); eglWaitGL(); eglSwapBuffers(eglDisplay, eglSurface); if (done) { break; } PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE); TranslateMessage(&msg); DispatchMessage(&msg); } while(1); /* Step 8 - Terminate OpenGL ES and destroy the window (if present). eglTerminate takes care of destroying any context or surface created with this display, so we don't need to call eglDestroySurface or eglDestroyContext here. */ cleanup: eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglTerminate(eglDisplay); /* Step 9 - Destroy the eglWindow. Again, this is platform specific and delegated to a separate function. */ // Release the device context if (hDC) { ReleaseDC(hWnd, hDC); } // Destroy the eglWindow if (hWnd) { DestroyWindow(hWnd); } return 0; }
bool InitOGLES() { EGLConfig configs[10]; EGLConfig config; EGLint matchingConfigs; #if USE_16BPP // 16BPP: RGB565 const EGLint configAttribs[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_ALPHA_SIZE, EGL_DONT_CARE, EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, EGL_DONT_CARE, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE, EGL_NONE }; #else // 24BPP: RGB888 const EGLint configAttribs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, EGL_DONT_CARE, EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, EGL_DONT_CARE, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE, EGL_NONE }; #endif EGLint aEGLContextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; // Get EGL display hDC = GetWindowDC(hWnd); glesDisplay = eglGetDisplay(hDC); // Initialize EGL if (!eglInitialize(glesDisplay, NULL, NULL)) { return false; } // Choose EGL config if (!eglChooseConfig(glesDisplay, configAttribs, &configs[0], 10, &matchingConfigs)) { return false; } if (matchingConfigs < 1) { return false; } // Choose EGL config #if USE_16BPP for ( int i=0; i < matchingConfigs; i++ ) { EGLint value; /* Use this to explicitly check that the EGL config has the expected color depths */ eglGetConfigAttrib( glesDisplay, configs[i], EGL_RED_SIZE, &value ); if ( 5 != value ){ continue; } eglGetConfigAttrib( glesDisplay, configs[i], EGL_GREEN_SIZE, &value ); if ( 6 != value ){ continue; } eglGetConfigAttrib( glesDisplay, configs[i], EGL_BLUE_SIZE, &value ); if ( 5 != value ){ continue; } eglGetConfigAttrib( glesDisplay, configs[i], EGL_ALPHA_SIZE, &value ); if ( 0 != value ){ continue; } eglGetConfigAttrib( glesDisplay, configs[i], EGL_SAMPLES, &value ); if ( 4 != value ){ continue; } config = configs[i]; break; } #else config = configs[0]; #endif // Create EGL window surface glesSurface = eglCreateWindowSurface(glesDisplay, config, hWnd, NULL ); if (!glesSurface) { return false; } // Create EGL rendering context glesContext = eglCreateContext(glesDisplay, config, 0, aEGLContextAttributes); // Attach the EGL rendering context to EGL surfaces eglMakeCurrent(glesDisplay, glesSurface, glesSurface, glesContext); return TRUE; }
bool CCEGLView::initGL() { int rc = 0; int screenFormat = SCREEN_FORMAT_RGBA8888; #ifdef __X86__ int screenUsage = SCREEN_USAGE_OPENGL_ES2; #else int screenUsage = SCREEN_USAGE_DISPLAY|SCREEN_USAGE_OPENGL_ES2; // Physical device copy directly into physical display #endif // int screenSwapInterval = WINDOW_VSYNC ? 1 : 0; int screenTransparency = SCREEN_TRANSPARENCY_NONE; int angle = atoi(getenv("ORIENTATION")); // Hard-coded to (0,0). int windowPosition[] = { 0, 0 }; EGLint eglConfigCount; EGLConfig config; // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Create the screen context. rc = screen_create_context(&m_screenContext, 0); if (rc) { perror("screen_create_context"); return false; } // Create the screen window. rc = screen_create_window(&m_screenWindow, m_screenContext); if (rc) { perror("screen_create_window"); return false; } // Set/get any window prooperties. rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_FORMAT, &screenFormat); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)"); return false; } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_USAGE, &screenUsage); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)"); return false; } char *width_str = getenv("WIDTH"); char *height_str = getenv("HEIGHT"); if (width_str && height_str) { int screen_res[2]; screen_res[0] = atoi(width_str); screen_res[1] = atoi(height_str); int rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, screen_res); if (rc) { fprintf(stderr, "screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); return false; } } else { screen_display_t screen_display; rc = screen_get_window_property_pv(m_screenWindow, SCREEN_PROPERTY_DISPLAY, (void **)&screen_display); if (rc) { perror("screen_get_window_property_pv(SCREEN_PROPERTY_DISPLAY)"); return false; } screen_display_mode_t screen_mode; rc = screen_get_display_property_pv(screen_display, SCREEN_PROPERTY_MODE, (void**)&screen_mode); if (rc) { perror("screen_get_display_property_pv(SCREEN_PROPERTY_MODE)"); return false; } int size[2]; rc = screen_get_window_property_iv(m_screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, size); if (rc) { perror("screen_get_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); return false; } int buffer_size[2] = { size[0], size[1] }; if ((angle == 0) || (angle == 180)) { if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] > size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else if ((angle == 90) || (angle == 270)) { if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] < size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else { perror("Navigator returned an unexpected orientation angle."); return false; } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_ROTATION, &angle); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)"); return false; } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, buffer_size); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); return false; } } if (windowPosition[0] != 0 || windowPosition[1] != 0) { rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_POSITION, windowPosition); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)"); return false; } } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_TRANSPARENCY, &screenTransparency); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_TRANSPARENCY)"); return false; } // Double buffered. rc = screen_create_window_buffers(m_screenWindow, 2); if (rc) { perror("screen_create_window_buffers"); return false; } // Create screen event object. rc = screen_create_event(&m_screenEvent); if (rc) { perror("screen_create_event"); return false; } // Request screen events. screen_request_events(m_screenContext); // Get the EGL display and initialize. m_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); return false; } if (eglInitialize(m_eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); return false; } if (eglChooseConfig(m_eglDisplay, eglConfigAttrs, &config, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); return false; } m_eglContext = eglCreateContext(m_eglDisplay, config, EGL_NO_CONTEXT, eglContextAttrs); if (m_eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); return false; } m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, m_screenWindow, eglSurfaceAttrs); if (m_eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); return false; } if (eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); return false; } EGLint width, height; if ((m_eglDisplay == EGL_NO_DISPLAY) || (m_eglSurface == EGL_NO_SURFACE) ) return EXIT_FAILURE; eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width); eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height); m_obScreenSize.width = width; m_obScreenSize.height = height; // Set vsync. // eglSwapInterval(m_eglDisplay, screenSwapInterval); return true; }
//------------------------------------------------------------------------------------------------- bool GlesBox::bindEGLContext(uint32_t width, uint32_t height, uint64_t native_windows_id) { if (core_->display_ == nullptr) { // Get Display core_->display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (core_->display_ == EGL_NO_DISPLAY) { LOGE("Cann't get EGL display on native windows."); return false; } // Initialize EGL EGLint majorVersion(0), minorVersion(0); if (!eglInitialize(core_->display_, &majorVersion, &minorVersion)) { LOGE("Cann't eglInitialize EGL display."); return false; } eglBindAPI(EGL_OPENGL_ES_API); // Get configs EGLint numConfigs(0); if (!eglGetConfigs(core_->display_, NULL, 0, &numConfigs)) { LOGE("eglGetConfigs fail."); return false; } // Choose config EGLint attribList[] = { EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_NONE }; if (!eglChooseConfig(core_->display_, attribList, &core_->config_, 1, &numConfigs)) { LOGE("eglChooseConfig fail."); return false; } createSurface(width, height, native_windows_id); // Create a GL context EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE}; core_->context_ = eglCreateContext(core_->display_, core_->config_, EGL_NO_CONTEXT, contextAttribs); if (core_->context_ == EGL_NO_CONTEXT) { LOGE("eglCreateContext fail."); return false; } } //renew a surface because of size changed if (core_->width_ != width || core_->height_ != height) { createSurface(width, height, native_windows_id); } // Make the context current if ( !eglMakeCurrent(core_->display_, core_->surface_, core_->surface_, core_->context_) ) { LOGE("eglMakeCurrent fail."); return false; } return true; }
// Initialize EGL // GLFWbool _glfwInitEGL(void) { int i; const char* sonames[] = { #if defined(_GLFW_WIN32) "libEGL.dll", "EGL.dll", #elif defined(_GLFW_COCOA) "libEGL.dylib", #else "libEGL.so.1", #endif NULL }; for (i = 0; sonames[i]; i++) { _glfw.egl.handle = _glfw_dlopen(sonames[i]); if (_glfw.egl.handle) break; } if (!_glfw.egl.handle) return GLFW_FALSE; _glfw.egl.GetConfigAttrib = (PFNEGLGETCONFIGATTRIBPROC) _glfw_dlsym(_glfw.egl.handle, "eglGetConfigAttrib"); _glfw.egl.GetConfigs = (PFNEGLGETCONFIGSPROC) _glfw_dlsym(_glfw.egl.handle, "eglGetConfigs"); _glfw.egl.GetDisplay = (PFNEGLGETDISPLAYPROC) _glfw_dlsym(_glfw.egl.handle, "eglGetDisplay"); _glfw.egl.GetError = (PFNEGLGETERRORPROC) _glfw_dlsym(_glfw.egl.handle, "eglGetError"); _glfw.egl.Initialize = (PFNEGLINITIALIZEPROC) _glfw_dlsym(_glfw.egl.handle, "eglInitialize"); _glfw.egl.Terminate = (PFNEGLTERMINATEPROC) _glfw_dlsym(_glfw.egl.handle, "eglTerminate"); _glfw.egl.BindAPI = (PFNEGLBINDAPIPROC) _glfw_dlsym(_glfw.egl.handle, "eglBindAPI"); _glfw.egl.CreateContext = (PFNEGLCREATECONTEXTPROC) _glfw_dlsym(_glfw.egl.handle, "eglCreateContext"); _glfw.egl.DestroySurface = (PFNEGLDESTROYSURFACEPROC) _glfw_dlsym(_glfw.egl.handle, "eglDestroySurface"); _glfw.egl.DestroyContext = (PFNEGLDESTROYCONTEXTPROC) _glfw_dlsym(_glfw.egl.handle, "eglDestroyContext"); _glfw.egl.CreateWindowSurface = (PFNEGLCREATEWINDOWSURFACEPROC) _glfw_dlsym(_glfw.egl.handle, "eglCreateWindowSurface"); _glfw.egl.MakeCurrent = (PFNEGLMAKECURRENTPROC) _glfw_dlsym(_glfw.egl.handle, "eglMakeCurrent"); _glfw.egl.SwapBuffers = (PFNEGLSWAPBUFFERSPROC) _glfw_dlsym(_glfw.egl.handle, "eglSwapBuffers"); _glfw.egl.SwapInterval = (PFNEGLSWAPINTERVALPROC) _glfw_dlsym(_glfw.egl.handle, "eglSwapInterval"); _glfw.egl.QueryString = (PFNEGLQUERYSTRINGPROC) _glfw_dlsym(_glfw.egl.handle, "eglQueryString"); _glfw.egl.GetProcAddress = (PFNEGLGETPROCADDRESSPROC) _glfw_dlsym(_glfw.egl.handle, "eglGetProcAddress"); _glfw.egl.display = eglGetDisplay(_GLFW_EGL_NATIVE_DISPLAY); if (_glfw.egl.display == EGL_NO_DISPLAY) { _glfwInputError(GLFW_API_UNAVAILABLE, "EGL: Failed to get EGL display: %s", getEGLErrorString(eglGetError())); _glfwTerminateEGL(); return GLFW_FALSE; } if (!eglInitialize(_glfw.egl.display, &_glfw.egl.major, &_glfw.egl.minor)) { _glfwInputError(GLFW_API_UNAVAILABLE, "EGL: Failed to initialize EGL: %s", getEGLErrorString(eglGetError())); _glfwTerminateEGL(); return GLFW_FALSE; } _glfw.egl.KHR_create_context = extensionSupportedEGL("EGL_KHR_create_context"); _glfw.egl.KHR_create_context_no_error = extensionSupportedEGL("EGL_KHR_create_context_no_error"); _glfw.egl.KHR_gl_colorspace = extensionSupportedEGL("EGL_KHR_gl_colorspace"); return GLFW_TRUE; }
/** ** Initialize the video part for SDL. */ void InitVideoSdl() { Uint32 flags = 0; if (SDL_WasInit(SDL_INIT_VIDEO) == 0) { #ifndef USE_WIN32 // Fix tablet input in full-screen mode SDL_putenv(strdup("SDL_MOUSE_RELATIVE=0")); #endif int res = SDL_Init( #ifdef DEBUG SDL_INIT_NOPARACHUTE | #endif SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER); if (res < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } // Clean up on exit atexit(SDL_Quit); #ifdef USE_MAEMO maemo_init(); #endif // If debug is enabled, Stratagus disable SDL Parachute. // So we need gracefully handle segfaults and aborts. #if defined(DEBUG) && !defined(USE_WIN32) signal(SIGSEGV, CleanExit); signal(SIGABRT, CleanExit); #endif // Set WindowManager Title if (!FullGameName.empty()) { SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str()); } else if (!Parameters::Instance.applicationName.empty()) { SDL_WM_SetCaption(Parameters::Instance.applicationName.c_str(), Parameters::Instance.applicationName.c_str()); } else { SDL_WM_SetCaption("Stratagus", "Stratagus"); } #if ! defined(USE_WIN32) && ! defined(USE_MAEMO) #if defined(USE_OPENGL) || defined(USE_GLES) // Make sure, that we not create OpenGL textures (and do not call OpenGL functions), when creating icon surface bool UseOpenGL_orig = UseOpenGL; UseOpenGL = false; #endif SDL_Surface *icon = NULL; CGraphic *g = NULL; struct stat st; std::string FullGameNameL = FullGameName; for (size_t i = 0; i < FullGameNameL.size(); ++i) { FullGameNameL[i] = tolower(FullGameNameL[i]); } std::string ApplicationName = Parameters::Instance.applicationName; std::string ApplicationNameL = ApplicationName; for (size_t i = 0; i < ApplicationNameL.size(); ++i) { ApplicationNameL[i] = tolower(ApplicationNameL[i]); } std::vector <std::string> pixmaps; pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameName + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameNameL + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameName + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameNameL + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationName + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationNameL + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationName + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationNameL + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + "Stratagus" + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + "stratagus" + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "Stratagus" + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "stratagus" + ".png"); for (size_t i = 0; i < pixmaps.size(); ++i) { if (stat(pixmaps[i].c_str(), &st) == 0) { if (g) { CGraphic::Free(g); } g = CGraphic::New(pixmaps[i].c_str()); g->Load(); icon = g->Surface; if (icon) { break; } } } if (icon) { SDL_WM_SetIcon(icon, 0); } if (g) { CGraphic::Free(g); } #if defined(USE_OPENGL) || defined(USE_GLES) UseOpenGL = UseOpenGL_orig; #endif #endif #ifdef USE_WIN32 HWND hwnd = NULL; HICON hicon = NULL; SDL_SysWMinfo info; SDL_VERSION(&info.version); if (SDL_GetWMInfo(&info)) { hwnd = info.window; } if (hwnd) { hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0); } if (hicon) { SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon); SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon); } #endif } // Initialize the display #ifdef USE_MAEMO // TODO: Support window mode and portrait mode resolution on Maemo - Nokia N900 Video.FullScreen = 1; Video.Width = 800; Video.Height = 480; #endif #if !defined(USE_OPENGL) && !defined(USE_GLES) flags = SDL_HWSURFACE | SDL_HWPALETTE; #endif // Sam said: better for windows. /* SDL_HWSURFACE|SDL_HWPALETTE | */ if (Video.FullScreen) { flags |= SDL_FULLSCREEN; } #if defined(USE_OPENGL) || defined(USE_GLES) if (UseOpenGL) { #ifdef USE_GLES_NATIVE flags |= SDL_OPENGLES; #endif #ifdef USE_GLES_MAEMO if (SDL_GLES_Init(SDL_GLES_VERSION_1_1) < 0) { fprintf(stderr, "Couldn't initialize SDL_GLES: %s\n", SDL_GetError()); exit(1); } // Clean up GLES on exit atexit(SDL_GLES_Quit); flags |= SDL_SWSURFACE; #endif #ifdef USE_OPENGL flags |= SDL_OPENGL | SDL_GL_DOUBLEBUFFER; #endif } #endif if (!Video.Width || !Video.Height) { Video.Width = 640; Video.Height = 480; } if (!Video.Depth) { Video.Depth = 32; } #if defined(USE_OPENGL) || defined(USE_GLES) if (!Video.ViewportWidth || !Video.ViewportHeight) { Video.ViewportWidth = Video.Width; Video.ViewportHeight = Video.Height; } TheScreen = SDL_SetVideoMode(Video.ViewportWidth, Video.ViewportHeight, Video.Depth, flags); #else TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, Video.Depth, flags); #endif if (TheScreen && (TheScreen->format->BitsPerPixel != 16 && TheScreen->format->BitsPerPixel != 32)) { // Only support 16 and 32 bpp, default to 16 #if defined(USE_OPENGL) || defined(USE_GLES) TheScreen = SDL_SetVideoMode(Video.ViewportWidth, Video.ViewportHeight, 16, flags); #else TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, 16, flags); #endif } if (TheScreen == NULL) { fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", Video.Width, Video.Height, Video.Depth, SDL_GetError()); exit(1); } Video.FullScreen = (TheScreen->flags & SDL_FULLSCREEN) ? 1 : 0; Video.Depth = TheScreen->format->BitsPerPixel; #if defined(USE_TOUCHSCREEN) && defined(USE_WIN32) // Must not allow SDL to switch to relative mouse coordinates // with touchscreen when going fullscreen. So we don't hide the // cursor, but instead set a transparent 1px cursor Uint8 emptyCursor[] = {'\0'}; Video.blankCursor = SDL_CreateCursor(emptyCursor, emptyCursor, 1, 1, 0, 0); SDL_SetCursor(Video.blankCursor); #else // Turn cursor off, we use our own. SDL_ShowCursor(SDL_DISABLE); #endif // Make default character translation easier SDL_EnableUNICODE(1); #if defined(USE_OPENGL) || defined(USE_GLES) if (UseOpenGL) { #ifdef USE_GLES_MAEMO SDL_GLES_Context *context = SDL_GLES_CreateContext(); if (!context) { fprintf(stderr, "Couldn't initialize SDL_GLES_CreateContext: %s\n", SDL_GetError()); exit(1); } if (SDL_GLES_MakeCurrent(context) < 0) { fprintf(stderr, "Couldn't initialize SDL_GLES_MakeCurrent: %s\n", SDL_GetError()); exit(1); } // atexit(GLES_DeleteContext(context)); #endif #ifdef USE_GLES_EGL // Get the SDL window handle SDL_SysWMinfo sysInfo; //Will hold our Window information SDL_VERSION(&sysInfo.version); //Set SDL version if (SDL_GetWMInfo(&sysInfo) <= 0) { fprintf(stderr, "Unable to get window handle\n"); exit(1); } eglDisplay = eglGetDisplay((EGLNativeDisplayType)sysInfo.info.x11.display); if (!eglDisplay) { fprintf(stderr, "Couldn't open EGL Display\n"); exit(1); } if (!eglInitialize(eglDisplay, NULL, NULL)) { fprintf(stderr, "Couldn't initialize EGL Display\n"); exit(1); } // Find a matching config EGLint configAttribs[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE}; EGLint numConfigsOut = 0; EGLConfig eglConfig; if (eglChooseConfig(eglDisplay, configAttribs, &eglConfig, 1, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0) { fprintf(stderr, "Unable to find appropriate EGL config\n"); exit(1); } eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)sysInfo.info.x11.window, 0); if (eglSurface == EGL_NO_SURFACE) { fprintf(stderr, "Unable to create EGL surface\n"); exit(1); } // Bind GLES and create the context eglBindAPI(EGL_OPENGL_ES_API); EGLint contextParams[] = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE}; EGLContext eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL); if (eglContext == EGL_NO_CONTEXT) { fprintf(stderr, "Unable to create GLES context\n"); exit(1); } if (eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) == EGL_FALSE) { fprintf(stderr, "Unable to make GLES context current\n"); exit(1); } #endif InitOpenGL(); } #endif InitKey2Str(); ColorBlack = Video.MapRGB(TheScreen->format, 0, 0, 0); ColorDarkGreen = Video.MapRGB(TheScreen->format, 48, 100, 4); ColorLightBlue = Video.MapRGB(TheScreen->format, 52, 113, 166); ColorBlue = Video.MapRGB(TheScreen->format, 0, 0, 252); ColorOrange = Video.MapRGB(TheScreen->format, 248, 140, 20); ColorWhite = Video.MapRGB(TheScreen->format, 252, 248, 240); ColorLightGray = Video.MapRGB(TheScreen->format, 192, 192, 192); ColorGray = Video.MapRGB(TheScreen->format, 128, 128, 128); ColorDarkGray = Video.MapRGB(TheScreen->format, 64, 64, 64); ColorRed = Video.MapRGB(TheScreen->format, 252, 0, 0); ColorGreen = Video.MapRGB(TheScreen->format, 0, 252, 0); ColorYellow = Video.MapRGB(TheScreen->format, 252, 252, 0); UI.MouseWarpPos.x = UI.MouseWarpPos.y = -1; }
bool create_ANGLE(EGLNativeWindowType hWnd, int msaaSampleCount, EGLDisplay* eglDisplay, EGLContext* eglContext, EGLSurface* eglSurface, EGLConfig* eglConfig) { static const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; static const EGLint configAttribList[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8, EGL_NONE }; static const EGLint surfaceAttribList[] = { EGL_NONE, EGL_NONE }; EGLDisplay display = eglGetDisplay(GetDC(hWnd)); if (display == EGL_NO_DISPLAY ) { return false; } // Initialize EGL EGLint majorVersion, minorVersion; if (!eglInitialize(display, &majorVersion, &minorVersion)) { return false; } EGLint numConfigs; if (!eglGetConfigs(display, NULL, 0, &numConfigs)) { return false; } // Choose config bool foundConfig = false; if (msaaSampleCount) { static const int kConfigAttribListCnt = SK_ARRAY_COUNT(configAttribList); EGLint msaaConfigAttribList[kConfigAttribListCnt + 4]; memcpy(msaaConfigAttribList, configAttribList, sizeof(configAttribList)); SkASSERT(EGL_NONE == msaaConfigAttribList[kConfigAttribListCnt - 1]); msaaConfigAttribList[kConfigAttribListCnt - 1] = EGL_SAMPLE_BUFFERS; msaaConfigAttribList[kConfigAttribListCnt + 0] = 1; msaaConfigAttribList[kConfigAttribListCnt + 1] = EGL_SAMPLES; msaaConfigAttribList[kConfigAttribListCnt + 2] = msaaSampleCount; msaaConfigAttribList[kConfigAttribListCnt + 3] = EGL_NONE; if (eglChooseConfig(display, configAttribList, eglConfig, 1, &numConfigs)) { SkASSERT(numConfigs > 0); foundConfig = true; } } if (!foundConfig) { if (!eglChooseConfig(display, configAttribList, eglConfig, 1, &numConfigs)) { return false; } } // Create a surface EGLSurface surface = eglCreateWindowSurface(display, *eglConfig, (EGLNativeWindowType)hWnd, surfaceAttribList); if (surface == EGL_NO_SURFACE) { return false; } // Create a GL context EGLContext context = eglCreateContext(display, *eglConfig, EGL_NO_CONTEXT, contextAttribs ); if (context == EGL_NO_CONTEXT ) { return false; } // Make the context current if (!eglMakeCurrent(display, surface, surface, context)) { return false; } *eglDisplay = display; *eglContext = context; *eglSurface = surface; return true; }
void InitGraphics(bool convertToHSI) { bcm_host_init(); int32_t success = 0; EGLBoolean result; EGLint num_config; static EGL_DISPMANX_WINDOW_T nativewindow; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; static const EGLint attribute_list[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; static const EGLint context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLConfig config; // get an EGL display connection GDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(GDisplay!=EGL_NO_DISPLAY); check(); // initialize the EGL display connection result = eglInitialize(GDisplay, NULL, NULL); assert(EGL_FALSE != result); check(); // get an appropriate EGL frame buffer configuration result = eglChooseConfig(GDisplay, attribute_list, &config, 1, &num_config); assert(EGL_FALSE != result); check(); // get an appropriate EGL frame buffer configuration result = eglBindAPI(EGL_OPENGL_ES_API); assert(EGL_FALSE != result); check(); // create an EGL rendering context GContext = eglCreateContext(GDisplay, config, EGL_NO_CONTEXT, context_attributes); assert(GContext!=EGL_NO_CONTEXT); check(); // create an EGL window surface success = graphics_get_display_size(0 /* LCD */, &GScreenWidth, &GScreenHeight); assert( success >= 0 ); dst_rect.x = 0; dst_rect.y = 0; dst_rect.width = GScreenWidth; dst_rect.height = GScreenHeight; src_rect.x = 0; src_rect.y = 0; src_rect.width = GScreenWidth << 16; src_rect.height = GScreenHeight << 16; dispman_display = vc_dispmanx_display_open( 0 /* LCD */); dispman_update = vc_dispmanx_update_start( 0 ); dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display, 0/*layer*/, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/); nativewindow.element = dispman_element; nativewindow.width = GScreenWidth; nativewindow.height = GScreenHeight; vc_dispmanx_update_submit_sync( dispman_update ); check(); GSurface = eglCreateWindowSurface( GDisplay, config, &nativewindow, NULL ); assert(GSurface != EGL_NO_SURFACE); check(); // connect the context to the surface result = eglMakeCurrent(GDisplay, GSurface, GSurface, GContext); assert(EGL_FALSE != result); check(); // Set background color and clear buffers glClearColor(0.15f, 0.25f, 0.35f, 1.0f); glClear( GL_COLOR_BUFFER_BIT ); //load the test shaders GSimpleVS.LoadVertexShader("simplevertshader.glsl"); if(convertToHSI) GSimpleFS.LoadFragmentShader("rgbtohsifragshader.glsl"); else GSimpleFS.LoadFragmentShader("simplefragshader.glsl"); GSimpleProg.Create(&GSimpleVS,&GSimpleFS); check(); glUseProgram(GSimpleProg.GetId()); check(); //create an ickle vertex buffer static const GLfloat quad_vertex_positions[] = { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }; glGenBuffers(1, &GQuadVertexBuffer); check(); glBindBuffer(GL_ARRAY_BUFFER, GQuadVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertex_positions), quad_vertex_positions, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); check(); }
static CCXEGL * create(TWindow * pWindow) { CCXEGL * pEGL = new CCXEGL; Boolean bSuccess = FALSE; do { CC_BREAK_IF(! pEGL); TUChar szError[] = {'E','R','R','O','R',0}; TUChar szEglInitFailed[] = {'e','g','l','I','n','i','t','i','a','l','i','z','e',' ','f','a','i','l','e','d',0}; TUChar szCreateContextFailed[] = {'e','g','l','C','r','e','a','t','e','C','o','n','t','e','x','t',' ','f','a','i','l','e','d',0}; TUChar szEglCreateWindowSurfaceFailed[] = {'e','g','l','C','r','e','a','t','e','W','i','n','d','o','w','S','u','r','f','a','c','e',' ','f','a','i','l','e','d',0}; TUChar szEglMakeCurrentFailed[] = {'e','g','l','M','a','k','e','C','u','r','r','e','n','t',' ','f','a','i','l','e','d',0}; pEGL->m_eglNativeWindow = pWindow; EGLDisplay eglDisplay; CC_BREAK_IF(EGL_NO_DISPLAY == (eglDisplay = eglGetDisplay(pEGL->m_eglNativeDisplay))); EGLint nMajor, nMinor; EGLBoolean bEglRet; bEglRet = eglInitialize(eglDisplay, &nMajor, &nMinor); if ( EGL_FALSE == bEglRet || 1 != nMajor ) { TApplication::GetCurrentApplication()->MessageBox(szEglInitFailed, szError, WMB_OK); break; } const EGLint aConfigAttribs[] = { EGL_LEVEL, 0, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NATIVE_RENDERABLE, EGL_FALSE, EGL_DEPTH_SIZE, 16, EGL_NONE, }; EGLint iConfigs; EGLConfig eglConfig; CC_BREAK_IF( EGL_FALSE == eglChooseConfig(eglDisplay, aConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1) ); EGLContext eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL); if (EGL_NO_CONTEXT == eglContext) { TApplication::GetCurrentApplication()->MessageBox(szCreateContextFailed, szError, WMB_OK); break; } EGLSurface eglSurface; eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, pEGL->m_eglNativeWindow, NULL); if (EGL_NO_SURFACE == eglSurface) { TApplication::GetCurrentApplication()->MessageBox(szEglCreateWindowSurfaceFailed, szError, WMB_OK); break; } bEglRet = eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); if (EGL_FALSE == bEglRet) { TApplication::GetCurrentApplication()->MessageBox(szEglMakeCurrentFailed, szError, WMB_OK); break; } pEGL->m_eglDisplay = eglDisplay; pEGL->m_eglConfig = eglConfig; pEGL->m_eglContext = eglContext; pEGL->m_eglSurface = eglSurface; bSuccess = TRUE; } while (0); if (! bSuccess) { CC_SAFE_DELETE(pEGL); } return pEGL; }