unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Third <alan@idiocy.org>
To: Aaron Jensen <aaronjensen@gmail.com>
Cc: "Mattias Engdegård" <mattiase@acm.org>,
	"Robert Pluim" <rpluim@gmail.com>,
	32932@debbugs.gnu.org
Subject: bug#32932: 27.0.50; render bugs on macOS Mojave
Date: Mon, 10 Feb 2020 07:44:04 +0000	[thread overview]
Message-ID: <20200210074404.GA44470@breton.holly.idiocy.org> (raw)
In-Reply-To: <CAHyO48zHt9zNGtZGKG9MzkkjMoRJirux3TgEBeNWU9Cf=z=K=Q@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 662 bytes --]

On Sat, Feb 08, 2020 at 08:45:25AM -0800, Aaron Jensen wrote:
> 
> Yeah, unfortunately. Are you on Catalina? I can try the build on my
> work machine as well. It's a 2019 MBP 16", so pretty close to my 2017
> at home, but different. A hardware issue seems unlikely...

No, I’m still on Mojave.

Anyway, one last thing to try. I was reading up on how MacVim handled
this same issue, and while their drawing techniques are quite
different, they did find performance problems with CGLayers so
switched to using CGImages. I’ve given that a go and I don’t think
this patch is quite as fast here as the CGLayer one, but it may be
faster for you.

-- 
Alan Third

[-- Attachment #2: 0001-Use-CGImage-instead-of-NSBitmapImageRep-bug-32932.patch --]
[-- Type: text/plain, Size: 8628 bytes --]

From 2b514b8dfac196a7e19ccb4718f3b40afd09d8e0 Mon Sep 17 00:00:00 2001
From: Alan Third <alan@idiocy.org>
Date: Sat, 1 Feb 2020 21:17:29 +0000
Subject: [PATCH] Use CGImage instead of NSBitmapImageRep (bug#32932)

---
 src/nsterm.h |   4 +-
 src/nsterm.m | 115 +++++++++++++++++++++++++++++----------------------
 2 files changed, 68 insertions(+), 51 deletions(-)

diff --git a/src/nsterm.h b/src/nsterm.h
index 980ca534cf..7c6197f128 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -418,7 +418,7 @@ #define NSTRACE_UNSILENCE()
    NSWindow *nonfs_window;
    BOOL fs_is_native;
 #ifdef NS_IMPL_COCOA
-   NSBitmapImageRep *drawingBuffer;
+   CGContextRef drawingBuffer;
 #endif
 @public
    struct frame *emacsframe;
@@ -464,7 +464,7 @@ #define NSTRACE_UNSILENCE()
 - (void)focusOnDrawingBuffer;
 #endif
 - (void)copyRect:(NSRect)srcRect to:(NSRect)dstRect;
-- (void)createDrawingBufferWithRect:(NSRect)rect;
+- (void)createDrawingBuffer;
 
 /* Non-notification versions of NSView methods. Used for direct calls.  */
 - (void)windowWillEnterFullScreen;
diff --git a/src/nsterm.m b/src/nsterm.m
index 9d427b9b38..8761f65da9 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1141,7 +1141,6 @@ static NSRect constrain_frame_rect(NSRect frameRect, bool isFullscreen)
 
 #ifdef NS_IMPL_COCOA
   [NSGraphicsContext setCurrentContext:nil];
-  [view display];
 #else
   block_input ();
 
@@ -2853,7 +2852,9 @@ so some key presses (TAB) are swallowed by the system.  */
   ns_unfocus (f);
 
   /* as of 2006/11 or so this is now needed */
-  ns_redraw_scroll_bars (f);
+  /* FIXME: I don't see any reason for this and removing it makes no
+     difference here.  Do we need it for GNUstep?  */
+  //ns_redraw_scroll_bars (f);
   unblock_input ();
 }
 
@@ -3169,18 +3170,6 @@ so some key presses (TAB) are swallowed by the system.  */
 
       NSTRACE_RECT ("fromRect", fromRect);
 
-      /* Because we're drawing into an offscreen buffer which isn't
-         flipped, the images come out upside down.  To work around it
-         we need to do some fancy transforms.  */
-      {
-        NSAffineTransform *transform = [NSAffineTransform transform];
-        [transform translateXBy:0 yBy:NSMaxY(imageRect)];
-        [transform scaleXBy:1 yBy:-1];
-        [transform concat];
-
-        imageRect.origin.y = 0;
-      }
-
       [img drawInRect: imageRect
              fromRect: fromRect
             operation: NSCompositingOperationSourceOver
@@ -3938,11 +3927,6 @@ Function modeled after x_draw_glyph_string_box ().
 
       NSAffineTransform *doTransform = [NSAffineTransform transform];
 
-      /* We have to flip the image around the X axis as the offscreen
-         bitmap we're drawing to is flipped.  */
-      [doTransform scaleXBy:1 yBy:-1];
-      [doTransform translateXBy:0 yBy:-[img size].height];
-
       /* ImageMagick images don't have transforms.  */
       if (img->transform)
         [doTransform appendTransform:img->transform];
@@ -4838,7 +4822,7 @@ in certain situations (rapid incoming events).
   if (NILP (window->vertical_scroll_bar))
     {
       if (width > 0 && height > 0)
-	ns_clear_frame_area (f, left, top, width, height);
+        ns_clear_frame_area (f, left, top, width, height);
 
       bar = [[EmacsScroller alloc] initFrame: r window: win];
       wset_vertical_scroll_bar (window, make_mint_ptr (bar));
@@ -7104,7 +7088,7 @@ - (void) updateFrameSize: (BOOL) delay
          from non-native fullscreen, in other circumstances it appears
          to be a noop.  (bug#28872) */
       wr = NSMakeRect (0, 0, neww, newh);
-      [self createDrawingBufferWithRect:wr];
+      [self createDrawingBuffer];
       [view setFrame: wr];
 
       // To do: consider using [NSNotificationCenter postNotificationName:].
@@ -7444,7 +7428,8 @@ - (instancetype) initFrameFromEmacs: (struct frame *)f
   maximizing_resize = NO;
 #endif
 
-  [self createDrawingBufferWithRect:r];
+  [self setCanDrawConcurrently:YES];
+  [self createDrawingBuffer];
 
   win = [[EmacsWindow alloc]
             initWithContentRect: r
@@ -8229,20 +8214,28 @@ - (instancetype)toggleToolbar: (id)sender
 }
 
 
-- (void)createDrawingBufferWithRect:(NSRect)rect
-  /* Create and store a new NSBitmapImageRep for Emacs to draw
-     into.
+- (void)createDrawingBuffer
+  /* Create and store a new CGLayer for Emacs to draw into.
 
-     Drawing to an offscreen bitmap doesn't work in GNUstep as there's
-     a bug in graphicsContextWithBitmapImageRep
-     (https://savannah.gnu.org/bugs/?38405).  So under GNUstep we
-     retain the old method of drawing direct to the EmacsView.  */
+     We can't do this in GNUstep as there's no CGLayer or equivalent.
+     So under GNUstep we retain the old method of drawing direct to
+     the EmacsView.  */
 {
 #ifdef NS_IMPL_COCOA
+  NSGraphicsContext *screen;
+  CGFloat scale = [[self window] backingScaleFactor];
+  NSRect frame = [self frame];
+
   if (drawingBuffer != nil)
-    [drawingBuffer release];
+    CGContextRelease (drawingBuffer);
+
+  drawingBuffer = CGBitmapContextCreate (nil, NSWidth (frame) * scale,
+                                           NSHeight (frame) * scale,
+                                           8, 0, self.window.colorSpace.CGColorSpace,
+                                         kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
 
-  drawingBuffer = [[self bitmapImageRepForCachingDisplayInRect:rect] retain];
+  CGContextTranslateCTM(drawingBuffer, 0, NSHeight (frame) * scale);
+  CGContextScaleCTM(drawingBuffer, scale, -scale);
 #endif
 }
 
@@ -8250,11 +8243,11 @@ - (void)createDrawingBufferWithRect:(NSRect)rect
 #ifdef NS_IMPL_COCOA
 - (void)focusOnDrawingBuffer
 {
-  /* Creating the graphics context each time is very slow, but it
-     doesn't seem possible to cache and reuse it.  */
-  [NSGraphicsContext
-    setCurrentContext:
-      [NSGraphicsContext graphicsContextWithBitmapImageRep:drawingBuffer]];
+  NSGraphicsContext *buf =
+    [NSGraphicsContext
+        graphicsContextWithCGContext:drawingBuffer flipped:YES];
+
+  [NSGraphicsContext setCurrentContext:buf];
 }
 
 
@@ -8269,7 +8262,7 @@ - (void)windowDidChangeBackingProperties:(NSNotification *)notification
    if (old != new)
      {
        NSRect frame = [self frame];
-       [self createDrawingBufferWithRect:frame];
+       [self createDrawingBuffer];
        ns_clear_frame (emacsframe);
        expose_frame (emacsframe, 0, 0, NSWidth (frame), NSHeight (frame));
      }
@@ -8284,13 +8277,27 @@ - (void)copyRect:(NSRect)srcRect to:(NSRect)dstRect
   NSTRACE_RECT ("Destination", dstRect);
 
 #ifdef NS_IMPL_COCOA
-  [drawingBuffer drawInRect:dstRect
-                   fromRect:srcRect
-                  operation:NSCompositingOperationCopy
-                   fraction:1.0
-             respectFlipped:NO
-                      hints:nil];
+  CGImageRef copy;
+  NSAffineTransform *setOrigin = [NSAffineTransform transform];
+  NSRect frame = [self frame];
+  CGFloat xoffset = NSMinX (dstRect) - NSMinX (srcRect);
+  CGFloat yoffset = NSMinY (dstRect) - NSMinY (srcRect);
+
+  [[NSGraphicsContext currentContext] saveGraphicsState];
+
+  NSRectClip (dstRect);
 
+  [setOrigin scaleXBy:1 yBy:-1];
+  [setOrigin translateXBy:xoffset
+                      yBy:-NSHeight (frame) - yoffset];
+  [setOrigin concat];
+
+  copy = CGBitmapContextCreateImage (drawingBuffer);
+  CGContextDrawImage (drawingBuffer, frame, copy);
+
+  CGImageRelease (copy);
+
+  [[NSGraphicsContext currentContext] restoreGraphicsState];
   [self setNeedsDisplayInRect:dstRect];
 #else
   hide_bell();              // Ensure the bell image isn't scrolled.
@@ -8304,6 +8311,20 @@ - (void)copyRect:(NSRect)srcRect to:(NSRect)dstRect
 }
 
 
+- (BOOL)wantsUpdateLayer
+{
+    return YES;
+}
+
+
+- (void)updateLayer
+{
+  CGImageRef contentsImage = CGBitmapContextCreateImage(drawingBuffer);
+  self.layer.contents = (id)contentsImage;
+  CGImageRelease(contentsImage);
+}
+
+
 - (void)drawRect: (NSRect)rect
 {
   NSTRACE ("[EmacsView drawRect:" NSTRACE_FMT_RECT "]",
@@ -8313,12 +8334,8 @@ - (void)drawRect: (NSRect)rect
     return;
 
 #ifdef NS_IMPL_COCOA
-  [drawingBuffer drawInRect:rect
-                   fromRect:rect
-                  operation:NSCompositingOperationSourceOver
-                   fraction:1
-             respectFlipped:NO
-                      hints:nil];
+  // CGContextRef ctx = [[NSGraphicsContext currentContext] CGContext];
+  // CGContextDrawLayerInRect (ctx, NSRectToCGRect ([self frame]), drawingBuffer);
 #else
   int x = NSMinX (rect), y = NSMinY (rect);
   int width = NSWidth (rect), height = NSHeight (rect);
-- 
2.24.0


  reply	other threads:[~2020-02-10  7:44 UTC|newest]

Thread overview: 144+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-04 13:05 bug#32932: 27.0.50; render bugs on macOS Mojave Aaron Jensen
2018-10-04 14:07 ` Alan Third
2018-10-04 17:33   ` Charles A. Roelli
2018-10-04 17:48   ` Aaron Jensen
2018-10-04 18:25     ` Alan Third
     [not found]       ` <CAHyO48xS6yOWVvw2Gu+Hjumahe5BC3-EA+Mwztz4831Ac2U6aA@mail.gmail.com>
2018-10-04 18:45         ` Alan Third
2018-10-04 21:51           ` Alan Third
2018-10-04 23:03             ` Aaron Jensen
     [not found]               ` <CAHyO48zMuX95RB7hRYxAxt6wH_XB6sF1kmnbWZWmjpPhnkqjdg@mail.gmail.com>
2018-10-09  7:15                 ` Boris Buliga
2018-10-10 18:27                   ` Alan Third
2018-10-11  3:40                     ` Aaron Jensen
2018-10-14  8:19                       ` Aaron Jensen
2018-10-14  9:04                         ` Boris Buliga
2018-10-14 18:20                         ` Alan Third
2018-10-14 20:17                           ` Aaron Jensen
2018-10-16  4:53                             ` Boris Buliga
2018-10-16  8:39                               ` Boris Buliga
2018-10-16 19:04                               ` Aaron Jensen
2018-10-19 16:26                                 ` Aaron Jensen
2018-10-19 18:48                                   ` Alan Third
2018-10-19 19:24                                     ` Aaron Jensen
2018-10-20 20:04                                       ` Alan Third
2018-10-23  2:15                                         ` Aaron Jensen
2018-10-24 10:42                                           ` Alan Third
2018-10-29  2:18                                             ` Aaron Jensen
2018-10-29 16:09                                               ` Alan Third
2018-10-29 17:41                                                 ` Boris Buliga
2018-10-30  5:56                                                 ` Aaron Jensen
2018-10-30 15:35                                                   ` Boris Buliga
2018-10-31 21:59                                                     ` Alan Third
2018-11-01  4:25                                                       ` Aaron Jensen
2018-10-31 17:12                                                   ` Alan Third
2018-11-01  4:51                                                     ` Aaron Jensen
2018-11-01  4:58                                                       ` Aaron Jensen
2018-11-01  5:11                                                         ` Aaron Jensen
2018-11-01  6:13                                                           ` Boris Buliga
2018-11-01  6:51                                                             ` Aaron Jensen
2018-11-01 18:10                                                               ` Eli Zaretskii
2018-11-01 19:52                                                                 ` Aaron Jensen
2018-11-01 20:12                                                                   ` Eli Zaretskii
2018-11-01 20:29                                                                     ` Aaron Jensen
2018-11-03  9:23                                                                       ` Eli Zaretskii
2018-11-01 22:55                                                                 ` Alan Third
2018-11-03  9:31                                                                   ` Eli Zaretskii
2018-11-03 20:36                                                                     ` Alan Third
2018-11-03 21:03                                                                       ` Eli Zaretskii
2018-11-04 13:24                                                                         ` Alan Third
2018-11-04 20:11                                                                           ` Alan Third
2018-11-05 16:11                                                                             ` Aaron Jensen
2018-11-05 18:55                                                                               ` Alan Third
2018-11-06  4:04                                                                                 ` Aaron Jensen
2018-11-06 14:58                                                                                   ` Aaron Jensen
2018-11-08 15:21                                                                                     ` Alan Third
2018-11-08 15:35                                                                                       ` Eli Zaretskii
2018-11-08 16:17                                                                                         ` Alan Third
2018-11-08 16:28                                                                                           ` Aaron Jensen
2018-11-08 23:21                                                                                             ` Alan Third
2018-11-09  1:02                                                                                               ` Aaron Jensen
2018-11-09  9:08                                                                                                 ` bug#32932: [PATCH v2] Fix more drawing bugs in NS port (bug#32932) Alan Third
2018-11-09 13:45                                                                                                   ` Aaron Jensen
2018-11-09 14:15                                                                                                     ` Aaron Jensen
2018-11-13 22:13                                                                                                       ` Alan Third
2018-11-14 17:08                                                                                                         ` Aaron Jensen
2018-11-14 18:19                                                                                                           ` Alan Third
2018-11-16  1:20                                                                                                             ` Aaron Jensen
2018-11-19 22:35                                                                                                               ` Alan Third
2018-11-20  2:30                                                                                                                 ` Aaron Jensen
2018-11-23 18:17                                                                                                                   ` Alan Third
2018-11-26 16:20                                                                                                                     ` Aaron Jensen
2019-01-25 14:02                                                                                                                       ` Aaron Jensen
2019-01-25 22:01                                                                                                                         ` Alan Third
2018-11-09  8:02                                                                                               ` bug#32932: 27.0.50; render bugs on macOS Mojave Eli Zaretskii
2018-11-08 16:51                                                                                           ` Eli Zaretskii
2018-11-08 23:23                                                                                             ` Alan Third
2018-11-03 17:57                                                                   ` Aaron Jensen
2018-11-03 19:09                                                                     ` Alan Third
2018-11-03 20:51                                                                       ` Alan Third
2018-11-03 23:56                                                                         ` Aaron Jensen
2018-11-04 13:24                                                                           ` Alan Third
2018-11-04 17:12                                                                             ` Aaron Jensen
2018-11-04 18:28                                                                               ` Eli Zaretskii
2018-10-04 19:43       ` Aaron Jensen
2018-11-03 17:56 ` Aaron Jensen
2018-11-03 18:17   ` Eli Zaretskii
2018-11-05 16:20     ` Aaron Jensen
2018-11-27  1:42 ` bug#32932: 26.2: Too many flickers with normal operations on macOS 10.13.6 Zhang Haijun
2019-11-11 18:16 ` bug#32932: 27.0.50; render bugs on macOS Mojave Alan Third
2019-11-12 13:27   ` Robert Pluim
2019-11-12 14:38     ` Alan Third
2020-01-25 12:44       ` Alan Third
2020-01-25 13:37         ` Eli Zaretskii
2020-01-27 11:06           ` Robert Pluim
2020-01-27 20:45             ` Alan Third
2020-01-28  3:21               ` Eli Zaretskii
2020-01-28 18:23                 ` Alan Third
2020-01-28 19:35 ` Aaron Jensen
2020-01-28 20:07   ` Eli Zaretskii
2020-01-28 20:11     ` Aaron Jensen
2020-01-28 20:21       ` Eli Zaretskii
2020-01-28 20:24         ` Aaron Jensen
2020-01-29 10:08   ` Alan Third via Bug reports for GNU Emacs, the Swiss army knife of text editors
2020-01-29 16:32     ` Aaron Jensen
2020-01-29 20:04       ` Alan Third
2020-01-30  1:40         ` Aaron Jensen
2020-01-30 19:11           ` Alan Third
2020-01-30 20:07             ` Aaron Jensen
2020-01-31 14:57               ` Robert Pluim
2020-01-31 20:23                 ` Alan Third
2020-01-31 20:26                   ` Aaron Jensen
2020-02-01  1:26                     ` Aaron Jensen
2020-02-01 14:22                       ` Alan Third
2020-02-01 16:29                         ` Aaron Jensen
2020-02-01 21:20                           ` Alan Third
2020-02-01 23:05                             ` Aaron Jensen
2020-02-02 13:42                               ` Alan Third
2020-01-31  1:16             ` Stefan Kangas
2020-01-31 21:34 ` Mattias Engdegård
2020-02-02 12:31 ` Mattias Engdegård
2020-02-02 13:46   ` Alan Third
2020-02-02 16:49     ` Aaron Jensen
2020-02-02 22:30       ` Alan Third
2020-02-02 22:44         ` Mattias Engdegård
2020-02-03  0:14         ` Aaron Jensen
2020-02-03  7:39           ` Alan Third
2020-02-03  8:16             ` Aaron Jensen
2020-02-03 20:44               ` Alan Third
2020-02-03 20:46                 ` Aaron Jensen
2020-02-03 21:30                   ` Alan Third
2020-02-06 18:04                     ` Aaron Jensen
2020-02-07 20:18                       ` Alan Third
2020-02-08  1:26                         ` Aaron Jensen
2020-02-08 14:13                           ` Alan Third
2020-02-08 16:45                             ` Aaron Jensen
2020-02-10  7:44                               ` Alan Third [this message]
2020-02-10 15:21                                 ` Aaron Jensen
2020-02-10 17:14                                   ` Aaron Jensen
2020-02-10 21:33                                   ` Alan Third
2020-02-13 17:24                                     ` Aaron Jensen
2020-02-13 18:32                                       ` Alan Third
2020-02-03 16:04             ` Mattias Engdegård
2020-02-03 16:05               ` Aaron Jensen
2020-02-03 16:09                 ` Mattias Engdegård
2020-02-03 21:28           ` Alan Third
2020-02-10  8:59 ` Robert Pluim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200210074404.GA44470@breton.holly.idiocy.org \
    --to=alan@idiocy.org \
    --cc=32932@debbugs.gnu.org \
    --cc=aaronjensen@gmail.com \
    --cc=mattiase@acm.org \
    --cc=rpluim@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).