Debian Codegraph @

gdk/quartz/gdkpixmap-quartz.c

raw
1 /* gdkpixmap-quartz.c
2 *
3 * Copyright (C) 2005 Imendio AB
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 #include "config.h"
22
23 #include "gdkpixmap.h"
24 #include "gdkprivate-quartz.h"
25
26 static gpointer parent_class;
27
28 static void
29 gdk_pixmap_impl_quartz_init (GdkPixmapImplQuartz *impl)
30 {
31 }
32
33 static void
34 gdk_pixmap_impl_quartz_get_size (GdkDrawable *drawable,
35 gint *width,
36 gint *height)
37 {
38 if (width)
39 *width = GDK_PIXMAP_IMPL_QUARTZ (drawable)->width;
40 if (height)
41 *height = GDK_PIXMAP_IMPL_QUARTZ (drawable)->height;
42 }
43
44 static void
45 gdk_pixmap_impl_quartz_get_image_parameters (GdkPixmap *pixmap,
46 gint *bits_per_component,
47 gint *bits_per_pixel,
48 gint *bytes_per_row,
49 CGColorSpaceRef *colorspace,
50 CGImageAlphaInfo *alpha_info)
51 {
52 GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
53 gint depth = GDK_PIXMAP_OBJECT (pixmap)->depth;
54
55 switch (depth)
56 {
57 case 24:
58 if (bits_per_component)
59 *bits_per_component = 8;
60
61 if (bits_per_pixel)
62 *bits_per_pixel = 32;
63
64 if (bytes_per_row)
65 *bytes_per_row = impl->width * 4;
66
67 if (colorspace)
68 *colorspace = CGColorSpaceCreateDeviceRGB ();
69
70 if (alpha_info)
71 *alpha_info = kCGImageAlphaNoneSkipLast;
72 break;
73
74 case 32:
75 if (bits_per_component)
76 *bits_per_component = 8;
77
78 if (bits_per_pixel)
79 *bits_per_pixel = 32;
80
81 if (bytes_per_row)
82 *bytes_per_row = impl->width * 4;
83
84 if (colorspace)
85 *colorspace = CGColorSpaceCreateDeviceRGB ();
86
87 if (alpha_info)
88 *alpha_info = kCGImageAlphaPremultipliedFirst;
89 break;
90
91 case 1:
92 if (bits_per_component)
93 *bits_per_component = 8;
94
95 if (bits_per_pixel)
96 *bits_per_pixel = 8;
97
98 if (bytes_per_row)
99 *bytes_per_row = impl->width;
100
101 if (colorspace)
102 *colorspace = CGColorSpaceCreateWithName (kCGColorSpaceGenericGray);
103
104 if (alpha_info)
105 *alpha_info = kCGImageAlphaNone;
106 break;
107
108 default:
109 g_assert_not_reached ();
110 break;
111 }
112 }
113
114 static CGContextRef
115 gdk_pixmap_impl_quartz_get_context (GdkDrawable *drawable,
116 gboolean antialias)
117 {
118 GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (drawable);
119 CGContextRef cg_context;
120 gint bits_per_component, bytes_per_row;
121 CGColorSpaceRef colorspace;
122 CGImageAlphaInfo alpha_info;
123
124 gdk_pixmap_impl_quartz_get_image_parameters (GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper,
125 &bits_per_component,
126 NULL,
127 &bytes_per_row,
128 &colorspace,
129 &alpha_info);
130
131 cg_context = CGBitmapContextCreate (impl->data,
132 impl->width, impl->height,
133 bits_per_component,
134 bytes_per_row,
135 colorspace,
136 alpha_info);
137
138 if (cg_context)
139 {
140 CGContextSetAllowsAntialiasing (cg_context, antialias);
141
142 CGColorSpaceRelease (colorspace);
143
144 /* convert coordinates from core graphics to gtk+ */
145 CGContextTranslateCTM (cg_context, 0, impl->height);
146 CGContextScaleCTM (cg_context, 1.0, -1.0);
147 }
148 return cg_context;
149 }
150
151 static void
152 gdk_pixmap_impl_quartz_finalize (GObject *object)
153 {
154 GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (object);
155
156 CGDataProviderRelease (impl->data_provider);
157
158 _gdk_quartz_drawable_finish (GDK_DRAWABLE (impl));
159
160 G_OBJECT_CLASS (parent_class)->finalize (object);
161 }
162
163 static void
164 gdk_pixmap_impl_quartz_class_init (GdkPixmapImplQuartzClass *klass)
165 {
166 GObjectClass *object_class = G_OBJECT_CLASS (klass);
167 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
168 GdkDrawableImplQuartzClass *drawable_quartz_class = GDK_DRAWABLE_IMPL_QUARTZ_CLASS (klass);
169
170 parent_class = g_type_class_peek_parent (klass);
171
172 object_class->finalize = gdk_pixmap_impl_quartz_finalize;
173
174 drawable_class->get_size = gdk_pixmap_impl_quartz_get_size;
175
176 drawable_quartz_class->get_context = gdk_pixmap_impl_quartz_get_context;
177 }
178
179 GType
180 _gdk_pixmap_impl_quartz_get_type (void)
181 {
182 static GType object_type = 0;
183
184 if (!object_type)
185 {
186 const GTypeInfo object_info =
187 {
188 sizeof (GdkPixmapImplQuartzClass),
189 (GBaseInitFunc) NULL,
190 (GBaseFinalizeFunc) NULL,
191 (GClassInitFunc) gdk_pixmap_impl_quartz_class_init,
192 NULL, /* class_finalize */
193 NULL, /* class_data */
194 sizeof (GdkPixmapImplQuartz),
195 0, /* n_preallocs */
196 (GInstanceInitFunc) gdk_pixmap_impl_quartz_init
197 };
198
199 object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_QUARTZ,
200 "GdkPixmapImplQuartz",
201 &object_info,
202 0);
203 }
204
205 return object_type;
206 }
207
208 GType
209 _gdk_pixmap_impl_get_type (void)
210 {
211 return _gdk_pixmap_impl_quartz_get_type ();
212 }
213
214 static inline gboolean
215 depth_supported (int depth)
216 {
217 if (depth != 24 && depth != 32 && depth != 1)
218 {
219 g_warning ("Unsupported bit depth %d\n", depth);
220 return FALSE;
221 }
222
223 return TRUE;
224 }
225
226 CGImageRef
227 _gdk_pixmap_get_cgimage (GdkPixmap *pixmap)
228 {
229 GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
230 gint bits_per_component, bits_per_pixel, bytes_per_row;
231 CGColorSpaceRef colorspace;
232 CGImageAlphaInfo alpha_info;
233 CGImageRef image;
234
235 gdk_pixmap_impl_quartz_get_image_parameters (pixmap,
236 &bits_per_component,
237 &bits_per_pixel,
238 &bytes_per_row,
239 &colorspace,
240 &alpha_info);
241
242 image = CGImageCreate (impl->width, impl->height,
243 bits_per_component, bits_per_pixel,
244 bytes_per_row, colorspace,
245 alpha_info,
246 impl->data_provider, NULL, FALSE,
247 kCGRenderingIntentDefault);
248 CGColorSpaceRelease (colorspace);
249
250 return image;
251 }
252
253 static void
254 data_provider_release (void *info, const void *data, size_t size)
255 {
256 g_free (info);
257 }
258
259 GdkPixmap*
260 _gdk_pixmap_new (GdkDrawable *drawable,
261 gint width,
262 gint height,
263 gint depth)
264 {
265 GdkPixmap *pixmap;
266 GdkDrawableImplQuartz *draw_impl;
267 GdkPixmapImplQuartz *pix_impl;
268 gint window_depth;
269 gint bytes_per_row;
270
271 g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
272 g_return_val_if_fail ((drawable != NULL) || (depth != -1), NULL);
273 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
274
275 if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
276 return NULL;
277
278 if (!drawable)
279 drawable = gdk_screen_get_root_window (gdk_screen_get_default ());
280
281 window_depth = gdk_drawable_get_depth (GDK_DRAWABLE (drawable));
282
283 if (depth == -1)
284 depth = window_depth;
285
286 if (!depth_supported (depth))
287 return NULL;
288
289 pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
290 draw_impl = GDK_DRAWABLE_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
291 pix_impl = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
292 draw_impl->wrapper = GDK_DRAWABLE (pixmap);
293
294 g_assert (depth == 24 || depth == 32 || depth == 1);
295
296 pix_impl->width = width;
297 pix_impl->height = height;
298 GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
299
300 gdk_pixmap_impl_quartz_get_image_parameters (pixmap,
301 NULL, NULL,
302 &bytes_per_row,
303 NULL, NULL);
304
305 pix_impl->data = g_malloc (height * bytes_per_row);
306 pix_impl->data_provider = CGDataProviderCreateWithData (pix_impl->data,
307 pix_impl->data,
308 height * bytes_per_row,
309 data_provider_release);
310
311 if (depth == window_depth) {
312 GdkColormap *colormap = gdk_drawable_get_colormap (drawable);
313
314 if (colormap)
315 gdk_drawable_set_colormap (pixmap, colormap);
316 }
317
318 return pixmap;
319 }
320
321 GdkPixmap *
322 _gdk_bitmap_create_from_data (GdkDrawable *window,
323 const gchar *data,
324 gint width,
325 gint height)
326 {
327 GdkPixmap *pixmap;
328 GdkPixmapImplQuartz *impl;
329 int x, y, bpl;
330
331 g_return_val_if_fail (data != NULL, NULL);
332 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
333 g_return_val_if_fail (window == NULL || GDK_IS_DRAWABLE (window), NULL);
334
335 pixmap = gdk_pixmap_new (window, width, height, 1);
336 impl = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
337
338 /* Bytes per line: Each line consumes an integer number of bytes, possibly
339 * ignoring any excess bits. */
340 bpl = (width + 7) / 8;
341 for (y = 0; y < height; y++)
342 {
343 guchar *dst = impl->data + y * width;
344 const gchar *src = data + (y * bpl);
345 for (x = 0; x < width; x++)
346 {
347 if ((src[x / 8] >> x % 8) & 1)
348 *dst = 0xff;
349 else
350 *dst = 0;
351
352 dst++;
353 }
354 }
355
356 return pixmap;
357 }
358
359 GdkPixmap*
360 _gdk_pixmap_create_from_data (GdkDrawable *drawable,
361 const gchar *data,
362 gint width,
363 gint height,
364 gint depth,
365 const GdkColor *fg,
366 const GdkColor *bg)
367 {
368 /* FIXME: Implement */
369 return NULL;
370 }
371
372 GdkPixmap *
373 gdk_pixmap_foreign_new_for_display (GdkDisplay *display,
374 GdkNativeWindow anid)
375 {
376 return NULL;
377 }
378
379 GdkPixmap*
380 gdk_pixmap_foreign_new (GdkNativeWindow anid)
381 {
382 return NULL;
383 }
384
385 GdkPixmap *
386 gdk_pixmap_foreign_new_for_screen (GdkScreen *screen,
387 GdkNativeWindow anid,
388 gint width,
389 gint height,
390 gint depth)
391 {
392 return NULL;
393 }
394
395 GdkPixmap*
396 gdk_pixmap_lookup (GdkNativeWindow anid)
397 {
398 return NULL;
399 }
400
401 GdkPixmap*
402 gdk_pixmap_lookup_for_display (GdkDisplay *display, GdkNativeWindow anid)
403 {
404 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
405 return NULL;
406 }