Add MSAA option to SampleApp

Review URL: http://codereview.appspot.com/5969049


git-svn-id: http://skia.googlecode.com/svn/trunk@3627 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index 92c9e6c..1cd9c67 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -139,7 +139,8 @@
         : fCurContext(NULL)
         , fCurIntf(NULL)
         , fCurRenderTarget(NULL)
-        , fBackend(kNone_BackEndType) {
+        , fBackend(kNone_BackEndType)
+        , fMSAASampleCount(0) {
     }
 
     virtual ~DefaultDeviceManager() {
@@ -148,7 +149,7 @@
         SkSafeUnref(fCurRenderTarget);
     }
 
-    virtual void setUpBackend(SampleWindow* win) {
+    virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
         SkASSERT(kNone_BackEndType == fBackend);
 
         fBackend = kNone_BackEndType;
@@ -175,11 +176,12 @@
                 break;
         }
 
-        bool result = win->attach(fBackend);
+        bool result = win->attach(fBackend, msaaSampleCount);
         if (!result) {
             SkDebugf("Failed to initialize GL");
             return;
         }
+        fMSAASampleCount = msaaSampleCount;
 
         SkASSERT(NULL == fCurIntf);
         switch (win->getDeviceType()) {
@@ -287,7 +289,7 @@
     virtual void windowSizeChanged(SampleWindow* win) {
 
         if (fCurContext) {
-            win->attach(fBackend);
+            win->attach(fBackend, fMSAASampleCount);
 
             GrPlatformRenderTargetDesc desc;
             desc.fWidth = SkScalarRound(win->width());
@@ -307,12 +309,18 @@
     virtual GrContext* getGrContext() {
         return fCurContext;
     }
+
+    virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
+        return fCurRenderTarget;
+    }
+
 private:
     GrContext*              fCurContext;
     const GrGLInterface*    fCurIntf;
     GrRenderTarget*         fCurRenderTarget;
 
     SkOSWindow::SkBackEndTypes fBackend;
+    int fMSAASampleCount;
 
     typedef SampleWindow::DeviceManager INHERITED;
 };
@@ -645,9 +653,10 @@
 }
 
 static void usage(const char * argv0) {
-    SkDebugf("%s [--slide sampleName] [-i resourcePath]\n", argv0);
+    SkDebugf("%s [--slide sampleName] [-i resourcePath] [-msaa sampleCount]\n", argv0);
     SkDebugf("    sampleName: sample at which to start.\n");
     SkDebugf("    resourcePath: directory that stores image resources.\n");
+    SkDebugf("    msaa: request multisampling with the given sample count.\n");
 }
 
 SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager) : INHERITED(hwnd) {
@@ -663,6 +672,7 @@
 
     const char* resourcePath = NULL;
     fCurrIndex = -1;
+    fMSAASampleCount = 0;
 
     const char* const commandName = argv[0];
     char* const* stop = argv + argc;
@@ -680,7 +690,12 @@
                     fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
                 }
             }
-        } 
+        } else if (strcmp(*argv, "--msaa") == 0) {
+            ++argv;
+            if (argv < stop && **argv) {
+                fMSAASampleCount = atoi(*argv);
+            }
+        }
         else {
             usage(commandName);
         }
@@ -820,7 +835,7 @@
         devManager->ref();
         fDevManager = devManager;
     }
-    fDevManager->setUpBackend(this);
+    fDevManager->setUpBackend(this, fMSAASampleCount);
 
     // If another constructor set our dimensions, ensure that our
     // onSizeChange gets called.
@@ -1671,7 +1686,7 @@
 
     fDeviceType = type;
 
-    fDevManager->setUpBackend(this);
+    fDevManager->setUpBackend(this, fMSAASampleCount);
 
     this->updateTitle();
     this->inval(NULL);
@@ -1867,6 +1882,16 @@
     "null-gl: "
 };
 
+static const bool gDeviceTypeIsGPU[] = {
+    false,
+    false,
+    true,
+#if SK_ANGLE
+    true,
+#endif
+    true
+};
+
 static const char* trystate_str(SkOSMenu::TriState state,
                                 const char trueStr[], const char falseStr[]) {
     if (SkOSMenu::kOnState == state) {
@@ -1926,6 +1951,12 @@
         title.prepend("! ");
     }
 
+    if (gDeviceTypeIsGPU[fDeviceType] &&
+        fDevManager->getGrRenderTarget()->numSamples() > 0) {
+        title.appendf(" [MSAA: %d]",
+                       fDevManager->getGrRenderTarget()->numSamples());
+    }
+
     this->setTitle(title.c_str());
 }