OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "VulkanViewer.h" | 8 #include "VulkanViewer.h" |
9 | 9 |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkRandom.h" | 11 #include "SkRandom.h" |
12 #include "SkCommonFlags.h" | 12 #include "SkCommonFlags.h" |
13 | 13 |
14 DEFINE_string(key, "", | 14 DEFINE_string(key, "", |
15 "Space-separated key/value pairs to add to JSON identifying this b
uilder."); | 15 "Space-separated key/value pairs to add to JSON identifying this b
uilder."); |
16 | 16 |
17 Application* Application::Create(int argc, char** argv, void* platformData) { | 17 Application* Application::Create(int argc, char** argv, void* platformData) { |
18 return new VulkanViewer(argc, argv, platformData); | 18 return new VulkanViewer(argc, argv, platformData); |
19 } | 19 } |
20 | 20 |
21 static bool on_key_handler(int key, bool keyDown, void* userData) { | 21 static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t m
odifiers, |
| 22 void* userData) { |
22 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); | 23 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); |
23 | 24 |
24 return vv->onKey(key, keyDown); | 25 return vv->onKey(key, state, modifiers); |
25 } | |
26 | |
27 static bool on_mouse_handler(int x, int y, bool mouseDown, void* userData) { | |
28 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); | |
29 | |
30 return vv->onMouse(x, y, mouseDown); | |
31 } | 26 } |
32 | 27 |
33 static void on_paint_handler(SkCanvas* canvas, void* userData) { | 28 static void on_paint_handler(SkCanvas* canvas, void* userData) { |
34 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); | 29 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); |
35 | 30 |
36 return vv->onPaint(canvas); | 31 return vv->onPaint(canvas); |
37 } | 32 } |
38 | 33 |
39 VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) : | 34 VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) : |
40 fGMs(skiagm::GMRegistry::Head()){ | 35 fGMs(skiagm::GMRegistry::Head()){ |
41 | 36 |
42 fWindow = Window::CreateNativeWindow(platformData); | 37 fWindow = Window::CreateNativeWindow(platformData); |
43 fWindow->attach(Window::kVulkan_BackendType, 0, nullptr); | 38 fWindow->attach(Window::kVulkan_BackendType, 0, nullptr); |
44 | 39 |
45 // register callbacks | 40 // register callbacks |
46 fWindow->registerKeyFunc(on_key_handler, this); | 41 fWindow->registerKeyFunc(on_key_handler, this); |
47 fWindow->registerMouseFunc(on_mouse_handler, this); | |
48 fWindow->registerPaintFunc(on_paint_handler, this); | 42 fWindow->registerPaintFunc(on_paint_handler, this); |
49 | 43 |
50 fWindow->setTitle("VulkanViewer"); | 44 fWindow->setTitle("VulkanViewer"); |
51 fWindow->show(); | 45 fWindow->show(); |
52 } | 46 } |
53 | 47 |
54 VulkanViewer::~VulkanViewer() { | 48 VulkanViewer::~VulkanViewer() { |
55 fWindow->detach(); | 49 fWindow->detach(); |
56 delete fWindow; | 50 delete fWindow; |
57 } | 51 } |
58 | 52 |
59 bool VulkanViewer::onKey(int key, bool keyDown) { | 53 bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t mod
ifiers) { |
60 if (keyDown) { | 54 if (Window::kDown_InputState == state && (modifiers & Window::kFirstPress_Mo
difierKey) && |
61 fInputHandler.onKeyDown(key); | 55 key == Window::kRight_Key) { |
62 } else { | 56 fGMs = fGMs->next(); |
63 fInputHandler.onKeyUp(key); | 57 } |
64 } | 58 |
65 return true; | 59 return true; |
66 } | 60 } |
67 | 61 |
68 bool VulkanViewer::onMouse(int x, int y, bool mouseDown) { | |
69 if (mouseDown) { | |
70 fInputHandler.onMouseDown(x, y); | |
71 } else { | |
72 fInputHandler.onMouseUp(); | |
73 } | |
74 return true; | |
75 } | |
76 | |
77 void VulkanViewer::onPaint(SkCanvas* canvas) { | 62 void VulkanViewer::onPaint(SkCanvas* canvas) { |
78 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); | 63 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
79 | 64 |
80 canvas->save(); | 65 canvas->save(); |
81 | 66 |
82 gm->draw(canvas); | 67 gm->draw(canvas); |
83 | 68 |
84 canvas->restore(); | 69 canvas->restore(); |
85 } | 70 } |
86 | 71 |
87 void VulkanViewer::onIdle(float dt) { | 72 void VulkanViewer::onIdle(float dt) { |
88 if (fInputHandler.isKeyPressed('l') || fInputHandler.isKeyPressed('L')) { | |
89 fGMs = fGMs->next(); | |
90 } | |
91 fWindow->onPaint(); | 73 fWindow->onPaint(); |
92 | |
93 fInputHandler.Update(); | |
94 } | 74 } |
OLD | NEW |