Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/jules/TileWorker.java
32288 views
/*1* Copyright (c) 2010, 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.util.*;2829public class TileWorker implements Runnable {30final static int RASTERIZED_TILE_SYNC_GRANULARITY = 8;31final ArrayList<JulesTile> rasterizedTileConsumerCache =32new ArrayList<JulesTile>();33final LinkedList<JulesTile> rasterizedBuffers = new LinkedList<JulesTile>();3435IdleTileCache tileCache;36JulesAATileGenerator tileGenerator;37int workerStartIndex;38volatile int consumerPos = 0;3940/* Threading statistics */41int mainThreadCnt = 0;42int workerCnt = 0;43int doubled = 0;4445public TileWorker(JulesAATileGenerator tileGenerator, int workerStartIndex, IdleTileCache tileCache) {46this.tileGenerator = tileGenerator;47this.workerStartIndex = workerStartIndex;48this.tileCache = tileCache;49}5051public void run() {52ArrayList<JulesTile> tiles = new ArrayList<JulesTile>(16);5354for (int i = workerStartIndex; i < tileGenerator.getTileCount(); i++) {55TileTrapContainer tile = tileGenerator.getTrapContainer(i);5657if (tile != null && tile.getTileAlpha() == 127) {58JulesTile rasterizedTile =59tileGenerator.rasterizeTile(i,60tileCache.getIdleTileWorker(61tileGenerator.getTileCount() - i - 1));62tiles.add(rasterizedTile);6364if (tiles.size() > RASTERIZED_TILE_SYNC_GRANULARITY) {65addRasterizedTiles(tiles);66tiles.clear();67}68}6970i = Math.max(i, consumerPos + RASTERIZED_TILE_SYNC_GRANULARITY / 2);71}72addRasterizedTiles(tiles);7374tileCache.disposeRasterizerResources();75}7677/**78* Returns a rasterized tile for the specified tilePos,79* or null if it isn't available.80* Allowed caller: MaskBlit/Consumer-Thread81*/82public JulesTile getPreRasterizedTile(int tilePos) {83JulesTile tile = null;8485if (rasterizedTileConsumerCache.size() == 0 &&86tilePos >= workerStartIndex)87{88synchronized (rasterizedBuffers) {89rasterizedTileConsumerCache.addAll(rasterizedBuffers);90rasterizedBuffers.clear();91}92}9394while (tile == null && rasterizedTileConsumerCache.size() > 0) {95JulesTile t = rasterizedTileConsumerCache.get(0);9697if (t.getTilePos() > tilePos) {98break;99}100101if (t.getTilePos() < tilePos) {102tileCache.releaseTile(t);103doubled++;104}105106if (t.getTilePos() <= tilePos) {107rasterizedTileConsumerCache.remove(0);108}109110if (t.getTilePos() == tilePos) {111tile = t;112}113}114115if (tile == null) {116mainThreadCnt++;117118// If there are no tiles left, tell the producer the current119// position. This avoids producing tiles twice.120consumerPos = tilePos;121} else {122workerCnt++;123}124125return tile;126}127128private void addRasterizedTiles(ArrayList<JulesTile> tiles) {129synchronized (rasterizedBuffers) {130rasterizedBuffers.addAll(tiles);131}132}133134/**135* Releases cached tiles.136* Allowed caller: MaskBlit/Consumer-Thread137*/138public void disposeConsumerResources() {139synchronized (rasterizedBuffers) {140tileCache.releaseTiles(rasterizedBuffers);141}142143tileCache.releaseTiles(rasterizedTileConsumerCache);144}145}146147148