1 // File: OpenGl_ResourceCleaner.cxx
2 // Created: 18.03.11 9:40:00
3 // Author: Anton POLETAEV
5 #include <OpenGl_ResourceCleaner.hxx>
6 #include <OpenGl_ResourceVBO.hxx>
8 //=======================================================================
9 //function : OpenGl_ResourceCleaner
10 //purpose : Constructor
11 //=======================================================================
13 OpenGl_ResourceCleaner::OpenGl_ResourceCleaner()
15 mySharedContexts.Clear();
16 mySharedQueue.Clear();
17 myInstanceQueue.Clear();
20 //=======================================================================
21 //function : AppendContext
22 //purpose : Append given OpenGl context to the OpenGl_ResourceCleaner
24 //=======================================================================
26 void OpenGl_ResourceCleaner::AppendContext(GLCONTEXT theContext, Standard_Boolean isShared)
29 // appending shared context
32 mySharedContexts.Add(theContext);
36 // if context is already in the list
37 if (myInstanceQueue.IsBound(theContext))
39 QueueOfResources *aQueue = &myInstanceQueue.ChangeFind(theContext);
44 // context is not in the list - create empty queue for it and add queue to the list
45 QueueOfResources aQueue;
47 myInstanceQueue.Bind(theContext, aQueue);
53 //=======================================================================
54 //function : AddResource
55 //purpose : Tell the OpenGl_ResourceCleaner to clean up the OpenGl
57 //=======================================================================
59 Standard_Boolean OpenGl_ResourceCleaner::AddResource(GLCONTEXT theContext, Handle_OpenGl_Resource theResource)
61 // if context found in the shared list
62 if (mySharedContexts.Contains(theContext))
64 mySharedQueue.Push(theResource);
67 // if not, then it might be found in the non-shared list
68 else if (myInstanceQueue.IsBound(theContext))
70 QueueOfResources * aQueue = &myInstanceQueue.ChangeFind(theContext);
71 aQueue->Push(theResource);
75 // context is not under OpenGl_ResourceCleaner control, do not tell to clean the resource
76 return Standard_False;
79 //=======================================================================
81 //purpose : Cancel clean procedure for all the resources added to the
82 // OpenGl_ResourceCleaner
83 //=======================================================================
85 void OpenGl_ResourceCleaner::Clear()
87 mySharedQueue.Clear();
88 DataMapOfContextsResources::Iterator anIter(myInstanceQueue);
90 // remove the resources from the list
91 for (anIter.Reset(); anIter.More(); anIter.Next())
93 QueueOfResources * aQueue = &anIter.ChangeValue();
99 //=======================================================================
101 //purpose : Cancel clean procedure for all the resources of the specific
102 // OpenGl context which were added to the OpenGl_ResourceCleaner
103 //=======================================================================
105 Standard_Boolean OpenGl_ResourceCleaner::Clear(GLCONTEXT theContext)
107 // check if the context in the the control list
108 if (myInstanceQueue.IsBound(theContext))
110 QueueOfResources * aQueue = &myInstanceQueue.ChangeFind(theContext);
112 return Standard_True;
115 return Standard_False;
118 //=======================================================================
119 //function : ClearShared
120 //purpose : Cancel clean procedure for all of the shared resources
121 //=======================================================================
123 void OpenGl_ResourceCleaner::ClearShared()
125 mySharedQueue.Clear();
128 //=======================================================================
130 //purpose : Clear the unused resources for active OpenGl context
131 //=======================================================================
133 void OpenGl_ResourceCleaner::Cleanup (const Handle(OpenGl_Context)& theGlContext)
135 GLCONTEXT aContext = GET_GL_CONTEXT();
136 if (aContext == NULL)
139 // if the context is found in shared list
140 if (mySharedContexts.Contains (aContext))
142 while (mySharedQueue.Size() > 0)
144 mySharedQueue.Front()->Clean (theGlContext); // delete resource memory
148 // if the context is found in non-shared list
149 else if (myInstanceQueue.IsBound (aContext))
151 QueueOfResources* aQueue = &myInstanceQueue.ChangeFind (aContext);
152 while (aQueue->Size() > 0)
154 aQueue->Front()->Clean (theGlContext); // delete resource memory
160 //=======================================================================
161 //function : RemoveContext
162 //purpose : Remove the OpenGl context from the OpenGl_ResourceCleaner list
163 //=======================================================================
165 void OpenGl_ResourceCleaner::RemoveContext(GLCONTEXT theContext)
167 // if context can be found in shared list try to remove it
168 // if it wasn't in there, try to remove it from non-shared list
169 if (!mySharedContexts.Remove(theContext))
170 myInstanceQueue.UnBind(theContext);
172 // if no contexts in shared list, then there is no need to clean
173 // the resources on redraw
174 if (mySharedContexts.Size() == 0)
175 mySharedQueue.Clear();
179 //=======================================================================
180 //function : GetSharedContext
181 //purpose : Get any of shared contexts from the OpenGl_ResourceCleaner list
182 //=======================================================================
184 GLCONTEXT OpenGl_ResourceCleaner::GetSharedContext() const
186 if(mySharedContexts.Size() > 0)
188 MapOfContexts::Iterator anIter(mySharedContexts);
190 return anIter.Value();
196 //=======================================================================
197 //function : GetInstance
198 //purpose : Get the global instance of OpenGl_ResourceCleaner
199 //=======================================================================
201 OpenGl_ResourceCleaner* OpenGl_ResourceCleaner::GetInstance()
203 // the static instance entity
204 static OpenGl_ResourceCleaner* anInstance = NULL;
206 if (anInstance == NULL)
207 anInstance = new OpenGl_ResourceCleaner;