comparison mac/dw.m @ 826:6bb8bff36548

Implemented thread specific colors. Allows threads to have their own colors... also reducing color object recreation. Still leaking a little in Control Center... but it is significantly reduced.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 26 Mar 2011 20:51:40 +0000
parents 8a2e3138e1e4
children dc094750d284
comparison
equal deleted inserted replaced
825:8a2e3138e1e4 826:6bb8bff36548
70 else if(thiscolor < 17) 70 else if(thiscolor < 17)
71 { 71 {
72 return _colors[thiscolor]; 72 return _colors[thiscolor];
73 } 73 }
74 return 0; 74 return 0;
75 }
76
77 /* Thread specific storage */
78 #if !defined(GARBAGE_COLLECT)
79 pthread_key_t _dw_pool_key;
80 #endif
81 pthread_key_t _dw_fg_color_key;
82 pthread_key_t _dw_bg_color_key;
83
84 /* Create a default colors for a thread */
85 void _init_colors(void)
86 {
87 NSColor *fgcolor = [[NSColor grayColor] retain];
88 NSColor *bgcolor = [[NSColor blackColor] retain];
89
90 pthread_setspecific(_dw_fg_color_key, fgcolor);
91 pthread_setspecific(_dw_bg_color_key, bgcolor);
75 } 92 }
76 93
77 typedef struct _sighandler 94 typedef struct _sighandler
78 { 95 {
79 struct _sighandler *next; 96 struct _sighandler *next;
3937 * green: green value. 3954 * green: green value.
3938 * blue: blue value. 3955 * blue: blue value.
3939 */ 3956 */
3940 void API dw_color_foreground_set(unsigned long value) 3957 void API dw_color_foreground_set(unsigned long value)
3941 { 3958 {
3942 /* This may need to be thread specific */ 3959 NSColor *oldcolor = pthread_getspecific(_dw_fg_color_key);
3960 NSColor *newcolor;
3961
3943 _foreground = _get_color(value); 3962 _foreground = _get_color(value);
3963
3964 newcolor = [[NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green:
3965 DW_GREEN_VALUE(_foreground)/255.0 blue:
3966 DW_BLUE_VALUE(_foreground)/255.0 alpha: 1] retain];
3967 pthread_setspecific(_dw_fg_color_key, newcolor);
3968 [oldcolor release];
3944 } 3969 }
3945 3970
3946 /* Sets the current background drawing color. 3971 /* Sets the current background drawing color.
3947 * Parameters: 3972 * Parameters:
3948 * red: red value. 3973 * red: red value.
3949 * green: green value. 3974 * green: green value.
3950 * blue: blue value. 3975 * blue: blue value.
3951 */ 3976 */
3952 void API dw_color_background_set(unsigned long value) 3977 void API dw_color_background_set(unsigned long value)
3953 { 3978 {
3954 /* This may need to be thread specific */ 3979 NSColor *oldcolor = pthread_getspecific(_dw_bg_color_key);
3980 NSColor *newcolor;
3981
3955 _background = _get_color(value); 3982 _background = _get_color(value);
3983
3984 newcolor = [[NSColor colorWithDeviceRed: DW_RED_VALUE(_background)/255.0 green:
3985 DW_GREEN_VALUE(_background)/255.0 blue:
3986 DW_BLUE_VALUE(_background)/255.0 alpha: 1] retain];
3987 pthread_setspecific(_dw_bg_color_key, newcolor);
3988 [oldcolor release];
3956 } 3989 }
3957 3990
3958 /* Allows the user to choose a color using the system's color chooser dialog. 3991 /* Allows the user to choose a color using the system's color chooser dialog.
3959 * Parameters: 3992 * Parameters:
3960 * value: current color 3993 * value: current color
4027 } 4060 }
4028 _DWLastDrawable = handle; 4061 _DWLastDrawable = handle;
4029 } 4062 }
4030 NSBezierPath* aPath = [NSBezierPath bezierPath]; 4063 NSBezierPath* aPath = [NSBezierPath bezierPath];
4031 [aPath setLineWidth: 0.5]; 4064 [aPath setLineWidth: 0.5];
4032 NSColor *color = [NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green: DW_GREEN_VALUE(_foreground)/255.0 blue: DW_BLUE_VALUE(_foreground)/255.0 alpha: 1]; 4065 NSColor *color = pthread_getspecific(_dw_fg_color_key);
4033 [color set]; 4066 [color set];
4034 4067
4035 [aPath moveToPoint:NSMakePoint(x, y)]; 4068 [aPath moveToPoint:NSMakePoint(x, y)];
4036 [aPath stroke]; 4069 [aPath stroke];
4037 [image unlockFocus]; 4070 [image unlockFocus];
4066 } 4099 }
4067 _DWLastDrawable = handle; 4100 _DWLastDrawable = handle;
4068 } 4101 }
4069 NSBezierPath* aPath = [NSBezierPath bezierPath]; 4102 NSBezierPath* aPath = [NSBezierPath bezierPath];
4070 [aPath setLineWidth: 0.5]; 4103 [aPath setLineWidth: 0.5];
4071 NSColor *color = [NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green: DW_GREEN_VALUE(_foreground)/255.0 blue: DW_BLUE_VALUE(_foreground)/255.0 alpha: 1]; 4104 NSColor *color = pthread_getspecific(_dw_fg_color_key);
4072 [color set]; 4105 [color set];
4073 4106
4074 [aPath moveToPoint:NSMakePoint(x1, y1)]; 4107 [aPath moveToPoint:NSMakePoint(x1, y1)];
4075 [aPath lineToPoint:NSMakePoint(x2, y2)]; 4108 [aPath lineToPoint:NSMakePoint(x2, y2)];
4076 [aPath stroke]; 4109 [aPath stroke];
4102 if([image lockFocusIfCanDraw] == NO) 4135 if([image lockFocusIfCanDraw] == NO)
4103 { 4136 {
4104 DW_MUTEX_UNLOCK; 4137 DW_MUTEX_UNLOCK;
4105 return; 4138 return;
4106 } 4139 }
4107 NSColor *color = [NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green: DW_GREEN_VALUE(_foreground)/255.0 blue: DW_BLUE_VALUE(_foreground)/255.0 alpha: 1]; 4140 NSColor *color = pthread_getspecific(_dw_fg_color_key);
4108 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil]; 4141 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil];
4109 if(font) 4142 if(font)
4110 { 4143 {
4111 [dict setValue:font forKey:NSFontAttributeName]; 4144 [dict setValue:font forKey:NSFontAttributeName];
4112 } 4145 }
4123 { 4156 {
4124 font = [render font]; 4157 font = [render font];
4125 } 4158 }
4126 image = (id)pixmap->image; 4159 image = (id)pixmap->image;
4127 [image lockFocus]; 4160 [image lockFocus];
4128 NSColor *color = [NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green: DW_GREEN_VALUE(_foreground)/255.0 blue: DW_BLUE_VALUE(_foreground)/255.0 alpha: 1]; 4161 NSColor *color = pthread_getspecific(_dw_fg_color_key);
4129 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil]; 4162 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil];
4130 if(font) 4163 if(font)
4131 { 4164 {
4132 [dict setValue:font forKey:NSFontAttributeName]; 4165 [dict setValue:font forKey:NSFontAttributeName];
4133 } 4166 }
4207 } 4240 }
4208 _DWLastDrawable = handle; 4241 _DWLastDrawable = handle;
4209 } 4242 }
4210 NSBezierPath* aPath = [NSBezierPath bezierPath]; 4243 NSBezierPath* aPath = [NSBezierPath bezierPath];
4211 [aPath setLineWidth: 0.5]; 4244 [aPath setLineWidth: 0.5];
4212 NSColor *color = [NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green: DW_GREEN_VALUE(_foreground)/255.0 blue: DW_BLUE_VALUE(_foreground)/255.0 alpha: 1]; 4245 NSColor *color = pthread_getspecific(_dw_fg_color_key);
4213 [color set]; 4246 [color set];
4214 4247
4215 [aPath moveToPoint:NSMakePoint(*x, *y)]; 4248 [aPath moveToPoint:NSMakePoint(*x, *y)];
4216 for(z=1;z<npoints;z++) 4249 for(z=1;z<npoints;z++)
4217 { 4250 {
4256 } 4289 }
4257 _DWLastDrawable = handle; 4290 _DWLastDrawable = handle;
4258 } 4291 }
4259 NSBezierPath* aPath = [NSBezierPath bezierPath]; 4292 NSBezierPath* aPath = [NSBezierPath bezierPath];
4260 [aPath setLineWidth: 0.5]; 4293 [aPath setLineWidth: 0.5];
4261 NSColor *color = [NSColor colorWithDeviceRed: DW_RED_VALUE(_foreground)/255.0 green: DW_GREEN_VALUE(_foreground)/255.0 blue: DW_BLUE_VALUE(_foreground)/255.0 alpha: 1]; 4294 NSColor *color = pthread_getspecific(_dw_fg_color_key);
4262 [color set]; 4295 [color set];
4263 4296
4264 [aPath moveToPoint:NSMakePoint(x, y)]; 4297 [aPath moveToPoint:NSMakePoint(x, y)];
4265 [aPath lineToPoint:NSMakePoint(x, y + height)]; 4298 [aPath lineToPoint:NSMakePoint(x, y + height)];
4266 [aPath lineToPoint:NSMakePoint(x + width, y + height)]; 4299 [aPath lineToPoint:NSMakePoint(x + width, y + height)];
7874 free(eve); 7907 free(eve);
7875 } 7908 }
7876 return 0; 7909 return 0;
7877 } 7910 }
7878 7911
7879 #if !defined(GARBAGE_COLLECT)
7880 pthread_key_t _dw_pool_key;
7881 #endif
7882
7883 /* Mac specific function to cause garbage collection */ 7912 /* Mac specific function to cause garbage collection */
7884 void _dw_pool_drain(void) 7913 void _dw_pool_drain(void)
7885 { 7914 {
7886 #if !defined(GARBAGE_COLLECT) 7915 #if !defined(GARBAGE_COLLECT)
7887 NSAutoreleasePool *pool = pthread_getspecific(_dw_pool_key); 7916 NSAutoreleasePool *pool = pthread_getspecific(_dw_pool_key);
7894 /* 7923 /*
7895 * Setup thread independent pools. 7924 * Setup thread independent pools.
7896 */ 7925 */
7897 void _dwthreadstart(void *data) 7926 void _dwthreadstart(void *data)
7898 { 7927 {
7899 void (*threadfunc)(void *) = NULL; 7928 void (*threadfunc)(void *) = NULL;
7900 void **tmp = (void **)data; 7929 void **tmp = (void **)data;
7930 NSColor *color;
7931
7901 /* If we aren't using garbage collection we need autorelease pools */ 7932 /* If we aren't using garbage collection we need autorelease pools */
7902 #if !defined(GARBAGE_COLLECT) 7933 #if !defined(GARBAGE_COLLECT)
7903 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 7934 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
7904 pthread_setspecific(_dw_pool_key, pool); 7935 pthread_setspecific(_dw_pool_key, pool);
7905 #endif 7936 #endif
7906 7937 _init_colors();
7907 threadfunc = (void (*)(void *))tmp[0]; 7938
7908 7939 threadfunc = (void (*)(void *))tmp[0];
7909 threadfunc(tmp[1]); 7940
7941 /* Start our thread function */
7942 threadfunc(tmp[1]);
7943
7910 /* Release the pool when we are done so we don't leak */ 7944 /* Release the pool when we are done so we don't leak */
7945 color = pthread_getspecific(_dw_fg_color_key);
7946 [color release];
7947 color = pthread_getspecific(_dw_bg_color_key);
7948 [color release];
7911 #if !defined(GARBAGE_COLLECT) 7949 #if !defined(GARBAGE_COLLECT)
7912 pool = pthread_getspecific(_dw_pool_key); 7950 pool = pthread_getspecific(_dw_pool_key);
7913 [pool drain]; 7951 [pool drain];
7914 #endif 7952 #endif
7915 free(tmp); 7953 free(tmp);
7916 } 7954 }
7917 7955
7918 void _dw_default_font(char *fontname) 7956 void _dw_default_font(char *fontname)
7919 { 7957 {
7920 if(DWDefaultFont) 7958 if(DWDefaultFont)
7945 #if !defined(GARBAGE_COLLECT) 7983 #if !defined(GARBAGE_COLLECT)
7946 pthread_key_create(&_dw_pool_key, NULL); 7984 pthread_key_create(&_dw_pool_key, NULL);
7947 pool = [[NSAutoreleasePool alloc] init]; 7985 pool = [[NSAutoreleasePool alloc] init];
7948 pthread_setspecific(_dw_pool_key, pool); 7986 pthread_setspecific(_dw_pool_key, pool);
7949 #endif 7987 #endif
7988 pthread_key_create(&_dw_fg_color_key, NULL);
7989 pthread_key_create(&_dw_bg_color_key, NULL);
7990 _init_colors();
7950 /* Create a default main menu, with just the application menu */ 7991 /* Create a default main menu, with just the application menu */
7951 DWMainMenu = _generate_main_menu(); 7992 DWMainMenu = _generate_main_menu();
7952 [DWMainMenu retain]; 7993 [DWMainMenu retain];
7953 [DWApp setMainMenu:DWMainMenu]; 7994 [DWApp setMainMenu:DWMainMenu];
7954 DWObj = [[DWObject alloc] init]; 7995 DWObj = [[DWObject alloc] init];