Path: blob/master/src/java.desktop/unix/classes/sun/font/XRGlyphCache.java
41153 views
/*1* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.font;2627import java.io.*;28import java.util.*;2930import sun.awt.*;31import sun.java2d.xr.*;3233/**34* Glyph cache used by the XRender pipeline.35*36* @author Clemens Eisserer37*/3839public class XRGlyphCache implements GlyphDisposedListener {40XRBackend con;41XRCompositeManager maskBuffer;42HashMap<MutableInteger, XRGlyphCacheEntry> cacheMap = new HashMap<MutableInteger, XRGlyphCacheEntry>(256);4344int nextID = 1;45MutableInteger tmp = new MutableInteger(0);4647int grayGlyphSet;48int lcdGlyphSet;4950int time = 0;51int cachedPixels = 0;52static final int MAX_CACHED_PIXELS = 100000;5354ArrayList<Integer> freeGlyphIDs = new ArrayList<Integer>(255);5556static final boolean batchGlyphUpload = true; // Boolean.parseBoolean(System.getProperty("sun.java2d.xrender.batchGlyphUpload"));5758public XRGlyphCache(XRCompositeManager maskBuf) {59this.con = maskBuf.getBackend();60this.maskBuffer = maskBuf;6162grayGlyphSet = con.XRenderCreateGlyphSet(XRUtils.PictStandardA8);63lcdGlyphSet = con.XRenderCreateGlyphSet(XRUtils.PictStandardARGB32);6465StrikeCache.addGlyphDisposedListener(this);66}6768public void glyphDisposed(ArrayList<Long> glyphPtrList) {69try {70SunToolkit.awtLock();7172GrowableIntArray glyphIDList = new GrowableIntArray(1, glyphPtrList.size());73for (long glyphPtr : glyphPtrList) {74int glyphID = XRGlyphCacheEntry.getGlyphID(glyphPtr);7576//Check if glyph hasn't been freed already77if (glyphID != 0) {78glyphIDList.addInt(glyphID);79}80}81freeGlyphs(glyphIDList);82} finally {83SunToolkit.awtUnlock();84}85}8687protected int getFreeGlyphID() {88if (freeGlyphIDs.size() > 0) {89int newID = freeGlyphIDs.remove(freeGlyphIDs.size() - 1);90return newID;91}92return nextID++;93}9495protected XRGlyphCacheEntry getEntryForPointer(long imgPtr) {96int id = XRGlyphCacheEntry.getGlyphID(imgPtr);9798if (id == 0) {99return null;100}101102tmp.setValue(id);103return cacheMap.get(tmp);104}105106public XRGlyphCacheEntry[] cacheGlyphs(GlyphList glyphList) {107time++;108109XRGlyphCacheEntry[] entries = new XRGlyphCacheEntry[glyphList.getNumGlyphs()];110long[] imgPtrs = glyphList.getImages();111ArrayList<XRGlyphCacheEntry> uncachedGlyphs = null;112113for (int i = 0; i < glyphList.getNumGlyphs(); i++) {114XRGlyphCacheEntry glyph;115116if (imgPtrs[i] == 0L) {117continue;118}119// Find uncached glyphs and queue them for upload120if ((glyph = getEntryForPointer(imgPtrs[i])) == null) {121glyph = new XRGlyphCacheEntry(imgPtrs[i], glyphList);122glyph.setGlyphID(getFreeGlyphID());123cacheMap.put(new MutableInteger(glyph.getGlyphID()), glyph);124125if (uncachedGlyphs == null) {126uncachedGlyphs = new ArrayList<XRGlyphCacheEntry>();127}128uncachedGlyphs.add(glyph);129}130glyph.setLastUsed(time);131entries[i] = glyph;132}133134// Add glyphs to cache135if (uncachedGlyphs != null) {136uploadGlyphs(entries, uncachedGlyphs, glyphList, null);137}138139return entries;140}141142protected void uploadGlyphs(XRGlyphCacheEntry[] glyphs, ArrayList<XRGlyphCacheEntry> uncachedGlyphs, GlyphList gl, int[] glIndices) {143for (XRGlyphCacheEntry glyph : uncachedGlyphs) {144cachedPixels += glyph.getPixelCnt();145}146147if (cachedPixels > MAX_CACHED_PIXELS) {148clearCache(glyphs);149}150151boolean containsLCDGlyphs = containsLCDGlyphs(uncachedGlyphs);152List<XRGlyphCacheEntry>[] seperatedGlyphList = seperateGlyphTypes(uncachedGlyphs, containsLCDGlyphs);153List<XRGlyphCacheEntry> grayGlyphList = seperatedGlyphList[0];154List<XRGlyphCacheEntry> lcdGlyphList = seperatedGlyphList[1];155156/*157* Some XServers crash when uploading multiple glyphs at once. TODO:158* Implement build-switch in local case for distributors who know their159* XServer is fixed160*/161if (batchGlyphUpload) {162if (grayGlyphList != null && grayGlyphList.size() > 0) {163con.XRenderAddGlyphs(grayGlyphSet, gl, grayGlyphList, generateGlyphImageStream(grayGlyphList));164}165if (lcdGlyphList != null && lcdGlyphList.size() > 0) {166con.XRenderAddGlyphs(lcdGlyphSet, gl, lcdGlyphList, generateGlyphImageStream(lcdGlyphList));167}168} else {169ArrayList<XRGlyphCacheEntry> tmpList = new ArrayList<XRGlyphCacheEntry>(1);170tmpList.add(null);171172for (XRGlyphCacheEntry entry : uncachedGlyphs) {173tmpList.set(0, entry);174175if (entry.getGlyphSet() == grayGlyphSet) {176con.XRenderAddGlyphs(grayGlyphSet, gl, tmpList, generateGlyphImageStream(tmpList));177} else {178con.XRenderAddGlyphs(lcdGlyphSet, gl, tmpList, generateGlyphImageStream(tmpList));179}180}181}182}183184/**185* Seperates lcd and grayscale glyphs queued for upload, and sets the186* appropriate glyphset for the cache entries.187*/188protected List<XRGlyphCacheEntry>[] seperateGlyphTypes(List<XRGlyphCacheEntry> glyphList, boolean containsLCDGlyphs) {189ArrayList<XRGlyphCacheEntry> lcdGlyphs = null;190ArrayList<XRGlyphCacheEntry> grayGlyphs = null;191192for (XRGlyphCacheEntry cacheEntry : glyphList) {193if (cacheEntry.isGrayscale(containsLCDGlyphs)) {194if (grayGlyphs == null) {195grayGlyphs = new ArrayList<>(glyphList.size());196}197cacheEntry.setGlyphSet(grayGlyphSet);198grayGlyphs.add(cacheEntry);199} else {200if (lcdGlyphs == null) {201lcdGlyphs = new ArrayList<>(glyphList.size());202}203cacheEntry.setGlyphSet(lcdGlyphSet);204lcdGlyphs.add(cacheEntry);205}206}207// Arrays and generics don't play well together208@SuppressWarnings({"unchecked", "rawtypes"})209List<XRGlyphCacheEntry>[] tmp =210(List<XRGlyphCacheEntry>[]) (new List[] { grayGlyphs, lcdGlyphs });211return tmp;212}213214/**215* Copies the glyph-images into a continous buffer, required for uploading.216*/217protected byte[] generateGlyphImageStream(List<XRGlyphCacheEntry> glyphList) {218boolean isLCDGlyph = glyphList.get(0).getGlyphSet() == lcdGlyphSet;219220ByteArrayOutputStream stream = new ByteArrayOutputStream((isLCDGlyph ? 4 : 1) * 48 * glyphList.size());221for (XRGlyphCacheEntry cacheEntry : glyphList) {222cacheEntry.writePixelData(stream, isLCDGlyph);223}224225return stream.toByteArray();226}227228protected boolean containsLCDGlyphs(List<XRGlyphCacheEntry> entries) {229boolean containsLCDGlyphs = false;230231for (XRGlyphCacheEntry entry : entries) {232containsLCDGlyphs = !(entry.getSourceRowBytes() == entry.getWidth());233234if (containsLCDGlyphs) {235return true;236}237}238return false;239}240241protected void clearCache(XRGlyphCacheEntry[] glyps) {242/*243* Glyph uploading is so slow anyway, we can afford some inefficiency244* here, as the cache should usually be quite small. TODO: Implement245* something not that stupid ;)246*/247ArrayList<XRGlyphCacheEntry> cacheList = new ArrayList<XRGlyphCacheEntry>(cacheMap.values());248Collections.sort(cacheList, new Comparator<XRGlyphCacheEntry>() {249public int compare(XRGlyphCacheEntry e1, XRGlyphCacheEntry e2) {250return e2.getLastUsed() - e1.getLastUsed();251}252});253254for (XRGlyphCacheEntry glyph : glyps) {255glyph.setPinned();256}257258GrowableIntArray deleteGlyphList = new GrowableIntArray(1, 10);259int pixelsToRelease = cachedPixels - MAX_CACHED_PIXELS;260261for (int i = cacheList.size() - 1; i >= 0 && pixelsToRelease > 0; i--) {262XRGlyphCacheEntry entry = cacheList.get(i);263264if (!entry.isPinned()) {265pixelsToRelease -= entry.getPixelCnt();266deleteGlyphList.addInt(entry.getGlyphID());267}268}269270for (XRGlyphCacheEntry glyph : glyps) {271glyph.setUnpinned();272}273274freeGlyphs(deleteGlyphList);275}276277private void freeGlyphs(GrowableIntArray glyphIdList) {278GrowableIntArray removedLCDGlyphs = new GrowableIntArray(1, 10);279GrowableIntArray removedGrayscaleGlyphs = new GrowableIntArray(1, 10);280281for (int i=0; i < glyphIdList.getSize(); i++) {282int glyphId = glyphIdList.getInt(i);283freeGlyphIDs.add(glyphId);284285tmp.setValue(glyphId);286XRGlyphCacheEntry entry = cacheMap.get(tmp);287cachedPixels -= entry.getPixelCnt();288cacheMap.remove(tmp);289290if (entry.getGlyphSet() == grayGlyphSet) {291removedGrayscaleGlyphs.addInt(glyphId);292} else {293removedLCDGlyphs.addInt(glyphId);294}295296entry.setGlyphID(0);297}298299if (removedGrayscaleGlyphs.getSize() > 0) {300con.XRenderFreeGlyphs(grayGlyphSet, removedGrayscaleGlyphs.getSizedArray());301}302303if (removedLCDGlyphs.getSize() > 0) {304con.XRenderFreeGlyphs(lcdGlyphSet, removedLCDGlyphs.getSizedArray());305}306}307}308309310