1 /*
   2  * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.loops;
  27 
  28 public final class RenderCache {
  29     final class Entry {
  30         private SurfaceType src;
  31         private CompositeType comp;
  32         private SurfaceType dst;
  33         private Object value;
  34 
  35         public Entry(SurfaceType src,
  36                      CompositeType comp,
  37                      SurfaceType dst,
  38                      Object value)
  39         {
  40             this.src = src;
  41             this.comp = comp;
  42             this.dst = dst;
  43             this.value = value;
  44         }
  45 
  46         public boolean matches(SurfaceType src,
  47                                CompositeType comp,
  48                                SurfaceType dst)
  49         {
  50             // bug 4725045: using equals() causes different SurfaceType
  51             // objects with the same strings to match in the cache, which is
  52             // not the behavior we want.  Constrain the match to succeed only
  53             // on object matches instead.
  54             return ((this.src == src) &&
  55                     (this.comp == comp) &&
  56                     (this.dst == dst));
  57         }
  58 
  59         public Object getValue() {
  60             return value;
  61         }
  62     }
  63 
  64     private Entry entries[];
  65 
  66     public RenderCache(int size) {
  67         entries = new Entry[size];
  68     }
  69 
  70     public synchronized Object get(SurfaceType src,
  71                       CompositeType comp,
  72                       SurfaceType dst)
  73     {
  74         int max = entries.length - 1;
  75         for (int i = max; i >= 0; i--) {
  76             Entry e = entries[i];
  77             if (e == null) {
  78                 break;
  79             }
  80             if (e.matches(src, comp, dst)) {
  81                 if (i < max - 4) {
  82                     System.arraycopy(entries, i+1, entries, i, max - i);
  83                     entries[max] = e;
  84                 }
  85                 return e.getValue();
  86             }
  87         }
  88 
  89         return null;
  90     }
  91 
  92     public synchronized void put(SurfaceType src,
  93                     CompositeType comp,
  94                     SurfaceType dst,
  95                     Object value)
  96     {
  97         Entry e = new Entry(src, comp, dst, value);
  98 
  99         int num = entries.length;
 100         System.arraycopy(entries, 1, entries, 0, num - 1);
 101         entries[num - 1] = e;
 102     }
 103 }