< prev index next >

src/java.desktop/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java

Print this page

        

@@ -39,10 +39,11 @@
 import java.awt.image.ColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.DirectColorModel;
 import java.awt.image.VolatileImage;
 import java.awt.image.WritableRaster;
+import java.util.HashMap;
 
 import sun.awt.CGraphicsConfig;
 import sun.awt.CGraphicsDevice;
 import sun.awt.image.OffScreenImage;
 import sun.awt.image.SunVolatileImage;

@@ -82,10 +83,12 @@
     private static native boolean initCGL();
     private static native long getCGLConfigInfo(int displayID, int visualnum,
                                                 int swapInterval);
     private static native int getOGLCapabilities(long configInfo);
 
+    private static final HashMap<Long, Integer> pGCRefCounts = new HashMap<>();
+
     /**
      * Returns GL_MAX_TEXTURE_SIZE from the shared opengl context. Must be
      * called under OGLRQ lock, because this method change current context.
      *
      * @return GL_MAX_TEXTURE_SIZE

@@ -104,11 +107,11 @@
         this.pixfmt = pixfmt;
         this.pConfigInfo = configInfo;
         this.oglCaps = oglCaps;
         this.maxTextureSize = maxTextureSize;
         context = new OGLContext(OGLRenderQueue.getInstance(), this);
-
+        refPConfigInfo(pConfigInfo);
         // add a record to the Disposer so that we destroy the native
         // CGLGraphicsConfigInfo data when this object goes away
         Disposer.addRecord(disposerReferent,
                            new CGLGCDisposerRecord(pConfigInfo));
     }

@@ -167,10 +170,35 @@
         int oglCaps = getOGLCapabilities(cfginfo);
         ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);
         return new CGLGraphicsConfig(device, pixfmt, cfginfo, textureSize, caps);
     }
 
+    static void refPConfigInfo(long pConfigInfo) {
+        synchronized (pGCRefCounts) {
+            Integer count = pGCRefCounts.get(pConfigInfo);
+            if (count == null) count = 0;
+            count++;
+            pGCRefCounts.put(pConfigInfo, count);
+        }
+    }
+
+    static void deRefPConfigInfo(long pConfigInfo) {
+        synchronized (pGCRefCounts) {
+            Integer count = pGCRefCounts.get(pConfigInfo);
+            if (count != null) {
+                count--;
+                if (count == 0) {
+                    OGLRenderQueue.disposeGraphicsConfig(pConfigInfo);
+                    pGCRefCounts.remove(pConfigInfo);
+                }
+                else {
+                    pGCRefCounts.put(pConfigInfo, count);
+                }
+            }
+        }
+    }
+
     public static boolean isCGLAvailable() {
         return cglAvailable;
     }
 
     /**

@@ -234,11 +262,11 @@
         public CGLGCDisposerRecord(long pCfgInfo) {
             this.pCfgInfo = pCfgInfo;
         }
         public void dispose() {
             if (pCfgInfo != 0) {
-                OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);
+                deRefPConfigInfo(pCfgInfo);
                 pCfgInfo = 0;
             }
         }
     }
 
< prev index next >