XRGlyphCache.java revision 2685
2370N/A/*
2685N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2370N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2370N/A *
2370N/A * This code is free software; you can redistribute it and/or modify it
2370N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Oracle designates this
2370N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Oracle in the LICENSE file that accompanied this code.
2370N/A *
2370N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2370N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2370N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2370N/A * version 2 for more details (a copy is included in the LICENSE file that
2370N/A * accompanied this code).
2370N/A *
2370N/A * You should have received a copy of the GNU General Public License version
2370N/A * 2 along with this work; if not, write to the Free Software Foundation,
2370N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2370N/A *
2685N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2685N/A * or visit www.oracle.com if you need additional information or have any
2685N/A * questions.
2370N/A */
2370N/A
2370N/Apackage sun.font;
2370N/A
2370N/Aimport java.io.*;
2370N/Aimport java.util.*;
2370N/A
2370N/Aimport sun.awt.*;
2370N/Aimport sun.java2d.xr.*;
2370N/A
2370N/A/**
2370N/A * Glyph cache used by the XRender pipeline.
2370N/A *
2370N/A * @author Clemens Eisserer
2370N/A */
2370N/A
2370N/Apublic class XRGlyphCache implements GlyphDisposedListener {
2370N/A XRBackend con;
2370N/A XRCompositeManager maskBuffer;
2370N/A HashMap<MutableInteger, XRGlyphCacheEntry> cacheMap = new HashMap<MutableInteger, XRGlyphCacheEntry>(256);
2370N/A
2370N/A int nextID = 1;
2370N/A MutableInteger tmp = new MutableInteger(0);
2370N/A
2370N/A int grayGlyphSet;
2370N/A int lcdGlyphSet;
2370N/A
2370N/A int time = 0;
2370N/A int cachedPixels = 0;
2370N/A static final int MAX_CACHED_PIXELS = 100000;
2370N/A
2370N/A ArrayList<Integer> freeGlyphIDs = new ArrayList<Integer>(255);
2370N/A
2370N/A static final boolean batchGlyphUpload = true; // Boolean.parseBoolean(System.getProperty("sun.java2d.xrender.batchGlyphUpload"));
2370N/A
2370N/A public XRGlyphCache(XRCompositeManager maskBuf) {
2370N/A this.con = maskBuf.getBackend();
2370N/A this.maskBuffer = maskBuf;
2370N/A
2370N/A grayGlyphSet = con.XRenderCreateGlyphSet(XRUtils.PictStandardA8);
2370N/A lcdGlyphSet = con.XRenderCreateGlyphSet(XRUtils.PictStandardARGB32);
2370N/A
2370N/A StrikeCache.addGlyphDisposedListener(this);
2370N/A }
2370N/A
2370N/A public void glyphDisposed(ArrayList<Long> glyphPtrList) {
2370N/A try {
2370N/A SunToolkit.awtLock();
2370N/A
2370N/A ArrayList<Integer> glyphIDList = new ArrayList<Integer>(glyphPtrList.size());
2370N/A for (long glyphPtr : glyphPtrList) {
2370N/A glyphIDList.add(XRGlyphCacheEntry.getGlyphID(glyphPtr));
2370N/A }
2370N/A freeGlyphs(glyphIDList);
2370N/A } finally {
2370N/A SunToolkit.awtUnlock();
2370N/A }
2370N/A }
2370N/A
2370N/A protected int getFreeGlyphID() {
2370N/A if (freeGlyphIDs.size() > 0) {
2370N/A int newID = freeGlyphIDs.remove(freeGlyphIDs.size() - 1);
2370N/A ;
2370N/A return newID;
2370N/A }
2370N/A return nextID++;
2370N/A }
2370N/A
2370N/A protected XRGlyphCacheEntry getEntryForPointer(long imgPtr) {
2370N/A int id = XRGlyphCacheEntry.getGlyphID(imgPtr);
2370N/A
2370N/A if (id == 0) {
2370N/A return null;
2370N/A }
2370N/A
2370N/A tmp.setValue(id);
2370N/A return cacheMap.get(tmp);
2370N/A }
2370N/A
2370N/A public XRGlyphCacheEntry[] cacheGlyphs(GlyphList glyphList) {
2370N/A time++;
2370N/A
2370N/A XRGlyphCacheEntry[] entries = new XRGlyphCacheEntry[glyphList.getNumGlyphs()];
2370N/A long[] imgPtrs = glyphList.getImages();
2370N/A ArrayList<XRGlyphCacheEntry> uncachedGlyphs = null;
2370N/A
2370N/A for (int i = 0; i < glyphList.getNumGlyphs(); i++) {
2370N/A XRGlyphCacheEntry glyph;
2370N/A
2370N/A // Find uncached glyphs and queue them for upload
2370N/A if ((glyph = getEntryForPointer(imgPtrs[i])) == null) {
2370N/A glyph = new XRGlyphCacheEntry(imgPtrs[i], glyphList);
2370N/A glyph.setGlyphID(getFreeGlyphID());
2370N/A cacheMap.put(new MutableInteger(glyph.getGlyphID()), glyph);
2370N/A
2370N/A if (uncachedGlyphs == null) {
2370N/A uncachedGlyphs = new ArrayList<XRGlyphCacheEntry>();
2370N/A }
2370N/A uncachedGlyphs.add(glyph);
2370N/A }
2370N/A glyph.setLastUsed(time);
2370N/A entries[i] = glyph;
2370N/A }
2370N/A
2370N/A // Add glyphs to cache
2370N/A if (uncachedGlyphs != null) {
2370N/A uploadGlyphs(entries, uncachedGlyphs, glyphList, null);
2370N/A }
2370N/A
2370N/A return entries;
2370N/A }
2370N/A
2370N/A protected void uploadGlyphs(XRGlyphCacheEntry[] glyphs, ArrayList<XRGlyphCacheEntry> uncachedGlyphs, GlyphList gl, int[] glIndices) {
2370N/A for (XRGlyphCacheEntry glyph : uncachedGlyphs) {
2370N/A cachedPixels += glyph.getPixelCnt();
2370N/A }
2370N/A
2370N/A if (cachedPixels > MAX_CACHED_PIXELS) {
2370N/A clearCache(glyphs);
2370N/A }
2370N/A
2370N/A boolean containsLCDGlyphs = containsLCDGlyphs(uncachedGlyphs);
2370N/A List<XRGlyphCacheEntry>[] seperatedGlyphList = seperateGlyphTypes(uncachedGlyphs, containsLCDGlyphs);
2370N/A List<XRGlyphCacheEntry> grayGlyphList = seperatedGlyphList[0];
2370N/A List<XRGlyphCacheEntry> lcdGlyphList = seperatedGlyphList[1];
2370N/A
2370N/A /*
2370N/A * Some XServers crash when uploading multiple glyphs at once. TODO:
2370N/A * Implement build-switch in local case for distributors who know their
2370N/A * XServer is fixed
2370N/A */
2370N/A if (batchGlyphUpload) {
2370N/A if (grayGlyphList != null && grayGlyphList.size() > 0) {
2370N/A con.XRenderAddGlyphs(grayGlyphSet, gl, grayGlyphList, generateGlyphImageStream(grayGlyphList));
2370N/A }
2370N/A if (lcdGlyphList != null && lcdGlyphList.size() > 0) {
2370N/A con.XRenderAddGlyphs(lcdGlyphSet, gl, lcdGlyphList, generateGlyphImageStream(lcdGlyphList));
2370N/A }
2370N/A } else {
2370N/A ArrayList<XRGlyphCacheEntry> tmpList = new ArrayList<XRGlyphCacheEntry>(1);
2370N/A tmpList.add(null);
2370N/A
2370N/A for (XRGlyphCacheEntry entry : uncachedGlyphs) {
2370N/A tmpList.set(0, entry);
2370N/A
2370N/A if (entry.getGlyphSet() == grayGlyphSet) {
2370N/A con.XRenderAddGlyphs(grayGlyphSet, gl, tmpList, generateGlyphImageStream(tmpList));
2370N/A } else {
2370N/A con.XRenderAddGlyphs(lcdGlyphSet, gl, tmpList, generateGlyphImageStream(tmpList));
2370N/A }
2370N/A }
2370N/A }
2370N/A }
2370N/A
2370N/A /**
2370N/A * Seperates lcd and grayscale glyphs queued for upload, and sets the
2370N/A * appropriate glyphset for the cache entries.
2370N/A */
2370N/A protected List<XRGlyphCacheEntry>[] seperateGlyphTypes(List<XRGlyphCacheEntry> glyphList, boolean containsLCDGlyphs) {
2370N/A ArrayList<XRGlyphCacheEntry> lcdGlyphs = null;
2370N/A ArrayList<XRGlyphCacheEntry> grayGlyphs = null;
2370N/A
2370N/A for (XRGlyphCacheEntry cacheEntry : glyphList) {
2370N/A if (cacheEntry.isGrayscale(containsLCDGlyphs)) {
2370N/A if (grayGlyphs == null) {
2370N/A grayGlyphs = new ArrayList<XRGlyphCacheEntry>(glyphList.size());
2370N/A }
2370N/A cacheEntry.setGlyphSet(grayGlyphSet);
2370N/A grayGlyphs.add(cacheEntry);
2370N/A } else {
2370N/A if (lcdGlyphs == null) {
2370N/A lcdGlyphs = new ArrayList<XRGlyphCacheEntry>(glyphList.size());
2370N/A }
2370N/A cacheEntry.setGlyphSet(lcdGlyphSet);
2370N/A lcdGlyphs.add(cacheEntry);
2370N/A }
2370N/A }
2370N/A
2370N/A return new List[] { grayGlyphs, lcdGlyphs };
2370N/A }
2370N/A
2370N/A /**
2370N/A * Copies the glyph-images into a continous buffer, required for uploading.
2370N/A */
2370N/A protected byte[] generateGlyphImageStream(List<XRGlyphCacheEntry> glyphList) {
2370N/A boolean isLCDGlyph = glyphList.get(0).getGlyphSet() == lcdGlyphSet;
2370N/A
2370N/A ByteArrayOutputStream stream = new ByteArrayOutputStream((isLCDGlyph ? 4 : 1) * 48 * glyphList.size());
2370N/A for (XRGlyphCacheEntry cacheEntry : glyphList) {
2370N/A cacheEntry.writePixelData(stream, isLCDGlyph);
2370N/A }
2370N/A
2370N/A return stream.toByteArray();
2370N/A }
2370N/A
2370N/A protected boolean containsLCDGlyphs(List<XRGlyphCacheEntry> entries) {
2370N/A boolean containsLCDGlyphs = false;
2370N/A
2370N/A for (XRGlyphCacheEntry entry : entries) {
2370N/A containsLCDGlyphs = !(entry.getSourceRowBytes() == entry.getWidth());
2370N/A
2370N/A if (containsLCDGlyphs) {
2370N/A return true;
2370N/A }
2370N/A }
2370N/A return false;
2370N/A }
2370N/A
2370N/A protected void clearCache(XRGlyphCacheEntry[] glyps) {
2370N/A /*
2370N/A * Glyph uploading is so slow anyway, we can afford some inefficiency
2370N/A * here, as the cache should usually be quite small. TODO: Implement
2370N/A * something not that stupid ;)
2370N/A */
2370N/A ArrayList<XRGlyphCacheEntry> cacheList = new ArrayList<XRGlyphCacheEntry>(cacheMap.values());
2370N/A Collections.sort(cacheList, new Comparator<XRGlyphCacheEntry>() {
2370N/A public int compare(XRGlyphCacheEntry e1, XRGlyphCacheEntry e2) {
2370N/A return e2.getLastUsed() - e1.getLastUsed();
2370N/A }
2370N/A });
2370N/A
2370N/A for (XRGlyphCacheEntry glyph : glyps) {
2370N/A glyph.setPinned();
2370N/A }
2370N/A
2370N/A ArrayList<Integer> deleteGlyphList = new ArrayList<Integer>();
2370N/A int pixelsToRelease = cachedPixels - MAX_CACHED_PIXELS;
2370N/A
2370N/A for (int i = cacheList.size() - 1; i >= 0 && pixelsToRelease > 0; i--) {
2370N/A XRGlyphCacheEntry entry = cacheList.get(i);
2370N/A
2370N/A if (!entry.isPinned()) {
2370N/A pixelsToRelease -= entry.getPixelCnt();
2370N/A deleteGlyphList.add(new Integer(entry.getGlyphID()));
2370N/A }
2370N/A }
2370N/A
2370N/A for (XRGlyphCacheEntry glyph : glyps) {
2370N/A glyph.setUnpinned();
2370N/A }
2370N/A
2370N/A freeGlyphs(deleteGlyphList);
2370N/A }
2370N/A
2370N/A private void freeGlyphs(List<Integer> glyphIdList) {
2370N/A
2370N/A freeGlyphIDs.addAll(glyphIdList);
2370N/A
2370N/A GrowableIntArray removedLCDGlyphs = new GrowableIntArray(1, 1);
2370N/A GrowableIntArray removedGrayscaleGlyphs = new GrowableIntArray(1, 1);
2370N/A
2370N/A for (Integer glyphId : glyphIdList) {
2370N/A tmp.setValue(glyphId.intValue());
2370N/A XRGlyphCacheEntry entry = cacheMap.get(tmp);
2370N/A cachedPixels -= entry.getPixelCnt();
2370N/A
2370N/A int removedGlyphID = entry.getGlyphID();
2370N/A tmp.setValue(removedGlyphID);
2370N/A cacheMap.remove(tmp);
2370N/A
2370N/A if (entry.getGlyphSet() == grayGlyphSet) {
2370N/A removedGrayscaleGlyphs.addInt(removedGlyphID);
2370N/A } else {
2370N/A removedLCDGlyphs.addInt(removedGlyphID);
2370N/A }
2370N/A
2370N/A entry.setGlyphID(0);
2370N/A }
2370N/A
2370N/A if (removedGrayscaleGlyphs.getSize() > 0) {
2370N/A con.XRenderFreeGlyphs(grayGlyphSet, removedGrayscaleGlyphs.getSizedArray());
2370N/A }
2370N/A
2370N/A if (removedLCDGlyphs.getSize() > 0) {
2370N/A con.XRenderFreeGlyphs(lcdGlyphSet, removedLCDGlyphs.getSizedArray());
2370N/A }
2370N/A }
2370N/A}