aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/DeviceViewAction.java
blob: 7d912bafe208b14a19542ec61572ed7b51138e4c (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
package com.android.ide.eclipse.gltrace;

import com.android.ddmlib.Client;
import com.android.ddmlib.ClientData;
import com.android.ide.eclipse.ddms.IClientAction;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import java.lang.reflect.InvocationTargetException;

public class DeviceViewAction implements IClientAction {
    private static final class StartTraceAction extends Action {
        private static final int LOCAL_FORWARDED_PORT = 6049;

        private Client mClient;

        public StartTraceAction() {
            super("Start OpenGL Trace");
            setImageDescriptor(GlTracePlugin.getImageDescriptor("/icons/connect.png")); //$NON-NLS-1$
            setClient(null);
        }

        public void setClient(Client c) {
            mClient = c;
            clientChanged();
        }

        private void clientChanged() {
            if (mClient == null) {
                setEnabled(false);
                return;
            }

            ClientData cd = mClient.getClientData();
            if (cd.hasFeature(ClientData.FEATURE_OPENGL_TRACING)) {
                setEnabled(true);
                setToolTipText("Trace OpenGL calls");
            } else {
                setEnabled(false);
                setToolTipText("Selected VM does not support tracing OpenGL calls");
            }
        }

        @Override
        public void run() {
            if (mClient == null) {
                return;
            }

            Shell shell = Display.getDefault().getActiveShell();
            GLTraceOptionsDialog dlg = new GLTraceOptionsDialog(shell, false,
                    mClient.getClientData().getClientDescription());
            if (dlg.open() != Window.OK) {
                return;
            }

            // start tracing on the client
            mClient.startOpenGlTracing();

            try {
                CollectTraceAction.setupForwarding(mClient.getDevice(), LOCAL_FORWARDED_PORT);
            } catch (Exception e) {
                MessageDialog.openError(shell, "Setup GL Trace",
                        "Error while setting up port forwarding: " + e.getMessage());
                return;
            }

            // wait for a few seconds for the client to start the trace server
            try {
                new ProgressMonitorDialog(shell).run(true, true, new IRunnableWithProgress() {
                    @Override
                    public void run(IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                        Thread.sleep(3000);
                    }
                });
            } catch (Exception e) {
            }

            // retrieve the trace from the device
            TraceOptions traceOptions = dlg.getTraceOptions();
            CollectTraceAction.startTracing(shell, traceOptions, LOCAL_FORWARDED_PORT);

            // inform the client that it doesn't need to be traced anymore
            mClient.stopOpenGlTracing();

            // remove port forwarding
            CollectTraceAction.disablePortForwarding(mClient.getDevice(), LOCAL_FORWARDED_PORT);

            // and finally open the editor to view the file
            CollectTraceAction.openInEditor(shell, traceOptions.traceDestination);
        }
    }

    private static final StartTraceAction sAction = new StartTraceAction();

    @Override
    public Action getAction() {
        return sAction;
    }

    @Override
    public void selectedClientChanged(Client c) {
        sAction.setClient(c);
    }
}