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