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.compiler.backend.sw.me; 27 28 import java.io.InputStreamReader; 29 import java.io.Reader; 30 import java.util.HashMap; 31 import java.util.HashSet; 32 import java.util.Map; 33 import java.util.Set; 34 import com.sun.scenario.effect.compiler.JSLParser; 35 import com.sun.scenario.effect.compiler.model.BaseType; 36 import com.sun.scenario.effect.compiler.model.Qualifier; 37 import com.sun.scenario.effect.compiler.model.Type; 38 import com.sun.scenario.effect.compiler.model.Variable; 39 import com.sun.scenario.effect.compiler.tree.FuncDef; 40 import com.sun.scenario.effect.compiler.tree.ProgramUnit; 41 import com.sun.scenario.effect.compiler.tree.TreeScanner; 42 import org.antlr.stringtemplate.StringTemplate; 43 import org.antlr.stringtemplate.StringTemplateGroup; 44 import org.antlr.stringtemplate.language.DefaultTemplateLexer; 45 46 /** 47 */ 48 public class MEBackend extends TreeScanner { 49 50 private final JSLParser parser; 51 private final String body; 52 53 public MEBackend(JSLParser parser, ProgramUnit program) { 54 // TODO: will be removed once we clean up static usage 55 resetStatics(); 56 57 this.parser = parser; 58 59 METreeScanner scanner = new METreeScanner(); 60 scanner.scan(program); 61 this.body = scanner.getResult(); 62 } 63 64 public static class GenCode { 65 public String javaCode; 66 public String nativeCode; 67 } 68 69 private static void appendGetRelease(StringBuilder get, 70 StringBuilder rel, 71 String ctype, 72 String cbufName, String jarrayName) 73 { 74 get.append("j" + ctype + " *" + cbufName + " = (j" + ctype + " *)"); 75 get.append("env->GetPrimitiveArrayCritical(" + jarrayName + ", 0);\n"); 76 get.append("if (" + cbufName + " == NULL) return;\n"); 77 rel.append("env->ReleasePrimitiveArrayCritical(" + jarrayName + ", " + cbufName + ", JNI_ABORT);\n"); 78 } 79 80 public final GenCode getGenCode(String effectName, 81 String peerName, 82 String interfaceName) 83 { 84 Map<String, Variable> vars = parser.getSymbolTable().getGlobalVariables(); 85 StringBuilder interfaceDecl = new StringBuilder(); 86 StringBuilder constants = new StringBuilder(); 87 StringBuilder samplers = new StringBuilder(); 88 StringBuilder srcRects = new StringBuilder(); 89 StringBuilder posDecls = new StringBuilder(); 90 StringBuilder pixInitY = new StringBuilder(); 91 StringBuilder pixInitX = new StringBuilder(); 92 StringBuilder posIncrY = new StringBuilder(); 93 StringBuilder posInitY = new StringBuilder(); 94 StringBuilder posIncrX = new StringBuilder(); 95 StringBuilder posInitX = new StringBuilder(); 96 StringBuilder jparams = new StringBuilder(); 97 StringBuilder jparamDecls = new StringBuilder(); 98 StringBuilder cparamDecls = new StringBuilder(); 99 StringBuilder arrayGet = new StringBuilder(); 100 StringBuilder arrayRelease = new StringBuilder(); 101 102 appendGetRelease(arrayGet, arrayRelease, "int", "dst", "dst_arr"); 103 104 // TODO: only need to declare these if pixcoord is referenced 105 // somewhere in the program... 106 pixInitY.append("float pixcoord_y = (float)dy;\n"); 107 pixInitX.append("float pixcoord_x = (float)dx;\n"); 108 109 for (Variable v : vars.values()) { 110 if (v.getQualifier() == Qualifier.CONST && v.getConstValue() == null) { 111 // this must be a special built-in variable (e.g. pos0); 112 // these are handled elsewhere, so just continue... 113 continue; 114 } 115 116 Type t = v.getType(); 117 BaseType bt = t.getBaseType(); 118 String vtype = bt.toString(); 119 String vname = v.getName(); 120 if (v.getQualifier() != null && bt != BaseType.SAMPLER) { 121 String accName = v.getAccessorName(); 122 if (v.isArray()) { 123 // TODO: we currently assume that param arrays will be 124 // stored in NIO Int/FloatBuffers, but the inner loop 125 // expects to access them as Java arrays, so we convert 126 // here; this is obviously bad for performance, so we need 127 // to come up with a better system soon... 128 String bufType = (bt == BaseType.FLOAT) ? 129 "FloatBuffer" : "IntBuffer"; 130 String bufName = vname + "_buf"; 131 String arrayName = vname + "_arr"; 132 constants.append(bufType + " " + bufName + " = " + accName + "();\n"); 133 constants.append(vtype + "[] " + arrayName); 134 constants.append(" = new " + vtype + "["); 135 constants.append(bufName + ".capacity()];\n"); 136 constants.append(bufName + ".get(" + arrayName + ");\n"); 137 jparams.append(",\n"); 138 jparams.append(arrayName); 139 jparamDecls.append(",\n"); 140 jparamDecls.append(vtype + "[] " + vname); 141 cparamDecls.append(",\n"); 142 cparamDecls.append("j" + vtype + "Array " + vname); 143 appendGetRelease(arrayGet, arrayRelease, vtype, arrayName, vname); 144 } else { 145 if (t.isVector()) { 146 String arrayName = vname + "_arr"; 147 constants.append(vtype + "[] " + arrayName + " = " + accName + "();\n"); 148 jparams.append(",\n"); 149 jparamDecls.append(",\n"); 150 cparamDecls.append(",\n"); 151 for (int i = 0; i < t.getNumFields(); i++) { 152 if (i > 0) { 153 jparams.append(", "); 154 jparamDecls.append(", "); 155 cparamDecls.append(", "); 156 } 157 String vn = vname + getSuffix(i); 158 jparams.append(arrayName + "[" + i + "]"); 159 jparamDecls.append(vtype + " " + vn); 160 cparamDecls.append("j" + vtype + " " + vn); 161 } 162 } else { 163 constants.append(vtype + " " + vname); 164 if (v.getQualifier() == Qualifier.CONST) { 165 constants.append(" = " + v.getConstValue()); 166 } else { 167 constants.append(" = " + accName + "()"); 168 } 169 constants.append(";\n"); 170 jparams.append(",\n"); 171 jparams.append(vname); 172 jparamDecls.append(",\n"); 173 jparamDecls.append(vtype + " " + vname); 174 cparamDecls.append(",\n"); 175 cparamDecls.append("j" + vtype + " " + vname); 176 } 177 } 178 } else if (v.getQualifier() == Qualifier.PARAM && bt == BaseType.SAMPLER) { 179 int i = v.getReg(); 180 if (t == Type.FSAMPLER) { 181 samplers.append("FloatMap src" + i + " = (FloatMap)getSamplerData(" + i + ");\n"); 182 samplers.append("int src" + i + "x = 0;\n"); 183 samplers.append("int src" + i + "y = 0;\n"); 184 samplers.append("int src" + i + "w = src" + i + ".getWidth();\n"); 185 samplers.append("int src" + i + "h = src" + i + ".getHeight();\n"); 186 samplers.append("int src" + i + "scan = src" + i + ".getWidth();\n"); 187 samplers.append("float[] " + vname + " = src" + i + ".getData();\n"); 188 189 // TODO: for now, assume [0,0,1,1] 190 srcRects.append("float[] src" + i + "Rect = new float[] {0,0,1,1};\n"); 191 192 jparams.append(",\n"); 193 jparams.append(vname); 194 195 jparamDecls.append(",\n"); 196 jparamDecls.append("float[] " + vname + "_arr"); 197 198 cparamDecls.append(",\n"); 199 cparamDecls.append("jfloatArray " + vname + "_arr"); 200 201 appendGetRelease(arrayGet, arrayRelease, "float", vname, vname + "_arr"); 202 } else { 203 samplers.append("BufferedImage src" + i + " = (BufferedImage)inputs[" + i + "].getImage();\n"); 204 samplers.append("int src" + i + "x = 0;\n"); 205 samplers.append("int src" + i + "y = 0;\n"); 206 samplers.append("int src" + i + "w = src" + i + ".getWidth();\n"); 207 samplers.append("int src" + i + "h = src" + i + ".getHeight();\n"); 208 samplers.append("int src" + i + "scan = src" + i + ".getWidth();\n"); 209 samplers.append("int[] " + vname + " =\n"); 210 samplers.append(" ((DataBufferInt)src" + i + ".getRaster().getDataBuffer()).getData();\n"); 211 212 samplers.append("Rectangle src" + i + "Bounds = new Rectangle("); 213 samplers.append("src" + i + "x, "); 214 samplers.append("src" + i + "y, "); 215 samplers.append("src" + i + "w, "); 216 samplers.append("src" + i + "h);\n"); 217 samplers.append("setInputBounds(" + i + ", inputs[" + i + "].getBounds());\n"); 218 samplers.append("setInputNativeBounds(" + i + ", src" + i + "Bounds);\n"); 219 220 if (t == Type.LSAMPLER) { 221 arrayGet.append("float " + vname + "_vals[4];\n"); 222 } 223 224 // the source rect decls need to come after all calls to 225 // setInput[Native]Bounds() for all inputs (since the 226 // getSourceRegion() impl may need to query the bounds of 227 // other inputs, as is the case in PhongLighting)... 228 srcRects.append("float[] src" + i + "Rect = getSourceRegion(" + i + ");\n"); 229 230 jparams.append(",\n"); 231 jparams.append(vname); 232 233 jparamDecls.append(",\n"); 234 jparamDecls.append("int[] " + vname + "_arr"); 235 236 cparamDecls.append(",\n"); 237 cparamDecls.append("jintArray " + vname + "_arr"); 238 239 appendGetRelease(arrayGet, arrayRelease, "int", vname, vname + "_arr"); 240 } 241 242 posDecls.append("float inc" + i + "_x = (src" + i + "Rect_x2 - src" + i + "Rect_x1) / dstw;\n"); 243 posDecls.append("float inc" + i + "_y = (src" + i + "Rect_y2 - src" + i + "Rect_y1) / dsth;\n"); 244 245 posInitY.append("float pos" + i + "_y = src" + i + "Rect_y1 + inc" + i + "_y*0.5f;\n"); 246 posInitX.append("float pos" + i + "_x = src" + i + "Rect_x1 + inc" + i + "_x*0.5f;\n"); 247 posIncrX.append("pos" + i + "_x += inc" + i + "_x;\n"); 248 posIncrY.append("pos" + i + "_y += inc" + i + "_y;\n"); 249 250 jparams.append(",\n"); 251 jparams.append("src" + i + "Rect[0], src" + i + "Rect[1],\n"); 252 jparams.append("src" + i + "Rect[2], src" + i + "Rect[3],\n"); 253 jparams.append("src" + i + "w, src" + i + "h, src" + i + "scan"); 254 255 jparamDecls.append(",\n"); 256 jparamDecls.append("float src" + i + "Rect_x1, float src" + i + "Rect_y1,\n"); 257 jparamDecls.append("float src" + i + "Rect_x2, float src" + i + "Rect_y2,\n"); 258 jparamDecls.append("int src" + i + "w, int src" + i + "h, int src" + i + "scan"); 259 260 cparamDecls.append(",\n"); 261 cparamDecls.append("jfloat src" + i + "Rect_x1, jfloat src" + i + "Rect_y1,\n"); 262 cparamDecls.append("jfloat src" + i + "Rect_x2, jfloat src" + i + "Rect_y2,\n"); 263 cparamDecls.append("jint src" + i + "w, jint src" + i + "h, jint src" + i + "scan"); 264 } 265 } 266 267 if (interfaceName != null) { 268 interfaceDecl.append("implements "+interfaceName); 269 } 270 271 Reader template = new InputStreamReader(getClass().getResourceAsStream("MEJavaGlue.stg")); 272 StringTemplateGroup group = new StringTemplateGroup(template, DefaultTemplateLexer.class); 273 StringTemplate jglue = group.getInstanceOf("glue"); 274 jglue.setAttribute("effectName", effectName); 275 jglue.setAttribute("peerName", peerName); 276 jglue.setAttribute("interfaceDecl", interfaceDecl.toString()); 277 jglue.setAttribute("usercode", usercode.toString()); 278 jglue.setAttribute("samplers", samplers.toString()); 279 jglue.setAttribute("srcRects", srcRects.toString()); 280 jglue.setAttribute("constants", constants.toString()); 281 jglue.setAttribute("params", jparams.toString()); 282 jglue.setAttribute("paramDecls", jparamDecls.toString()); 283 284 template = new InputStreamReader(getClass().getResourceAsStream("MENativeGlue.stg")); 285 group = new StringTemplateGroup(template, DefaultTemplateLexer.class); 286 StringTemplate cglue = group.getInstanceOf("glue"); 287 cglue.setAttribute("peerName", peerName); 288 cglue.setAttribute("jniName", peerName.replace("_", "_1")); 289 cglue.setAttribute("paramDecls", cparamDecls.toString()); 290 cglue.setAttribute("arrayGet", arrayGet.toString()); 291 cglue.setAttribute("arrayRelease", arrayRelease.toString()); 292 cglue.setAttribute("posDecls", posDecls.toString()); 293 cglue.setAttribute("pixInitY", pixInitY.toString()); 294 cglue.setAttribute("pixInitX", pixInitX.toString()); 295 cglue.setAttribute("posIncrY", posIncrY.toString()); 296 cglue.setAttribute("posInitY", posInitY.toString()); 297 cglue.setAttribute("posIncrX", posIncrX.toString()); 298 cglue.setAttribute("posInitX", posInitX.toString()); 299 cglue.setAttribute("body", body); 300 301 GenCode gen = new GenCode(); 302 gen.javaCode = jglue.toString(); 303 gen.nativeCode = cglue.toString(); 304 return gen; 305 } 306 307 // TODO: need better mechanism for querying fields 308 private static char[] fields = {'x', 'y', 'z', 'w'}; 309 public static String getSuffix(int i) { 310 return "_" + fields[i]; 311 } 312 313 static int getFieldIndex(char field) { 314 switch (field) { 315 case 'r': 316 case 'x': 317 return 0; 318 case 'g': 319 case 'y': 320 return 1; 321 case 'b': 322 case 'z': 323 return 2; 324 case 'a': 325 case 'w': 326 return 3; 327 default: 328 throw new InternalError(); 329 } 330 } 331 332 // TODO: these shouldn't be implemented as a static method 333 private static Map<String, FuncDef> funcDefs = new HashMap<String, FuncDef>(); 334 static void putFuncDef(FuncDef def) { 335 funcDefs.put(def.getFunction().getName(), def); 336 } 337 static FuncDef getFuncDef(String name) { 338 return funcDefs.get(name); 339 } 340 341 private static Set<String> resultVars = new HashSet<String>(); 342 static boolean isResultVarDeclared(String vname) { 343 return resultVars.contains(vname); 344 } 345 static void declareResultVar(String vname) { 346 resultVars.add(vname); 347 } 348 349 private static StringBuilder usercode = new StringBuilder(); 350 static void addGlueBlock(String block) { 351 usercode.append(block); 352 } 353 354 private static void resetStatics() { 355 funcDefs.clear(); 356 resultVars.clear(); 357 usercode = new StringBuilder(); 358 } 359 }