Subversion Repositories Kolibri OS

Rev

Rev 4472 | Rev 4485 | 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. EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
  18.                          int width, int height, int stride, int name);
  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, buffer;
  37.  
  38.  
  39.     int fd;
  40.  
  41.     fd = get_service("DISPLAY");
  42.     gbm = gbm_create_device(fd);
  43.     if( gbm == NULL){
  44.         printf("failed to initialize GBM device");
  45.         return 1;
  46.     };
  47.  
  48.     init_pixlib(HW_BIT_BLIT);
  49.  
  50.     dpy = eglGetDisplay((EGLNativeDisplayType)gbm);
  51.  
  52.     if (!eglInitialize(dpy, &major, &minor))
  53.         printf("failed to initialize EGL display");
  54.  
  55.     printf("EGL_VERSION = %s\n", eglQueryString(dpy, EGL_VERSION));
  56.     printf("EGL_VENDOR = %s\n", eglQueryString(dpy, EGL_VENDOR));
  57.     printf("EGL_EXTENSIONS = %s\n", eglQueryString(dpy, EGL_EXTENSIONS));
  58.     printf("EGL_CLIENT_APIS = %s\n",eglQueryString(dpy, EGL_CLIENT_APIS));
  59.  
  60.     i = 0;
  61.     config_attribs[i++] = EGL_RED_SIZE;
  62.     config_attribs[i++] = 1;
  63.     config_attribs[i++] = EGL_GREEN_SIZE;
  64.     config_attribs[i++] = 1;
  65.     config_attribs[i++] = EGL_BLUE_SIZE;
  66.     config_attribs[i++] = 1;
  67.     config_attribs[i++] = EGL_DEPTH_SIZE;
  68.     config_attribs[i++] = 1;
  69.  
  70.     config_attribs[i++] = EGL_SURFACE_TYPE;
  71.     config_attribs[i++] = EGL_WINDOW_BIT;
  72.  
  73.     config_attribs[i++] = EGL_RENDERABLE_TYPE;
  74.     config_attribs[i++] = EGL_OPENGL_BIT;
  75.     config_attribs[i] = EGL_NONE;
  76.  
  77.     if (!eglChooseConfig(dpy,config_attribs, &config, 1, &num_configs) || !num_configs)
  78.         printf("failed to choose a config");
  79.  
  80.     eglBindAPI(EGL_OPENGL_API);
  81.     context = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL);
  82.     if (!context)
  83.         printf("failed to create context");
  84.  
  85.     gs = gbm_surface_create(gbm, 1024, 768, GBM_BO_FORMAT_ARGB8888, GBM_BO_USE_RENDERING);
  86.  
  87.  
  88.     BeginDraw();
  89.     DrawWindow(20, 20, 400+9, 300+24, "gl-render", 0x000000, 0x74);
  90.     EndDraw();
  91.  
  92.     sna_create_mask();
  93.  
  94.     surface = eglCreateWindowSurface(dpy,config, (EGLNativeWindowType)gs, NULL);
  95.     if (surface == EGL_NO_SURFACE)
  96.         printf("failed to create surface");
  97.  
  98.     if (!eglMakeCurrent(dpy, surface, surface, context))
  99.         printf("failed to make window current");
  100.  
  101.  
  102.     if(fd)
  103.     {
  104.         int ret;
  105.         GLenum status;
  106.         struct drm_i915_fb_info fb;
  107.  
  108.         memset(&fb, 0, sizeof(fb));
  109.         ret = drmIoctl(fd, SRV_FBINFO, &fb);
  110.         if( ret != 0 )
  111.             printf("failed to get framebuffer info\n");
  112.  
  113.                 fb_image = px_create_image(dpy,context,fb.width,fb.height,
  114.                                    fb.pitch,fb.name);
  115.  
  116.         printf("fb_image %p\n", fb_image);
  117.  
  118.   asm volatile ("int3");
  119.  
  120.         glGenTextures(1, &texture);
  121.         glBindTexture(GL_TEXTURE_2D, texture);
  122.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  123.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  124.  
  125.         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,fb_image);
  126.         glBindTexture(GL_TEXTURE_2D, 0);
  127.  
  128.             glGenFramebuffers(1, &buffer);
  129.         glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  130.         glFramebufferTexture2D(GL_FRAMEBUFFER,
  131.                                          GL_COLOR_ATTACHMENT0,
  132.                                          GL_TEXTURE_2D, texture,0);
  133.         status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  134.         if (status != GL_FRAMEBUFFER_COMPLETE)
  135.         {
  136.             const char *str;
  137.             switch (status)
  138.             {
  139.                 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  140.                     str = "incomplete attachment";
  141.                     break;
  142.                 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  143.                     str = "incomplete/missing attachment";
  144.                     break;
  145.                 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  146.                     str = "incomplete draw buffer";
  147.                     break;
  148.                 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  149.                     str = "incomplete read buffer";
  150.                     break;
  151.                 case GL_FRAMEBUFFER_UNSUPPORTED:
  152.                     str = "unsupported";
  153.                     break;
  154.                 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  155.                     str = "incomplete multiple";
  156.                     break;
  157.                 default:
  158.                     str = "unknown error";
  159.                     break;
  160.             }
  161.  
  162.             printf("destination is framebuffer incomplete: %s [%#x]\n",
  163.                            str, status);
  164.         }
  165.  
  166.         glViewport(0, 0,fb.width, fb.height);
  167.  
  168.         glMatrixMode(GL_PROJECTION);
  169.         glLoadIdentity();
  170.         glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 100.0);
  171.         glMatrixMode(GL_MODELVIEW);
  172.         glLoadIdentity();
  173.     }
  174.  
  175.     glClearColor( 0, 0, 0, 1);
  176.  
  177.     list = glGenLists(1);
  178.     glNewList(list, GL_COMPILE);
  179.  
  180.    /* XXX: this state-change will only be executed if list is called
  181.     * from outside a begin/end pair:
  182.     */
  183.     glShadeModel( GL_FLAT );
  184.     glBegin(GL_TRIANGLES);
  185.     glColor3f(0,0,.7);
  186.     glVertex3f( -0.9,  0.9, -30.0);
  187.     glColor3f(0,.9,0);
  188.     glVertex3f( -0.9, -0.9, -30.0);
  189.     glColor3f(.8,0,0);
  190.     glVertex3f(  0.9,  0.0, -30.0);
  191.     glEnd();
  192.  
  193.    /* This statechange is potentially NOT redundant:
  194.     */
  195.     glShadeModel( GL_FLAT );
  196.     glBegin(GL_TRIANGLES);
  197.     glColor3f(0,1,0);
  198.     glVertex3f( -0.5,  0.5, -30.0);
  199.     glColor3f(0,0,1);
  200.     glVertex3f( -0.5, -0.5, -30.0);
  201.     glColor3f(1,0,0);
  202.     glVertex3f(  0.5,  0.0, -30.0);
  203.     glEnd();
  204.  
  205.     glEndList();
  206.  
  207.   asm volatile ("int3");
  208.  
  209.     glDrawBuffer(GL_BACK);
  210.  
  211.     glClear(GL_COLOR_BUFFER_BIT);
  212.  
  213.     glShadeModel( GL_SMOOTH );
  214.  
  215.     glBegin(GL_TRIANGLES);
  216.  
  217.    /* Note: call the list from inside a begin/end pair.  The end is
  218.     * provided by the display list...
  219.     */
  220.     glCallList(list);
  221.     glFlush();
  222.  
  223.  
  224. //    eglSwapBuffers(dpy, surface);
  225.  
  226.     glFinish();
  227.     eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  228. //    eglDestroySurface(dpy, surface);
  229.   //  gbm_surface_destroy(gs);
  230.     eglDestroyContext(dpy, context);
  231.     eglTerminate(dpy);
  232.  
  233.     while(1)
  234.     {
  235.         delay(1);
  236.     }
  237.  
  238.     return 0;
  239. }
  240.  
  241. int drmIoctl(int fd, unsigned long request, void *arg)
  242. {
  243.     ioctl_t  io;
  244.  
  245.     io.handle   = fd;
  246.     io.io_code  = request;
  247.     io.input    = arg;
  248.     io.inp_size = 64;
  249.     io.output   = NULL;
  250.     io.out_size = 0;
  251.  
  252.     return call_service(&io);
  253. }
  254.  
  255. EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
  256.                          int width, int height, int stride, int name)
  257. {
  258.         EGLImageKHR image;
  259.         EGLint attribs[] = {
  260.                 EGL_WIDTH, 0,
  261.                 EGL_HEIGHT, 0,
  262.                 EGL_DRM_BUFFER_STRIDE_MESA, 0,
  263.                 EGL_DRM_BUFFER_FORMAT_MESA,
  264.                 EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
  265.                 EGL_DRM_BUFFER_USE_MESA,
  266.                 EGL_DRM_BUFFER_USE_SHARE_MESA |
  267.                     EGL_DRM_BUFFER_USE_SCANOUT_MESA,
  268.                 EGL_NONE
  269.         };
  270.         attribs[1] = width;
  271.         attribs[3] = height;
  272.         attribs[5] = stride;
  273.  
  274.     printf("%s w:%d :%d pitch:%d handle %d\n", __FUNCTION__,
  275.            width, height, stride, name);
  276.  
  277.         image = eglCreateImageKHR(display, context, EGL_DRM_BUFFER_MESA,
  278.                                                  (void *) (uintptr_t)name, attribs);
  279.  
  280.         return image;
  281. }
  282.