1 // Created on: 2011-03-18
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #ifndef _OPENGL_RESOURCECLEANER_H
22 #define _OPENGL_RESOURCECLEANER_H
24 #include <OpenGl_GlCore11.hxx>
25 #include <NCollection_Queue.hxx>
26 #include <NCollection_List.hxx>
27 #include <NCollection_Map.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <OpenGl_Resource.hxx>
31 class OpenGl_Resource;
32 class Handle(OpenGl_Resource);
33 class Handle(OpenGl_Context);
35 typedef NCollection_Queue<Handle_OpenGl_Resource> QueueOfResources;
36 typedef NCollection_DataMap<GLCONTEXT, QueueOfResources> DataMapOfContextsResources;
37 typedef NCollection_Map<GLCONTEXT> MapOfContexts;
39 //! OpenGl_ResourceCleaner should be used to clean OpenGl memory resources;
40 //! The reason is that the resources might be shared between the contexts and
41 //! should be cleaned up only while suitable context is active;
42 class OpenGl_ResourceCleaner
48 OpenGl_ResourceCleaner();
51 virtual ~OpenGl_ResourceCleaner() { }
53 //! Append OpenGl context to the OpenGl_ResourceCleaner
55 void AppendContext(GLCONTEXT theContext, Standard_Boolean isShared);
57 //! Tell the OpenGl_ResourceCleaner to clean up the OpenGl memory resource
58 //! which has been created by the specified OpenGl context;
59 //! The context should be in the OpenGl_ResourceCleaner control list.
60 Standard_Boolean AddResource(GLCONTEXT theContext, Handle_OpenGl_Resource theResource);
62 //! Cancel clean procedure for all the resources added to the OpenGl_ResourceCleaner.
65 //! Cancel clean procedure for all the resources of the specific OpenGl context
66 //! which were added to the OpenGl_ResourceCleaner.
67 Standard_Boolean Clear(GLCONTEXT theContext);
69 //! Cancel clean procedure for all of the shared resources.
72 //! Clear the unused resources for active OpenGl context;
73 //! You should add the cleaner resources by AddResources method;
74 //! It is suggested to call this method right before the OpenGl
75 //! new frame drawing procedure starts.
76 void Cleanup (const Handle(OpenGl_Context)& theGlContext);
78 //! Remove the OpenGl context from the OpenGl_ResourceCleaner control list.
79 void RemoveContext(GLCONTEXT theContext);
81 //! Get any of shared contexts from the OpenGl_ResourceCleaner list
82 //! to share resources with a new one
83 GLCONTEXT GetSharedContext() const;
85 //! Get the global instance of OpenGl_ResourceCleaner
86 static OpenGl_ResourceCleaner* GetInstance();
90 DataMapOfContextsResources myInstanceQueue; // map for queues of non-shared context's resources
91 QueueOfResources mySharedQueue; // queue of shared context's resources
92 MapOfContexts mySharedContexts; // the control list of shared contexts