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

Side by Side Diff: tools/viewer/Viewer.cpp

Issue 1982643004: Implement touch control (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: SkMutex Created 4 years, 7 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 unified diff | Download patch
« no previous file with comments | « tools/viewer/Viewer.h ('k') | tools/viewer/sk_app/Window.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "Viewer.h" 8 #include "Viewer.h"
9 9
10 #include "GMSlide.h" 10 #include "GMSlide.h"
(...skipping 10 matching lines...) Expand all
21 Application* Application::Create(int argc, char** argv, void* platformData) { 21 Application* Application::Create(int argc, char** argv, void* platformData) {
22 return new Viewer(argc, argv, platformData); 22 return new Viewer(argc, argv, platformData);
23 } 23 }
24 24
25 static void on_paint_handler(SkCanvas* canvas, void* userData) { 25 static void on_paint_handler(SkCanvas* canvas, void* userData) {
26 Viewer* vv = reinterpret_cast<Viewer*>(userData); 26 Viewer* vv = reinterpret_cast<Viewer*>(userData);
27 27
28 return vv->onPaint(canvas); 28 return vv->onPaint(canvas);
29 } 29 }
30 30
31 static bool on_touch_handler(int owner, Window::InputState state, float x, float y, void* userData)
32 {
33 Viewer* viewer = reinterpret_cast<Viewer*>(userData);
34
35 return viewer->onTouch(owner, state, x, y);
36 }
37
31 DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); 38 DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
32 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi ng this builder."); 39 DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON identifyi ng this builder.");
33 DEFINE_string2(match, m, nullptr, 40 DEFINE_string2(match, m, nullptr,
34 "[~][^]substring[$] [...] of bench name to run.\n" 41 "[~][^]substring[$] [...] of bench name to run.\n"
35 "Multiple matches may be separated by spaces.\n" 42 "Multiple matches may be separated by spaces.\n"
36 "~ causes a matching bench to always be skipped\n" 43 "~ causes a matching bench to always be skipped\n"
37 "^ requires the start of the bench to match\n" 44 "^ requires the start of the bench to match\n"
38 "$ requires the end of the bench to match\n" 45 "$ requires the end of the bench to match\n"
39 "^ and $ requires an exact match\n" 46 "^ and $ requires an exact match\n"
40 "If a bench does not match any list entry,\n" 47 "If a bench does not match any list entry,\n"
(...skipping 17 matching lines...) Expand all
58 SkDebugf("\n"); 65 SkDebugf("\n");
59 66
60 SkCommandLineFlags::Parse(argc, argv); 67 SkCommandLineFlags::Parse(argc, argv);
61 68
62 fWindow = Window::CreateNativeWindow(platformData); 69 fWindow = Window::CreateNativeWindow(platformData);
63 fWindow->attach(Window::kVulkan_BackendType, DisplayParams()); 70 fWindow->attach(Window::kVulkan_BackendType, DisplayParams());
64 71
65 // register callbacks 72 // register callbacks
66 fCommands.attach(fWindow); 73 fCommands.attach(fWindow);
67 fWindow->registerPaintFunc(on_paint_handler, this); 74 fWindow->registerPaintFunc(on_paint_handler, this);
75 fWindow->registerTouchFunc(on_touch_handler, this);
68 76
69 // add key-bindings 77 // add key-bindings
70 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() { 78 fCommands.addCommand('s', "Overlays", "Toggle stats display", [this]() {
71 this->fDisplayStats = !this->fDisplayStats; 79 this->fDisplayStats = !this->fDisplayStats;
72 fWindow->inval(); 80 fWindow->inval();
73 }); 81 });
74 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() { 82 fCommands.addCommand('c', "Modes", "Toggle sRGB color mode", [this]() {
75 DisplayParams params = fWindow->getDisplayParams(); 83 DisplayParams params = fWindow->getDisplayParams();
76 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType ) 84 params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType )
77 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; 85 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType;
(...skipping 27 matching lines...) Expand all
105 }); 113 });
106 114
107 // set up slides 115 // set up slides
108 this->initSlides(); 116 this->initSlides();
109 117
110 fAnimTimer.run(); 118 fAnimTimer.run();
111 119
112 // set up first frame 120 // set up first frame
113 fCurrentSlide = 0; 121 fCurrentSlide = 0;
114 setupCurrentSlide(-1); 122 setupCurrentSlide(-1);
115 updateMatrix();
116 123
117 fWindow->show(); 124 fWindow->show();
118 } 125 }
119 126
120 void Viewer::initSlides() { 127 void Viewer::initSlides() {
121 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head()); 128 const skiagm::GMRegistry* gms(skiagm::GMRegistry::Head());
122 while (gms) { 129 while (gms) {
123 SkAutoTDelete<skiagm::GM> gm(gms->factory()(nullptr)); 130 SkAutoTDelete<skiagm::GM> gm(gms->factory()(nullptr));
124 131
125 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) { 132 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, gm->getName())) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 fZoomLevel += delta; 205 fZoomLevel += delta;
199 if (fZoomLevel > 0) { 206 if (fZoomLevel > 0) {
200 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL); 207 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
201 fZoomScale = fZoomLevel + SK_Scalar1; 208 fZoomScale = fZoomLevel + SK_Scalar1;
202 } else if (fZoomLevel < 0) { 209 } else if (fZoomLevel < 0) {
203 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL); 210 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
204 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel); 211 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
205 } else { 212 } else {
206 fZoomScale = SK_Scalar1; 213 fZoomScale = SK_Scalar1;
207 } 214 }
208 this->updateMatrix();
209 } 215 }
210 216
211 void Viewer::updateMatrix(){ 217 SkMatrix Viewer::computeMatrix() {
212 SkMatrix m; 218 SkMatrix m;
213 m.reset(); 219 m.reset();
214 220
215 if (fZoomLevel) { 221 if (fZoomLevel) {
216 SkPoint center; 222 SkPoint center;
217 //m = this->getLocalMatrix();//.invert(&m); 223 //m = this->getLocalMatrix();//.invert(&m);
218 m.mapXY(fZoomCenterX, fZoomCenterY, &center); 224 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
219 SkScalar cx = center.fX; 225 SkScalar cx = center.fX;
220 SkScalar cy = center.fY; 226 SkScalar cy = center.fY;
221 227
222 m.setTranslate(-cx, -cy); 228 m.setTranslate(-cx, -cy);
223 m.postScale(fZoomScale, fZoomScale); 229 m.postScale(fZoomScale, fZoomScale);
224 m.postTranslate(cx, cy); 230 m.postTranslate(cx, cy);
225 } 231 }
226 232
227 // TODO: add gesture support 233 m.preConcat(fGesture.localM());
228 // Apply any gesture matrix 234 m.preConcat(fGesture.globalM());
229 //m.preConcat(fGesture.localM());
230 //m.preConcat(fGesture.globalM());
231 235
232 fLocalMatrix = m; 236 return m;
233 } 237 }
234 238
235 void Viewer::onPaint(SkCanvas* canvas) { 239 void Viewer::onPaint(SkCanvas* canvas) {
236 240
237 int count = canvas->save(); 241 int count = canvas->save();
238 242
239 if (fWindow->supportsContentRect()) { 243 if (fWindow->supportsContentRect()) {
240 SkRect contentRect = fWindow->getContentRect(); 244 SkRect contentRect = fWindow->getContentRect();
241 canvas->clipRect(contentRect); 245 canvas->clipRect(contentRect);
242 canvas->translate(contentRect.fLeft, contentRect.fTop); 246 canvas->translate(contentRect.fLeft, contentRect.fTop);
243 } 247 }
244 248
245 canvas->clear(SK_ColorWHITE); 249 canvas->clear(SK_ColorWHITE);
246 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) { 250 if (fWindow->supportsContentRect() && fWindow->scaleContentToFit()) {
247 const SkRect contentRect = fWindow->getContentRect(); 251 const SkRect contentRect = fWindow->getContentRect();
248 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); 252 const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions();
249 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize. height()); 253 const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize. height());
250 SkMatrix matrix; 254 SkMatrix matrix;
251 matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kCenter_ScaleTo Fit); 255 matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kCenter_ScaleTo Fit);
252 canvas->concat(matrix); 256 canvas->concat(matrix);
253 } 257 }
254 canvas->concat(fLocalMatrix); 258 canvas->concat(computeMatrix());
255 259
256 fSlides[fCurrentSlide]->draw(canvas); 260 fSlides[fCurrentSlide]->draw(canvas);
257 canvas->restoreToCount(count); 261 canvas->restoreToCount(count);
258 262
259 if (fDisplayStats) { 263 if (fDisplayStats) {
260 drawStats(canvas); 264 drawStats(canvas);
261 } 265 }
262 fCommands.drawHelp(canvas); 266 fCommands.drawHelp(canvas);
263 } 267 }
264 268
269 bool Viewer::onTouch(int owner, Window::InputState state, float x, float y) {
270 void* castedOwner = reinterpret_cast<void*>(owner);
271 switch (state) {
272 case Window::kUp_InputState: {
273 fGesture.touchEnd(castedOwner);
274 break;
275 }
276 case Window::kDown_InputState: {
277 fGesture.touchBegin(castedOwner, x, y);
278 break;
279 }
280 case Window::kMove_InputState: {
281 fGesture.touchMoved(castedOwner, x, y);
282 break;
283 }
284 }
285 fWindow->inval();
286 return true;
287 }
288
265 void Viewer::drawStats(SkCanvas* canvas) { 289 void Viewer::drawStats(SkCanvas* canvas) {
266 static const float kPixelPerMS = 2.0f; 290 static const float kPixelPerMS = 2.0f;
267 static const int kDisplayWidth = 130; 291 static const int kDisplayWidth = 130;
268 static const int kDisplayHeight = 100; 292 static const int kDisplayHeight = 100;
269 static const int kDisplayPadding = 10; 293 static const int kDisplayPadding = 10;
270 static const int kGraphPadding = 3; 294 static const int kGraphPadding = 3;
271 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps 295 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
272 296
273 SkISize canvasSize = canvas->getDeviceSize(); 297 SkISize canvasSize = canvas->getDeviceSize();
274 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth -kDisplayPadding), 298 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth -kDisplayPadding),
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 // Record measurements 338 // Record measurements
315 fMeasurements[fCurrentMeasurement++] = ms; 339 fMeasurements[fCurrentMeasurement++] = ms;
316 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod 340 fCurrentMeasurement &= (kMeasurementCount - 1); // fast mod
317 SkASSERT(fCurrentMeasurement < kMeasurementCount); 341 SkASSERT(fCurrentMeasurement < kMeasurementCount);
318 342
319 fAnimTimer.updateTime(); 343 fAnimTimer.updateTime();
320 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) { 344 if (fSlides[fCurrentSlide]->animate(fAnimTimer) || fDisplayStats) {
321 fWindow->inval(); 345 fWindow->inval();
322 } 346 }
323 } 347 }
OLDNEW
« no previous file with comments | « tools/viewer/Viewer.h ('k') | tools/viewer/sk_app/Window.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698