diff options
author | Bob Badour <bbadour@google.com> | 2020-05-06 14:17:12 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2020-05-06 14:17:12 +0000 |
commit | fc7cda06f54946e3a03ea008c1ba086d90aeef84 (patch) | |
tree | fd845444b59dfc72656b7781596e0b1a0662c4c7 /eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format | |
parent | 1aa104fb55f1b87494e4e2bc38e23416f486adfd (diff) | |
parent | 512ba8f80c0a2422f9e58a1401cb142db190a56c (diff) | |
download | sdk-fc7cda06f54946e3a03ea008c1ba086d90aeef84.tar.gz |
Merge "Revert "Remove unused project.""
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format')
3 files changed, 375 insertions, 0 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLAPISpec.java b/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLAPISpec.java new file mode 100644 index 000000000..57b587cb3 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLAPISpec.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.eclipse.gltrace.format; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Model a single GL API function call's specification. + */ +public class GLAPISpec { + private static final String GL_SPECS_FILE = "/entries.in"; //$NON-NLS-1$ + private static final String GLES2_ENTRIES_HEADER_V1 = + "# com.android.ide.eclipse.gltrace.glentries, v1"; //$NON-NLS-1$ + private static Map<String, GLAPISpec> sApiSpecs; + + private final String mGLFunction; + private final GLDataTypeSpec mReturnType; + private final List<GLDataTypeSpec> mArgs; + + private GLAPISpec(String glFunction, GLDataTypeSpec returnType, List<GLDataTypeSpec> args) { + mGLFunction = glFunction; + mReturnType = returnType; + mArgs = args; + } + + public String getFunction() { + return mGLFunction; + } + + public GLDataTypeSpec getReturnValue() { + return mReturnType; + } + + public List<GLDataTypeSpec> getArgs() { + return mArgs; + } + + public static Map<String, GLAPISpec> getSpecs() { + if (sApiSpecs == null) { + sApiSpecs = parseApiSpecs(GLAPISpec.class.getResourceAsStream(GL_SPECS_FILE)); + } + + return sApiSpecs; + } + + private static Map<String, GLAPISpec> parseApiSpecs(InputStream specFile) { + BufferedReader reader = new BufferedReader(new InputStreamReader(specFile)); + Map<String, GLAPISpec> specs = new HashMap<String, GLAPISpec>(400); + + try{ + String header = reader.readLine().trim(); + assert header.equals(GLES2_ENTRIES_HEADER_V1); + + String line; + while ((line = reader.readLine()) != null) { + // strip away the comments + int commentPos = line.indexOf('#'); + if (commentPos != -1) { + line = line.substring(0, commentPos); + } + line = line.trim(); + + // parse non empty lines + if (line.length() > 0) { + GLAPISpec spec = parseLine(line); + specs.put(spec.getFunction(), spec); + } + } + + specFile.close(); + } catch (IOException e) { + // this is unlikely to happen as the file is present within this .jar file. + // Even if it does happen, we just return whatever we've read till now. The net + // impact will be that the function calls will not be parsed fully and will just + // display the function name. + } + + return specs; + } + + /** + * Parse a GL API Specification entry from "/entries.in". Each line is of the format: + * {@code returnType, funcName, arg*}. This method is package private for testing. + */ + static GLAPISpec parseLine(String line) { + List<String> words = Arrays.asList(line.split(",")); + + String retType = words.get(0).trim(); + String func = words.get(1).trim(); + List<String> argDefinitions = words.subList(2, words.size()); + + List<GLDataTypeSpec> glArgs = new ArrayList<GLDataTypeSpec>(argDefinitions.size()/2); + for (String argDefn: argDefinitions) { + // an argDefn is something like: "const GLvoid* data" + argDefn = argDefn.trim(); + int lastSeparator = argDefn.lastIndexOf(' '); + if (lastSeparator == -1) { + // no space => a void type with no argument name + glArgs.add(new GLDataTypeSpec(argDefn, null)); + } else { + // everything upto the last space is the type + String type = argDefn.substring(0, lastSeparator); + + // and the last word is the variable name + String name = argDefn.substring(lastSeparator + 1); + glArgs.add(new GLDataTypeSpec(type, name)); + } + } + + return new GLAPISpec(func, new GLDataTypeSpec(retType, null), glArgs); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLDataTypeSpec.java b/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLDataTypeSpec.java new file mode 100644 index 000000000..de9c9b686 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLDataTypeSpec.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.eclipse.gltrace.format; + +import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType.Type; + +public class GLDataTypeSpec { + private final String mCType; + private final Type mType; + private final String mName; + private final boolean mIsPointer; + + public GLDataTypeSpec(String type, String name) { + mCType = type; + mName = name; + + mType = getDataType(type); + mIsPointer = type.contains("*"); //$NON-NLS-1$ + } + + private Type getDataType(String type) { + type = type.toLowerCase(); + + // We use type.contains() rather than type.equals since we are matching against + // the type name along with qualifiers. e.g. "void", "GLvoid" and "void*" should + // all be assigned the same type. + if (type.contains("boolean")) { //$NON-NLS-1$ + return Type.BOOL; + } else if (type.contains("enum")) { //$NON-NLS-1$ + return Type.ENUM; + } else if (type.contains("float") || type.contains("clampf")) { //$NON-NLS-1$ //$NON-NLS-2$ + return Type.FLOAT; + } else if (type.contains("void")) { //$NON-NLS-1$ + return Type.VOID; + } else if (type.contains("char")) { //$NON-NLS-1$ + return Type.CHAR; + } else { + // Matches all of the following types: + // glclampx, gluint, glint, glshort, glsizei, glfixed, + // glsizeiptr, glintptr, glbitfield, glfixed, glubyte. + // We might do custom formatting for these types in the future. + return Type.INT; + } + } + + public Type getDataType() { + return mType; + } + + public String getCType() { + return mCType; + } + + public String getArgName() { + return mName; + } + + public boolean isPointer() { + return mIsPointer; + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLMessageFormatter.java b/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLMessageFormatter.java new file mode 100644 index 000000000..6c34005ac --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/format/GLMessageFormatter.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.eclipse.gltrace.format; + +import com.android.ide.eclipse.gltrace.GLEnum; +import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage; +import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType; +import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType.Type; + +import java.util.List; +import java.util.Map; + +/** + * GLMessageFormatter is used to format and create a string representation for a {@link GLMessage}. + * It is provided with a specification for all GL Functions. Using this information, each + * GLMessage is parsed and formatted appropriately for display. + */ +public class GLMessageFormatter { + private static final String GL_NO_ERROR = "GL_NO_ERROR"; + private Map<String, GLAPISpec> mAPISpecs; + private enum DataTypeContext { CONTEXT_ARGUMENT, CONTEXT_RETURNVALUE }; + + public GLMessageFormatter(Map<String, GLAPISpec> specs) { + mAPISpecs = specs; + } + + public String formatGLMessage(GLMessage glMessage) { + GLAPISpec apiSpec = mAPISpecs.get(glMessage.getFunction().toString()); + if (apiSpec == null) { + return glMessage.getFunction().toString(); + } + + return formatCall(apiSpec, glMessage) + formatReturnValue(apiSpec, glMessage); + } + + private String formatReturnValue(GLAPISpec apiSpec, GLMessage glMessage) { + if (apiSpec.getReturnValue().getDataType() == Type.VOID) { + return ""; + } + + GLDataTypeSpec returnSpec = apiSpec.getReturnValue(); + return String.format(" = (%s) %s", returnSpec.getCType(), //$NON-NLS-1$ + formatDataValue(glMessage.getReturnValue(), + returnSpec, + DataTypeContext.CONTEXT_RETURNVALUE)); + } + + private String formatCall(GLAPISpec apiSpec, GLMessage glMessage) { + return String.format("%s(%s)", apiSpec.getFunction(), //$NON-NLS-1$ + formatArgs(glMessage, apiSpec.getArgs())); + } + + private String formatArgs(GLMessage glMessage, List<GLDataTypeSpec> argSpecs) { + int sizeEstimate = 10 + argSpecs.size() * 5; + StringBuilder sb = new StringBuilder(sizeEstimate); + + for (int i = 0; i < argSpecs.size(); i++) { + GLDataTypeSpec argSpec = argSpecs.get(i); + + if (argSpec.getDataType() == Type.VOID && !argSpec.isPointer()) { + sb.append("void"); //$NON-NLS-1$ + } else { + sb.append(argSpec.getArgName()); + sb.append(" = "); //$NON-NLS-1$ + sb.append(formatDataValue(glMessage.getArgs(i), + argSpec, + DataTypeContext.CONTEXT_ARGUMENT)); + } + + if (i < argSpecs.size() - 1) { + sb.append(", "); //$NON-NLS-1$ + } + } + + return sb.toString(); + } + + private String formatDataValue(DataType var, GLDataTypeSpec typeSpec, DataTypeContext context) { + if (typeSpec.isPointer()) { + return formatPointer(var, typeSpec.getDataType()); + } + + switch (typeSpec.getDataType()) { + case VOID: + return ""; + case BOOL: + return Boolean.toString(var.getBoolValue(0)); + case FLOAT: + return String.format("%f", var.getFloatValue(0)); //$NON-NLS-1$ + case INT: + return Integer.toString(var.getIntValue(0)); + case ENUM: + if (var.getIntValue(0) == 0 && context == DataTypeContext.CONTEXT_RETURNVALUE) { + return GL_NO_ERROR; + } else { + return GLEnum.valueOf(var.getIntValue(0)).toString(); + } + default: + return "(unknown type)"; //$NON-NLS-1$ + } + } + + private String formatPointer(DataType var, Type typeSpec) { + if (var.getType() != typeSpec && !isEnumTypeWithIntData(var, typeSpec)) { + // the type of the data in the message does not match expected specification. + // in such a case, just print the data as a pointer and don't try to interpret it. + if (var.getIntValueCount() > 0) { + return String.format("0x%x", var.getIntValue(0)); //$NON-NLS-1$ + } else { + return "0x??"; //$NON-NLS-1$ + } + } + + // Display as array if possible + switch (typeSpec) { + case BOOL: + return var.getBoolValueList().toString(); + case FLOAT: + return var.getFloatValueList().toString(); + case INT: + return var.getIntValueList().toString(); + case CHAR: + return var.getCharValueList().get(0).toStringUtf8(); + case ENUM: + List<Integer> vals = var.getIntValueList(); + StringBuilder sb = new StringBuilder(vals.size() * 5); + sb.append('['); + for (Integer v: vals) { + sb.append(GLEnum.valueOf(v.intValue())); + } + sb.append(']'); + return sb.toString(); + case VOID: + if (var.getRawBytesList().size() > 0) { + return String.format("[ %d bytes ]", var.getRawBytesList().get(0).size()); //$NON-NLS-1$ + } + return "[]"; //$NON-NLS-1$ + } + + // We have a pointer, but we don't have the data pointed to. + // Just format and return the pointer (points to device memory) + if (var.getIntValue(0) == 0) { + return "NULL"; //$NON-NLS-1$ + } else { + return String.format("0x%x", var.getIntValue(0)); //$NON-NLS-1$ + } + } + + private boolean isEnumTypeWithIntData(DataType var, Type typeSpec) { + return var.getType() == Type.INT && typeSpec == Type.ENUM; + } +} |