[cairo] [patch 2/2] gl: add nsgl test cases for nsgl-window&

Henry (Yu) Song - SISA hsong at sisa.samsung.com
Fri Jan 4 19:09:02 PST 2013


>From ed13470c39ad6806a5351d80a21415390183b4b4 Mon Sep 17 00:00:00 2001
From: Henry Song <hsong at sisa.samsung.com>
Date: Fri, 4 Jan 2013 19:04:21 -0800
Subject: [PATCH 2/2] gl: add nsgl test case for nsgl-window&

---
 boilerplate/cairo-boilerplate-nsgl.m | 133 +++++++++++++++++++++++++++++++++--
 1 file changed, 129 insertions(+), 4 deletions(-)

diff --git a/boilerplate/cairo-boilerplate-nsgl.m b/boilerplate/cairo-boilerplate-nsgl.m
index aea5c75..2b66eaa 100644
--- a/boilerplate/cairo-boilerplate-nsgl.m
+++ b/boilerplate/cairo-boilerplate-nsgl.m
@@ -180,7 +180,7 @@ _cairo_boilerplate_nsgl_create_view (const char *name,
     NSOpenGLView *view;

     NSScreen *screen = [NSScreen mainScreen];
-    NSRect frame = [screen frame];
+    NSRect frame = [screen visibleFrame];
     int screen_height = frame.size.height;

     gltc = xcalloc (1, sizeof (nsgl_target_closure_t));
@@ -227,9 +227,98 @@ _cairo_boilerplate_nsgl_create_view (const char *name,
     return surface;
 }

+static cairo_surface_t *
+_cairo_boilerplate_nsgl_create_view_db (const char  *name,
+ cairo_content_t   content,
+ double   width,
+ double   height,
+ double   max_width,
+ double   max_height,
+ cairo_boilerplate_mode_t   mode,
+ void **closure)
+{
+    cairo_status_t status;
+    nsgl_target_closure_t *gltc;
+    cairo_surface_t *surface;
+    int style = NSTexturedBackgroundWindowMask;
+
+    NSWindow *win;
+    NSOpenGLView *view;
+
+    NSScreen *screen = [NSScreen mainScreen];
+    NSRect frame = [screen visibleFrame];
+    int screen_height = frame.size.height;
+
+    gltc = xcalloc (1, sizeof (nsgl_target_closure_t));
+    gltc->window = nil;
+    gltc->view = nil;
+    *closure = gltc;
+
+    gltc->pool = [[NSAutoreleasePool alloc] init];
+    [NSApplication sharedApplication];
+
+    if (width < 1)
+ width = 1;
+    if (height < 1)
+ height = 1;
+
+    win = [[NSWindow alloc] initWithContentRect: NSMakeRect (0, screen_height - ceil (height), ceil (width), ceil (height))
+    styleMask: style
+    backing: NSBackingStoreBuffered
+    defer: NO];
+
+    view = [[NSGLTestView alloc] initWithFrame: NSMakeRect (0, 0, ceil (width), ceil (height))];
+    [win setContentView: view];
+
+    gltc->view = view;
+    gltc->window = win;
+
+    gltc->ctx = [gltc->view openGLContext];
+
+    if (!gltc->ctx)
+ return NULL;
+
+    [gltc->ctx makeCurrentContext];
+    gltc->device = cairo_nsgl_device_create (gltc->ctx);
+
+    gltc->surface = cairo_gl_surface_create_for_view (gltc->device,
+      gltc->view,
+      ceil (width),
+      ceil (height));
+    surface = cairo_surface_create_similar (gltc->surface, content, width, height);
+
+    status = cairo_surface_set_user_data (surface, &gl_closure_key, gltc, NULL);
+
+    if (status == CAIRO_STATUS_SUCCESS) {
+ [win orderFront: win];
+ return surface;
+    }
+
+    cairo_surface_destroy (surface);
+    _cairo_boilerplate_nsgl_cleanup (gltc);
+
+    return cairo_boilerplate_surface_create_in_error (status);
+}
+
 static cairo_status_t
 _cairo_boilerplate_nsgl_finish_window (cairo_surface_t *surface)
 {
+    nsgl_target_closure_t *gltc = cairo_surface_get_user_data (surface,
+       &gl_closure_key);
+
+    if (gltc != NULL && gltc->surface != NULL) {
+ cairo_t *cr;
+
+ cr = cairo_create (gltc->surface);
+ cairo_surface_set_device_offset (surface, 0, 0);
+ cairo_set_source_surface (cr, surface, 0, 0);
+ cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+ cairo_paint (cr);
+ cairo_destroy (cr);
+
+ surface = gltc->surface;
+    }
+
     cairo_gl_surface_swapbuffers (surface);
     return CAIRO_STATUS_SUCCESS;
 }
@@ -247,6 +336,27 @@ _cairo_boilerplate_nsgl_synchronize (void *closure)
     cairo_device_release (gltc->device);
 }

+static char *
+_cairo_boilerplate_nsgl_describe (void *closure)
+{
+    nsgl_target_closure_t *gltc = closure;
+    char *s;
+    const GLubyte *vendor, *renderer, *version;
+
+    if (cairo_device_acquire (gltc->device))
+ return NULL;
+
+    vendor   = glGetString (GL_VENDOR);
+    renderer = glGetString (GL_RENDERER);
+    version  = glGetString (GL_VERSION);
+
+    xasprintf (&s, "%s %s %s", vendor, renderer, version);
+
+    cairo_device_release (gltc->device);
+
+    return s;
+}
+
 static const cairo_boilerplate_target_t targets[] = {
     {
  "nsgl", "gl", NULL, NULL,
@@ -259,7 +369,7 @@ static const cairo_boilerplate_target_t targets[] = {
  cairo_surface_write_to_png,
  _cairo_boilerplate_nsgl_cleanup,
  _cairo_boilerplate_nsgl_synchronize,
-        NULL,
+        _cairo_boilerplate_nsgl_describe,
  TRUE, FALSE, FALSE
     },
     {
@@ -273,7 +383,7 @@ static const cairo_boilerplate_target_t targets[] = {
  cairo_surface_write_to_png,
  _cairo_boilerplate_nsgl_cleanup,
  _cairo_boilerplate_nsgl_synchronize,
-        NULL,
+        _cairo_boilerplate_nsgl_describe,
  TRUE, FALSE, FALSE
     },
     {
@@ -288,7 +398,22 @@ static const cairo_boilerplate_target_t targets[] = {
  cairo_surface_write_to_png,
  _cairo_boilerplate_nsgl_cleanup,
  _cairo_boilerplate_nsgl_synchronize,
-        NULL,
+        _cairo_boilerplate_nsgl_describe,
+ FALSE, FALSE, FALSE
+    },
+    {
+ "nsgl-windowi&", "gl", NULL, NULL,
+ CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
+ "cairo_nsgl_surface_create_for_window",
+ _cairo_boilerplate_nsgl_create_view_db,
+ cairo_surface_create_similar,
+ NULL,
+ _cairo_boilerplate_nsgl_finish_window,
+ _cairo_boilerplate_get_image_surface,
+ cairo_surface_write_to_png,
+ _cairo_boilerplate_nsgl_cleanup,
+ _cairo_boilerplate_nsgl_synchronize,
+        _cairo_boilerplate_nsgl_describe,
  FALSE, FALSE, FALSE
     }
 };
--
1.8.0.1

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20130105/ec3decff/attachment.html>


More information about the cairo mailing list