1 /*
   2  * Copyright (c) 2011, 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 class CStrikeDisposer extends FontStrikeDisposer {
  50 
  51     long pNativeScalerContext;
  52 
  53     public CStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
  54                                long pContext, int[] images)
  55     {
  56         super(font2D, desc, 0L, images);
  57         pNativeScalerContext = pContext;
  58     }
  59 
  60     public CStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
  61                                long pContext, long[] images)
  62     {
  63         super(font2D, desc, 0L, images);
  64         pNativeScalerContext = pContext;
  65     }
  66 
  67     public CStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
  68                                long pContext)
  69     {
  70         super(font2D, desc, 0L);
  71         pNativeScalerContext = pContext;
  72     }
  73 
  74     public CStrikeDisposer(Font2D font2D, FontStrikeDesc desc) {
  75         super(font2D, desc);
  76     }
  77 
  78     public synchronized void dispose() {
  79         if (!disposed) {
  80             if (pNativeScalerContext != 0L) {
  81                 freeNativeScalerContext(pNativeScalerContext);
  82             }
  83             super.dispose();
  84         }
  85     }
  86 
  87     private native void freeNativeScalerContext(long pContext);
  88 }