1 /*
2  * Copyright (c) 2003, 2005, 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.font;
27 
28 /*
29  * This keeps track of data that needs to be cleaned up once a
30  * strike is freed.
31  * a) The native memory that is the glyph image cache.
32  * b) removing the "desc" key from the strike's map.
33  * This is safe to do because this disposer is invoked only when the
34  * reference object has been cleared, which means the value indexed by
35  * this key is just an empty reference object.
36  * It is possible that a new FontStrike has been created that would
37  * be referenced by the same (equals) key. If it is placed in the map
38  * before this disposer is executed, then we do not want to remove that
39  * object. We should only remove an object where the value is null.
40  * So we first verify that the key still points to a cleared reference.
41  * Updates to the map thus need to be synchronized.
42  *
43  * A WeakHashmap will automatically clean up, but we might maintain a
44  * reference to the "desc" key in the FontStrike (value) which would
45  * prevent the keys from being discarded. And since the strike is the only
46  * place is likely we would maintain such a strong reference, then the map
47  * entries would be removed much more promptly than we need.
48  */
49 
50 class NativeStrikeDisposer extends FontStrikeDisposer {
51 
52     long pNativeScalerContext;
53 
NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc, long pContext, int[] images)54     public NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
55                               long pContext, int[] images) {
56         super(font2D, desc, 0L, images);
57         pNativeScalerContext = pContext;
58     }
59 
NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc, long pContext, long[] images)60     public NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
61                               long pContext, long[] images) {
62         super(font2D, desc, 0L, images);
63         pNativeScalerContext = pContext;
64     }
65 
NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc, long pContext)66     public NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
67                               long pContext) {
68         super(font2D, desc, 0L);
69         pNativeScalerContext = pContext;
70     }
71 
NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc)72     public NativeStrikeDisposer(Font2D font2D, FontStrikeDesc desc) {
73         super(font2D, desc);
74     }
75 
dispose()76     public synchronized void dispose() {
77         if (!disposed) {
78             if (pNativeScalerContext != 0L) {
79                 freeNativeScalerContext(pNativeScalerContext);
80             }
81             super.dispose();
82         }
83     }
84 
freeNativeScalerContext(long pContext)85     private native void freeNativeScalerContext(long pContext);
86 }
87