| 1 |
/* gdkdisplaylinksource.c |
| 2 |
* |
| 3 |
* Copyright (C) 2015 Christian Hergert <[email protected]> |
| 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, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* Authors: |
| 19 |
* Christian Hergert <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <mach/mach_time.h> |
| 25 |
|
| 26 |
#include "gdkinternal-quartz.h" |
| 27 |
#include "gdkdisplaylinksource.h" |
| 28 |
|
| 29 |
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200 |
| 30 |
#define GDK_QUARTZ_APPLICATION_DEFINED NSApplicationDefined |
| 31 |
#else |
| 32 |
#define GDK_QUARTZ_APPLICATION_DEFINED NSEventTypeApplicationDefined |
| 33 |
#endif |
| 34 |
|
| 35 |
static gint64 host_to_frame_clock_time (gint64 host_time); |
| 36 |
|
| 37 |
static gboolean |
| 38 |
gdk_display_link_source_prepare (GSource *source, |
| 39 |
gint *timeout_) |
| 40 |
{ |
| 41 |
GdkDisplayLinkSource *impl = (GdkDisplayLinkSource *)source; |
| 42 |
gint64 now; |
| 43 |
|
| 44 |
now = g_source_get_time (source); |
| 45 |
|
| 46 |
if (now < impl->presentation_time) |
| 47 |
*timeout_ = (impl->presentation_time - now) / 1000L; |
| 48 |
else |
| 49 |
*timeout_ = -1; |
| 50 |
|
| 51 |
return impl->needs_dispatch; |
| 52 |
} |
| 53 |
|
| 54 |
static gboolean |
| 55 |
gdk_display_link_source_check (GSource *source) |
| 56 |
{ |
| 57 |
GdkDisplayLinkSource *impl = (GdkDisplayLinkSource *)source; |
| 58 |
return impl->needs_dispatch; |
| 59 |
} |
| 60 |
|
| 61 |
static gboolean |
| 62 |
gdk_display_link_source_dispatch (GSource *source, |
| 63 |
GSourceFunc callback, |
| 64 |
gpointer user_data) |
| 65 |
{ |
| 66 |
GdkDisplayLinkSource *impl = (GdkDisplayLinkSource *)source; |
| 67 |
gboolean ret = G_SOURCE_CONTINUE; |
| 68 |
|
| 69 |
impl->needs_dispatch = FALSE; |
| 70 |
|
| 71 |
if (callback != NULL) |
| 72 |
ret = callback (user_data); |
| 73 |
|
| 74 |
return ret; |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
gdk_display_link_source_finalize (GSource *source) |
| 79 |
{ |
| 80 |
GdkDisplayLinkSource *impl = (GdkDisplayLinkSource *)source; |
| 81 |
|
| 82 |
CVDisplayLinkStop (impl->display_link); |
| 83 |
CVDisplayLinkRelease (impl->display_link); |
| 84 |
} |
| 85 |
|
| 86 |
static GSourceFuncs gdk_display_link_source_funcs = { |
| 87 |
gdk_display_link_source_prepare, |
| 88 |
gdk_display_link_source_check, |
| 89 |
gdk_display_link_source_dispatch, |
| 90 |
gdk_display_link_source_finalize |
| 91 |
}; |
| 92 |
|
| 93 |
void |
| 94 |
gdk_display_link_source_pause (GdkDisplayLinkSource *source) |
| 95 |
{ |
| 96 |
CVDisplayLinkStop (source->display_link); |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
gdk_display_link_source_unpause (GdkDisplayLinkSource *source) |
| 101 |
{ |
| 102 |
CVDisplayLinkStart (source->display_link); |
| 103 |
} |
| 104 |
|
| 105 |
static CVReturn |
| 106 |
gdk_display_link_source_frame_cb (CVDisplayLinkRef display_link, |
| 107 |
const CVTimeStamp *inNow, |
| 108 |
const CVTimeStamp *inOutputTime, |
| 109 |
CVOptionFlags flagsIn, |
| 110 |
CVOptionFlags *flagsOut, |
| 111 |
void *user_data) |
| 112 |
{ |
| 113 |
GdkDisplayLinkSource *impl = user_data; |
| 114 |
gint64 presentation_time; |
| 115 |
gboolean needs_wakeup; |
| 116 |
|
| 117 |
needs_wakeup = !g_atomic_int_get (&impl->needs_dispatch); |
| 118 |
|
| 119 |
presentation_time = host_to_frame_clock_time (inOutputTime->hostTime); |
| 120 |
|
| 121 |
impl->presentation_time = presentation_time; |
| 122 |
impl->needs_dispatch = TRUE; |
| 123 |
|
| 124 |
if (needs_wakeup) |
| 125 |
{ |
| 126 |
NSEvent *event; |
| 127 |
|
| 128 |
/* Post a message so we'll break out of the message loop. |
| 129 |
* |
| 130 |
* We don't use g_main_context_wakeup() here because that |
| 131 |
* would result in sending a message to the pipe(2) fd in |
| 132 |
* the select thread which would then send this message as |
| 133 |
* well. Lots of extra work. |
| 134 |
*/ |
| 135 |
event = [NSEvent otherEventWithType: GDK_QUARTZ_APPLICATION_DEFINED |
| 136 |
location: NSZeroPoint |
| 137 |
modifierFlags: 0 |
| 138 |
timestamp: 0 |
| 139 |
windowNumber: 0 |
| 140 |
context: nil |
| 141 |
subtype: GDK_QUARTZ_EVENT_SUBTYPE_EVENTLOOP |
| 142 |
data1: 0 |
| 143 |
data2: 0]; |
| 144 |
|
| 145 |
[NSApp postEvent:event atStart:YES]; |
| 146 |
} |
| 147 |
|
| 148 |
return kCVReturnSuccess; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* gdk_display_link_source_new: |
| 153 |
* |
| 154 |
* Creates a new #GSource that will activate the dispatch function upon |
| 155 |
* notification from a CVDisplayLink that a new frame should be drawn. |
| 156 |
* |
| 157 |
* Effort is made to keep the transition from the high-priority |
| 158 |
* CVDisplayLink thread into this GSource lightweight. However, this is |
| 159 |
* somewhat non-ideal since the best case would be to do the drawing |
| 160 |
* from the high-priority thread. |
| 161 |
* |
| 162 |
* Returns: (transfer full): A newly created #GSource. |
| 163 |
*/ |
| 164 |
GSource * |
| 165 |
gdk_display_link_source_new (void) |
| 166 |
{ |
| 167 |
GdkDisplayLinkSource *impl; |
| 168 |
GSource *source; |
| 169 |
CVReturn ret; |
| 170 |
double period; |
| 171 |
|
| 172 |
source = g_source_new (&gdk_display_link_source_funcs, sizeof *impl); |
| 173 |
impl = (GdkDisplayLinkSource *)source; |
| 174 |
|
| 175 |
/* |
| 176 |
* Create our link based on currently connected displays. |
| 177 |
* If there are multiple displays, this will be something that tries |
| 178 |
* to work for all of them. In the future, we may want to explore multiple |
| 179 |
* links based on the connected displays. |
| 180 |
*/ |
| 181 |
ret = CVDisplayLinkCreateWithActiveCGDisplays (&impl->display_link); |
| 182 |
if (ret != kCVReturnSuccess) |
| 183 |
{ |
| 184 |
g_warning ("Failed to initialize CVDisplayLink!"); |
| 185 |
return source; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* Determine our nominal period between frames. |
| 190 |
*/ |
| 191 |
period = CVDisplayLinkGetActualOutputVideoRefreshPeriod (impl->display_link); |
| 192 |
if (period == 0.0) |
| 193 |
period = 1.0 / 60.0; |
| 194 |
impl->refresh_interval = period * 1000000L; |
| 195 |
|
| 196 |
/* |
| 197 |
* Wire up our callback to be executed within the high-priority thread. |
| 198 |
*/ |
| 199 |
CVDisplayLinkSetOutputCallback (impl->display_link, |
| 200 |
gdk_display_link_source_frame_cb, |
| 201 |
source); |
| 202 |
|
| 203 |
g_source_set_name (source, "[gdk] quartz frame clock"); |
| 204 |
|
| 205 |
return source; |
| 206 |
} |
| 207 |
|
| 208 |
static gint64 |
| 209 |
host_to_frame_clock_time (gint64 host_time) |
| 210 |
{ |
| 211 |
static mach_timebase_info_data_t timebase_info; |
| 212 |
|
| 213 |
/* |
| 214 |
* NOTE: |
| 215 |
* |
| 216 |
* This code is taken from GLib to match g_get_monotonic_time(). |
| 217 |
*/ |
| 218 |
if (G_UNLIKELY (timebase_info.denom == 0)) |
| 219 |
{ |
| 220 |
/* This is a fraction that we must use to scale |
| 221 |
* mach_absolute_time() by in order to reach nanoseconds. |
| 222 |
* |
| 223 |
* We've only ever observed this to be 1/1, but maybe it could be |
| 224 |
* 1000/1 if mach time is microseconds already, or 1/1000 if |
| 225 |
* picoseconds. Try to deal nicely with that. |
| 226 |
*/ |
| 227 |
mach_timebase_info (&timebase_info); |
| 228 |
|
| 229 |
/* We actually want microseconds... */ |
| 230 |
if (timebase_info.numer % 1000 == 0) |
| 231 |
timebase_info.numer /= 1000; |
| 232 |
else |
| 233 |
timebase_info.denom *= 1000; |
| 234 |
|
| 235 |
/* We want to make the numer 1 to avoid having to multiply... */ |
| 236 |
if (timebase_info.denom % timebase_info.numer == 0) |
| 237 |
{ |
| 238 |
timebase_info.denom /= timebase_info.numer; |
| 239 |
timebase_info.numer = 1; |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
/* We could just multiply by timebase_info.numer below, but why |
| 244 |
* bother for a case that may never actually exist... |
| 245 |
* |
| 246 |
* Plus -- performing the multiplication would risk integer |
| 247 |
* overflow. If we ever actually end up in this situation, we |
| 248 |
* should more carefully evaluate the correct course of action. |
| 249 |
*/ |
| 250 |
mach_timebase_info (&timebase_info); /* Get a fresh copy for a better message */ |
| 251 |
g_error ("Got weird mach timebase info of %d/%d. Please file a bug against GLib.", |
| 252 |
timebase_info.numer, timebase_info.denom); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return host_time / timebase_info.denom; |
| 257 |
} |