More accurate render time and continuous fresh
authorliyuqian <liyuqian@google.com>
Wed, 6 Jul 2016 21:11:32 +0000 (14:11 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 6 Jul 2016 21:11:32 +0000 (14:11 -0700)
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2129613002

Review-Url: https://codereview.chromium.org/2129613002

platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/StateAdapter.java
tools/viewer/Viewer.cpp
tools/viewer/Viewer.h
tools/viewer/sk_app/Application.h
tools/viewer/sk_app/android/main_android.cpp
tools/viewer/sk_app/android/surface_glue_android.cpp
tools/viewer/sk_app/unix/main_unix.cpp
tools/viewer/sk_app/win/main_win.cpp

index 6c080bd..3aea965 100644 (file)
@@ -37,6 +37,9 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
     private static final String OPTIONS = "options";
     private static final String BACKEND_STATE_NAME = "Backend";
     private static final String FPS_STATE_NAME = "FPS";
+    private static final String REFRESH_STATE_NAME = "Refresh";
+    private static final String ON = "ON";
+    private static final String OFF = "OFF";
     private static final int FILTER_LENGTH = 20;
 
     private ViewerActivity mViewerActivity;
@@ -103,6 +106,8 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
                     @Override
                     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                         mFPSFloatText.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
+                        // Quickly set the bool fRefresh in native Viewer app for continuous refresh
+                        mViewerActivity.onStateChanged(REFRESH_STATE_NAME, isChecked ? ON : OFF);
                     }
                 });
                 return view;
index 0ed7e26..ff77cc4 100644 (file)
@@ -20,6 +20,7 @@
 #include "SkRandom.h"
 #include "SkStream.h"
 #include "SkSurface.h"
+#include "SkTime.h"
 
 using namespace sk_app;
 
@@ -86,10 +87,12 @@ const char* kFpsStateName = "FPS";
 const char* kSplitScreenStateName = "Split screen";
 const char* kON = "ON";
 const char* kOFF = "OFF";
+const char* kRefreshStateName = "Refresh";
 
 Viewer::Viewer(int argc, char** argv, void* platformData)
     : fCurrentMeasurement(0)
     , fDisplayStats(false)
+    , fRefresh(false)
     , fSplitScreen(false)
     , fBackendType(sk_app::Window::kNativeGL_BackendType)
     , fZoomCenterX(0.0f)
@@ -400,6 +403,9 @@ void Viewer::drawSlide(SkCanvas* canvas, bool inSplitScreen) {
 }
 
 void Viewer::onPaint(SkCanvas* canvas) {
+    // Record measurements
+    double startTime = SkTime::GetMSecs();
+
     drawSlide(canvas, false);
     if (fSplitScreen && fWindow->supportsContentRect()) {
         drawSlide(canvas, true);
@@ -409,6 +415,11 @@ void Viewer::onPaint(SkCanvas* canvas) {
         drawStats(canvas);
     }
     fCommands.drawHelp(canvas);
+
+    fMeasurements[fCurrentMeasurement++] = SkTime::GetMSecs() - startTime;
+    fCurrentMeasurement &= (kMeasurementCount - 1);  // fast mod
+    SkASSERT(fCurrentMeasurement < kMeasurementCount);
+    updateUIState(); // Update the FPS
 }
 
 bool Viewer::onTouch(intptr_t owner, Window::InputState state, float x, float y) {
@@ -480,16 +491,10 @@ void Viewer::drawStats(SkCanvas* canvas) {
     canvas->restore();
 }
 
-void Viewer::onIdle(double ms) {
-    // Record measurements
-    fMeasurements[fCurrentMeasurement++] = ms;
-    fCurrentMeasurement &= (kMeasurementCount - 1);  // fast mod
-    SkASSERT(fCurrentMeasurement < kMeasurementCount);
-
+void Viewer::onIdle() {
     fAnimTimer.updateTime();
-    if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) {
+    if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats || fRefresh) {
         fWindow->inval();
-        updateUIState(); // Update the FPS
     }
 }
 
@@ -596,6 +601,10 @@ void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateVa
             fWindow->inval();
             updateUIState();
         }
+    } else if (stateName.equals(kRefreshStateName)) {
+        // This state is actually NOT in the UI state.
+        // We use this to allow Android to quickly set bool fRefresh.
+        fRefresh = stateValue.equals(kON);
     } else {
         SkDebugf("Unknown stateName: %s", stateName.c_str());
     }
index e140e1b..1ed135d 100644 (file)
@@ -24,7 +24,7 @@ public:
     ~Viewer() override;
 
     void onPaint(SkCanvas* canvas);
-    void onIdle(double ms) override;
+    void onIdle() override;
     bool onTouch(intptr_t owner, sk_app::Window::InputState state, float x, float y);
     void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
 
@@ -52,6 +52,7 @@ private:
     int                    fCurrentSlide;
 
     bool                   fDisplayStats;
+    bool                   fRefresh; // whether to continuously refresh for measuring render time
 
     // whether to split the screen and draw two copies of the slide, one with sRGB and one without
     bool                   fSplitScreen;
index 235ff09..df9a20d 100644 (file)
@@ -16,7 +16,7 @@ public:
 
     virtual ~Application() {}
 
-    virtual void onIdle(double ms) = 0;
+    virtual void onIdle() = 0;
 };
 
 }   // namespace sk_app
index 9334f0c..ee2dab5 100644 (file)
@@ -13,8 +13,6 @@
 #include "../Application.h"
 #include "Timer.h"
 
-static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
-
 using sk_app::Application;
 
 /**
@@ -36,9 +34,6 @@ void android_main(struct android_app* state) {
                                                            const_cast<char**>(gCmdLine),
                                                            state));
 
-    double currentTime = 0.0;
-    double previousTime = 0.0;
-
     // loop waiting for stuff to do.
     while (1) {
         // Read all pending events.
@@ -60,9 +55,7 @@ void android_main(struct android_app* state) {
                 return;
             }
 
-            previousTime = currentTime;
-            currentTime = now_ms();
-            vkApp->onIdle(currentTime - previousTime);
+            vkApp->onIdle();
         }
     }
 }
index 0c1ab0c..d001cd5 100644 (file)
@@ -22,7 +22,6 @@
 #include "SkTypes.h"
 #include "SkUtils.h"
 #include "Window_android.h"
-#include "SkTime.h"
 
 namespace sk_app {
 
@@ -167,8 +166,6 @@ int SkiaAndroidApp::message_callback(int fd, int events, void* data) {
     return 1;  // continue receiving callbacks
 }
 
-static double now_ms() { return SkTime::GetMSecs(); }
-
 void* SkiaAndroidApp::pthread_main(void* arg) {
     SkDebugf("pthread_main begins");
 
@@ -184,17 +181,13 @@ void* SkiaAndroidApp::pthread_main(void* arg) {
 
     skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp);
 
-    double currentTime = 0.0;
-    double previousTime = 0.0;
     while (true) {
         const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr);
 
         if (ident >= 0) {
             SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident);
         } else {
-            previousTime = currentTime;
-            currentTime = now_ms();
-            skiaAndroidApp->fApp->onIdle(currentTime - previousTime);
+            skiaAndroidApp->fApp->onIdle();
         }
     }
 
index d595379..5e6b7fe 100644 (file)
@@ -13,8 +13,6 @@
 
 using sk_app::Application;
 
-static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
-
 void finishWindow(sk_app::Window_unix* win) {
     win->finishResize();
     win->finishPaint();
@@ -26,9 +24,6 @@ int main(int argc, char**argv) {
 
     Application* app = Application::Create(argc, argv, (void*)display);
 
-    double currentTime = 0.0;
-    double previousTime = 0.0;
-
     // Get the file descriptor for the X display
     int x11_fd = ConnectionNumber(display);
     fd_set in_fds;
@@ -75,9 +70,7 @@ int main(int argc, char**argv) {
         
         pendingWindows.foreach(finishWindow);
         if (pendingWindows.count() > 0) {
-            previousTime = currentTime;
-            currentTime = now_ms();
-            app->onIdle(currentTime - previousTime);        
+            app->onIdle();
         }
         pendingWindows.reset();
     }
index ba1629c..4800258 100644 (file)
@@ -27,8 +27,6 @@ static char* tchar_to_utf8(const TCHAR* str) {
 #endif
 }
 
-static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
-
 // This file can work with GUI or CONSOLE subsystem types since we define _tWinMain and main().
 
 static int main_common(HINSTANCE hInstance, int show, int argc, char**argv);
@@ -66,18 +64,13 @@ static int main_common(HINSTANCE hInstance, int show, int argc, char**argv) {
 
     MSG msg = { 0 };
 
-    double currentTime = 0.0;
-    double previousTime = 0.0;
-
     // Main message loop
     while (WM_QUIT != msg.message) {
         if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
         } else {
-            previousTime = currentTime;
-            currentTime = now_ms();
-            app->onIdle(currentTime - previousTime);
+            app->onIdle();
         }
     }