| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Go Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 package app | |
| 6 | |
| 7 /* | |
| 8 #cgo android LDFLAGS: -llog -landroid -lEGL -lGLESv2 | |
| 9 #include <android/log.h> | |
| 10 #include <android/native_activity.h> | |
| 11 #include <android/input.h> | |
| 12 #include <EGL/egl.h> | |
| 13 #include <GLES/gl.h> | |
| 14 | |
| 15 // TODO(crawshaw): Test configuration on more devices. | |
| 16 const EGLint RGB_888[] = { | |
| 17 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
| 18 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, | |
| 19 EGL_BLUE_SIZE, 8, | |
| 20 EGL_GREEN_SIZE, 8, | |
| 21 EGL_RED_SIZE, 8, | |
| 22 EGL_DEPTH_SIZE, 16, | |
| 23 EGL_CONFIG_CAVEAT, EGL_NONE, | |
| 24 EGL_NONE | |
| 25 }; | |
| 26 | |
| 27 EGLint windowWidth; | |
| 28 EGLint windowHeight; | |
| 29 EGLDisplay display; | |
| 30 EGLSurface surface; | |
| 31 | |
| 32 #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, "Go", __VA_ARGS__) | |
| 33 | |
| 34 void createEGLWindow(ANativeWindow* window) { | |
| 35 EGLint numConfigs, format; | |
| 36 EGLConfig config; | |
| 37 EGLContext context; | |
| 38 | |
| 39 display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
| 40 if (!eglInitialize(display, 0, 0)) { | |
| 41 LOG_ERROR("EGL initialize failed"); | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 if (!eglChooseConfig(display, RGB_888, &config, 1, &numConfigs)) { | |
| 46 LOG_ERROR("EGL choose RGB_888 config failed"); | |
| 47 return; | |
| 48 } | |
| 49 if (numConfigs <= 0) { | |
| 50 LOG_ERROR("EGL no config found"); | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); | |
| 55 if (ANativeWindow_setBuffersGeometry(window, 0, 0, format) != 0) { | |
| 56 LOG_ERROR("EGL set buffers geometry failed"); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 surface = eglCreateWindowSurface(display, config, window, NULL); | |
| 61 if (surface == EGL_NO_SURFACE) { | |
| 62 LOG_ERROR("EGL create surface failed"); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NON
E }; | |
| 67 context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttri
bs); | |
| 68 | |
| 69 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { | |
| 70 LOG_ERROR("eglMakeCurrent failed"); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 eglQuerySurface(display, surface, EGL_WIDTH, &windowWidth); | |
| 75 eglQuerySurface(display, surface, EGL_HEIGHT, &windowHeight); | |
| 76 } | |
| 77 | |
| 78 #undef LOG_ERROR | |
| 79 */ | |
| 80 import "C" | |
| 81 import ( | |
| 82 "log" | |
| 83 | |
| 84 "golang.org/x/mobile/event" | |
| 85 "golang.org/x/mobile/geom" | |
| 86 "golang.org/x/mobile/gl" | |
| 87 ) | |
| 88 | |
| 89 func windowDrawLoop(cb Callbacks, w *C.ANativeWindow, queue *C.AInputQueue) { | |
| 90 C.createEGLWindow(w) | |
| 91 | |
| 92 // TODO: is the library or the app responsible for clearing the buffers? | |
| 93 gl.ClearColor(0, 0, 0, 1) | |
| 94 gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) | |
| 95 C.eglSwapBuffers(C.display, C.surface) | |
| 96 | |
| 97 if errv := gl.GetError(); errv != gl.NO_ERROR { | |
| 98 log.Printf("GL initialization error: %s", errv) | |
| 99 } | |
| 100 | |
| 101 geom.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt) | |
| 102 geom.Height = geom.Pt(float32(C.windowHeight) / geom.PixelsPerPt) | |
| 103 | |
| 104 // Wait until geometry and GL is initialized before cb.Start. | |
| 105 runStart(cb) | |
| 106 | |
| 107 for { | |
| 108 processEvents(cb, queue) | |
| 109 select { | |
| 110 case <-windowDestroyed: | |
| 111 if cb.Stop != nil { | |
| 112 cb.Stop() | |
| 113 } | |
| 114 return | |
| 115 default: | |
| 116 if cb.Draw != nil { | |
| 117 cb.Draw() | |
| 118 } | |
| 119 C.eglSwapBuffers(C.display, C.surface) | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 func processEvents(cb Callbacks, queue *C.AInputQueue) { | |
| 125 var event *C.AInputEvent | |
| 126 for C.AInputQueue_getEvent(queue, &event) >= 0 { | |
| 127 if C.AInputQueue_preDispatchEvent(queue, event) != 0 { | |
| 128 continue | |
| 129 } | |
| 130 processEvent(cb, event) | |
| 131 C.AInputQueue_finishEvent(queue, event, 0) | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 func processEvent(cb Callbacks, e *C.AInputEvent) { | |
| 136 switch C.AInputEvent_getType(e) { | |
| 137 case C.AINPUT_EVENT_TYPE_KEY: | |
| 138 log.Printf("TODO input event: key") | |
| 139 case C.AINPUT_EVENT_TYPE_MOTION: | |
| 140 if cb.Touch == nil { | |
| 141 return | |
| 142 } | |
| 143 | |
| 144 // At most one of the events in this batch is an up or down even
t; get its index and type. | |
| 145 upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EV
ENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT | |
| 146 upDownTyp := event.TouchMove | |
| 147 switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK
{ | |
| 148 case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER
_DOWN: | |
| 149 upDownTyp = event.TouchStart | |
| 150 case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_U
P: | |
| 151 upDownTyp = event.TouchEnd | |
| 152 } | |
| 153 | |
| 154 for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i <
n; i++ { | |
| 155 typ := event.TouchMove | |
| 156 if i == upDownIndex { | |
| 157 typ = upDownTyp | |
| 158 } | |
| 159 x := C.AMotionEvent_getX(e, i) | |
| 160 y := C.AMotionEvent_getY(e, i) | |
| 161 cb.Touch(event.Touch{ | |
| 162 Type: typ, | |
| 163 Loc: geom.Point{ | |
| 164 X: geom.Pt(float32(x) / geom.PixelsPerPt
), | |
| 165 Y: geom.Pt(float32(y) / geom.PixelsPerPt
), | |
| 166 }, | |
| 167 }) | |
| 168 } | |
| 169 default: | |
| 170 log.Printf("unknown input event, type=%d", C.AInputEvent_getType
(e)) | |
| 171 } | |
| 172 } | |
| OLD | NEW |