Home | History | Annotate | Download | only in gltrace
      1 package com.android.ide.eclipse.gltrace;
      2 
      3 import com.android.ddmlib.Client;
      4 import com.android.ddmlib.ClientData;
      5 import com.android.ide.eclipse.ddms.IClientAction;
      6 
      7 import org.eclipse.core.runtime.IProgressMonitor;
      8 import org.eclipse.jface.action.Action;
      9 import org.eclipse.jface.dialogs.MessageDialog;
     10 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
     11 import org.eclipse.jface.operation.IRunnableWithProgress;
     12 import org.eclipse.jface.window.Window;
     13 import org.eclipse.swt.widgets.Display;
     14 import org.eclipse.swt.widgets.Shell;
     15 
     16 import java.lang.reflect.InvocationTargetException;
     17 
     18 public class DeviceViewAction implements IClientAction {
     19     private static final class StartTraceAction extends Action {
     20         private static final int LOCAL_FORWARDED_PORT = 6049;
     21 
     22         private Client mClient;
     23 
     24         public StartTraceAction() {
     25             super("Start OpenGL Trace");
     26             setImageDescriptor(GlTracePlugin.getImageDescriptor("/icons/connect.png")); //$NON-NLS-1$
     27             setClient(null);
     28         }
     29 
     30         public void setClient(Client c) {
     31             mClient = c;
     32             clientChanged();
     33         }
     34 
     35         private void clientChanged() {
     36             if (mClient == null) {
     37                 setEnabled(false);
     38                 return;
     39             }
     40 
     41             ClientData cd = mClient.getClientData();
     42             if (cd.hasFeature(ClientData.FEATURE_OPENGL_TRACING)) {
     43                 setEnabled(true);
     44                 setToolTipText("Trace OpenGL calls");
     45             } else {
     46                 setEnabled(false);
     47                 setToolTipText("Selected VM does not support tracing OpenGL calls");
     48             }
     49         }
     50 
     51         @Override
     52         public void run() {
     53             if (mClient == null) {
     54                 return;
     55             }
     56 
     57             Shell shell = Display.getDefault().getActiveShell();
     58             GLTraceOptionsDialog dlg = new GLTraceOptionsDialog(shell, false,
     59                     mClient.getClientData().getClientDescription());
     60             if (dlg.open() != Window.OK) {
     61                 return;
     62             }
     63 
     64             // start tracing on the client
     65             mClient.startOpenGlTracing();
     66 
     67             try {
     68                 CollectTraceAction.setupForwarding(mClient.getDevice(), LOCAL_FORWARDED_PORT);
     69             } catch (Exception e) {
     70                 MessageDialog.openError(shell, "Setup GL Trace",
     71                         "Error while setting up port forwarding: " + e.getMessage());
     72                 return;
     73             }
     74 
     75             // wait for a few seconds for the client to start the trace server
     76             try {
     77                 new ProgressMonitorDialog(shell).run(true, true, new IRunnableWithProgress() {
     78                     @Override
     79                     public void run(IProgressMonitor monitor)
     80                             throws InvocationTargetException, InterruptedException {
     81                         Thread.sleep(3000);
     82                     }
     83                 });
     84             } catch (Exception e) {
     85             }
     86 
     87             // retrieve the trace from the device
     88             TraceOptions traceOptions = dlg.getTraceOptions();
     89             CollectTraceAction.startTracing(shell, traceOptions, LOCAL_FORWARDED_PORT);
     90 
     91             // inform the client that it doesn't need to be traced anymore
     92             mClient.stopOpenGlTracing();
     93 
     94             // remove port forwarding
     95             CollectTraceAction.disablePortForwarding(mClient.getDevice(), LOCAL_FORWARDED_PORT);
     96 
     97             // and finally open the editor to view the file
     98             CollectTraceAction.openInEditor(shell, traceOptions.traceDestination);
     99         }
    100     }
    101 
    102     private static final StartTraceAction sAction = new StartTraceAction();
    103 
    104     @Override
    105     public Action getAction() {
    106         return sAction;
    107     }
    108 
    109     @Override
    110     public void selectedClientChanged(Client c) {
    111         sAction.setClient(c);
    112     }
    113 }
    114