jdk/src/share/classes/sun/java2d/pipe/BufferedTextPipe.java
author erikj
Thu, 07 Jun 2012 18:05:09 -0700
changeset 12813 c10ab96dcf41
parent 5506 202f599c92aa
child 16734 da1901d79073
permissions -rw-r--r--
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI Reviewed-by: ohair, ohrstrom, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.java2d.pipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AlphaComposite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Composite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.font.GlyphList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.java2d.SunGraphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import static sun.java2d.pipe.BufferedOpCodes.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    35
import javax.tools.annotation.GenerateNativeHeader;
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    36
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    37
/* No native methods here, but the constants are needed in the supporting JNI code */
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    38
@GenerateNativeHeader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public abstract class BufferedTextPipe extends GlyphListPipe {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final int BYTES_PER_GLYPH_IMAGE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final int BYTES_PER_GLYPH_POSITION = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * The following offsets are used to pack the parameters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * createPackedParams().  (They are also used at the native level when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * unpacking the params.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int OFFSET_CONTRAST  = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int OFFSET_RGBORDER  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int OFFSET_SUBPIXPOS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int OFFSET_POSITIONS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Packs the given parameters into a single int value in order to save
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * space on the rendering queue.  Note that most of these parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * are only used for rendering LCD-optimized text, but conditionalizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * this work wouldn't make any impact on performance, so we will pack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * those parameters even in the non-LCD case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static int createPackedParams(SunGraphics2D sg2d, GlyphList gl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            (((gl.usePositions() ? 1 : 0)   << OFFSET_POSITIONS) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
             ((gl.isSubPixPos()  ? 1 : 0)   << OFFSET_SUBPIXPOS) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
             ((gl.isRGBOrder()   ? 1 : 0)   << OFFSET_RGBORDER ) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             ((sg2d.lcdTextContrast & 0xff) << OFFSET_CONTRAST ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected final RenderQueue rq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected BufferedTextPipe(RenderQueue rq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.rq = rq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected void drawGlyphList(SunGraphics2D sg2d, GlyphList gl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * The native drawGlyphList() only works with two composite types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         *    - CompositeType.SrcOver (with any extra alpha), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         *    - CompositeType.Xor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Composite comp = sg2d.composite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (comp == AlphaComposite.Src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
             * In addition to the composite types listed above, the logic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
             * in OGL/D3DSurfaceData.validatePipe() allows for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             * CompositeType.SrcNoEa, but only in the presence of an opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             * color.  If we reach this case, we know the color is opaque,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * and therefore SrcNoEa is the same as SrcOverNoEa, so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * override the composite here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            comp = AlphaComposite.SrcOver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        rq.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            validateContext(sg2d, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            enqueueGlyphList(sg2d, gl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            rq.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private void enqueueGlyphList(final SunGraphics2D sg2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                  final GlyphList gl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        final int totalGlyphs = gl.getNumGlyphs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int glyphBytesRequired = totalGlyphs * BYTES_PER_GLYPH_IMAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int posBytesRequired =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            gl.usePositions() ? totalGlyphs * BYTES_PER_GLYPH_POSITION : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int totalBytesRequired = 24 + glyphBytesRequired + posBytesRequired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        final long[] images = gl.getImages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        final float glyphListOrigX = gl.getX() + 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        final float glyphListOrigY = gl.getY() + 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // make sure the RenderQueue keeps a hard reference to the FontStrike
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // so that the associated glyph images are not disposed while enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        rq.addReference(gl.getStrike());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (totalBytesRequired <= buf.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (totalBytesRequired > buf.remaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // process the queue first and then enqueue the glyphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                rq.flushNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            rq.ensureAlignment(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            buf.putInt(DRAW_GLYPH_LIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // enqueue parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            buf.putInt(totalGlyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            buf.putInt(createPackedParams(sg2d, gl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            buf.putFloat(glyphListOrigX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            buf.putFloat(glyphListOrigY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // now enqueue glyph information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            buf.put(images, 0, totalGlyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (gl.usePositions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                float[] positions = gl.getPositions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                buf.put(positions, 0, 2*totalGlyphs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // queue is too small to accomodate glyphs; perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // the operation directly on the queue flushing thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            rq.flushAndInvokeNow(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    drawGlyphList(totalGlyphs, gl.usePositions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                  gl.isSubPixPos(), gl.isRGBOrder(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                  sg2d.lcdTextContrast,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                  glyphListOrigX, glyphListOrigY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                  images, gl.getPositions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Called as a separate Runnable when the operation is too large to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * on the RenderQueue.  The OGL/D3D pipelines each have their own (small)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * native implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected abstract void drawGlyphList(int numGlyphs, boolean usePositions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                          boolean subPixPos, boolean rgbOrder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                          int lcdContrast,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                          float glOrigX, float glOrigY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                          long[] images, float[] positions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Validates the state in the provided SunGraphics2D object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected abstract void validateContext(SunGraphics2D sg2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                            Composite comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}