trim: Add framework for performing dependency analysis while trimming

The current analysis is as brain-dead as possible. All calls are
interpreted as affecting the current "state" resource, and all calls
are also interpreted as depending on the current "state". In other
words, no calls at all will be trimmed. Obviously, this is not useful
analysis, but all the framewok is now present for easily doing more
interesting things.

A new --no-deps option is now made available to "apitrace trim" so
that users can avoid all dependency analysis and still get the prior
behavior where only calls explicitly included in the --calls option
are included in the trim output.
trim: Complain if given extraneous arguments.

Previously additional arguments were silently ignored, (which can lead
to confusion if the user expects to be able to specify a bare callset
on the command line rather than specifying a --calls option).
trim: Prune uninteresting calls while trimming (unless --no-prune is passed)

Examine the CALL_FLAG_UNINTERESTING flag and leave out these calls
when trimming. These calls are generally the calls that have no side
effects, (such as query functions). Some calls without side effects
might be considered interesting, (such as glGetError returning a
non-zero error).

This pruning behavior can be avoided by passing the new --no-prune
option (or the new --exact option which is identical to passing both
--no-prune and -no-deps).
trim: Add a more condensed usage message for error cases.

When explicitly asking for help, ("apitrace help trim" or "apitrace
trim --help"), the complete help message is still provided.

But when an error case triggers a usage message, (such as an invalid
command-line argument), a more compact usage message is provided so as
to not overwhelm the error message (such as "invalid argument:
--foo").
trim: Avoid doing any analysis past the end of the user-specified range.

This doesn't actually change any results, (the dependency analysis
already ensures that calls beyond the range won't get included).

But this is a nice optimization to prevent wasted time and memory
analyzing these calls that are really easy to reject.
trim: Trim most drawing operations outside of the user-specified range

Care is taken to consider a glBegin/glEnd block as a single drawing
operation, (so that it will enitrely included or entirely trimmed).

We're also careful to avoid trimming any drawing operation when there
are active side effects (other than rendering to the default
framebuffer). For example, if there is an active binding of
GL_FRAMEBUFFER or GL_DRAW_FRAMEBUFFER to any buffer object, or if we
are withing a glBeginTransformFeedback/glEndTransformFeedback block;
in any of these cases the drawing operation will not be trimmed.
trim: Close some memory leaks.

The parse_call() function allocates an object and returns a pointer,
so we need to explicitly call delete on that object.
trim: Drop unused textures while trimming.

We introduce a new dependencies map so that some resources can depend
on other resources, effectively providing a tree of calls that need to
be included when the user requests a particular call be preserved in
the trim.

For example, a SwapBuffers call depends on the "framebuffer".
A rendering operation makes "framebuffer" depend on "render-state".
Enabling texturing makes "render-state" depend on "texture-target-FOO".
Binding a texture makes "texture-target-FOO" depend on "texture-X".
Uploading a texture makes the current call provide "texture-X".
Creating a texture makes the current call provide "texture-X".

This way, if a particular SwapBuffers call is included in the trimmed
output, all of the above will be included. Otherwise, if a SwapBuffers
is trimmed, then the "framebuffer" dependencies will be cleared and
none of the above calls will be included in the trimmed output.
trim: Add support for trimming out unused shaders.

The provide/consume analysis framework is finally starting to show
dividends. This is a fairly significant amount of new functionality
for "apitrace trim" yet it requires relatively little new code.
trim: Fix bug linking incorrect programs to shaders through samplers.

Previously, the code was mapping locations returned by
glGetUniformLocation and looking in the location map to try ot
determine which program to link in a glUniform1i call. This was
entirely bogus, as distinct programs do not have distinct location
spaces.

The correct answer is to simply track the currently active program and
link it to textures being bound to shaders.
trim: Abstract common stringstream idioms into shared functions.

Using the new providef, linkf, and unlinkf functions shrinks the code
a bit. It's tempting to provide <stdargs> functions with full
printf-like formatting, but it's really not necessary as of yet.
trim: Add a --frames option for selecting frames to trim.

This accepts a "frame set" which has syntax identical to the "call
set" of the existing --calls option. So, one could trim to just the
drawing operations of the first 10 frames of a trace with
--frames=0-9/draw.
trim: Add a new --print-callset option.

This is useful whenever one has a need to manually tweak the trim operation.

Simply run:

	apitrace trim --print-callset --frames=<whatever> --calls=<whatever>

as desired. Then take the resulting callset that is printed and run:

	apitrace trim --calls=<callset> --exact

This should yield an identical trimming operation. Then this operation
can be tweaked by appending call ranges to the --calls option,
deleting rangess from the --calls option, or by appending a new
--frames option.
trim: Perform correct state tracking for glActiveTexture

Not all glBindTexture calls are created equal. With calls to
glActiveTexture intervening, calls to glBindTexture are actually
operating on indenpendent texture units.

It's actually quite easy to simple insert the number of the currently
active texture unit into the name of the texture target resource.
trim: Conservatively make shader programs depend on textures

Some shader programs sample textures, so if the shader is included in
the trimmed output the texture needs to be included as well.

We don't have support inside of "apitrace trim" to parse GLSL to find
names of samplers to find these dependencies. Instead, we provide here
a simple heuristic:

In order to sample a texture, the OpenGL program must associate the
name of a texture unit with a location in the shader by calling
glUniform1i (or glUniform1iv). So we can notice whenever texture-unit
names are being passed and create a dependency from the shader to all
texture targets for that unit.

This is conservative on two fronts:

  1. The value being passed to glUniformi might only be coincidentally
     the same as a texture-unit name and be actually unrelated to
     texture sampling.

  2. The shader likely doesn't depend on all texture targets, but
     again we can't know which without looking inside the shader, so
     we associate all of them.
trim: Add support for display lists.

The calls specified between glNewList and glEndList need to have
special treatment. For now we do the simplest thing and
unconditionally include all such calls in the trim output.

Eventually we will want to track display-list resources similar to
textures and trim away unused display lists.
trim: Never trim any textures bound within a display list.

This is conservative. The more efficient approach would create a
resource link from the display list to the texture, but that will wait
for when we actually have display-list resources.
trim: Never trim any glBindTexture calls.

This shouldn't be necessary. I've got a case where trimming out the
glBindTexture of an unused texture changes the behavoir, which
shouldn't be the case.

While I'll still try to track down that bug, this patch provides
conservative behavior that hopefully won't break anything. (The only
real downside is the potential of an implementation getting confused
by a glBindTexture with a value that was not returned by
glGenTextures).
trim: Move TraceAnalyzer out of cli_trim.cpp into its own file.

This class had long since outgrown where it was living at the top of
cli_trim.cpp. Thanks to José for recommending this.
trim: Print a warning message when doing dependency analysis.

As requested by José prior to merging this code upstream.