From nobody Sun Nov 9 19:11:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551718831732619.3514273919236; Mon, 4 Mar 2019 09:00:31 -0800 (PST) Received: from localhost ([127.0.0.1]:57283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h0qx2-00046O-KX for importer@patchew.org; Mon, 04 Mar 2019 12:00:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h0qn5-0004SS-UJ for qemu-devel@nongnu.org; Mon, 04 Mar 2019 11:50:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h0qn3-0005ms-Tu for qemu-devel@nongnu.org; Mon, 04 Mar 2019 11:50:11 -0500 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]:34816) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h0qn3-0005mG-Jr for qemu-devel@nongnu.org; Mon, 04 Mar 2019 11:50:09 -0500 Received: by mail-wr1-x42e.google.com with SMTP id t18so6334172wrx.2 for ; Mon, 04 Mar 2019 08:50:09 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id x74sm7238018wmf.22.2019.03.04.08.50.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Mar 2019 08:50:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=iEZ8xWsGFQnnfCPhhe+VWXCcfkJ4AqfX00aEDhMQjI4=; b=NjvJ4usM2LUw35CvtXW3ZwL6ugl2Q1voI3IfoAofe7lsnv5c5QfZeQC8ibKjeEzjru cWJPqQLbOSrSOqEdHhx8Z+b9z38wPrSroeUraR2CBhM7MmGEklrlVy48EPHEs4wG02Yh B4vR33fRYmsJ/xSdTh1PUDZN6gatX5rE9uJ1+v2JbIWisI+MRuxa5G4AllVQFuuBJ7aG 5msaxSOVxYprAAD4OyjhSp3QUiYjXgjXTDFPtUN62u89QH2rc+fguRynfx9t2e9dMV7v MdVURxet6SQNStJrhnJ2+p56QLF2Q4dUBX7ksXC/Gj82tKMdvXJYkM2ya0+429Eko2Uz SVuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iEZ8xWsGFQnnfCPhhe+VWXCcfkJ4AqfX00aEDhMQjI4=; b=UjwHuqrQLgFgF4PTc9UZhgq3GilBzt34fftgULAILMdfZWNqBtEt1b9tTEuiF41pM6 5sH/WVwsaTxd5L+ZGfpbeUBdB059K3IPEr03dUzY53h/SGZimAn4nvFuDaiZd74RfJg9 i9Lpyi0Bwcn/62lYoRaGKsVtACEBdBKUx03c/i0r+NT4e2RVwqwkj3NAgPiGwBbMuLyh 9xEHtRZxj7mfPdbjts0Zum8+uW7GPtIRithjogobTNY1fvTaVX1V7CTAxbM4IhTHIOvP C8WF7BZG3gcZZhb0lNAHapz1iBfO/uRPZwERxi6UUxuQTDY4qrm7fnqb/ihjQsCQzFM6 b5Lg== X-Gm-Message-State: APjAAAWFs+A/FJM3JCnVugH5/LXiFyCu8kD+z8CZjZnkijeoCrT4osK9 UXh1AS70GXK0O81JPS7/8Msry30QrgY= X-Google-Smtp-Source: APXvYqxU1Q9BuJELtpYp+vv+4eRAtJeF7VkuvBcBxryViihcSrS16C0yE+01nQ9jHjnrVhCDr56fow== X-Received: by 2002:adf:e90b:: with SMTP id f11mr10082492wrm.36.1551718208215; Mon, 04 Mar 2019 08:50:08 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 4 Mar 2019 16:49:58 +0000 Message-Id: <20190304164958.9362-8-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190304164958.9362-1-peter.maydell@linaro.org> References: <20190304164958.9362-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::42e Subject: [Qemu-devel] [PULL 7/7] ui/cocoa: Perform UI operations only on the main thread X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The OSX Mojave release is more picky about enforcing the Cocoa API restriction that only the main thread may perform UI calls. To accommodate this we need to restructure the Cocoa code: * the special OSX main() creates a second thread and uses that to call the vl.c qemu_main(); the original main thread goes into the OSX event loop * the refresh, switch and update callbacks asynchronously tell the main thread to do the necessary work * the refresh callback no longer does the "get events from the UI event queue and handle them" loop, since we now use the stock OSX event loop. Instead our NSApplication sendEvent method will either deal with them or pass them on to OSX All these things have to be changed in one commit, to avoid breaking bisection. Note that since we use dispatch_get_main_queue(), this bumps our minimum version requirement to OSX 10.10 Yosemite (released in 2014, unsupported by Apple since 2017). Signed-off-by: Peter Maydell Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190225102433.22401-8-peter.maydell@linaro.org Message-id: 20190214102816.3393-8-peter.maydell@linaro.org --- ui/cocoa.m | 195 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 79 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 00e3db69c91..420b2411c10 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -129,6 +129,9 @@ bool stretch_video; NSTextField *pauseLabel; NSArray * supportedImageFileTypes; =20 +static QemuSemaphore display_init_sem; +static QemuSemaphore app_started_sem; + // Utility functions to run specified code block with iothread lock held typedef void (^CodeBlock)(void); typedef bool (^BoolCodeBlock)(void); @@ -325,6 +328,7 @@ static void handleAnyDeviceErrors(Error * err) NSWindow *fullScreenWindow; float cx,cy,cw,ch,cdx,cdy; CGDataProviderRef dataProviderRef; + pixman_image_t *pixman_image; BOOL modifiers_state[256]; BOOL isMouseGrabbed; BOOL isFullscreen; @@ -383,8 +387,10 @@ QemuCocoaView *cocoaView; { COCOA_DEBUG("QemuCocoaView: dealloc\n"); =20 - if (dataProviderRef) + if (dataProviderRef) { CGDataProviderRelease(dataProviderRef); + pixman_image_unref(pixman_image); + } =20 [super dealloc]; } @@ -535,13 +541,16 @@ QemuCocoaView *cocoaView; } =20 // update screenBuffer - if (dataProviderRef) + if (dataProviderRef) { CGDataProviderRelease(dataProviderRef); + pixman_image_unref(pixman_image); + } =20 //sync host window color space with guests screen.bitsPerPixel =3D PIXMAN_FORMAT_BPP(image_format); screen.bitsPerComponent =3D DIV_ROUND_UP(screen.bitsPerPixel, 8) * 2; =20 + pixman_image =3D image; dataProviderRef =3D CGDataProviderCreateWithData(NULL, pixman_image_ge= t_data(image), w * 4 * h, NULL); =20 // update windows @@ -1013,7 +1022,6 @@ QemuCocoaView *cocoaView; #endif { } -- (void)startEmulationWithArgc:(int)argc argv:(char**)argv; - (void)doToggleFullScreen:(id)sender; - (void)toggleFullScreen:(id)sender; - (void)showQEMUDoc:(id)sender; @@ -1101,8 +1109,8 @@ QemuCocoaView *cocoaView; - (void)applicationDidFinishLaunching: (NSNotification *) note { COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); - // launch QEMU, with the global args - [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; + /* Tell cocoa_display_init to proceed */ + qemu_sem_post(&app_started_sem); } =20 - (void)applicationWillTerminate:(NSNotification *)aNotification @@ -1145,15 +1153,6 @@ QemuCocoaView *cocoaView; [cocoaView raiseAllKeys]; } =20 -- (void)startEmulationWithArgc:(int)argc argv:(char**)argv -{ - COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); - - int status; - status =3D qemu_main(argc, argv, *_NSGetEnviron()); - exit(status); -} - /* We abstract the method called by the Enter Fullscreen menu item * because Mac OS 10.7 and higher disables it. This is because of the * menu item's old selector's name toggleFullScreen: @@ -1489,7 +1488,9 @@ QemuCocoaView *cocoaView; - (void)sendEvent:(NSEvent *)event { COCOA_DEBUG("QemuApplication: sendEvent\n"); - [super sendEvent: event]; + if (![cocoaView handleEvent:event]) { + [super sendEvent: event]; + } } @end =20 @@ -1672,32 +1673,59 @@ static void addRemovableDevicesMenuItems(void) qapi_free_BlockInfoList(pointerToFree); } =20 -int main (int argc, const char * argv[]) { +/* + * The startup process for the OSX/Cocoa UI is complicated, because + * OSX insists that the UI runs on the initial main thread, and so we + * need to start a second thread which runs the vl.c qemu_main(): + * + * Initial thread: 2nd thread: + * in main(): + * create qemu-main thread + * wait on display_init semaphore + * call qemu_main() + * ... + * in cocoa_display_init(): + * post the display_init semaphore + * wait on app_started semaphore + * create application, menus, etc + * enter OSX run loop + * in applicationDidFinishLaunching: + * post app_started semaphore + * tell main thread to fullscreen if n= eeded + * [...] + * run qemu main-loop + * + * We do this in two stages so that we don't do the creation of the + * GUI application menus and so on for command line options like --help + * where we want to just print text to stdout and exit immediately. + */ =20 +static void *call_qemu_main(void *opaque) +{ + int status; + + COCOA_DEBUG("Second thread: calling qemu_main()\n"); + status =3D qemu_main(gArgc, gArgv, *_NSGetEnviron()); + COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n"); + exit(status); +} + +int main (int argc, const char * argv[]) { + QemuThread thread; + + COCOA_DEBUG("Entered main()\n"); gArgc =3D argc; gArgv =3D (char **)argv; - int i; =20 - /* In case we don't need to display a window, let's not do that */ - for (i =3D 1; i < argc; i++) { - const char *opt =3D argv[i]; + qemu_sem_init(&display_init_sem, 0); + qemu_sem_init(&app_started_sem, 0); =20 - if (opt[0] =3D=3D '-') { - /* Treat --foo the same as -foo. */ - if (opt[1] =3D=3D '-') { - opt++; - } - if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || - !strcmp(opt, "-vnc") || - !strcmp(opt, "-nographic") || - !strcmp(opt, "-version") || - !strcmp(opt, "-curses") || - !strcmp(opt, "-display") || - !strcmp(opt, "-qtest")) { - return qemu_main(gArgc, gArgv, *_NSGetEnviron()); - } - } - } + qemu_thread_create(&thread, "qemu_main", call_qemu_main, + NULL, QEMU_THREAD_DETACHED); + + COCOA_DEBUG("Main thread: waiting for display_init_sem\n"); + qemu_sem_wait(&display_init_sem); + COCOA_DEBUG("Main thread: initializing app\n"); =20 NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; =20 @@ -1710,12 +1738,24 @@ int main (int argc, const char * argv[]) { =20 create_initial_menus(); =20 + /* + * Create the menu entries which depend on QEMU state (for consoles + * and removeable devices). These make calls back into QEMU functions, + * which is OK because at this point we know that the second thread + * holds the iothread lock and is synchronously waiting for us to + * finish. + */ + add_console_menu_entries(); + addRemovableDevicesMenuItems(); + // Create an Application controller QemuCocoaAppController *appController =3D [[QemuCocoaAppController all= oc] init]; [NSApp setDelegate:appController]; =20 // Start the main event loop + COCOA_DEBUG("Main thread: entering OSX run loop\n"); [NSApp run]; + COCOA_DEBUG("Main thread: left OSX run loop, exiting\n"); =20 [appController release]; [pool release]; @@ -1733,17 +1773,19 @@ static void cocoa_update(DisplayChangeListener *dcl, =20 COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); =20 - NSRect rect; - if ([cocoaView cdx] =3D=3D 1.0) { - rect =3D NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); - } else { - rect =3D NSMakeRect( - x * [cocoaView cdx], - ([cocoaView gscreen].height - y - h) * [cocoaView cdy], - w * [cocoaView cdx], - h * [cocoaView cdy]); - } - [cocoaView setNeedsDisplayInRect:rect]; + dispatch_async(dispatch_get_main_queue(), ^{ + NSRect rect; + if ([cocoaView cdx] =3D=3D 1.0) { + rect =3D NSMakeRect(x, [cocoaView gscreen].height - y - h, w, = h); + } else { + rect =3D NSMakeRect( + x * [cocoaView cdx], + ([cocoaView gscreen].height - y - h) * [cocoaView cdy], + w * [cocoaView cdx], + h * [cocoaView cdy]); + } + [cocoaView setNeedsDisplayInRect:rect]; + }); =20 [pool release]; } @@ -1752,9 +1794,19 @@ static void cocoa_switch(DisplayChangeListener *dcl, DisplaySurface *surface) { NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; + pixman_image_t *image =3D surface->image; =20 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); - [cocoaView switchSurface:surface->image]; + + // The DisplaySurface will be freed as soon as this callback returns. + // We take a reference to the underlying pixman image here so it does + // not disappear from under our feet; the switchSurface method will + // deref the old image when it is done with it. + pixman_image_ref(image); + + dispatch_async(dispatch_get_main_queue(), ^{ + [cocoaView switchSurface:image]; + }); [pool release]; } =20 @@ -1766,26 +1818,15 @@ static void cocoa_refresh(DisplayChangeListener *dc= l) graphic_hw_update(NULL); =20 if (qemu_input_is_absolute()) { - if (![cocoaView isAbsoluteEnabled]) { - if ([cocoaView isMouseGrabbed]) { - [cocoaView ungrabMouse]; + dispatch_async(dispatch_get_main_queue(), ^{ + if (![cocoaView isAbsoluteEnabled]) { + if ([cocoaView isMouseGrabbed]) { + [cocoaView ungrabMouse]; + } } - } - [cocoaView setAbsoluteEnabled:YES]; + [cocoaView setAbsoluteEnabled:YES]; + }); } - - NSDate *distantPast; - NSEvent *event; - distantPast =3D [NSDate distantPast]; - do { - event =3D [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:di= stantPast - inMode: NSDefaultRunLoopMode dequeue:YES]; - if (event !=3D nil) { - if (![cocoaView handleEvent:event]) { - [NSApp sendEvent:event]; - } - } - } while(event !=3D nil); [pool release]; } =20 @@ -1806,10 +1847,17 @@ static void cocoa_display_init(DisplayState *ds, Di= splayOptions *opts) { COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); =20 + /* Tell main thread to go ahead and create the app and enter the run l= oop */ + qemu_sem_post(&display_init_sem); + qemu_sem_wait(&app_started_sem); + COCOA_DEBUG("cocoa_display_init: app start completed\n"); + /* if fullscreen mode is to be used */ if (opts->has_full_screen && opts->full_screen) { - [NSApp activateIgnoringOtherApps: YES]; - [(QemuCocoaAppController *)[[NSApplication sharedApplication] dele= gate] toggleFullScreen: nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [NSApp activateIgnoringOtherApps: YES]; + [(QemuCocoaAppController *)[[NSApplication sharedApplication] = delegate] toggleFullScreen: nil]; + }); } =20 dcl =3D g_malloc0(sizeof(DisplayChangeListener)); @@ -1820,17 +1868,6 @@ static void cocoa_display_init(DisplayState *ds, Dis= playOptions *opts) =20 // register cleanup function atexit(cocoa_cleanup); - - /* At this point QEMU has created all the consoles, so we can add View - * menu entries for them. - */ - add_console_menu_entries(); - - /* Give all removable devices a menu item. - * Has to be called after QEMU has started to - * find out what removable devices it has. - */ - addRemovableDevicesMenuItems(); } =20 static QemuDisplay qemu_display_cocoa =3D { --=20 2.20.1