aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer
diff options
context:
space:
mode:
authorGravatar liyuqian <liyuqian@google.com>2016-05-27 08:52:52 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-27 08:52:52 -0700
commite5a6cd9832eaa7011dee162283ff6470f82a3fdc (patch)
tree5a79d4b3bbcdd3435b0c473e6dfc679fb4c0fb05 /tools/viewer
parent3f9960949cba16b3a59b72f7b9833d4624044d6d (diff)
Now we can use drawer to view the state information of the native app, and set its state using the spinner.
Diffstat (limited to 'tools/viewer')
-rw-r--r--tools/viewer/Viewer.cpp66
-rw-r--r--tools/viewer/Viewer.h3
-rw-r--r--tools/viewer/sk_app/Window.cpp8
-rw-r--r--tools/viewer/sk_app/Window.h13
-rw-r--r--tools/viewer/sk_app/android/Window_android.cpp4
-rw-r--r--tools/viewer/sk_app/android/Window_android.h1
-rw-r--r--tools/viewer/sk_app/android/surface_glue_android.cpp26
-rw-r--r--tools/viewer/sk_app/android/surface_glue_android.h13
8 files changed, 130 insertions, 4 deletions
diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp
index b450f2192d..9f3b8f2e27 100644
--- a/tools/viewer/Viewer.cpp
+++ b/tools/viewer/Viewer.cpp
@@ -35,7 +35,13 @@ static bool on_touch_handler(int owner, Window::InputState state, float x, float
return viewer->onTouch(owner, state, x, y);
}
-DEFINE_bool2(fullscreen, f, false, "Run fullscreen.");
+static void on_ui_state_changed_handler(const SkString& stateName, const SkString& stateValue, void* userData) {
+ Viewer* viewer = reinterpret_cast<Viewer*>(userData);
+
+ return viewer->onUIStateChanged(stateName, stateValue);
+}
+
+DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifying this builder.");
DEFINE_string2(match, m, nullptr,
"[~][^]substring[$] [...] of bench name to run.\n"
@@ -54,6 +60,12 @@ const char *kBackendTypeStrings[sk_app::Window::kBackendTypeCount] = {
" [Vulkan]"
};
+const char* kName = "name";
+const char* kValue = "value";
+const char* kOptions = "options";
+const char* kSlideStateName = "Slide";
+const char* kBackendStateName = "Backend";
+
Viewer::Viewer(int argc, char** argv, void* platformData)
: fCurrentMeasurement(0)
, fDisplayStats(false)
@@ -83,6 +95,7 @@ Viewer::Viewer(int argc, char** argv, void* platformData)
fCommands.attach(fWindow);
fWindow->registerPaintFunc(on_paint_handler, this);
fWindow->registerTouchFunc(on_touch_handler, this);
+ fWindow->registerUIStateChangedFunc(on_ui_state_changed_handler, this);
// add key-bindings
fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
@@ -220,6 +233,10 @@ void Viewer::updateTitle() {
}
void Viewer::setupCurrentSlide(int previousSlide) {
+ if (fCurrentSlide == previousSlide) {
+ return; // no change; do nothing
+ }
+
fGesture.reset();
fDefaultMatrix.reset();
fDefaultMatrixInv.reset();
@@ -242,6 +259,7 @@ void Viewer::setupCurrentSlide(int previousSlide) {
}
this->updateTitle();
+ this->updateUIState();
fSlides[fCurrentSlide]->load();
if (previousSlide >= 0) {
fSlides[previousSlide]->unload();
@@ -389,3 +407,49 @@ void Viewer::onIdle(double ms) {
fWindow->inval();
}
}
+
+void Viewer::updateUIState() {
+ Json::Value slideState(Json::objectValue);
+ slideState[kName] = kSlideStateName;
+ slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str();
+ Json::Value allSlideNames(Json::arrayValue);
+ for(auto slide : fSlides) {
+ allSlideNames.append(Json::Value(slide->getName().c_str()));
+ }
+ slideState[kOptions] = allSlideNames;
+
+ // This state is currently a demo for the one without options.
+ // We will be able to change the backend too.
+ Json::Value backendState(Json::objectValue);
+ backendState[kName] = kBackendStateName;
+ backendState[kValue] = fBackendType == sk_app::Window::kVulkan_BackendType ?
+ "Vulkan" : "Other than Vulkan";
+ backendState[kOptions] = Json::Value(Json::arrayValue);
+
+ Json::Value state(Json::arrayValue);
+ state.append(slideState);
+ state.append(backendState);
+
+ fWindow->setUIState(state);
+}
+
+void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
+ // Currently, we only recognize the Slide state
+ if (stateName.equals(kSlideStateName)) {
+ int previousSlide = fCurrentSlide;
+ fCurrentSlide = 0;
+ for(auto slide : fSlides) {
+ if (slide->getName().equals(stateValue)) {
+ setupCurrentSlide(previousSlide);
+ break;
+ }
+ fCurrentSlide++;
+ }
+ if (fCurrentSlide >= fSlides.count()) {
+ fCurrentSlide = previousSlide;
+ SkDebugf("Slide not found: %s", stateValue.c_str());
+ }
+ } else {
+ SkDebugf("Unknown stateName: %s", stateName.c_str());
+ }
+}
diff --git a/tools/viewer/Viewer.h b/tools/viewer/Viewer.h
index 0bafee175b..e752b3f939 100644
--- a/tools/viewer/Viewer.h
+++ b/tools/viewer/Viewer.h
@@ -25,12 +25,15 @@ public:
void onPaint(SkCanvas* canvas);
void onIdle(double ms) override;
bool onTouch(int owner, sk_app::Window::InputState state, float x, float y);
+ void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
private:
void initSlides();
void updateTitle();
void setupCurrentSlide(int previousSlide);
+ void updateUIState();
+
void drawStats(SkCanvas* canvas);
void changeZoomLevel(float delta);
diff --git a/tools/viewer/sk_app/Window.cpp b/tools/viewer/sk_app/Window.cpp
index ba905e00f5..1c798df399 100644
--- a/tools/viewer/sk_app/Window.cpp
+++ b/tools/viewer/sk_app/Window.cpp
@@ -32,12 +32,16 @@ static bool default_touch_func(int owner, Window::InputState state, float x, flo
return false;
}
+static void default_ui_state_changed_func(
+ const SkString& stateName, const SkString& stateValue, void* userData) {}
+
static void default_paint_func(SkCanvas*, void* userData) {}
Window::Window() : fCharFunc(default_char_func)
, fKeyFunc(default_key_func)
, fMouseFunc(default_mouse_func)
, fTouchFunc(default_touch_func)
+ , fUIStateChangedFunc(default_ui_state_changed_func)
, fPaintFunc(default_paint_func) {
}
@@ -62,6 +66,10 @@ bool Window::onTouch(int owner, InputState state, float x, float y) {
return fTouchFunc(owner, state, x, y, fTouchUserData);
}
+void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
+ return fUIStateChangedFunc(stateName, stateValue, fUIStateChangedUserData);
+}
+
void Window::onPaint() {
markInvalProcessed();
sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
diff --git a/tools/viewer/sk_app/Window.h b/tools/viewer/sk_app/Window.h
index 65e1542e2c..9ca4231c80 100644
--- a/tools/viewer/sk_app/Window.h
+++ b/tools/viewer/sk_app/Window.h
@@ -12,6 +12,7 @@
#include "SkRect.h"
#include "SkTouchGesture.h"
#include "SkTypes.h"
+#include "SkJSONCPP.h"
class SkCanvas;
@@ -27,6 +28,7 @@ public:
virtual void setTitle(const char*) = 0;
virtual void show() = 0;
+ virtual void setUIState(const Json::Value& state) {} // do nothing in default
// Shedules an invalidation event for window if one is not currently pending.
// Make sure that either onPaint or markInvalReceived is called when the client window consumes
@@ -110,6 +112,8 @@ public:
typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
typedef bool(*OnTouchFunc)(int owner, InputState state, float x, float y, void* userData);
+ typedef void(*OnUIStateChangedFunc)(
+ const SkString& stateName, const SkString& stateValue, void* userData);
typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
void registerCharFunc(OnCharFunc func, void* userData) {
@@ -137,10 +141,16 @@ public:
fTouchUserData = userData;
}
+ void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
+ fUIStateChangedFunc = func;
+ fUIStateChangedUserData = userData;
+ }
+
bool onChar(SkUnichar c, uint32_t modifiers);
bool onKey(Key key, InputState state, uint32_t modifiers);
bool onMouse(int x, int y, InputState state, uint32_t modifiers);
bool onTouch(int owner, InputState state, float x, float y); // multi-owner = multi-touch
+ void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
void onPaint();
void onResize(uint32_t width, uint32_t height);
@@ -164,6 +174,9 @@ protected:
void* fMouseUserData;
OnTouchFunc fTouchFunc;
void* fTouchUserData;
+ OnUIStateChangedFunc
+ fUIStateChangedFunc;
+ void* fUIStateChangedUserData;
OnPaintFunc fPaintFunc;
void* fPaintUserData;
diff --git a/tools/viewer/sk_app/android/Window_android.cpp b/tools/viewer/sk_app/android/Window_android.cpp
index ed03c814dd..4f33870c1a 100644
--- a/tools/viewer/sk_app/android/Window_android.cpp
+++ b/tools/viewer/sk_app/android/Window_android.cpp
@@ -41,6 +41,10 @@ void Window_android::setTitle(const char* title) {
fSkiaAndroidApp->setTitle(title);
}
+void Window_android::setUIState(const Json::Value& state) {
+ fSkiaAndroidApp->setUIState(state);
+}
+
bool Window_android::attach(BackendType attachType, const DisplayParams& params) {
if (kVulkan_BackendType != attachType) {
return false;
diff --git a/tools/viewer/sk_app/android/Window_android.h b/tools/viewer/sk_app/android/Window_android.h
index f7d348bc91..f1c079835b 100644
--- a/tools/viewer/sk_app/android/Window_android.h
+++ b/tools/viewer/sk_app/android/Window_android.h
@@ -28,6 +28,7 @@ public:
bool attach(BackendType attachType, const DisplayParams& params) override;
void onInval() override;
+ void setUIState(const Json::Value& state) override;
void paintIfNeeded();
diff --git a/tools/viewer/sk_app/android/surface_glue_android.cpp b/tools/viewer/sk_app/android/surface_glue_android.cpp
index 958b7876b8..daf26a3852 100644
--- a/tools/viewer/sk_app/android/surface_glue_android.cpp
+++ b/tools/viewer/sk_app/android/surface_glue_android.cpp
@@ -46,6 +46,7 @@ SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) {
fAndroidApp = env->NewGlobalRef(androidApp);
jclass cls = env->GetObjectClass(fAndroidApp);
fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V");
+ fSetStateMethodID = env->GetMethodID(cls, "setState", "(Ljava/lang/String;)V");
fNativeWindow = nullptr;
pthread_create(&fThread, nullptr, pthread_main, this);
}
@@ -70,6 +71,12 @@ void SkiaAndroidApp::setTitle(const char* title) const {
fPThreadEnv->DeleteLocalRef(titleString);
}
+void SkiaAndroidApp::setUIState(const Json::Value& state) const {
+ jstring jstr = fPThreadEnv->NewStringUTF(state.toStyledString().c_str());
+ fPThreadEnv->CallVoidMethod(fAndroidApp, fSetStateMethodID, jstr);
+ fPThreadEnv->DeleteLocalRef(jstr);
+}
+
void SkiaAndroidApp::postMessage(const Message& message) const {
SkDEBUGCODE(auto writeSize =) write(fPipes[1], &message, sizeof(message));
SkASSERT(writeSize == sizeof(message));
@@ -139,6 +146,12 @@ int SkiaAndroidApp::message_callback(int fd, int events, void* data) {
message.fTouchY);
break;
}
+ case kUIStateChanged: {
+ skiaAndroidApp->fWindow->onUIStateChanged(*message.stateName, *message.stateValue);
+ delete message.stateName;
+ delete message.stateValue;
+ break;
+ }
default: {
// do nothing
}
@@ -229,4 +242,17 @@ extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched(
skiaAndroidApp->postMessage(message);
}
+extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onUIStateChanged(
+ JNIEnv* env, jobject activity, jlong handle, jstring stateName, jstring stateValue) {
+ auto skiaAndroidApp = (SkiaAndroidApp*)handle;
+ Message message(kUIStateChanged);
+ const char* nameChars = env->GetStringUTFChars(stateName, nullptr);
+ const char* valueChars = env->GetStringUTFChars(stateValue, nullptr);
+ message.stateName = new SkString(nameChars);
+ message.stateValue = new SkString(valueChars);
+ skiaAndroidApp->postMessage(message);
+ env->ReleaseStringUTFChars(stateName, nameChars);
+ env->ReleaseStringUTFChars(stateValue, valueChars);
+}
+
} // namespace sk_app
diff --git a/tools/viewer/sk_app/android/surface_glue_android.h b/tools/viewer/sk_app/android/surface_glue_android.h
index a4698336f0..3bbf3af9c3 100644
--- a/tools/viewer/sk_app/android/surface_glue_android.h
+++ b/tools/viewer/sk_app/android/surface_glue_android.h
@@ -12,6 +12,8 @@
#include <android/native_window_jni.h>
+#include "SkString.h"
+
#include "../Application.h"
#include "../Window.h"
@@ -25,7 +27,8 @@ enum MessageType {
kDestroyApp,
kContentInvalidated,
kKeyPressed,
- kTouched
+ kTouched,
+ kUIStateChanged,
};
struct Message {
@@ -35,6 +38,9 @@ struct Message {
int fTouchOwner, fTouchState;
float fTouchX, fTouchY;
+ SkString* stateName;
+ SkString* stateValue;
+
Message() {}
Message(MessageType t) : fType(t) {}
};
@@ -49,8 +55,9 @@ struct SkiaAndroidApp {
void postMessage(const Message& message) const;
void readMessage(Message* message) const;
- // This must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
+ // These must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
void setTitle(const char* title) const;
+ void setUIState(const Json::Value& state) const;
private:
pthread_t fThread;
@@ -58,7 +65,7 @@ private:
int fPipes[2]; // 0 is the read message pipe, 1 is the write message pipe
JavaVM* fJavaVM;
JNIEnv* fPThreadEnv;
- jmethodID fSetTitleMethodID;
+ jmethodID fSetTitleMethodID, fSetStateMethodID;
// This must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
~SkiaAndroidApp();