| OLD | NEW |
| (Empty) |
| 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_android.h" | |
| 9 | |
| 10 #include "VulkanTestContext_android.h" | |
| 11 | |
| 12 namespace sk_app { | |
| 13 | |
| 14 Window* Window::CreateNativeWindow(void* platformData) { | |
| 15 Window_android* window = new Window_android(); | |
| 16 if (!window->init((android_app*)platformData)) { | |
| 17 delete window; | |
| 18 return nullptr; | |
| 19 } | |
| 20 return window; | |
| 21 } | |
| 22 | |
| 23 static void handle_cmd(struct android_app* app, int32_t cmd); | |
| 24 static int32_t handle_input(struct android_app* app, AInputEvent* event); | |
| 25 | |
| 26 bool Window_android::init(android_app* app) { | |
| 27 SkASSERT(app); | |
| 28 mApp = app; | |
| 29 mApp->userData = this; | |
| 30 mApp->onAppCmd = handle_cmd; | |
| 31 mApp->onInputEvent = handle_input; | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 void Window_android::setTitle(const char* title) { | |
| 36 //todo | |
| 37 SkDebugf("Title: %s", title); | |
| 38 } | |
| 39 | |
| 40 bool Window_android::attach(BackEndType attachType, int msaaSampleCount) { | |
| 41 if (kVulkan_BackendType != attachType) { | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 mSampleCount = msaaSampleCount; | |
| 46 | |
| 47 // We delay the creation of fTestContext until Android informs us that | |
| 48 // the native window is ready to use. | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 void Window_android::initDisplay(ANativeWindow* window) { | |
| 53 SkASSERT(window); | |
| 54 ContextPlatformData_android platformData; | |
| 55 platformData.fNativeWindow = window; | |
| 56 fTestContext = VulkanTestContext::Create((void*)&platformData, mSampleCount)
; | |
| 57 } | |
| 58 | |
| 59 static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) { | |
| 60 if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) { | |
| 61 SkDebugf("Failure writing android_app cmd: %s\n", strerror(errno)); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void Window_android::inval() { | |
| 66 android_app_write_cmd(mApp, APP_CMD_INVAL_WINDOW); | |
| 67 } | |
| 68 | |
| 69 void Window_android::paintIfNeeded() { | |
| 70 if (mApp->window || !mContentRect.isEmpty()) { | |
| 71 this->onPaint(); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Process the next main command. | |
| 77 */ | |
| 78 static void handle_cmd(struct android_app* app, int32_t cmd) { | |
| 79 Window_android* window = (Window_android*)app->userData; | |
| 80 switch (cmd) { | |
| 81 case APP_CMD_INIT_WINDOW: | |
| 82 // The window is being shown, get it ready. | |
| 83 SkASSERT(app->window); | |
| 84 window->initDisplay(app->window); | |
| 85 window->paintIfNeeded(); | |
| 86 break; | |
| 87 case APP_CMD_WINDOW_RESIZED: { | |
| 88 int width = ANativeWindow_getWidth(app->window); | |
| 89 int height = ANativeWindow_getHeight(app->window); | |
| 90 window->onResize(width, height); | |
| 91 break; | |
| 92 } | |
| 93 case APP_CMD_CONTENT_RECT_CHANGED: | |
| 94 window->setContentRect(app->contentRect.left, app->contentRect.top, | |
| 95 app->contentRect.right, app->contentRect.bott
om); | |
| 96 window->paintIfNeeded(); | |
| 97 break; | |
| 98 case APP_CMD_TERM_WINDOW: | |
| 99 // The window is being hidden or closed, clean it up. | |
| 100 window->detach(); | |
| 101 break; | |
| 102 case APP_CMD_INVAL_WINDOW: | |
| 103 window->paintIfNeeded(); | |
| 104 break; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 static Window::Key get_key(int32_t keycode) { | |
| 109 static const struct { | |
| 110 int32_t fAndroidKey; | |
| 111 Window::Key fWindowKey; | |
| 112 } gPair[] = { | |
| 113 { AKEYCODE_BACK, Window::kBack_Key }, | |
| 114 { AKEYCODE_VOLUME_UP, Window::kLeft_Key }, | |
| 115 { AKEYCODE_VOLUME_DOWN, Window::kRight_Key } | |
| 116 }; | |
| 117 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { | |
| 118 if (gPair[i].fAndroidKey == keycode) { | |
| 119 return gPair[i].fWindowKey; | |
| 120 } | |
| 121 } | |
| 122 return Window::kNONE_Key; | |
| 123 } | |
| 124 | |
| 125 static Window::InputState get_action(int32_t action) { | |
| 126 static const struct { | |
| 127 int32_t fAndroidAction; | |
| 128 Window::InputState fInputState; | |
| 129 } gPair[] = { | |
| 130 { AKEY_STATE_DOWN, Window::kDown_InputState }, | |
| 131 { AKEY_STATE_UP, Window::kUp_InputState }, | |
| 132 }; | |
| 133 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { | |
| 134 if (gPair[i].fAndroidAction == action) { | |
| 135 return gPair[i].fInputState; | |
| 136 } | |
| 137 } | |
| 138 return Window::kMove_InputState; | |
| 139 } | |
| 140 | |
| 141 static int32_t get_key_modifiers(AInputEvent* event) { | |
| 142 static const struct { | |
| 143 int32_t fAndroidState; | |
| 144 int32_t fWindowModifier; | |
| 145 } gPair[] = { | |
| 146 { AMETA_SHIFT_ON, Window::kShift_ModifierKey }, | |
| 147 { AMETA_CTRL_ON, Window::kControl_ModifierKey }, | |
| 148 }; | |
| 149 | |
| 150 int32_t metaState = AKeyEvent_getMetaState(event); | |
| 151 int32_t modifiers = 0; | |
| 152 | |
| 153 if (AKeyEvent_getRepeatCount(event) == 0) { | |
| 154 modifiers |= Window::kFirstPress_ModifierKey; | |
| 155 } | |
| 156 | |
| 157 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { | |
| 158 if (gPair[i].fAndroidState == metaState) { | |
| 159 modifiers |= gPair[i].fWindowModifier; | |
| 160 } | |
| 161 } | |
| 162 return modifiers; | |
| 163 } | |
| 164 | |
| 165 /** | |
| 166 * Process the next input event. | |
| 167 */ | |
| 168 static int32_t handle_input(struct android_app* app, AInputEvent* event) { | |
| 169 Window_android* window = (Window_android*)app->userData; | |
| 170 switch(AInputEvent_getType(event)) { | |
| 171 case AINPUT_EVENT_TYPE_MOTION: | |
| 172 break; | |
| 173 case AINPUT_EVENT_TYPE_KEY: | |
| 174 Window::Key key = get_key(AKeyEvent_getKeyCode(event)); | |
| 175 Window::InputState state = get_action(AKeyEvent_getAction(event)); | |
| 176 int32_t mod = get_key_modifiers(event); | |
| 177 window->onKey(key, state, mod); | |
| 178 return true; // eat all key events | |
| 179 } | |
| 180 return 0; | |
| 181 } | |
| 182 | |
| 183 } // namespace sk_app | |
| OLD | NEW |