Path: blob/master/src/java.desktop/unix/classes/sun/java2d/xr/XRBackendNative.java
41159 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.xr;2627import java.awt.geom.*;28import java.util.*;2930import sun.font.*;31import sun.java2d.pipe.*;3233import static sun.java2d.xr.XRUtils.XDoubleToFixed;3435/**36* Native implementation of XRBackend.37* Almost direct 1:1 binding to libX11.38*39* @author Clemens Eisserer40*/4142public class XRBackendNative implements XRBackend {4344static {45initIDs();46}4748private static long FMTPTR_A8;49private static long FMTPTR_ARGB32;50private static long MASK_XIMG;5152private static native void initIDs();5354public native long createGC(int drawable);5556public native void freeGC(long gc);5758public native int createPixmap(int drawable, int depth,59int width, int height);6061private native int createPictureNative(int drawable, long formatID);6263public native void freePicture(int picture);6465public native void freePixmap(int pixmap);6667public native void setGCExposures(long gc, boolean exposure);6869public native void setGCForeground(long gc, int pixel);7071public native void setPictureRepeat(int picture, int repeat);7273public native void copyArea(int src, int dst, long gc,74int srcx, int srcy, int width, int height,75int dstx, int dsty);7677public native void setGCMode(long gc, boolean copy);7879private static native void GCRectanglesNative(int drawable, long gc,80int[] rectArray, int rectCnt);8182public native void renderComposite(byte op, int src, int mask,83int dst, int srcX, int srcY,84int maskX, int maskY, int dstX, int dstY,85int width, int height);8687private native void renderRectangle(int dst, byte op,88short red, short green,89short blue, short alpha,90int x, int y, int width, int height);9192private static native void93XRenderRectanglesNative(int dst, byte op,94short red, short green,95short blue, short alpha,96int[] rects, int rectCnt);9798private native void XRSetTransformNative(int pic,99int m00, int m01, int m02,100int m10, int m11, int m12);101102private static native int103XRCreateLinearGradientPaintNative(float[] fractionsArray,104short[] pixelsArray,105int x1, int y1, int x2, int y2,106int numStops, int repeat);107108private static native int109XRCreateRadialGradientPaintNative(float[] fractionsArray,110short[] pixelsArray, int numStops,111int centerX, int centerY,112int innerRadius, int outerRadius,113int repeat);114115public native void setFilter(int picture, int filter);116117private static native void XRSetClipNative(long dst,118int x1, int y1, int x2, int y2,119Region clip, boolean isGC);120121public void GCRectangles(int drawable, long gc, GrowableRectArray rects) {122GCRectanglesNative(drawable, gc, rects.getArray(), rects.getSize());123}124125public int createPicture(int drawable, int formatID) {126return createPictureNative(drawable, getFormatPtr(formatID));127}128129public void setPictureTransform(int picture, AffineTransform transform) {130XRSetTransformNative(picture,131XDoubleToFixed(transform.getScaleX()),132XDoubleToFixed(transform.getShearX()),133XDoubleToFixed(transform.getTranslateX()),134XDoubleToFixed(transform.getShearY()),135XDoubleToFixed(transform.getScaleY()),136XDoubleToFixed(transform.getTranslateY()));137}138139public void renderRectangle(int dst, byte op, XRColor color,140int x, int y, int width, int height) {141renderRectangle(dst, op, (short)color.red, (short)color.green,142(short)color.blue, (short)color.alpha,143x, y, width, height);144}145146private short[] getRenderColors(int[] pixels) {147short[] renderColors = new short[pixels.length * 4];148149XRColor c = new XRColor();150for (int i = 0; i < pixels.length; i++) {151c.setColorValues(pixels[i]);152renderColors[i * 4 + 0] = (short) c.alpha;153renderColors[i * 4 + 1] = (short) c.red;154renderColors[i * 4 + 2] = (short) c.green;155renderColors[i * 4 + 3] = (short) c.blue;156}157158return renderColors;159}160161private static long getFormatPtr(int formatID) {162switch (formatID) {163case XRUtils.PictStandardA8:164return FMTPTR_A8;165case XRUtils.PictStandardARGB32:166return FMTPTR_ARGB32;167}168169return 0L;170}171172public int createLinearGradient(Point2D p1, Point2D p2, float[] fractions,173int[] pixels, int repeat) {174175short[] colorValues = getRenderColors(pixels);176int gradient =177XRCreateLinearGradientPaintNative(fractions, colorValues,178XDoubleToFixed(p1.getX()), XDoubleToFixed(p1.getY()),179XDoubleToFixed(p2.getX()), XDoubleToFixed(p2.getY()),180fractions.length, repeat);181return gradient;182}183184public int createRadialGradient(float centerX, float centerY,185float innerRadius, float outerRadius,186float[] fractions, int[] pixels, int repeat) {187188short[] colorValues = getRenderColors(pixels);189return XRCreateRadialGradientPaintNative190(fractions, colorValues, fractions.length,191XDoubleToFixed(centerX),192XDoubleToFixed(centerY),193XDoubleToFixed(innerRadius),194XDoubleToFixed(outerRadius),195repeat);196}197198public void setGCClipRectangles(long gc, Region clip) {199XRSetClipNative(gc, clip.getLoX(), clip.getLoY(),200clip.getHiX(), clip.getHiY(),201clip.isRectangular() ? null : clip, true);202}203204public void setClipRectangles(int picture, Region clip) {205if (clip != null) {206XRSetClipNative(picture, clip.getLoX(), clip.getLoY(),207clip.getHiX(), clip.getHiY(),208clip.isRectangular() ? null : clip, false);209} else {210XRSetClipNative(picture, 0, 0, 32767, 32767, null, false);211}212}213214public void renderRectangles(int dst, byte op, XRColor color,215GrowableRectArray rects) {216XRenderRectanglesNative(dst, op,217(short) color.red, (short) color.green,218(short) color.blue, (short) color.alpha,219rects.getArray(), rects220.getSize());221}222223private static long[] getGlyphInfoPtrs(List<XRGlyphCacheEntry> cacheEntries) {224long[] glyphInfoPtrs = new long[cacheEntries.size()];225for (int i = 0; i < cacheEntries.size(); i++) {226glyphInfoPtrs[i] = cacheEntries.get(i).getGlyphInfoPtr();227}228return glyphInfoPtrs;229}230231public void XRenderAddGlyphs(int glyphSet, GlyphList gl,232List<XRGlyphCacheEntry> cacheEntries,233byte[] pixelData) {234long[] glyphInfoPtrs = getGlyphInfoPtrs(cacheEntries);235XRAddGlyphsNative(glyphSet, glyphInfoPtrs,236glyphInfoPtrs.length, pixelData, pixelData.length);237}238239public void XRenderFreeGlyphs(int glyphSet, int[] gids) {240XRFreeGlyphsNative(glyphSet, gids, gids.length);241}242243private static native void XRAddGlyphsNative(int glyphSet,244long[] glyphInfoPtrs,245int glyphCnt,246byte[] pixelData,247int pixelDataLength);248249private static native void XRFreeGlyphsNative(int glyphSet,250int[] gids, int idCnt);251252private static native void253XRenderCompositeTextNative(int op, int src, int dst,254int srcX, int srcY, long maskFormat,255int[] eltArray, int[] glyphIDs, int eltCnt,256int glyphCnt);257258public int XRenderCreateGlyphSet(int formatID) {259return XRenderCreateGlyphSetNative(getFormatPtr(formatID));260}261262private static native int XRenderCreateGlyphSetNative(long format);263264public void XRenderCompositeText(byte op, int src, int dst,265int maskFormatID,266int sx, int sy, int dx, int dy,267int glyphset, GrowableEltArray elts) {268269GrowableIntArray glyphs = elts.getGlyphs();270XRenderCompositeTextNative(op, src, dst, sx, sy, 0, elts.getArray(),271glyphs.getArray(), elts.getSize(),272glyphs.getSize());273}274275public void putMaskImage(int drawable, long gc, byte[] imageData,276int sx, int sy, int dx, int dy,277int width, int height, int maskOff,278int maskScan, float ea) {279putMaskNative(drawable, gc, imageData, sx, sy, dx, dy,280width, height, maskOff, maskScan, ea, MASK_XIMG);281}282283private static native void putMaskNative(int drawable, long gc,284byte[] imageData,285int sx, int sy, int dx, int dy,286int width, int height,287int maskOff, int maskScan,288float ea, long xImg);289290public void padBlit(byte op, int srcPict, int maskPict, int dstPict,291AffineTransform maskTrx, int maskWidth, int maskHeight,292int lastMaskWidth, int lastMaskHeight,293int sx, int sy, int dx, int dy, int w, int h) {294295padBlitNative(op, srcPict, maskPict, dstPict,296XDoubleToFixed(maskTrx.getScaleX()),297XDoubleToFixed(maskTrx.getShearX()),298XDoubleToFixed(maskTrx.getTranslateX()),299XDoubleToFixed(maskTrx.getShearY()),300XDoubleToFixed(maskTrx.getScaleY()),301XDoubleToFixed(maskTrx.getTranslateY()),302maskWidth, maskHeight, lastMaskWidth, lastMaskHeight,303sx, sy, dx, dy, w, h);304}305306private static native void padBlitNative(byte op, int srcPict,307int maskPict, int dstPict,308int m00, int m01, int m02,309int m10, int m11, int m12,310int maskWidth, int maskHeight,311int lastMaskWidth,312int lastMaskHeight,313int sx, int sy, int dx, int dy,314int w, int h);315316}317318319