Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/BufferedTextPipe.java
41159 views
/*1* Copyright (c) 2007, 2013, 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.java2d.pipe;2627import java.awt.AlphaComposite;28import java.awt.Composite;29import sun.font.GlyphList;30import sun.java2d.SunGraphics2D;31import sun.java2d.SurfaceData;32import static sun.java2d.pipe.BufferedOpCodes.*;3334import java.lang.annotation.Native;3536public abstract class BufferedTextPipe extends GlyphListPipe {3738@Native private static final int BYTES_PER_GLYPH_IMAGE = 8;39@Native private static final int BYTES_PER_GLYPH_POSITION = 8;4041/**42* The following offsets are used to pack the parameters in43* createPackedParams(). (They are also used at the native level when44* unpacking the params.)45*/46@Native private static final int OFFSET_CONTRAST = 8;47@Native private static final int OFFSET_RGBORDER = 2;48@Native private static final int OFFSET_SUBPIXPOS = 1;49@Native private static final int OFFSET_POSITIONS = 0;5051/**52* Packs the given parameters into a single int value in order to save53* space on the rendering queue. Note that most of these parameters54* are only used for rendering LCD-optimized text, but conditionalizing55* this work wouldn't make any impact on performance, so we will pack56* those parameters even in the non-LCD case.57*/58private static int createPackedParams(SunGraphics2D sg2d, GlyphList gl) {59return60(((gl.usePositions() ? 1 : 0) << OFFSET_POSITIONS) |61((gl.isSubPixPos() ? 1 : 0) << OFFSET_SUBPIXPOS) |62((gl.isRGBOrder() ? 1 : 0) << OFFSET_RGBORDER ) |63((sg2d.lcdTextContrast & 0xff) << OFFSET_CONTRAST ));64}6566protected final RenderQueue rq;6768protected BufferedTextPipe(RenderQueue rq) {69this.rq = rq;70}7172@Override73protected void drawGlyphList(SunGraphics2D sg2d, GlyphList gl) {74/*75* The native drawGlyphList() only works with two composite types:76* - CompositeType.SrcOver (with any extra alpha), or77* - CompositeType.Xor78*/79Composite comp = sg2d.composite;80if (comp == AlphaComposite.Src) {81/*82* In addition to the composite types listed above, the logic83* in OGL/D3DSurfaceData.validatePipe() allows for84* CompositeType.SrcNoEa, but only in the presence of an opaque85* color. If we reach this case, we know the color is opaque,86* and therefore SrcNoEa is the same as SrcOverNoEa, so we87* override the composite here.88*/89comp = AlphaComposite.SrcOver;90}9192rq.lock();93try {94validateContext(sg2d, comp);95enqueueGlyphList(sg2d, gl);96} finally {97rq.unlock();98}99}100101private void enqueueGlyphList(final SunGraphics2D sg2d,102final GlyphList gl)103{104// assert rq.lock.isHeldByCurrentThread();105RenderBuffer buf = rq.getBuffer();106final int totalGlyphs = gl.getNumGlyphs();107int glyphBytesRequired = totalGlyphs * BYTES_PER_GLYPH_IMAGE;108int posBytesRequired =109gl.usePositions() ? totalGlyphs * BYTES_PER_GLYPH_POSITION : 0;110int totalBytesRequired = 24 + glyphBytesRequired + posBytesRequired;111112final long[] images = gl.getImages();113final float glyphListOrigX = gl.getX() + 0.5f;114final float glyphListOrigY = gl.getY() + 0.5f;115116// make sure the RenderQueue keeps a hard reference to the FontStrike117// so that the associated glyph images are not disposed while enqueued118rq.addReference(gl.getStrike());119120if (totalBytesRequired <= buf.capacity()) {121if (totalBytesRequired > buf.remaining()) {122// process the queue first and then enqueue the glyphs123rq.flushNow();124}125rq.ensureAlignment(20);126buf.putInt(DRAW_GLYPH_LIST);127// enqueue parameters128buf.putInt(totalGlyphs);129buf.putInt(createPackedParams(sg2d, gl));130buf.putFloat(glyphListOrigX);131buf.putFloat(glyphListOrigY);132// now enqueue glyph information133buf.put(images, 0, totalGlyphs);134if (gl.usePositions()) {135float[] positions = gl.getPositions();136buf.put(positions, 0, 2*totalGlyphs);137}138} else {139// queue is too small to accommodate glyphs; perform140// the operation directly on the queue flushing thread141rq.flushAndInvokeNow(new Runnable() {142public void run() {143drawGlyphList(totalGlyphs, gl.usePositions(),144gl.isSubPixPos(), gl.isRGBOrder(),145sg2d.lcdTextContrast,146glyphListOrigX, glyphListOrigY,147images, gl.getPositions());148}149});150}151}152153/**154* Called as a separate Runnable when the operation is too large to fit155* on the RenderQueue. The OGL/D3D pipelines each have their own (small)156* native implementation of this method.157*/158protected abstract void drawGlyphList(int numGlyphs, boolean usePositions,159boolean subPixPos, boolean rgbOrder,160int lcdContrast,161float glOrigX, float glOrigY,162long[] images, float[] positions);163164/**165* Validates the state in the provided SunGraphics2D object.166*/167protected abstract void validateContext(SunGraphics2D sg2d,168Composite comp);169}170171172