Update change log.
[platform/upstream/cairo.git] / boilerplate / cairo-boilerplate-cogl.c
1 /* Cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Chris Wilson
4  *
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.
12  *
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
18  *
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/
23  *
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.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Chris Wilson.
31  */
32
33 #include "cairo-boilerplate-private.h"
34
35 #include <cairo-cogl.h>
36 #include <cogl/cogl2-experimental.h>
37
38 typedef struct _cogl_closure {
39     cairo_device_t *device;
40     CoglFramebuffer *fb;
41     cairo_surface_t *surface;
42 } cogl_closure_t;
43
44 static const cairo_user_data_key_t cogl_closure_key;
45
46 static CoglContext *context = NULL;
47
48 static void
49 _cairo_boilerplate_cogl_cleanup (void *abstract_closure)
50 {
51     cogl_closure_t *closure = abstract_closure;
52
53     cogl_object_unref (closure->fb);
54
55     cairo_device_finish (closure->device);
56     cairo_device_destroy (closure->device);
57
58     free (closure);
59 }
60
61 static cairo_surface_t *
62 _cairo_boilerplate_cogl_create_offscreen_color_surface (const char              *name,
63                                                         cairo_content_t          content,
64                                                         double                   width,
65                                                         double                   height,
66                                                         double                   max_width,
67                                                         double                   max_height,
68                                                         cairo_boilerplate_mode_t mode,
69                                                         void                   **abstract_closure)
70 {
71     cairo_device_t *device;
72     CoglTexture *tex;
73     CoglHandle offscreen;
74     CoglFramebuffer *fb;
75     cogl_closure_t *closure;
76     cairo_status_t status;
77
78     if (!context)
79         context = cogl_context_new (NULL, NULL);
80
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);
87
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,
92                 -1, 100);
93     cogl_pop_framebuffer ();
94
95     closure = malloc (sizeof (cogl_closure_t));
96     *abstract_closure = closure;
97     closure->device = device;
98     closure->fb = fb;
99     closure->surface = cairo_cogl_surface_create (device, fb);
100
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;
105
106     _cairo_boilerplate_cogl_cleanup (closure);
107     return cairo_boilerplate_surface_create_in_error (status);
108 }
109
110 static cairo_surface_t *
111 _cairo_boilerplate_cogl_create_onscreen_color_surface (const char              *name,
112                                                        cairo_content_t          content,
113                                                        double                   width,
114                                                        double                   height,
115                                                        double                   max_width,
116                                                        double                   max_height,
117                                                        cairo_boilerplate_mode_t mode,
118                                                        void                   **abstract_closure)
119 {
120     cairo_device_t *device;
121     CoglOnscreen *onscreen;
122     CoglFramebuffer *fb;
123     cogl_closure_t *closure;
124     cairo_status_t status;
125
126     if (!context)
127         context = cogl_context_new (NULL, NULL);
128
129     device = cairo_cogl_device_create (context);
130     onscreen = cogl_onscreen_new (context, width, height);
131     fb = COGL_FRAMEBUFFER (onscreen);
132
133     cogl_onscreen_show (onscreen);
134
135     cogl_push_framebuffer (fb);
136     cogl_ortho (0, cogl_framebuffer_get_width (fb),
137                 cogl_framebuffer_get_height (fb), 0,
138                 -1, 100);
139     cogl_pop_framebuffer ();
140
141     closure = malloc (sizeof (cogl_closure_t));
142     *abstract_closure = closure;
143     closure->device = device;
144     closure->fb = fb;
145     closure->surface = cairo_cogl_surface_create (device, fb);
146
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;
151
152     _cairo_boilerplate_cogl_cleanup (closure);
153     return cairo_boilerplate_surface_create_in_error (status);
154 }
155
156 static cairo_status_t
157 _cairo_boilerplate_cogl_finish_onscreen (cairo_surface_t *surface)
158 {
159     cogl_closure_t *closure = cairo_surface_get_user_data (surface, &cogl_closure_key);
160
161     cairo_cogl_surface_end_frame (surface);
162
163     cogl_framebuffer_swap_buffers (closure->fb);
164
165     return CAIRO_STATUS_SUCCESS;
166 }
167
168 static void
169 _cairo_boilerplate_cogl_synchronize (void *abstract_closure)
170 {
171     cogl_closure_t *closure = abstract_closure;
172     cogl_framebuffer_finish (closure->fb);
173 }
174
175 static const cairo_boilerplate_target_t targets[] = {
176     {
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,
182         NULL, NULL,
183         _cairo_boilerplate_get_image_surface,
184         cairo_surface_write_to_png,
185         _cairo_boilerplate_cogl_cleanup,
186         _cairo_boilerplate_cogl_synchronize,
187         NULL,
188         TRUE, FALSE, FALSE
189     },
190     {
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,
196         NULL,
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,
202         NULL,
203         TRUE, FALSE, FALSE
204     }
205 };
206 CAIRO_BOILERPLATE (cogl, targets)