jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Window.h" |
| 9 | |
| 10 | #include "SkSurface.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "VulkanTestContext.h" |
| 13 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 14 | static bool default_char_func(SkUnichar c, uint32_t modifiers, void* userData) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 15 | return false; |
| 16 | } |
| 17 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 18 | static bool default_key_func(Window::Key key, Window::InputState state, uint32_t modifiers, |
| 19 | void* userData) { |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | static bool default_mouse_func(int x, int y, Window::InputState state, uint32_t modifiers, |
| 24 | void* userData) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 25 | return false; |
| 26 | } |
| 27 | |
| 28 | static void default_paint_func(SkCanvas*, void* userData) {} |
| 29 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 30 | Window::Window() : fCharFunc(default_char_func) |
| 31 | , fKeyFunc(default_key_func) |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 32 | , fMouseFunc(default_mouse_func) |
| 33 | , fPaintFunc(default_paint_func) { |
| 34 | } |
| 35 | |
| 36 | void Window::detach() { |
| 37 | delete fTestContext; |
| 38 | fTestContext = nullptr; |
| 39 | } |
| 40 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 41 | bool Window::onChar(SkUnichar c, uint32_t modifiers) { |
| 42 | return fCharFunc(c, modifiers, fCharUserData); |
| 43 | } |
| 44 | |
| 45 | bool Window::onKey(Key key, InputState state, uint32_t modifiers) { |
| 46 | return fKeyFunc(key, state, modifiers, fKeyUserData); |
| 47 | } |
| 48 | |
| 49 | bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) { |
| 50 | return fMouseFunc(x, y, state, modifiers, fMouseUserData); |
| 51 | } |
| 52 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 53 | void Window::onPaint() { |
| 54 | SkSurface* backbuffer = fTestContext->getBackbufferSurface(); |
| 55 | if (backbuffer) { |
| 56 | // draw into the canvas of this surface |
| 57 | SkCanvas* canvas = backbuffer->getCanvas(); |
| 58 | |
| 59 | fPaintFunc(canvas, fPaintUserData); |
| 60 | |
| 61 | canvas->flush(); |
| 62 | |
| 63 | fTestContext->swapBuffers(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 64 | } else { |
| 65 | // try recreating testcontext |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 71 | void Window::onResize(uint32_t w, uint32_t h) { |
| 72 | fTestContext->resize(w, h); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 73 | } |