Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Unified Diff: tools/vulkan/viewer/VulkanViewer.cpp

Issue 1873733003: More cleanup in the Vulkan viewer (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/vulkan/viewer/VulkanViewer.h ('k') | tools/vulkan/win/Window_win.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/vulkan/viewer/VulkanViewer.cpp
diff --git a/tools/vulkan/viewer/VulkanViewer.cpp b/tools/vulkan/viewer/VulkanViewer.cpp
index 55da5546f75623edf6893ead23eae2f1e9937177..52b6ad19f53e8e181de3901c51dac7084647ebb8 100644
--- a/tools/vulkan/viewer/VulkanViewer.cpp
+++ b/tools/vulkan/viewer/VulkanViewer.cpp
@@ -27,6 +27,12 @@ static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t m
return vv->onKey(key, state, modifiers);
}
+static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) {
+ VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData);
+
+ return vv->onChar(c, modifiers);
+}
+
static void on_paint_handler(SkCanvas* canvas, void* userData) {
VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData);
@@ -46,12 +52,10 @@ DEFINE_string2(match, m, nullptr,
"it is skipped unless some list entry starts with ~");
DEFINE_string(skps, "skps", "Directory to read skps from.");
-
-
-
-
-
-VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) : fCurrentMeasurement(0) {
+VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData)
+ : fCurrentMeasurement(0)
+ , fDisplayStats(false)
+{
memset(fMeasurements, 0, sizeof(fMeasurements));
SkDebugf("Command line arguments: ");
@@ -67,46 +71,18 @@ VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) : fCurrent
// register callbacks
fWindow->registerKeyFunc(on_key_handler, this);
+ fWindow->registerCharFunc(on_char_handler, this);
fWindow->registerPaintFunc(on_paint_handler, this);
// set up slides
this->initSlides();
// set up first frame
- SkString title("VulkanViewer: ");
- title.append(fSlides[0]->getName());
fCurrentSlide = 0;
- fWindow->setTitle(title.c_str());
- fWindow->show();
-}
-
-static sk_sp<SkPicture> read_picture(const char path[]) {
- if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
- return nullptr;
- }
+ setupCurrentSlide(-1);
+ fLocalMatrix.reset();
- SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
- if (stream.get() == nullptr) {
- SkDebugf("Could not read %s.\n", path);
- return nullptr;
- }
-
- auto pic = SkPicture::MakeFromStream(stream.get());
- if (!pic) {
- SkDebugf("Could not read %s as an SkPicture.\n", path);
- }
- return pic;
-}
-
-
-static sk_sp<SKPSlide> loadSKP(const SkString& path) {
- sk_sp<SkPicture> pic = read_picture(path.c_str());
- if (!pic) {
- return nullptr;
- }
-
- SkString name = SkOSPath::Basename(path.c_str());
- return sk_sp<SKPSlide>(new SKPSlide(name.c_str(), pic));
+ fWindow->show();
}
void VulkanViewer::initSlides() {
@@ -132,17 +108,25 @@ void VulkanViewer::initSlides() {
// SKPs
for (int i = 0; i < FLAGS_skps.count(); i++) {
if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
+ if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
+ continue;
+ }
+
SkString path(FLAGS_skps[i]);
- sk_sp<SKPSlide> slide = loadSKP(path);
+ sk_sp<SKPSlide> slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path));
if (slide) {
fSlides.push_back(slide);
}
} else {
SkOSFile::Iter it(FLAGS_skps[i], ".skp");
- SkString path;
- while (it.next(&path)) {
- SkString skpName = SkOSPath::Join(FLAGS_skps[i], path.c_str());
- sk_sp<SKPSlide> slide = loadSKP(skpName);
+ SkString skpName;
+ while (it.next(&skpName)) {
+ if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) {
+ continue;
+ }
+
+ SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str());
+ sk_sp<SKPSlide> slide(new SKPSlide(skpName, path));
if (slide) {
fSlides.push_back(slide);
}
@@ -157,39 +141,124 @@ VulkanViewer::~VulkanViewer() {
delete fWindow;
}
+void VulkanViewer::setupCurrentSlide(int previousSlide) {
+ SkString title("VulkanViewer: ");
+ title.append(fSlides[fCurrentSlide]->getName());
+ fSlides[fCurrentSlide]->load();
+ if (previousSlide >= 0) {
+ fSlides[previousSlide]->unload();
+ }
+ fWindow->setTitle(title.c_str());
+ fWindow->inval();
+}
+
+#define MAX_ZOOM_LEVEL 8
+#define MIN_ZOOM_LEVEL -8
+
+void VulkanViewer::changeZoomLevel(float delta) {
+ fZoomLevel += delta;
+ if (fZoomLevel > 0) {
+ fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
+ fZoomScale = fZoomLevel + SK_Scalar1;
+ } else if (fZoomLevel < 0) {
+ fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
+ fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
+ } else {
+ fZoomScale = SK_Scalar1;
+ }
+ this->updateMatrix();
+}
+
+void VulkanViewer::updateMatrix(){
+ SkMatrix m;
+ m.reset();
+
+ if (fZoomLevel) {
+ SkPoint center;
+ //m = this->getLocalMatrix();//.invert(&m);
+ m.mapXY(fZoomCenterX, fZoomCenterY, &center);
+ SkScalar cx = center.fX;
+ SkScalar cy = center.fY;
+
+ m.setTranslate(-cx, -cy);
+ m.postScale(fZoomScale, fZoomScale);
+ m.postTranslate(cx, cy);
+ }
+
+ // TODO: add gesture support
+ // Apply any gesture matrix
+ //m.preConcat(fGesture.localM());
+ //m.preConcat(fGesture.globalM());
+
+ fLocalMatrix = m;
+}
+
bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t modifiers) {
- if (Window::kDown_InputState == state && (modifiers & Window::kFirstPress_ModifierKey)) {
- if (key == Window::kRight_Key) {
- fCurrentSlide++;
- if (fCurrentSlide >= fSlides.count()) {
- fCurrentSlide = 0;
+ if (Window::kDown_InputState == state) {
+ switch (key) {
+ case Window::kRight_Key: {
+ int previousSlide = fCurrentSlide;
+ fCurrentSlide++;
+ if (fCurrentSlide >= fSlides.count()) {
+ fCurrentSlide = 0;
+ }
+ setupCurrentSlide(previousSlide);
+ return true;
+ }
+
+ case Window::kLeft_Key: {
+ int previousSlide = fCurrentSlide;
+ fCurrentSlide--;
+ if (fCurrentSlide < 0) {
+ fCurrentSlide = fSlides.count() - 1;
+ }
+ SkString title("VulkanViewer: ");
+ title.append(fSlides[fCurrentSlide]->getName());
+ fWindow->setTitle(title.c_str());
+ setupCurrentSlide(previousSlide);
+ return true;
+ }
+
+ case Window::kUp_Key: {
+ this->changeZoomLevel(1.f / 32.f);
+ return true;
}
- SkString title("VulkanViewer: ");
- title.append(fSlides[fCurrentSlide]->getName());
- fWindow->setTitle(title.c_str());
- } else if (key == Window::kLeft_Key) {
- fCurrentSlide--;
- if (fCurrentSlide < 0) {
- fCurrentSlide = fSlides.count()-1;
+
+ case Window::kDown_Key: {
+ this->changeZoomLevel(-1.f / 32.f);
+ return true;
}
- SkString title("VulkanViewer: ");
- title.append(fSlides[fCurrentSlide]->getName());
- fWindow->setTitle(title.c_str());
+
+ default:
+ break;
}
}
- return true;
+ return false;
+}
+
+bool VulkanViewer::onChar(SkUnichar c, uint32_t modifiers) {
+ if ('s' == c) {
+ fDisplayStats = !fDisplayStats;
+ return true;
+ }
+
+ return false;
}
void VulkanViewer::onPaint(SkCanvas* canvas) {
canvas->clear(SK_ColorWHITE);
- canvas->save();
+ int count = canvas->save();
+ canvas->setMatrix(fLocalMatrix);
+
fSlides[fCurrentSlide]->draw(canvas);
- canvas->restore();
+ canvas->restoreToCount(count);
- drawStats(canvas);
+ if (fDisplayStats) {
+ drawStats(canvas);
+ }
}
void VulkanViewer::drawStats(SkCanvas* canvas) {
@@ -240,5 +309,8 @@ void VulkanViewer::onIdle(double ms) {
fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod
SkASSERT(fCurrentMeasurement < kMeasurementCount);
- fWindow->onPaint();
+ fAnimTimer.updateTime();
+ if (fDisplayStats || fSlides[fCurrentSlide]->animate(fAnimTimer)) {
+ fWindow->inval();
+ }
}
« no previous file with comments | « tools/vulkan/viewer/VulkanViewer.h ('k') | tools/vulkan/win/Window_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698