| 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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 return vv->onKey(key, state, modifiers); | 25 return vv->onKey(key, state, modifiers); |
| 26 } | 26 } |
| 27 | 27 |
| 28 static void on_paint_handler(SkCanvas* canvas, void* userData) { | 28 static void on_paint_handler(SkCanvas* canvas, void* userData) { |
| 29 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); | 29 VulkanViewer* vv = reinterpret_cast<VulkanViewer*>(userData); |
| 30 | 30 |
| 31 return vv->onPaint(canvas); | 31 return vv->onPaint(canvas); |
| 32 } | 32 } |
| 33 | 33 |
| 34 VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) : | 34 VulkanViewer::VulkanViewer(int argc, char** argv, void* platformData) |
| 35 fGMs(skiagm::GMRegistry::Head()){ | 35 : fGMs(skiagm::GMRegistry::Head()) |
| 36 , fCurrentMeasurement(0) { |
| 37 memset(fMeasurements, 0, sizeof(fMeasurements)); |
| 36 | 38 |
| 37 fWindow = Window::CreateNativeWindow(platformData); | 39 fWindow = Window::CreateNativeWindow(platformData); |
| 38 fWindow->attach(Window::kVulkan_BackendType, 0, nullptr); | 40 fWindow->attach(Window::kVulkan_BackendType, 0, nullptr); |
| 39 | 41 |
| 40 // register callbacks | 42 // register callbacks |
| 41 fWindow->registerKeyFunc(on_key_handler, this); | 43 fWindow->registerKeyFunc(on_key_handler, this); |
| 42 fWindow->registerPaintFunc(on_paint_handler, this); | 44 fWindow->registerPaintFunc(on_paint_handler, this); |
| 43 | 45 |
| 44 fWindow->setTitle("VulkanViewer"); | 46 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
| 47 SkString title("VulkanViewer: "); |
| 48 title.append(gm->getName()); |
| 49 fWindow->setTitle(title.c_str()); |
| 45 fWindow->show(); | 50 fWindow->show(); |
| 46 } | 51 } |
| 47 | 52 |
| 48 VulkanViewer::~VulkanViewer() { | 53 VulkanViewer::~VulkanViewer() { |
| 49 fWindow->detach(); | 54 fWindow->detach(); |
| 50 delete fWindow; | 55 delete fWindow; |
| 51 } | 56 } |
| 52 | 57 |
| 53 bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t mod
ifiers) { | 58 bool VulkanViewer::onKey(Window::Key key, Window::InputState state, uint32_t mod
ifiers) { |
| 54 if (Window::kDown_InputState == state && (modifiers & Window::kFirstPress_Mo
difierKey) && | 59 if (Window::kDown_InputState == state && (modifiers & Window::kFirstPress_Mo
difierKey) && |
| 55 key == Window::kRight_Key) { | 60 key == Window::kRight_Key) { |
| 56 fGMs = fGMs->next(); | 61 fGMs = fGMs->next(); |
| 57 } | 62 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
| 63 SkString title("VulkanViewer: "); |
| 64 title.append(gm->getName()); |
| 65 fWindow->setTitle(title.c_str()); |
| 66 } |
| 58 | 67 |
| 59 return true; | 68 return true; |
| 60 } | 69 } |
| 61 | 70 |
| 62 void VulkanViewer::onPaint(SkCanvas* canvas) { | 71 void VulkanViewer::onPaint(SkCanvas* canvas) { |
| 63 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); | 72 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
| 64 | 73 |
| 65 canvas->save(); | 74 canvas->save(); |
| 75 gm->draw(canvas); |
| 76 canvas->restore(); |
| 66 | 77 |
| 67 gm->draw(canvas); | 78 drawStats(canvas); |
| 79 } |
| 80 |
| 81 void VulkanViewer::drawStats(SkCanvas* canvas) { |
| 82 static const float kPixelPerMS = 2.0f; |
| 83 static const int kDisplayWidth = 130; |
| 84 static const int kDisplayHeight = 100; |
| 85 static const int kDisplayPadding = 10; |
| 86 static const int kGraphPadding = 3; |
| 87 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps |
| 88 |
| 89 SkISize canvasSize = canvas->getDeviceSize(); |
| 90 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth
-kDisplayPadding), |
| 91 SkIntToScalar(kDisplayPadding), |
| 92 SkIntToScalar(kDisplayWidth), SkIntToScalar(k
DisplayHeight)); |
| 93 SkPaint paint; |
| 94 canvas->save(); |
| 95 |
| 96 canvas->clipRect(rect); |
| 97 paint.setColor(SK_ColorBLACK); |
| 98 canvas->drawRect(rect, paint); |
| 99 // draw the 16ms line |
| 100 paint.setColor(SK_ColorLTGRAY); |
| 101 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS, |
| 102 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint); |
| 103 paint.setColor(SK_ColorRED); |
| 104 paint.setStyle(SkPaint::kStroke_Style); |
| 105 canvas->drawRect(rect, paint); |
| 106 |
| 107 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding; |
| 108 const int xStep = 2; |
| 109 const int startY = SkScalarTruncToInt(rect.fBottom); |
| 110 int i = fCurrentMeasurement; |
| 111 do { |
| 112 int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // rou
nd to nearest value |
| 113 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY), |
| 114 SkIntToScalar(x), SkIntToScalar(endY), paint); |
| 115 i++; |
| 116 i &= (kMeasurementCount - 1); // fast mod |
| 117 x += xStep; |
| 118 } while (i != fCurrentMeasurement); |
| 68 | 119 |
| 69 canvas->restore(); | 120 canvas->restore(); |
| 70 } | 121 } |
| 71 | 122 |
| 72 void VulkanViewer::onIdle(float dt) { | 123 void VulkanViewer::onIdle(double ms) { |
| 124 // Record measurements |
| 125 fMeasurements[fCurrentMeasurement++] = ms; |
| 126 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod |
| 127 SkASSERT(fCurrentMeasurement < kMeasurementCount); |
| 128 |
| 73 fWindow->onPaint(); | 129 fWindow->onPaint(); |
| 74 } | 130 } |
| OLD | NEW |