01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /**
19: * @author Oleg V. Khaschansky
20: * @version $Revision$
21: */package org.apache.harmony.awt.gl.opengl;
22:
23: import java.lang.ref.Reference;
24: import java.lang.ref.ReferenceQueue;
25: import java.lang.ref.WeakReference;
26: import java.util.HashMap;
27: import java.util.WeakHashMap;
28: import org.apache.harmony.awt.gl.Surface;
29: import org.apache.harmony.awt.gl.opengl.OGLBlitter.OGLTextureParams;
30:
31: // TODO - this class could be a prototype for the common resource cache.
32: // E.g. opengl contexts could be bounded to the thread that created the context and
33: // this cache will manage the destruction of the contexts.
34: public class TextureCache {
35: private static final HashMap<WeakReference<Surface>, OGLTextureParams> ref2texture = new HashMap<WeakReference<Surface>, OGLTextureParams>();
36:
37: private static final ThreadLocal<TextureCache> localInstance = new ThreadLocal<TextureCache>() {
38: @Override
39: public TextureCache initialValue() {
40: return new TextureCache();
41: }
42: };
43:
44: static TextureCache getInstance() {
45: return localInstance.get();
46: }
47:
48: private final ReferenceQueue<Surface> rq = new ReferenceQueue<Surface>();
49:
50: private final WeakHashMap<Surface, WeakReference<Surface>> surface2ref = new WeakHashMap<Surface, WeakReference<Surface>>();
51:
52: void add(Surface key, OGLBlitter.OGLTextureParams texture) {
53: WeakReference<Surface> ref = new WeakReference<Surface>(key, rq);
54: surface2ref.put(key, ref);
55: ref2texture.put(ref, texture);
56: }
57:
58: void cleanupTextures() {
59: Reference<? extends Surface> ref;
60: while ((ref = rq.poll()) != null) {
61: OGLBlitter.OGLTextureParams tp = ref2texture.remove(ref);
62: tp.deleteTexture();
63: }
64: }
65:
66: OGLBlitter.OGLTextureParams findTexture(Surface key) {
67: OGLBlitter.OGLTextureParams tp = ref2texture.get(surface2ref
68: .get(key));
69: return tp;
70: }
71:
72: void remove(Surface key) {
73: WeakReference<Surface> ref = surface2ref.remove(key);
74: if (ref != null) {
75: ref.clear();
76: OGLBlitter.OGLTextureParams tp = ref2texture.remove(ref);
77: tp.deleteTexture();
78: }
79: }
80: }
|