From 1a142fdd96dc29eebad8175ffa0345f7562e0398 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 7 Jun 2017 22:51:20 +0200 Subject: Add helpers for retain/release and use in bitmap.m --- frontends/cocoa/NetSurf.xcodeproj/project.pbxproj | 29 +++++----- frontends/cocoa/arc.h | 22 ++++++++ frontends/cocoa/bitmap.m | 69 ++++++++--------------- 3 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 frontends/cocoa/arc.h (limited to 'frontends/cocoa') diff --git a/frontends/cocoa/NetSurf.xcodeproj/project.pbxproj b/frontends/cocoa/NetSurf.xcodeproj/project.pbxproj index 8c32db748..bc1bee765 100644 --- a/frontends/cocoa/NetSurf.xcodeproj/project.pbxproj +++ b/frontends/cocoa/NetSurf.xcodeproj/project.pbxproj @@ -702,6 +702,7 @@ 263A29011EE412B4005C52B9 /* uievents.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = uievents.idl; sourceTree = ""; }; 263A29021EE412B4005C52B9 /* urlutils.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = urlutils.idl; sourceTree = ""; }; 263A29061EE4196E005C52B9 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; + 26890F951EE89C5F00063C30 /* arc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = arc.h; sourceTree = SOURCE_ROOT; }; 2689BB2D1EE543E20090B679 /* PSMTabBarControl+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "PSMTabBarControl+Private.h"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -735,11 +736,12 @@ 261DB2501318444F00C59F12 /* extract-strings.sh */, ); name = Tools; - sourceTree = ""; + sourceTree = SOURCE_ROOT; }; 263A25EF1EE40BD6005C52B9 /* Cocoa */ = { isa = PBXGroup; children = ( + 26890F951EE89C5F00063C30 /* arc.h */, 263A26021EE40BFC005C52B9 /* PSMTabBarControl */, 263A262C1EE40BFC005C52B9 /* res */, 263A26611EE40BFC005C52B9 /* apple_image.h */, @@ -807,8 +809,7 @@ 263A269F1EE40BFC005C52B9 /* URLFieldCell.m */, ); name = Cocoa; - path = NetSurfBrowser; - sourceTree = ""; + sourceTree = SOURCE_ROOT; }; 263A26021EE40BFC005C52B9 /* PSMTabBarControl */ = { isa = PBXGroup; @@ -845,7 +846,7 @@ 2689BB2D1EE543E20090B679 /* PSMTabBarControl+Private.h */, ); path = PSMTabBarControl; - sourceTree = SOURCE_ROOT; + sourceTree = ""; }; 263A26031EE40BFC005C52B9 /* Images */ = { isa = PBXGroup; @@ -899,7 +900,7 @@ 263A26601EE40BFC005C52B9 /* SearchWindow.xib */, ); path = res; - sourceTree = SOURCE_ROOT; + sourceTree = ""; }; 263A264C1EE40BFC005C52B9 /* Icons */ = { isa = PBXGroup; @@ -925,7 +926,8 @@ 263A27B81EE40C4B005C52B9 /* utils */, ); name = Engine; - sourceTree = ""; + path = ../..; + sourceTree = SOURCE_ROOT; }; 263A26A11EE40C4A005C52B9 /* content */ = { isa = PBXGroup; @@ -956,8 +958,7 @@ 263A273F1EE40C4B005C52B9 /* urldb.c */, 263A27401EE40C4B005C52B9 /* urldb.h */, ); - name = content; - path = ../../content; + path = content; sourceTree = ""; }; 263A26AD1EE40C4A005C52B9 /* fetchers */ = { @@ -1096,8 +1097,7 @@ 263A277B1EE40C4B005C52B9 /* version.c */, 263A277C1EE40C4B005C52B9 /* version.h */, ); - name = desktop; - path = ../../desktop; + path = desktop; sourceTree = ""; }; 263A277D1EE40C4B005C52B9 /* include */ = { @@ -1105,8 +1105,7 @@ children = ( 263A277E1EE40C4B005C52B9 /* netsurf */, ); - name = include; - path = ../../include; + path = include; sourceTree = ""; }; 263A277E1EE40C4B005C52B9 /* netsurf */ = { @@ -1176,8 +1175,7 @@ 263A27B61EE40C4B005C52B9 /* textplain.c */, 263A27B71EE40C4B005C52B9 /* textplain.h */, ); - name = render; - path = ../../render; + path = render; sourceTree = ""; }; 263A27B81EE40C4B005C52B9 /* utils */ = { @@ -1250,8 +1248,7 @@ 263A280E1EE40C4B005C52B9 /* utsname.h */, 263A280F1EE40C4B005C52B9 /* valgrind.supp */, ); - name = utils; - path = ../../utils; + path = utils; sourceTree = ""; }; 263A27CF1EE40C4B005C52B9 /* http */ = { diff --git a/frontends/cocoa/arc.h b/frontends/cocoa/arc.h new file mode 100644 index 000000000..bfdf31c5c --- /dev/null +++ b/frontends/cocoa/arc.h @@ -0,0 +1,22 @@ +#ifndef arc_h +#define arc_h + +/** + * Retains an Objective-C object and returns a pointer that can be passed to C. + * @param object The object to retain + * @return Pointer suitable to be stored in C code. + */ +static inline void *arc_retain(id object) { + return (__bridge_retained void *)object; +} + +/** + * Releases the Objective-C object pointed to by a C pointer. + * @param pointer Object pointer to release. + */ +static inline void arc_release(void *pointer) { + id object = (__bridge_transfer id)pointer; + object = nil; +} + +#endif diff --git a/frontends/cocoa/bitmap.m b/frontends/cocoa/bitmap.m index 597a5af79..554d5e0bf 100644 --- a/frontends/cocoa/bitmap.m +++ b/frontends/cocoa/bitmap.m @@ -31,6 +31,8 @@ #import "cocoa/plotter.h" #import "cocoa/bitmap.h" +#import "cocoa/arc.h" + #define BITS_PER_SAMPLE (8) #define SAMPLES_PER_PIXEL (4) #define BITS_PER_PIXEL (BITS_PER_SAMPLE * SAMPLES_PER_PIXEL) @@ -41,7 +43,6 @@ #define ALPHA_OFFSET (3) static CGImageRef cocoa_prepare_bitmap(void *bitmap); -//static NSMapTable *cocoa_get_bitmap_cache( void ); static inline NSMapTable *cocoa_get_bitmap_cache(void) { @@ -52,40 +53,32 @@ static inline NSMapTable *cocoa_get_bitmap_cache(void) return cache; } +static inline NSBitmapImageRep *get_bitmap(void *bitmap) { + NSCParameterAssert(bitmap); + return (__bridge NSBitmapImageRep *)bitmap; +} + static int bitmap_get_width(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return (int)[bmp pixelsWide]; + return (int)get_bitmap(bitmap).pixelsWide; } static int bitmap_get_height(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return (int)[bmp pixelsHigh]; + return (int)get_bitmap(bitmap).pixelsHigh; } static bool bitmap_get_opaque(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp isOpaque]; + return get_bitmap(bitmap).isOpaque; } static void bitmap_destroy(void *bitmap) { NSCParameterAssert(NULL != bitmap); - NSMapTable *cache = cocoa_get_bitmap_cache(); - CGImageRef image = NSMapGet(cache, bitmap); - if (NULL != image) { - CGImageRelease(image); - NSMapRemove(cache, bitmap); - } - - NSBitmapImageRep *bmp = (__bridge_transfer NSBitmapImageRep *)bitmap; - bmp = nil; + cocoa_bitmap_modified(bitmap); + arc_release(bitmap); } static void *bitmap_create(int width, int height, unsigned int state) @@ -103,35 +96,27 @@ static void *bitmap_create(int width, int height, unsigned int state) bytesPerRow:BYTES_PER_PIXEL * width bitsPerPixel:BITS_PER_PIXEL]; - return (__bridge_retained void *)bmp; + return arc_retain(bmp); } static void bitmap_set_opaque(void *bitmap, bool opaque) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - [bmp setOpaque:opaque ? YES : NO]; + get_bitmap(bitmap).opaque = opaque ? YES : NO; } static unsigned char *bitmap_get_buffer(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bitmapData]; + return get_bitmap(bitmap).bitmapData; } static size_t bitmap_get_rowstride(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bytesPerRow]; + return get_bitmap(bitmap).bytesPerRow; } static size_t bitmap_get_bpp(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bitsPerPixel] / 8; + return get_bitmap(bitmap).bitsPerPixel / 8; } static bool bitmap_test_opaque(void *bitmap) @@ -159,11 +144,8 @@ static bool bitmap_test_opaque(void *bitmap) static bool bitmap_save(void *bitmap, const char *path, unsigned flags) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - - NSData *tiff = [bmp TIFFRepresentation]; - return [tiff writeToFile:[NSString stringWithUTF8String:path] atomically:YES]; + NSData *tiff = get_bitmap(bitmap).TIFFRepresentation; + return [tiff writeToFile:@(path) atomically:YES]; } void cocoa_bitmap_modified(void *bitmap) @@ -191,17 +173,16 @@ CGImageRef cocoa_get_cgimage(void *bitmap) static CGImageRef cocoa_prepare_bitmap(void *bitmap) { - NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = get_bitmap(bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + size_t w = bmp.pixelsWide; + size_t h = bmp.pixelsHigh; - size_t w = [bmp pixelsWide]; - size_t h = [bmp pixelsHigh]; + CGImageRef original = bmp.CGImage; - CGImageRef original = [bmp CGImage]; - - if (h <= 1) + if (h <= 1) { return CGImageRetain(original); + } void *data = malloc(4 * w * h); -- cgit v1.2.3