Back to index

lightning-sunbird  0.9+nobinonly
nsGCCache.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Stuart Parmenter <pavlov@netscape.com>
00025  *   Mike Shaver <shaver@zeroknowledge.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 
00042 #ifndef nsGCCacheXlib_h___
00043 #define nsGCCacheXlib_h___
00044 
00045 #include <X11/Xlib.h>
00046 #include <X11/Xutil.h>
00047 
00048 #include "nscore.h"
00049 #include "nsDebug.h"
00050 #include "prclist.h"
00051 #include <stdlib.h>
00052 #include <string.h>
00053 
00054 #define countof(x) ((int)(sizeof(x) / sizeof (*x)))
00055 #define GC_CACHE_SIZE 64
00056 
00057 #ifdef DEBUG
00058 #define DEBUG_METER(x) x
00059 #else
00060 #define DEBUG_METER(x)
00061 #endif
00062 
00063 class nsGCCacheXlib;
00064 
00065 class xGC {
00066   friend class nsGCCacheXlib;
00067 
00068 public:
00069   xGC(Display *display, Drawable d, unsigned long valuemask, XGCValues *values)
00070   {
00071     mRefCnt = 0;
00072     mDisplay = display;
00073     mGC = ::XCreateGC(display, d, valuemask, values);
00074   }
00075 
00076   virtual ~xGC() {
00077     ::XFreeGC(mDisplay, mGC);
00078   }
00079 
00080   PRInt32 AddRef(void) {
00081 #ifdef DEBUG  
00082     if(mRefCnt>400) abort();
00083 #endif /* DEBUG */
00084     NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
00085     ++mRefCnt;
00086     return mRefCnt;
00087   }
00088   PRInt32 Release(void) {
00089     NS_PRECONDITION(0 != mRefCnt, "dup release");
00090     --mRefCnt;
00091     if (mRefCnt == 0) {
00092       mRefCnt = 1; /* stabilize */
00093       delete this;
00094       return 0;
00095     }
00096     return mRefCnt;
00097   }
00098 
00099   //operator GC() { return mGC; }
00100   operator const GC() { return (const GC)mGC; }
00101 
00102 private:
00103   PRInt32 mRefCnt;
00104   Display *mDisplay;
00105   GC mGC;
00106 };
00107 
00108 
00109 struct GCCacheEntryXlib
00110 {
00111   PRCList clist;
00112   unsigned long flags;
00113   XGCValues gcv;
00114   Region clipRegion;
00115   xGC *gc;
00116 };
00117 
00118 class nsGCCacheXlib
00119 {
00120  public:
00121   nsGCCacheXlib();
00122   virtual ~nsGCCacheXlib();
00123 
00124   void Flush(unsigned long flags);
00125 
00126   xGC *GetGC(Display *display, Window window, unsigned long flags, XGCValues *gcv, Region clipRegion);
00127   
00128 private:
00129   void ReuseGC(GCCacheEntryXlib *entry, unsigned long flags, XGCValues *gcv);
00130   PRCList GCCache;
00131   PRCList GCFreeList;
00132   void free_cache_entry(PRCList *clist);
00133   void move_cache_entry(PRCList *clist);
00134   static void XCopyRegion(Region src, Region dr_return);
00135   static Region copyRegion;
00136   void ReportStats();
00137 
00138   DEBUG_METER(
00139               struct {
00140                 int hits[GC_CACHE_SIZE];
00141                 int misses;
00142                 int reclaim;
00143               } GCCacheStats;
00144               )
00145 
00146 };
00147 
00148 #endif