Subversion Repositories Kolibri OS

Rev

Rev 4464 | Rev 4473 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5.  
  6. #define EGL_EGLEXT_PROTOTYPES
  7. #define GL_GLEXT_PROTOTYPES
  8.  
  9. #include "EGL/egl.h"
  10. #include "EGL/eglext.h"
  11. #include "GL/gl.h"
  12. #include "gbm.h"
  13. #include <i915_drm.h>
  14. #include <kos32sys.h>
  15. #include <pixlib2.h>
  16.  
  17. static EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
  18.                          int width, int height, int stride, int name, int depth);
  19.  
  20. int main()
  21. {
  22.     struct gbm_device *gbm;
  23.     struct gbm_surface  *gs;
  24.  
  25.     EGLDisplay dpy;
  26.     EGLint major, minor;
  27.  
  28.     EGLContext context;
  29.     EGLSurface surface;
  30.     EGLImageKHR fb_image;
  31.     EGLConfig config;
  32.  
  33.     EGLint config_attribs[32];
  34.     EGLint num_configs, i;
  35.     GLint list;
  36.     GLuint *texture;
  37.  
  38.     int fd;
  39.  
  40.     fd = get_service("DISPLAY");
  41.     gbm = gbm_create_device(fd);
  42.     if( gbm == NULL){
  43.         printf("failed to initialize GBM device");
  44.         return 1;
  45.     };
  46.  
  47.     init_pixlib(HW_BIT_BLIT);
  48.  
  49.     dpy = eglGetDisplay((EGLNativeDisplayType)gbm);
  50.  
  51.     if (!eglInitialize(dpy, &major, &minor))
  52.         printf("failed to initialize EGL display");
  53.  
  54.     printf("EGL_VERSION = %s\n", eglQueryString(dpy, EGL_VERSION));
  55.     printf("EGL_VENDOR = %s\n", eglQueryString(dpy, EGL_VENDOR));
  56.     printf("EGL_EXTENSIONS = %s\n", eglQueryString(dpy, EGL_EXTENSIONS));
  57.     printf("EGL_CLIENT_APIS = %s\n",eglQueryString(dpy, EGL_CLIENT_APIS));
  58.  
  59.     i = 0;
  60.     config_attribs[i++] = EGL_RED_SIZE;
  61.     config_attribs[i++] = 1;
  62.     config_attribs[i++] = EGL_GREEN_SIZE;
  63.     config_attribs[i++] = 1;
  64.     config_attribs[i++] = EGL_BLUE_SIZE;
  65.     config_attribs[i++] = 1;
  66.     config_attribs[i++] = EGL_DEPTH_SIZE;
  67.     config_attribs[i++] = 1;
  68.  
  69.     config_attribs[i++] = EGL_SURFACE_TYPE;
  70.     config_attribs[i++] = EGL_WINDOW_BIT;
  71.  
  72.     config_attribs[i++] = EGL_RENDERABLE_TYPE;
  73.     config_attribs[i++] = EGL_OPENGL_BIT;
  74.     config_attribs[i] = EGL_NONE;
  75.  
  76.     if (!eglChooseConfig(dpy,config_attribs, &config, 1, &num_configs) || !num_configs)
  77.         printf("failed to choose a config");
  78.  
  79.     eglBindAPI(EGL_OPENGL_API);
  80.     context = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL);
  81.     if (!context)
  82.         printf("failed to create context");
  83.  
  84.     gs = gbm_surface_create(gbm, 400, 300, GBM_BO_FORMAT_ARGB8888, GBM_BO_USE_RENDERING);
  85.  
  86.  
  87.     BeginDraw();
  88.     DrawWindow(20, 20, 400+9, 300+24, "gl-render", 0x000000, 0x74);
  89.     EndDraw();
  90.  
  91.     sna_create_mask();
  92.  
  93.     surface = eglCreateWindowSurface(dpy,config, (EGLNativeWindowType)gs, NULL);
  94.     if (surface == EGL_NO_SURFACE)
  95.         printf("failed to create surface");
  96.  
  97.     if (!eglMakeCurrent(dpy, surface, surface, context))
  98.         printf("failed to make window current");
  99.  
  100.  
  101.     if(fd)
  102.     {
  103.         int ret;
  104.         struct drm_i915_fb_info fb;
  105.         struct drm_gem_open open_arg;
  106.  
  107.         memset(&fb, 0, sizeof(fb));
  108.         ret = drmIoctl(fd, SRV_FBINFO, &fb);
  109.         if( ret != 0 )
  110.             printf("failed to get framebuffer info\n");
  111.  
  112.         memset(&open_arg,0,sizeof(open_arg));
  113.         open_arg.name = fb.name;
  114.         ret = drmIoctl(fd,DRM_IOCTL_GEM_OPEN,&open_arg);
  115.         if (ret != 0) {
  116.             printf("Couldn't reference framebuffer handle 0x%08x\n", fb.name);
  117.                 }
  118.  
  119.   asm volatile ("int3");
  120.  
  121.                 fb_image = px_create_image(dpy,context,fb.width,fb.height,
  122.                                    fb.pitch,open_arg.handle,32);
  123.  
  124.         glGenTextures(1, texture);
  125.         glBindTexture(GL_TEXTURE_2D, *texture);
  126.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  127.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  128.  
  129.         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,fb_image);
  130.         glBindTexture(GL_TEXTURE_2D, 0);
  131.  
  132.     }
  133.  
  134.     glClearColor( 0, 0, 0, 1);
  135.  
  136.     list = glGenLists(1);
  137.     glNewList(list, GL_COMPILE);
  138.  
  139.    /* XXX: this state-change will only be executed if list is called
  140.     * from outside a begin/end pair:
  141.     */
  142.     glShadeModel( GL_FLAT );
  143.     glBegin(GL_TRIANGLES);
  144.     glColor3f(0,0,.7);
  145.     glVertex3f( -0.9,  0.9, -30.0);
  146.     glColor3f(0,.9,0);
  147.     glVertex3f( -0.9, -0.9, -30.0);
  148.     glColor3f(.8,0,0);
  149.     glVertex3f(  0.9,  0.0, -30.0);
  150.     glEnd();
  151.  
  152.    /* This statechange is potentially NOT redundant:
  153.     */
  154.     glShadeModel( GL_FLAT );
  155.     glBegin(GL_TRIANGLES);
  156.     glColor3f(0,1,0);
  157.     glVertex3f( -0.5,  0.5, -30.0);
  158.     glColor3f(0,0,1);
  159.     glVertex3f( -0.5, -0.5, -30.0);
  160.     glColor3f(1,0,0);
  161.     glVertex3f(  0.5,  0.0, -30.0);
  162.     glEnd();
  163.  
  164.     glEndList();
  165.  
  166.   asm volatile ("int3");
  167.  
  168.     glDrawBuffer(GL_BACK);
  169.  
  170.     glViewport(0, 0, (GLint)400, (GLint)300);
  171.  
  172.     glMatrixMode(GL_PROJECTION);
  173.     glLoadIdentity();
  174.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 100.0);
  175.     glMatrixMode(GL_MODELVIEW);
  176.  
  177.     glClear(GL_COLOR_BUFFER_BIT);
  178.  
  179.     glShadeModel( GL_SMOOTH );
  180.  
  181.     glBegin(GL_TRIANGLES);
  182.  
  183.    /* Note: call the list from inside a begin/end pair.  The end is
  184.     * provided by the display list...
  185.     */
  186.     glCallList(list);
  187.     glFlush();
  188.  
  189.  
  190.     eglSwapBuffers(dpy, surface);
  191.  
  192.     glFinish();
  193.     eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  194.     eglDestroySurface(dpy, surface);
  195.     gbm_surface_destroy(gs);
  196.     eglDestroyContext(dpy, context);
  197.     eglTerminate(dpy);
  198.  
  199.     while(1)
  200.     {
  201.         delay(1);
  202.     }
  203.  
  204.     return 0;
  205. }
  206.  
  207. int drmIoctl(int fd, unsigned long request, void *arg)
  208. {
  209.     ioctl_t  io;
  210.  
  211.     io.handle   = fd;
  212.     io.io_code  = request;
  213.     io.input    = arg;
  214.     io.inp_size = 64;
  215.     io.output   = NULL;
  216.     io.out_size = 0;
  217.  
  218.     return call_service(&io);
  219. }
  220.  
  221. static EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
  222.                          int width, int height, int stride, int name, int depth)
  223. {
  224.         EGLImageKHR image;
  225.         EGLint attribs[] = {
  226.                 EGL_WIDTH, 0,
  227.                 EGL_HEIGHT, 0,
  228.                 EGL_DRM_BUFFER_STRIDE_MESA, 0,
  229.                 EGL_DRM_BUFFER_FORMAT_MESA,
  230.                 EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
  231.                 EGL_DRM_BUFFER_USE_MESA,
  232.                 EGL_DRM_BUFFER_USE_SHARE_MESA |
  233.                     EGL_DRM_BUFFER_USE_SCANOUT_MESA,
  234.                 EGL_NONE
  235.         };
  236.         attribs[1] = width;
  237.         attribs[3] = height;
  238.         attribs[5] = stride;
  239.         if (depth != 32 && depth != 24)
  240.                 return EGL_NO_IMAGE_KHR;
  241.         image = eglCreateImageKHR(display, context, EGL_DRM_BUFFER_MESA,
  242.                                                  (void *) (uintptr_t)name, attribs);
  243.         if (image == EGL_NO_IMAGE_KHR)
  244.                 return EGL_NO_IMAGE_KHR;
  245.  
  246.         return image;
  247. }
  248.