2370N/A/*
2685N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2370N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2370N/A *
2370N/A * This code is free software; you can redistribute it and/or modify it
2370N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Oracle designates this
2370N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Oracle in the LICENSE file that accompanied this code.
2370N/A *
2370N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2370N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2370N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2370N/A * version 2 for more details (a copy is included in the LICENSE file that
2370N/A * accompanied this code).
2370N/A *
2370N/A * You should have received a copy of the GNU General Public License version
2370N/A * 2 along with this work; if not, write to the Free Software Foundation,
2370N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2370N/A *
2685N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2685N/A * or visit www.oracle.com if you need additional information or have any
2685N/A * questions.
2370N/A */
2370N/A
2370N/Apackage sun.java2d.jules;
2370N/A
2370N/Aimport java.util.*;
2370N/A
2370N/Apublic class TileWorker implements Runnable {
2370N/A final static int RASTERIZED_TILE_SYNC_GRANULARITY = 8;
2370N/A final ArrayList<JulesTile> rasterizedTileConsumerCache =
2370N/A new ArrayList<JulesTile>();
2370N/A final LinkedList<JulesTile> rasterizedBuffers = new LinkedList<JulesTile>();
2370N/A
2370N/A IdleTileCache tileCache;
2370N/A JulesAATileGenerator tileGenerator;
2370N/A int workerStartIndex;
2370N/A volatile int consumerPos = 0;
2370N/A
2370N/A /* Threading statistics */
2370N/A int mainThreadCnt = 0;
2370N/A int workerCnt = 0;
2370N/A int doubled = 0;
2370N/A
2370N/A public TileWorker(JulesAATileGenerator tileGenerator, int workerStartIndex, IdleTileCache tileCache) {
2370N/A this.tileGenerator = tileGenerator;
2370N/A this.workerStartIndex = workerStartIndex;
2370N/A this.tileCache = tileCache;
2370N/A }
2370N/A
2370N/A public void run() {
2370N/A ArrayList<JulesTile> tiles = new ArrayList<JulesTile>(16);
2370N/A
2370N/A for (int i = workerStartIndex; i < tileGenerator.getTileCount(); i++) {
2370N/A TileTrapContainer tile = tileGenerator.getTrapContainer(i);
2370N/A
2370N/A if (tile != null && tile.getTileAlpha() == 127) {
2370N/A JulesTile rasterizedTile =
2370N/A tileGenerator.rasterizeTile(i,
2370N/A tileCache.getIdleTileWorker(
2370N/A tileGenerator.getTileCount() - i - 1));
2370N/A tiles.add(rasterizedTile);
2370N/A
2370N/A if (tiles.size() > RASTERIZED_TILE_SYNC_GRANULARITY) {
2370N/A addRasterizedTiles(tiles);
2370N/A tiles.clear();
2370N/A }
2370N/A }
2370N/A
2370N/A i = Math.max(i, consumerPos + RASTERIZED_TILE_SYNC_GRANULARITY / 2);
2370N/A }
2370N/A addRasterizedTiles(tiles);
2370N/A
2370N/A tileCache.disposeRasterizerResources();
2370N/A }
2370N/A
2370N/A /**
2370N/A * Returns a rasterized tile for the specified tilePos,
2370N/A * or null if it isn't available.
2370N/A * Allowed caller: MaskBlit/Consumer-Thread
2370N/A */
2370N/A public JulesTile getPreRasterizedTile(int tilePos) {
2370N/A JulesTile tile = null;
2370N/A
2370N/A if (rasterizedTileConsumerCache.size() == 0 &&
2370N/A tilePos >= workerStartIndex)
2370N/A {
2370N/A synchronized (rasterizedBuffers) {
2370N/A rasterizedTileConsumerCache.addAll(rasterizedBuffers);
2370N/A rasterizedBuffers.clear();
2370N/A }
2370N/A }
2370N/A
2370N/A while (tile == null && rasterizedTileConsumerCache.size() > 0) {
2370N/A JulesTile t = rasterizedTileConsumerCache.get(0);
2370N/A
2370N/A if (t.getTilePos() > tilePos) {
2370N/A break;
2370N/A }
2370N/A
2370N/A if (t.getTilePos() < tilePos) {
2370N/A tileCache.releaseTile(t);
2370N/A doubled++;
2370N/A }
2370N/A
2370N/A if (t.getTilePos() <= tilePos) {
2370N/A rasterizedTileConsumerCache.remove(0);
2370N/A }
2370N/A
2370N/A if (t.getTilePos() == tilePos) {
2370N/A tile = t;
2370N/A }
2370N/A }
2370N/A
2370N/A if (tile == null) {
2370N/A mainThreadCnt++;
2370N/A
2370N/A // If there are no tiles left, tell the producer the current
2370N/A // position. This avoids producing tiles twice.
2370N/A consumerPos = tilePos;
2370N/A } else {
2370N/A workerCnt++;
2370N/A }
2370N/A
2370N/A return tile;
2370N/A }
2370N/A
2370N/A private void addRasterizedTiles(ArrayList<JulesTile> tiles) {
2370N/A synchronized (rasterizedBuffers) {
2370N/A rasterizedBuffers.addAll(tiles);
2370N/A }
2370N/A }
2370N/A
2370N/A /**
2370N/A * Releases cached tiles.
2370N/A * Allowed caller: MaskBlit/Consumer-Thread
2370N/A */
2370N/A public void disposeConsumerResources() {
2370N/A synchronized (rasterizedBuffers) {
2370N/A tileCache.releaseTiles(rasterizedBuffers);
2370N/A }
2370N/A
2370N/A tileCache.releaseTiles(rasterizedTileConsumerCache);
2370N/A }
2370N/A}