Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/font/XRGlyphCache.java
32287 views
/*1* Copyright (c) 2010, 2011, 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<XRGlyphCacheEntry>(glyphList.size());196}197cacheEntry.setGlyphSet(grayGlyphSet);198grayGlyphs.add(cacheEntry);199} else {200if (lcdGlyphs == null) {201lcdGlyphs = new ArrayList<XRGlyphCacheEntry>(glyphList.size());202}203cacheEntry.setGlyphSet(lcdGlyphSet);204lcdGlyphs.add(cacheEntry);205}206}207208return new List[] { grayGlyphs, lcdGlyphs };209}210211/**212* Copies the glyph-images into a continous buffer, required for uploading.213*/214protected byte[] generateGlyphImageStream(List<XRGlyphCacheEntry> glyphList) {215boolean isLCDGlyph = glyphList.get(0).getGlyphSet() == lcdGlyphSet;216217ByteArrayOutputStream stream = new ByteArrayOutputStream((isLCDGlyph ? 4 : 1) * 48 * glyphList.size());218for (XRGlyphCacheEntry cacheEntry : glyphList) {219cacheEntry.writePixelData(stream, isLCDGlyph);220}221222return stream.toByteArray();223}224225protected boolean containsLCDGlyphs(List<XRGlyphCacheEntry> entries) {226boolean containsLCDGlyphs = false;227228for (XRGlyphCacheEntry entry : entries) {229containsLCDGlyphs = !(entry.getSourceRowBytes() == entry.getWidth());230231if (containsLCDGlyphs) {232return true;233}234}235return false;236}237238protected void clearCache(XRGlyphCacheEntry[] glyps) {239/*240* Glyph uploading is so slow anyway, we can afford some inefficiency241* here, as the cache should usually be quite small. TODO: Implement242* something not that stupid ;)243*/244ArrayList<XRGlyphCacheEntry> cacheList = new ArrayList<XRGlyphCacheEntry>(cacheMap.values());245Collections.sort(cacheList, new Comparator<XRGlyphCacheEntry>() {246public int compare(XRGlyphCacheEntry e1, XRGlyphCacheEntry e2) {247return e2.getLastUsed() - e1.getLastUsed();248}249});250251for (XRGlyphCacheEntry glyph : glyps) {252glyph.setPinned();253}254255GrowableIntArray deleteGlyphList = new GrowableIntArray(1, 10);256int pixelsToRelease = cachedPixels - MAX_CACHED_PIXELS;257258for (int i = cacheList.size() - 1; i >= 0 && pixelsToRelease > 0; i--) {259XRGlyphCacheEntry entry = cacheList.get(i);260261if (!entry.isPinned()) {262pixelsToRelease -= entry.getPixelCnt();263deleteGlyphList.addInt(entry.getGlyphID());264}265}266267for (XRGlyphCacheEntry glyph : glyps) {268glyph.setUnpinned();269}270271freeGlyphs(deleteGlyphList);272}273274private void freeGlyphs(GrowableIntArray glyphIdList) {275GrowableIntArray removedLCDGlyphs = new GrowableIntArray(1, 10);276GrowableIntArray removedGrayscaleGlyphs = new GrowableIntArray(1, 10);277278for (int i=0; i < glyphIdList.getSize(); i++) {279int glyphId = glyphIdList.getInt(i);280freeGlyphIDs.add(glyphId);281282tmp.setValue(glyphId);283XRGlyphCacheEntry entry = cacheMap.get(tmp);284cachedPixels -= entry.getPixelCnt();285cacheMap.remove(tmp);286287if (entry.getGlyphSet() == grayGlyphSet) {288removedGrayscaleGlyphs.addInt(glyphId);289} else {290removedLCDGlyphs.addInt(glyphId);291}292293entry.setGlyphID(0);294}295296if (removedGrayscaleGlyphs.getSize() > 0) {297con.XRenderFreeGlyphs(grayGlyphSet, removedGrayscaleGlyphs.getSizedArray());298}299300if (removedLCDGlyphs.getSize() > 0) {301con.XRenderFreeGlyphs(lcdGlyphSet, removedLCDGlyphs.getSizedArray());302}303}304}305306307