1 /*
   2  * Copyright (c) 2009, 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.ps;
  27 
  28 import com.sun.javafx.geom.Rectangle;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.prism.Texture;
  31 import com.sun.prism.ps.Shader;
  32 import com.sun.prism.ps.ShaderGraphics;
  33 import com.sun.scenario.effect.FilterContext;
  34 import com.sun.scenario.effect.Filterable;
  35 import com.sun.scenario.effect.ImageData;
  36 import com.sun.scenario.effect.impl.Renderer;
  37 import com.sun.scenario.effect.impl.prism.PrTexture;
  38 
  39 public abstract class PPSOneSamplerPeer extends PPSEffectPeer {
  40 
  41     private Shader shader;
  42 
  43     protected PPSOneSamplerPeer(FilterContext fctx, Renderer r, String shaderName) {
  44         super(fctx, r, shaderName);
  45     }
  46 
  47     @Override
  48     public void dispose() {
  49         if (shader != null) {
  50             shader.dispose();
  51         }
  52     }
  53 
  54     @Override
  55     ImageData filterImpl(ImageData... inputs) {
  56         Filterable srcF = inputs[0].getUntransformedImage();
  57         final PrTexture srcTex = (PrTexture) srcF;
  58         final Rectangle srcBounds = inputs[0].getUntransformedBounds();
  59         final Rectangle dstBounds = getDestBounds();
  60         final int dstw = dstBounds.width;
  61         final int dsth = dstBounds.height;
  62 
  63         PPSRenderer renderer = getRenderer();
  64         PPSDrawable dst = renderer.getCompatibleImage(dstw, dsth);
  65         if (dst == null) {
  66             renderer.markLost();
  67             return new ImageData(getFilterContext(), dst, dstBounds);
  68         }
  69         setDestNativeBounds(dst.getPhysicalWidth(), dst.getPhysicalHeight());
  70 
  71         BaseTransform srcTransform = inputs[0].getTransform();
  72         setInputBounds(0, srcBounds);
  73         setInputTransform(0, srcTransform);
  74         setInputNativeBounds(0, srcTex.getNativeBounds());
  75         float srcRect[] = new float[8];
  76         int srcCoords = getTextureCoordinates(0, srcRect,
  77                                               srcBounds.x, srcBounds.y,
  78                                               srcF.getPhysicalWidth(),
  79                                               srcF.getPhysicalHeight(),
  80                                               dstBounds,
  81                                               srcTransform);
  82 
  83         ShaderGraphics g = dst.createGraphics();
  84         if (g == null) {
  85             renderer.markLost();
  86             return new ImageData(getFilterContext(), dst, dstBounds);
  87         }
  88         if (shader == null) {
  89             shader = createShader();
  90         }
  91         if (shader == null || !shader.isValid()) {
  92             renderer.markLost();
  93             return new ImageData(getFilterContext(), dst, dstBounds);
  94         }
  95         g.setExternalShader(shader);
  96         updateShader(shader);
  97 
  98         float dx1 = 0;
  99         float dy1 = 0;
 100         float dx2 = dstw;
 101         float dy2 = dsth;
 102 
 103         // take the texture content origin into account
 104         Texture ptex = srcTex.getTextureObject();
 105         if (ptex == null) {
 106             renderer.markLost();
 107             return new ImageData(getFilterContext(), dst, dstBounds);
 108         }
 109         float txoff = ((float)ptex.getContentX()) / ptex.getPhysicalWidth();
 110         float tyoff = ((float)ptex.getContentY()) / ptex.getPhysicalHeight();
 111         float tx11 = txoff + srcRect[0];
 112         float ty11 = tyoff + srcRect[1];
 113         float tx22 = txoff + srcRect[2];
 114         float ty22 = tyoff + srcRect[3];
 115         if (srcCoords < 8) {
 116             g.drawTextureRaw(ptex,
 117                              dx1, dy1, dx2, dy2,
 118                              tx11, ty11, tx22, ty22);
 119         } else {
 120             float tx21 = txoff + srcRect[4];
 121             float ty21 = tyoff + srcRect[5];
 122             float tx12 = txoff + srcRect[6];
 123             float ty12 = tyoff + srcRect[7];
 124 
 125             g.drawMappedTextureRaw(ptex,
 126                                    dx1, dy1, dx2, dy2,
 127                                    tx11, ty11, tx21, ty21,
 128                                    tx12, ty12, tx22, ty22);
 129         }
 130         g.setExternalShader(null);
 131 
 132         return new ImageData(getFilterContext(), dst, dstBounds);
 133     }
 134 }