01: /*
02: * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any 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, CompositeType comp,
36: SurfaceType dst, Object value) {
37: this .src = src;
38: this .comp = comp;
39: this .dst = dst;
40: this .value = value;
41: }
42:
43: public boolean matches(SurfaceType src, CompositeType comp,
44: SurfaceType dst) {
45: // bug 4725045: using equals() causes different SurfaceType
46: // objects with the same strings to match in the cache, which is
47: // not the behavior we want. Constrain the match to succeed only
48: // on object matches instead.
49: return ((this .src == src) && (this .comp == comp) && (this .dst == dst));
50: }
51:
52: public Object getValue() {
53: return value;
54: }
55: }
56:
57: private Entry entries[];
58:
59: public RenderCache(int size) {
60: entries = new Entry[size];
61: }
62:
63: public synchronized Object get(SurfaceType src, CompositeType comp,
64: SurfaceType dst) {
65: int max = entries.length - 1;
66: for (int i = max; i >= 0; i--) {
67: Entry e = entries[i];
68: if (e == null) {
69: break;
70: }
71: if (e.matches(src, comp, dst)) {
72: if (i < max - 4) {
73: System.arraycopy(entries, i + 1, entries, i, max
74: - i);
75: entries[max] = e;
76: }
77: return e.getValue();
78: }
79: }
80:
81: return null;
82: }
83:
84: public synchronized void put(SurfaceType src, CompositeType comp,
85: SurfaceType dst, Object value) {
86: Entry e = new Entry(src, comp, dst, value);
87:
88: int num = entries.length;
89: System.arraycopy(entries, 1, entries, 0, num - 1);
90: entries[num - 1] = e;
91: }
92: }
|