[PATCH v2 08/18] mt trace: track GL context create/destroy calls

Imre Deak imre.deak at intel.com
Mon May 21 05:57:07 PDT 2012


On Mon, 2012-05-21 at 10:54 +0300, Imre Deak wrote:
> On Fri, 2012-05-18 at 16:03 +0300, Pauli Nieminen wrote:
> > On Tue, May 15, 2012 at 05:11:03PM +0300, Imre Deak wrote:
>> >[...]
> > > --- /dev/null
> > > +++ b/wrappers/gltrace_state.cpp
> > > @@ -0,0 +1,71 @@
> > > +#include <gltrace.hpp>
> > > +#include <os_thread.hpp>
> > > +#include <assert.h>
> > > +
> > > +namespace gltrace {
> > > +
> > > +static std::map<unsigned long, Context *> context_map;
> > > +
> > > +class ThreadState {
> > > +public:
> > > +	Context *current_context;
> > > +	Context dummy_context;
> > > +
> > > +	ThreadState(void) : current_context(NULL) { }
> > > +};
> > > +
> > > +static os::thread_specific_ptr<struct ThreadState> thread_state;
> > > +
> > > +static ThreadState *get_ts(void)
> > > +{
> > > +	struct ThreadState *ts = thread_state.get();
> > > +
> > > +	if (!ts) {
> > > +		ts = new ThreadState;
> > > +		thread_state.reset(ts);
> > > +	}
> > > +
> > > +	return ts;
> > > +}
> > > +
> > > +void createContext(unsigned long context_id)
> > > +{
> > > +	assert(context_map.find(context_id) == context_map.end());
> > > +	context_map[context_id] = new Context();
> > > +}
> > > +
> > > +void destroyContext(unsigned long context_id)
> > > +{
> > > +	struct ThreadState *ts = get_ts();
> > > +	Context *ctx;
> > > +
> > > +	ctx = context_map[context_id];
> > > +	if (ctx == ts->current_context)
> > > +		ts->current_context = NULL;
> > > +
> > > +	context_map.erase(context_id);
> > > +	delete ctx;
> > 
> > eglDestroyContext is immediate only if context isn't current in any
> > thread. In case context is current in any thread destruction is
> > delayed untill all threads have released the context with MakeCurrent.
> > 
> > Implementing that would be simple with std::shared_ptr (c++11) or
> > boost::shared_ptr. You could keep shared_ptr in context_map and
> > ThreadState. In context destruction simple erasing the context from
> > context_map would work correctly.
> > 
> > Later on when setContext replaces the current context we will
> > automatically free the deleted context.
> 
> Though at the moment we don't do anything with the shared context
> parameter, we would need it to handle buffer lookups when the manual
> tracking implemented in this patchset is enabled. So yes, this needs
> fixing along the buffer lookup code.

After rethinking this, we don't actually need to care about shared
contexts at the moment and thus don't need refcounting of contexts. The
buffer lookup I referred to above is needed only for EGL (on Android).
In EGL sharing a context means sharing texture buffers, but no other
buffers. But in our buffer lookup workaround we only care about
GL_ELEMENT_ARRAY_BUFFER types.

--Imre




More information about the apitrace mailing list