OCC22199 OpenGL memory leaks in TKOpenGl
[occt.git] / src / OpenGl / OpenGl_ResourceCleaner.hxx
1 // File:      OpenGl_ResourceCleaner.hxx
2 // Created:   18.03.11 9:30:00
3 // Author:    Anton POLETAEV
4
5 #ifndef _OPENGL_RESOURCECLEANER_H
6 #define _OPENGL_RESOURCECLEANER_H
7
8 #include <OpenGl_tgl_all.hxx>
9 #include <NCollection_Queue.hxx>
10 #include <NCollection_List.hxx>
11 #include <NCollection_Map.hxx>
12 #include <NCollection_DataMap.hxx>
13 #include <OpenGl_Resource.hxx>
14
15 class OpenGl_Resource;
16 class Handle_OpenGl_Resource;
17
18 typedef NCollection_Queue<Handle_OpenGl_Resource> QueueOfResources;
19 typedef NCollection_DataMap<GLCONTEXT, QueueOfResources> DataMapOfContextsResources;
20 typedef NCollection_Map<GLCONTEXT> MapOfContexts;
21
22 //! OpenGl_ResourceCleaner should be used to clean OpenGl memory resources;
23 //! The reason is that the resources might be shared between the contexts and
24 //! should be cleaned up only while suitable context is active;
25 class OpenGl_ResourceCleaner
26 {
27
28 public:
29         
30   //! Constructor
31   OpenGl_ResourceCleaner();
32
33   //! Destructor
34   virtual ~OpenGl_ResourceCleaner() { }
35
36   //! Append OpenGl context to the OpenGl_ResourceCleaner
37   //! control list.
38   void AppendContext(GLCONTEXT theContext, Standard_Boolean isShared);  
39   
40   //! Tell the OpenGl_ResourceCleaner to clean up the OpenGl memory resource
41   //! which has been created by the specified OpenGl context;
42   //! The context should be in the OpenGl_ResourceCleaner control list.
43   Standard_Boolean AddResource(GLCONTEXT theContext, Handle_OpenGl_Resource theResource);
44
45   //! Cancel clean procedure for all the resources added to the OpenGl_ResourceCleaner.
46   void Clear();
47
48   //! Cancel clean procedure for all the resources of the specific OpenGl context 
49   //! which were added to the OpenGl_ResourceCleaner.
50   Standard_Boolean Clear(GLCONTEXT theContext);
51
52   //! Cancel clean procedure for all of the shared resources.
53   void ClearShared();
54
55   //! Clear the unused resources for active OpenGl context;
56   //! You should add the cleaner resources by AddResources method;
57   //! It is suggested to call this method right before the OpenGl
58   //! new frame drawing procedure starts.
59   void Cleanup();
60
61   //! Remove the OpenGl context from the OpenGl_ResourceCleaner control list.
62   void RemoveContext(GLCONTEXT theContext);
63
64   //! Get any of shared contexts from the OpenGl_ResourceCleaner list 
65   //! to share resources with a new one
66   GLCONTEXT GetSharedContext() const;
67
68   //! Get the global instance of OpenGl_ResourceCleaner
69   static OpenGl_ResourceCleaner* GetInstance();
70
71 private:
72
73   DataMapOfContextsResources myInstanceQueue;  // map for queues of non-shared context's resources
74   QueueOfResources mySharedQueue;              // queue of shared context's resources
75   MapOfContexts mySharedContexts;              // the control list of shared contexts
76
77 };
78
79 #endif