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