1 /*
   2  * Copyright (c) 2014, 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 com.sun.prism.impl;
  27 
  28 import com.sun.glass.ui.Pixels;
  29 import com.sun.prism.PixelSource;
  30 import java.lang.ref.WeakReference;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 
  34 /**
  35  * Base concrete implementation of the {@code PixelSource} interface which
  36  * manages {@link Pixels} objects in the state of being consumed (uploaded
  37  * to the screen usually), in flight in the queue of upload requests, and
  38  * idle waiting to be reused for temporary storage for future uploads.
  39  * All {@code Pixels} objects currently saved for reuse will all be the
  40  * same dimensions and scale which are tracked by calling the
  41  * {@link #validate(int, int, float) validate()} method.
  42  * <p>
  43  * At most we will need 3 sets of pixels:
  44  * One may be "in use", a hard reference stored in beingConsumed
  45  * Another may be "in the queue", hard ref stored in enqueued
  46  * A third may be needed to prepare new pixels while those two are in
  47  * transit.
  48  * If the third is filled with pixels and enqueued while the previously
  49  * mentioned two are still in their stages of use, then it will replace
  50  * the second object as the "enqueued" reference and the previously
  51  * enqueued object will then become itself the "third unused" reference.
  52  * If everything happens in lock step we will often have only one
  53  * set of pixels.  If the consumer/displayer gets slightly or occasionally
  54  * behind we might end up with two sets of pixels in play.  Only when things
  55  * get really bad with multiple deliveries enqueued during the processing
  56  * of a single earlier delivery will we end up with three sets of
  57  * {@code Pixels} objects in play.
  58  */
  59 public class QueuedPixelSource implements PixelSource {
  60     private volatile Pixels beingConsumed;
  61     private volatile Pixels enqueued;
  62     private final List<WeakReference<Pixels>> saved =
  63          new ArrayList<WeakReference<Pixels>>(3);
  64     private int pixelW;
  65     private int pixelH;
  66     private float pixelScale;
  67 
  68     @Override
  69     public synchronized Pixels getLatestPixels() {
  70         if (beingConsumed != null) {
  71             throw new IllegalStateException("already consuming pixels: "+beingConsumed);
  72         }
  73         if (enqueued != null) {
  74             beingConsumed = enqueued;
  75             enqueued = null;
  76         }
  77         return beingConsumed;
  78     }
  79 
  80     @Override
  81     public synchronized void doneWithPixels(Pixels used) {
  82         if (beingConsumed != used) {
  83             throw new IllegalStateException("wrong pixels buffer: "+used+" != "+beingConsumed);
  84         }
  85         beingConsumed = null;
  86     }
  87 
  88     @Override
  89     public synchronized void skipLatestPixels() {
  90         if (beingConsumed != null) {
  91             throw new IllegalStateException("cannot skip while processing: "+beingConsumed);
  92         }
  93         enqueued = null;
  94     }
  95 
  96     /**
  97      * Validates the saved pixels objects against the specified dimensions
  98      * and pixel scale and returns a boolean indicating if the pixel buffers
  99      * are still valid.
 100      * This method may free old saved buffers so that they will not be reused,
 101      * but it will leave the existing enqueued buffers alone until they are
 102      * eventually replaced.
 103      * 
 104      * @param w the intended width of the {@code Pixels} objects
 105      * @param h the intended height of the {@code Pixels} objects
 106      * @param scale the intended pixel scale of the {@code Pixels} objects
 107      * @return 
 108      */
 109     public synchronized boolean validate(int w, int h, float scale) {
 110         if (w != pixelW || h != pixelH || scale != pixelScale) {
 111             saved.clear();
 112             pixelW = w;
 113             pixelH = h;
 114             pixelScale = scale;
 115             return false;
 116         }
 117         return true;
 118     }
 119 
 120     /**
 121      * Return an unused Pixels object previously used with this
 122      * {@code PixelSource}, or null if there are none.
 123      * The caller should create a new {@code Pixels} object if
 124      * this method returns null and register it with a call
 125      * to {@link #enqueuePixels(com.sun.glass.ui.Pixels) enqueuePixels()}
 126      * when it is filled with data.
 127      * 
 128      * @return an unused {@code Pixels} object or null if the caller
 129      *         should create a new one
 130      */
 131     public synchronized Pixels getUnusedPixels() {
 132         int i = 0;
 133         while (i < saved.size()) {
 134             WeakReference<Pixels> ref = saved.get(i);
 135             Pixels p = ref.get();
 136             if (p == null) {
 137                 saved.remove(i);
 138                 continue;
 139             }
 140             if (p != beingConsumed && p != enqueued) {
 141                 assert(p.getWidthUnsafe() == pixelW &&
 142                        p.getHeightUnsafe() == pixelH &&
 143                        p.getScaleUnsafe() == pixelScale);
 144                 return p;
 145             }
 146             i++;
 147         }
 148         return null;
 149     }
 150 
 151     /**
 152      * Place the indicated {@code Pixels} object into the enqueued state,
 153      * replacing any other objects that are currently enqueued but not yet
 154      * being used by the consumer, and register the object for later
 155      * reuse.
 156      * 
 157      * @param pixels the {@code Pixels} object to be enqueued (and saved for reuse)
 158      */
 159     public synchronized void enqueuePixels(Pixels pixels) {
 160         if (pixels.getWidthUnsafe() != pixelW ||
 161             pixels.getHeightUnsafe() != pixelH ||
 162             pixels.getScaleUnsafe() != pixelScale)
 163         {
 164             throw new IllegalArgumentException("Pixels object: "+pixels+
 165                                                "does not match validated parameters: "+
 166                                                pixelW+" x "+pixelH+" @ "+pixelScale+"x");
 167         }
 168         enqueued = pixels;
 169         // Now make sure it is in our saved array since this could be
 170         // the first time we have seen this particular storage object.
 171         int i = 0;
 172         while (i < saved.size()) {
 173             WeakReference<Pixels> ref = saved.get(i);
 174             Pixels p = ref.get();
 175             if (p == null) {
 176                 saved.remove(i);
 177                 continue;
 178             }
 179             if (p == pixels) {
 180                 // Found it - already known.
 181                 return;
 182             }
 183             i++;
 184         }
 185         // Did not find it, this must be a new storage object.
 186         if (saved.size() >= 3) {
 187             throw new InternalError("too many Pixels objects saved");
 188         }
 189         saved.add(new WeakReference<Pixels>(pixels));
 190     }
 191 }