Debian Codegraph @

gdk/quartz/gdkdrawable-quartz.c

raw
1 /* gdkdrawable-quartz.c
2 *
3 * Copyright (C) 2005-2007 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 #include <sys/time.h>
23 #include <cairo-quartz.h>
24 #include "gdkprivate-quartz.h"
25
26 static gpointer parent_class;
27
28 static cairo_user_data_key_t gdk_quartz_cairo_key;
29
30 typedef struct {
31 GdkDrawable *drawable;
32 CGContextRef cg_context;
33 } GdkQuartzCairoSurfaceData;
34
35 void
36 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
37 int width,
38 int height)
39 {
40 /* This is not supported with quartz surfaces. */
41 }
42
43 static void
44 gdk_quartz_cairo_surface_destroy (void *data)
45 {
46 GdkQuartzCairoSurfaceData *surface_data = data;
47 GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (surface_data->drawable);
48
49 impl->cairo_surface = NULL;
50
51 gdk_quartz_drawable_release_context (surface_data->drawable,
52 surface_data->cg_context);
53
54 g_free (surface_data);
55 }
56
57 cairo_surface_t *
58 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
59 int width,
60 int height)
61 {
62 CGContextRef cg_context;
63 GdkQuartzCairoSurfaceData *surface_data;
64 cairo_surface_t *surface;
65
66 cg_context = gdk_quartz_drawable_get_context (drawable, TRUE);
67
68 surface_data = g_new (GdkQuartzCairoSurfaceData, 1);
69 surface_data->drawable = drawable;
70 surface_data->cg_context = cg_context;
71
72 if (cg_context)
73 surface = cairo_quartz_surface_create_for_cg_context (cg_context,
74 width, height);
75 else
76 surface = cairo_quartz_surface_create(CAIRO_FORMAT_ARGB32, width, height);
77
78 cairo_surface_set_user_data (surface, &gdk_quartz_cairo_key,
79 surface_data,
80 gdk_quartz_cairo_surface_destroy);
81
82 return surface;
83 }
84
85 static cairo_surface_t *
86 gdk_quartz_ref_cairo_surface (GdkDrawable *drawable)
87 {
88 GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
89
90 if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable) &&
91 GDK_WINDOW_DESTROYED (impl->wrapper))
92 return NULL;
93
94 if (!impl->cairo_surface)
95 {
96 int width, height;
97
98 gdk_drawable_get_size (drawable, &width, &height);
99 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable,
100 width, height);
101 }
102 else
103 cairo_surface_reference (impl->cairo_surface);
104
105 return impl->cairo_surface;
106 }
107
108 static void
109 gdk_quartz_set_colormap (GdkDrawable *drawable,
110 GdkColormap *colormap)
111 {
112 GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
113
114 if (impl->colormap == colormap)
115 return;
116
117 if (impl->colormap)
118 g_object_unref (impl->colormap);
119 impl->colormap = colormap;
120 if (impl->colormap)
121 g_object_ref (impl->colormap);
122 }
123
124 static GdkColormap*
125 gdk_quartz_get_colormap (GdkDrawable *drawable)
126 {
127 return GDK_DRAWABLE_IMPL_QUARTZ (drawable)->colormap;
128 }
129
130 static GdkScreen*
131 gdk_quartz_get_screen (GdkDrawable *drawable)
132 {
133 return _gdk_screen;
134 }
135
136 static GdkVisual*
137 gdk_quartz_get_visual (GdkDrawable *drawable)
138 {
139 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper);
140 }
141
142 static int
143 gdk_quartz_get_depth (GdkDrawable *drawable)
144 {
145 /* This is a bit bogus but I'm not sure the other way is better */
146
147 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper);
148 }
149
150 static void
151 gdk_quartz_draw_rectangle (GdkDrawable *drawable,
152 GdkGC *gc,
153 gboolean filled,
154 gint x,
155 gint y,
156 gint width,
157 gint height)
158 {
159 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
160
161 if (!context)
162 return;
163
164 if(!_gdk_quartz_gc_update_cg_context (gc,
165 drawable,
166 context,
167 filled ?
168 GDK_QUARTZ_CONTEXT_FILL :
169 GDK_QUARTZ_CONTEXT_STROKE))
170 {
171 gdk_quartz_drawable_release_context (drawable, context);
172 return;
173 }
174 if (filled)
175 {
176 CGRect rect = CGRectMake (x, y, width, height);
177
178 CGContextFillRect (context, rect);
179 }
180 else
181 {
182 CGRect rect = CGRectMake (x + 0.5, y + 0.5, width, height);
183
184 CGContextStrokeRect (context, rect);
185 }
186
187 gdk_quartz_drawable_release_context (drawable, context);
188 }
189
190 static void
191 gdk_quartz_draw_arc (GdkDrawable *drawable,
192 GdkGC *gc,
193 gboolean filled,
194 gint x,
195 gint y,
196 gint width,
197 gint height,
198 gint angle1,
199 gint angle2)
200 {
201 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
202 float start_angle, end_angle;
203 gboolean clockwise = FALSE;
204
205 if (!context)
206 return;
207
208 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
209 filled ?
210 GDK_QUARTZ_CONTEXT_FILL :
211 GDK_QUARTZ_CONTEXT_STROKE))
212 {
213 gdk_quartz_drawable_release_context (drawable, context);
214 return;
215 }
216 start_angle = angle1 * 2.0 * G_PI / 360.0 / 64.0;
217 end_angle = start_angle + angle2 * 2.0 * G_PI / 360.0 / 64.0;
218
219 /* angle2 is relative to angle1 and can be negative, which switches
220 * the drawing direction
221 */
222 if (angle2 < 0)
223 clockwise = TRUE;
224
225 /* below, flip the coordinate system back to its original y-diretion
226 * so the angles passed to CGContextAddArc() are interpreted as
227 * expected
228 *
229 * FIXME: the implementation below works only for perfect circles
230 * (width == height). Any other aspect ratio either scales the
231 * line width unevenly or scales away the path entirely for very
232 * small line widths (esp. for line_width == 0, which is a hair
233 * line on X11 but must be approximated with the thinnest possible
234 * line on quartz).
235 */
236
237 if (filled)
238 {
239 CGContextTranslateCTM (context,
240 x + width / 2.0,
241 y + height / 2.0);
242 CGContextScaleCTM (context, 1.0, - (double)height / (double)width);
243
244 CGContextMoveToPoint (context, 0, 0);
245 CGContextAddArc (context, 0, 0, width / 2.0,
246 start_angle, end_angle,
247 clockwise);
248 CGContextClosePath (context);
249 CGContextFillPath (context);
250 }
251 else
252 {
253 CGContextTranslateCTM (context,
254 x + width / 2.0 + 0.5,
255 y + height / 2.0 + 0.5);
256 CGContextScaleCTM (context, 1.0, - (double)height / (double)width);
257
258 CGContextAddArc (context, 0, 0, width / 2.0,
259 start_angle, end_angle,
260 clockwise);
261 CGContextStrokePath (context);
262 }
263
264 gdk_quartz_drawable_release_context (drawable, context);
265 }
266
267 static void
268 gdk_quartz_draw_polygon (GdkDrawable *drawable,
269 GdkGC *gc,
270 gboolean filled,
271 GdkPoint *points,
272 gint npoints)
273 {
274 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
275 int i;
276
277 if (!context)
278 return;
279
280 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
281 filled ?
282 GDK_QUARTZ_CONTEXT_FILL :
283 GDK_QUARTZ_CONTEXT_STROKE))
284 {
285 gdk_quartz_drawable_release_context (drawable, context);
286 return;
287 }
288 if (filled)
289 {
290 CGContextMoveToPoint (context, points[0].x, points[0].y);
291 for (i = 1; i < npoints; i++)
292 CGContextAddLineToPoint (context, points[i].x, points[i].y);
293
294 CGContextClosePath (context);
295 CGContextFillPath (context);
296 }
297 else
298 {
299 CGContextMoveToPoint (context, points[0].x + 0.5, points[0].y + 0.5);
300 for (i = 1; i < npoints; i++)
301 CGContextAddLineToPoint (context, points[i].x + 0.5, points[i].y + 0.5);
302
303 CGContextClosePath (context);
304 CGContextStrokePath (context);
305 }
306
307 gdk_quartz_drawable_release_context (drawable, context);
308 }
309
310 static void
311 gdk_quartz_draw_text (GdkDrawable *drawable,
312 GdkFont *font,
313 GdkGC *gc,
314 gint x,
315 gint y,
316 const gchar *text,
317 gint text_length)
318 {
319 /* FIXME: Implement */
320 }
321
322 static void
323 gdk_quartz_draw_text_wc (GdkDrawable *drawable,
324 GdkFont *font,
325 GdkGC *gc,
326 gint x,
327 gint y,
328 const GdkWChar *text,
329 gint text_length)
330 {
331 /* FIXME: Implement */
332 }
333
334 static void
335 gdk_quartz_draw_drawable (GdkDrawable *drawable,
336 GdkGC *gc,
337 GdkPixmap *src,
338 gint xsrc,
339 gint ysrc,
340 gint xdest,
341 gint ydest,
342 gint width,
343 gint height,
344 GdkDrawable *original_src)
345 {
346 int src_depth = gdk_drawable_get_depth (src);
347 int dest_depth = gdk_drawable_get_depth (drawable);
348 GdkDrawableImplQuartz *src_impl;
349
350 if (GDK_IS_WINDOW_IMPL_QUARTZ (src))
351 {
352 GdkWindowImplQuartz *window_impl;
353
354 window_impl = GDK_WINDOW_IMPL_QUARTZ (src);
355
356 /* We do support moving areas on the same drawable, if it can be done
357 * by using a scroll. FIXME: We need to check that the params support
358 * this hack, and make sure it's done properly with any offsets etc?
359 */
360 if (drawable == (GdkDrawable *)window_impl)
361 {
362 NSRect rect = NSMakeRect (xsrc, ysrc, width, height);
363 NSSize offset = NSMakeSize (xdest - xsrc, ydest - ysrc);
364 GdkRectangle tmp_rect;
365 GdkRegion *orig_region, *offset_region, *need_display_region;
366 GdkWindow *window = GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper;
367
368 /* Origin region */
369 tmp_rect.x = xsrc;
370 tmp_rect.y = ysrc;
371 tmp_rect.width = width;
372 tmp_rect.height = height;
373 orig_region = gdk_region_rectangle (&tmp_rect);
374
375 /* Destination region (or the offset region) */
376 offset_region = gdk_region_copy (orig_region);
377 gdk_region_offset (offset_region, offset.width, offset.height);
378
379 need_display_region = gdk_region_copy (orig_region);
380
381 if (window_impl->in_paint_rect_count == 0)
382 {
383 GdkRegion *bottom_border_region;
384
385 /* If we are not in drawRect:, we can use scrollRect:.
386 * We apply scrollRect on the rectangle to be moved and
387 * subtract this area from the rectangle that needs display.
388 *
389 * Note: any area in this moved region that already needed
390 * display will be handled by GDK (queue translation).
391 *
392 * Queuing the redraw below is important, otherwise the
393 * results from scrollRect will not take effect!
394 */
395 [window_impl->view scrollRect:rect by:offset];
396
397 gdk_region_subtract (need_display_region, offset_region);
398
399 /* Here we take special care with the bottom window border,
400 * which extents 4 pixels and typically draws rounded corners.
401 */
402 tmp_rect.x = 0;
403 tmp_rect.y = gdk_window_get_height (window) - 4;
404 tmp_rect.width = gdk_window_get_width (window);
405 tmp_rect.height = 4;
406
407 if (gdk_region_rect_in (offset_region, &tmp_rect) !=
408 GDK_OVERLAP_RECTANGLE_OUT)
409 {
410 /* We are copying pixels to the bottom border, we need
411 * to submit this area for redisplay to get the rounded
412 * corners drawn.
413 */
414 gdk_region_union_with_rect (need_display_region,
415 &tmp_rect);
416 }
417
418 /* Compute whether the bottom border is moved elsewhere.
419 * Because this part will have rounded corners, we have
420 * to fill the contents of where the rounded corners used
421 * to be. We post this area for redisplay.
422 */
423 bottom_border_region = gdk_region_rectangle (&tmp_rect);
424 gdk_region_intersect (bottom_border_region,
425 orig_region);
426
427 gdk_region_offset (bottom_border_region,
428 offset.width, offset.height);
429
430 gdk_region_union (need_display_region, bottom_border_region);
431
432 gdk_region_destroy (bottom_border_region);
433 }
434 else
435 {
436 /* If we cannot handle things with a scroll, we must redisplay
437 * the union of the source area and the destination area.
438 */
439 gdk_region_union (need_display_region, offset_region);
440 }
441
442 _gdk_quartz_window_set_needs_display_in_region (window,
443 need_display_region);
444
445 gdk_region_destroy (orig_region);
446 gdk_region_destroy (offset_region);
447 gdk_region_destroy (need_display_region);
448 }
449 else
450 g_warning ("Drawing with window source != dest is not supported");
451
452 return;
453 }
454 else if (GDK_IS_DRAWABLE_IMPL_QUARTZ (src))
455 src_impl = GDK_DRAWABLE_IMPL_QUARTZ (src);
456 else if (GDK_IS_PIXMAP (src))
457 src_impl = GDK_DRAWABLE_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (src)->impl);
458 else
459 {
460 g_warning ("Unsupported source %s", G_OBJECT_TYPE_NAME (src));
461 return;
462 }
463
464 /* Handle drawable and pixmap sources. */
465 if (src_depth == 1)
466 {
467 /* FIXME: src depth 1 is not supported yet */
468 g_warning ("Source with depth 1 unsupported");
469 }
470 else if (dest_depth != 0 && src_depth == dest_depth)
471 {
472 GdkPixmapImplQuartz *pixmap_impl = GDK_PIXMAP_IMPL_QUARTZ (src_impl);
473 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
474 CGImageRef image;
475
476 if (!context)
477 return;
478
479 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
480 GDK_QUARTZ_CONTEXT_STROKE))
481 {
482 gdk_quartz_drawable_release_context (drawable, context);
483 return;
484 }
485 CGContextClipToRect (context, CGRectMake (xdest, ydest, width, height));
486 CGContextTranslateCTM (context, xdest - xsrc, ydest - ysrc +
487 pixmap_impl->height);
488 CGContextScaleCTM (context, 1.0, -1.0);
489
490 image = _gdk_pixmap_get_cgimage (src);
491 CGContextDrawImage (context,
492 CGRectMake (0, 0, pixmap_impl->width, pixmap_impl->height),
493 image);
494 CGImageRelease (image);
495
496 gdk_quartz_drawable_release_context (drawable, context);
497 }
498 else
499 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
500 src_depth, dest_depth);
501 }
502
503 static void
504 gdk_quartz_draw_points (GdkDrawable *drawable,
505 GdkGC *gc,
506 GdkPoint *points,
507 gint npoints)
508 {
509 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
510 int i;
511
512 if (!context)
513 return;
514
515 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
516 GDK_QUARTZ_CONTEXT_STROKE |
517 GDK_QUARTZ_CONTEXT_FILL))
518 {
519 gdk_quartz_drawable_release_context (drawable, context);
520 return;
521 }
522 /* Just draw 1x1 rectangles */
523 for (i = 0; i < npoints; i++)
524 {
525 CGRect rect = CGRectMake (points[i].x, points[i].y, 1, 1);
526 CGContextFillRect (context, rect);
527 }
528
529 gdk_quartz_drawable_release_context (drawable, context);
530 }
531
532 static inline void
533 gdk_quartz_fix_cap_not_last_line (GdkGCQuartz *private,
534 gint x1,
535 gint y1,
536 gint x2,
537 gint y2,
538 gint *xfix,
539 gint *yfix)
540 {
541 *xfix = 0;
542 *yfix = 0;
543
544 if (private->cap_style == GDK_CAP_NOT_LAST && private->line_width == 0)
545 {
546 /* fix only vertical and horizontal lines for now */
547
548 if (y1 == y2 && x1 != x2)
549 {
550 *xfix = (x1 < x2) ? -1 : 1;
551 }
552 else if (x1 == x2 && y1 != y2)
553 {
554 *yfix = (y1 < y2) ? -1 : 1;
555 }
556 }
557 }
558
559 static void
560 gdk_quartz_draw_segments (GdkDrawable *drawable,
561 GdkGC *gc,
562 GdkSegment *segs,
563 gint nsegs)
564 {
565 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
566 GdkGCQuartz *private;
567 int i;
568
569 if (!context)
570 return;
571
572 private = GDK_GC_QUARTZ (gc);
573
574 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
575 GDK_QUARTZ_CONTEXT_STROKE))
576 {
577 gdk_quartz_drawable_release_context (drawable, context);
578 return;
579 }
580 for (i = 0; i < nsegs; i++)
581 {
582 gint xfix, yfix;
583
584 gdk_quartz_fix_cap_not_last_line (private,
585 segs[i].x1, segs[i].y1,
586 segs[i].x2, segs[i].y2,
587 &xfix, &yfix);
588
589 CGContextMoveToPoint (context, segs[i].x1 + 0.5, segs[i].y1 + 0.5);
590 CGContextAddLineToPoint (context, segs[i].x2 + 0.5 + xfix, segs[i].y2 + 0.5 + yfix);
591 }
592
593 CGContextStrokePath (context);
594
595 gdk_quartz_drawable_release_context (drawable, context);
596 }
597
598 static void
599 gdk_quartz_draw_lines (GdkDrawable *drawable,
600 GdkGC *gc,
601 GdkPoint *points,
602 gint npoints)
603 {
604 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
605 GdkGCQuartz *private;
606 gint xfix, yfix;
607 gint i;
608
609 if (!context)
610 return;
611
612 private = GDK_GC_QUARTZ (gc);
613
614 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
615 GDK_QUARTZ_CONTEXT_STROKE))
616 {
617 gdk_quartz_drawable_release_context (drawable, context);
618 return;
619 }
620 CGContextMoveToPoint (context, points[0].x + 0.5, points[0].y + 0.5);
621
622 for (i = 1; i < npoints - 1; i++)
623 CGContextAddLineToPoint (context, points[i].x + 0.5, points[i].y + 0.5);
624
625 gdk_quartz_fix_cap_not_last_line (private,
626 points[npoints - 2].x, points[npoints - 2].y,
627 points[npoints - 1].x, points[npoints - 1].y,
628 &xfix, &yfix);
629
630 CGContextAddLineToPoint (context,
631 points[npoints - 1].x + 0.5 + xfix,
632 points[npoints - 1].y + 0.5 + yfix);
633
634 CGContextStrokePath (context);
635
636 gdk_quartz_drawable_release_context (drawable, context);
637 }
638
639 static void
640 gdk_quartz_draw_pixbuf (GdkDrawable *drawable,
641 GdkGC *gc,
642 GdkPixbuf *pixbuf,
643 gint src_x,
644 gint src_y,
645 gint dest_x,
646 gint dest_y,
647 gint width,
648 gint height,
649 GdkRgbDither dither,
650 gint x_dither,
651 gint y_dither)
652 {
653 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
654 CGColorSpaceRef colorspace;
655 CGDataProviderRef data_provider;
656 CGImageRef image;
657 void *data;
658 int rowstride, pixbuf_width, pixbuf_height;
659 gboolean has_alpha;
660
661 if (!context)
662 return;
663
664 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
665 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
666 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
667 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
668
669 data = gdk_pixbuf_get_pixels (pixbuf);
670
671 colorspace = CGColorSpaceCreateDeviceRGB ();
672 data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
673
674 image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
675 has_alpha ? 32 : 24, rowstride,
676 colorspace,
677 has_alpha ? kCGImageAlphaLast : 0,
678 data_provider, NULL, FALSE,
679 kCGRenderingIntentDefault);
680
681 CGDataProviderRelease (data_provider);
682 CGColorSpaceRelease (colorspace);
683
684 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
685 GDK_QUARTZ_CONTEXT_STROKE))
686 {
687 gdk_quartz_drawable_release_context (drawable, context);
688 return;
689 }
690 CGContextClipToRect (context, CGRectMake (dest_x, dest_y, width, height));
691 CGContextTranslateCTM (context, dest_x - src_x, dest_y - src_y + pixbuf_height);
692 CGContextScaleCTM (context, 1, -1);
693
694 CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
695 CGImageRelease (image);
696
697 gdk_quartz_drawable_release_context (drawable, context);
698 }
699
700 static void
701 gdk_quartz_draw_image (GdkDrawable *drawable,
702 GdkGC *gc,
703 GdkImage *image,
704 gint xsrc,
705 gint ysrc,
706 gint xdest,
707 gint ydest,
708 gint width,
709 gint height)
710 {
711 CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
712 CGColorSpaceRef colorspace;
713 CGDataProviderRef data_provider;
714 CGImageRef cgimage;
715
716 if (!context)
717 return;
718
719 colorspace = CGColorSpaceCreateDeviceRGB ();
720 data_provider = CGDataProviderCreateWithData (NULL, image->mem, image->height * image->bpl, NULL);
721
722 /* FIXME: Make sure that this function draws 32-bit images correctly,
723 * also check endianness wrt kCGImageAlphaNoneSkipFirst */
724 cgimage = CGImageCreate (image->width, image->height, 8,
725 32, image->bpl,
726 colorspace,
727 kCGImageAlphaNoneSkipFirst,
728 data_provider, NULL, FALSE, kCGRenderingIntentDefault);
729
730 CGDataProviderRelease (data_provider);
731 CGColorSpaceRelease (colorspace);
732
733 if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context,
734 GDK_QUARTZ_CONTEXT_STROKE))
735 {
736 gdk_quartz_drawable_release_context (drawable, context);
737 return;
738 }
739
740 CGContextClipToRect (context, CGRectMake (xdest, ydest, width, height));
741 CGContextTranslateCTM (context, xdest - xsrc, ydest - ysrc + image->height);
742 CGContextScaleCTM (context, 1, -1);
743
744 CGContextDrawImage (context, CGRectMake (0, 0, image->width, image->height), cgimage);
745 CGImageRelease (cgimage);
746
747 gdk_quartz_drawable_release_context (drawable, context);
748 }
749
750 static void
751 gdk_drawable_impl_quartz_finalize (GObject *object)
752 {
753 GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (object);
754
755 if (impl->colormap)
756 g_object_unref (impl->colormap);
757
758 G_OBJECT_CLASS (parent_class)->finalize (object);
759 }
760
761 static void
762 gdk_drawable_impl_quartz_class_init (GdkDrawableImplQuartzClass *klass)
763 {
764 GObjectClass *object_class = G_OBJECT_CLASS (klass);
765 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
766
767 parent_class = g_type_class_peek_parent (klass);
768
769 object_class->finalize = gdk_drawable_impl_quartz_finalize;
770
771 drawable_class->create_gc = _gdk_quartz_gc_new;
772 drawable_class->draw_rectangle = gdk_quartz_draw_rectangle;
773 drawable_class->draw_arc = gdk_quartz_draw_arc;
774 drawable_class->draw_polygon = gdk_quartz_draw_polygon;
775 drawable_class->draw_text = gdk_quartz_draw_text;
776 drawable_class->draw_text_wc = gdk_quartz_draw_text_wc;
777 drawable_class->draw_drawable_with_src = gdk_quartz_draw_drawable;
778 drawable_class->draw_points = gdk_quartz_draw_points;
779 drawable_class->draw_segments = gdk_quartz_draw_segments;
780 drawable_class->draw_lines = gdk_quartz_draw_lines;
781 drawable_class->draw_image = gdk_quartz_draw_image;
782 drawable_class->draw_pixbuf = gdk_quartz_draw_pixbuf;
783
784 drawable_class->ref_cairo_surface = gdk_quartz_ref_cairo_surface;
785
786 drawable_class->set_colormap = gdk_quartz_set_colormap;
787 drawable_class->get_colormap = gdk_quartz_get_colormap;
788
789 drawable_class->get_depth = gdk_quartz_get_depth;
790 drawable_class->get_screen = gdk_quartz_get_screen;
791 drawable_class->get_visual = gdk_quartz_get_visual;
792
793 drawable_class->_copy_to_image = _gdk_quartz_image_copy_to_image;
794 }
795
796 GType
797 gdk_drawable_impl_quartz_get_type (void)
798 {
799 static GType object_type = 0;
800
801 if (!object_type)
802 {
803 const GTypeInfo object_info =
804 {
805 sizeof (GdkDrawableImplQuartzClass),
806 (GBaseInitFunc) NULL,
807 (GBaseFinalizeFunc) NULL,
808 (GClassInitFunc) gdk_drawable_impl_quartz_class_init,
809 NULL, /* class_finalize */
810 NULL, /* class_data */
811 sizeof (GdkDrawableImplQuartz),
812 0, /* n_preallocs */
813 (GInstanceInitFunc) NULL,
814 };
815
816 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
817 "GdkDrawableImplQuartz",
818 &object_info, 0);
819 }
820
821 return object_type;
822 }
823
824 CGContextRef
825 gdk_quartz_drawable_get_context (GdkDrawable *drawable,
826 gboolean antialias)
827 {
828 if (!GDK_DRAWABLE_IMPL_QUARTZ_GET_CLASS (drawable)->get_context)
829 {
830 g_warning ("%s doesn't implement GdkDrawableImplQuartzClass::get_context()",
831 G_OBJECT_TYPE_NAME (drawable));
832 return NULL;
833 }
834
835 return GDK_DRAWABLE_IMPL_QUARTZ_GET_CLASS (drawable)->get_context (drawable, antialias);
836 }
837
838 /* Help preventing "beam sync penalty" where CG makes all graphics code
839 * block until the next vsync if we try to flush (including call display on
840 * a view) too often. We do this by limiting the manual flushing done
841 * outside of expose calls to less than some frequency when measured over
842 * the last 4 flushes. This is a bit arbitray, but seems to make it possible
843 * for some quick manual flushes (such as gtkruler or gimp's marching ants)
844 * without hitting the max flush frequency.
845 *
846 * If drawable NULL, no flushing is done, only registering that a flush was
847 * done externally.
848 */
849 void
850 _gdk_quartz_drawable_flush (GdkDrawable *drawable)
851 {
852 static struct timeval prev_tv;
853 static gint intervals[4];
854 static gint index;
855 struct timeval tv;
856 gint ms;
857
858 gettimeofday (&tv, NULL);
859 ms = (tv.tv_sec - prev_tv.tv_sec) * 1000 + (tv.tv_usec - prev_tv.tv_usec) / 1000;
860 intervals[index++ % 4] = ms;
861
862 if (drawable)
863 {
864 ms = intervals[0] + intervals[1] + intervals[2] + intervals[3];
865
866 /* ~25Hz on average. */
867 if (ms > 4*40)
868 {
869 if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
870 {
871 GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
872
873 [window_impl->toplevel flushWindow];
874 }
875
876 prev_tv = tv;
877 }
878 }
879 else
880 prev_tv = tv;
881 }
882
883 void
884 gdk_quartz_drawable_release_context (GdkDrawable *drawable,
885 CGContextRef cg_context)
886 {
887 if (!cg_context)
888 return;
889
890 if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
891 {
892 GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
893
894 CGContextRestoreGState (cg_context);
895 CGContextSetAllowsAntialiasing (cg_context, TRUE);
896
897 /* See comment in gdk_quartz_drawable_get_context(). */
898 if (window_impl->in_paint_rect_count == 0)
899 {
900 _gdk_quartz_drawable_flush (drawable);
901 [window_impl->view unlockFocus];
902 }
903 }
904 else if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable))
905 CGContextRelease (cg_context);
906 }
907
908 void
909 _gdk_quartz_drawable_finish (GdkDrawable *drawable)
910 {
911 GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
912
913 if (impl->cairo_surface)
914 {
915 cairo_surface_finish (impl->cairo_surface);
916 cairo_surface_set_user_data (impl->cairo_surface, &gdk_quartz_cairo_key,
917 NULL, NULL);
918 impl->cairo_surface = NULL;
919 }
920 }