src/java.desktop/unix/classes/sun/java2d/jules/IdleTileCache.java
changeset 47919 66350f079368
parent 47918 a82c9f231737
parent 47880 bbd692ad4fa3
child 47920 52c9e8d2f8d9
child 48093 2cb07c3778e1
equal deleted inserted replaced
47918:a82c9f231737 47919:66350f079368
     1 /*
       
     2  * Copyright (c) 2010, 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.jules;
       
    27 
       
    28 import java.util.*;
       
    29 
       
    30 public class IdleTileCache {
       
    31     static final int IDLE_TILE_SYNC_GRANULARITY = 16;
       
    32     static final ArrayList<JulesTile> idleBuffers = new ArrayList<JulesTile>();
       
    33 
       
    34     ArrayList<JulesTile> idleTileWorkerCacheList = new ArrayList<JulesTile>();
       
    35     ArrayList<JulesTile> idleTileConsumerCacheList =
       
    36               new ArrayList<JulesTile>(IDLE_TILE_SYNC_GRANULARITY);
       
    37 
       
    38     /**
       
    39      * Return a cached Tile, if possible from cache.
       
    40      * Allowed caller: Rasterizer/Producer-Thread
       
    41      *
       
    42      * @param: maxCache - Specify the maximum amount of tiles needed
       
    43      */
       
    44     public JulesTile getIdleTileWorker(int maxCache) {
       
    45         /* Try to fetch idle tiles from the global cache list */
       
    46         if (idleTileWorkerCacheList.size() == 0) {
       
    47             idleTileWorkerCacheList.ensureCapacity(maxCache);
       
    48 
       
    49             synchronized (idleBuffers) {
       
    50                 for (int i = 0; i < maxCache && idleBuffers.size() > 0; i++) {
       
    51                     idleTileWorkerCacheList.add(
       
    52                             idleBuffers.remove(idleBuffers.size() - 1));
       
    53                 }
       
    54             }
       
    55         }
       
    56 
       
    57         if (idleTileWorkerCacheList.size() > 0) {
       
    58             return idleTileWorkerCacheList.remove(idleTileWorkerCacheList.size() - 1);
       
    59         }
       
    60 
       
    61         return new JulesTile();
       
    62     }
       
    63 
       
    64     /**
       
    65      * Release tile and allow it to be re-used by another thread. Allowed
       
    66      *  Allowed caller: MaskBlit/Consumer-Thread
       
    67      */
       
    68     public void releaseTile(JulesTile tile) {
       
    69         if (tile != null && tile.hasBuffer()) {
       
    70             idleTileConsumerCacheList.add(tile);
       
    71 
       
    72             if (idleTileConsumerCacheList.size() > IDLE_TILE_SYNC_GRANULARITY) {
       
    73                 synchronized (idleBuffers) {
       
    74                     idleBuffers.addAll(idleTileConsumerCacheList);
       
    75                 }
       
    76                 idleTileConsumerCacheList.clear();
       
    77             }
       
    78         }
       
    79     }
       
    80 
       
    81     /**
       
    82      * Releases thread-local tiles cached for use by the rasterizing thread.
       
    83      * Allowed caller: Rasterizer/Producer-Thread
       
    84      */
       
    85     public void disposeRasterizerResources() {
       
    86         releaseTiles(idleTileWorkerCacheList);
       
    87     }
       
    88 
       
    89     /**
       
    90      * Releases thread-local tiles cached for performance reasons. Allowed
       
    91      * Allowed caller: MaskBlit/Consumer-Thread
       
    92      */
       
    93     public void disposeConsumerResources() {
       
    94         releaseTiles(idleTileConsumerCacheList);
       
    95     }
       
    96 
       
    97     /**
       
    98      * Release a list of tiles and allow it to be re-used by another thread.
       
    99      * Thread safe.
       
   100      */
       
   101     public void releaseTiles(List<JulesTile> tileList) {
       
   102         if (tileList.size() > 0) {
       
   103             synchronized (idleBuffers) {
       
   104                 idleBuffers.addAll(tileList);
       
   105             }
       
   106             tileList.clear();
       
   107         }
       
   108     }
       
   109 }