Explorar el Código

Frame-rate throttling Cocoa as a temporary hack until the canvas upload is synchronized.

David Piuva hace 7 meses
padre
commit
89b3ecb325
Se han modificado 2 ficheros con 37 adiciones y 16 borrados
  1. 3 4
      README.md
  2. 34 12
      Source/windowManagers/CocoaWindow.mm

+ 3 - 4
README.md

@@ -62,14 +62,13 @@ If you are not redistributing the source code, then you do not have to tell anyo
 Theme, GUI, font and sound APIs are still under active development and may have significant changes before a stable version 1.0 is ready, because some code is just a primitive placeholder until the advanced implementation can replace it, and one must try to actually use the library before usability problems become obvious. Buffer, file, image, draw, filter, string and time APIs are however already quite version stable. You can choose to stick with a specific version for each new project, keep updated with the latest changes, or wait for stable version 1.0.
 
 ## How you can help
-* Port to Macintosh or Wayland using the same principles of minimal dependency.
 * Test this beta version and give feedback on the design before version 1.0 is released.
 * Create different types of game engines with open-source tools.
 
 ## Supported CPU hardware:
-* **Intel/AMD** using **SSE2** intrinsics and optional extensions.
+* **Intel/AMD** using **SSE2**, **AVX** and **AVX2** intrinsics and optional extensions.
 * **ARM** using **NEON** intrinsics.
-* Unknown CPU architectures, without SIMD vectorization as a fallback solution.
+* Unknown CPU architectures, by running the same vector operations without SIMD hardware acceleration. This is still faster than naive loops with one iteration per element, because multiple scalar operations in parallel are better at filling the processor's execution window.
 
 ## Platforms:
 * **Linux**, tested on Mint, Mate, Manjaro, Ubuntu, RaspberryPi OS, Raspbian (Buster or later).
@@ -78,7 +77,7 @@ Currently supporting X11 and Wayland is planned for future versions.
 * **Microsoft Windows**, but slower than on Linux because Windows has lots of background processes and slower threading and memory management.
 
 ## Partial support for:
-* MacOS has software rendering and a CoreAudio sound backend working, but still needs to replace X11 with a native port.
+* MacOS still has features missing in the Cocoa integration, but will soon be officially supported.
 
 ## Might also work on:
 * BSD and Solaris have code targeting the platforms in fileAPI.cpp for getting the application folder, but there are likely some applications missing for running the build script.

+ 34 - 12
Source/windowManagers/CocoaWindow.mm

@@ -2,13 +2,18 @@
 // Early alpha version!
 //   Do not use in released applications.
 // Missing features:
-//   * Needs to remove the title and button decorations when in full screen mode, so that the view covers the entire screen instead of getting corrupted mouse coordinates.
 //   * Copy and paste with clipboard.
-//   * Minimizing the window.
-//   * Toggling full-screen from the application and when starting with fullscreen requested.
+//     Not yet implemented.
+//   * Minimizing the window
+//     It just bounces back instantly.
+//   * Toggling full-screen
+//     The window and view do not resize when entering or leaving full-screen.
+//     The application does not detect when the window has been maximized.
 //   * Synchronization of canvas upload.
-//   * Setting cursor position and visibility
-//   ...
+//     There is a temporary frame-rate limiting hack to hide the worst glitches.
+//   * Setting cursor position and visibility.
+//     Not yet implemented.
+//   See if anything more is missing...
 
 #import <Cocoa/Cocoa.h>
 
@@ -39,6 +44,9 @@ private:
 	bool pressedShift = false;
 	bool pressedAltOption = false;
 
+	// TODO: Replace frame-rate throttling with correct synchronization.
+	double lastDisplayTime = 0.0;
+
 	// Double buffering to allow drawing to a canvas while displaying the previous one
 	// The image which can be drawn to, sharing memory with the Cocoa image
 	dsr::AlignedImageRgbaU8 canvas[bufferCount];
@@ -85,7 +93,15 @@ public:
 	~CocoaWindow();
 	// Full-screen
 	void setFullScreen(bool enabled) override {
-		// TODO: Implement full-screen.
+		int newWindowState = enabled ? 2 : 1;
+		if (newWindowState != this->windowState) {
+			if (enabled) {
+				this->window.styleMask &= ~(NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable);
+			} else {
+				this->window.styleMask |= NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
+			}
+			this->windowState = newWindowState;
+		}
 	};
 	bool isFullScreen() override { return this->windowState == 2; }
 	// Showing the content
@@ -125,15 +141,13 @@ CocoaWindow::CocoaWindow(const dsr::String& title, int width, int height) {
 	@autoreleasepool {
 		this->window = [[NSWindow alloc]
 		  initWithContentRect:region
-		  styleMask: (
-			  NSWindowStyleMaskTitled
-			| NSWindowStyleMaskClosable
-			| NSWindowStyleMaskMiniaturizable
-			| NSWindowStyleMaskResizable
-		  )
+		  styleMask:0
 		  backing: NSBackingStoreBuffered
 		  defer: NO];
 	}
+
+	this->setFullScreen(fullScreen);
+
 	// Create a color space
 	this->colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
 	if (this->colorSpace == nullptr) {
@@ -503,6 +517,14 @@ void CocoaWindow::showCanvas() {
 			view.wantsLayer = YES;
 			view.layer.contents = (__bridge id)image;
 			CGImageRelease(image);
+
+			// TODO: Replace frame-rate throttling with correct synchronization.
+			static const double minimumFrameTime = 1.0 / 120.0;
+			double newTime = dsr::time_getSeconds();
+			if (newTime < this->lastDisplayTime + minimumFrameTime) {
+				dsr::time_sleepSeconds(this->lastDisplayTime + minimumFrameTime - newTime);
+			}
+			this->lastDisplayTime = newTime;
 		}
 	}
 }