Showing the changes to java/pkg/sun/java2d/pipe/BufferedTextPipe.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,168 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2007, 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.java2d.pipe; | |
| 27 | + | |
| 28 | +import java.awt.AlphaComposite; | |
| 29 | +import java.awt.Composite; | |
| 30 | +import sun.font.GlyphList; | |
| 31 | +import sun.java2d.SunGraphics2D; | |
| 32 | +import sun.java2d.SurfaceData; | |
| 33 | +import static sun.java2d.pipe.BufferedOpCodes.*; | |
| 34 | + | |
| 35 | +public abstract class BufferedTextPipe extends GlyphListPipe { | |
| 36 | + | |
| 37 | + private static final int BYTES_PER_GLYPH_IMAGE = 8; | |
| 38 | + private static final int BYTES_PER_GLYPH_POSITION = 8; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * The following offsets are used to pack the parameters in | |
| 42 | + * createPackedParams(). (They are also used at the native level when | |
| 43 | + * unpacking the params.) | |
| 44 | + */ | |
| 45 | + private static final int OFFSET_CONTRAST = 8; | |
| 46 | + private static final int OFFSET_RGBORDER = 2; | |
| 47 | + private static final int OFFSET_SUBPIXPOS = 1; | |
| 48 | + private static final int OFFSET_POSITIONS = 0; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Packs the given parameters into a single int value in order to save | |
| 52 | + * space on the rendering queue. Note that most of these parameters | |
| 53 | + * are only used for rendering LCD-optimized text, but conditionalizing | |
| 54 | + * this work wouldn't make any impact on performance, so we will pack | |
| 55 | + * those parameters even in the non-LCD case. | |
| 56 | + */ | |
| 57 | + private static int createPackedParams(SunGraphics2D sg2d, GlyphList gl) { | |
| 58 | + return | |
| 59 | + (((gl.usePositions() ? 1 : 0) << OFFSET_POSITIONS) | | |
| 60 | + ((gl.isSubPixPos() ? 1 : 0) << OFFSET_SUBPIXPOS) | | |
| 61 | + ((gl.isRGBOrder() ? 1 : 0) << OFFSET_RGBORDER ) | | |
| 62 | + ((sg2d.lcdTextContrast & 0xff) << OFFSET_CONTRAST )); | |
| 63 | + } | |
| 64 | + | |
| 65 | + protected final RenderQueue rq; | |
| 66 | + | |
| 67 | + protected BufferedTextPipe(RenderQueue rq) { | |
| 68 | + this.rq = rq; | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Override | |
| 72 | + protected void drawGlyphList(SunGraphics2D sg2d, GlyphList gl) { | |
| 73 | + /* | |
| 74 | + * The native drawGlyphList() only works with two composite types: | |
| 75 | + * - CompositeType.SrcOver (with any extra alpha), or | |
| 76 | + * - CompositeType.Xor | |
| 77 | + */ | |
| 78 | + Composite comp = sg2d.composite; | |
| 79 | + if (comp == AlphaComposite.Src) { | |
| 80 | + /* | |
| 81 | + * In addition to the composite types listed above, the logic | |
| 82 | + * in OGL/D3DSurfaceData.validatePipe() allows for | |
| 83 | + * CompositeType.SrcNoEa, but only in the presence of an opaque | |
| 84 | + * color. If we reach this case, we know the color is opaque, | |
| 85 | + * and therefore SrcNoEa is the same as SrcOverNoEa, so we | |
| 86 | + * override the composite here. | |
| 87 | + */ | |
| 88 | + comp = AlphaComposite.SrcOver; | |
| 89 | + } | |
| 90 | + | |
| 91 | + rq.lock(); | |
| 92 | + try { | |
| 93 | + validateContext(sg2d, comp); | |
| 94 | + enqueueGlyphList(sg2d, gl); | |
| 95 | + } finally { | |
| 96 | + rq.unlock(); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + private void enqueueGlyphList(final SunGraphics2D sg2d, | |
| 101 | + final GlyphList gl) | |
| 102 | + { | |
| 103 | + // assert rq.lock.isHeldByCurrentThread(); | |
| 104 | + RenderBuffer buf = rq.getBuffer(); | |
| 105 | + final int totalGlyphs = gl.getNumGlyphs(); | |
| 106 | + int glyphBytesRequired = totalGlyphs * BYTES_PER_GLYPH_IMAGE; | |
| 107 | + int posBytesRequired = | |
| 108 | + gl.usePositions() ? totalGlyphs * BYTES_PER_GLYPH_POSITION : 0; | |
| 109 | + int totalBytesRequired = 24 + glyphBytesRequired + posBytesRequired; | |
| 110 | + | |
| 111 | + final long[] images = gl.getImages(); | |
| 112 | + final float glyphListOrigX = gl.getX() + 0.5f; | |
| 113 | + final float glyphListOrigY = gl.getY() + 0.5f; | |
| 114 | + | |
| 115 | + // make sure the RenderQueue keeps a hard reference to the FontStrike | |
| 116 | + // so that the associated glyph images are not disposed while enqueued | |
| 117 | + rq.addReference(gl.getStrike()); | |
| 118 | + | |
| 119 | + if (totalBytesRequired <= buf.capacity()) { | |
| 120 | + if (totalBytesRequired > buf.remaining()) { | |
| 121 | + // process the queue first and then enqueue the glyphs | |
| 122 | + rq.flushNow(); | |
| 123 | + } | |
| 124 | + rq.ensureAlignment(20); | |
| 125 | + buf.putInt(DRAW_GLYPH_LIST); | |
| 126 | + // enqueue parameters | |
| 127 | + buf.putInt(totalGlyphs); | |
| 128 | + buf.putInt(createPackedParams(sg2d, gl)); | |
| 129 | + buf.putFloat(glyphListOrigX); | |
| 130 | + buf.putFloat(glyphListOrigY); | |
| 131 | + // now enqueue glyph information | |
| 132 | + buf.put(images, 0, totalGlyphs); | |
| 133 | + if (gl.usePositions()) { | |
| 134 | + float[] positions = gl.getPositions(); | |
| 135 | + buf.put(positions, 0, 2*totalGlyphs); | |
| 136 | + } | |
| 137 | + } else { | |
| 138 | + // queue is too small to accomodate glyphs; perform | |
| 139 | + // the operation directly on the queue flushing thread | |
| 140 | + rq.flushAndInvokeNow(new Runnable() { | |
| 141 | + public void run() { | |
| 142 | + drawGlyphList(totalGlyphs, gl.usePositions(), | |
| 143 | + gl.isSubPixPos(), gl.isRGBOrder(), | |
| 144 | + sg2d.lcdTextContrast, | |
| 145 | + glyphListOrigX, glyphListOrigY, | |
| 146 | + images, gl.getPositions()); | |
| 147 | + } | |
| 148 | + }); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * Called as a separate Runnable when the operation is too large to fit | |
| 154 | + * on the RenderQueue. The OGL/D3D pipelines each have their own (small) | |
| 155 | + * native implementation of this method. | |
| 156 | + */ | |
| 157 | + protected abstract void drawGlyphList(int numGlyphs, boolean usePositions, | |
| 158 | + boolean subPixPos, boolean rgbOrder, | |
| 159 | + int lcdContrast, | |
| 160 | + float glOrigX, float glOrigY, | |
| 161 | + long[] images, float[] positions); | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Validates the state in the provided SunGraphics2D object. | |
| 165 | + */ | |
| 166 | + protected abstract void validateContext(SunGraphics2D sg2d, | |
| 167 | + Composite comp); | |
| 168 | +} | |