1bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root/*
2bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * Copyright (C) 2009 The Android Open Source Project
3bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root *
4bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * Licensed under the Apache License, Version 2.0 (the "License");
5bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * you may not use this file except in compliance with the License.
6bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * You may obtain a copy of the License at
7bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root *
8bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root *      http://www.apache.org/licenses/LICENSE-2.0
9bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root *
10bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * Unless required by applicable law or agreed to in writing, software
11bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * distributed under the License is distributed on an "AS IS" BASIS,
12bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * See the License for the specific language governing permissions and
14bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root * limitations under the License.
15bd393b7f39a71a13893aea8b64474b0b07ebe0ddKenny Root */
16412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
17412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichimport java.io.BufferedReader;
18412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichimport java.io.File;
19412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichimport java.io.FileOutputStream;
20412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichimport java.io.FileReader;
21412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichimport java.io.IOException;
22412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichimport java.io.PrintStream;
23412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
24412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevichpublic class GenerateGLES {
25412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
26412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich    static void copy(String filename, PrintStream out) throws IOException {
27412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        BufferedReader br = new BufferedReader(new FileReader(filename));
28412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        String s;
29412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        while ((s = br.readLine()) != null) {
30412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            out.println(s);
31412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        }
32412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich    }
33412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
34412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich    private static void emit(GLESCodeEmitter emitter,
35412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                             BufferedReader specReader,
36412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                             PrintStream glStream,
37412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                             PrintStream cStream) throws Exception {
38412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        String s = null;
39412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        while ((s = specReader.readLine()) != null) {
40412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            if (s.trim().startsWith("//")) {
41412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                continue;
42412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            }
43412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
44412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            CFunc cfunc = CFunc.parseCFunc(s);
45412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            String fname = cfunc.getName();
46412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            String stubRoot = "stubs/gles11/" + fname;
47412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            String javaPath = stubRoot + ".java";
48412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            File f = new File(javaPath);
49412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            if (f.exists()) {
50412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                System.out.println("Special-casing function " + fname);
51412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                copy(javaPath, glStream);
52412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                copy(stubRoot + ".cpp", cStream);
53412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
54412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                // Register native function names
55412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                // This should be improved to require fewer discrete files
56412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                String filename = stubRoot + ".nativeReg";
57412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                BufferedReader br =
58412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                    new BufferedReader(new FileReader(filename));
59412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                String nfunc;
60412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                while ((nfunc = br.readLine()) != null) {
61412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                    emitter.addNativeRegistration(nfunc);
62412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                }
63412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            } else {
64412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                emitter.emitCode(cfunc, s);
65412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            }
66412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        }
67412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich    }
68412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
69412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich    public static void main(String[] args) throws Exception {
70412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        int aidx = 0;
71412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        while ((aidx < args.length) && (args[aidx].charAt(0) == '-')) {
72412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            switch (args[aidx].charAt(1)) {
73412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            default:
74412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                System.err.println("Unknown flag: " + args[aidx]);
75412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                System.exit(1);
76412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            }
77412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
78412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            aidx++;
79412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        }
80412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
81412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        BufferedReader checksReader =
82412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            new BufferedReader(new FileReader("specs/gles11/checks.spec"));
83412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        ParameterChecker checker = new ParameterChecker(checksReader);
84412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich
85412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        // Generate files
86412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        for(String suffix: new String[] {"GLES10", "GLES10Ext",
8763ed38dc52b27c5b39b11717d0c7736e1756db42Jesse Hall                "GLES11", "GLES11Ext", "GLES20", "GLES30"})
88412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        {
89412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            BufferedReader spec11Reader =
90412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                new BufferedReader(new FileReader("specs/gles11/"
91412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                        + suffix + ".spec"));
92412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            String gl11Filename = "android/opengl/" + suffix + ".java";
93412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            String gl11cFilename = "android_opengl_" + suffix + ".cpp";
94412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            PrintStream gl11Stream =
95412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                new PrintStream(new FileOutputStream("out/" + gl11Filename));
96412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            PrintStream gl11cStream =
97412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                new PrintStream(new FileOutputStream("out/" + gl11cFilename));
98412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            copy("stubs/gles11/" + suffix + "Header.java-if", gl11Stream);
99412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            copy("stubs/gles11/" + suffix + "cHeader.cpp", gl11cStream);
100bf13ba5e77804ca7466acb7100cdaf7c14cc0fb7Mathias Agopian            copy("stubs/gles11/common.cpp", gl11cStream);
101412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            GLESCodeEmitter emitter = new GLESCodeEmitter(
102412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                    "android/opengl/" + suffix,
103412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                    checker, gl11Stream, gl11cStream);
104412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            emit(emitter, spec11Reader, gl11Stream, gl11cStream);
105412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            emitter.emitNativeRegistration("register_android_opengl_jni_"
106412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich                    + suffix);
107412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            gl11Stream.println("}");
108412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            gl11Stream.close();
109412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich            gl11cStream.close();
110412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich        }
111412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich    }
112412f38f270b71bbc3214362eb58687cbf26d6df2Jack Palevich}
113