[comp.windows.x] Question on creation on gc's

schultz@NRL-AIC.ARPA (Alan Schultz) (05/26/88)

I am using CLX and have the following question.

I have a lot of graphics functions that get called often. In each function,
I create a graphics context as needed, and assign it to a temporary
variable. Example:

(defun some-simple-graphics-primitive (x y color)
	(let ((gc (create-gcontext :drawable *window*
				   :foreground color)))
           (draw-rectangle *window* gc x y *width* *height*))
)

This is oversimplified, but the basic creation of gc's is demonstrated.
I read in the Xlib manual that gc's are valuable resources, and I also see
that primitives exist in CLX for manipulating gc's that already exist.
QUESTION: Is the above expensive? Or since the gc is assigned to a
temporary variable, is it freed up after the function returns?

I have MANY such functions that get called OFTEN. Is it better to create
one gc at the top level, and then modify it as necessary, or is the above
ok?

Also, is one approach better for time efficiency?

Thanks,

   Alan C. Schultz
   Code 5510
   Navy Center for Applied Research in Artificial Intelligence (NCAR A I)
   Naval Research Laboratory
   Washington, D.C. 20375-5000
   ARPA: schultz@nrl-aic.arpa
   (202) 767-2877

	      

RWS@ZERMATT.LCS.MIT.EDU (Robert Scheifler) (05/26/88)

    Date: Wed, 25 May 88 16:12:17 EDT
    From: Alan Schultz <schultz@nrl-aic.arpa>

    (defun some-simple-graphics-primitive (x y color)
	    (let ((gc (create-gcontext :drawable *window*
				       :foreground color)))
	       (draw-rectangle *window* gc x y *width* *height*))
    )

    QUESTION: Is the above expensive? Or since the gc is assigned to a
    temporary variable, is it freed up after the function returns?

GC's are *not* freed just because the local variable binding goes away.
You are creating many resources in the server, and never freeing them.
This is very expensive, and eventually you will run out of resource ids
or memory.

    I have MANY such functions that get called OFTEN. Is it better to create
    one gc at the top level, and then modify it as necessary

Yes, much better.  Take a look at the WITH-GCONTEXT macro, which
provides a dynamic binding mechanism (that C is sorely in need of).