2005-11-22 10:03:32 +00:00
|
|
|
/* gdkdrawable-quartz.c
|
|
|
|
*
|
2007-03-10 21:58:49 +00:00
|
|
|
* Copyright (C) 2005-2007 Imendio AB
|
2005-11-22 10:03:32 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2009-02-09 14:46:30 +00:00
|
|
|
#include <sys/time.h>
|
2006-04-16 19:33:11 +00:00
|
|
|
#include <cairo-quartz.h>
|
2005-11-22 10:03:32 +00:00
|
|
|
#include "gdkprivate-quartz.h"
|
|
|
|
|
|
|
|
static gpointer parent_class;
|
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
static cairo_user_data_key_t gdk_quartz_cairo_key;
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
typedef struct {
|
|
|
|
GdkDrawable *drawable;
|
|
|
|
CGContextRef cg_context;
|
|
|
|
} GdkQuartzCairoSurfaceData;
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2010-08-08 16:17:29 +00:00
|
|
|
gboolean
|
2009-01-05 21:42:36 +00:00
|
|
|
_gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
/* This is not supported with quartz surfaces. */
|
2010-08-08 16:17:29 +00:00
|
|
|
return FALSE;
|
2009-02-18 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_cairo_surface_destroy (void *data)
|
|
|
|
{
|
|
|
|
GdkQuartzCairoSurfaceData *surface_data = data;
|
|
|
|
GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (surface_data->drawable);
|
|
|
|
|
|
|
|
impl->cairo_surface = NULL;
|
|
|
|
|
|
|
|
gdk_quartz_drawable_release_context (surface_data->drawable,
|
|
|
|
surface_data->cg_context);
|
|
|
|
|
|
|
|
g_free (surface_data);
|
2009-01-05 21:42:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 20:16:28 +00:00
|
|
|
static cairo_surface_t *
|
|
|
|
gdk_quartz_create_cairo_surface (GdkDrawable *drawable,
|
|
|
|
int width,
|
|
|
|
int height)
|
2009-01-05 21:42:36 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
CGContextRef cg_context;
|
|
|
|
GdkQuartzCairoSurfaceData *surface_data;
|
|
|
|
cairo_surface_t *surface;
|
2009-01-05 21:42:36 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
cg_context = gdk_quartz_drawable_get_context (drawable, TRUE);
|
2009-01-20 13:04:03 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
if (!cg_context)
|
|
|
|
return NULL;
|
2009-01-05 21:42:36 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
surface_data = g_new (GdkQuartzCairoSurfaceData, 1);
|
|
|
|
surface_data->drawable = drawable;
|
|
|
|
surface_data->cg_context = cg_context;
|
2009-01-20 13:04:03 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
surface = cairo_quartz_surface_create_for_cg_context (cg_context,
|
|
|
|
width, height);
|
2009-01-05 21:42:36 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
cairo_surface_set_user_data (surface, &gdk_quartz_cairo_key,
|
|
|
|
surface_data,
|
|
|
|
gdk_quartz_cairo_surface_destroy);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
return surface;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static cairo_surface_t *
|
|
|
|
gdk_quartz_ref_cairo_surface (GdkDrawable *drawable)
|
|
|
|
{
|
|
|
|
GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
|
|
|
|
|
|
|
|
if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable) &&
|
|
|
|
GDK_WINDOW_DESTROYED (impl->wrapper))
|
|
|
|
return NULL;
|
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
if (!impl->cairo_surface)
|
|
|
|
{
|
2010-09-20 10:53:06 +00:00
|
|
|
impl->cairo_surface =
|
|
|
|
gdk_quartz_create_cairo_surface (drawable,
|
|
|
|
gdk_window_get_width (impl->wrapper),
|
|
|
|
gdk_window_get_height (impl->wrapper));
|
2007-03-10 21:58:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cairo_surface_reference (impl->cairo_surface);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
return impl->cairo_surface;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_drawable_impl_quartz_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_drawable_impl_quartz_class_init (GdkDrawableImplQuartzClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gdk_drawable_impl_quartz_finalize;
|
|
|
|
|
|
|
|
drawable_class->ref_cairo_surface = gdk_quartz_ref_cairo_surface;
|
2010-08-25 20:16:28 +00:00
|
|
|
drawable_class->create_cairo_surface = gdk_quartz_create_cairo_surface;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
|
|
|
gdk_drawable_impl_quartz_get_type (void)
|
|
|
|
{
|
|
|
|
static GType object_type = 0;
|
|
|
|
|
|
|
|
if (!object_type)
|
|
|
|
{
|
2009-11-06 00:21:09 +00:00
|
|
|
const GTypeInfo object_info =
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
|
|
|
sizeof (GdkDrawableImplQuartzClass),
|
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gdk_drawable_impl_quartz_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GdkDrawableImplQuartz),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
|
|
|
|
"GdkDrawableImplQuartz",
|
|
|
|
&object_info, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return object_type;
|
|
|
|
}
|
|
|
|
|
2008-07-20 16:14:35 +00:00
|
|
|
CGContextRef
|
2006-08-08 09:12:10 +00:00
|
|
|
gdk_quartz_drawable_get_context (GdkDrawable *drawable,
|
|
|
|
gboolean antialias)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2008-07-20 16:14:35 +00:00
|
|
|
if (!GDK_DRAWABLE_IMPL_QUARTZ_GET_CLASS (drawable)->get_context)
|
2007-03-10 21:58:49 +00:00
|
|
|
{
|
2008-07-20 16:14:35 +00:00
|
|
|
g_warning ("%s doesn't implement GdkDrawableImplQuartzClass::get_context()",
|
|
|
|
G_OBJECT_TYPE_NAME (drawable));
|
|
|
|
return NULL;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:14:35 +00:00
|
|
|
return GDK_DRAWABLE_IMPL_QUARTZ_GET_CLASS (drawable)->get_context (drawable, antialias);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
/* Help preventing "beam sync penalty" where CG makes all graphics code
|
2009-02-09 14:46:30 +00:00
|
|
|
* block until the next vsync if we try to flush (including call display on
|
|
|
|
* a view) too often. We do this by limiting the manual flushing done
|
2009-02-18 15:20:06 +00:00
|
|
|
* outside of expose calls to less than some frequency when measured over
|
|
|
|
* the last 4 flushes. This is a bit arbitray, but seems to make it possible
|
|
|
|
* for some quick manual flushes (such as gtkruler or gimp's marching ants)
|
|
|
|
* without hitting the max flush frequency.
|
2009-02-09 14:46:30 +00:00
|
|
|
*
|
2009-02-18 15:20:06 +00:00
|
|
|
* If drawable NULL, no flushing is done, only registering that a flush was
|
|
|
|
* done externally.
|
2009-02-09 14:46:30 +00:00
|
|
|
*/
|
|
|
|
void
|
2009-02-18 15:20:06 +00:00
|
|
|
_gdk_quartz_drawable_flush (GdkDrawable *drawable)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
|
|
|
static struct timeval prev_tv;
|
2009-02-18 15:20:06 +00:00
|
|
|
static gint intervals[4];
|
|
|
|
static gint index;
|
2009-02-09 14:46:30 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gint ms;
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
2009-02-18 15:20:06 +00:00
|
|
|
ms = (tv.tv_sec - prev_tv.tv_sec) * 1000 + (tv.tv_usec - prev_tv.tv_usec) / 1000;
|
|
|
|
intervals[index++ % 4] = ms;
|
2009-02-09 14:46:30 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
if (drawable)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
ms = intervals[0] + intervals[1] + intervals[2] + intervals[3];
|
2009-02-09 14:46:30 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
/* ~25Hz on average. */
|
|
|
|
if (ms > 4*40)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
|
|
|
|
{
|
|
|
|
GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
|
|
|
|
|
|
|
|
[window_impl->toplevel flushWindow];
|
|
|
|
}
|
|
|
|
|
2009-02-09 14:46:30 +00:00
|
|
|
prev_tv = tv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev_tv = tv;
|
|
|
|
}
|
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
void
|
2007-03-10 21:58:49 +00:00
|
|
|
gdk_quartz_drawable_release_context (GdkDrawable *drawable,
|
|
|
|
CGContextRef cg_context)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
|
|
|
if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
|
|
|
|
{
|
2007-03-10 21:58:49 +00:00
|
|
|
GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
CGContextRestoreGState (cg_context);
|
|
|
|
CGContextSetAllowsAntialiasing (cg_context, TRUE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-05-05 10:19:39 +00:00
|
|
|
/* See comment in gdk_quartz_drawable_get_context(). */
|
|
|
|
if (window_impl->in_paint_rect_count == 0)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
_gdk_quartz_drawable_flush (drawable);
|
2009-02-09 14:46:30 +00:00
|
|
|
[window_impl->view unlockFocus];
|
|
|
|
}
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
2007-03-10 21:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gdk_quartz_drawable_finish (GdkDrawable *drawable)
|
|
|
|
{
|
|
|
|
GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
|
|
|
|
|
|
|
|
if (impl->cairo_surface)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2007-03-10 21:58:49 +00:00
|
|
|
cairo_surface_finish (impl->cairo_surface);
|
|
|
|
cairo_surface_set_user_data (impl->cairo_surface, &gdk_quartz_cairo_key,
|
|
|
|
NULL, NULL);
|
|
|
|
impl->cairo_surface = NULL;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
2007-03-10 21:58:49 +00:00
|
|
|
}
|