1 /* Cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Chris Wilson
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Chris Wilson.
33 #include "cairo-boilerplate-private.h"
35 #include <cairo-cogl.h>
36 #include <cogl/cogl2-experimental.h>
38 typedef struct _cogl_closure {
39 cairo_device_t *device;
41 cairo_surface_t *surface;
44 static const cairo_user_data_key_t cogl_closure_key;
46 static CoglContext *context = NULL;
49 _cairo_boilerplate_cogl_cleanup (void *abstract_closure)
51 cogl_closure_t *closure = abstract_closure;
53 cogl_object_unref (closure->fb);
55 cairo_device_finish (closure->device);
56 cairo_device_destroy (closure->device);
61 static cairo_surface_t *
62 _cairo_boilerplate_cogl_create_offscreen_color_surface (const char *name,
63 cairo_content_t content,
68 cairo_boilerplate_mode_t mode,
69 void **abstract_closure)
71 cairo_device_t *device;
75 cogl_closure_t *closure;
76 cairo_status_t status;
79 context = cogl_context_new (NULL, NULL);
81 device = cairo_cogl_device_create (context);
82 tex = cogl_texture_new_with_size (width, height,
83 COGL_TEXTURE_NO_SLICING,
84 COGL_PIXEL_FORMAT_BGRA_8888_PRE);
85 offscreen = cogl_offscreen_new_to_texture (tex);
86 fb = COGL_FRAMEBUFFER (offscreen);
88 cogl_framebuffer_allocate (fb, NULL);
89 cogl_push_framebuffer (fb);
90 cogl_ortho (0, cogl_framebuffer_get_width (fb),
91 cogl_framebuffer_get_height (fb), 0,
93 cogl_pop_framebuffer ();
95 closure = malloc (sizeof (cogl_closure_t));
96 *abstract_closure = closure;
97 closure->device = device;
99 closure->surface = cairo_cogl_surface_create (device, fb);
101 status = cairo_surface_set_user_data (closure->surface,
102 &cogl_closure_key, closure, NULL);
103 if (status == CAIRO_STATUS_SUCCESS)
104 return closure->surface;
106 _cairo_boilerplate_cogl_cleanup (closure);
107 return cairo_boilerplate_surface_create_in_error (status);
110 static cairo_surface_t *
111 _cairo_boilerplate_cogl_create_onscreen_color_surface (const char *name,
112 cairo_content_t content,
117 cairo_boilerplate_mode_t mode,
118 void **abstract_closure)
120 cairo_device_t *device;
121 CoglOnscreen *onscreen;
123 cogl_closure_t *closure;
124 cairo_status_t status;
127 context = cogl_context_new (NULL, NULL);
129 device = cairo_cogl_device_create (context);
130 onscreen = cogl_onscreen_new (context, width, height);
131 fb = COGL_FRAMEBUFFER (onscreen);
133 cogl_onscreen_show (onscreen);
135 cogl_push_framebuffer (fb);
136 cogl_ortho (0, cogl_framebuffer_get_width (fb),
137 cogl_framebuffer_get_height (fb), 0,
139 cogl_pop_framebuffer ();
141 closure = malloc (sizeof (cogl_closure_t));
142 *abstract_closure = closure;
143 closure->device = device;
145 closure->surface = cairo_cogl_surface_create (device, fb);
147 status = cairo_surface_set_user_data (closure->surface,
148 &cogl_closure_key, closure, NULL);
149 if (status == CAIRO_STATUS_SUCCESS)
150 return closure->surface;
152 _cairo_boilerplate_cogl_cleanup (closure);
153 return cairo_boilerplate_surface_create_in_error (status);
156 static cairo_status_t
157 _cairo_boilerplate_cogl_finish_onscreen (cairo_surface_t *surface)
159 cogl_closure_t *closure = cairo_surface_get_user_data (surface, &cogl_closure_key);
161 cairo_cogl_surface_end_frame (surface);
163 cogl_framebuffer_swap_buffers (closure->fb);
165 return CAIRO_STATUS_SUCCESS;
169 _cairo_boilerplate_cogl_synchronize (void *abstract_closure)
171 cogl_closure_t *closure = abstract_closure;
172 cogl_framebuffer_finish (closure->fb);
175 static const cairo_boilerplate_target_t targets[] = {
177 "cogl-offscreen-color", "cogl", NULL, NULL,
178 CAIRO_SURFACE_TYPE_COGL, CAIRO_CONTENT_COLOR_ALPHA, 1,
179 "cairo_cogl_device_create",
180 _cairo_boilerplate_cogl_create_offscreen_color_surface,
181 cairo_surface_create_similar,
183 _cairo_boilerplate_get_image_surface,
184 cairo_surface_write_to_png,
185 _cairo_boilerplate_cogl_cleanup,
186 _cairo_boilerplate_cogl_synchronize,
191 "cogl-onscreen-color", "cogl", NULL, NULL,
192 CAIRO_SURFACE_TYPE_COGL, CAIRO_CONTENT_COLOR_ALPHA, 1,
193 "cairo_cogl_device_create",
194 _cairo_boilerplate_cogl_create_onscreen_color_surface,
195 cairo_surface_create_similar,
197 _cairo_boilerplate_cogl_finish_onscreen,
198 _cairo_boilerplate_get_image_surface,
199 cairo_surface_write_to_png,
200 _cairo_boilerplate_cogl_cleanup,
201 _cairo_boilerplate_cogl_synchronize,
206 CAIRO_BOILERPLATE (cogl, targets)