changeset 7966:70ae8cc1e691

Fix RT-38183: ManagedResource warnings when screen configurations change Reviewed by: kcr, felipe
author flar <James.Graham@oracle.com>
date Wed, 03 Sep 2014 15:05:40 -0700
parents c1d51a6e4338
children 613a8ae3a0f4
files modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGCanvas.java modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGNode.java modules/graphics/src/main/java/com/sun/scenario/effect/impl/ImagePool.java modules/graphics/src/main/java/com/sun/scenario/effect/impl/PoolFilterable.java modules/graphics/src/main/java/com/sun/scenario/effect/impl/Renderer.java modules/graphics/src/main/java/com/sun/scenario/effect/impl/prism/PrDrawable.java
diffstat 6 files changed, 94 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGCanvas.java	Wed Sep 03 19:18:28 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGCanvas.java	Wed Sep 03 15:05:40 2014 -0700
@@ -1648,7 +1648,9 @@
         {
             Filterable f = PrDrawable.create(fctx, tex);
             Rectangle r = new Rectangle(tex.getContentWidth(), tex.getContentHeight());
+            f.lock();
             ImageData id = new ImageData(fctx, f, r);
+            id.setReusable(true);
             if (pixelscale != 1.0f || !transform.isIdentity()) {
                 Affine2D a2d = new Affine2D();
                 a2d.scale(1.0f / pixelscale, 1.0f / pixelscale);
--- a/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGNode.java	Wed Sep 03 19:18:28 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGNode.java	Wed Sep 03 15:05:40 2014 -0700
@@ -2432,7 +2432,10 @@
                                 Object renderHelper,
                                 Effect defaultInput)
         {
-            return new ImageData(fctx, img, new Rectangle(bounds));
+            img.lock();
+            ImageData id = new ImageData(fctx, img, new Rectangle(bounds));
+            id.setReusable(true);
+            return id;
         }
 
         @Override
--- a/modules/graphics/src/main/java/com/sun/scenario/effect/impl/ImagePool.java	Wed Sep 03 19:18:28 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/scenario/effect/impl/ImagePool.java	Wed Sep 03 15:05:40 2014 -0700
@@ -76,10 +76,10 @@
 
     static final int QUANT = 32;
 
-    private final List<SoftReference<Filterable>> unlocked =
-        new ArrayList<SoftReference<Filterable>>();
-    private final List<SoftReference<Filterable>> locked =
-        new ArrayList<SoftReference<Filterable>>();
+    private final List<SoftReference<PoolFilterable>> unlocked =
+        new ArrayList<SoftReference<PoolFilterable>>();
+    private final List<SoftReference<PoolFilterable>> locked =
+        new ArrayList<SoftReference<PoolFilterable>>();
 
     // On Canmore with the PowerVR SGX chip, there is a driver issue
     // that causes incorrect rendering if one tries to reuse an FBO
@@ -97,8 +97,8 @@
     // (where there would normally be reuse).
     private final boolean usePurgatory = Boolean.getBoolean("decora.purgatory");
     private final List<Filterable> hardPurgatory = new ArrayList<Filterable>();
-    private final List<SoftReference<Filterable>> softPurgatory =
-        new ArrayList<SoftReference<Filterable>>();
+    private final List<SoftReference<PoolFilterable>> softPurgatory =
+        new ArrayList<SoftReference<PoolFilterable>>();
 
     /**
      * Package-private constructor.
@@ -106,7 +106,7 @@
     ImagePool() {
     }
 
-    public synchronized Filterable checkOut(Renderer renderer, int w, int h) {
+    public synchronized PoolFilterable checkOut(Renderer renderer, int w, int h) {
         if (w <= 0 || h <= 0) {
             // if image is empty in any way, return a small non-empty image.
             w = h = 1;
@@ -123,13 +123,13 @@
         pixelsAccessed += ((long) w) * h;
         // first look for an already cached image of sufficient size,
         // choosing the one that is closest in size to the requested dimensions
-        SoftReference<Filterable> chosenEntry = null;
-        Filterable chosenImage = null;
+        SoftReference<PoolFilterable> chosenEntry = null;
+        PoolFilterable chosenImage = null;
         int mindiff = Integer.MAX_VALUE;
-        Iterator<SoftReference<Filterable>> entries = unlocked.iterator();
+        Iterator<SoftReference<PoolFilterable>> entries = unlocked.iterator();
         while (entries.hasNext()) {
-            SoftReference<Filterable> entry = entries.next();
-            Filterable eimg = entry.get();
+            SoftReference<PoolFilterable> entry = entries.next();
+            PoolFilterable eimg = entry.get();
             if (eimg == null) {
                 entries.remove();
                 continue;
@@ -170,7 +170,7 @@
         // get rid of expired entries from locked list
         entries = locked.iterator();
         while (entries.hasNext()) {
-            SoftReference<Filterable> entry = entries.next();
+            SoftReference<PoolFilterable> entry = entries.next();
             Filterable eimg = entry.get();
             if (eimg == null) {
                 entries.remove();
@@ -178,7 +178,7 @@
         }
 
         // if all else fails, just create a new one...
-        Filterable img = null;
+        PoolFilterable img = null;
         try {
             img = renderer.createCompatibleImage(w, h);
         } catch (OutOfMemoryError e) {}
@@ -191,19 +191,20 @@
             } catch (OutOfMemoryError e) {}
         }
         if (img != null) {
-            locked.add(new SoftReference<Filterable>(img));
+            img.setImagePool(this);
+            locked.add(new SoftReference<PoolFilterable>(img));
             numCreated++;
             pixelsCreated += ((long) w) * h;
         }
         return img;
     }
 
-    public synchronized void checkIn(Filterable img) {
-        SoftReference<Filterable> chosenEntry = null;
+    public synchronized void checkIn(PoolFilterable img) {
+        SoftReference<PoolFilterable> chosenEntry = null;
         Filterable chosenImage = null;
-        Iterator<SoftReference<Filterable>> entries = locked.iterator();
+        Iterator<SoftReference<PoolFilterable>> entries = locked.iterator();
         while (entries.hasNext()) {
-            SoftReference<Filterable> entry = entries.next();
+            SoftReference<PoolFilterable> entry = entries.next();
             Filterable eimg = entry.get();
             if (eimg == null) {
                 entries.remove();
@@ -244,7 +245,7 @@
 
     private void pruneCache() {
         // flush all unlocked images
-        for (SoftReference<Filterable> r : unlocked) {
+        for (SoftReference<PoolFilterable> r : unlocked) {
             Filterable image = r.get();
             if (image != null) {
                 image.flush();
@@ -260,7 +261,7 @@
     }
 
     public synchronized void dispose() {
-        for (SoftReference<Filterable> r : unlocked) {
+        for (SoftReference<PoolFilterable> r : unlocked) {
             Filterable image = r.get();
             if (image != null) {
                 image.flush();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/graphics/src/main/java/com/sun/scenario/effect/impl/PoolFilterable.java	Wed Sep 03 15:05:40 2014 -0700
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.scenario.effect.impl;
+
+import com.sun.scenario.effect.Filterable;
+
+public interface PoolFilterable extends Filterable {
+    public void setImagePool(ImagePool pool);
+    public ImagePool getImagePool();
+}
--- a/modules/graphics/src/main/java/com/sun/scenario/effect/impl/Renderer.java	Wed Sep 03 19:18:28 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/scenario/effect/impl/Renderer.java	Wed Sep 03 15:05:40 2014 -0700
@@ -110,14 +110,23 @@
 
     public abstract int getCompatibleWidth(int w);
     public abstract int getCompatibleHeight(int h);
-    public abstract Filterable createCompatibleImage(int w, int h);
+    public abstract PoolFilterable createCompatibleImage(int w, int h);
 
-    public Filterable getCompatibleImage(int w, int h) {
+    public PoolFilterable getCompatibleImage(int w, int h) {
         return imagePool.checkOut(this, w, h);
     }
 
     public void releaseCompatibleImage(Filterable image) {
-        imagePool.checkIn(image);
+        if (image instanceof PoolFilterable) {
+            ImagePool pool = ((PoolFilterable) image).getImagePool();
+            if (pool != null) {
+                pool.checkIn((PoolFilterable) image);
+                return;
+            }
+//        } else {
+            // Error?
+        }
+        image.unlock();
     }
 
     /**
--- a/modules/graphics/src/main/java/com/sun/scenario/effect/impl/prism/PrDrawable.java	Wed Sep 03 19:18:28 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/scenario/effect/impl/prism/PrDrawable.java	Wed Sep 03 15:05:40 2014 -0700
@@ -28,11 +28,14 @@
 import com.sun.prism.Graphics;
 import com.sun.prism.RTTexture;
 import com.sun.scenario.effect.FilterContext;
-import com.sun.scenario.effect.Filterable;
+import com.sun.scenario.effect.impl.ImagePool;
+import com.sun.scenario.effect.impl.PoolFilterable;
 import com.sun.scenario.effect.impl.Renderer;
+import java.lang.ref.WeakReference;
 
-public abstract class PrDrawable extends PrTexture<RTTexture> implements Filterable {
-    
+public abstract class PrDrawable extends PrTexture<RTTexture> implements PoolFilterable {
+    private WeakReference<ImagePool> pool;
+
     public static PrDrawable create(FilterContext fctx, RTTexture rtt) {
         return ((PrRenderer) Renderer.getRenderer(fctx)).createDrawable(rtt);
     }
@@ -41,24 +44,34 @@
         super(rtt);
     }
 
-    public float getPixelScale() {
+    @Override
+    public void setImagePool(ImagePool pool) {
+        this.pool = new WeakReference<>(pool);
+    }
+
+    @Override
+    public ImagePool getImagePool() {
+        return pool == null ? null : pool.get();
+    }
+
+    @Override public float getPixelScale() {
         return 1.0f;
     }
 
     @Override public int getMaxContentWidth() {
-        return ((RTTexture)getTextureObject()).getMaxContentWidth();
+        return getTextureObject().getMaxContentWidth();
     }
     
     @Override public int getMaxContentHeight() {
-        return ((RTTexture)getTextureObject()).getMaxContentHeight();
+        return getTextureObject().getMaxContentHeight();
     }
     
     @Override public void setContentWidth(int contentW) {
-        ((RTTexture)getTextureObject()).setContentWidth(contentW);
+        getTextureObject().setContentWidth(contentW);
     }
 
     @Override public void setContentHeight(int contentH) {
-        ((RTTexture)getTextureObject()).setContentHeight(contentH);
+        getTextureObject().setContentHeight(contentH);
     }
  
     public abstract Graphics createGraphics();