emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/ns/surface-stuff 6f1e1ba: Make drawing as much like the Mac port


From: Alan Third
Subject: scratch/ns/surface-stuff 6f1e1ba: Make drawing as much like the Mac port as possible
Date: Sun, 23 May 2021 16:59:08 -0400 (EDT)

branch: scratch/ns/surface-stuff
commit 6f1e1ba633bb3d85d475de829feaf7922fe703d2
Author: Alan Third <alan@idiocy.org>
Commit: Alan Third <alan@idiocy.org>

    Make drawing as much like the Mac port as possible
---
 src/nsterm.h |   5 ++-
 src/nsterm.m | 118 ++++++++++++++++++++++++++---------------------------------
 2 files changed, 55 insertions(+), 68 deletions(-)

diff --git a/src/nsterm.h b/src/nsterm.h
index 0596f3f..8c1f5e7 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -718,12 +718,13 @@ typedef id instancetype;
 #ifdef NS_DRAW_TO_BUFFER
 @interface EmacsSurface : NSObject
 {
-  NSMutableArray *cache;
   NSSize size;
   CGColorSpaceRef colorSpace;
   IOSurfaceRef currentSurface;
   IOSurfaceRef lastSurface;
-  CGContextRef context;
+  CGContextRef currentContext;
+  CGContextRef lastContext;
+  BOOL surfaceIsLocked;
   CGFloat scale;
 }
 - (id) initWithSize: (NSSize)s ColorSpace: (CGColorSpaceRef)cs Scale: 
(CGFloat)scale;
diff --git a/src/nsterm.m b/src/nsterm.m
index f616824..75cab57 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -8515,6 +8515,10 @@ not_in_argv (NSString *arg)
      surface twice in a row.  */
   [surface releaseContext];
   [[self layer] setContents:(id)[surface getSurface]];
+
+  if (![self wantsLayer])
+    [self setWantsLayer:YES];
+
   [surface performSelectorOnMainThread:@selector (getContext)
                             withObject:nil
                          waitUntilDone:NO];
@@ -9718,8 +9722,6 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
    probably be some sort of pruning job that removes excess
    surfaces.  */
 
-#define CACHE_MAX_SIZE 2
-
 - (id) initWithSize: (NSSize)s
          ColorSpace: (CGColorSpaceRef)cs
               Scale: (CGFloat)scl
@@ -9728,10 +9730,10 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
 
   [super init];
 
-  cache = [[NSMutableArray arrayWithCapacity:CACHE_MAX_SIZE] retain];
   size = s;
   colorSpace = cs;
   scale = scl;
+  surfaceIsLocked = NO;
 
   return self;
 }
@@ -9739,16 +9741,15 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
 
 - (void) dealloc
 {
-  if (context)
-    CGContextRelease (context);
+  if (currentContext)
+    CGContextRelease (currentContext);
+  if (lastContext)
+    CGContextRelease (lastContext);
 
   if (currentSurface)
     CFRelease (currentSurface);
-
-  for (id object in cache)
-    CFRelease ((IOSurfaceRef)object);
-
-  [cache release];
+  if (lastSurface)
+    CFRelease (lastSurface);
 
   [super dealloc];
 }
@@ -9768,65 +9769,50 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
 {
   NSTRACE ("[EmacsSurface getContext]");
 
-  if (!context)
+  if (!currentContext)
     {
-      IOSurfaceRef surface = NULL;
-
-      NSTRACE_MSG ("IOSurface count: %lu", [cache count] + (lastSurface ? 1 : 
0));
-
-      for (id object in cache)
-        {
-          if (!IOSurfaceIsInUse ((IOSurfaceRef)object))
-            {
-              surface = (IOSurfaceRef)object;
-              [cache removeObject:object];
-              break;
-            }
-        }
+      int bytesPerRow = IOSurfaceAlignProperty (kIOSurfaceBytesPerRow,
+                                                size.width * 4);
 
-      if (!surface && [cache count] >= CACHE_MAX_SIZE)
-        {
-          /* Just grab the first one off the cache.  This may result
-             in tearing effects.  The alternative is to wait for one
-             of the surfaces to become free.  */
-          surface = (IOSurfaceRef)[cache firstObject];
-          [cache removeObject:(id)surface];
-        }
-      else 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']});
-        }
+      currentSurface = 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']});
 
-      IOReturn lockStatus = IOSurfaceLock (surface, 0, nil);
+      IOReturn lockStatus = IOSurfaceLock (currentSurface, 0, nil);
       if (lockStatus != kIOReturnSuccess)
         NSLog (@"Failed to lock surface: %x", lockStatus);
 
-      [self copyContentsTo:surface];
+      surfaceIsLocked = YES;
+
+      [self copyContentsTo:currentSurface];
+
+      currentContext = CGBitmapContextCreate (IOSurfaceGetBaseAddress 
(currentSurface),
+                                              IOSurfaceGetWidth 
(currentSurface),
+                                              IOSurfaceGetHeight 
(currentSurface),
+                                              8,
+                                              IOSurfaceGetBytesPerRow 
(currentSurface),
+                                              colorSpace,
+                                              (kCGImageAlphaPremultipliedFirst
+                                               | kCGBitmapByteOrder32Host));
 
-      currentSurface = surface;
+      CGContextTranslateCTM(currentContext, 0, size.height);
+      CGContextScaleCTM(currentContext, scale, -scale);
+    }
 
-      context = CGBitmapContextCreate (IOSurfaceGetBaseAddress 
(currentSurface),
-                                       IOSurfaceGetWidth (currentSurface),
-                                       IOSurfaceGetHeight (currentSurface),
-                                       8,
-                                       IOSurfaceGetBytesPerRow 
(currentSurface),
-                                       colorSpace,
-                                       (kCGImageAlphaPremultipliedFirst
-                                        | kCGBitmapByteOrder32Host));
+  if (!surfaceIsLocked)
+    {
+      IOReturn lockStatus = IOSurfaceLock (currentSurface, 0, nil);
+      if (lockStatus != kIOReturnSuccess)
+        NSLog (@"Failed to lock surface: %x", lockStatus);
+      surfaceIsLocked = YES;
 
-      CGContextTranslateCTM(context, 0, size.height);
-      CGContextScaleCTM(context, scale, -scale);
+      [self copyContentsTo:currentSurface];
     }
 
-  return context;
+  return currentContext;
 }
 
 
@@ -9836,20 +9822,21 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
 {
   NSTRACE ("[EmacsSurface releaseContextAndGetSurface]");
 
-  if (!context)
+  if (!currentContext)
     return;
 
-  CGContextRelease (context);
-  context = NULL;
-
   IOReturn lockStatus = IOSurfaceUnlock (currentSurface, 0, nil);
   if (lockStatus != kIOReturnSuccess)
     NSLog (@"Failed to unlock surface: %x", lockStatus);
 
-  /* Put currentSurface back on the end of the cache.  */
-  [cache addObject:(id)currentSurface];
+  surfaceIsLocked = NO;
+
+  IOSurfaceRef tmpsfc = lastSurface;
+  CGContextRef tmpCtx = lastContext;
   lastSurface = currentSurface;
-  currentSurface = NULL;
+  lastContext = currentContext;
+  currentSurface = tmpsfc;
+  currentContext = tmpCtx;
 }
 
 
@@ -9892,7 +9879,6 @@ nswindow_orderedIndex_sort (id w1, id w2, void *c)
     NSLog (@"Failed to unlock source surface: %x", lockStatus);
 }
 
-#undef CACHE_MAX_SIZE
 
 @end /* EmacsSurface */
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]