aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/views/detail/ShaderUniformDetailsProvider.java
blob: 2e3f7254b562280020c2ea11c4b734c7c1d1a3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2012 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.views.detail;

import com.android.ide.eclipse.gltrace.GLEnum;
import com.android.ide.eclipse.gltrace.state.GLCompositeProperty;
import com.android.ide.eclipse.gltrace.state.GLStateType;
import com.android.ide.eclipse.gltrace.state.IGLProperty;
import com.google.common.base.Joiner;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

import java.util.Collections;
import java.util.List;

public class ShaderUniformDetailsProvider implements IStateDetailProvider {
    private Text mTextControl;
    private static final Joiner JOINER = Joiner.on(", ");

    @Override
    public boolean isApplicable(IGLProperty state) {
        return getShaderUniformProperty(state) != null;
    }

    @Override
    public void createControl(Composite parent) {
        mTextControl = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI | SWT.WRAP);
        mTextControl.setEditable(false);
    }

    @Override
    public void disposeControl() {
    }

    @Override
    public Control getControl() {
        return mTextControl;
    }

    @Override
    public void updateControl(IGLProperty state) {
        IGLProperty uniform = getShaderUniformProperty(state);
        if (uniform instanceof GLCompositeProperty) {
            GLCompositeProperty uniformProperty = (GLCompositeProperty) uniform;
            IGLProperty nameProperty = uniformProperty.getProperty(GLStateType.UNIFORM_NAME);
            IGLProperty typeProperty = uniformProperty.getProperty(GLStateType.UNIFORM_TYPE);
            IGLProperty valueProperty = uniformProperty.getProperty(GLStateType.UNIFORM_VALUE);

            String name = (String) nameProperty.getValue();
            GLEnum type = (GLEnum) typeProperty.getValue();
            Object value = valueProperty.getValue();
            mTextControl.setText(formatUniform(name, type, value));
            mTextControl.setEnabled(true);
        } else {
            mTextControl.setText(""); //$NON-NLS-1$
            mTextControl.setEnabled(false);
        }
    }

    private String formatUniform(String name, GLEnum type, Object value) {
        String valueText;

        switch (type) {
            case GL_FLOAT:
            case GL_FLOAT_VEC2:
            case GL_FLOAT_VEC3:
            case GL_FLOAT_VEC4:
            case GL_INT:
            case GL_INT_VEC2:
            case GL_INT_VEC3:
            case GL_INT_VEC4:
            case GL_BOOL:
            case GL_BOOL_VEC2:
            case GL_BOOL_VEC3:
            case GL_BOOL_VEC4:
                valueText = formatVector(value);
                break;
            case GL_FLOAT_MAT2:
                valueText = formatMatrix(2, value);
                break;
            case GL_FLOAT_MAT3:
                valueText = formatMatrix(3, value);
                break;
            case GL_FLOAT_MAT4:
                valueText = formatMatrix(4, value);
                break;
            case GL_SAMPLER_2D:
            case GL_SAMPLER_CUBE:
            default:
                valueText = value.toString();
                break;
        }

        return String.format("%s %s = %s", type, name, valueText); //$NON-NLS-1$
    }

    private String formatVector(Object value) {
        if (value instanceof List<?>) {
            List<?> list = (List<?>) value;
            StringBuilder sb = new StringBuilder(list.size() * 4);
            sb.append('[');
            JOINER.appendTo(sb, list);
            sb.append(']');
            return sb.toString();
        }

        return value.toString();
    }

    private String formatMatrix(int dimension, Object value) {
        if (value instanceof List<?>) {
            List<?> list = (List<?>) value;
            if (list.size() != dimension * dimension) {
                // Uniforms can only be square matrices, so this scenario should
                // not occur.
                return formatVector(value);
            }

            StringBuilder sb = new StringBuilder(list.size() * 4);
            sb.append('[');
            sb.append('\n');
            for (int i = 0; i < dimension; i++) {
                sb.append("    "); //$NON-NLS-1$
                JOINER.appendTo(sb, list.subList(i * dimension, (i + 1) * dimension));
                sb.append('\n');
            }
            sb.append(']');
            return sb.toString();
        }

        return value.toString();
    }

    /**
     * Get the {@link GLStateType#PER_UNIFORM_STATE} property given a node in
     * the state hierarchy.
     */
    private IGLProperty getShaderUniformProperty(IGLProperty state) {
        if (state.getType() == GLStateType.PER_UNIFORM_STATE) {
            return state;
        }

        state = state.getParent();
        if (state != null && state.getType() == GLStateType.PER_UNIFORM_STATE) {
            return state;
        }

        return null;
    }

    @Override
    public List<IContributionItem> getToolBarItems() {
        return Collections.emptyList();
    }
}