// @(#)root/gl:$Id$
// Author:  Timur Pocheptsov, Jun 2007

#include <utility>
#include <list>

/*************************************************************************
 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TGLContext
#define ROOT_TGLContext

class TGLContextIdentity;

#ifndef ROOT_TGLFormat
#include "TGLFormat.h"
#endif
#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif

class TGLContextPrivate;
class TGLPaintDevice;
//class TGLPBuffer;
class TGLWidget;
class TGLFontManager;

class TGLContext
{
   friend class TGLContextPrivate;
   friend class TGLWidget;
   //   friend class TGLPBuffer;

private:
   TGLPaintDevice *fDevice;
   TGLContextPrivate *fPimpl;

   Bool_t fFromCtor;//To prohibit user's calls of SetContext.
   Bool_t fValid;

   TGLContextIdentity *fIdentity;

   static Bool_t fgGlewInitDone;

public:
   TGLContext(TGLWidget *glWidget, Bool_t shareDefault=kTRUE, const TGLContext *shareList=0);
   //   TGLContext(TGLPBuffer *glPbuf, const TGLContext *shareList = 0);

   TGLContextIdentity *GetIdentity()const;

   virtual ~TGLContext();

   Bool_t           MakeCurrent();
   Bool_t           ClearCurrent();
   void             SwapBuffers();

   //This functions are public _ONLY_ for calls via
   //gROOT under win32. Please, DO NOT CALL IT DIRECTLY.
   void             SetContext(TGLWidget *widget, const TGLContext *shareList);
   // void          SetContextPB(TGLPBuffer *pbuff, const TGLContext *shareList);
   void             Release();

   Bool_t           IsValid() const { return fValid; }

   static TGLContext *GetCurrent();
   static void GlewInit();

private:
   TGLContext(const TGLContext &);
   TGLContext &operator = (const TGLContext &);

   ClassDef(TGLContext, 0); // Control internal gl-context resources.
};


//______________________________________________________________________________

class TGLContextIdentity
{

protected:
   TGLFontManager*      fFontManager;  // FreeType font manager.

public:
   TGLContextIdentity();
   virtual ~TGLContextIdentity();

   void AddRef(TGLContext* ctx);
   void Release(TGLContext* ctx);

   void AddClientRef()  { ++fClientCnt; }
   void ReleaseClient() { --fClientCnt; CheckDestroy(); }

   Int_t GetRefCnt()       const { return fCnt; }
   Int_t GetClientRefCnt() const { return fClientCnt; }

   Bool_t IsValid() const { return fCnt > 0; }

   void RegisterDLNameRangeToWipe(UInt_t base, Int_t size);
   void DeleteGLResources();

   static TGLContextIdentity *GetCurrent();

   static TGLContextIdentity *GetDefaultIdentity();
   static TGLContext         *GetDefaultContextAny();

   TGLFontManager            *GetFontManager();

private:
   Int_t fCnt;
   Int_t fClientCnt;

   void CheckDestroy();

   typedef std::pair<UInt_t, Int_t>  DLRange_t;
   typedef std::list<DLRange_t>      DLTrash_t;
   typedef DLTrash_t::const_iterator DLTrashIt_t;

   typedef std::list<TGLContext*>    CtxList_t;

   DLTrash_t fDLTrash;
   CtxList_t fCtxs;

   static TGLContextIdentity * fgDefaultIdentity;

   ClassDef(TGLContextIdentity, 0); // Identity of a shared GL context.
};

#endif
 TGLContext.h:1
 TGLContext.h:2
 TGLContext.h:3
 TGLContext.h:4
 TGLContext.h:5
 TGLContext.h:6
 TGLContext.h:7
 TGLContext.h:8
 TGLContext.h:9
 TGLContext.h:10
 TGLContext.h:11
 TGLContext.h:12
 TGLContext.h:13
 TGLContext.h:14
 TGLContext.h:15
 TGLContext.h:16
 TGLContext.h:17
 TGLContext.h:18
 TGLContext.h:19
 TGLContext.h:20
 TGLContext.h:21
 TGLContext.h:22
 TGLContext.h:23
 TGLContext.h:24
 TGLContext.h:25
 TGLContext.h:26
 TGLContext.h:27
 TGLContext.h:28
 TGLContext.h:29
 TGLContext.h:30
 TGLContext.h:31
 TGLContext.h:32
 TGLContext.h:33
 TGLContext.h:34
 TGLContext.h:35
 TGLContext.h:36
 TGLContext.h:37
 TGLContext.h:38
 TGLContext.h:39
 TGLContext.h:40
 TGLContext.h:41
 TGLContext.h:42
 TGLContext.h:43
 TGLContext.h:44
 TGLContext.h:45
 TGLContext.h:46
 TGLContext.h:47
 TGLContext.h:48
 TGLContext.h:49
 TGLContext.h:50
 TGLContext.h:51
 TGLContext.h:52
 TGLContext.h:53
 TGLContext.h:54
 TGLContext.h:55
 TGLContext.h:56
 TGLContext.h:57
 TGLContext.h:58
 TGLContext.h:59
 TGLContext.h:60
 TGLContext.h:61
 TGLContext.h:62
 TGLContext.h:63
 TGLContext.h:64
 TGLContext.h:65
 TGLContext.h:66
 TGLContext.h:67
 TGLContext.h:68
 TGLContext.h:69
 TGLContext.h:70
 TGLContext.h:71
 TGLContext.h:72
 TGLContext.h:73
 TGLContext.h:74
 TGLContext.h:75
 TGLContext.h:76
 TGLContext.h:77
 TGLContext.h:78
 TGLContext.h:79
 TGLContext.h:80
 TGLContext.h:81
 TGLContext.h:82
 TGLContext.h:83
 TGLContext.h:84
 TGLContext.h:85
 TGLContext.h:86
 TGLContext.h:87
 TGLContext.h:88
 TGLContext.h:89
 TGLContext.h:90
 TGLContext.h:91
 TGLContext.h:92
 TGLContext.h:93
 TGLContext.h:94
 TGLContext.h:95
 TGLContext.h:96
 TGLContext.h:97
 TGLContext.h:98
 TGLContext.h:99
 TGLContext.h:100
 TGLContext.h:101
 TGLContext.h:102
 TGLContext.h:103
 TGLContext.h:104
 TGLContext.h:105
 TGLContext.h:106
 TGLContext.h:107
 TGLContext.h:108
 TGLContext.h:109
 TGLContext.h:110
 TGLContext.h:111
 TGLContext.h:112
 TGLContext.h:113
 TGLContext.h:114
 TGLContext.h:115
 TGLContext.h:116
 TGLContext.h:117
 TGLContext.h:118
 TGLContext.h:119
 TGLContext.h:120
 TGLContext.h:121
 TGLContext.h:122
 TGLContext.h:123
 TGLContext.h:124
 TGLContext.h:125
 TGLContext.h:126
 TGLContext.h:127
 TGLContext.h:128
 TGLContext.h:129
 TGLContext.h:130
 TGLContext.h:131
 TGLContext.h:132
 TGLContext.h:133
 TGLContext.h:134