macOS: Don't use offscreen rendering.

Since Catalina or earlier this no longer causes artifacts when Cocoa
controls are overlaid on a GL layer. Conversely, offscreen rendering
is very slow, especially on HiDPI screens.

Co-Authored-By: Koen Schmeets <hello@koenschmeets.nl>
pull/507/head
Thomas Roughton 2019-11-23 13:29:09 +01:00 committed by whitequark
parent e386d405d6
commit 9b61f988be
1 changed files with 15 additions and 38 deletions

View File

@ -344,7 +344,7 @@ MenuBarRef GetOrCreateMainMenu(bool *unique) {
// Cocoa NSView and NSWindow extensions // Cocoa NSView and NSWindow extensions
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@interface SSView : NSView @interface SSView : NSOpenGLView
@property Platform::Window *receiver; @property Platform::Window *receiver;
@property BOOL acceptsFirstResponder; @property BOOL acceptsFirstResponder;
@ -362,8 +362,6 @@ MenuBarRef GetOrCreateMainMenu(bool *unique) {
@implementation SSView @implementation SSView
{ {
GlOffscreen offscreen;
NSOpenGLContext *glContext;
NSTrackingArea *trackingArea; NSTrackingArea *trackingArea;
NSTextField *editor; NSTextField *editor;
} }
@ -371,17 +369,15 @@ MenuBarRef GetOrCreateMainMenu(bool *unique) {
@synthesize acceptsFirstResponder; @synthesize acceptsFirstResponder;
- (id)initWithFrame:(NSRect)frameRect { - (id)initWithFrame:(NSRect)frameRect {
if(self = [super initWithFrame:frameRect]) { NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFAColorSize, 24,
NSOpenGLPFADepthSize, 24,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
if(self = [super initWithFrame:frameRect pixelFormat:pixelFormat]) {
self.wantsBestResolutionOpenGLSurface = YES;
self.wantsLayer = YES; self.wantsLayer = YES;
NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFAColorSize, 24,
NSOpenGLPFADepthSize, 24,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
glContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:NULL];
editor = [[NSTextField alloc] init]; editor = [[NSTextField alloc] init];
editor.editable = YES; editor.editable = YES;
[[editor cell] setWraps:NO]; [[editor cell] setWraps:NO];
@ -394,7 +390,6 @@ MenuBarRef GetOrCreateMainMenu(bool *unique) {
} }
- (void)dealloc { - (void)dealloc {
offscreen.Clear();
} }
- (BOOL)isFlipped { - (BOOL)isFlipped {
@ -404,29 +399,11 @@ MenuBarRef GetOrCreateMainMenu(bool *unique) {
@synthesize receiver; @synthesize receiver;
- (void)drawRect:(NSRect)aRect { - (void)drawRect:(NSRect)aRect {
[glContext makeCurrentContext]; [[self openGLContext] makeCurrentContext];
if(receiver->onRender) {
NSSize size = [self convertSizeToBacking:self.bounds.size]; receiver->onRender();
int width = (int)size.width, }
height = (int)size.height; [[self openGLContext] flushBuffer];
offscreen.Render(width, height, [&] {
if(receiver->onRender) {
receiver->onRender();
}
});
CGDataProviderRef provider = CGDataProviderCreateWithData(
NULL, &offscreen.data[0], width * height * 4, NULL);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef image = CGImageCreate(width, height, 8, 32,
width * 4, colorspace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
provider, NULL, true, kCGRenderingIntentDefault);
CGContextDrawImage((CGContextRef) [[NSGraphicsContext currentContext] CGContext],
[self bounds], image);
CGImageRelease(image);
CGDataProviderRelease(provider);
} }
- (BOOL)acceptsFirstMouse:(NSEvent *)event { - (BOOL)acceptsFirstMouse:(NSEvent *)event {
@ -892,7 +869,7 @@ public:
} }
void GetContentSize(double *width, double *height) override { void GetContentSize(double *width, double *height) override {
NSSize nsSize = [ssView frame].size; NSSize nsSize = ssView.frame.size;
*width = nsSize.width; *width = nsSize.width;
*height = nsSize.height; *height = nsSize.height;
} }