[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/ns/surface-stuff 589aa84 1/2: Make NS port swap buffers less oft
From: |
Alan Third |
Subject: |
scratch/ns/surface-stuff 589aa84 1/2: Make NS port swap buffers less often |
Date: |
Fri, 21 May 2021 13:10:28 -0400 (EDT) |
branch: scratch/ns/surface-stuff
commit 589aa84d65d1fc673ed1dfe716548e709f61bbd4
Author: Alan Third <alan@idiocy.org>
Commit: Alan Third <alan@idiocy.org>
Make NS port swap buffers less often
* src/nsterm.h: Update EmacsSurface definition.
* src/nsterm.m ([EmacsView focusOnDrawingBuffer]): Don't change the
CGContext's settings directly.
([EmacsView unfocusDrawingBuffer]): Don't release the context here.
([EmacsView viewWillDraw]): Release the context here.
([EmacsSurface initWithSize:ColorSpace:Scale:]): Add the scale factor
and if there's already a CGContext available, reuse it..
([EmacsSurface releaseContext]): If there's no context don't try to
release it.
---
src/nsterm.h | 3 +-
src/nsterm.m | 94 ++++++++++++++++++++++++++++++++++--------------------------
2 files changed, 55 insertions(+), 42 deletions(-)
diff --git a/src/nsterm.h b/src/nsterm.h
index 017c239..0596f3f 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -724,8 +724,9 @@ typedef id instancetype;
IOSurfaceRef currentSurface;
IOSurfaceRef lastSurface;
CGContextRef context;
+ CGFloat scale;
}
-- (id) initWithSize: (NSSize)s ColorSpace: (CGColorSpaceRef)cs;
+- (id) initWithSize: (NSSize)s ColorSpace: (CGColorSpaceRef)cs Scale:
(CGFloat)scale;
- (void) dealloc;
- (NSSize) getSize;
- (CGContextRef) getContext;
diff --git a/src/nsterm.m b/src/nsterm.m
index bb20886..e5917f9 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -8353,19 +8353,17 @@ not_in_argv (NSString *arg)
surface = [[EmacsSurface alloc] initWithSize:s
ColorSpace:[[[self window] colorSpace]
- CGColorSpace]];
+ CGColorSpace]
+ Scale:scale];
/* Since we're using NSViewLayerContentsRedrawOnSetNeedsDisplay
the layer's scale factor is not set automatically, so do it
now. */
- [[self layer] setContentsScale:[[self window] backingScaleFactor]];
+ [[self layer] setContentsScale:scale];
}
CGContextRef context = [surface getContext];
- CGContextTranslateCTM(context, 0, [surface getSize].height);
- CGContextScaleCTM(context, scale, -scale);
-
[NSGraphicsContext
setCurrentContext:[NSGraphicsContext
graphicsContextWithCGContext:context
@@ -8378,7 +8376,6 @@ not_in_argv (NSString *arg)
NSTRACE ("[EmacsView unfocusDrawingBuffer]");
[NSGraphicsContext setCurrentContext:nil];
- [surface releaseContext];
[self setNeedsDisplay:YES];
}
@@ -8485,6 +8482,8 @@ not_in_argv (NSString *arg)
unblock_input ();
waiting_for_input = owfi;
}
+
+ [surface releaseContext];
}
@@ -9720,6 +9719,7 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
- (id) initWithSize: (NSSize)s
ColorSpace: (CGColorSpaceRef)cs
+ Scale: (CGFloat)scl
{
NSTRACE ("[EmacsSurface initWithSize:ColorSpace:]");
@@ -9728,6 +9728,7 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
cache = [[NSMutableArray arrayWithCapacity:3] retain];
size = s;
colorSpace = cs;
+ scale = scl;
return self;
}
@@ -9764,50 +9765,58 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
calls cannot be nested. */
- (CGContextRef) getContext
{
- IOSurfaceRef surface = NULL;
-
NSTRACE ("[EmacsSurface getContextWithSize:]");
- NSTRACE_MSG ("IOSurface count: %lu", [cache count] + (lastSurface ? 1 : 0));
- for (id object in cache)
+ if (!context)
{
- if (!IOSurfaceIsInUse ((IOSurfaceRef)object))
- {
- surface = (IOSurfaceRef)object;
- [cache removeObject:object];
- break;
- }
- }
+ IOSurfaceRef surface = NULL;
- if (!surface)
- {
- int bytesPerRow = IOSurfaceAlignProperty (kIOSurfaceBytesPerRow,
- size.width * 4);
+ NSTRACE_MSG ("IOSurface count: %lu", [cache count] + (lastSurface ? 1 :
0));
- surface = IOSurfaceCreate
- ((CFDictionaryRef)@{(id)kIOSurfaceWidth:[NSNumber
numberWithInt:size.width],
- (id)kIOSurfaceHeight:[NSNumber numberWithInt:size.height],
- (id)kIOSurfaceBytesPerRow:[NSNumber numberWithInt:bytesPerRow],
- (id)kIOSurfaceBytesPerElement:[NSNumber numberWithInt:4],
- (id)kIOSurfacePixelFormat:[NSNumber
numberWithUnsignedInt:'BGRA']});
- }
+ for (id object in cache)
+ {
+ if (!IOSurfaceIsInUse ((IOSurfaceRef)object))
+ {
+ surface = (IOSurfaceRef)object;
+ [cache removeObject:object];
+ break;
+ }
+ }
- IOReturn lockStatus = IOSurfaceLock (surface, 0, nil);
- if (lockStatus != kIOReturnSuccess)
- NSLog (@"Failed to lock surface: %x", lockStatus);
+ if (!surface)
+ {
+ int bytesPerRow = IOSurfaceAlignProperty (kIOSurfaceBytesPerRow,
+ size.width * 4);
+
+ surface = IOSurfaceCreate
+ ((CFDictionaryRef)@{(id)kIOSurfaceWidth:[NSNumber
numberWithInt:size.width],
+ (id)kIOSurfaceHeight:[NSNumber numberWithInt:size.height],
+ (id)kIOSurfaceBytesPerRow:[NSNumber numberWithInt:bytesPerRow],
+ (id)kIOSurfaceBytesPerElement:[NSNumber numberWithInt:4],
+ (id)kIOSurfacePixelFormat:[NSNumber
numberWithUnsignedInt:'BGRA']});
+ }
- [self copyContentsTo:surface];
+ IOReturn lockStatus = IOSurfaceLock (surface, 0, nil);
+ if (lockStatus != kIOReturnSuccess)
+ NSLog (@"Failed to lock surface: %x", lockStatus);
- currentSurface = surface;
+ [self copyContentsTo:surface];
+
+ currentSurface = surface;
+
+ context = CGBitmapContextCreate (IOSurfaceGetBaseAddress
(currentSurface),
+ IOSurfaceGetWidth (currentSurface),
+ IOSurfaceGetHeight (currentSurface),
+ 8,
+ IOSurfaceGetBytesPerRow
(currentSurface),
+ colorSpace,
+ (kCGImageAlphaPremultipliedFirst
+ | kCGBitmapByteOrder32Host));
+
+ CGContextTranslateCTM(context, 0, size.height);
+ CGContextScaleCTM(context, scale, -scale);
+ }
- context = CGBitmapContextCreate (IOSurfaceGetBaseAddress (currentSurface),
- IOSurfaceGetWidth (currentSurface),
- IOSurfaceGetHeight (currentSurface),
- 8,
- IOSurfaceGetBytesPerRow (currentSurface),
- colorSpace,
- (kCGImageAlphaPremultipliedFirst
- | kCGBitmapByteOrder32Host));
return context;
}
@@ -9818,6 +9827,9 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
{
NSTRACE ("[EmacsSurface releaseContextAndGetSurface]");
+ if (!context)
+ return;
+
CGContextRelease (context);
context = NULL;