Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/MacVim/MMCoreTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
CGGlyph *glyphs;
CGPoint *positions;
NSMutableArray *fontCache;

CGLayerRef layer;
CGContextRef layerContext;
// These are used in MMCoreTextView+ToolTip.m
id trackingRectOwner_; // (not retained)
void *trackingRectUserData_;
Expand Down
78 changes: 65 additions & 13 deletions src/MacVim/MMCoreTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,19 @@ - (void)drawRect:(NSRect)rect
[self batchDrawData:data];

[drawData removeAllObjects];

CGLayerRef l = [self getLayer];

/* during a live resize, we will have around a stale layer until the
* refresh messages travel back from the vim process. we push the old
* layer in at an offset to get rid of jitter due to lines changing position. */

CGSize layerSize = CGLayerGetSize(l);
CGSize frameSize = [self frame].size;
NSRect drawRect = NSMakeRect(0, frameSize.height - layerSize.height, layerSize.width, layerSize.height);

CGContextDrawLayerInRect([context graphicsPort], drawRect, l);

}

- (void)performBatchDrawWithData:(NSData *)data
Expand All @@ -605,6 +618,33 @@ - (void)performBatchDrawWithData:(NSData *)data
[self display];
}

- (void)releaseLayer
{
if (layer) {
CGLayerRelease(layer);
layer = nil;
layerContext = nil;
}
}
- (CGLayerRef)getLayer
{
if (!layer) {
NSGraphicsContext *context = [NSGraphicsContext currentContext];
NSRect frame = [self frame];
layer = CGLayerCreateWithContext([context graphicsPort], frame.size, NULL);
}
return layer;
}

- (CGContextRef)getLayerContext
{
if (!layerContext)
layerContext = CGLayerGetContext([self getLayer]);

return layerContext;
}


- (NSSize)constrainRows:(int *)rows columns:(int *)cols toSize:(NSSize)size
{
// TODO:
Expand Down Expand Up @@ -852,7 +892,7 @@ - (void)batchDrawData:(NSData *)data
const void *end = bytes + [data length];

#if MM_DEBUG_DRAWING
ASLogNotice(@"====> BEGIN %s", _cmd);
ASLogNotice(@"====> BEGIN");
#endif
// TODO: Sanity check input

Expand Down Expand Up @@ -908,10 +948,13 @@ - (void)batchDrawData:(NSData *)data
column:col
numRows:height
numColumns:width];
[signImg drawInRect:r
fromRect:NSZeroRect
operation:NSCompositingOperationSourceOver
fraction:1.0];

CGContextRef context = [self getLayerContext];
CGImageRef cgImage = [signImg CGImageForProposedRect:&r
context:nil
hints:nil];

CGContextDrawImage(context, r, cgImage);
} else if (DrawStringDrawType == type) {
int bg = *((int*)bytes); bytes += sizeof(int);
int fg = *((int*)bytes); bytes += sizeof(int);
Expand Down Expand Up @@ -1013,7 +1056,7 @@ - (void)batchDrawData:(NSData *)data
}

#if MM_DEBUG_DRAWING
ASLogNotice(@"<==== END %s", _cmd);
ASLogNotice(@"<==== END");
#endif
}

Expand Down Expand Up @@ -1294,7 +1337,7 @@ - (void)drawString:(const UniChar *)chars length:(UniCharCount)length
withFlags:(int)flags foregroundColor:(int)fg
backgroundColor:(int)bg specialColor:(int)sp
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextRef context = [self getLayerContext];
NSRect frame = [self bounds];
float x = col*cellSize.width + insetSize.width;
float y = frame.size.height - insetSize.height - (1+row)*cellSize.height;
Expand Down Expand Up @@ -1412,8 +1455,16 @@ - (void)drawString:(const UniChar *)chars length:(UniCharCount)length

- (void)scrollRect:(NSRect)rect lineCount:(int)count
{
NSSize delta={0, -count * cellSize.height};
[self scrollRect:rect by:delta];
CGContextRef context = [self getLayerContext];
int yOffset = count * cellSize.height;
NSRect clipRect = rect;
clipRect.origin.y -= yOffset;

/* draw self on top of self, offset so as to "scroll" lines vertically. */
CGContextSaveGState(context);
CGContextClipToRect(context, clipRect);
CGContextDrawLayerAtPoint(context, CGPointMake(0, -yOffset), [self getLayer]);
CGContextRestoreGState(context);
}

- (void)deleteLinesFromRow:(int)row lineCount:(int)count
Expand Down Expand Up @@ -1455,7 +1506,7 @@ - (void)insertLinesAtRow:(int)row lineCount:(int)count
- (void)clearBlockFromRow:(int)row1 column:(int)col1 toRow:(int)row2
column:(int)col2 color:(int)color
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextRef context = [self getLayerContext];
NSRect rect = [self rectFromRow:row1 column:col1 toRow:row2 column:col2];

CGContextSetRGBFillColor(context, RED(color), GREEN(color), BLUE(color),
Expand All @@ -1468,7 +1519,8 @@ - (void)clearBlockFromRow:(int)row1 column:(int)col1 toRow:(int)row2

- (void)clearAll
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
[self releaseLayer];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why releaseLayer here?

@osheroff osheroff Nov 15, 2016 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm basically using it as a stand-in for resize code: when we're in live-resize, I need to keep the old (smaller or larger) layer around in order to draw smoothly -- so I can't release (to resize) the layer and re-initialize it on a window-resize event, I have to wait until we've got the batch for the resized window.

I suppose I coulda also hooked into SetTextDimensionsMsgID or something like that, but I'd be less sure of the timing of all the various events; might be jittery or racy.

CGContextRef context = [self getLayerContext];
NSRect rect = [self bounds];
float r = [defaultBackgroundColor redComponent];
float g = [defaultBackgroundColor greenComponent];
Expand All @@ -1484,7 +1536,7 @@ - (void)clearAll
- (void)drawInsertionPointAtRow:(int)row column:(int)col shape:(int)shape
fraction:(int)percent color:(int)color
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextRef context = [self getLayerContext];
NSRect rect = [self rectForRow:row column:col numRows:1 numColumns:1];

CGContextSaveGState(context);
Expand Down Expand Up @@ -1533,7 +1585,7 @@ - (void)drawInvertedRectAtRow:(int)row column:(int)col numRows:(int)nrows
numColumns:(int)ncols
{
// TODO: THIS CODE HAS NOT BEEN TESTED!
CGContextRef cgctx = [[NSGraphicsContext currentContext] graphicsPort];
CGContextRef cgctx = [self getLayerContext];
CGContextSaveGState(cgctx);
CGContextSetBlendMode(cgctx, kCGBlendModeDifference);
CGContextSetRGBFillColor(cgctx, 1.0, 1.0, 1.0, 1.0);
Expand Down