From 2a0e262253f3849362f7ac4db6f5e8bb04d77491 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 4 Feb 2018 22:30:59 -0500 Subject: Reuse CGColorSpace and CGDataProvider in gfcocoa --- bin/gfcocoa.m | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'bin') diff --git a/bin/gfcocoa.m b/bin/gfcocoa.m index d91107b9..0e2fbe2c 100644 --- a/bin/gfcocoa.m +++ b/bin/gfcocoa.m @@ -29,10 +29,17 @@ extern void input(char in); @interface BufferView : NSView { size_t bufSize; uint32_t *buf; + CGColorSpaceRef colorSpace; + CGDataProviderRef dataProvider; } @end @implementation BufferView +- (instancetype) initWithFrame: (NSRect) frameRect { + colorSpace = CGColorSpaceCreateDeviceRGB(); + return [super initWithFrame: frameRect]; +} + - (void) draw { draw(buf, [self frame].size.width, [self frame].size.height); [self setNeedsDisplay: YES]; @@ -45,6 +52,8 @@ extern void input(char in); bufSize = newBufSize; buf = malloc(bufSize); if (!buf) err(EX_OSERR, "malloc(%zu)", bufSize); + CGDataProviderRelease(dataProvider); + dataProvider = CGDataProviderCreateWithData(NULL, buf, bufSize, NULL); } [self draw]; } @@ -52,25 +61,15 @@ extern void input(char in); - (void) drawRect: (NSRect) UNUSED dirtyRect { NSSize size = [self frame].size; CGContextRef ctx = [[NSGraphicsContext currentContext] CGContext]; - CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); - CGDataProviderRef data = CGDataProviderCreateWithData(NULL, buf, bufSize, NULL); CGImageRef image = CGImageCreate( - size.width, - size.height, - 8, - 32, - 4 * size.width, - rgb, - kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, - data, - NULL, - false, - kCGRenderingIntentDefault + size.width, size.height, + 8, 32, 4 * size.width, + colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, + dataProvider, + NULL, false, kCGRenderingIntentDefault ); CGContextDrawImage(ctx, [self frame], image); CGImageRelease(image); - CGDataProviderRelease(data); - CGColorSpaceRelease(rgb); } - (BOOL) acceptsFirstResponder { -- cgit 1.4.1