1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.gltrace.views.detail;
18 
19 import com.android.ide.eclipse.gltrace.GLEnum;
20 import com.android.ide.eclipse.gltrace.GLUtils;
21 import com.android.ide.eclipse.gltrace.state.GLCompositeProperty;
22 import com.android.ide.eclipse.gltrace.state.GLStateType;
23 import com.android.ide.eclipse.gltrace.state.IGLProperty;
24 
25 import org.eclipse.jface.action.IContributionItem;
26 import org.eclipse.jface.layout.GridDataFactory;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Combo;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
36 
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40 
41 public class VboDetailProvider implements IStateDetailProvider {
42     private static enum DisplayFormat {
43         GL_FLOAT,
44         GL_BYTE,
45         GL_UNSIGNED_BYTE,
46         GL_SHORT,
47         GL_UNSIGNED_SHORT,
48         GL_FIXED,
49     }
50 
51     private Composite mComposite;
52 
53     private Label mSizeLabel;
54     private Label mUsageLabel;
55     private Label mTypeLabel;
56     private Combo mDisplayFormatCombo;
57     private Text mTextControl;
58 
59     private byte[] mBufferData;
60 
61     @Override
isApplicable(IGLProperty state)62     public boolean isApplicable(IGLProperty state) {
63         return getVboProperty(state) != null;
64     }
65 
66     @Override
createControl(Composite parent)67     public void createControl(Composite parent) {
68         mComposite = new Composite(parent, SWT.NONE);
69         GridLayout layout = new GridLayout(2, false);
70         layout.marginWidth = layout.marginHeight = 0;
71         mComposite.setLayout(layout);
72         GridDataFactory.fillDefaults().grab(true, true).applyTo(mComposite);
73 
74         Label l = new Label(mComposite, SWT.NONE);
75         l.setText("Size: ");
76         GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(l);
77 
78         mSizeLabel = new Label(mComposite, SWT.NONE);
79         GridDataFactory.fillDefaults().grab(true, false).applyTo(mSizeLabel);
80 
81         l = new Label(mComposite, SWT.NONE);
82         l.setText("Usage: ");
83         GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(l);
84 
85         mUsageLabel = new Label(mComposite, SWT.NONE);
86         GridDataFactory.fillDefaults().grab(true, false).applyTo(mUsageLabel);
87 
88         l = new Label(mComposite, SWT.NONE);
89         l.setText("Type: ");
90         GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(l);
91 
92         mTypeLabel = new Label(mComposite, SWT.NONE);
93         GridDataFactory.fillDefaults().grab(true, false).applyTo(mTypeLabel);
94 
95         l = new Label(mComposite, SWT.NONE);
96         l.setText("Format Data As: ");
97         GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(l);
98 
99         DisplayFormat[] values = DisplayFormat.values();
100         List<String> formats = new ArrayList<String>(values.length);
101         for (DisplayFormat format: values) {
102             formats.add(format.name());
103         }
104 
105         mDisplayFormatCombo = new Combo(mComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
106         mDisplayFormatCombo.setItems(formats.toArray(new String[formats.size()]));
107         mDisplayFormatCombo.select(0);
108         mDisplayFormatCombo.addSelectionListener(new SelectionAdapter() {
109             @Override
110             public void widgetSelected(SelectionEvent e) {
111                 updateContents();
112             }
113         });
114         GridDataFactory.fillDefaults().grab(true, false).applyTo(mDisplayFormatCombo);
115 
116         mTextControl = new Text(mComposite, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI
117                                                    | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
118         GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(mTextControl);
119         mTextControl.setEditable(false);
120     }
121 
122     @Override
disposeControl()123     public void disposeControl() {
124     }
125 
126     @Override
getControl()127     public Control getControl() {
128         return mComposite;
129     }
130 
131     @Override
updateControl(IGLProperty state)132     public void updateControl(IGLProperty state) {
133         IGLProperty vbo = getVboProperty(state);
134         if (vbo instanceof GLCompositeProperty) {
135             GLCompositeProperty vboProperty = (GLCompositeProperty) vbo;
136 
137             IGLProperty sizeProperty = vboProperty.getProperty(GLStateType.BUFFER_SIZE);
138             mSizeLabel.setText(sizeProperty.getStringValue() + " bytes"); //$NON-NLS-1$
139 
140             IGLProperty usageProperty = vboProperty.getProperty(GLStateType.BUFFER_USAGE);
141             mUsageLabel.setText(usageProperty.getStringValue());
142 
143             IGLProperty typeProperty = vboProperty.getProperty(GLStateType.BUFFER_TYPE);
144             mTypeLabel.setText(typeProperty.getStringValue());
145 
146             IGLProperty dataProperty = vboProperty.getProperty(GLStateType.BUFFER_DATA);
147             mBufferData = (byte[]) dataProperty.getValue();
148         } else {
149             mBufferData = null;
150         }
151 
152         updateContents();
153     }
154 
updateContents()155     private void updateContents() {
156         if (mBufferData != null) {
157             mTextControl.setText(GLUtils.formatData(mBufferData,
158                     GLEnum.valueOf(mDisplayFormatCombo.getText())));
159             mTextControl.setEnabled(true);
160             mDisplayFormatCombo.setEnabled(true);
161         } else {
162             mTextControl.setText("");
163             mTextControl.setEnabled(false);
164             mDisplayFormatCombo.setEnabled(false);
165         }
166     }
167 
168     @Override
getToolBarItems()169     public List<IContributionItem> getToolBarItems() {
170         return Collections.emptyList();
171     }
172 
173     /**
174      * Get the {@link GLStateType#VBO_COMPOSITE} property given a node in
175      * the state hierarchy.
176      */
getVboProperty(IGLProperty state)177     private IGLProperty getVboProperty(IGLProperty state) {
178         if (state.getType() == GLStateType.VBO_COMPOSITE) {
179             return state;
180         }
181 
182         state = state.getParent();
183         if (state != null && state.getType() == GLStateType.VBO_COMPOSITE) {
184             return state;
185         }
186 
187         return null;
188     }
189 }
190