1 /*
   2  * Copyright (c) 2008, 2013, 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.scenario.effect;
  27 
  28 import com.sun.javafx.geom.Point2D;
  29 import com.sun.javafx.geom.BaseBounds;
  30 import com.sun.javafx.geom.DirtyRegionContainer;
  31 import com.sun.javafx.geom.DirtyRegionPool;
  32 import com.sun.javafx.geom.RectBounds;
  33 import com.sun.javafx.geom.Rectangle;
  34 import com.sun.javafx.geom.transform.BaseTransform;
  35 
  36 /**
  37  * An effect that renders a rectangular region that is filled ("flooded")
  38  * with the given paint.  This is equivalent to rendering a
  39  * filled rectangle into an image and using an {@code Identity} effect,
  40  * except that it is more convenient and potentially much more efficient.
  41  */
  42 public class Flood extends CoreEffect {
  43 
  44     private Object paint;
  45     private RectBounds bounds = new RectBounds();
  46 
  47     /**
  48      * Constructs a new {@code Flood} effect using the given platform-specific
  49      * paint object with an empty bounds for the flood region.
  50      *
  51      * @param paint the platform-specific paint object used to flood the region
  52      * @throws IllegalArgumentException if {@code paint} is null
  53      */
  54     public Flood(Object paint) {
  55         if (paint == null) {
  56             throw new IllegalArgumentException("Paint must be non-null");
  57         }
  58         this.paint = paint;
  59         updatePeerKey("Flood");
  60     }
  61 
  62     /**
  63      * Constructs a new {@code Flood} effect using the given platform-specific
  64      * paint object to cover the indicated rectangular {@code bounds}.
  65      *
  66      * @param paint the platform-specific paint object used to flood the region
  67      * @param bounds the rectangular area to cover
  68      * @throws IllegalArgumentException if either {@code paint} or
  69      *                                  {@code bounds} is null
  70      */
  71     public Flood(Object paint, RectBounds bounds) {
  72         this(paint);
  73         if (bounds == null) {
  74             throw new IllegalArgumentException("Bounds must be non-null");
  75         }
  76         this.bounds.setBounds(bounds);
  77     }
  78 
  79     /**
  80      * Returns the platform-specific paint object used to flood the region.
  81      *
  82      * @return the flood paint
  83      */
  84     public Object getPaint() {
  85         return paint;
  86     }
  87 
  88     /**
  89      * Sets the platform-specific paint object used to flood the region.
  90      *
  91      * @param paint the flood paint
  92      * @throws IllegalArgumentException if {@code paint} is null
  93      */
  94     public void setPaint(Object paint) {
  95         if (paint == null) {
  96             throw new IllegalArgumentException("Paint must be non-null");
  97         }
  98         Object old = this.paint;
  99         this.paint = paint;
 100     }
 101 
 102     public RectBounds getFloodBounds() {
 103         return new RectBounds(bounds);
 104     }
 105 
 106     public void setFloodBounds(RectBounds bounds) {
 107         if (bounds == null) {
 108             throw new IllegalArgumentException("Bounds must be non-null");
 109         }
 110         RectBounds old = new RectBounds(this.bounds);
 111         this.bounds.setBounds(bounds);
 112     }
 113 
 114     @Override
 115     public BaseBounds getBounds(BaseTransform transform,
 116                               Effect defaultInput)
 117     {
 118         return transformBounds(transform, bounds);
 119     }
 120 
 121     /**
 122      * Transform the specified point {@code p} from the coordinate space
 123      * of the primary content input to the coordinate space of the effect
 124      * output.
 125      * In essence, this method asks the question "Which output coordinate
 126      * is most affected by the data at the specified coordinate in the
 127      * primary source input?"
 128      * <p>
 129      * Since no source input is used, any output coordinate is equally
 130      * affected, or unaffected, by any source coordinate so an undefined
 131      * point {@code (NaN, NaN)} is returned.
 132      *
 133      * @param p the point in the coordinate space of the primary content
 134      *          input to be transformed
 135      * @param defaultInput the default input {@code Effect} to be used in
 136      *                     all cases where a filter has a null input
 137      * @return the undefined point {@code (NaN, NaN)}
 138      */
 139     @Override
 140     public Point2D transform(Point2D p, Effect defaultInput) {
 141         return new Point2D(Float.NaN, Float.NaN);
 142     }
 143 
 144     /**
 145      * Transform the specified point {@code p} from the coordinate space
 146      * of the output of the effect into the coordinate space of the
 147      * primary content input.
 148      * In essence, this method asks the question "Which source coordinate
 149      * contributes most to the definition of the output at the specified
 150      * coordinate?"
 151      * <p>
 152      * Since the Flood effect does not use any source data it returns
 153      * an undefined coordinate {@code (NaN, NaN)} for all requests.
 154      *
 155      * @param p the point in the coordinate space of the result output
 156      *          to be transformed
 157      * @param defaultInput the default input {@code Effect} to be used in
 158      *                     all cases where a filter has a null input
 159      * @return the undefined point {@code (NaN, NaN)}
 160      */
 161     @Override
 162     public Point2D untransform(Point2D p, Effect defaultInput) {
 163         return new Point2D(Float.NaN, Float.NaN);
 164     }
 165 
 166     @Override
 167     protected Rectangle getInputClip(int inputIndex,
 168                                      BaseTransform transform,
 169                                      Rectangle outputClip)
 170     {
 171         // TODO: Intersect with the flood bounds?
 172         return outputClip;
 173     }
 174 
 175     @Override
 176     public boolean reducesOpaquePixels() {
 177         return true;
 178     }
 179 
 180     @Override
 181     public DirtyRegionContainer getDirtyRegions(Effect defaultInput, DirtyRegionPool regionPool) {
 182         DirtyRegionContainer drc = regionPool.checkOut();
 183         drc.reset();
 184         return drc;
 185     }
 186 }