Bug 547399 - [10.14] NPEs caused by NSGraphicsContext.currentContext()
etc. returning null

Change-Id: I1c3565fb54942a330fff82092d70d8c0114e3b97
(cherry picked from commit 0daa8702b7a82e99f264cd1ef41f61438541bad1)
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java
index 2d63d51..0b8c35b 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java
@@ -2173,6 +2173,18 @@
 			window.setAlphaValue(alpha);
 		}
 		graphicsContext = NSGraphicsContext.graphicsContextWithWindow (window);
+		if (graphicsContext == null) {
+			// create a bitmap based context, which will still work e.g. for text size computations
+			// it is unclear if the bitmap needs to be larger than the text to be measured.
+			// the following values should be big enough in any case.
+			int width = 1920;
+			int height = 256;
+			NSBitmapImageRep rep = (NSBitmapImageRep) new NSBitmapImageRep().alloc();
+			rep = rep.initWithBitmapDataPlanes(0, width, height, 8, 3, false, false, OS.NSDeviceRGBColorSpace,
+					OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32);
+			graphicsContext = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
+			rep.release();
+		}
 		NSGraphicsContext flippedContext = NSGraphicsContext.graphicsContextWithGraphicsPort(graphicsContext.graphicsPort(), true);
 		graphicsContext = flippedContext;
 		if (data != null) {
@@ -5105,6 +5117,17 @@
 }
 
 void update (boolean all) {
+	if(NSGraphicsContext.currentContext() == null) {
+		/*
+		 * If linked against macOS 10.14 SDK, or when native dark mode support is
+		 * enabled via Info.plist, views are displayed using Core Animation and drawing
+		 * is only possible, when cocoa invokes drawRect of a dirty view (which it does
+		 * by a run loop observer invoked during calls of
+		 * NSApplication#nextEventMatchingMask, only after more than approx. 10ms have
+		 * passed since the last redraw).
+		 */
+		return;
+ 	}
 //	checkWidget();
 	NSArray isPainting = display.isPainting;
 	if (isPainting.containsObject(view)) return;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
index fd81f4f..5a0c9e1 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
@@ -3085,6 +3085,18 @@
 		screenWindow = window;
 	}
 	NSGraphicsContext context = screenWindow.graphicsContext();
+	if (context == null) {
+		// create a bitmap based context, which will still work e.g. for text size computations
+		// it is unclear if the bitmap needs to be larger than the text to be measured.
+		// the following values should be big enough in any case.
+		int width = 1920;
+		int height = 256;
+		NSBitmapImageRep rep = (NSBitmapImageRep) new NSBitmapImageRep().alloc();
+		rep = rep.initWithBitmapDataPlanes(0, width, height, 8, 3, false, false, OS.NSDeviceRGBColorSpace,
+				OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32);
+		context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
+		rep.release();
+	}
 //	NSAffineTransform transform = NSAffineTransform.transform();
 //	NSSize size = handle.size();
 //	transform.translateXBy(0, size.height);