[patch] gl: add test cases for nsgl-window

"Henry (Yu) Song - SISA" <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <3955FA337689574EB32F94B12A7E6E9E2484816C@sisaex01sj>
From da087e70822996ed0d8e2556a3bd9fda5a0b5d9d Mon Sep 17 00:00:00 2001
From: Henry Song <[email protected]>
Date: Fri, 4 Jan 2013 18:05:53 -0800
Subject: [PATCH] gl: add test cases for nsgl-window

---
 boilerplate/cairo-boilerplate-nsgl.m | 134 ++++++++++++++++++++++++++++++++++-
 1 file changed, 132 insertions(+), 2 deletions(-)

diff --git a/boilerplate/cairo-boilerplate-nsgl.m b/boilerplate/cairo-boilerplate-nsgl.m
index f6ead00..2247886 100644
--- a/boilerplate/cairo-boilerplate-nsgl.m
+++ b/boilerplate/cairo-boilerplate-nsgl.m
@@ -40,17 +40,50 @@

 #import <AppKit/NSOpenGL.h>
 #import <Foundation/NSAutoreleasePool.h>
+#import <Cocoa/Cocoa.h>

 static const cairo_user_data_key_t gl_closure_key;

 typedef struct _nsgl_target_closure {
     NSOpenGLContext *ctx;
     NSAutoreleasePool *pool;
+    NSOpenGLView *view;
+    NSWindow *window;

     cairo_device_t *device;
     cairo_surface_t *surface;
 } nsgl_target_closure_t;

+@interface NSGLTestView : NSOpenGLView {
+}
+
+@end
+
+@implementation NSGLTestView
+
++ (NSOpenGLPixelFormat*)defaultPixelFormat
+{
+    NSOpenGLPixelFormatAttribute attrs[] =
+    {
+ NSOpenGLPFADoubleBuffer,
+ NSOpenGLPFADepthSize, 24,
+ NSOpenGLPFAStencilSize, 8,
+ NSOpenGLPFAAlphaSize, 8,
+ NSOpenGLPFASampleBuffers, 1,
+ NSOpenGLPFASamples, 4,
+ NSOpenGLPFAMultisample,
+ 0
+    };
+
+    NSOpenGLPixelFormat *classPixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
+    if (!classPixelFormat)
+ exit (-1);
+
+    return classPixelFormat;
+}
+
+@end
+
 static void
 _cairo_boilerplate_nsgl_cleanup (void *closure)
 {
@@ -60,7 +93,15 @@ _cairo_boilerplate_nsgl_cleanup (void *closure)
     cairo_device_destroy (gltc->device);

     [NSOpenGLContext clearCurrentContext];
-    [gltc->ctx release];
+
+    if (gltc->window) {
+ [gltc->window orderOut: nil];
+ [gltc->window release];
+ [gltc->view release];
+    }
+    else
+ [gltc->ctx release];
+
     [gltc->pool release];

     free (gltc);
@@ -88,6 +129,8 @@ _cairo_boilerplate_nsgl_create_surface (const char *name,
     };

     gltc = xcalloc (1, sizeof (nsgl_target_closure_t));
+    gltc->window = nil;
+    gltc->view = nil;
     *closure = gltc;
     gltc->pool = [[NSAutoreleasePool alloc] init];

@@ -119,6 +162,78 @@ _cairo_boilerplate_nsgl_create_surface (const char *name,
     return surface;
 }

+static cairo_surface_t *
+_cairo_boilerplate_nsgl_create_view (const char *name,
+     cairo_content_t content,
+     double width,
+     double height,
+     double max_width,
+     double max_height,
+     cairo_boilerplate_mode_t   mode,
+     void **closure)
+{
+    nsgl_target_closure_t *gltc;
+    cairo_surface_t *surface;
+    int style = NSTexturedBackgroundWindowMask;
+
+    NSWindow *win;
+    NSOpenGLView *view;
+
+    NSScreen *screen = [NSScreen mainScreen];
+    NSRect frame = [screen frame];
+    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 = surface = cairo_gl_surface_create_for_view (gltc->device,
+ gltc->view,
+ ceil (width),
+ ceil (height));
+    if (cairo_surface_status (surface))
+ _cairo_boilerplate_nsgl_cleanup (gltc);
+
+    [win orderFront: win];
+
+    return surface;
+}
+
+static cairo_status_t
+_cairo_boilerplate_nsgl_finish_window (cairo_surface_t *surface)
+{
+    cairo_gl_surface_swapbuffers (surface);
+    return CAIRO_STATUS_SUCCESS;
+}
+
 static void
 _cairo_boilerplate_nsgl_synchronize (void *closure)
 {
@@ -136,7 +251,7 @@ static const cairo_boilerplate_target_t targets[] = {
     {
  "nsgl", "gl", NULL, NULL,
  CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
- "cairo_nsgl_device_create",
+ "cairo_nsgl_surface_create",
  _cairo_boilerplate_nsgl_create_surface,
  cairo_surface_create_similar,
  NULL, NULL,
@@ -146,6 +261,21 @@ static const cairo_boilerplate_target_t targets[] = {
  _cairo_boilerplate_nsgl_synchronize,
         NULL,
  TRUE, FALSE, FALSE
+    },
+    {
+ "nsgl-window", "gl", NULL, NULL,
+ CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
+ "cairo_nsgl_surface_create_for_window",
+ _cairo_boilerplate_nsgl_create_view,
+ 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,
+        NULL,
+ FALSE, FALSE, FALSE
     }
 };
 CAIRO_BOILERPLATE (nsgl, targets)
--
1.8.0.1

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.