1 /*
   2  * Copyright (c) 2011, 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.impl.prism;
  27 
  28 import com.sun.javafx.geom.Rectangle;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.prism.Graphics;
  31 import com.sun.prism.Texture;
  32 import com.sun.scenario.effect.Effect;
  33 import com.sun.scenario.effect.FilterContext;
  34 import com.sun.scenario.effect.ImageData;
  35 import com.sun.scenario.effect.Reflection;
  36 import com.sun.scenario.effect.impl.EffectPeer;
  37 import com.sun.scenario.effect.impl.Renderer;
  38 import com.sun.scenario.effect.impl.state.RenderState;
  39 
  40 public class PrReflectionPeer extends EffectPeer {
  41 
  42     public PrReflectionPeer(FilterContext fctx, Renderer r, String uniqueName) {
  43         super(fctx, r, uniqueName);
  44     }
  45 
  46     @Override
  47     public ImageData filter(Effect effect,
  48                             RenderState rstate,
  49                             BaseTransform transform,
  50                             Rectangle outputClip,
  51                             ImageData... inputs)
  52     {
  53         FilterContext fctx = getFilterContext();
  54         Reflection reflect = (Reflection)effect;
  55 
  56         Rectangle inputbounds = inputs[0].getUntransformedBounds();
  57         int srcW = inputbounds.width;
  58         int srcH = inputbounds.height;
  59         float refY = srcH + reflect.getTopOffset();
  60         float refH = reflect.getFraction() * srcH;
  61         int irefY1 = (int) Math.floor(refY);
  62         int irefY2 = (int) Math.ceil(refY + refH);
  63         int irefH = irefY2 - irefY1;
  64 
  65         int dstH = (irefY2 > srcH) ? irefY2 : srcH;
  66         // RT-27389: take clipping into account...
  67         PrDrawable dst = (PrDrawable)getRenderer().getCompatibleImage(srcW, dstH);
  68         if (!inputs[0].validate(fctx) || dst == null) {
  69             return new ImageData(fctx, null, inputbounds);
  70         }
  71         PrDrawable src = (PrDrawable)inputs[0].getUntransformedImage();
  72         Texture srctex = src.getTextureObject();
  73 
  74         Graphics gdst = dst.createGraphics();
  75         gdst.transform(inputs[0].getTransform());
  76         float sx1 = 0f;
  77         float sy1 = srcH-irefH;
  78         float sx2 = srcW;
  79         float sy2 = srcH;
  80         gdst.drawTextureVO(srctex,
  81                            reflect.getBottomOpacity(),
  82                            reflect.getTopOpacity(),
  83                            0, irefY2, srcW, irefY1,
  84                            sx1, sy1, sx2, sy2);
  85         gdst.drawTexture(srctex, 0, 0, srcW, srcH);
  86 
  87         Rectangle newbounds =
  88             new Rectangle(inputbounds.x, inputbounds.y, srcW, dstH);
  89         return new ImageData(fctx, dst, newbounds);
  90     }
  91 }