Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/jules/JulesAATileGenerator.java
32288 views
/*1* Copyright (c) 2010, 2013, 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.jules;2627import java.awt.*;28import java.awt.geom.*;29import java.util.concurrent.*;30import sun.java2d.pipe.*;31import sun.java2d.xr.*;3233public class JulesAATileGenerator implements AATileGenerator {34/* Threading stuff */35final static ExecutorService rasterThreadPool =36Executors.newCachedThreadPool();37final static int CPU_CNT = Runtime.getRuntime().availableProcessors();3839final static boolean ENABLE_THREADING = false;40final static int THREAD_MIN = 16;41final static int THREAD_BEGIN = 16;4243IdleTileCache tileCache;44TileWorker worker;45boolean threaded = false;46int rasterTileCnt;4748/* Tiling */49final static int TILE_SIZE = 32;50final static int TILE_SIZE_FP = 32 << 16;51int left, right, top, bottom, width, height;52int leftFP, topFP;53int tileCnt, tilesX, tilesY;54int currTilePos = 0;55TrapezoidList traps;56TileTrapContainer[] tiledTrapArray;57JulesTile mainTile;5859public JulesAATileGenerator(Shape s, AffineTransform at, Region clip,60BasicStroke bs, boolean thin,61boolean normalize, int[] bbox) {62JulesPathBuf buf = new JulesPathBuf();6364if (bs == null) {65traps = buf.tesselateFill(s, at, clip);66} else {67traps = buf.tesselateStroke(s, bs, thin, false, true, at, clip);68}6970calculateArea(bbox);71bucketSortTraps();72calculateTypicalAlpha();7374threaded = ENABLE_THREADING &&75rasterTileCnt >= THREAD_MIN && CPU_CNT >= 2;76if (threaded) {77tileCache = new IdleTileCache();78worker = new TileWorker(this, THREAD_BEGIN, tileCache);79rasterThreadPool.execute(worker);80}8182mainTile = new JulesTile();83}8485private static native long86rasterizeTrapezoidsNative(long pixmanImagePtr, int[] traps,87int[] trapPos, int trapCnt,88byte[] buffer, int xOff, int yOff);8990private static native void freePixmanImgPtr(long pixmanImgPtr);9192private void calculateArea(int[] bbox) {93tilesX = 0;94tilesY = 0;95tileCnt = 0;96bbox[0] = 0;97bbox[1] = 0;98bbox[2] = 0;99bbox[3] = 0;100101if (traps.getSize() > 0) {102left = traps.getLeft();103right = traps.getRight();104top = traps.getTop();105bottom = traps.getBottom();106leftFP = left << 16;107topFP = top << 16;108109bbox[0] = left;110bbox[1] = top;111bbox[2] = right;112bbox[3] = bottom;113114width = right - left;115height = bottom - top;116117if (width > 0 && height > 0) {118tilesX = (int) Math.ceil(((double) width) / TILE_SIZE);119tilesY = (int) Math.ceil(((double) height) / TILE_SIZE);120tileCnt = tilesY * tilesX;121tiledTrapArray = new TileTrapContainer[tileCnt];122} else {123// If there is no area touched by the traps, don't124// render them.125traps.setSize(0);126}127}128}129130131private void bucketSortTraps() {132133for (int i = 0; i < traps.getSize(); i++) {134int top = traps.getTop(i) - XRUtils.XDoubleToFixed(this.top);135int bottom = traps.getBottom(i) - topFP;136int p1xLeft = traps.getP1XLeft(i) - leftFP;137int p2xLeft = traps.getP2XLeft(i) - leftFP;138int p1xRight = traps.getP1XRight(i) - leftFP;139int p2xRight = traps.getP2XRight(i) - leftFP;140141int minLeft = Math.min(p1xLeft, p2xLeft);142int maxRight = Math.max(p1xRight, p2xRight);143144maxRight = maxRight > 0 ? maxRight - 1 : maxRight;145bottom = bottom > 0 ? bottom - 1 : bottom;146147int startTileY = top / TILE_SIZE_FP;148int endTileY = bottom / TILE_SIZE_FP;149int startTileX = minLeft / TILE_SIZE_FP;150int endTileX = maxRight / TILE_SIZE_FP;151152for (int n = startTileY; n <= endTileY; n++) {153154for (int m = startTileX; m <= endTileX; m++) {155int trapArrayPos = n * tilesX + m;156TileTrapContainer trapTileList = tiledTrapArray[trapArrayPos];157if (trapTileList == null) {158trapTileList = new TileTrapContainer(new GrowableIntArray(1, 16));159tiledTrapArray[trapArrayPos] = trapTileList;160}161162trapTileList.getTraps().addInt(i);163}164}165}166}167168public void getAlpha(byte[] tileBuffer, int offset, int rowstride) {169JulesTile tile = null;170171if (threaded) {172tile = worker.getPreRasterizedTile(currTilePos);173}174175if (tile != null) {176System.arraycopy(tile.getImgBuffer(), 0,177tileBuffer, 0, tileBuffer.length);178tileCache.releaseTile(tile);179} else {180mainTile.setImgBuffer(tileBuffer);181rasterizeTile(currTilePos, mainTile);182}183184nextTile();185}186187public void calculateTypicalAlpha() {188rasterTileCnt = 0;189190for (int index = 0; index < tileCnt; index++) {191192TileTrapContainer trapCont = tiledTrapArray[index];193if (trapCont != null) {194GrowableIntArray trapList = trapCont.getTraps();195196int tileAlpha = 127;197if (trapList == null || trapList.getSize() == 0) {198tileAlpha = 0;199} else if (doTrapsCoverTile(trapList, index)) {200tileAlpha = 0xff;201}202203if (tileAlpha == 127 || tileAlpha == 0xff) {204rasterTileCnt++;205}206207trapCont.setTileAlpha(tileAlpha);208}209}210}211212/*213* Optimization for large fills. Foutunatly cairo does generate an y-sorted214* list of trapezoids. This makes it quite simple to check whether a tile is215* fully covered by traps by: - Checking whether the tile is fully covered by216* traps vertically (trap 2 starts where trap 1 ended) - Checking whether all217* traps cover the tile horizontally This also works, when a single tile218* coveres the whole tile.219*/220protected boolean doTrapsCoverTile(GrowableIntArray trapList, int tileIndex) {221222// Don't bother optimizing tiles with lots of traps, usually it won't223// succeed anyway.224if (trapList.getSize() > TILE_SIZE) {225return false;226}227228int tileStartX = getXPos(tileIndex) * TILE_SIZE_FP + leftFP;229int tileStartY = getYPos(tileIndex) * TILE_SIZE_FP + topFP;230int tileEndX = tileStartX + TILE_SIZE_FP;231int tileEndY = tileStartY + TILE_SIZE_FP;232233// Check whether first tile covers the beginning of the tile vertically234int firstTop = traps.getTop(trapList.getInt(0));235int firstBottom = traps.getBottom(trapList.getInt(0));236if (firstTop > tileStartY || firstBottom < tileStartY) {237return false;238}239240// Initialize lastBottom with top, in order to pass the checks for the241// first iteration242int lastBottom = firstTop;243244for (int i = 0; i < trapList.getSize(); i++) {245int trapPos = trapList.getInt(i);246if (traps.getP1XLeft(trapPos) > tileStartX ||247traps.getP2XLeft(trapPos) > tileStartX ||248traps.getP1XRight(trapPos) < tileEndX ||249traps.getP2XRight(trapPos) < tileEndX ||250traps.getTop(trapPos) != lastBottom)251{252return false;253}254lastBottom = traps.getBottom(trapPos);255}256257// When the last trap covered the tileEnd vertically, the tile is fully258// covered259return lastBottom >= tileEndY;260}261262public int getTypicalAlpha() {263if (tiledTrapArray[currTilePos] == null) {264return 0;265} else {266return tiledTrapArray[currTilePos].getTileAlpha();267}268}269270public void dispose() {271freePixmanImgPtr(mainTile.getPixmanImgPtr());272273if (threaded) {274tileCache.disposeConsumerResources();275worker.disposeConsumerResources();276}277}278279protected JulesTile rasterizeTile(int tileIndex, JulesTile tile) {280int tileOffsetX = left + getXPos(tileIndex) * TILE_SIZE;281int tileOffsetY = top + getYPos(tileIndex) * TILE_SIZE;282TileTrapContainer trapCont = tiledTrapArray[tileIndex];283GrowableIntArray trapList = trapCont.getTraps();284285if (trapCont.getTileAlpha() == 127) {286long pixmanImgPtr =287rasterizeTrapezoidsNative(tile.getPixmanImgPtr(),288traps.getTrapArray(),289trapList.getArray(),290trapList.getSize(),291tile.getImgBuffer(),292tileOffsetX, tileOffsetY);293tile.setPixmanImgPtr(pixmanImgPtr);294}295296tile.setTilePos(tileIndex);297return tile;298}299300protected int getXPos(int arrayPos) {301return arrayPos % tilesX;302}303304protected int getYPos(int arrayPos) {305return arrayPos / tilesX;306}307308public void nextTile() {309currTilePos++;310}311312public int getTileHeight() {313return TILE_SIZE;314}315316public int getTileWidth() {317return TILE_SIZE;318}319320public int getTileCount() {321return tileCnt;322}323324public TileTrapContainer getTrapContainer(int index) {325return tiledTrapArray[index];326}327}328329330