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