Loading

demo.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wayland-client.h>
  4. #include "owl-mach-ipc-unstable-v1-client-procotol.h"
  5. #include "owl-iosurface-unstable-v1-client-procotol.h"
  6.  
  7. #include <mach/mach.h>
  8. #include <servers/bootstrap.h>
  9. #include "owl-mach-ipc-unstable-v1-mig-user.h"
  10. #include "owl-iosurface-unstable-v1-mig-user.h"
  11.  
  12. #include <IOSurface/IOSurface.h>
  13. #include <CoreFoundation/CoreFoundation.h>
  14. #include <OpenGL/OpenGL.h>
  15. #include <OpenGL/CGLIOSurface.h>
  16.  
  17. void renderStuff(IOSurfaceRef surface) {
  18.     CGLPixelFormatObj pixelFormat;
  19.     GLint npix;
  20.     const CGLPixelFormatAttribute attrs[1] = { 0 };
  21.     CGLChoosePixelFormat(attrs, &pixelFormat, &npix);
  22.     CGLContextObj context;
  23.     CGLCreateContext(pixelFormat, NULL, &context);
  24.     CGLSetCurrentContext(context);
  25.  
  26.     GLuint name, namef, namedb;
  27.  
  28.     glGenTextures(1, &name);
  29.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, name);
  30.  
  31.     CGLTexImageIOSurface2D(context, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, 512, 400,
  32.                            GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, surface, 0);
  33.  
  34.     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  35.     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  36.     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  37.     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  38.  
  39.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
  40.  
  41.     glGenFramebuffersEXT(1, &namef);
  42.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, namef);
  43.     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, name, 0);
  44.  
  45.     glGenRenderbuffersEXT(1, &namedb);
  46.     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, namedb);
  47.     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, 512, 400);
  48.  
  49.     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, namedb);
  50.     glClearColor(1.0f, 0.675f, 0.675f, 1.0f);
  51.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  52.     glFlush();
  53.  
  54.     printf("%x\n", glGetError());
  55. }
  56.  
  57. CFNumberRef makeCFInt(int n) {
  58.     return CFNumberCreate(NULL, kCFNumberIntType, &n);
  59. }
  60.  
  61. IOSurfaceRef createSurface() {
  62.     CFMutableDictionaryRef d = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  63.     CFDictionaryAddValue(d, kIOSurfaceWidth, makeCFInt(512));
  64.     CFDictionaryAddValue(d, kIOSurfaceHeight, makeCFInt(400));
  65.     CFDictionaryAddValue(d, kIOSurfaceBytesPerElement, makeCFInt(4));
  66.  
  67.     IOSurfaceRef surface = IOSurfaceCreate(d);
  68.  
  69.     return surface;
  70. }
  71.  
  72. static mach_port_t sport;
  73. char *secret;
  74.  
  75. static void mach_ipc_port_secret_handler
  76. (
  77.     void *data,
  78.     struct zowl_mach_ipc_port_v1 *mach_ipc_port,
  79.     const char *l_secret
  80. ) {
  81.     secret = strdup(l_secret);
  82. }
  83.  
  84. static const struct zowl_mach_ipc_port_v1_listener mach_ipc_port_listener = {
  85.     .secret = mach_ipc_port_secret_handler
  86. };
  87.  
  88. static void mach_ipc_bootstrap_name_handler
  89. (
  90.     void *data,
  91.     struct zowl_mach_ipc_v1 *mach_ipc,
  92.     const char *name
  93. ) {
  94.     kern_return_t kr = bootstrap_look_up(bootstrap_port, name, &sport);
  95.  
  96.     if (kr != KERN_SUCCESS) {
  97.         fprintf(stderr, "bootstrap_look_up(%s) = %x\n", name, kr);
  98.         return;
  99.     }
  100.  
  101.     fprintf(stderr, "bootstrap_look_up(%s) SUCCESS -> %d\n", name, sport);
  102. }
  103.  
  104. static struct zowl_mach_ipc_v1_listener mach_ipc_listener = {
  105.     .bootstrap_name = mach_ipc_bootstrap_name_handler
  106. };
  107.  
  108.  
  109. struct wl_compositor *compositor;
  110. struct wl_shell *shell;
  111.  
  112. struct zowl_iosurface_manager_v1 *iosurface_manager;
  113. struct zowl_mach_ipc_port_v1 *mach_ipc_port;
  114.  
  115. static void registry_global_handler
  116. (
  117.     void *data,
  118.     struct wl_registry *registry,
  119.     uint32_t name,
  120.     const char *interface,
  121.     uint32_t version
  122. ) {
  123.     if (strcmp(interface, "zowl_mach_ipc_v1") == 0) {
  124.         struct zowl_mach_ipc_v1 *mach_ipc = wl_registry_bind(registry, name, &zowl_mach_ipc_v1_interface, 1);
  125.         zowl_mach_ipc_v1_add_listener(mach_ipc, &mach_ipc_listener, NULL);
  126.         mach_ipc_port = zowl_mach_ipc_v1_create_port(mach_ipc);
  127.         zowl_mach_ipc_port_v1_add_listener(mach_ipc_port, &mach_ipc_port_listener, NULL);
  128.     } else if (strcmp(interface, "zowl_iosurface_manager_v1") == 0) {
  129.         iosurface_manager = wl_registry_bind(registry, name, &zowl_iosurface_manager_v1_interface, 1);
  130.     } else if (strcmp(interface, "wl_compositor") == 0) {
  131.         compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
  132.     } else if (strcmp(interface, "wl_shell") == 0) {
  133.         shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
  134.     }
  135. }
  136.  
  137. static void registry_global_remove_handler
  138. (
  139.     void *data,
  140.     struct wl_registry *registry,
  141.     uint32_t name
  142. ) {}
  143.  
  144. static const struct wl_registry_listener registry_listener = {
  145.     .global = registry_global_handler,
  146.     .global_remove = registry_global_remove_handler
  147. };
  148.  
  149. int main() {
  150.     struct wl_display *display = wl_display_connect(NULL);
  151.  
  152.     if (display == NULL) {
  153.         perror("wl_display_connect");
  154.         return 1;
  155.     }
  156.  
  157.     struct wl_registry *registry = wl_display_get_registry(display);
  158.     wl_registry_add_listener(registry, &registry_listener, NULL);
  159.  
  160.     wl_display_roundtrip(display);
  161.  
  162.     struct zowl_iosurface_v1 *iosurface = zowl_iosurface_manager_v1_create_surface(iosurface_manager, mach_ipc_port);
  163.  
  164.     wl_display_roundtrip(display);
  165.  
  166.     mach_port_t the_port;
  167.     kern_return_t kr = owl_mach_ipc_v1_retrieve_port(sport, secret, &the_port);
  168.     if (kr != KERN_SUCCESS) {
  169.         fprintf(stderr, "owl_mach_ipc_v1_retrieve_port(%s) = %x\n", secret, kr);
  170.         return;
  171.     }
  172.     fprintf(stderr, "owl_mach_ipc_v1_retrieve_port(%s) SUCCESS -> %d\n", secret, the_port);
  173.  
  174.     // This is where you create a real IOSurface and get its port.
  175.     IOSurfaceRef real_iosurface = createSurface();
  176.     mach_port_t my_port = IOSurfaceCreateMachPort(real_iosurface);
  177.  
  178.     kr = owl_iosurface_v1_set_surface_port(the_port, my_port);
  179.     if (kr != KERN_SUCCESS) {
  180.         fprintf(stderr, "owl_iosurface_v1_set_surface_port(%d, %d) = %x\n", the_port, my_port, kr);
  181.         return;
  182.     }
  183.     fprintf(stderr, "owl_iosurface_v1_set_surface_port(%d, %d) SUCCESS\n", the_port, my_port);
  184.  
  185.     struct wl_buffer *buffer = zowl_iosurface_v1_create_buffer(iosurface);
  186.  
  187.     struct wl_surface *surface = wl_compositor_create_surface(compositor);
  188.     struct wl_shell_surface *shell_surface = wl_shell_get_shell_surface(shell, surface);
  189.     wl_shell_surface_set_toplevel(shell_surface);
  190.     wl_shell_surface_set_title(shell_surface, "Weird Square");
  191.  
  192.     renderStuff(real_iosurface);
  193.  
  194.     wl_surface_attach(surface, buffer, 0, 0);
  195.     wl_surface_commit(surface);
  196.  
  197.     while (1) {
  198.         int ret = wl_display_dispatch(display);
  199.         if (ret < 0) {
  200.             perror("wl_display_dispatch");
  201.             return 1;
  202.         }
  203.     }
  204. }