Path: blob/master/src/java.desktop/share/classes/sun/java2d/loops/RenderCache.java
41159 views
/*1* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.loops;2627public final class RenderCache {28final class Entry {29private SurfaceType src;30private CompositeType comp;31private SurfaceType dst;32private Object value;3334public Entry(SurfaceType src,35CompositeType comp,36SurfaceType dst,37Object value)38{39this.src = src;40this.comp = comp;41this.dst = dst;42this.value = value;43}4445public boolean matches(SurfaceType src,46CompositeType comp,47SurfaceType dst)48{49// bug 4725045: using equals() causes different SurfaceType50// objects with the same strings to match in the cache, which is51// not the behavior we want. Constrain the match to succeed only52// on object matches instead.53return ((this.src == src) &&54(this.comp == comp) &&55(this.dst == dst));56}5758public Object getValue() {59return value;60}61}6263private Entry[] entries;6465public RenderCache(int size) {66entries = new Entry[size];67}6869public synchronized Object get(SurfaceType src,70CompositeType comp,71SurfaceType dst)72{73int max = entries.length - 1;74for (int i = max; i >= 0; i--) {75Entry e = entries[i];76if (e == null) {77break;78}79if (e.matches(src, comp, dst)) {80if (i < max - 4) {81System.arraycopy(entries, i+1, entries, i, max - i);82entries[max] = e;83}84return e.getValue();85}86}8788return null;89}9091public synchronized void put(SurfaceType src,92CompositeType comp,93SurfaceType dst,94Object value)95{96Entry e = new Entry(src, comp, dst, value);9798int num = entries.length;99System.arraycopy(entries, 1, entries, 0, num - 1);100entries[num - 1] = e;101}102}103104105