132001f49Smrg/*
232001f49Smrg * Copyright (C) 2011 LunarG Inc.
332001f49Smrg *
432001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a
532001f49Smrg * copy of this software and associated documentation files (the "Software"),
632001f49Smrg * to deal in the Software without restriction, including without limitation
732001f49Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
832001f49Smrg * and/or sell copies of the Software, and to permit persons to whom the
932001f49Smrg * Software is furnished to do so, subject to the following conditions:
1032001f49Smrg *
1132001f49Smrg * The above copyright notice and this permission notice shall be included
1232001f49Smrg * in all copies or substantial portions of the Software.
1332001f49Smrg *
1432001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1532001f49Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1632001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1732001f49Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1832001f49Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1932001f49Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2032001f49Smrg * DEALINGS IN THE SOFTWARE.
2132001f49Smrg *
2232001f49Smrg * Authors:
2332001f49Smrg *    Chia-I Wu <olv@lunarg.com>
2432001f49Smrg */
2532001f49Smrg
2632001f49Smrg/*
2732001f49Smrg * This is a demonstration of EGL on fbdev:
2832001f49Smrg *
2932001f49Smrg *   The native display is the fd of the device;
3032001f49Smrg *   There is only one native window, NULL;
3132001f49Smrg *   There is no native pixmaps.
3232001f49Smrg *
3332001f49Smrg * It is the app's responsibility to set up the tty, open the fb device, and
3432001f49Smrg * initialize EGL.
3532001f49Smrg */
3632001f49Smrg
3732001f49Smrg#include <stdio.h>
3832001f49Smrg#include <stdlib.h>
3932001f49Smrg#include <string.h>
4032001f49Smrg#include <stdarg.h>
4132001f49Smrg
4232001f49Smrg/* for tty */
4332001f49Smrg#include <linux/kd.h>
4432001f49Smrg#include <linux/vt.h>
4532001f49Smrg#include <sys/ioctl.h>
4632001f49Smrg#include <termios.h>
4732001f49Smrg#include <unistd.h>
4832001f49Smrg#include <signal.h>
4932001f49Smrg
5032001f49Smrg/* for fbdev */
5132001f49Smrg#include <linux/fb.h>
5232001f49Smrg#include <sys/types.h>
5332001f49Smrg#include <sys/stat.h>
5432001f49Smrg#include <fcntl.h>
5532001f49Smrg
5632001f49Smrg/* for EGL */
5732001f49Smrg#include <EGL/egl.h>
5832001f49Smrg#include <GLES/gl.h>
5932001f49Smrg
6032001f49Smrgstatic int tty_fd = -1;
6132001f49Smrgstatic int tty_saved_vtno;
6232001f49Smrg
6332001f49Smrgstatic int tty_open_vt(int vtno)
6432001f49Smrg{
6532001f49Smrg   const char tty[] = "/dev/tty%d";
6632001f49Smrg   char name[64];
6732001f49Smrg   int size, flags;
6832001f49Smrg
6932001f49Smrg   size = snprintf(name, sizeof(name), tty, vtno);
7032001f49Smrg   if (size >= sizeof(name))
7132001f49Smrg      return -1;
7232001f49Smrg
7332001f49Smrg   flags = (vtno) ? O_RDWR | O_NDELAY : O_WRONLY;
7432001f49Smrg
7532001f49Smrg   return open(name, flags);
7632001f49Smrg}
7732001f49Smrg
7832001f49Smrgstatic int tty_switch_vt(int fd, int vtno)
7932001f49Smrg{
8032001f49Smrg   int ret;
8132001f49Smrg
8232001f49Smrg   ret = ioctl(fd, VT_ACTIVATE, vtno);
8332001f49Smrg   if (ret >= 0)
8432001f49Smrg      ret = ioctl(fd, VT_WAITACTIVE, vtno);
8532001f49Smrg
8632001f49Smrg   return ret;
8732001f49Smrg}
8832001f49Smrg
8932001f49Smrg
9032001f49Smrgstatic int tty_init_vt(void)
9132001f49Smrg{
9232001f49Smrg   struct vt_stat vts;
9332001f49Smrg   int fd, vtno;
9432001f49Smrg
9532001f49Smrg   /* get the next available tty number */
9632001f49Smrg   fd = tty_open_vt(0);
9732001f49Smrg   if (fd < 0)
9832001f49Smrg      return -1;
9932001f49Smrg   if (ioctl(fd, VT_OPENQRY, &vtno) < 0)
10032001f49Smrg      goto fail;
10132001f49Smrg   close(fd);
10232001f49Smrg
10332001f49Smrg   fd = tty_open_vt(vtno);
10432001f49Smrg   if (fd < 0)
10532001f49Smrg      return -1;
10632001f49Smrg
10732001f49Smrg   /* save the current VT */
10832001f49Smrg   if (ioctl(fd, VT_GETSTATE, &vts) < 0)
10932001f49Smrg      goto fail;
11032001f49Smrg   tty_saved_vtno = vts.v_active;
11132001f49Smrg
11232001f49Smrg   if (tty_switch_vt(fd, vtno))
11332001f49Smrg      goto fail;
11432001f49Smrg
11532001f49Smrg   return fd;
11632001f49Smrg
11732001f49Smrgfail:
11832001f49Smrg   close(fd);
11932001f49Smrg   return -1;
12032001f49Smrg}
12132001f49Smrg
12232001f49Smrgstatic void
12332001f49Smrgtty_close(void)
12432001f49Smrg{
12532001f49Smrg   /* restore */
12632001f49Smrg   ioctl(tty_fd, KDSETMODE, KD_TEXT);
12732001f49Smrg   tty_switch_vt(tty_fd, tty_saved_vtno);
12832001f49Smrg
12932001f49Smrg   close(tty_fd);
13032001f49Smrg}
13132001f49Smrg
13232001f49Smrgstatic void
13332001f49Smrgsignal_handler(int sig)
13432001f49Smrg{
13532001f49Smrg   if (tty_fd >= 0)
13632001f49Smrg      tty_close();
13732001f49Smrg}
13832001f49Smrg
13932001f49Smrgstatic int tty_open(void)
14032001f49Smrg{
14132001f49Smrg   struct sigaction sa;
14232001f49Smrg
14332001f49Smrg   tty_fd = tty_init_vt();
14432001f49Smrg   if (tty_fd < 0)
14532001f49Smrg      return -1;
14632001f49Smrg
14732001f49Smrg   /* install the signal handler */
14832001f49Smrg   memset(&sa, 0, sizeof(sa));
14932001f49Smrg   sigemptyset(&sa.sa_mask);
15032001f49Smrg   sa.sa_handler = signal_handler;
15132001f49Smrg   if (sigaction(SIGINT, &sa, NULL))
15232001f49Smrg      goto fail;
15332001f49Smrg   if (sigaction(SIGTERM, &sa, NULL))
15432001f49Smrg      goto fail;
15532001f49Smrg   if (sigaction(SIGABRT, &sa, NULL))
15632001f49Smrg      goto fail;
15732001f49Smrg
15832001f49Smrg   if (ioctl(tty_fd, KDSETMODE, KD_GRAPHICS) < 0)
15932001f49Smrg      goto fail;
16032001f49Smrg
16132001f49Smrg   tcflush(tty_fd, TCIOFLUSH);
16232001f49Smrg
16332001f49Smrg   return 0;
16432001f49Smrg
16532001f49Smrgfail:
16632001f49Smrg   tty_close();
16732001f49Smrg   tty_fd = -1;
16832001f49Smrg   return -1;
16932001f49Smrg}
17032001f49Smrg
17132001f49Smrgstatic EGLDisplay egl_dpy;
17232001f49Smrgstatic EGLContext egl_ctx;
17332001f49Smrgstatic EGLSurface egl_surf;
17432001f49Smrgstatic EGLBoolean egl_verbose;
17532001f49Smrg
17632001f49Smrgstatic void
17732001f49Smrgegl_fatal(char *format, ...)
17832001f49Smrg{
17932001f49Smrg   va_list args;
18032001f49Smrg
18132001f49Smrg   va_start(args, format);
18232001f49Smrg   vfprintf(stderr, format, args);
18332001f49Smrg   va_end(args);
18432001f49Smrg   putc('\n', stderr);
18532001f49Smrg
18632001f49Smrg   abort();
18732001f49Smrg}
18832001f49Smrg
18932001f49Smrgstatic void
19032001f49Smrgegl_init_for_fbdev(int fd, EGLBoolean verbose)
19132001f49Smrg{
19232001f49Smrg   const EGLNativeWindowType native_win = (EGLNativeWindowType) NULL;
19332001f49Smrg   EGLint major, minor, num_configs;
19432001f49Smrg   EGLConfig conf;
19532001f49Smrg
19632001f49Smrg   egl_verbose = verbose;
19732001f49Smrg
19832001f49Smrg   /* make Mesa/EGL happy */
19932001f49Smrg   setenv("EGL_PLATFORM", "fbdev", 0);
20032001f49Smrg
20132001f49Smrg   egl_dpy = eglGetDisplay((EGLNativeDisplayType) fd);
20232001f49Smrg   if (egl_dpy == EGL_NO_DISPLAY)
20332001f49Smrg      egl_fatal("failed to get a display");
20432001f49Smrg   if (!eglInitialize(egl_dpy, &major, &minor))
20532001f49Smrg      egl_fatal("failed to initialize EGL");
20632001f49Smrg
20732001f49Smrg   if (egl_verbose) {
20832001f49Smrg      printf("EGL %d.%d\n", major, minor);
20932001f49Smrg      printf("EGL_VENDOR: %s\n", eglQueryString(egl_dpy, EGL_VENDOR));
21032001f49Smrg      printf("EGL_VERSION: %s\n", eglQueryString(egl_dpy, EGL_VERSION));
21132001f49Smrg      printf("EGL_EXTENSIONS: %s\n", eglQueryString(egl_dpy, EGL_EXTENSIONS));
21232001f49Smrg      printf("EGL_CLIENT_APIS: %s\n",
21332001f49Smrg            eglQueryString(egl_dpy, EGL_CLIENT_APIS));
21432001f49Smrg   }
21532001f49Smrg
21632001f49Smrg   if (!eglChooseConfig(egl_dpy, NULL, &conf, 1, &num_configs) ||
21732001f49Smrg       !num_configs)
21832001f49Smrg      egl_fatal("failed to choose a config");
21932001f49Smrg
22032001f49Smrg   egl_ctx = eglCreateContext(egl_dpy, conf, EGL_NO_CONTEXT, NULL);
22132001f49Smrg   if (egl_ctx == EGL_NO_CONTEXT)
22232001f49Smrg      egl_fatal("failed to create a context");
22332001f49Smrg
22432001f49Smrg   egl_surf = eglCreateWindowSurface(egl_dpy, conf, native_win, NULL);
22532001f49Smrg   if (egl_surf == EGL_NO_SURFACE)
22632001f49Smrg      egl_fatal("failed to create a surface");
22732001f49Smrg
22832001f49Smrg   if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx))
22932001f49Smrg      egl_fatal("failed to make context/surface current");
23032001f49Smrg}
23132001f49Smrg
23232001f49Smrgstatic void
23332001f49Smrgegl_present(void)
23432001f49Smrg{
23532001f49Smrg   if (!eglSwapBuffers(egl_dpy, egl_surf))
23632001f49Smrg      egl_fatal("failed to swap buffers");
23732001f49Smrg}
23832001f49Smrg
23932001f49Smrgstatic void
24032001f49Smrgegl_destroy(void)
24132001f49Smrg{
24232001f49Smrg   eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
24332001f49Smrg   eglDestroyContext(egl_dpy, egl_surf);
24432001f49Smrg   eglDestroyContext(egl_dpy, egl_ctx);
24532001f49Smrg   eglTerminate(egl_dpy);
24632001f49Smrg
24732001f49Smrg   egl_surf = EGL_NO_SURFACE;
24832001f49Smrg   egl_ctx = EGL_NO_CONTEXT;
24932001f49Smrg   egl_dpy = EGL_NO_DISPLAY;
25032001f49Smrg}
25132001f49Smrg
25232001f49Smrg/* stolen from tri.c */
25332001f49Smrgstatic void
25432001f49Smrgdraw(int frame)
25532001f49Smrg{
25632001f49Smrg   static const GLfloat verts[3][2] = {
25732001f49Smrg      { -1, -1 },
25832001f49Smrg      {  1, -1 },
25932001f49Smrg      {  0,  1 }
26032001f49Smrg   };
26132001f49Smrg   static const GLfloat colors[3][4] = {
26232001f49Smrg      { 1, 0, 0, 1 },
26332001f49Smrg      { 0, 1, 0, 1 },
26432001f49Smrg      { 0, 0, 1, 1 }
26532001f49Smrg   };
26632001f49Smrg   GLfloat view_rotz = (GLfloat) frame;
26732001f49Smrg
26832001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
26932001f49Smrg
27032001f49Smrg   glPushMatrix();
27132001f49Smrg   glRotatef(view_rotz, 0, 0, 1);
27232001f49Smrg
27332001f49Smrg   glVertexPointer(2, GL_FLOAT, 0, verts);
27432001f49Smrg   glColorPointer(4, GL_FLOAT, 0, colors);
27532001f49Smrg
27632001f49Smrg   glEnableClientState(GL_VERTEX_ARRAY);
27732001f49Smrg   glEnableClientState(GL_COLOR_ARRAY);
27832001f49Smrg
27932001f49Smrg   /* draw triangle */
28032001f49Smrg   glDrawArrays(GL_TRIANGLES, 0, 3);
28132001f49Smrg
28232001f49Smrg   glDisableClientState(GL_VERTEX_ARRAY);
28332001f49Smrg   glDisableClientState(GL_COLOR_ARRAY);
28432001f49Smrg
28532001f49Smrg   glPopMatrix();
28632001f49Smrg}
28732001f49Smrg
28832001f49Smrgstatic void
28932001f49Smrginit(int width, int height)
29032001f49Smrg{
29132001f49Smrg   GLfloat ar = (GLfloat) width / height;
29232001f49Smrg
29332001f49Smrg   glViewport(0, 0, width, height);
29432001f49Smrg
29532001f49Smrg   glMatrixMode(GL_PROJECTION);
29632001f49Smrg   glLoadIdentity();
29732001f49Smrg   glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
29832001f49Smrg
29932001f49Smrg   glMatrixMode(GL_MODELVIEW);
30032001f49Smrg   glLoadIdentity();
30132001f49Smrg   glTranslatef(0.0, 0.0, -10.0);
30232001f49Smrg}
30332001f49Smrg
30432001f49Smrgint
30532001f49Smrgmain(int argc, char **argv)
30632001f49Smrg{
30732001f49Smrg   const char fbdev[] = "/dev/fb0";
30832001f49Smrg   struct fb_var_screeninfo vinfo;
30932001f49Smrg   int fd, tty_err, frame;
31032001f49Smrg
31132001f49Smrg   fd = open(fbdev, O_RDWR);
31232001f49Smrg   if (fd < 0)
31332001f49Smrg      egl_fatal("failed to open %s", fbdev);
31432001f49Smrg
31532001f49Smrg   memset(&vinfo, 0, sizeof(vinfo));
31632001f49Smrg   if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo))
31732001f49Smrg      egl_fatal("failed to get fb info");
31832001f49Smrg
31932001f49Smrg   /* initialize EGL */
32032001f49Smrg   egl_init_for_fbdev(fd, EGL_TRUE);
32132001f49Smrg
32232001f49Smrg   /* try to open a new tty */
32332001f49Smrg   tty_err = tty_open();
32432001f49Smrg
32532001f49Smrg   init(vinfo.xres, vinfo.yres);
32632001f49Smrg   for (frame = 0; frame <= 180; frame++) {
32732001f49Smrg      draw(frame);
32832001f49Smrg      egl_present();
32932001f49Smrg   }
33032001f49Smrg
33132001f49Smrg   if (!tty_err)
33232001f49Smrg      tty_close();
33332001f49Smrg
33432001f49Smrg   egl_destroy();
33532001f49Smrg   close(fd);
33632001f49Smrg
33732001f49Smrg   return 0;
33832001f49Smrg}
339